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)
33 /* Some forward references */
34 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
35 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
37 unsigned int yaffs_wr_attempts;
41 * There are open inodes in yaffsfs_Inode.
42 * There are open handles in yaffsfs_Handle.
44 * Things are structured this way to be like the Linux VFS model
45 * so that interactions with the yaffs guts calls are similar.
46 * That means more common code paths and less special code.
47 * That means better testing etc.
51 int count; /* Number of handles accessing this inode */
58 int inodeId:13; /* Index to corresponding yaffsfs_Inode */
59 int useCount:16; /* Use count for this handle */
60 __u32 position; /* current position in file */
63 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
64 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
65 static int yaffsfs_handlesInitialised;
69 * Inilitalise handle management on start-up.
72 static void yaffsfs_InitHandles(void)
75 if(yaffsfs_handlesInitialised)
78 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
79 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
80 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
81 yaffsfs_handle[i].inodeId = -1;
84 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
86 if(h < 0 || h >= YAFFSFS_N_HANDLES)
89 return &yaffsfs_handle[h];
92 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
94 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
96 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
97 return &yaffsfs_inode[h->inodeId];
102 yaffs_Object *yaffsfs_GetHandleObject(int handle)
104 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
113 * yaffsfs_GetInodeIdForObject
114 * Grab an inode entry when opening a new inode.
117 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
121 yaffsfs_Inode *in = NULL;
124 obj = yaffs_GetEquivalentObject(obj);
126 /* Look for it. If we can't find it then make one */
127 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
128 if(yaffsfs_inode[i].iObj == obj)
132 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
133 if(!yaffsfs_inode[i].iObj)
139 in = &yaffsfs_inode[ret];
150 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
157 yaffs_DeleteObject(obj);
164 static void yaffsfs_PutInode(int inodeId)
166 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
167 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
170 yaffsfs_ReleaseInode(in);
177 * Grab a handle (when opening a file)
180 static int yaffsfs_GetNewHandle(void)
185 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
186 h = yaffsfs_GetHandlePointer(i);
188 /* todo bug: should never happen */
191 memset(h,0,sizeof(yaffsfs_Handle));
202 * Increase use of handle when reading/writing a file
204 static int yaffsfs_GetHandle(int handle)
206 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
208 if(h && h->useCount > 0){
216 * Let go of a handle when closing a file or aborting an open or
217 * ending a read or write.
219 static int yaffsfs_PutHandle(int handle)
221 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
223 if(h && h->useCount > 0){
227 yaffsfs_PutInode(h->inodeId);
239 * Stuff to search for a directory from a path
243 int yaffsfs_Match(YCHAR a, YCHAR b)
249 int yaffsfs_IsPathDivider(YCHAR ch)
251 const YCHAR *str = YAFFS_PATH_DIVIDERS;
264 YLIST_HEAD(yaffsfs_deviceList);
269 * Scan the configuration list to find the root.
270 * Curveballs: Should match paths that end in '/' too
271 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
273 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
275 struct ylist_head *cfg;
276 const YCHAR *leftOver;
278 yaffs_Device *retval = NULL;
279 yaffs_Device *dev = 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 ylist_for_each(cfg, &yaffsfs_deviceList){
290 dev = ylist_entry(cfg, yaffs_Device, devList);
297 while(matching && *p && *leftOver){
298 /* Skip over any /s */
299 while(yaffsfs_IsPathDivider(*p))
302 /* Skip over any /s */
303 while(yaffsfs_IsPathDivider(*leftOver))
306 /* Now match the text part */
308 *p && !yaffsfs_IsPathDivider(*p) &&
309 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
310 if(yaffsfs_Match(*p,*leftOver)){
320 /* Skip over any /s in leftOver */
321 while(yaffsfs_IsPathDivider(*leftOver))
324 // Skip over any /s in p
325 while(yaffsfs_IsPathDivider(*p))
328 // p should now be at the end of the string (ie. fully matched)
332 if( matching && (thisMatchLength > longestMatch))
335 *restOfPath = (YCHAR *)leftOver;
337 longestMatch = thisMatchLength;
345 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
347 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
348 const YCHAR *leftOver;
350 yaffs_Device *retval = NULL;
352 int longestMatch = -1;
355 * Check all configs, choose the one that:
356 * 1) Actually matches a prefix (ie /a amd /abc will not match
357 * 2) Matches the longest.
359 while(cfg && cfg->prefix && cfg->dev){
364 while(*p && /* unmatched part of prefix */
365 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) &&
366 *leftOver && yaffsfs_Match(*p,*leftOver)){
373 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && /* end of prefix */
374 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && /* no more in this path name part */
375 (thisMatchLength > longestMatch)){
377 *restOfPath = (YCHAR *)leftOver;
379 longestMatch = thisMatchLength;
387 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
392 dev= yaffsfs_FindDevice(path,restOfPath);
393 if(dev && dev->isMounted){
399 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
402 while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
403 YCHAR *alias = obj->variant.symLinkVariant.alias;
405 if(yaffsfs_IsPathDivider(*alias))
406 /* Starts with a /, need to scan from root up */
407 obj = yaffsfs_FindObject(NULL,alias,symDepth++);
409 /* Relative to here, so use the parent of the symlink as a start */
410 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
417 * yaffsfs_FindDirectory
418 * Parse a path to determine the directory and the name within the directory.
420 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
422 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,
423 const YCHAR *path, YCHAR **name, int symDepth)
427 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
430 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
435 restOfPath = (YCHAR *)path;
438 dir = yaffsfs_FindRoot(path,&restOfPath);
443 * curve ball: also throw away surplus '/'
444 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
446 while(yaffsfs_IsPathDivider(*restOfPath))
447 restOfPath++; /* get rid of '/' */
452 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
453 if (i < YAFFS_MAX_NAME_LENGTH){
454 str[i] = *restOfPath;
462 /* got to the end of the string */
465 if(yaffs_strcmp(str,_Y(".")) == 0)
469 else if(yaffs_strcmp(str,_Y("..")) == 0)
472 dir = yaffs_FindObjectByName(dir,str);
474 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
475 dir = yaffsfs_FollowLink(dir,symDepth);
478 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
483 /* directory did not exist. */
487 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,
488 const YCHAR *path,YCHAR **name,int symDepth)
490 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
494 * yaffsfs_FindObject turns a path for an existing object into the object
496 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
501 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
504 return yaffs_FindObjectByName(dir,name);
510 int yaffs_dup(int fd)
513 yaffsfs_Handle *oldPtr = NULL;
514 yaffsfs_Handle *newPtr = NULL;
518 oldPtr = yaffsfs_GetHandlePointer(fd);
519 if(oldPtr && oldPtr->useCount > 0)
520 newHandle = yaffsfs_GetNewHandle();
522 newPtr = yaffsfs_GetHandlePointer(newHandle);
530 yaffsfs_SetError(-EBADF);
532 yaffsfs_SetError(-ENOMEM);
538 int yaffs_open(const YCHAR *path, int oflag, int mode)
540 yaffs_Object *obj = NULL;
541 yaffs_Object *dir = NULL;
544 yaffsfs_Handle *h = NULL;
547 int errorReported = 0;
549 /* O_EXCL only has meaning if O_CREAT is specified */
550 if(!(oflag & O_CREAT))
553 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
554 if( (oflag & O_CREAT) & (oflag & O_EXCL))
557 /* Todo: Are there any more flag combos to sanitise ? */
562 handle = yaffsfs_GetNewHandle();
566 h = yaffsfs_GetHandlePointer(handle);
569 /* try to find the exisiting object */
570 obj = yaffsfs_FindObject(NULL,path,0);
572 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
573 obj = yaffsfs_FollowLink(obj,symDepth++);
576 obj = yaffs_GetEquivalentObject(obj);
579 obj->variantType != YAFFS_OBJECT_TYPE_FILE &&
580 obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
585 /* The file already exists or it might be a directory */
587 /* If it is a directory then we can't open it as a file */
588 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
590 yaffsfs_SetError(-EISDIR);
594 /* Open should fail if O_CREAT and O_EXCL are specified since
597 if((oflag & O_EXCL) && (oflag & O_CREAT)){
599 yaffsfs_SetError(-EEXIST);
603 /* Check file permissions */
604 if( (oflag & (O_RDWR | O_WRONLY)) == 0 && /* ie O_RDONLY */
605 !(obj->yst_mode & S_IREAD))
608 if( (oflag & O_RDWR) &&
609 !(obj->yst_mode & S_IREAD))
612 if( (oflag & (O_RDWR | O_WRONLY)) &&
613 !(obj->yst_mode & S_IWRITE))
616 } else if((oflag & O_CREAT)) {
617 /* Let's see if we can create this file */
618 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
619 if(dir && dir->myDev->readOnly){
620 yaffsfs_SetError(-EINVAL);
623 obj = yaffs_MknodFile(dir,name,mode,0,0);
625 yaffsfs_SetError(-ENOTDIR);
630 if(obj && !openDenied) {
631 int inodeId = yaffsfs_GetInodeIdForObject(obj);
635 * Todo: Fix any problem if inodes run out, though that
636 * can't happen if the number of inode items >= number of handles.
640 h->inodeId = inodeId;
641 h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
642 h->append = (oflag & O_APPEND) ? 1 : 0;
645 /* Hook inode to object */
646 obj->myInode = (void*) &yaffsfs_inode[inodeId];
648 if((oflag & O_TRUNC) && !h->readOnly)
649 yaffs_ResizeFile(obj,0);
651 yaffsfs_PutHandle(handle);
653 yaffsfs_SetError(-EACCES);
665 int yaffs_Dofsync(int fd,int datasync)
667 yaffsfs_Handle *h = NULL;
672 h = yaffsfs_GetHandlePointer(fd);
674 if(h && h->useCount > 0)
676 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
679 yaffsfs_SetError(-EBADF);
688 int yaffs_fsync(int fd)
690 return yaffs_Dofsync(fd,0);
693 int yaffs_flush(int fd)
695 return yaffs_fsync(fd);
698 int yaffs_fdatasync(int fd)
700 return yaffs_Dofsync(fd,1);
703 int yaffs_close(int fd)
705 yaffsfs_Handle *h = NULL;
710 h = yaffsfs_GetHandlePointer(fd);
712 if(h && h->useCount > 0) {
714 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
715 yaffsfs_PutHandle(fd);
719 yaffsfs_SetError(-EBADF);
730 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
732 yaffsfs_Handle *h = NULL;
733 yaffs_Object *obj = NULL;
739 unsigned int maxRead;
740 __u8 *buf = (__u8 *)vbuf;
743 h = yaffsfs_GetHandlePointer(fd);
744 obj = yaffsfs_GetHandleObject(fd);
748 yaffsfs_SetError(-EBADF);
750 } else if( h && obj){
754 startPos = h->position;
758 if(yaffs_GetObjectFileLength(obj) > pos)
759 maxRead = yaffs_GetObjectFileLength(obj) - pos;
767 yaffsfs_GetHandle(fd);
770 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
773 nRead = yaffs_ReadDataFromFile(obj,buf,pos,nToRead);
784 nbyte = 0; /* no more to read */
794 yaffsfs_PutHandle(fd);
798 h->position = startPos + totalRead;
808 return (totalRead >= 0) ? totalRead : -1;
812 int yaffs_read(int fd, void *buf, unsigned int nbyte)
814 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
817 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
819 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
822 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
824 yaffsfs_Handle *h = NULL;
825 yaffs_Object *obj = NULL;
829 int totalWritten = 0;
830 int writeThrough = 0;
832 const __u8 *buf = (const __u8 *)vbuf;
835 h = yaffsfs_GetHandlePointer(fd);
836 obj = yaffsfs_GetHandleObject(fd);
840 yaffsfs_SetError(-EBADF);
842 } else if( h && obj && (h->readOnly || obj->myDev->readOnly)){
843 yaffsfs_SetError(-EINVAL);
845 } else if( h && obj){
847 startPos = yaffs_GetObjectFileLength(obj);
851 startPos = h->position;
853 yaffsfs_GetHandle(fd);
856 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
860 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nToWrite,writeThrough);
862 totalWritten += nWritten;
867 if(nWritten == nToWrite)
872 if(nWritten < 1 && totalWritten < 1){
873 yaffsfs_SetError(-ENOSPC);
883 yaffsfs_PutHandle(fd);
887 h->position = startPos + totalWritten;
896 return (totalWritten >= 0) ? totalWritten : -1;
899 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
901 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
904 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
906 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
910 int yaffs_truncate(const YCHAR *path,off_t newSize)
912 yaffs_Object *obj = NULL;
913 int result = YAFFS_FAIL;
917 obj = yaffsfs_FindObject(NULL,path,0);
919 obj = yaffs_GetEquivalentObject(obj);
922 yaffsfs_SetError(-ENOENT);
923 else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
924 yaffsfs_SetError(-EISDIR);
925 else if(obj->myDev->readOnly)
926 yaffsfs_SetError(-EINVAL);
928 result = yaffs_ResizeFile(obj,newSize);
933 return (result) ? 0 : -1;
936 int yaffs_ftruncate(int fd, off_t newSize)
938 yaffsfs_Handle *h = NULL;
939 yaffs_Object *obj = NULL;
943 h = yaffsfs_GetHandlePointer(fd);
944 obj = yaffsfs_GetHandleObject(fd);
948 yaffsfs_SetError(-EBADF);
949 else if(obj->myDev->readOnly)
950 yaffsfs_SetError(-EINVAL);
952 /* resize the file */
953 result = yaffs_ResizeFile(obj,newSize);
957 return (result) ? 0 : -1;
961 off_t yaffs_lseek(int fd, off_t offset, int whence)
963 yaffsfs_Handle *h = NULL;
964 yaffs_Object *obj = NULL;
969 h = yaffsfs_GetHandlePointer(fd);
970 obj = yaffsfs_GetHandleObject(fd);
974 yaffsfs_SetError(-EBADF);
975 else if(whence == SEEK_SET){
979 else if(whence == SEEK_CUR) {
980 if( (h->position + offset) >= 0)
981 pos = (h->position + offset);
983 else if(whence == SEEK_END) {
984 fSize = yaffs_GetObjectFileLength(obj);
985 if(fSize >= 0 && (fSize + offset) >= 0)
986 pos = fSize + offset;
1002 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1004 yaffs_Object *dir = NULL;
1005 yaffs_Object *obj = NULL;
1007 int result = YAFFS_FAIL;
1011 obj = yaffsfs_FindObject(NULL,path,0);
1012 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1014 yaffsfs_SetError(-ENOTDIR);
1016 yaffsfs_SetError(-ENOENT);
1017 else if(obj->myDev->readOnly)
1018 yaffsfs_SetError(-EINVAL);
1019 else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1020 yaffsfs_SetError(-EISDIR);
1021 else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
1022 yaffsfs_SetError(-ENOTDIR);
1024 result = yaffs_Unlink(dir,name);
1026 if(result == YAFFS_FAIL && isDirectory)
1027 yaffsfs_SetError(-ENOTEMPTY);
1034 return (result == YAFFS_FAIL) ? -1 : 0;
1038 int yaffs_rmdir(const YCHAR *path)
1040 return yaffsfs_DoUnlink(path,1);
1043 int yaffs_unlink(const YCHAR *path)
1045 return yaffsfs_DoUnlink(path,0);
1048 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1050 yaffs_Object *olddir = NULL;
1051 yaffs_Object *newdir = NULL;
1052 yaffs_Object *obj = NULL;
1055 int result= YAFFS_FAIL;
1056 int renameAllowed = 1;
1060 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1061 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1062 obj = yaffsfs_FindObject(NULL,oldPath,0);
1064 if(!olddir || !newdir || !obj) {
1066 yaffsfs_SetError(-EBADF);
1068 } else if(obj->myDev->readOnly){
1069 yaffsfs_SetError(-EINVAL);
1071 } else if(olddir->myDev != newdir->myDev) {
1072 /* oops must be on same device */
1074 yaffsfs_SetError(-EXDEV);
1076 } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
1078 * It is a directory, check that it is not being renamed to
1079 * being its own decendent.
1080 * Do this by tracing from the new directory back to the root, checking for obj
1083 yaffs_Object *xx = newdir;
1085 while( renameAllowed && xx){
1091 yaffsfs_SetError(-EACCES);
1095 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1099 return (result == YAFFS_FAIL) ? -1 : 0;
1103 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1108 obj = yaffs_GetEquivalentObject(obj);
1111 buf->st_dev = (int)obj->myDev->osContext;
1112 buf->st_ino = obj->objectId;
1113 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1115 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1116 buf->st_mode |= S_IFDIR;
1117 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1118 buf->st_mode |= S_IFLNK;
1119 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1120 buf->st_mode |= S_IFREG;
1122 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1125 buf->st_rdev = obj->yst_rdev;
1126 buf->st_size = yaffs_GetObjectFileLength(obj);
1127 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1128 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1129 #if CONFIG_YAFFS_WINCE
1130 buf->yst_wince_atime[0] = obj->win_atime[0];
1131 buf->yst_wince_atime[1] = obj->win_atime[1];
1132 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1133 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1134 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1135 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1137 buf->yst_atime = obj->yst_atime;
1138 buf->yst_ctime = obj->yst_ctime;
1139 buf->yst_mtime = obj->yst_mtime;
1146 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1153 obj = yaffsfs_FindObject(NULL,path,0);
1156 obj = yaffsfs_FollowLink(obj,0);
1159 retVal = yaffsfs_DoStat(obj,buf);
1161 /* todo error not found */
1162 yaffsfs_SetError(-ENOENT);
1170 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1172 return yaffsfs_DoStatOrLStat(path,buf,0);
1175 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1177 return yaffsfs_DoStatOrLStat(path,buf,1);
1180 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1187 obj = yaffsfs_GetHandleObject(fd);
1190 retVal = yaffsfs_DoStat(obj,buf);
1193 yaffsfs_SetError(-EBADF);
1200 #ifndef CONFIG_YAFFS_WINCE
1201 /* xattrib functions */
1204 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1211 obj = yaffsfs_FindObject(NULL,path,0);
1214 obj = yaffsfs_FollowLink(obj,0);
1217 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1219 yaffsfs_SetError(retVal);
1223 /* todo error not found */
1224 yaffsfs_SetError(-ENOENT);
1232 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1234 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1237 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1239 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1244 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1251 obj = yaffsfs_GetHandleObject(fd);
1254 retVal = yaffs_SetXAttribute(obj,name,data,size,flags);
1256 yaffsfs_SetError(retVal);
1261 yaffsfs_SetError(-EBADF);
1268 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1275 obj = yaffsfs_FindObject(NULL,path,0);
1278 obj = yaffsfs_FollowLink(obj,0);
1281 retVal = yaffs_GetXAttribute(obj,name,data,size);
1283 yaffsfs_SetError(retVal);
1287 /* todo error not found */
1288 yaffsfs_SetError(-ENOENT);
1296 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1298 return yaffs_do_getxattr( path, name, data, size, 1);
1300 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1302 return yaffs_do_getxattr( path, name, data, size, 0);
1307 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1314 obj = yaffsfs_GetHandleObject(fd);
1317 retVal = yaffs_GetXAttribute(obj,name,data,size);
1319 yaffsfs_SetError(retVal);
1324 yaffsfs_SetError(-EBADF);
1331 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1338 obj = yaffsfs_FindObject(NULL,path,0);
1341 obj = yaffsfs_FollowLink(obj,0);
1344 retVal = yaffs_ListXAttributes(obj, data,size);
1346 yaffsfs_SetError(retVal);
1350 /* todo error not found */
1351 yaffsfs_SetError(-ENOENT);
1359 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1361 return yaffs_do_listxattr(path, data, size, 1);
1364 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1366 return yaffs_do_listxattr(path, data, size, 0);
1369 int yaffs_flistxattr(int fd, char *data, int size)
1376 obj = yaffsfs_GetHandleObject(fd);
1379 retVal = yaffs_ListXAttributes(obj,data,size);
1381 yaffsfs_SetError(retVal);
1386 yaffsfs_SetError(-EBADF);
1393 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1400 obj = yaffsfs_FindObject(NULL,path,0);
1403 obj = yaffsfs_FollowLink(obj,0);
1406 retVal = yaffs_RemoveXAttribute(obj,name);
1408 yaffsfs_SetError(retVal);
1412 /* todo error not found */
1413 yaffsfs_SetError(-ENOENT);
1421 int yaffs_removexattr(const YCHAR *path, const char *name)
1423 return yaffs_do_removexattr(path, name, 1);
1426 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1428 return yaffs_do_removexattr(path, name, 0);
1431 int yaffs_fremovexattr(int fd, const char *name)
1438 obj = yaffsfs_GetHandleObject(fd);
1441 retVal = yaffs_RemoveXAttribute(obj,name);
1443 yaffsfs_SetError(retVal);
1448 yaffsfs_SetError(-EBADF);
1456 #ifdef CONFIG_YAFFS_WINCE
1457 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1464 obj = yaffsfs_GetHandleObject(fd);
1469 wctime[0] = obj->win_ctime[0];
1470 wctime[1] = obj->win_ctime[1];
1473 watime[0] = obj->win_atime[0];
1474 watime[1] = obj->win_atime[1];
1477 wmtime[0] = obj->win_mtime[0];
1478 wmtime[1] = obj->win_mtime[1];
1485 yaffsfs_SetError(-EBADF);
1493 int yaffs_set_wince_times(int fd,
1494 const unsigned *wctime,
1495 const unsigned *watime,
1496 const unsigned *wmtime)
1503 obj = yaffsfs_GetHandleObject(fd);
1508 obj->win_ctime[0] = wctime[0];
1509 obj->win_ctime[1] = wctime[1];
1512 obj->win_atime[0] = watime[0];
1513 obj->win_atime[1] = watime[1];
1516 obj->win_mtime[0] = wmtime[0];
1517 obj->win_mtime[1] = wmtime[1];
1521 result = yaffs_FlushFile(obj,0,0);
1525 yaffsfs_SetError(-EBADF);
1535 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1540 obj = yaffs_GetEquivalentObject(obj);
1543 obj->yst_mode = mode;
1545 result = yaffs_FlushFile(obj,0,0);
1548 return result == YAFFS_OK ? 0 : -1;
1552 int yaffs_access(const YCHAR *path, int amode)
1559 obj = yaffsfs_FindObject(NULL,path,0);
1564 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1566 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1568 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1572 yaffsfs_SetError(-EACCES);
1576 /* todo error not found */
1577 yaffsfs_SetError(-ENOENT);
1588 int yaffs_chmod(const YCHAR *path, mode_t mode)
1595 obj = yaffsfs_FindObject(NULL,path,0);
1598 yaffsfs_SetError(-ENOENT);
1599 else if(obj->myDev->readOnly)
1600 yaffsfs_SetError(-EINVAL);
1602 retVal = yaffsfs_DoChMod(obj,mode);
1611 int yaffs_fchmod(int fd, mode_t mode)
1618 obj = yaffsfs_GetHandleObject(fd);
1621 yaffsfs_SetError(-ENOENT);
1622 else if(obj->myDev->readOnly)
1623 yaffsfs_SetError(-EINVAL);
1625 retVal = yaffsfs_DoChMod(obj,mode);
1633 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1635 yaffs_Object *parent = NULL;
1636 yaffs_Object *dir = NULL;
1641 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1642 if(parent && yaffs_strnlen(name,5) == 0){
1643 /* Trying to make the root itself */
1644 yaffsfs_SetError(-EEXIST);
1645 } else if(parent && parent->myDev->readOnly){
1646 yaffsfs_SetError(-EINVAL);
1649 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1654 yaffsfs_SetError(-ENOENT); /* missing path */
1655 else if (yaffs_FindObjectByName(parent,name))
1656 yaffsfs_SetError(-EEXIST); /* the name already exists */
1658 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1668 void * yaffs_getdev(const YCHAR *path)
1670 yaffs_Device *dev=NULL;
1672 dev = yaffsfs_FindDevice(path,&dummy);
1676 int yaffs_mount2(const YCHAR *path,int readOnly)
1679 int result=YAFFS_FAIL;
1680 yaffs_Device *dev=NULL;
1683 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1687 yaffsfs_InitHandles();
1689 dev = yaffsfs_FindDevice(path,&dummy);
1691 if(!dev->isMounted){
1692 dev->readOnly = readOnly ? 1 : 0;
1693 result = yaffs_GutsInitialise(dev);
1694 if(result == YAFFS_FAIL)
1695 /* todo error - mount failed */
1696 yaffsfs_SetError(-ENOMEM);
1697 retVal = result ? 0 : -1;
1701 /* todo error - already mounted. */
1702 yaffsfs_SetError(-EBUSY);
1704 /* todo error - no device */
1705 yaffsfs_SetError(-ENODEV);
1712 int yaffs_mount(const YCHAR *path)
1714 return yaffs_mount2(path,0);
1717 int yaffs_sync(const YCHAR *path)
1720 yaffs_Device *dev=NULL;
1724 dev = yaffsfs_FindDevice(path,&dummy);
1728 yaffs_FlushEntireDeviceCache(dev);
1729 yaffs_CheckpointSave(dev);
1733 /* todo error - not mounted. */
1734 yaffsfs_SetError(-EINVAL);
1737 /* todo error - no device */
1738 yaffsfs_SetError(-ENODEV);
1745 int yaffs_remount(const YCHAR *path, int force, int readOnly)
1748 yaffs_Device *dev=NULL;
1752 dev = yaffsfs_FindDevice(path,&dummy);
1758 yaffs_FlushEntireDeviceCache(dev);
1760 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1761 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1762 inUse = 1; /* the device is in use, can't unmount */
1765 if(!inUse || force){
1767 yaffs_CheckpointSave(dev);
1768 dev->readOnly = readOnly ? 1 : 0;
1771 yaffsfs_SetError(-EBUSY);
1774 yaffsfs_SetError(-EINVAL);
1778 yaffsfs_SetError(-ENODEV);
1785 int yaffs_unmount2(const YCHAR *path, int force)
1788 yaffs_Device *dev=NULL;
1792 dev = yaffsfs_FindDevice(path,&dummy);
1798 yaffs_FlushEntireDeviceCache(dev);
1799 yaffs_CheckpointSave(dev);
1801 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1802 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1803 inUse = 1; /* the device is in use, can't unmount */
1806 if(!inUse || force){
1807 yaffs_Deinitialise(dev);
1811 /* todo error can't unmount as files are open */
1812 yaffsfs_SetError(-EBUSY);
1815 /* todo error - not mounted. */
1816 yaffsfs_SetError(-EINVAL);
1820 /* todo error - no device */
1821 yaffsfs_SetError(-ENODEV);
1828 int yaffs_unmount(const YCHAR *path)
1830 return yaffs_unmount2(path,0);
1833 loff_t yaffs_freespace(const YCHAR *path)
1836 yaffs_Device *dev=NULL;
1840 dev = yaffsfs_FindDevice(path,&dummy);
1841 if(dev && dev->isMounted){
1842 retVal = yaffs_GetNumberOfFreeChunks(dev);
1843 retVal *= dev->nDataBytesPerChunk;
1846 yaffsfs_SetError(-EINVAL);
1852 loff_t yaffs_totalspace(const YCHAR *path)
1855 yaffs_Device *dev=NULL;
1859 dev = yaffsfs_FindDevice(path,&dummy);
1860 if(dev && dev->isMounted){
1861 retVal = (dev->param.endBlock - dev->param.startBlock + 1) - dev->param.nReservedBlocks;
1862 retVal *= dev->param.nChunksPerBlock;
1863 retVal *= dev->nDataBytesPerChunk;
1866 yaffsfs_SetError(-EINVAL);
1872 int yaffs_inodecount(const YCHAR *path)
1875 yaffs_Device *dev=NULL;
1879 dev = yaffsfs_FindDevice(path,&dummy);
1880 if(dev && dev->isMounted) {
1881 int nObjects = dev->nObjects;
1882 if(nObjects > dev->nHardLinks)
1883 retVal = nObjects - dev->nHardLinks;
1887 yaffsfs_SetError(-EINVAL);
1894 void yaffs_AddDevice(yaffs_Device *dev)
1897 dev->param.removeObjectCallback = yaffsfs_RemoveObjectCallback;
1899 if(!dev->devList.next)
1900 YINIT_LIST_HEAD(&dev->devList);
1902 ylist_add(&dev->devList,&yaffsfs_deviceList);
1905 void yaffs_RemoveDevice(yaffs_Device *dev)
1907 ylist_del_init(&dev->devList);
1913 /* Directory search stuff. */
1916 * Directory search context
1918 * NB this is an opaque structure.
1925 yaffs_dirent de; /* directory entry being used by this dsc */
1926 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
1927 yaffs_Object *dirObj; /* ptr to directory being searched */
1928 yaffs_Object *nextReturn; /* obj to be returned by next readddir */
1930 struct ylist_head others;
1931 } yaffsfs_DirectorySearchContext;
1935 static struct ylist_head search_contexts;
1938 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1942 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1946 if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1947 dsc->nextReturn = NULL;
1949 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1950 yaffs_Object,siblings);
1952 /* Hey someone isn't playing nice! */
1956 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1960 dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1962 if( dsc->nextReturn == NULL ||
1963 ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1964 dsc->nextReturn = NULL;
1966 struct ylist_head *next = dsc->nextReturn->siblings.next;
1968 if( next == &dsc->dirObj->variant.directoryVariant.children)
1969 dsc->nextReturn = NULL; /* end of list */
1971 dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1974 /* Hey someone isn't playing nice! */
1978 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1981 struct ylist_head *i;
1982 yaffsfs_DirectorySearchContext *dsc;
1984 /* if search contexts not initilised then skip */
1985 if(!search_contexts.next)
1988 /* Iterate through the directory search contexts.
1989 * If any are the one being removed, then advance the dsc to
1990 * the next one to prevent a hanging ptr.
1992 ylist_for_each(i, &search_contexts) {
1994 dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1995 if(dsc->nextReturn == obj)
1996 yaffsfs_DirAdvance(dsc);
2002 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2004 yaffs_DIR *dir = NULL;
2005 yaffs_Object *obj = NULL;
2006 yaffsfs_DirectorySearchContext *dsc = NULL;
2010 obj = yaffsfs_FindObject(NULL,dirname,0);
2012 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
2014 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2015 dir = (yaffs_DIR *)dsc;
2018 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2019 dsc->magic = YAFFS_MAGIC;
2021 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2022 YINIT_LIST_HEAD(&dsc->others);
2024 if(!search_contexts.next)
2025 YINIT_LIST_HEAD(&search_contexts);
2027 ylist_add(&dsc->others,&search_contexts);
2028 yaffsfs_SetDirRewound(dsc);
2038 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2040 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2041 struct yaffs_dirent *retVal = NULL;
2045 if(dsc && dsc->magic == YAFFS_MAGIC){
2046 yaffsfs_SetError(0);
2047 if(dsc->nextReturn){
2048 dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
2049 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2050 dsc->de.d_off = dsc->offset++;
2051 yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2052 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2054 /* this should not happen! */
2055 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2057 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2059 yaffsfs_DirAdvance(dsc);
2063 yaffsfs_SetError(-EBADF);
2072 void yaffs_rewinddir(yaffs_DIR *dirp)
2074 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2078 yaffsfs_SetDirRewound(dsc);
2084 int yaffs_closedir(yaffs_DIR *dirp)
2086 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2090 ylist_del(&dsc->others); /* unhook from list */
2096 /* End of directory stuff */
2099 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2101 yaffs_Object *parent = NULL;
2105 int mode = 0; /* ignore for now */
2108 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2109 if(parent && parent->myDev->readOnly)
2110 yaffsfs_SetError(-EINVAL);
2112 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
2116 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2120 yaffsfs_SetError(-EINVAL);
2130 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2132 yaffs_Object *obj = NULL;
2138 obj = yaffsfs_FindObject(NULL,path,0);
2141 yaffsfs_SetError(-ENOENT);
2143 } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
2144 yaffsfs_SetError(-EINVAL);
2147 YCHAR *alias = obj->variant.symLinkVariant.alias;
2148 memset(buf,0,bufsiz);
2149 yaffs_strncpy(buf,alias,bufsiz - 1);
2156 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2158 /* Creates a link called newpath to existing oldpath */
2159 yaffs_Object *obj = NULL;
2160 yaffs_Object *target = NULL;
2162 int newNameLength = 0;
2167 obj = yaffsfs_FindObject(NULL,oldpath,0);
2168 target = yaffsfs_FindObject(NULL,newpath,0);
2171 yaffsfs_SetError(-ENOENT);
2173 } else if(obj->myDev->readOnly){
2174 yaffsfs_SetError(-EINVAL);
2177 yaffsfs_SetError(-EEXIST);
2180 yaffs_Object *newdir = NULL;
2181 yaffs_Object *link = NULL;
2185 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2188 yaffsfs_SetError(-ENOTDIR);
2190 }else if(newdir->myDev != obj->myDev){
2191 yaffsfs_SetError(-EXDEV);
2195 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2197 if(newNameLength == 0){
2198 yaffsfs_SetError(-ENOENT);
2200 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
2201 yaffsfs_SetError(-ENAMETOOLONG);
2206 link = yaffs_Link(newdir,newname,obj);
2210 yaffsfs_SetError(-ENOSPC);
2221 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2226 int yaffs_DumpDevStruct(const YCHAR *path)
2231 yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
2234 yaffs_Device *dev = obj->myDev;
2237 "nPageWrites.......... %d\n"
2238 "nPageReads........... %d\n"
2239 "nBlockErasures....... %d\n"
2240 "nGCCopies............ %d\n"
2241 "garbageCollections... %d\n"
2242 "passiveGarbageColl'ns %d\n"
2246 dev->nBlockErasures,
2248 dev->garbageCollections,
2249 dev->passiveGarbageCollections