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.1 2003-01-21 03:32:17 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;
355 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
360 handle = yaffsfs_GetHandle();
365 h = yaffsfs_GetHandlePointer(handle);
368 // try to find the exisiting object
369 obj = yaffsfs_FindObject(NULL,path,0);
371 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
374 obj = yaffsfs_FollowLink(obj,symDepth++);
379 // Check if the object is already in use
380 alreadyOpen = alreadyExclusive = 0;
382 for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
386 yaffsfs_handle[i].inUse &&
387 obj == yaffsfs_handle[i].obj)
390 if(yaffsfs_handle[i].exclusive)
392 alreadyExclusive = 1;
397 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
402 // Check file permissions
403 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
404 !(obj->st_mode & S_IREAD))
409 if( (oflag & O_RDWR) &&
410 !(obj->st_mode & S_IREAD))
415 if( (oflag & (O_RDWR | O_WRONLY)) &&
416 !(obj->st_mode & S_IWRITE))
423 else if((oflag & O_CREAT))
425 // Let's see if we can create this file
426 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
427 obj = yaffs_MknodFile(dir,name,mode,0,0);
430 if(obj && !openDenied)
434 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
435 h->append = (oflag & O_APPEND) ? 1 : 0;
436 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
440 if((oflag & O_TRUNC) && !h->readOnly)
443 yaffs_ResizeFile(obj,0);
449 yaffsfs_PutHandle(handle);
450 yaffsfs_SetError(-EACCESS);
461 int yaffs_close(int fd)
463 yaffsfs_Handle *h = NULL;
468 h = yaffsfs_GetHandlePointer(fd);
473 yaffs_FlushFile(h->obj,1);
475 if(h->obj->inUse <= 0 && h->obj->unlinked)
477 yaffs_DeleteFile(h->obj);
479 yaffsfs_PutHandle(fd);
485 yaffsfs_SetError(-EBADF);
494 int yaffs_read(int fd, void *buf, unsigned int nbyte)
496 yaffsfs_Handle *h = NULL;
497 yaffs_Object *obj = NULL;
503 h = yaffsfs_GetHandlePointer(fd);
504 obj = yaffsfs_GetHandleObject(fd);
509 yaffsfs_SetError(-EBADF);
514 if(yaffs_GetObjectFileLength(obj) > pos)
516 maxRead = yaffs_GetObjectFileLength(obj) - pos;
531 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
534 h->position = pos + nRead;
551 return (nRead >= 0) ? nRead : -1;
555 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
557 yaffsfs_Handle *h = NULL;
558 yaffs_Object *obj = NULL;
563 h = yaffsfs_GetHandlePointer(fd);
564 obj = yaffsfs_GetHandleObject(fd);
569 yaffsfs_SetError(-EBADF);
571 else if( h && obj && h->readOnly)
579 pos = yaffs_GetObjectFileLength(obj);
586 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
590 h->position = pos + nWritten;
602 return (nWritten >= 0) ? nWritten : -1;
606 off_t yaffs_lseek(int fd, off_t offset, int whence)
608 yaffsfs_Handle *h = NULL;
609 yaffs_Object *obj = NULL;
614 h = yaffsfs_GetHandlePointer(fd);
615 obj = yaffsfs_GetHandleObject(fd);
620 yaffsfs_SetError(-EBADF);
622 else if(whence == SEEK_SET)
629 else if(whence == SEEK_CUR)
631 if( (h->position + offset) >= 0)
633 pos = (h->position + offset);
636 else if(whence == SEEK_END)
638 fSize = yaffs_GetObjectFileLength(obj);
639 if(fSize >= 0 && (fSize + offset) >= 0)
641 pos = fSize + offset;
661 int yaffsfs_DoUnlink(const char *path,int isDirectory)
663 yaffs_Object *dir = NULL;
664 yaffs_Object *obj = NULL;
666 int result = YAFFS_FAIL;
670 obj = yaffsfs_FindObject(NULL,path,0);
671 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
674 yaffsfs_SetError(-ENOTDIR);
678 yaffsfs_SetError(-ENOENT);
680 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
682 yaffsfs_SetError(-EISDIR);
684 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
686 yaffsfs_SetError(-ENOTDIR);
688 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
690 yaffsfs_SetError(-ENOTDIR);
694 result = yaffs_Unlink(dir,name);
696 if(result == YAFFS_FAIL && isDirectory)
698 yaffsfs_SetError(-ENOTEMPTY);
706 return (result == YAFFS_FAIL) ? -1 : 0;
708 int yaffs_rmdir(const char *path)
710 return yaffsfs_DoUnlink(path,1);
713 int yaffs_unlink(const char *path)
715 return yaffsfs_DoUnlink(path,0);
718 int yaffs_rename(const char *oldPath, const char *newPath)
720 yaffs_Object *olddir = NULL;
721 yaffs_Object *newdir = NULL;
724 int result= YAFFS_FAIL;
728 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
729 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
731 if(!olddir || !newdir)
734 yaffsfs_SetError(-EBADF);
736 else if(olddir->myDev != newdir->myDev)
738 // oops must be on same device
740 yaffsfs_SetError(-EXDEV);
744 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
749 return (result == YAFFS_FAIL) ? -1 : 0;
753 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
759 obj = yaffs_GetEquivalentObject(obj);
764 buf->st_dev = (int)obj->myDev->genericDevice;
765 buf->st_ino = obj->objectId;
766 buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
768 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
770 buf->st_mode |= S_IFDIR;
772 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
774 buf->st_mode |= S_IFLNK;
776 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
778 buf->st_mode |= S_IFREG;
781 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
784 buf->st_rdev = obj->st_rdev;
785 buf->st_size = yaffs_GetObjectFileLength(obj);
786 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
787 buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
788 buf->st_atime = obj->st_atime;
789 buf->st_ctime = obj->st_ctime;
790 buf->st_mtime = obj->st_mtime;
796 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
803 obj = yaffsfs_FindObject(NULL,path,0);
807 obj = yaffsfs_FollowLink(obj,0);
812 retVal = yaffsfs_DoStat(obj,buf);
816 // todo error not found
817 yaffsfs_SetError(-ENOENT);
826 int yaffs_stat(const char *path, struct yaffs_stat *buf)
828 return yaffsfs_DoStatOrLStat(path,buf,0);
831 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
833 return yaffsfs_DoStatOrLStat(path,buf,1);
836 int yaffs_fstat(int fd, struct yaffs_stat *buf)
843 obj = yaffsfs_GetHandleObject(fd);
847 retVal = yaffsfs_DoStat(obj,buf);
852 yaffsfs_SetError(-EBADF);
860 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
866 obj = yaffs_GetEquivalentObject(obj);
873 result = yaffs_FlushFile(obj,0);
876 return result == YAFFS_OK ? 0 : -1;
880 int yaffs_chmod(const char *path, mode_t mode)
887 obj = yaffsfs_FindObject(NULL,path,0);
891 retVal = yaffsfs_DoChMod(obj,mode);
895 // todo error not found
896 yaffsfs_SetError(-ENOENT);
906 int yaffs_fchmod(int fd, mode_t mode)
913 obj = yaffsfs_GetHandleObject(fd);
917 retVal = yaffsfs_DoChMod(obj,mode);
922 yaffsfs_SetError(-EBADF);
931 int yaffs_mkdir(const char *path, mode_t mode)
933 yaffs_Object *parent = NULL;
939 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
940 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
947 yaffsfs_SetError(-ENOSPC); // just assume no space for now
956 int yaffs_mount(const char *path)
959 int result=YAFFS_FAIL;
960 yaffs_Device *dev=NULL;
964 dev = yaffsfs_FindDevice(path,&dummy);
969 result = yaffs_GutsInitialise(dev);
970 if(result == YAFFS_FAIL)
972 // todo error - mount failed
973 yaffsfs_SetError(-ENOMEM);
975 retVal = result ? 0 : -1;
980 //todo error - already mounted.
981 yaffsfs_SetError(-EBUSY);
986 // todo error - no device
987 yaffsfs_SetError(-ENODEV);
994 int yaffs_unmount(const char *path)
997 yaffs_Device *dev=NULL;
1001 dev = yaffsfs_FindDevice(path,&dummy);
1008 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1010 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1012 inUse = 1; // the device is in use, can't unmount
1018 yaffs_Deinitialise(dev);
1024 // todo error can't unmount as files are open
1025 yaffsfs_SetError(-EBUSY);
1031 //todo error - not mounted.
1032 yaffsfs_SetError(-EINVAL);
1038 // todo error - no device
1039 yaffsfs_SetError(-ENODEV);
1046 off_t yaffs_freespace(const char *path)
1049 yaffs_Device *dev=NULL;
1053 dev = yaffsfs_FindDevice(path,&dummy);
1056 retVal = yaffs_GetNumberOfFreeChunks(dev);
1057 retVal *= YAFFS_BYTES_PER_CHUNK;
1062 yaffsfs_SetError(-EINVAL);
1069 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1072 yaffsfs_DeviceConfiguration *cfg;
1074 yaffsfs_configurationList = cfgList;
1076 yaffsfs_InitHandles();
1078 cfg = yaffsfs_configurationList;
1080 while(cfg && cfg->prefix && cfg->dev)
1082 cfg->dev->isMounted = 0;
1091 // Directory search stuff.
1093 yaffs_DIR *yaffs_opendir(const char *dirname)
1095 yaffs_DIR *dir = NULL;
1096 yaffs_Object *obj = NULL;
1097 yaffsfs_DirectorySearchContext *dsc = NULL;
1101 obj = yaffsfs_FindObject(NULL,dirname,0);
1103 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1106 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1107 dir = (yaffs_DIR *)dsc;
1110 dsc->magic = YAFFS_MAGIC;
1112 memset(dsc->name,0,NAME_MAX+1);
1113 strncpy(dsc->name,dirname,NAME_MAX);
1123 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1125 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1126 struct yaffs_dirent *retVal = NULL;
1127 struct list_head *i;
1128 yaffs_Object *entry = NULL;
1130 yaffs_Object *obj = NULL;
1131 struct yaffsfs_ObjectListEntry *list = NULL;
1138 if(dsc && dsc->magic == YAFFS_MAGIC)
1140 yaffsfs_SetError(0);
1142 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1144 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1147 list_for_each(i,&obj->variant.directoryVariant.children)
1150 entry = (i) ? list_entry(i, yaffs_Object,siblings) : NULL;
1156 while(list && !inList)
1158 if(list->objectId == entry->objectId)
1165 if(!inList) goto foundNew;
1172 if(!inList && entry)
1174 //This is the entry we're going to return;
1175 struct yaffsfs_ObjectListEntry *le;
1177 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1181 le->next = dsc->list;
1182 le->objectId = entry->objectId;
1185 dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1186 dsc->de.d_off = offset;
1187 yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1188 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1199 yaffsfs_SetError(-EBADF);
1208 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc)
1211 struct yaffsfs_ObjectListEntry *le;
1213 if(dsc && dsc->magic == YAFFS_MAGIC)
1218 dsc->list = dsc->list->next;
1225 void yaffs_rewinddir(yaffs_DIR *dirp)
1227 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1230 yaffsfs_ListClear(dsc);
1235 int yaffs_closedir(yaffs_DIR *dirp)
1237 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1240 yaffsfs_ListClear(dsc);
1249 int yaffs_symlink(const char *oldpath, const char *newpath)
1251 yaffs_Object *parent = NULL;
1255 int mode = 0; // ignore for now
1258 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1259 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1266 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1276 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1278 yaffs_Object *obj = NULL;
1284 obj = yaffsfs_FindObject(NULL,path,0);
1288 yaffsfs_SetError(-ENOENT);
1291 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1293 yaffsfs_SetError(-EINVAL);
1298 char *alias = obj->variant.symLinkVariant.alias;
1299 memset(buf,0,bufsiz);
1300 strncpy(buf,alias,bufsiz - 1);
1307 int yaffs_link(const char *oldpath, const char *newpath);
1308 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1310 int yaffs_DumpDevStruct(const char *path)
1314 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1318 yaffs_Device *dev = obj->myDev;
1321 "nPageWrites.......... %d\n"
1322 "nPageReads........... %d\n"
1323 "nBlockErasures....... %d\n"
1324 "nGCCopies............ %d\n"
1325 "garbageCollections... %d\n"
1326 "passiveGarbageColl'ns %d\n"
1330 dev->nBlockErasures,
1332 dev->garbageCollections,
1333 dev->passiveGarbageCollections