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.6 2003-03-11 09:54:40 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;
742 yaffs_Object *obj = NULL;
745 int result= YAFFS_FAIL;
746 int renameAllowed = 1;
750 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
751 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
752 obj = yaffsfs_FindObject(NULL,oldPath,0);
754 if(!olddir || !newdir || !obj)
757 yaffsfs_SetError(-EBADF);
760 else if(olddir->myDev != newdir->myDev)
762 // oops must be on same device
764 yaffsfs_SetError(-EXDEV);
767 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
769 // It is a directory, check that it is not being renamed to
770 // being its own decendent.
771 // Do this by tracing from the new directory back to the root, checking for obj
773 yaffs_Object *xx = newdir;
775 while( renameAllowed && xx)
783 if(!renameAllowed) yaffsfs_SetError(-EACCESS);
788 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
793 return (result == YAFFS_FAIL) ? -1 : 0;
797 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
803 obj = yaffs_GetEquivalentObject(obj);
808 buf->st_dev = (int)obj->myDev->genericDevice;
809 buf->st_ino = obj->objectId;
810 buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
812 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
814 buf->st_mode |= S_IFDIR;
816 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
818 buf->st_mode |= S_IFLNK;
820 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
822 buf->st_mode |= S_IFREG;
825 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
828 buf->st_rdev = obj->st_rdev;
829 buf->st_size = yaffs_GetObjectFileLength(obj);
830 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
831 buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
832 buf->st_atime = obj->st_atime;
833 buf->st_ctime = obj->st_ctime;
834 buf->st_mtime = obj->st_mtime;
840 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
847 obj = yaffsfs_FindObject(NULL,path,0);
851 obj = yaffsfs_FollowLink(obj,0);
856 retVal = yaffsfs_DoStat(obj,buf);
860 // todo error not found
861 yaffsfs_SetError(-ENOENT);
870 int yaffs_stat(const char *path, struct yaffs_stat *buf)
872 return yaffsfs_DoStatOrLStat(path,buf,0);
875 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
877 return yaffsfs_DoStatOrLStat(path,buf,1);
880 int yaffs_fstat(int fd, struct yaffs_stat *buf)
887 obj = yaffsfs_GetHandleObject(fd);
891 retVal = yaffsfs_DoStat(obj,buf);
896 yaffsfs_SetError(-EBADF);
904 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
910 obj = yaffs_GetEquivalentObject(obj);
917 result = yaffs_FlushFile(obj,0);
920 return result == YAFFS_OK ? 0 : -1;
924 int yaffs_chmod(const char *path, mode_t mode)
931 obj = yaffsfs_FindObject(NULL,path,0);
935 retVal = yaffsfs_DoChMod(obj,mode);
939 // todo error not found
940 yaffsfs_SetError(-ENOENT);
950 int yaffs_fchmod(int fd, mode_t mode)
957 obj = yaffsfs_GetHandleObject(fd);
961 retVal = yaffsfs_DoChMod(obj,mode);
966 yaffsfs_SetError(-EBADF);
975 int yaffs_mkdir(const char *path, mode_t mode)
977 yaffs_Object *parent = NULL;
983 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
984 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
991 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1000 int yaffs_mount(const char *path)
1003 int result=YAFFS_FAIL;
1004 yaffs_Device *dev=NULL;
1008 dev = yaffsfs_FindDevice(path,&dummy);
1013 result = yaffs_GutsInitialise(dev);
1014 if(result == YAFFS_FAIL)
1016 // todo error - mount failed
1017 yaffsfs_SetError(-ENOMEM);
1019 retVal = result ? 0 : -1;
1024 //todo error - already mounted.
1025 yaffsfs_SetError(-EBUSY);
1030 // todo error - no device
1031 yaffsfs_SetError(-ENODEV);
1038 int yaffs_unmount(const char *path)
1041 yaffs_Device *dev=NULL;
1045 dev = yaffsfs_FindDevice(path,&dummy);
1052 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1054 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1056 inUse = 1; // the device is in use, can't unmount
1062 yaffs_Deinitialise(dev);
1068 // todo error can't unmount as files are open
1069 yaffsfs_SetError(-EBUSY);
1075 //todo error - not mounted.
1076 yaffsfs_SetError(-EINVAL);
1082 // todo error - no device
1083 yaffsfs_SetError(-ENODEV);
1090 off_t yaffs_freespace(const char *path)
1093 yaffs_Device *dev=NULL;
1097 dev = yaffsfs_FindDevice(path,&dummy);
1100 retVal = yaffs_GetNumberOfFreeChunks(dev);
1101 retVal *= YAFFS_BYTES_PER_CHUNK;
1106 yaffsfs_SetError(-EINVAL);
1113 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1116 yaffsfs_DeviceConfiguration *cfg;
1118 yaffsfs_configurationList = cfgList;
1120 yaffsfs_InitHandles();
1122 cfg = yaffsfs_configurationList;
1124 while(cfg && cfg->prefix && cfg->dev)
1126 cfg->dev->isMounted = 0;
1135 // Directory search stuff.
1137 yaffs_DIR *yaffs_opendir(const char *dirname)
1139 yaffs_DIR *dir = NULL;
1140 yaffs_Object *obj = NULL;
1141 yaffsfs_DirectorySearchContext *dsc = NULL;
1145 obj = yaffsfs_FindObject(NULL,dirname,0);
1147 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1150 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1151 dir = (yaffs_DIR *)dsc;
1154 dsc->magic = YAFFS_MAGIC;
1156 memset(dsc->name,0,NAME_MAX+1);
1157 strncpy(dsc->name,dirname,NAME_MAX);
1167 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1169 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1170 struct yaffs_dirent *retVal = NULL;
1171 struct list_head *i;
1172 yaffs_Object *entry = NULL;
1174 yaffs_Object *obj = NULL;
1175 struct yaffsfs_ObjectListEntry *list = NULL;
1182 if(dsc && dsc->magic == YAFFS_MAGIC)
1184 yaffsfs_SetError(0);
1186 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1188 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1191 list_for_each(i,&obj->variant.directoryVariant.children)
1194 entry = (i) ? list_entry(i, yaffs_Object,siblings) : NULL;
1200 while(list && !inList)
1202 if(list->objectId == entry->objectId)
1209 if(!inList) goto foundNew;
1216 if(!inList && entry)
1218 //This is the entry we're going to return;
1219 struct yaffsfs_ObjectListEntry *le;
1221 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1225 le->next = dsc->list;
1226 le->objectId = entry->objectId;
1229 dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1230 dsc->de.d_off = offset;
1231 yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1232 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1243 yaffsfs_SetError(-EBADF);
1252 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc)
1255 struct yaffsfs_ObjectListEntry *le;
1257 if(dsc && dsc->magic == YAFFS_MAGIC)
1262 dsc->list = dsc->list->next;
1269 void yaffs_rewinddir(yaffs_DIR *dirp)
1271 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1274 yaffsfs_ListClear(dsc);
1279 int yaffs_closedir(yaffs_DIR *dirp)
1281 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1284 yaffsfs_ListClear(dsc);
1293 int yaffs_symlink(const char *oldpath, const char *newpath)
1295 yaffs_Object *parent = NULL;
1299 int mode = 0; // ignore for now
1302 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1303 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1310 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1320 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1322 yaffs_Object *obj = NULL;
1328 obj = yaffsfs_FindObject(NULL,path,0);
1332 yaffsfs_SetError(-ENOENT);
1335 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1337 yaffsfs_SetError(-EINVAL);
1342 char *alias = obj->variant.symLinkVariant.alias;
1343 memset(buf,0,bufsiz);
1344 strncpy(buf,alias,bufsiz - 1);
1351 int yaffs_link(const char *oldpath, const char *newpath);
1352 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1354 int yaffs_DumpDevStruct(const char *path)
1358 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1362 yaffs_Device *dev = obj->myDev;
1365 "nPageWrites.......... %d\n"
1366 "nPageReads........... %d\n"
1367 "nBlockErasures....... %d\n"
1368 "nGCCopies............ %d\n"
1369 "garbageCollections... %d\n"
1370 "passiveGarbageColl'ns %d\n"
1374 dev->nBlockErasures,
1376 dev->garbageCollections,
1377 dev->passiveGarbageCollections