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);
224 * Grab a handle (when opening a file)
227 static int yaffsfs_GetNewHandle(void)
232 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
233 h = yaffsfs_GetHandlePointer(i);
235 /* todo bug: should never happen */
238 memset(h,0,sizeof(yaffsfs_Handle));
249 * Increase use of handle when reading/writing a file
251 static int yaffsfs_GetHandle(int handle)
253 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
255 if(h && h->useCount > 0){
263 * Let go of a handle when closing a file or aborting an open or
264 * ending a read or write.
266 static int yaffsfs_PutHandle(int handle)
268 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
270 if(h && h->useCount > 0){
274 yaffsfs_PutInode(h->inodeId);
282 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
285 struct yaffs_obj *obj;
287 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
288 h = yaffsfs_GetHandlePointer(i);
289 obj = yaffsfs_GetHandleObject(i);
290 if(h && h->useCount>0 && obj && obj->my_dev == dev){
292 yaffsfs_PutInode(h->inodeId);
302 * Stuff to handle names.
306 int yaffsfs_Match(YCHAR a, YCHAR b)
312 int yaffsfs_IsPathDivider(YCHAR ch)
314 const YCHAR *str = YAFFS_PATH_DIVIDERS;
325 int yaffsfs_CheckNameLength(const char *name)
329 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
332 yaffsfs_SetError(-ENOENT);
334 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
335 yaffsfs_SetError(-ENAMETOOLONG);
342 LIST_HEAD(yaffsfs_deviceList);
347 * Scan the configuration list to find the device
348 * Curveballs: Should match paths that end in '/' too
349 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
351 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
353 struct list_head *cfg;
354 const YCHAR *leftOver;
356 struct yaffs_dev *retval = NULL;
357 struct yaffs_dev *dev = NULL;
359 int longestMatch = -1;
363 * Check all configs, choose the one that:
364 * 1) Actually matches a prefix (ie /a amd /abc will not match
365 * 2) Matches the longest.
367 list_for_each(cfg, &yaffsfs_deviceList){
368 dev = list_entry(cfg, struct yaffs_dev, dev_list);
375 while(matching && *p && *leftOver){
376 /* Skip over any /s */
377 while(yaffsfs_IsPathDivider(*p))
380 /* Skip over any /s */
381 while(yaffsfs_IsPathDivider(*leftOver))
384 /* Now match the text part */
386 *p && !yaffsfs_IsPathDivider(*p) &&
387 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
388 if(yaffsfs_Match(*p,*leftOver)){
398 /* Skip over any /s in leftOver */
399 while(yaffsfs_IsPathDivider(*leftOver))
402 // Skip over any /s in p
403 while(yaffsfs_IsPathDivider(*p))
406 // p should now be at the end of the string (ie. fully matched)
410 if( matching && (thisMatchLength > longestMatch))
413 *restOfPath = (YCHAR *)leftOver;
415 longestMatch = thisMatchLength;
422 static int yaffsfs_CheckPath(const YCHAR *path)
426 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
427 if(yaffsfs_IsPathDivider(*path)){
435 return (*path) ? -1 : 0;
438 /* FindMountPoint only returns a dev entry if the path is a mount point */
439 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
441 struct yaffs_dev *dev;
442 YCHAR *restOfPath=NULL;
443 dev = yaffsfs_FindDevice(path,&restOfPath);
444 if(dev && restOfPath && *restOfPath)
449 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
452 struct yaffs_dev *dev;
454 dev= yaffsfs_FindDevice(path,restOfPath);
455 if(dev && dev->is_mounted){
456 return dev->root_dir;
461 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
465 obj = yaffs_get_equivalent_obj(obj);
467 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
468 YCHAR *alias = obj->variant.symlink_variant.alias;
470 if(yaffsfs_IsPathDivider(*alias))
471 /* Starts with a /, need to scan from root up */
472 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL);
474 /* Relative to here, so use the parent of the symlink as a start */
475 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL);
482 * yaffsfs_FindDirectory
483 * Parse a path to determine the directory and the name within the directory.
485 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
487 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
488 const YCHAR *path, YCHAR **name, int symDepth)
490 struct yaffs_obj *dir;
492 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
495 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
500 restOfPath = (YCHAR *)path;
503 dir = yaffsfs_FindRoot(path,&restOfPath);
508 * curve ball: also throw away surplus '/'
509 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
511 while(yaffsfs_IsPathDivider(*restOfPath))
512 restOfPath++; /* get rid of '/' */
517 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
518 if (i < YAFFS_MAX_NAME_LENGTH){
519 str[i] = *restOfPath;
527 /* got to the end of the string */
530 if(yaffs_strcmp(str,_Y(".")) == 0)
534 else if(yaffs_strcmp(str,_Y("..")) == 0)
537 dir = yaffs_find_by_name(dir,str);
539 dir = yaffsfs_FollowLink(dir,symDepth);
541 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
546 /* directory did not exist. */
550 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
551 const YCHAR *path,YCHAR **name,int symDepth)
553 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
557 * yaffsfs_FindObject turns a path for an existing object into the object
559 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
560 const YCHAR *path,int symDepth, int getEquiv,
561 struct yaffs_obj **dirOut)
563 struct yaffs_obj *dir;
564 struct yaffs_obj *obj;
567 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
573 obj = yaffs_find_by_name(dir,name);
578 obj = yaffs_get_equivalent_obj(obj);
584 int yaffs_dup(int fd)
587 yaffsfs_Handle *oldPtr = NULL;
588 yaffsfs_Handle *newPtr = NULL;
592 oldPtr = yaffsfs_GetHandlePointer(fd);
593 if(oldPtr && oldPtr->useCount > 0)
594 newHandle = yaffsfs_GetNewHandle();
596 newPtr = yaffsfs_GetHandlePointer(newHandle);
604 yaffsfs_SetError(-EBADF);
606 yaffsfs_SetError(-ENOMEM);
614 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
616 struct yaffs_obj *obj = NULL;
617 struct yaffs_obj *dir = NULL;
620 yaffsfs_Handle *yh = NULL;
623 int errorReported = 0;
624 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
625 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
626 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
627 u8 sharedReadAllowed;
628 u8 sharedWriteAllowed;
634 if(yaffsfs_CheckPath(path) < 0){
635 yaffsfs_SetError(-ENAMETOOLONG);
639 /* O_EXCL only has meaning if O_CREAT is specified */
640 if(!(oflag & O_CREAT))
643 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
644 if( (oflag & O_CREAT) & (oflag & O_EXCL))
647 /* Todo: Are there any more flag combos to sanitise ? */
649 /* Figure out if reading or writing is requested */
651 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
652 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
656 handle = yaffsfs_GetNewHandle();
659 yaffsfs_SetError(-ENFILE);
663 yh = yaffsfs_GetHandlePointer(handle);
665 /* try to find the exisiting object */
666 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
668 obj = yaffsfs_FollowLink(obj,symDepth++);
671 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
672 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
678 /* The file already exists or it might be a directory */
680 /* If it is a directory then we can't open it as a file */
681 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
683 yaffsfs_SetError(-EISDIR);
687 /* Open should fail if O_CREAT and O_EXCL are specified since
690 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
692 yaffsfs_SetError(-EEXIST);
696 /* Check file permissions */
697 if( readRequested && !(obj->yst_mode & S_IREAD))
700 if( writeRequested && !(obj->yst_mode & S_IWRITE))
703 if(openDenied && !errorReported ) {
704 /* Error if the file exists but permissions are refused. */
705 yaffsfs_SetError(-EACCES);
709 /* Check sharing of an existing object. */
713 sharedReadAllowed = 1;
714 sharedWriteAllowed = 1;
717 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
718 hx = &yaffsfs_handle[i];
719 if(hx->useCount > 0 &&
721 yaffsfs_inode[hx->inodeId].iObj == obj){
723 sharedReadAllowed = 0;
725 sharedWriteAllowed = 0;
735 if((!sharedReadAllowed && readRequested)||
736 (!shareRead && alreadyReading) ||
737 (!sharedWriteAllowed && writeRequested) ||
738 (!shareWrite && alreadyWriting)){
740 yaffsfs_SetError(-EBUSY);
747 /* If we could not open an existing object, then let's see if
748 * the directory exists. If not, error.
750 if(!obj && !errorReported){
751 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
753 yaffsfs_SetError(-ENOTDIR);
758 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
759 /* Let's see if we can create this file if it does not exist. */
760 if(dir->my_dev->read_only){
761 yaffsfs_SetError(-EINVAL);
764 obj = yaffs_create_file(dir,name,mode,0,0);
766 if(!obj && !errorReported){
767 yaffsfs_SetError(-ENOSPC);
772 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
773 /* Error if the file does not exist and CREAT is not set. */
774 yaffsfs_SetError(-ENOENT);
778 if(obj && !openDenied) {
779 int inodeId = yaffsfs_GetInodeIdForObject(obj);
783 * Todo: Fix any problem if inodes run out, though that
784 * can't happen if the number of inode items >= number of handles.
788 yh->inodeId = inodeId;
789 yh->reading = readRequested;
790 yh->writing = writeRequested;
791 yh->append = (oflag & O_APPEND) ? 1 : 0;
793 yh->shareRead = shareRead;
794 yh->shareWrite = shareWrite;
796 /* Hook inode to object */
797 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
799 if((oflag & O_TRUNC) && yh->writing)
800 yaffs_resize_file(obj,0);
802 yaffsfs_PutHandle(handle);
804 yaffsfs_SetError(0); /* Problem */
814 int yaffs_open(const YCHAR *path, int oflag, int mode)
816 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
819 int yaffs_Dofsync(int fd,int datasync)
821 yaffsfs_Handle *h = NULL;
826 h = yaffsfs_GetHandlePointer(fd);
828 if(h && h->useCount > 0)
830 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
833 yaffsfs_SetError(-EBADF);
842 int yaffs_fsync(int fd)
844 return yaffs_Dofsync(fd,0);
847 int yaffs_flush(int fd)
849 return yaffs_fsync(fd);
852 int yaffs_fdatasync(int fd)
854 return yaffs_Dofsync(fd,1);
857 int yaffs_close(int fd)
859 yaffsfs_Handle *h = NULL;
864 h = yaffsfs_GetHandlePointer(fd);
866 if(h && h->useCount > 0) {
868 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
869 yaffsfs_PutHandle(fd);
873 yaffsfs_SetError(-EBADF);
884 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
886 yaffsfs_Handle *h = NULL;
887 struct yaffs_obj *obj = NULL;
894 unsigned int maxRead;
895 u8 *buf = (u8 *)vbuf;
898 h = yaffsfs_GetHandlePointer(fd);
899 obj = yaffsfs_GetHandleObject(fd);
903 yaffsfs_SetError(-EBADF);
905 } else if(!h->reading){
906 /* Not a reading handle */
907 yaffsfs_SetError(-EINVAL);
909 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
910 yaffsfs_SetError(-EINVAL);
916 startPos = h->position;
920 if(yaffs_get_obj_length(obj) > pos)
921 maxRead = yaffs_get_obj_length(obj) - pos;
929 yaffsfs_GetHandle(fd);
931 endPos = pos + nbyte;
933 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
934 nbyte > YAFFS_MAX_FILE_SIZE ||
935 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
941 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
946 * Need to reverify object in case the device was
947 * unmounted in another thread.
949 obj = yaffsfs_GetHandleObject(fd);
953 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
964 nbyte = 0; /* no more to read */
974 yaffsfs_PutHandle(fd);
978 h->position = startPos + totalRead;
980 yaffsfs_SetError(-EINVAL);
987 return (totalRead >= 0) ? totalRead : -1;
991 int yaffs_read(int fd, void *buf, unsigned int nbyte)
993 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
996 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
998 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1001 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1003 yaffsfs_Handle *h = NULL;
1004 struct yaffs_obj *obj = NULL;
1009 int totalWritten = 0;
1010 int write_trhrough = 0;
1012 const u8 *buf = (const u8 *)vbuf;
1015 h = yaffsfs_GetHandlePointer(fd);
1016 obj = yaffsfs_GetHandleObject(fd);
1020 yaffsfs_SetError(-EBADF);
1022 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1023 yaffsfs_SetError(-EINVAL);
1027 startPos = yaffs_get_obj_length(obj);
1031 startPos = h->position;
1033 yaffsfs_GetHandle(fd);
1035 endPos = pos + nbyte;
1037 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1038 nbyte > YAFFS_MAX_FILE_SIZE ||
1039 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1046 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1047 if(nToWrite > nbyte)
1051 * Need to reverify object in case the device was
1052 * remounted or unmounted in another thread.
1054 obj = yaffsfs_GetHandleObject(fd);
1055 if(!obj || obj->my_dev->read_only)
1058 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1061 totalWritten += nWritten;
1066 if(nWritten == nToWrite)
1071 if(nWritten < 1 && totalWritten < 1){
1072 yaffsfs_SetError(-ENOSPC);
1082 yaffsfs_PutHandle(fd);
1085 if(totalWritten > 0)
1086 h->position = startPos + totalWritten;
1088 yaffsfs_SetError(-EINVAL);
1094 return (totalWritten >= 0) ? totalWritten : -1;
1097 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1099 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1102 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1104 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1108 int yaffs_truncate(const YCHAR *path,off_t new_size)
1110 struct yaffs_obj *obj = NULL;
1111 struct yaffs_obj *dir = NULL;
1112 int result = YAFFS_FAIL;
1114 if(yaffsfs_CheckPath(path) < 0){
1115 yaffsfs_SetError(-ENAMETOOLONG);
1121 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1124 yaffsfs_SetError(-ENOTDIR);
1126 yaffsfs_SetError(-ENOENT);
1127 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1128 yaffsfs_SetError(-EISDIR);
1129 else if(obj->my_dev->read_only)
1130 yaffsfs_SetError(-EACCES);
1131 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1132 yaffsfs_SetError(-EINVAL);
1134 result = yaffs_resize_file(obj,new_size);
1138 return (result) ? 0 : -1;
1141 int yaffs_ftruncate(int fd, off_t new_size)
1143 yaffsfs_Handle *h = NULL;
1144 struct yaffs_obj *obj = NULL;
1148 h = yaffsfs_GetHandlePointer(fd);
1149 obj = yaffsfs_GetHandleObject(fd);
1153 yaffsfs_SetError(-EBADF);
1154 else if(obj->my_dev->read_only)
1155 yaffsfs_SetError(-EACCES);
1156 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1157 yaffsfs_SetError(-EINVAL);
1159 /* resize the file */
1160 result = yaffs_resize_file(obj,new_size);
1163 return (result) ? 0 : -1;
1167 off_t yaffs_lseek(int fd, off_t offset, int whence)
1169 yaffsfs_Handle *h = NULL;
1170 struct yaffs_obj *obj = NULL;
1175 h = yaffsfs_GetHandlePointer(fd);
1176 obj = yaffsfs_GetHandleObject(fd);
1179 yaffsfs_SetError(-EBADF);
1180 else if(offset > YAFFS_MAX_FILE_SIZE)
1181 yaffsfs_SetError(-EINVAL);
1183 if(whence == SEEK_SET){
1186 } else if(whence == SEEK_CUR) {
1187 if( (h->position + offset) >= 0)
1188 pos = (h->position + offset);
1189 } else if(whence == SEEK_END) {
1190 fSize = yaffs_get_obj_length(obj);
1191 if(fSize >= 0 && (fSize + offset) >= 0)
1192 pos = fSize + offset;
1195 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1198 yaffsfs_SetError(-EINVAL);
1209 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1211 struct yaffs_obj *dir = NULL;
1212 struct yaffs_obj *obj = NULL;
1214 int result = YAFFS_FAIL;
1216 if(yaffsfs_CheckPath(path) < 0){
1217 yaffsfs_SetError(-ENAMETOOLONG);
1223 obj = yaffsfs_FindObject(NULL,path,0,0,NULL);
1224 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1227 yaffsfs_SetError(-ENOTDIR);
1229 yaffsfs_SetError(-ENOENT);
1230 else if(obj->my_dev->read_only)
1231 yaffsfs_SetError(-EINVAL);
1232 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1233 yaffsfs_SetError(-EISDIR);
1234 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1235 yaffsfs_SetError(-ENOTDIR);
1237 result = yaffs_unlinker(dir,name);
1239 if(result == YAFFS_FAIL && isDirectory)
1240 yaffsfs_SetError(-ENOTEMPTY);
1245 return (result == YAFFS_FAIL) ? -1 : 0;
1249 int yaffs_unlink(const YCHAR *path)
1251 return yaffsfs_DoUnlink(path,0);
1254 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1256 struct yaffs_obj *olddir = NULL;
1257 struct yaffs_obj *newdir = NULL;
1258 struct yaffs_obj *obj = NULL;
1261 int result= YAFFS_FAIL;
1262 int rename_allowed = 1;
1266 if(yaffsfs_CheckPath(newPath) < 0){
1267 yaffsfs_SetError(-ENAMETOOLONG);
1271 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1272 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1273 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1275 if(!olddir || !newdir) {
1276 yaffsfs_SetError(-ENOTDIR);
1279 yaffsfs_SetError(-ENOENT);
1281 } else if(obj->my_dev->read_only){
1282 yaffsfs_SetError(-EROFS);
1284 } else if(olddir->my_dev != newdir->my_dev) {
1285 /* Rename must be on same device */
1286 yaffsfs_SetError(-EXDEV);
1288 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1290 * It is a directory, check that it is not being renamed to
1291 * being its own decendent.
1292 * Do this by tracing from the new directory back to the root, checking for obj
1295 struct yaffs_obj *xx = newdir;
1297 while( rename_allowed && xx){
1303 yaffsfs_SetError(-EINVAL);
1307 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1311 return (result == YAFFS_FAIL) ? -1 : 0;
1315 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1319 obj = yaffs_get_equivalent_obj(obj);
1322 buf->st_dev = (int)obj->my_dev->os_context;
1323 buf->st_ino = obj->obj_id;
1324 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1326 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1327 buf->st_mode |= S_IFDIR;
1328 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1329 buf->st_mode |= S_IFLNK;
1330 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1331 buf->st_mode |= S_IFREG;
1333 buf->st_nlink = yaffs_get_obj_link_count(obj);
1336 buf->st_rdev = obj->yst_rdev;
1337 buf->st_size = yaffs_get_obj_length(obj);
1338 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1339 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1340 #if CONFIG_YAFFS_WINCE
1341 buf->yst_wince_atime[0] = obj->win_atime[0];
1342 buf->yst_wince_atime[1] = obj->win_atime[1];
1343 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1344 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1345 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1346 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1348 buf->yst_atime = obj->yst_atime;
1349 buf->yst_ctime = obj->yst_ctime;
1350 buf->yst_mtime = obj->yst_mtime;
1357 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1359 struct yaffs_obj *obj=NULL;
1360 struct yaffs_obj *dir=NULL;
1364 if(yaffsfs_CheckPath(path) < 0){
1365 yaffsfs_SetError(-ENAMETOOLONG);
1371 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1374 obj = yaffsfs_FollowLink(obj,0);
1377 yaffsfs_SetError(-ENOTDIR);
1379 yaffsfs_SetError(-ENOENT);
1381 retVal = yaffsfs_DoStat(obj,buf);
1389 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1391 return yaffsfs_DoStatOrLStat(path,buf,0);
1394 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1396 return yaffsfs_DoStatOrLStat(path,buf,1);
1399 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1401 struct yaffs_obj *obj;
1406 obj = yaffsfs_GetHandleObject(fd);
1409 retVal = yaffsfs_DoStat(obj,buf);
1412 yaffsfs_SetError(-EBADF);
1419 #ifndef CONFIG_YAFFS_WINCE
1420 /* xattrib functions */
1423 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1425 struct yaffs_obj *obj;
1426 struct yaffs_obj *dir;
1430 if(yaffsfs_CheckPath(path) < 0){
1431 yaffsfs_SetError(-ENAMETOOLONG);
1437 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1440 obj = yaffsfs_FollowLink(obj,0);
1443 yaffsfs_SetError(-ENOTDIR);
1445 yaffsfs_SetError(-ENOENT);
1447 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1449 yaffsfs_SetError(retVal);
1460 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1462 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1465 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1467 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1472 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1474 struct yaffs_obj *obj;
1479 obj = yaffsfs_GetHandleObject(fd);
1482 yaffsfs_SetError(-EBADF);
1484 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1486 yaffsfs_SetError(retVal);
1496 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1498 struct yaffs_obj *obj;
1499 struct yaffs_obj *dir;
1503 if(yaffsfs_CheckPath(path) < 0){
1504 yaffsfs_SetError(-ENAMETOOLONG);
1510 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1513 obj = yaffsfs_FollowLink(obj,0);
1516 yaffsfs_SetError(-ENOTDIR);
1518 yaffsfs_SetError(-ENOENT);
1520 retVal = yaffs_get_xattrib(obj,name,data,size);
1522 yaffsfs_SetError(retVal);
1532 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1534 return yaffs_do_getxattr( path, name, data, size, 1);
1536 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1538 return yaffs_do_getxattr( path, name, data, size, 0);
1543 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1545 struct yaffs_obj *obj;
1550 obj = yaffsfs_GetHandleObject(fd);
1553 retVal = yaffs_get_xattrib(obj,name,data,size);
1555 yaffsfs_SetError(retVal);
1560 yaffsfs_SetError(-EBADF);
1567 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1569 struct yaffs_obj *obj=NULL;
1570 struct yaffs_obj *dir=NULL;
1574 if(yaffsfs_CheckPath(path) < 0){
1575 yaffsfs_SetError(-ENAMETOOLONG);
1581 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1584 obj = yaffsfs_FollowLink(obj,0);
1587 yaffsfs_SetError(-ENOTDIR);
1589 yaffsfs_SetError(-ENOENT);
1591 retVal = yaffs_list_xattrib(obj, data,size);
1593 yaffsfs_SetError(retVal);
1604 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1606 return yaffs_do_listxattr(path, data, size, 1);
1609 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1611 return yaffs_do_listxattr(path, data, size, 0);
1614 int yaffs_flistxattr(int fd, char *data, int size)
1616 struct yaffs_obj *obj;
1621 obj = yaffsfs_GetHandleObject(fd);
1624 retVal = yaffs_list_xattrib(obj,data,size);
1626 yaffsfs_SetError(retVal);
1631 yaffsfs_SetError(-EBADF);
1638 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1640 struct yaffs_obj *obj=NULL;
1641 struct yaffs_obj *dir=NULL;
1645 if(yaffsfs_CheckPath(path) < 0){
1646 yaffsfs_SetError(-ENAMETOOLONG);
1652 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1655 obj = yaffsfs_FollowLink(obj,0);
1658 yaffsfs_SetError(-ENOTDIR);
1660 yaffsfs_SetError(-ENOENT);
1662 retVal = yaffs_remove_xattrib(obj,name);
1664 yaffsfs_SetError(retVal);
1675 int yaffs_removexattr(const YCHAR *path, const char *name)
1677 return yaffs_do_removexattr(path, name, 1);
1680 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1682 return yaffs_do_removexattr(path, name, 0);
1685 int yaffs_fremovexattr(int fd, const char *name)
1687 struct yaffs_obj *obj;
1692 obj = yaffsfs_GetHandleObject(fd);
1695 retVal = yaffs_remove_xattrib(obj,name);
1697 yaffsfs_SetError(retVal);
1702 yaffsfs_SetError(-EBADF);
1710 #ifdef CONFIG_YAFFS_WINCE
1711 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1713 struct yaffs_obj *obj;
1718 obj = yaffsfs_GetHandleObject(fd);
1723 wctime[0] = obj->win_ctime[0];
1724 wctime[1] = obj->win_ctime[1];
1727 watime[0] = obj->win_atime[0];
1728 watime[1] = obj->win_atime[1];
1731 wmtime[0] = obj->win_mtime[0];
1732 wmtime[1] = obj->win_mtime[1];
1739 yaffsfs_SetError(-EBADF);
1747 int yaffs_set_wince_times(int fd,
1748 const unsigned *wctime,
1749 const unsigned *watime,
1750 const unsigned *wmtime)
1752 struct yaffs_obj *obj;
1757 obj = yaffsfs_GetHandleObject(fd);
1762 obj->win_ctime[0] = wctime[0];
1763 obj->win_ctime[1] = wctime[1];
1766 obj->win_atime[0] = watime[0];
1767 obj->win_atime[1] = watime[1];
1770 obj->win_mtime[0] = wmtime[0];
1771 obj->win_mtime[1] = wmtime[1];
1775 result = yaffs_flush_file(obj,0,0);
1779 yaffsfs_SetError(-EBADF);
1789 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1794 obj = yaffs_get_equivalent_obj(obj);
1797 obj->yst_mode = mode;
1799 result = yaffs_flush_file(obj,0,0);
1802 return result == YAFFS_OK ? 0 : -1;
1806 int yaffs_access(const YCHAR *path, int amode)
1808 struct yaffs_obj *obj=NULL;
1809 struct yaffs_obj *dir=NULL;
1813 if(yaffsfs_CheckPath(path) < 0){
1814 yaffsfs_SetError(-ENAMETOOLONG);
1818 if(amode & ~(R_OK | W_OK | X_OK)){
1819 yaffsfs_SetError(-EINVAL);
1825 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1828 yaffsfs_SetError(-ENOTDIR);
1830 yaffsfs_SetError(-ENOENT);
1834 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1836 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1838 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1842 yaffsfs_SetError(-EACCES);
1854 int yaffs_chmod(const YCHAR *path, mode_t mode)
1856 struct yaffs_obj *obj=NULL;
1857 struct yaffs_obj *dir=NULL;
1860 if(yaffsfs_CheckPath(path) < 0){
1861 yaffsfs_SetError(-ENAMETOOLONG);
1866 yaffsfs_SetError(-EINVAL);
1872 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1875 yaffsfs_SetError(-ENOTDIR);
1877 yaffsfs_SetError(-ENOENT);
1878 else if(obj->my_dev->read_only)
1879 yaffsfs_SetError(-EROFS);
1881 retVal = yaffsfs_DoChMod(obj,mode);
1890 int yaffs_fchmod(int fd, mode_t mode)
1892 struct yaffs_obj *obj;
1896 yaffsfs_SetError(-EINVAL);
1901 obj = yaffsfs_GetHandleObject(fd);
1904 yaffsfs_SetError(-EBADF);
1905 else if(obj->my_dev->read_only)
1906 yaffsfs_SetError(-EROFS);
1908 retVal = yaffsfs_DoChMod(obj,mode);
1916 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1918 YCHAR *alt_path = NULL;
1923 * We don't have a definition for max path length.
1924 * We will use 3 * max name length instead.
1927 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1929 /* If the last character is a path divider, then we need to
1930 * trim it back so that the name look-up works properly.
1931 * eg. /foo/new_dir/ -> /foo/newdir
1932 * Curveball: Need to handle multiple path dividers:
1933 * eg. /foof/sdfse///// -> /foo/sdfse
1935 if(path_length > 0 &&
1936 yaffsfs_IsPathDivider(path[path_length-1])){
1937 alt_path = YMALLOC(path_length + 1);
1940 strcpy(alt_path, path);
1941 for(i = path_length-1;
1942 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1944 alt_path[i] = (YCHAR) 0;
1946 *ret_path = alt_path;
1950 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1952 struct yaffs_obj *parent = NULL;
1953 struct yaffs_obj *dir = NULL;
1955 YCHAR *alt_path = NULL;
1958 if(yaffsfs_CheckPath(path) < 0){
1959 yaffsfs_SetError(-ENAMETOOLONG);
1963 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1964 yaffsfs_SetError(-ENOMEM);
1971 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1973 yaffsfs_SetError(-ENOTDIR);
1974 else if(parent && yaffs_strnlen(name,5) == 0){
1975 /* Trying to make the root itself */
1976 yaffsfs_SetError(-EEXIST);
1977 } else if(parent && parent->my_dev->read_only)
1978 yaffsfs_SetError(-EROFS);
1980 dir = yaffs_create_dir(parent,name,mode,0,0);
1983 else if (yaffs_find_by_name(parent,name))
1984 yaffsfs_SetError(-EEXIST); /* the name already exists */
1986 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1997 int yaffs_rmdir(const YCHAR *path)
2002 if(yaffsfs_CheckPath(path) < 0){
2003 yaffsfs_SetError(-ENAMETOOLONG);
2007 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2008 yaffsfs_SetError(-ENOMEM);
2013 result = yaffsfs_DoUnlink(path,1);
2020 void * yaffs_getdev(const YCHAR *path)
2022 struct yaffs_dev *dev=NULL;
2024 dev = yaffsfs_FindDevice(path,&dummy);
2028 int yaffs_mount2(const YCHAR *path,int read_only)
2031 int result=YAFFS_FAIL;
2032 struct yaffs_dev *dev=NULL;
2034 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2036 if(yaffsfs_CheckPath(path) < 0){
2037 yaffsfs_SetError(-ENAMETOOLONG);
2043 yaffsfs_InitHandles();
2045 dev = yaffsfs_FindMountPoint(path);
2047 if(!dev->is_mounted){
2048 dev->read_only = read_only ? 1 : 0;
2049 result = yaffs_guts_initialise(dev);
2050 if(result == YAFFS_FAIL)
2051 yaffsfs_SetError(-ENOMEM);
2052 retVal = result ? 0 : -1;
2056 yaffsfs_SetError(-EBUSY);
2058 yaffsfs_SetError(-ENODEV);
2065 int yaffs_mount(const YCHAR *path)
2067 return yaffs_mount2(path,0);
2070 int yaffs_sync(const YCHAR *path)
2073 struct yaffs_dev *dev=NULL;
2076 if(yaffsfs_CheckPath(path) < 0){
2077 yaffsfs_SetError(-ENAMETOOLONG);
2082 dev = yaffsfs_FindDevice(path,&dummy);
2084 if(dev->is_mounted){
2086 yaffs_flush_whole_cache(dev);
2087 yaffs_checkpoint_save(dev);
2091 yaffsfs_SetError(-EINVAL);
2094 yaffsfs_SetError(-ENODEV);
2101 int yaffs_remount(const YCHAR *path, int force, int read_only)
2104 struct yaffs_dev *dev=NULL;
2107 if(yaffsfs_CheckPath(path) < 0){
2108 yaffsfs_SetError(-ENAMETOOLONG);
2113 dev = yaffsfs_FindMountPoint(path);
2115 if(dev->is_mounted){
2119 yaffs_flush_whole_cache(dev);
2121 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2122 yh = & yaffsfs_handle[i];
2123 if(yh->useCount>0 &&
2124 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2125 inUse = 1; /* the device is in use, can't unmount */
2128 if(!inUse || force){
2130 yaffs_checkpoint_save(dev);
2131 dev->read_only = read_only ? 1 : 0;
2134 yaffsfs_SetError(-EBUSY);
2137 yaffsfs_SetError(-EINVAL);
2141 yaffsfs_SetError(-ENODEV);
2148 int yaffs_unmount2(const YCHAR *path, int force)
2151 struct yaffs_dev *dev=NULL;
2153 if(yaffsfs_CheckPath(path) < 0){
2154 yaffsfs_SetError(-ENAMETOOLONG);
2159 dev = yaffsfs_FindMountPoint(path);
2161 if(dev->is_mounted){
2165 yaffs_flush_whole_cache(dev);
2166 yaffs_checkpoint_save(dev);
2168 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2169 if(yaffsfs_handle[i].useCount > 0 &&
2170 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2171 inUse = 1; /* the device is in use, can't unmount */
2174 if(!inUse || force){
2176 yaffsfs_BreakDeviceHandles(dev);
2177 yaffs_deinitialise(dev);
2181 yaffsfs_SetError(-EBUSY);
2184 yaffsfs_SetError(-EINVAL);
2187 yaffsfs_SetError(-ENODEV);
2194 int yaffs_unmount(const YCHAR *path)
2196 return yaffs_unmount2(path,0);
2199 loff_t yaffs_freespace(const YCHAR *path)
2202 struct yaffs_dev *dev=NULL;
2205 if(yaffsfs_CheckPath(path) < 0){
2206 yaffsfs_SetError(-ENAMETOOLONG);
2211 dev = yaffsfs_FindDevice(path,&dummy);
2212 if(dev && dev->is_mounted){
2213 retVal = yaffs_get_n_free_chunks(dev);
2214 retVal *= dev->data_bytes_per_chunk;
2217 yaffsfs_SetError(-EINVAL);
2223 loff_t yaffs_totalspace(const YCHAR *path)
2226 struct yaffs_dev *dev=NULL;
2229 if(yaffsfs_CheckPath(path) < 0){
2230 yaffsfs_SetError(-ENAMETOOLONG);
2235 dev = yaffsfs_FindDevice(path,&dummy);
2236 if(dev && dev->is_mounted){
2237 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2238 dev->param.n_reserved_blocks;
2239 retVal *= dev->param.chunks_per_block;
2240 retVal *= dev->data_bytes_per_chunk;
2243 yaffsfs_SetError(-EINVAL);
2249 int yaffs_inodecount(const YCHAR *path)
2252 struct yaffs_dev *dev=NULL;
2255 if(yaffsfs_CheckPath(path) < 0){
2256 yaffsfs_SetError(-ENAMETOOLONG);
2261 dev = yaffsfs_FindDevice(path,&dummy);
2262 if(dev && dev->is_mounted) {
2263 int n_obj = dev->n_obj;
2264 if(n_obj > dev->n_hardlinks)
2265 retVal = n_obj - dev->n_hardlinks;
2269 yaffsfs_SetError(-EINVAL);
2276 void yaffs_add_device(struct yaffs_dev *dev)
2278 dev->is_mounted = 0;
2279 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2281 if(!dev->dev_list.next)
2282 INIT_LIST_HEAD(&dev->dev_list);
2284 list_add(&dev->dev_list,&yaffsfs_deviceList);
2287 void yaffs_remove_device(struct yaffs_dev *dev)
2289 list_del_init(&dev->dev_list);
2295 /* Directory search stuff. */
2298 * Directory search context
2300 * NB this is an opaque structure.
2307 yaffs_dirent de; /* directory entry being used by this dsc */
2308 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2309 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2310 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2312 struct list_head others;
2313 } yaffsfs_DirectorySearchContext;
2317 static struct list_head search_contexts;
2320 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2324 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2328 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2329 dsc->nextReturn = NULL;
2331 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2332 struct yaffs_obj,siblings);
2334 /* Hey someone isn't playing nice! */
2338 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2342 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2344 if( dsc->nextReturn == NULL ||
2345 list_empty(&dsc->dirObj->variant.dir_variant.children))
2346 dsc->nextReturn = NULL;
2348 struct list_head *next = dsc->nextReturn->siblings.next;
2350 if( next == &dsc->dirObj->variant.dir_variant.children)
2351 dsc->nextReturn = NULL; /* end of list */
2353 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2356 /* Hey someone isn't playing nice! */
2360 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2363 struct list_head *i;
2364 yaffsfs_DirectorySearchContext *dsc;
2366 /* if search contexts not initilised then skip */
2367 if(!search_contexts.next)
2370 /* Iterate through the directory search contexts.
2371 * If any are the one being removed, then advance the dsc to
2372 * the next one to prevent a hanging ptr.
2374 list_for_each(i, &search_contexts) {
2376 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2377 if(dsc->nextReturn == obj)
2378 yaffsfs_DirAdvance(dsc);
2384 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2386 yaffs_DIR *dir = NULL;
2387 struct yaffs_obj *obj = NULL;
2388 yaffsfs_DirectorySearchContext *dsc = NULL;
2390 if(yaffsfs_CheckPath(dirname) < 0){
2391 yaffsfs_SetError(-ENAMETOOLONG);
2397 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2399 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2401 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2402 dir = (yaffs_DIR *)dsc;
2405 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2406 dsc->magic = YAFFS_MAGIC;
2408 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2409 INIT_LIST_HEAD(&dsc->others);
2411 if(!search_contexts.next)
2412 INIT_LIST_HEAD(&search_contexts);
2414 list_add(&dsc->others,&search_contexts);
2415 yaffsfs_SetDirRewound(dsc);
2425 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2427 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2428 struct yaffs_dirent *retVal = NULL;
2432 if(dsc && dsc->magic == YAFFS_MAGIC){
2433 yaffsfs_SetError(0);
2434 if(dsc->nextReturn){
2435 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2436 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2437 dsc->de.d_off = dsc->offset++;
2438 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2439 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2441 /* this should not happen! */
2442 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2444 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2446 yaffsfs_DirAdvance(dsc);
2450 yaffsfs_SetError(-EBADF);
2459 void yaffs_rewinddir(yaffs_DIR *dirp)
2461 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2465 yaffsfs_SetDirRewound(dsc);
2471 int yaffs_closedir(yaffs_DIR *dirp)
2473 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2477 list_del(&dsc->others); /* unhook from list */
2483 /* End of directory stuff */
2486 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2488 struct yaffs_obj *parent = NULL;
2489 struct yaffs_obj *obj;
2492 int mode = 0; /* ignore for now */
2494 if(yaffsfs_CheckPath(newpath) < 0){
2495 yaffsfs_SetError(-ENAMETOOLONG);
2499 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2501 yaffsfs_SetError(-ENOTDIR);
2502 else if( strlen(name) < 1)
2503 yaffsfs_SetError(-ENOENT);
2504 else if(parent->my_dev->read_only)
2505 yaffsfs_SetError(-EROFS);
2507 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2510 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL))
2511 yaffsfs_SetError(-EEXIST);
2513 yaffsfs_SetError(-ENOSPC);
2522 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2524 struct yaffs_obj *obj = NULL;
2525 struct yaffs_obj *dir = NULL;
2530 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2533 yaffsfs_SetError(-ENOTDIR);
2535 yaffsfs_SetError(-ENOENT);
2536 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2537 yaffsfs_SetError(-EINVAL);
2539 YCHAR *alias = obj->variant.symlink_variant.alias;
2540 memset(buf,0,bufsiz);
2541 yaffs_strncpy(buf,alias,bufsiz - 1);
2548 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2550 /* Creates a link called newpath to existing oldpath */
2551 struct yaffs_obj *obj = NULL;
2552 struct yaffs_obj *lnk = NULL;
2553 struct yaffs_obj *obj_dir = NULL;
2554 struct yaffs_obj *lnk_dir = NULL;
2558 if(yaffsfs_CheckPath(linkpath) < 0){
2559 yaffsfs_SetError(-ENAMETOOLONG);
2565 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir);
2566 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2567 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2569 if(!obj_dir || !lnk_dir)
2570 yaffsfs_SetError(-ENOTDIR);
2572 yaffsfs_SetError(-ENOENT);
2573 else if(obj->my_dev->read_only)
2574 yaffsfs_SetError(-EINVAL);
2576 yaffsfs_SetError(-EEXIST);
2577 else if(lnk_dir->my_dev != obj->my_dev)
2578 yaffsfs_SetError(-EXDEV);
2580 retVal = yaffsfs_CheckNameLength(newname);
2583 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2587 yaffsfs_SetError(-ENOSPC);
2597 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2603 yaffsfs_SetError(-EINVAL);
2609 * D E B U G F U N C T I O N S
2614 * Returns number of handles attached to the object
2616 int yaffs_n_handles(const YCHAR *path)
2618 struct yaffs_obj *obj;
2620 if(yaffsfs_CheckPath(path) < 0){
2621 yaffsfs_SetError(-ENAMETOOLONG);
2625 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2628 return yaffsfs_CountHandles(obj);
2633 int yaffs_get_error(void)
2635 return yaffsfs_GetLastError();
2638 int yaffs_set_error(int error)
2640 yaffsfs_SetError(error);
2644 int yaffs_dump_dev(const YCHAR *path)
2651 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2654 struct yaffs_dev *dev = obj->my_dev;
2657 "n_page_writes.......... %d\n"
2658 "n_page_reads........... %d\n"
2659 "n_erasures....... %d\n"
2660 "n_gc_copies............ %d\n"
2661 "garbageCollections... %d\n"
2662 "passiveGarbageColl'ns %d\n"
2668 dev->garbageCollections,
2669 dev->passiveGarbageCollections