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.26 2009-09-23 23:24:55 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;
174 // Check all configs, choose the one that:
175 // 1) Actually matches a prefix (ie /a amd /abc will not match
176 // 2) Matches the longest.
177 while(cfg && cfg->prefix && cfg->dev)
185 while(matching && *p && *leftOver){
187 while(yaffsfs_IsPathDivider(*p))
191 while(yaffsfs_IsPathDivider(*leftOver))
194 // Now match the text part
196 *p && !yaffsfs_IsPathDivider(*p) &&
197 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
198 if(yaffsfs_Match(*p,*leftOver)){
208 // Skip over any /s in leftOver
209 while(yaffsfs_IsPathDivider(*leftOver))
213 if( matching && (thisMatchLength > longestMatch))
216 *restOfPath = (YCHAR *)leftOver;
218 longestMatch = thisMatchLength;
227 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
229 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
230 const YCHAR *leftOver;
232 yaffs_Device *retval = NULL;
234 int longestMatch = -1;
236 // Check all configs, choose the one that:
237 // 1) Actually matches a prefix (ie /a amd /abc will not match
238 // 2) Matches the longest.
239 while(cfg && cfg->prefix && cfg->dev)
245 while(*p && //unmatched part of prefix
246 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
248 yaffsfs_Match(*p,*leftOver))
256 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
257 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
258 (thisMatchLength > longestMatch))
261 *restOfPath = (YCHAR *)leftOver;
263 longestMatch = thisMatchLength;
271 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
276 dev= yaffsfs_FindDevice(path,restOfPath);
277 if(dev && dev->isMounted)
284 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
287 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
289 YCHAR *alias = obj->variant.symLinkVariant.alias;
291 if(yaffsfs_IsPathDivider(*alias))
293 // Starts with a /, need to scan from root up
294 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
298 // Relative to here, so use the parent of the symlink as a start
299 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
306 // yaffsfs_FindDirectory
307 // Parse a path to determine the directory and the name within the directory.
309 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
310 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
314 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
317 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
325 restOfPath = (YCHAR *)path;
329 dir = yaffsfs_FindRoot(path,&restOfPath);
335 // curve ball: also throw away surplus '/'
336 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
337 while(yaffsfs_IsPathDivider(*restOfPath))
339 restOfPath++; // get rid of '/'
345 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath))
347 if (i < YAFFS_MAX_NAME_LENGTH)
349 str[i] = *restOfPath;
358 // got to the end of the string
363 if(yaffs_strcmp(str,_Y(".")) == 0)
367 else if(yaffs_strcmp(str,_Y("..")) == 0)
373 dir = yaffs_FindObjectByName(dir,str);
375 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
378 dir = yaffsfs_FollowLink(dir,symDepth);
382 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
389 // directory did not exist.
393 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
395 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
398 // yaffsfs_FindObject turns a path for an existing object into the object
400 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
405 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
409 return yaffs_FindObjectByName(dir,name);
416 int yaffs_dup(int fd)
419 yaffsfs_Handle *oldPtr = NULL;
420 yaffsfs_Handle *newPtr = NULL;
424 oldPtr = yaffsfs_GetHandlePointer(fd);
425 if(oldPtr && oldPtr->inUse)
426 newHandle = yaffsfs_GetHandle();
428 newPtr = yaffsfs_GetHandlePointer(newHandle);
436 yaffsfs_SetError(-EBADF);
438 yaffsfs_SetError(-ENOMEM);
444 int yaffs_open(const YCHAR *path, int oflag, int mode)
446 yaffs_Object *obj = NULL;
447 yaffs_Object *dir = NULL;
450 yaffsfs_Handle *h = NULL;
452 int alreadyExclusive = 0;
455 int errorReported = 0;
460 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
465 handle = yaffsfs_GetHandle();
470 h = yaffsfs_GetHandlePointer(handle);
473 // try to find the exisiting object
474 obj = yaffsfs_FindObject(NULL,path,0);
476 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
479 obj = yaffsfs_FollowLink(obj,symDepth++);
482 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
489 // Check if the object is already in use
490 alreadyOpen = alreadyExclusive = 0;
492 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
496 yaffsfs_handle[i].inUse &&
497 obj == yaffsfs_handle[i].obj)
500 if(yaffsfs_handle[i].exclusive)
502 alreadyExclusive = 1;
507 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
512 // Open should fail if O_CREAT and O_EXCL are specified
513 if((oflag & O_EXCL) && (oflag & O_CREAT))
516 yaffsfs_SetError(-EEXIST);
520 // Check file permissions
521 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
522 !(obj->yst_mode & S_IREAD))
527 if( (oflag & O_RDWR) &&
528 !(obj->yst_mode & S_IREAD))
533 if( (oflag & (O_RDWR | O_WRONLY)) &&
534 !(obj->yst_mode & S_IWRITE))
541 else if((oflag & O_CREAT))
543 // Let's see if we can create this file
544 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
547 obj = yaffs_MknodFile(dir,name,mode,0,0);
551 yaffsfs_SetError(-ENOTDIR);
556 if(obj && !openDenied)
560 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
561 h->append = (oflag & O_APPEND) ? 1 : 0;
562 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
566 if((oflag & O_TRUNC) && !h->readOnly)
568 yaffs_ResizeFile(obj,0);
574 yaffsfs_PutHandle(handle);
577 yaffsfs_SetError(-EACCES);
590 int yaffs_Dofsync(int fd,int datasync)
592 yaffsfs_Handle *h = NULL;
597 h = yaffsfs_GetHandlePointer(fd);
602 yaffs_FlushFile(h->obj,1,datasync);
607 yaffsfs_SetError(-EBADF);
616 int yaffs_fsync(int fd)
618 return yaffs_Dofsync(fd,0);
621 int yaffs_fdatasync(int fd)
623 return yaffs_Dofsync(fd,1);
626 int yaffs_close(int fd)
628 yaffsfs_Handle *h = NULL;
633 h = yaffsfs_GetHandlePointer(fd);
638 yaffs_FlushFile(h->obj,1,0);
640 if(h->obj->inUse <= 0 && h->obj->unlinked)
642 yaffs_DeleteObject(h->obj);
644 yaffsfs_PutHandle(fd);
650 yaffsfs_SetError(-EBADF);
659 int yaffs_read(int fd, void *buf, unsigned int nbyte)
661 yaffsfs_Handle *h = NULL;
662 yaffs_Object *obj = NULL;
665 unsigned int maxRead;
668 h = yaffsfs_GetHandlePointer(fd);
669 obj = yaffsfs_GetHandleObject(fd);
674 yaffsfs_SetError(-EBADF);
679 if(yaffs_GetObjectFileLength(obj) > pos)
681 maxRead = yaffs_GetObjectFileLength(obj) - pos;
696 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
699 h->position = pos + nRead;
716 return (nRead >= 0) ? nRead : -1;
720 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
722 yaffsfs_Handle *h = NULL;
723 yaffs_Object *obj = NULL;
726 unsigned int maxRead;
729 h = yaffsfs_GetHandlePointer(fd);
730 obj = yaffsfs_GetHandleObject(fd);
735 yaffsfs_SetError(-EBADF);
740 if(yaffs_GetObjectFileLength(obj) > pos)
742 maxRead = yaffs_GetObjectFileLength(obj) - pos;
757 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
769 return (nRead >= 0) ? nRead : -1;
773 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
775 yaffsfs_Handle *h = NULL;
776 yaffs_Object *obj = NULL;
779 int writeThrough = 0;
782 h = yaffsfs_GetHandlePointer(fd);
783 obj = yaffsfs_GetHandleObject(fd);
788 yaffsfs_SetError(-EBADF);
790 else if( h && obj && h->readOnly)
798 pos = yaffs_GetObjectFileLength(obj);
805 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
809 h->position = pos + nWritten;
821 return (nWritten >= 0) ? nWritten : -1;
825 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
827 yaffsfs_Handle *h = NULL;
828 yaffs_Object *obj = NULL;
831 int writeThrough = 0;
834 h = yaffsfs_GetHandlePointer(fd);
835 obj = yaffsfs_GetHandleObject(fd);
840 yaffsfs_SetError(-EBADF);
842 else if( h && obj && h->readOnly)
850 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
852 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
853 yaffsfs_SetError(-ENOSPC);
860 return (nWritten >= 0) ? nWritten : -1;
865 int yaffs_truncate(const YCHAR *path,off_t newSize)
867 yaffs_Object *obj = NULL;
868 int result = YAFFS_FAIL;
872 obj = yaffsfs_FindObject(NULL,path,0);
874 obj = yaffs_GetEquivalentObject(obj);
878 yaffsfs_SetError(-ENOENT);
880 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
882 yaffsfs_SetError(-EISDIR);
886 result = yaffs_ResizeFile(obj,newSize);
892 return (result) ? 0 : -1;
895 int yaffs_ftruncate(int fd, off_t newSize)
897 yaffsfs_Handle *h = NULL;
898 yaffs_Object *obj = NULL;
902 h = yaffsfs_GetHandlePointer(fd);
903 obj = yaffsfs_GetHandleObject(fd);
908 yaffsfs_SetError(-EBADF);
913 result = yaffs_ResizeFile(obj,newSize);
918 return (result) ? 0 : -1;
922 off_t yaffs_lseek(int fd, off_t offset, int whence)
924 yaffsfs_Handle *h = NULL;
925 yaffs_Object *obj = NULL;
930 h = yaffsfs_GetHandlePointer(fd);
931 obj = yaffsfs_GetHandleObject(fd);
936 yaffsfs_SetError(-EBADF);
938 else if(whence == SEEK_SET)
945 else if(whence == SEEK_CUR)
947 if( (h->position + offset) >= 0)
949 pos = (h->position + offset);
952 else if(whence == SEEK_END)
954 fSize = yaffs_GetObjectFileLength(obj);
955 if(fSize >= 0 && (fSize + offset) >= 0)
957 pos = fSize + offset;
977 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
979 yaffs_Object *dir = NULL;
980 yaffs_Object *obj = NULL;
982 int result = YAFFS_FAIL;
986 obj = yaffsfs_FindObject(NULL,path,0);
987 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
990 yaffsfs_SetError(-ENOTDIR);
994 yaffsfs_SetError(-ENOENT);
996 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
998 yaffsfs_SetError(-EISDIR);
1000 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1002 yaffsfs_SetError(-ENOTDIR);
1006 result = yaffs_Unlink(dir,name);
1008 if(result == YAFFS_FAIL && isDirectory)
1010 yaffsfs_SetError(-ENOTEMPTY);
1018 return (result == YAFFS_FAIL) ? -1 : 0;
1022 int yaffs_rmdir(const YCHAR *path)
1024 return yaffsfs_DoUnlink(path,1);
1027 int yaffs_unlink(const YCHAR *path)
1029 return yaffsfs_DoUnlink(path,0);
1032 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1034 yaffs_Object *olddir = NULL;
1035 yaffs_Object *newdir = NULL;
1036 yaffs_Object *obj = NULL;
1039 int result= YAFFS_FAIL;
1040 int renameAllowed = 1;
1044 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1045 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1046 obj = yaffsfs_FindObject(NULL,oldPath,0);
1048 if(!olddir || !newdir || !obj)
1051 yaffsfs_SetError(-EBADF);
1054 else if(olddir->myDev != newdir->myDev)
1056 // oops must be on same device
1058 yaffsfs_SetError(-EXDEV);
1061 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1063 // It is a directory, check that it is not being renamed to
1064 // being its own decendent.
1065 // Do this by tracing from the new directory back to the root, checking for obj
1067 yaffs_Object *xx = newdir;
1069 while( renameAllowed && xx)
1077 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1082 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1087 return (result == YAFFS_FAIL) ? -1 : 0;
1091 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1097 obj = yaffs_GetEquivalentObject(obj);
1102 buf->st_dev = (int)obj->myDev->genericDevice;
1103 buf->st_ino = obj->objectId;
1104 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1106 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1108 buf->st_mode |= S_IFDIR;
1110 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1112 buf->st_mode |= S_IFLNK;
1114 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1116 buf->st_mode |= S_IFREG;
1119 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1122 buf->st_rdev = obj->yst_rdev;
1123 buf->st_size = yaffs_GetObjectFileLength(obj);
1124 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1125 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1126 #if CONFIG_YAFFS_WINCE
1127 buf->yst_wince_atime[0] = obj->win_atime[0];
1128 buf->yst_wince_atime[1] = obj->win_atime[1];
1129 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1130 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1131 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1132 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1134 buf->yst_atime = obj->yst_atime;
1135 buf->yst_ctime = obj->yst_ctime;
1136 buf->yst_mtime = obj->yst_mtime;
1143 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1150 obj = yaffsfs_FindObject(NULL,path,0);
1154 obj = yaffsfs_FollowLink(obj,0);
1159 retVal = yaffsfs_DoStat(obj,buf);
1163 // todo error not found
1164 yaffsfs_SetError(-ENOENT);
1173 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1175 return yaffsfs_DoStatOrLStat(path,buf,0);
1178 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1180 return yaffsfs_DoStatOrLStat(path,buf,1);
1183 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1190 obj = yaffsfs_GetHandleObject(fd);
1194 retVal = yaffsfs_DoStat(obj,buf);
1199 yaffsfs_SetError(-EBADF);
1207 #ifdef CONFIG_YAFFS_WINCE
1208 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1215 obj = yaffsfs_GetHandleObject(fd);
1221 wctime[0] = obj->win_ctime[0];
1222 wctime[1] = obj->win_ctime[1];
1225 watime[0] = obj->win_atime[0];
1226 watime[1] = obj->win_atime[1];
1229 wmtime[0] = obj->win_mtime[0];
1230 wmtime[1] = obj->win_mtime[1];
1239 yaffsfs_SetError(-EBADF);
1248 int yaffs_set_wince_times(int fd,
1249 const unsigned *wctime,
1250 const unsigned *watime,
1251 const unsigned *wmtime)
1258 obj = yaffsfs_GetHandleObject(fd);
1264 obj->win_ctime[0] = wctime[0];
1265 obj->win_ctime[1] = wctime[1];
1268 obj->win_atime[0] = watime[0];
1269 obj->win_atime[1] = watime[1];
1272 obj->win_mtime[0] = wmtime[0];
1273 obj->win_mtime[1] = wmtime[1];
1277 result = yaffs_FlushFile(obj,0,0);
1283 yaffsfs_SetError(-EBADF);
1294 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1300 obj = yaffs_GetEquivalentObject(obj);
1305 obj->yst_mode = mode;
1307 result = yaffs_FlushFile(obj,0,0);
1310 return result == YAFFS_OK ? 0 : -1;
1314 int yaffs_access(const YCHAR *path, int amode)
1321 obj = yaffsfs_FindObject(NULL,path,0);
1327 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1329 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1331 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1335 yaffsfs_SetError(-EACCES);
1341 // todo error not found
1342 yaffsfs_SetError(-ENOENT);
1353 int yaffs_chmod(const YCHAR *path, mode_t mode)
1360 obj = yaffsfs_FindObject(NULL,path,0);
1364 retVal = yaffsfs_DoChMod(obj,mode);
1368 // todo error not found
1369 yaffsfs_SetError(-ENOENT);
1379 int yaffs_fchmod(int fd, mode_t mode)
1386 obj = yaffsfs_GetHandleObject(fd);
1390 retVal = yaffsfs_DoChMod(obj,mode);
1395 yaffsfs_SetError(-EBADF);
1404 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1406 yaffs_Object *parent = NULL;
1407 yaffs_Object *dir = NULL;
1412 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1414 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1422 yaffsfs_SetError(-ENOENT); // missing path
1424 else if (yaffs_FindObjectByName(parent,name)){
1425 yaffsfs_SetError(-EEXIST); // the name already exists
1428 yaffsfs_SetError(-ENOSPC); // just assume no space
1437 int yaffs_mount(const YCHAR *path)
1440 int result=YAFFS_FAIL;
1441 yaffs_Device *dev=NULL;
1444 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1447 dev = yaffsfs_FindDevice(path,&dummy);
1452 result = yaffs_GutsInitialise(dev);
1453 if(result == YAFFS_FAIL)
1455 // todo error - mount failed
1456 yaffsfs_SetError(-ENOMEM);
1458 retVal = result ? 0 : -1;
1463 //todo error - already mounted.
1464 yaffsfs_SetError(-EBUSY);
1469 // todo error - no device
1470 yaffsfs_SetError(-ENODEV);
1477 int yaffs_sync(const YCHAR *path)
1480 yaffs_Device *dev=NULL;
1484 dev = yaffsfs_FindDevice(path,&dummy);
1490 yaffs_FlushEntireDeviceCache(dev);
1491 yaffs_CheckpointSave(dev);
1497 //todo error - not mounted.
1498 yaffsfs_SetError(-EINVAL);
1504 // todo error - no device
1505 yaffsfs_SetError(-ENODEV);
1512 int yaffs_unmount(const YCHAR *path)
1515 yaffs_Device *dev=NULL;
1519 dev = yaffsfs_FindDevice(path,&dummy);
1527 yaffs_FlushEntireDeviceCache(dev);
1528 yaffs_CheckpointSave(dev);
1530 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1532 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1534 inUse = 1; // the device is in use, can't unmount
1540 yaffs_Deinitialise(dev);
1546 // todo error can't unmount as files are open
1547 yaffsfs_SetError(-EBUSY);
1553 //todo error - not mounted.
1554 yaffsfs_SetError(-EINVAL);
1560 // todo error - no device
1561 yaffsfs_SetError(-ENODEV);
1568 loff_t yaffs_freespace(const YCHAR *path)
1571 yaffs_Device *dev=NULL;
1575 dev = yaffsfs_FindDevice(path,&dummy);
1576 if(dev && dev->isMounted)
1578 retVal = yaffs_GetNumberOfFreeChunks(dev);
1579 retVal *= dev->nDataBytesPerChunk;
1584 yaffsfs_SetError(-EINVAL);
1591 loff_t yaffs_totalspace(const YCHAR *path)
1594 yaffs_Device *dev=NULL;
1598 dev = yaffsfs_FindDevice(path,&dummy);
1599 if(dev && dev->isMounted)
1601 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1602 retVal *= dev->nChunksPerBlock;
1603 retVal *= dev->nDataBytesPerChunk;
1608 yaffsfs_SetError(-EINVAL);
1615 int yaffs_inodecount(const YCHAR *path)
1618 yaffs_Device *dev=NULL;
1622 dev = yaffsfs_FindDevice(path,&dummy);
1623 if(dev && dev->isMounted) {
1624 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1625 if(nObjects > dev->nHardLinks)
1626 retVal = nObjects - dev->nHardLinks;
1630 yaffsfs_SetError(-EINVAL);
1639 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1642 yaffsfs_DeviceConfiguration *cfg;
1644 yaffsfs_configurationList = cfgList;
1646 yaffsfs_InitHandles();
1648 cfg = yaffsfs_configurationList;
1650 while(cfg && cfg->prefix && cfg->dev)
1652 cfg->dev->isMounted = 0;
1653 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1662 // Directory search stuff.
1665 // Directory search context
1667 // NB this is an opaque structure.
1673 yaffs_dirent de; /* directory entry being used by this dsc */
1674 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1675 yaffs_Object *dirObj; /* ptr to directory being searched */
1676 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1678 struct ylist_head others;
1679 } yaffsfs_DirectorySearchContext;
1683 static struct ylist_head search_contexts;
1686 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1690 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1694 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1695 dsc->nextReturn = NULL;
1697 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1698 yaffs_Object,siblings);
1701 /* Hey someone isn't playing nice! */
1705 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1709 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1711 if( dsc->nextReturn == NULL ||
1712 ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1713 dsc->nextReturn = NULL;
1715 struct ylist_head *next = dsc->nextReturn->siblings.next;
1717 if( next == &dsc->dirObj->variant.directoryVariant.children)
1718 dsc->nextReturn = NULL; /* end of list */
1720 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1723 /* Hey someone isn't playing nice! */
1727 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1730 struct ylist_head *i;
1731 yaffsfs_DirectorySearchContext *dsc;
1733 /* if search contexts not initilised then skip */
1734 if(!search_contexts.next)
1737 /* Iterate through the directory search contexts.
1738 * If any are the one being removed, then advance the dsc to
1739 * the next one to prevent a hanging ptr.
1741 ylist_for_each(i, &search_contexts) {
1743 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1744 if(dsc->nextReturn == obj)
1745 yaffsfs_DirAdvance(dsc);
1751 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1753 yaffs_DIR *dir = NULL;
1754 yaffs_Object *obj = NULL;
1755 yaffsfs_DirectorySearchContext *dsc = NULL;
1759 obj = yaffsfs_FindObject(NULL,dirname,0);
1761 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1764 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1765 dir = (yaffs_DIR *)dsc;
1768 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1769 dsc->magic = YAFFS_MAGIC;
1771 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1772 YINIT_LIST_HEAD(&dsc->others);
1774 if(!search_contexts.next)
1775 YINIT_LIST_HEAD(&search_contexts);
1777 ylist_add(&dsc->others,&search_contexts);
1778 yaffsfs_SetDirRewound(dsc); }
1787 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1789 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1790 struct yaffs_dirent *retVal = NULL;
1794 if(dsc && dsc->magic == YAFFS_MAGIC){
1795 yaffsfs_SetError(0);
1796 if(dsc->nextReturn){
1797 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1798 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1799 dsc->de.d_off = dsc->offset++;
1800 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1801 if(yaffs_strlen(dsc->de.d_name) == 0)
1803 // this should not happen!
1804 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1806 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1808 yaffsfs_DirAdvance(dsc);
1814 yaffsfs_SetError(-EBADF);
1824 void yaffs_rewinddir(yaffs_DIR *dirp)
1826 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1830 yaffsfs_SetDirRewound(dsc);
1836 int yaffs_closedir(yaffs_DIR *dirp)
1838 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1842 ylist_del(&dsc->others); /* unhook from list */
1848 // end of directory stuff
1851 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1853 yaffs_Object *parent = NULL;
1857 int mode = 0; // ignore for now
1860 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1862 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1869 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1873 yaffsfs_SetError(-EINVAL);
1883 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1885 yaffs_Object *obj = NULL;
1891 obj = yaffsfs_FindObject(NULL,path,0);
1895 yaffsfs_SetError(-ENOENT);
1898 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1900 yaffsfs_SetError(-EINVAL);
1905 YCHAR *alias = obj->variant.symLinkVariant.alias;
1906 memset(buf,0,bufsiz);
1907 yaffs_strncpy(buf,alias,bufsiz - 1);
1914 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1916 // Creates a link called newpath to existing oldpath
1917 yaffs_Object *obj = NULL;
1918 yaffs_Object *target = NULL;
1924 obj = yaffsfs_FindObject(NULL,oldpath,0);
1925 target = yaffsfs_FindObject(NULL,newpath,0);
1929 yaffsfs_SetError(-ENOENT);
1934 yaffsfs_SetError(-EEXIST);
1939 yaffs_Object *newdir = NULL;
1940 yaffs_Object *link = NULL;
1944 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1948 yaffsfs_SetError(-ENOTDIR);
1951 else if(newdir->myDev != obj->myDev)
1953 yaffsfs_SetError(-EXDEV);
1956 if(newdir && yaffs_strlen(newname) > 0)
1958 link = yaffs_Link(newdir,newname,obj);
1963 yaffsfs_SetError(-ENOSPC);
1974 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev);
1976 int yaffs_DumpDevStruct(const YCHAR *path)
1981 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1985 yaffs_Device *dev = obj->myDev;
1988 "nPageWrites.......... %d\n"
1989 "nPageReads........... %d\n"
1990 "nBlockErasures....... %d\n"
1991 "nGCCopies............ %d\n"
1992 "garbageCollections... %d\n"
1993 "passiveGarbageColl'ns %d\n"
1997 dev->nBlockErasures,
1999 dev->garbageCollections,
2000 dev->passiveGarbageCollections