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.32 2010-02-05 03:59:04 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);
585 obj = yaffs_MknodFile(dir,name,mode,0,0);
587 yaffsfs_SetError(-ENOTDIR);
592 if(obj && !openDenied) {
593 int inodeId = yaffsfs_GetInodeIdForObject(obj);
597 * Todo: Fix any problem if inodes run out, though that
598 * can't happen if the number of inode items >= number of handles.
602 h->inodeId = inodeId;
603 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
604 h->append = (oflag & O_APPEND) ? 1 : 0;
605 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
608 /* Hook inode to object */
609 obj->myInode = (void*) &yaffsfs_inode[inodeId];
611 if((oflag & O_TRUNC) && !h->readOnly)
612 yaffs_ResizeFile(obj,0);
614 yaffsfs_PutHandle(handle);
616 yaffsfs_SetError(-EACCES);
628 int yaffs_Dofsync(int fd,int datasync)
630 yaffsfs_Handle *h = NULL;
635 h = yaffsfs_GetHandlePointer(fd);
637 if(h && h->useCount > 0)
639 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
642 yaffsfs_SetError(-EBADF);
651 int yaffs_fsync(int fd)
653 return yaffs_Dofsync(fd,0);
656 int yaffs_flush(int fd)
658 return yaffs_fsync(fd);
661 int yaffs_fdatasync(int fd)
663 return yaffs_Dofsync(fd,1);
666 int yaffs_close(int fd)
668 yaffsfs_Handle *h = NULL;
673 h = yaffsfs_GetHandlePointer(fd);
675 if(h && h->useCount > 0) {
677 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
678 yaffsfs_PutHandle(fd);
682 yaffsfs_SetError(-EBADF);
693 int yaffsfs_do_read(int fd, void *buf, unsigned int nbyte, int isPread, int offset)
695 yaffsfs_Handle *h = NULL;
696 yaffs_Object *obj = NULL;
702 unsigned int maxRead;
705 h = yaffsfs_GetHandlePointer(fd);
706 obj = yaffsfs_GetHandleObject(fd);
710 yaffsfs_SetError(-EBADF);
712 } else if( h && obj){
716 startPos = h->position;
718 if(yaffs_GetObjectFileLength(obj) > pos)
719 maxRead = yaffs_GetObjectFileLength(obj) - pos;
728 yaffsfs_GetHandle(fd);
732 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
736 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
747 nbyte = 0; /* no more to read */
757 yaffsfs_PutHandle(fd);
760 h->position = startPos + totalRead;
772 return (totalRead >= 0) ? totalRead : -1;
776 int yaffs_read(int fd, void *buf, unsigned int nbyte)
778 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
781 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
783 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
786 int yaffsfs_do_write(int fd, const void *buf, unsigned int nbyte, int isPwrite, int offset)
788 yaffsfs_Handle *h = NULL;
789 yaffs_Object *obj = NULL;
793 int totalWritten = 0;
794 int writeThrough = 0;
798 h = yaffsfs_GetHandlePointer(fd);
799 obj = yaffsfs_GetHandleObject(fd);
803 yaffsfs_SetError(-EBADF);
805 } else if( h && obj && h->readOnly){
806 yaffsfs_SetError(-EINVAL);
808 } else if( h && obj){
812 startPos = yaffs_GetObjectFileLength(obj);
814 startPos = h->position;
816 yaffsfs_GetHandle(fd);
819 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
823 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
825 totalWritten += nWritten;
830 if(nWritten == nToWrite)
835 if(nWritten < 1 && totalWritten < 1){
836 yaffsfs_SetError(-ENOSPC);
846 yaffsfs_PutHandle(fd);
850 h->position = startPos + totalWritten;
862 return (totalWritten >= 0) ? totalWritten : -1;
866 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
868 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
871 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
873 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
877 int yaffs_truncate(const YCHAR *path,off_t newSize)
879 yaffs_Object *obj = NULL;
880 int result = YAFFS_FAIL;
884 obj = yaffsfs_FindObject(NULL,path,0);
886 obj = yaffs_GetEquivalentObject(obj);
889 yaffsfs_SetError(-ENOENT);
890 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
891 yaffsfs_SetError(-EISDIR);
893 result = yaffs_ResizeFile(obj,newSize);
898 return (result) ? 0 : -1;
901 int yaffs_ftruncate(int fd, off_t newSize)
903 yaffsfs_Handle *h = NULL;
904 yaffs_Object *obj = NULL;
908 h = yaffsfs_GetHandlePointer(fd);
909 obj = yaffsfs_GetHandleObject(fd);
913 yaffsfs_SetError(-EBADF);
916 result = yaffs_ResizeFile(obj,newSize);
920 return (result) ? 0 : -1;
924 off_t yaffs_lseek(int fd, off_t offset, int whence)
926 yaffsfs_Handle *h = NULL;
927 yaffs_Object *obj = NULL;
932 h = yaffsfs_GetHandlePointer(fd);
933 obj = yaffsfs_GetHandleObject(fd);
937 yaffsfs_SetError(-EBADF);
938 else if(whence == SEEK_SET){
942 else if(whence == SEEK_CUR) {
943 if( (h->position + offset) >= 0)
944 pos = (h->position + offset);
946 else if(whence == SEEK_END) {
947 fSize = yaffs_GetObjectFileLength(obj);
948 if(fSize >= 0 && (fSize + offset) >= 0)
949 pos = fSize + offset;
965 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
967 yaffs_Object *dir = NULL;
968 yaffs_Object *obj = NULL;
970 int result = YAFFS_FAIL;
974 obj = yaffsfs_FindObject(NULL,path,0);
975 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
977 yaffsfs_SetError(-ENOTDIR);
979 yaffsfs_SetError(-ENOENT);
980 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
981 yaffsfs_SetError(-EISDIR);
982 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
983 yaffsfs_SetError(-ENOTDIR);
985 result = yaffs_Unlink(dir,name);
987 if(result == YAFFS_FAIL && isDirectory)
988 yaffsfs_SetError(-ENOTEMPTY);
995 return (result == YAFFS_FAIL) ? -1 : 0;
999 int yaffs_rmdir(const YCHAR *path)
1001 return yaffsfs_DoUnlink(path,1);
1004 int yaffs_unlink(const YCHAR *path)
1006 return yaffsfs_DoUnlink(path,0);
1009 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1011 yaffs_Object *olddir = NULL;
1012 yaffs_Object *newdir = NULL;
1013 yaffs_Object *obj = NULL;
1016 int result= YAFFS_FAIL;
1017 int renameAllowed = 1;
1021 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1022 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1023 obj = yaffsfs_FindObject(NULL,oldPath,0);
1025 if(!olddir || !newdir || !obj) {
1027 yaffsfs_SetError(-EBADF);
1029 } else if(olddir->myDev != newdir->myDev) {
1030 // oops must be on same device
1032 yaffsfs_SetError(-EXDEV);
1034 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
1035 // It is a directory, check that it is not being renamed to
1036 // being its own decendent.
1037 // Do this by tracing from the new directory back to the root, checking for obj
1039 yaffs_Object *xx = newdir;
1041 while( renameAllowed && xx){
1047 yaffsfs_SetError(-EACCES);
1051 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1055 return (result == YAFFS_FAIL) ? -1 : 0;
1059 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1064 obj = yaffs_GetEquivalentObject(obj);
1067 buf->st_dev = (int)obj->myDev->genericDevice;
1068 buf->st_ino = obj->objectId;
1069 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1071 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1072 buf->st_mode |= S_IFDIR;
1073 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1074 buf->st_mode |= S_IFLNK;
1075 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1076 buf->st_mode |= S_IFREG;
1078 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1081 buf->st_rdev = obj->yst_rdev;
1082 buf->st_size = yaffs_GetObjectFileLength(obj);
1083 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1084 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1085 #if CONFIG_YAFFS_WINCE
1086 buf->yst_wince_atime[0] = obj->win_atime[0];
1087 buf->yst_wince_atime[1] = obj->win_atime[1];
1088 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1089 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1090 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1091 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1093 buf->yst_atime = obj->yst_atime;
1094 buf->yst_ctime = obj->yst_ctime;
1095 buf->yst_mtime = obj->yst_mtime;
1102 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1109 obj = yaffsfs_FindObject(NULL,path,0);
1112 obj = yaffsfs_FollowLink(obj,0);
1115 retVal = yaffsfs_DoStat(obj,buf);
1117 // todo error not found
1118 yaffsfs_SetError(-ENOENT);
1126 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1128 return yaffsfs_DoStatOrLStat(path,buf,0);
1131 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1133 return yaffsfs_DoStatOrLStat(path,buf,1);
1136 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1143 obj = yaffsfs_GetHandleObject(fd);
1146 retVal = yaffsfs_DoStat(obj,buf);
1149 yaffsfs_SetError(-EBADF);
1156 #ifdef CONFIG_YAFFS_WINCE
1157 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1164 obj = yaffsfs_GetHandleObject(fd);
1169 wctime[0] = obj->win_ctime[0];
1170 wctime[1] = obj->win_ctime[1];
1173 watime[0] = obj->win_atime[0];
1174 watime[1] = obj->win_atime[1];
1177 wmtime[0] = obj->win_mtime[0];
1178 wmtime[1] = obj->win_mtime[1];
1185 yaffsfs_SetError(-EBADF);
1193 int yaffs_set_wince_times(int fd,
1194 const unsigned *wctime,
1195 const unsigned *watime,
1196 const unsigned *wmtime)
1203 obj = yaffsfs_GetHandleObject(fd);
1208 obj->win_ctime[0] = wctime[0];
1209 obj->win_ctime[1] = wctime[1];
1212 obj->win_atime[0] = watime[0];
1213 obj->win_atime[1] = watime[1];
1216 obj->win_mtime[0] = wmtime[0];
1217 obj->win_mtime[1] = wmtime[1];
1221 result = yaffs_FlushFile(obj,0,0);
1225 yaffsfs_SetError(-EBADF);
1235 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1240 obj = yaffs_GetEquivalentObject(obj);
1243 obj->yst_mode = mode;
1245 result = yaffs_FlushFile(obj,0,0);
1248 return result == YAFFS_OK ? 0 : -1;
1252 int yaffs_access(const YCHAR *path, int amode)
1259 obj = yaffsfs_FindObject(NULL,path,0);
1264 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1266 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1268 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1272 yaffsfs_SetError(-EACCES);
1276 // todo error not found
1277 yaffsfs_SetError(-ENOENT);
1288 int yaffs_chmod(const YCHAR *path, mode_t mode)
1295 obj = yaffsfs_FindObject(NULL,path,0);
1298 retVal = yaffsfs_DoChMod(obj,mode);
1300 // todo error not found
1301 yaffsfs_SetError(-ENOENT);
1310 int yaffs_fchmod(int fd, mode_t mode)
1317 obj = yaffsfs_GetHandleObject(fd);
1320 retVal = yaffsfs_DoChMod(obj,mode);
1323 yaffsfs_SetError(-EBADF);
1331 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1333 yaffs_Object *parent = NULL;
1334 yaffs_Object *dir = NULL;
1339 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1341 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1346 yaffsfs_SetError(-ENOENT); // missing path
1347 else if (yaffs_FindObjectByName(parent,name))
1348 yaffsfs_SetError(-EEXIST); // the name already exists
1350 yaffsfs_SetError(-ENOSPC); // just assume no space
1359 int yaffs_mount(const YCHAR *path)
1362 int result=YAFFS_FAIL;
1363 yaffs_Device *dev=NULL;
1366 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1369 dev = yaffsfs_FindDevice(path,&dummy);
1371 if(!dev->isMounted){
1372 result = yaffs_GutsInitialise(dev);
1373 if(result == YAFFS_FAIL)
1374 // todo error - mount failed
1375 yaffsfs_SetError(-ENOMEM);
1376 retVal = result ? 0 : -1;
1380 //todo error - already mounted.
1381 yaffsfs_SetError(-EBUSY);
1383 // todo error - no device
1384 yaffsfs_SetError(-ENODEV);
1391 int yaffs_sync(const YCHAR *path)
1394 yaffs_Device *dev=NULL;
1398 dev = yaffsfs_FindDevice(path,&dummy);
1402 yaffs_FlushEntireDeviceCache(dev);
1403 yaffs_CheckpointSave(dev);
1407 //todo error - not mounted.
1408 yaffsfs_SetError(-EINVAL);
1411 // todo error - no device
1412 yaffsfs_SetError(-ENODEV);
1419 int yaffs_unmount(const YCHAR *path)
1422 yaffs_Device *dev=NULL;
1426 dev = yaffsfs_FindDevice(path,&dummy);
1432 yaffs_FlushEntireDeviceCache(dev);
1433 yaffs_CheckpointSave(dev);
1435 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1436 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1437 inUse = 1; // the device is in use, can't unmount
1441 yaffs_Deinitialise(dev);
1445 // todo error can't unmount as files are open
1446 yaffsfs_SetError(-EBUSY);
1449 //todo error - not mounted.
1450 yaffsfs_SetError(-EINVAL);
1454 // todo error - no device
1455 yaffsfs_SetError(-ENODEV);
1462 loff_t yaffs_freespace(const YCHAR *path)
1465 yaffs_Device *dev=NULL;
1469 dev = yaffsfs_FindDevice(path,&dummy);
1470 if(dev && dev->isMounted){
1471 retVal = yaffs_GetNumberOfFreeChunks(dev);
1472 retVal *= dev->nDataBytesPerChunk;
1475 yaffsfs_SetError(-EINVAL);
1481 loff_t yaffs_totalspace(const YCHAR *path)
1484 yaffs_Device *dev=NULL;
1488 dev = yaffsfs_FindDevice(path,&dummy);
1489 if(dev && dev->isMounted){
1490 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1491 retVal *= dev->nChunksPerBlock;
1492 retVal *= dev->nDataBytesPerChunk;
1495 yaffsfs_SetError(-EINVAL);
1501 int yaffs_inodecount(const YCHAR *path)
1504 yaffs_Device *dev=NULL;
1508 dev = yaffsfs_FindDevice(path,&dummy);
1509 if(dev && dev->isMounted) {
1510 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1511 if(nObjects > dev->nHardLinks)
1512 retVal = nObjects - dev->nHardLinks;
1516 yaffsfs_SetError(-EINVAL);
1524 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1527 yaffsfs_DeviceConfiguration *cfg;
1529 yaffsfs_configurationList = cfgList;
1531 yaffsfs_InitHandles();
1533 cfg = yaffsfs_configurationList;
1535 while(cfg && cfg->prefix && cfg->dev){
1536 cfg->dev->isMounted = 0;
1537 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1546 // Directory search stuff.
1549 // Directory search context
1551 // NB this is an opaque structure.
1557 yaffs_dirent de; /* directory entry being used by this dsc */
1558 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1559 yaffs_Object *dirObj; /* ptr to directory being searched */
1560 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1562 struct ylist_head others;
1563 } yaffsfs_DirectorySearchContext;
1567 static struct ylist_head search_contexts;
1570 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1574 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1578 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1579 dsc->nextReturn = NULL;
1581 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1582 yaffs_Object,siblings);
1584 /* Hey someone isn't playing nice! */
1588 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1592 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1594 if( dsc->nextReturn == NULL ||
1595 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1596 dsc->nextReturn = NULL;
1598 struct ylist_head *next = dsc->nextReturn->siblings.next;
1600 if( next == &dsc->dirObj->variant.directoryVariant.children)
1601 dsc->nextReturn = NULL; /* end of list */
1603 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1606 /* Hey someone isn't playing nice! */
1610 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1613 struct ylist_head *i;
1614 yaffsfs_DirectorySearchContext *dsc;
1616 /* if search contexts not initilised then skip */
1617 if(!search_contexts.next)
1620 /* Iterate through the directory search contexts.
1621 * If any are the one being removed, then advance the dsc to
1622 * the next one to prevent a hanging ptr.
1624 ylist_for_each(i, &search_contexts) {
1626 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1627 if(dsc->nextReturn == obj)
1628 yaffsfs_DirAdvance(dsc);
1634 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1636 yaffs_DIR *dir = NULL;
1637 yaffs_Object *obj = NULL;
1638 yaffsfs_DirectorySearchContext *dsc = NULL;
1642 obj = yaffsfs_FindObject(NULL,dirname,0);
1644 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1646 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1647 dir = (yaffs_DIR *)dsc;
1650 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1651 dsc->magic = YAFFS_MAGIC;
1653 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1654 YINIT_LIST_HEAD(&dsc->others);
1656 if(!search_contexts.next)
1657 YINIT_LIST_HEAD(&search_contexts);
1659 ylist_add(&dsc->others,&search_contexts);
1660 yaffsfs_SetDirRewound(dsc);
1670 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1672 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1673 struct yaffs_dirent *retVal = NULL;
1677 if(dsc && dsc->magic == YAFFS_MAGIC){
1678 yaffsfs_SetError(0);
1679 if(dsc->nextReturn){
1680 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1681 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1682 dsc->de.d_off = dsc->offset++;
1683 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1684 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1686 // this should not happen!
1687 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1689 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1691 yaffsfs_DirAdvance(dsc);
1695 yaffsfs_SetError(-EBADF);
1704 void yaffs_rewinddir(yaffs_DIR *dirp)
1706 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1710 yaffsfs_SetDirRewound(dsc);
1716 int yaffs_closedir(yaffs_DIR *dirp)
1718 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1722 ylist_del(&dsc->others); /* unhook from list */
1728 // end of directory stuff
1731 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1733 yaffs_Object *parent = NULL;
1737 int mode = 0; // ignore for now
1740 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1742 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1746 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1750 yaffsfs_SetError(-EINVAL);
1760 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1762 yaffs_Object *obj = NULL;
1768 obj = yaffsfs_FindObject(NULL,path,0);
1771 yaffsfs_SetError(-ENOENT);
1773 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1774 yaffsfs_SetError(-EINVAL);
1777 YCHAR *alias = obj->variant.symLinkVariant.alias;
1778 memset(buf,0,bufsiz);
1779 yaffs_strncpy(buf,alias,bufsiz - 1);
1786 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1788 // Creates a link called newpath to existing oldpath
1789 yaffs_Object *obj = NULL;
1790 yaffs_Object *target = NULL;
1792 int newNameLength = 0;
1797 obj = yaffsfs_FindObject(NULL,oldpath,0);
1798 target = yaffsfs_FindObject(NULL,newpath,0);
1801 yaffsfs_SetError(-ENOENT);
1804 yaffsfs_SetError(-EEXIST);
1807 yaffs_Object *newdir = NULL;
1808 yaffs_Object *link = NULL;
1812 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1815 yaffsfs_SetError(-ENOTDIR);
1817 }else if(newdir->myDev != obj->myDev){
1818 yaffsfs_SetError(-EXDEV);
1822 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1824 if(newNameLength == 0){
1825 yaffsfs_SetError(-ENOENT);
1827 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1828 yaffsfs_SetError(-ENAMETOOLONG);
1833 link = yaffs_Link(newdir,newname,obj);
1837 yaffsfs_SetError(-ENOSPC);
1848 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1853 int yaffs_DumpDevStruct(const YCHAR *path)
1858 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1861 yaffs_Device *dev = obj->myDev;
1864 "nPageWrites.......... %d\n"
1865 "nPageReads........... %d\n"
1866 "nBlockErasures....... %d\n"
1867 "nGCCopies............ %d\n"
1868 "garbageCollections... %d\n"
1869 "passiveGarbageColl'ns %d\n"
1873 dev->nBlockErasures,
1875 dev->garbageCollections,
1876 dev->passiveGarbageCollections