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.20 2008-07-02 20:17:41 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;
46 __u8 inUse:1; // this handle is in use
47 __u8 readOnly:1; // this handle is read only
48 __u8 append:1; // append only
49 __u8 exclusive:1; // exclusive
50 __u32 position; // current position in file
51 yaffs_Object *obj; // the object
55 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
58 /// Inilitalise handles on start-up.
60 static int yaffsfs_InitHandles(void)
63 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
65 yaffsfs_handle[i].inUse = 0;
66 yaffsfs_handle[i].obj = NULL;
71 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
73 if(h < 0 || h >= YAFFSFS_N_HANDLES)
78 return &yaffsfs_handle[h];
81 yaffs_Object *yaffsfs_GetHandleObject(int handle)
83 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
95 // Grab a handle (when opening a file)
98 static int yaffsfs_GetHandle(void)
103 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
105 h = yaffsfs_GetHandlePointer(i);
108 // todo bug: should never happen
112 memset(h,0,sizeof(yaffsfs_Handle));
121 // Let go of a handle (when closing a file)
123 static int yaffsfs_PutHandle(int handle)
125 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
137 // Stuff to search for a directory from a path
140 int yaffsfs_Match(YCHAR a, YCHAR b)
146 int yaffsfs_IsPathDivider(YCHAR ch)
148 YCHAR *str = YAFFS_PATH_DIVIDERS;
159 // yaffsfs_FindDevice
161 // Scan the configuration list to find the root.
162 // Curveballs: Should match paths that end in '/' too
163 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
164 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
166 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
167 const YCHAR *leftOver;
169 yaffs_Device *retval = NULL;
171 int longestMatch = -1;
173 // Check all configs, choose the one that:
174 // 1) Actually matches a prefix (ie /a amd /abc will not match
175 // 2) Matches the longest.
176 while(cfg && cfg->prefix && cfg->dev)
183 // Strip off any leading /'s
185 while(yaffsfs_IsPathDivider(*p))
188 while(yaffsfs_IsPathDivider(*leftOver))
191 while(*p && *leftOver &&
192 yaffsfs_Match(*p,*leftOver))
198 // Skip over any multiple /'s to treat them as one or
199 // skip over a trailling / in the prefix, but not the matching string
200 while(yaffsfs_IsPathDivider(*p) &&
201 (yaffsfs_IsPathDivider(*(p+1)) || !(*(p+1))))
204 // Only skip over multiple /'s
205 while(yaffsfs_IsPathDivider(*leftOver) &&
206 yaffsfs_IsPathDivider(*(leftOver+1)))
212 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
213 (thisMatchLength > longestMatch))
216 *restOfPath = (YCHAR *)leftOver;
218 longestMatch = thisMatchLength;
226 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
228 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
229 const YCHAR *leftOver;
231 yaffs_Device *retval = NULL;
233 int longestMatch = -1;
235 // Check all configs, choose the one that:
236 // 1) Actually matches a prefix (ie /a amd /abc will not match
237 // 2) Matches the longest.
238 while(cfg && cfg->prefix && cfg->dev)
244 while(*p && //unmatched part of prefix
245 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
247 yaffsfs_Match(*p,*leftOver))
255 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
256 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
257 (thisMatchLength > longestMatch))
260 *restOfPath = (YCHAR *)leftOver;
262 longestMatch = thisMatchLength;
270 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
275 dev= yaffsfs_FindDevice(path,restOfPath);
276 if(dev && dev->isMounted)
283 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
286 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
288 YCHAR *alias = obj->variant.symLinkVariant.alias;
290 if(yaffsfs_IsPathDivider(*alias))
292 // Starts with a /, need to scan from root up
293 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
297 // Relative to here, so use the parent of the symlink as a start
298 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
305 // yaffsfs_FindDirectory
306 // Parse a path to determine the directory and the name within the directory.
308 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
309 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
313 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
316 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
324 restOfPath = (YCHAR *)path;
328 dir = yaffsfs_FindRoot(path,&restOfPath);
334 // curve ball: also throw away surplus '/'
335 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
336 while(yaffsfs_IsPathDivider(*restOfPath))
338 restOfPath++; // get rid of '/'
344 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath))
346 if (i < YAFFS_MAX_NAME_LENGTH)
348 str[i] = *restOfPath;
357 // got to the end of the string
362 if(yaffs_strcmp(str,_Y(".")) == 0)
366 else if(yaffs_strcmp(str,_Y("..")) == 0)
372 dir = yaffs_FindObjectByName(dir,str);
374 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
377 dir = yaffsfs_FollowLink(dir,symDepth);
381 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
388 // directory did not exist.
392 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
394 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
397 // yaffsfs_FindObject turns a path for an existing object into the object
399 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
404 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
408 return yaffs_FindObjectByName(dir,name);
415 int yaffs_dup(int fd)
418 yaffsfs_Handle *oldPtr = NULL;
419 yaffsfs_Handle *newPtr = NULL;
423 oldPtr = yaffsfs_GetHandlePointer(fd);
424 if(oldPtr && oldPtr->inUse)
425 newHandle = yaffsfs_GetHandle();
427 newPtr = yaffsfs_GetHandlePointer(newHandle);
435 yaffsfs_SetError(-EBADF);
437 yaffsfs_SetError(-ENOMEM);
443 int yaffs_open(const YCHAR *path, int oflag, int mode)
445 yaffs_Object *obj = NULL;
446 yaffs_Object *dir = NULL;
449 yaffsfs_Handle *h = NULL;
451 int alreadyExclusive = 0;
454 int errorReported = 0;
459 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
464 handle = yaffsfs_GetHandle();
469 h = yaffsfs_GetHandlePointer(handle);
472 // try to find the exisiting object
473 obj = yaffsfs_FindObject(NULL,path,0);
475 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
478 obj = yaffsfs_FollowLink(obj,symDepth++);
481 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
488 // Check if the object is already in use
489 alreadyOpen = alreadyExclusive = 0;
491 for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
495 yaffsfs_handle[i].inUse &&
496 obj == yaffsfs_handle[i].obj)
499 if(yaffsfs_handle[i].exclusive)
501 alreadyExclusive = 1;
506 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
511 // Open should fail if O_CREAT and O_EXCL are specified
512 if((oflag & O_EXCL) && (oflag & O_CREAT))
515 yaffsfs_SetError(-EEXIST);
519 // Check file permissions
520 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
521 !(obj->yst_mode & S_IREAD))
526 if( (oflag & O_RDWR) &&
527 !(obj->yst_mode & S_IREAD))
532 if( (oflag & (O_RDWR | O_WRONLY)) &&
533 !(obj->yst_mode & S_IWRITE))
540 else if((oflag & O_CREAT))
542 // Let's see if we can create this file
543 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
546 obj = yaffs_MknodFile(dir,name,mode,0,0);
550 yaffsfs_SetError(-ENOTDIR);
555 if(obj && !openDenied)
559 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
560 h->append = (oflag & O_APPEND) ? 1 : 0;
561 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
565 if((oflag & O_TRUNC) && !h->readOnly)
567 yaffs_ResizeFile(obj,0);
573 yaffsfs_PutHandle(handle);
576 yaffsfs_SetError(-EACCES);
589 int yaffs_flush(int fd)
591 yaffsfs_Handle *h = NULL;
596 h = yaffsfs_GetHandlePointer(fd);
601 yaffs_FlushFile(h->obj,1);
606 yaffsfs_SetError(-EBADF);
616 int yaffs_close(int fd)
618 yaffsfs_Handle *h = NULL;
623 h = yaffsfs_GetHandlePointer(fd);
628 yaffs_FlushFile(h->obj,1);
630 if(h->obj->inUse <= 0 && h->obj->unlinked)
632 yaffs_DeleteFile(h->obj);
634 yaffsfs_PutHandle(fd);
640 yaffsfs_SetError(-EBADF);
649 int yaffs_read(int fd, void *buf, unsigned int nbyte)
651 yaffsfs_Handle *h = NULL;
652 yaffs_Object *obj = NULL;
655 unsigned int maxRead;
658 h = yaffsfs_GetHandlePointer(fd);
659 obj = yaffsfs_GetHandleObject(fd);
664 yaffsfs_SetError(-EBADF);
669 if(yaffs_GetObjectFileLength(obj) > pos)
671 maxRead = yaffs_GetObjectFileLength(obj) - pos;
686 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
689 h->position = pos + nRead;
706 return (nRead >= 0) ? nRead : -1;
710 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
712 yaffsfs_Handle *h = NULL;
713 yaffs_Object *obj = NULL;
716 unsigned int maxRead;
719 h = yaffsfs_GetHandlePointer(fd);
720 obj = yaffsfs_GetHandleObject(fd);
725 yaffsfs_SetError(-EBADF);
730 if(yaffs_GetObjectFileLength(obj) > pos)
732 maxRead = yaffs_GetObjectFileLength(obj) - pos;
747 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
759 return (nRead >= 0) ? nRead : -1;
763 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
765 yaffsfs_Handle *h = NULL;
766 yaffs_Object *obj = NULL;
769 int writeThrough = 0;
772 h = yaffsfs_GetHandlePointer(fd);
773 obj = yaffsfs_GetHandleObject(fd);
778 yaffsfs_SetError(-EBADF);
780 else if( h && obj && h->readOnly)
788 pos = yaffs_GetObjectFileLength(obj);
795 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
799 h->position = pos + nWritten;
811 return (nWritten >= 0) ? nWritten : -1;
815 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
817 yaffsfs_Handle *h = NULL;
818 yaffs_Object *obj = NULL;
821 int writeThrough = 0;
824 h = yaffsfs_GetHandlePointer(fd);
825 obj = yaffsfs_GetHandleObject(fd);
830 yaffsfs_SetError(-EBADF);
832 else if( h && obj && h->readOnly)
840 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
842 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
843 yaffsfs_SetError(-ENOSPC);
850 return (nWritten >= 0) ? nWritten : -1;
855 int yaffs_truncate(const YCHAR *path,off_t newSize)
857 yaffs_Object *obj = NULL;
858 int result = YAFFS_FAIL;
862 obj = yaffsfs_FindObject(NULL,path,0);
864 obj = yaffs_GetEquivalentObject(obj);
868 yaffsfs_SetError(-ENOENT);
870 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
872 yaffsfs_SetError(-EISDIR);
876 result = yaffs_ResizeFile(obj,newSize);
882 return (result) ? 0 : -1;
885 int yaffs_ftruncate(int fd, off_t newSize)
887 yaffsfs_Handle *h = NULL;
888 yaffs_Object *obj = NULL;
892 h = yaffsfs_GetHandlePointer(fd);
893 obj = yaffsfs_GetHandleObject(fd);
898 yaffsfs_SetError(-EBADF);
903 result = yaffs_ResizeFile(obj,newSize);
908 return (result) ? 0 : -1;
912 off_t yaffs_lseek(int fd, off_t offset, int whence)
914 yaffsfs_Handle *h = NULL;
915 yaffs_Object *obj = NULL;
920 h = yaffsfs_GetHandlePointer(fd);
921 obj = yaffsfs_GetHandleObject(fd);
926 yaffsfs_SetError(-EBADF);
928 else if(whence == SEEK_SET)
935 else if(whence == SEEK_CUR)
937 if( (h->position + offset) >= 0)
939 pos = (h->position + offset);
942 else if(whence == SEEK_END)
944 fSize = yaffs_GetObjectFileLength(obj);
945 if(fSize >= 0 && (fSize + offset) >= 0)
947 pos = fSize + offset;
967 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
969 yaffs_Object *dir = NULL;
970 yaffs_Object *obj = NULL;
972 int result = YAFFS_FAIL;
976 obj = yaffsfs_FindObject(NULL,path,0);
977 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
980 yaffsfs_SetError(-ENOTDIR);
984 yaffsfs_SetError(-ENOENT);
986 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
988 yaffsfs_SetError(-EISDIR);
990 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
992 yaffsfs_SetError(-ENOTDIR);
996 result = yaffs_Unlink(dir,name);
998 if(result == YAFFS_FAIL && isDirectory)
1000 yaffsfs_SetError(-ENOTEMPTY);
1008 return (result == YAFFS_FAIL) ? -1 : 0;
1012 int yaffs_rmdir(const YCHAR *path)
1014 return yaffsfs_DoUnlink(path,1);
1017 int yaffs_unlink(const YCHAR *path)
1019 return yaffsfs_DoUnlink(path,0);
1022 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1024 yaffs_Object *olddir = NULL;
1025 yaffs_Object *newdir = NULL;
1026 yaffs_Object *obj = NULL;
1029 int result= YAFFS_FAIL;
1030 int renameAllowed = 1;
1034 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1035 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1036 obj = yaffsfs_FindObject(NULL,oldPath,0);
1038 if(!olddir || !newdir || !obj)
1041 yaffsfs_SetError(-EBADF);
1044 else if(olddir->myDev != newdir->myDev)
1046 // oops must be on same device
1048 yaffsfs_SetError(-EXDEV);
1051 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1053 // It is a directory, check that it is not being renamed to
1054 // being its own decendent.
1055 // Do this by tracing from the new directory back to the root, checking for obj
1057 yaffs_Object *xx = newdir;
1059 while( renameAllowed && xx)
1067 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1072 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1077 return (result == YAFFS_FAIL) ? -1 : 0;
1081 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1087 obj = yaffs_GetEquivalentObject(obj);
1092 buf->st_dev = (int)obj->myDev->genericDevice;
1093 buf->st_ino = obj->objectId;
1094 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1096 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1098 buf->st_mode |= S_IFDIR;
1100 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1102 buf->st_mode |= S_IFLNK;
1104 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1106 buf->st_mode |= S_IFREG;
1109 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1112 buf->st_rdev = obj->yst_rdev;
1113 buf->st_size = yaffs_GetObjectFileLength(obj);
1114 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1115 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1116 #if CONFIG_YAFFS_WINCE
1117 buf->yst_wince_atime[0] = obj->win_atime[0];
1118 buf->yst_wince_atime[1] = obj->win_atime[1];
1119 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1120 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1121 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1122 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1124 buf->yst_atime = obj->yst_atime;
1125 buf->yst_ctime = obj->yst_ctime;
1126 buf->yst_mtime = obj->yst_mtime;
1133 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1140 obj = yaffsfs_FindObject(NULL,path,0);
1144 obj = yaffsfs_FollowLink(obj,0);
1149 retVal = yaffsfs_DoStat(obj,buf);
1153 // todo error not found
1154 yaffsfs_SetError(-ENOENT);
1163 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1165 return yaffsfs_DoStatOrLStat(path,buf,0);
1168 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1170 return yaffsfs_DoStatOrLStat(path,buf,1);
1173 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1180 obj = yaffsfs_GetHandleObject(fd);
1184 retVal = yaffsfs_DoStat(obj,buf);
1189 yaffsfs_SetError(-EBADF);
1197 #ifdef CONFIG_YAFFS_WINCE
1198 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1205 obj = yaffsfs_GetHandleObject(fd);
1211 wctime[0] = obj->win_ctime[0];
1212 wctime[1] = obj->win_ctime[1];
1215 watime[0] = obj->win_atime[0];
1216 watime[1] = obj->win_atime[1];
1219 wmtime[0] = obj->win_mtime[0];
1220 wmtime[1] = obj->win_mtime[1];
1229 yaffsfs_SetError(-EBADF);
1238 int yaffs_set_wince_times(int fd,
1239 const unsigned *wctime,
1240 const unsigned *watime,
1241 const unsigned *wmtime)
1248 obj = yaffsfs_GetHandleObject(fd);
1254 obj->win_ctime[0] = wctime[0];
1255 obj->win_ctime[1] = wctime[1];
1258 obj->win_atime[0] = watime[0];
1259 obj->win_atime[1] = watime[1];
1262 obj->win_mtime[0] = wmtime[0];
1263 obj->win_mtime[1] = wmtime[1];
1267 result = yaffs_FlushFile(obj,0);
1273 yaffsfs_SetError(-EBADF);
1284 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1290 obj = yaffs_GetEquivalentObject(obj);
1295 obj->yst_mode = mode;
1297 result = yaffs_FlushFile(obj,0);
1300 return result == YAFFS_OK ? 0 : -1;
1304 int yaffs_access(const YCHAR *path, int amode)
1311 obj = yaffsfs_FindObject(NULL,path,0);
1317 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1319 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1321 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1325 yaffsfs_SetError(-EACCES);
1331 // todo error not found
1332 yaffsfs_SetError(-ENOENT);
1343 int yaffs_chmod(const YCHAR *path, mode_t mode)
1350 obj = yaffsfs_FindObject(NULL,path,0);
1354 retVal = yaffsfs_DoChMod(obj,mode);
1358 // todo error not found
1359 yaffsfs_SetError(-ENOENT);
1369 int yaffs_fchmod(int fd, mode_t mode)
1376 obj = yaffsfs_GetHandleObject(fd);
1380 retVal = yaffsfs_DoChMod(obj,mode);
1385 yaffsfs_SetError(-EBADF);
1394 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1396 yaffs_Object *parent = NULL;
1397 yaffs_Object *dir = NULL;
1402 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1404 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1412 yaffsfs_SetError(-ENOENT); // missing path
1414 else if (yaffs_FindObjectByName(parent,name)){
1415 yaffsfs_SetError(-EEXIST); // the name already exists
1418 yaffsfs_SetError(-ENOSPC); // just assume no space
1427 int yaffs_mount(const YCHAR *path)
1430 int result=YAFFS_FAIL;
1431 yaffs_Device *dev=NULL;
1434 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1437 dev = yaffsfs_FindDevice(path,&dummy);
1442 result = yaffs_GutsInitialise(dev);
1443 if(result == YAFFS_FAIL)
1445 // todo error - mount failed
1446 yaffsfs_SetError(-ENOMEM);
1448 retVal = result ? 0 : -1;
1453 //todo error - already mounted.
1454 yaffsfs_SetError(-EBUSY);
1459 // todo error - no device
1460 yaffsfs_SetError(-ENODEV);
1467 int yaffs_sync(const YCHAR *path)
1470 yaffs_Device *dev=NULL;
1474 dev = yaffsfs_FindDevice(path,&dummy);
1480 yaffs_FlushEntireDeviceCache(dev);
1481 yaffs_CheckpointSave(dev);
1487 //todo error - not mounted.
1488 yaffsfs_SetError(-EINVAL);
1494 // todo error - no device
1495 yaffsfs_SetError(-ENODEV);
1502 int yaffs_unmount(const YCHAR *path)
1505 yaffs_Device *dev=NULL;
1509 dev = yaffsfs_FindDevice(path,&dummy);
1517 yaffs_FlushEntireDeviceCache(dev);
1518 yaffs_CheckpointSave(dev);
1520 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1522 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1524 inUse = 1; // the device is in use, can't unmount
1530 yaffs_Deinitialise(dev);
1536 // todo error can't unmount as files are open
1537 yaffsfs_SetError(-EBUSY);
1543 //todo error - not mounted.
1544 yaffsfs_SetError(-EINVAL);
1550 // todo error - no device
1551 yaffsfs_SetError(-ENODEV);
1558 loff_t yaffs_freespace(const YCHAR *path)
1561 yaffs_Device *dev=NULL;
1565 dev = yaffsfs_FindDevice(path,&dummy);
1566 if(dev && dev->isMounted)
1568 retVal = yaffs_GetNumberOfFreeChunks(dev);
1569 retVal *= dev->nDataBytesPerChunk;
1574 yaffsfs_SetError(-EINVAL);
1581 loff_t yaffs_totalspace(const YCHAR *path)
1584 yaffs_Device *dev=NULL;
1588 dev = yaffsfs_FindDevice(path,&dummy);
1589 if(dev && dev->isMounted)
1591 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1592 retVal *= dev->nChunksPerBlock;
1593 retVal *= dev->nDataBytesPerChunk;
1598 yaffsfs_SetError(-EINVAL);
1607 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1610 yaffsfs_DeviceConfiguration *cfg;
1612 yaffsfs_configurationList = cfgList;
1614 yaffsfs_InitHandles();
1616 cfg = yaffsfs_configurationList;
1618 while(cfg && cfg->prefix && cfg->dev)
1620 cfg->dev->isMounted = 0;
1621 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1630 // Directory search stuff.
1633 // Directory search context
1635 // NB this is an opaque structure.
1641 yaffs_dirent de; /* directory entry being used by this dsc */
1642 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1643 yaffs_Object *dirObj; /* ptr to directory being searched */
1644 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1646 struct ylist_head others;
1647 } yaffsfs_DirectorySearchContext;
1651 static struct ylist_head search_contexts;
1654 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1658 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1662 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1663 dsc->nextReturn = NULL;
1665 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1666 yaffs_Object,siblings);
1669 /* Hey someone isn't playing nice! */
1673 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1677 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1679 if( dsc->nextReturn == NULL ||
1680 ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1681 dsc->nextReturn = NULL;
1683 struct ylist_head *next = dsc->nextReturn->siblings.next;
1685 if( next == &dsc->dirObj->variant.directoryVariant.children)
1686 dsc->nextReturn = NULL; /* end of list */
1688 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1691 /* Hey someone isn't playing nice! */
1695 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1698 struct ylist_head *i;
1699 yaffsfs_DirectorySearchContext *dsc;
1701 /* if search contexts not initilised then skip */
1702 if(!search_contexts.next)
1705 /* Iterate through the directory search contexts.
1706 * If any are the one being removed, then advance the dsc to
1707 * the next one to prevent a hanging ptr.
1709 ylist_for_each(i, &search_contexts) {
1711 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1712 if(dsc->nextReturn == obj)
1713 yaffsfs_DirAdvance(dsc);
1719 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1721 yaffs_DIR *dir = NULL;
1722 yaffs_Object *obj = NULL;
1723 yaffsfs_DirectorySearchContext *dsc = NULL;
1727 obj = yaffsfs_FindObject(NULL,dirname,0);
1729 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1732 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1733 dir = (yaffs_DIR *)dsc;
1736 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1737 dsc->magic = YAFFS_MAGIC;
1739 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1740 YINIT_LIST_HEAD(&dsc->others);
1742 if(!search_contexts.next)
1743 YINIT_LIST_HEAD(&search_contexts);
1745 ylist_add(&dsc->others,&search_contexts);
1746 yaffsfs_SetDirRewound(dsc); }
1755 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1757 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1758 struct yaffs_dirent *retVal = NULL;
1762 if(dsc && dsc->magic == YAFFS_MAGIC){
1763 yaffsfs_SetError(0);
1764 if(dsc->nextReturn){
1765 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1766 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1767 dsc->de.d_off = dsc->offset++;
1768 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1769 if(yaffs_strlen(dsc->de.d_name) == 0)
1771 // this should not happen!
1772 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1774 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1776 yaffsfs_DirAdvance(dsc);
1782 yaffsfs_SetError(-EBADF);
1792 void yaffs_rewinddir(yaffs_DIR *dirp)
1794 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1798 yaffsfs_SetDirRewound(dsc);
1804 int yaffs_closedir(yaffs_DIR *dirp)
1806 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1810 ylist_del(&dsc->others); /* unhook from list */
1816 // end of directory stuff
1819 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1821 yaffs_Object *parent = NULL;
1825 int mode = 0; // ignore for now
1828 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1829 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1836 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1846 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1848 yaffs_Object *obj = NULL;
1854 obj = yaffsfs_FindObject(NULL,path,0);
1858 yaffsfs_SetError(-ENOENT);
1861 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1863 yaffsfs_SetError(-EINVAL);
1868 YCHAR *alias = obj->variant.symLinkVariant.alias;
1869 memset(buf,0,bufsiz);
1870 yaffs_strncpy(buf,alias,bufsiz - 1);
1877 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1879 // Creates a link called newpath to existing oldpath
1880 yaffs_Object *obj = NULL;
1881 yaffs_Object *target = NULL;
1887 obj = yaffsfs_FindObject(NULL,oldpath,0);
1888 target = yaffsfs_FindObject(NULL,newpath,0);
1892 yaffsfs_SetError(-ENOENT);
1897 yaffsfs_SetError(-EEXIST);
1902 yaffs_Object *newdir = NULL;
1903 yaffs_Object *link = NULL;
1907 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1911 yaffsfs_SetError(-ENOTDIR);
1914 else if(newdir->myDev != obj->myDev)
1916 yaffsfs_SetError(-EXDEV);
1919 if(newdir && yaffs_strlen(newname) > 0)
1921 link = yaffs_Link(newdir,newname,obj);
1926 yaffsfs_SetError(-ENOSPC);
1937 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev);
1939 int yaffs_DumpDevStruct(const YCHAR *path)
1944 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1948 yaffs_Device *dev = obj->myDev;
1951 "nPageWrites.......... %d\n"
1952 "nPageReads........... %d\n"
1953 "nBlockErasures....... %d\n"
1954 "nGCCopies............ %d\n"
1955 "garbageCollections... %d\n"
1956 "passiveGarbageColl'ns %d\n"
1960 dev->nBlockErasures,
1962 dev->garbageCollections,
1963 dev->passiveGarbageCollections