2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffsfs.c The interface functions for using YAFFS via a "direct" interface.
5 * Copyright (C) 2002 Aleph One Ltd.
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
16 #include "yaffs_guts.h"
18 #include <string.h> // for memset
21 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
24 #define NULL ((void *)0)
28 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.5 2003-03-11 05:16:53 charles Exp $";
30 // configurationList is the list of devices that are supported
31 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
35 // Directory search context
37 // NB this is an opaque structure.
39 struct yaffsfs_ObjectListEntry
42 struct yaffsfs_ObjectListEntry *next;
50 struct yaffsfs_ObjectListEntry *list;
51 char name[NAME_MAX+1];
53 } yaffsfs_DirectorySearchContext;
61 __u8 inUse:1; // this handle is in use
62 __u8 readOnly:1; // this handle is read only
63 __u8 append:1; // append only
64 __u8 exclusive:1; // exclusive
65 __u32 position; // current position in file
66 yaffs_Object *obj; // the object
70 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path, int symDepth);
74 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
77 /// Inilitalise handles on start-up.
79 static int yaffsfs_InitHandles(void)
82 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
84 yaffsfs_handle[i].inUse = 0;
85 yaffsfs_handle[i].obj = NULL;
90 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
92 if(h < 0 || h >= YAFFSFS_N_HANDLES)
97 return &yaffsfs_handle[h];
100 yaffs_Object *yaffsfs_GetHandleObject(int handle)
102 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
114 // Grab a handle (when opening a file)
117 static int yaffsfs_GetHandle(void)
122 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
124 h = yaffsfs_GetHandlePointer(i);
127 // todo bug: should never happen
131 memset(h,0,sizeof(yaffsfs_Handle));
140 // Let go of a handle (when closing a file)
142 static int yaffsfs_PutHandle(int handle)
144 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
156 // Stuff to search for a directory from a path
159 int yaffsfs_Match(char a, char b)
165 // yaffsfs_FindDevice
167 // Scan the configuration list to find the root.
168 static yaffs_Device *yaffsfs_FindDevice(const char *path, char **restOfPath)
170 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
171 const char *leftOver;
174 while(cfg && cfg->prefix && cfg->dev)
178 while(*p && *leftOver && yaffsfs_Match(*p,*leftOver))
186 *restOfPath = (char *)leftOver;
194 static yaffs_Object *yaffsfs_FindRoot(const char *path, char **restOfPath)
199 dev= yaffsfs_FindDevice(path,restOfPath);
200 if(dev && dev->isMounted)
207 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
210 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
212 char *alias = obj->variant.symLinkVariant.alias;
216 // Starts with a /, need to scan from root up
217 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
221 // Relative to here, so use the parent of the symlink as a start
222 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
229 // yaffsfs_FindDirectory
230 // Parse a path to determine the directory and the name within the directory.
232 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
233 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const char *path,char **name,int symDepth)
237 char str[YAFFS_MAX_NAME_LENGTH+1];
240 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
248 restOfPath = (char *)path;
252 dir = yaffsfs_FindRoot(path,&restOfPath);
258 // curve ball: also throw away surplus '/'
259 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
260 while(*restOfPath == '/')
262 restOfPath++; // get rid of '/'
268 while(*restOfPath && *restOfPath != '/')
270 if (i < YAFFS_MAX_NAME_LENGTH)
272 str[i] = *restOfPath;
281 // got to the end of the string
286 if(strcmp(str,".") == 0)
290 else if(strcmp(str,"..") == 0)
296 dir = yaffs_FindObjectByName(dir,str);
298 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
301 dir = yaffsfs_FollowLink(dir,symDepth);
305 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
312 // directory did not exist.
316 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const char *path,char **name,int symDepth)
318 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
321 // yaffsfs_FindObject turns a path for an existing object into the object
323 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const char *path,int symDepth)
328 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
332 return yaffs_FindObjectByName(dir,name);
340 int yaffs_open(const char *path, int oflag, int mode)
342 yaffs_Object *obj = NULL;
343 yaffs_Object *dir = NULL;
346 yaffsfs_Handle *h = NULL;
348 int alreadyExclusive = 0;
351 int errorReported = 0;
356 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
361 handle = yaffsfs_GetHandle();
366 h = yaffsfs_GetHandlePointer(handle);
369 // try to find the exisiting object
370 obj = yaffsfs_FindObject(NULL,path,0);
372 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
375 obj = yaffsfs_FollowLink(obj,symDepth++);
380 // Check if the object is already in use
381 alreadyOpen = alreadyExclusive = 0;
383 for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
387 yaffsfs_handle[i].inUse &&
388 obj == yaffsfs_handle[i].obj)
391 if(yaffsfs_handle[i].exclusive)
393 alreadyExclusive = 1;
398 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
403 // Open should fail if O_CREAT and O_EXCL are specified
404 if((oflag & O_EXCL) && (oflag & O_CREAT))
407 yaffsfs_SetError(-EEXIST);
411 // Check file permissions
412 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
413 !(obj->st_mode & S_IREAD))
418 if( (oflag & O_RDWR) &&
419 !(obj->st_mode & S_IREAD))
424 if( (oflag & (O_RDWR | O_WRONLY)) &&
425 !(obj->st_mode & S_IWRITE))
432 else if((oflag & O_CREAT))
434 // Let's see if we can create this file
435 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
438 obj = yaffs_MknodFile(dir,name,mode,0,0);
442 yaffsfs_SetError(-ENOTDIR);
446 if(obj && !openDenied)
450 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
451 h->append = (oflag & O_APPEND) ? 1 : 0;
452 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
456 if((oflag & O_TRUNC) && !h->readOnly)
459 yaffs_ResizeFile(obj,0);
465 yaffsfs_PutHandle(handle);
468 yaffsfs_SetError(-EACCESS);
481 int yaffs_close(int fd)
483 yaffsfs_Handle *h = NULL;
488 h = yaffsfs_GetHandlePointer(fd);
493 yaffs_FlushFile(h->obj,1);
495 if(h->obj->inUse <= 0 && h->obj->unlinked)
497 yaffs_DeleteFile(h->obj);
499 yaffsfs_PutHandle(fd);
505 yaffsfs_SetError(-EBADF);
514 int yaffs_read(int fd, void *buf, unsigned int nbyte)
516 yaffsfs_Handle *h = NULL;
517 yaffs_Object *obj = NULL;
523 h = yaffsfs_GetHandlePointer(fd);
524 obj = yaffsfs_GetHandleObject(fd);
529 yaffsfs_SetError(-EBADF);
534 if(yaffs_GetObjectFileLength(obj) > pos)
536 maxRead = yaffs_GetObjectFileLength(obj) - pos;
551 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
554 h->position = pos + nRead;
571 return (nRead >= 0) ? nRead : -1;
575 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
577 yaffsfs_Handle *h = NULL;
578 yaffs_Object *obj = NULL;
583 h = yaffsfs_GetHandlePointer(fd);
584 obj = yaffsfs_GetHandleObject(fd);
589 yaffsfs_SetError(-EBADF);
591 else if( h && obj && h->readOnly)
599 pos = yaffs_GetObjectFileLength(obj);
606 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
610 h->position = pos + nWritten;
622 return (nWritten >= 0) ? nWritten : -1;
626 off_t yaffs_lseek(int fd, off_t offset, int whence)
628 yaffsfs_Handle *h = NULL;
629 yaffs_Object *obj = NULL;
634 h = yaffsfs_GetHandlePointer(fd);
635 obj = yaffsfs_GetHandleObject(fd);
640 yaffsfs_SetError(-EBADF);
642 else if(whence == SEEK_SET)
649 else if(whence == SEEK_CUR)
651 if( (h->position + offset) >= 0)
653 pos = (h->position + offset);
656 else if(whence == SEEK_END)
658 fSize = yaffs_GetObjectFileLength(obj);
659 if(fSize >= 0 && (fSize + offset) >= 0)
661 pos = fSize + offset;
681 int yaffsfs_DoUnlink(const char *path,int isDirectory)
683 yaffs_Object *dir = NULL;
684 yaffs_Object *obj = NULL;
686 int result = YAFFS_FAIL;
690 obj = yaffsfs_FindObject(NULL,path,0);
691 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
694 yaffsfs_SetError(-ENOTDIR);
698 yaffsfs_SetError(-ENOENT);
700 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
702 yaffsfs_SetError(-EISDIR);
704 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
706 yaffsfs_SetError(-ENOTDIR);
708 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
710 yaffsfs_SetError(-ENOTDIR);
714 result = yaffs_Unlink(dir,name);
716 if(result == YAFFS_FAIL && isDirectory)
718 yaffsfs_SetError(-ENOTEMPTY);
726 return (result == YAFFS_FAIL) ? -1 : 0;
728 int yaffs_rmdir(const char *path)
730 return yaffsfs_DoUnlink(path,1);
733 int yaffs_unlink(const char *path)
735 return yaffsfs_DoUnlink(path,0);
738 int yaffs_rename(const char *oldPath, const char *newPath)
740 yaffs_Object *olddir = NULL;
741 yaffs_Object *newdir = NULL;
744 int result= YAFFS_FAIL;
748 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
749 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
751 if(!olddir || !newdir)
754 yaffsfs_SetError(-EBADF);
756 else if(olddir->myDev != newdir->myDev)
758 // oops must be on same device
760 yaffsfs_SetError(-EXDEV);
764 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
769 return (result == YAFFS_FAIL) ? -1 : 0;
773 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
779 obj = yaffs_GetEquivalentObject(obj);
784 buf->st_dev = (int)obj->myDev->genericDevice;
785 buf->st_ino = obj->objectId;
786 buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
788 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
790 buf->st_mode |= S_IFDIR;
792 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
794 buf->st_mode |= S_IFLNK;
796 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
798 buf->st_mode |= S_IFREG;
801 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
804 buf->st_rdev = obj->st_rdev;
805 buf->st_size = yaffs_GetObjectFileLength(obj);
806 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
807 buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
808 buf->st_atime = obj->st_atime;
809 buf->st_ctime = obj->st_ctime;
810 buf->st_mtime = obj->st_mtime;
816 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
823 obj = yaffsfs_FindObject(NULL,path,0);
827 obj = yaffsfs_FollowLink(obj,0);
832 retVal = yaffsfs_DoStat(obj,buf);
836 // todo error not found
837 yaffsfs_SetError(-ENOENT);
846 int yaffs_stat(const char *path, struct yaffs_stat *buf)
848 return yaffsfs_DoStatOrLStat(path,buf,0);
851 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
853 return yaffsfs_DoStatOrLStat(path,buf,1);
856 int yaffs_fstat(int fd, struct yaffs_stat *buf)
863 obj = yaffsfs_GetHandleObject(fd);
867 retVal = yaffsfs_DoStat(obj,buf);
872 yaffsfs_SetError(-EBADF);
880 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
886 obj = yaffs_GetEquivalentObject(obj);
893 result = yaffs_FlushFile(obj,0);
896 return result == YAFFS_OK ? 0 : -1;
900 int yaffs_chmod(const char *path, mode_t mode)
907 obj = yaffsfs_FindObject(NULL,path,0);
911 retVal = yaffsfs_DoChMod(obj,mode);
915 // todo error not found
916 yaffsfs_SetError(-ENOENT);
926 int yaffs_fchmod(int fd, mode_t mode)
933 obj = yaffsfs_GetHandleObject(fd);
937 retVal = yaffsfs_DoChMod(obj,mode);
942 yaffsfs_SetError(-EBADF);
951 int yaffs_mkdir(const char *path, mode_t mode)
953 yaffs_Object *parent = NULL;
959 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
960 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
967 yaffsfs_SetError(-ENOSPC); // just assume no space for now
976 int yaffs_mount(const char *path)
979 int result=YAFFS_FAIL;
980 yaffs_Device *dev=NULL;
984 dev = yaffsfs_FindDevice(path,&dummy);
989 result = yaffs_GutsInitialise(dev);
990 if(result == YAFFS_FAIL)
992 // todo error - mount failed
993 yaffsfs_SetError(-ENOMEM);
995 retVal = result ? 0 : -1;
1000 //todo error - already mounted.
1001 yaffsfs_SetError(-EBUSY);
1006 // todo error - no device
1007 yaffsfs_SetError(-ENODEV);
1014 int yaffs_unmount(const char *path)
1017 yaffs_Device *dev=NULL;
1021 dev = yaffsfs_FindDevice(path,&dummy);
1028 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1030 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1032 inUse = 1; // the device is in use, can't unmount
1038 yaffs_Deinitialise(dev);
1044 // todo error can't unmount as files are open
1045 yaffsfs_SetError(-EBUSY);
1051 //todo error - not mounted.
1052 yaffsfs_SetError(-EINVAL);
1058 // todo error - no device
1059 yaffsfs_SetError(-ENODEV);
1066 off_t yaffs_freespace(const char *path)
1069 yaffs_Device *dev=NULL;
1073 dev = yaffsfs_FindDevice(path,&dummy);
1076 retVal = yaffs_GetNumberOfFreeChunks(dev);
1077 retVal *= YAFFS_BYTES_PER_CHUNK;
1082 yaffsfs_SetError(-EINVAL);
1089 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1092 yaffsfs_DeviceConfiguration *cfg;
1094 yaffsfs_configurationList = cfgList;
1096 yaffsfs_InitHandles();
1098 cfg = yaffsfs_configurationList;
1100 while(cfg && cfg->prefix && cfg->dev)
1102 cfg->dev->isMounted = 0;
1111 // Directory search stuff.
1113 yaffs_DIR *yaffs_opendir(const char *dirname)
1115 yaffs_DIR *dir = NULL;
1116 yaffs_Object *obj = NULL;
1117 yaffsfs_DirectorySearchContext *dsc = NULL;
1121 obj = yaffsfs_FindObject(NULL,dirname,0);
1123 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1126 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1127 dir = (yaffs_DIR *)dsc;
1130 dsc->magic = YAFFS_MAGIC;
1132 memset(dsc->name,0,NAME_MAX+1);
1133 strncpy(dsc->name,dirname,NAME_MAX);
1143 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1145 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1146 struct yaffs_dirent *retVal = NULL;
1147 struct list_head *i;
1148 yaffs_Object *entry = NULL;
1150 yaffs_Object *obj = NULL;
1151 struct yaffsfs_ObjectListEntry *list = NULL;
1158 if(dsc && dsc->magic == YAFFS_MAGIC)
1160 yaffsfs_SetError(0);
1162 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1164 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1167 list_for_each(i,&obj->variant.directoryVariant.children)
1170 entry = (i) ? list_entry(i, yaffs_Object,siblings) : NULL;
1176 while(list && !inList)
1178 if(list->objectId == entry->objectId)
1185 if(!inList) goto foundNew;
1192 if(!inList && entry)
1194 //This is the entry we're going to return;
1195 struct yaffsfs_ObjectListEntry *le;
1197 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1201 le->next = dsc->list;
1202 le->objectId = entry->objectId;
1205 dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1206 dsc->de.d_off = offset;
1207 yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1208 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1219 yaffsfs_SetError(-EBADF);
1228 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc)
1231 struct yaffsfs_ObjectListEntry *le;
1233 if(dsc && dsc->magic == YAFFS_MAGIC)
1238 dsc->list = dsc->list->next;
1245 void yaffs_rewinddir(yaffs_DIR *dirp)
1247 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1250 yaffsfs_ListClear(dsc);
1255 int yaffs_closedir(yaffs_DIR *dirp)
1257 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1260 yaffsfs_ListClear(dsc);
1269 int yaffs_symlink(const char *oldpath, const char *newpath)
1271 yaffs_Object *parent = NULL;
1275 int mode = 0; // ignore for now
1278 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1279 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1286 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1296 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1298 yaffs_Object *obj = NULL;
1304 obj = yaffsfs_FindObject(NULL,path,0);
1308 yaffsfs_SetError(-ENOENT);
1311 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1313 yaffsfs_SetError(-EINVAL);
1318 char *alias = obj->variant.symLinkVariant.alias;
1319 memset(buf,0,bufsiz);
1320 strncpy(buf,alias,bufsiz - 1);
1327 int yaffs_link(const char *oldpath, const char *newpath);
1328 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1330 int yaffs_DumpDevStruct(const char *path)
1334 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1338 yaffs_Device *dev = obj->myDev;
1341 "nPageWrites.......... %d\n"
1342 "nPageReads........... %d\n"
1343 "nBlockErasures....... %d\n"
1344 "nGCCopies............ %d\n"
1345 "garbageCollections... %d\n"
1346 "passiveGarbageColl'ns %d\n"
1350 dev->nBlockErasures,
1352 dev->garbageCollections,
1353 dev->passiveGarbageCollections