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"
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 */
89 yaffs_dirent de; /* directory entry being used by this dsc */
90 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
91 struct yaffs_obj *dirObj; /* ptr to directory being searched */
92 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
93 struct list_head others;
96 } yaffsfs_DirectorySearchContext;
99 static yaffsfs_DirectorySearchContext yaffsfs_dsc[YAFFSFS_N_DSC];
100 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
101 static yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
102 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
104 static int yaffsfs_handlesInitialised;
107 unsigned yaffs_set_trace(unsigned tm)
109 yaffs_trace_mask = tm;
110 return yaffs_trace_mask;
113 unsigned yaffs_get_trace(void)
115 return yaffs_trace_mask;
120 * Inilitalise handle management on start-up.
123 static void yaffsfs_InitHandles(void)
126 if(yaffsfs_handlesInitialised)
129 memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
130 memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
131 memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
132 memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
134 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
135 yaffsfs_fd[i].inodeId = -1;
136 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
137 yaffsfs_handle[i].fdId = -1;
140 static yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
142 if(h >= 0 && h < YAFFSFS_N_HANDLES)
143 return &yaffsfs_handle[h];
147 static yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
149 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
151 if(h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
152 return &yaffsfs_fd[h->fdId];
157 static yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
159 yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
161 if(fd && fd->handleCount > 0 &&
162 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
163 return &yaffsfs_inode[fd->inodeId];
168 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
170 yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
179 * yaffsfs_FindInodeIdForObject
180 * Find the inode entry for an object, if it exists.
183 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
189 obj = yaffs_get_equivalent_obj(obj);
191 /* Look for it in open inode table*/
192 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
193 if(yaffsfs_inode[i].iObj == obj)
200 * yaffsfs_GetInodeIdForObject
201 * Grab an inode entry when opening a new inode.
203 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
207 yaffsfs_Inode *in = NULL;
210 obj = yaffs_get_equivalent_obj(obj);
212 ret = yaffsfs_FindInodeIdForObject(obj);
214 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
215 if(!yaffsfs_inode[i].iObj)
220 in = &yaffsfs_inode[ret];
232 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
234 int i = yaffsfs_FindInodeIdForObject(obj);
237 return yaffsfs_inode[i].count;
242 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
244 struct yaffs_obj *obj;
251 obj->my_inode = NULL;
256 static void yaffsfs_PutInode(int inodeId)
258 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
259 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
262 yaffsfs_ReleaseInode(in);
270 static int yaffsfs_NewHandle(yaffsfs_Handle **hptr)
275 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
276 h = &yaffsfs_handle[i];
278 memset(h,0,sizeof(yaffsfs_Handle));
289 static int yaffsfs_NewHandleAndFileDes(void)
293 yaffsfs_Handle *h = NULL;
294 int handle = yaffsfs_NewHandle(&h);
299 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
301 if(fd->handleCount < 1){
302 memset(fd,0,sizeof(yaffsfs_FileDes));
310 /* Dump the handle because we could not get a fd */
317 * Increase use of handle when reading/writing a file
318 * Also gets the file descriptor.
321 static int yaffsfs_GetHandle(int handle)
323 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
325 if(h && h->useCount > 0){
334 * Let go of a handle when closing a file or aborting an open or
335 * ending a read or write.
338 static int yaffsfs_PutFileDes(int fdId)
342 if(fdId >= 0 && fdId < YAFFSFS_N_HANDLES){
343 fd = &yaffsfs_fd[fdId];
345 if(fd->handleCount < 1){
346 if(fd->inodeId >= 0){
347 yaffsfs_PutInode(fd->inodeId);
354 static int yaffsfs_PutHandle(int handle)
356 yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
358 if(h && h->useCount > 0){
361 yaffsfs_PutFileDes(h->fdId);
369 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
373 struct yaffs_obj *obj;
375 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
376 h = yaffsfs_HandleToPointer(i);
377 fd = yaffsfs_HandleToFileDes(i);
378 obj = yaffsfs_HandleToObject(i);
379 if(h && h->useCount > 0){
383 if(fd && fd->handleCount>0 && obj && obj->my_dev == dev){
386 yaffsfs_PutInode(fd->inodeId);
396 * Stuff to handle names.
398 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
400 static int yaffs_toupper(YCHAR a)
402 if(a >= 'a' && a <= 'z')
403 return (a - 'a') + 'A';
408 int yaffsfs_Match(YCHAR a, YCHAR b)
410 return (yaffs_toupper(a) == yaffs_toupper(b));
413 int yaffsfs_Match(YCHAR a, YCHAR b)
420 int yaffsfs_IsPathDivider(YCHAR ch)
422 const YCHAR *str = YAFFS_PATH_DIVIDERS;
433 int yaffsfs_CheckNameLength(const char *name)
437 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
440 yaffsfs_SetError(-ENOENT);
442 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
443 yaffsfs_SetError(-ENAMETOOLONG);
451 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
453 YCHAR *alt_path = NULL;
458 * We don't have a definition for max path length.
459 * We will use 3 * max name length instead.
462 path_length = yaffs_strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
464 /* If the last character is a path divider, then we need to
465 * trim it back so that the name look-up works properly.
466 * eg. /foo/new_dir/ -> /foo/newdir
467 * Curveball: Need to handle multiple path dividers:
468 * eg. /foof/sdfse///// -> /foo/sdfse
470 if(path_length > 0 &&
471 yaffsfs_IsPathDivider(path[path_length-1])){
472 alt_path = kmalloc(path_length + 1, 0);
475 yaffs_strcpy(alt_path, path);
476 for(i = path_length-1;
477 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
479 alt_path[i] = (YCHAR) 0;
481 *ret_path = alt_path;
486 LIST_HEAD(yaffsfs_deviceList);
491 * Scan the configuration list to find the device
492 * Curveballs: Should match paths that end in '/' too
493 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
495 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
497 struct list_head *cfg;
498 const YCHAR *leftOver;
500 struct yaffs_dev *retval = NULL;
501 struct yaffs_dev *dev = NULL;
503 int longestMatch = -1;
507 * Check all configs, choose the one that:
508 * 1) Actually matches a prefix (ie /a amd /abc will not match
509 * 2) Matches the longest.
511 list_for_each(cfg, &yaffsfs_deviceList){
512 dev = list_entry(cfg, struct yaffs_dev, dev_list);
519 while(matching && *p && *leftOver){
520 /* Skip over any /s */
521 while(yaffsfs_IsPathDivider(*p))
524 /* Skip over any /s */
525 while(yaffsfs_IsPathDivider(*leftOver))
528 /* Now match the text part */
530 *p && !yaffsfs_IsPathDivider(*p) &&
531 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
532 if(yaffsfs_Match(*p,*leftOver)){
542 /* Skip over any /s in leftOver */
543 while(yaffsfs_IsPathDivider(*leftOver))
546 // Skip over any /s in p
547 while(yaffsfs_IsPathDivider(*p))
550 // p should now be at the end of the string (ie. fully matched)
554 if( matching && (thisMatchLength > longestMatch))
557 *restOfPath = (YCHAR *)leftOver;
559 longestMatch = thisMatchLength;
566 static int yaffsfs_CheckPath(const YCHAR *path)
570 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
571 if(yaffsfs_IsPathDivider(*path)){
579 return (*path) ? -1 : 0;
582 /* FindMountPoint only returns a dev entry if the path is a mount point */
583 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
585 struct yaffs_dev *dev;
586 YCHAR *restOfPath=NULL;
587 dev = yaffsfs_FindDevice(path,&restOfPath);
588 if(dev && restOfPath && *restOfPath)
593 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
596 struct yaffs_dev *dev;
598 dev= yaffsfs_FindDevice(path,restOfPath);
599 if(dev && dev->is_mounted){
600 return dev->root_dir;
605 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
606 int symDepth, int *loop)
610 obj = yaffs_get_equivalent_obj(obj);
612 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
613 YCHAR *alias = obj->variant.symlink_variant.alias;
615 if(yaffsfs_IsPathDivider(*alias))
616 /* Starts with a /, need to scan from root up */
617 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
620 /* Relative to here, so use the parent of the symlink as a start */
621 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
629 * yaffsfs_FindDirectory
630 * Parse a path to determine the directory and the name within the directory.
632 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
634 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
635 const YCHAR *path, YCHAR **name, int symDepth,
636 int *notDir,int *loop)
638 struct yaffs_obj *dir;
640 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
643 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
651 restOfPath = (YCHAR *)path;
654 dir = yaffsfs_FindRoot(path,&restOfPath);
660 * curve ball: also throw away surplus '/'
661 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
663 while(yaffsfs_IsPathDivider(*restOfPath))
664 restOfPath++; /* get rid of '/' */
669 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
670 if (i < YAFFS_MAX_NAME_LENGTH){
671 str[i] = *restOfPath;
679 /* got to the end of the string */
682 if(yaffs_strcmp(str,_Y(".")) == 0){
684 } else if(yaffs_strcmp(str,_Y("..")) == 0) {
687 dir = yaffs_find_by_name(dir,str);
689 dir = yaffsfs_FollowLink(dir,symDepth,loop);
691 if(dir && dir->variant_type !=
692 YAFFS_OBJECT_TYPE_DIRECTORY){
701 /* directory did not exist. */
705 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
712 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
716 * yaffsfs_FindObject turns a path for an existing object into the object
718 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
719 const YCHAR *path,int symDepth, int getEquiv,
720 struct yaffs_obj **dirOut, int *notDir,int *loop)
722 struct yaffs_obj *dir;
723 struct yaffs_obj *obj;
726 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
732 obj = yaffs_find_by_name(dir,name);
737 obj = yaffs_get_equivalent_obj(obj);
743 /*************************************************************************
744 * Start of yaffsfs visible functions.
745 *************************************************************************/
747 int yaffs_dup(int handle)
749 int newHandleNumber = -1;
750 yaffsfs_FileDes *existingFD = NULL;
751 yaffsfs_Handle *existingHandle = NULL;
752 yaffsfs_Handle *newHandle = NULL;
755 existingHandle = yaffsfs_HandleToPointer(handle);
756 existingFD = yaffsfs_HandleToFileDes(handle);
758 newHandleNumber = yaffsfs_NewHandle(&newHandle);
760 newHandle->fdId = existingHandle->fdId;
761 existingFD->handleCount++;
767 yaffsfs_SetError(-EBADF);
769 yaffsfs_SetError(-ENOMEM);
771 return newHandleNumber;
775 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
777 int current_objects = dev->n_obj - dev->n_deleted_files;
779 if(dev->param.max_objects && current_objects > dev->param.max_objects)
785 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
787 struct yaffs_obj *obj = NULL;
788 struct yaffs_obj *dir = NULL;
791 yaffsfs_FileDes *fd = NULL;
794 int errorReported = 0;
795 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
796 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
797 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
798 u8 sharedReadAllowed;
799 u8 sharedWriteAllowed;
808 yaffsfs_SetError(-EFAULT);
812 if(yaffsfs_CheckPath(path) < 0){
813 yaffsfs_SetError(-ENAMETOOLONG);
817 /* O_EXCL only has meaning if O_CREAT is specified */
818 if(!(oflag & O_CREAT))
821 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
822 if( (oflag & O_CREAT) & (oflag & O_EXCL))
825 /* Todo: Are there any more flag combos to sanitise ? */
827 /* Figure out if reading or writing is requested */
829 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
830 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
834 handle = yaffsfs_NewHandleAndFileDes();
837 yaffsfs_SetError(-ENFILE);
841 fd = yaffsfs_HandleToFileDes(handle);
843 /* try to find the exisiting object */
844 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
846 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
849 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
850 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
856 /* The file already exists or it might be a directory */
858 /* If it is a directory then we can't open it as a file */
859 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
861 yaffsfs_SetError(-EISDIR);
865 /* Open should fail if O_CREAT and O_EXCL are specified since
868 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
870 yaffsfs_SetError(-EEXIST);
874 /* Check file permissions */
875 if( readRequested && !(obj->yst_mode & S_IREAD))
878 if( writeRequested && !(obj->yst_mode & S_IWRITE))
881 if( !errorReported && writeRequested &&
882 obj->my_dev->read_only){
884 yaffsfs_SetError(-EROFS);
888 if(openDenied && !errorReported ) {
889 /* Error if the file exists but permissions are refused. */
890 yaffsfs_SetError(-EACCES);
894 /* Check sharing of an existing object. */
896 yaffsfs_FileDes *fdx;
899 sharedReadAllowed = 1;
900 sharedWriteAllowed = 1;
903 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
904 fdx = &yaffsfs_fd[i];
905 if(fdx->handleCount > 0 &&
907 yaffsfs_inode[fdx->inodeId].iObj == obj){
909 sharedReadAllowed = 0;
911 sharedWriteAllowed = 0;
921 if((!sharedReadAllowed && readRequested)||
922 (!shareRead && alreadyReading) ||
923 (!sharedWriteAllowed && writeRequested) ||
924 (!shareWrite && alreadyWriting)){
926 yaffsfs_SetError(-EBUSY);
933 /* If we could not open an existing object, then let's see if
934 * the directory exists. If not, error.
936 if(!obj && !errorReported){
937 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
939 yaffsfs_SetError(-ENOTDIR);
942 yaffsfs_SetError(-ELOOP);
945 yaffsfs_SetError(-ENOENT);
950 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
951 /* Let's see if we can create this file if it does not exist. */
952 if(dir->my_dev->read_only){
953 yaffsfs_SetError(-EROFS);
955 } else if(yaffsfs_TooManyObjects(dir->my_dev)) {
956 yaffsfs_SetError(-ENFILE);
959 obj = yaffs_create_file(dir,name,mode,0,0);
961 if(!obj && !errorReported){
962 yaffsfs_SetError(-ENOSPC);
967 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
968 /* Error if the file does not exist and CREAT is not set. */
969 yaffsfs_SetError(-ENOENT);
973 if(obj && !openDenied) {
974 int inodeId = yaffsfs_GetInodeIdForObject(obj);
978 * Todo: Fix any problem if inodes run out, though that
979 * can't happen if the number of inode items >= number of handles.
983 fd->inodeId = inodeId;
984 fd->reading = readRequested;
985 fd->writing = writeRequested;
986 fd->append = (oflag & O_APPEND) ? 1 : 0;
988 fd->shareRead = shareRead;
989 fd->shareWrite = shareWrite;
991 /* Hook inode to object */
992 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
994 if((oflag & O_TRUNC) && fd->writing)
995 yaffs_resize_file(obj,0);
997 yaffsfs_PutHandle(handle);
999 yaffsfs_SetError(0); /* Problem */
1009 int yaffs_open(const YCHAR *path, int oflag, int mode)
1011 return yaffs_open_sharing(path, oflag, mode,
1012 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1015 int yaffs_Dofsync(int handle,int datasync)
1018 struct yaffs_obj *obj;
1022 obj = yaffsfs_HandleToObject(handle);
1025 yaffsfs_SetError(-EBADF);
1026 else if(obj->my_dev->read_only)
1027 yaffsfs_SetError(-EROFS);
1029 yaffs_flush_file(obj,1,datasync);
1038 int yaffs_fsync(int handle)
1040 return yaffs_Dofsync(handle,0);
1043 int yaffs_flush(int handle)
1045 return yaffs_fsync(handle);
1048 int yaffs_fdatasync(int handle)
1050 return yaffs_Dofsync(handle,1);
1053 int yaffs_close(int handle)
1055 yaffsfs_Handle *h = NULL;
1056 struct yaffs_obj *obj = NULL;
1061 h = yaffsfs_HandleToPointer(handle);
1062 obj = yaffsfs_HandleToObject(handle);
1065 yaffsfs_SetError(-EBADF);
1068 yaffs_flush_file(obj,1,0);
1069 yaffsfs_PutHandle(handle);
1080 int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte, int isPread, loff_t offset)
1082 yaffsfs_FileDes *fd = NULL;
1083 struct yaffs_obj *obj = NULL;
1085 loff_t startPos = 0;
1091 u8 *buf = (u8 *)vbuf;
1094 yaffsfs_SetError(-EFAULT);
1099 fd = yaffsfs_HandleToFileDes(handle);
1100 obj = yaffsfs_HandleToObject(handle);
1104 yaffsfs_SetError(-EBADF);
1106 } else if(!fd->reading){
1107 /* Not a reading handle */
1108 yaffsfs_SetError(-EINVAL);
1110 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
1111 yaffsfs_SetError(-EINVAL);
1117 startPos = fd->position;
1121 if(yaffs_get_obj_length(obj) > pos)
1122 maxRead = yaffs_get_obj_length(obj) - pos;
1130 yaffsfs_GetHandle(handle);
1132 endPos = pos + nbyte;
1134 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1135 nbyte > YAFFS_MAX_FILE_SIZE ||
1136 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1142 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1147 * Need to reverify object in case the device was
1148 * unmounted in another thread.
1150 obj = yaffsfs_HandleToObject(handle);
1154 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1162 if(nRead == nToRead)
1165 nbyte = 0; /* no more to read */
1175 yaffsfs_PutHandle(handle);
1179 fd->position = startPos + totalRead;
1181 yaffsfs_SetError(-EINVAL);
1188 return (totalRead >= 0) ? totalRead : -1;
1192 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1194 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1197 int yaffs_pread(int handle, void *buf, unsigned int nbyte, loff_t offset)
1199 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1202 int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte, int isPwrite, loff_t offset)
1204 yaffsfs_FileDes *fd = NULL;
1205 struct yaffs_obj *obj = NULL;
1207 loff_t startPos = 0;
1210 int totalWritten = 0;
1211 int write_trhrough = 0;
1213 const u8 *buf = (const u8 *)vbuf;
1216 yaffsfs_SetError(-EFAULT);
1221 fd = yaffsfs_HandleToFileDes(handle);
1222 obj = yaffsfs_HandleToObject(handle);
1226 yaffsfs_SetError(-EBADF);
1228 } else if(!fd->writing){
1229 yaffsfs_SetError(-EINVAL);
1231 } else if(obj->my_dev->read_only){
1232 yaffsfs_SetError(-EROFS);
1236 startPos = yaffs_get_obj_length(obj);
1240 startPos = fd->position;
1242 yaffsfs_GetHandle(handle);
1244 endPos = pos + nbyte;
1246 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1247 nbyte > YAFFS_MAX_FILE_SIZE ||
1248 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1255 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1256 if(nToWrite > nbyte)
1260 * Need to reverify object in case the device was
1261 * remounted or unmounted in another thread.
1263 obj = yaffsfs_HandleToObject(handle);
1264 if(!obj || obj->my_dev->read_only)
1267 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1270 totalWritten += nWritten;
1275 if(nWritten == nToWrite)
1280 if(nWritten < 1 && totalWritten < 1){
1281 yaffsfs_SetError(-ENOSPC);
1291 yaffsfs_PutHandle(handle);
1294 if(totalWritten > 0)
1295 fd->position = startPos + totalWritten;
1297 yaffsfs_SetError(-EINVAL);
1303 return (totalWritten >= 0) ? totalWritten : -1;
1306 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1308 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1311 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, loff_t offset)
1313 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1317 int yaffs_truncate(const YCHAR *path,loff_t new_size)
1319 struct yaffs_obj *obj = NULL;
1320 struct yaffs_obj *dir = NULL;
1321 int result = YAFFS_FAIL;
1326 yaffsfs_SetError(-EFAULT);
1330 if(yaffsfs_CheckPath(path) < 0){
1331 yaffsfs_SetError(-ENAMETOOLONG);
1337 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1338 obj = yaffsfs_FollowLink(obj,0,&loop);
1341 yaffsfs_SetError(-ENOTDIR);
1343 yaffsfs_SetError(-ELOOP);
1344 else if(!dir || !obj)
1345 yaffsfs_SetError(-ENOENT);
1346 else if(obj->my_dev->read_only)
1347 yaffsfs_SetError(-EROFS);
1348 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1349 yaffsfs_SetError(-EISDIR);
1350 else if(obj->my_dev->read_only)
1351 yaffsfs_SetError(-EROFS);
1352 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1353 yaffsfs_SetError(-EINVAL);
1355 result = yaffs_resize_file(obj, new_size);
1359 return (result) ? 0 : -1;
1362 int yaffs_ftruncate(int handle, loff_t new_size)
1364 yaffsfs_FileDes *fd = NULL;
1365 struct yaffs_obj *obj = NULL;
1369 fd = yaffsfs_HandleToFileDes(handle);
1370 obj = yaffsfs_HandleToObject(handle);
1374 yaffsfs_SetError(-EBADF);
1375 else if(!fd->writing)
1376 yaffsfs_SetError(-EINVAL);
1377 else if(obj->my_dev->read_only)
1378 yaffsfs_SetError(-EROFS);
1379 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1380 yaffsfs_SetError(-EINVAL);
1382 /* resize the file */
1383 result = yaffs_resize_file(obj, new_size);
1386 return (result) ? 0 : -1;
1390 loff_t yaffs_lseek(int handle, loff_t offset, int whence)
1392 yaffsfs_FileDes *fd = NULL;
1393 struct yaffs_obj *obj = NULL;
1398 fd = yaffsfs_HandleToFileDes(handle);
1399 obj = yaffsfs_HandleToObject(handle);
1402 yaffsfs_SetError(-EBADF);
1403 else if(offset > YAFFS_MAX_FILE_SIZE)
1404 yaffsfs_SetError(-EINVAL);
1406 if(whence == SEEK_SET){
1409 } else if(whence == SEEK_CUR) {
1410 if( (fd->position + offset) >= 0)
1411 pos = (fd->position + offset);
1412 } else if(whence == SEEK_END) {
1413 fSize = yaffs_get_obj_length(obj);
1414 if(fSize >= 0 && (fSize + offset) >= 0)
1415 pos = fSize + offset;
1418 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1421 yaffsfs_SetError(-EINVAL);
1432 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1434 struct yaffs_obj *dir = NULL;
1435 struct yaffs_obj *obj = NULL;
1437 int result = YAFFS_FAIL;
1442 yaffsfs_SetError(-EFAULT);
1446 if(yaffsfs_CheckPath(path) < 0){
1447 yaffsfs_SetError(-ENAMETOOLONG);
1453 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1454 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1457 yaffsfs_SetError(-ENOTDIR);
1459 yaffsfs_SetError(-ELOOP);
1461 yaffsfs_SetError(-ENOENT);
1462 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1463 yaffsfs_SetError(-EINVAL);
1465 yaffsfs_SetError(-ENOENT);
1466 else if(obj->my_dev->read_only)
1467 yaffsfs_SetError(-EROFS);
1468 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1469 yaffsfs_SetError(-EISDIR);
1470 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1471 yaffsfs_SetError(-ENOTDIR);
1472 else if(isDirectory && obj == obj->my_dev->root_dir)
1473 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1475 result = yaffs_unlinker(dir,name);
1477 if(result == YAFFS_FAIL && isDirectory)
1478 yaffsfs_SetError(-ENOTEMPTY);
1483 return (result == YAFFS_FAIL) ? -1 : 0;
1487 int yaffs_unlink(const YCHAR *path)
1489 return yaffsfs_DoUnlink(path,0);
1492 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1494 struct yaffs_obj *olddir = NULL;
1495 struct yaffs_obj *newdir = NULL;
1496 struct yaffs_obj *obj = NULL;
1497 struct yaffs_obj *newobj = NULL;
1500 int result= YAFFS_FAIL;
1501 int rename_allowed = 1;
1507 YCHAR *alt_newpath=NULL;
1509 if(!oldPath || !newPath){
1510 yaffsfs_SetError(-EFAULT);
1514 if(yaffsfs_CheckPath(oldPath) < 0 ||
1515 yaffsfs_CheckPath(newPath) < 0){
1516 yaffsfs_SetError(-ENAMETOOLONG);
1520 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1521 yaffsfs_SetError(-ENOMEM);
1525 newPath = alt_newpath;
1530 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1531 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1532 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1533 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1535 /* If the object being renamed is a directory and the
1536 * path ended with a "/" then the olddir == obj.
1537 * We pass through NULL for the old name to tell the lower layers
1538 * to use olddir as the object.
1544 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1545 yaffsfs_SetError(-ENOTDIR);
1547 } else if(oldLoop || newLoop) {
1548 yaffsfs_SetError(-ELOOP);
1550 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1551 yaffsfs_SetError(-EINVAL);
1553 }else if(!olddir || !newdir || !obj) {
1554 yaffsfs_SetError(-ENOENT);
1556 } else if(obj->my_dev->read_only){
1557 yaffsfs_SetError(-EROFS);
1559 } else if(yaffs_is_non_empty_dir(newobj)){
1560 yaffsfs_SetError(-ENOTEMPTY);
1562 } else if(olddir->my_dev != newdir->my_dev) {
1563 /* Rename must be on same device */
1564 yaffsfs_SetError(-EXDEV);
1566 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1568 * It is a directory, check that it is not being renamed to
1569 * being its own decendent.
1570 * Do this by tracing from the new directory back to the root,
1574 struct yaffs_obj *xx = newdir;
1576 while( rename_allowed && xx){
1582 yaffsfs_SetError(-EINVAL);
1586 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1593 return (result == YAFFS_FAIL) ? -1 : 0;
1597 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1601 obj = yaffs_get_equivalent_obj(obj);
1604 buf->st_dev = (int)obj->my_dev->os_context;
1605 buf->st_ino = obj->obj_id;
1606 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1608 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1609 buf->st_mode |= S_IFDIR;
1610 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1611 buf->st_mode |= S_IFLNK;
1612 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1613 buf->st_mode |= S_IFREG;
1615 buf->st_nlink = yaffs_get_obj_link_count(obj);
1618 buf->st_rdev = obj->yst_rdev;
1619 buf->st_size = yaffs_get_obj_length(obj);
1620 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1621 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1622 #if CONFIG_YAFFS_WINCE
1623 buf->yst_wince_atime[0] = obj->win_atime[0];
1624 buf->yst_wince_atime[1] = obj->win_atime[1];
1625 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1626 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1627 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1628 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1630 buf->yst_atime = obj->yst_atime;
1631 buf->yst_ctime = obj->yst_ctime;
1632 buf->yst_mtime = obj->yst_mtime;
1639 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1641 struct yaffs_obj *obj=NULL;
1642 struct yaffs_obj *dir=NULL;
1648 yaffsfs_SetError(-EFAULT);
1652 if(yaffsfs_CheckPath(path) < 0){
1653 yaffsfs_SetError(-ENAMETOOLONG);
1659 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1662 obj = yaffsfs_FollowLink(obj,0,&loop);
1665 yaffsfs_SetError(-ENOTDIR);
1667 yaffsfs_SetError(-ELOOP);
1668 else if(!dir || !obj)
1669 yaffsfs_SetError(-ENOENT);
1671 retVal = yaffsfs_DoStat(obj,buf);
1679 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1681 return yaffsfs_DoStatOrLStat(path,buf,0);
1684 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1686 return yaffsfs_DoStatOrLStat(path,buf,1);
1689 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1691 struct yaffs_obj *obj;
1696 yaffsfs_SetError(-EFAULT);
1701 obj = yaffsfs_HandleToObject(fd);
1704 retVal = yaffsfs_DoStat(obj,buf);
1707 yaffsfs_SetError(-EBADF);
1714 static int yaffsfs_DoUtime(struct yaffs_obj *obj,const struct yaffs_utimbuf *buf)
1719 struct yaffs_utimbuf local;
1721 obj = yaffs_get_equivalent_obj(obj);
1723 if(obj && obj->my_dev->read_only) {
1724 yaffsfs_SetError(-EROFS);
1730 local.actime = Y_CURRENT_TIME;
1731 local.modtime = local.actime;
1736 obj->yst_atime = buf->actime;
1737 obj->yst_mtime = buf->modtime;
1739 result = yaffs_flush_file(obj,0,0);
1740 retVal = result == YAFFS_OK ? 0 : -1;
1746 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1748 struct yaffs_obj *obj=NULL;
1749 struct yaffs_obj *dir=NULL;
1755 yaffsfs_SetError(-EFAULT);
1759 if(yaffsfs_CheckPath(path) < 0){
1760 yaffsfs_SetError(-ENAMETOOLONG);
1766 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1769 yaffsfs_SetError(-ENOTDIR);
1771 yaffsfs_SetError(-ELOOP);
1772 else if(!dir || !obj)
1773 yaffsfs_SetError(-ENOENT);
1775 retVal = yaffsfs_DoUtime(obj,buf);
1782 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1784 struct yaffs_obj *obj;
1789 obj = yaffsfs_HandleToObject(fd);
1792 retVal = yaffsfs_DoUtime(obj,buf);
1795 yaffsfs_SetError(-EBADF);
1803 #ifndef CONFIG_YAFFS_WINCE
1804 /* xattrib functions */
1807 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1808 const void *data, int size, int flags, int follow)
1810 struct yaffs_obj *obj;
1811 struct yaffs_obj *dir;
1817 if(!path || !name || !data){
1818 yaffsfs_SetError(-EFAULT);
1822 if(yaffsfs_CheckPath(path) < 0){
1823 yaffsfs_SetError(-ENAMETOOLONG);
1829 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1832 obj = yaffsfs_FollowLink(obj,0,&loop);
1835 yaffsfs_SetError(-ENOTDIR);
1837 yaffsfs_SetError(-ELOOP);
1838 else if(!dir || !obj)
1839 yaffsfs_SetError(-ENOENT);
1841 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1843 yaffsfs_SetError(retVal);
1854 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1856 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1859 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1861 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1866 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1868 struct yaffs_obj *obj;
1873 yaffsfs_SetError(-EFAULT);
1878 obj = yaffsfs_HandleToObject(fd);
1881 yaffsfs_SetError(-EBADF);
1883 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1885 yaffsfs_SetError(retVal);
1895 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1897 struct yaffs_obj *obj;
1898 struct yaffs_obj *dir;
1903 if(!path || !name || !data ){
1904 yaffsfs_SetError(-EFAULT);
1908 if(yaffsfs_CheckPath(path) < 0){
1909 yaffsfs_SetError(-ENAMETOOLONG);
1915 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1918 obj = yaffsfs_FollowLink(obj,0,&loop);
1921 yaffsfs_SetError(-ENOTDIR);
1923 yaffsfs_SetError(-ELOOP);
1924 else if(!dir || !obj)
1925 yaffsfs_SetError(-ENOENT);
1927 retVal = yaffs_get_xattrib(obj,name,data,size);
1929 yaffsfs_SetError(retVal);
1939 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1941 return yaffs_do_getxattr( path, name, data, size, 1);
1943 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1945 return yaffs_do_getxattr( path, name, data, size, 0);
1950 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1952 struct yaffs_obj *obj;
1956 if(!name || !data ){
1957 yaffsfs_SetError(-EFAULT);
1962 obj = yaffsfs_HandleToObject(fd);
1965 retVal = yaffs_get_xattrib(obj,name,data,size);
1967 yaffsfs_SetError(retVal);
1972 yaffsfs_SetError(-EBADF);
1979 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1981 struct yaffs_obj *obj=NULL;
1982 struct yaffs_obj *dir=NULL;
1987 if(!path || !data ){
1988 yaffsfs_SetError(-EFAULT);
1992 if(yaffsfs_CheckPath(path) < 0){
1993 yaffsfs_SetError(-ENAMETOOLONG);
1999 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
2002 obj = yaffsfs_FollowLink(obj,0,&loop);
2005 yaffsfs_SetError(-ENOTDIR);
2007 yaffsfs_SetError(-ELOOP);
2008 else if(!dir || !obj)
2009 yaffsfs_SetError(-ENOENT);
2011 retVal = yaffs_list_xattrib(obj, data,size);
2013 yaffsfs_SetError(retVal);
2024 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2026 return yaffs_do_listxattr(path, data, size, 1);
2029 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2031 return yaffs_do_listxattr(path, data, size, 0);
2034 int yaffs_flistxattr(int fd, char *data, int size)
2036 struct yaffs_obj *obj;
2041 yaffsfs_SetError(-EFAULT);
2046 obj = yaffsfs_HandleToObject(fd);
2049 retVal = yaffs_list_xattrib(obj,data,size);
2051 yaffsfs_SetError(retVal);
2056 yaffsfs_SetError(-EBADF);
2063 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
2065 struct yaffs_obj *obj=NULL;
2066 struct yaffs_obj *dir=NULL;
2072 yaffsfs_SetError(-EFAULT);
2076 if(yaffsfs_CheckPath(path) < 0){
2077 yaffsfs_SetError(-ENAMETOOLONG);
2083 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2086 obj = yaffsfs_FollowLink(obj,0,&loop);
2089 yaffsfs_SetError(-ENOTDIR);
2091 yaffsfs_SetError(-ELOOP);
2092 else if(!dir || !obj)
2093 yaffsfs_SetError(-ENOENT);
2095 retVal = yaffs_remove_xattrib(obj,name);
2097 yaffsfs_SetError(retVal);
2108 int yaffs_removexattr(const YCHAR *path, const char *name)
2110 return yaffs_do_removexattr(path, name, 1);
2113 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2115 return yaffs_do_removexattr(path, name, 0);
2118 int yaffs_fremovexattr(int fd, const char *name)
2120 struct yaffs_obj *obj;
2125 yaffsfs_SetError(-EFAULT);
2130 obj = yaffsfs_HandleToObject(fd);
2133 retVal = yaffs_remove_xattrib(obj,name);
2135 yaffsfs_SetError(retVal);
2140 yaffsfs_SetError(-EBADF);
2148 #ifdef CONFIG_YAFFS_WINCE
2149 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
2151 struct yaffs_obj *obj;
2156 obj = yaffsfs_HandleToObject(fd);
2161 wctime[0] = obj->win_ctime[0];
2162 wctime[1] = obj->win_ctime[1];
2165 watime[0] = obj->win_atime[0];
2166 watime[1] = obj->win_atime[1];
2169 wmtime[0] = obj->win_mtime[0];
2170 wmtime[1] = obj->win_mtime[1];
2177 yaffsfs_SetError(-EBADF);
2185 int yaffs_set_wince_times(int fd,
2186 const unsigned *wctime,
2187 const unsigned *watime,
2188 const unsigned *wmtime)
2190 struct yaffs_obj *obj;
2195 obj = yaffsfs_HandleToObject(fd);
2200 obj->win_ctime[0] = wctime[0];
2201 obj->win_ctime[1] = wctime[1];
2204 obj->win_atime[0] = watime[0];
2205 obj->win_atime[1] = watime[1];
2208 obj->win_mtime[0] = wmtime[0];
2209 obj->win_mtime[1] = wmtime[1];
2213 result = yaffs_flush_file(obj,0,0);
2217 yaffsfs_SetError(-EBADF);
2227 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2232 obj = yaffs_get_equivalent_obj(obj);
2235 obj->yst_mode = mode;
2237 result = yaffs_flush_file(obj,0,0);
2240 return result == YAFFS_OK ? 0 : -1;
2244 int yaffs_access(const YCHAR *path, int amode)
2246 struct yaffs_obj *obj=NULL;
2247 struct yaffs_obj *dir=NULL;
2253 yaffsfs_SetError(-EFAULT);
2257 if(yaffsfs_CheckPath(path) < 0){
2258 yaffsfs_SetError(-ENAMETOOLONG);
2262 if(amode & ~(R_OK | W_OK | X_OK)){
2263 yaffsfs_SetError(-EINVAL);
2269 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2270 obj = yaffsfs_FollowLink(obj,0,&loop);
2273 yaffsfs_SetError(-ENOTDIR);
2275 yaffsfs_SetError(-ELOOP);
2276 else if(!dir || !obj)
2277 yaffsfs_SetError(-ENOENT);
2278 else if((amode & W_OK) && obj->my_dev->read_only)
2279 yaffsfs_SetError(-EROFS);
2283 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2285 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2287 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2291 yaffsfs_SetError(-EACCES);
2303 int yaffs_chmod(const YCHAR *path, mode_t mode)
2305 struct yaffs_obj *obj=NULL;
2306 struct yaffs_obj *dir=NULL;
2312 yaffsfs_SetError(-EFAULT);
2316 if(yaffsfs_CheckPath(path) < 0){
2317 yaffsfs_SetError(-ENAMETOOLONG);
2322 yaffsfs_SetError(-EINVAL);
2328 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2329 obj = yaffsfs_FollowLink(obj,0,&loop);
2332 yaffsfs_SetError(-ENOTDIR);
2334 yaffsfs_SetError(-ELOOP);
2335 else if(!dir || !obj)
2336 yaffsfs_SetError(-ENOENT);
2337 else if(obj->my_dev->read_only)
2338 yaffsfs_SetError(-EROFS);
2340 retVal = yaffsfs_DoChMod(obj,mode);
2349 int yaffs_fchmod(int fd, mode_t mode)
2351 struct yaffs_obj *obj;
2355 yaffsfs_SetError(-EINVAL);
2360 obj = yaffsfs_HandleToObject(fd);
2363 yaffsfs_SetError(-EBADF);
2364 else if(obj->my_dev->read_only)
2365 yaffsfs_SetError(-EROFS);
2367 retVal = yaffsfs_DoChMod(obj,mode);
2374 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2376 struct yaffs_obj *parent = NULL;
2377 struct yaffs_obj *dir = NULL;
2379 YCHAR *alt_path = NULL;
2385 yaffsfs_SetError(-EFAULT);
2389 if(yaffsfs_CheckPath(path) < 0){
2390 yaffsfs_SetError(-ENAMETOOLONG);
2394 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2395 yaffsfs_SetError(-ENOMEM);
2402 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2403 if(!parent && notDir)
2404 yaffsfs_SetError(-ENOTDIR);
2406 yaffsfs_SetError(-ELOOP);
2408 yaffsfs_SetError(-ENOENT);
2409 else if(yaffsfs_TooManyObjects(parent->my_dev))
2410 yaffsfs_SetError(-ENFILE);
2411 else if(yaffs_strnlen(name,5) == 0){
2412 /* Trying to make the root itself */
2413 yaffsfs_SetError(-EEXIST);
2414 } else if(parent->my_dev->read_only)
2415 yaffsfs_SetError(-EROFS);
2417 dir = yaffs_create_dir(parent,name,mode,0,0);
2420 else if (yaffs_find_by_name(parent,name))
2421 yaffsfs_SetError(-EEXIST); /* the name already exists */
2423 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2434 int yaffs_rmdir(const YCHAR *path)
2440 yaffsfs_SetError(-EFAULT);
2444 if(yaffsfs_CheckPath(path) < 0){
2445 yaffsfs_SetError(-ENAMETOOLONG);
2449 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2450 yaffsfs_SetError(-ENOMEM);
2455 result = yaffsfs_DoUnlink(path,1);
2462 void * yaffs_getdev(const YCHAR *path)
2464 struct yaffs_dev *dev=NULL;
2466 dev = yaffsfs_FindDevice(path,&dummy);
2470 int yaffs_mount_common(const YCHAR *path,int read_only, int skip_checkpt)
2473 int result=YAFFS_FAIL;
2474 struct yaffs_dev *dev=NULL;
2477 yaffsfs_SetError(-EFAULT);
2481 yaffs_trace(YAFFS_TRACE_MOUNT,"yaffs: Mounting %s",path);
2483 if(yaffsfs_CheckPath(path) < 0){
2484 yaffsfs_SetError(-ENAMETOOLONG);
2490 yaffsfs_InitHandles();
2492 dev = yaffsfs_FindMountPoint(path);
2494 if(!dev->is_mounted){
2495 dev->read_only = read_only ? 1 : 0;
2497 u8 skip = dev->param.skip_checkpt_rd;
2498 dev->param.skip_checkpt_rd = 1;
2499 result = yaffs_guts_initialise(dev);
2500 dev->param.skip_checkpt_rd = skip;
2502 result = yaffs_guts_initialise(dev);
2505 if(result == YAFFS_FAIL)
2506 yaffsfs_SetError(-ENOMEM);
2507 retVal = result ? 0 : -1;
2511 yaffsfs_SetError(-EBUSY);
2513 yaffsfs_SetError(-ENODEV);
2520 int yaffs_mount2(const YCHAR *path, int readonly)
2522 return yaffs_mount_common(path, readonly, 0);
2524 int yaffs_mount(const YCHAR *path)
2526 return yaffs_mount_common(path, 0, 0);
2529 int yaffs_sync(const YCHAR *path)
2532 struct yaffs_dev *dev=NULL;
2536 yaffsfs_SetError(-EFAULT);
2540 if(yaffsfs_CheckPath(path) < 0){
2541 yaffsfs_SetError(-ENAMETOOLONG);
2546 dev = yaffsfs_FindDevice(path,&dummy);
2548 if(!dev->is_mounted)
2549 yaffsfs_SetError(-EINVAL);
2550 else if(dev->read_only)
2551 yaffsfs_SetError(-EROFS);
2554 yaffs_flush_whole_cache(dev);
2555 yaffs_checkpoint_save(dev);
2560 yaffsfs_SetError(-ENODEV);
2567 static int yaffsfs_IsDevBusy(struct yaffs_dev * dev)
2570 struct yaffs_obj *obj;
2572 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
2573 obj = yaffsfs_HandleToObject(i);
2574 if(obj && obj->my_dev == dev)
2581 int yaffs_remount(const YCHAR *path, int force, int read_only)
2584 struct yaffs_dev *dev=NULL;
2587 yaffsfs_SetError(-EFAULT);
2591 if(yaffsfs_CheckPath(path) < 0){
2592 yaffsfs_SetError(-ENAMETOOLONG);
2597 dev = yaffsfs_FindMountPoint(path);
2599 if(dev->is_mounted){
2600 yaffs_flush_whole_cache(dev);
2602 if(force || ! yaffsfs_IsDevBusy(dev)){
2604 yaffs_checkpoint_save(dev);
2605 dev->read_only = read_only ? 1 : 0;
2608 yaffsfs_SetError(-EBUSY);
2611 yaffsfs_SetError(-EINVAL);
2615 yaffsfs_SetError(-ENODEV);
2622 int yaffs_unmount2(const YCHAR *path, int force)
2625 struct yaffs_dev *dev=NULL;
2628 yaffsfs_SetError(-EFAULT);
2632 if(yaffsfs_CheckPath(path) < 0){
2633 yaffsfs_SetError(-ENAMETOOLONG);
2638 dev = yaffsfs_FindMountPoint(path);
2640 if(dev->is_mounted){
2642 yaffs_flush_whole_cache(dev);
2643 yaffs_checkpoint_save(dev);
2644 inUse = yaffsfs_IsDevBusy(dev);
2645 if(!inUse || force){
2647 yaffsfs_BreakDeviceHandles(dev);
2648 yaffs_deinitialise(dev);
2652 yaffsfs_SetError(-EBUSY);
2655 yaffsfs_SetError(-EINVAL);
2658 yaffsfs_SetError(-ENODEV);
2665 int yaffs_unmount(const YCHAR *path)
2667 return yaffs_unmount2(path,0);
2670 loff_t yaffs_freespace(const YCHAR *path)
2673 struct yaffs_dev *dev=NULL;
2677 yaffsfs_SetError(-EFAULT);
2681 if(yaffsfs_CheckPath(path) < 0){
2682 yaffsfs_SetError(-ENAMETOOLONG);
2687 dev = yaffsfs_FindDevice(path,&dummy);
2688 if(dev && dev->is_mounted){
2689 retVal = yaffs_get_n_free_chunks(dev);
2690 retVal *= dev->data_bytes_per_chunk;
2693 yaffsfs_SetError(-EINVAL);
2699 loff_t yaffs_totalspace(const YCHAR *path)
2702 struct yaffs_dev *dev=NULL;
2706 yaffsfs_SetError(-EFAULT);
2710 if(yaffsfs_CheckPath(path) < 0){
2711 yaffsfs_SetError(-ENAMETOOLONG);
2716 dev = yaffsfs_FindDevice(path,&dummy);
2717 if(dev && dev->is_mounted){
2718 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2719 dev->param.n_reserved_blocks;
2720 retVal *= dev->param.chunks_per_block;
2721 retVal *= dev->data_bytes_per_chunk;
2724 yaffsfs_SetError(-EINVAL);
2730 int yaffs_inodecount(const YCHAR *path)
2733 struct yaffs_dev *dev=NULL;
2737 yaffsfs_SetError(-EFAULT);
2741 if(yaffsfs_CheckPath(path) < 0){
2742 yaffsfs_SetError(-ENAMETOOLONG);
2747 dev = yaffsfs_FindDevice(path,&dummy);
2748 if(dev && dev->is_mounted) {
2749 int n_obj = dev->n_obj;
2750 if(n_obj > dev->n_hardlinks)
2751 retVal = n_obj - dev->n_hardlinks;
2755 yaffsfs_SetError(-EINVAL);
2762 void yaffs_add_device(struct yaffs_dev *dev)
2764 struct list_head *cfg;
2765 /* First check that the device is not in the list. */
2767 list_for_each(cfg, &yaffsfs_deviceList){
2768 if(dev == list_entry(cfg, struct yaffs_dev, dev_list))
2772 dev->is_mounted = 0;
2773 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2775 if(!dev->dev_list.next)
2776 INIT_LIST_HEAD(&dev->dev_list);
2778 list_add(&dev->dev_list,&yaffsfs_deviceList);
2781 void yaffs_remove_device(struct yaffs_dev *dev)
2783 list_del_init(&dev->dev_list);
2786 /* Functions to iterate through devices. NB Use with extreme care! */
2788 static struct list_head *dev_iterator;
2789 void yaffs_dev_rewind(void)
2791 dev_iterator = yaffsfs_deviceList.next;
2794 struct yaffs_dev *yaffs_next_dev(void)
2796 struct yaffs_dev *retval;
2800 if(dev_iterator == &yaffsfs_deviceList)
2803 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2804 dev_iterator = dev_iterator->next;
2808 /* Directory search stuff. */
2811 static struct list_head search_contexts;
2814 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2818 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2822 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2823 dsc->nextReturn = NULL;
2825 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2826 struct yaffs_obj,siblings);
2828 /* Hey someone isn't playing nice! */
2832 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2836 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2838 if( dsc->nextReturn == NULL ||
2839 list_empty(&dsc->dirObj->variant.dir_variant.children))
2840 dsc->nextReturn = NULL;
2842 struct list_head *next = dsc->nextReturn->siblings.next;
2844 if( next == &dsc->dirObj->variant.dir_variant.children)
2845 dsc->nextReturn = NULL; /* end of list */
2847 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2850 /* Hey someone isn't playing nice! */
2854 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2857 struct list_head *i;
2858 yaffsfs_DirectorySearchContext *dsc;
2860 /* if search contexts not initilised then skip */
2861 if(!search_contexts.next)
2864 /* Iterate through the directory search contexts.
2865 * If any are the one being removed, then advance the dsc to
2866 * the next one to prevent a hanging ptr.
2868 list_for_each(i, &search_contexts) {
2870 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2871 if(dsc->nextReturn == obj)
2872 yaffsfs_DirAdvance(dsc);
2878 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2880 yaffs_DIR *dir = NULL;
2881 struct yaffs_obj *obj = NULL;
2882 yaffsfs_DirectorySearchContext *dsc = NULL;
2887 yaffsfs_SetError(-EFAULT);
2891 if(yaffsfs_CheckPath(dirname) < 0){
2892 yaffsfs_SetError(-ENAMETOOLONG);
2898 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2901 yaffsfs_SetError(-ENOTDIR);
2903 yaffsfs_SetError(-ELOOP);
2905 yaffsfs_SetError(-ENOENT);
2906 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2907 yaffsfs_SetError(-ENOTDIR);
2911 for(i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
2912 if(!yaffsfs_dsc[i].inUse)
2913 dsc = & yaffsfs_dsc[i];
2916 dir = (yaffs_DIR *)dsc;
2919 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2922 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2923 INIT_LIST_HEAD(&dsc->others);
2925 if(!search_contexts.next)
2926 INIT_LIST_HEAD(&search_contexts);
2928 list_add(&dsc->others,&search_contexts);
2929 yaffsfs_SetDirRewound(dsc);
2939 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2941 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2942 struct yaffs_dirent *retVal = NULL;
2946 if(dsc && dsc->inUse){
2947 yaffsfs_SetError(0);
2948 if(dsc->nextReturn){
2949 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2950 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2951 dsc->de.d_off = dsc->offset++;
2952 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2953 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2955 /* this should not happen! */
2956 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2958 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2960 yaffsfs_DirAdvance(dsc);
2964 yaffsfs_SetError(-EBADF);
2973 void yaffs_rewinddir(yaffs_DIR *dirp)
2975 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2979 yaffsfs_SetDirRewound(dsc);
2985 int yaffs_closedir(yaffs_DIR *dirp)
2987 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2990 yaffsfs_SetError(-EFAULT);
2996 list_del(&dsc->others); /* unhook from list */
3001 /* End of directory stuff */
3004 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3006 struct yaffs_obj *parent = NULL;
3007 struct yaffs_obj *obj;
3010 int mode = 0; /* ignore for now */
3014 if(!oldpath || !newpath){
3015 yaffsfs_SetError(-EFAULT);
3019 if(yaffsfs_CheckPath(newpath) < 0 ||
3020 yaffsfs_CheckPath(oldpath) < 0){
3021 yaffsfs_SetError(-ENAMETOOLONG);
3026 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
3027 if(!parent && notDir)
3028 yaffsfs_SetError(-ENOTDIR);
3030 yaffsfs_SetError(-ELOOP);
3031 else if( !parent || yaffs_strnlen(name,5) < 1)
3032 yaffsfs_SetError(-ENOENT);
3033 else if(yaffsfs_TooManyObjects(parent->my_dev))
3034 yaffsfs_SetError(-ENFILE);
3035 else if(parent->my_dev->read_only)
3036 yaffsfs_SetError(-EROFS);
3038 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
3041 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
3042 yaffsfs_SetError(-EEXIST);
3044 yaffsfs_SetError(-ENOSPC);
3053 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3055 struct yaffs_obj *obj = NULL;
3056 struct yaffs_obj *dir = NULL;
3062 yaffsfs_SetError(-EFAULT);
3068 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
3071 yaffsfs_SetError(-ENOTDIR);
3073 yaffsfs_SetError(-ELOOP);
3074 else if(!dir || !obj)
3075 yaffsfs_SetError(-ENOENT);
3076 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3077 yaffsfs_SetError(-EINVAL);
3079 YCHAR *alias = obj->variant.symlink_variant.alias;
3080 memset(buf,0,bufsiz);
3081 yaffs_strncpy(buf,alias,bufsiz - 1);
3088 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3090 /* Creates a link called newpath to existing oldpath */
3091 struct yaffs_obj *obj = NULL;
3092 struct yaffs_obj *lnk = NULL;
3093 struct yaffs_obj *obj_dir = NULL;
3094 struct yaffs_obj *lnk_dir = NULL;
3102 if(!oldpath || !linkpath){
3103 yaffsfs_SetError(-EFAULT);
3107 if(yaffsfs_CheckPath(linkpath) < 0 ||
3108 yaffsfs_CheckPath(oldpath) < 0){
3109 yaffsfs_SetError(-ENAMETOOLONG);
3115 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
3116 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
3117 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
3119 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3120 yaffsfs_SetError(-ENOTDIR);
3121 else if(objLoop || lnkLoop)
3122 yaffsfs_SetError(-ELOOP);
3123 else if(!obj_dir || !lnk_dir || !obj)
3124 yaffsfs_SetError(-ENOENT);
3125 else if(obj->my_dev->read_only)
3126 yaffsfs_SetError(-EROFS);
3127 else if(yaffsfs_TooManyObjects(obj->my_dev))
3128 yaffsfs_SetError(-ENFILE);
3130 yaffsfs_SetError(-EEXIST);
3131 else if(lnk_dir->my_dev != obj->my_dev)
3132 yaffsfs_SetError(-EXDEV);
3134 retVal = yaffsfs_CheckNameLength(newname);
3137 lnk = yaffs_link_obj(lnk_dir,newname,obj);
3141 yaffsfs_SetError(-ENOSPC);
3151 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3157 yaffsfs_SetError(-EINVAL);
3163 * D E B U G F U N C T I O N S
3168 * Returns number of handles attached to the object
3170 int yaffs_n_handles(const YCHAR *path)
3172 struct yaffs_obj *obj;
3175 yaffsfs_SetError(-EFAULT);
3179 if(yaffsfs_CheckPath(path) < 0){
3180 yaffsfs_SetError(-ENAMETOOLONG);
3184 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
3187 return yaffsfs_CountHandles(obj);
3192 int yaffs_get_error(void)
3194 return yaffsfs_GetLastError();
3197 int yaffs_set_error(int error)
3199 yaffsfs_SetError(error);
3203 int yaffs_dump_dev(const YCHAR *path)
3210 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
3213 struct yaffs_dev *dev = obj->my_dev;
3216 "n_page_writes.......... %d\n"
3217 "n_page_reads........... %d\n"
3218 "n_erasures....... %d\n"
3219 "n_gc_copies............ %d\n"
3220 "garbageCollections... %d\n"
3221 "passiveGarbageColl'ns %d\n"
3227 dev->garbageCollections,
3228 dev->passiveGarbageCollections