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);
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);
584 if(dir && dir->myDev->readOnly){
585 yaffsfs_SetError(-EINVAL);
588 obj = yaffs_MknodFile(dir,name,mode,0,0);
590 yaffsfs_SetError(-ENOTDIR);
595 if(obj && !openDenied) {
596 int inodeId = yaffsfs_GetInodeIdForObject(obj);
600 * Todo: Fix any problem if inodes run out, though that
601 * can't happen if the number of inode items >= number of handles.
605 h->inodeId = inodeId;
606 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
607 h->append = (oflag & O_APPEND) ? 1 : 0;
608 h->exclusive = (oflag & O_EXCL) ? 1 : 0;
611 /* Hook inode to object */
612 obj->myInode = (void*) &yaffsfs_inode[inodeId];
614 if((oflag & O_TRUNC) && !h->readOnly)
615 yaffs_ResizeFile(obj,0);
617 yaffsfs_PutHandle(handle);
619 yaffsfs_SetError(-EACCES);
631 int yaffs_Dofsync(int fd,int datasync)
633 yaffsfs_Handle *h = NULL;
638 h = yaffsfs_GetHandlePointer(fd);
640 if(h && h->useCount > 0)
642 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
645 yaffsfs_SetError(-EBADF);
654 int yaffs_fsync(int fd)
656 return yaffs_Dofsync(fd,0);
659 int yaffs_flush(int fd)
661 return yaffs_fsync(fd);
664 int yaffs_fdatasync(int fd)
666 return yaffs_Dofsync(fd,1);
669 int yaffs_close(int fd)
671 yaffsfs_Handle *h = NULL;
676 h = yaffsfs_GetHandlePointer(fd);
678 if(h && h->useCount > 0) {
680 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
681 yaffsfs_PutHandle(fd);
685 yaffsfs_SetError(-EBADF);
696 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
698 yaffsfs_Handle *h = NULL;
699 yaffs_Object *obj = NULL;
705 unsigned int maxRead;
706 __u8 *buf = (__u8 *)vbuf;
709 h = yaffsfs_GetHandlePointer(fd);
710 obj = yaffsfs_GetHandleObject(fd);
714 yaffsfs_SetError(-EBADF);
716 } else if( h && obj){
720 startPos = h->position;
724 if(yaffs_GetObjectFileLength(obj) > pos)
725 maxRead = yaffs_GetObjectFileLength(obj) - pos;
733 yaffsfs_GetHandle(fd);
736 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
739 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
750 nbyte = 0; /* no more to read */
760 yaffsfs_PutHandle(fd);
764 h->position = startPos + totalRead;
774 return (totalRead >= 0) ? totalRead : -1;
778 int yaffs_read(int fd, void *buf, unsigned int nbyte)
780 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
783 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
785 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
788 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
790 yaffsfs_Handle *h = NULL;
791 yaffs_Object *obj = NULL;
795 int totalWritten = 0;
796 int writeThrough = 0;
798 const __u8 *buf = (const __u8 *)vbuf;
801 h = yaffsfs_GetHandlePointer(fd);
802 obj = yaffsfs_GetHandleObject(fd);
806 yaffsfs_SetError(-EBADF);
808 } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
809 yaffsfs_SetError(-EINVAL);
811 } else if( h && obj){
815 startPos = yaffs_GetObjectFileLength(obj);
817 startPos = h->position;
819 yaffsfs_GetHandle(fd);
822 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
826 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
828 totalWritten += nWritten;
833 if(nWritten == nToWrite)
838 if(nWritten < 1 && totalWritten < 1){
839 yaffsfs_SetError(-ENOSPC);
849 yaffsfs_PutHandle(fd);
853 h->position = startPos + totalWritten;
862 return (totalWritten >= 0) ? totalWritten : -1;
865 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
867 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
870 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
872 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
876 int yaffs_truncate(const YCHAR *path,off_t newSize)
878 yaffs_Object *obj = NULL;
879 int result = YAFFS_FAIL;
883 obj = yaffsfs_FindObject(NULL,path,0);
885 obj = yaffs_GetEquivalentObject(obj);
888 yaffsfs_SetError(-ENOENT);
889 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
890 yaffsfs_SetError(-EISDIR);
891 else if(obj->myDev->readOnly)
892 yaffsfs_SetError(-EINVAL);
894 result = yaffs_ResizeFile(obj,newSize);
899 return (result) ? 0 : -1;
902 int yaffs_ftruncate(int fd, off_t newSize)
904 yaffsfs_Handle *h = NULL;
905 yaffs_Object *obj = NULL;
909 h = yaffsfs_GetHandlePointer(fd);
910 obj = yaffsfs_GetHandleObject(fd);
914 yaffsfs_SetError(-EBADF);
915 else if(obj->myDev->readOnly)
916 yaffsfs_SetError(-EINVAL);
919 result = yaffs_ResizeFile(obj,newSize);
923 return (result) ? 0 : -1;
927 off_t yaffs_lseek(int fd, off_t offset, int whence)
929 yaffsfs_Handle *h = NULL;
930 yaffs_Object *obj = NULL;
935 h = yaffsfs_GetHandlePointer(fd);
936 obj = yaffsfs_GetHandleObject(fd);
940 yaffsfs_SetError(-EBADF);
941 else if(whence == SEEK_SET){
945 else if(whence == SEEK_CUR) {
946 if( (h->position + offset) >= 0)
947 pos = (h->position + offset);
949 else if(whence == SEEK_END) {
950 fSize = yaffs_GetObjectFileLength(obj);
951 if(fSize >= 0 && (fSize + offset) >= 0)
952 pos = fSize + offset;
968 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
970 yaffs_Object *dir = NULL;
971 yaffs_Object *obj = NULL;
973 int result = YAFFS_FAIL;
977 obj = yaffsfs_FindObject(NULL,path,0);
978 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
980 yaffsfs_SetError(-ENOTDIR);
982 yaffsfs_SetError(-ENOENT);
983 else if(obj->myDev->readOnly)
984 yaffsfs_SetError(-EINVAL);
985 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
986 yaffsfs_SetError(-EISDIR);
987 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
988 yaffsfs_SetError(-ENOTDIR);
990 result = yaffs_Unlink(dir,name);
992 if(result == YAFFS_FAIL && isDirectory)
993 yaffsfs_SetError(-ENOTEMPTY);
1000 return (result == YAFFS_FAIL) ? -1 : 0;
1004 int yaffs_rmdir(const YCHAR *path)
1006 return yaffsfs_DoUnlink(path,1);
1009 int yaffs_unlink(const YCHAR *path)
1011 return yaffsfs_DoUnlink(path,0);
1014 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1016 yaffs_Object *olddir = NULL;
1017 yaffs_Object *newdir = NULL;
1018 yaffs_Object *obj = NULL;
1021 int result= YAFFS_FAIL;
1022 int renameAllowed = 1;
1026 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1027 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1028 obj = yaffsfs_FindObject(NULL,oldPath,0);
1030 if(!olddir || !newdir || !obj) {
1032 yaffsfs_SetError(-EBADF);
1034 } else if(obj->myDev->readOnly){
1035 yaffsfs_SetError(-EINVAL);
1037 } else if(olddir->myDev != newdir->myDev) {
1038 // oops must be on same device
1040 yaffsfs_SetError(-EXDEV);
1042 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
1043 // It is a directory, check that it is not being renamed to
1044 // being its own decendent.
1045 // Do this by tracing from the new directory back to the root, checking for obj
1047 yaffs_Object *xx = newdir;
1049 while( renameAllowed && xx){
1055 yaffsfs_SetError(-EACCES);
1059 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1063 return (result == YAFFS_FAIL) ? -1 : 0;
1067 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1072 obj = yaffs_GetEquivalentObject(obj);
1075 buf->st_dev = (int)obj->myDev->context;
1076 buf->st_ino = obj->objectId;
1077 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1079 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1080 buf->st_mode |= S_IFDIR;
1081 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1082 buf->st_mode |= S_IFLNK;
1083 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1084 buf->st_mode |= S_IFREG;
1086 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1089 buf->st_rdev = obj->yst_rdev;
1090 buf->st_size = yaffs_GetObjectFileLength(obj);
1091 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1092 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1093 #if CONFIG_YAFFS_WINCE
1094 buf->yst_wince_atime[0] = obj->win_atime[0];
1095 buf->yst_wince_atime[1] = obj->win_atime[1];
1096 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1097 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1098 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1099 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1101 buf->yst_atime = obj->yst_atime;
1102 buf->yst_ctime = obj->yst_ctime;
1103 buf->yst_mtime = obj->yst_mtime;
1110 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1117 obj = yaffsfs_FindObject(NULL,path,0);
1120 obj = yaffsfs_FollowLink(obj,0);
1123 retVal = yaffsfs_DoStat(obj,buf);
1125 // todo error not found
1126 yaffsfs_SetError(-ENOENT);
1134 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1136 return yaffsfs_DoStatOrLStat(path,buf,0);
1139 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1141 return yaffsfs_DoStatOrLStat(path,buf,1);
1144 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1151 obj = yaffsfs_GetHandleObject(fd);
1154 retVal = yaffsfs_DoStat(obj,buf);
1157 yaffsfs_SetError(-EBADF);
1164 #ifdef CONFIG_YAFFS_WINCE
1165 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1172 obj = yaffsfs_GetHandleObject(fd);
1177 wctime[0] = obj->win_ctime[0];
1178 wctime[1] = obj->win_ctime[1];
1181 watime[0] = obj->win_atime[0];
1182 watime[1] = obj->win_atime[1];
1185 wmtime[0] = obj->win_mtime[0];
1186 wmtime[1] = obj->win_mtime[1];
1193 yaffsfs_SetError(-EBADF);
1201 int yaffs_set_wince_times(int fd,
1202 const unsigned *wctime,
1203 const unsigned *watime,
1204 const unsigned *wmtime)
1211 obj = yaffsfs_GetHandleObject(fd);
1216 obj->win_ctime[0] = wctime[0];
1217 obj->win_ctime[1] = wctime[1];
1220 obj->win_atime[0] = watime[0];
1221 obj->win_atime[1] = watime[1];
1224 obj->win_mtime[0] = wmtime[0];
1225 obj->win_mtime[1] = wmtime[1];
1229 result = yaffs_FlushFile(obj,0,0);
1233 yaffsfs_SetError(-EBADF);
1243 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1248 obj = yaffs_GetEquivalentObject(obj);
1251 obj->yst_mode = mode;
1253 result = yaffs_FlushFile(obj,0,0);
1256 return result == YAFFS_OK ? 0 : -1;
1260 int yaffs_access(const YCHAR *path, int amode)
1267 obj = yaffsfs_FindObject(NULL,path,0);
1272 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1274 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1276 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1280 yaffsfs_SetError(-EACCES);
1284 // todo error not found
1285 yaffsfs_SetError(-ENOENT);
1296 int yaffs_chmod(const YCHAR *path, mode_t mode)
1303 obj = yaffsfs_FindObject(NULL,path,0);
1306 yaffsfs_SetError(-ENOENT);
1307 else if(obj->myDev->readOnly)
1308 yaffsfs_SetError(-EINVAL);
1310 retVal = yaffsfs_DoChMod(obj,mode);
1319 int yaffs_fchmod(int fd, mode_t mode)
1326 obj = yaffsfs_GetHandleObject(fd);
1329 yaffsfs_SetError(-ENOENT);
1330 else if(obj->myDev->readOnly)
1331 yaffsfs_SetError(-EINVAL);
1333 retVal = yaffsfs_DoChMod(obj,mode);
1341 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1343 yaffs_Object *parent = NULL;
1344 yaffs_Object *dir = NULL;
1349 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1350 if(parent && parent->myDev->readOnly){
1351 yaffsfs_SetError(-EINVAL);
1354 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1359 yaffsfs_SetError(-ENOENT); // missing path
1360 else if (yaffs_FindObjectByName(parent,name))
1361 yaffsfs_SetError(-EEXIST); // the name already exists
1363 yaffsfs_SetError(-ENOSPC); // just assume no space
1373 int yaffs_mount2(const YCHAR *path,int readOnly)
1376 int result=YAFFS_FAIL;
1377 yaffs_Device *dev=NULL;
1380 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1383 dev = yaffsfs_FindDevice(path,&dummy);
1385 if(!dev->isMounted){
1386 dev->readOnly = readOnly ? 1 : 0;
1387 result = yaffs_GutsInitialise(dev);
1388 if(result == YAFFS_FAIL)
1389 // todo error - mount failed
1390 yaffsfs_SetError(-ENOMEM);
1391 retVal = result ? 0 : -1;
1395 //todo error - already mounted.
1396 yaffsfs_SetError(-EBUSY);
1398 // todo error - no device
1399 yaffsfs_SetError(-ENODEV);
1406 int yaffs_mount(const YCHAR *path)
1408 return yaffs_mount2(path,0);
1411 int yaffs_sync(const YCHAR *path)
1414 yaffs_Device *dev=NULL;
1418 dev = yaffsfs_FindDevice(path,&dummy);
1422 yaffs_FlushEntireDeviceCache(dev);
1423 yaffs_CheckpointSave(dev);
1427 //todo error - not mounted.
1428 yaffsfs_SetError(-EINVAL);
1431 // todo error - no device
1432 yaffsfs_SetError(-ENODEV);
1439 int yaffs_remount(const YCHAR *path, int force, int readOnly)
1442 yaffs_Device *dev=NULL;
1446 dev = yaffsfs_FindDevice(path,&dummy);
1452 yaffs_FlushEntireDeviceCache(dev);
1454 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1455 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1456 inUse = 1; // the device is in use, can't unmount
1459 if(!inUse || force){
1461 yaffs_CheckpointSave(dev);
1462 dev->readOnly = readOnly ? 1 : 0;
1465 yaffsfs_SetError(-EBUSY);
1468 yaffsfs_SetError(-EINVAL);
1472 yaffsfs_SetError(-ENODEV);
1479 int yaffs_unmount2(const YCHAR *path, int force)
1482 yaffs_Device *dev=NULL;
1486 dev = yaffsfs_FindDevice(path,&dummy);
1492 yaffs_FlushEntireDeviceCache(dev);
1493 yaffs_CheckpointSave(dev);
1495 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1496 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1497 inUse = 1; // the device is in use, can't unmount
1500 if(!inUse || force){
1501 yaffs_Deinitialise(dev);
1505 // todo error can't unmount as files are open
1506 yaffsfs_SetError(-EBUSY);
1509 //todo error - not mounted.
1510 yaffsfs_SetError(-EINVAL);
1514 // todo error - no device
1515 yaffsfs_SetError(-ENODEV);
1522 int yaffs_unmount(const YCHAR *path)
1524 return yaffs_unmount2(path,0);
1527 loff_t yaffs_freespace(const YCHAR *path)
1530 yaffs_Device *dev=NULL;
1534 dev = yaffsfs_FindDevice(path,&dummy);
1535 if(dev && dev->isMounted){
1536 retVal = yaffs_GetNumberOfFreeChunks(dev);
1537 retVal *= dev->nDataBytesPerChunk;
1540 yaffsfs_SetError(-EINVAL);
1546 loff_t yaffs_totalspace(const YCHAR *path)
1549 yaffs_Device *dev=NULL;
1553 dev = yaffsfs_FindDevice(path,&dummy);
1554 if(dev && dev->isMounted){
1555 retVal = (dev->param.endBlock - dev->param.startBlock + 1) - dev->param.nReservedBlocks;
1556 retVal *= dev->param.nChunksPerBlock;
1557 retVal *= dev->nDataBytesPerChunk;
1560 yaffsfs_SetError(-EINVAL);
1566 int yaffs_inodecount(const YCHAR *path)
1569 yaffs_Device *dev=NULL;
1573 dev = yaffsfs_FindDevice(path,&dummy);
1574 if(dev && dev->isMounted) {
1575 int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1576 if(nObjects > dev->nHardLinks)
1577 retVal = nObjects - dev->nHardLinks;
1581 yaffsfs_SetError(-EINVAL);
1589 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1592 yaffsfs_DeviceConfiguration *cfg;
1594 yaffsfs_configurationList = cfgList;
1596 yaffsfs_InitHandles();
1598 cfg = yaffsfs_configurationList;
1600 while(cfg && cfg->prefix && cfg->dev){
1601 cfg->dev->isMounted = 0;
1602 cfg->dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
1611 // Directory search stuff.
1614 // Directory search context
1616 // NB this is an opaque structure.
1622 yaffs_dirent de; /* directory entry being used by this dsc */
1623 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1624 yaffs_Object *dirObj; /* ptr to directory being searched */
1625 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1627 struct ylist_head others;
1628 } yaffsfs_DirectorySearchContext;
1632 static struct ylist_head search_contexts;
1635 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1639 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1643 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1644 dsc->nextReturn = NULL;
1646 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1647 yaffs_Object,siblings);
1649 /* Hey someone isn't playing nice! */
1653 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1657 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1659 if( dsc->nextReturn == NULL ||
1660 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1661 dsc->nextReturn = NULL;
1663 struct ylist_head *next = dsc->nextReturn->siblings.next;
1665 if( next == &dsc->dirObj->variant.directoryVariant.children)
1666 dsc->nextReturn = NULL; /* end of list */
1668 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1671 /* Hey someone isn't playing nice! */
1675 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1678 struct ylist_head *i;
1679 yaffsfs_DirectorySearchContext *dsc;
1681 /* if search contexts not initilised then skip */
1682 if(!search_contexts.next)
1685 /* Iterate through the directory search contexts.
1686 * If any are the one being removed, then advance the dsc to
1687 * the next one to prevent a hanging ptr.
1689 ylist_for_each(i, &search_contexts) {
1691 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1692 if(dsc->nextReturn == obj)
1693 yaffsfs_DirAdvance(dsc);
1699 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1701 yaffs_DIR *dir = NULL;
1702 yaffs_Object *obj = NULL;
1703 yaffsfs_DirectorySearchContext *dsc = NULL;
1707 obj = yaffsfs_FindObject(NULL,dirname,0);
1709 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1711 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1712 dir = (yaffs_DIR *)dsc;
1715 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1716 dsc->magic = YAFFS_MAGIC;
1718 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1719 YINIT_LIST_HEAD(&dsc->others);
1721 if(!search_contexts.next)
1722 YINIT_LIST_HEAD(&search_contexts);
1724 ylist_add(&dsc->others,&search_contexts);
1725 yaffsfs_SetDirRewound(dsc);
1735 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1737 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1738 struct yaffs_dirent *retVal = NULL;
1742 if(dsc && dsc->magic == YAFFS_MAGIC){
1743 yaffsfs_SetError(0);
1744 if(dsc->nextReturn){
1745 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1746 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1747 dsc->de.d_off = dsc->offset++;
1748 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1749 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1751 // this should not happen!
1752 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1754 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1756 yaffsfs_DirAdvance(dsc);
1760 yaffsfs_SetError(-EBADF);
1769 void yaffs_rewinddir(yaffs_DIR *dirp)
1771 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1775 yaffsfs_SetDirRewound(dsc);
1781 int yaffs_closedir(yaffs_DIR *dirp)
1783 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1787 ylist_del(&dsc->others); /* unhook from list */
1793 // end of directory stuff
1796 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1798 yaffs_Object *parent = NULL;
1802 int mode = 0; // ignore for now
1805 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1806 if(parent && parent->myDev->readOnly)
1807 yaffsfs_SetError(-EINVAL);
1809 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1813 yaffsfs_SetError(-ENOSPC); // just assume no space for now
1817 yaffsfs_SetError(-EINVAL);
1827 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1829 yaffs_Object *obj = NULL;
1835 obj = yaffsfs_FindObject(NULL,path,0);
1838 yaffsfs_SetError(-ENOENT);
1840 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1841 yaffsfs_SetError(-EINVAL);
1844 YCHAR *alias = obj->variant.symLinkVariant.alias;
1845 memset(buf,0,bufsiz);
1846 yaffs_strncpy(buf,alias,bufsiz - 1);
1853 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1855 // Creates a link called newpath to existing oldpath
1856 yaffs_Object *obj = NULL;
1857 yaffs_Object *target = NULL;
1859 int newNameLength = 0;
1864 obj = yaffsfs_FindObject(NULL,oldpath,0);
1865 target = yaffsfs_FindObject(NULL,newpath,0);
1868 yaffsfs_SetError(-ENOENT);
1870 } else if(obj->myDev->readOnly){
1871 yaffsfs_SetError(-EINVAL);
1874 yaffsfs_SetError(-EEXIST);
1877 yaffs_Object *newdir = NULL;
1878 yaffs_Object *link = NULL;
1882 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1885 yaffsfs_SetError(-ENOTDIR);
1887 }else if(newdir->myDev != obj->myDev){
1888 yaffsfs_SetError(-EXDEV);
1892 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1894 if(newNameLength == 0){
1895 yaffsfs_SetError(-ENOENT);
1897 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1898 yaffsfs_SetError(-ENAMETOOLONG);
1903 link = yaffs_Link(newdir,newname,obj);
1907 yaffsfs_SetError(-ENOSPC);
1918 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1923 int yaffs_DumpDevStruct(const YCHAR *path)
1928 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1931 yaffs_Device *dev = obj->myDev;
1934 "nPageWrites.......... %d\n"
1935 "nPageReads........... %d\n"
1936 "nBlockErasures....... %d\n"
1937 "nGCCopies............ %d\n"
1938 "garbageCollections... %d\n"
1939 "passiveGarbageColl'ns %d\n"
1943 dev->nBlockErasures,
1945 dev->garbageCollections,
1946 dev->passiveGarbageCollections