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_rmdir(const YCHAR *path)
1192 return yaffsfs_DoUnlink(path,1);
1195 int yaffs_unlink(const YCHAR *path)
1197 return yaffsfs_DoUnlink(path,0);
1200 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1202 struct yaffs_obj *olddir = NULL;
1203 struct yaffs_obj *newdir = NULL;
1204 struct yaffs_obj *obj = NULL;
1207 int result= YAFFS_FAIL;
1208 int rename_allowed = 1;
1212 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1213 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1214 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1216 if(!olddir || !newdir) {
1217 yaffsfs_SetError(-ENOTDIR);
1220 yaffsfs_SetError(-ENOENT);
1222 } else if(obj->my_dev->read_only){
1223 yaffsfs_SetError(-EROFS);
1225 } else if(olddir->my_dev != newdir->my_dev) {
1226 /* Rename must be on same device */
1227 yaffsfs_SetError(-EXDEV);
1229 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1231 * It is a directory, check that it is not being renamed to
1232 * being its own decendent.
1233 * Do this by tracing from the new directory back to the root, checking for obj
1236 struct yaffs_obj *xx = newdir;
1238 while( rename_allowed && xx){
1244 yaffsfs_SetError(-EINVAL);
1248 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1252 return (result == YAFFS_FAIL) ? -1 : 0;
1256 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1260 obj = yaffs_get_equivalent_obj(obj);
1263 buf->st_dev = (int)obj->my_dev->os_context;
1264 buf->st_ino = obj->obj_id;
1265 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1267 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1268 buf->st_mode |= S_IFDIR;
1269 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1270 buf->st_mode |= S_IFLNK;
1271 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1272 buf->st_mode |= S_IFREG;
1274 buf->st_nlink = yaffs_get_obj_link_count(obj);
1277 buf->st_rdev = obj->yst_rdev;
1278 buf->st_size = yaffs_get_obj_length(obj);
1279 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1280 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1281 #if CONFIG_YAFFS_WINCE
1282 buf->yst_wince_atime[0] = obj->win_atime[0];
1283 buf->yst_wince_atime[1] = obj->win_atime[1];
1284 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1285 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1286 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1287 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1289 buf->yst_atime = obj->yst_atime;
1290 buf->yst_ctime = obj->yst_ctime;
1291 buf->yst_mtime = obj->yst_mtime;
1298 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1300 struct yaffs_obj *obj=NULL;
1301 struct yaffs_obj *dir=NULL;
1307 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1310 obj = yaffsfs_FollowLink(obj,0);
1313 yaffsfs_SetError(-ENOTDIR);
1315 yaffsfs_SetError(-ENOENT);
1317 retVal = yaffsfs_DoStat(obj,buf);
1325 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1327 return yaffsfs_DoStatOrLStat(path,buf,0);
1330 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1332 return yaffsfs_DoStatOrLStat(path,buf,1);
1335 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1337 struct yaffs_obj *obj;
1342 obj = yaffsfs_GetHandleObject(fd);
1345 retVal = yaffsfs_DoStat(obj,buf);
1348 yaffsfs_SetError(-EBADF);
1355 #ifndef CONFIG_YAFFS_WINCE
1356 /* xattrib functions */
1359 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1361 struct yaffs_obj *obj;
1362 struct yaffs_obj *dir;
1368 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1371 obj = yaffsfs_FollowLink(obj,0);
1374 yaffsfs_SetError(-ENOTDIR);
1376 yaffsfs_SetError(-ENOENT);
1378 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1380 yaffsfs_SetError(retVal);
1391 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1393 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1396 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1398 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1403 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1405 struct yaffs_obj *obj;
1410 obj = yaffsfs_GetHandleObject(fd);
1413 yaffsfs_SetError(-EBADF);
1415 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1417 yaffsfs_SetError(retVal);
1427 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1429 struct yaffs_obj *obj;
1430 struct yaffs_obj *dir;
1436 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1439 obj = yaffsfs_FollowLink(obj,0);
1442 yaffsfs_SetError(-ENOTDIR);
1444 yaffsfs_SetError(-ENOENT);
1446 retVal = yaffs_get_xattrib(obj,name,data,size);
1448 yaffsfs_SetError(retVal);
1458 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1460 return yaffs_do_getxattr( path, name, data, size, 1);
1462 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1464 return yaffs_do_getxattr( path, name, data, size, 0);
1469 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1471 struct yaffs_obj *obj;
1476 obj = yaffsfs_GetHandleObject(fd);
1479 retVal = yaffs_get_xattrib(obj,name,data,size);
1481 yaffsfs_SetError(retVal);
1486 yaffsfs_SetError(-EBADF);
1493 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1495 struct yaffs_obj *obj=NULL;
1496 struct yaffs_obj *dir=NULL;
1502 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1505 obj = yaffsfs_FollowLink(obj,0);
1508 yaffsfs_SetError(-ENOTDIR);
1510 yaffsfs_SetError(-ENOENT);
1512 retVal = yaffs_list_xattrib(obj, data,size);
1514 yaffsfs_SetError(retVal);
1525 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1527 return yaffs_do_listxattr(path, data, size, 1);
1530 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1532 return yaffs_do_listxattr(path, data, size, 0);
1535 int yaffs_flistxattr(int fd, char *data, int size)
1537 struct yaffs_obj *obj;
1542 obj = yaffsfs_GetHandleObject(fd);
1545 retVal = yaffs_list_xattrib(obj,data,size);
1547 yaffsfs_SetError(retVal);
1552 yaffsfs_SetError(-EBADF);
1559 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1561 struct yaffs_obj *obj=NULL;
1562 struct yaffs_obj *dir=NULL;
1568 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1571 obj = yaffsfs_FollowLink(obj,0);
1574 yaffsfs_SetError(-ENOTDIR);
1576 yaffsfs_SetError(-ENOENT);
1578 retVal = yaffs_remove_xattrib(obj,name);
1580 yaffsfs_SetError(retVal);
1591 int yaffs_removexattr(const YCHAR *path, const char *name)
1593 return yaffs_do_removexattr(path, name, 1);
1596 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1598 return yaffs_do_removexattr(path, name, 0);
1601 int yaffs_fremovexattr(int fd, const char *name)
1603 struct yaffs_obj *obj;
1608 obj = yaffsfs_GetHandleObject(fd);
1611 retVal = yaffs_remove_xattrib(obj,name);
1613 yaffsfs_SetError(retVal);
1618 yaffsfs_SetError(-EBADF);
1626 #ifdef CONFIG_YAFFS_WINCE
1627 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1629 struct yaffs_obj *obj;
1634 obj = yaffsfs_GetHandleObject(fd);
1639 wctime[0] = obj->win_ctime[0];
1640 wctime[1] = obj->win_ctime[1];
1643 watime[0] = obj->win_atime[0];
1644 watime[1] = obj->win_atime[1];
1647 wmtime[0] = obj->win_mtime[0];
1648 wmtime[1] = obj->win_mtime[1];
1655 yaffsfs_SetError(-EBADF);
1663 int yaffs_set_wince_times(int fd,
1664 const unsigned *wctime,
1665 const unsigned *watime,
1666 const unsigned *wmtime)
1668 struct yaffs_obj *obj;
1673 obj = yaffsfs_GetHandleObject(fd);
1678 obj->win_ctime[0] = wctime[0];
1679 obj->win_ctime[1] = wctime[1];
1682 obj->win_atime[0] = watime[0];
1683 obj->win_atime[1] = watime[1];
1686 obj->win_mtime[0] = wmtime[0];
1687 obj->win_mtime[1] = wmtime[1];
1691 result = yaffs_flush_file(obj,0,0);
1695 yaffsfs_SetError(-EBADF);
1705 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1710 obj = yaffs_get_equivalent_obj(obj);
1713 obj->yst_mode = mode;
1715 result = yaffs_flush_file(obj,0,0);
1718 return result == YAFFS_OK ? 0 : -1;
1722 int yaffs_access(const YCHAR *path, int amode)
1724 struct yaffs_obj *obj=NULL;
1725 struct yaffs_obj *dir=NULL;
1729 if(amode & ~(R_OK | W_OK | X_OK)){
1730 yaffsfs_SetError(-EINVAL);
1736 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1739 yaffsfs_SetError(-ENOTDIR);
1741 yaffsfs_SetError(-ENOENT);
1745 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1747 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1749 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1753 yaffsfs_SetError(-EACCES);
1765 int yaffs_chmod(const YCHAR *path, mode_t mode)
1767 struct yaffs_obj *obj=NULL;
1768 struct yaffs_obj *dir=NULL;
1772 yaffsfs_SetError(-EINVAL);
1778 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1781 yaffsfs_SetError(-ENOTDIR);
1783 yaffsfs_SetError(-ENOENT);
1784 else if(obj->my_dev->read_only)
1785 yaffsfs_SetError(-EROFS);
1787 retVal = yaffsfs_DoChMod(obj,mode);
1796 int yaffs_fchmod(int fd, mode_t mode)
1798 struct yaffs_obj *obj;
1802 yaffsfs_SetError(-EINVAL);
1807 obj = yaffsfs_GetHandleObject(fd);
1810 yaffsfs_SetError(-EBADF);
1811 else if(obj->my_dev->read_only)
1812 yaffsfs_SetError(-EROFS);
1814 retVal = yaffsfs_DoChMod(obj,mode);
1822 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1824 struct yaffs_obj *parent = NULL;
1825 struct yaffs_obj *dir = NULL;
1827 YCHAR *use_path = NULL;
1828 int path_length = 0;
1834 * We don't have a definition for max path length.
1835 * We will use 3 * max name length instead.
1838 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1840 /* If the last character is a path divider, then we need to
1841 * trim it back so that the name look-up works properly.
1842 * eg. /foo/new_dir/ -> /foo/newdir
1843 * Curveball: Need to handle multiple path dividers:
1844 * eg. /foof/sdfse///// -> /foo/sdfse
1846 if(path_length > 0 &&
1847 yaffsfs_IsPathDivider(path[path_length-1])){
1848 use_path = YMALLOC(path_length + 1);
1850 yaffsfs_SetError(-ENOMEM);
1853 strcpy(use_path, path);
1854 for(i = path_length-1;
1855 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1857 use_path[i] = (YCHAR) 0;
1862 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1863 if(parent && yaffs_strnlen(name,5) == 0){
1864 /* Trying to make the root itself */
1865 yaffsfs_SetError(-EEXIST);
1866 } else if(parent && parent->my_dev->read_only){
1867 yaffsfs_SetError(-EINVAL);
1870 dir = yaffs_create_dir(parent,name,mode,0,0);
1875 yaffsfs_SetError(-ENOENT); /* missing path */
1876 else if (yaffs_find_by_name(parent,name))
1877 yaffsfs_SetError(-EEXIST); /* the name already exists */
1879 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1892 void * yaffs_getdev(const YCHAR *path)
1894 struct yaffs_dev *dev=NULL;
1896 dev = yaffsfs_FindDevice(path,&dummy);
1900 int yaffs_mount2(const YCHAR *path,int read_only)
1903 int result=YAFFS_FAIL;
1904 struct yaffs_dev *dev=NULL;
1906 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1910 yaffsfs_InitHandles();
1912 dev = yaffsfs_FindMountPoint(path);
1914 if(!dev->is_mounted){
1915 dev->read_only = read_only ? 1 : 0;
1916 result = yaffs_guts_initialise(dev);
1917 if(result == YAFFS_FAIL)
1918 /* todo error - mount failed */
1919 yaffsfs_SetError(-ENOMEM);
1920 retVal = result ? 0 : -1;
1924 /* todo error - already mounted. */
1925 yaffsfs_SetError(-EBUSY);
1927 /* todo error - no device */
1928 yaffsfs_SetError(-ENODEV);
1935 int yaffs_mount(const YCHAR *path)
1937 return yaffs_mount2(path,0);
1940 int yaffs_sync(const YCHAR *path)
1943 struct yaffs_dev *dev=NULL;
1947 dev = yaffsfs_FindDevice(path,&dummy);
1949 if(dev->is_mounted){
1951 yaffs_flush_whole_cache(dev);
1952 yaffs_checkpoint_save(dev);
1956 /* todo error - not mounted. */
1957 yaffsfs_SetError(-EINVAL);
1960 /* todo error - no device */
1961 yaffsfs_SetError(-ENODEV);
1968 int yaffs_remount(const YCHAR *path, int force, int read_only)
1971 struct yaffs_dev *dev=NULL;
1975 dev = yaffsfs_FindMountPoint(path);
1977 if(dev->is_mounted){
1981 yaffs_flush_whole_cache(dev);
1983 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1984 yh = & yaffsfs_handle[i];
1985 if(yh->useCount>0 &&
1986 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
1987 inUse = 1; /* the device is in use, can't unmount */
1990 if(!inUse || force){
1992 yaffs_checkpoint_save(dev);
1993 dev->read_only = read_only ? 1 : 0;
1996 yaffsfs_SetError(-EBUSY);
1999 yaffsfs_SetError(-EINVAL);
2003 yaffsfs_SetError(-ENODEV);
2010 int yaffs_unmount2(const YCHAR *path, int force)
2013 struct yaffs_dev *dev=NULL;
2016 dev = yaffsfs_FindMountPoint(path);
2018 if(dev->is_mounted){
2022 yaffs_flush_whole_cache(dev);
2023 yaffs_checkpoint_save(dev);
2025 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2026 if(yaffsfs_handle[i].useCount > 0 &&
2027 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2028 inUse = 1; /* the device is in use, can't unmount */
2031 if(!inUse || force){
2033 yaffsfs_PutDeviceHandles(dev);
2034 yaffs_deinitialise(dev);
2038 /* todo error can't unmount as files are open */
2039 yaffsfs_SetError(-EBUSY);
2042 /* todo error - not mounted. */
2043 yaffsfs_SetError(-EINVAL);
2046 /* todo error - no device */
2047 yaffsfs_SetError(-ENODEV);
2054 int yaffs_unmount(const YCHAR *path)
2056 return yaffs_unmount2(path,0);
2059 loff_t yaffs_freespace(const YCHAR *path)
2062 struct yaffs_dev *dev=NULL;
2066 dev = yaffsfs_FindDevice(path,&dummy);
2067 if(dev && dev->is_mounted){
2068 retVal = yaffs_get_n_free_chunks(dev);
2069 retVal *= dev->data_bytes_per_chunk;
2072 yaffsfs_SetError(-EINVAL);
2078 loff_t yaffs_totalspace(const YCHAR *path)
2081 struct yaffs_dev *dev=NULL;
2085 dev = yaffsfs_FindDevice(path,&dummy);
2086 if(dev && dev->is_mounted){
2087 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2088 retVal *= dev->param.chunks_per_block;
2089 retVal *= dev->data_bytes_per_chunk;
2092 yaffsfs_SetError(-EINVAL);
2098 int yaffs_inodecount(const YCHAR *path)
2101 struct yaffs_dev *dev=NULL;
2105 dev = yaffsfs_FindDevice(path,&dummy);
2106 if(dev && dev->is_mounted) {
2107 int n_obj = dev->n_obj;
2108 if(n_obj > dev->n_hardlinks)
2109 retVal = n_obj - dev->n_hardlinks;
2113 yaffsfs_SetError(-EINVAL);
2120 void yaffs_add_device(struct yaffs_dev *dev)
2122 dev->is_mounted = 0;
2123 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2125 if(!dev->dev_list.next)
2126 INIT_LIST_HEAD(&dev->dev_list);
2128 list_add(&dev->dev_list,&yaffsfs_deviceList);
2131 void yaffs_remove_device(struct yaffs_dev *dev)
2133 list_del_init(&dev->dev_list);
2139 /* Directory search stuff. */
2142 * Directory search context
2144 * NB this is an opaque structure.
2151 yaffs_dirent de; /* directory entry being used by this dsc */
2152 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2153 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2154 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2156 struct list_head others;
2157 } yaffsfs_DirectorySearchContext;
2161 static struct list_head search_contexts;
2164 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2168 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2172 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2173 dsc->nextReturn = NULL;
2175 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2176 struct yaffs_obj,siblings);
2178 /* Hey someone isn't playing nice! */
2182 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2186 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2188 if( dsc->nextReturn == NULL ||
2189 list_empty(&dsc->dirObj->variant.dir_variant.children))
2190 dsc->nextReturn = NULL;
2192 struct list_head *next = dsc->nextReturn->siblings.next;
2194 if( next == &dsc->dirObj->variant.dir_variant.children)
2195 dsc->nextReturn = NULL; /* end of list */
2197 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2200 /* Hey someone isn't playing nice! */
2204 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2207 struct list_head *i;
2208 yaffsfs_DirectorySearchContext *dsc;
2210 /* if search contexts not initilised then skip */
2211 if(!search_contexts.next)
2214 /* Iterate through the directory search contexts.
2215 * If any are the one being removed, then advance the dsc to
2216 * the next one to prevent a hanging ptr.
2218 list_for_each(i, &search_contexts) {
2220 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2221 if(dsc->nextReturn == obj)
2222 yaffsfs_DirAdvance(dsc);
2228 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2230 yaffs_DIR *dir = NULL;
2231 struct yaffs_obj *obj = NULL;
2232 yaffsfs_DirectorySearchContext *dsc = NULL;
2236 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2238 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2240 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2241 dir = (yaffs_DIR *)dsc;
2244 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2245 dsc->magic = YAFFS_MAGIC;
2247 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2248 INIT_LIST_HEAD(&dsc->others);
2250 if(!search_contexts.next)
2251 INIT_LIST_HEAD(&search_contexts);
2253 list_add(&dsc->others,&search_contexts);
2254 yaffsfs_SetDirRewound(dsc);
2264 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2266 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2267 struct yaffs_dirent *retVal = NULL;
2271 if(dsc && dsc->magic == YAFFS_MAGIC){
2272 yaffsfs_SetError(0);
2273 if(dsc->nextReturn){
2274 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2275 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2276 dsc->de.d_off = dsc->offset++;
2277 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2278 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2280 /* this should not happen! */
2281 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2283 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2285 yaffsfs_DirAdvance(dsc);
2289 yaffsfs_SetError(-EBADF);
2298 void yaffs_rewinddir(yaffs_DIR *dirp)
2300 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2304 yaffsfs_SetDirRewound(dsc);
2310 int yaffs_closedir(yaffs_DIR *dirp)
2312 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2316 list_del(&dsc->others); /* unhook from list */
2322 /* End of directory stuff */
2325 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2327 struct yaffs_obj *parent = NULL;
2328 struct yaffs_obj *obj;
2331 int mode = 0; /* ignore for now */
2334 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2335 if(parent && parent->my_dev->read_only)
2336 yaffsfs_SetError(-EINVAL);
2338 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2342 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2346 yaffsfs_SetError(-EINVAL);
2356 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2358 struct yaffs_obj *obj = NULL;
2359 struct yaffs_obj *dir = NULL;
2364 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2367 yaffsfs_SetError(-ENOTDIR);
2369 yaffsfs_SetError(-ENOENT);
2370 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2371 yaffsfs_SetError(-EINVAL);
2373 YCHAR *alias = obj->variant.symlink_variant.alias;
2374 memset(buf,0,bufsiz);
2375 yaffs_strncpy(buf,alias,bufsiz - 1);
2382 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2384 /* Creates a link called newpath to existing oldpath */
2385 struct yaffs_obj *obj = NULL;
2386 struct yaffs_obj *lnk = NULL;
2391 obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL);
2392 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2395 yaffsfs_SetError(-ENOENT);
2396 else if(obj->my_dev->read_only)
2397 yaffsfs_SetError(-EINVAL);
2399 yaffsfs_SetError(-EEXIST);
2401 struct yaffs_obj *newdir = NULL;
2402 struct yaffs_obj *link = NULL;
2406 newdir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2409 yaffsfs_SetError(-ENOTDIR);
2410 else if(newdir->my_dev != obj->my_dev)
2411 yaffsfs_SetError(-EXDEV);
2413 retVal = yaffsfs_CheckNameLength(newname);
2416 link = yaffs_link_obj(newdir,newname,obj);
2420 yaffsfs_SetError(-ENOSPC);
2431 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2443 * Returns number of handles attached to the object
2445 int yaffs_n_handles(const YCHAR *path)
2447 struct yaffs_obj *obj;
2449 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2451 return yaffsfs_CountHandles(obj);
2454 int yaffs_get_error(void)
2456 return yaffsfs_GetLastError();
2459 int yaffs_set_error(int error)
2461 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2462 yaffsfs_SetError(error);
2466 int yaffs_dump_dev(const YCHAR *path)
2473 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2476 struct yaffs_dev *dev = obj->my_dev;
2479 "n_page_writes.......... %d\n"
2480 "n_page_reads........... %d\n"
2481 "n_erasures....... %d\n"
2482 "n_gc_copies............ %d\n"
2483 "garbageCollections... %d\n"
2484 "passiveGarbageColl'ns %d\n"
2490 dev->garbageCollections,
2491 dev->passiveGarbageCollections