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"
18 #include "yaffs_trace.h"
20 #include <string.h> // for memset
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
34 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.33 2010-02-16 23:24:57 charles Exp $";
36 // configurationList is the list of devices that are supported
37 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
40 /* Some forward references */
41 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
42 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
49 unsigned int yaffs_wr_attempts;
57 __u8 readOnly:1; // this handle is read only
58 __u8 append:1; // append only
59 __u8 exclusive:1; // exclusive
60 int inodeId:13; // the object
61 int useCount:16; // Use count for this handle
62 __u32 position; // current position in file
65 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
66 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
69 /// Inilitalise handles on start-up.
71 static int yaffsfs_InitHandles(void)
74 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
75 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
76 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
77 yaffsfs_handle[i].inodeId = -1;
82 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
84 if(h < 0 || h >= YAFFSFS_N_HANDLES)
87 return &yaffsfs_handle[h];
90 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
92 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
94 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
95 return &yaffsfs_inode[h->inodeId];
100 yaffs_Object *yaffsfs_GetHandleObject(int handle)
102 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
110 //yaffsfs_GetInodeIdForObject
111 // Grab an inode entry when opening a new inode.
114 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
118 yaffsfs_Inode *in = NULL;
121 obj = yaffs_GetEquivalentObject(obj);
123 /* Look for it. If we can't find it then make one */
124 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
125 if(yaffsfs_inode[i].iObj == obj)
129 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
130 if(!yaffsfs_inode[i].iObj)
136 in = &yaffsfs_inode[ret];
147 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
154 yaffs_DeleteObject(obj);
161 static void yaffsfs_PutInode(int inodeId)
163 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
164 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
167 yaffsfs_ReleaseInode(in);
173 // Grab a handle (when opening a file)
176 static int yaffsfs_GetNewHandle(void)
181 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
182 h = yaffsfs_GetHandlePointer(i);
184 // todo bug: should never happen
187 memset(h,0,sizeof(yaffsfs_Handle));
197 // Increase use of handle when reading/writing a file
198 static int yaffsfs_GetHandle(int handle)
200 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
202 if(h && h->useCount > 0){
209 // Let go of a handle when closing a file or aborting an open or
210 // ending a read or write.
212 static int yaffsfs_PutHandle(int handle)
214 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
216 if(h && h->useCount > 0){
220 yaffsfs_PutInode(h->inodeId);
231 // Stuff to search for a directory from a path
234 int yaffsfs_Match(YCHAR a, YCHAR b)
240 int yaffsfs_IsPathDivider(YCHAR ch)
242 YCHAR *str = YAFFS_PATH_DIVIDERS;
253 // yaffsfs_FindDevice
255 // Scan the configuration list to find the root.
256 // Curveballs: Should match paths that end in '/' too
257 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
258 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
260 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
261 const YCHAR *leftOver;
263 yaffs_Device *retval = NULL;
265 int longestMatch = -1;
268 // Check all configs, choose the one that:
269 // 1) Actually matches a prefix (ie /a amd /abc will not match
270 // 2) Matches the longest.
271 while(cfg && cfg->prefix && cfg->dev){
278 while(matching && *p && *leftOver){
280 while(yaffsfs_IsPathDivider(*p))
284 while(yaffsfs_IsPathDivider(*leftOver))
287 // Now match the text part
289 *p && !yaffsfs_IsPathDivider(*p) &&
290 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
291 if(yaffsfs_Match(*p,*leftOver)){
301 // Skip over any /s in leftOver
302 while(yaffsfs_IsPathDivider(*leftOver))
306 if( matching && (thisMatchLength > longestMatch)){
308 *restOfPath = (YCHAR *)leftOver;
310 longestMatch = thisMatchLength;
319 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
321 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
322 const YCHAR *leftOver;
324 yaffs_Device *retval = NULL;
326 int longestMatch = -1;
328 // Check all configs, choose the one that:
329 // 1) Actually matches a prefix (ie /a amd /abc will not match
330 // 2) Matches the longest.
331 while(cfg && cfg->prefix && cfg->dev){
336 while(*p && //unmatched part of prefix
337 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
339 yaffsfs_Match(*p,*leftOver)){
346 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
347 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
348 (thisMatchLength > longestMatch)){
350 *restOfPath = (YCHAR *)leftOver;
352 longestMatch = thisMatchLength;
360 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
365 dev= yaffsfs_FindDevice(path,restOfPath);
366 if(dev && dev->isMounted){
372 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
375 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
376 YCHAR *alias = obj->variant.symLinkVariant.alias;
378 if(yaffsfs_IsPathDivider(*alias))
379 // Starts with a /, need to scan from root up
380 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
382 // Relative to here, so use the parent of the symlink as a start
383 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
389 // yaffsfs_FindDirectory
390 // Parse a path to determine the directory and the name within the directory.
392 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
393 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
397 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
400 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
405 restOfPath = (YCHAR *)path;
408 dir = yaffsfs_FindRoot(path,&restOfPath);
412 // curve ball: also throw away surplus '/'
413 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
414 while(yaffsfs_IsPathDivider(*restOfPath))
415 restOfPath++; // get rid of '/'
420 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
421 if (i < YAFFS_MAX_NAME_LENGTH){
422 str[i] = *restOfPath;
430 // got to the end of the string
433 if(yaffs_strcmp(str,_Y(".")) == 0)
437 else if(yaffs_strcmp(str,_Y("..")) == 0)
440 dir = yaffs_FindObjectByName(dir,str);
442 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
443 dir = yaffsfs_FollowLink(dir,symDepth);
446 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
451 // directory did not exist.
455 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
457 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
460 // yaffsfs_FindObject turns a path for an existing object into the object
462 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
467 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
470 return yaffs_FindObjectByName(dir,name);
476 int yaffs_dup(int fd)
479 yaffsfs_Handle *oldPtr = NULL;
480 yaffsfs_Handle *newPtr = NULL;
484 oldPtr = yaffsfs_GetHandlePointer(fd);
485 if(oldPtr && oldPtr->useCount > 0)
486 newHandle = yaffsfs_GetNewHandle();
488 newPtr = yaffsfs_GetHandlePointer(newHandle);
496 yaffsfs_SetError(-EBADF);
498 yaffsfs_SetError(-ENOMEM);
504 int yaffs_open(const YCHAR *path, int oflag, int mode)
506 yaffs_Object *obj = NULL;
507 yaffs_Object *dir = NULL;
510 yaffsfs_Handle *h = NULL;
512 int alreadyExclusive = 0;
515 int errorReported = 0;
520 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
525 handle = yaffsfs_GetNewHandle();
529 h = yaffsfs_GetHandlePointer(handle);
532 // try to find the exisiting object
533 obj = yaffsfs_FindObject(NULL,path,0);
535 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
536 obj = yaffsfs_FollowLink(obj,symDepth++);
539 obj = yaffs_GetEquivalentObject(obj);
541 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
545 // Check if the object is already in use
546 alreadyOpen = alreadyExclusive = 0;
548 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
550 yaffsfs_handle[i].useCount > 0 &&
551 obj == yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj){
553 if(yaffsfs_handle[i].exclusive)
554 alreadyExclusive = 1;
558 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
561 // Open should fail if O_CREAT and O_EXCL are specified
562 if((oflag & O_EXCL) && (oflag & O_CREAT)){
564 yaffsfs_SetError(-EEXIST);
568 // Check file permissions
569 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
570 !(obj->yst_mode & S_IREAD))
573 if( (oflag & O_RDWR) &&
574 !(obj->yst_mode & S_IREAD))
577 if( (oflag & (O_RDWR | O_WRONLY)) &&
578 !(obj->yst_mode & S_IWRITE))
581 } else if((oflag & O_CREAT)) {
582 // Let's see if we can create this file
583 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
584 if(dir && dir->myDev->readOnly){
585 yaffsfs_SetError(-EINVAL);
588 obj = yaffs_MknodFile(dir,name,mode,0,0);
590 yaffsfs_SetError(-ENOTDIR);
595 if(obj && !openDenied) {
596 int inodeId = yaffsfs_GetInodeIdForObject(obj);
600 * Todo: Fix any problem if inodes run out, though that
601 * can't happen if the number of inode items >= number of handles.
605 h->inodeId = inodeId;
606 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
607 h->append = (oflag & O_APPEND) ? 1 : 0;
608 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
611 /* Hook inode to object */
612 obj->myInode = (void*) &yaffsfs_inode[inodeId];
614 if((oflag & O_TRUNC) && !h->readOnly)
615 yaffs_ResizeFile(obj,0);
617 yaffsfs_PutHandle(handle);
619 yaffsfs_SetError(-EACCES);
631 int yaffs_Dofsync(int fd,int datasync)
633 yaffsfs_Handle *h = NULL;
638 h = yaffsfs_GetHandlePointer(fd);
640 if(h && h->useCount > 0)
642 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
645 yaffsfs_SetError(-EBADF);
654 int yaffs_fsync(int fd)
656 return yaffs_Dofsync(fd,0);
659 int yaffs_flush(int fd)
661 return yaffs_fsync(fd);
664 int yaffs_fdatasync(int fd)
666 return yaffs_Dofsync(fd,1);
669 int yaffs_close(int fd)
671 yaffsfs_Handle *h = NULL;
676 h = yaffsfs_GetHandlePointer(fd);
678 if(h && h->useCount > 0) {
680 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
681 yaffsfs_PutHandle(fd);
685 yaffsfs_SetError(-EBADF);
696 int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offset)
698 yaffsfs_Handle *h = NULL;
699 yaffs_Object *obj = NULL;
705 unsigned int maxRead;
708 h = yaffsfs_GetHandlePointer(fd);
709 obj = yaffsfs_GetHandleObject(fd);
713 yaffsfs_SetError(-EBADF);
715 } else if( h && obj){
719 startPos = h->position;
721 if(yaffs_GetObjectFileLength(obj) > pos)
722 maxRead = yaffs_GetObjectFileLength(obj) - pos;
731 yaffsfs_GetHandle(fd);
735 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
739 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
750 nbyte = 0; /* no more to read */
760 yaffsfs_PutHandle(fd);
763 h->position = startPos + totalRead;
775 return (totalRead >= 0) ? totalRead : -1;
779 int yaffs_read(int fd, void *buf, unsigned int nbyte)
781 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
784 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
786 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
789 int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite, int offset)
791 yaffsfs_Handle *h = NULL;
792 yaffs_Object *obj = NULL;
796 int totalWritten = 0;
797 int writeThrough = 0;
801 h = yaffsfs_GetHandlePointer(fd);
802 obj = yaffsfs_GetHandleObject(fd);
806 yaffsfs_SetError(-EBADF);
808 } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
809 yaffsfs_SetError(-EINVAL);
811 } else if( h && obj){
815 startPos = yaffs_GetObjectFileLength(obj);
817 startPos = h->position;
819 yaffsfs_GetHandle(fd);
822 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
826 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
828 totalWritten += nWritten;
833 if(nWritten == nToWrite)
838 if(nWritten < 1 && totalWritten < 1){
839 yaffsfs_SetError(-ENOSPC);
849 yaffsfs_PutHandle(fd);
853 h->position = startPos + totalWritten;
865 return (totalWritten >= 0) ? totalWritten : -1;
869 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
871 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
874 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
876 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
880 int yaffs_truncate(const YCHAR *path,off_t newSize)
882 yaffs_Object *obj = NULL;
883 int result = YAFFS_FAIL;
887 obj = yaffsfs_FindObject(NULL,path,0);
889 obj = yaffs_GetEquivalentObject(obj);
892 yaffsfs_SetError(-ENOENT);
893 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
894 yaffsfs_SetError(-EISDIR);
895 else if(obj->myDev->readOnly)
896 yaffsfs_SetError(-EINVAL);
898 result = yaffs_ResizeFile(obj,newSize);
903 return (result) ? 0 : -1;
906 int yaffs_ftruncate(int fd, off_t newSize)
908 yaffsfs_Handle *h = NULL;
909 yaffs_Object *obj = NULL;
913 h = yaffsfs_GetHandlePointer(fd);
914 obj = yaffsfs_GetHandleObject(fd);
918 yaffsfs_SetError(-EBADF);
919 else if(obj->myDev->readOnly)
920 yaffsfs_SetError(-EINVAL);
923 result = yaffs_ResizeFile(obj,newSize);
927 return (result) ? 0 : -1;
931 off_t yaffs_lseek(int fd, off_t offset, int whence)
933 yaffsfs_Handle *h = NULL;
934 yaffs_Object *obj = NULL;
939 h = yaffsfs_GetHandlePointer(fd);
940 obj = yaffsfs_GetHandleObject(fd);
944 yaffsfs_SetError(-EBADF);
945 else if(whence == SEEK_SET){
949 else if(whence == SEEK_CUR) {
950 if( (h->position + offset) >= 0)
951 pos = (h->position + offset);
953 else if(whence == SEEK_END) {
954 fSize = yaffs_GetObjectFileLength(obj);
955 if(fSize >= 0 && (fSize + offset) >= 0)
956 pos = fSize + offset;
972 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
974 yaffs_Object *dir = NULL;
975 yaffs_Object *obj = NULL;
977 int result = YAFFS_FAIL;
981 obj = yaffsfs_FindObject(NULL,path,0);
982 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
984 yaffsfs_SetError(-ENOTDIR);
986 yaffsfs_SetError(-ENOENT);
987 else if(obj->myDev->readOnly)
988 yaffsfs_SetError(-EINVAL);
989 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
990 yaffsfs_SetError(-EISDIR);
991 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
992 yaffsfs_SetError(-ENOTDIR);
994 result = yaffs_Unlink(dir,name);
996 if(result == YAFFS_FAIL && isDirectory)
997 yaffsfs_SetError(-ENOTEMPTY);
1004 return (result == YAFFS_FAIL) ? -1 : 0;
1008 int yaffs_rmdir(const YCHAR *path)
1010 return yaffsfs_DoUnlink(path,1);
1013 int yaffs_unlink(const YCHAR *path)
1015 return yaffsfs_DoUnlink(path,0);
1018 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1020 yaffs_Object *olddir = NULL;
1021 yaffs_Object *newdir = NULL;
1022 yaffs_Object *obj = NULL;
1025 int result= YAFFS_FAIL;
1026 int renameAllowed = 1;
1030 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1031 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1032 obj = yaffsfs_FindObject(NULL,oldPath,0);
1034 if(!olddir || !newdir || !obj) {
1036 yaffsfs_SetError(-EBADF);
1038 } else if(obj->myDev->readOnly){
1039 yaffsfs_SetError(-EINVAL);
1041 } else if(olddir->myDev != newdir->myDev) {
1042 // oops must be on same device
1044 yaffsfs_SetError(-EXDEV);
1046 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
1047 // It is a directory, check that it is not being renamed to
1048 // being its own decendent.
1049 // Do this by tracing from the new directory back to the root, checking for obj
1051 yaffs_Object *xx = newdir;
1053 while( renameAllowed && xx){
1059 yaffsfs_SetError(-EACCES);
1063 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1067 return (result == YAFFS_FAIL) ? -1 : 0;
1071 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1076 obj = yaffs_GetEquivalentObject(obj);
1079 buf->st_dev = (int)obj->myDev->genericDevice;
1080 buf->st_ino = obj->objectId;
1081 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1083 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1084 buf->st_mode |= S_IFDIR;
1085 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1086 buf->st_mode |= S_IFLNK;
1087 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1088 buf->st_mode |= S_IFREG;
1090 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1093 buf->st_rdev = obj->yst_rdev;
1094 buf->st_size = yaffs_GetObjectFileLength(obj);
1095 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1096 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1097 #if CONFIG_YAFFS_WINCE
1098 buf->yst_wince_atime[0] = obj->win_atime[0];
1099 buf->yst_wince_atime[1] = obj->win_atime[1];
1100 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1101 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1102 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1103 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1105 buf->yst_atime = obj->yst_atime;
1106 buf->yst_ctime = obj->yst_ctime;
1107 buf->yst_mtime = obj->yst_mtime;
1114 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1121 obj = yaffsfs_FindObject(NULL,path,0);
1124 obj = yaffsfs_FollowLink(obj,0);
1127 retVal = yaffsfs_DoStat(obj,buf);
1129 // todo error not found
1130 yaffsfs_SetError(-ENOENT);
1138 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1140 return yaffsfs_DoStatOrLStat(path,buf,0);
1143 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1145 return yaffsfs_DoStatOrLStat(path,buf,1);
1148 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1155 obj = yaffsfs_GetHandleObject(fd);
1158 retVal = yaffsfs_DoStat(obj,buf);
1161 yaffsfs_SetError(-EBADF);
1168 #ifdef CONFIG_YAFFS_WINCE
1169 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1176 obj = yaffsfs_GetHandleObject(fd);
1181 wctime[0] = obj->win_ctime[0];
1182 wctime[1] = obj->win_ctime[1];
1185 watime[0] = obj->win_atime[0];
1186 watime[1] = obj->win_atime[1];
1189 wmtime[0] = obj->win_mtime[0];
1190 wmtime[1] = obj->win_mtime[1];
1197 yaffsfs_SetError(-EBADF);
1205 int yaffs_set_wince_times(int fd,
1206 const unsigned *wctime,
1207 const unsigned *watime,
1208 const unsigned *wmtime)
1215 obj = yaffsfs_GetHandleObject(fd);
1220 obj->win_ctime[0] = wctime[0];
1221 obj->win_ctime[1] = wctime[1];
1224 obj->win_atime[0] = watime[0];
1225 obj->win_atime[1] = watime[1];
1228 obj->win_mtime[0] = wmtime[0];
1229 obj->win_mtime[1] = wmtime[1];
1233 result = yaffs_FlushFile(obj,0,0);
1237 yaffsfs_SetError(-EBADF);
1247 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1252 obj = yaffs_GetEquivalentObject(obj);
1255 obj->yst_mode = mode;
1257 result = yaffs_FlushFile(obj,0,0);
1260 return result == YAFFS_OK ? 0 : -1;
1264 int yaffs_access(const YCHAR *path, int amode)
1271 obj = yaffsfs_FindObject(NULL,path,0);
1276 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1278 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1280 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1284 yaffsfs_SetError(-EACCES);
1288 // todo error not found
1289 yaffsfs_SetError(-ENOENT);
1300 int yaffs_chmod(const YCHAR *path, mode_t mode)
1307 obj = yaffsfs_FindObject(NULL,path,0);
1310 yaffsfs_SetError(-ENOENT);
1311 else if(obj->myDev->readOnly)
1312 yaffsfs_SetError(-EINVAL);
1314 retVal = yaffsfs_DoChMod(obj,mode);
1323 int yaffs_fchmod(int fd, mode_t mode)
1330 obj = yaffsfs_GetHandleObject(fd);
1333 yaffsfs_SetError(-ENOENT);
1334 else if(obj->myDev->readOnly)
1335 yaffsfs_SetError(-EINVAL);
1337 retVal = yaffsfs_DoChMod(obj,mode);
1345 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1347 yaffs_Object *parent = NULL;
1348 yaffs_Object *dir = NULL;
1353 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1354 if(parent && parent->myDev->readOnly){
1355 yaffsfs_SetError(-EINVAL);
1358 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1363 yaffsfs_SetError(-ENOENT); // missing path
1364 else if (yaffs_FindObjectByName(parent,name))
1365 yaffsfs_SetError(-EEXIST); // the name already exists
1367 yaffsfs_SetError(-ENOSPC); // just assume no space
1377 int yaffs_mount2(const YCHAR *path,int readOnly)
1380 int result=YAFFS_FAIL;
1381 yaffs_Device *dev=NULL;
1384 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1387 dev = yaffsfs_FindDevice(path,&dummy);
1389 if(!dev->isMounted){
1390 dev->readOnly = readOnly ? 1 : 0;
1391 result = yaffs_GutsInitialise(dev);
1392 if(result == YAFFS_FAIL)
1393 // todo error - mount failed
1394 yaffsfs_SetError(-ENOMEM);
1395 retVal = result ? 0 : -1;
1399 //todo error - already mounted.
1400 yaffsfs_SetError(-EBUSY);
1402 // todo error - no device
1403 yaffsfs_SetError(-ENODEV);
1410 int yaffs_mount(const YCHAR *path)
1412 return yaffs_mount2(path,0);
1415 int yaffs_sync(const YCHAR *path)
1418 yaffs_Device *dev=NULL;
1422 dev = yaffsfs_FindDevice(path,&dummy);
1426 yaffs_FlushEntireDeviceCache(dev);
1427 yaffs_CheckpointSave(dev);
1431 //todo error - not mounted.
1432 yaffsfs_SetError(-EINVAL);
1435 // todo error - no device
1436 yaffsfs_SetError(-ENODEV);
1443 int yaffs_remount(const YCHAR *path, int force, int readOnly)
1446 yaffs_Device *dev=NULL;
1450 dev = yaffsfs_FindDevice(path,&dummy);
1456 yaffs_FlushEntireDeviceCache(dev);
1458 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1459 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1460 inUse = 1; // the device is in use, can't unmount
1463 if(!inUse || force){
1465 yaffs_CheckpointSave(dev);
1466 dev->readOnly = readOnly ? 1 : 0;
1469 yaffsfs_SetError(-EBUSY);
1472 yaffsfs_SetError(-EINVAL);
1476 yaffsfs_SetError(-ENODEV);
1483 int yaffs_unmount2(const YCHAR *path, int force)
1486 yaffs_Device *dev=NULL;
1490 dev = yaffsfs_FindDevice(path,&dummy);
1496 yaffs_FlushEntireDeviceCache(dev);
1497 yaffs_CheckpointSave(dev);
1499 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1500 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1501 inUse = 1; // the device is in use, can't unmount
1504 if(!inUse || force){
1505 yaffs_Deinitialise(dev);
1509 // todo error can't unmount as files are open
1510 yaffsfs_SetError(-EBUSY);
1513 //todo error - not mounted.
1514 yaffsfs_SetError(-EINVAL);
1518 // todo error - no device
1519 yaffsfs_SetError(-ENODEV);
1526 int yaffs_unmount(const YCHAR *path)
1528 return yaffs_unmount2(path,0);
1531 loff_t yaffs_freespace(const YCHAR *path)
1534 yaffs_Device *dev=NULL;
1538 dev = yaffsfs_FindDevice(path,&dummy);
1539 if(dev && dev->isMounted){
1540 retVal = yaffs_GetNumberOfFreeChunks(dev);
1541 retVal *= dev->nDataBytesPerChunk;
1544 yaffsfs_SetError(-EINVAL);
1550 loff_t yaffs_totalspace(const YCHAR *path)
1553 yaffs_Device *dev=NULL;
1557 dev = yaffsfs_FindDevice(path,&dummy);
1558 if(dev && dev->isMounted){
1559 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1560 retVal *= dev->nChunksPerBlock;
1561 retVal *= dev->nDataBytesPerChunk;
1564 yaffsfs_SetError(-EINVAL);
1570 int yaffs_inodecount(const YCHAR *path)
1573 yaffs_Device *dev=NULL;
1577 dev = yaffsfs_FindDevice(path,&dummy);
1578 if(dev && dev->isMounted) {
1579 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1580 if(nObjects > dev->nHardLinks)
1581 retVal = nObjects - dev->nHardLinks;
1585 yaffsfs_SetError(-EINVAL);
1593 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1596 yaffsfs_DeviceConfiguration *cfg;
1598 yaffsfs_configurationList = cfgList;
1600 yaffsfs_InitHandles();
1602 cfg = yaffsfs_configurationList;
1604 while(cfg && cfg->prefix && cfg->dev){
1605 cfg->dev->isMounted = 0;
1606 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1615 // Directory search stuff.
1618 // Directory search context
1620 // NB this is an opaque structure.
1626 yaffs_dirent de; /* directory entry being used by this dsc */
1627 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1628 yaffs_Object *dirObj; /* ptr to directory being searched */
1629 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1631 struct ylist_head others;
1632 } yaffsfs_DirectorySearchContext;
1636 static struct ylist_head search_contexts;
1639 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1643 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1647 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1648 dsc->nextReturn = NULL;
1650 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1651 yaffs_Object,siblings);
1653 /* Hey someone isn't playing nice! */
1657 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1661 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1663 if( dsc->nextReturn == NULL ||
1664 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1665 dsc->nextReturn = NULL;
1667 struct ylist_head *next = dsc->nextReturn->siblings.next;
1669 if( next == &dsc->dirObj->variant.directoryVariant.children)
1670 dsc->nextReturn = NULL; /* end of list */
1672 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1675 /* Hey someone isn't playing nice! */
1679 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1682 struct ylist_head *i;
1683 yaffsfs_DirectorySearchContext *dsc;
1685 /* if search contexts not initilised then skip */
1686 if(!search_contexts.next)
1689 /* Iterate through the directory search contexts.
1690 * If any are the one being removed, then advance the dsc to
1691 * the next one to prevent a hanging ptr.
1693 ylist_for_each(i, &search_contexts) {
1695 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1696 if(dsc->nextReturn == obj)
1697 yaffsfs_DirAdvance(dsc);
1703 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1705 yaffs_DIR *dir = NULL;
1706 yaffs_Object *obj = NULL;
1707 yaffsfs_DirectorySearchContext *dsc = NULL;
1711 obj = yaffsfs_FindObject(NULL,dirname,0);
1713 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1715 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1716 dir = (yaffs_DIR *)dsc;
1719 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1720 dsc->magic = YAFFS_MAGIC;
1722 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1723 YINIT_LIST_HEAD(&dsc->others);
1725 if(!search_contexts.next)
1726 YINIT_LIST_HEAD(&search_contexts);
1728 ylist_add(&dsc->others,&search_contexts);
1729 yaffsfs_SetDirRewound(dsc);
1739 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1741 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1742 struct yaffs_dirent *retVal = NULL;
1746 if(dsc && dsc->magic == YAFFS_MAGIC){
1747 yaffsfs_SetError(0);
1748 if(dsc->nextReturn){
1749 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1750 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1751 dsc->de.d_off = dsc->offset++;
1752 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1753 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1755 // this should not happen!
1756 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1758 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1760 yaffsfs_DirAdvance(dsc);
1764 yaffsfs_SetError(-EBADF);
1773 void yaffs_rewinddir(yaffs_DIR *dirp)
1775 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1779 yaffsfs_SetDirRewound(dsc);
1785 int yaffs_closedir(yaffs_DIR *dirp)
1787 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1791 ylist_del(&dsc->others); /* unhook from list */
1797 // end of directory stuff
1800 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1802 yaffs_Object *parent = NULL;
1806 int mode = 0; // ignore for now
1809 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1810 if(parent && parent->myDev->readOnly)
1811 yaffsfs_SetError(-EINVAL);
1813 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1817 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1821 yaffsfs_SetError(-EINVAL);
1831 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1833 yaffs_Object *obj = NULL;
1839 obj = yaffsfs_FindObject(NULL,path,0);
1842 yaffsfs_SetError(-ENOENT);
1844 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1845 yaffsfs_SetError(-EINVAL);
1848 YCHAR *alias = obj->variant.symLinkVariant.alias;
1849 memset(buf,0,bufsiz);
1850 yaffs_strncpy(buf,alias,bufsiz - 1);
1857 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1859 // Creates a link called newpath to existing oldpath
1860 yaffs_Object *obj = NULL;
1861 yaffs_Object *target = NULL;
1863 int newNameLength = 0;
1868 obj = yaffsfs_FindObject(NULL,oldpath,0);
1869 target = yaffsfs_FindObject(NULL,newpath,0);
1872 yaffsfs_SetError(-ENOENT);
1874 } else if(obj->myDev->readOnly){
1875 yaffsfs_SetError(-EINVAL);
1878 yaffsfs_SetError(-EEXIST);
1881 yaffs_Object *newdir = NULL;
1882 yaffs_Object *link = NULL;
1886 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1889 yaffsfs_SetError(-ENOTDIR);
1891 }else if(newdir->myDev != obj->myDev){
1892 yaffsfs_SetError(-EXDEV);
1896 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1898 if(newNameLength == 0){
1899 yaffsfs_SetError(-ENOENT);
1901 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1902 yaffsfs_SetError(-ENAMETOOLONG);
1907 link = yaffs_Link(newdir,newname,obj);
1911 yaffsfs_SetError(-ENOSPC);
1922 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1927 int yaffs_DumpDevStruct(const YCHAR *path)
1932 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1935 yaffs_Device *dev = obj->myDev;
1938 "nPageWrites.......... %d\n"
1939 "nPageReads........... %d\n"
1940 "nBlockErasures....... %d\n"
1941 "nGCCopies............ %d\n"
1942 "garbageCollections... %d\n"
1943 "passiveGarbageColl'ns %d\n"
1947 dev->nBlockErasures,
1949 dev->garbageCollections,
1950 dev->passiveGarbageCollections