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 loff_t 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));
398 int yaffsfs_Match(YCHAR a, YCHAR b)
405 int yaffsfs_IsPathDivider(YCHAR ch)
407 const YCHAR *str = YAFFS_PATH_DIVIDERS;
418 int yaffsfs_CheckNameLength(const char *name)
422 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
425 yaffsfs_SetError(-ENOENT);
427 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
428 yaffsfs_SetError(-ENAMETOOLONG);
436 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
438 YCHAR *alt_path = NULL;
443 * We don't have a definition for max path length.
444 * We will use 3 * max name length instead.
447 path_length = yaffs_strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
449 /* If the last character is a path divider, then we need to
450 * trim it back so that the name look-up works properly.
451 * eg. /foo/new_dir/ -> /foo/newdir
452 * Curveball: Need to handle multiple path dividers:
453 * eg. /foof/sdfse///// -> /foo/sdfse
455 if(path_length > 0 &&
456 yaffsfs_IsPathDivider(path[path_length-1])){
457 alt_path = kmalloc(path_length + 1, 0);
460 yaffs_strcpy(alt_path, path);
461 for(i = path_length-1;
462 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
464 alt_path[i] = (YCHAR) 0;
466 *ret_path = alt_path;
471 LIST_HEAD(yaffsfs_deviceList);
476 * Scan the configuration list to find the device
477 * Curveballs: Should match paths that end in '/' too
478 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
480 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
482 struct list_head *cfg;
483 const YCHAR *leftOver;
485 struct yaffs_dev *retval = NULL;
486 struct yaffs_dev *dev = NULL;
488 int longestMatch = -1;
492 * Check all configs, choose the one that:
493 * 1) Actually matches a prefix (ie /a amd /abc will not match
494 * 2) Matches the longest.
496 list_for_each(cfg, &yaffsfs_deviceList){
497 dev = list_entry(cfg, struct yaffs_dev, dev_list);
504 while(matching && *p && *leftOver){
505 /* Skip over any /s */
506 while(yaffsfs_IsPathDivider(*p))
509 /* Skip over any /s */
510 while(yaffsfs_IsPathDivider(*leftOver))
513 /* Now match the text part */
515 *p && !yaffsfs_IsPathDivider(*p) &&
516 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
517 if(yaffsfs_Match(*p,*leftOver)){
527 /* Skip over any /s in leftOver */
528 while(yaffsfs_IsPathDivider(*leftOver))
531 // Skip over any /s in p
532 while(yaffsfs_IsPathDivider(*p))
535 // p should now be at the end of the string (ie. fully matched)
539 if( matching && (thisMatchLength > longestMatch))
542 *restOfPath = (YCHAR *)leftOver;
544 longestMatch = thisMatchLength;
551 static int yaffsfs_CheckPath(const YCHAR *path)
555 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
556 if(yaffsfs_IsPathDivider(*path)){
564 return (*path) ? -1 : 0;
567 /* FindMountPoint only returns a dev entry if the path is a mount point */
568 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
570 struct yaffs_dev *dev;
571 YCHAR *restOfPath=NULL;
572 dev = yaffsfs_FindDevice(path,&restOfPath);
573 if(dev && restOfPath && *restOfPath)
578 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
581 struct yaffs_dev *dev;
583 dev= yaffsfs_FindDevice(path,restOfPath);
584 if(dev && dev->is_mounted){
585 return dev->root_dir;
590 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
591 int symDepth, int *loop)
595 obj = yaffs_get_equivalent_obj(obj);
597 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
598 YCHAR *alias = obj->variant.symlink_variant.alias;
600 if(yaffsfs_IsPathDivider(*alias))
601 /* Starts with a /, need to scan from root up */
602 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
605 /* Relative to here, so use the parent of the symlink as a start */
606 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
614 * yaffsfs_FindDirectory
615 * Parse a path to determine the directory and the name within the directory.
617 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
619 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
620 const YCHAR *path, YCHAR **name, int symDepth,
621 int *notDir,int *loop)
623 struct yaffs_obj *dir;
625 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
628 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
636 restOfPath = (YCHAR *)path;
639 dir = yaffsfs_FindRoot(path,&restOfPath);
645 * curve ball: also throw away surplus '/'
646 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
648 while(yaffsfs_IsPathDivider(*restOfPath))
649 restOfPath++; /* get rid of '/' */
654 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
655 if (i < YAFFS_MAX_NAME_LENGTH){
656 str[i] = *restOfPath;
664 /* got to the end of the string */
667 if(yaffs_strcmp(str,_Y(".")) == 0){
669 } else if(yaffs_strcmp(str,_Y("..")) == 0) {
672 dir = yaffs_find_by_name(dir,str);
674 dir = yaffsfs_FollowLink(dir,symDepth,loop);
676 if(dir && dir->variant_type !=
677 YAFFS_OBJECT_TYPE_DIRECTORY){
686 /* directory did not exist. */
690 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
697 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
701 * yaffsfs_FindObject turns a path for an existing object into the object
703 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
704 const YCHAR *path,int symDepth, int getEquiv,
705 struct yaffs_obj **dirOut, int *notDir,int *loop)
707 struct yaffs_obj *dir;
708 struct yaffs_obj *obj;
711 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
717 obj = yaffs_find_by_name(dir,name);
722 obj = yaffs_get_equivalent_obj(obj);
728 /*************************************************************************
729 * Start of yaffsfs visible functions.
730 *************************************************************************/
732 int yaffs_dup(int handle)
734 int newHandleNumber = -1;
735 yaffsfs_FileDes *existingFD = NULL;
736 yaffsfs_Handle *existingHandle = NULL;
737 yaffsfs_Handle *newHandle = NULL;
740 existingHandle = yaffsfs_HandleToPointer(handle);
741 existingFD = yaffsfs_HandleToFileDes(handle);
743 newHandleNumber = yaffsfs_NewHandle(&newHandle);
745 newHandle->fdId = existingHandle->fdId;
746 existingFD->handleCount++;
752 yaffsfs_SetError(-EBADF);
754 yaffsfs_SetError(-ENOMEM);
756 return newHandleNumber;
760 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
762 int current_objects = dev->n_obj - dev->n_deleted_files;
764 if(dev->param.max_objects && current_objects > dev->param.max_objects)
770 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
772 struct yaffs_obj *obj = NULL;
773 struct yaffs_obj *dir = NULL;
776 yaffsfs_FileDes *fd = NULL;
779 int errorReported = 0;
780 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
781 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
782 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
783 u8 sharedReadAllowed;
784 u8 sharedWriteAllowed;
793 yaffsfs_SetError(-EFAULT);
797 if(yaffsfs_CheckPath(path) < 0){
798 yaffsfs_SetError(-ENAMETOOLONG);
802 /* O_EXCL only has meaning if O_CREAT is specified */
803 if(!(oflag & O_CREAT))
806 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
807 if( (oflag & O_CREAT) & (oflag & O_EXCL))
810 /* Todo: Are there any more flag combos to sanitise ? */
812 /* Figure out if reading or writing is requested */
814 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
815 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
819 handle = yaffsfs_NewHandleAndFileDes();
822 yaffsfs_SetError(-ENFILE);
826 fd = yaffsfs_HandleToFileDes(handle);
828 /* try to find the exisiting object */
829 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
831 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
834 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
835 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
841 /* The file already exists or it might be a directory */
843 /* If it is a directory then we can't open it as a file */
844 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
846 yaffsfs_SetError(-EISDIR);
850 /* Open should fail if O_CREAT and O_EXCL are specified since
853 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
855 yaffsfs_SetError(-EEXIST);
859 /* Check file permissions */
860 if( readRequested && !(obj->yst_mode & S_IREAD))
863 if( writeRequested && !(obj->yst_mode & S_IWRITE))
866 if( !errorReported && writeRequested &&
867 obj->my_dev->read_only){
869 yaffsfs_SetError(-EROFS);
873 if(openDenied && !errorReported ) {
874 /* Error if the file exists but permissions are refused. */
875 yaffsfs_SetError(-EACCES);
879 /* Check sharing of an existing object. */
881 yaffsfs_FileDes *fdx;
884 sharedReadAllowed = 1;
885 sharedWriteAllowed = 1;
888 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
889 fdx = &yaffsfs_fd[i];
890 if(fdx->handleCount > 0 &&
892 yaffsfs_inode[fdx->inodeId].iObj == obj){
894 sharedReadAllowed = 0;
896 sharedWriteAllowed = 0;
906 if((!sharedReadAllowed && readRequested)||
907 (!shareRead && alreadyReading) ||
908 (!sharedWriteAllowed && writeRequested) ||
909 (!shareWrite && alreadyWriting)){
911 yaffsfs_SetError(-EBUSY);
918 /* If we could not open an existing object, then let's see if
919 * the directory exists. If not, error.
921 if(!obj && !errorReported){
922 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
924 yaffsfs_SetError(-ENOTDIR);
927 yaffsfs_SetError(-ELOOP);
930 yaffsfs_SetError(-ENOENT);
935 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
936 /* Let's see if we can create this file if it does not exist. */
937 if(dir->my_dev->read_only){
938 yaffsfs_SetError(-EROFS);
940 } else if(yaffsfs_TooManyObjects(dir->my_dev)) {
941 yaffsfs_SetError(-ENFILE);
944 obj = yaffs_create_file(dir,name,mode,0,0);
946 if(!obj && !errorReported){
947 yaffsfs_SetError(-ENOSPC);
952 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
953 /* Error if the file does not exist and CREAT is not set. */
954 yaffsfs_SetError(-ENOENT);
958 if(obj && !openDenied) {
959 int inodeId = yaffsfs_GetInodeIdForObject(obj);
963 * Todo: Fix any problem if inodes run out, though that
964 * can't happen if the number of inode items >= number of handles.
968 fd->inodeId = inodeId;
969 fd->reading = readRequested;
970 fd->writing = writeRequested;
971 fd->append = (oflag & O_APPEND) ? 1 : 0;
973 fd->shareRead = shareRead;
974 fd->shareWrite = shareWrite;
976 /* Hook inode to object */
977 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
979 if((oflag & O_TRUNC) && fd->writing)
980 yaffs_resize_file(obj,0);
982 yaffsfs_PutHandle(handle);
984 yaffsfs_SetError(0); /* Problem */
994 int yaffs_open(const YCHAR *path, int oflag, int mode)
996 return yaffs_open_sharing(path, oflag, mode,
997 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1000 int yaffs_Dofsync(int handle,int datasync)
1003 struct yaffs_obj *obj;
1007 obj = yaffsfs_HandleToObject(handle);
1010 yaffsfs_SetError(-EBADF);
1011 else if(obj->my_dev->read_only)
1012 yaffsfs_SetError(-EROFS);
1014 yaffs_flush_file(obj,1,datasync);
1023 int yaffs_fsync(int handle)
1025 return yaffs_Dofsync(handle,0);
1028 int yaffs_flush(int handle)
1030 return yaffs_fsync(handle);
1033 int yaffs_fdatasync(int handle)
1035 return yaffs_Dofsync(handle,1);
1038 int yaffs_close(int handle)
1040 yaffsfs_Handle *h = NULL;
1041 struct yaffs_obj *obj = NULL;
1046 h = yaffsfs_HandleToPointer(handle);
1047 obj = yaffsfs_HandleToObject(handle);
1050 yaffsfs_SetError(-EBADF);
1053 yaffs_flush_file(obj,1,0);
1054 yaffsfs_PutHandle(handle);
1065 int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte, int isPread, loff_t offset)
1067 yaffsfs_FileDes *fd = NULL;
1068 struct yaffs_obj *obj = NULL;
1070 loff_t startPos = 0;
1076 u8 *buf = (u8 *)vbuf;
1079 yaffsfs_SetError(-EFAULT);
1084 fd = yaffsfs_HandleToFileDes(handle);
1085 obj = yaffsfs_HandleToObject(handle);
1089 yaffsfs_SetError(-EBADF);
1091 } else if(!fd->reading){
1092 /* Not a reading handle */
1093 yaffsfs_SetError(-EINVAL);
1095 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
1096 yaffsfs_SetError(-EINVAL);
1102 startPos = fd->position;
1106 if(yaffs_get_obj_length(obj) > pos)
1107 maxRead = yaffs_get_obj_length(obj) - pos;
1115 yaffsfs_GetHandle(handle);
1117 endPos = pos + nbyte;
1119 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1120 nbyte > YAFFS_MAX_FILE_SIZE ||
1121 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1127 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1132 * Need to reverify object in case the device was
1133 * unmounted in another thread.
1135 obj = yaffsfs_HandleToObject(handle);
1139 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1147 if(nRead == nToRead)
1150 nbyte = 0; /* no more to read */
1160 yaffsfs_PutHandle(handle);
1164 fd->position = startPos + totalRead;
1166 yaffsfs_SetError(-EINVAL);
1173 return (totalRead >= 0) ? totalRead : -1;
1177 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1179 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1182 int yaffs_pread(int handle, void *buf, unsigned int nbyte, loff_t offset)
1184 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1187 int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte, int isPwrite, loff_t offset)
1189 yaffsfs_FileDes *fd = NULL;
1190 struct yaffs_obj *obj = NULL;
1192 loff_t startPos = 0;
1195 int totalWritten = 0;
1196 int write_trhrough = 0;
1198 const u8 *buf = (const u8 *)vbuf;
1201 yaffsfs_SetError(-EFAULT);
1206 fd = yaffsfs_HandleToFileDes(handle);
1207 obj = yaffsfs_HandleToObject(handle);
1211 yaffsfs_SetError(-EBADF);
1213 } else if(!fd->writing){
1214 yaffsfs_SetError(-EINVAL);
1216 } else if(obj->my_dev->read_only){
1217 yaffsfs_SetError(-EROFS);
1221 startPos = yaffs_get_obj_length(obj);
1225 startPos = fd->position;
1227 yaffsfs_GetHandle(handle);
1229 endPos = pos + nbyte;
1231 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1232 nbyte > YAFFS_MAX_FILE_SIZE ||
1233 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1240 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1241 if(nToWrite > nbyte)
1245 * Need to reverify object in case the device was
1246 * remounted or unmounted in another thread.
1248 obj = yaffsfs_HandleToObject(handle);
1249 if(!obj || obj->my_dev->read_only)
1252 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1255 totalWritten += nWritten;
1260 if(nWritten == nToWrite)
1265 if(nWritten < 1 && totalWritten < 1){
1266 yaffsfs_SetError(-ENOSPC);
1276 yaffsfs_PutHandle(handle);
1279 if(totalWritten > 0)
1280 fd->position = startPos + totalWritten;
1282 yaffsfs_SetError(-EINVAL);
1288 return (totalWritten >= 0) ? totalWritten : -1;
1291 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1293 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1296 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, loff_t offset)
1298 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1302 int yaffs_truncate(const YCHAR *path,loff_t new_size)
1304 struct yaffs_obj *obj = NULL;
1305 struct yaffs_obj *dir = NULL;
1306 int result = YAFFS_FAIL;
1311 yaffsfs_SetError(-EFAULT);
1315 if(yaffsfs_CheckPath(path) < 0){
1316 yaffsfs_SetError(-ENAMETOOLONG);
1322 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1323 obj = yaffsfs_FollowLink(obj,0,&loop);
1326 yaffsfs_SetError(-ENOTDIR);
1328 yaffsfs_SetError(-ELOOP);
1329 else if(!dir || !obj)
1330 yaffsfs_SetError(-ENOENT);
1331 else if(obj->my_dev->read_only)
1332 yaffsfs_SetError(-EROFS);
1333 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1334 yaffsfs_SetError(-EISDIR);
1335 else if(obj->my_dev->read_only)
1336 yaffsfs_SetError(-EROFS);
1337 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1338 yaffsfs_SetError(-EINVAL);
1340 result = yaffs_resize_file(obj, new_size);
1344 return (result) ? 0 : -1;
1347 int yaffs_ftruncate(int handle, loff_t new_size)
1349 yaffsfs_FileDes *fd = NULL;
1350 struct yaffs_obj *obj = NULL;
1354 fd = yaffsfs_HandleToFileDes(handle);
1355 obj = yaffsfs_HandleToObject(handle);
1359 yaffsfs_SetError(-EBADF);
1360 else if(!fd->writing)
1361 yaffsfs_SetError(-EINVAL);
1362 else if(obj->my_dev->read_only)
1363 yaffsfs_SetError(-EROFS);
1364 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1365 yaffsfs_SetError(-EINVAL);
1367 /* resize the file */
1368 result = yaffs_resize_file(obj, new_size);
1371 return (result) ? 0 : -1;
1375 loff_t yaffs_lseek(int handle, loff_t offset, int whence)
1377 yaffsfs_FileDes *fd = NULL;
1378 struct yaffs_obj *obj = NULL;
1383 fd = yaffsfs_HandleToFileDes(handle);
1384 obj = yaffsfs_HandleToObject(handle);
1387 yaffsfs_SetError(-EBADF);
1388 else if(offset > YAFFS_MAX_FILE_SIZE)
1389 yaffsfs_SetError(-EINVAL);
1391 if(whence == SEEK_SET){
1394 } else if(whence == SEEK_CUR) {
1395 if( (fd->position + offset) >= 0)
1396 pos = (fd->position + offset);
1397 } else if(whence == SEEK_END) {
1398 fSize = yaffs_get_obj_length(obj);
1399 if(fSize >= 0 && (fSize + offset) >= 0)
1400 pos = fSize + offset;
1403 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1406 yaffsfs_SetError(-EINVAL);
1417 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1419 struct yaffs_obj *dir = NULL;
1420 struct yaffs_obj *obj = NULL;
1422 int result = YAFFS_FAIL;
1427 yaffsfs_SetError(-EFAULT);
1431 if(yaffsfs_CheckPath(path) < 0){
1432 yaffsfs_SetError(-ENAMETOOLONG);
1438 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1439 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1442 yaffsfs_SetError(-ENOTDIR);
1444 yaffsfs_SetError(-ELOOP);
1446 yaffsfs_SetError(-ENOENT);
1447 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1448 yaffsfs_SetError(-EINVAL);
1450 yaffsfs_SetError(-ENOENT);
1451 else if(obj->my_dev->read_only)
1452 yaffsfs_SetError(-EROFS);
1453 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1454 yaffsfs_SetError(-EISDIR);
1455 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1456 yaffsfs_SetError(-ENOTDIR);
1457 else if(isDirectory && obj == obj->my_dev->root_dir)
1458 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1460 result = yaffs_unlinker(dir,name);
1462 if(result == YAFFS_FAIL && isDirectory)
1463 yaffsfs_SetError(-ENOTEMPTY);
1468 return (result == YAFFS_FAIL) ? -1 : 0;
1472 int yaffs_unlink(const YCHAR *path)
1474 return yaffsfs_DoUnlink(path,0);
1477 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1479 struct yaffs_obj *olddir = NULL;
1480 struct yaffs_obj *newdir = NULL;
1481 struct yaffs_obj *obj = NULL;
1482 struct yaffs_obj *newobj = NULL;
1485 int result= YAFFS_FAIL;
1486 int rename_allowed = 1;
1492 YCHAR *alt_newpath=NULL;
1494 if(!oldPath || !newPath){
1495 yaffsfs_SetError(-EFAULT);
1499 if(yaffsfs_CheckPath(oldPath) < 0 ||
1500 yaffsfs_CheckPath(newPath) < 0){
1501 yaffsfs_SetError(-ENAMETOOLONG);
1505 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1506 yaffsfs_SetError(-ENOMEM);
1510 newPath = alt_newpath;
1515 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1516 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1517 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1518 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1520 /* If the object being renamed is a directory and the
1521 * path ended with a "/" then the olddir == obj.
1522 * We pass through NULL for the old name to tell the lower layers
1523 * to use olddir as the object.
1529 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1530 yaffsfs_SetError(-ENOTDIR);
1532 } else if(oldLoop || newLoop) {
1533 yaffsfs_SetError(-ELOOP);
1535 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1536 yaffsfs_SetError(-EINVAL);
1538 }else if(!olddir || !newdir || !obj) {
1539 yaffsfs_SetError(-ENOENT);
1541 } else if(obj->my_dev->read_only){
1542 yaffsfs_SetError(-EROFS);
1544 } else if(yaffs_is_non_empty_dir(newobj)){
1545 yaffsfs_SetError(-ENOTEMPTY);
1547 } else if(olddir->my_dev != newdir->my_dev) {
1548 /* Rename must be on same device */
1549 yaffsfs_SetError(-EXDEV);
1551 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1553 * It is a directory, check that it is not being renamed to
1554 * being its own decendent.
1555 * Do this by tracing from the new directory back to the root,
1559 struct yaffs_obj *xx = newdir;
1561 while( rename_allowed && xx){
1567 yaffsfs_SetError(-EINVAL);
1571 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1578 return (result == YAFFS_FAIL) ? -1 : 0;
1582 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1586 obj = yaffs_get_equivalent_obj(obj);
1589 buf->st_dev = (int)obj->my_dev->os_context;
1590 buf->st_ino = obj->obj_id;
1591 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1593 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1594 buf->st_mode |= S_IFDIR;
1595 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1596 buf->st_mode |= S_IFLNK;
1597 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1598 buf->st_mode |= S_IFREG;
1600 buf->st_nlink = yaffs_get_obj_link_count(obj);
1603 buf->st_rdev = obj->yst_rdev;
1604 buf->st_size = yaffs_get_obj_length(obj);
1605 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1606 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1607 #if CONFIG_YAFFS_WINCE
1608 buf->yst_wince_atime[0] = obj->win_atime[0];
1609 buf->yst_wince_atime[1] = obj->win_atime[1];
1610 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1611 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1612 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1613 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1615 buf->yst_atime = obj->yst_atime;
1616 buf->yst_ctime = obj->yst_ctime;
1617 buf->yst_mtime = obj->yst_mtime;
1624 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1626 struct yaffs_obj *obj=NULL;
1627 struct yaffs_obj *dir=NULL;
1633 yaffsfs_SetError(-EFAULT);
1637 if(yaffsfs_CheckPath(path) < 0){
1638 yaffsfs_SetError(-ENAMETOOLONG);
1644 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1647 obj = yaffsfs_FollowLink(obj,0,&loop);
1650 yaffsfs_SetError(-ENOTDIR);
1652 yaffsfs_SetError(-ELOOP);
1653 else if(!dir || !obj)
1654 yaffsfs_SetError(-ENOENT);
1656 retVal = yaffsfs_DoStat(obj,buf);
1664 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1666 return yaffsfs_DoStatOrLStat(path,buf,0);
1669 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1671 return yaffsfs_DoStatOrLStat(path,buf,1);
1674 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1676 struct yaffs_obj *obj;
1681 yaffsfs_SetError(-EFAULT);
1686 obj = yaffsfs_HandleToObject(fd);
1689 retVal = yaffsfs_DoStat(obj,buf);
1692 yaffsfs_SetError(-EBADF);
1699 static int yaffsfs_DoUtime(struct yaffs_obj *obj,const struct yaffs_utimbuf *buf)
1704 struct yaffs_utimbuf local;
1706 obj = yaffs_get_equivalent_obj(obj);
1708 if(obj && obj->my_dev->read_only) {
1709 yaffsfs_SetError(-EROFS);
1715 local.actime = Y_CURRENT_TIME;
1716 local.modtime = local.actime;
1721 obj->yst_atime = buf->actime;
1722 obj->yst_mtime = buf->modtime;
1724 result = yaffs_flush_file(obj,0,0);
1725 retVal = result == YAFFS_OK ? 0 : -1;
1731 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1733 struct yaffs_obj *obj=NULL;
1734 struct yaffs_obj *dir=NULL;
1740 yaffsfs_SetError(-EFAULT);
1744 if(yaffsfs_CheckPath(path) < 0){
1745 yaffsfs_SetError(-ENAMETOOLONG);
1751 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1754 yaffsfs_SetError(-ENOTDIR);
1756 yaffsfs_SetError(-ELOOP);
1757 else if(!dir || !obj)
1758 yaffsfs_SetError(-ENOENT);
1760 retVal = yaffsfs_DoUtime(obj,buf);
1767 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1769 struct yaffs_obj *obj;
1774 obj = yaffsfs_HandleToObject(fd);
1777 retVal = yaffsfs_DoUtime(obj,buf);
1780 yaffsfs_SetError(-EBADF);
1788 #ifndef CONFIG_YAFFS_WINCE
1789 /* xattrib functions */
1792 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1793 const void *data, int size, int flags, int follow)
1795 struct yaffs_obj *obj;
1796 struct yaffs_obj *dir;
1802 if(!path || !name || !data){
1803 yaffsfs_SetError(-EFAULT);
1807 if(yaffsfs_CheckPath(path) < 0){
1808 yaffsfs_SetError(-ENAMETOOLONG);
1814 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1817 obj = yaffsfs_FollowLink(obj,0,&loop);
1820 yaffsfs_SetError(-ENOTDIR);
1822 yaffsfs_SetError(-ELOOP);
1823 else if(!dir || !obj)
1824 yaffsfs_SetError(-ENOENT);
1826 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1828 yaffsfs_SetError(retVal);
1839 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1841 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1844 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1846 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1851 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1853 struct yaffs_obj *obj;
1858 yaffsfs_SetError(-EFAULT);
1863 obj = yaffsfs_HandleToObject(fd);
1866 yaffsfs_SetError(-EBADF);
1868 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1870 yaffsfs_SetError(retVal);
1880 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1882 struct yaffs_obj *obj;
1883 struct yaffs_obj *dir;
1888 if(!path || !name || !data ){
1889 yaffsfs_SetError(-EFAULT);
1893 if(yaffsfs_CheckPath(path) < 0){
1894 yaffsfs_SetError(-ENAMETOOLONG);
1900 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1903 obj = yaffsfs_FollowLink(obj,0,&loop);
1906 yaffsfs_SetError(-ENOTDIR);
1908 yaffsfs_SetError(-ELOOP);
1909 else if(!dir || !obj)
1910 yaffsfs_SetError(-ENOENT);
1912 retVal = yaffs_get_xattrib(obj,name,data,size);
1914 yaffsfs_SetError(retVal);
1924 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1926 return yaffs_do_getxattr( path, name, data, size, 1);
1928 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1930 return yaffs_do_getxattr( path, name, data, size, 0);
1935 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1937 struct yaffs_obj *obj;
1941 if(!name || !data ){
1942 yaffsfs_SetError(-EFAULT);
1947 obj = yaffsfs_HandleToObject(fd);
1950 retVal = yaffs_get_xattrib(obj,name,data,size);
1952 yaffsfs_SetError(retVal);
1957 yaffsfs_SetError(-EBADF);
1964 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1966 struct yaffs_obj *obj=NULL;
1967 struct yaffs_obj *dir=NULL;
1972 if(!path || !data ){
1973 yaffsfs_SetError(-EFAULT);
1977 if(yaffsfs_CheckPath(path) < 0){
1978 yaffsfs_SetError(-ENAMETOOLONG);
1984 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1987 obj = yaffsfs_FollowLink(obj,0,&loop);
1990 yaffsfs_SetError(-ENOTDIR);
1992 yaffsfs_SetError(-ELOOP);
1993 else if(!dir || !obj)
1994 yaffsfs_SetError(-ENOENT);
1996 retVal = yaffs_list_xattrib(obj, data,size);
1998 yaffsfs_SetError(retVal);
2009 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2011 return yaffs_do_listxattr(path, data, size, 1);
2014 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2016 return yaffs_do_listxattr(path, data, size, 0);
2019 int yaffs_flistxattr(int fd, char *data, int size)
2021 struct yaffs_obj *obj;
2026 yaffsfs_SetError(-EFAULT);
2031 obj = yaffsfs_HandleToObject(fd);
2034 retVal = yaffs_list_xattrib(obj,data,size);
2036 yaffsfs_SetError(retVal);
2041 yaffsfs_SetError(-EBADF);
2048 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
2050 struct yaffs_obj *obj=NULL;
2051 struct yaffs_obj *dir=NULL;
2057 yaffsfs_SetError(-EFAULT);
2061 if(yaffsfs_CheckPath(path) < 0){
2062 yaffsfs_SetError(-ENAMETOOLONG);
2068 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2071 obj = yaffsfs_FollowLink(obj,0,&loop);
2074 yaffsfs_SetError(-ENOTDIR);
2076 yaffsfs_SetError(-ELOOP);
2077 else if(!dir || !obj)
2078 yaffsfs_SetError(-ENOENT);
2080 retVal = yaffs_remove_xattrib(obj,name);
2082 yaffsfs_SetError(retVal);
2093 int yaffs_removexattr(const YCHAR *path, const char *name)
2095 return yaffs_do_removexattr(path, name, 1);
2098 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2100 return yaffs_do_removexattr(path, name, 0);
2103 int yaffs_fremovexattr(int fd, const char *name)
2105 struct yaffs_obj *obj;
2110 yaffsfs_SetError(-EFAULT);
2115 obj = yaffsfs_HandleToObject(fd);
2118 retVal = yaffs_remove_xattrib(obj,name);
2120 yaffsfs_SetError(retVal);
2125 yaffsfs_SetError(-EBADF);
2133 #ifdef CONFIG_YAFFS_WINCE
2134 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
2136 struct yaffs_obj *obj;
2141 obj = yaffsfs_HandleToObject(fd);
2146 wctime[0] = obj->win_ctime[0];
2147 wctime[1] = obj->win_ctime[1];
2150 watime[0] = obj->win_atime[0];
2151 watime[1] = obj->win_atime[1];
2154 wmtime[0] = obj->win_mtime[0];
2155 wmtime[1] = obj->win_mtime[1];
2162 yaffsfs_SetError(-EBADF);
2170 int yaffs_set_wince_times(int fd,
2171 const unsigned *wctime,
2172 const unsigned *watime,
2173 const unsigned *wmtime)
2175 struct yaffs_obj *obj;
2180 obj = yaffsfs_HandleToObject(fd);
2185 obj->win_ctime[0] = wctime[0];
2186 obj->win_ctime[1] = wctime[1];
2189 obj->win_atime[0] = watime[0];
2190 obj->win_atime[1] = watime[1];
2193 obj->win_mtime[0] = wmtime[0];
2194 obj->win_mtime[1] = wmtime[1];
2198 result = yaffs_flush_file(obj,0,0);
2202 yaffsfs_SetError(-EBADF);
2212 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2217 obj = yaffs_get_equivalent_obj(obj);
2220 obj->yst_mode = mode;
2222 result = yaffs_flush_file(obj,0,0);
2225 return result == YAFFS_OK ? 0 : -1;
2229 int yaffs_access(const YCHAR *path, int amode)
2231 struct yaffs_obj *obj=NULL;
2232 struct yaffs_obj *dir=NULL;
2238 yaffsfs_SetError(-EFAULT);
2242 if(yaffsfs_CheckPath(path) < 0){
2243 yaffsfs_SetError(-ENAMETOOLONG);
2247 if(amode & ~(R_OK | W_OK | X_OK)){
2248 yaffsfs_SetError(-EINVAL);
2254 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2255 obj = yaffsfs_FollowLink(obj,0,&loop);
2258 yaffsfs_SetError(-ENOTDIR);
2260 yaffsfs_SetError(-ELOOP);
2261 else if(!dir || !obj)
2262 yaffsfs_SetError(-ENOENT);
2263 else if((amode & W_OK) && obj->my_dev->read_only)
2264 yaffsfs_SetError(-EROFS);
2268 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2270 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2272 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2276 yaffsfs_SetError(-EACCES);
2288 int yaffs_chmod(const YCHAR *path, mode_t mode)
2290 struct yaffs_obj *obj=NULL;
2291 struct yaffs_obj *dir=NULL;
2297 yaffsfs_SetError(-EFAULT);
2301 if(yaffsfs_CheckPath(path) < 0){
2302 yaffsfs_SetError(-ENAMETOOLONG);
2307 yaffsfs_SetError(-EINVAL);
2313 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2314 obj = yaffsfs_FollowLink(obj,0,&loop);
2317 yaffsfs_SetError(-ENOTDIR);
2319 yaffsfs_SetError(-ELOOP);
2320 else if(!dir || !obj)
2321 yaffsfs_SetError(-ENOENT);
2322 else if(obj->my_dev->read_only)
2323 yaffsfs_SetError(-EROFS);
2325 retVal = yaffsfs_DoChMod(obj,mode);
2334 int yaffs_fchmod(int fd, mode_t mode)
2336 struct yaffs_obj *obj;
2340 yaffsfs_SetError(-EINVAL);
2345 obj = yaffsfs_HandleToObject(fd);
2348 yaffsfs_SetError(-EBADF);
2349 else if(obj->my_dev->read_only)
2350 yaffsfs_SetError(-EROFS);
2352 retVal = yaffsfs_DoChMod(obj,mode);
2359 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2361 struct yaffs_obj *parent = NULL;
2362 struct yaffs_obj *dir = NULL;
2364 YCHAR *alt_path = NULL;
2370 yaffsfs_SetError(-EFAULT);
2374 if(yaffsfs_CheckPath(path) < 0){
2375 yaffsfs_SetError(-ENAMETOOLONG);
2379 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2380 yaffsfs_SetError(-ENOMEM);
2387 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2388 if(!parent && notDir)
2389 yaffsfs_SetError(-ENOTDIR);
2391 yaffsfs_SetError(-ELOOP);
2393 yaffsfs_SetError(-ENOENT);
2394 else if(yaffsfs_TooManyObjects(parent->my_dev))
2395 yaffsfs_SetError(-ENFILE);
2396 else if(yaffs_strnlen(name,5) == 0){
2397 /* Trying to make the root itself */
2398 yaffsfs_SetError(-EEXIST);
2399 } else if(parent->my_dev->read_only)
2400 yaffsfs_SetError(-EROFS);
2402 dir = yaffs_create_dir(parent,name,mode,0,0);
2405 else if (yaffs_find_by_name(parent,name))
2406 yaffsfs_SetError(-EEXIST); /* the name already exists */
2408 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2419 int yaffs_rmdir(const YCHAR *path)
2425 yaffsfs_SetError(-EFAULT);
2429 if(yaffsfs_CheckPath(path) < 0){
2430 yaffsfs_SetError(-ENAMETOOLONG);
2434 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2435 yaffsfs_SetError(-ENOMEM);
2440 result = yaffsfs_DoUnlink(path,1);
2447 void * yaffs_getdev(const YCHAR *path)
2449 struct yaffs_dev *dev=NULL;
2451 dev = yaffsfs_FindDevice(path,&dummy);
2455 int yaffs_mount_common(const YCHAR *path,int read_only, int skip_checkpt)
2458 int result=YAFFS_FAIL;
2459 struct yaffs_dev *dev=NULL;
2462 yaffsfs_SetError(-EFAULT);
2466 yaffs_trace(YAFFS_TRACE_MOUNT,"yaffs: Mounting %s",path);
2468 if(yaffsfs_CheckPath(path) < 0){
2469 yaffsfs_SetError(-ENAMETOOLONG);
2475 yaffsfs_InitHandles();
2477 dev = yaffsfs_FindMountPoint(path);
2479 if(!dev->is_mounted){
2480 dev->read_only = read_only ? 1 : 0;
2482 u8 skip = dev->param.skip_checkpt_rd;
2483 dev->param.skip_checkpt_rd = 1;
2484 result = yaffs_guts_initialise(dev);
2485 dev->param.skip_checkpt_rd = skip;
2487 result = yaffs_guts_initialise(dev);
2490 if(result == YAFFS_FAIL)
2491 yaffsfs_SetError(-ENOMEM);
2492 retVal = result ? 0 : -1;
2496 yaffsfs_SetError(-EBUSY);
2498 yaffsfs_SetError(-ENODEV);
2505 int yaffs_mount2(const YCHAR *path, int readonly)
2507 return yaffs_mount_common(path, readonly, 0);
2509 int yaffs_mount(const YCHAR *path)
2511 return yaffs_mount_common(path, 0, 0);
2514 int yaffs_sync(const YCHAR *path)
2517 struct yaffs_dev *dev=NULL;
2521 yaffsfs_SetError(-EFAULT);
2525 if(yaffsfs_CheckPath(path) < 0){
2526 yaffsfs_SetError(-ENAMETOOLONG);
2531 dev = yaffsfs_FindDevice(path,&dummy);
2533 if(!dev->is_mounted)
2534 yaffsfs_SetError(-EINVAL);
2535 else if(dev->read_only)
2536 yaffsfs_SetError(-EROFS);
2539 yaffs_flush_whole_cache(dev);
2540 yaffs_checkpoint_save(dev);
2545 yaffsfs_SetError(-ENODEV);
2552 static int yaffsfs_IsDevBusy(struct yaffs_dev * dev)
2555 struct yaffs_obj *obj;
2557 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
2558 obj = yaffsfs_HandleToObject(i);
2559 if(obj && obj->my_dev == dev)
2566 int yaffs_remount(const YCHAR *path, int force, int read_only)
2569 struct yaffs_dev *dev=NULL;
2572 yaffsfs_SetError(-EFAULT);
2576 if(yaffsfs_CheckPath(path) < 0){
2577 yaffsfs_SetError(-ENAMETOOLONG);
2582 dev = yaffsfs_FindMountPoint(path);
2584 if(dev->is_mounted){
2585 yaffs_flush_whole_cache(dev);
2587 if(force || ! yaffsfs_IsDevBusy(dev)){
2589 yaffs_checkpoint_save(dev);
2590 dev->read_only = read_only ? 1 : 0;
2593 yaffsfs_SetError(-EBUSY);
2596 yaffsfs_SetError(-EINVAL);
2600 yaffsfs_SetError(-ENODEV);
2607 int yaffs_unmount2(const YCHAR *path, int force)
2610 struct yaffs_dev *dev=NULL;
2613 yaffsfs_SetError(-EFAULT);
2617 if(yaffsfs_CheckPath(path) < 0){
2618 yaffsfs_SetError(-ENAMETOOLONG);
2623 dev = yaffsfs_FindMountPoint(path);
2625 if(dev->is_mounted){
2627 yaffs_flush_whole_cache(dev);
2628 yaffs_checkpoint_save(dev);
2629 inUse = yaffsfs_IsDevBusy(dev);
2630 if(!inUse || force){
2632 yaffsfs_BreakDeviceHandles(dev);
2633 yaffs_deinitialise(dev);
2637 yaffsfs_SetError(-EBUSY);
2640 yaffsfs_SetError(-EINVAL);
2643 yaffsfs_SetError(-ENODEV);
2650 int yaffs_unmount(const YCHAR *path)
2652 return yaffs_unmount2(path,0);
2655 loff_t yaffs_freespace(const YCHAR *path)
2658 struct yaffs_dev *dev=NULL;
2662 yaffsfs_SetError(-EFAULT);
2666 if(yaffsfs_CheckPath(path) < 0){
2667 yaffsfs_SetError(-ENAMETOOLONG);
2672 dev = yaffsfs_FindDevice(path,&dummy);
2673 if(dev && dev->is_mounted){
2674 retVal = yaffs_get_n_free_chunks(dev);
2675 retVal *= dev->data_bytes_per_chunk;
2678 yaffsfs_SetError(-EINVAL);
2684 loff_t yaffs_totalspace(const YCHAR *path)
2687 struct yaffs_dev *dev=NULL;
2691 yaffsfs_SetError(-EFAULT);
2695 if(yaffsfs_CheckPath(path) < 0){
2696 yaffsfs_SetError(-ENAMETOOLONG);
2701 dev = yaffsfs_FindDevice(path,&dummy);
2702 if(dev && dev->is_mounted){
2703 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2704 dev->param.n_reserved_blocks;
2705 retVal *= dev->param.chunks_per_block;
2706 retVal *= dev->data_bytes_per_chunk;
2709 yaffsfs_SetError(-EINVAL);
2715 int yaffs_inodecount(const YCHAR *path)
2718 struct yaffs_dev *dev=NULL;
2722 yaffsfs_SetError(-EFAULT);
2726 if(yaffsfs_CheckPath(path) < 0){
2727 yaffsfs_SetError(-ENAMETOOLONG);
2732 dev = yaffsfs_FindDevice(path,&dummy);
2733 if(dev && dev->is_mounted) {
2734 int n_obj = dev->n_obj;
2735 if(n_obj > dev->n_hardlinks)
2736 retVal = n_obj - dev->n_hardlinks;
2740 yaffsfs_SetError(-EINVAL);
2747 void yaffs_add_device(struct yaffs_dev *dev)
2749 struct list_head *cfg;
2750 /* First check that the device is not in the list. */
2752 list_for_each(cfg, &yaffsfs_deviceList){
2753 if(dev == list_entry(cfg, struct yaffs_dev, dev_list))
2757 dev->is_mounted = 0;
2758 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2760 if(!dev->dev_list.next)
2761 INIT_LIST_HEAD(&dev->dev_list);
2763 list_add(&dev->dev_list,&yaffsfs_deviceList);
2766 void yaffs_remove_device(struct yaffs_dev *dev)
2768 list_del_init(&dev->dev_list);
2774 /* Directory search stuff. */
2777 * Directory search context
2779 * NB this is an opaque structure.
2786 yaffs_dirent de; /* directory entry being used by this dsc */
2787 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2788 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2789 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2791 struct list_head others;
2792 } yaffsfs_DirectorySearchContext;
2796 static struct list_head search_contexts;
2799 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2803 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2807 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2808 dsc->nextReturn = NULL;
2810 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2811 struct yaffs_obj,siblings);
2813 /* Hey someone isn't playing nice! */
2817 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2821 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2823 if( dsc->nextReturn == NULL ||
2824 list_empty(&dsc->dirObj->variant.dir_variant.children))
2825 dsc->nextReturn = NULL;
2827 struct list_head *next = dsc->nextReturn->siblings.next;
2829 if( next == &dsc->dirObj->variant.dir_variant.children)
2830 dsc->nextReturn = NULL; /* end of list */
2832 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2835 /* Hey someone isn't playing nice! */
2839 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2842 struct list_head *i;
2843 yaffsfs_DirectorySearchContext *dsc;
2845 /* if search contexts not initilised then skip */
2846 if(!search_contexts.next)
2849 /* Iterate through the directory search contexts.
2850 * If any are the one being removed, then advance the dsc to
2851 * the next one to prevent a hanging ptr.
2853 list_for_each(i, &search_contexts) {
2855 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2856 if(dsc->nextReturn == obj)
2857 yaffsfs_DirAdvance(dsc);
2863 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2865 yaffs_DIR *dir = NULL;
2866 struct yaffs_obj *obj = NULL;
2867 yaffsfs_DirectorySearchContext *dsc = NULL;
2872 yaffsfs_SetError(-EFAULT);
2876 if(yaffsfs_CheckPath(dirname) < 0){
2877 yaffsfs_SetError(-ENAMETOOLONG);
2883 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2886 yaffsfs_SetError(-ENOTDIR);
2888 yaffsfs_SetError(-ELOOP);
2890 yaffsfs_SetError(-ENOENT);
2891 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2892 yaffsfs_SetError(-ENOTDIR);
2895 dsc = kmalloc(sizeof(yaffsfs_DirectorySearchContext), 0);
2896 dir = (yaffs_DIR *)dsc;
2899 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2900 dsc->magic = YAFFS_MAGIC;
2902 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2903 INIT_LIST_HEAD(&dsc->others);
2905 if(!search_contexts.next)
2906 INIT_LIST_HEAD(&search_contexts);
2908 list_add(&dsc->others,&search_contexts);
2909 yaffsfs_SetDirRewound(dsc);
2919 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2921 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2922 struct yaffs_dirent *retVal = NULL;
2926 if(dsc && dsc->magic == YAFFS_MAGIC){
2927 yaffsfs_SetError(0);
2928 if(dsc->nextReturn){
2929 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2930 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2931 dsc->de.d_off = dsc->offset++;
2932 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2933 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2935 /* this should not happen! */
2936 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2938 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2940 yaffsfs_DirAdvance(dsc);
2944 yaffsfs_SetError(-EBADF);
2953 void yaffs_rewinddir(yaffs_DIR *dirp)
2955 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2959 yaffsfs_SetDirRewound(dsc);
2965 int yaffs_closedir(yaffs_DIR *dirp)
2967 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2970 yaffsfs_SetError(-EFAULT);
2976 list_del(&dsc->others); /* unhook from list */
2982 /* End of directory stuff */
2985 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2987 struct yaffs_obj *parent = NULL;
2988 struct yaffs_obj *obj;
2991 int mode = 0; /* ignore for now */
2995 if(!oldpath || !newpath){
2996 yaffsfs_SetError(-EFAULT);
3000 if(yaffsfs_CheckPath(newpath) < 0 ||
3001 yaffsfs_CheckPath(oldpath) < 0){
3002 yaffsfs_SetError(-ENAMETOOLONG);
3007 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
3008 if(!parent && notDir)
3009 yaffsfs_SetError(-ENOTDIR);
3011 yaffsfs_SetError(-ELOOP);
3012 else if( !parent || yaffs_strnlen(name,5) < 1)
3013 yaffsfs_SetError(-ENOENT);
3014 else if(yaffsfs_TooManyObjects(parent->my_dev))
3015 yaffsfs_SetError(-ENFILE);
3016 else if(parent->my_dev->read_only)
3017 yaffsfs_SetError(-EROFS);
3019 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
3022 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
3023 yaffsfs_SetError(-EEXIST);
3025 yaffsfs_SetError(-ENOSPC);
3034 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3036 struct yaffs_obj *obj = NULL;
3037 struct yaffs_obj *dir = NULL;
3043 yaffsfs_SetError(-EFAULT);
3049 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
3052 yaffsfs_SetError(-ENOTDIR);
3054 yaffsfs_SetError(-ELOOP);
3055 else if(!dir || !obj)
3056 yaffsfs_SetError(-ENOENT);
3057 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3058 yaffsfs_SetError(-EINVAL);
3060 YCHAR *alias = obj->variant.symlink_variant.alias;
3061 memset(buf,0,bufsiz);
3062 yaffs_strncpy(buf,alias,bufsiz - 1);
3069 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3071 /* Creates a link called newpath to existing oldpath */
3072 struct yaffs_obj *obj = NULL;
3073 struct yaffs_obj *lnk = NULL;
3074 struct yaffs_obj *obj_dir = NULL;
3075 struct yaffs_obj *lnk_dir = NULL;
3083 if(!oldpath || !linkpath){
3084 yaffsfs_SetError(-EFAULT);
3088 if(yaffsfs_CheckPath(linkpath) < 0 ||
3089 yaffsfs_CheckPath(oldpath) < 0){
3090 yaffsfs_SetError(-ENAMETOOLONG);
3096 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
3097 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
3098 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
3100 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3101 yaffsfs_SetError(-ENOTDIR);
3102 else if(objLoop || lnkLoop)
3103 yaffsfs_SetError(-ELOOP);
3104 else if(!obj_dir || !lnk_dir || !obj)
3105 yaffsfs_SetError(-ENOENT);
3106 else if(obj->my_dev->read_only)
3107 yaffsfs_SetError(-EROFS);
3108 else if(yaffsfs_TooManyObjects(obj->my_dev))
3109 yaffsfs_SetError(-ENFILE);
3111 yaffsfs_SetError(-EEXIST);
3112 else if(lnk_dir->my_dev != obj->my_dev)
3113 yaffsfs_SetError(-EXDEV);
3115 retVal = yaffsfs_CheckNameLength(newname);
3118 lnk = yaffs_link_obj(lnk_dir,newname,obj);
3122 yaffsfs_SetError(-ENOSPC);
3132 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3138 yaffsfs_SetError(-EINVAL);
3144 * D E B U G F U N C T I O N S
3149 * Returns number of handles attached to the object
3151 int yaffs_n_handles(const YCHAR *path)
3153 struct yaffs_obj *obj;
3156 yaffsfs_SetError(-EFAULT);
3160 if(yaffsfs_CheckPath(path) < 0){
3161 yaffsfs_SetError(-ENAMETOOLONG);
3165 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
3168 return yaffsfs_CountHandles(obj);
3173 int yaffs_get_error(void)
3175 return yaffsfs_GetLastError();
3178 int yaffs_set_error(int error)
3180 yaffsfs_SetError(error);
3184 int yaffs_dump_dev(const YCHAR *path)
3191 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
3194 struct yaffs_dev *dev = obj->my_dev;
3197 "n_page_writes.......... %d\n"
3198 "n_page_reads........... %d\n"
3199 "n_erasures....... %d\n"
3200 "n_gc_copies............ %d\n"
3201 "garbageCollections... %d\n"
3202 "passiveGarbageColl'ns %d\n"
3208 dev->garbageCollections,
3209 dev->passiveGarbageCollections