2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 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.35 2010-02-25 22:38:03 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);
44 unsigned int yaffs_wr_attempts;
48 * There are open inodes in yaffsfs_Inode.
49 * There are open handles in yaffsfs_Handle.
51 * Things are structured this way to be like the Linux VFS model
52 * so that interactions with the yaffs guts calls are similar.
53 * That means more common code paths and less special code.
54 * That means better testing etc.
58 int count; /* Number of handles accessing this inode */
65 int inodeId:13; /* Index to corresponding yaffsfs_Inode */
66 int useCount:16; /* Use count for this handle */
67 __u32 position; /* current position in file */
70 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
71 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
75 * Inilitalise handle management on start-up.
78 static int yaffsfs_InitHandles(void)
81 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
82 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
83 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
84 yaffsfs_handle[i].inodeId = -1;
89 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
91 if(h < 0 || h >= YAFFSFS_N_HANDLES)
94 return &yaffsfs_handle[h];
97 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
99 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
101 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
102 return &yaffsfs_inode[h->inodeId];
107 yaffs_Object *yaffsfs_GetHandleObject(int handle)
109 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
118 * yaffsfs_GetInodeIdForObject
119 * Grab an inode entry when opening a new inode.
122 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
126 yaffsfs_Inode *in = NULL;
129 obj = yaffs_GetEquivalentObject(obj);
131 /* Look for it. If we can't find it then make one */
132 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
133 if(yaffsfs_inode[i].iObj == obj)
137 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
138 if(!yaffsfs_inode[i].iObj)
144 in = &yaffsfs_inode[ret];
155 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
162 yaffs_DeleteObject(obj);
169 static void yaffsfs_PutInode(int inodeId)
171 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
172 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
175 yaffsfs_ReleaseInode(in);
182 * Grab a handle (when opening a file)
185 static int yaffsfs_GetNewHandle(void)
190 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
191 h = yaffsfs_GetHandlePointer(i);
193 /* todo bug: should never happen */
196 memset(h,0,sizeof(yaffsfs_Handle));
207 * Increase use of handle when reading/writing a file
209 static int yaffsfs_GetHandle(int handle)
211 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
213 if(h && h->useCount > 0){
221 * Let go of a handle when closing a file or aborting an open or
222 * ending a read or write.
224 static int yaffsfs_PutHandle(int handle)
226 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
228 if(h && h->useCount > 0){
232 yaffsfs_PutInode(h->inodeId);
244 * Stuff to search for a directory from a path
248 int yaffsfs_Match(YCHAR a, YCHAR b)
254 int yaffsfs_IsPathDivider(YCHAR ch)
256 YCHAR *str = YAFFS_PATH_DIVIDERS;
270 * Scan the configuration list to find the root.
271 * Curveballs: Should match paths that end in '/' too
272 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
274 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
276 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
277 const YCHAR *leftOver;
279 yaffs_Device *retval = NULL;
281 int longestMatch = -1;
285 * Check all configs, choose the one that:
286 * 1) Actually matches a prefix (ie /a amd /abc will not match
287 * 2) Matches the longest.
289 while(cfg && cfg->prefix && cfg->dev){
296 while(matching && *p && *leftOver){
297 /* Skip over any /s */
298 while(yaffsfs_IsPathDivider(*p))
301 /* Skip over any /s */
302 while(yaffsfs_IsPathDivider(*leftOver))
305 /* Now match the text part */
307 *p && !yaffsfs_IsPathDivider(*p) &&
308 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
309 if(yaffsfs_Match(*p,*leftOver)){
319 /* Skip over any /s in leftOver */
320 while(yaffsfs_IsPathDivider(*leftOver))
324 if( matching && (thisMatchLength > longestMatch)){
326 *restOfPath = (YCHAR *)leftOver;
328 longestMatch = thisMatchLength;
337 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
339 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
340 const YCHAR *leftOver;
342 yaffs_Device *retval = NULL;
344 int longestMatch = -1;
347 * Check all configs, choose the one that:
348 * 1) Actually matches a prefix (ie /a amd /abc will not match
349 * 2) Matches the longest.
351 while(cfg && cfg->prefix && cfg->dev){
356 while(*p && /* unmatched part of prefix */
357 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) &&
358 *leftOver && yaffsfs_Match(*p,*leftOver)){
365 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && /* end of prefix */
366 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && /* no more in this path name part */
367 (thisMatchLength > longestMatch)){
369 *restOfPath = (YCHAR *)leftOver;
371 longestMatch = thisMatchLength;
379 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
384 dev= yaffsfs_FindDevice(path,restOfPath);
385 if(dev && dev->isMounted){
391 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
394 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
395 YCHAR *alias = obj->variant.symLinkVariant.alias;
397 if(yaffsfs_IsPathDivider(*alias))
398 /* Starts with a /, need to scan from root up */
399 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
401 /* Relative to here, so use the parent of the symlink as a start */
402 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
409 * yaffsfs_FindDirectory
410 * Parse a path to determine the directory and the name within the directory.
412 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
414 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,
415 const YCHAR *path, YCHAR **name, int symDepth)
419 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
422 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
427 restOfPath = (YCHAR *)path;
430 dir = yaffsfs_FindRoot(path,&restOfPath);
435 * curve ball: also throw away surplus '/'
436 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
438 while(yaffsfs_IsPathDivider(*restOfPath))
439 restOfPath++; /* get rid of '/' */
444 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
445 if (i < YAFFS_MAX_NAME_LENGTH){
446 str[i] = *restOfPath;
454 /* got to the end of the string */
457 if(yaffs_strcmp(str,_Y(".")) == 0)
461 else if(yaffs_strcmp(str,_Y("..")) == 0)
464 dir = yaffs_FindObjectByName(dir,str);
466 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
467 dir = yaffsfs_FollowLink(dir,symDepth);
470 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
475 /* directory did not exist. */
479 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,
480 const YCHAR *path,YCHAR **name,int symDepth)
482 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
486 * yaffsfs_FindObject turns a path for an existing object into the object
488 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
493 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
496 return yaffs_FindObjectByName(dir,name);
502 int yaffs_dup(int fd)
505 yaffsfs_Handle *oldPtr = NULL;
506 yaffsfs_Handle *newPtr = NULL;
510 oldPtr = yaffsfs_GetHandlePointer(fd);
511 if(oldPtr && oldPtr->useCount > 0)
512 newHandle = yaffsfs_GetNewHandle();
514 newPtr = yaffsfs_GetHandlePointer(newHandle);
522 yaffsfs_SetError(-EBADF);
524 yaffsfs_SetError(-ENOMEM);
530 int yaffs_open(const YCHAR *path, int oflag, int mode)
532 yaffs_Object *obj = NULL;
533 yaffs_Object *dir = NULL;
536 yaffsfs_Handle *h = NULL;
539 int errorReported = 0;
541 /* O_EXCL only has meaning if O_CREAT is specified */
542 if(!(oflag & O_CREAT))
545 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
546 if( (oflag & O_CREAT) & (oflag & O_EXCL))
549 /* Todo: Are there any more flag combos to sanitise ? */
554 handle = yaffsfs_GetNewHandle();
558 h = yaffsfs_GetHandlePointer(handle);
561 /* try to find the exisiting object */
562 obj = yaffsfs_FindObject(NULL,path,0);
564 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
565 obj = yaffsfs_FollowLink(obj,symDepth++);
568 obj = yaffs_GetEquivalentObject(obj);
570 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
575 /* The file already exists */
577 /* Open should fail if O_CREAT and O_EXCL are specified since
580 if((oflag & O_EXCL) && (oflag & O_CREAT)){
582 yaffsfs_SetError(-EEXIST);
586 /* Check file permissions */
587 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && /* ie O_RDONLY */
588 !(obj->yst_mode & S_IREAD))
591 if( (oflag & O_RDWR) &&
592 !(obj->yst_mode & S_IREAD))
595 if( (oflag & (O_RDWR | O_WRONLY)) &&
596 !(obj->yst_mode & S_IWRITE))
599 } else if((oflag & O_CREAT)) {
600 /* Let's see if we can create this file */
601 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
602 if(dir && dir->myDev->readOnly){
603 yaffsfs_SetError(-EINVAL);
606 obj = yaffs_MknodFile(dir,name,mode,0,0);
608 yaffsfs_SetError(-ENOTDIR);
613 if(obj && !openDenied) {
614 int inodeId = yaffsfs_GetInodeIdForObject(obj);
618 * Todo: Fix any problem if inodes run out, though that
619 * can't happen if the number of inode items >= number of handles.
623 h->inodeId = inodeId;
624 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
625 h->append = (oflag & O_APPEND) ? 1 : 0;
628 /* Hook inode to object */
629 obj->myInode = (void*) &yaffsfs_inode[inodeId];
631 if((oflag & O_TRUNC) && !h->readOnly)
632 yaffs_ResizeFile(obj,0);
634 yaffsfs_PutHandle(handle);
636 yaffsfs_SetError(-EACCES);
648 int yaffs_Dofsync(int fd,int datasync)
650 yaffsfs_Handle *h = NULL;
655 h = yaffsfs_GetHandlePointer(fd);
657 if(h && h->useCount > 0)
659 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
662 yaffsfs_SetError(-EBADF);
671 int yaffs_fsync(int fd)
673 return yaffs_Dofsync(fd,0);
676 int yaffs_flush(int fd)
678 return yaffs_fsync(fd);
681 int yaffs_fdatasync(int fd)
683 return yaffs_Dofsync(fd,1);
686 int yaffs_close(int fd)
688 yaffsfs_Handle *h = NULL;
693 h = yaffsfs_GetHandlePointer(fd);
695 if(h && h->useCount > 0) {
697 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
698 yaffsfs_PutHandle(fd);
702 yaffsfs_SetError(-EBADF);
713 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
715 yaffsfs_Handle *h = NULL;
716 yaffs_Object *obj = NULL;
722 unsigned int maxRead;
723 __u8 *buf = (__u8 *)vbuf;
726 h = yaffsfs_GetHandlePointer(fd);
727 obj = yaffsfs_GetHandleObject(fd);
731 yaffsfs_SetError(-EBADF);
733 } else if( h && obj){
737 startPos = h->position;
741 if(yaffs_GetObjectFileLength(obj) > pos)
742 maxRead = yaffs_GetObjectFileLength(obj) - pos;
750 yaffsfs_GetHandle(fd);
753 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
756 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
767 nbyte = 0; /* no more to read */
777 yaffsfs_PutHandle(fd);
781 h->position = startPos + totalRead;
791 return (totalRead >= 0) ? totalRead : -1;
795 int yaffs_read(int fd, void *buf, unsigned int nbyte)
797 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
800 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
802 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
805 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
807 yaffsfs_Handle *h = NULL;
808 yaffs_Object *obj = NULL;
812 int totalWritten = 0;
813 int writeThrough = 0;
815 const __u8 *buf = (const __u8 *)vbuf;
818 h = yaffsfs_GetHandlePointer(fd);
819 obj = yaffsfs_GetHandleObject(fd);
823 yaffsfs_SetError(-EBADF);
825 } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
826 yaffsfs_SetError(-EINVAL);
828 } else if( h && obj){
830 startPos = yaffs_GetObjectFileLength(obj);
834 startPos = h->position;
836 yaffsfs_GetHandle(fd);
839 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
843 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
845 totalWritten += nWritten;
850 if(nWritten == nToWrite)
855 if(nWritten < 1 && totalWritten < 1){
856 yaffsfs_SetError(-ENOSPC);
866 yaffsfs_PutHandle(fd);
870 h->position = startPos + totalWritten;
879 return (totalWritten >= 0) ? totalWritten : -1;
882 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
884 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
887 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
889 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
893 int yaffs_truncate(const YCHAR *path,off_t newSize)
895 yaffs_Object *obj = NULL;
896 int result = YAFFS_FAIL;
900 obj = yaffsfs_FindObject(NULL,path,0);
902 obj = yaffs_GetEquivalentObject(obj);
905 yaffsfs_SetError(-ENOENT);
906 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
907 yaffsfs_SetError(-EISDIR);
908 else if(obj->myDev->readOnly)
909 yaffsfs_SetError(-EINVAL);
911 result = yaffs_ResizeFile(obj,newSize);
916 return (result) ? 0 : -1;
919 int yaffs_ftruncate(int fd, off_t newSize)
921 yaffsfs_Handle *h = NULL;
922 yaffs_Object *obj = NULL;
926 h = yaffsfs_GetHandlePointer(fd);
927 obj = yaffsfs_GetHandleObject(fd);
931 yaffsfs_SetError(-EBADF);
932 else if(obj->myDev->readOnly)
933 yaffsfs_SetError(-EINVAL);
935 /* resize the file */
936 result = yaffs_ResizeFile(obj,newSize);
940 return (result) ? 0 : -1;
944 off_t yaffs_lseek(int fd, off_t offset, int whence)
946 yaffsfs_Handle *h = NULL;
947 yaffs_Object *obj = NULL;
952 h = yaffsfs_GetHandlePointer(fd);
953 obj = yaffsfs_GetHandleObject(fd);
957 yaffsfs_SetError(-EBADF);
958 else if(whence == SEEK_SET){
962 else if(whence == SEEK_CUR) {
963 if( (h->position + offset) >= 0)
964 pos = (h->position + offset);
966 else if(whence == SEEK_END) {
967 fSize = yaffs_GetObjectFileLength(obj);
968 if(fSize >= 0 && (fSize + offset) >= 0)
969 pos = fSize + offset;
985 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
987 yaffs_Object *dir = NULL;
988 yaffs_Object *obj = NULL;
990 int result = YAFFS_FAIL;
994 obj = yaffsfs_FindObject(NULL,path,0);
995 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
997 yaffsfs_SetError(-ENOTDIR);
999 yaffsfs_SetError(-ENOENT);
1000 else if(obj->myDev->readOnly)
1001 yaffsfs_SetError(-EINVAL);
1002 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1003 yaffsfs_SetError(-EISDIR);
1004 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1005 yaffsfs_SetError(-ENOTDIR);
1007 result = yaffs_Unlink(dir,name);
1009 if(result == YAFFS_FAIL && isDirectory)
1010 yaffsfs_SetError(-ENOTEMPTY);
1017 return (result == YAFFS_FAIL) ? -1 : 0;
1021 int yaffs_rmdir(const YCHAR *path)
1023 return yaffsfs_DoUnlink(path,1);
1026 int yaffs_unlink(const YCHAR *path)
1028 return yaffsfs_DoUnlink(path,0);
1031 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1033 yaffs_Object *olddir = NULL;
1034 yaffs_Object *newdir = NULL;
1035 yaffs_Object *obj = NULL;
1038 int result= YAFFS_FAIL;
1039 int renameAllowed = 1;
1043 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1044 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1045 obj = yaffsfs_FindObject(NULL,oldPath,0);
1047 if(!olddir || !newdir || !obj) {
1049 yaffsfs_SetError(-EBADF);
1051 } else if(obj->myDev->readOnly){
1052 yaffsfs_SetError(-EINVAL);
1054 } else if(olddir->myDev != newdir->myDev) {
1055 /* oops must be on same device */
1057 yaffsfs_SetError(-EXDEV);
1059 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
1061 * It is a directory, check that it is not being renamed to
1062 * being its own decendent.
1063 * Do this by tracing from the new directory back to the root, checking for obj
1066 yaffs_Object *xx = newdir;
1068 while( renameAllowed && xx){
1074 yaffsfs_SetError(-EACCES);
1078 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1082 return (result == YAFFS_FAIL) ? -1 : 0;
1086 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1091 obj = yaffs_GetEquivalentObject(obj);
1094 buf->st_dev = (int)obj->myDev->context;
1095 buf->st_ino = obj->objectId;
1096 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1098 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1099 buf->st_mode |= S_IFDIR;
1100 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1101 buf->st_mode |= S_IFLNK;
1102 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1103 buf->st_mode |= S_IFREG;
1105 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1108 buf->st_rdev = obj->yst_rdev;
1109 buf->st_size = yaffs_GetObjectFileLength(obj);
1110 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1111 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1112 #if CONFIG_YAFFS_WINCE
1113 buf->yst_wince_atime[0] = obj->win_atime[0];
1114 buf->yst_wince_atime[1] = obj->win_atime[1];
1115 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1116 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1117 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1118 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1120 buf->yst_atime = obj->yst_atime;
1121 buf->yst_ctime = obj->yst_ctime;
1122 buf->yst_mtime = obj->yst_mtime;
1129 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1136 obj = yaffsfs_FindObject(NULL,path,0);
1139 obj = yaffsfs_FollowLink(obj,0);
1142 retVal = yaffsfs_DoStat(obj,buf);
1144 /* todo error not found */
1145 yaffsfs_SetError(-ENOENT);
1153 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1155 return yaffsfs_DoStatOrLStat(path,buf,0);
1158 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1160 return yaffsfs_DoStatOrLStat(path,buf,1);
1163 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1170 obj = yaffsfs_GetHandleObject(fd);
1173 retVal = yaffsfs_DoStat(obj,buf);
1176 yaffsfs_SetError(-EBADF);
1184 /* xattrib functions */
1187 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1194 obj = yaffsfs_FindObject(NULL,path,0);
1197 obj = yaffsfs_FollowLink(obj,0);
1200 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1202 yaffsfs_SetError(retVal);
1206 /* todo error not found */
1207 yaffsfs_SetError(-ENOENT);
1215 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1217 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1220 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1222 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1227 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1234 obj = yaffsfs_GetHandleObject(fd);
1237 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1239 yaffsfs_SetError(retVal);
1244 yaffsfs_SetError(-EBADF);
1251 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1258 obj = yaffsfs_FindObject(NULL,path,0);
1261 obj = yaffsfs_FollowLink(obj,0);
1264 retVal = yaffs_GetXAttribute(obj,name,data,size);
1266 yaffsfs_SetError(retVal);
1270 /* todo error not found */
1271 yaffsfs_SetError(-ENOENT);
1279 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1281 return yaffs_do_getxattr( path, name, data, size, 1);
1283 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1285 return yaffs_do_getxattr( path, name, data, size, 0);
1290 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1297 obj = yaffsfs_GetHandleObject(fd);
1300 retVal = yaffs_GetXAttribute(obj,name,data,size);
1302 yaffsfs_SetError(retVal);
1307 yaffsfs_SetError(-EBADF);
1314 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1321 obj = yaffsfs_FindObject(NULL,path,0);
1324 obj = yaffsfs_FollowLink(obj,0);
1327 retVal = yaffs_ListXAttributes(obj, data,size);
1329 yaffsfs_SetError(retVal);
1333 /* todo error not found */
1334 yaffsfs_SetError(-ENOENT);
1342 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1344 return yaffs_do_listxattr(path, data, size, 1);
1347 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1349 return yaffs_do_listxattr(path, data, size, 0);
1352 int yaffs_flistxattr(int fd, char *data, int size)
1359 obj = yaffsfs_GetHandleObject(fd);
1362 retVal = yaffs_ListXAttributes(obj,data,size);
1364 yaffsfs_SetError(retVal);
1369 yaffsfs_SetError(-EBADF);
1376 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1383 obj = yaffsfs_FindObject(NULL,path,0);
1386 obj = yaffsfs_FollowLink(obj,0);
1389 retVal = yaffs_RemoveXAttribute(obj,name);
1391 yaffsfs_SetError(retVal);
1395 /* todo error not found */
1396 yaffsfs_SetError(-ENOENT);
1404 int yaffs_removexattr(const YCHAR *path, const char *name)
1406 return yaffs_do_removexattr(path, name, 1);
1409 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1411 return yaffs_do_removexattr(path, name, 0);
1414 int yaffs_fremovexattr(int fd, const char *name)
1421 obj = yaffsfs_GetHandleObject(fd);
1424 retVal = yaffs_RemoveXAttribute(obj,name);
1426 yaffsfs_SetError(retVal);
1431 yaffsfs_SetError(-EBADF);
1438 #ifdef CONFIG_YAFFS_WINCE
1439 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1446 obj = yaffsfs_GetHandleObject(fd);
1451 wctime[0] = obj->win_ctime[0];
1452 wctime[1] = obj->win_ctime[1];
1455 watime[0] = obj->win_atime[0];
1456 watime[1] = obj->win_atime[1];
1459 wmtime[0] = obj->win_mtime[0];
1460 wmtime[1] = obj->win_mtime[1];
1467 yaffsfs_SetError(-EBADF);
1475 int yaffs_set_wince_times(int fd,
1476 const unsigned *wctime,
1477 const unsigned *watime,
1478 const unsigned *wmtime)
1485 obj = yaffsfs_GetHandleObject(fd);
1490 obj->win_ctime[0] = wctime[0];
1491 obj->win_ctime[1] = wctime[1];
1494 obj->win_atime[0] = watime[0];
1495 obj->win_atime[1] = watime[1];
1498 obj->win_mtime[0] = wmtime[0];
1499 obj->win_mtime[1] = wmtime[1];
1503 result = yaffs_FlushFile(obj,0,0);
1507 yaffsfs_SetError(-EBADF);
1517 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1522 obj = yaffs_GetEquivalentObject(obj);
1525 obj->yst_mode = mode;
1527 result = yaffs_FlushFile(obj,0,0);
1530 return result == YAFFS_OK ? 0 : -1;
1534 int yaffs_access(const YCHAR *path, int amode)
1541 obj = yaffsfs_FindObject(NULL,path,0);
1546 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1548 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1550 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1554 yaffsfs_SetError(-EACCES);
1558 /* todo error not found */
1559 yaffsfs_SetError(-ENOENT);
1570 int yaffs_chmod(const YCHAR *path, mode_t mode)
1577 obj = yaffsfs_FindObject(NULL,path,0);
1580 yaffsfs_SetError(-ENOENT);
1581 else if(obj->myDev->readOnly)
1582 yaffsfs_SetError(-EINVAL);
1584 retVal = yaffsfs_DoChMod(obj,mode);
1593 int yaffs_fchmod(int fd, mode_t mode)
1600 obj = yaffsfs_GetHandleObject(fd);
1603 yaffsfs_SetError(-ENOENT);
1604 else if(obj->myDev->readOnly)
1605 yaffsfs_SetError(-EINVAL);
1607 retVal = yaffsfs_DoChMod(obj,mode);
1615 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1617 yaffs_Object *parent = NULL;
1618 yaffs_Object *dir = NULL;
1623 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1624 if(parent && parent->myDev->readOnly){
1625 yaffsfs_SetError(-EINVAL);
1628 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1633 yaffsfs_SetError(-ENOENT); /* missing path */
1634 else if (yaffs_FindObjectByName(parent,name))
1635 yaffsfs_SetError(-EEXIST); /* the name already exists */
1637 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1647 int yaffs_mount2(const YCHAR *path,int readOnly)
1650 int result=YAFFS_FAIL;
1651 yaffs_Device *dev=NULL;
1654 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1657 dev = yaffsfs_FindDevice(path,&dummy);
1659 if(!dev->isMounted){
1660 dev->readOnly = readOnly ? 1 : 0;
1661 result = yaffs_GutsInitialise(dev);
1662 if(result == YAFFS_FAIL)
1663 /* todo error - mount failed */
1664 yaffsfs_SetError(-ENOMEM);
1665 retVal = result ? 0 : -1;
1669 /* todo error - already mounted. */
1670 yaffsfs_SetError(-EBUSY);
1672 /* todo error - no device */
1673 yaffsfs_SetError(-ENODEV);
1680 int yaffs_mount(const YCHAR *path)
1682 return yaffs_mount2(path,0);
1685 int yaffs_sync(const YCHAR *path)
1688 yaffs_Device *dev=NULL;
1692 dev = yaffsfs_FindDevice(path,&dummy);
1696 yaffs_FlushEntireDeviceCache(dev);
1697 yaffs_CheckpointSave(dev);
1701 /* todo error - not mounted. */
1702 yaffsfs_SetError(-EINVAL);
1705 /* todo error - no device */
1706 yaffsfs_SetError(-ENODEV);
1713 int yaffs_remount(const YCHAR *path, int force, int readOnly)
1716 yaffs_Device *dev=NULL;
1720 dev = yaffsfs_FindDevice(path,&dummy);
1726 yaffs_FlushEntireDeviceCache(dev);
1728 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1729 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1730 inUse = 1; /* the device is in use, can't unmount */
1733 if(!inUse || force){
1735 yaffs_CheckpointSave(dev);
1736 dev->readOnly = readOnly ? 1 : 0;
1739 yaffsfs_SetError(-EBUSY);
1742 yaffsfs_SetError(-EINVAL);
1746 yaffsfs_SetError(-ENODEV);
1753 int yaffs_unmount2(const YCHAR *path, int force)
1756 yaffs_Device *dev=NULL;
1760 dev = yaffsfs_FindDevice(path,&dummy);
1766 yaffs_FlushEntireDeviceCache(dev);
1767 yaffs_CheckpointSave(dev);
1769 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1770 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1771 inUse = 1; /* the device is in use, can't unmount */
1774 if(!inUse || force){
1775 yaffs_Deinitialise(dev);
1779 /* todo error can't unmount as files are open */
1780 yaffsfs_SetError(-EBUSY);
1783 /* todo error - not mounted. */
1784 yaffsfs_SetError(-EINVAL);
1788 /* todo error - no device */
1789 yaffsfs_SetError(-ENODEV);
1796 int yaffs_unmount(const YCHAR *path)
1798 return yaffs_unmount2(path,0);
1801 loff_t yaffs_freespace(const YCHAR *path)
1804 yaffs_Device *dev=NULL;
1808 dev = yaffsfs_FindDevice(path,&dummy);
1809 if(dev && dev->isMounted){
1810 retVal = yaffs_GetNumberOfFreeChunks(dev);
1811 retVal *= dev->nDataBytesPerChunk;
1814 yaffsfs_SetError(-EINVAL);
1820 loff_t yaffs_totalspace(const YCHAR *path)
1823 yaffs_Device *dev=NULL;
1827 dev = yaffsfs_FindDevice(path,&dummy);
1828 if(dev && dev->isMounted){
1829 retVal = (dev->param.endBlock - dev->param.startBlock + 1) - dev->param.nReservedBlocks;
1830 retVal *= dev->param.nChunksPerBlock;
1831 retVal *= dev->nDataBytesPerChunk;
1834 yaffsfs_SetError(-EINVAL);
1840 int yaffs_inodecount(const YCHAR *path)
1843 yaffs_Device *dev=NULL;
1847 dev = yaffsfs_FindDevice(path,&dummy);
1848 if(dev && dev->isMounted) {
1849 int nObjects = dev->nObjects;
1850 if(nObjects > dev->nHardLinks)
1851 retVal = nObjects - dev->nHardLinks;
1855 yaffsfs_SetError(-EINVAL);
1863 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1866 yaffsfs_DeviceConfiguration *cfg;
1868 yaffsfs_configurationList = cfgList;
1870 yaffsfs_InitHandles();
1872 cfg = yaffsfs_configurationList;
1874 while(cfg && cfg->prefix && cfg->dev){
1875 cfg->dev->isMounted = 0;
1876 cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
1884 /* Directory search stuff. */
1887 * Directory search context
1889 * NB this is an opaque structure.
1896 yaffs_dirent de; /* directory entry being used by this dsc */
1897 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1898 yaffs_Object *dirObj; /* ptr to directory being searched */
1899 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1901 struct ylist_head others;
1902 } yaffsfs_DirectorySearchContext;
1906 static struct ylist_head search_contexts;
1909 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1913 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1917 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1918 dsc->nextReturn = NULL;
1920 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1921 yaffs_Object,siblings);
1923 /* Hey someone isn't playing nice! */
1927 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1931 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1933 if( dsc->nextReturn == NULL ||
1934 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1935 dsc->nextReturn = NULL;
1937 struct ylist_head *next = dsc->nextReturn->siblings.next;
1939 if( next == &dsc->dirObj->variant.directoryVariant.children)
1940 dsc->nextReturn = NULL; /* end of list */
1942 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1945 /* Hey someone isn't playing nice! */
1949 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1952 struct ylist_head *i;
1953 yaffsfs_DirectorySearchContext *dsc;
1955 /* if search contexts not initilised then skip */
1956 if(!search_contexts.next)
1959 /* Iterate through the directory search contexts.
1960 * If any are the one being removed, then advance the dsc to
1961 * the next one to prevent a hanging ptr.
1963 ylist_for_each(i, &search_contexts) {
1965 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1966 if(dsc->nextReturn == obj)
1967 yaffsfs_DirAdvance(dsc);
1973 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1975 yaffs_DIR *dir = NULL;
1976 yaffs_Object *obj = NULL;
1977 yaffsfs_DirectorySearchContext *dsc = NULL;
1981 obj = yaffsfs_FindObject(NULL,dirname,0);
1983 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1985 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1986 dir = (yaffs_DIR *)dsc;
1989 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1990 dsc->magic = YAFFS_MAGIC;
1992 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1993 YINIT_LIST_HEAD(&dsc->others);
1995 if(!search_contexts.next)
1996 YINIT_LIST_HEAD(&search_contexts);
1998 ylist_add(&dsc->others,&search_contexts);
1999 yaffsfs_SetDirRewound(dsc);
2009 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2011 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2012 struct yaffs_dirent *retVal = NULL;
2016 if(dsc && dsc->magic == YAFFS_MAGIC){
2017 yaffsfs_SetError(0);
2018 if(dsc->nextReturn){
2019 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
2020 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2021 dsc->de.d_off = dsc->offset++;
2022 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2023 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2025 /* this should not happen! */
2026 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2028 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2030 yaffsfs_DirAdvance(dsc);
2034 yaffsfs_SetError(-EBADF);
2043 void yaffs_rewinddir(yaffs_DIR *dirp)
2045 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2049 yaffsfs_SetDirRewound(dsc);
2055 int yaffs_closedir(yaffs_DIR *dirp)
2057 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2061 ylist_del(&dsc->others); /* unhook from list */
2067 /* End of directory stuff */
2070 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2072 yaffs_Object *parent = NULL;
2076 int mode = 0; /* ignore for now */
2079 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2080 if(parent && parent->myDev->readOnly)
2081 yaffsfs_SetError(-EINVAL);
2083 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
2087 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2091 yaffsfs_SetError(-EINVAL);
2101 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2103 yaffs_Object *obj = NULL;
2109 obj = yaffsfs_FindObject(NULL,path,0);
2112 yaffsfs_SetError(-ENOENT);
2114 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
2115 yaffsfs_SetError(-EINVAL);
2118 YCHAR *alias = obj->variant.symLinkVariant.alias;
2119 memset(buf,0,bufsiz);
2120 yaffs_strncpy(buf,alias,bufsiz - 1);
2127 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2129 /* Creates a link called newpath to existing oldpath */
2130 yaffs_Object *obj = NULL;
2131 yaffs_Object *target = NULL;
2133 int newNameLength = 0;
2138 obj = yaffsfs_FindObject(NULL,oldpath,0);
2139 target = yaffsfs_FindObject(NULL,newpath,0);
2142 yaffsfs_SetError(-ENOENT);
2144 } else if(obj->myDev->readOnly){
2145 yaffsfs_SetError(-EINVAL);
2148 yaffsfs_SetError(-EEXIST);
2151 yaffs_Object *newdir = NULL;
2152 yaffs_Object *link = NULL;
2156 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2159 yaffsfs_SetError(-ENOTDIR);
2161 }else if(newdir->myDev != obj->myDev){
2162 yaffsfs_SetError(-EXDEV);
2166 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2168 if(newNameLength == 0){
2169 yaffsfs_SetError(-ENOENT);
2171 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
2172 yaffsfs_SetError(-ENAMETOOLONG);
2177 link = yaffs_Link(newdir,newname,obj);
2181 yaffsfs_SetError(-ENOSPC);
2192 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2197 int yaffs_DumpDevStruct(const YCHAR *path)
2202 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
2205 yaffs_Device *dev = obj->myDev;
2208 "nPageWrites.......... %d\n"
2209 "nPageReads........... %d\n"
2210 "nBlockErasures....... %d\n"
2211 "nGCCopies............ %d\n"
2212 "garbageCollections... %d\n"
2213 "passiveGarbageColl'ns %d\n"
2217 dev->nBlockErasures,
2219 dev->garbageCollections,
2220 dev->passiveGarbageCollections