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 struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
36 int symDepth, int getEquiv,
37 struct yaffs_obj **dirOut);
39 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
41 unsigned int yaffs_wr_attempts;
45 * There are open inodes in yaffsfs_Inode.
46 * There are open handles in yaffsfs_Handle.
48 * Things are structured this way to be like the Linux VFS model
49 * so that interactions with the yaffs guts calls are similar.
50 * That means more common code paths and less special code.
51 * That means better testing etc.
55 int count; /* Number of handles accessing this inode */
56 struct yaffs_obj *iObj;
65 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
66 int useCount:10; /* 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];
72 static int yaffsfs_handlesInitialised;
75 unsigned yaffs_set_trace(unsigned tm)
77 yaffs_trace_mask = tm;
78 return yaffs_trace_mask;
81 unsigned yaffs_get_trace(void)
83 return yaffs_trace_mask;
88 * Inilitalise handle management on start-up.
91 static void yaffsfs_InitHandles(void)
94 if(yaffsfs_handlesInitialised)
97 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
98 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
99 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
100 yaffsfs_handle[i].inodeId = -1;
103 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
105 if(h < 0 || h >= YAFFSFS_N_HANDLES)
108 return &yaffsfs_handle[h];
111 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
113 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
115 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
116 return &yaffsfs_inode[h->inodeId];
121 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
123 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
132 * yaffsfs_FindInodeIdForObject
133 * Find the inode entry for an object, if it exists.
136 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
142 obj = yaffs_get_equivalent_obj(obj);
144 /* Look for it in open inode table*/
145 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
146 if(yaffsfs_inode[i].iObj == obj)
153 * yaffsfs_GetInodeIdForObject
154 * Grab an inode entry when opening a new inode.
156 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
160 yaffsfs_Inode *in = NULL;
163 obj = yaffs_get_equivalent_obj(obj);
165 ret = yaffsfs_FindInodeIdForObject(obj);
167 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
168 if(!yaffsfs_inode[i].iObj)
173 in = &yaffsfs_inode[ret];
185 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
187 int i = yaffsfs_FindInodeIdForObject(obj);
190 return yaffsfs_inode[i].count;
195 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
197 struct yaffs_obj *obj;
204 obj->my_inode = NULL;
209 static void yaffsfs_PutInode(int inodeId)
211 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
212 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
215 yaffsfs_ReleaseInode(in);
222 * Grab a handle (when opening a file)
225 static int yaffsfs_GetNewHandle(void)
230 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
231 h = yaffsfs_GetHandlePointer(i);
233 /* todo bug: should never happen */
236 memset(h,0,sizeof(yaffsfs_Handle));
247 * Increase use of handle when reading/writing a file
249 static int yaffsfs_GetHandle(int handle)
251 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
253 if(h && h->useCount > 0){
261 * Let go of a handle when closing a file or aborting an open or
262 * ending a read or write.
264 static int yaffsfs_PutHandle(int handle)
266 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
268 if(h && h->useCount > 0){
272 yaffsfs_PutInode(h->inodeId);
280 static void yaffsfs_PutDeviceHandles(struct yaffs_dev *dev)
284 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
285 yh = & yaffsfs_handle[i];
287 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
288 yaffsfs_PutHandle(i);
296 * Stuff to handle names.
300 int yaffsfs_Match(YCHAR a, YCHAR b)
306 int yaffsfs_IsPathDivider(YCHAR ch)
308 const YCHAR *str = YAFFS_PATH_DIVIDERS;
319 int yaffsfs_CheckNameLength(const char *name)
323 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
326 yaffsfs_SetError(-ENOENT);
328 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
329 yaffsfs_SetError(-ENAMETOOLONG);
336 LIST_HEAD(yaffsfs_deviceList);
341 * Scan the configuration list to find the device
342 * Curveballs: Should match paths that end in '/' too
343 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
345 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
347 struct list_head *cfg;
348 const YCHAR *leftOver;
350 struct yaffs_dev *retval = NULL;
351 struct yaffs_dev *dev = NULL;
353 int longestMatch = -1;
357 * Check all configs, choose the one that:
358 * 1) Actually matches a prefix (ie /a amd /abc will not match
359 * 2) Matches the longest.
361 list_for_each(cfg, &yaffsfs_deviceList){
362 dev = list_entry(cfg, struct yaffs_dev, dev_list);
369 while(matching && *p && *leftOver){
370 /* Skip over any /s */
371 while(yaffsfs_IsPathDivider(*p))
374 /* Skip over any /s */
375 while(yaffsfs_IsPathDivider(*leftOver))
378 /* Now match the text part */
380 *p && !yaffsfs_IsPathDivider(*p) &&
381 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
382 if(yaffsfs_Match(*p,*leftOver)){
392 /* Skip over any /s in leftOver */
393 while(yaffsfs_IsPathDivider(*leftOver))
396 // Skip over any /s in p
397 while(yaffsfs_IsPathDivider(*p))
400 // p should now be at the end of the string (ie. fully matched)
404 if( matching && (thisMatchLength > longestMatch))
407 *restOfPath = (YCHAR *)leftOver;
409 longestMatch = thisMatchLength;
416 static int yaffsfs_CheckPath(const YCHAR *path)
420 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
421 if(yaffsfs_IsPathDivider(*path)){
429 return (*path) ? -1 : 0;
432 /* FindMountPoint only returns a dev entry if the path is a mount point */
433 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
435 struct yaffs_dev *dev;
436 YCHAR *restOfPath=NULL;
437 dev = yaffsfs_FindDevice(path,&restOfPath);
438 if(dev && restOfPath && *restOfPath)
443 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
446 struct yaffs_dev *dev;
448 dev= yaffsfs_FindDevice(path,restOfPath);
449 if(dev && dev->is_mounted){
450 return dev->root_dir;
455 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
459 obj = yaffs_get_equivalent_obj(obj);
461 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
462 YCHAR *alias = obj->variant.symlink_variant.alias;
464 if(yaffsfs_IsPathDivider(*alias))
465 /* Starts with a /, need to scan from root up */
466 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL);
468 /* Relative to here, so use the parent of the symlink as a start */
469 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL);
476 * yaffsfs_FindDirectory
477 * Parse a path to determine the directory and the name within the directory.
479 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
481 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
482 const YCHAR *path, YCHAR **name, int symDepth)
484 struct yaffs_obj *dir;
486 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
489 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
494 restOfPath = (YCHAR *)path;
497 dir = yaffsfs_FindRoot(path,&restOfPath);
502 * curve ball: also throw away surplus '/'
503 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
505 while(yaffsfs_IsPathDivider(*restOfPath))
506 restOfPath++; /* get rid of '/' */
511 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
512 if (i < YAFFS_MAX_NAME_LENGTH){
513 str[i] = *restOfPath;
521 /* got to the end of the string */
524 if(yaffs_strcmp(str,_Y(".")) == 0)
528 else if(yaffs_strcmp(str,_Y("..")) == 0)
531 dir = yaffs_find_by_name(dir,str);
533 dir = yaffsfs_FollowLink(dir,symDepth);
535 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
540 /* directory did not exist. */
544 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
545 const YCHAR *path,YCHAR **name,int symDepth)
547 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
551 * yaffsfs_FindObject turns a path for an existing object into the object
553 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
554 const YCHAR *path,int symDepth, int getEquiv,
555 struct yaffs_obj **dirOut)
557 struct yaffs_obj *dir;
558 struct yaffs_obj *obj;
561 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
567 obj = yaffs_find_by_name(dir,name);
572 obj = yaffs_get_equivalent_obj(obj);
578 int yaffs_dup(int fd)
581 yaffsfs_Handle *oldPtr = NULL;
582 yaffsfs_Handle *newPtr = NULL;
586 oldPtr = yaffsfs_GetHandlePointer(fd);
587 if(oldPtr && oldPtr->useCount > 0)
588 newHandle = yaffsfs_GetNewHandle();
590 newPtr = yaffsfs_GetHandlePointer(newHandle);
598 yaffsfs_SetError(-EBADF);
600 yaffsfs_SetError(-ENOMEM);
608 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
610 struct yaffs_obj *obj = NULL;
611 struct yaffs_obj *dir = NULL;
614 yaffsfs_Handle *yh = NULL;
617 int errorReported = 0;
618 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
619 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
620 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
621 u8 sharedReadAllowed;
622 u8 sharedWriteAllowed;
628 if(yaffsfs_CheckPath(path) < 0){
629 yaffsfs_SetError(-ENAMETOOLONG);
633 /* O_EXCL only has meaning if O_CREAT is specified */
634 if(!(oflag & O_CREAT))
637 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
638 if( (oflag & O_CREAT) & (oflag & O_EXCL))
641 /* Todo: Are there any more flag combos to sanitise ? */
643 /* Figure out if reading or writing is requested */
645 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
646 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
650 handle = yaffsfs_GetNewHandle();
653 yaffsfs_SetError(-ENFILE);
657 yh = yaffsfs_GetHandlePointer(handle);
659 /* try to find the exisiting object */
660 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
662 obj = yaffsfs_FollowLink(obj,symDepth++);
665 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
666 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
672 /* The file already exists or it might be a directory */
674 /* If it is a directory then we can't open it as a file */
675 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
677 yaffsfs_SetError(-EISDIR);
681 /* Open should fail if O_CREAT and O_EXCL are specified since
684 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
686 yaffsfs_SetError(-EEXIST);
690 /* Check file permissions */
691 if( readRequested && !(obj->yst_mode & S_IREAD))
694 if( writeRequested && !(obj->yst_mode & S_IWRITE))
697 if(openDenied && !errorReported ) {
698 /* Error if the file exists but permissions are refused. */
699 yaffsfs_SetError(-EACCES);
703 /* Check sharing of an existing object. */
707 sharedReadAllowed = 1;
708 sharedWriteAllowed = 1;
711 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
712 hx = &yaffsfs_handle[i];
713 if(hx->useCount > 0 &&
715 yaffsfs_inode[hx->inodeId].iObj == obj){
717 sharedReadAllowed = 0;
719 sharedWriteAllowed = 0;
729 if((!sharedReadAllowed && readRequested)||
730 (!shareRead && alreadyReading) ||
731 (!sharedWriteAllowed && writeRequested) ||
732 (!shareWrite && alreadyWriting)){
734 yaffsfs_SetError(-EBUSY);
741 /* If we could not open an existing object, then let's see if
742 * the directory exists. If not, error.
744 if(!obj && !errorReported){
745 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
747 yaffsfs_SetError(-ENOTDIR);
752 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
753 /* Let's see if we can create this file if it does not exist. */
754 if(dir->my_dev->read_only){
755 yaffsfs_SetError(-EINVAL);
758 obj = yaffs_create_file(dir,name,mode,0,0);
760 if(!obj && !errorReported){
761 yaffsfs_SetError(-ENOSPC);
766 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
767 /* Error if the file does not exist and CREAT is not set. */
768 yaffsfs_SetError(-ENOENT);
772 if(obj && !openDenied) {
773 int inodeId = yaffsfs_GetInodeIdForObject(obj);
777 * Todo: Fix any problem if inodes run out, though that
778 * can't happen if the number of inode items >= number of handles.
782 yh->inodeId = inodeId;
783 yh->reading = readRequested;
784 yh->writing = writeRequested;
785 yh->append = (oflag & O_APPEND) ? 1 : 0;
787 yh->shareRead = shareRead;
788 yh->shareWrite = shareWrite;
790 /* Hook inode to object */
791 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
793 if((oflag & O_TRUNC) && yh->writing)
794 yaffs_resize_file(obj,0);
796 yaffsfs_PutHandle(handle);
798 yaffsfs_SetError(0); /* Problem */
808 int yaffs_open(const YCHAR *path, int oflag, int mode)
810 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
813 int yaffs_Dofsync(int fd,int datasync)
815 yaffsfs_Handle *h = NULL;
820 h = yaffsfs_GetHandlePointer(fd);
822 if(h && h->useCount > 0)
824 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
827 yaffsfs_SetError(-EBADF);
836 int yaffs_fsync(int fd)
838 return yaffs_Dofsync(fd,0);
841 int yaffs_flush(int fd)
843 return yaffs_fsync(fd);
846 int yaffs_fdatasync(int fd)
848 return yaffs_Dofsync(fd,1);
851 int yaffs_close(int fd)
853 yaffsfs_Handle *h = NULL;
858 h = yaffsfs_GetHandlePointer(fd);
860 if(h && h->useCount > 0) {
862 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
863 yaffsfs_PutHandle(fd);
867 yaffsfs_SetError(-EBADF);
878 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
880 yaffsfs_Handle *h = NULL;
881 struct yaffs_obj *obj = NULL;
887 unsigned int maxRead;
888 u8 *buf = (u8 *)vbuf;
891 h = yaffsfs_GetHandlePointer(fd);
892 obj = yaffsfs_GetHandleObject(fd);
896 yaffsfs_SetError(-EBADF);
898 } else if(!h->reading){
899 /* Not a reading handle */
900 yaffsfs_SetError(-EINVAL);
902 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
903 yaffsfs_SetError(-EINVAL);
909 startPos = h->position;
913 if(yaffs_get_obj_length(obj) > pos)
914 maxRead = yaffs_get_obj_length(obj) - pos;
922 yaffsfs_GetHandle(fd);
925 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
930 * Need to reverify object in case the device was
931 * unmounted in another thread.
933 obj = yaffsfs_GetHandleObject(fd);
937 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
948 nbyte = 0; /* no more to read */
958 yaffsfs_PutHandle(fd);
962 h->position = startPos + totalRead;
972 return (totalRead >= 0) ? totalRead : -1;
976 int yaffs_read(int fd, void *buf, unsigned int nbyte)
978 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
981 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
983 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
986 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
988 yaffsfs_Handle *h = NULL;
989 struct yaffs_obj *obj = NULL;
993 int totalWritten = 0;
994 int write_trhrough = 0;
996 const u8 *buf = (const u8 *)vbuf;
999 h = yaffsfs_GetHandlePointer(fd);
1000 obj = yaffsfs_GetHandleObject(fd);
1004 yaffsfs_SetError(-EBADF);
1006 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1007 yaffsfs_SetError(-EINVAL);
1011 startPos = yaffs_get_obj_length(obj);
1015 startPos = h->position;
1017 yaffsfs_GetHandle(fd);
1021 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1022 if(nToWrite > nbyte)
1026 * Need to reverify object in case the device was
1027 * remounted or unmounted in another thread.
1029 obj = yaffsfs_GetHandleObject(fd);
1030 if(!obj || obj->my_dev->read_only)
1033 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1036 totalWritten += nWritten;
1041 if(nWritten == nToWrite)
1046 if(nWritten < 1 && totalWritten < 1){
1047 yaffsfs_SetError(-ENOSPC);
1057 yaffsfs_PutHandle(fd);
1060 if(totalWritten > 0)
1061 h->position = startPos + totalWritten;
1070 return (totalWritten >= 0) ? totalWritten : -1;
1073 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1075 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1078 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1080 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1084 int yaffs_truncate(const YCHAR *path,off_t new_size)
1086 struct yaffs_obj *obj = NULL;
1087 struct yaffs_obj *dir = NULL;
1088 int result = YAFFS_FAIL;
1090 if(yaffsfs_CheckPath(path) < 0){
1091 yaffsfs_SetError(-ENAMETOOLONG);
1097 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1100 yaffsfs_SetError(-ENOTDIR);
1102 yaffsfs_SetError(-ENOENT);
1103 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1104 yaffsfs_SetError(-EISDIR);
1105 else if(obj->my_dev->read_only)
1106 yaffsfs_SetError(-EACCES);
1107 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1108 yaffsfs_SetError(-EINVAL);
1110 result = yaffs_resize_file(obj,new_size);
1114 return (result) ? 0 : -1;
1117 int yaffs_ftruncate(int fd, off_t new_size)
1119 yaffsfs_Handle *h = NULL;
1120 struct yaffs_obj *obj = NULL;
1124 h = yaffsfs_GetHandlePointer(fd);
1125 obj = yaffsfs_GetHandleObject(fd);
1129 yaffsfs_SetError(-EBADF);
1130 else if(obj->my_dev->read_only)
1131 yaffsfs_SetError(-EACCES);
1132 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1133 yaffsfs_SetError(-EINVAL);
1135 /* resize the file */
1136 result = yaffs_resize_file(obj,new_size);
1139 return (result) ? 0 : -1;
1143 off_t yaffs_lseek(int fd, off_t offset, int whence)
1145 yaffsfs_Handle *h = NULL;
1146 struct yaffs_obj *obj = NULL;
1151 h = yaffsfs_GetHandlePointer(fd);
1152 obj = yaffsfs_GetHandleObject(fd);
1155 yaffsfs_SetError(-EBADF);
1156 else if(offset > YAFFS_MAX_FILE_SIZE)
1157 yaffsfs_SetError(-EINVAL);
1159 if(whence == SEEK_SET){
1162 } else if(whence == SEEK_CUR) {
1163 if( (h->position + offset) >= 0)
1164 pos = (h->position + offset);
1165 } else if(whence == SEEK_END) {
1166 fSize = yaffs_get_obj_length(obj);
1167 if(fSize >= 0 && (fSize + offset) >= 0)
1168 pos = fSize + offset;
1174 yaffsfs_SetError(-EINVAL);
1183 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1185 struct yaffs_obj *dir = NULL;
1186 struct yaffs_obj *obj = NULL;
1188 int result = YAFFS_FAIL;
1190 if(yaffsfs_CheckPath(path) < 0){
1191 yaffsfs_SetError(-ENAMETOOLONG);
1197 obj = yaffsfs_FindObject(NULL,path,0,0,NULL);
1198 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1201 yaffsfs_SetError(-ENOTDIR);
1203 yaffsfs_SetError(-ENOENT);
1204 else if(obj->my_dev->read_only)
1205 yaffsfs_SetError(-EINVAL);
1206 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1207 yaffsfs_SetError(-EISDIR);
1208 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1209 yaffsfs_SetError(-ENOTDIR);
1211 result = yaffs_unlinker(dir,name);
1213 if(result == YAFFS_FAIL && isDirectory)
1214 yaffsfs_SetError(-ENOTEMPTY);
1221 return (result == YAFFS_FAIL) ? -1 : 0;
1225 int yaffs_unlink(const YCHAR *path)
1227 return yaffsfs_DoUnlink(path,0);
1230 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1232 struct yaffs_obj *olddir = NULL;
1233 struct yaffs_obj *newdir = NULL;
1234 struct yaffs_obj *obj = NULL;
1237 int result= YAFFS_FAIL;
1238 int rename_allowed = 1;
1242 if(yaffsfs_CheckPath(newPath) < 0){
1243 yaffsfs_SetError(-ENAMETOOLONG);
1247 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1248 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1249 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1251 if(!olddir || !newdir) {
1252 yaffsfs_SetError(-ENOTDIR);
1255 yaffsfs_SetError(-ENOENT);
1257 } else if(obj->my_dev->read_only){
1258 yaffsfs_SetError(-EROFS);
1260 } else if(olddir->my_dev != newdir->my_dev) {
1261 /* Rename must be on same device */
1262 yaffsfs_SetError(-EXDEV);
1264 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1266 * It is a directory, check that it is not being renamed to
1267 * being its own decendent.
1268 * Do this by tracing from the new directory back to the root, checking for obj
1271 struct yaffs_obj *xx = newdir;
1273 while( rename_allowed && xx){
1279 yaffsfs_SetError(-EINVAL);
1283 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1287 return (result == YAFFS_FAIL) ? -1 : 0;
1291 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1295 obj = yaffs_get_equivalent_obj(obj);
1298 buf->st_dev = (int)obj->my_dev->os_context;
1299 buf->st_ino = obj->obj_id;
1300 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1302 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1303 buf->st_mode |= S_IFDIR;
1304 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1305 buf->st_mode |= S_IFLNK;
1306 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1307 buf->st_mode |= S_IFREG;
1309 buf->st_nlink = yaffs_get_obj_link_count(obj);
1312 buf->st_rdev = obj->yst_rdev;
1313 buf->st_size = yaffs_get_obj_length(obj);
1314 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1315 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1316 #if CONFIG_YAFFS_WINCE
1317 buf->yst_wince_atime[0] = obj->win_atime[0];
1318 buf->yst_wince_atime[1] = obj->win_atime[1];
1319 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1320 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1321 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1322 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1324 buf->yst_atime = obj->yst_atime;
1325 buf->yst_ctime = obj->yst_ctime;
1326 buf->yst_mtime = obj->yst_mtime;
1333 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1335 struct yaffs_obj *obj=NULL;
1336 struct yaffs_obj *dir=NULL;
1340 if(yaffsfs_CheckPath(path) < 0){
1341 yaffsfs_SetError(-ENAMETOOLONG);
1347 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1350 obj = yaffsfs_FollowLink(obj,0);
1353 yaffsfs_SetError(-ENOTDIR);
1355 yaffsfs_SetError(-ENOENT);
1357 retVal = yaffsfs_DoStat(obj,buf);
1365 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1367 return yaffsfs_DoStatOrLStat(path,buf,0);
1370 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1372 return yaffsfs_DoStatOrLStat(path,buf,1);
1375 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1377 struct yaffs_obj *obj;
1382 obj = yaffsfs_GetHandleObject(fd);
1385 retVal = yaffsfs_DoStat(obj,buf);
1388 yaffsfs_SetError(-EBADF);
1395 #ifndef CONFIG_YAFFS_WINCE
1396 /* xattrib functions */
1399 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1401 struct yaffs_obj *obj;
1402 struct yaffs_obj *dir;
1406 if(yaffsfs_CheckPath(path) < 0){
1407 yaffsfs_SetError(-ENAMETOOLONG);
1413 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1416 obj = yaffsfs_FollowLink(obj,0);
1419 yaffsfs_SetError(-ENOTDIR);
1421 yaffsfs_SetError(-ENOENT);
1423 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1425 yaffsfs_SetError(retVal);
1436 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1438 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1441 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1443 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1448 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1450 struct yaffs_obj *obj;
1455 obj = yaffsfs_GetHandleObject(fd);
1458 yaffsfs_SetError(-EBADF);
1460 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1462 yaffsfs_SetError(retVal);
1472 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1474 struct yaffs_obj *obj;
1475 struct yaffs_obj *dir;
1479 if(yaffsfs_CheckPath(path) < 0){
1480 yaffsfs_SetError(-ENAMETOOLONG);
1486 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1489 obj = yaffsfs_FollowLink(obj,0);
1492 yaffsfs_SetError(-ENOTDIR);
1494 yaffsfs_SetError(-ENOENT);
1496 retVal = yaffs_get_xattrib(obj,name,data,size);
1498 yaffsfs_SetError(retVal);
1508 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1510 return yaffs_do_getxattr( path, name, data, size, 1);
1512 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1514 return yaffs_do_getxattr( path, name, data, size, 0);
1519 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1521 struct yaffs_obj *obj;
1526 obj = yaffsfs_GetHandleObject(fd);
1529 retVal = yaffs_get_xattrib(obj,name,data,size);
1531 yaffsfs_SetError(retVal);
1536 yaffsfs_SetError(-EBADF);
1543 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1545 struct yaffs_obj *obj=NULL;
1546 struct yaffs_obj *dir=NULL;
1550 if(yaffsfs_CheckPath(path) < 0){
1551 yaffsfs_SetError(-ENAMETOOLONG);
1557 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1560 obj = yaffsfs_FollowLink(obj,0);
1563 yaffsfs_SetError(-ENOTDIR);
1565 yaffsfs_SetError(-ENOENT);
1567 retVal = yaffs_list_xattrib(obj, data,size);
1569 yaffsfs_SetError(retVal);
1580 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1582 return yaffs_do_listxattr(path, data, size, 1);
1585 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1587 return yaffs_do_listxattr(path, data, size, 0);
1590 int yaffs_flistxattr(int fd, char *data, int size)
1592 struct yaffs_obj *obj;
1597 obj = yaffsfs_GetHandleObject(fd);
1600 retVal = yaffs_list_xattrib(obj,data,size);
1602 yaffsfs_SetError(retVal);
1607 yaffsfs_SetError(-EBADF);
1614 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1616 struct yaffs_obj *obj=NULL;
1617 struct yaffs_obj *dir=NULL;
1621 if(yaffsfs_CheckPath(path) < 0){
1622 yaffsfs_SetError(-ENAMETOOLONG);
1628 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1631 obj = yaffsfs_FollowLink(obj,0);
1634 yaffsfs_SetError(-ENOTDIR);
1636 yaffsfs_SetError(-ENOENT);
1638 retVal = yaffs_remove_xattrib(obj,name);
1640 yaffsfs_SetError(retVal);
1651 int yaffs_removexattr(const YCHAR *path, const char *name)
1653 return yaffs_do_removexattr(path, name, 1);
1656 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1658 return yaffs_do_removexattr(path, name, 0);
1661 int yaffs_fremovexattr(int fd, const char *name)
1663 struct yaffs_obj *obj;
1668 obj = yaffsfs_GetHandleObject(fd);
1671 retVal = yaffs_remove_xattrib(obj,name);
1673 yaffsfs_SetError(retVal);
1678 yaffsfs_SetError(-EBADF);
1686 #ifdef CONFIG_YAFFS_WINCE
1687 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1689 struct yaffs_obj *obj;
1694 obj = yaffsfs_GetHandleObject(fd);
1699 wctime[0] = obj->win_ctime[0];
1700 wctime[1] = obj->win_ctime[1];
1703 watime[0] = obj->win_atime[0];
1704 watime[1] = obj->win_atime[1];
1707 wmtime[0] = obj->win_mtime[0];
1708 wmtime[1] = obj->win_mtime[1];
1715 yaffsfs_SetError(-EBADF);
1723 int yaffs_set_wince_times(int fd,
1724 const unsigned *wctime,
1725 const unsigned *watime,
1726 const unsigned *wmtime)
1728 struct yaffs_obj *obj;
1733 obj = yaffsfs_GetHandleObject(fd);
1738 obj->win_ctime[0] = wctime[0];
1739 obj->win_ctime[1] = wctime[1];
1742 obj->win_atime[0] = watime[0];
1743 obj->win_atime[1] = watime[1];
1746 obj->win_mtime[0] = wmtime[0];
1747 obj->win_mtime[1] = wmtime[1];
1751 result = yaffs_flush_file(obj,0,0);
1755 yaffsfs_SetError(-EBADF);
1765 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1770 obj = yaffs_get_equivalent_obj(obj);
1773 obj->yst_mode = mode;
1775 result = yaffs_flush_file(obj,0,0);
1778 return result == YAFFS_OK ? 0 : -1;
1782 int yaffs_access(const YCHAR *path, int amode)
1784 struct yaffs_obj *obj=NULL;
1785 struct yaffs_obj *dir=NULL;
1789 if(yaffsfs_CheckPath(path) < 0){
1790 yaffsfs_SetError(-ENAMETOOLONG);
1794 if(amode & ~(R_OK | W_OK | X_OK)){
1795 yaffsfs_SetError(-EINVAL);
1801 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1804 yaffsfs_SetError(-ENOTDIR);
1806 yaffsfs_SetError(-ENOENT);
1810 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1812 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1814 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1818 yaffsfs_SetError(-EACCES);
1830 int yaffs_chmod(const YCHAR *path, mode_t mode)
1832 struct yaffs_obj *obj=NULL;
1833 struct yaffs_obj *dir=NULL;
1836 if(yaffsfs_CheckPath(path) < 0){
1837 yaffsfs_SetError(-ENAMETOOLONG);
1842 yaffsfs_SetError(-EINVAL);
1848 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1851 yaffsfs_SetError(-ENOTDIR);
1853 yaffsfs_SetError(-ENOENT);
1854 else if(obj->my_dev->read_only)
1855 yaffsfs_SetError(-EROFS);
1857 retVal = yaffsfs_DoChMod(obj,mode);
1866 int yaffs_fchmod(int fd, mode_t mode)
1868 struct yaffs_obj *obj;
1872 yaffsfs_SetError(-EINVAL);
1877 obj = yaffsfs_GetHandleObject(fd);
1880 yaffsfs_SetError(-EBADF);
1881 else if(obj->my_dev->read_only)
1882 yaffsfs_SetError(-EROFS);
1884 retVal = yaffsfs_DoChMod(obj,mode);
1892 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1894 YCHAR *alt_path = NULL;
1899 * We don't have a definition for max path length.
1900 * We will use 3 * max name length instead.
1903 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1905 /* If the last character is a path divider, then we need to
1906 * trim it back so that the name look-up works properly.
1907 * eg. /foo/new_dir/ -> /foo/newdir
1908 * Curveball: Need to handle multiple path dividers:
1909 * eg. /foof/sdfse///// -> /foo/sdfse
1911 if(path_length > 0 &&
1912 yaffsfs_IsPathDivider(path[path_length-1])){
1913 alt_path = YMALLOC(path_length + 1);
1916 strcpy(alt_path, path);
1917 for(i = path_length-1;
1918 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1920 alt_path[i] = (YCHAR) 0;
1922 *ret_path = alt_path;
1926 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1928 struct yaffs_obj *parent = NULL;
1929 struct yaffs_obj *dir = NULL;
1931 YCHAR *alt_path = NULL;
1934 if(yaffsfs_CheckPath(path) < 0){
1935 yaffsfs_SetError(-ENAMETOOLONG);
1939 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1940 yaffsfs_SetError(-ENOMEM);
1947 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1949 yaffsfs_SetError(-ENOTDIR);
1950 else if(parent && yaffs_strnlen(name,5) == 0){
1951 /* Trying to make the root itself */
1952 yaffsfs_SetError(-EEXIST);
1953 } else if(parent && parent->my_dev->read_only)
1954 yaffsfs_SetError(-EROFS);
1956 dir = yaffs_create_dir(parent,name,mode,0,0);
1959 else if (yaffs_find_by_name(parent,name))
1960 yaffsfs_SetError(-EEXIST); /* the name already exists */
1962 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1973 int yaffs_rmdir(const YCHAR *path)
1978 if(yaffsfs_CheckPath(path) < 0){
1979 yaffsfs_SetError(-ENAMETOOLONG);
1983 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1984 yaffsfs_SetError(-ENOMEM);
1989 result = yaffsfs_DoUnlink(path,1);
1996 void * yaffs_getdev(const YCHAR *path)
1998 struct yaffs_dev *dev=NULL;
2000 dev = yaffsfs_FindDevice(path,&dummy);
2004 int yaffs_mount2(const YCHAR *path,int read_only)
2007 int result=YAFFS_FAIL;
2008 struct yaffs_dev *dev=NULL;
2010 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2012 if(yaffsfs_CheckPath(path) < 0){
2013 yaffsfs_SetError(-ENAMETOOLONG);
2019 yaffsfs_InitHandles();
2021 dev = yaffsfs_FindMountPoint(path);
2023 if(!dev->is_mounted){
2024 dev->read_only = read_only ? 1 : 0;
2025 result = yaffs_guts_initialise(dev);
2026 if(result == YAFFS_FAIL)
2027 /* todo error - mount failed */
2028 yaffsfs_SetError(-ENOMEM);
2029 retVal = result ? 0 : -1;
2033 /* todo error - already mounted. */
2034 yaffsfs_SetError(-EBUSY);
2036 /* todo error - no device */
2037 yaffsfs_SetError(-ENODEV);
2044 int yaffs_mount(const YCHAR *path)
2046 return yaffs_mount2(path,0);
2049 int yaffs_sync(const YCHAR *path)
2052 struct yaffs_dev *dev=NULL;
2055 if(yaffsfs_CheckPath(path) < 0){
2056 yaffsfs_SetError(-ENAMETOOLONG);
2061 dev = yaffsfs_FindDevice(path,&dummy);
2063 if(dev->is_mounted){
2065 yaffs_flush_whole_cache(dev);
2066 yaffs_checkpoint_save(dev);
2070 /* todo error - not mounted. */
2071 yaffsfs_SetError(-EINVAL);
2074 /* todo error - no device */
2075 yaffsfs_SetError(-ENODEV);
2082 int yaffs_remount(const YCHAR *path, int force, int read_only)
2085 struct yaffs_dev *dev=NULL;
2088 if(yaffsfs_CheckPath(path) < 0){
2089 yaffsfs_SetError(-ENAMETOOLONG);
2094 dev = yaffsfs_FindMountPoint(path);
2096 if(dev->is_mounted){
2100 yaffs_flush_whole_cache(dev);
2102 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2103 yh = & yaffsfs_handle[i];
2104 if(yh->useCount>0 &&
2105 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2106 inUse = 1; /* the device is in use, can't unmount */
2109 if(!inUse || force){
2111 yaffs_checkpoint_save(dev);
2112 dev->read_only = read_only ? 1 : 0;
2115 yaffsfs_SetError(-EBUSY);
2118 yaffsfs_SetError(-EINVAL);
2122 yaffsfs_SetError(-ENODEV);
2129 int yaffs_unmount2(const YCHAR *path, int force)
2132 struct yaffs_dev *dev=NULL;
2134 if(yaffsfs_CheckPath(path) < 0){
2135 yaffsfs_SetError(-ENAMETOOLONG);
2140 dev = yaffsfs_FindMountPoint(path);
2142 if(dev->is_mounted){
2146 yaffs_flush_whole_cache(dev);
2147 yaffs_checkpoint_save(dev);
2149 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2150 if(yaffsfs_handle[i].useCount > 0 &&
2151 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2152 inUse = 1; /* the device is in use, can't unmount */
2155 if(!inUse || force){
2157 yaffsfs_PutDeviceHandles(dev);
2158 yaffs_deinitialise(dev);
2162 /* todo error can't unmount as files are open */
2163 yaffsfs_SetError(-EBUSY);
2166 /* todo error - not mounted. */
2167 yaffsfs_SetError(-EINVAL);
2170 /* todo error - no device */
2171 yaffsfs_SetError(-ENODEV);
2178 int yaffs_unmount(const YCHAR *path)
2180 return yaffs_unmount2(path,0);
2183 loff_t yaffs_freespace(const YCHAR *path)
2186 struct yaffs_dev *dev=NULL;
2189 if(yaffsfs_CheckPath(path) < 0){
2190 yaffsfs_SetError(-ENAMETOOLONG);
2195 dev = yaffsfs_FindDevice(path,&dummy);
2196 if(dev && dev->is_mounted){
2197 retVal = yaffs_get_n_free_chunks(dev);
2198 retVal *= dev->data_bytes_per_chunk;
2201 yaffsfs_SetError(-EINVAL);
2207 loff_t yaffs_totalspace(const YCHAR *path)
2210 struct yaffs_dev *dev=NULL;
2213 if(yaffsfs_CheckPath(path) < 0){
2214 yaffsfs_SetError(-ENAMETOOLONG);
2219 dev = yaffsfs_FindDevice(path,&dummy);
2220 if(dev && dev->is_mounted){
2221 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2222 dev->param.n_reserved_blocks;
2223 retVal *= dev->param.chunks_per_block;
2224 retVal *= dev->data_bytes_per_chunk;
2227 yaffsfs_SetError(-EINVAL);
2233 int yaffs_inodecount(const YCHAR *path)
2236 struct yaffs_dev *dev=NULL;
2239 if(yaffsfs_CheckPath(path) < 0){
2240 yaffsfs_SetError(-ENAMETOOLONG);
2245 dev = yaffsfs_FindDevice(path,&dummy);
2246 if(dev && dev->is_mounted) {
2247 int n_obj = dev->n_obj;
2248 if(n_obj > dev->n_hardlinks)
2249 retVal = n_obj - dev->n_hardlinks;
2253 yaffsfs_SetError(-EINVAL);
2260 void yaffs_add_device(struct yaffs_dev *dev)
2262 dev->is_mounted = 0;
2263 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2265 if(!dev->dev_list.next)
2266 INIT_LIST_HEAD(&dev->dev_list);
2268 list_add(&dev->dev_list,&yaffsfs_deviceList);
2271 void yaffs_remove_device(struct yaffs_dev *dev)
2273 list_del_init(&dev->dev_list);
2279 /* Directory search stuff. */
2282 * Directory search context
2284 * NB this is an opaque structure.
2291 yaffs_dirent de; /* directory entry being used by this dsc */
2292 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2293 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2294 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2296 struct list_head others;
2297 } yaffsfs_DirectorySearchContext;
2301 static struct list_head search_contexts;
2304 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2308 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2312 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2313 dsc->nextReturn = NULL;
2315 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2316 struct yaffs_obj,siblings);
2318 /* Hey someone isn't playing nice! */
2322 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2326 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2328 if( dsc->nextReturn == NULL ||
2329 list_empty(&dsc->dirObj->variant.dir_variant.children))
2330 dsc->nextReturn = NULL;
2332 struct list_head *next = dsc->nextReturn->siblings.next;
2334 if( next == &dsc->dirObj->variant.dir_variant.children)
2335 dsc->nextReturn = NULL; /* end of list */
2337 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2340 /* Hey someone isn't playing nice! */
2344 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2347 struct list_head *i;
2348 yaffsfs_DirectorySearchContext *dsc;
2350 /* if search contexts not initilised then skip */
2351 if(!search_contexts.next)
2354 /* Iterate through the directory search contexts.
2355 * If any are the one being removed, then advance the dsc to
2356 * the next one to prevent a hanging ptr.
2358 list_for_each(i, &search_contexts) {
2360 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2361 if(dsc->nextReturn == obj)
2362 yaffsfs_DirAdvance(dsc);
2368 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2370 yaffs_DIR *dir = NULL;
2371 struct yaffs_obj *obj = NULL;
2372 yaffsfs_DirectorySearchContext *dsc = NULL;
2374 if(yaffsfs_CheckPath(dirname) < 0){
2375 yaffsfs_SetError(-ENAMETOOLONG);
2381 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2383 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2385 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2386 dir = (yaffs_DIR *)dsc;
2389 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2390 dsc->magic = YAFFS_MAGIC;
2392 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2393 INIT_LIST_HEAD(&dsc->others);
2395 if(!search_contexts.next)
2396 INIT_LIST_HEAD(&search_contexts);
2398 list_add(&dsc->others,&search_contexts);
2399 yaffsfs_SetDirRewound(dsc);
2409 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2411 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2412 struct yaffs_dirent *retVal = NULL;
2416 if(dsc && dsc->magic == YAFFS_MAGIC){
2417 yaffsfs_SetError(0);
2418 if(dsc->nextReturn){
2419 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2420 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2421 dsc->de.d_off = dsc->offset++;
2422 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2423 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2425 /* this should not happen! */
2426 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2428 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2430 yaffsfs_DirAdvance(dsc);
2434 yaffsfs_SetError(-EBADF);
2443 void yaffs_rewinddir(yaffs_DIR *dirp)
2445 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2449 yaffsfs_SetDirRewound(dsc);
2455 int yaffs_closedir(yaffs_DIR *dirp)
2457 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2461 list_del(&dsc->others); /* unhook from list */
2467 /* End of directory stuff */
2470 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2472 struct yaffs_obj *parent = NULL;
2473 struct yaffs_obj *obj;
2476 int mode = 0; /* ignore for now */
2478 if(yaffsfs_CheckPath(newpath) < 0){
2479 yaffsfs_SetError(-ENAMETOOLONG);
2483 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2485 yaffsfs_SetError(-ENOTDIR);
2486 else if( strlen(name) < 1)
2487 yaffsfs_SetError(-ENOENT);
2488 else if(parent->my_dev->read_only)
2489 yaffsfs_SetError(-EROFS);
2491 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2494 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL))
2495 yaffsfs_SetError(-EEXIST);
2497 yaffsfs_SetError(-ENOSPC);
2506 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2508 struct yaffs_obj *obj = NULL;
2509 struct yaffs_obj *dir = NULL;
2514 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2517 yaffsfs_SetError(-ENOTDIR);
2519 yaffsfs_SetError(-ENOENT);
2520 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2521 yaffsfs_SetError(-EINVAL);
2523 YCHAR *alias = obj->variant.symlink_variant.alias;
2524 memset(buf,0,bufsiz);
2525 yaffs_strncpy(buf,alias,bufsiz - 1);
2532 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2534 /* Creates a link called newpath to existing oldpath */
2535 struct yaffs_obj *obj = NULL;
2536 struct yaffs_obj *lnk = NULL;
2539 if(yaffsfs_CheckPath(linkpath) < 0){
2540 yaffsfs_SetError(-ENAMETOOLONG);
2546 obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL);
2547 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2550 yaffsfs_SetError(-ENOENT);
2551 else if(obj->my_dev->read_only)
2552 yaffsfs_SetError(-EINVAL);
2554 yaffsfs_SetError(-EEXIST);
2556 struct yaffs_obj *newdir = NULL;
2557 struct yaffs_obj *link = NULL;
2561 newdir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2564 yaffsfs_SetError(-ENOTDIR);
2565 else if(newdir->my_dev != obj->my_dev)
2566 yaffsfs_SetError(-EXDEV);
2568 retVal = yaffsfs_CheckNameLength(newname);
2571 link = yaffs_link_obj(newdir,newname,obj);
2575 yaffsfs_SetError(-ENOSPC);
2586 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2592 yaffsfs_SetError(-EINVAL);
2600 * Returns number of handles attached to the object
2602 int yaffs_n_handles(const YCHAR *path)
2604 struct yaffs_obj *obj;
2606 if(yaffsfs_CheckPath(path) < 0){
2607 yaffsfs_SetError(-ENAMETOOLONG);
2611 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2614 return yaffsfs_CountHandles(obj);
2619 int yaffs_get_error(void)
2621 return yaffsfs_GetLastError();
2624 int yaffs_set_error(int error)
2626 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2627 yaffsfs_SetError(error);
2631 int yaffs_dump_dev(const YCHAR *path)
2638 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2641 struct yaffs_dev *dev = obj->my_dev;
2644 "n_page_writes.......... %d\n"
2645 "n_page_reads........... %d\n"
2646 "n_erasures....... %d\n"
2647 "n_gc_copies............ %d\n"
2648 "garbageCollections... %d\n"
2649 "passiveGarbageColl'ns %d\n"
2655 dev->garbageCollections,
2656 dev->passiveGarbageCollections