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.27 2009-10-08 01:57:59 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_flush(int fd)
623 return yaffs_fsync(fd);
626 int yaffs_fdatasync(int fd)
628 return yaffs_Dofsync(fd,1);
631 int yaffs_close(int fd)
633 yaffsfs_Handle *h = NULL;
638 h = yaffsfs_GetHandlePointer(fd);
643 yaffs_FlushFile(h->obj,1,0);
645 if(h->obj->inUse <= 0 && h->obj->unlinked)
647 yaffs_DeleteObject(h->obj);
649 yaffsfs_PutHandle(fd);
655 yaffsfs_SetError(-EBADF);
664 int yaffs_read(int fd, void *buf, unsigned int nbyte)
666 yaffsfs_Handle *h = NULL;
667 yaffs_Object *obj = NULL;
670 unsigned int maxRead;
673 h = yaffsfs_GetHandlePointer(fd);
674 obj = yaffsfs_GetHandleObject(fd);
679 yaffsfs_SetError(-EBADF);
684 if(yaffs_GetObjectFileLength(obj) > pos)
686 maxRead = yaffs_GetObjectFileLength(obj) - pos;
701 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
704 h->position = pos + nRead;
721 return (nRead >= 0) ? nRead : -1;
725 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
727 yaffsfs_Handle *h = NULL;
728 yaffs_Object *obj = NULL;
731 unsigned int maxRead;
734 h = yaffsfs_GetHandlePointer(fd);
735 obj = yaffsfs_GetHandleObject(fd);
740 yaffsfs_SetError(-EBADF);
745 if(yaffs_GetObjectFileLength(obj) > pos)
747 maxRead = yaffs_GetObjectFileLength(obj) - pos;
762 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
774 return (nRead >= 0) ? nRead : -1;
778 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
780 yaffsfs_Handle *h = NULL;
781 yaffs_Object *obj = NULL;
784 int writeThrough = 0;
787 h = yaffsfs_GetHandlePointer(fd);
788 obj = yaffsfs_GetHandleObject(fd);
793 yaffsfs_SetError(-EBADF);
795 else if( h && obj && h->readOnly)
803 pos = yaffs_GetObjectFileLength(obj);
810 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
814 h->position = pos + nWritten;
826 return (nWritten >= 0) ? nWritten : -1;
830 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
832 yaffsfs_Handle *h = NULL;
833 yaffs_Object *obj = NULL;
836 int writeThrough = 0;
839 h = yaffsfs_GetHandlePointer(fd);
840 obj = yaffsfs_GetHandleObject(fd);
845 yaffsfs_SetError(-EBADF);
847 else if( h && obj && h->readOnly)
855 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
857 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
858 yaffsfs_SetError(-ENOSPC);
865 return (nWritten >= 0) ? nWritten : -1;
870 int yaffs_truncate(const YCHAR *path,off_t newSize)
872 yaffs_Object *obj = NULL;
873 int result = YAFFS_FAIL;
877 obj = yaffsfs_FindObject(NULL,path,0);
879 obj = yaffs_GetEquivalentObject(obj);
883 yaffsfs_SetError(-ENOENT);
885 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
887 yaffsfs_SetError(-EISDIR);
891 result = yaffs_ResizeFile(obj,newSize);
897 return (result) ? 0 : -1;
900 int yaffs_ftruncate(int fd, off_t newSize)
902 yaffsfs_Handle *h = NULL;
903 yaffs_Object *obj = NULL;
907 h = yaffsfs_GetHandlePointer(fd);
908 obj = yaffsfs_GetHandleObject(fd);
913 yaffsfs_SetError(-EBADF);
918 result = yaffs_ResizeFile(obj,newSize);
923 return (result) ? 0 : -1;
927 off_t yaffs_lseek(int fd, off_t offset, int whence)
929 yaffsfs_Handle *h = NULL;
930 yaffs_Object *obj = NULL;
935 h = yaffsfs_GetHandlePointer(fd);
936 obj = yaffsfs_GetHandleObject(fd);
941 yaffsfs_SetError(-EBADF);
943 else if(whence == SEEK_SET)
950 else if(whence == SEEK_CUR)
952 if( (h->position + offset) >= 0)
954 pos = (h->position + offset);
957 else if(whence == SEEK_END)
959 fSize = yaffs_GetObjectFileLength(obj);
960 if(fSize >= 0 && (fSize + offset) >= 0)
962 pos = fSize + offset;
982 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
984 yaffs_Object *dir = NULL;
985 yaffs_Object *obj = NULL;
987 int result = YAFFS_FAIL;
991 obj = yaffsfs_FindObject(NULL,path,0);
992 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
995 yaffsfs_SetError(-ENOTDIR);
999 yaffsfs_SetError(-ENOENT);
1001 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1003 yaffsfs_SetError(-EISDIR);
1005 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1007 yaffsfs_SetError(-ENOTDIR);
1011 result = yaffs_Unlink(dir,name);
1013 if(result == YAFFS_FAIL && isDirectory)
1015 yaffsfs_SetError(-ENOTEMPTY);
1023 return (result == YAFFS_FAIL) ? -1 : 0;
1027 int yaffs_rmdir(const YCHAR *path)
1029 return yaffsfs_DoUnlink(path,1);
1032 int yaffs_unlink(const YCHAR *path)
1034 return yaffsfs_DoUnlink(path,0);
1037 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1039 yaffs_Object *olddir = NULL;
1040 yaffs_Object *newdir = NULL;
1041 yaffs_Object *obj = NULL;
1044 int result= YAFFS_FAIL;
1045 int renameAllowed = 1;
1049 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1050 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1051 obj = yaffsfs_FindObject(NULL,oldPath,0);
1053 if(!olddir || !newdir || !obj)
1056 yaffsfs_SetError(-EBADF);
1059 else if(olddir->myDev != newdir->myDev)
1061 // oops must be on same device
1063 yaffsfs_SetError(-EXDEV);
1066 else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1068 // It is a directory, check that it is not being renamed to
1069 // being its own decendent.
1070 // Do this by tracing from the new directory back to the root, checking for obj
1072 yaffs_Object *xx = newdir;
1074 while( renameAllowed && xx)
1082 if(!renameAllowed) yaffsfs_SetError(-EACCES);
1087 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1092 return (result == YAFFS_FAIL) ? -1 : 0;
1096 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1102 obj = yaffs_GetEquivalentObject(obj);
1107 buf->st_dev = (int)obj->myDev->genericDevice;
1108 buf->st_ino = obj->objectId;
1109 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1111 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1113 buf->st_mode |= S_IFDIR;
1115 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1117 buf->st_mode |= S_IFLNK;
1119 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1121 buf->st_mode |= S_IFREG;
1124 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1127 buf->st_rdev = obj->yst_rdev;
1128 buf->st_size = yaffs_GetObjectFileLength(obj);
1129 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1130 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1131 #if CONFIG_YAFFS_WINCE
1132 buf->yst_wince_atime[0] = obj->win_atime[0];
1133 buf->yst_wince_atime[1] = obj->win_atime[1];
1134 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1135 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1136 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1137 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1139 buf->yst_atime = obj->yst_atime;
1140 buf->yst_ctime = obj->yst_ctime;
1141 buf->yst_mtime = obj->yst_mtime;
1148 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1155 obj = yaffsfs_FindObject(NULL,path,0);
1159 obj = yaffsfs_FollowLink(obj,0);
1164 retVal = yaffsfs_DoStat(obj,buf);
1168 // todo error not found
1169 yaffsfs_SetError(-ENOENT);
1178 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1180 return yaffsfs_DoStatOrLStat(path,buf,0);
1183 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1185 return yaffsfs_DoStatOrLStat(path,buf,1);
1188 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1195 obj = yaffsfs_GetHandleObject(fd);
1199 retVal = yaffsfs_DoStat(obj,buf);
1204 yaffsfs_SetError(-EBADF);
1212 #ifdef CONFIG_YAFFS_WINCE
1213 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1220 obj = yaffsfs_GetHandleObject(fd);
1226 wctime[0] = obj->win_ctime[0];
1227 wctime[1] = obj->win_ctime[1];
1230 watime[0] = obj->win_atime[0];
1231 watime[1] = obj->win_atime[1];
1234 wmtime[0] = obj->win_mtime[0];
1235 wmtime[1] = obj->win_mtime[1];
1244 yaffsfs_SetError(-EBADF);
1253 int yaffs_set_wince_times(int fd,
1254 const unsigned *wctime,
1255 const unsigned *watime,
1256 const unsigned *wmtime)
1263 obj = yaffsfs_GetHandleObject(fd);
1269 obj->win_ctime[0] = wctime[0];
1270 obj->win_ctime[1] = wctime[1];
1273 obj->win_atime[0] = watime[0];
1274 obj->win_atime[1] = watime[1];
1277 obj->win_mtime[0] = wmtime[0];
1278 obj->win_mtime[1] = wmtime[1];
1282 result = yaffs_FlushFile(obj,0,0);
1288 yaffsfs_SetError(-EBADF);
1299 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1305 obj = yaffs_GetEquivalentObject(obj);
1310 obj->yst_mode = mode;
1312 result = yaffs_FlushFile(obj,0,0);
1315 return result == YAFFS_OK ? 0 : -1;
1319 int yaffs_access(const YCHAR *path, int amode)
1326 obj = yaffsfs_FindObject(NULL,path,0);
1332 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1334 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1336 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1340 yaffsfs_SetError(-EACCES);
1346 // todo error not found
1347 yaffsfs_SetError(-ENOENT);
1358 int yaffs_chmod(const YCHAR *path, mode_t mode)
1365 obj = yaffsfs_FindObject(NULL,path,0);
1369 retVal = yaffsfs_DoChMod(obj,mode);
1373 // todo error not found
1374 yaffsfs_SetError(-ENOENT);
1384 int yaffs_fchmod(int fd, mode_t mode)
1391 obj = yaffsfs_GetHandleObject(fd);
1395 retVal = yaffsfs_DoChMod(obj,mode);
1400 yaffsfs_SetError(-EBADF);
1409 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1411 yaffs_Object *parent = NULL;
1412 yaffs_Object *dir = NULL;
1417 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1419 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1427 yaffsfs_SetError(-ENOENT); // missing path
1429 else if (yaffs_FindObjectByName(parent,name)){
1430 yaffsfs_SetError(-EEXIST); // the name already exists
1433 yaffsfs_SetError(-ENOSPC); // just assume no space
1442 int yaffs_mount(const YCHAR *path)
1445 int result=YAFFS_FAIL;
1446 yaffs_Device *dev=NULL;
1449 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1452 dev = yaffsfs_FindDevice(path,&dummy);
1457 result = yaffs_GutsInitialise(dev);
1458 if(result == YAFFS_FAIL)
1460 // todo error - mount failed
1461 yaffsfs_SetError(-ENOMEM);
1463 retVal = result ? 0 : -1;
1468 //todo error - already mounted.
1469 yaffsfs_SetError(-EBUSY);
1474 // todo error - no device
1475 yaffsfs_SetError(-ENODEV);
1482 int yaffs_sync(const YCHAR *path)
1485 yaffs_Device *dev=NULL;
1489 dev = yaffsfs_FindDevice(path,&dummy);
1495 yaffs_FlushEntireDeviceCache(dev);
1496 yaffs_CheckpointSave(dev);
1502 //todo error - not mounted.
1503 yaffsfs_SetError(-EINVAL);
1509 // todo error - no device
1510 yaffsfs_SetError(-ENODEV);
1517 int yaffs_unmount(const YCHAR *path)
1520 yaffs_Device *dev=NULL;
1524 dev = yaffsfs_FindDevice(path,&dummy);
1532 yaffs_FlushEntireDeviceCache(dev);
1533 yaffs_CheckpointSave(dev);
1535 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++)
1537 if(yaffsfs_handle[i].inUse && yaffsfs_handle[i].obj->myDev == dev)
1539 inUse = 1; // the device is in use, can't unmount
1545 yaffs_Deinitialise(dev);
1551 // todo error can't unmount as files are open
1552 yaffsfs_SetError(-EBUSY);
1558 //todo error - not mounted.
1559 yaffsfs_SetError(-EINVAL);
1565 // todo error - no device
1566 yaffsfs_SetError(-ENODEV);
1573 loff_t yaffs_freespace(const YCHAR *path)
1576 yaffs_Device *dev=NULL;
1580 dev = yaffsfs_FindDevice(path,&dummy);
1581 if(dev && dev->isMounted)
1583 retVal = yaffs_GetNumberOfFreeChunks(dev);
1584 retVal *= dev->nDataBytesPerChunk;
1589 yaffsfs_SetError(-EINVAL);
1596 loff_t yaffs_totalspace(const YCHAR *path)
1599 yaffs_Device *dev=NULL;
1603 dev = yaffsfs_FindDevice(path,&dummy);
1604 if(dev && dev->isMounted)
1606 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1607 retVal *= dev->nChunksPerBlock;
1608 retVal *= dev->nDataBytesPerChunk;
1613 yaffsfs_SetError(-EINVAL);
1620 int yaffs_inodecount(const YCHAR *path)
1623 yaffs_Device *dev=NULL;
1627 dev = yaffsfs_FindDevice(path,&dummy);
1628 if(dev && dev->isMounted) {
1629 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1630 if(nObjects > dev->nHardLinks)
1631 retVal = nObjects - dev->nHardLinks;
1635 yaffsfs_SetError(-EINVAL);
1644 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1647 yaffsfs_DeviceConfiguration *cfg;
1649 yaffsfs_configurationList = cfgList;
1651 yaffsfs_InitHandles();
1653 cfg = yaffsfs_configurationList;
1655 while(cfg && cfg->prefix && cfg->dev)
1657 cfg->dev->isMounted = 0;
1658 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1667 // Directory search stuff.
1670 // Directory search context
1672 // NB this is an opaque structure.
1678 yaffs_dirent de; /* directory entry being used by this dsc */
1679 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1680 yaffs_Object *dirObj; /* ptr to directory being searched */
1681 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1683 struct ylist_head others;
1684 } yaffsfs_DirectorySearchContext;
1688 static struct ylist_head search_contexts;
1691 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1695 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1699 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1700 dsc->nextReturn = NULL;
1702 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1703 yaffs_Object,siblings);
1706 /* Hey someone isn't playing nice! */
1710 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1714 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1716 if( dsc->nextReturn == NULL ||
1717 ylist_empty(&dsc->dirObj->variant.directoryVariant.children)){
1718 dsc->nextReturn = NULL;
1720 struct ylist_head *next = dsc->nextReturn->siblings.next;
1722 if( next == &dsc->dirObj->variant.directoryVariant.children)
1723 dsc->nextReturn = NULL; /* end of list */
1725 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1728 /* Hey someone isn't playing nice! */
1732 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1735 struct ylist_head *i;
1736 yaffsfs_DirectorySearchContext *dsc;
1738 /* if search contexts not initilised then skip */
1739 if(!search_contexts.next)
1742 /* Iterate through the directory search contexts.
1743 * If any are the one being removed, then advance the dsc to
1744 * the next one to prevent a hanging ptr.
1746 ylist_for_each(i, &search_contexts) {
1748 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1749 if(dsc->nextReturn == obj)
1750 yaffsfs_DirAdvance(dsc);
1756 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1758 yaffs_DIR *dir = NULL;
1759 yaffs_Object *obj = NULL;
1760 yaffsfs_DirectorySearchContext *dsc = NULL;
1764 obj = yaffsfs_FindObject(NULL,dirname,0);
1766 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1769 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1770 dir = (yaffs_DIR *)dsc;
1773 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1774 dsc->magic = YAFFS_MAGIC;
1776 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1777 YINIT_LIST_HEAD(&dsc->others);
1779 if(!search_contexts.next)
1780 YINIT_LIST_HEAD(&search_contexts);
1782 ylist_add(&dsc->others,&search_contexts);
1783 yaffsfs_SetDirRewound(dsc); }
1792 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1794 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1795 struct yaffs_dirent *retVal = NULL;
1799 if(dsc && dsc->magic == YAFFS_MAGIC){
1800 yaffsfs_SetError(0);
1801 if(dsc->nextReturn){
1802 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1803 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1804 dsc->de.d_off = dsc->offset++;
1805 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1806 if(yaffs_strlen(dsc->de.d_name) == 0)
1808 // this should not happen!
1809 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1811 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1813 yaffsfs_DirAdvance(dsc);
1819 yaffsfs_SetError(-EBADF);
1829 void yaffs_rewinddir(yaffs_DIR *dirp)
1831 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1835 yaffsfs_SetDirRewound(dsc);
1841 int yaffs_closedir(yaffs_DIR *dirp)
1843 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1847 ylist_del(&dsc->others); /* unhook from list */
1853 // end of directory stuff
1856 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1858 yaffs_Object *parent = NULL;
1862 int mode = 0; // ignore for now
1865 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1867 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1874 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1878 yaffsfs_SetError(-EINVAL);
1888 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1890 yaffs_Object *obj = NULL;
1896 obj = yaffsfs_FindObject(NULL,path,0);
1900 yaffsfs_SetError(-ENOENT);
1903 else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK)
1905 yaffsfs_SetError(-EINVAL);
1910 YCHAR *alias = obj->variant.symLinkVariant.alias;
1911 memset(buf,0,bufsiz);
1912 yaffs_strncpy(buf,alias,bufsiz - 1);
1919 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1921 // Creates a link called newpath to existing oldpath
1922 yaffs_Object *obj = NULL;
1923 yaffs_Object *target = NULL;
1929 obj = yaffsfs_FindObject(NULL,oldpath,0);
1930 target = yaffsfs_FindObject(NULL,newpath,0);
1934 yaffsfs_SetError(-ENOENT);
1939 yaffsfs_SetError(-EEXIST);
1944 yaffs_Object *newdir = NULL;
1945 yaffs_Object *link = NULL;
1949 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1953 yaffsfs_SetError(-ENOTDIR);
1956 else if(newdir->myDev != obj->myDev)
1958 yaffsfs_SetError(-EXDEV);
1961 if(newdir && yaffs_strlen(newname) > 0)
1963 link = yaffs_Link(newdir,newname,obj);
1968 yaffsfs_SetError(-ENOSPC);
1979 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1984 int yaffs_DumpDevStruct(const YCHAR *path)
1989 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1993 yaffs_Device *dev = obj->myDev;
1996 "nPageWrites.......... %d\n"
1997 "nPageReads........... %d\n"
1998 "nBlockErasures....... %d\n"
1999 "nGCCopies............ %d\n"
2000 "garbageCollections... %d\n"
2001 "passiveGarbageColl'ns %d\n"
2005 dev->nBlockErasures,
2007 dev->garbageCollections,
2008 dev->passiveGarbageCollections