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.
383 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
385 static int yaffs_toupper(YCHAR a)
387 if(a >= 'a' && a <= 'z')
388 return (a - 'a') + 'A';
393 int yaffsfs_Match(YCHAR a, YCHAR b)
395 return (yaffs_toupper(a) == yaffs_toupper(b));
399 int yaffsfs_Match(YCHAR a, YCHAR b)
406 int yaffsfs_IsPathDivider(YCHAR ch)
408 const YCHAR *str = YAFFS_PATH_DIVIDERS;
419 int yaffsfs_CheckNameLength(const char *name)
423 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
426 yaffsfs_SetError(-ENOENT);
428 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
429 yaffsfs_SetError(-ENAMETOOLONG);
437 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
439 YCHAR *alt_path = NULL;
444 * We don't have a definition for max path length.
445 * We will use 3 * max name length instead.
448 path_length = yaffs_strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
450 /* If the last character is a path divider, then we need to
451 * trim it back so that the name look-up works properly.
452 * eg. /foo/new_dir/ -> /foo/newdir
453 * Curveball: Need to handle multiple path dividers:
454 * eg. /foof/sdfse///// -> /foo/sdfse
456 if(path_length > 0 &&
457 yaffsfs_IsPathDivider(path[path_length-1])){
458 alt_path = kmalloc(path_length + 1, 0);
461 strcpy(alt_path, path);
462 for(i = path_length-1;
463 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
465 alt_path[i] = (YCHAR) 0;
467 *ret_path = alt_path;
472 LIST_HEAD(yaffsfs_deviceList);
477 * Scan the configuration list to find the device
478 * Curveballs: Should match paths that end in '/' too
479 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
481 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
483 struct list_head *cfg;
484 const YCHAR *leftOver;
486 struct yaffs_dev *retval = NULL;
487 struct yaffs_dev *dev = NULL;
489 int longestMatch = -1;
493 * Check all configs, choose the one that:
494 * 1) Actually matches a prefix (ie /a amd /abc will not match
495 * 2) Matches the longest.
497 list_for_each(cfg, &yaffsfs_deviceList){
498 dev = list_entry(cfg, struct yaffs_dev, dev_list);
505 while(matching && *p && *leftOver){
506 /* Skip over any /s */
507 while(yaffsfs_IsPathDivider(*p))
510 /* Skip over any /s */
511 while(yaffsfs_IsPathDivider(*leftOver))
514 /* Now match the text part */
516 *p && !yaffsfs_IsPathDivider(*p) &&
517 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
518 if(yaffsfs_Match(*p,*leftOver)){
528 /* Skip over any /s in leftOver */
529 while(yaffsfs_IsPathDivider(*leftOver))
532 // Skip over any /s in p
533 while(yaffsfs_IsPathDivider(*p))
536 // p should now be at the end of the string (ie. fully matched)
540 if( matching && (thisMatchLength > longestMatch))
543 *restOfPath = (YCHAR *)leftOver;
545 longestMatch = thisMatchLength;
552 static int yaffsfs_CheckPath(const YCHAR *path)
556 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
557 if(yaffsfs_IsPathDivider(*path)){
565 return (*path) ? -1 : 0;
568 /* FindMountPoint only returns a dev entry if the path is a mount point */
569 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
571 struct yaffs_dev *dev;
572 YCHAR *restOfPath=NULL;
573 dev = yaffsfs_FindDevice(path,&restOfPath);
574 if(dev && restOfPath && *restOfPath)
579 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
582 struct yaffs_dev *dev;
584 dev= yaffsfs_FindDevice(path,restOfPath);
585 if(dev && dev->is_mounted){
586 return dev->root_dir;
591 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
592 int symDepth, int *loop)
596 obj = yaffs_get_equivalent_obj(obj);
598 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
599 YCHAR *alias = obj->variant.symlink_variant.alias;
601 if(yaffsfs_IsPathDivider(*alias))
602 /* Starts with a /, need to scan from root up */
603 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
606 /* Relative to here, so use the parent of the symlink as a start */
607 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
615 * yaffsfs_FindDirectory
616 * Parse a path to determine the directory and the name within the directory.
618 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
620 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
621 const YCHAR *path, YCHAR **name, int symDepth,
622 int *notDir,int *loop)
624 struct yaffs_obj *dir;
626 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
629 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
637 restOfPath = (YCHAR *)path;
640 dir = yaffsfs_FindRoot(path,&restOfPath);
646 * curve ball: also throw away surplus '/'
647 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
649 while(yaffsfs_IsPathDivider(*restOfPath))
650 restOfPath++; /* get rid of '/' */
655 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
656 if (i < YAFFS_MAX_NAME_LENGTH){
657 str[i] = *restOfPath;
665 /* got to the end of the string */
668 if(yaffs_strcmp(str,_Y(".")) == 0){
670 } else if(yaffs_strcmp(str,_Y("..")) == 0) {
673 dir = yaffs_find_by_name(dir,str);
675 dir = yaffsfs_FollowLink(dir,symDepth,loop);
677 if(dir && dir->variant_type !=
678 YAFFS_OBJECT_TYPE_DIRECTORY){
687 /* directory did not exist. */
691 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
698 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
702 * yaffsfs_FindObject turns a path for an existing object into the object
704 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
705 const YCHAR *path,int symDepth, int getEquiv,
706 struct yaffs_obj **dirOut, int *notDir,int *loop)
708 struct yaffs_obj *dir;
709 struct yaffs_obj *obj;
712 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
718 obj = yaffs_find_by_name(dir,name);
723 obj = yaffs_get_equivalent_obj(obj);
729 /*************************************************************************
730 * Start of yaffsfs visible functions.
731 *************************************************************************/
733 int yaffs_dup(int handle)
735 int newHandleNumber = -1;
736 yaffsfs_FileDes *existingFD = NULL;
737 yaffsfs_Handle *existingHandle = NULL;
738 yaffsfs_Handle *newHandle = NULL;
741 existingHandle = yaffsfs_HandleToPointer(handle);
742 existingFD = yaffsfs_HandleToFileDes(handle);
744 newHandleNumber = yaffsfs_NewHandle(&newHandle);
746 newHandle->fdId = existingHandle->fdId;
747 existingFD->handleCount++;
753 yaffsfs_SetError(-EBADF);
755 yaffsfs_SetError(-ENOMEM);
757 return newHandleNumber;
761 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
763 int current_objects = dev->n_obj - dev->n_deleted_files;
765 if(dev->param.max_objects && current_objects > dev->param.max_objects)
771 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
773 struct yaffs_obj *obj = NULL;
774 struct yaffs_obj *dir = NULL;
777 yaffsfs_FileDes *fd = NULL;
780 int errorReported = 0;
781 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
782 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
783 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
784 u8 sharedReadAllowed;
785 u8 sharedWriteAllowed;
794 yaffsfs_SetError(-EFAULT);
798 if(yaffsfs_CheckPath(path) < 0){
799 yaffsfs_SetError(-ENAMETOOLONG);
803 /* O_EXCL only has meaning if O_CREAT is specified */
804 if(!(oflag & O_CREAT))
807 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
808 if( (oflag & O_CREAT) & (oflag & O_EXCL))
811 /* Todo: Are there any more flag combos to sanitise ? */
813 /* Figure out if reading or writing is requested */
815 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
816 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
820 handle = yaffsfs_NewHandleAndFileDes();
823 yaffsfs_SetError(-ENFILE);
827 fd = yaffsfs_HandleToFileDes(handle);
829 /* try to find the exisiting object */
830 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
832 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
835 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
836 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
842 /* The file already exists or it might be a directory */
844 /* If it is a directory then we can't open it as a file */
845 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
847 yaffsfs_SetError(-EISDIR);
851 /* Open should fail if O_CREAT and O_EXCL are specified since
854 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
856 yaffsfs_SetError(-EEXIST);
860 /* Check file permissions */
861 if( readRequested && !(obj->yst_mode & S_IREAD))
864 if( writeRequested && !(obj->yst_mode & S_IWRITE))
867 if( !errorReported && writeRequested &&
868 obj->my_dev->read_only){
870 yaffsfs_SetError(-EROFS);
874 if(openDenied && !errorReported ) {
875 /* Error if the file exists but permissions are refused. */
876 yaffsfs_SetError(-EACCES);
880 /* Check sharing of an existing object. */
882 yaffsfs_FileDes *fdx;
885 sharedReadAllowed = 1;
886 sharedWriteAllowed = 1;
889 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
890 fdx = &yaffsfs_fd[i];
891 if(fdx->handleCount > 0 &&
893 yaffsfs_inode[fdx->inodeId].iObj == obj){
895 sharedReadAllowed = 0;
897 sharedWriteAllowed = 0;
907 if((!sharedReadAllowed && readRequested)||
908 (!shareRead && alreadyReading) ||
909 (!sharedWriteAllowed && writeRequested) ||
910 (!shareWrite && alreadyWriting)){
912 yaffsfs_SetError(-EBUSY);
919 /* If we could not open an existing object, then let's see if
920 * the directory exists. If not, error.
922 if(!obj && !errorReported){
923 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
925 yaffsfs_SetError(-ENOTDIR);
928 yaffsfs_SetError(-ELOOP);
931 yaffsfs_SetError(-ENOENT);
936 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
937 /* Let's see if we can create this file if it does not exist. */
938 if(dir->my_dev->read_only){
939 yaffsfs_SetError(-EROFS);
941 } else if(yaffsfs_TooManyObjects(dir->my_dev)) {
942 yaffsfs_SetError(-ENFILE);
945 obj = yaffs_create_file(dir,name,mode,0,0);
947 if(!obj && !errorReported){
948 yaffsfs_SetError(-ENOSPC);
953 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
954 /* Error if the file does not exist and CREAT is not set. */
955 yaffsfs_SetError(-ENOENT);
959 if(obj && !openDenied) {
960 int inodeId = yaffsfs_GetInodeIdForObject(obj);
964 * Todo: Fix any problem if inodes run out, though that
965 * can't happen if the number of inode items >= number of handles.
969 fd->inodeId = inodeId;
970 fd->reading = readRequested;
971 fd->writing = writeRequested;
972 fd->append = (oflag & O_APPEND) ? 1 : 0;
974 fd->shareRead = shareRead;
975 fd->shareWrite = shareWrite;
977 /* Hook inode to object */
978 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
980 if((oflag & O_TRUNC) && fd->writing)
981 yaffs_resize_file(obj,0);
983 yaffsfs_PutHandle(handle);
985 yaffsfs_SetError(0); /* Problem */
995 int yaffs_open(const YCHAR *path, int oflag, int mode)
997 return yaffs_open_sharing(path, oflag, mode,
998 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1001 int yaffs_Dofsync(int handle,int datasync)
1004 struct yaffs_obj *obj;
1008 obj = yaffsfs_HandleToObject(handle);
1011 yaffsfs_SetError(-EBADF);
1012 else if(obj->my_dev->read_only)
1013 yaffsfs_SetError(-EROFS);
1015 yaffs_flush_file(obj,1,datasync);
1024 int yaffs_fsync(int handle)
1026 return yaffs_Dofsync(handle,0);
1029 int yaffs_flush(int handle)
1031 return yaffs_fsync(handle);
1034 int yaffs_fdatasync(int handle)
1036 return yaffs_Dofsync(handle,1);
1039 int yaffs_close(int handle)
1041 yaffsfs_Handle *h = NULL;
1042 struct yaffs_obj *obj = NULL;
1047 h = yaffsfs_HandleToPointer(handle);
1048 obj = yaffsfs_HandleToObject(handle);
1051 yaffsfs_SetError(-EBADF);
1054 yaffs_flush_file(obj,1,0);
1055 yaffsfs_PutHandle(handle);
1066 int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte, int isPread, int offset)
1068 yaffsfs_FileDes *fd = NULL;
1069 struct yaffs_obj *obj = NULL;
1076 unsigned int maxRead;
1077 u8 *buf = (u8 *)vbuf;
1080 yaffsfs_SetError(-EFAULT);
1085 fd = yaffsfs_HandleToFileDes(handle);
1086 obj = yaffsfs_HandleToObject(handle);
1090 yaffsfs_SetError(-EBADF);
1092 } else if(!fd->reading){
1093 /* Not a reading handle */
1094 yaffsfs_SetError(-EINVAL);
1096 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
1097 yaffsfs_SetError(-EINVAL);
1103 startPos = fd->position;
1107 if(yaffs_get_obj_length(obj) > pos)
1108 maxRead = yaffs_get_obj_length(obj) - pos;
1116 yaffsfs_GetHandle(handle);
1118 endPos = pos + nbyte;
1120 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1121 nbyte > YAFFS_MAX_FILE_SIZE ||
1122 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1128 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1133 * Need to reverify object in case the device was
1134 * unmounted in another thread.
1136 obj = yaffsfs_HandleToObject(handle);
1140 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1148 if(nRead == nToRead)
1151 nbyte = 0; /* no more to read */
1161 yaffsfs_PutHandle(handle);
1165 fd->position = startPos + totalRead;
1167 yaffsfs_SetError(-EINVAL);
1174 return (totalRead >= 0) ? totalRead : -1;
1178 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1180 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1183 int yaffs_pread(int handle, void *buf, unsigned int nbyte, unsigned int offset)
1185 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1188 int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1190 yaffsfs_FileDes *fd = NULL;
1191 struct yaffs_obj *obj = NULL;
1196 int totalWritten = 0;
1197 int write_trhrough = 0;
1199 const u8 *buf = (const u8 *)vbuf;
1202 yaffsfs_SetError(-EFAULT);
1207 fd = yaffsfs_HandleToFileDes(handle);
1208 obj = yaffsfs_HandleToObject(handle);
1212 yaffsfs_SetError(-EBADF);
1214 } else if(!fd->writing){
1215 yaffsfs_SetError(-EINVAL);
1217 } else if(obj->my_dev->read_only){
1218 yaffsfs_SetError(-EROFS);
1222 startPos = yaffs_get_obj_length(obj);
1226 startPos = fd->position;
1228 yaffsfs_GetHandle(handle);
1230 endPos = pos + nbyte;
1232 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1233 nbyte > YAFFS_MAX_FILE_SIZE ||
1234 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1241 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1242 if(nToWrite > nbyte)
1246 * Need to reverify object in case the device was
1247 * remounted or unmounted in another thread.
1249 obj = yaffsfs_HandleToObject(handle);
1250 if(!obj || obj->my_dev->read_only)
1253 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1256 totalWritten += nWritten;
1261 if(nWritten == nToWrite)
1266 if(nWritten < 1 && totalWritten < 1){
1267 yaffsfs_SetError(-ENOSPC);
1277 yaffsfs_PutHandle(handle);
1280 if(totalWritten > 0)
1281 fd->position = startPos + totalWritten;
1283 yaffsfs_SetError(-EINVAL);
1289 return (totalWritten >= 0) ? totalWritten : -1;
1292 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1294 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1297 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1299 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1303 int yaffs_truncate(const YCHAR *path,off_t new_size)
1305 struct yaffs_obj *obj = NULL;
1306 struct yaffs_obj *dir = NULL;
1307 int result = YAFFS_FAIL;
1312 yaffsfs_SetError(-EFAULT);
1316 if(yaffsfs_CheckPath(path) < 0){
1317 yaffsfs_SetError(-ENAMETOOLONG);
1323 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1324 obj = yaffsfs_FollowLink(obj,0,&loop);
1327 yaffsfs_SetError(-ENOTDIR);
1329 yaffsfs_SetError(-ELOOP);
1330 else if(!dir || !obj)
1331 yaffsfs_SetError(-ENOENT);
1332 else if(obj->my_dev->read_only)
1333 yaffsfs_SetError(-EROFS);
1334 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1335 yaffsfs_SetError(-EISDIR);
1336 else if(obj->my_dev->read_only)
1337 yaffsfs_SetError(-EROFS);
1338 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1339 yaffsfs_SetError(-EINVAL);
1341 result = yaffs_resize_file(obj,new_size);
1345 return (result) ? 0 : -1;
1348 int yaffs_ftruncate(int handle, off_t new_size)
1350 yaffsfs_FileDes *fd = NULL;
1351 struct yaffs_obj *obj = NULL;
1355 fd = yaffsfs_HandleToFileDes(handle);
1356 obj = yaffsfs_HandleToObject(handle);
1360 yaffsfs_SetError(-EBADF);
1361 else if(!fd->writing)
1362 yaffsfs_SetError(-EINVAL);
1363 else if(obj->my_dev->read_only)
1364 yaffsfs_SetError(-EROFS);
1365 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1366 yaffsfs_SetError(-EINVAL);
1368 /* resize the file */
1369 result = yaffs_resize_file(obj,new_size);
1372 return (result) ? 0 : -1;
1376 off_t yaffs_lseek(int handle, off_t offset, int whence)
1378 yaffsfs_FileDes *fd = NULL;
1379 struct yaffs_obj *obj = NULL;
1384 fd = yaffsfs_HandleToFileDes(handle);
1385 obj = yaffsfs_HandleToObject(handle);
1388 yaffsfs_SetError(-EBADF);
1389 else if(offset > YAFFS_MAX_FILE_SIZE)
1390 yaffsfs_SetError(-EINVAL);
1392 if(whence == SEEK_SET){
1395 } else if(whence == SEEK_CUR) {
1396 if( (fd->position + offset) >= 0)
1397 pos = (fd->position + offset);
1398 } else if(whence == SEEK_END) {
1399 fSize = yaffs_get_obj_length(obj);
1400 if(fSize >= 0 && (fSize + offset) >= 0)
1401 pos = fSize + offset;
1404 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1407 yaffsfs_SetError(-EINVAL);
1418 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1420 struct yaffs_obj *dir = NULL;
1421 struct yaffs_obj *obj = NULL;
1423 int result = YAFFS_FAIL;
1428 yaffsfs_SetError(-EFAULT);
1432 if(yaffsfs_CheckPath(path) < 0){
1433 yaffsfs_SetError(-ENAMETOOLONG);
1439 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1440 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1443 yaffsfs_SetError(-ENOTDIR);
1445 yaffsfs_SetError(-ELOOP);
1447 yaffsfs_SetError(-ENOENT);
1448 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1449 yaffsfs_SetError(-EINVAL);
1451 yaffsfs_SetError(-ENOENT);
1452 else if(obj->my_dev->read_only)
1453 yaffsfs_SetError(-EROFS);
1454 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1455 yaffsfs_SetError(-EISDIR);
1456 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1457 yaffsfs_SetError(-ENOTDIR);
1458 else if(isDirectory && obj == obj->my_dev->root_dir)
1459 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1461 result = yaffs_unlinker(dir,name);
1463 if(result == YAFFS_FAIL && isDirectory)
1464 yaffsfs_SetError(-ENOTEMPTY);
1469 return (result == YAFFS_FAIL) ? -1 : 0;
1473 int yaffs_unlink(const YCHAR *path)
1475 return yaffsfs_DoUnlink(path,0);
1478 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1480 struct yaffs_obj *olddir = NULL;
1481 struct yaffs_obj *newdir = NULL;
1482 struct yaffs_obj *obj = NULL;
1483 struct yaffs_obj *newobj = NULL;
1486 int result= YAFFS_FAIL;
1487 int rename_allowed = 1;
1493 YCHAR *alt_newpath=NULL;
1495 if(!oldPath || !newPath){
1496 yaffsfs_SetError(-EFAULT);
1500 if(yaffsfs_CheckPath(oldPath) < 0 ||
1501 yaffsfs_CheckPath(newPath) < 0){
1502 yaffsfs_SetError(-ENAMETOOLONG);
1506 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1507 yaffsfs_SetError(-ENOMEM);
1511 newPath = alt_newpath;
1516 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1517 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1518 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1519 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1521 /* If the object being renamed is a directory and the
1522 * path ended with a "/" then the olddir == obj.
1523 * We pass through NULL for the old name to tell the lower layers
1524 * to use olddir as the object.
1530 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1531 yaffsfs_SetError(-ENOTDIR);
1533 } else if(oldLoop || newLoop) {
1534 yaffsfs_SetError(-ELOOP);
1536 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1537 yaffsfs_SetError(-EINVAL);
1539 }else if(!olddir || !newdir || !obj) {
1540 yaffsfs_SetError(-ENOENT);
1542 } else if(obj->my_dev->read_only){
1543 yaffsfs_SetError(-EROFS);
1545 } else if(yaffs_is_non_empty_dir(newobj)){
1546 yaffsfs_SetError(-ENOTEMPTY);
1548 } else if(olddir->my_dev != newdir->my_dev) {
1549 /* Rename must be on same device */
1550 yaffsfs_SetError(-EXDEV);
1552 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1554 * It is a directory, check that it is not being renamed to
1555 * being its own decendent.
1556 * Do this by tracing from the new directory back to the root,
1560 struct yaffs_obj *xx = newdir;
1562 while( rename_allowed && xx){
1568 yaffsfs_SetError(-EINVAL);
1572 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1579 return (result == YAFFS_FAIL) ? -1 : 0;
1583 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1587 obj = yaffs_get_equivalent_obj(obj);
1590 buf->st_dev = (int)obj->my_dev->os_context;
1591 buf->st_ino = obj->obj_id;
1592 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1594 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1595 buf->st_mode |= S_IFDIR;
1596 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1597 buf->st_mode |= S_IFLNK;
1598 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1599 buf->st_mode |= S_IFREG;
1601 buf->st_nlink = yaffs_get_obj_link_count(obj);
1604 buf->st_rdev = obj->yst_rdev;
1605 buf->st_size = yaffs_get_obj_length(obj);
1606 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1607 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1608 #if CONFIG_YAFFS_WINCE
1609 buf->yst_wince_atime[0] = obj->win_atime[0];
1610 buf->yst_wince_atime[1] = obj->win_atime[1];
1611 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1612 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1613 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1614 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1616 buf->yst_atime = obj->yst_atime;
1617 buf->yst_ctime = obj->yst_ctime;
1618 buf->yst_mtime = obj->yst_mtime;
1625 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1627 struct yaffs_obj *obj=NULL;
1628 struct yaffs_obj *dir=NULL;
1634 yaffsfs_SetError(-EFAULT);
1638 if(yaffsfs_CheckPath(path) < 0){
1639 yaffsfs_SetError(-ENAMETOOLONG);
1645 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1648 obj = yaffsfs_FollowLink(obj,0,&loop);
1651 yaffsfs_SetError(-ENOTDIR);
1653 yaffsfs_SetError(-ELOOP);
1654 else if(!dir || !obj)
1655 yaffsfs_SetError(-ENOENT);
1657 retVal = yaffsfs_DoStat(obj,buf);
1665 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1667 return yaffsfs_DoStatOrLStat(path,buf,0);
1670 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1672 return yaffsfs_DoStatOrLStat(path,buf,1);
1675 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1677 struct yaffs_obj *obj;
1682 yaffsfs_SetError(-EFAULT);
1687 obj = yaffsfs_HandleToObject(fd);
1690 retVal = yaffsfs_DoStat(obj,buf);
1693 yaffsfs_SetError(-EBADF);
1700 static int yaffsfs_DoUtime(struct yaffs_obj *obj,const struct yaffs_utimbuf *buf)
1705 struct yaffs_utimbuf local;
1707 obj = yaffs_get_equivalent_obj(obj);
1709 if(obj && obj->my_dev->read_only) {
1710 yaffsfs_SetError(-EROFS);
1716 local.actime = Y_CURRENT_TIME;
1717 local.modtime = local.actime;
1722 obj->yst_atime = buf->actime;
1723 obj->yst_mtime = buf->modtime;
1725 result = yaffs_flush_file(obj,0,0);
1726 retVal = result == YAFFS_OK ? 0 : -1;
1732 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1734 struct yaffs_obj *obj=NULL;
1735 struct yaffs_obj *dir=NULL;
1741 yaffsfs_SetError(-EFAULT);
1745 if(yaffsfs_CheckPath(path) < 0){
1746 yaffsfs_SetError(-ENAMETOOLONG);
1752 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1755 yaffsfs_SetError(-ENOTDIR);
1757 yaffsfs_SetError(-ELOOP);
1758 else if(!dir || !obj)
1759 yaffsfs_SetError(-ENOENT);
1761 retVal = yaffsfs_DoUtime(obj,buf);
1768 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1770 struct yaffs_obj *obj;
1775 obj = yaffsfs_HandleToObject(fd);
1778 retVal = yaffsfs_DoUtime(obj,buf);
1781 yaffsfs_SetError(-EBADF);
1789 #ifndef CONFIG_YAFFS_WINCE
1790 /* xattrib functions */
1793 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1794 const void *data, int size, int flags, int follow)
1796 struct yaffs_obj *obj;
1797 struct yaffs_obj *dir;
1803 if(!path || !name || !data){
1804 yaffsfs_SetError(-EFAULT);
1808 if(yaffsfs_CheckPath(path) < 0){
1809 yaffsfs_SetError(-ENAMETOOLONG);
1815 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1818 obj = yaffsfs_FollowLink(obj,0,&loop);
1821 yaffsfs_SetError(-ENOTDIR);
1823 yaffsfs_SetError(-ELOOP);
1824 else if(!dir || !obj)
1825 yaffsfs_SetError(-ENOENT);
1827 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1829 yaffsfs_SetError(retVal);
1840 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1842 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1845 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1847 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1852 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1854 struct yaffs_obj *obj;
1859 yaffsfs_SetError(-EFAULT);
1864 obj = yaffsfs_HandleToObject(fd);
1867 yaffsfs_SetError(-EBADF);
1869 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1871 yaffsfs_SetError(retVal);
1881 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1883 struct yaffs_obj *obj;
1884 struct yaffs_obj *dir;
1889 if(!path || !name || !data ){
1890 yaffsfs_SetError(-EFAULT);
1894 if(yaffsfs_CheckPath(path) < 0){
1895 yaffsfs_SetError(-ENAMETOOLONG);
1901 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1904 obj = yaffsfs_FollowLink(obj,0,&loop);
1907 yaffsfs_SetError(-ENOTDIR);
1909 yaffsfs_SetError(-ELOOP);
1910 else if(!dir || !obj)
1911 yaffsfs_SetError(-ENOENT);
1913 retVal = yaffs_get_xattrib(obj,name,data,size);
1915 yaffsfs_SetError(retVal);
1925 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1927 return yaffs_do_getxattr( path, name, data, size, 1);
1929 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1931 return yaffs_do_getxattr( path, name, data, size, 0);
1936 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1938 struct yaffs_obj *obj;
1942 if(!name || !data ){
1943 yaffsfs_SetError(-EFAULT);
1948 obj = yaffsfs_HandleToObject(fd);
1951 retVal = yaffs_get_xattrib(obj,name,data,size);
1953 yaffsfs_SetError(retVal);
1958 yaffsfs_SetError(-EBADF);
1965 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1967 struct yaffs_obj *obj=NULL;
1968 struct yaffs_obj *dir=NULL;
1973 if(!path || !data ){
1974 yaffsfs_SetError(-EFAULT);
1978 if(yaffsfs_CheckPath(path) < 0){
1979 yaffsfs_SetError(-ENAMETOOLONG);
1985 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1988 obj = yaffsfs_FollowLink(obj,0,&loop);
1991 yaffsfs_SetError(-ENOTDIR);
1993 yaffsfs_SetError(-ELOOP);
1994 else if(!dir || !obj)
1995 yaffsfs_SetError(-ENOENT);
1997 retVal = yaffs_list_xattrib(obj, data,size);
1999 yaffsfs_SetError(retVal);
2010 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2012 return yaffs_do_listxattr(path, data, size, 1);
2015 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2017 return yaffs_do_listxattr(path, data, size, 0);
2020 int yaffs_flistxattr(int fd, char *data, int size)
2022 struct yaffs_obj *obj;
2027 yaffsfs_SetError(-EFAULT);
2032 obj = yaffsfs_HandleToObject(fd);
2035 retVal = yaffs_list_xattrib(obj,data,size);
2037 yaffsfs_SetError(retVal);
2042 yaffsfs_SetError(-EBADF);
2049 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
2051 struct yaffs_obj *obj=NULL;
2052 struct yaffs_obj *dir=NULL;
2058 yaffsfs_SetError(-EFAULT);
2062 if(yaffsfs_CheckPath(path) < 0){
2063 yaffsfs_SetError(-ENAMETOOLONG);
2069 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2072 obj = yaffsfs_FollowLink(obj,0,&loop);
2075 yaffsfs_SetError(-ENOTDIR);
2077 yaffsfs_SetError(-ELOOP);
2078 else if(!dir || !obj)
2079 yaffsfs_SetError(-ENOENT);
2081 retVal = yaffs_remove_xattrib(obj,name);
2083 yaffsfs_SetError(retVal);
2094 int yaffs_removexattr(const YCHAR *path, const char *name)
2096 return yaffs_do_removexattr(path, name, 1);
2099 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2101 return yaffs_do_removexattr(path, name, 0);
2104 int yaffs_fremovexattr(int fd, const char *name)
2106 struct yaffs_obj *obj;
2111 yaffsfs_SetError(-EFAULT);
2116 obj = yaffsfs_HandleToObject(fd);
2119 retVal = yaffs_remove_xattrib(obj,name);
2121 yaffsfs_SetError(retVal);
2126 yaffsfs_SetError(-EBADF);
2134 #ifdef CONFIG_YAFFS_WINCE
2135 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
2137 struct yaffs_obj *obj;
2142 obj = yaffsfs_HandleToObject(fd);
2147 wctime[0] = obj->win_ctime[0];
2148 wctime[1] = obj->win_ctime[1];
2151 watime[0] = obj->win_atime[0];
2152 watime[1] = obj->win_atime[1];
2155 wmtime[0] = obj->win_mtime[0];
2156 wmtime[1] = obj->win_mtime[1];
2163 yaffsfs_SetError(-EBADF);
2171 int yaffs_set_wince_times(int fd,
2172 const unsigned *wctime,
2173 const unsigned *watime,
2174 const unsigned *wmtime)
2176 struct yaffs_obj *obj;
2181 obj = yaffsfs_HandleToObject(fd);
2186 obj->win_ctime[0] = wctime[0];
2187 obj->win_ctime[1] = wctime[1];
2190 obj->win_atime[0] = watime[0];
2191 obj->win_atime[1] = watime[1];
2194 obj->win_mtime[0] = wmtime[0];
2195 obj->win_mtime[1] = wmtime[1];
2199 result = yaffs_flush_file(obj,0,0);
2203 yaffsfs_SetError(-EBADF);
2213 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2218 obj = yaffs_get_equivalent_obj(obj);
2221 obj->yst_mode = mode;
2223 result = yaffs_flush_file(obj,0,0);
2226 return result == YAFFS_OK ? 0 : -1;
2230 int yaffs_access(const YCHAR *path, int amode)
2232 struct yaffs_obj *obj=NULL;
2233 struct yaffs_obj *dir=NULL;
2239 yaffsfs_SetError(-EFAULT);
2243 if(yaffsfs_CheckPath(path) < 0){
2244 yaffsfs_SetError(-ENAMETOOLONG);
2248 if(amode & ~(R_OK | W_OK | X_OK)){
2249 yaffsfs_SetError(-EINVAL);
2255 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2256 obj = yaffsfs_FollowLink(obj,0,&loop);
2259 yaffsfs_SetError(-ENOTDIR);
2261 yaffsfs_SetError(-ELOOP);
2262 else if(!dir || !obj)
2263 yaffsfs_SetError(-ENOENT);
2264 else if((amode & W_OK) && obj->my_dev->read_only)
2265 yaffsfs_SetError(-EROFS);
2269 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2271 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2273 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2277 yaffsfs_SetError(-EACCES);
2289 int yaffs_chmod(const YCHAR *path, mode_t mode)
2291 struct yaffs_obj *obj=NULL;
2292 struct yaffs_obj *dir=NULL;
2298 yaffsfs_SetError(-EFAULT);
2302 if(yaffsfs_CheckPath(path) < 0){
2303 yaffsfs_SetError(-ENAMETOOLONG);
2308 yaffsfs_SetError(-EINVAL);
2314 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2315 obj = yaffsfs_FollowLink(obj,0,&loop);
2318 yaffsfs_SetError(-ENOTDIR);
2320 yaffsfs_SetError(-ELOOP);
2321 else if(!dir || !obj)
2322 yaffsfs_SetError(-ENOENT);
2323 else if(obj->my_dev->read_only)
2324 yaffsfs_SetError(-EROFS);
2326 retVal = yaffsfs_DoChMod(obj,mode);
2335 int yaffs_fchmod(int fd, mode_t mode)
2337 struct yaffs_obj *obj;
2341 yaffsfs_SetError(-EINVAL);
2346 obj = yaffsfs_HandleToObject(fd);
2349 yaffsfs_SetError(-EBADF);
2350 else if(obj->my_dev->read_only)
2351 yaffsfs_SetError(-EROFS);
2353 retVal = yaffsfs_DoChMod(obj,mode);
2360 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2362 struct yaffs_obj *parent = NULL;
2363 struct yaffs_obj *dir = NULL;
2365 YCHAR *alt_path = NULL;
2371 yaffsfs_SetError(-EFAULT);
2375 if(yaffsfs_CheckPath(path) < 0){
2376 yaffsfs_SetError(-ENAMETOOLONG);
2380 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2381 yaffsfs_SetError(-ENOMEM);
2388 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2389 if(!parent && notDir)
2390 yaffsfs_SetError(-ENOTDIR);
2392 yaffsfs_SetError(-ELOOP);
2394 yaffsfs_SetError(-ENOENT);
2395 else if(yaffsfs_TooManyObjects(parent->my_dev))
2396 yaffsfs_SetError(-ENFILE);
2397 else if(yaffs_strnlen(name,5) == 0){
2398 /* Trying to make the root itself */
2399 yaffsfs_SetError(-EEXIST);
2400 } else if(parent->my_dev->read_only)
2401 yaffsfs_SetError(-EROFS);
2403 dir = yaffs_create_dir(parent,name,mode,0,0);
2406 else if (yaffs_find_by_name(parent,name))
2407 yaffsfs_SetError(-EEXIST); /* the name already exists */
2409 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2420 int yaffs_rmdir(const YCHAR *path)
2426 yaffsfs_SetError(-EFAULT);
2430 if(yaffsfs_CheckPath(path) < 0){
2431 yaffsfs_SetError(-ENAMETOOLONG);
2435 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2436 yaffsfs_SetError(-ENOMEM);
2441 result = yaffsfs_DoUnlink(path,1);
2448 void * yaffs_getdev(const YCHAR *path)
2450 struct yaffs_dev *dev=NULL;
2452 dev = yaffsfs_FindDevice(path,&dummy);
2456 int yaffs_mount2(const YCHAR *path,int read_only)
2459 int result=YAFFS_FAIL;
2460 struct yaffs_dev *dev=NULL;
2463 yaffsfs_SetError(-EFAULT);
2467 yaffs_trace(YAFFS_TRACE_MOUNT,"yaffs: Mounting %s",path);
2469 if(yaffsfs_CheckPath(path) < 0){
2470 yaffsfs_SetError(-ENAMETOOLONG);
2476 yaffsfs_InitHandles();
2478 dev = yaffsfs_FindMountPoint(path);
2480 if(!dev->is_mounted){
2481 dev->read_only = read_only ? 1 : 0;
2482 result = yaffs_guts_initialise(dev);
2483 if(result == YAFFS_FAIL)
2484 yaffsfs_SetError(-ENOMEM);
2485 retVal = result ? 0 : -1;
2489 yaffsfs_SetError(-EBUSY);
2491 yaffsfs_SetError(-ENODEV);
2498 int yaffs_mount(const YCHAR *path)
2500 return yaffs_mount2(path,0);
2503 int yaffs_sync(const YCHAR *path)
2506 struct yaffs_dev *dev=NULL;
2510 yaffsfs_SetError(-EFAULT);
2514 if(yaffsfs_CheckPath(path) < 0){
2515 yaffsfs_SetError(-ENAMETOOLONG);
2520 dev = yaffsfs_FindDevice(path,&dummy);
2522 if(!dev->is_mounted)
2523 yaffsfs_SetError(-EINVAL);
2524 else if(dev->read_only)
2525 yaffsfs_SetError(-EROFS);
2528 yaffs_flush_whole_cache(dev);
2529 yaffs_checkpoint_save(dev);
2534 yaffsfs_SetError(-ENODEV);
2541 static int yaffsfs_IsDevBusy(struct yaffs_dev * dev)
2544 struct yaffs_obj *obj;
2546 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
2547 obj = yaffsfs_HandleToObject(i);
2548 if(obj && obj->my_dev == dev)
2555 int yaffs_remount(const YCHAR *path, int force, int read_only)
2558 struct yaffs_dev *dev=NULL;
2561 yaffsfs_SetError(-EFAULT);
2565 if(yaffsfs_CheckPath(path) < 0){
2566 yaffsfs_SetError(-ENAMETOOLONG);
2571 dev = yaffsfs_FindMountPoint(path);
2573 if(dev->is_mounted){
2574 yaffs_flush_whole_cache(dev);
2576 if(force || ! yaffsfs_IsDevBusy(dev)){
2578 yaffs_checkpoint_save(dev);
2579 dev->read_only = read_only ? 1 : 0;
2582 yaffsfs_SetError(-EBUSY);
2585 yaffsfs_SetError(-EINVAL);
2589 yaffsfs_SetError(-ENODEV);
2596 int yaffs_unmount2(const YCHAR *path, int force)
2599 struct yaffs_dev *dev=NULL;
2602 yaffsfs_SetError(-EFAULT);
2606 if(yaffsfs_CheckPath(path) < 0){
2607 yaffsfs_SetError(-ENAMETOOLONG);
2612 dev = yaffsfs_FindMountPoint(path);
2614 if(dev->is_mounted){
2616 yaffs_flush_whole_cache(dev);
2617 yaffs_checkpoint_save(dev);
2618 inUse = yaffsfs_IsDevBusy(dev);
2619 if(!inUse || force){
2621 yaffsfs_BreakDeviceHandles(dev);
2622 yaffs_deinitialise(dev);
2626 yaffsfs_SetError(-EBUSY);
2629 yaffsfs_SetError(-EINVAL);
2632 yaffsfs_SetError(-ENODEV);
2639 int yaffs_unmount(const YCHAR *path)
2641 return yaffs_unmount2(path,0);
2644 loff_t yaffs_freespace(const YCHAR *path)
2647 struct yaffs_dev *dev=NULL;
2651 yaffsfs_SetError(-EFAULT);
2655 if(yaffsfs_CheckPath(path) < 0){
2656 yaffsfs_SetError(-ENAMETOOLONG);
2661 dev = yaffsfs_FindDevice(path,&dummy);
2662 if(dev && dev->is_mounted){
2663 retVal = yaffs_get_n_free_chunks(dev);
2664 retVal *= dev->data_bytes_per_chunk;
2667 yaffsfs_SetError(-EINVAL);
2673 loff_t yaffs_totalspace(const YCHAR *path)
2676 struct yaffs_dev *dev=NULL;
2680 yaffsfs_SetError(-EFAULT);
2684 if(yaffsfs_CheckPath(path) < 0){
2685 yaffsfs_SetError(-ENAMETOOLONG);
2690 dev = yaffsfs_FindDevice(path,&dummy);
2691 if(dev && dev->is_mounted){
2692 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2693 dev->param.n_reserved_blocks;
2694 retVal *= dev->param.chunks_per_block;
2695 retVal *= dev->data_bytes_per_chunk;
2698 yaffsfs_SetError(-EINVAL);
2704 int yaffs_inodecount(const YCHAR *path)
2707 struct yaffs_dev *dev=NULL;
2711 yaffsfs_SetError(-EFAULT);
2715 if(yaffsfs_CheckPath(path) < 0){
2716 yaffsfs_SetError(-ENAMETOOLONG);
2721 dev = yaffsfs_FindDevice(path,&dummy);
2722 if(dev && dev->is_mounted) {
2723 int n_obj = dev->n_obj;
2724 if(n_obj > dev->n_hardlinks)
2725 retVal = n_obj - dev->n_hardlinks;
2729 yaffsfs_SetError(-EINVAL);
2736 void yaffs_add_device(struct yaffs_dev *dev)
2738 struct list_head *cfg;
2739 /* First check that the device is not in the list. */
2741 list_for_each(cfg, &yaffsfs_deviceList){
2742 if(dev == list_entry(cfg, struct yaffs_dev, dev_list))
2746 dev->is_mounted = 0;
2747 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2749 if(!dev->dev_list.next)
2750 INIT_LIST_HEAD(&dev->dev_list);
2752 list_add(&dev->dev_list,&yaffsfs_deviceList);
2755 void yaffs_remove_device(struct yaffs_dev *dev)
2757 list_del_init(&dev->dev_list);
2763 /* Directory search stuff. */
2766 * Directory search context
2768 * NB this is an opaque structure.
2775 yaffs_dirent de; /* directory entry being used by this dsc */
2776 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2777 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2778 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2780 struct list_head others;
2781 } yaffsfs_DirectorySearchContext;
2785 static struct list_head search_contexts;
2788 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2792 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2796 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2797 dsc->nextReturn = NULL;
2799 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2800 struct yaffs_obj,siblings);
2802 /* Hey someone isn't playing nice! */
2806 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2810 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2812 if( dsc->nextReturn == NULL ||
2813 list_empty(&dsc->dirObj->variant.dir_variant.children))
2814 dsc->nextReturn = NULL;
2816 struct list_head *next = dsc->nextReturn->siblings.next;
2818 if( next == &dsc->dirObj->variant.dir_variant.children)
2819 dsc->nextReturn = NULL; /* end of list */
2821 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2824 /* Hey someone isn't playing nice! */
2828 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2831 struct list_head *i;
2832 yaffsfs_DirectorySearchContext *dsc;
2834 /* if search contexts not initilised then skip */
2835 if(!search_contexts.next)
2838 /* Iterate through the directory search contexts.
2839 * If any are the one being removed, then advance the dsc to
2840 * the next one to prevent a hanging ptr.
2842 list_for_each(i, &search_contexts) {
2844 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2845 if(dsc->nextReturn == obj)
2846 yaffsfs_DirAdvance(dsc);
2852 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2854 yaffs_DIR *dir = NULL;
2855 struct yaffs_obj *obj = NULL;
2856 yaffsfs_DirectorySearchContext *dsc = NULL;
2861 yaffsfs_SetError(-EFAULT);
2865 if(yaffsfs_CheckPath(dirname) < 0){
2866 yaffsfs_SetError(-ENAMETOOLONG);
2872 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2875 yaffsfs_SetError(-ENOTDIR);
2877 yaffsfs_SetError(-ELOOP);
2879 yaffsfs_SetError(-ENOENT);
2880 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2881 yaffsfs_SetError(-ENOTDIR);
2884 dsc = kmalloc(sizeof(yaffsfs_DirectorySearchContext), 0);
2885 dir = (yaffs_DIR *)dsc;
2888 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2889 dsc->magic = YAFFS_MAGIC;
2891 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2892 INIT_LIST_HEAD(&dsc->others);
2894 if(!search_contexts.next)
2895 INIT_LIST_HEAD(&search_contexts);
2897 list_add(&dsc->others,&search_contexts);
2898 yaffsfs_SetDirRewound(dsc);
2908 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2910 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2911 struct yaffs_dirent *retVal = NULL;
2915 if(dsc && dsc->magic == YAFFS_MAGIC){
2916 yaffsfs_SetError(0);
2917 if(dsc->nextReturn){
2918 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2919 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2920 dsc->de.d_off = dsc->offset++;
2921 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2922 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2924 /* this should not happen! */
2925 strcpy(dsc->de.d_name,_Y("zz"));
2927 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2929 yaffsfs_DirAdvance(dsc);
2933 yaffsfs_SetError(-EBADF);
2942 void yaffs_rewinddir(yaffs_DIR *dirp)
2944 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2948 yaffsfs_SetDirRewound(dsc);
2954 int yaffs_closedir(yaffs_DIR *dirp)
2956 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2959 yaffsfs_SetError(-EFAULT);
2965 list_del(&dsc->others); /* unhook from list */
2971 /* End of directory stuff */
2974 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2976 struct yaffs_obj *parent = NULL;
2977 struct yaffs_obj *obj;
2980 int mode = 0; /* ignore for now */
2984 if(!oldpath || !newpath){
2985 yaffsfs_SetError(-EFAULT);
2989 if(yaffsfs_CheckPath(newpath) < 0 ||
2990 yaffsfs_CheckPath(oldpath) < 0){
2991 yaffsfs_SetError(-ENAMETOOLONG);
2996 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2997 if(!parent && notDir)
2998 yaffsfs_SetError(-ENOTDIR);
3000 yaffsfs_SetError(-ELOOP);
3001 else if( !parent || yaffs_strnlen(name,5) < 1)
3002 yaffsfs_SetError(-ENOENT);
3003 else if(yaffsfs_TooManyObjects(parent->my_dev))
3004 yaffsfs_SetError(-ENFILE);
3005 else if(parent->my_dev->read_only)
3006 yaffsfs_SetError(-EROFS);
3008 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
3011 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
3012 yaffsfs_SetError(-EEXIST);
3014 yaffsfs_SetError(-ENOSPC);
3023 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3025 struct yaffs_obj *obj = NULL;
3026 struct yaffs_obj *dir = NULL;
3032 yaffsfs_SetError(-EFAULT);
3038 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
3041 yaffsfs_SetError(-ENOTDIR);
3043 yaffsfs_SetError(-ELOOP);
3044 else if(!dir || !obj)
3045 yaffsfs_SetError(-ENOENT);
3046 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3047 yaffsfs_SetError(-EINVAL);
3049 YCHAR *alias = obj->variant.symlink_variant.alias;
3050 memset(buf,0,bufsiz);
3051 yaffs_strncpy(buf,alias,bufsiz - 1);
3058 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3060 /* Creates a link called newpath to existing oldpath */
3061 struct yaffs_obj *obj = NULL;
3062 struct yaffs_obj *lnk = NULL;
3063 struct yaffs_obj *obj_dir = NULL;
3064 struct yaffs_obj *lnk_dir = NULL;
3072 if(!oldpath || !linkpath){
3073 yaffsfs_SetError(-EFAULT);
3077 if(yaffsfs_CheckPath(linkpath) < 0 ||
3078 yaffsfs_CheckPath(oldpath) < 0){
3079 yaffsfs_SetError(-ENAMETOOLONG);
3085 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
3086 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
3087 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
3089 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3090 yaffsfs_SetError(-ENOTDIR);
3091 else if(objLoop || lnkLoop)
3092 yaffsfs_SetError(-ELOOP);
3093 else if(!obj_dir || !lnk_dir || !obj)
3094 yaffsfs_SetError(-ENOENT);
3095 else if(obj->my_dev->read_only)
3096 yaffsfs_SetError(-EROFS);
3097 else if(yaffsfs_TooManyObjects(obj->my_dev))
3098 yaffsfs_SetError(-ENFILE);
3100 yaffsfs_SetError(-EEXIST);
3101 else if(lnk_dir->my_dev != obj->my_dev)
3102 yaffsfs_SetError(-EXDEV);
3104 retVal = yaffsfs_CheckNameLength(newname);
3107 lnk = yaffs_link_obj(lnk_dir,newname,obj);
3111 yaffsfs_SetError(-ENOSPC);
3121 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3127 yaffsfs_SetError(-EINVAL);
3133 * D E B U G F U N C T I O N S
3138 * Returns number of handles attached to the object
3140 int yaffs_n_handles(const YCHAR *path)
3142 struct yaffs_obj *obj;
3145 yaffsfs_SetError(-EFAULT);
3149 if(yaffsfs_CheckPath(path) < 0){
3150 yaffsfs_SetError(-ENAMETOOLONG);
3154 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
3157 return yaffsfs_CountHandles(obj);
3162 int yaffs_get_error(void)
3164 return yaffsfs_GetLastError();
3167 int yaffs_set_error(int error)
3169 yaffsfs_SetError(error);
3173 int yaffs_dump_dev(const YCHAR *path)
3180 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
3183 struct yaffs_dev *dev = obj->my_dev;
3186 "n_page_writes.......... %d\n"
3187 "n_page_reads........... %d\n"
3188 "n_erasures....... %d\n"
3189 "n_gc_copies............ %d\n"
3190 "garbageCollections... %d\n"
3191 "passiveGarbageColl'ns %d\n"
3197 dev->garbageCollections,
3198 dev->passiveGarbageCollections