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 /* FindMountPoint only returns a dev entry if the path is a mount point */
417 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
419 struct yaffs_dev *dev;
420 YCHAR *restOfPath=NULL;
421 dev = yaffsfs_FindDevice(path,&restOfPath);
422 if(dev && restOfPath && *restOfPath)
427 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
430 struct yaffs_dev *dev;
432 dev= yaffsfs_FindDevice(path,restOfPath);
433 if(dev && dev->is_mounted){
434 return dev->root_dir;
439 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
443 obj = yaffs_get_equivalent_obj(obj);
445 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
446 YCHAR *alias = obj->variant.symlink_variant.alias;
448 if(yaffsfs_IsPathDivider(*alias))
449 /* Starts with a /, need to scan from root up */
450 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL);
452 /* Relative to here, so use the parent of the symlink as a start */
453 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL);
460 * yaffsfs_FindDirectory
461 * Parse a path to determine the directory and the name within the directory.
463 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
465 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
466 const YCHAR *path, YCHAR **name, int symDepth)
468 struct yaffs_obj *dir;
470 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
473 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
478 restOfPath = (YCHAR *)path;
481 dir = yaffsfs_FindRoot(path,&restOfPath);
486 * curve ball: also throw away surplus '/'
487 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
489 while(yaffsfs_IsPathDivider(*restOfPath))
490 restOfPath++; /* get rid of '/' */
495 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
496 if (i < YAFFS_MAX_NAME_LENGTH){
497 str[i] = *restOfPath;
505 /* got to the end of the string */
508 if(yaffs_strcmp(str,_Y(".")) == 0)
512 else if(yaffs_strcmp(str,_Y("..")) == 0)
515 dir = yaffs_find_by_name(dir,str);
517 dir = yaffsfs_FollowLink(dir,symDepth);
519 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
524 /* directory did not exist. */
528 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
529 const YCHAR *path,YCHAR **name,int symDepth)
531 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
535 * yaffsfs_FindObject turns a path for an existing object into the object
537 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
538 const YCHAR *path,int symDepth, int getEquiv,
539 struct yaffs_obj **dirOut)
541 struct yaffs_obj *dir;
542 struct yaffs_obj *obj;
545 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
551 obj = yaffs_find_by_name(dir,name);
556 obj = yaffs_get_equivalent_obj(obj);
562 int yaffs_dup(int fd)
565 yaffsfs_Handle *oldPtr = NULL;
566 yaffsfs_Handle *newPtr = NULL;
570 oldPtr = yaffsfs_GetHandlePointer(fd);
571 if(oldPtr && oldPtr->useCount > 0)
572 newHandle = yaffsfs_GetNewHandle();
574 newPtr = yaffsfs_GetHandlePointer(newHandle);
582 yaffsfs_SetError(-EBADF);
584 yaffsfs_SetError(-ENOMEM);
592 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
594 struct yaffs_obj *obj = NULL;
595 struct yaffs_obj *dir = NULL;
598 yaffsfs_Handle *yh = NULL;
601 int errorReported = 0;
602 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
603 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
604 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
605 u8 sharedReadAllowed;
606 u8 sharedWriteAllowed;
612 /* O_EXCL only has meaning if O_CREAT is specified */
613 if(!(oflag & O_CREAT))
616 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
617 if( (oflag & O_CREAT) & (oflag & O_EXCL))
620 /* Todo: Are there any more flag combos to sanitise ? */
622 /* Figure out if reading or writing is requested */
624 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
625 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
629 handle = yaffsfs_GetNewHandle();
632 yaffsfs_SetError(-ENFILE);
636 yh = yaffsfs_GetHandlePointer(handle);
638 /* try to find the exisiting object */
639 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
641 obj = yaffsfs_FollowLink(obj,symDepth++);
644 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
645 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
651 /* The file already exists or it might be a directory */
653 /* If it is a directory then we can't open it as a file */
654 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
656 yaffsfs_SetError(-EISDIR);
660 /* Open should fail if O_CREAT and O_EXCL are specified since
663 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
665 yaffsfs_SetError(-EEXIST);
669 /* Check file permissions */
670 if( readRequested && !(obj->yst_mode & S_IREAD))
673 if( writeRequested && !(obj->yst_mode & S_IWRITE))
676 if(openDenied && !errorReported ) {
677 /* Error if the file exists but permissions are refused. */
678 yaffsfs_SetError(-EACCES);
682 /* Check sharing of an existing object. */
686 sharedReadAllowed = 1;
687 sharedWriteAllowed = 1;
690 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
691 hx = &yaffsfs_handle[i];
692 if(hx->useCount > 0 &&
694 yaffsfs_inode[hx->inodeId].iObj == obj){
696 sharedReadAllowed = 0;
698 sharedWriteAllowed = 0;
708 if((!sharedReadAllowed && readRequested)||
709 (!shareRead && alreadyReading) ||
710 (!sharedWriteAllowed && writeRequested) ||
711 (!shareWrite && alreadyWriting)){
713 yaffsfs_SetError(-EBUSY);
720 /* If we could not open an existing object, then let's see if
721 * the directory exists. If not, error.
723 if(!obj && !errorReported){
724 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
726 yaffsfs_SetError(-ENOTDIR);
731 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
732 /* Let's see if we can create this file if it does not exist. */
733 if(dir->my_dev->read_only){
734 yaffsfs_SetError(-EINVAL);
737 obj = yaffs_create_file(dir,name,mode,0,0);
739 if(!obj && !errorReported){
740 yaffsfs_SetError(-ENOSPC);
745 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
746 /* Error if the file does not exist and CREAT is not set. */
747 yaffsfs_SetError(-ENOENT);
751 if(obj && !openDenied) {
752 int inodeId = yaffsfs_GetInodeIdForObject(obj);
756 * Todo: Fix any problem if inodes run out, though that
757 * can't happen if the number of inode items >= number of handles.
761 yh->inodeId = inodeId;
762 yh->reading = readRequested;
763 yh->writing = writeRequested;
764 yh->append = (oflag & O_APPEND) ? 1 : 0;
766 yh->shareRead = shareRead;
767 yh->shareWrite = shareWrite;
769 /* Hook inode to object */
770 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
772 if((oflag & O_TRUNC) && yh->writing)
773 yaffs_resize_file(obj,0);
775 yaffsfs_PutHandle(handle);
777 yaffsfs_SetError(0); /* Problem */
787 int yaffs_open(const YCHAR *path, int oflag, int mode)
789 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
792 int yaffs_Dofsync(int fd,int datasync)
794 yaffsfs_Handle *h = NULL;
799 h = yaffsfs_GetHandlePointer(fd);
801 if(h && h->useCount > 0)
803 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
806 yaffsfs_SetError(-EBADF);
815 int yaffs_fsync(int fd)
817 return yaffs_Dofsync(fd,0);
820 int yaffs_flush(int fd)
822 return yaffs_fsync(fd);
825 int yaffs_fdatasync(int fd)
827 return yaffs_Dofsync(fd,1);
830 int yaffs_close(int fd)
832 yaffsfs_Handle *h = NULL;
837 h = yaffsfs_GetHandlePointer(fd);
839 if(h && h->useCount > 0) {
841 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
842 yaffsfs_PutHandle(fd);
846 yaffsfs_SetError(-EBADF);
857 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
859 yaffsfs_Handle *h = NULL;
860 struct yaffs_obj *obj = NULL;
866 unsigned int maxRead;
867 u8 *buf = (u8 *)vbuf;
870 h = yaffsfs_GetHandlePointer(fd);
871 obj = yaffsfs_GetHandleObject(fd);
875 yaffsfs_SetError(-EBADF);
877 } else if(!h->reading){
878 /* Not a reading handle */
879 yaffsfs_SetError(-EINVAL);
885 startPos = h->position;
889 if(yaffs_get_obj_length(obj) > pos)
890 maxRead = yaffs_get_obj_length(obj) - pos;
898 yaffsfs_GetHandle(fd);
901 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
906 * Need to reverify object in case the device was
907 * unmounted in another thread.
909 obj = yaffsfs_GetHandleObject(fd);
913 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
924 nbyte = 0; /* no more to read */
934 yaffsfs_PutHandle(fd);
938 h->position = startPos + totalRead;
948 return (totalRead >= 0) ? totalRead : -1;
952 int yaffs_read(int fd, void *buf, unsigned int nbyte)
954 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
957 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
959 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
962 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
964 yaffsfs_Handle *h = NULL;
965 struct yaffs_obj *obj = NULL;
969 int totalWritten = 0;
970 int write_trhrough = 0;
972 const u8 *buf = (const u8 *)vbuf;
975 h = yaffsfs_GetHandlePointer(fd);
976 obj = yaffsfs_GetHandleObject(fd);
980 yaffsfs_SetError(-EBADF);
982 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
983 yaffsfs_SetError(-EINVAL);
987 startPos = yaffs_get_obj_length(obj);
991 startPos = h->position;
993 yaffsfs_GetHandle(fd);
997 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1002 * Need to reverify object in case the device was
1003 * remounted or unmounted in another thread.
1005 obj = yaffsfs_GetHandleObject(fd);
1006 if(!obj || obj->my_dev->read_only)
1009 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1012 totalWritten += nWritten;
1017 if(nWritten == nToWrite)
1022 if(nWritten < 1 && totalWritten < 1){
1023 yaffsfs_SetError(-ENOSPC);
1033 yaffsfs_PutHandle(fd);
1036 if(totalWritten > 0)
1037 h->position = startPos + totalWritten;
1046 return (totalWritten >= 0) ? totalWritten : -1;
1049 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1051 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1054 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1056 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1060 int yaffs_truncate(const YCHAR *path,off_t new_size)
1062 struct yaffs_obj *obj = NULL;
1063 struct yaffs_obj *dir = NULL;
1064 int result = YAFFS_FAIL;
1068 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1071 yaffsfs_SetError(-ENOTDIR);
1073 yaffsfs_SetError(-ENOENT);
1074 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1075 yaffsfs_SetError(-EISDIR);
1076 else if(obj->my_dev->read_only)
1077 yaffsfs_SetError(-EACCES);
1078 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1079 yaffsfs_SetError(-EINVAL);
1081 result = yaffs_resize_file(obj,new_size);
1085 return (result) ? 0 : -1;
1088 int yaffs_ftruncate(int fd, off_t new_size)
1090 yaffsfs_Handle *h = NULL;
1091 struct yaffs_obj *obj = NULL;
1095 h = yaffsfs_GetHandlePointer(fd);
1096 obj = yaffsfs_GetHandleObject(fd);
1100 yaffsfs_SetError(-EBADF);
1101 else if(obj->my_dev->read_only)
1102 yaffsfs_SetError(-EACCES);
1103 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1104 yaffsfs_SetError(-EINVAL);
1106 /* resize the file */
1107 result = yaffs_resize_file(obj,new_size);
1110 return (result) ? 0 : -1;
1114 off_t yaffs_lseek(int fd, off_t offset, int whence)
1116 yaffsfs_Handle *h = NULL;
1117 struct yaffs_obj *obj = NULL;
1122 h = yaffsfs_GetHandlePointer(fd);
1123 obj = yaffsfs_GetHandleObject(fd);
1127 yaffsfs_SetError(-EBADF);
1129 if(whence == SEEK_SET){
1132 } else if(whence == SEEK_CUR) {
1133 if( (h->position + offset) >= 0)
1134 pos = (h->position + offset);
1135 } else if(whence == SEEK_END) {
1136 fSize = yaffs_get_obj_length(obj);
1137 if(fSize >= 0 && (fSize + offset) >= 0)
1138 pos = fSize + offset;
1144 yaffsfs_SetError(-EINVAL);
1153 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1155 struct yaffs_obj *dir = NULL;
1156 struct yaffs_obj *obj = NULL;
1158 int result = YAFFS_FAIL;
1162 obj = yaffsfs_FindObject(NULL,path,0,0,NULL);
1163 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1166 yaffsfs_SetError(-ENOTDIR);
1168 yaffsfs_SetError(-ENOENT);
1169 else if(obj->my_dev->read_only)
1170 yaffsfs_SetError(-EINVAL);
1171 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1172 yaffsfs_SetError(-EISDIR);
1173 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1174 yaffsfs_SetError(-ENOTDIR);
1176 result = yaffs_unlinker(dir,name);
1178 if(result == YAFFS_FAIL && isDirectory)
1179 yaffsfs_SetError(-ENOTEMPTY);
1186 return (result == YAFFS_FAIL) ? -1 : 0;
1190 int yaffs_unlink(const YCHAR *path)
1192 return yaffsfs_DoUnlink(path,0);
1195 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1197 struct yaffs_obj *olddir = NULL;
1198 struct yaffs_obj *newdir = NULL;
1199 struct yaffs_obj *obj = NULL;
1202 int result= YAFFS_FAIL;
1203 int rename_allowed = 1;
1207 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1208 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1209 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1211 if(!olddir || !newdir) {
1212 yaffsfs_SetError(-ENOTDIR);
1215 yaffsfs_SetError(-ENOENT);
1217 } else if(obj->my_dev->read_only){
1218 yaffsfs_SetError(-EROFS);
1220 } else if(olddir->my_dev != newdir->my_dev) {
1221 /* Rename must be on same device */
1222 yaffsfs_SetError(-EXDEV);
1224 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1226 * It is a directory, check that it is not being renamed to
1227 * being its own decendent.
1228 * Do this by tracing from the new directory back to the root, checking for obj
1231 struct yaffs_obj *xx = newdir;
1233 while( rename_allowed && xx){
1239 yaffsfs_SetError(-EINVAL);
1243 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1247 return (result == YAFFS_FAIL) ? -1 : 0;
1251 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1255 obj = yaffs_get_equivalent_obj(obj);
1258 buf->st_dev = (int)obj->my_dev->os_context;
1259 buf->st_ino = obj->obj_id;
1260 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1262 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1263 buf->st_mode |= S_IFDIR;
1264 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1265 buf->st_mode |= S_IFLNK;
1266 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1267 buf->st_mode |= S_IFREG;
1269 buf->st_nlink = yaffs_get_obj_link_count(obj);
1272 buf->st_rdev = obj->yst_rdev;
1273 buf->st_size = yaffs_get_obj_length(obj);
1274 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1275 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1276 #if CONFIG_YAFFS_WINCE
1277 buf->yst_wince_atime[0] = obj->win_atime[0];
1278 buf->yst_wince_atime[1] = obj->win_atime[1];
1279 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1280 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1281 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1282 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1284 buf->yst_atime = obj->yst_atime;
1285 buf->yst_ctime = obj->yst_ctime;
1286 buf->yst_mtime = obj->yst_mtime;
1293 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1295 struct yaffs_obj *obj=NULL;
1296 struct yaffs_obj *dir=NULL;
1302 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1305 obj = yaffsfs_FollowLink(obj,0);
1308 yaffsfs_SetError(-ENOTDIR);
1310 yaffsfs_SetError(-ENOENT);
1312 retVal = yaffsfs_DoStat(obj,buf);
1320 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1322 return yaffsfs_DoStatOrLStat(path,buf,0);
1325 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1327 return yaffsfs_DoStatOrLStat(path,buf,1);
1330 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1332 struct yaffs_obj *obj;
1337 obj = yaffsfs_GetHandleObject(fd);
1340 retVal = yaffsfs_DoStat(obj,buf);
1343 yaffsfs_SetError(-EBADF);
1350 #ifndef CONFIG_YAFFS_WINCE
1351 /* xattrib functions */
1354 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1356 struct yaffs_obj *obj;
1357 struct yaffs_obj *dir;
1363 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1366 obj = yaffsfs_FollowLink(obj,0);
1369 yaffsfs_SetError(-ENOTDIR);
1371 yaffsfs_SetError(-ENOENT);
1373 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1375 yaffsfs_SetError(retVal);
1386 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1388 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1391 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1393 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1398 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1400 struct yaffs_obj *obj;
1405 obj = yaffsfs_GetHandleObject(fd);
1408 yaffsfs_SetError(-EBADF);
1410 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1412 yaffsfs_SetError(retVal);
1422 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1424 struct yaffs_obj *obj;
1425 struct yaffs_obj *dir;
1431 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1434 obj = yaffsfs_FollowLink(obj,0);
1437 yaffsfs_SetError(-ENOTDIR);
1439 yaffsfs_SetError(-ENOENT);
1441 retVal = yaffs_get_xattrib(obj,name,data,size);
1443 yaffsfs_SetError(retVal);
1453 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1455 return yaffs_do_getxattr( path, name, data, size, 1);
1457 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1459 return yaffs_do_getxattr( path, name, data, size, 0);
1464 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1466 struct yaffs_obj *obj;
1471 obj = yaffsfs_GetHandleObject(fd);
1474 retVal = yaffs_get_xattrib(obj,name,data,size);
1476 yaffsfs_SetError(retVal);
1481 yaffsfs_SetError(-EBADF);
1488 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1490 struct yaffs_obj *obj=NULL;
1491 struct yaffs_obj *dir=NULL;
1497 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1500 obj = yaffsfs_FollowLink(obj,0);
1503 yaffsfs_SetError(-ENOTDIR);
1505 yaffsfs_SetError(-ENOENT);
1507 retVal = yaffs_list_xattrib(obj, data,size);
1509 yaffsfs_SetError(retVal);
1520 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1522 return yaffs_do_listxattr(path, data, size, 1);
1525 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1527 return yaffs_do_listxattr(path, data, size, 0);
1530 int yaffs_flistxattr(int fd, char *data, int size)
1532 struct yaffs_obj *obj;
1537 obj = yaffsfs_GetHandleObject(fd);
1540 retVal = yaffs_list_xattrib(obj,data,size);
1542 yaffsfs_SetError(retVal);
1547 yaffsfs_SetError(-EBADF);
1554 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1556 struct yaffs_obj *obj=NULL;
1557 struct yaffs_obj *dir=NULL;
1563 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1566 obj = yaffsfs_FollowLink(obj,0);
1569 yaffsfs_SetError(-ENOTDIR);
1571 yaffsfs_SetError(-ENOENT);
1573 retVal = yaffs_remove_xattrib(obj,name);
1575 yaffsfs_SetError(retVal);
1586 int yaffs_removexattr(const YCHAR *path, const char *name)
1588 return yaffs_do_removexattr(path, name, 1);
1591 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1593 return yaffs_do_removexattr(path, name, 0);
1596 int yaffs_fremovexattr(int fd, const char *name)
1598 struct yaffs_obj *obj;
1603 obj = yaffsfs_GetHandleObject(fd);
1606 retVal = yaffs_remove_xattrib(obj,name);
1608 yaffsfs_SetError(retVal);
1613 yaffsfs_SetError(-EBADF);
1621 #ifdef CONFIG_YAFFS_WINCE
1622 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1624 struct yaffs_obj *obj;
1629 obj = yaffsfs_GetHandleObject(fd);
1634 wctime[0] = obj->win_ctime[0];
1635 wctime[1] = obj->win_ctime[1];
1638 watime[0] = obj->win_atime[0];
1639 watime[1] = obj->win_atime[1];
1642 wmtime[0] = obj->win_mtime[0];
1643 wmtime[1] = obj->win_mtime[1];
1650 yaffsfs_SetError(-EBADF);
1658 int yaffs_set_wince_times(int fd,
1659 const unsigned *wctime,
1660 const unsigned *watime,
1661 const unsigned *wmtime)
1663 struct yaffs_obj *obj;
1668 obj = yaffsfs_GetHandleObject(fd);
1673 obj->win_ctime[0] = wctime[0];
1674 obj->win_ctime[1] = wctime[1];
1677 obj->win_atime[0] = watime[0];
1678 obj->win_atime[1] = watime[1];
1681 obj->win_mtime[0] = wmtime[0];
1682 obj->win_mtime[1] = wmtime[1];
1686 result = yaffs_flush_file(obj,0,0);
1690 yaffsfs_SetError(-EBADF);
1700 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1705 obj = yaffs_get_equivalent_obj(obj);
1708 obj->yst_mode = mode;
1710 result = yaffs_flush_file(obj,0,0);
1713 return result == YAFFS_OK ? 0 : -1;
1717 int yaffs_access(const YCHAR *path, int amode)
1719 struct yaffs_obj *obj=NULL;
1720 struct yaffs_obj *dir=NULL;
1724 if(amode & ~(R_OK | W_OK | X_OK)){
1725 yaffsfs_SetError(-EINVAL);
1731 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1734 yaffsfs_SetError(-ENOTDIR);
1736 yaffsfs_SetError(-ENOENT);
1740 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1742 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1744 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1748 yaffsfs_SetError(-EACCES);
1760 int yaffs_chmod(const YCHAR *path, mode_t mode)
1762 struct yaffs_obj *obj=NULL;
1763 struct yaffs_obj *dir=NULL;
1767 yaffsfs_SetError(-EINVAL);
1773 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1776 yaffsfs_SetError(-ENOTDIR);
1778 yaffsfs_SetError(-ENOENT);
1779 else if(obj->my_dev->read_only)
1780 yaffsfs_SetError(-EROFS);
1782 retVal = yaffsfs_DoChMod(obj,mode);
1791 int yaffs_fchmod(int fd, mode_t mode)
1793 struct yaffs_obj *obj;
1797 yaffsfs_SetError(-EINVAL);
1802 obj = yaffsfs_GetHandleObject(fd);
1805 yaffsfs_SetError(-EBADF);
1806 else if(obj->my_dev->read_only)
1807 yaffsfs_SetError(-EROFS);
1809 retVal = yaffsfs_DoChMod(obj,mode);
1817 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1819 YCHAR *alt_path = NULL;
1824 * We don't have a definition for max path length.
1825 * We will use 3 * max name length instead.
1828 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1830 /* If the last character is a path divider, then we need to
1831 * trim it back so that the name look-up works properly.
1832 * eg. /foo/new_dir/ -> /foo/newdir
1833 * Curveball: Need to handle multiple path dividers:
1834 * eg. /foof/sdfse///// -> /foo/sdfse
1836 if(path_length > 0 &&
1837 yaffsfs_IsPathDivider(path[path_length-1])){
1838 alt_path = YMALLOC(path_length + 1);
1841 strcpy(alt_path, path);
1842 for(i = path_length-1;
1843 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1845 alt_path[i] = (YCHAR) 0;
1847 *ret_path = alt_path;
1851 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1853 struct yaffs_obj *parent = NULL;
1854 struct yaffs_obj *dir = NULL;
1856 YCHAR *alt_path = NULL;
1859 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1860 yaffsfs_SetError(-ENOMEM);
1867 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1869 yaffsfs_SetError(-ENOTDIR);
1870 else if(parent && yaffs_strnlen(name,5) == 0){
1871 /* Trying to make the root itself */
1872 yaffsfs_SetError(-EEXIST);
1873 } else if(parent && parent->my_dev->read_only)
1874 yaffsfs_SetError(-EROFS);
1876 dir = yaffs_create_dir(parent,name,mode,0,0);
1879 else if (yaffs_find_by_name(parent,name))
1880 yaffsfs_SetError(-EEXIST); /* the name already exists */
1882 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1893 int yaffs_rmdir(const YCHAR *path)
1898 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1899 yaffsfs_SetError(-ENOMEM);
1904 result = yaffsfs_DoUnlink(path,1);
1911 void * yaffs_getdev(const YCHAR *path)
1913 struct yaffs_dev *dev=NULL;
1915 dev = yaffsfs_FindDevice(path,&dummy);
1919 int yaffs_mount2(const YCHAR *path,int read_only)
1922 int result=YAFFS_FAIL;
1923 struct yaffs_dev *dev=NULL;
1925 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1929 yaffsfs_InitHandles();
1931 dev = yaffsfs_FindMountPoint(path);
1933 if(!dev->is_mounted){
1934 dev->read_only = read_only ? 1 : 0;
1935 result = yaffs_guts_initialise(dev);
1936 if(result == YAFFS_FAIL)
1937 /* todo error - mount failed */
1938 yaffsfs_SetError(-ENOMEM);
1939 retVal = result ? 0 : -1;
1943 /* todo error - already mounted. */
1944 yaffsfs_SetError(-EBUSY);
1946 /* todo error - no device */
1947 yaffsfs_SetError(-ENODEV);
1954 int yaffs_mount(const YCHAR *path)
1956 return yaffs_mount2(path,0);
1959 int yaffs_sync(const YCHAR *path)
1962 struct yaffs_dev *dev=NULL;
1966 dev = yaffsfs_FindDevice(path,&dummy);
1968 if(dev->is_mounted){
1970 yaffs_flush_whole_cache(dev);
1971 yaffs_checkpoint_save(dev);
1975 /* todo error - not mounted. */
1976 yaffsfs_SetError(-EINVAL);
1979 /* todo error - no device */
1980 yaffsfs_SetError(-ENODEV);
1987 int yaffs_remount(const YCHAR *path, int force, int read_only)
1990 struct yaffs_dev *dev=NULL;
1994 dev = yaffsfs_FindMountPoint(path);
1996 if(dev->is_mounted){
2000 yaffs_flush_whole_cache(dev);
2002 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2003 yh = & yaffsfs_handle[i];
2004 if(yh->useCount>0 &&
2005 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2006 inUse = 1; /* the device is in use, can't unmount */
2009 if(!inUse || force){
2011 yaffs_checkpoint_save(dev);
2012 dev->read_only = read_only ? 1 : 0;
2015 yaffsfs_SetError(-EBUSY);
2018 yaffsfs_SetError(-EINVAL);
2022 yaffsfs_SetError(-ENODEV);
2029 int yaffs_unmount2(const YCHAR *path, int force)
2032 struct yaffs_dev *dev=NULL;
2035 dev = yaffsfs_FindMountPoint(path);
2037 if(dev->is_mounted){
2041 yaffs_flush_whole_cache(dev);
2042 yaffs_checkpoint_save(dev);
2044 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2045 if(yaffsfs_handle[i].useCount > 0 &&
2046 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2047 inUse = 1; /* the device is in use, can't unmount */
2050 if(!inUse || force){
2052 yaffsfs_PutDeviceHandles(dev);
2053 yaffs_deinitialise(dev);
2057 /* todo error can't unmount as files are open */
2058 yaffsfs_SetError(-EBUSY);
2061 /* todo error - not mounted. */
2062 yaffsfs_SetError(-EINVAL);
2065 /* todo error - no device */
2066 yaffsfs_SetError(-ENODEV);
2073 int yaffs_unmount(const YCHAR *path)
2075 return yaffs_unmount2(path,0);
2078 loff_t yaffs_freespace(const YCHAR *path)
2081 struct yaffs_dev *dev=NULL;
2085 dev = yaffsfs_FindDevice(path,&dummy);
2086 if(dev && dev->is_mounted){
2087 retVal = yaffs_get_n_free_chunks(dev);
2088 retVal *= dev->data_bytes_per_chunk;
2091 yaffsfs_SetError(-EINVAL);
2097 loff_t yaffs_totalspace(const YCHAR *path)
2100 struct yaffs_dev *dev=NULL;
2104 dev = yaffsfs_FindDevice(path,&dummy);
2105 if(dev && dev->is_mounted){
2106 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2107 retVal *= dev->param.chunks_per_block;
2108 retVal *= dev->data_bytes_per_chunk;
2111 yaffsfs_SetError(-EINVAL);
2117 int yaffs_inodecount(const YCHAR *path)
2120 struct yaffs_dev *dev=NULL;
2124 dev = yaffsfs_FindDevice(path,&dummy);
2125 if(dev && dev->is_mounted) {
2126 int n_obj = dev->n_obj;
2127 if(n_obj > dev->n_hardlinks)
2128 retVal = n_obj - dev->n_hardlinks;
2132 yaffsfs_SetError(-EINVAL);
2139 void yaffs_add_device(struct yaffs_dev *dev)
2141 dev->is_mounted = 0;
2142 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2144 if(!dev->dev_list.next)
2145 INIT_LIST_HEAD(&dev->dev_list);
2147 list_add(&dev->dev_list,&yaffsfs_deviceList);
2150 void yaffs_remove_device(struct yaffs_dev *dev)
2152 list_del_init(&dev->dev_list);
2158 /* Directory search stuff. */
2161 * Directory search context
2163 * NB this is an opaque structure.
2170 yaffs_dirent de; /* directory entry being used by this dsc */
2171 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2172 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2173 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2175 struct list_head others;
2176 } yaffsfs_DirectorySearchContext;
2180 static struct list_head search_contexts;
2183 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2187 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2191 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2192 dsc->nextReturn = NULL;
2194 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2195 struct yaffs_obj,siblings);
2197 /* Hey someone isn't playing nice! */
2201 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2205 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2207 if( dsc->nextReturn == NULL ||
2208 list_empty(&dsc->dirObj->variant.dir_variant.children))
2209 dsc->nextReturn = NULL;
2211 struct list_head *next = dsc->nextReturn->siblings.next;
2213 if( next == &dsc->dirObj->variant.dir_variant.children)
2214 dsc->nextReturn = NULL; /* end of list */
2216 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2219 /* Hey someone isn't playing nice! */
2223 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2226 struct list_head *i;
2227 yaffsfs_DirectorySearchContext *dsc;
2229 /* if search contexts not initilised then skip */
2230 if(!search_contexts.next)
2233 /* Iterate through the directory search contexts.
2234 * If any are the one being removed, then advance the dsc to
2235 * the next one to prevent a hanging ptr.
2237 list_for_each(i, &search_contexts) {
2239 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2240 if(dsc->nextReturn == obj)
2241 yaffsfs_DirAdvance(dsc);
2247 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2249 yaffs_DIR *dir = NULL;
2250 struct yaffs_obj *obj = NULL;
2251 yaffsfs_DirectorySearchContext *dsc = NULL;
2255 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2257 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2259 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2260 dir = (yaffs_DIR *)dsc;
2263 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2264 dsc->magic = YAFFS_MAGIC;
2266 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2267 INIT_LIST_HEAD(&dsc->others);
2269 if(!search_contexts.next)
2270 INIT_LIST_HEAD(&search_contexts);
2272 list_add(&dsc->others,&search_contexts);
2273 yaffsfs_SetDirRewound(dsc);
2283 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2285 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2286 struct yaffs_dirent *retVal = NULL;
2290 if(dsc && dsc->magic == YAFFS_MAGIC){
2291 yaffsfs_SetError(0);
2292 if(dsc->nextReturn){
2293 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2294 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2295 dsc->de.d_off = dsc->offset++;
2296 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2297 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2299 /* this should not happen! */
2300 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2302 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2304 yaffsfs_DirAdvance(dsc);
2308 yaffsfs_SetError(-EBADF);
2317 void yaffs_rewinddir(yaffs_DIR *dirp)
2319 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2323 yaffsfs_SetDirRewound(dsc);
2329 int yaffs_closedir(yaffs_DIR *dirp)
2331 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2335 list_del(&dsc->others); /* unhook from list */
2341 /* End of directory stuff */
2344 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2346 struct yaffs_obj *parent = NULL;
2347 struct yaffs_obj *obj;
2350 int mode = 0; /* ignore for now */
2353 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2355 yaffsfs_SetError(-ENOTDIR);
2356 else if( strlen(name) < 1)
2357 yaffsfs_SetError(-ENOENT);
2358 else if(parent->my_dev->read_only)
2359 yaffsfs_SetError(-EROFS);
2361 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2365 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2376 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2378 struct yaffs_obj *obj = NULL;
2379 struct yaffs_obj *dir = NULL;
2384 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2387 yaffsfs_SetError(-ENOTDIR);
2389 yaffsfs_SetError(-ENOENT);
2390 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2391 yaffsfs_SetError(-EINVAL);
2393 YCHAR *alias = obj->variant.symlink_variant.alias;
2394 memset(buf,0,bufsiz);
2395 yaffs_strncpy(buf,alias,bufsiz - 1);
2402 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2404 /* Creates a link called newpath to existing oldpath */
2405 struct yaffs_obj *obj = NULL;
2406 struct yaffs_obj *lnk = NULL;
2411 obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL);
2412 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2415 yaffsfs_SetError(-ENOENT);
2416 else if(obj->my_dev->read_only)
2417 yaffsfs_SetError(-EINVAL);
2419 yaffsfs_SetError(-EEXIST);
2421 struct yaffs_obj *newdir = NULL;
2422 struct yaffs_obj *link = NULL;
2426 newdir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2429 yaffsfs_SetError(-ENOTDIR);
2430 else if(newdir->my_dev != obj->my_dev)
2431 yaffsfs_SetError(-EXDEV);
2433 retVal = yaffsfs_CheckNameLength(newname);
2436 link = yaffs_link_obj(newdir,newname,obj);
2440 yaffsfs_SetError(-ENOSPC);
2451 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2463 * Returns number of handles attached to the object
2465 int yaffs_n_handles(const YCHAR *path)
2467 struct yaffs_obj *obj;
2469 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2471 return yaffsfs_CountHandles(obj);
2474 int yaffs_get_error(void)
2476 return yaffsfs_GetLastError();
2479 int yaffs_set_error(int error)
2481 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2482 yaffsfs_SetError(error);
2486 int yaffs_dump_dev(const YCHAR *path)
2493 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2496 struct yaffs_dev *dev = obj->my_dev;
2499 "n_page_writes.......... %d\n"
2500 "n_page_reads........... %d\n"
2501 "n_erasures....... %d\n"
2502 "n_gc_copies............ %d\n"
2503 "garbageCollections... %d\n"
2504 "passiveGarbageColl'ns %d\n"
2510 dev->garbageCollections,
2511 dev->passiveGarbageCollections