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.4 2003-02-24 23:49:29 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);
445 if(obj && !openDenied)
449 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
450 h->append = (oflag & O_APPEND) ? 1 : 0;
451 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
455 if((oflag & O_TRUNC) && !h->readOnly)
458 yaffs_ResizeFile(obj,0);
464 yaffsfs_PutHandle(handle);
467 yaffsfs_SetError(-EACCESS);
480 int yaffs_close(int fd)
482 yaffsfs_Handle *h = NULL;
487 h = yaffsfs_GetHandlePointer(fd);
492 yaffs_FlushFile(h->obj,1);
494 if(h->obj->inUse <= 0 && h->obj->unlinked)
496 yaffs_DeleteFile(h->obj);
498 yaffsfs_PutHandle(fd);
504 yaffsfs_SetError(-EBADF);
513 int yaffs_read(int fd, void *buf, unsigned int nbyte)
515 yaffsfs_Handle *h = NULL;
516 yaffs_Object *obj = NULL;
522 h = yaffsfs_GetHandlePointer(fd);
523 obj = yaffsfs_GetHandleObject(fd);
528 yaffsfs_SetError(-EBADF);
533 if(yaffs_GetObjectFileLength(obj) > pos)
535 maxRead = yaffs_GetObjectFileLength(obj) - pos;
550 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
553 h->position = pos + nRead;
570 return (nRead >= 0) ? nRead : -1;
574 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
576 yaffsfs_Handle *h = NULL;
577 yaffs_Object *obj = NULL;
582 h = yaffsfs_GetHandlePointer(fd);
583 obj = yaffsfs_GetHandleObject(fd);
588 yaffsfs_SetError(-EBADF);
590 else if( h && obj && h->readOnly)
598 pos = yaffs_GetObjectFileLength(obj);
605 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
609 h->position = pos + nWritten;
621 return (nWritten >= 0) ? nWritten : -1;
625 off_t yaffs_lseek(int fd, off_t offset, int whence)
627 yaffsfs_Handle *h = NULL;
628 yaffs_Object *obj = NULL;
633 h = yaffsfs_GetHandlePointer(fd);
634 obj = yaffsfs_GetHandleObject(fd);
639 yaffsfs_SetError(-EBADF);
641 else if(whence == SEEK_SET)
648 else if(whence == SEEK_CUR)
650 if( (h->position + offset) >= 0)
652 pos = (h->position + offset);
655 else if(whence == SEEK_END)
657 fSize = yaffs_GetObjectFileLength(obj);
658 if(fSize >= 0 && (fSize + offset) >= 0)
660 pos = fSize + offset;
680 int yaffsfs_DoUnlink(const char *path,int isDirectory)
682 yaffs_Object *dir = NULL;
683 yaffs_Object *obj = NULL;
685 int result = YAFFS_FAIL;
689 obj = yaffsfs_FindObject(NULL,path,0);
690 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
693 yaffsfs_SetError(-ENOTDIR);
697 yaffsfs_SetError(-ENOENT);
699 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
701 yaffsfs_SetError(-EISDIR);
703 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
705 yaffsfs_SetError(-ENOTDIR);
707 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
709 yaffsfs_SetError(-ENOTDIR);
713 result = yaffs_Unlink(dir,name);
715 if(result == YAFFS_FAIL && isDirectory)
717 yaffsfs_SetError(-ENOTEMPTY);
725 return (result == YAFFS_FAIL) ? -1 : 0;
727 int yaffs_rmdir(const char *path)
729 return yaffsfs_DoUnlink(path,1);
732 int yaffs_unlink(const char *path)
734 return yaffsfs_DoUnlink(path,0);
737 int yaffs_rename(const char *oldPath, const char *newPath)
739 yaffs_Object *olddir = NULL;
740 yaffs_Object *newdir = NULL;
743 int result= YAFFS_FAIL;
747 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
748 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
750 if(!olddir || !newdir)
753 yaffsfs_SetError(-EBADF);
755 else if(olddir->myDev != newdir->myDev)
757 // oops must be on same device
759 yaffsfs_SetError(-EXDEV);
763 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
768 return (result == YAFFS_FAIL) ? -1 : 0;
772 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
778 obj = yaffs_GetEquivalentObject(obj);
783 buf->st_dev = (int)obj->myDev->genericDevice;
784 buf->st_ino = obj->objectId;
785 buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
787 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
789 buf->st_mode |= S_IFDIR;
791 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
793 buf->st_mode |= S_IFLNK;
795 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
797 buf->st_mode |= S_IFREG;
800 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
803 buf->st_rdev = obj->st_rdev;
804 buf->st_size = yaffs_GetObjectFileLength(obj);
805 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
806 buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
807 buf->st_atime = obj->st_atime;
808 buf->st_ctime = obj->st_ctime;
809 buf->st_mtime = obj->st_mtime;
815 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
822 obj = yaffsfs_FindObject(NULL,path,0);
826 obj = yaffsfs_FollowLink(obj,0);
831 retVal = yaffsfs_DoStat(obj,buf);
835 // todo error not found
836 yaffsfs_SetError(-ENOENT);
845 int yaffs_stat(const char *path, struct yaffs_stat *buf)
847 return yaffsfs_DoStatOrLStat(path,buf,0);
850 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
852 return yaffsfs_DoStatOrLStat(path,buf,1);
855 int yaffs_fstat(int fd, struct yaffs_stat *buf)
862 obj = yaffsfs_GetHandleObject(fd);
866 retVal = yaffsfs_DoStat(obj,buf);
871 yaffsfs_SetError(-EBADF);
879 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
885 obj = yaffs_GetEquivalentObject(obj);
892 result = yaffs_FlushFile(obj,0);
895 return result == YAFFS_OK ? 0 : -1;
899 int yaffs_chmod(const char *path, mode_t mode)
906 obj = yaffsfs_FindObject(NULL,path,0);
910 retVal = yaffsfs_DoChMod(obj,mode);
914 // todo error not found
915 yaffsfs_SetError(-ENOENT);
925 int yaffs_fchmod(int fd, mode_t mode)
932 obj = yaffsfs_GetHandleObject(fd);
936 retVal = yaffsfs_DoChMod(obj,mode);
941 yaffsfs_SetError(-EBADF);
950 int yaffs_mkdir(const char *path, mode_t mode)
952 yaffs_Object *parent = NULL;
958 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
959 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
966 yaffsfs_SetError(-ENOSPC); // just assume no space for now
975 int yaffs_mount(const char *path)
978 int result=YAFFS_FAIL;
979 yaffs_Device *dev=NULL;
983 dev = yaffsfs_FindDevice(path,&dummy);
988 result = yaffs_GutsInitialise(dev);
989 if(result == YAFFS_FAIL)
991 // todo error - mount failed
992 yaffsfs_SetError(-ENOMEM);
994 retVal = result ? 0 : -1;
999 //todo error - already mounted.
1000 yaffsfs_SetError(-EBUSY);
1005 // todo error - no device
1006 yaffsfs_SetError(-ENODEV);
1013 int yaffs_unmount(const char *path)
1016 yaffs_Device *dev=NULL;
1020 dev = yaffsfs_FindDevice(path,&dummy);
1027 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1029 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1031 inUse = 1; // the device is in use, can't unmount
1037 yaffs_Deinitialise(dev);
1043 // todo error can't unmount as files are open
1044 yaffsfs_SetError(-EBUSY);
1050 //todo error - not mounted.
1051 yaffsfs_SetError(-EINVAL);
1057 // todo error - no device
1058 yaffsfs_SetError(-ENODEV);
1065 off_t yaffs_freespace(const char *path)
1068 yaffs_Device *dev=NULL;
1072 dev = yaffsfs_FindDevice(path,&dummy);
1075 retVal = yaffs_GetNumberOfFreeChunks(dev);
1076 retVal *= YAFFS_BYTES_PER_CHUNK;
1081 yaffsfs_SetError(-EINVAL);
1088 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1091 yaffsfs_DeviceConfiguration *cfg;
1093 yaffsfs_configurationList = cfgList;
1095 yaffsfs_InitHandles();
1097 cfg = yaffsfs_configurationList;
1099 while(cfg && cfg->prefix && cfg->dev)
1101 cfg->dev->isMounted = 0;
1110 // Directory search stuff.
1112 yaffs_DIR *yaffs_opendir(const char *dirname)
1114 yaffs_DIR *dir = NULL;
1115 yaffs_Object *obj = NULL;
1116 yaffsfs_DirectorySearchContext *dsc = NULL;
1120 obj = yaffsfs_FindObject(NULL,dirname,0);
1122 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1125 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1126 dir = (yaffs_DIR *)dsc;
1129 dsc->magic = YAFFS_MAGIC;
1131 memset(dsc->name,0,NAME_MAX+1);
1132 strncpy(dsc->name,dirname,NAME_MAX);
1142 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1144 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1145 struct yaffs_dirent *retVal = NULL;
1146 struct list_head *i;
1147 yaffs_Object *entry = NULL;
1149 yaffs_Object *obj = NULL;
1150 struct yaffsfs_ObjectListEntry *list = NULL;
1157 if(dsc && dsc->magic == YAFFS_MAGIC)
1159 yaffsfs_SetError(0);
1161 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1163 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1166 list_for_each(i,&obj->variant.directoryVariant.children)
1169 entry = (i) ? list_entry(i, yaffs_Object,siblings) : NULL;
1175 while(list && !inList)
1177 if(list->objectId == entry->objectId)
1184 if(!inList) goto foundNew;
1191 if(!inList && entry)
1193 //This is the entry we're going to return;
1194 struct yaffsfs_ObjectListEntry *le;
1196 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1200 le->next = dsc->list;
1201 le->objectId = entry->objectId;
1204 dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1205 dsc->de.d_off = offset;
1206 yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1207 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1218 yaffsfs_SetError(-EBADF);
1227 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc)
1230 struct yaffsfs_ObjectListEntry *le;
1232 if(dsc && dsc->magic == YAFFS_MAGIC)
1237 dsc->list = dsc->list->next;
1244 void yaffs_rewinddir(yaffs_DIR *dirp)
1246 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1249 yaffsfs_ListClear(dsc);
1254 int yaffs_closedir(yaffs_DIR *dirp)
1256 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1259 yaffsfs_ListClear(dsc);
1268 int yaffs_symlink(const char *oldpath, const char *newpath)
1270 yaffs_Object *parent = NULL;
1274 int mode = 0; // ignore for now
1277 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1278 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1285 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1295 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1297 yaffs_Object *obj = NULL;
1303 obj = yaffsfs_FindObject(NULL,path,0);
1307 yaffsfs_SetError(-ENOENT);
1310 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1312 yaffsfs_SetError(-EINVAL);
1317 char *alias = obj->variant.symLinkVariant.alias;
1318 memset(buf,0,bufsiz);
1319 strncpy(buf,alias,bufsiz - 1);
1326 int yaffs_link(const char *oldpath, const char *newpath);
1327 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1329 int yaffs_DumpDevStruct(const char *path)
1333 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1337 yaffs_Device *dev = obj->myDev;
1340 "nPageWrites.......... %d\n"
1341 "nPageReads........... %d\n"
1342 "nBlockErasures....... %d\n"
1343 "nGCCopies............ %d\n"
1344 "garbageCollections... %d\n"
1345 "passiveGarbageColl'ns %d\n"
1349 dev->nBlockErasures,
1351 dev->garbageCollections,
1352 dev->passiveGarbageCollections