2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 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,
38 int *notDir, int *loop);
40 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
42 unsigned int yaffs_wr_attempts;
46 * There are open inodes in yaffsfs_Inode.
47 * There are open file descriptors in yaffsfs_FileDes.
48 * There are open handles in yaffsfs_FileDes.
50 * Things are structured this way to be like the Linux VFS model
51 * so that interactions with the yaffs guts calls are similar.
52 * That means more common code paths and less special code.
53 * That means better testing etc.
55 * We have 3 layers because:
56 * A handle is different than an fd because you can use dup()
57 * to create a new handle that accesses the *same* fd. The two
58 * handles will use the same offset (part of the fd). We only close
59 * down the fd when there are no more handles accessing it.
61 * More than one fd can currently access one file, but each fd
62 * has its own permsiions and offset.
66 int count; /* Number of handles accessing this inode */
67 struct yaffs_obj *iObj;
76 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
77 int handleCount:10; /* Number of handles for this fd */
78 u32 position; /* current position in file */
86 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
87 static yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
88 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
90 static int yaffsfs_handlesInitialised;
93 unsigned yaffs_set_trace(unsigned tm)
95 yaffs_trace_mask = tm;
96 return yaffs_trace_mask;
99 unsigned yaffs_get_trace(void)
101 return yaffs_trace_mask;
106 * Inilitalise handle management on start-up.
109 static void yaffsfs_InitHandles(void)
112 if(yaffsfs_handlesInitialised)
115 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
116 memset(yaffsfs_fd,0,sizeof(yaffsfs_fd));
117 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
119 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
120 yaffsfs_fd[i].inodeId = -1;
121 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
122 yaffsfs_handle[i].fdId = -1;
125 static yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
127 if(h >= 0 && h < YAFFSFS_N_HANDLES)
128 return &yaffsfs_handle[h];
132 static yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
134 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
136 if(h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
137 return &yaffsfs_fd[h->fdId];
142 static yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
144 yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
146 if(fd && fd->handleCount > 0 &&
147 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
148 return &yaffsfs_inode[fd->inodeId];
153 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
155 yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
164 * yaffsfs_FindInodeIdForObject
165 * Find the inode entry for an object, if it exists.
168 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
174 obj = yaffs_get_equivalent_obj(obj);
176 /* Look for it in open inode table*/
177 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
178 if(yaffsfs_inode[i].iObj == obj)
185 * yaffsfs_GetInodeIdForObject
186 * Grab an inode entry when opening a new inode.
188 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
192 yaffsfs_Inode *in = NULL;
195 obj = yaffs_get_equivalent_obj(obj);
197 ret = yaffsfs_FindInodeIdForObject(obj);
199 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
200 if(!yaffsfs_inode[i].iObj)
205 in = &yaffsfs_inode[ret];
217 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
219 int i = yaffsfs_FindInodeIdForObject(obj);
222 return yaffsfs_inode[i].count;
227 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
229 struct yaffs_obj *obj;
236 obj->my_inode = NULL;
241 static void yaffsfs_PutInode(int inodeId)
243 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
244 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
247 yaffsfs_ReleaseInode(in);
255 static int yaffsfs_NewHandle(yaffsfs_Handle **hptr)
260 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
261 h = &yaffsfs_handle[i];
263 memset(h,0,sizeof(yaffsfs_Handle));
274 static int yaffsfs_NewHandleAndFileDes(void)
278 yaffsfs_Handle *h = NULL;
279 int handle = yaffsfs_NewHandle(&h);
284 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
286 if(fd->handleCount < 1){
287 memset(fd,0,sizeof(yaffsfs_FileDes));
295 /* Dump the handle because we could not get a fd */
302 * Increase use of handle when reading/writing a file
303 * Also gets the file descriptor.
306 static int yaffsfs_GetHandle(int handle)
308 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
310 if(h && h->useCount > 0){
319 * Let go of a handle when closing a file or aborting an open or
320 * ending a read or write.
323 static int yaffsfs_PutFileDes(int fdId)
327 if(fdId >= 0 && fdId < YAFFSFS_N_HANDLES){
328 fd = &yaffsfs_fd[fdId];
330 if(fd->handleCount < 1){
331 if(fd->inodeId >= 0){
332 yaffsfs_PutInode(fd->inodeId);
339 static int yaffsfs_PutHandle(int handle)
341 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
343 if(h && h->useCount > 0){
346 yaffsfs_PutFileDes(h->fdId);
354 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
358 struct yaffs_obj *obj;
360 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
361 h = yaffsfs_HandleToPointer(i);
362 fd = yaffsfs_HandleToFileDes(i);
363 obj = yaffsfs_HandleToObject(i);
364 if(h && h->useCount > 0){
368 if(fd && fd->handleCount>0 && obj && obj->my_dev == dev){
371 yaffsfs_PutInode(fd->inodeId);
381 * Stuff to handle names.
385 int yaffsfs_Match(YCHAR a, YCHAR b)
391 int yaffsfs_IsPathDivider(YCHAR ch)
393 const YCHAR *str = YAFFS_PATH_DIVIDERS;
404 int yaffsfs_CheckNameLength(const char *name)
408 int nameLength = strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
411 yaffsfs_SetError(-ENOENT);
413 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
414 yaffsfs_SetError(-ENAMETOOLONG);
422 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
424 YCHAR *alt_path = NULL;
429 * We don't have a definition for max path length.
430 * We will use 3 * max name length instead.
433 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
435 /* If the last character is a path divider, then we need to
436 * trim it back so that the name look-up works properly.
437 * eg. /foo/new_dir/ -> /foo/newdir
438 * Curveball: Need to handle multiple path dividers:
439 * eg. /foof/sdfse///// -> /foo/sdfse
441 if(path_length > 0 &&
442 yaffsfs_IsPathDivider(path[path_length-1])){
443 alt_path = kmalloc(path_length + 1, 0);
446 strcpy(alt_path, path);
447 for(i = path_length-1;
448 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
450 alt_path[i] = (YCHAR) 0;
452 *ret_path = alt_path;
457 LIST_HEAD(yaffsfs_deviceList);
462 * Scan the configuration list to find the device
463 * Curveballs: Should match paths that end in '/' too
464 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
466 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
468 struct list_head *cfg;
469 const YCHAR *leftOver;
471 struct yaffs_dev *retval = NULL;
472 struct yaffs_dev *dev = NULL;
474 int longestMatch = -1;
478 * Check all configs, choose the one that:
479 * 1) Actually matches a prefix (ie /a amd /abc will not match
480 * 2) Matches the longest.
482 list_for_each(cfg, &yaffsfs_deviceList){
483 dev = list_entry(cfg, struct yaffs_dev, dev_list);
490 while(matching && *p && *leftOver){
491 /* Skip over any /s */
492 while(yaffsfs_IsPathDivider(*p))
495 /* Skip over any /s */
496 while(yaffsfs_IsPathDivider(*leftOver))
499 /* Now match the text part */
501 *p && !yaffsfs_IsPathDivider(*p) &&
502 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
503 if(yaffsfs_Match(*p,*leftOver)){
513 /* Skip over any /s in leftOver */
514 while(yaffsfs_IsPathDivider(*leftOver))
517 // Skip over any /s in p
518 while(yaffsfs_IsPathDivider(*p))
521 // p should now be at the end of the string (ie. fully matched)
525 if( matching && (thisMatchLength > longestMatch))
528 *restOfPath = (YCHAR *)leftOver;
530 longestMatch = thisMatchLength;
537 static int yaffsfs_CheckPath(const YCHAR *path)
541 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
542 if(yaffsfs_IsPathDivider(*path)){
550 return (*path) ? -1 : 0;
553 /* FindMountPoint only returns a dev entry if the path is a mount point */
554 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
556 struct yaffs_dev *dev;
557 YCHAR *restOfPath=NULL;
558 dev = yaffsfs_FindDevice(path,&restOfPath);
559 if(dev && restOfPath && *restOfPath)
564 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
567 struct yaffs_dev *dev;
569 dev= yaffsfs_FindDevice(path,restOfPath);
570 if(dev && dev->is_mounted){
571 return dev->root_dir;
576 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
577 int symDepth, int *loop)
581 obj = yaffs_get_equivalent_obj(obj);
583 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
584 YCHAR *alias = obj->variant.symlink_variant.alias;
586 if(yaffsfs_IsPathDivider(*alias))
587 /* Starts with a /, need to scan from root up */
588 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
591 /* Relative to here, so use the parent of the symlink as a start */
592 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
600 * yaffsfs_FindDirectory
601 * Parse a path to determine the directory and the name within the directory.
603 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
605 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
606 const YCHAR *path, YCHAR **name, int symDepth,
607 int *notDir,int *loop)
609 struct yaffs_obj *dir;
611 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
614 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
622 restOfPath = (YCHAR *)path;
625 dir = yaffsfs_FindRoot(path,&restOfPath);
631 * curve ball: also throw away surplus '/'
632 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
634 while(yaffsfs_IsPathDivider(*restOfPath))
635 restOfPath++; /* get rid of '/' */
640 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
641 if (i < YAFFS_MAX_NAME_LENGTH){
642 str[i] = *restOfPath;
650 /* got to the end of the string */
653 if(strcmp(str,_Y(".")) == 0){
655 } else if(strcmp(str,_Y("..")) == 0) {
658 dir = yaffs_find_by_name(dir,str);
660 dir = yaffsfs_FollowLink(dir,symDepth,loop);
662 if(dir && dir->variant_type !=
663 YAFFS_OBJECT_TYPE_DIRECTORY){
672 /* directory did not exist. */
676 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
683 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
687 * yaffsfs_FindObject turns a path for an existing object into the object
689 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
690 const YCHAR *path,int symDepth, int getEquiv,
691 struct yaffs_obj **dirOut, int *notDir,int *loop)
693 struct yaffs_obj *dir;
694 struct yaffs_obj *obj;
697 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
703 obj = yaffs_find_by_name(dir,name);
708 obj = yaffs_get_equivalent_obj(obj);
714 /*************************************************************************
715 * Start of yaffsfs visible functions.
716 *************************************************************************/
718 int yaffs_dup(int handle)
720 int newHandleNumber = -1;
721 yaffsfs_FileDes *existingFD = NULL;
722 yaffsfs_Handle *existingHandle = NULL;
723 yaffsfs_Handle *newHandle = NULL;
726 existingHandle = yaffsfs_HandleToPointer(handle);
727 existingFD = yaffsfs_HandleToFileDes(handle);
729 newHandleNumber = yaffsfs_NewHandle(&newHandle);
731 newHandle->fdId = existingHandle->fdId;
732 existingFD->handleCount++;
738 yaffsfs_SetError(-EBADF);
740 yaffsfs_SetError(-ENOMEM);
742 return newHandleNumber;
746 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
748 int current_objects = dev->n_obj - dev->n_deleted_files;
750 if(dev->param.max_objects && current_objects > dev->param.max_objects)
756 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
758 struct yaffs_obj *obj = NULL;
759 struct yaffs_obj *dir = NULL;
762 yaffsfs_FileDes *fd = NULL;
765 int errorReported = 0;
766 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
767 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
768 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
769 u8 sharedReadAllowed;
770 u8 sharedWriteAllowed;
779 yaffsfs_SetError(-EFAULT);
783 if(yaffsfs_CheckPath(path) < 0){
784 yaffsfs_SetError(-ENAMETOOLONG);
788 /* O_EXCL only has meaning if O_CREAT is specified */
789 if(!(oflag & O_CREAT))
792 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
793 if( (oflag & O_CREAT) & (oflag & O_EXCL))
796 /* Todo: Are there any more flag combos to sanitise ? */
798 /* Figure out if reading or writing is requested */
800 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
801 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
805 handle = yaffsfs_NewHandleAndFileDes();
808 yaffsfs_SetError(-ENFILE);
812 fd = yaffsfs_HandleToFileDes(handle);
814 /* try to find the exisiting object */
815 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
817 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
820 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
821 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
827 /* The file already exists or it might be a directory */
829 /* If it is a directory then we can't open it as a file */
830 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
832 yaffsfs_SetError(-EISDIR);
836 /* Open should fail if O_CREAT and O_EXCL are specified since
839 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
841 yaffsfs_SetError(-EEXIST);
845 /* Check file permissions */
846 if( readRequested && !(obj->yst_mode & S_IREAD))
849 if( writeRequested && !(obj->yst_mode & S_IWRITE))
852 if( !errorReported && writeRequested &&
853 obj->my_dev->read_only){
855 yaffsfs_SetError(-EROFS);
859 if(openDenied && !errorReported ) {
860 /* Error if the file exists but permissions are refused. */
861 yaffsfs_SetError(-EACCES);
865 /* Check sharing of an existing object. */
867 yaffsfs_FileDes *fdx;
870 sharedReadAllowed = 1;
871 sharedWriteAllowed = 1;
874 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
875 fdx = &yaffsfs_fd[i];
876 if(fdx->handleCount > 0 &&
878 yaffsfs_inode[fdx->inodeId].iObj == obj){
880 sharedReadAllowed = 0;
882 sharedWriteAllowed = 0;
892 if((!sharedReadAllowed && readRequested)||
893 (!shareRead && alreadyReading) ||
894 (!sharedWriteAllowed && writeRequested) ||
895 (!shareWrite && alreadyWriting)){
897 yaffsfs_SetError(-EBUSY);
904 /* If we could not open an existing object, then let's see if
905 * the directory exists. If not, error.
907 if(!obj && !errorReported){
908 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
910 yaffsfs_SetError(-ENOTDIR);
913 yaffsfs_SetError(-ELOOP);
916 yaffsfs_SetError(-ENOENT);
921 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
922 /* Let's see if we can create this file if it does not exist. */
923 if(dir->my_dev->read_only){
924 yaffsfs_SetError(-EROFS);
926 } else if(yaffsfs_TooManyObjects(dir->my_dev)) {
927 yaffsfs_SetError(-ENFILE);
930 obj = yaffs_create_file(dir,name,mode,0,0);
932 if(!obj && !errorReported){
933 yaffsfs_SetError(-ENOSPC);
938 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
939 /* Error if the file does not exist and CREAT is not set. */
940 yaffsfs_SetError(-ENOENT);
944 if(obj && !openDenied) {
945 int inodeId = yaffsfs_GetInodeIdForObject(obj);
949 * Todo: Fix any problem if inodes run out, though that
950 * can't happen if the number of inode items >= number of handles.
954 fd->inodeId = inodeId;
955 fd->reading = readRequested;
956 fd->writing = writeRequested;
957 fd->append = (oflag & O_APPEND) ? 1 : 0;
959 fd->shareRead = shareRead;
960 fd->shareWrite = shareWrite;
962 /* Hook inode to object */
963 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
965 if((oflag & O_TRUNC) && fd->writing)
966 yaffs_resize_file(obj,0);
968 yaffsfs_PutHandle(handle);
970 yaffsfs_SetError(0); /* Problem */
980 int yaffs_open(const YCHAR *path, int oflag, int mode)
982 return yaffs_open_sharing(path, oflag, mode,
983 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
986 int yaffs_Dofsync(int handle,int datasync)
989 struct yaffs_obj *obj;
993 obj = yaffsfs_HandleToObject(handle);
996 yaffsfs_SetError(-EBADF);
997 else if(obj->my_dev->read_only)
998 yaffsfs_SetError(-EROFS);
1000 yaffs_flush_file(obj,1,datasync);
1009 int yaffs_fsync(int handle)
1011 return yaffs_Dofsync(handle,0);
1014 int yaffs_flush(int handle)
1016 return yaffs_fsync(handle);
1019 int yaffs_fdatasync(int handle)
1021 return yaffs_Dofsync(handle,1);
1024 int yaffs_close(int handle)
1026 yaffsfs_Handle *h = NULL;
1027 struct yaffs_obj *obj = NULL;
1032 h = yaffsfs_HandleToPointer(handle);
1033 obj = yaffsfs_HandleToObject(handle);
1036 yaffsfs_SetError(-EBADF);
1039 yaffs_flush_file(obj,1,0);
1040 yaffsfs_PutHandle(handle);
1051 int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte, int isPread, int offset)
1053 yaffsfs_FileDes *fd = NULL;
1054 struct yaffs_obj *obj = NULL;
1061 unsigned int maxRead;
1062 u8 *buf = (u8 *)vbuf;
1065 yaffsfs_SetError(-EFAULT);
1070 fd = yaffsfs_HandleToFileDes(handle);
1071 obj = yaffsfs_HandleToObject(handle);
1075 yaffsfs_SetError(-EBADF);
1077 } else if(!fd->reading){
1078 /* Not a reading handle */
1079 yaffsfs_SetError(-EINVAL);
1081 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
1082 yaffsfs_SetError(-EINVAL);
1088 startPos = fd->position;
1092 if(yaffs_get_obj_length(obj) > pos)
1093 maxRead = yaffs_get_obj_length(obj) - pos;
1101 yaffsfs_GetHandle(handle);
1103 endPos = pos + nbyte;
1105 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1106 nbyte > YAFFS_MAX_FILE_SIZE ||
1107 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1113 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1118 * Need to reverify object in case the device was
1119 * unmounted in another thread.
1121 obj = yaffsfs_HandleToObject(handle);
1125 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1133 if(nRead == nToRead)
1136 nbyte = 0; /* no more to read */
1146 yaffsfs_PutHandle(handle);
1150 fd->position = startPos + totalRead;
1152 yaffsfs_SetError(-EINVAL);
1159 return (totalRead >= 0) ? totalRead : -1;
1163 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1165 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1168 int yaffs_pread(int handle, void *buf, unsigned int nbyte, unsigned int offset)
1170 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1173 int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1175 yaffsfs_FileDes *fd = NULL;
1176 struct yaffs_obj *obj = NULL;
1181 int totalWritten = 0;
1182 int write_trhrough = 0;
1184 const u8 *buf = (const u8 *)vbuf;
1187 yaffsfs_SetError(-EFAULT);
1192 fd = yaffsfs_HandleToFileDes(handle);
1193 obj = yaffsfs_HandleToObject(handle);
1197 yaffsfs_SetError(-EBADF);
1199 } else if(!fd->writing){
1200 yaffsfs_SetError(-EINVAL);
1202 } else if(obj->my_dev->read_only){
1203 yaffsfs_SetError(-EROFS);
1207 startPos = yaffs_get_obj_length(obj);
1211 startPos = fd->position;
1213 yaffsfs_GetHandle(handle);
1215 endPos = pos + nbyte;
1217 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1218 nbyte > YAFFS_MAX_FILE_SIZE ||
1219 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1226 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1227 if(nToWrite > nbyte)
1231 * Need to reverify object in case the device was
1232 * remounted or unmounted in another thread.
1234 obj = yaffsfs_HandleToObject(handle);
1235 if(!obj || obj->my_dev->read_only)
1238 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1241 totalWritten += nWritten;
1246 if(nWritten == nToWrite)
1251 if(nWritten < 1 && totalWritten < 1){
1252 yaffsfs_SetError(-ENOSPC);
1262 yaffsfs_PutHandle(handle);
1265 if(totalWritten > 0)
1266 fd->position = startPos + totalWritten;
1268 yaffsfs_SetError(-EINVAL);
1274 return (totalWritten >= 0) ? totalWritten : -1;
1277 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1279 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1282 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1284 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1288 int yaffs_truncate(const YCHAR *path,off_t new_size)
1290 struct yaffs_obj *obj = NULL;
1291 struct yaffs_obj *dir = NULL;
1292 int result = YAFFS_FAIL;
1297 yaffsfs_SetError(-EFAULT);
1301 if(yaffsfs_CheckPath(path) < 0){
1302 yaffsfs_SetError(-ENAMETOOLONG);
1308 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1309 obj = yaffsfs_FollowLink(obj,0,&loop);
1312 yaffsfs_SetError(-ENOTDIR);
1314 yaffsfs_SetError(-ELOOP);
1315 else if(!dir || !obj)
1316 yaffsfs_SetError(-ENOENT);
1317 else if(obj->my_dev->read_only)
1318 yaffsfs_SetError(-EROFS);
1319 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1320 yaffsfs_SetError(-EISDIR);
1321 else if(obj->my_dev->read_only)
1322 yaffsfs_SetError(-EROFS);
1323 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1324 yaffsfs_SetError(-EINVAL);
1326 result = yaffs_resize_file(obj,new_size);
1330 return (result) ? 0 : -1;
1333 int yaffs_ftruncate(int handle, off_t new_size)
1335 yaffsfs_FileDes *fd = NULL;
1336 struct yaffs_obj *obj = NULL;
1340 fd = yaffsfs_HandleToFileDes(handle);
1341 obj = yaffsfs_HandleToObject(handle);
1345 yaffsfs_SetError(-EBADF);
1346 else if(!fd->writing)
1347 yaffsfs_SetError(-EINVAL);
1348 else if(obj->my_dev->read_only)
1349 yaffsfs_SetError(-EROFS);
1350 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1351 yaffsfs_SetError(-EINVAL);
1353 /* resize the file */
1354 result = yaffs_resize_file(obj,new_size);
1357 return (result) ? 0 : -1;
1361 off_t yaffs_lseek(int handle, off_t offset, int whence)
1363 yaffsfs_FileDes *fd = NULL;
1364 struct yaffs_obj *obj = NULL;
1369 fd = yaffsfs_HandleToFileDes(handle);
1370 obj = yaffsfs_HandleToObject(handle);
1373 yaffsfs_SetError(-EBADF);
1374 else if(offset > YAFFS_MAX_FILE_SIZE)
1375 yaffsfs_SetError(-EINVAL);
1377 if(whence == SEEK_SET){
1380 } else if(whence == SEEK_CUR) {
1381 if( (fd->position + offset) >= 0)
1382 pos = (fd->position + offset);
1383 } else if(whence == SEEK_END) {
1384 fSize = yaffs_get_obj_length(obj);
1385 if(fSize >= 0 && (fSize + offset) >= 0)
1386 pos = fSize + offset;
1389 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1392 yaffsfs_SetError(-EINVAL);
1403 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1405 struct yaffs_obj *dir = NULL;
1406 struct yaffs_obj *obj = NULL;
1408 int result = YAFFS_FAIL;
1413 yaffsfs_SetError(-EFAULT);
1417 if(yaffsfs_CheckPath(path) < 0){
1418 yaffsfs_SetError(-ENAMETOOLONG);
1424 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1425 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1428 yaffsfs_SetError(-ENOTDIR);
1430 yaffsfs_SetError(-ELOOP);
1432 yaffsfs_SetError(-ENOENT);
1433 else if(strncmp(name,_Y("."),2) == 0)
1434 yaffsfs_SetError(-EINVAL);
1436 yaffsfs_SetError(-ENOENT);
1437 else if(obj->my_dev->read_only)
1438 yaffsfs_SetError(-EROFS);
1439 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1440 yaffsfs_SetError(-EISDIR);
1441 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1442 yaffsfs_SetError(-ENOTDIR);
1443 else if(isDirectory && obj == obj->my_dev->root_dir)
1444 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1446 result = yaffs_unlinker(dir,name);
1448 if(result == YAFFS_FAIL && isDirectory)
1449 yaffsfs_SetError(-ENOTEMPTY);
1454 return (result == YAFFS_FAIL) ? -1 : 0;
1458 int yaffs_unlink(const YCHAR *path)
1460 return yaffsfs_DoUnlink(path,0);
1463 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1465 struct yaffs_obj *olddir = NULL;
1466 struct yaffs_obj *newdir = NULL;
1467 struct yaffs_obj *obj = NULL;
1468 struct yaffs_obj *newobj = NULL;
1471 int result= YAFFS_FAIL;
1472 int rename_allowed = 1;
1478 YCHAR *alt_newpath=NULL;
1480 if(!oldPath || !newPath){
1481 yaffsfs_SetError(-EFAULT);
1485 if(yaffsfs_CheckPath(oldPath) < 0 ||
1486 yaffsfs_CheckPath(newPath) < 0){
1487 yaffsfs_SetError(-ENAMETOOLONG);
1491 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1492 yaffsfs_SetError(-ENOMEM);
1496 newPath = alt_newpath;
1501 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1502 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1503 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1504 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1506 /* If the object being renamed is a directory and the
1507 * path ended with a "/" then the olddir == obj.
1508 * We pass through NULL for the old name to tell the lower layers
1509 * to use olddir as the object.
1515 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1516 yaffsfs_SetError(-ENOTDIR);
1518 } else if(oldLoop || newLoop) {
1519 yaffsfs_SetError(-ELOOP);
1521 } else if (olddir && oldname && strncmp(oldname, _Y("."),2) == 0){
1522 yaffsfs_SetError(-EINVAL);
1524 }else if(!olddir || !newdir || !obj) {
1525 yaffsfs_SetError(-ENOENT);
1527 } else if(obj->my_dev->read_only){
1528 yaffsfs_SetError(-EROFS);
1530 } else if(yaffs_is_non_empty_dir(newobj)){
1531 yaffsfs_SetError(-ENOTEMPTY);
1533 } else if(olddir->my_dev != newdir->my_dev) {
1534 /* Rename must be on same device */
1535 yaffsfs_SetError(-EXDEV);
1537 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1539 * It is a directory, check that it is not being renamed to
1540 * being its own decendent.
1541 * Do this by tracing from the new directory back to the root,
1545 struct yaffs_obj *xx = newdir;
1547 while( rename_allowed && xx){
1553 yaffsfs_SetError(-EINVAL);
1557 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1564 return (result == YAFFS_FAIL) ? -1 : 0;
1568 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1572 obj = yaffs_get_equivalent_obj(obj);
1575 buf->st_dev = (int)obj->my_dev->os_context;
1576 buf->st_ino = obj->obj_id;
1577 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1579 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1580 buf->st_mode |= S_IFDIR;
1581 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1582 buf->st_mode |= S_IFLNK;
1583 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1584 buf->st_mode |= S_IFREG;
1586 buf->st_nlink = yaffs_get_obj_link_count(obj);
1589 buf->st_rdev = obj->yst_rdev;
1590 buf->st_size = yaffs_get_obj_length(obj);
1591 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1592 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1593 #if CONFIG_YAFFS_WINCE
1594 buf->yst_wince_atime[0] = obj->win_atime[0];
1595 buf->yst_wince_atime[1] = obj->win_atime[1];
1596 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1597 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1598 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1599 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1601 buf->yst_atime = obj->yst_atime;
1602 buf->yst_ctime = obj->yst_ctime;
1603 buf->yst_mtime = obj->yst_mtime;
1610 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1612 struct yaffs_obj *obj=NULL;
1613 struct yaffs_obj *dir=NULL;
1619 yaffsfs_SetError(-EFAULT);
1623 if(yaffsfs_CheckPath(path) < 0){
1624 yaffsfs_SetError(-ENAMETOOLONG);
1630 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1633 obj = yaffsfs_FollowLink(obj,0,&loop);
1636 yaffsfs_SetError(-ENOTDIR);
1638 yaffsfs_SetError(-ELOOP);
1639 else if(!dir || !obj)
1640 yaffsfs_SetError(-ENOENT);
1642 retVal = yaffsfs_DoStat(obj,buf);
1650 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1652 return yaffsfs_DoStatOrLStat(path,buf,0);
1655 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1657 return yaffsfs_DoStatOrLStat(path,buf,1);
1660 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1662 struct yaffs_obj *obj;
1667 yaffsfs_SetError(-EFAULT);
1672 obj = yaffsfs_HandleToObject(fd);
1675 retVal = yaffsfs_DoStat(obj,buf);
1678 yaffsfs_SetError(-EBADF);
1685 static int yaffsfs_DoUtime(struct yaffs_obj *obj,const struct yaffs_utimbuf *buf)
1690 struct yaffs_utimbuf local;
1692 obj = yaffs_get_equivalent_obj(obj);
1694 if(obj && obj->my_dev->read_only) {
1695 yaffsfs_SetError(-EROFS);
1701 local.actime = Y_CURRENT_TIME;
1702 local.modtime = local.actime;
1707 obj->yst_atime = buf->actime;
1708 obj->yst_mtime = buf->modtime;
1710 result = yaffs_flush_file(obj,0,0);
1711 retVal = result == YAFFS_OK ? 0 : -1;
1717 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1719 struct yaffs_obj *obj=NULL;
1720 struct yaffs_obj *dir=NULL;
1726 yaffsfs_SetError(-EFAULT);
1730 if(yaffsfs_CheckPath(path) < 0){
1731 yaffsfs_SetError(-ENAMETOOLONG);
1737 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1740 yaffsfs_SetError(-ENOTDIR);
1742 yaffsfs_SetError(-ELOOP);
1743 else if(!dir || !obj)
1744 yaffsfs_SetError(-ENOENT);
1746 retVal = yaffsfs_DoUtime(obj,buf);
1753 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1755 struct yaffs_obj *obj;
1760 obj = yaffsfs_HandleToObject(fd);
1763 retVal = yaffsfs_DoUtime(obj,buf);
1766 yaffsfs_SetError(-EBADF);
1774 #ifndef CONFIG_YAFFS_WINCE
1775 /* xattrib functions */
1778 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1779 const void *data, int size, int flags, int follow)
1781 struct yaffs_obj *obj;
1782 struct yaffs_obj *dir;
1788 if(!path || !name || !data){
1789 yaffsfs_SetError(-EFAULT);
1793 if(yaffsfs_CheckPath(path) < 0){
1794 yaffsfs_SetError(-ENAMETOOLONG);
1800 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1803 obj = yaffsfs_FollowLink(obj,0,&loop);
1806 yaffsfs_SetError(-ENOTDIR);
1808 yaffsfs_SetError(-ELOOP);
1809 else if(!dir || !obj)
1810 yaffsfs_SetError(-ENOENT);
1812 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1814 yaffsfs_SetError(retVal);
1825 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1827 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1830 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1832 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1837 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1839 struct yaffs_obj *obj;
1844 yaffsfs_SetError(-EFAULT);
1849 obj = yaffsfs_HandleToObject(fd);
1852 yaffsfs_SetError(-EBADF);
1854 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1856 yaffsfs_SetError(retVal);
1866 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1868 struct yaffs_obj *obj;
1869 struct yaffs_obj *dir;
1874 if(!path || !name || !data ){
1875 yaffsfs_SetError(-EFAULT);
1879 if(yaffsfs_CheckPath(path) < 0){
1880 yaffsfs_SetError(-ENAMETOOLONG);
1886 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1889 obj = yaffsfs_FollowLink(obj,0,&loop);
1892 yaffsfs_SetError(-ENOTDIR);
1894 yaffsfs_SetError(-ELOOP);
1895 else if(!dir || !obj)
1896 yaffsfs_SetError(-ENOENT);
1898 retVal = yaffs_get_xattrib(obj,name,data,size);
1900 yaffsfs_SetError(retVal);
1910 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1912 return yaffs_do_getxattr( path, name, data, size, 1);
1914 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1916 return yaffs_do_getxattr( path, name, data, size, 0);
1921 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1923 struct yaffs_obj *obj;
1927 if(!name || !data ){
1928 yaffsfs_SetError(-EFAULT);
1933 obj = yaffsfs_HandleToObject(fd);
1936 retVal = yaffs_get_xattrib(obj,name,data,size);
1938 yaffsfs_SetError(retVal);
1943 yaffsfs_SetError(-EBADF);
1950 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1952 struct yaffs_obj *obj=NULL;
1953 struct yaffs_obj *dir=NULL;
1958 if(!path || !data ){
1959 yaffsfs_SetError(-EFAULT);
1963 if(yaffsfs_CheckPath(path) < 0){
1964 yaffsfs_SetError(-ENAMETOOLONG);
1970 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1973 obj = yaffsfs_FollowLink(obj,0,&loop);
1976 yaffsfs_SetError(-ENOTDIR);
1978 yaffsfs_SetError(-ELOOP);
1979 else if(!dir || !obj)
1980 yaffsfs_SetError(-ENOENT);
1982 retVal = yaffs_list_xattrib(obj, data,size);
1984 yaffsfs_SetError(retVal);
1995 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1997 return yaffs_do_listxattr(path, data, size, 1);
2000 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2002 return yaffs_do_listxattr(path, data, size, 0);
2005 int yaffs_flistxattr(int fd, char *data, int size)
2007 struct yaffs_obj *obj;
2012 yaffsfs_SetError(-EFAULT);
2017 obj = yaffsfs_HandleToObject(fd);
2020 retVal = yaffs_list_xattrib(obj,data,size);
2022 yaffsfs_SetError(retVal);
2027 yaffsfs_SetError(-EBADF);
2034 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
2036 struct yaffs_obj *obj=NULL;
2037 struct yaffs_obj *dir=NULL;
2043 yaffsfs_SetError(-EFAULT);
2047 if(yaffsfs_CheckPath(path) < 0){
2048 yaffsfs_SetError(-ENAMETOOLONG);
2054 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2057 obj = yaffsfs_FollowLink(obj,0,&loop);
2060 yaffsfs_SetError(-ENOTDIR);
2062 yaffsfs_SetError(-ELOOP);
2063 else if(!dir || !obj)
2064 yaffsfs_SetError(-ENOENT);
2066 retVal = yaffs_remove_xattrib(obj,name);
2068 yaffsfs_SetError(retVal);
2079 int yaffs_removexattr(const YCHAR *path, const char *name)
2081 return yaffs_do_removexattr(path, name, 1);
2084 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2086 return yaffs_do_removexattr(path, name, 0);
2089 int yaffs_fremovexattr(int fd, const char *name)
2091 struct yaffs_obj *obj;
2096 yaffsfs_SetError(-EFAULT);
2101 obj = yaffsfs_HandleToObject(fd);
2104 retVal = yaffs_remove_xattrib(obj,name);
2106 yaffsfs_SetError(retVal);
2111 yaffsfs_SetError(-EBADF);
2119 #ifdef CONFIG_YAFFS_WINCE
2120 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
2122 struct yaffs_obj *obj;
2127 obj = yaffsfs_HandleToObject(fd);
2132 wctime[0] = obj->win_ctime[0];
2133 wctime[1] = obj->win_ctime[1];
2136 watime[0] = obj->win_atime[0];
2137 watime[1] = obj->win_atime[1];
2140 wmtime[0] = obj->win_mtime[0];
2141 wmtime[1] = obj->win_mtime[1];
2148 yaffsfs_SetError(-EBADF);
2156 int yaffs_set_wince_times(int fd,
2157 const unsigned *wctime,
2158 const unsigned *watime,
2159 const unsigned *wmtime)
2161 struct yaffs_obj *obj;
2166 obj = yaffsfs_HandleToObject(fd);
2171 obj->win_ctime[0] = wctime[0];
2172 obj->win_ctime[1] = wctime[1];
2175 obj->win_atime[0] = watime[0];
2176 obj->win_atime[1] = watime[1];
2179 obj->win_mtime[0] = wmtime[0];
2180 obj->win_mtime[1] = wmtime[1];
2184 result = yaffs_flush_file(obj,0,0);
2188 yaffsfs_SetError(-EBADF);
2198 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2203 obj = yaffs_get_equivalent_obj(obj);
2206 obj->yst_mode = mode;
2208 result = yaffs_flush_file(obj,0,0);
2211 return result == YAFFS_OK ? 0 : -1;
2215 int yaffs_access(const YCHAR *path, int amode)
2217 struct yaffs_obj *obj=NULL;
2218 struct yaffs_obj *dir=NULL;
2224 yaffsfs_SetError(-EFAULT);
2228 if(yaffsfs_CheckPath(path) < 0){
2229 yaffsfs_SetError(-ENAMETOOLONG);
2233 if(amode & ~(R_OK | W_OK | X_OK)){
2234 yaffsfs_SetError(-EINVAL);
2240 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2241 obj = yaffsfs_FollowLink(obj,0,&loop);
2244 yaffsfs_SetError(-ENOTDIR);
2246 yaffsfs_SetError(-ELOOP);
2247 else if(!dir || !obj)
2248 yaffsfs_SetError(-ENOENT);
2249 else if((amode & W_OK) && obj->my_dev->read_only)
2250 yaffsfs_SetError(-EROFS);
2254 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2256 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2258 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2262 yaffsfs_SetError(-EACCES);
2274 int yaffs_chmod(const YCHAR *path, mode_t mode)
2276 struct yaffs_obj *obj=NULL;
2277 struct yaffs_obj *dir=NULL;
2283 yaffsfs_SetError(-EFAULT);
2287 if(yaffsfs_CheckPath(path) < 0){
2288 yaffsfs_SetError(-ENAMETOOLONG);
2293 yaffsfs_SetError(-EINVAL);
2299 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2300 obj = yaffsfs_FollowLink(obj,0,&loop);
2303 yaffsfs_SetError(-ENOTDIR);
2305 yaffsfs_SetError(-ELOOP);
2306 else if(!dir || !obj)
2307 yaffsfs_SetError(-ENOENT);
2308 else if(obj->my_dev->read_only)
2309 yaffsfs_SetError(-EROFS);
2311 retVal = yaffsfs_DoChMod(obj,mode);
2320 int yaffs_fchmod(int fd, mode_t mode)
2322 struct yaffs_obj *obj;
2326 yaffsfs_SetError(-EINVAL);
2331 obj = yaffsfs_HandleToObject(fd);
2334 yaffsfs_SetError(-EBADF);
2335 else if(obj->my_dev->read_only)
2336 yaffsfs_SetError(-EROFS);
2338 retVal = yaffsfs_DoChMod(obj,mode);
2345 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2347 struct yaffs_obj *parent = NULL;
2348 struct yaffs_obj *dir = NULL;
2350 YCHAR *alt_path = NULL;
2356 yaffsfs_SetError(-EFAULT);
2360 if(yaffsfs_CheckPath(path) < 0){
2361 yaffsfs_SetError(-ENAMETOOLONG);
2365 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2366 yaffsfs_SetError(-ENOMEM);
2373 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2374 if(!parent && notDir)
2375 yaffsfs_SetError(-ENOTDIR);
2377 yaffsfs_SetError(-ELOOP);
2379 yaffsfs_SetError(-ENOENT);
2380 else if(yaffsfs_TooManyObjects(parent->my_dev))
2381 yaffsfs_SetError(-ENFILE);
2382 else if(strnlen(name,5) == 0){
2383 /* Trying to make the root itself */
2384 yaffsfs_SetError(-EEXIST);
2385 } else if(parent->my_dev->read_only)
2386 yaffsfs_SetError(-EROFS);
2388 dir = yaffs_create_dir(parent,name,mode,0,0);
2391 else if (yaffs_find_by_name(parent,name))
2392 yaffsfs_SetError(-EEXIST); /* the name already exists */
2394 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2405 int yaffs_rmdir(const YCHAR *path)
2411 yaffsfs_SetError(-EFAULT);
2415 if(yaffsfs_CheckPath(path) < 0){
2416 yaffsfs_SetError(-ENAMETOOLONG);
2420 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2421 yaffsfs_SetError(-ENOMEM);
2426 result = yaffsfs_DoUnlink(path,1);
2433 void * yaffs_getdev(const YCHAR *path)
2435 struct yaffs_dev *dev=NULL;
2437 dev = yaffsfs_FindDevice(path,&dummy);
2441 int yaffs_mount2(const YCHAR *path,int read_only)
2444 int result=YAFFS_FAIL;
2445 struct yaffs_dev *dev=NULL;
2448 yaffsfs_SetError(-EFAULT);
2452 yaffs_trace(YAFFS_TRACE_MOUNT,"yaffs: Mounting %s",path);
2454 if(yaffsfs_CheckPath(path) < 0){
2455 yaffsfs_SetError(-ENAMETOOLONG);
2461 yaffsfs_InitHandles();
2463 dev = yaffsfs_FindMountPoint(path);
2465 if(!dev->is_mounted){
2466 dev->read_only = read_only ? 1 : 0;
2467 result = yaffs_guts_initialise(dev);
2468 if(result == YAFFS_FAIL)
2469 yaffsfs_SetError(-ENOMEM);
2470 retVal = result ? 0 : -1;
2474 yaffsfs_SetError(-EBUSY);
2476 yaffsfs_SetError(-ENODEV);
2483 int yaffs_mount(const YCHAR *path)
2485 return yaffs_mount2(path,0);
2488 int yaffs_sync(const YCHAR *path)
2491 struct yaffs_dev *dev=NULL;
2495 yaffsfs_SetError(-EFAULT);
2499 if(yaffsfs_CheckPath(path) < 0){
2500 yaffsfs_SetError(-ENAMETOOLONG);
2505 dev = yaffsfs_FindDevice(path,&dummy);
2507 if(!dev->is_mounted)
2508 yaffsfs_SetError(-EINVAL);
2509 else if(dev->read_only)
2510 yaffsfs_SetError(-EROFS);
2513 yaffs_flush_whole_cache(dev);
2514 yaffs_checkpoint_save(dev);
2519 yaffsfs_SetError(-ENODEV);
2526 static int yaffsfs_IsDevBusy(struct yaffs_dev * dev)
2529 struct yaffs_obj *obj;
2531 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
2532 obj = yaffsfs_HandleToObject(i);
2533 if(obj && obj->my_dev == dev)
2540 int yaffs_remount(const YCHAR *path, int force, int read_only)
2543 struct yaffs_dev *dev=NULL;
2546 yaffsfs_SetError(-EFAULT);
2550 if(yaffsfs_CheckPath(path) < 0){
2551 yaffsfs_SetError(-ENAMETOOLONG);
2556 dev = yaffsfs_FindMountPoint(path);
2558 if(dev->is_mounted){
2559 yaffs_flush_whole_cache(dev);
2561 if(force || ! yaffsfs_IsDevBusy(dev)){
2563 yaffs_checkpoint_save(dev);
2564 dev->read_only = read_only ? 1 : 0;
2567 yaffsfs_SetError(-EBUSY);
2570 yaffsfs_SetError(-EINVAL);
2574 yaffsfs_SetError(-ENODEV);
2581 int yaffs_unmount2(const YCHAR *path, int force)
2584 struct yaffs_dev *dev=NULL;
2587 yaffsfs_SetError(-EFAULT);
2591 if(yaffsfs_CheckPath(path) < 0){
2592 yaffsfs_SetError(-ENAMETOOLONG);
2597 dev = yaffsfs_FindMountPoint(path);
2599 if(dev->is_mounted){
2601 yaffs_flush_whole_cache(dev);
2602 yaffs_checkpoint_save(dev);
2603 inUse = yaffsfs_IsDevBusy(dev);
2604 if(!inUse || force){
2606 yaffsfs_BreakDeviceHandles(dev);
2607 yaffs_deinitialise(dev);
2611 yaffsfs_SetError(-EBUSY);
2614 yaffsfs_SetError(-EINVAL);
2617 yaffsfs_SetError(-ENODEV);
2624 int yaffs_unmount(const YCHAR *path)
2626 return yaffs_unmount2(path,0);
2629 loff_t yaffs_freespace(const YCHAR *path)
2632 struct yaffs_dev *dev=NULL;
2636 yaffsfs_SetError(-EFAULT);
2640 if(yaffsfs_CheckPath(path) < 0){
2641 yaffsfs_SetError(-ENAMETOOLONG);
2646 dev = yaffsfs_FindDevice(path,&dummy);
2647 if(dev && dev->is_mounted){
2648 retVal = yaffs_get_n_free_chunks(dev);
2649 retVal *= dev->data_bytes_per_chunk;
2652 yaffsfs_SetError(-EINVAL);
2658 loff_t yaffs_totalspace(const YCHAR *path)
2661 struct yaffs_dev *dev=NULL;
2665 yaffsfs_SetError(-EFAULT);
2669 if(yaffsfs_CheckPath(path) < 0){
2670 yaffsfs_SetError(-ENAMETOOLONG);
2675 dev = yaffsfs_FindDevice(path,&dummy);
2676 if(dev && dev->is_mounted){
2677 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2678 dev->param.n_reserved_blocks;
2679 retVal *= dev->param.chunks_per_block;
2680 retVal *= dev->data_bytes_per_chunk;
2683 yaffsfs_SetError(-EINVAL);
2689 int yaffs_inodecount(const YCHAR *path)
2692 struct yaffs_dev *dev=NULL;
2696 yaffsfs_SetError(-EFAULT);
2700 if(yaffsfs_CheckPath(path) < 0){
2701 yaffsfs_SetError(-ENAMETOOLONG);
2706 dev = yaffsfs_FindDevice(path,&dummy);
2707 if(dev && dev->is_mounted) {
2708 int n_obj = dev->n_obj;
2709 if(n_obj > dev->n_hardlinks)
2710 retVal = n_obj - dev->n_hardlinks;
2714 yaffsfs_SetError(-EINVAL);
2721 void yaffs_add_device(struct yaffs_dev *dev)
2723 struct list_head *cfg;
2724 /* First check that the device is not in the list. */
2726 list_for_each(cfg, &yaffsfs_deviceList){
2727 if(dev == list_entry(cfg, struct yaffs_dev, dev_list))
2731 dev->is_mounted = 0;
2732 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2734 if(!dev->dev_list.next)
2735 INIT_LIST_HEAD(&dev->dev_list);
2737 list_add(&dev->dev_list,&yaffsfs_deviceList);
2740 void yaffs_remove_device(struct yaffs_dev *dev)
2742 list_del_init(&dev->dev_list);
2748 /* Directory search stuff. */
2751 * Directory search context
2753 * NB this is an opaque structure.
2760 yaffs_dirent de; /* directory entry being used by this dsc */
2761 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2762 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2763 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2765 struct list_head others;
2766 } yaffsfs_DirectorySearchContext;
2770 static struct list_head search_contexts;
2773 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2777 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2781 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2782 dsc->nextReturn = NULL;
2784 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2785 struct yaffs_obj,siblings);
2787 /* Hey someone isn't playing nice! */
2791 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2795 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2797 if( dsc->nextReturn == NULL ||
2798 list_empty(&dsc->dirObj->variant.dir_variant.children))
2799 dsc->nextReturn = NULL;
2801 struct list_head *next = dsc->nextReturn->siblings.next;
2803 if( next == &dsc->dirObj->variant.dir_variant.children)
2804 dsc->nextReturn = NULL; /* end of list */
2806 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2809 /* Hey someone isn't playing nice! */
2813 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2816 struct list_head *i;
2817 yaffsfs_DirectorySearchContext *dsc;
2819 /* if search contexts not initilised then skip */
2820 if(!search_contexts.next)
2823 /* Iterate through the directory search contexts.
2824 * If any are the one being removed, then advance the dsc to
2825 * the next one to prevent a hanging ptr.
2827 list_for_each(i, &search_contexts) {
2829 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2830 if(dsc->nextReturn == obj)
2831 yaffsfs_DirAdvance(dsc);
2837 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2839 yaffs_DIR *dir = NULL;
2840 struct yaffs_obj *obj = NULL;
2841 yaffsfs_DirectorySearchContext *dsc = NULL;
2846 yaffsfs_SetError(-EFAULT);
2850 if(yaffsfs_CheckPath(dirname) < 0){
2851 yaffsfs_SetError(-ENAMETOOLONG);
2857 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2860 yaffsfs_SetError(-ENOTDIR);
2862 yaffsfs_SetError(-ELOOP);
2864 yaffsfs_SetError(-ENOENT);
2865 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2866 yaffsfs_SetError(-ENOTDIR);
2869 dsc = kmalloc(sizeof(yaffsfs_DirectorySearchContext), 0);
2870 dir = (yaffs_DIR *)dsc;
2873 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2874 dsc->magic = YAFFS_MAGIC;
2876 strncpy(dsc->name,dirname,NAME_MAX);
2877 INIT_LIST_HEAD(&dsc->others);
2879 if(!search_contexts.next)
2880 INIT_LIST_HEAD(&search_contexts);
2882 list_add(&dsc->others,&search_contexts);
2883 yaffsfs_SetDirRewound(dsc);
2893 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2895 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2896 struct yaffs_dirent *retVal = NULL;
2900 if(dsc && dsc->magic == YAFFS_MAGIC){
2901 yaffsfs_SetError(0);
2902 if(dsc->nextReturn){
2903 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2904 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2905 dsc->de.d_off = dsc->offset++;
2906 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2907 if(strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2909 /* this should not happen! */
2910 strcpy(dsc->de.d_name,_Y("zz"));
2912 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2914 yaffsfs_DirAdvance(dsc);
2918 yaffsfs_SetError(-EBADF);
2927 void yaffs_rewinddir(yaffs_DIR *dirp)
2929 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2933 yaffsfs_SetDirRewound(dsc);
2939 int yaffs_closedir(yaffs_DIR *dirp)
2941 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2944 yaffsfs_SetError(-EFAULT);
2950 list_del(&dsc->others); /* unhook from list */
2956 /* End of directory stuff */
2959 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2961 struct yaffs_obj *parent = NULL;
2962 struct yaffs_obj *obj;
2965 int mode = 0; /* ignore for now */
2969 if(!oldpath || !newpath){
2970 yaffsfs_SetError(-EFAULT);
2974 if(yaffsfs_CheckPath(newpath) < 0 ||
2975 yaffsfs_CheckPath(oldpath) < 0){
2976 yaffsfs_SetError(-ENAMETOOLONG);
2981 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2982 if(!parent && notDir)
2983 yaffsfs_SetError(-ENOTDIR);
2985 yaffsfs_SetError(-ELOOP);
2986 else if( !parent || strnlen(name,5) < 1)
2987 yaffsfs_SetError(-ENOENT);
2988 else if(yaffsfs_TooManyObjects(parent->my_dev))
2989 yaffsfs_SetError(-ENFILE);
2990 else if(parent->my_dev->read_only)
2991 yaffsfs_SetError(-EROFS);
2993 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2996 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2997 yaffsfs_SetError(-EEXIST);
2999 yaffsfs_SetError(-ENOSPC);
3008 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3010 struct yaffs_obj *obj = NULL;
3011 struct yaffs_obj *dir = NULL;
3017 yaffsfs_SetError(-EFAULT);
3023 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
3026 yaffsfs_SetError(-ENOTDIR);
3028 yaffsfs_SetError(-ELOOP);
3029 else if(!dir || !obj)
3030 yaffsfs_SetError(-ENOENT);
3031 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3032 yaffsfs_SetError(-EINVAL);
3034 YCHAR *alias = obj->variant.symlink_variant.alias;
3035 memset(buf,0,bufsiz);
3036 strncpy(buf,alias,bufsiz - 1);
3043 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3045 /* Creates a link called newpath to existing oldpath */
3046 struct yaffs_obj *obj = NULL;
3047 struct yaffs_obj *lnk = NULL;
3048 struct yaffs_obj *obj_dir = NULL;
3049 struct yaffs_obj *lnk_dir = NULL;
3057 if(!oldpath || !linkpath){
3058 yaffsfs_SetError(-EFAULT);
3062 if(yaffsfs_CheckPath(linkpath) < 0 ||
3063 yaffsfs_CheckPath(oldpath) < 0){
3064 yaffsfs_SetError(-ENAMETOOLONG);
3070 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
3071 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
3072 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
3074 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3075 yaffsfs_SetError(-ENOTDIR);
3076 else if(objLoop || lnkLoop)
3077 yaffsfs_SetError(-ELOOP);
3078 else if(!obj_dir || !lnk_dir || !obj)
3079 yaffsfs_SetError(-ENOENT);
3080 else if(obj->my_dev->read_only)
3081 yaffsfs_SetError(-EROFS);
3082 else if(yaffsfs_TooManyObjects(obj->my_dev))
3083 yaffsfs_SetError(-ENFILE);
3085 yaffsfs_SetError(-EEXIST);
3086 else if(lnk_dir->my_dev != obj->my_dev)
3087 yaffsfs_SetError(-EXDEV);
3089 retVal = yaffsfs_CheckNameLength(newname);
3092 lnk = yaffs_link_obj(lnk_dir,newname,obj);
3096 yaffsfs_SetError(-ENOSPC);
3106 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3112 yaffsfs_SetError(-EINVAL);
3118 * D E B U G F U N C T I O N S
3123 * Returns number of handles attached to the object
3125 int yaffs_n_handles(const YCHAR *path)
3127 struct yaffs_obj *obj;
3130 yaffsfs_SetError(-EFAULT);
3134 if(yaffsfs_CheckPath(path) < 0){
3135 yaffsfs_SetError(-ENAMETOOLONG);
3139 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
3142 return yaffsfs_CountHandles(obj);
3147 int yaffs_get_error(void)
3149 return yaffsfs_GetLastError();
3152 int yaffs_set_error(int error)
3154 yaffsfs_SetError(error);
3158 int yaffs_dump_dev(const YCHAR *path)
3165 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
3168 struct yaffs_dev *dev = obj->my_dev;
3171 "n_page_writes.......... %d\n"
3172 "n_page_reads........... %d\n"
3173 "n_erasures....... %d\n"
3174 "n_gc_copies............ %d\n"
3175 "garbageCollections... %d\n"
3176 "passiveGarbageColl'ns %d\n"
3182 dev->garbageCollections,
3183 dev->passiveGarbageCollections