2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2007 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
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.
15 #include "yaffs_guts.h"
17 #include <string.h> // for memset
20 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
23 #define NULL ((void *)0)
27 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.29 2009-12-07 01:17:33 charles Exp $";
29 // configurationList is the list of devices that are supported
30 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
33 /* Some forward references */
34 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
35 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
42 unsigned int yaffs_wr_attempts;
45 __u8 inUse:1; // this handle is in use
46 __u8 readOnly:1; // this handle is read only
47 __u8 append:1; // append only
48 __u8 exclusive:1; // exclusive
49 __u32 position; // current position in file
50 yaffs_Object *obj; // the object
54 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
57 /// Inilitalise handles on start-up.
59 static int yaffsfs_InitHandles(void)
62 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
63 yaffsfs_handle[i].inUse = 0;
64 yaffsfs_handle[i].obj = NULL;
69 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
71 if(h < 0 || h >= YAFFSFS_N_HANDLES)
74 return &yaffsfs_handle[h];
77 yaffs_Object *yaffsfs_GetHandleObject(int handle)
79 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
89 // Grab a handle (when opening a file)
92 static int yaffsfs_GetHandle(void)
97 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
98 h = yaffsfs_GetHandlePointer(i);
100 // todo bug: should never happen
103 memset(h,0,sizeof(yaffsfs_Handle));
112 // Let go of a handle (when closing a file)
114 static int yaffsfs_PutHandle(int handle)
116 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
127 // Stuff to search for a directory from a path
130 int yaffsfs_Match(YCHAR a, YCHAR b)
136 int yaffsfs_IsPathDivider(YCHAR ch)
138 YCHAR *str = YAFFS_PATH_DIVIDERS;
149 // yaffsfs_FindDevice
151 // Scan the configuration list to find the root.
152 // Curveballs: Should match paths that end in '/' too
153 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
154 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
156 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
157 const YCHAR *leftOver;
159 yaffs_Device *retval = NULL;
161 int longestMatch = -1;
164 // Check all configs, choose the one that:
165 // 1) Actually matches a prefix (ie /a amd /abc will not match
166 // 2) Matches the longest.
167 while(cfg && cfg->prefix && cfg->dev){
174 while(matching && *p && *leftOver){
176 while(yaffsfs_IsPathDivider(*p))
180 while(yaffsfs_IsPathDivider(*leftOver))
183 // Now match the text part
185 *p && !yaffsfs_IsPathDivider(*p) &&
186 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
187 if(yaffsfs_Match(*p,*leftOver)){
197 // Skip over any /s in leftOver
198 while(yaffsfs_IsPathDivider(*leftOver))
202 if( matching && (thisMatchLength > longestMatch)){
204 *restOfPath = (YCHAR *)leftOver;
206 longestMatch = thisMatchLength;
215 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
217 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
218 const YCHAR *leftOver;
220 yaffs_Device *retval = NULL;
222 int longestMatch = -1;
224 // Check all configs, choose the one that:
225 // 1) Actually matches a prefix (ie /a amd /abc will not match
226 // 2) Matches the longest.
227 while(cfg && cfg->prefix && cfg->dev){
232 while(*p && //unmatched part of prefix
233 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
235 yaffsfs_Match(*p,*leftOver)){
242 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
243 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
244 (thisMatchLength > longestMatch)){
246 *restOfPath = (YCHAR *)leftOver;
248 longestMatch = thisMatchLength;
256 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
261 dev= yaffsfs_FindDevice(path,restOfPath);
262 if(dev && dev->isMounted){
268 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
271 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
272 YCHAR *alias = obj->variant.symLinkVariant.alias;
274 if(yaffsfs_IsPathDivider(*alias))
275 // Starts with a /, need to scan from root up
276 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
278 // Relative to here, so use the parent of the symlink as a start
279 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
285 // yaffsfs_FindDirectory
286 // Parse a path to determine the directory and the name within the directory.
288 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
289 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
293 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
296 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
301 restOfPath = (YCHAR *)path;
304 dir = yaffsfs_FindRoot(path,&restOfPath);
308 // curve ball: also throw away surplus '/'
309 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
310 while(yaffsfs_IsPathDivider(*restOfPath))
311 restOfPath++; // get rid of '/'
316 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
317 if (i < YAFFS_MAX_NAME_LENGTH){
318 str[i] = *restOfPath;
326 // got to the end of the string
329 if(yaffs_strcmp(str,_Y(".")) == 0)
333 else if(yaffs_strcmp(str,_Y("..")) == 0)
336 dir = yaffs_FindObjectByName(dir,str);
338 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
339 dir = yaffsfs_FollowLink(dir,symDepth);
342 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
347 // directory did not exist.
351 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
353 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
356 // yaffsfs_FindObject turns a path for an existing object into the object
358 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
363 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
366 return yaffs_FindObjectByName(dir,name);
372 int yaffs_dup(int fd)
375 yaffsfs_Handle *oldPtr = NULL;
376 yaffsfs_Handle *newPtr = NULL;
380 oldPtr = yaffsfs_GetHandlePointer(fd);
381 if(oldPtr && oldPtr->inUse)
382 newHandle = yaffsfs_GetHandle();
384 newPtr = yaffsfs_GetHandlePointer(newHandle);
392 yaffsfs_SetError(-EBADF);
394 yaffsfs_SetError(-ENOMEM);
400 int yaffs_open(const YCHAR *path, int oflag, int mode)
402 yaffs_Object *obj = NULL;
403 yaffs_Object *dir = NULL;
406 yaffsfs_Handle *h = NULL;
408 int alreadyExclusive = 0;
411 int errorReported = 0;
416 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
421 handle = yaffsfs_GetHandle();
425 h = yaffsfs_GetHandlePointer(handle);
428 // try to find the exisiting object
429 obj = yaffsfs_FindObject(NULL,path,0);
431 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
432 obj = yaffsfs_FollowLink(obj,symDepth++);
434 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
438 // Check if the object is already in use
439 alreadyOpen = alreadyExclusive = 0;
441 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
444 yaffsfs_handle[i].inUse &&
445 obj == yaffsfs_handle[i].obj){
447 if(yaffsfs_handle[i].exclusive)
448 alreadyExclusive = 1;
452 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
455 // Open should fail if O_CREAT and O_EXCL are specified
456 if((oflag & O_EXCL) && (oflag & O_CREAT)){
458 yaffsfs_SetError(-EEXIST);
462 // Check file permissions
463 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
464 !(obj->yst_mode & S_IREAD))
467 if( (oflag & O_RDWR) &&
468 !(obj->yst_mode & S_IREAD))
471 if( (oflag & (O_RDWR | O_WRONLY)) &&
472 !(obj->yst_mode & S_IWRITE))
475 } else if((oflag & O_CREAT)) {
476 // Let's see if we can create this file
477 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
479 obj = yaffs_MknodFile(dir,name,mode,0,0);
481 yaffsfs_SetError(-ENOTDIR);
486 if(obj && !openDenied) {
489 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
490 h->append = (oflag & O_APPEND) ? 1 : 0;
491 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
495 if((oflag & O_TRUNC) && !h->readOnly)
496 yaffs_ResizeFile(obj,0);
498 yaffsfs_PutHandle(handle);
500 yaffsfs_SetError(-EACCES);
512 int yaffs_Dofsync(int fd,int datasync)
514 yaffsfs_Handle *h = NULL;
519 h = yaffsfs_GetHandlePointer(fd);
523 yaffs_FlushFile(h->obj,1,datasync);
526 yaffsfs_SetError(-EBADF);
535 int yaffs_fsync(int fd)
537 return yaffs_Dofsync(fd,0);
540 int yaffs_flush(int fd)
542 return yaffs_fsync(fd);
545 int yaffs_fdatasync(int fd)
547 return yaffs_Dofsync(fd,1);
550 int yaffs_close(int fd)
552 yaffsfs_Handle *h = NULL;
557 h = yaffsfs_GetHandlePointer(fd);
561 yaffs_FlushFile(h->obj,1,0);
563 if(h->obj->inUse <= 0 && h->obj->unlinked)
564 yaffs_DeleteObject(h->obj);
566 yaffsfs_PutHandle(fd);
570 yaffsfs_SetError(-EBADF);
579 int yaffs_read(int fd, void *buf, unsigned int nbyte)
581 yaffsfs_Handle *h = NULL;
582 yaffs_Object *obj = NULL;
585 unsigned int maxRead;
588 h = yaffsfs_GetHandlePointer(fd);
589 obj = yaffsfs_GetHandleObject(fd);
593 yaffsfs_SetError(-EBADF);
597 if(yaffs_GetObjectFileLength(obj) > pos)
598 maxRead = yaffs_GetObjectFileLength(obj) - pos;
607 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
609 h->position = pos + nRead;
621 return (nRead >= 0) ? nRead : -1;
625 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
627 yaffsfs_Handle *h = NULL;
628 yaffs_Object *obj = NULL;
631 unsigned int maxRead;
634 h = yaffsfs_GetHandlePointer(fd);
635 obj = yaffsfs_GetHandleObject(fd);
639 yaffsfs_SetError(-EBADF);
642 if(yaffs_GetObjectFileLength(obj) > pos)
643 maxRead = yaffs_GetObjectFileLength(obj) - pos;
652 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
660 return (nRead >= 0) ? nRead : -1;
664 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
666 yaffsfs_Handle *h = NULL;
667 yaffs_Object *obj = NULL;
670 int writeThrough = 0;
673 h = yaffsfs_GetHandlePointer(fd);
674 obj = yaffsfs_GetHandleObject(fd);
678 yaffsfs_SetError(-EBADF);
679 else if( h && obj && h->readOnly){
681 } else if( h && obj){
683 pos = yaffs_GetObjectFileLength(obj);
687 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
690 h->position = pos + nWritten;
700 return (nWritten >= 0) ? nWritten : -1;
704 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
706 yaffsfs_Handle *h = NULL;
707 yaffs_Object *obj = NULL;
710 int writeThrough = 0;
713 h = yaffsfs_GetHandlePointer(fd);
714 obj = yaffsfs_GetHandleObject(fd);
718 yaffsfs_SetError(-EBADF);
719 else if( h && obj && h->readOnly){
725 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
727 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
728 yaffsfs_SetError(-ENOSPC);
735 return (nWritten >= 0) ? nWritten : -1;
740 int yaffs_truncate(const YCHAR *path,off_t newSize)
742 yaffs_Object *obj = NULL;
743 int result = YAFFS_FAIL;
747 obj = yaffsfs_FindObject(NULL,path,0);
749 obj = yaffs_GetEquivalentObject(obj);
752 yaffsfs_SetError(-ENOENT);
753 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
754 yaffsfs_SetError(-EISDIR);
756 result = yaffs_ResizeFile(obj,newSize);
761 return (result) ? 0 : -1;
764 int yaffs_ftruncate(int fd, off_t newSize)
766 yaffsfs_Handle *h = NULL;
767 yaffs_Object *obj = NULL;
771 h = yaffsfs_GetHandlePointer(fd);
772 obj = yaffsfs_GetHandleObject(fd);
776 yaffsfs_SetError(-EBADF);
779 result = yaffs_ResizeFile(obj,newSize);
783 return (result) ? 0 : -1;
787 off_t yaffs_lseek(int fd, off_t offset, int whence)
789 yaffsfs_Handle *h = NULL;
790 yaffs_Object *obj = NULL;
795 h = yaffsfs_GetHandlePointer(fd);
796 obj = yaffsfs_GetHandleObject(fd);
800 yaffsfs_SetError(-EBADF);
801 else if(whence == SEEK_SET){
805 else if(whence == SEEK_CUR) {
806 if( (h->position + offset) >= 0)
807 pos = (h->position + offset);
809 else if(whence == SEEK_END) {
810 fSize = yaffs_GetObjectFileLength(obj);
811 if(fSize >= 0 && (fSize + offset) >= 0)
812 pos = fSize + offset;
828 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
830 yaffs_Object *dir = NULL;
831 yaffs_Object *obj = NULL;
833 int result = YAFFS_FAIL;
837 obj = yaffsfs_FindObject(NULL,path,0);
838 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
840 yaffsfs_SetError(-ENOTDIR);
842 yaffsfs_SetError(-ENOENT);
843 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
844 yaffsfs_SetError(-EISDIR);
845 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
846 yaffsfs_SetError(-ENOTDIR);
848 result = yaffs_Unlink(dir,name);
850 if(result == YAFFS_FAIL && isDirectory)
851 yaffsfs_SetError(-ENOTEMPTY);
858 return (result == YAFFS_FAIL) ? -1 : 0;
862 int yaffs_rmdir(const YCHAR *path)
864 return yaffsfs_DoUnlink(path,1);
867 int yaffs_unlink(const YCHAR *path)
869 return yaffsfs_DoUnlink(path,0);
872 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
874 yaffs_Object *olddir = NULL;
875 yaffs_Object *newdir = NULL;
876 yaffs_Object *obj = NULL;
879 int result= YAFFS_FAIL;
880 int renameAllowed = 1;
884 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
885 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
886 obj = yaffsfs_FindObject(NULL,oldPath,0);
888 if(!olddir || !newdir || !obj) {
890 yaffsfs_SetError(-EBADF);
892 } else if(olddir->myDev != newdir->myDev) {
893 // oops must be on same device
895 yaffsfs_SetError(-EXDEV);
897 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
898 // It is a directory, check that it is not being renamed to
899 // being its own decendent.
900 // Do this by tracing from the new directory back to the root, checking for obj
902 yaffs_Object *xx = newdir;
904 while( renameAllowed && xx){
910 yaffsfs_SetError(-EACCES);
914 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
918 return (result == YAFFS_FAIL) ? -1 : 0;
922 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
927 obj = yaffs_GetEquivalentObject(obj);
930 buf->st_dev = (int)obj->myDev->genericDevice;
931 buf->st_ino = obj->objectId;
932 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
934 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
935 buf->st_mode |= S_IFDIR;
936 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
937 buf->st_mode |= S_IFLNK;
938 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
939 buf->st_mode |= S_IFREG;
941 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
944 buf->st_rdev = obj->yst_rdev;
945 buf->st_size = yaffs_GetObjectFileLength(obj);
946 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
947 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
948 #if CONFIG_YAFFS_WINCE
949 buf->yst_wince_atime[0] = obj->win_atime[0];
950 buf->yst_wince_atime[1] = obj->win_atime[1];
951 buf->yst_wince_ctime[0] = obj->win_ctime[0];
952 buf->yst_wince_ctime[1] = obj->win_ctime[1];
953 buf->yst_wince_mtime[0] = obj->win_mtime[0];
954 buf->yst_wince_mtime[1] = obj->win_mtime[1];
956 buf->yst_atime = obj->yst_atime;
957 buf->yst_ctime = obj->yst_ctime;
958 buf->yst_mtime = obj->yst_mtime;
965 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
972 obj = yaffsfs_FindObject(NULL,path,0);
975 obj = yaffsfs_FollowLink(obj,0);
978 retVal = yaffsfs_DoStat(obj,buf);
980 // todo error not found
981 yaffsfs_SetError(-ENOENT);
989 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
991 return yaffsfs_DoStatOrLStat(path,buf,0);
994 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
996 return yaffsfs_DoStatOrLStat(path,buf,1);
999 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1006 obj = yaffsfs_GetHandleObject(fd);
1009 retVal = yaffsfs_DoStat(obj,buf);
1012 yaffsfs_SetError(-EBADF);
1019 #ifdef CONFIG_YAFFS_WINCE
1020 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1027 obj = yaffsfs_GetHandleObject(fd);
1032 wctime[0] = obj->win_ctime[0];
1033 wctime[1] = obj->win_ctime[1];
1036 watime[0] = obj->win_atime[0];
1037 watime[1] = obj->win_atime[1];
1040 wmtime[0] = obj->win_mtime[0];
1041 wmtime[1] = obj->win_mtime[1];
1048 yaffsfs_SetError(-EBADF);
1056 int yaffs_set_wince_times(int fd,
1057 const unsigned *wctime,
1058 const unsigned *watime,
1059 const unsigned *wmtime)
1066 obj = yaffsfs_GetHandleObject(fd);
1071 obj->win_ctime[0] = wctime[0];
1072 obj->win_ctime[1] = wctime[1];
1075 obj->win_atime[0] = watime[0];
1076 obj->win_atime[1] = watime[1];
1079 obj->win_mtime[0] = wmtime[0];
1080 obj->win_mtime[1] = wmtime[1];
1084 result = yaffs_FlushFile(obj,0,0);
1088 yaffsfs_SetError(-EBADF);
1098 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1103 obj = yaffs_GetEquivalentObject(obj);
1106 obj->yst_mode = mode;
1108 result = yaffs_FlushFile(obj,0,0);
1111 return result == YAFFS_OK ? 0 : -1;
1115 int yaffs_access(const YCHAR *path, int amode)
1122 obj = yaffsfs_FindObject(NULL,path,0);
1127 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1129 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1131 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1135 yaffsfs_SetError(-EACCES);
1139 // todo error not found
1140 yaffsfs_SetError(-ENOENT);
1151 int yaffs_chmod(const YCHAR *path, mode_t mode)
1158 obj = yaffsfs_FindObject(NULL,path,0);
1161 retVal = yaffsfs_DoChMod(obj,mode);
1163 // todo error not found
1164 yaffsfs_SetError(-ENOENT);
1173 int yaffs_fchmod(int fd, mode_t mode)
1180 obj = yaffsfs_GetHandleObject(fd);
1183 retVal = yaffsfs_DoChMod(obj,mode);
1186 yaffsfs_SetError(-EBADF);
1194 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1196 yaffs_Object *parent = NULL;
1197 yaffs_Object *dir = NULL;
1202 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1204 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1209 yaffsfs_SetError(-ENOENT); // missing path
1210 else if (yaffs_FindObjectByName(parent,name))
1211 yaffsfs_SetError(-EEXIST); // the name already exists
1213 yaffsfs_SetError(-ENOSPC); // just assume no space
1222 int yaffs_mount(const YCHAR *path)
1225 int result=YAFFS_FAIL;
1226 yaffs_Device *dev=NULL;
1229 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1232 dev = yaffsfs_FindDevice(path,&dummy);
1234 if(!dev->isMounted){
1235 result = yaffs_GutsInitialise(dev);
1236 if(result == YAFFS_FAIL)
1237 // todo error - mount failed
1238 yaffsfs_SetError(-ENOMEM);
1239 retVal = result ? 0 : -1;
1243 //todo error - already mounted.
1244 yaffsfs_SetError(-EBUSY);
1246 // todo error - no device
1247 yaffsfs_SetError(-ENODEV);
1254 int yaffs_sync(const YCHAR *path)
1257 yaffs_Device *dev=NULL;
1261 dev = yaffsfs_FindDevice(path,&dummy);
1265 yaffs_FlushEntireDeviceCache(dev);
1266 yaffs_CheckpointSave(dev);
1270 //todo error - not mounted.
1271 yaffsfs_SetError(-EINVAL);
1274 // todo error - no device
1275 yaffsfs_SetError(-ENODEV);
1282 int yaffs_unmount(const YCHAR *path)
1285 yaffs_Device *dev=NULL;
1289 dev = yaffsfs_FindDevice(path,&dummy);
1295 yaffs_FlushEntireDeviceCache(dev);
1296 yaffs_CheckpointSave(dev);
1298 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1299 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1300 inUse = 1; // the device is in use, can't unmount
1304 yaffs_Deinitialise(dev);
1308 // todo error can't unmount as files are open
1309 yaffsfs_SetError(-EBUSY);
1312 //todo error - not mounted.
1313 yaffsfs_SetError(-EINVAL);
1317 // todo error - no device
1318 yaffsfs_SetError(-ENODEV);
1325 loff_t yaffs_freespace(const YCHAR *path)
1328 yaffs_Device *dev=NULL;
1332 dev = yaffsfs_FindDevice(path,&dummy);
1333 if(dev && dev->isMounted){
1334 retVal = yaffs_GetNumberOfFreeChunks(dev);
1335 retVal *= dev->nDataBytesPerChunk;
1338 yaffsfs_SetError(-EINVAL);
1344 loff_t yaffs_totalspace(const YCHAR *path)
1347 yaffs_Device *dev=NULL;
1351 dev = yaffsfs_FindDevice(path,&dummy);
1352 if(dev && dev->isMounted){
1353 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1354 retVal *= dev->nChunksPerBlock;
1355 retVal *= dev->nDataBytesPerChunk;
1358 yaffsfs_SetError(-EINVAL);
1364 int yaffs_inodecount(const YCHAR *path)
1367 yaffs_Device *dev=NULL;
1371 dev = yaffsfs_FindDevice(path,&dummy);
1372 if(dev && dev->isMounted) {
1373 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1374 if(nObjects > dev->nHardLinks)
1375 retVal = nObjects - dev->nHardLinks;
1379 yaffsfs_SetError(-EINVAL);
1387 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1390 yaffsfs_DeviceConfiguration *cfg;
1392 yaffsfs_configurationList = cfgList;
1394 yaffsfs_InitHandles();
1396 cfg = yaffsfs_configurationList;
1398 while(cfg && cfg->prefix && cfg->dev){
1399 cfg->dev->isMounted = 0;
1400 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1409 // Directory search stuff.
1412 // Directory search context
1414 // NB this is an opaque structure.
1420 yaffs_dirent de; /* directory entry being used by this dsc */
1421 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1422 yaffs_Object *dirObj; /* ptr to directory being searched */
1423 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1425 struct ylist_head others;
1426 } yaffsfs_DirectorySearchContext;
1430 static struct ylist_head search_contexts;
1433 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1437 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1441 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1442 dsc->nextReturn = NULL;
1444 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1445 yaffs_Object,siblings);
1447 /* Hey someone isn't playing nice! */
1451 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1455 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1457 if( dsc->nextReturn == NULL ||
1458 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1459 dsc->nextReturn = NULL;
1461 struct ylist_head *next = dsc->nextReturn->siblings.next;
1463 if( next == &dsc->dirObj->variant.directoryVariant.children)
1464 dsc->nextReturn = NULL; /* end of list */
1466 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1469 /* Hey someone isn't playing nice! */
1473 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1476 struct ylist_head *i;
1477 yaffsfs_DirectorySearchContext *dsc;
1479 /* if search contexts not initilised then skip */
1480 if(!search_contexts.next)
1483 /* Iterate through the directory search contexts.
1484 * If any are the one being removed, then advance the dsc to
1485 * the next one to prevent a hanging ptr.
1487 ylist_for_each(i, &search_contexts) {
1489 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1490 if(dsc->nextReturn == obj)
1491 yaffsfs_DirAdvance(dsc);
1497 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1499 yaffs_DIR *dir = NULL;
1500 yaffs_Object *obj = NULL;
1501 yaffsfs_DirectorySearchContext *dsc = NULL;
1505 obj = yaffsfs_FindObject(NULL,dirname,0);
1507 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1509 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1510 dir = (yaffs_DIR *)dsc;
1513 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1514 dsc->magic = YAFFS_MAGIC;
1516 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1517 YINIT_LIST_HEAD(&dsc->others);
1519 if(!search_contexts.next)
1520 YINIT_LIST_HEAD(&search_contexts);
1522 ylist_add(&dsc->others,&search_contexts);
1523 yaffsfs_SetDirRewound(dsc);
1533 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1535 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1536 struct yaffs_dirent *retVal = NULL;
1540 if(dsc && dsc->magic == YAFFS_MAGIC){
1541 yaffsfs_SetError(0);
1542 if(dsc->nextReturn){
1543 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1544 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1545 dsc->de.d_off = dsc->offset++;
1546 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1547 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1549 // this should not happen!
1550 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1552 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1554 yaffsfs_DirAdvance(dsc);
1558 yaffsfs_SetError(-EBADF);
1567 void yaffs_rewinddir(yaffs_DIR *dirp)
1569 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1573 yaffsfs_SetDirRewound(dsc);
1579 int yaffs_closedir(yaffs_DIR *dirp)
1581 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1585 ylist_del(&dsc->others); /* unhook from list */
1591 // end of directory stuff
1594 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1596 yaffs_Object *parent = NULL;
1600 int mode = 0; // ignore for now
1603 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1605 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1609 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1613 yaffsfs_SetError(-EINVAL);
1623 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1625 yaffs_Object *obj = NULL;
1631 obj = yaffsfs_FindObject(NULL,path,0);
1634 yaffsfs_SetError(-ENOENT);
1636 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1637 yaffsfs_SetError(-EINVAL);
1640 YCHAR *alias = obj->variant.symLinkVariant.alias;
1641 memset(buf,0,bufsiz);
1642 yaffs_strncpy(buf,alias,bufsiz - 1);
1649 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1651 // Creates a link called newpath to existing oldpath
1652 yaffs_Object *obj = NULL;
1653 yaffs_Object *target = NULL;
1655 int newNameLength = 0;
1660 obj = yaffsfs_FindObject(NULL,oldpath,0);
1661 target = yaffsfs_FindObject(NULL,newpath,0);
1664 yaffsfs_SetError(-ENOENT);
1667 yaffsfs_SetError(-EEXIST);
1670 yaffs_Object *newdir = NULL;
1671 yaffs_Object *link = NULL;
1675 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1678 yaffsfs_SetError(-ENOTDIR);
1680 }else if(newdir->myDev != obj->myDev){
1681 yaffsfs_SetError(-EXDEV);
1685 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1687 if(newNameLength == 0){
1688 yaffsfs_SetError(-ENOENT);
1690 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1691 yaffsfs_SetError(-ENAMETOOLONG);
1696 link = yaffs_Link(newdir,newname,obj);
1700 yaffsfs_SetError(-ENOSPC);
1711 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1716 int yaffs_DumpDevStruct(const YCHAR *path)
1721 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1724 yaffs_Device *dev = obj->myDev;
1727 "nPageWrites.......... %d\n"
1728 "nPageReads........... %d\n"
1729 "nBlockErasures....... %d\n"
1730 "nGCCopies............ %d\n"
1731 "garbageCollections... %d\n"
1732 "passiveGarbageColl'ns %d\n"
1736 dev->nBlockErasures,
1738 dev->garbageCollections,
1739 dev->passiveGarbageCollections