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.30 2010-01-06 04:00:23 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;
50 __u8 inUse:1; // this handle is in use
51 __u8 readOnly:1; // this handle is read only
52 __u8 append:1; // append only
53 __u8 exclusive:1; // exclusive
54 int inodeId:20; // the object
55 __u32 position; // current position in file
58 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
59 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
62 /// Inilitalise handles on start-up.
64 static int yaffsfs_InitHandles(void)
67 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
68 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
69 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
70 yaffsfs_handle[i].inodeId = -1;
75 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
77 if(h < 0 || h >= YAFFSFS_N_HANDLES)
80 return &yaffsfs_handle[h];
83 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
85 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
87 if(h && h->inUse && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
88 return &yaffsfs_inode[h->inodeId];
93 yaffs_Object *yaffsfs_GetHandleObject(int handle)
95 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
103 //yaffsfs_GetInodeIdForObject
104 // Grab an inode entry when opening a new inode.
107 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
111 yaffsfs_Inode *in = NULL;
114 obj = yaffs_GetEquivalentObject(obj);
116 /* Look for it. If we can't find it then make one */
117 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
118 if(yaffsfs_inode[i].iObj == obj)
122 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
123 if(!yaffsfs_inode[i].iObj)
129 in = &yaffsfs_inode[ret];
140 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
147 yaffs_DeleteObject(obj);
154 static void yaffsfs_PutInode(int inodeId)
156 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
157 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
160 yaffsfs_ReleaseInode(in);
166 // Grab a handle (when opening a file)
169 static int yaffsfs_GetHandle(void)
174 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
175 h = yaffsfs_GetHandlePointer(i);
177 // todo bug: should never happen
180 memset(h,0,sizeof(yaffsfs_Handle));
190 // Let go of a handle when closing a file or aborting an open.
192 static int yaffsfs_PutHandle(int handle)
194 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
198 yaffsfs_PutInode(h->inodeId);
208 // Stuff to search for a directory from a path
211 int yaffsfs_Match(YCHAR a, YCHAR b)
217 int yaffsfs_IsPathDivider(YCHAR ch)
219 YCHAR *str = YAFFS_PATH_DIVIDERS;
230 // yaffsfs_FindDevice
232 // Scan the configuration list to find the root.
233 // Curveballs: Should match paths that end in '/' too
234 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
235 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
237 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
238 const YCHAR *leftOver;
240 yaffs_Device *retval = NULL;
242 int longestMatch = -1;
245 // Check all configs, choose the one that:
246 // 1) Actually matches a prefix (ie /a amd /abc will not match
247 // 2) Matches the longest.
248 while(cfg && cfg->prefix && cfg->dev){
255 while(matching && *p && *leftOver){
257 while(yaffsfs_IsPathDivider(*p))
261 while(yaffsfs_IsPathDivider(*leftOver))
264 // Now match the text part
266 *p && !yaffsfs_IsPathDivider(*p) &&
267 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
268 if(yaffsfs_Match(*p,*leftOver)){
278 // Skip over any /s in leftOver
279 while(yaffsfs_IsPathDivider(*leftOver))
283 if( matching && (thisMatchLength > longestMatch)){
285 *restOfPath = (YCHAR *)leftOver;
287 longestMatch = thisMatchLength;
296 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
298 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
299 const YCHAR *leftOver;
301 yaffs_Device *retval = NULL;
303 int longestMatch = -1;
305 // Check all configs, choose the one that:
306 // 1) Actually matches a prefix (ie /a amd /abc will not match
307 // 2) Matches the longest.
308 while(cfg && cfg->prefix && cfg->dev){
313 while(*p && //unmatched part of prefix
314 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
316 yaffsfs_Match(*p,*leftOver)){
323 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && // end of prefix
324 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
325 (thisMatchLength > longestMatch)){
327 *restOfPath = (YCHAR *)leftOver;
329 longestMatch = thisMatchLength;
337 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
342 dev= yaffsfs_FindDevice(path,restOfPath);
343 if(dev && dev->isMounted){
349 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
352 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
353 YCHAR *alias = obj->variant.symLinkVariant.alias;
355 if(yaffsfs_IsPathDivider(*alias))
356 // Starts with a /, need to scan from root up
357 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
359 // Relative to here, so use the parent of the symlink as a start
360 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
366 // yaffsfs_FindDirectory
367 // Parse a path to determine the directory and the name within the directory.
369 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
370 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
374 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
377 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
382 restOfPath = (YCHAR *)path;
385 dir = yaffsfs_FindRoot(path,&restOfPath);
389 // curve ball: also throw away surplus '/'
390 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
391 while(yaffsfs_IsPathDivider(*restOfPath))
392 restOfPath++; // get rid of '/'
397 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
398 if (i < YAFFS_MAX_NAME_LENGTH){
399 str[i] = *restOfPath;
407 // got to the end of the string
410 if(yaffs_strcmp(str,_Y(".")) == 0)
414 else if(yaffs_strcmp(str,_Y("..")) == 0)
417 dir = yaffs_FindObjectByName(dir,str);
419 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
420 dir = yaffsfs_FollowLink(dir,symDepth);
423 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
428 // directory did not exist.
432 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
434 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
437 // yaffsfs_FindObject turns a path for an existing object into the object
439 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
444 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
447 return yaffs_FindObjectByName(dir,name);
453 int yaffs_dup(int fd)
456 yaffsfs_Handle *oldPtr = NULL;
457 yaffsfs_Handle *newPtr = NULL;
461 oldPtr = yaffsfs_GetHandlePointer(fd);
462 if(oldPtr && oldPtr->inUse)
463 newHandle = yaffsfs_GetHandle();
465 newPtr = yaffsfs_GetHandlePointer(newHandle);
473 yaffsfs_SetError(-EBADF);
475 yaffsfs_SetError(-ENOMEM);
481 int yaffs_open(const YCHAR *path, int oflag, int mode)
483 yaffs_Object *obj = NULL;
484 yaffs_Object *dir = NULL;
487 yaffsfs_Handle *h = NULL;
489 int alreadyExclusive = 0;
492 int errorReported = 0;
497 // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
502 handle = yaffsfs_GetHandle();
506 h = yaffsfs_GetHandlePointer(handle);
509 // try to find the exisiting object
510 obj = yaffsfs_FindObject(NULL,path,0);
512 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
513 obj = yaffsfs_FollowLink(obj,symDepth++);
516 obj = yaffs_GetEquivalentObject(obj);
518 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
522 // Check if the object is already in use
523 alreadyOpen = alreadyExclusive = 0;
525 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
527 yaffsfs_handle[i].inUse &&
528 obj == yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj){
530 if(yaffsfs_handle[i].exclusive)
531 alreadyExclusive = 1;
535 if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
538 // Open should fail if O_CREAT and O_EXCL are specified
539 if((oflag & O_EXCL) && (oflag & O_CREAT)){
541 yaffsfs_SetError(-EEXIST);
545 // Check file permissions
546 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && // ie O_RDONLY
547 !(obj->yst_mode & S_IREAD))
550 if( (oflag & O_RDWR) &&
551 !(obj->yst_mode & S_IREAD))
554 if( (oflag & (O_RDWR | O_WRONLY)) &&
555 !(obj->yst_mode & S_IWRITE))
558 } else if((oflag & O_CREAT)) {
559 // Let's see if we can create this file
560 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
562 obj = yaffs_MknodFile(dir,name,mode,0,0);
564 yaffsfs_SetError(-ENOTDIR);
569 if(obj && !openDenied) {
570 int inodeId = yaffsfs_GetInodeIdForObject(obj);
574 * Todo: Fix any problem if inodes run out, though that
575 * can't happen if the number of inode items >= number of handles.
579 h->inodeId = inodeId;
581 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
582 h->append = (oflag & O_APPEND) ? 1 : 0;
583 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
586 /* Hook inode to object */
587 obj->myInode = (void*) &yaffsfs_inode[inodeId];
589 if((oflag & O_TRUNC) && !h->readOnly)
590 yaffs_ResizeFile(obj,0);
592 yaffsfs_PutHandle(handle);
594 yaffsfs_SetError(-EACCES);
606 int yaffs_Dofsync(int fd,int datasync)
608 yaffsfs_Handle *h = NULL;
613 h = yaffsfs_GetHandlePointer(fd);
617 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
620 yaffsfs_SetError(-EBADF);
629 int yaffs_fsync(int fd)
631 return yaffs_Dofsync(fd,0);
634 int yaffs_flush(int fd)
636 return yaffs_fsync(fd);
639 int yaffs_fdatasync(int fd)
641 return yaffs_Dofsync(fd,1);
644 int yaffs_close(int fd)
646 yaffsfs_Handle *h = NULL;
651 h = yaffsfs_GetHandlePointer(fd);
655 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
656 yaffsfs_PutHandle(fd);
660 yaffsfs_SetError(-EBADF);
669 int yaffs_read(int fd, void *buf, unsigned int nbyte)
671 yaffsfs_Handle *h = NULL;
672 yaffs_Object *obj = NULL;
675 unsigned int maxRead;
678 h = yaffsfs_GetHandlePointer(fd);
679 obj = yaffsfs_GetHandleObject(fd);
683 yaffsfs_SetError(-EBADF);
687 if(yaffs_GetObjectFileLength(obj) > pos)
688 maxRead = yaffs_GetObjectFileLength(obj) - pos;
697 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
699 h->position = pos + nRead;
711 return (nRead >= 0) ? nRead : -1;
715 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
717 yaffsfs_Handle *h = NULL;
718 yaffs_Object *obj = NULL;
721 unsigned int maxRead;
724 h = yaffsfs_GetHandlePointer(fd);
725 obj = yaffsfs_GetHandleObject(fd);
729 yaffsfs_SetError(-EBADF);
732 if(yaffs_GetObjectFileLength(obj) > pos)
733 maxRead = yaffs_GetObjectFileLength(obj) - pos;
742 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
750 return (nRead >= 0) ? nRead : -1;
754 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
756 yaffsfs_Handle *h = NULL;
757 yaffs_Object *obj = NULL;
760 int writeThrough = 0;
763 h = yaffsfs_GetHandlePointer(fd);
764 obj = yaffsfs_GetHandleObject(fd);
768 yaffsfs_SetError(-EBADF);
769 else if( h && obj && h->readOnly){
771 } else if( h && obj){
773 pos = yaffs_GetObjectFileLength(obj);
777 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
780 h->position = pos + nWritten;
790 return (nWritten >= 0) ? nWritten : -1;
794 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
796 yaffsfs_Handle *h = NULL;
797 yaffs_Object *obj = NULL;
800 int writeThrough = 0;
803 h = yaffsfs_GetHandlePointer(fd);
804 obj = yaffsfs_GetHandleObject(fd);
808 yaffsfs_SetError(-EBADF);
809 else if( h && obj && h->readOnly){
815 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
817 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
818 yaffsfs_SetError(-ENOSPC);
825 return (nWritten >= 0) ? nWritten : -1;
830 int yaffs_truncate(const YCHAR *path,off_t newSize)
832 yaffs_Object *obj = NULL;
833 int result = YAFFS_FAIL;
837 obj = yaffsfs_FindObject(NULL,path,0);
839 obj = yaffs_GetEquivalentObject(obj);
842 yaffsfs_SetError(-ENOENT);
843 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
844 yaffsfs_SetError(-EISDIR);
846 result = yaffs_ResizeFile(obj,newSize);
851 return (result) ? 0 : -1;
854 int yaffs_ftruncate(int fd, off_t newSize)
856 yaffsfs_Handle *h = NULL;
857 yaffs_Object *obj = NULL;
861 h = yaffsfs_GetHandlePointer(fd);
862 obj = yaffsfs_GetHandleObject(fd);
866 yaffsfs_SetError(-EBADF);
869 result = yaffs_ResizeFile(obj,newSize);
873 return (result) ? 0 : -1;
877 off_t yaffs_lseek(int fd, off_t offset, int whence)
879 yaffsfs_Handle *h = NULL;
880 yaffs_Object *obj = NULL;
885 h = yaffsfs_GetHandlePointer(fd);
886 obj = yaffsfs_GetHandleObject(fd);
890 yaffsfs_SetError(-EBADF);
891 else if(whence == SEEK_SET){
895 else if(whence == SEEK_CUR) {
896 if( (h->position + offset) >= 0)
897 pos = (h->position + offset);
899 else if(whence == SEEK_END) {
900 fSize = yaffs_GetObjectFileLength(obj);
901 if(fSize >= 0 && (fSize + offset) >= 0)
902 pos = fSize + offset;
918 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
920 yaffs_Object *dir = NULL;
921 yaffs_Object *obj = NULL;
923 int result = YAFFS_FAIL;
927 obj = yaffsfs_FindObject(NULL,path,0);
928 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
930 yaffsfs_SetError(-ENOTDIR);
932 yaffsfs_SetError(-ENOENT);
933 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
934 yaffsfs_SetError(-EISDIR);
935 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
936 yaffsfs_SetError(-ENOTDIR);
938 result = yaffs_Unlink(dir,name);
940 if(result == YAFFS_FAIL && isDirectory)
941 yaffsfs_SetError(-ENOTEMPTY);
948 return (result == YAFFS_FAIL) ? -1 : 0;
952 int yaffs_rmdir(const YCHAR *path)
954 return yaffsfs_DoUnlink(path,1);
957 int yaffs_unlink(const YCHAR *path)
959 return yaffsfs_DoUnlink(path,0);
962 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
964 yaffs_Object *olddir = NULL;
965 yaffs_Object *newdir = NULL;
966 yaffs_Object *obj = NULL;
969 int result= YAFFS_FAIL;
970 int renameAllowed = 1;
974 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
975 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
976 obj = yaffsfs_FindObject(NULL,oldPath,0);
978 if(!olddir || !newdir || !obj) {
980 yaffsfs_SetError(-EBADF);
982 } else if(olddir->myDev != newdir->myDev) {
983 // oops must be on same device
985 yaffsfs_SetError(-EXDEV);
987 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
988 // It is a directory, check that it is not being renamed to
989 // being its own decendent.
990 // Do this by tracing from the new directory back to the root, checking for obj
992 yaffs_Object *xx = newdir;
994 while( renameAllowed && xx){
1000 yaffsfs_SetError(-EACCES);
1004 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1008 return (result == YAFFS_FAIL) ? -1 : 0;
1012 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1017 obj = yaffs_GetEquivalentObject(obj);
1020 buf->st_dev = (int)obj->myDev->genericDevice;
1021 buf->st_ino = obj->objectId;
1022 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1024 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1025 buf->st_mode |= S_IFDIR;
1026 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1027 buf->st_mode |= S_IFLNK;
1028 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1029 buf->st_mode |= S_IFREG;
1031 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1034 buf->st_rdev = obj->yst_rdev;
1035 buf->st_size = yaffs_GetObjectFileLength(obj);
1036 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1037 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1038 #if CONFIG_YAFFS_WINCE
1039 buf->yst_wince_atime[0] = obj->win_atime[0];
1040 buf->yst_wince_atime[1] = obj->win_atime[1];
1041 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1042 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1043 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1044 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1046 buf->yst_atime = obj->yst_atime;
1047 buf->yst_ctime = obj->yst_ctime;
1048 buf->yst_mtime = obj->yst_mtime;
1055 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1062 obj = yaffsfs_FindObject(NULL,path,0);
1065 obj = yaffsfs_FollowLink(obj,0);
1068 retVal = yaffsfs_DoStat(obj,buf);
1070 // todo error not found
1071 yaffsfs_SetError(-ENOENT);
1079 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1081 return yaffsfs_DoStatOrLStat(path,buf,0);
1084 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1086 return yaffsfs_DoStatOrLStat(path,buf,1);
1089 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1096 obj = yaffsfs_GetHandleObject(fd);
1099 retVal = yaffsfs_DoStat(obj,buf);
1102 yaffsfs_SetError(-EBADF);
1109 #ifdef CONFIG_YAFFS_WINCE
1110 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1117 obj = yaffsfs_GetHandleObject(fd);
1122 wctime[0] = obj->win_ctime[0];
1123 wctime[1] = obj->win_ctime[1];
1126 watime[0] = obj->win_atime[0];
1127 watime[1] = obj->win_atime[1];
1130 wmtime[0] = obj->win_mtime[0];
1131 wmtime[1] = obj->win_mtime[1];
1138 yaffsfs_SetError(-EBADF);
1146 int yaffs_set_wince_times(int fd,
1147 const unsigned *wctime,
1148 const unsigned *watime,
1149 const unsigned *wmtime)
1156 obj = yaffsfs_GetHandleObject(fd);
1161 obj->win_ctime[0] = wctime[0];
1162 obj->win_ctime[1] = wctime[1];
1165 obj->win_atime[0] = watime[0];
1166 obj->win_atime[1] = watime[1];
1169 obj->win_mtime[0] = wmtime[0];
1170 obj->win_mtime[1] = wmtime[1];
1174 result = yaffs_FlushFile(obj,0,0);
1178 yaffsfs_SetError(-EBADF);
1188 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1193 obj = yaffs_GetEquivalentObject(obj);
1196 obj->yst_mode = mode;
1198 result = yaffs_FlushFile(obj,0,0);
1201 return result == YAFFS_OK ? 0 : -1;
1205 int yaffs_access(const YCHAR *path, int amode)
1212 obj = yaffsfs_FindObject(NULL,path,0);
1217 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1219 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1221 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1225 yaffsfs_SetError(-EACCES);
1229 // todo error not found
1230 yaffsfs_SetError(-ENOENT);
1241 int yaffs_chmod(const YCHAR *path, mode_t mode)
1248 obj = yaffsfs_FindObject(NULL,path,0);
1251 retVal = yaffsfs_DoChMod(obj,mode);
1253 // todo error not found
1254 yaffsfs_SetError(-ENOENT);
1263 int yaffs_fchmod(int fd, mode_t mode)
1270 obj = yaffsfs_GetHandleObject(fd);
1273 retVal = yaffsfs_DoChMod(obj,mode);
1276 yaffsfs_SetError(-EBADF);
1284 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1286 yaffs_Object *parent = NULL;
1287 yaffs_Object *dir = NULL;
1292 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1294 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1299 yaffsfs_SetError(-ENOENT); // missing path
1300 else if (yaffs_FindObjectByName(parent,name))
1301 yaffsfs_SetError(-EEXIST); // the name already exists
1303 yaffsfs_SetError(-ENOSPC); // just assume no space
1312 int yaffs_mount(const YCHAR *path)
1315 int result=YAFFS_FAIL;
1316 yaffs_Device *dev=NULL;
1319 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1322 dev = yaffsfs_FindDevice(path,&dummy);
1324 if(!dev->isMounted){
1325 result = yaffs_GutsInitialise(dev);
1326 if(result == YAFFS_FAIL)
1327 // todo error - mount failed
1328 yaffsfs_SetError(-ENOMEM);
1329 retVal = result ? 0 : -1;
1333 //todo error - already mounted.
1334 yaffsfs_SetError(-EBUSY);
1336 // todo error - no device
1337 yaffsfs_SetError(-ENODEV);
1344 int yaffs_sync(const YCHAR *path)
1347 yaffs_Device *dev=NULL;
1351 dev = yaffsfs_FindDevice(path,&dummy);
1355 yaffs_FlushEntireDeviceCache(dev);
1356 yaffs_CheckpointSave(dev);
1360 //todo error - not mounted.
1361 yaffsfs_SetError(-EINVAL);
1364 // todo error - no device
1365 yaffsfs_SetError(-ENODEV);
1372 int yaffs_unmount(const YCHAR *path)
1375 yaffs_Device *dev=NULL;
1379 dev = yaffsfs_FindDevice(path,&dummy);
1385 yaffs_FlushEntireDeviceCache(dev);
1386 yaffs_CheckpointSave(dev);
1388 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1389 if(yaffsfs_handle[i].inUse && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1390 inUse = 1; // the device is in use, can't unmount
1394 yaffs_Deinitialise(dev);
1398 // todo error can't unmount as files are open
1399 yaffsfs_SetError(-EBUSY);
1402 //todo error - not mounted.
1403 yaffsfs_SetError(-EINVAL);
1407 // todo error - no device
1408 yaffsfs_SetError(-ENODEV);
1415 loff_t yaffs_freespace(const YCHAR *path)
1418 yaffs_Device *dev=NULL;
1422 dev = yaffsfs_FindDevice(path,&dummy);
1423 if(dev && dev->isMounted){
1424 retVal = yaffs_GetNumberOfFreeChunks(dev);
1425 retVal *= dev->nDataBytesPerChunk;
1428 yaffsfs_SetError(-EINVAL);
1434 loff_t yaffs_totalspace(const YCHAR *path)
1437 yaffs_Device *dev=NULL;
1441 dev = yaffsfs_FindDevice(path,&dummy);
1442 if(dev && dev->isMounted){
1443 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1444 retVal *= dev->nChunksPerBlock;
1445 retVal *= dev->nDataBytesPerChunk;
1448 yaffsfs_SetError(-EINVAL);
1454 int yaffs_inodecount(const YCHAR *path)
1457 yaffs_Device *dev=NULL;
1461 dev = yaffsfs_FindDevice(path,&dummy);
1462 if(dev && dev->isMounted) {
1463 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1464 if(nObjects > dev->nHardLinks)
1465 retVal = nObjects - dev->nHardLinks;
1469 yaffsfs_SetError(-EINVAL);
1477 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1480 yaffsfs_DeviceConfiguration *cfg;
1482 yaffsfs_configurationList = cfgList;
1484 yaffsfs_InitHandles();
1486 cfg = yaffsfs_configurationList;
1488 while(cfg && cfg->prefix && cfg->dev){
1489 cfg->dev->isMounted = 0;
1490 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1499 // Directory search stuff.
1502 // Directory search context
1504 // NB this is an opaque structure.
1510 yaffs_dirent de; /* directory entry being used by this dsc */
1511 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1512 yaffs_Object *dirObj; /* ptr to directory being searched */
1513 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1515 struct ylist_head others;
1516 } yaffsfs_DirectorySearchContext;
1520 static struct ylist_head search_contexts;
1523 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1527 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1531 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1532 dsc->nextReturn = NULL;
1534 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1535 yaffs_Object,siblings);
1537 /* Hey someone isn't playing nice! */
1541 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1545 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1547 if( dsc->nextReturn == NULL ||
1548 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1549 dsc->nextReturn = NULL;
1551 struct ylist_head *next = dsc->nextReturn->siblings.next;
1553 if( next == &dsc->dirObj->variant.directoryVariant.children)
1554 dsc->nextReturn = NULL; /* end of list */
1556 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1559 /* Hey someone isn't playing nice! */
1563 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1566 struct ylist_head *i;
1567 yaffsfs_DirectorySearchContext *dsc;
1569 /* if search contexts not initilised then skip */
1570 if(!search_contexts.next)
1573 /* Iterate through the directory search contexts.
1574 * If any are the one being removed, then advance the dsc to
1575 * the next one to prevent a hanging ptr.
1577 ylist_for_each(i, &search_contexts) {
1579 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1580 if(dsc->nextReturn == obj)
1581 yaffsfs_DirAdvance(dsc);
1587 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1589 yaffs_DIR *dir = NULL;
1590 yaffs_Object *obj = NULL;
1591 yaffsfs_DirectorySearchContext *dsc = NULL;
1595 obj = yaffsfs_FindObject(NULL,dirname,0);
1597 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1599 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1600 dir = (yaffs_DIR *)dsc;
1603 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1604 dsc->magic = YAFFS_MAGIC;
1606 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1607 YINIT_LIST_HEAD(&dsc->others);
1609 if(!search_contexts.next)
1610 YINIT_LIST_HEAD(&search_contexts);
1612 ylist_add(&dsc->others,&search_contexts);
1613 yaffsfs_SetDirRewound(dsc);
1623 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1625 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1626 struct yaffs_dirent *retVal = NULL;
1630 if(dsc && dsc->magic == YAFFS_MAGIC){
1631 yaffsfs_SetError(0);
1632 if(dsc->nextReturn){
1633 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1634 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1635 dsc->de.d_off = dsc->offset++;
1636 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1637 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1639 // this should not happen!
1640 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1642 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1644 yaffsfs_DirAdvance(dsc);
1648 yaffsfs_SetError(-EBADF);
1657 void yaffs_rewinddir(yaffs_DIR *dirp)
1659 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1663 yaffsfs_SetDirRewound(dsc);
1669 int yaffs_closedir(yaffs_DIR *dirp)
1671 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1675 ylist_del(&dsc->others); /* unhook from list */
1681 // end of directory stuff
1684 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1686 yaffs_Object *parent = NULL;
1690 int mode = 0; // ignore for now
1693 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1695 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1699 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1703 yaffsfs_SetError(-EINVAL);
1713 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1715 yaffs_Object *obj = NULL;
1721 obj = yaffsfs_FindObject(NULL,path,0);
1724 yaffsfs_SetError(-ENOENT);
1726 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1727 yaffsfs_SetError(-EINVAL);
1730 YCHAR *alias = obj->variant.symLinkVariant.alias;
1731 memset(buf,0,bufsiz);
1732 yaffs_strncpy(buf,alias,bufsiz - 1);
1739 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1741 // Creates a link called newpath to existing oldpath
1742 yaffs_Object *obj = NULL;
1743 yaffs_Object *target = NULL;
1745 int newNameLength = 0;
1750 obj = yaffsfs_FindObject(NULL,oldpath,0);
1751 target = yaffsfs_FindObject(NULL,newpath,0);
1754 yaffsfs_SetError(-ENOENT);
1757 yaffsfs_SetError(-EEXIST);
1760 yaffs_Object *newdir = NULL;
1761 yaffs_Object *link = NULL;
1765 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1768 yaffsfs_SetError(-ENOTDIR);
1770 }else if(newdir->myDev != obj->myDev){
1771 yaffsfs_SetError(-EXDEV);
1775 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1777 if(newNameLength == 0){
1778 yaffsfs_SetError(-ENOENT);
1780 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1781 yaffsfs_SetError(-ENAMETOOLONG);
1786 link = yaffs_Link(newdir,newname,obj);
1790 yaffsfs_SetError(-ENOSPC);
1801 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1806 int yaffs_DumpDevStruct(const YCHAR *path)
1811 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1814 yaffs_Device *dev = obj->myDev;
1817 "nPageWrites.......... %d\n"
1818 "nPageReads........... %d\n"
1819 "nBlockErasures....... %d\n"
1820 "nGCCopies............ %d\n"
1821 "garbageCollections... %d\n"
1822 "passiveGarbageColl'ns %d\n"
1826 dev->nBlockErasures,
1828 dev->garbageCollections,
1829 dev->passiveGarbageCollections