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.3 2003-02-19 00:29:55 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);
429 obj = yaffs_MknodFile(dir,name,mode,0,0);
433 } yaffsfs_SetError(-ENOTDIR);
436 if(obj && !openDenied)
440 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
441 h->append = (oflag & O_APPEND) ? 1 : 0;
442 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
446 if((oflag & O_TRUNC) && !h->readOnly)
449 yaffs_ResizeFile(obj,0);
455 yaffsfs_PutHandle(handle);
456 yaffsfs_SetError(-EACCESS);
467 int yaffs_close(int fd)
469 yaffsfs_Handle *h = NULL;
474 h = yaffsfs_GetHandlePointer(fd);
479 yaffs_FlushFile(h->obj,1);
481 if(h->obj->inUse <= 0 && h->obj->unlinked)
483 yaffs_DeleteFile(h->obj);
485 yaffsfs_PutHandle(fd);
491 yaffsfs_SetError(-EBADF);
500 int yaffs_read(int fd, void *buf, unsigned int nbyte)
502 yaffsfs_Handle *h = NULL;
503 yaffs_Object *obj = NULL;
509 h = yaffsfs_GetHandlePointer(fd);
510 obj = yaffsfs_GetHandleObject(fd);
515 yaffsfs_SetError(-EBADF);
520 if(yaffs_GetObjectFileLength(obj) > pos)
522 maxRead = yaffs_GetObjectFileLength(obj) - pos;
537 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
540 h->position = pos + nRead;
557 return (nRead >= 0) ? nRead : -1;
561 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
563 yaffsfs_Handle *h = NULL;
564 yaffs_Object *obj = NULL;
569 h = yaffsfs_GetHandlePointer(fd);
570 obj = yaffsfs_GetHandleObject(fd);
575 yaffsfs_SetError(-EBADF);
577 else if( h && obj && h->readOnly)
585 pos = yaffs_GetObjectFileLength(obj);
592 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte);
596 h->position = pos + nWritten;
608 return (nWritten >= 0) ? nWritten : -1;
612 off_t yaffs_lseek(int fd, off_t offset, int whence)
614 yaffsfs_Handle *h = NULL;
615 yaffs_Object *obj = NULL;
620 h = yaffsfs_GetHandlePointer(fd);
621 obj = yaffsfs_GetHandleObject(fd);
626 yaffsfs_SetError(-EBADF);
628 else if(whence == SEEK_SET)
635 else if(whence == SEEK_CUR)
637 if( (h->position + offset) >= 0)
639 pos = (h->position + offset);
642 else if(whence == SEEK_END)
644 fSize = yaffs_GetObjectFileLength(obj);
645 if(fSize >= 0 && (fSize + offset) >= 0)
647 pos = fSize + offset;
667 int yaffsfs_DoUnlink(const char *path,int isDirectory)
669 yaffs_Object *dir = NULL;
670 yaffs_Object *obj = NULL;
672 int result = YAFFS_FAIL;
676 obj = yaffsfs_FindObject(NULL,path,0);
677 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
680 yaffsfs_SetError(-ENOTDIR);
684 yaffsfs_SetError(-ENOENT);
686 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
688 yaffsfs_SetError(-EISDIR);
690 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
692 yaffsfs_SetError(-ENOTDIR);
694 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
696 yaffsfs_SetError(-ENOTDIR);
700 result = yaffs_Unlink(dir,name);
702 if(result == YAFFS_FAIL && isDirectory)
704 yaffsfs_SetError(-ENOTEMPTY);
712 return (result == YAFFS_FAIL) ? -1 : 0;
714 int yaffs_rmdir(const char *path)
716 return yaffsfs_DoUnlink(path,1);
719 int yaffs_unlink(const char *path)
721 return yaffsfs_DoUnlink(path,0);
724 int yaffs_rename(const char *oldPath, const char *newPath)
726 yaffs_Object *olddir = NULL;
727 yaffs_Object *newdir = NULL;
730 int result= YAFFS_FAIL;
734 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
735 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
737 if(!olddir || !newdir)
740 yaffsfs_SetError(-EBADF);
742 else if(olddir->myDev != newdir->myDev)
744 // oops must be on same device
746 yaffsfs_SetError(-EXDEV);
750 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
755 return (result == YAFFS_FAIL) ? -1 : 0;
759 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
765 obj = yaffs_GetEquivalentObject(obj);
770 buf->st_dev = (int)obj->myDev->genericDevice;
771 buf->st_ino = obj->objectId;
772 buf->st_mode = obj->st_mode & ~S_IFMT; // clear out file type bits
774 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
776 buf->st_mode |= S_IFDIR;
778 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
780 buf->st_mode |= S_IFLNK;
782 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
784 buf->st_mode |= S_IFREG;
787 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
790 buf->st_rdev = obj->st_rdev;
791 buf->st_size = yaffs_GetObjectFileLength(obj);
792 buf->st_blksize = YAFFS_BYTES_PER_CHUNK;
793 buf->st_blocks = (buf->st_size + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK;
794 buf->st_atime = obj->st_atime;
795 buf->st_ctime = obj->st_ctime;
796 buf->st_mtime = obj->st_mtime;
802 static int yaffsfs_DoStatOrLStat(const char *path, struct yaffs_stat *buf,int doLStat)
809 obj = yaffsfs_FindObject(NULL,path,0);
813 obj = yaffsfs_FollowLink(obj,0);
818 retVal = yaffsfs_DoStat(obj,buf);
822 // todo error not found
823 yaffsfs_SetError(-ENOENT);
832 int yaffs_stat(const char *path, struct yaffs_stat *buf)
834 return yaffsfs_DoStatOrLStat(path,buf,0);
837 int yaffs_lstat(const char *path, struct yaffs_stat *buf)
839 return yaffsfs_DoStatOrLStat(path,buf,1);
842 int yaffs_fstat(int fd, struct yaffs_stat *buf)
849 obj = yaffsfs_GetHandleObject(fd);
853 retVal = yaffsfs_DoStat(obj,buf);
858 yaffsfs_SetError(-EBADF);
866 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
872 obj = yaffs_GetEquivalentObject(obj);
879 result = yaffs_FlushFile(obj,0);
882 return result == YAFFS_OK ? 0 : -1;
886 int yaffs_chmod(const char *path, mode_t mode)
893 obj = yaffsfs_FindObject(NULL,path,0);
897 retVal = yaffsfs_DoChMod(obj,mode);
901 // todo error not found
902 yaffsfs_SetError(-ENOENT);
912 int yaffs_fchmod(int fd, mode_t mode)
919 obj = yaffsfs_GetHandleObject(fd);
923 retVal = yaffsfs_DoChMod(obj,mode);
928 yaffsfs_SetError(-EBADF);
937 int yaffs_mkdir(const char *path, mode_t mode)
939 yaffs_Object *parent = NULL;
945 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
946 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
953 yaffsfs_SetError(-ENOSPC); // just assume no space for now
962 int yaffs_mount(const char *path)
965 int result=YAFFS_FAIL;
966 yaffs_Device *dev=NULL;
970 dev = yaffsfs_FindDevice(path,&dummy);
975 result = yaffs_GutsInitialise(dev);
976 if(result == YAFFS_FAIL)
978 // todo error - mount failed
979 yaffsfs_SetError(-ENOMEM);
981 retVal = result ? 0 : -1;
986 //todo error - already mounted.
987 yaffsfs_SetError(-EBUSY);
992 // todo error - no device
993 yaffsfs_SetError(-ENODEV);
1000 int yaffs_unmount(const char *path)
1003 yaffs_Device *dev=NULL;
1007 dev = yaffsfs_FindDevice(path,&dummy);
1014 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1016 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1018 inUse = 1; // the device is in use, can't unmount
1024 yaffs_Deinitialise(dev);
1030 // todo error can't unmount as files are open
1031 yaffsfs_SetError(-EBUSY);
1037 //todo error - not mounted.
1038 yaffsfs_SetError(-EINVAL);
1044 // todo error - no device
1045 yaffsfs_SetError(-ENODEV);
1052 off_t yaffs_freespace(const char *path)
1055 yaffs_Device *dev=NULL;
1059 dev = yaffsfs_FindDevice(path,&dummy);
1062 retVal = yaffs_GetNumberOfFreeChunks(dev);
1063 retVal *= YAFFS_BYTES_PER_CHUNK;
1068 yaffsfs_SetError(-EINVAL);
1075 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1078 yaffsfs_DeviceConfiguration *cfg;
1080 yaffsfs_configurationList = cfgList;
1082 yaffsfs_InitHandles();
1084 cfg = yaffsfs_configurationList;
1086 while(cfg && cfg->prefix && cfg->dev)
1088 cfg->dev->isMounted = 0;
1097 // Directory search stuff.
1099 yaffs_DIR *yaffs_opendir(const char *dirname)
1101 yaffs_DIR *dir = NULL;
1102 yaffs_Object *obj = NULL;
1103 yaffsfs_DirectorySearchContext *dsc = NULL;
1107 obj = yaffsfs_FindObject(NULL,dirname,0);
1109 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1112 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1113 dir = (yaffs_DIR *)dsc;
1116 dsc->magic = YAFFS_MAGIC;
1118 memset(dsc->name,0,NAME_MAX+1);
1119 strncpy(dsc->name,dirname,NAME_MAX);
1129 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1131 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1132 struct yaffs_dirent *retVal = NULL;
1133 struct list_head *i;
1134 yaffs_Object *entry = NULL;
1136 yaffs_Object *obj = NULL;
1137 struct yaffsfs_ObjectListEntry *list = NULL;
1144 if(dsc && dsc->magic == YAFFS_MAGIC)
1146 yaffsfs_SetError(0);
1148 obj = yaffsfs_FindObject(NULL,dsc->name,0);
1150 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1153 list_for_each(i,&obj->variant.directoryVariant.children)
1156 entry = (i) ? list_entry(i, yaffs_Object,siblings) : NULL;
1162 while(list && !inList)
1164 if(list->objectId == entry->objectId)
1171 if(!inList) goto foundNew;
1178 if(!inList && entry)
1180 //This is the entry we're going to return;
1181 struct yaffsfs_ObjectListEntry *le;
1183 le = YMALLOC(sizeof(struct yaffsfs_ObjectListEntry));
1187 le->next = dsc->list;
1188 le->objectId = entry->objectId;
1191 dsc->de.d_ino = yaffs_GetEquivalentObject(entry)->objectId;
1192 dsc->de.d_off = offset;
1193 yaffs_GetObjectName(entry,dsc->de.d_name,NAME_MAX+1);
1194 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1205 yaffsfs_SetError(-EBADF);
1214 void yaffsfs_ListClear(yaffsfs_DirectorySearchContext *dsc)
1217 struct yaffsfs_ObjectListEntry *le;
1219 if(dsc && dsc->magic == YAFFS_MAGIC)
1224 dsc->list = dsc->list->next;
1231 void yaffs_rewinddir(yaffs_DIR *dirp)
1233 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1236 yaffsfs_ListClear(dsc);
1241 int yaffs_closedir(yaffs_DIR *dirp)
1243 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1246 yaffsfs_ListClear(dsc);
1255 int yaffs_symlink(const char *oldpath, const char *newpath)
1257 yaffs_Object *parent = NULL;
1261 int mode = 0; // ignore for now
1264 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1265 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1272 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1282 int yaffs_readlink(const char *path, char *buf, int bufsiz)
1284 yaffs_Object *obj = NULL;
1290 obj = yaffsfs_FindObject(NULL,path,0);
1294 yaffsfs_SetError(-ENOENT);
1297 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1299 yaffsfs_SetError(-EINVAL);
1304 char *alias = obj->variant.symLinkVariant.alias;
1305 memset(buf,0,bufsiz);
1306 strncpy(buf,alias,bufsiz - 1);
1313 int yaffs_link(const char *oldpath, const char *newpath);
1314 int yaffs_mknod(const char *pathname, mode_t mode, dev_t dev);
1316 int yaffs_DumpDevStruct(const char *path)
1320 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1324 yaffs_Device *dev = obj->myDev;
1327 "nPageWrites.......... %d\n"
1328 "nPageReads........... %d\n"
1329 "nBlockErasures....... %d\n"
1330 "nGCCopies............ %d\n"
1331 "garbageCollections... %d\n"
1332 "passiveGarbageColl'ns %d\n"
1336 dev->nBlockErasures,
1338 dev->garbageCollections,
1339 dev->passiveGarbageCollections