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){
832 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 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1194 obj = yaffsfs_FindObject(NULL,path,0);
1196 obj = yaffsfs_FollowLink(obj,0);
1199 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1201 /* todo error not found */
1202 yaffsfs_SetError(-ENOENT);
1210 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1217 obj = yaffsfs_GetHandleObject(fd);
1220 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1223 yaffsfs_SetError(-EBADF);
1230 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1237 obj = yaffsfs_FindObject(NULL,path,0);
1239 obj = yaffsfs_FollowLink(obj,0);
1242 retVal = yaffs_GetXAttribute(obj,name,data,size);
1244 /* todo error not found */
1245 yaffsfs_SetError(-ENOENT);
1253 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1260 obj = yaffsfs_GetHandleObject(fd);
1263 retVal = yaffs_GetXAttribute(obj,name,data,size);
1266 yaffsfs_SetError(-EBADF);
1273 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1280 obj = yaffsfs_FindObject(NULL,path,0);
1282 obj = yaffsfs_FollowLink(obj,0);
1285 retVal = yaffs_ListXAttributes(obj, data,size);
1287 /* todo error not found */
1288 yaffsfs_SetError(-ENOENT);
1296 int yaffs_flistxattr(int fd, char *data, int size)
1303 obj = yaffsfs_GetHandleObject(fd);
1306 retVal = yaffs_ListXAttributes(obj,data,size);
1309 yaffsfs_SetError(-EBADF);
1316 int yaffs_removexattr(const YCHAR *path, const char *name)
1323 obj = yaffsfs_FindObject(NULL,path,0);
1325 obj = yaffsfs_FollowLink(obj,0);
1328 retVal = yaffs_RemoveXAttribute(obj,name);
1330 /* todo error not found */
1331 yaffsfs_SetError(-ENOENT);
1339 int yaffs_fremovexattr(int fd, const char *name)
1346 obj = yaffsfs_GetHandleObject(fd);
1349 retVal = yaffs_RemoveXAttribute(obj,name);
1352 yaffsfs_SetError(-EBADF);
1359 #ifdef CONFIG_YAFFS_WINCE
1360 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1367 obj = yaffsfs_GetHandleObject(fd);
1372 wctime[0] = obj->win_ctime[0];
1373 wctime[1] = obj->win_ctime[1];
1376 watime[0] = obj->win_atime[0];
1377 watime[1] = obj->win_atime[1];
1380 wmtime[0] = obj->win_mtime[0];
1381 wmtime[1] = obj->win_mtime[1];
1388 yaffsfs_SetError(-EBADF);
1396 int yaffs_set_wince_times(int fd,
1397 const unsigned *wctime,
1398 const unsigned *watime,
1399 const unsigned *wmtime)
1406 obj = yaffsfs_GetHandleObject(fd);
1411 obj->win_ctime[0] = wctime[0];
1412 obj->win_ctime[1] = wctime[1];
1415 obj->win_atime[0] = watime[0];
1416 obj->win_atime[1] = watime[1];
1419 obj->win_mtime[0] = wmtime[0];
1420 obj->win_mtime[1] = wmtime[1];
1424 result = yaffs_FlushFile(obj,0,0);
1428 yaffsfs_SetError(-EBADF);
1438 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1443 obj = yaffs_GetEquivalentObject(obj);
1446 obj->yst_mode = mode;
1448 result = yaffs_FlushFile(obj,0,0);
1451 return result == YAFFS_OK ? 0 : -1;
1455 int yaffs_access(const YCHAR *path, int amode)
1462 obj = yaffsfs_FindObject(NULL,path,0);
1467 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1469 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1471 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1475 yaffsfs_SetError(-EACCES);
1479 /* todo error not found */
1480 yaffsfs_SetError(-ENOENT);
1491 int yaffs_chmod(const YCHAR *path, mode_t mode)
1498 obj = yaffsfs_FindObject(NULL,path,0);
1501 yaffsfs_SetError(-ENOENT);
1502 else if(obj->myDev->readOnly)
1503 yaffsfs_SetError(-EINVAL);
1505 retVal = yaffsfs_DoChMod(obj,mode);
1514 int yaffs_fchmod(int fd, mode_t mode)
1521 obj = yaffsfs_GetHandleObject(fd);
1524 yaffsfs_SetError(-ENOENT);
1525 else if(obj->myDev->readOnly)
1526 yaffsfs_SetError(-EINVAL);
1528 retVal = yaffsfs_DoChMod(obj,mode);
1536 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1538 yaffs_Object *parent = NULL;
1539 yaffs_Object *dir = NULL;
1544 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1545 if(parent && parent->myDev->readOnly){
1546 yaffsfs_SetError(-EINVAL);
1549 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1554 yaffsfs_SetError(-ENOENT); /* missing path */
1555 else if (yaffs_FindObjectByName(parent,name))
1556 yaffsfs_SetError(-EEXIST); /* the name already exists */
1558 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1568 int yaffs_mount2(const YCHAR *path,int readOnly)
1571 int result=YAFFS_FAIL;
1572 yaffs_Device *dev=NULL;
1575 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1578 dev = yaffsfs_FindDevice(path,&dummy);
1580 if(!dev->isMounted){
1581 dev->readOnly = readOnly ? 1 : 0;
1582 result = yaffs_GutsInitialise(dev);
1583 if(result == YAFFS_FAIL)
1584 /* todo error - mount failed */
1585 yaffsfs_SetError(-ENOMEM);
1586 retVal = result ? 0 : -1;
1590 /* todo error - already mounted. */
1591 yaffsfs_SetError(-EBUSY);
1593 /* todo error - no device */
1594 yaffsfs_SetError(-ENODEV);
1601 int yaffs_mount(const YCHAR *path)
1603 return yaffs_mount2(path,0);
1606 int yaffs_sync(const YCHAR *path)
1609 yaffs_Device *dev=NULL;
1613 dev = yaffsfs_FindDevice(path,&dummy);
1617 yaffs_FlushEntireDeviceCache(dev);
1618 yaffs_CheckpointSave(dev);
1622 /* todo error - not mounted. */
1623 yaffsfs_SetError(-EINVAL);
1626 /* todo error - no device */
1627 yaffsfs_SetError(-ENODEV);
1634 int yaffs_remount(const YCHAR *path, int force, int readOnly)
1637 yaffs_Device *dev=NULL;
1641 dev = yaffsfs_FindDevice(path,&dummy);
1647 yaffs_FlushEntireDeviceCache(dev);
1649 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1650 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1651 inUse = 1; /* the device is in use, can't unmount */
1654 if(!inUse || force){
1656 yaffs_CheckpointSave(dev);
1657 dev->readOnly = readOnly ? 1 : 0;
1660 yaffsfs_SetError(-EBUSY);
1663 yaffsfs_SetError(-EINVAL);
1667 yaffsfs_SetError(-ENODEV);
1674 int yaffs_unmount2(const YCHAR *path, int force)
1677 yaffs_Device *dev=NULL;
1681 dev = yaffsfs_FindDevice(path,&dummy);
1687 yaffs_FlushEntireDeviceCache(dev);
1688 yaffs_CheckpointSave(dev);
1690 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1691 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1692 inUse = 1; /* the device is in use, can't unmount */
1695 if(!inUse || force){
1696 yaffs_Deinitialise(dev);
1700 /* todo error can't unmount as files are open */
1701 yaffsfs_SetError(-EBUSY);
1704 /* todo error - not mounted. */
1705 yaffsfs_SetError(-EINVAL);
1709 /* todo error - no device */
1710 yaffsfs_SetError(-ENODEV);
1717 int yaffs_unmount(const YCHAR *path)
1719 return yaffs_unmount2(path,0);
1722 loff_t yaffs_freespace(const YCHAR *path)
1725 yaffs_Device *dev=NULL;
1729 dev = yaffsfs_FindDevice(path,&dummy);
1730 if(dev && dev->isMounted){
1731 retVal = yaffs_GetNumberOfFreeChunks(dev);
1732 retVal *= dev->nDataBytesPerChunk;
1735 yaffsfs_SetError(-EINVAL);
1741 loff_t yaffs_totalspace(const YCHAR *path)
1744 yaffs_Device *dev=NULL;
1748 dev = yaffsfs_FindDevice(path,&dummy);
1749 if(dev && dev->isMounted){
1750 retVal = (dev->param.endBlock - dev->param.startBlock + 1) - dev->param.nReservedBlocks;
1751 retVal *= dev->param.nChunksPerBlock;
1752 retVal *= dev->nDataBytesPerChunk;
1755 yaffsfs_SetError(-EINVAL);
1761 int yaffs_inodecount(const YCHAR *path)
1764 yaffs_Device *dev=NULL;
1768 dev = yaffsfs_FindDevice(path,&dummy);
1769 if(dev && dev->isMounted) {
1770 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1771 if(nObjects > dev->nHardLinks)
1772 retVal = nObjects - dev->nHardLinks;
1776 yaffsfs_SetError(-EINVAL);
1784 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1787 yaffsfs_DeviceConfiguration *cfg;
1789 yaffsfs_configurationList = cfgList;
1791 yaffsfs_InitHandles();
1793 cfg = yaffsfs_configurationList;
1795 while(cfg && cfg->prefix && cfg->dev){
1796 cfg->dev->isMounted = 0;
1797 cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
1805 /* Directory search stuff. */
1808 * Directory search context
1810 * NB this is an opaque structure.
1817 yaffs_dirent de; /* directory entry being used by this dsc */
1818 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1819 yaffs_Object *dirObj; /* ptr to directory being searched */
1820 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1822 struct ylist_head others;
1823 } yaffsfs_DirectorySearchContext;
1827 static struct ylist_head search_contexts;
1830 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1834 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1838 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1839 dsc->nextReturn = NULL;
1841 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1842 yaffs_Object,siblings);
1844 /* Hey someone isn't playing nice! */
1848 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1852 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1854 if( dsc->nextReturn == NULL ||
1855 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1856 dsc->nextReturn = NULL;
1858 struct ylist_head *next = dsc->nextReturn->siblings.next;
1860 if( next == &dsc->dirObj->variant.directoryVariant.children)
1861 dsc->nextReturn = NULL; /* end of list */
1863 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1866 /* Hey someone isn't playing nice! */
1870 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1873 struct ylist_head *i;
1874 yaffsfs_DirectorySearchContext *dsc;
1876 /* if search contexts not initilised then skip */
1877 if(!search_contexts.next)
1880 /* Iterate through the directory search contexts.
1881 * If any are the one being removed, then advance the dsc to
1882 * the next one to prevent a hanging ptr.
1884 ylist_for_each(i, &search_contexts) {
1886 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1887 if(dsc->nextReturn == obj)
1888 yaffsfs_DirAdvance(dsc);
1894 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1896 yaffs_DIR *dir = NULL;
1897 yaffs_Object *obj = NULL;
1898 yaffsfs_DirectorySearchContext *dsc = NULL;
1902 obj = yaffsfs_FindObject(NULL,dirname,0);
1904 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1906 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1907 dir = (yaffs_DIR *)dsc;
1910 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1911 dsc->magic = YAFFS_MAGIC;
1913 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1914 YINIT_LIST_HEAD(&dsc->others);
1916 if(!search_contexts.next)
1917 YINIT_LIST_HEAD(&search_contexts);
1919 ylist_add(&dsc->others,&search_contexts);
1920 yaffsfs_SetDirRewound(dsc);
1930 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1932 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1933 struct yaffs_dirent *retVal = NULL;
1937 if(dsc && dsc->magic == YAFFS_MAGIC){
1938 yaffsfs_SetError(0);
1939 if(dsc->nextReturn){
1940 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1941 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1942 dsc->de.d_off = dsc->offset++;
1943 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1944 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1946 /* this should not happen! */
1947 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1949 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1951 yaffsfs_DirAdvance(dsc);
1955 yaffsfs_SetError(-EBADF);
1964 void yaffs_rewinddir(yaffs_DIR *dirp)
1966 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1970 yaffsfs_SetDirRewound(dsc);
1976 int yaffs_closedir(yaffs_DIR *dirp)
1978 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1982 ylist_del(&dsc->others); /* unhook from list */
1988 /* End of directory stuff */
1991 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1993 yaffs_Object *parent = NULL;
1997 int mode = 0; /* ignore for now */
2000 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2001 if(parent && parent->myDev->readOnly)
2002 yaffsfs_SetError(-EINVAL);
2004 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
2008 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2012 yaffsfs_SetError(-EINVAL);
2022 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2024 yaffs_Object *obj = NULL;
2030 obj = yaffsfs_FindObject(NULL,path,0);
2033 yaffsfs_SetError(-ENOENT);
2035 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
2036 yaffsfs_SetError(-EINVAL);
2039 YCHAR *alias = obj->variant.symLinkVariant.alias;
2040 memset(buf,0,bufsiz);
2041 yaffs_strncpy(buf,alias,bufsiz - 1);
2048 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2050 /* Creates a link called newpath to existing oldpath */
2051 yaffs_Object *obj = NULL;
2052 yaffs_Object *target = NULL;
2054 int newNameLength = 0;
2059 obj = yaffsfs_FindObject(NULL,oldpath,0);
2060 target = yaffsfs_FindObject(NULL,newpath,0);
2063 yaffsfs_SetError(-ENOENT);
2065 } else if(obj->myDev->readOnly){
2066 yaffsfs_SetError(-EINVAL);
2069 yaffsfs_SetError(-EEXIST);
2072 yaffs_Object *newdir = NULL;
2073 yaffs_Object *link = NULL;
2077 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2080 yaffsfs_SetError(-ENOTDIR);
2082 }else if(newdir->myDev != obj->myDev){
2083 yaffsfs_SetError(-EXDEV);
2087 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2089 if(newNameLength == 0){
2090 yaffsfs_SetError(-ENOENT);
2092 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
2093 yaffsfs_SetError(-ENAMETOOLONG);
2098 link = yaffs_Link(newdir,newname,obj);
2102 yaffsfs_SetError(-ENOSPC);
2113 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2118 int yaffs_DumpDevStruct(const YCHAR *path)
2123 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
2126 yaffs_Device *dev = obj->myDev;
2129 "nPageWrites.......... %d\n"
2130 "nPageReads........... %d\n"
2131 "nBlockErasures....... %d\n"
2132 "nGCCopies............ %d\n"
2133 "garbageCollections... %d\n"
2134 "passiveGarbageColl'ns %d\n"
2138 dev->nBlockErasures,
2140 dev->garbageCollections,
2141 dev->passiveGarbageCollections