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.19 2008-05-05 07:58:58 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)))
211 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
212 (thisMatchLength > longestMatch))
215 *restOfPath = (YCHAR *)leftOver;
217 longestMatch = thisMatchLength;
224 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
226 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
227 const YCHAR *leftOver;
229 yaffs_Device *retval = NULL;
231 int longestMatch = -1;
233 // Check all configs, choose the one that:
234 // 1) Actually matches a prefix (ie /a amd /abc will not match
235 // 2) Matches the longest.
236 while(cfg && cfg->prefix && cfg->dev)
242 while(*p && //unmatched part of prefix
243 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
245 yaffsfs_Match(*p,*leftOver))
253 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
254 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
255 (thisMatchLength > longestMatch))
258 *restOfPath = (YCHAR *)leftOver;
260 longestMatch = thisMatchLength;
268 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
273 dev= yaffsfs_FindDevice(path,restOfPath);
274 if(dev && dev->isMounted)
281 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
284 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
286 YCHAR *alias = obj->variant.symLinkVariant.alias;
288 if(yaffsfs_IsPathDivider(*alias))
290 // Starts with a /, need to scan from root up
291 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
295 // Relative to here, so use the parent of the symlink as a start
296 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
303 // yaffsfs_FindDirectory
304 // Parse a path to determine the directory and the name within the directory.
306 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
307 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
311 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
314 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
322 restOfPath = (YCHAR *)path;
326 dir = yaffsfs_FindRoot(path,&restOfPath);
332 // curve ball: also throw away surplus '/'
333 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
334 while(yaffsfs_IsPathDivider(*restOfPath))
336 restOfPath++; // get rid of '/'
342 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath))
344 if (i < YAFFS_MAX_NAME_LENGTH)
346 str[i] = *restOfPath;
355 // got to the end of the string
360 if(yaffs_strcmp(str,_Y(".")) == 0)
364 else if(yaffs_strcmp(str,_Y("..")) == 0)
370 dir = yaffs_FindObjectByName(dir,str);
372 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
375 dir = yaffsfs_FollowLink(dir,symDepth);
379 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
386 // directory did not exist.
390 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
392 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
395 // yaffsfs_FindObject turns a path for an existing object into the object
397 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
402 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
406 return yaffs_FindObjectByName(dir,name);
413 int yaffs_dup(int fd)
416 yaffsfs_Handle *oldPtr = NULL;
417 yaffsfs_Handle *newPtr = NULL;
421 oldPtr = yaffsfs_GetHandlePointer(fd);
422 if(oldPtr && oldPtr->inUse)
423 newHandle = yaffsfs_GetHandle();
425 newPtr = yaffsfs_GetHandlePointer(newHandle);
433 yaffsfs_SetError(-EBADF);
435 yaffsfs_SetError(-ENOMEM);
441 int yaffs_open(const YCHAR *path, int oflag, int mode)
443 yaffs_Object *obj = NULL;
444 yaffs_Object *dir = NULL;
447 yaffsfs_Handle *h = NULL;
449 int alreadyExclusive = 0;
452 int errorReported = 0;
457 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
462 handle = yaffsfs_GetHandle();
467 h = yaffsfs_GetHandlePointer(handle);
470 // try to find the exisiting object
471 obj = yaffsfs_FindObject(NULL,path,0);
473 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
476 obj = yaffsfs_FollowLink(obj,symDepth++);
479 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
486 // Check if the object is already in use
487 alreadyOpen = alreadyExclusive = 0;
489 for(i = 0; i <= YAFFSFS_N_HANDLES; i++)
493 yaffsfs_handle[i].inUse &&
494 obj == yaffsfs_handle[i].obj)
497 if(yaffsfs_handle[i].exclusive)
499 alreadyExclusive = 1;
504 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
509 // Open should fail if O_CREAT and O_EXCL are specified
510 if((oflag & O_EXCL) && (oflag & O_CREAT))
513 yaffsfs_SetError(-EEXIST);
517 // Check file permissions
518 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
519 !(obj->yst_mode & S_IREAD))
524 if( (oflag & O_RDWR) &&
525 !(obj->yst_mode & S_IREAD))
530 if( (oflag & (O_RDWR | O_WRONLY)) &&
531 !(obj->yst_mode & S_IWRITE))
538 else if((oflag & O_CREAT))
540 // Let's see if we can create this file
541 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
544 obj = yaffs_MknodFile(dir,name,mode,0,0);
548 yaffsfs_SetError(-ENOTDIR);
553 if(obj && !openDenied)
557 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
558 h->append = (oflag & O_APPEND) ? 1 : 0;
559 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
563 if((oflag & O_TRUNC) && !h->readOnly)
566 yaffs_ResizeFile(obj,0);
572 yaffsfs_PutHandle(handle);
575 yaffsfs_SetError(-EACCES);
588 int yaffs_flush(int fd)
590 yaffsfs_Handle *h = NULL;
595 h = yaffsfs_GetHandlePointer(fd);
600 yaffs_FlushFile(h->obj,1);
605 yaffsfs_SetError(-EBADF);
614 int yaffs_close(int fd)
616 yaffsfs_Handle *h = NULL;
621 h = yaffsfs_GetHandlePointer(fd);
626 yaffs_FlushFile(h->obj,1);
628 if(h->obj->inUse <= 0 && h->obj->unlinked)
630 yaffs_DeleteFile(h->obj);
632 yaffsfs_PutHandle(fd);
638 yaffsfs_SetError(-EBADF);
647 int yaffs_read(int fd, void *buf, unsigned int nbyte)
649 yaffsfs_Handle *h = NULL;
650 yaffs_Object *obj = NULL;
653 unsigned int maxRead;
656 h = yaffsfs_GetHandlePointer(fd);
657 obj = yaffsfs_GetHandleObject(fd);
662 yaffsfs_SetError(-EBADF);
667 if(yaffs_GetObjectFileLength(obj) > pos)
669 maxRead = yaffs_GetObjectFileLength(obj) - pos;
684 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
687 h->position = pos + nRead;
704 return (nRead >= 0) ? nRead : -1;
708 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
710 yaffsfs_Handle *h = NULL;
711 yaffs_Object *obj = NULL;
714 unsigned int maxRead;
717 h = yaffsfs_GetHandlePointer(fd);
718 obj = yaffsfs_GetHandleObject(fd);
723 yaffsfs_SetError(-EBADF);
728 if(yaffs_GetObjectFileLength(obj) > pos)
730 maxRead = yaffs_GetObjectFileLength(obj) - pos;
745 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
757 return (nRead >= 0) ? nRead : -1;
761 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
763 yaffsfs_Handle *h = NULL;
764 yaffs_Object *obj = NULL;
767 int writeThrough = 0;
770 h = yaffsfs_GetHandlePointer(fd);
771 obj = yaffsfs_GetHandleObject(fd);
776 yaffsfs_SetError(-EBADF);
778 else if( h && obj && h->readOnly)
786 pos = yaffs_GetObjectFileLength(obj);
793 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
797 h->position = pos + nWritten;
809 return (nWritten >= 0) ? nWritten : -1;
813 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
815 yaffsfs_Handle *h = NULL;
816 yaffs_Object *obj = NULL;
819 int writeThrough = 0;
822 h = yaffsfs_GetHandlePointer(fd);
823 obj = yaffsfs_GetHandleObject(fd);
828 yaffsfs_SetError(-EBADF);
830 else if( h && obj && h->readOnly)
838 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
841 yaffsfs_SetError(-ENOSPC);
848 return (nWritten >= 0) ? nWritten : -1;
853 int yaffs_truncate(const YCHAR *path,off_t newSize)
855 yaffs_Object *obj = NULL;
856 int result = YAFFS_FAIL;
860 obj = yaffsfs_FindObject(NULL,path,0);
862 obj = yaffs_GetEquivalentObject(obj);
866 yaffsfs_SetError(-ENOENT);
868 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
870 yaffsfs_SetError(-EISDIR);
874 result = yaffs_ResizeFile(obj,newSize);
880 return (result) ? 0 : -1;
883 int yaffs_ftruncate(int fd, off_t newSize)
885 yaffsfs_Handle *h = NULL;
886 yaffs_Object *obj = NULL;
890 h = yaffsfs_GetHandlePointer(fd);
891 obj = yaffsfs_GetHandleObject(fd);
896 yaffsfs_SetError(-EBADF);
901 result = yaffs_ResizeFile(obj,newSize);
906 return (result) ? 0 : -1;
910 off_t yaffs_lseek(int fd, off_t offset, int whence)
912 yaffsfs_Handle *h = NULL;
913 yaffs_Object *obj = NULL;
918 h = yaffsfs_GetHandlePointer(fd);
919 obj = yaffsfs_GetHandleObject(fd);
924 yaffsfs_SetError(-EBADF);
926 else if(whence == SEEK_SET)
933 else if(whence == SEEK_CUR)
935 if( (h->position + offset) >= 0)
937 pos = (h->position + offset);
940 else if(whence == SEEK_END)
942 fSize = yaffs_GetObjectFileLength(obj);
943 if(fSize >= 0 && (fSize + offset) >= 0)
945 pos = fSize + offset;
965 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
967 yaffs_Object *dir = NULL;
968 yaffs_Object *obj = NULL;
970 int result = YAFFS_FAIL;
974 obj = yaffsfs_FindObject(NULL,path,0);
975 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
978 yaffsfs_SetError(-ENOTDIR);
982 yaffsfs_SetError(-ENOENT);
984 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
986 yaffsfs_SetError(-EISDIR);
988 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
990 yaffsfs_SetError(-ENOTDIR);
994 result = yaffs_Unlink(dir,name);
996 if(result == YAFFS_FAIL && isDirectory)
998 yaffsfs_SetError(-ENOTEMPTY);
1006 return (result == YAFFS_FAIL) ? -1 : 0;
1010 int yaffs_rmdir(const YCHAR *path)
1012 return yaffsfs_DoUnlink(path,1);
1015 int yaffs_unlink(const YCHAR *path)
1017 return yaffsfs_DoUnlink(path,0);
1020 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1022 yaffs_Object *olddir = NULL;
1023 yaffs_Object *newdir = NULL;
1024 yaffs_Object *obj = NULL;
1027 int result= YAFFS_FAIL;
1028 int renameAllowed = 1;
1032 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1033 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1034 obj = yaffsfs_FindObject(NULL,oldPath,0);
1036 if(!olddir || !newdir || !obj)
1039 yaffsfs_SetError(-EBADF);
1042 else if(olddir->myDev != newdir->myDev)
1044 // oops must be on same device
1046 yaffsfs_SetError(-EXDEV);
1049 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1051 // It is a directory, check that it is not being renamed to
1052 // being its own decendent.
1053 // Do this by tracing from the new directory back to the root, checking for obj
1055 yaffs_Object *xx = newdir;
1057 while( renameAllowed && xx)
1065 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1070 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1075 return (result == YAFFS_FAIL) ? -1 : 0;
1079 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1085 obj = yaffs_GetEquivalentObject(obj);
1090 buf->st_dev = (int)obj->myDev->genericDevice;
1091 buf->st_ino = obj->objectId;
1092 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1094 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1096 buf->st_mode |= S_IFDIR;
1098 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1100 buf->st_mode |= S_IFLNK;
1102 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1104 buf->st_mode |= S_IFREG;
1107 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1110 buf->st_rdev = obj->yst_rdev;
1111 buf->st_size = yaffs_GetObjectFileLength(obj);
1112 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1113 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1114 #if CONFIG_YAFFS_WINCE
1115 buf->yst_wince_atime[0] = obj->win_atime[0];
1116 buf->yst_wince_atime[1] = obj->win_atime[1];
1117 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1118 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1119 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1120 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1122 buf->yst_atime = obj->yst_atime;
1123 buf->yst_ctime = obj->yst_ctime;
1124 buf->yst_mtime = obj->yst_mtime;
1131 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1138 obj = yaffsfs_FindObject(NULL,path,0);
1142 obj = yaffsfs_FollowLink(obj,0);
1147 retVal = yaffsfs_DoStat(obj,buf);
1151 // todo error not found
1152 yaffsfs_SetError(-ENOENT);
1161 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1163 return yaffsfs_DoStatOrLStat(path,buf,0);
1166 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1168 return yaffsfs_DoStatOrLStat(path,buf,1);
1171 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1178 obj = yaffsfs_GetHandleObject(fd);
1182 retVal = yaffsfs_DoStat(obj,buf);
1187 yaffsfs_SetError(-EBADF);
1195 #ifdef CONFIG_YAFFS_WINCE
1196 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1203 obj = yaffsfs_GetHandleObject(fd);
1209 wctime[0] = obj->win_ctime[0];
1210 wctime[1] = obj->win_ctime[1];
1213 watime[0] = obj->win_atime[0];
1214 watime[1] = obj->win_atime[1];
1217 wmtime[0] = obj->win_mtime[0];
1218 wmtime[1] = obj->win_mtime[1];
1227 yaffsfs_SetError(-EBADF);
1236 int yaffs_set_wince_times(int fd,
1237 const unsigned *wctime,
1238 const unsigned *watime,
1239 const unsigned *wmtime)
1246 obj = yaffsfs_GetHandleObject(fd);
1252 obj->win_ctime[0] = wctime[0];
1253 obj->win_ctime[1] = wctime[1];
1256 obj->win_atime[0] = watime[0];
1257 obj->win_atime[1] = watime[1];
1260 obj->win_mtime[0] = wmtime[0];
1261 obj->win_mtime[1] = wmtime[1];
1269 yaffsfs_SetError(-EBADF);
1280 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1286 obj = yaffs_GetEquivalentObject(obj);
1291 obj->yst_mode = mode;
1293 result = yaffs_FlushFile(obj,0);
1296 return result == YAFFS_OK ? 0 : -1;
1300 int yaffs_access(const YCHAR *path, int amode)
1307 obj = yaffsfs_FindObject(NULL,path,0);
1313 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1315 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1317 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1321 yaffsfs_SetError(-EACCES);
1327 // todo error not found
1328 yaffsfs_SetError(-ENOENT);
1339 int yaffs_chmod(const YCHAR *path, mode_t mode)
1346 obj = yaffsfs_FindObject(NULL,path,0);
1350 retVal = yaffsfs_DoChMod(obj,mode);
1354 // todo error not found
1355 yaffsfs_SetError(-ENOENT);
1365 int yaffs_fchmod(int fd, mode_t mode)
1372 obj = yaffsfs_GetHandleObject(fd);
1376 retVal = yaffsfs_DoChMod(obj,mode);
1381 yaffsfs_SetError(-EBADF);
1390 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1392 yaffs_Object *parent = NULL;
1393 yaffs_Object *dir = NULL;
1398 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1400 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1408 yaffsfs_SetError(-ENOENT); // missing path
1410 else if (yaffs_FindObjectByName(parent,name)){
1411 yaffsfs_SetError(-EEXIST); // the name already exists
1414 yaffsfs_SetError(-ENOSPC); // just assume no space
1423 int yaffs_mount(const YCHAR *path)
1426 int result=YAFFS_FAIL;
1427 yaffs_Device *dev=NULL;
1430 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1433 dev = yaffsfs_FindDevice(path,&dummy);
1438 result = yaffs_GutsInitialise(dev);
1439 if(result == YAFFS_FAIL)
1441 // todo error - mount failed
1442 yaffsfs_SetError(-ENOMEM);
1444 retVal = result ? 0 : -1;
1449 //todo error - already mounted.
1450 yaffsfs_SetError(-EBUSY);
1455 // todo error - no device
1456 yaffsfs_SetError(-ENODEV);
1463 int yaffs_sync(const YCHAR *path)
1466 yaffs_Device *dev=NULL;
1470 dev = yaffsfs_FindDevice(path,&dummy);
1476 yaffs_FlushEntireDeviceCache(dev);
1477 yaffs_CheckpointSave(dev);
1483 //todo error - not mounted.
1484 yaffsfs_SetError(-EINVAL);
1490 // todo error - no device
1491 yaffsfs_SetError(-ENODEV);
1498 int yaffs_unmount(const YCHAR *path)
1501 yaffs_Device *dev=NULL;
1505 dev = yaffsfs_FindDevice(path,&dummy);
1513 yaffs_FlushEntireDeviceCache(dev);
1514 yaffs_CheckpointSave(dev);
1516 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1518 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1520 inUse = 1; // the device is in use, can't unmount
1526 yaffs_Deinitialise(dev);
1532 // todo error can't unmount as files are open
1533 yaffsfs_SetError(-EBUSY);
1539 //todo error - not mounted.
1540 yaffsfs_SetError(-EINVAL);
1546 // todo error - no device
1547 yaffsfs_SetError(-ENODEV);
1554 loff_t yaffs_freespace(const YCHAR *path)
1557 yaffs_Device *dev=NULL;
1561 dev = yaffsfs_FindDevice(path,&dummy);
1562 if(dev && dev->isMounted)
1564 retVal = yaffs_GetNumberOfFreeChunks(dev);
1565 retVal *= dev->nDataBytesPerChunk;
1570 yaffsfs_SetError(-EINVAL);
1577 loff_t yaffs_totalspace(const YCHAR *path)
1580 yaffs_Device *dev=NULL;
1584 dev = yaffsfs_FindDevice(path,&dummy);
1585 if(dev && dev->isMounted)
1587 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1588 retVal *= dev->nChunksPerBlock;
1589 retVal *= dev->nDataBytesPerChunk;
1594 yaffsfs_SetError(-EINVAL);
1603 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1606 yaffsfs_DeviceConfiguration *cfg;
1608 yaffsfs_configurationList = cfgList;
1610 yaffsfs_InitHandles();
1612 cfg = yaffsfs_configurationList;
1614 while(cfg && cfg->prefix && cfg->dev)
1616 cfg->dev->isMounted = 0;
1617 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1626 // Directory search stuff.
1629 // Directory search context
1631 // NB this is an opaque structure.
1637 yaffs_dirent de; /* directory entry being used by this dsc */
1638 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1639 yaffs_Object *dirObj; /* ptr to directory being searched */
1640 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1642 struct ylist_head others;
1643 } yaffsfs_DirectorySearchContext;
1647 static struct ylist_head search_contexts;
1650 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1654 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1658 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1659 dsc->nextReturn = NULL;
1661 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1662 yaffs_Object,siblings);
1665 /* Hey someone isn't playing nice! */
1669 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1673 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1675 if( dsc->nextReturn == NULL ||
1676 ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1677 dsc->nextReturn = NULL;
1679 struct ylist_head *next = dsc->nextReturn->siblings.next;
1681 if( next == &dsc->dirObj->variant.directoryVariant.children)
1682 dsc->nextReturn = NULL; /* end of list */
1684 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1687 /* Hey someone isn't playing nice! */
1691 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1694 struct ylist_head *i;
1695 yaffsfs_DirectorySearchContext *dsc;
1697 /* if search contexts not initilised then skip */
1698 if(!search_contexts.next)
1701 /* Iterate through the directory search contexts.
1702 * If any are the one being removed, then advance the dsc to
1703 * the next one to prevent a hanging ptr.
1705 ylist_for_each(i, &search_contexts) {
1707 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1708 if(dsc->nextReturn == obj)
1709 yaffsfs_DirAdvance(dsc);
1715 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1717 yaffs_DIR *dir = NULL;
1718 yaffs_Object *obj = NULL;
1719 yaffsfs_DirectorySearchContext *dsc = NULL;
1723 obj = yaffsfs_FindObject(NULL,dirname,0);
1725 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1728 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1729 dir = (yaffs_DIR *)dsc;
1732 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1733 dsc->magic = YAFFS_MAGIC;
1735 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1736 YINIT_LIST_HEAD(&dsc->others);
1738 if(!search_contexts.next)
1739 YINIT_LIST_HEAD(&search_contexts);
1741 ylist_add(&dsc->others,&search_contexts);
1742 yaffsfs_SetDirRewound(dsc); }
1751 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1753 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1754 struct yaffs_dirent *retVal = NULL;
1758 if(dsc && dsc->magic == YAFFS_MAGIC){
1759 yaffsfs_SetError(0);
1760 if(dsc->nextReturn){
1761 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1762 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1763 dsc->de.d_off = dsc->offset++;
1764 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1765 if(yaffs_strlen(dsc->de.d_name) == 0)
1767 // this should not happen!
1768 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1770 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1772 yaffsfs_DirAdvance(dsc);
1778 yaffsfs_SetError(-EBADF);
1788 void yaffs_rewinddir(yaffs_DIR *dirp)
1790 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1794 yaffsfs_SetDirRewound(dsc);
1800 int yaffs_closedir(yaffs_DIR *dirp)
1802 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1806 ylist_del(&dsc->others); /* unhook from list */
1812 // end of directory stuff
1815 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1817 yaffs_Object *parent = NULL;
1821 int mode = 0; // ignore for now
1824 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1825 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1832 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1842 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1844 yaffs_Object *obj = NULL;
1850 obj = yaffsfs_FindObject(NULL,path,0);
1854 yaffsfs_SetError(-ENOENT);
1857 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1859 yaffsfs_SetError(-EINVAL);
1864 YCHAR *alias = obj->variant.symLinkVariant.alias;
1865 memset(buf,0,bufsiz);
1866 yaffs_strncpy(buf,alias,bufsiz - 1);
1873 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1875 // Creates a link called newpath to existing oldpath
1876 yaffs_Object *obj = NULL;
1877 yaffs_Object *target = NULL;
1883 obj = yaffsfs_FindObject(NULL,oldpath,0);
1884 target = yaffsfs_FindObject(NULL,newpath,0);
1888 yaffsfs_SetError(-ENOENT);
1893 yaffsfs_SetError(-EEXIST);
1898 yaffs_Object *newdir = NULL;
1899 yaffs_Object *link = NULL;
1903 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1907 yaffsfs_SetError(-ENOTDIR);
1910 else if(newdir->myDev != obj->myDev)
1912 yaffsfs_SetError(-EXDEV);
1915 if(newdir && yaffs_strlen(newname) > 0)
1917 link = yaffs_Link(newdir,newname,obj);
1922 yaffsfs_SetError(-ENOSPC);
1933 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev);
1935 int yaffs_DumpDevStruct(const YCHAR *path)
1940 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1944 yaffs_Device *dev = obj->myDev;
1947 "nPageWrites.......... %d\n"
1948 "nPageReads........... %d\n"
1949 "nBlockErasures....... %d\n"
1950 "nGCCopies............ %d\n"
1951 "garbageCollections... %d\n"
1952 "passiveGarbageColl'ns %d\n"
1956 dev->nBlockErasures,
1958 dev->garbageCollections,
1959 dev->passiveGarbageCollections