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.24 2008-11-11 01:47:46 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_flush(int fd)
592 yaffsfs_Handle *h = NULL;
597 h = yaffsfs_GetHandlePointer(fd);
602 yaffs_FlushFile(h->obj,1);
607 yaffsfs_SetError(-EBADF);
617 int yaffs_close(int fd)
619 yaffsfs_Handle *h = NULL;
624 h = yaffsfs_GetHandlePointer(fd);
629 yaffs_FlushFile(h->obj,1);
631 if(h->obj->inUse <= 0 && h->obj->unlinked)
633 yaffs_DeleteFile(h->obj);
635 yaffsfs_PutHandle(fd);
641 yaffsfs_SetError(-EBADF);
650 int yaffs_read(int fd, void *buf, unsigned int nbyte)
652 yaffsfs_Handle *h = NULL;
653 yaffs_Object *obj = NULL;
656 unsigned int maxRead;
659 h = yaffsfs_GetHandlePointer(fd);
660 obj = yaffsfs_GetHandleObject(fd);
665 yaffsfs_SetError(-EBADF);
670 if(yaffs_GetObjectFileLength(obj) > pos)
672 maxRead = yaffs_GetObjectFileLength(obj) - pos;
687 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
690 h->position = pos + nRead;
707 return (nRead >= 0) ? nRead : -1;
711 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
713 yaffsfs_Handle *h = NULL;
714 yaffs_Object *obj = NULL;
717 unsigned int maxRead;
720 h = yaffsfs_GetHandlePointer(fd);
721 obj = yaffsfs_GetHandleObject(fd);
726 yaffsfs_SetError(-EBADF);
731 if(yaffs_GetObjectFileLength(obj) > pos)
733 maxRead = yaffs_GetObjectFileLength(obj) - pos;
748 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
760 return (nRead >= 0) ? nRead : -1;
764 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
766 yaffsfs_Handle *h = NULL;
767 yaffs_Object *obj = NULL;
770 int writeThrough = 0;
773 h = yaffsfs_GetHandlePointer(fd);
774 obj = yaffsfs_GetHandleObject(fd);
779 yaffsfs_SetError(-EBADF);
781 else if( h && obj && h->readOnly)
789 pos = yaffs_GetObjectFileLength(obj);
796 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
800 h->position = pos + nWritten;
812 return (nWritten >= 0) ? nWritten : -1;
816 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
818 yaffsfs_Handle *h = NULL;
819 yaffs_Object *obj = NULL;
822 int writeThrough = 0;
825 h = yaffsfs_GetHandlePointer(fd);
826 obj = yaffsfs_GetHandleObject(fd);
831 yaffsfs_SetError(-EBADF);
833 else if( h && obj && h->readOnly)
841 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
843 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
844 yaffsfs_SetError(-ENOSPC);
851 return (nWritten >= 0) ? nWritten : -1;
856 int yaffs_truncate(const YCHAR *path,off_t newSize)
858 yaffs_Object *obj = NULL;
859 int result = YAFFS_FAIL;
863 obj = yaffsfs_FindObject(NULL,path,0);
865 obj = yaffs_GetEquivalentObject(obj);
869 yaffsfs_SetError(-ENOENT);
871 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
873 yaffsfs_SetError(-EISDIR);
877 result = yaffs_ResizeFile(obj,newSize);
883 return (result) ? 0 : -1;
886 int yaffs_ftruncate(int fd, off_t newSize)
888 yaffsfs_Handle *h = NULL;
889 yaffs_Object *obj = NULL;
893 h = yaffsfs_GetHandlePointer(fd);
894 obj = yaffsfs_GetHandleObject(fd);
899 yaffsfs_SetError(-EBADF);
904 result = yaffs_ResizeFile(obj,newSize);
909 return (result) ? 0 : -1;
913 off_t yaffs_lseek(int fd, off_t offset, int whence)
915 yaffsfs_Handle *h = NULL;
916 yaffs_Object *obj = NULL;
921 h = yaffsfs_GetHandlePointer(fd);
922 obj = yaffsfs_GetHandleObject(fd);
927 yaffsfs_SetError(-EBADF);
929 else if(whence == SEEK_SET)
936 else if(whence == SEEK_CUR)
938 if( (h->position + offset) >= 0)
940 pos = (h->position + offset);
943 else if(whence == SEEK_END)
945 fSize = yaffs_GetObjectFileLength(obj);
946 if(fSize >= 0 && (fSize + offset) >= 0)
948 pos = fSize + offset;
968 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
970 yaffs_Object *dir = NULL;
971 yaffs_Object *obj = NULL;
973 int result = YAFFS_FAIL;
977 obj = yaffsfs_FindObject(NULL,path,0);
978 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
981 yaffsfs_SetError(-ENOTDIR);
985 yaffsfs_SetError(-ENOENT);
987 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
989 yaffsfs_SetError(-EISDIR);
991 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
993 yaffsfs_SetError(-ENOTDIR);
997 result = yaffs_Unlink(dir,name);
999 if(result == YAFFS_FAIL && isDirectory)
1001 yaffsfs_SetError(-ENOTEMPTY);
1009 return (result == YAFFS_FAIL) ? -1 : 0;
1013 int yaffs_rmdir(const YCHAR *path)
1015 return yaffsfs_DoUnlink(path,1);
1018 int yaffs_unlink(const YCHAR *path)
1020 return yaffsfs_DoUnlink(path,0);
1023 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1025 yaffs_Object *olddir = NULL;
1026 yaffs_Object *newdir = NULL;
1027 yaffs_Object *obj = NULL;
1030 int result= YAFFS_FAIL;
1031 int renameAllowed = 1;
1035 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1036 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1037 obj = yaffsfs_FindObject(NULL,oldPath,0);
1039 if(!olddir || !newdir || !obj)
1042 yaffsfs_SetError(-EBADF);
1045 else if(olddir->myDev != newdir->myDev)
1047 // oops must be on same device
1049 yaffsfs_SetError(-EXDEV);
1052 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1054 // It is a directory, check that it is not being renamed to
1055 // being its own decendent.
1056 // Do this by tracing from the new directory back to the root, checking for obj
1058 yaffs_Object *xx = newdir;
1060 while( renameAllowed && xx)
1068 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1073 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1078 return (result == YAFFS_FAIL) ? -1 : 0;
1082 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1088 obj = yaffs_GetEquivalentObject(obj);
1093 buf->st_dev = (int)obj->myDev->genericDevice;
1094 buf->st_ino = obj->objectId;
1095 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1097 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1099 buf->st_mode |= S_IFDIR;
1101 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1103 buf->st_mode |= S_IFLNK;
1105 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1107 buf->st_mode |= S_IFREG;
1110 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1113 buf->st_rdev = obj->yst_rdev;
1114 buf->st_size = yaffs_GetObjectFileLength(obj);
1115 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1116 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1117 #if CONFIG_YAFFS_WINCE
1118 buf->yst_wince_atime[0] = obj->win_atime[0];
1119 buf->yst_wince_atime[1] = obj->win_atime[1];
1120 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1121 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1122 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1123 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1125 buf->yst_atime = obj->yst_atime;
1126 buf->yst_ctime = obj->yst_ctime;
1127 buf->yst_mtime = obj->yst_mtime;
1134 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1141 obj = yaffsfs_FindObject(NULL,path,0);
1145 obj = yaffsfs_FollowLink(obj,0);
1150 retVal = yaffsfs_DoStat(obj,buf);
1154 // todo error not found
1155 yaffsfs_SetError(-ENOENT);
1164 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1166 return yaffsfs_DoStatOrLStat(path,buf,0);
1169 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1171 return yaffsfs_DoStatOrLStat(path,buf,1);
1174 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1181 obj = yaffsfs_GetHandleObject(fd);
1185 retVal = yaffsfs_DoStat(obj,buf);
1190 yaffsfs_SetError(-EBADF);
1198 #ifdef CONFIG_YAFFS_WINCE
1199 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1206 obj = yaffsfs_GetHandleObject(fd);
1212 wctime[0] = obj->win_ctime[0];
1213 wctime[1] = obj->win_ctime[1];
1216 watime[0] = obj->win_atime[0];
1217 watime[1] = obj->win_atime[1];
1220 wmtime[0] = obj->win_mtime[0];
1221 wmtime[1] = obj->win_mtime[1];
1230 yaffsfs_SetError(-EBADF);
1239 int yaffs_set_wince_times(int fd,
1240 const unsigned *wctime,
1241 const unsigned *watime,
1242 const unsigned *wmtime)
1249 obj = yaffsfs_GetHandleObject(fd);
1255 obj->win_ctime[0] = wctime[0];
1256 obj->win_ctime[1] = wctime[1];
1259 obj->win_atime[0] = watime[0];
1260 obj->win_atime[1] = watime[1];
1263 obj->win_mtime[0] = wmtime[0];
1264 obj->win_mtime[1] = wmtime[1];
1268 result = yaffs_FlushFile(obj,0);
1274 yaffsfs_SetError(-EBADF);
1285 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1291 obj = yaffs_GetEquivalentObject(obj);
1296 obj->yst_mode = mode;
1298 result = yaffs_FlushFile(obj,0);
1301 return result == YAFFS_OK ? 0 : -1;
1305 int yaffs_access(const YCHAR *path, int amode)
1312 obj = yaffsfs_FindObject(NULL,path,0);
1318 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1320 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1322 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1326 yaffsfs_SetError(-EACCES);
1332 // todo error not found
1333 yaffsfs_SetError(-ENOENT);
1344 int yaffs_chmod(const YCHAR *path, mode_t mode)
1351 obj = yaffsfs_FindObject(NULL,path,0);
1355 retVal = yaffsfs_DoChMod(obj,mode);
1359 // todo error not found
1360 yaffsfs_SetError(-ENOENT);
1370 int yaffs_fchmod(int fd, mode_t mode)
1377 obj = yaffsfs_GetHandleObject(fd);
1381 retVal = yaffsfs_DoChMod(obj,mode);
1386 yaffsfs_SetError(-EBADF);
1395 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1397 yaffs_Object *parent = NULL;
1398 yaffs_Object *dir = NULL;
1403 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1405 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1413 yaffsfs_SetError(-ENOENT); // missing path
1415 else if (yaffs_FindObjectByName(parent,name)){
1416 yaffsfs_SetError(-EEXIST); // the name already exists
1419 yaffsfs_SetError(-ENOSPC); // just assume no space
1428 int yaffs_mount(const YCHAR *path)
1431 int result=YAFFS_FAIL;
1432 yaffs_Device *dev=NULL;
1435 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1438 dev = yaffsfs_FindDevice(path,&dummy);
1443 result = yaffs_GutsInitialise(dev);
1444 if(result == YAFFS_FAIL)
1446 // todo error - mount failed
1447 yaffsfs_SetError(-ENOMEM);
1449 retVal = result ? 0 : -1;
1454 //todo error - already mounted.
1455 yaffsfs_SetError(-EBUSY);
1460 // todo error - no device
1461 yaffsfs_SetError(-ENODEV);
1468 int yaffs_sync(const YCHAR *path)
1471 yaffs_Device *dev=NULL;
1475 dev = yaffsfs_FindDevice(path,&dummy);
1481 yaffs_FlushEntireDeviceCache(dev);
1482 yaffs_CheckpointSave(dev);
1488 //todo error - not mounted.
1489 yaffsfs_SetError(-EINVAL);
1495 // todo error - no device
1496 yaffsfs_SetError(-ENODEV);
1503 int yaffs_unmount(const YCHAR *path)
1506 yaffs_Device *dev=NULL;
1510 dev = yaffsfs_FindDevice(path,&dummy);
1518 yaffs_FlushEntireDeviceCache(dev);
1519 yaffs_CheckpointSave(dev);
1521 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1523 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1525 inUse = 1; // the device is in use, can't unmount
1531 yaffs_Deinitialise(dev);
1537 // todo error can't unmount as files are open
1538 yaffsfs_SetError(-EBUSY);
1544 //todo error - not mounted.
1545 yaffsfs_SetError(-EINVAL);
1551 // todo error - no device
1552 yaffsfs_SetError(-ENODEV);
1559 loff_t yaffs_freespace(const YCHAR *path)
1562 yaffs_Device *dev=NULL;
1566 dev = yaffsfs_FindDevice(path,&dummy);
1567 if(dev && dev->isMounted)
1569 retVal = yaffs_GetNumberOfFreeChunks(dev);
1570 retVal *= dev->nDataBytesPerChunk;
1575 yaffsfs_SetError(-EINVAL);
1582 loff_t yaffs_totalspace(const YCHAR *path)
1585 yaffs_Device *dev=NULL;
1589 dev = yaffsfs_FindDevice(path,&dummy);
1590 if(dev && dev->isMounted)
1592 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1593 retVal *= dev->nChunksPerBlock;
1594 retVal *= dev->nDataBytesPerChunk;
1599 yaffsfs_SetError(-EINVAL);
1606 int yaffs_inodecount(const YCHAR *path)
1609 yaffs_Device *dev=NULL;
1613 dev = yaffsfs_FindDevice(path,&dummy);
1614 if(dev && dev->isMounted) {
1615 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1616 if(nObjects > dev->nHardLinks)
1617 retVal = nObjects - dev->nHardLinks;
1621 yaffsfs_SetError(-EINVAL);
1630 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1633 yaffsfs_DeviceConfiguration *cfg;
1635 yaffsfs_configurationList = cfgList;
1637 yaffsfs_InitHandles();
1639 cfg = yaffsfs_configurationList;
1641 while(cfg && cfg->prefix && cfg->dev)
1643 cfg->dev->isMounted = 0;
1644 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1653 // Directory search stuff.
1656 // Directory search context
1658 // NB this is an opaque structure.
1664 yaffs_dirent de; /* directory entry being used by this dsc */
1665 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1666 yaffs_Object *dirObj; /* ptr to directory being searched */
1667 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1669 struct ylist_head others;
1670 } yaffsfs_DirectorySearchContext;
1674 static struct ylist_head search_contexts;
1677 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1681 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1685 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1686 dsc->nextReturn = NULL;
1688 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1689 yaffs_Object,siblings);
1692 /* Hey someone isn't playing nice! */
1696 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1700 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1702 if( dsc->nextReturn == NULL ||
1703 ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1704 dsc->nextReturn = NULL;
1706 struct ylist_head *next = dsc->nextReturn->siblings.next;
1708 if( next == &dsc->dirObj->variant.directoryVariant.children)
1709 dsc->nextReturn = NULL; /* end of list */
1711 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1714 /* Hey someone isn't playing nice! */
1718 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1721 struct ylist_head *i;
1722 yaffsfs_DirectorySearchContext *dsc;
1724 /* if search contexts not initilised then skip */
1725 if(!search_contexts.next)
1728 /* Iterate through the directory search contexts.
1729 * If any are the one being removed, then advance the dsc to
1730 * the next one to prevent a hanging ptr.
1732 ylist_for_each(i, &search_contexts) {
1734 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1735 if(dsc->nextReturn == obj)
1736 yaffsfs_DirAdvance(dsc);
1742 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1744 yaffs_DIR *dir = NULL;
1745 yaffs_Object *obj = NULL;
1746 yaffsfs_DirectorySearchContext *dsc = NULL;
1750 obj = yaffsfs_FindObject(NULL,dirname,0);
1752 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1755 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1756 dir = (yaffs_DIR *)dsc;
1759 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1760 dsc->magic = YAFFS_MAGIC;
1762 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1763 YINIT_LIST_HEAD(&dsc->others);
1765 if(!search_contexts.next)
1766 YINIT_LIST_HEAD(&search_contexts);
1768 ylist_add(&dsc->others,&search_contexts);
1769 yaffsfs_SetDirRewound(dsc); }
1778 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1780 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1781 struct yaffs_dirent *retVal = NULL;
1785 if(dsc && dsc->magic == YAFFS_MAGIC){
1786 yaffsfs_SetError(0);
1787 if(dsc->nextReturn){
1788 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1789 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1790 dsc->de.d_off = dsc->offset++;
1791 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1792 if(yaffs_strlen(dsc->de.d_name) == 0)
1794 // this should not happen!
1795 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1797 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1799 yaffsfs_DirAdvance(dsc);
1805 yaffsfs_SetError(-EBADF);
1815 void yaffs_rewinddir(yaffs_DIR *dirp)
1817 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1821 yaffsfs_SetDirRewound(dsc);
1827 int yaffs_closedir(yaffs_DIR *dirp)
1829 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1833 ylist_del(&dsc->others); /* unhook from list */
1839 // end of directory stuff
1842 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1844 yaffs_Object *parent = NULL;
1848 int mode = 0; // ignore for now
1851 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1852 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1859 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1869 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1871 yaffs_Object *obj = NULL;
1877 obj = yaffsfs_FindObject(NULL,path,0);
1881 yaffsfs_SetError(-ENOENT);
1884 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1886 yaffsfs_SetError(-EINVAL);
1891 YCHAR *alias = obj->variant.symLinkVariant.alias;
1892 memset(buf,0,bufsiz);
1893 yaffs_strncpy(buf,alias,bufsiz - 1);
1900 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1902 // Creates a link called newpath to existing oldpath
1903 yaffs_Object *obj = NULL;
1904 yaffs_Object *target = NULL;
1910 obj = yaffsfs_FindObject(NULL,oldpath,0);
1911 target = yaffsfs_FindObject(NULL,newpath,0);
1915 yaffsfs_SetError(-ENOENT);
1920 yaffsfs_SetError(-EEXIST);
1925 yaffs_Object *newdir = NULL;
1926 yaffs_Object *link = NULL;
1930 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1934 yaffsfs_SetError(-ENOTDIR);
1937 else if(newdir->myDev != obj->myDev)
1939 yaffsfs_SetError(-EXDEV);
1942 if(newdir && yaffs_strlen(newname) > 0)
1944 link = yaffs_Link(newdir,newname,obj);
1949 yaffsfs_SetError(-ENOSPC);
1960 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev);
1962 int yaffs_DumpDevStruct(const YCHAR *path)
1967 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1971 yaffs_Device *dev = obj->myDev;
1974 "nPageWrites.......... %d\n"
1975 "nPageReads........... %d\n"
1976 "nBlockErasures....... %d\n"
1977 "nGCCopies............ %d\n"
1978 "garbageCollections... %d\n"
1979 "passiveGarbageColl'ns %d\n"
1983 dev->nBlockErasures,
1985 dev->garbageCollections,
1986 dev->passiveGarbageCollections