2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
20 #include <string.h> /* for memset */
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
36 int symDepth, int getEquiv,
37 struct yaffs_obj **dirOut);
39 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
41 unsigned int yaffs_wr_attempts;
45 * There are open inodes in yaffsfs_Inode.
46 * There are open handles in yaffsfs_Handle.
48 * Things are structured this way to be like the Linux VFS model
49 * so that interactions with the yaffs guts calls are similar.
50 * That means more common code paths and less special code.
51 * That means better testing etc.
55 int count; /* Number of handles accessing this inode */
56 struct yaffs_obj *iObj;
65 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
66 int useCount:10; /* Use count for this handle */
67 u32 position; /* current position in file */
70 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
71 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
72 static int yaffsfs_handlesInitialised;
75 unsigned yaffs_set_trace(unsigned tm)
77 yaffs_trace_mask = tm;
78 return yaffs_trace_mask;
81 unsigned yaffs_get_trace(void)
83 return yaffs_trace_mask;
88 * Inilitalise handle management on start-up.
91 static void yaffsfs_InitHandles(void)
94 if(yaffsfs_handlesInitialised)
97 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
98 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
99 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
100 yaffsfs_handle[i].inodeId = -1;
103 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
105 if(h < 0 || h >= YAFFSFS_N_HANDLES)
108 return &yaffsfs_handle[h];
111 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
113 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
115 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
116 return &yaffsfs_inode[h->inodeId];
121 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
123 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
132 * yaffsfs_FindInodeIdForObject
133 * Find the inode entry for an object, if it exists.
136 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
142 obj = yaffs_get_equivalent_obj(obj);
144 /* Look for it in open inode table*/
145 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
146 if(yaffsfs_inode[i].iObj == obj)
153 * yaffsfs_GetInodeIdForObject
154 * Grab an inode entry when opening a new inode.
156 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
160 yaffsfs_Inode *in = NULL;
163 obj = yaffs_get_equivalent_obj(obj);
165 ret = yaffsfs_FindInodeIdForObject(obj);
167 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
168 if(!yaffsfs_inode[i].iObj)
173 in = &yaffsfs_inode[ret];
185 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
187 int i = yaffsfs_FindInodeIdForObject(obj);
190 return yaffsfs_inode[i].count;
195 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
197 struct yaffs_obj *obj;
204 obj->my_inode = NULL;
209 static void yaffsfs_PutInode(int inodeId)
211 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
212 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
215 yaffsfs_ReleaseInode(in);
222 * Grab a handle (when opening a file)
225 static int yaffsfs_GetNewHandle(void)
230 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
231 h = yaffsfs_GetHandlePointer(i);
233 /* todo bug: should never happen */
236 memset(h,0,sizeof(yaffsfs_Handle));
247 * Increase use of handle when reading/writing a file
249 static int yaffsfs_GetHandle(int handle)
251 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
253 if(h && h->useCount > 0){
261 * Let go of a handle when closing a file or aborting an open or
262 * ending a read or write.
264 static int yaffsfs_PutHandle(int handle)
266 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
268 if(h && h->useCount > 0){
272 yaffsfs_PutInode(h->inodeId);
280 static void yaffsfs_PutDeviceHandles(struct yaffs_dev *dev)
284 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
285 yh = & yaffsfs_handle[i];
287 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
288 yaffsfs_PutHandle(i);
296 * Stuff to handle names.
300 int yaffsfs_Match(YCHAR a, YCHAR b)
306 int yaffsfs_IsPathDivider(YCHAR ch)
308 const YCHAR *str = YAFFS_PATH_DIVIDERS;
319 int yaffsfs_CheckNameLength(const char *name)
323 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
326 yaffsfs_SetError(-ENOENT);
328 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
329 yaffsfs_SetError(-ENAMETOOLONG);
336 LIST_HEAD(yaffsfs_deviceList);
341 * Scan the configuration list to find the device
342 * Curveballs: Should match paths that end in '/' too
343 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
345 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
347 struct list_head *cfg;
348 const YCHAR *leftOver;
350 struct yaffs_dev *retval = NULL;
351 struct yaffs_dev *dev = NULL;
353 int longestMatch = -1;
357 * Check all configs, choose the one that:
358 * 1) Actually matches a prefix (ie /a amd /abc will not match
359 * 2) Matches the longest.
361 list_for_each(cfg, &yaffsfs_deviceList){
362 dev = list_entry(cfg, struct yaffs_dev, dev_list);
369 while(matching && *p && *leftOver){
370 /* Skip over any /s */
371 while(yaffsfs_IsPathDivider(*p))
374 /* Skip over any /s */
375 while(yaffsfs_IsPathDivider(*leftOver))
378 /* Now match the text part */
380 *p && !yaffsfs_IsPathDivider(*p) &&
381 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
382 if(yaffsfs_Match(*p,*leftOver)){
392 /* Skip over any /s in leftOver */
393 while(yaffsfs_IsPathDivider(*leftOver))
396 // Skip over any /s in p
397 while(yaffsfs_IsPathDivider(*p))
400 // p should now be at the end of the string (ie. fully matched)
404 if( matching && (thisMatchLength > longestMatch))
407 *restOfPath = (YCHAR *)leftOver;
409 longestMatch = thisMatchLength;
416 static int yaffsfs_CheckPath(const YCHAR *path)
420 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
421 if(yaffsfs_IsPathDivider(*path)){
429 return (*path) ? -1 : 0;
432 /* FindMountPoint only returns a dev entry if the path is a mount point */
433 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
435 struct yaffs_dev *dev;
436 YCHAR *restOfPath=NULL;
437 dev = yaffsfs_FindDevice(path,&restOfPath);
438 if(dev && restOfPath && *restOfPath)
443 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
446 struct yaffs_dev *dev;
448 dev= yaffsfs_FindDevice(path,restOfPath);
449 if(dev && dev->is_mounted){
450 return dev->root_dir;
455 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
459 obj = yaffs_get_equivalent_obj(obj);
461 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
462 YCHAR *alias = obj->variant.symlink_variant.alias;
464 if(yaffsfs_IsPathDivider(*alias))
465 /* Starts with a /, need to scan from root up */
466 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL);
468 /* Relative to here, so use the parent of the symlink as a start */
469 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL);
476 * yaffsfs_FindDirectory
477 * Parse a path to determine the directory and the name within the directory.
479 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
481 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
482 const YCHAR *path, YCHAR **name, int symDepth)
484 struct yaffs_obj *dir;
486 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
489 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
494 restOfPath = (YCHAR *)path;
497 dir = yaffsfs_FindRoot(path,&restOfPath);
502 * curve ball: also throw away surplus '/'
503 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
505 while(yaffsfs_IsPathDivider(*restOfPath))
506 restOfPath++; /* get rid of '/' */
511 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
512 if (i < YAFFS_MAX_NAME_LENGTH){
513 str[i] = *restOfPath;
521 /* got to the end of the string */
524 if(yaffs_strcmp(str,_Y(".")) == 0)
528 else if(yaffs_strcmp(str,_Y("..")) == 0)
531 dir = yaffs_find_by_name(dir,str);
533 dir = yaffsfs_FollowLink(dir,symDepth);
535 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
540 /* directory did not exist. */
544 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
545 const YCHAR *path,YCHAR **name,int symDepth)
547 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
551 * yaffsfs_FindObject turns a path for an existing object into the object
553 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
554 const YCHAR *path,int symDepth, int getEquiv,
555 struct yaffs_obj **dirOut)
557 struct yaffs_obj *dir;
558 struct yaffs_obj *obj;
561 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
567 obj = yaffs_find_by_name(dir,name);
572 obj = yaffs_get_equivalent_obj(obj);
578 int yaffs_dup(int fd)
581 yaffsfs_Handle *oldPtr = NULL;
582 yaffsfs_Handle *newPtr = NULL;
586 oldPtr = yaffsfs_GetHandlePointer(fd);
587 if(oldPtr && oldPtr->useCount > 0)
588 newHandle = yaffsfs_GetNewHandle();
590 newPtr = yaffsfs_GetHandlePointer(newHandle);
598 yaffsfs_SetError(-EBADF);
600 yaffsfs_SetError(-ENOMEM);
608 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
610 struct yaffs_obj *obj = NULL;
611 struct yaffs_obj *dir = NULL;
614 yaffsfs_Handle *yh = NULL;
617 int errorReported = 0;
618 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
619 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
620 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
621 u8 sharedReadAllowed;
622 u8 sharedWriteAllowed;
628 if(yaffsfs_CheckPath(path) < 0){
629 yaffsfs_SetError(-ENAMETOOLONG);
633 /* O_EXCL only has meaning if O_CREAT is specified */
634 if(!(oflag & O_CREAT))
637 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
638 if( (oflag & O_CREAT) & (oflag & O_EXCL))
641 /* Todo: Are there any more flag combos to sanitise ? */
643 /* Figure out if reading or writing is requested */
645 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
646 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
650 handle = yaffsfs_GetNewHandle();
653 yaffsfs_SetError(-ENFILE);
657 yh = yaffsfs_GetHandlePointer(handle);
659 /* try to find the exisiting object */
660 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
662 obj = yaffsfs_FollowLink(obj,symDepth++);
665 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
666 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
672 /* The file already exists or it might be a directory */
674 /* If it is a directory then we can't open it as a file */
675 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
677 yaffsfs_SetError(-EISDIR);
681 /* Open should fail if O_CREAT and O_EXCL are specified since
684 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
686 yaffsfs_SetError(-EEXIST);
690 /* Check file permissions */
691 if( readRequested && !(obj->yst_mode & S_IREAD))
694 if( writeRequested && !(obj->yst_mode & S_IWRITE))
697 if(openDenied && !errorReported ) {
698 /* Error if the file exists but permissions are refused. */
699 yaffsfs_SetError(-EACCES);
703 /* Check sharing of an existing object. */
707 sharedReadAllowed = 1;
708 sharedWriteAllowed = 1;
711 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
712 hx = &yaffsfs_handle[i];
713 if(hx->useCount > 0 &&
715 yaffsfs_inode[hx->inodeId].iObj == obj){
717 sharedReadAllowed = 0;
719 sharedWriteAllowed = 0;
729 if((!sharedReadAllowed && readRequested)||
730 (!shareRead && alreadyReading) ||
731 (!sharedWriteAllowed && writeRequested) ||
732 (!shareWrite && alreadyWriting)){
734 yaffsfs_SetError(-EBUSY);
741 /* If we could not open an existing object, then let's see if
742 * the directory exists. If not, error.
744 if(!obj && !errorReported){
745 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
747 yaffsfs_SetError(-ENOTDIR);
752 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
753 /* Let's see if we can create this file if it does not exist. */
754 if(dir->my_dev->read_only){
755 yaffsfs_SetError(-EINVAL);
758 obj = yaffs_create_file(dir,name,mode,0,0);
760 if(!obj && !errorReported){
761 yaffsfs_SetError(-ENOSPC);
766 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
767 /* Error if the file does not exist and CREAT is not set. */
768 yaffsfs_SetError(-ENOENT);
772 if(obj && !openDenied) {
773 int inodeId = yaffsfs_GetInodeIdForObject(obj);
777 * Todo: Fix any problem if inodes run out, though that
778 * can't happen if the number of inode items >= number of handles.
782 yh->inodeId = inodeId;
783 yh->reading = readRequested;
784 yh->writing = writeRequested;
785 yh->append = (oflag & O_APPEND) ? 1 : 0;
787 yh->shareRead = shareRead;
788 yh->shareWrite = shareWrite;
790 /* Hook inode to object */
791 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
793 if((oflag & O_TRUNC) && yh->writing)
794 yaffs_resize_file(obj,0);
796 yaffsfs_PutHandle(handle);
798 yaffsfs_SetError(0); /* Problem */
808 int yaffs_open(const YCHAR *path, int oflag, int mode)
810 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
813 int yaffs_Dofsync(int fd,int datasync)
815 yaffsfs_Handle *h = NULL;
820 h = yaffsfs_GetHandlePointer(fd);
822 if(h && h->useCount > 0)
824 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
827 yaffsfs_SetError(-EBADF);
836 int yaffs_fsync(int fd)
838 return yaffs_Dofsync(fd,0);
841 int yaffs_flush(int fd)
843 return yaffs_fsync(fd);
846 int yaffs_fdatasync(int fd)
848 return yaffs_Dofsync(fd,1);
851 int yaffs_close(int fd)
853 yaffsfs_Handle *h = NULL;
858 h = yaffsfs_GetHandlePointer(fd);
860 if(h && h->useCount > 0) {
862 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
863 yaffsfs_PutHandle(fd);
867 yaffsfs_SetError(-EBADF);
878 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
880 yaffsfs_Handle *h = NULL;
881 struct yaffs_obj *obj = NULL;
888 unsigned int maxRead;
889 u8 *buf = (u8 *)vbuf;
892 h = yaffsfs_GetHandlePointer(fd);
893 obj = yaffsfs_GetHandleObject(fd);
897 yaffsfs_SetError(-EBADF);
899 } else if(!h->reading){
900 /* Not a reading handle */
901 yaffsfs_SetError(-EINVAL);
903 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
904 yaffsfs_SetError(-EINVAL);
910 startPos = h->position;
914 if(yaffs_get_obj_length(obj) > pos)
915 maxRead = yaffs_get_obj_length(obj) - pos;
923 yaffsfs_GetHandle(fd);
925 endPos = pos + nbyte;
927 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
928 nbyte > YAFFS_MAX_FILE_SIZE ||
929 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
935 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
940 * Need to reverify object in case the device was
941 * unmounted in another thread.
943 obj = yaffsfs_GetHandleObject(fd);
947 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
958 nbyte = 0; /* no more to read */
968 yaffsfs_PutHandle(fd);
972 h->position = startPos + totalRead;
974 yaffsfs_SetError(-EINVAL);
981 return (totalRead >= 0) ? totalRead : -1;
985 int yaffs_read(int fd, void *buf, unsigned int nbyte)
987 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
990 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
992 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
995 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
997 yaffsfs_Handle *h = NULL;
998 struct yaffs_obj *obj = NULL;
1003 int totalWritten = 0;
1004 int write_trhrough = 0;
1006 const u8 *buf = (const u8 *)vbuf;
1009 h = yaffsfs_GetHandlePointer(fd);
1010 obj = yaffsfs_GetHandleObject(fd);
1014 yaffsfs_SetError(-EBADF);
1016 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1017 yaffsfs_SetError(-EINVAL);
1021 startPos = yaffs_get_obj_length(obj);
1025 startPos = h->position;
1027 yaffsfs_GetHandle(fd);
1029 endPos = pos + nbyte;
1031 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1032 nbyte > YAFFS_MAX_FILE_SIZE ||
1033 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1040 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1041 if(nToWrite > nbyte)
1045 * Need to reverify object in case the device was
1046 * remounted or unmounted in another thread.
1048 obj = yaffsfs_GetHandleObject(fd);
1049 if(!obj || obj->my_dev->read_only)
1052 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1055 totalWritten += nWritten;
1060 if(nWritten == nToWrite)
1065 if(nWritten < 1 && totalWritten < 1){
1066 yaffsfs_SetError(-ENOSPC);
1076 yaffsfs_PutHandle(fd);
1079 if(totalWritten > 0)
1080 h->position = startPos + totalWritten;
1082 yaffsfs_SetError(-EINVAL);
1088 return (totalWritten >= 0) ? totalWritten : -1;
1091 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1093 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1096 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1098 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1102 int yaffs_truncate(const YCHAR *path,off_t new_size)
1104 struct yaffs_obj *obj = NULL;
1105 struct yaffs_obj *dir = NULL;
1106 int result = YAFFS_FAIL;
1108 if(yaffsfs_CheckPath(path) < 0){
1109 yaffsfs_SetError(-ENAMETOOLONG);
1115 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1118 yaffsfs_SetError(-ENOTDIR);
1120 yaffsfs_SetError(-ENOENT);
1121 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1122 yaffsfs_SetError(-EISDIR);
1123 else if(obj->my_dev->read_only)
1124 yaffsfs_SetError(-EACCES);
1125 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1126 yaffsfs_SetError(-EINVAL);
1128 result = yaffs_resize_file(obj,new_size);
1132 return (result) ? 0 : -1;
1135 int yaffs_ftruncate(int fd, off_t new_size)
1137 yaffsfs_Handle *h = NULL;
1138 struct yaffs_obj *obj = NULL;
1142 h = yaffsfs_GetHandlePointer(fd);
1143 obj = yaffsfs_GetHandleObject(fd);
1147 yaffsfs_SetError(-EBADF);
1148 else if(obj->my_dev->read_only)
1149 yaffsfs_SetError(-EACCES);
1150 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1151 yaffsfs_SetError(-EINVAL);
1153 /* resize the file */
1154 result = yaffs_resize_file(obj,new_size);
1157 return (result) ? 0 : -1;
1161 off_t yaffs_lseek(int fd, off_t offset, int whence)
1163 yaffsfs_Handle *h = NULL;
1164 struct yaffs_obj *obj = NULL;
1169 h = yaffsfs_GetHandlePointer(fd);
1170 obj = yaffsfs_GetHandleObject(fd);
1173 yaffsfs_SetError(-EBADF);
1174 else if(offset > YAFFS_MAX_FILE_SIZE)
1175 yaffsfs_SetError(-EINVAL);
1177 if(whence == SEEK_SET){
1180 } else if(whence == SEEK_CUR) {
1181 if( (h->position + offset) >= 0)
1182 pos = (h->position + offset);
1183 } else if(whence == SEEK_END) {
1184 fSize = yaffs_get_obj_length(obj);
1185 if(fSize >= 0 && (fSize + offset) >= 0)
1186 pos = fSize + offset;
1192 yaffsfs_SetError(-EINVAL);
1201 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1203 struct yaffs_obj *dir = NULL;
1204 struct yaffs_obj *obj = NULL;
1206 int result = YAFFS_FAIL;
1208 if(yaffsfs_CheckPath(path) < 0){
1209 yaffsfs_SetError(-ENAMETOOLONG);
1215 obj = yaffsfs_FindObject(NULL,path,0,0,NULL);
1216 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1219 yaffsfs_SetError(-ENOTDIR);
1221 yaffsfs_SetError(-ENOENT);
1222 else if(obj->my_dev->read_only)
1223 yaffsfs_SetError(-EINVAL);
1224 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1225 yaffsfs_SetError(-EISDIR);
1226 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1227 yaffsfs_SetError(-ENOTDIR);
1229 result = yaffs_unlinker(dir,name);
1231 if(result == YAFFS_FAIL && isDirectory)
1232 yaffsfs_SetError(-ENOTEMPTY);
1239 return (result == YAFFS_FAIL) ? -1 : 0;
1243 int yaffs_unlink(const YCHAR *path)
1245 return yaffsfs_DoUnlink(path,0);
1248 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1250 struct yaffs_obj *olddir = NULL;
1251 struct yaffs_obj *newdir = NULL;
1252 struct yaffs_obj *obj = NULL;
1255 int result= YAFFS_FAIL;
1256 int rename_allowed = 1;
1260 if(yaffsfs_CheckPath(newPath) < 0){
1261 yaffsfs_SetError(-ENAMETOOLONG);
1265 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1266 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1267 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1269 if(!olddir || !newdir) {
1270 yaffsfs_SetError(-ENOTDIR);
1273 yaffsfs_SetError(-ENOENT);
1275 } else if(obj->my_dev->read_only){
1276 yaffsfs_SetError(-EROFS);
1278 } else if(olddir->my_dev != newdir->my_dev) {
1279 /* Rename must be on same device */
1280 yaffsfs_SetError(-EXDEV);
1282 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1284 * It is a directory, check that it is not being renamed to
1285 * being its own decendent.
1286 * Do this by tracing from the new directory back to the root, checking for obj
1289 struct yaffs_obj *xx = newdir;
1291 while( rename_allowed && xx){
1297 yaffsfs_SetError(-EINVAL);
1301 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1305 return (result == YAFFS_FAIL) ? -1 : 0;
1309 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1313 obj = yaffs_get_equivalent_obj(obj);
1316 buf->st_dev = (int)obj->my_dev->os_context;
1317 buf->st_ino = obj->obj_id;
1318 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1320 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1321 buf->st_mode |= S_IFDIR;
1322 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1323 buf->st_mode |= S_IFLNK;
1324 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1325 buf->st_mode |= S_IFREG;
1327 buf->st_nlink = yaffs_get_obj_link_count(obj);
1330 buf->st_rdev = obj->yst_rdev;
1331 buf->st_size = yaffs_get_obj_length(obj);
1332 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1333 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1334 #if CONFIG_YAFFS_WINCE
1335 buf->yst_wince_atime[0] = obj->win_atime[0];
1336 buf->yst_wince_atime[1] = obj->win_atime[1];
1337 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1338 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1339 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1340 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1342 buf->yst_atime = obj->yst_atime;
1343 buf->yst_ctime = obj->yst_ctime;
1344 buf->yst_mtime = obj->yst_mtime;
1351 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1353 struct yaffs_obj *obj=NULL;
1354 struct yaffs_obj *dir=NULL;
1358 if(yaffsfs_CheckPath(path) < 0){
1359 yaffsfs_SetError(-ENAMETOOLONG);
1365 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1368 obj = yaffsfs_FollowLink(obj,0);
1371 yaffsfs_SetError(-ENOTDIR);
1373 yaffsfs_SetError(-ENOENT);
1375 retVal = yaffsfs_DoStat(obj,buf);
1383 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1385 return yaffsfs_DoStatOrLStat(path,buf,0);
1388 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1390 return yaffsfs_DoStatOrLStat(path,buf,1);
1393 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1395 struct yaffs_obj *obj;
1400 obj = yaffsfs_GetHandleObject(fd);
1403 retVal = yaffsfs_DoStat(obj,buf);
1406 yaffsfs_SetError(-EBADF);
1413 #ifndef CONFIG_YAFFS_WINCE
1414 /* xattrib functions */
1417 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1419 struct yaffs_obj *obj;
1420 struct yaffs_obj *dir;
1424 if(yaffsfs_CheckPath(path) < 0){
1425 yaffsfs_SetError(-ENAMETOOLONG);
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_set_xattrib(obj,name,data,size,flags);
1443 yaffsfs_SetError(retVal);
1454 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1456 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1459 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1461 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1466 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1468 struct yaffs_obj *obj;
1473 obj = yaffsfs_GetHandleObject(fd);
1476 yaffsfs_SetError(-EBADF);
1478 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1480 yaffsfs_SetError(retVal);
1490 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1492 struct yaffs_obj *obj;
1493 struct yaffs_obj *dir;
1497 if(yaffsfs_CheckPath(path) < 0){
1498 yaffsfs_SetError(-ENAMETOOLONG);
1504 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1507 obj = yaffsfs_FollowLink(obj,0);
1510 yaffsfs_SetError(-ENOTDIR);
1512 yaffsfs_SetError(-ENOENT);
1514 retVal = yaffs_get_xattrib(obj,name,data,size);
1516 yaffsfs_SetError(retVal);
1526 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1528 return yaffs_do_getxattr( path, name, data, size, 1);
1530 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1532 return yaffs_do_getxattr( path, name, data, size, 0);
1537 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1539 struct yaffs_obj *obj;
1544 obj = yaffsfs_GetHandleObject(fd);
1547 retVal = yaffs_get_xattrib(obj,name,data,size);
1549 yaffsfs_SetError(retVal);
1554 yaffsfs_SetError(-EBADF);
1561 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1563 struct yaffs_obj *obj=NULL;
1564 struct yaffs_obj *dir=NULL;
1568 if(yaffsfs_CheckPath(path) < 0){
1569 yaffsfs_SetError(-ENAMETOOLONG);
1575 obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1578 obj = yaffsfs_FollowLink(obj,0);
1581 yaffsfs_SetError(-ENOTDIR);
1583 yaffsfs_SetError(-ENOENT);
1585 retVal = yaffs_list_xattrib(obj, data,size);
1587 yaffsfs_SetError(retVal);
1598 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1600 return yaffs_do_listxattr(path, data, size, 1);
1603 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1605 return yaffs_do_listxattr(path, data, size, 0);
1608 int yaffs_flistxattr(int fd, char *data, int size)
1610 struct yaffs_obj *obj;
1615 obj = yaffsfs_GetHandleObject(fd);
1618 retVal = yaffs_list_xattrib(obj,data,size);
1620 yaffsfs_SetError(retVal);
1625 yaffsfs_SetError(-EBADF);
1632 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1634 struct yaffs_obj *obj=NULL;
1635 struct yaffs_obj *dir=NULL;
1639 if(yaffsfs_CheckPath(path) < 0){
1640 yaffsfs_SetError(-ENAMETOOLONG);
1646 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1649 obj = yaffsfs_FollowLink(obj,0);
1652 yaffsfs_SetError(-ENOTDIR);
1654 yaffsfs_SetError(-ENOENT);
1656 retVal = yaffs_remove_xattrib(obj,name);
1658 yaffsfs_SetError(retVal);
1669 int yaffs_removexattr(const YCHAR *path, const char *name)
1671 return yaffs_do_removexattr(path, name, 1);
1674 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1676 return yaffs_do_removexattr(path, name, 0);
1679 int yaffs_fremovexattr(int fd, const char *name)
1681 struct yaffs_obj *obj;
1686 obj = yaffsfs_GetHandleObject(fd);
1689 retVal = yaffs_remove_xattrib(obj,name);
1691 yaffsfs_SetError(retVal);
1696 yaffsfs_SetError(-EBADF);
1704 #ifdef CONFIG_YAFFS_WINCE
1705 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1707 struct yaffs_obj *obj;
1712 obj = yaffsfs_GetHandleObject(fd);
1717 wctime[0] = obj->win_ctime[0];
1718 wctime[1] = obj->win_ctime[1];
1721 watime[0] = obj->win_atime[0];
1722 watime[1] = obj->win_atime[1];
1725 wmtime[0] = obj->win_mtime[0];
1726 wmtime[1] = obj->win_mtime[1];
1733 yaffsfs_SetError(-EBADF);
1741 int yaffs_set_wince_times(int fd,
1742 const unsigned *wctime,
1743 const unsigned *watime,
1744 const unsigned *wmtime)
1746 struct yaffs_obj *obj;
1751 obj = yaffsfs_GetHandleObject(fd);
1756 obj->win_ctime[0] = wctime[0];
1757 obj->win_ctime[1] = wctime[1];
1760 obj->win_atime[0] = watime[0];
1761 obj->win_atime[1] = watime[1];
1764 obj->win_mtime[0] = wmtime[0];
1765 obj->win_mtime[1] = wmtime[1];
1769 result = yaffs_flush_file(obj,0,0);
1773 yaffsfs_SetError(-EBADF);
1783 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1788 obj = yaffs_get_equivalent_obj(obj);
1791 obj->yst_mode = mode;
1793 result = yaffs_flush_file(obj,0,0);
1796 return result == YAFFS_OK ? 0 : -1;
1800 int yaffs_access(const YCHAR *path, int amode)
1802 struct yaffs_obj *obj=NULL;
1803 struct yaffs_obj *dir=NULL;
1807 if(yaffsfs_CheckPath(path) < 0){
1808 yaffsfs_SetError(-ENAMETOOLONG);
1812 if(amode & ~(R_OK | W_OK | X_OK)){
1813 yaffsfs_SetError(-EINVAL);
1819 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1822 yaffsfs_SetError(-ENOTDIR);
1824 yaffsfs_SetError(-ENOENT);
1828 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1830 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1832 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1836 yaffsfs_SetError(-EACCES);
1848 int yaffs_chmod(const YCHAR *path, mode_t mode)
1850 struct yaffs_obj *obj=NULL;
1851 struct yaffs_obj *dir=NULL;
1854 if(yaffsfs_CheckPath(path) < 0){
1855 yaffsfs_SetError(-ENAMETOOLONG);
1860 yaffsfs_SetError(-EINVAL);
1866 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1869 yaffsfs_SetError(-ENOTDIR);
1871 yaffsfs_SetError(-ENOENT);
1872 else if(obj->my_dev->read_only)
1873 yaffsfs_SetError(-EROFS);
1875 retVal = yaffsfs_DoChMod(obj,mode);
1884 int yaffs_fchmod(int fd, mode_t mode)
1886 struct yaffs_obj *obj;
1890 yaffsfs_SetError(-EINVAL);
1895 obj = yaffsfs_GetHandleObject(fd);
1898 yaffsfs_SetError(-EBADF);
1899 else if(obj->my_dev->read_only)
1900 yaffsfs_SetError(-EROFS);
1902 retVal = yaffsfs_DoChMod(obj,mode);
1910 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1912 YCHAR *alt_path = NULL;
1917 * We don't have a definition for max path length.
1918 * We will use 3 * max name length instead.
1921 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1923 /* If the last character is a path divider, then we need to
1924 * trim it back so that the name look-up works properly.
1925 * eg. /foo/new_dir/ -> /foo/newdir
1926 * Curveball: Need to handle multiple path dividers:
1927 * eg. /foof/sdfse///// -> /foo/sdfse
1929 if(path_length > 0 &&
1930 yaffsfs_IsPathDivider(path[path_length-1])){
1931 alt_path = YMALLOC(path_length + 1);
1934 strcpy(alt_path, path);
1935 for(i = path_length-1;
1936 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1938 alt_path[i] = (YCHAR) 0;
1940 *ret_path = alt_path;
1944 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1946 struct yaffs_obj *parent = NULL;
1947 struct yaffs_obj *dir = NULL;
1949 YCHAR *alt_path = NULL;
1952 if(yaffsfs_CheckPath(path) < 0){
1953 yaffsfs_SetError(-ENAMETOOLONG);
1957 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1958 yaffsfs_SetError(-ENOMEM);
1965 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1967 yaffsfs_SetError(-ENOTDIR);
1968 else if(parent && yaffs_strnlen(name,5) == 0){
1969 /* Trying to make the root itself */
1970 yaffsfs_SetError(-EEXIST);
1971 } else if(parent && parent->my_dev->read_only)
1972 yaffsfs_SetError(-EROFS);
1974 dir = yaffs_create_dir(parent,name,mode,0,0);
1977 else if (yaffs_find_by_name(parent,name))
1978 yaffsfs_SetError(-EEXIST); /* the name already exists */
1980 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1991 int yaffs_rmdir(const YCHAR *path)
1996 if(yaffsfs_CheckPath(path) < 0){
1997 yaffsfs_SetError(-ENAMETOOLONG);
2001 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2002 yaffsfs_SetError(-ENOMEM);
2007 result = yaffsfs_DoUnlink(path,1);
2014 void * yaffs_getdev(const YCHAR *path)
2016 struct yaffs_dev *dev=NULL;
2018 dev = yaffsfs_FindDevice(path,&dummy);
2022 int yaffs_mount2(const YCHAR *path,int read_only)
2025 int result=YAFFS_FAIL;
2026 struct yaffs_dev *dev=NULL;
2028 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2030 if(yaffsfs_CheckPath(path) < 0){
2031 yaffsfs_SetError(-ENAMETOOLONG);
2037 yaffsfs_InitHandles();
2039 dev = yaffsfs_FindMountPoint(path);
2041 if(!dev->is_mounted){
2042 dev->read_only = read_only ? 1 : 0;
2043 result = yaffs_guts_initialise(dev);
2044 if(result == YAFFS_FAIL)
2045 /* todo error - mount failed */
2046 yaffsfs_SetError(-ENOMEM);
2047 retVal = result ? 0 : -1;
2051 /* todo error - already mounted. */
2052 yaffsfs_SetError(-EBUSY);
2054 /* todo error - no device */
2055 yaffsfs_SetError(-ENODEV);
2062 int yaffs_mount(const YCHAR *path)
2064 return yaffs_mount2(path,0);
2067 int yaffs_sync(const YCHAR *path)
2070 struct yaffs_dev *dev=NULL;
2073 if(yaffsfs_CheckPath(path) < 0){
2074 yaffsfs_SetError(-ENAMETOOLONG);
2079 dev = yaffsfs_FindDevice(path,&dummy);
2081 if(dev->is_mounted){
2083 yaffs_flush_whole_cache(dev);
2084 yaffs_checkpoint_save(dev);
2088 /* todo error - not mounted. */
2089 yaffsfs_SetError(-EINVAL);
2092 /* todo error - no device */
2093 yaffsfs_SetError(-ENODEV);
2100 int yaffs_remount(const YCHAR *path, int force, int read_only)
2103 struct yaffs_dev *dev=NULL;
2106 if(yaffsfs_CheckPath(path) < 0){
2107 yaffsfs_SetError(-ENAMETOOLONG);
2112 dev = yaffsfs_FindMountPoint(path);
2114 if(dev->is_mounted){
2118 yaffs_flush_whole_cache(dev);
2120 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2121 yh = & yaffsfs_handle[i];
2122 if(yh->useCount>0 &&
2123 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2124 inUse = 1; /* the device is in use, can't unmount */
2127 if(!inUse || force){
2129 yaffs_checkpoint_save(dev);
2130 dev->read_only = read_only ? 1 : 0;
2133 yaffsfs_SetError(-EBUSY);
2136 yaffsfs_SetError(-EINVAL);
2140 yaffsfs_SetError(-ENODEV);
2147 int yaffs_unmount2(const YCHAR *path, int force)
2150 struct yaffs_dev *dev=NULL;
2152 if(yaffsfs_CheckPath(path) < 0){
2153 yaffsfs_SetError(-ENAMETOOLONG);
2158 dev = yaffsfs_FindMountPoint(path);
2160 if(dev->is_mounted){
2164 yaffs_flush_whole_cache(dev);
2165 yaffs_checkpoint_save(dev);
2167 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2168 if(yaffsfs_handle[i].useCount > 0 &&
2169 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2170 inUse = 1; /* the device is in use, can't unmount */
2173 if(!inUse || force){
2175 yaffsfs_PutDeviceHandles(dev);
2176 yaffs_deinitialise(dev);
2180 /* todo error can't unmount as files are open */
2181 yaffsfs_SetError(-EBUSY);
2184 /* todo error - not mounted. */
2185 yaffsfs_SetError(-EINVAL);
2188 /* todo error - no device */
2189 yaffsfs_SetError(-ENODEV);
2196 int yaffs_unmount(const YCHAR *path)
2198 return yaffs_unmount2(path,0);
2201 loff_t yaffs_freespace(const YCHAR *path)
2204 struct yaffs_dev *dev=NULL;
2207 if(yaffsfs_CheckPath(path) < 0){
2208 yaffsfs_SetError(-ENAMETOOLONG);
2213 dev = yaffsfs_FindDevice(path,&dummy);
2214 if(dev && dev->is_mounted){
2215 retVal = yaffs_get_n_free_chunks(dev);
2216 retVal *= dev->data_bytes_per_chunk;
2219 yaffsfs_SetError(-EINVAL);
2225 loff_t yaffs_totalspace(const YCHAR *path)
2228 struct yaffs_dev *dev=NULL;
2231 if(yaffsfs_CheckPath(path) < 0){
2232 yaffsfs_SetError(-ENAMETOOLONG);
2237 dev = yaffsfs_FindDevice(path,&dummy);
2238 if(dev && dev->is_mounted){
2239 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2240 dev->param.n_reserved_blocks;
2241 retVal *= dev->param.chunks_per_block;
2242 retVal *= dev->data_bytes_per_chunk;
2245 yaffsfs_SetError(-EINVAL);
2251 int yaffs_inodecount(const YCHAR *path)
2254 struct yaffs_dev *dev=NULL;
2257 if(yaffsfs_CheckPath(path) < 0){
2258 yaffsfs_SetError(-ENAMETOOLONG);
2263 dev = yaffsfs_FindDevice(path,&dummy);
2264 if(dev && dev->is_mounted) {
2265 int n_obj = dev->n_obj;
2266 if(n_obj > dev->n_hardlinks)
2267 retVal = n_obj - dev->n_hardlinks;
2271 yaffsfs_SetError(-EINVAL);
2278 void yaffs_add_device(struct yaffs_dev *dev)
2280 dev->is_mounted = 0;
2281 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2283 if(!dev->dev_list.next)
2284 INIT_LIST_HEAD(&dev->dev_list);
2286 list_add(&dev->dev_list,&yaffsfs_deviceList);
2289 void yaffs_remove_device(struct yaffs_dev *dev)
2291 list_del_init(&dev->dev_list);
2297 /* Directory search stuff. */
2300 * Directory search context
2302 * NB this is an opaque structure.
2309 yaffs_dirent de; /* directory entry being used by this dsc */
2310 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2311 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2312 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2314 struct list_head others;
2315 } yaffsfs_DirectorySearchContext;
2319 static struct list_head search_contexts;
2322 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2326 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2330 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2331 dsc->nextReturn = NULL;
2333 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2334 struct yaffs_obj,siblings);
2336 /* Hey someone isn't playing nice! */
2340 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2344 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2346 if( dsc->nextReturn == NULL ||
2347 list_empty(&dsc->dirObj->variant.dir_variant.children))
2348 dsc->nextReturn = NULL;
2350 struct list_head *next = dsc->nextReturn->siblings.next;
2352 if( next == &dsc->dirObj->variant.dir_variant.children)
2353 dsc->nextReturn = NULL; /* end of list */
2355 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2358 /* Hey someone isn't playing nice! */
2362 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2365 struct list_head *i;
2366 yaffsfs_DirectorySearchContext *dsc;
2368 /* if search contexts not initilised then skip */
2369 if(!search_contexts.next)
2372 /* Iterate through the directory search contexts.
2373 * If any are the one being removed, then advance the dsc to
2374 * the next one to prevent a hanging ptr.
2376 list_for_each(i, &search_contexts) {
2378 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2379 if(dsc->nextReturn == obj)
2380 yaffsfs_DirAdvance(dsc);
2386 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2388 yaffs_DIR *dir = NULL;
2389 struct yaffs_obj *obj = NULL;
2390 yaffsfs_DirectorySearchContext *dsc = NULL;
2392 if(yaffsfs_CheckPath(dirname) < 0){
2393 yaffsfs_SetError(-ENAMETOOLONG);
2399 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2401 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2403 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2404 dir = (yaffs_DIR *)dsc;
2407 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2408 dsc->magic = YAFFS_MAGIC;
2410 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2411 INIT_LIST_HEAD(&dsc->others);
2413 if(!search_contexts.next)
2414 INIT_LIST_HEAD(&search_contexts);
2416 list_add(&dsc->others,&search_contexts);
2417 yaffsfs_SetDirRewound(dsc);
2427 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2429 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2430 struct yaffs_dirent *retVal = NULL;
2434 if(dsc && dsc->magic == YAFFS_MAGIC){
2435 yaffsfs_SetError(0);
2436 if(dsc->nextReturn){
2437 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2438 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2439 dsc->de.d_off = dsc->offset++;
2440 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2441 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2443 /* this should not happen! */
2444 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2446 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2448 yaffsfs_DirAdvance(dsc);
2452 yaffsfs_SetError(-EBADF);
2461 void yaffs_rewinddir(yaffs_DIR *dirp)
2463 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2467 yaffsfs_SetDirRewound(dsc);
2473 int yaffs_closedir(yaffs_DIR *dirp)
2475 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2479 list_del(&dsc->others); /* unhook from list */
2485 /* End of directory stuff */
2488 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2490 struct yaffs_obj *parent = NULL;
2491 struct yaffs_obj *obj;
2494 int mode = 0; /* ignore for now */
2496 if(yaffsfs_CheckPath(newpath) < 0){
2497 yaffsfs_SetError(-ENAMETOOLONG);
2501 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2503 yaffsfs_SetError(-ENOTDIR);
2504 else if( strlen(name) < 1)
2505 yaffsfs_SetError(-ENOENT);
2506 else if(parent->my_dev->read_only)
2507 yaffsfs_SetError(-EROFS);
2509 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2512 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL))
2513 yaffsfs_SetError(-EEXIST);
2515 yaffsfs_SetError(-ENOSPC);
2524 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2526 struct yaffs_obj *obj = NULL;
2527 struct yaffs_obj *dir = NULL;
2532 obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2535 yaffsfs_SetError(-ENOTDIR);
2537 yaffsfs_SetError(-ENOENT);
2538 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2539 yaffsfs_SetError(-EINVAL);
2541 YCHAR *alias = obj->variant.symlink_variant.alias;
2542 memset(buf,0,bufsiz);
2543 yaffs_strncpy(buf,alias,bufsiz - 1);
2550 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2552 /* Creates a link called newpath to existing oldpath */
2553 struct yaffs_obj *obj = NULL;
2554 struct yaffs_obj *lnk = NULL;
2557 if(yaffsfs_CheckPath(linkpath) < 0){
2558 yaffsfs_SetError(-ENAMETOOLONG);
2564 obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL);
2565 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2568 yaffsfs_SetError(-ENOENT);
2569 else if(obj->my_dev->read_only)
2570 yaffsfs_SetError(-EINVAL);
2572 yaffsfs_SetError(-EEXIST);
2574 struct yaffs_obj *newdir = NULL;
2575 struct yaffs_obj *link = NULL;
2579 newdir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2582 yaffsfs_SetError(-ENOTDIR);
2583 else if(newdir->my_dev != obj->my_dev)
2584 yaffsfs_SetError(-EXDEV);
2586 retVal = yaffsfs_CheckNameLength(newname);
2589 link = yaffs_link_obj(newdir,newname,obj);
2593 yaffsfs_SetError(-ENOSPC);
2604 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2610 yaffsfs_SetError(-EINVAL);
2618 * Returns number of handles attached to the object
2620 int yaffs_n_handles(const YCHAR *path)
2622 struct yaffs_obj *obj;
2624 if(yaffsfs_CheckPath(path) < 0){
2625 yaffsfs_SetError(-ENAMETOOLONG);
2629 obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2632 return yaffsfs_CountHandles(obj);
2637 int yaffs_get_error(void)
2639 return yaffsfs_GetLastError();
2642 int yaffs_set_error(int error)
2644 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2645 yaffsfs_SetError(error);
2649 int yaffs_dump_dev(const YCHAR *path)
2656 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2659 struct yaffs_dev *dev = obj->my_dev;
2662 "n_page_writes.......... %d\n"
2663 "n_page_reads........... %d\n"
2664 "n_erasures....... %d\n"
2665 "n_gc_copies............ %d\n"
2666 "garbageCollections... %d\n"
2667 "passiveGarbageColl'ns %d\n"
2673 dev->garbageCollections,
2674 dev->passiveGarbageCollections