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 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.31 2010-01-11 04:06:47 charles Exp $";
31 // configurationList is the list of devices that are supported
32 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
35 /* Some forward references */
36 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
37 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
44 unsigned int yaffs_wr_attempts;
52 __u8 inUse:1; // this handle is in use
53 __u8 readOnly:1; // this handle is read only
54 __u8 append:1; // append only
55 __u8 exclusive:1; // exclusive
56 int inodeId:20; // the object
57 __u32 position; // current position in file
60 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
61 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
64 /// Inilitalise handles on start-up.
66 static int yaffsfs_InitHandles(void)
69 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
70 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
71 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
72 yaffsfs_handle[i].inodeId = -1;
77 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
79 if(h < 0 || h >= YAFFSFS_N_HANDLES)
82 return &yaffsfs_handle[h];
85 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
87 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
89 if(h && h->inUse && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
90 return &yaffsfs_inode[h->inodeId];
95 yaffs_Object *yaffsfs_GetHandleObject(int handle)
97 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
105 //yaffsfs_GetInodeIdForObject
106 // Grab an inode entry when opening a new inode.
109 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
113 yaffsfs_Inode *in = NULL;
116 obj = yaffs_GetEquivalentObject(obj);
118 /* Look for it. If we can't find it then make one */
119 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
120 if(yaffsfs_inode[i].iObj == obj)
124 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
125 if(!yaffsfs_inode[i].iObj)
131 in = &yaffsfs_inode[ret];
142 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
149 yaffs_DeleteObject(obj);
156 static void yaffsfs_PutInode(int inodeId)
158 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
159 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
162 yaffsfs_ReleaseInode(in);
168 // Grab a handle (when opening a file)
171 static int yaffsfs_GetHandle(void)
176 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
177 h = yaffsfs_GetHandlePointer(i);
179 // todo bug: should never happen
182 memset(h,0,sizeof(yaffsfs_Handle));
192 // Let go of a handle when closing a file or aborting an open.
194 static int yaffsfs_PutHandle(int handle)
196 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
200 yaffsfs_PutInode(h->inodeId);
210 // Stuff to search for a directory from a path
213 int yaffsfs_Match(YCHAR a, YCHAR b)
219 int yaffsfs_IsPathDivider(YCHAR ch)
221 YCHAR *str = YAFFS_PATH_DIVIDERS;
232 // yaffsfs_FindDevice
234 // Scan the configuration list to find the root.
235 // Curveballs: Should match paths that end in '/' too
236 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
237 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
239 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
240 const YCHAR *leftOver;
242 yaffs_Device *retval = NULL;
244 int longestMatch = -1;
247 // Check all configs, choose the one that:
248 // 1) Actually matches a prefix (ie /a amd /abc will not match
249 // 2) Matches the longest.
250 while(cfg && cfg->prefix && cfg->dev){
257 while(matching && *p && *leftOver){
259 while(yaffsfs_IsPathDivider(*p))
263 while(yaffsfs_IsPathDivider(*leftOver))
266 // Now match the text part
268 *p && !yaffsfs_IsPathDivider(*p) &&
269 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
270 if(yaffsfs_Match(*p,*leftOver)){
280 // Skip over any /s in leftOver
281 while(yaffsfs_IsPathDivider(*leftOver))
285 if( matching && (thisMatchLength > longestMatch)){
287 *restOfPath = (YCHAR *)leftOver;
289 longestMatch = thisMatchLength;
298 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
300 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
301 const YCHAR *leftOver;
303 yaffs_Device *retval = NULL;
305 int longestMatch = -1;
307 // Check all configs, choose the one that:
308 // 1) Actually matches a prefix (ie /a amd /abc will not match
309 // 2) Matches the longest.
310 while(cfg && cfg->prefix && cfg->dev){
315 while(*p && //unmatched part of prefix
316 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
318 yaffsfs_Match(*p,*leftOver)){
325 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
326 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
327 (thisMatchLength > longestMatch)){
329 *restOfPath = (YCHAR *)leftOver;
331 longestMatch = thisMatchLength;
339 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
344 dev= yaffsfs_FindDevice(path,restOfPath);
345 if(dev && dev->isMounted){
351 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
354 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
355 YCHAR *alias = obj->variant.symLinkVariant.alias;
357 if(yaffsfs_IsPathDivider(*alias))
358 // Starts with a /, need to scan from root up
359 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
361 // Relative to here, so use the parent of the symlink as a start
362 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
368 // yaffsfs_FindDirectory
369 // Parse a path to determine the directory and the name within the directory.
371 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
372 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
376 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
379 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
384 restOfPath = (YCHAR *)path;
387 dir = yaffsfs_FindRoot(path,&restOfPath);
391 // curve ball: also throw away surplus '/'
392 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
393 while(yaffsfs_IsPathDivider(*restOfPath))
394 restOfPath++; // get rid of '/'
399 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
400 if (i < YAFFS_MAX_NAME_LENGTH){
401 str[i] = *restOfPath;
409 // got to the end of the string
412 if(yaffs_strcmp(str,_Y(".")) == 0)
416 else if(yaffs_strcmp(str,_Y("..")) == 0)
419 dir = yaffs_FindObjectByName(dir,str);
421 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
422 dir = yaffsfs_FollowLink(dir,symDepth);
425 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
430 // directory did not exist.
434 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
436 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
439 // yaffsfs_FindObject turns a path for an existing object into the object
441 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
446 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
449 return yaffs_FindObjectByName(dir,name);
455 int yaffs_dup(int fd)
458 yaffsfs_Handle *oldPtr = NULL;
459 yaffsfs_Handle *newPtr = NULL;
463 oldPtr = yaffsfs_GetHandlePointer(fd);
464 if(oldPtr && oldPtr->inUse)
465 newHandle = yaffsfs_GetHandle();
467 newPtr = yaffsfs_GetHandlePointer(newHandle);
475 yaffsfs_SetError(-EBADF);
477 yaffsfs_SetError(-ENOMEM);
483 int yaffs_open(const YCHAR *path, int oflag, int mode)
485 yaffs_Object *obj = NULL;
486 yaffs_Object *dir = NULL;
489 yaffsfs_Handle *h = NULL;
491 int alreadyExclusive = 0;
494 int errorReported = 0;
499 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
504 handle = yaffsfs_GetHandle();
508 h = yaffsfs_GetHandlePointer(handle);
511 // try to find the exisiting object
512 obj = yaffsfs_FindObject(NULL,path,0);
514 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
515 obj = yaffsfs_FollowLink(obj,symDepth++);
518 obj = yaffs_GetEquivalentObject(obj);
520 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
524 // Check if the object is already in use
525 alreadyOpen = alreadyExclusive = 0;
527 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
529 yaffsfs_handle[i].inUse &&
530 obj == yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj){
532 if(yaffsfs_handle[i].exclusive)
533 alreadyExclusive = 1;
537 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
540 // Open should fail if O_CREAT and O_EXCL are specified
541 if((oflag & O_EXCL) && (oflag & O_CREAT)){
543 yaffsfs_SetError(-EEXIST);
547 // Check file permissions
548 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
549 !(obj->yst_mode & S_IREAD))
552 if( (oflag & O_RDWR) &&
553 !(obj->yst_mode & S_IREAD))
556 if( (oflag & (O_RDWR | O_WRONLY)) &&
557 !(obj->yst_mode & S_IWRITE))
560 } else if((oflag & O_CREAT)) {
561 // Let's see if we can create this file
562 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
564 obj = yaffs_MknodFile(dir,name,mode,0,0);
566 yaffsfs_SetError(-ENOTDIR);
571 if(obj && !openDenied) {
572 int inodeId = yaffsfs_GetInodeIdForObject(obj);
576 * Todo: Fix any problem if inodes run out, though that
577 * can't happen if the number of inode items >= number of handles.
581 h->inodeId = inodeId;
583 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
584 h->append = (oflag & O_APPEND) ? 1 : 0;
585 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
588 /* Hook inode to object */
589 obj->myInode = (void*) &yaffsfs_inode[inodeId];
591 if((oflag & O_TRUNC) && !h->readOnly)
592 yaffs_ResizeFile(obj,0);
594 yaffsfs_PutHandle(handle);
596 yaffsfs_SetError(-EACCES);
608 int yaffs_Dofsync(int fd,int datasync)
610 yaffsfs_Handle *h = NULL;
615 h = yaffsfs_GetHandlePointer(fd);
619 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
622 yaffsfs_SetError(-EBADF);
631 int yaffs_fsync(int fd)
633 return yaffs_Dofsync(fd,0);
636 int yaffs_flush(int fd)
638 return yaffs_fsync(fd);
641 int yaffs_fdatasync(int fd)
643 return yaffs_Dofsync(fd,1);
646 int yaffs_close(int fd)
648 yaffsfs_Handle *h = NULL;
653 h = yaffsfs_GetHandlePointer(fd);
657 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
658 yaffsfs_PutHandle(fd);
662 yaffsfs_SetError(-EBADF);
671 int yaffs_read(int fd, void *buf, unsigned int nbyte)
673 yaffsfs_Handle *h = NULL;
674 yaffs_Object *obj = NULL;
677 unsigned int maxRead;
680 h = yaffsfs_GetHandlePointer(fd);
681 obj = yaffsfs_GetHandleObject(fd);
685 yaffsfs_SetError(-EBADF);
689 if(yaffs_GetObjectFileLength(obj) > pos)
690 maxRead = yaffs_GetObjectFileLength(obj) - pos;
699 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
701 h->position = pos + nRead;
713 return (nRead >= 0) ? nRead : -1;
717 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
719 yaffsfs_Handle *h = NULL;
720 yaffs_Object *obj = NULL;
723 unsigned int maxRead;
726 h = yaffsfs_GetHandlePointer(fd);
727 obj = yaffsfs_GetHandleObject(fd);
731 yaffsfs_SetError(-EBADF);
734 if(yaffs_GetObjectFileLength(obj) > pos)
735 maxRead = yaffs_GetObjectFileLength(obj) - pos;
744 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
752 return (nRead >= 0) ? nRead : -1;
756 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
758 yaffsfs_Handle *h = NULL;
759 yaffs_Object *obj = NULL;
762 int writeThrough = 0;
765 h = yaffsfs_GetHandlePointer(fd);
766 obj = yaffsfs_GetHandleObject(fd);
770 yaffsfs_SetError(-EBADF);
771 else if( h && obj && h->readOnly){
773 } else if( h && obj){
775 pos = yaffs_GetObjectFileLength(obj);
779 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
782 h->position = pos + nWritten;
792 return (nWritten >= 0) ? nWritten : -1;
796 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
798 yaffsfs_Handle *h = NULL;
799 yaffs_Object *obj = NULL;
802 int writeThrough = 0;
805 h = yaffsfs_GetHandlePointer(fd);
806 obj = yaffsfs_GetHandleObject(fd);
810 yaffsfs_SetError(-EBADF);
811 else if( h && obj && h->readOnly){
817 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
819 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
820 yaffsfs_SetError(-ENOSPC);
827 return (nWritten >= 0) ? nWritten : -1;
832 int yaffs_truncate(const YCHAR *path,off_t newSize)
834 yaffs_Object *obj = NULL;
835 int result = YAFFS_FAIL;
839 obj = yaffsfs_FindObject(NULL,path,0);
841 obj = yaffs_GetEquivalentObject(obj);
844 yaffsfs_SetError(-ENOENT);
845 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
846 yaffsfs_SetError(-EISDIR);
848 result = yaffs_ResizeFile(obj,newSize);
853 return (result) ? 0 : -1;
856 int yaffs_ftruncate(int fd, off_t newSize)
858 yaffsfs_Handle *h = NULL;
859 yaffs_Object *obj = NULL;
863 h = yaffsfs_GetHandlePointer(fd);
864 obj = yaffsfs_GetHandleObject(fd);
868 yaffsfs_SetError(-EBADF);
871 result = yaffs_ResizeFile(obj,newSize);
875 return (result) ? 0 : -1;
879 off_t yaffs_lseek(int fd, off_t offset, int whence)
881 yaffsfs_Handle *h = NULL;
882 yaffs_Object *obj = NULL;
887 h = yaffsfs_GetHandlePointer(fd);
888 obj = yaffsfs_GetHandleObject(fd);
892 yaffsfs_SetError(-EBADF);
893 else if(whence == SEEK_SET){
897 else if(whence == SEEK_CUR) {
898 if( (h->position + offset) >= 0)
899 pos = (h->position + offset);
901 else if(whence == SEEK_END) {
902 fSize = yaffs_GetObjectFileLength(obj);
903 if(fSize >= 0 && (fSize + offset) >= 0)
904 pos = fSize + offset;
920 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
922 yaffs_Object *dir = NULL;
923 yaffs_Object *obj = NULL;
925 int result = YAFFS_FAIL;
929 obj = yaffsfs_FindObject(NULL,path,0);
930 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
932 yaffsfs_SetError(-ENOTDIR);
934 yaffsfs_SetError(-ENOENT);
935 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
936 yaffsfs_SetError(-EISDIR);
937 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
938 yaffsfs_SetError(-ENOTDIR);
940 result = yaffs_Unlink(dir,name);
942 if(result == YAFFS_FAIL && isDirectory)
943 yaffsfs_SetError(-ENOTEMPTY);
950 return (result == YAFFS_FAIL) ? -1 : 0;
954 int yaffs_rmdir(const YCHAR *path)
956 return yaffsfs_DoUnlink(path,1);
959 int yaffs_unlink(const YCHAR *path)
961 return yaffsfs_DoUnlink(path,0);
964 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
966 yaffs_Object *olddir = NULL;
967 yaffs_Object *newdir = NULL;
968 yaffs_Object *obj = NULL;
971 int result= YAFFS_FAIL;
972 int renameAllowed = 1;
976 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
977 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
978 obj = yaffsfs_FindObject(NULL,oldPath,0);
980 if(!olddir || !newdir || !obj) {
982 yaffsfs_SetError(-EBADF);
984 } else if(olddir->myDev != newdir->myDev) {
985 // oops must be on same device
987 yaffsfs_SetError(-EXDEV);
989 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
990 // It is a directory, check that it is not being renamed to
991 // being its own decendent.
992 // Do this by tracing from the new directory back to the root, checking for obj
994 yaffs_Object *xx = newdir;
996 while( renameAllowed && xx){
1002 yaffsfs_SetError(-EACCES);
1006 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1010 return (result == YAFFS_FAIL) ? -1 : 0;
1014 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1019 obj = yaffs_GetEquivalentObject(obj);
1022 buf->st_dev = (int)obj->myDev->genericDevice;
1023 buf->st_ino = obj->objectId;
1024 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1026 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1027 buf->st_mode |= S_IFDIR;
1028 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1029 buf->st_mode |= S_IFLNK;
1030 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1031 buf->st_mode |= S_IFREG;
1033 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1036 buf->st_rdev = obj->yst_rdev;
1037 buf->st_size = yaffs_GetObjectFileLength(obj);
1038 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1039 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1040 #if CONFIG_YAFFS_WINCE
1041 buf->yst_wince_atime[0] = obj->win_atime[0];
1042 buf->yst_wince_atime[1] = obj->win_atime[1];
1043 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1044 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1045 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1046 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1048 buf->yst_atime = obj->yst_atime;
1049 buf->yst_ctime = obj->yst_ctime;
1050 buf->yst_mtime = obj->yst_mtime;
1057 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1064 obj = yaffsfs_FindObject(NULL,path,0);
1067 obj = yaffsfs_FollowLink(obj,0);
1070 retVal = yaffsfs_DoStat(obj,buf);
1072 // todo error not found
1073 yaffsfs_SetError(-ENOENT);
1081 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1083 return yaffsfs_DoStatOrLStat(path,buf,0);
1086 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1088 return yaffsfs_DoStatOrLStat(path,buf,1);
1091 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1098 obj = yaffsfs_GetHandleObject(fd);
1101 retVal = yaffsfs_DoStat(obj,buf);
1104 yaffsfs_SetError(-EBADF);
1111 #ifdef CONFIG_YAFFS_WINCE
1112 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1119 obj = yaffsfs_GetHandleObject(fd);
1124 wctime[0] = obj->win_ctime[0];
1125 wctime[1] = obj->win_ctime[1];
1128 watime[0] = obj->win_atime[0];
1129 watime[1] = obj->win_atime[1];
1132 wmtime[0] = obj->win_mtime[0];
1133 wmtime[1] = obj->win_mtime[1];
1140 yaffsfs_SetError(-EBADF);
1148 int yaffs_set_wince_times(int fd,
1149 const unsigned *wctime,
1150 const unsigned *watime,
1151 const unsigned *wmtime)
1158 obj = yaffsfs_GetHandleObject(fd);
1163 obj->win_ctime[0] = wctime[0];
1164 obj->win_ctime[1] = wctime[1];
1167 obj->win_atime[0] = watime[0];
1168 obj->win_atime[1] = watime[1];
1171 obj->win_mtime[0] = wmtime[0];
1172 obj->win_mtime[1] = wmtime[1];
1176 result = yaffs_FlushFile(obj,0,0);
1180 yaffsfs_SetError(-EBADF);
1190 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1195 obj = yaffs_GetEquivalentObject(obj);
1198 obj->yst_mode = mode;
1200 result = yaffs_FlushFile(obj,0,0);
1203 return result == YAFFS_OK ? 0 : -1;
1207 int yaffs_access(const YCHAR *path, int amode)
1214 obj = yaffsfs_FindObject(NULL,path,0);
1219 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1221 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1223 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1227 yaffsfs_SetError(-EACCES);
1231 // todo error not found
1232 yaffsfs_SetError(-ENOENT);
1243 int yaffs_chmod(const YCHAR *path, mode_t mode)
1250 obj = yaffsfs_FindObject(NULL,path,0);
1253 retVal = yaffsfs_DoChMod(obj,mode);
1255 // todo error not found
1256 yaffsfs_SetError(-ENOENT);
1265 int yaffs_fchmod(int fd, mode_t mode)
1272 obj = yaffsfs_GetHandleObject(fd);
1275 retVal = yaffsfs_DoChMod(obj,mode);
1278 yaffsfs_SetError(-EBADF);
1286 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1288 yaffs_Object *parent = NULL;
1289 yaffs_Object *dir = NULL;
1294 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1296 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1301 yaffsfs_SetError(-ENOENT); // missing path
1302 else if (yaffs_FindObjectByName(parent,name))
1303 yaffsfs_SetError(-EEXIST); // the name already exists
1305 yaffsfs_SetError(-ENOSPC); // just assume no space
1314 int yaffs_mount(const YCHAR *path)
1317 int result=YAFFS_FAIL;
1318 yaffs_Device *dev=NULL;
1321 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1324 dev = yaffsfs_FindDevice(path,&dummy);
1326 if(!dev->isMounted){
1327 result = yaffs_GutsInitialise(dev);
1328 if(result == YAFFS_FAIL)
1329 // todo error - mount failed
1330 yaffsfs_SetError(-ENOMEM);
1331 retVal = result ? 0 : -1;
1335 //todo error - already mounted.
1336 yaffsfs_SetError(-EBUSY);
1338 // todo error - no device
1339 yaffsfs_SetError(-ENODEV);
1346 int yaffs_sync(const YCHAR *path)
1349 yaffs_Device *dev=NULL;
1353 dev = yaffsfs_FindDevice(path,&dummy);
1357 yaffs_FlushEntireDeviceCache(dev);
1358 yaffs_CheckpointSave(dev);
1362 //todo error - not mounted.
1363 yaffsfs_SetError(-EINVAL);
1366 // todo error - no device
1367 yaffsfs_SetError(-ENODEV);
1374 int yaffs_unmount(const YCHAR *path)
1377 yaffs_Device *dev=NULL;
1381 dev = yaffsfs_FindDevice(path,&dummy);
1387 yaffs_FlushEntireDeviceCache(dev);
1388 yaffs_CheckpointSave(dev);
1390 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1391 if(yaffsfs_handle[i].inUse && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1392 inUse = 1; // the device is in use, can't unmount
1396 yaffs_Deinitialise(dev);
1400 // todo error can't unmount as files are open
1401 yaffsfs_SetError(-EBUSY);
1404 //todo error - not mounted.
1405 yaffsfs_SetError(-EINVAL);
1409 // todo error - no device
1410 yaffsfs_SetError(-ENODEV);
1417 loff_t yaffs_freespace(const YCHAR *path)
1420 yaffs_Device *dev=NULL;
1424 dev = yaffsfs_FindDevice(path,&dummy);
1425 if(dev && dev->isMounted){
1426 retVal = yaffs_GetNumberOfFreeChunks(dev);
1427 retVal *= dev->nDataBytesPerChunk;
1430 yaffsfs_SetError(-EINVAL);
1436 loff_t yaffs_totalspace(const YCHAR *path)
1439 yaffs_Device *dev=NULL;
1443 dev = yaffsfs_FindDevice(path,&dummy);
1444 if(dev && dev->isMounted){
1445 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1446 retVal *= dev->nChunksPerBlock;
1447 retVal *= dev->nDataBytesPerChunk;
1450 yaffsfs_SetError(-EINVAL);
1456 int yaffs_inodecount(const YCHAR *path)
1459 yaffs_Device *dev=NULL;
1463 dev = yaffsfs_FindDevice(path,&dummy);
1464 if(dev && dev->isMounted) {
1465 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1466 if(nObjects > dev->nHardLinks)
1467 retVal = nObjects - dev->nHardLinks;
1471 yaffsfs_SetError(-EINVAL);
1479 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1482 yaffsfs_DeviceConfiguration *cfg;
1484 yaffsfs_configurationList = cfgList;
1486 yaffsfs_InitHandles();
1488 cfg = yaffsfs_configurationList;
1490 while(cfg && cfg->prefix && cfg->dev){
1491 cfg->dev->isMounted = 0;
1492 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1501 // Directory search stuff.
1504 // Directory search context
1506 // NB this is an opaque structure.
1512 yaffs_dirent de; /* directory entry being used by this dsc */
1513 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1514 yaffs_Object *dirObj; /* ptr to directory being searched */
1515 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1517 struct ylist_head others;
1518 } yaffsfs_DirectorySearchContext;
1522 static struct ylist_head search_contexts;
1525 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1529 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1533 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1534 dsc->nextReturn = NULL;
1536 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1537 yaffs_Object,siblings);
1539 /* Hey someone isn't playing nice! */
1543 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1547 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1549 if( dsc->nextReturn == NULL ||
1550 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1551 dsc->nextReturn = NULL;
1553 struct ylist_head *next = dsc->nextReturn->siblings.next;
1555 if( next == &dsc->dirObj->variant.directoryVariant.children)
1556 dsc->nextReturn = NULL; /* end of list */
1558 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1561 /* Hey someone isn't playing nice! */
1565 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1568 struct ylist_head *i;
1569 yaffsfs_DirectorySearchContext *dsc;
1571 /* if search contexts not initilised then skip */
1572 if(!search_contexts.next)
1575 /* Iterate through the directory search contexts.
1576 * If any are the one being removed, then advance the dsc to
1577 * the next one to prevent a hanging ptr.
1579 ylist_for_each(i, &search_contexts) {
1581 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1582 if(dsc->nextReturn == obj)
1583 yaffsfs_DirAdvance(dsc);
1589 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1591 yaffs_DIR *dir = NULL;
1592 yaffs_Object *obj = NULL;
1593 yaffsfs_DirectorySearchContext *dsc = NULL;
1597 obj = yaffsfs_FindObject(NULL,dirname,0);
1599 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1601 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1602 dir = (yaffs_DIR *)dsc;
1605 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1606 dsc->magic = YAFFS_MAGIC;
1608 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1609 YINIT_LIST_HEAD(&dsc->others);
1611 if(!search_contexts.next)
1612 YINIT_LIST_HEAD(&search_contexts);
1614 ylist_add(&dsc->others,&search_contexts);
1615 yaffsfs_SetDirRewound(dsc);
1625 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1627 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1628 struct yaffs_dirent *retVal = NULL;
1632 if(dsc && dsc->magic == YAFFS_MAGIC){
1633 yaffsfs_SetError(0);
1634 if(dsc->nextReturn){
1635 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1636 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1637 dsc->de.d_off = dsc->offset++;
1638 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1639 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1641 // this should not happen!
1642 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1644 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1646 yaffsfs_DirAdvance(dsc);
1650 yaffsfs_SetError(-EBADF);
1659 void yaffs_rewinddir(yaffs_DIR *dirp)
1661 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1665 yaffsfs_SetDirRewound(dsc);
1671 int yaffs_closedir(yaffs_DIR *dirp)
1673 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1677 ylist_del(&dsc->others); /* unhook from list */
1683 // end of directory stuff
1686 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1688 yaffs_Object *parent = NULL;
1692 int mode = 0; // ignore for now
1695 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1697 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1701 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1705 yaffsfs_SetError(-EINVAL);
1715 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1717 yaffs_Object *obj = NULL;
1723 obj = yaffsfs_FindObject(NULL,path,0);
1726 yaffsfs_SetError(-ENOENT);
1728 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1729 yaffsfs_SetError(-EINVAL);
1732 YCHAR *alias = obj->variant.symLinkVariant.alias;
1733 memset(buf,0,bufsiz);
1734 yaffs_strncpy(buf,alias,bufsiz - 1);
1741 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1743 // Creates a link called newpath to existing oldpath
1744 yaffs_Object *obj = NULL;
1745 yaffs_Object *target = NULL;
1747 int newNameLength = 0;
1752 obj = yaffsfs_FindObject(NULL,oldpath,0);
1753 target = yaffsfs_FindObject(NULL,newpath,0);
1756 yaffsfs_SetError(-ENOENT);
1759 yaffsfs_SetError(-EEXIST);
1762 yaffs_Object *newdir = NULL;
1763 yaffs_Object *link = NULL;
1767 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1770 yaffsfs_SetError(-ENOTDIR);
1772 }else if(newdir->myDev != obj->myDev){
1773 yaffsfs_SetError(-EXDEV);
1777 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1779 if(newNameLength == 0){
1780 yaffsfs_SetError(-ENOENT);
1782 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1783 yaffsfs_SetError(-ENAMETOOLONG);
1788 link = yaffs_Link(newdir,newname,obj);
1792 yaffsfs_SetError(-ENOSPC);
1803 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1808 int yaffs_DumpDevStruct(const YCHAR *path)
1813 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1816 yaffs_Device *dev = obj->myDev;
1819 "nPageWrites.......... %d\n"
1820 "nPageReads........... %d\n"
1821 "nBlockErasures....... %d\n"
1822 "nGCCopies............ %d\n"
1823 "garbageCollections... %d\n"
1824 "passiveGarbageColl'ns %d\n"
1828 dev->nBlockErasures,
1830 dev->garbageCollections,
1831 dev->passiveGarbageCollections