2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
20 #include <string.h> /* for memset */
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
36 int symDepth, int getEquiv,
37 struct yaffs_obj **dirOut,
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 handles in yaffsfs_Handle.
49 * Things are structured this way to be like the Linux VFS model
50 * so that interactions with the yaffs guts calls are similar.
51 * That means more common code paths and less special code.
52 * That means better testing etc.
56 int count; /* Number of handles accessing this inode */
57 struct yaffs_obj *iObj;
66 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
67 int useCount:10; /* Use count for this handle */
68 u32 position; /* current position in file */
71 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
72 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
73 static int yaffsfs_handlesInitialised;
76 unsigned yaffs_set_trace(unsigned tm)
78 yaffs_trace_mask = tm;
79 return yaffs_trace_mask;
82 unsigned yaffs_get_trace(void)
84 return yaffs_trace_mask;
89 * Inilitalise handle management on start-up.
92 static void yaffsfs_InitHandles(void)
95 if(yaffsfs_handlesInitialised)
98 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
99 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
100 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
101 yaffsfs_handle[i].inodeId = -1;
104 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
106 if(h < 0 || h >= YAFFSFS_N_HANDLES)
109 return &yaffsfs_handle[h];
112 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
114 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
116 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
117 return &yaffsfs_inode[h->inodeId];
122 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
124 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
133 * yaffsfs_FindInodeIdForObject
134 * Find the inode entry for an object, if it exists.
137 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
143 obj = yaffs_get_equivalent_obj(obj);
145 /* Look for it in open inode table*/
146 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
147 if(yaffsfs_inode[i].iObj == obj)
154 * yaffsfs_GetInodeIdForObject
155 * Grab an inode entry when opening a new inode.
157 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
161 yaffsfs_Inode *in = NULL;
164 obj = yaffs_get_equivalent_obj(obj);
166 ret = yaffsfs_FindInodeIdForObject(obj);
168 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
169 if(!yaffsfs_inode[i].iObj)
174 in = &yaffsfs_inode[ret];
186 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
188 int i = yaffsfs_FindInodeIdForObject(obj);
191 return yaffsfs_inode[i].count;
196 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
198 struct yaffs_obj *obj;
205 obj->my_inode = NULL;
210 static void yaffsfs_PutInode(int inodeId)
212 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
213 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
216 yaffsfs_ReleaseInode(in);
225 * Grab a handle (when opening a file)
228 static int yaffsfs_GetNewHandle(void)
233 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
234 h = yaffsfs_GetHandlePointer(i);
236 /* todo bug: should never happen */
239 memset(h,0,sizeof(yaffsfs_Handle));
250 * Increase use of handle when reading/writing a file
252 static int yaffsfs_GetHandle(int handle)
254 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
256 if(h && h->useCount > 0){
264 * Let go of a handle when closing a file or aborting an open or
265 * ending a read or write.
267 static int yaffsfs_PutHandle(int handle)
269 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
271 if(h && h->useCount > 0){
275 yaffsfs_PutInode(h->inodeId);
283 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
286 struct yaffs_obj *obj;
288 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
289 h = yaffsfs_GetHandlePointer(i);
290 obj = yaffsfs_GetHandleObject(i);
291 if(h && h->useCount>0 && obj && obj->my_dev == dev){
293 yaffsfs_PutInode(h->inodeId);
303 * Stuff to handle names.
307 int yaffsfs_Match(YCHAR a, YCHAR b)
313 int yaffsfs_IsPathDivider(YCHAR ch)
315 const YCHAR *str = YAFFS_PATH_DIVIDERS;
326 int yaffsfs_CheckNameLength(const char *name)
330 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
333 yaffsfs_SetError(-ENOENT);
335 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
336 yaffsfs_SetError(-ENAMETOOLONG);
344 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
346 YCHAR *alt_path = NULL;
351 * We don't have a definition for max path length.
352 * We will use 3 * max name length instead.
355 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
357 /* If the last character is a path divider, then we need to
358 * trim it back so that the name look-up works properly.
359 * eg. /foo/new_dir/ -> /foo/newdir
360 * Curveball: Need to handle multiple path dividers:
361 * eg. /foof/sdfse///// -> /foo/sdfse
363 if(path_length > 0 &&
364 yaffsfs_IsPathDivider(path[path_length-1])){
365 alt_path = YMALLOC(path_length + 1);
368 strcpy(alt_path, path);
369 for(i = path_length-1;
370 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
372 alt_path[i] = (YCHAR) 0;
374 *ret_path = alt_path;
379 LIST_HEAD(yaffsfs_deviceList);
384 * Scan the configuration list to find the device
385 * Curveballs: Should match paths that end in '/' too
386 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
388 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
390 struct list_head *cfg;
391 const YCHAR *leftOver;
393 struct yaffs_dev *retval = NULL;
394 struct yaffs_dev *dev = NULL;
396 int longestMatch = -1;
400 * Check all configs, choose the one that:
401 * 1) Actually matches a prefix (ie /a amd /abc will not match
402 * 2) Matches the longest.
404 list_for_each(cfg, &yaffsfs_deviceList){
405 dev = list_entry(cfg, struct yaffs_dev, dev_list);
412 while(matching && *p && *leftOver){
413 /* Skip over any /s */
414 while(yaffsfs_IsPathDivider(*p))
417 /* Skip over any /s */
418 while(yaffsfs_IsPathDivider(*leftOver))
421 /* Now match the text part */
423 *p && !yaffsfs_IsPathDivider(*p) &&
424 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
425 if(yaffsfs_Match(*p,*leftOver)){
435 /* Skip over any /s in leftOver */
436 while(yaffsfs_IsPathDivider(*leftOver))
439 // Skip over any /s in p
440 while(yaffsfs_IsPathDivider(*p))
443 // p should now be at the end of the string (ie. fully matched)
447 if( matching && (thisMatchLength > longestMatch))
450 *restOfPath = (YCHAR *)leftOver;
452 longestMatch = thisMatchLength;
459 static int yaffsfs_CheckPath(const YCHAR *path)
463 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
464 if(yaffsfs_IsPathDivider(*path)){
472 return (*path) ? -1 : 0;
475 /* FindMountPoint only returns a dev entry if the path is a mount point */
476 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
478 struct yaffs_dev *dev;
479 YCHAR *restOfPath=NULL;
480 dev = yaffsfs_FindDevice(path,&restOfPath);
481 if(dev && restOfPath && *restOfPath)
486 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
489 struct yaffs_dev *dev;
491 dev= yaffsfs_FindDevice(path,restOfPath);
492 if(dev && dev->is_mounted){
493 return dev->root_dir;
498 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
499 int symDepth, int *loop)
503 obj = yaffs_get_equivalent_obj(obj);
505 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
506 YCHAR *alias = obj->variant.symlink_variant.alias;
508 if(yaffsfs_IsPathDivider(*alias))
509 /* Starts with a /, need to scan from root up */
510 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
513 /* Relative to here, so use the parent of the symlink as a start */
514 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
522 * yaffsfs_FindDirectory
523 * Parse a path to determine the directory and the name within the directory.
525 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
527 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
528 const YCHAR *path, YCHAR **name, int symDepth,
529 int *notDir,int *loop)
531 struct yaffs_obj *dir;
533 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
536 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
544 restOfPath = (YCHAR *)path;
547 dir = yaffsfs_FindRoot(path,&restOfPath);
553 * curve ball: also throw away surplus '/'
554 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
556 while(yaffsfs_IsPathDivider(*restOfPath))
557 restOfPath++; /* get rid of '/' */
562 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
563 if (i < YAFFS_MAX_NAME_LENGTH){
564 str[i] = *restOfPath;
572 /* got to the end of the string */
575 if(yaffs_strcmp(str,_Y(".")) == 0)
579 else if(yaffs_strcmp(str,_Y("..")) == 0)
582 dir = yaffs_find_by_name(dir,str);
584 dir = yaffsfs_FollowLink(dir,symDepth,loop);
586 if(dir && dir->variant_type !=
587 YAFFS_OBJECT_TYPE_DIRECTORY){
596 /* directory did not exist. */
600 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
607 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
611 * yaffsfs_FindObject turns a path for an existing object into the object
613 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
614 const YCHAR *path,int symDepth, int getEquiv,
615 struct yaffs_obj **dirOut, int *notDir,int *loop)
617 struct yaffs_obj *dir;
618 struct yaffs_obj *obj;
621 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
627 obj = yaffs_find_by_name(dir,name);
632 obj = yaffs_get_equivalent_obj(obj);
638 int yaffs_dup(int fd)
641 yaffsfs_Handle *oldPtr = NULL;
642 yaffsfs_Handle *newPtr = NULL;
646 oldPtr = yaffsfs_GetHandlePointer(fd);
647 if(oldPtr && oldPtr->useCount > 0)
648 newHandle = yaffsfs_GetNewHandle();
650 newPtr = yaffsfs_GetHandlePointer(newHandle);
658 yaffsfs_SetError(-EBADF);
660 yaffsfs_SetError(-ENOMEM);
668 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
670 struct yaffs_obj *obj = NULL;
671 struct yaffs_obj *dir = NULL;
674 yaffsfs_Handle *yh = NULL;
677 int errorReported = 0;
678 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
679 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
680 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
681 u8 sharedReadAllowed;
682 u8 sharedWriteAllowed;
690 if(yaffsfs_CheckPath(path) < 0){
691 yaffsfs_SetError(-ENAMETOOLONG);
695 /* O_EXCL only has meaning if O_CREAT is specified */
696 if(!(oflag & O_CREAT))
699 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
700 if( (oflag & O_CREAT) & (oflag & O_EXCL))
703 /* Todo: Are there any more flag combos to sanitise ? */
705 /* Figure out if reading or writing is requested */
707 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
708 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
712 handle = yaffsfs_GetNewHandle();
715 yaffsfs_SetError(-ENFILE);
719 yh = yaffsfs_GetHandlePointer(handle);
721 /* try to find the exisiting object */
722 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
724 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
727 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
728 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
734 /* The file already exists or it might be a directory */
736 /* If it is a directory then we can't open it as a file */
737 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
739 yaffsfs_SetError(-EISDIR);
743 /* Open should fail if O_CREAT and O_EXCL are specified since
746 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
748 yaffsfs_SetError(-EEXIST);
752 /* Check file permissions */
753 if( readRequested && !(obj->yst_mode & S_IREAD))
756 if( writeRequested && !(obj->yst_mode & S_IWRITE))
759 if(openDenied && !errorReported ) {
760 /* Error if the file exists but permissions are refused. */
761 yaffsfs_SetError(-EACCES);
765 /* Check sharing of an existing object. */
769 sharedReadAllowed = 1;
770 sharedWriteAllowed = 1;
773 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
774 hx = &yaffsfs_handle[i];
775 if(hx->useCount > 0 &&
777 yaffsfs_inode[hx->inodeId].iObj == obj){
779 sharedReadAllowed = 0;
781 sharedWriteAllowed = 0;
791 if((!sharedReadAllowed && readRequested)||
792 (!shareRead && alreadyReading) ||
793 (!sharedWriteAllowed && writeRequested) ||
794 (!shareWrite && alreadyWriting)){
796 yaffsfs_SetError(-EBUSY);
803 /* If we could not open an existing object, then let's see if
804 * the directory exists. If not, error.
806 if(!obj && !errorReported){
807 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
809 yaffsfs_SetError(-ENOTDIR);
812 yaffsfs_SetError(-ELOOP);
815 yaffsfs_SetError(-ENOENT);
820 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
821 /* Let's see if we can create this file if it does not exist. */
822 if(dir->my_dev->read_only){
823 yaffsfs_SetError(-EINVAL);
826 obj = yaffs_create_file(dir,name,mode,0,0);
828 if(!obj && !errorReported){
829 yaffsfs_SetError(-ENOSPC);
834 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
835 /* Error if the file does not exist and CREAT is not set. */
836 yaffsfs_SetError(-ENOENT);
840 if(obj && !openDenied) {
841 int inodeId = yaffsfs_GetInodeIdForObject(obj);
845 * Todo: Fix any problem if inodes run out, though that
846 * can't happen if the number of inode items >= number of handles.
850 yh->inodeId = inodeId;
851 yh->reading = readRequested;
852 yh->writing = writeRequested;
853 yh->append = (oflag & O_APPEND) ? 1 : 0;
855 yh->shareRead = shareRead;
856 yh->shareWrite = shareWrite;
858 /* Hook inode to object */
859 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
861 if((oflag & O_TRUNC) && yh->writing)
862 yaffs_resize_file(obj,0);
864 yaffsfs_PutHandle(handle);
866 yaffsfs_SetError(0); /* Problem */
876 int yaffs_open(const YCHAR *path, int oflag, int mode)
878 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
881 int yaffs_Dofsync(int fd,int datasync)
883 yaffsfs_Handle *h = NULL;
888 h = yaffsfs_GetHandlePointer(fd);
890 if(h && h->useCount > 0)
892 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
895 yaffsfs_SetError(-EBADF);
904 int yaffs_fsync(int fd)
906 return yaffs_Dofsync(fd,0);
909 int yaffs_flush(int fd)
911 return yaffs_fsync(fd);
914 int yaffs_fdatasync(int fd)
916 return yaffs_Dofsync(fd,1);
919 int yaffs_close(int fd)
921 yaffsfs_Handle *h = NULL;
926 h = yaffsfs_GetHandlePointer(fd);
928 if(h && h->useCount > 0) {
930 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
931 yaffsfs_PutHandle(fd);
935 yaffsfs_SetError(-EBADF);
946 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
948 yaffsfs_Handle *h = NULL;
949 struct yaffs_obj *obj = NULL;
956 unsigned int maxRead;
957 u8 *buf = (u8 *)vbuf;
960 h = yaffsfs_GetHandlePointer(fd);
961 obj = yaffsfs_GetHandleObject(fd);
965 yaffsfs_SetError(-EBADF);
967 } else if(!h->reading){
968 /* Not a reading handle */
969 yaffsfs_SetError(-EINVAL);
971 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
972 yaffsfs_SetError(-EINVAL);
978 startPos = h->position;
982 if(yaffs_get_obj_length(obj) > pos)
983 maxRead = yaffs_get_obj_length(obj) - pos;
991 yaffsfs_GetHandle(fd);
993 endPos = pos + nbyte;
995 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
996 nbyte > YAFFS_MAX_FILE_SIZE ||
997 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1003 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1008 * Need to reverify object in case the device was
1009 * unmounted in another thread.
1011 obj = yaffsfs_GetHandleObject(fd);
1015 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1023 if(nRead == nToRead)
1026 nbyte = 0; /* no more to read */
1036 yaffsfs_PutHandle(fd);
1040 h->position = startPos + totalRead;
1042 yaffsfs_SetError(-EINVAL);
1049 return (totalRead >= 0) ? totalRead : -1;
1053 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1055 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1058 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1060 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1063 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1065 yaffsfs_Handle *h = NULL;
1066 struct yaffs_obj *obj = NULL;
1071 int totalWritten = 0;
1072 int write_trhrough = 0;
1074 const u8 *buf = (const u8 *)vbuf;
1077 h = yaffsfs_GetHandlePointer(fd);
1078 obj = yaffsfs_GetHandleObject(fd);
1082 yaffsfs_SetError(-EBADF);
1084 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1085 yaffsfs_SetError(-EINVAL);
1089 startPos = yaffs_get_obj_length(obj);
1093 startPos = h->position;
1095 yaffsfs_GetHandle(fd);
1097 endPos = pos + nbyte;
1099 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1100 nbyte > YAFFS_MAX_FILE_SIZE ||
1101 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1108 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1109 if(nToWrite > nbyte)
1113 * Need to reverify object in case the device was
1114 * remounted or unmounted in another thread.
1116 obj = yaffsfs_GetHandleObject(fd);
1117 if(!obj || obj->my_dev->read_only)
1120 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1123 totalWritten += nWritten;
1128 if(nWritten == nToWrite)
1133 if(nWritten < 1 && totalWritten < 1){
1134 yaffsfs_SetError(-ENOSPC);
1144 yaffsfs_PutHandle(fd);
1147 if(totalWritten > 0)
1148 h->position = startPos + totalWritten;
1150 yaffsfs_SetError(-EINVAL);
1156 return (totalWritten >= 0) ? totalWritten : -1;
1159 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1161 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1164 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1166 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1170 int yaffs_truncate(const YCHAR *path,off_t new_size)
1172 struct yaffs_obj *obj = NULL;
1173 struct yaffs_obj *dir = NULL;
1174 int result = YAFFS_FAIL;
1178 if(yaffsfs_CheckPath(path) < 0){
1179 yaffsfs_SetError(-ENAMETOOLONG);
1185 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1186 obj = yaffsfs_FollowLink(obj,0,&loop);
1189 yaffsfs_SetError(-ENOTDIR);
1191 yaffsfs_SetError(-ELOOP);
1192 else if(!dir || !obj)
1193 yaffsfs_SetError(-ENOENT);
1194 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1195 yaffsfs_SetError(-EISDIR);
1196 else if(obj->my_dev->read_only)
1197 yaffsfs_SetError(-EACCES);
1198 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1199 yaffsfs_SetError(-EINVAL);
1201 result = yaffs_resize_file(obj,new_size);
1205 return (result) ? 0 : -1;
1208 int yaffs_ftruncate(int fd, off_t new_size)
1210 yaffsfs_Handle *h = NULL;
1211 struct yaffs_obj *obj = NULL;
1215 h = yaffsfs_GetHandlePointer(fd);
1216 obj = yaffsfs_GetHandleObject(fd);
1220 yaffsfs_SetError(-EBADF);
1221 else if(obj->my_dev->read_only)
1222 yaffsfs_SetError(-EACCES);
1223 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1224 yaffsfs_SetError(-EINVAL);
1226 /* resize the file */
1227 result = yaffs_resize_file(obj,new_size);
1230 return (result) ? 0 : -1;
1234 off_t yaffs_lseek(int fd, off_t offset, int whence)
1236 yaffsfs_Handle *h = NULL;
1237 struct yaffs_obj *obj = NULL;
1242 h = yaffsfs_GetHandlePointer(fd);
1243 obj = yaffsfs_GetHandleObject(fd);
1246 yaffsfs_SetError(-EBADF);
1247 else if(offset > YAFFS_MAX_FILE_SIZE)
1248 yaffsfs_SetError(-EINVAL);
1250 if(whence == SEEK_SET){
1253 } else if(whence == SEEK_CUR) {
1254 if( (h->position + offset) >= 0)
1255 pos = (h->position + offset);
1256 } else if(whence == SEEK_END) {
1257 fSize = yaffs_get_obj_length(obj);
1258 if(fSize >= 0 && (fSize + offset) >= 0)
1259 pos = fSize + offset;
1262 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1265 yaffsfs_SetError(-EINVAL);
1276 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1278 struct yaffs_obj *dir = NULL;
1279 struct yaffs_obj *obj = NULL;
1281 int result = YAFFS_FAIL;
1285 if(yaffsfs_CheckPath(path) < 0){
1286 yaffsfs_SetError(-ENAMETOOLONG);
1292 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1293 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1296 yaffsfs_SetError(-ENOTDIR);
1298 yaffsfs_SetError(-ELOOP);
1300 yaffsfs_SetError(-ENOENT);
1301 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1302 yaffsfs_SetError(-EINVAL);
1304 yaffsfs_SetError(-ENOENT);
1305 else if(obj->my_dev->read_only)
1306 yaffsfs_SetError(-EINVAL);
1307 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1308 yaffsfs_SetError(-EISDIR);
1309 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1310 yaffsfs_SetError(-ENOTDIR);
1311 else if(isDirectory && obj == obj->my_dev->root_dir)
1312 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1314 result = yaffs_unlinker(dir,name);
1316 if(result == YAFFS_FAIL && isDirectory)
1317 yaffsfs_SetError(-ENOTEMPTY);
1322 return (result == YAFFS_FAIL) ? -1 : 0;
1326 int yaffs_unlink(const YCHAR *path)
1328 return yaffsfs_DoUnlink(path,0);
1331 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1333 struct yaffs_obj *olddir = NULL;
1334 struct yaffs_obj *newdir = NULL;
1335 struct yaffs_obj *obj = NULL;
1336 struct yaffs_obj *newobj = NULL;
1339 int result= YAFFS_FAIL;
1340 int rename_allowed = 1;
1346 YCHAR *alt_newpath=NULL;
1348 if(yaffsfs_CheckPath(newPath) < 0){
1349 yaffsfs_SetError(-ENAMETOOLONG);
1353 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1354 yaffsfs_SetError(-ENOMEM);
1358 newPath = alt_newpath;
1363 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1364 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1365 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1366 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1368 /* If the object being renamed is a directory and the
1369 * path ended with a "/" then the olddir == obj.
1370 * We pass through NULL for the old name to tell the lower layers
1371 * to use olddir as the object.
1377 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1378 yaffsfs_SetError(-ENOTDIR);
1380 } else if(oldLoop || newLoop) {
1381 yaffsfs_SetError(-ELOOP);
1383 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1384 yaffsfs_SetError(-EINVAL);
1386 }else if(!olddir || !newdir || !obj) {
1387 yaffsfs_SetError(-ENOENT);
1389 } else if(obj->my_dev->read_only){
1390 yaffsfs_SetError(-EROFS);
1392 } else if(yaffs_is_non_empty_dir(newobj)){
1393 yaffsfs_SetError(-ENOTEMPTY);
1395 } else if(olddir->my_dev != newdir->my_dev) {
1396 /* Rename must be on same device */
1397 yaffsfs_SetError(-EXDEV);
1399 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1401 * It is a directory, check that it is not being renamed to
1402 * being its own decendent.
1403 * Do this by tracing from the new directory back to the root,
1407 struct yaffs_obj *xx = newdir;
1409 while( rename_allowed && xx){
1415 yaffsfs_SetError(-EINVAL);
1419 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1426 return (result == YAFFS_FAIL) ? -1 : 0;
1430 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1434 obj = yaffs_get_equivalent_obj(obj);
1437 buf->st_dev = (int)obj->my_dev->os_context;
1438 buf->st_ino = obj->obj_id;
1439 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1441 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1442 buf->st_mode |= S_IFDIR;
1443 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1444 buf->st_mode |= S_IFLNK;
1445 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1446 buf->st_mode |= S_IFREG;
1448 buf->st_nlink = yaffs_get_obj_link_count(obj);
1451 buf->st_rdev = obj->yst_rdev;
1452 buf->st_size = yaffs_get_obj_length(obj);
1453 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1454 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1455 #if CONFIG_YAFFS_WINCE
1456 buf->yst_wince_atime[0] = obj->win_atime[0];
1457 buf->yst_wince_atime[1] = obj->win_atime[1];
1458 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1459 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1460 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1461 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1463 buf->yst_atime = obj->yst_atime;
1464 buf->yst_ctime = obj->yst_ctime;
1465 buf->yst_mtime = obj->yst_mtime;
1472 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1474 struct yaffs_obj *obj=NULL;
1475 struct yaffs_obj *dir=NULL;
1480 if(yaffsfs_CheckPath(path) < 0){
1481 yaffsfs_SetError(-ENAMETOOLONG);
1487 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1490 obj = yaffsfs_FollowLink(obj,0,&loop);
1493 yaffsfs_SetError(-ENOTDIR);
1495 yaffsfs_SetError(-ELOOP);
1496 else if(!dir || !obj)
1497 yaffsfs_SetError(-ENOENT);
1499 retVal = yaffsfs_DoStat(obj,buf);
1507 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1509 return yaffsfs_DoStatOrLStat(path,buf,0);
1512 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1514 return yaffsfs_DoStatOrLStat(path,buf,1);
1517 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1519 struct yaffs_obj *obj;
1524 obj = yaffsfs_GetHandleObject(fd);
1527 retVal = yaffsfs_DoStat(obj,buf);
1530 yaffsfs_SetError(-EBADF);
1537 #ifndef CONFIG_YAFFS_WINCE
1538 /* xattrib functions */
1541 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1543 struct yaffs_obj *obj;
1544 struct yaffs_obj *dir;
1550 if(yaffsfs_CheckPath(path) < 0){
1551 yaffsfs_SetError(-ENAMETOOLONG);
1557 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1560 obj = yaffsfs_FollowLink(obj,0,&loop);
1563 yaffsfs_SetError(-ENOTDIR);
1565 yaffsfs_SetError(-ELOOP);
1566 else if(!dir || !obj)
1567 yaffsfs_SetError(-ENOENT);
1569 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1571 yaffsfs_SetError(retVal);
1582 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1584 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1587 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1589 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1594 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1596 struct yaffs_obj *obj;
1601 obj = yaffsfs_GetHandleObject(fd);
1604 yaffsfs_SetError(-EBADF);
1606 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1608 yaffsfs_SetError(retVal);
1618 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1620 struct yaffs_obj *obj;
1621 struct yaffs_obj *dir;
1626 if(yaffsfs_CheckPath(path) < 0){
1627 yaffsfs_SetError(-ENAMETOOLONG);
1633 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1636 obj = yaffsfs_FollowLink(obj,0,&loop);
1639 yaffsfs_SetError(-ENOTDIR);
1641 yaffsfs_SetError(-ELOOP);
1642 else if(!dir || !obj)
1643 yaffsfs_SetError(-ENOENT);
1645 retVal = yaffs_get_xattrib(obj,name,data,size);
1647 yaffsfs_SetError(retVal);
1657 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1659 return yaffs_do_getxattr( path, name, data, size, 1);
1661 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1663 return yaffs_do_getxattr( path, name, data, size, 0);
1668 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1670 struct yaffs_obj *obj;
1675 obj = yaffsfs_GetHandleObject(fd);
1678 retVal = yaffs_get_xattrib(obj,name,data,size);
1680 yaffsfs_SetError(retVal);
1685 yaffsfs_SetError(-EBADF);
1692 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1694 struct yaffs_obj *obj=NULL;
1695 struct yaffs_obj *dir=NULL;
1700 if(yaffsfs_CheckPath(path) < 0){
1701 yaffsfs_SetError(-ENAMETOOLONG);
1707 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1710 obj = yaffsfs_FollowLink(obj,0,&loop);
1713 yaffsfs_SetError(-ENOTDIR);
1715 yaffsfs_SetError(-ELOOP);
1716 else if(!dir || !obj)
1717 yaffsfs_SetError(-ENOENT);
1719 retVal = yaffs_list_xattrib(obj, data,size);
1721 yaffsfs_SetError(retVal);
1732 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1734 return yaffs_do_listxattr(path, data, size, 1);
1737 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1739 return yaffs_do_listxattr(path, data, size, 0);
1742 int yaffs_flistxattr(int fd, char *data, int size)
1744 struct yaffs_obj *obj;
1749 obj = yaffsfs_GetHandleObject(fd);
1752 retVal = yaffs_list_xattrib(obj,data,size);
1754 yaffsfs_SetError(retVal);
1759 yaffsfs_SetError(-EBADF);
1766 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1768 struct yaffs_obj *obj=NULL;
1769 struct yaffs_obj *dir=NULL;
1774 if(yaffsfs_CheckPath(path) < 0){
1775 yaffsfs_SetError(-ENAMETOOLONG);
1781 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1784 obj = yaffsfs_FollowLink(obj,0,&loop);
1787 yaffsfs_SetError(-ENOTDIR);
1789 yaffsfs_SetError(-ELOOP);
1790 else if(!dir || !obj)
1791 yaffsfs_SetError(-ENOENT);
1793 retVal = yaffs_remove_xattrib(obj,name);
1795 yaffsfs_SetError(retVal);
1806 int yaffs_removexattr(const YCHAR *path, const char *name)
1808 return yaffs_do_removexattr(path, name, 1);
1811 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1813 return yaffs_do_removexattr(path, name, 0);
1816 int yaffs_fremovexattr(int fd, const char *name)
1818 struct yaffs_obj *obj;
1823 obj = yaffsfs_GetHandleObject(fd);
1826 retVal = yaffs_remove_xattrib(obj,name);
1828 yaffsfs_SetError(retVal);
1833 yaffsfs_SetError(-EBADF);
1841 #ifdef CONFIG_YAFFS_WINCE
1842 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1844 struct yaffs_obj *obj;
1849 obj = yaffsfs_GetHandleObject(fd);
1854 wctime[0] = obj->win_ctime[0];
1855 wctime[1] = obj->win_ctime[1];
1858 watime[0] = obj->win_atime[0];
1859 watime[1] = obj->win_atime[1];
1862 wmtime[0] = obj->win_mtime[0];
1863 wmtime[1] = obj->win_mtime[1];
1870 yaffsfs_SetError(-EBADF);
1878 int yaffs_set_wince_times(int fd,
1879 const unsigned *wctime,
1880 const unsigned *watime,
1881 const unsigned *wmtime)
1883 struct yaffs_obj *obj;
1888 obj = yaffsfs_GetHandleObject(fd);
1893 obj->win_ctime[0] = wctime[0];
1894 obj->win_ctime[1] = wctime[1];
1897 obj->win_atime[0] = watime[0];
1898 obj->win_atime[1] = watime[1];
1901 obj->win_mtime[0] = wmtime[0];
1902 obj->win_mtime[1] = wmtime[1];
1906 result = yaffs_flush_file(obj,0,0);
1910 yaffsfs_SetError(-EBADF);
1920 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1925 obj = yaffs_get_equivalent_obj(obj);
1928 obj->yst_mode = mode;
1930 result = yaffs_flush_file(obj,0,0);
1933 return result == YAFFS_OK ? 0 : -1;
1937 int yaffs_access(const YCHAR *path, int amode)
1939 struct yaffs_obj *obj=NULL;
1940 struct yaffs_obj *dir=NULL;
1945 if(yaffsfs_CheckPath(path) < 0){
1946 yaffsfs_SetError(-ENAMETOOLONG);
1950 if(amode & ~(R_OK | W_OK | X_OK)){
1951 yaffsfs_SetError(-EINVAL);
1957 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1958 obj = yaffsfs_FollowLink(obj,0,&loop);
1961 yaffsfs_SetError(-ENOTDIR);
1963 yaffsfs_SetError(-ELOOP);
1964 else if(!dir || !obj)
1965 yaffsfs_SetError(-ENOENT);
1966 else if((amode & W_OK) && obj->my_dev->read_only)
1967 yaffsfs_SetError(-EROFS);
1971 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1973 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1975 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1979 yaffsfs_SetError(-EACCES);
1991 int yaffs_chmod(const YCHAR *path, mode_t mode)
1993 struct yaffs_obj *obj=NULL;
1994 struct yaffs_obj *dir=NULL;
1999 if(yaffsfs_CheckPath(path) < 0){
2000 yaffsfs_SetError(-ENAMETOOLONG);
2005 yaffsfs_SetError(-EINVAL);
2011 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2012 obj = yaffsfs_FollowLink(obj,0,&loop);
2015 yaffsfs_SetError(-ENOTDIR);
2017 yaffsfs_SetError(-ELOOP);
2018 else if(!dir || !obj)
2019 yaffsfs_SetError(-ENOENT);
2020 else if(obj->my_dev->read_only)
2021 yaffsfs_SetError(-EROFS);
2023 retVal = yaffsfs_DoChMod(obj,mode);
2032 int yaffs_fchmod(int fd, mode_t mode)
2034 struct yaffs_obj *obj;
2038 yaffsfs_SetError(-EINVAL);
2043 obj = yaffsfs_GetHandleObject(fd);
2046 yaffsfs_SetError(-EBADF);
2047 else if(obj->my_dev->read_only)
2048 yaffsfs_SetError(-EROFS);
2050 retVal = yaffsfs_DoChMod(obj,mode);
2057 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2059 struct yaffs_obj *parent = NULL;
2060 struct yaffs_obj *dir = NULL;
2062 YCHAR *alt_path = NULL;
2067 if(yaffsfs_CheckPath(path) < 0){
2068 yaffsfs_SetError(-ENAMETOOLONG);
2072 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2073 yaffsfs_SetError(-ENOMEM);
2080 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2081 if(!parent && notDir)
2082 yaffsfs_SetError(-ENOTDIR);
2084 yaffsfs_SetError(-ELOOP);
2086 yaffsfs_SetError(-ENOENT);
2087 else if(yaffs_strnlen(name,5) == 0){
2088 /* Trying to make the root itself */
2089 yaffsfs_SetError(-EEXIST);
2090 } else if(parent->my_dev->read_only)
2091 yaffsfs_SetError(-EROFS);
2093 dir = yaffs_create_dir(parent,name,mode,0,0);
2096 else if (yaffs_find_by_name(parent,name))
2097 yaffsfs_SetError(-EEXIST); /* the name already exists */
2099 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2110 int yaffs_rmdir(const YCHAR *path)
2115 if(yaffsfs_CheckPath(path) < 0){
2116 yaffsfs_SetError(-ENAMETOOLONG);
2120 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2121 yaffsfs_SetError(-ENOMEM);
2126 result = yaffsfs_DoUnlink(path,1);
2133 void * yaffs_getdev(const YCHAR *path)
2135 struct yaffs_dev *dev=NULL;
2137 dev = yaffsfs_FindDevice(path,&dummy);
2141 int yaffs_mount2(const YCHAR *path,int read_only)
2144 int result=YAFFS_FAIL;
2145 struct yaffs_dev *dev=NULL;
2147 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2149 if(yaffsfs_CheckPath(path) < 0){
2150 yaffsfs_SetError(-ENAMETOOLONG);
2156 yaffsfs_InitHandles();
2158 dev = yaffsfs_FindMountPoint(path);
2160 if(!dev->is_mounted){
2161 dev->read_only = read_only ? 1 : 0;
2162 result = yaffs_guts_initialise(dev);
2163 if(result == YAFFS_FAIL)
2164 yaffsfs_SetError(-ENOMEM);
2165 retVal = result ? 0 : -1;
2169 yaffsfs_SetError(-EBUSY);
2171 yaffsfs_SetError(-ENODEV);
2178 int yaffs_mount(const YCHAR *path)
2180 return yaffs_mount2(path,0);
2183 int yaffs_sync(const YCHAR *path)
2186 struct yaffs_dev *dev=NULL;
2189 if(yaffsfs_CheckPath(path) < 0){
2190 yaffsfs_SetError(-ENAMETOOLONG);
2195 dev = yaffsfs_FindDevice(path,&dummy);
2197 if(dev->is_mounted){
2199 yaffs_flush_whole_cache(dev);
2200 yaffs_checkpoint_save(dev);
2204 yaffsfs_SetError(-EINVAL);
2207 yaffsfs_SetError(-ENODEV);
2214 int yaffs_remount(const YCHAR *path, int force, int read_only)
2217 struct yaffs_dev *dev=NULL;
2220 if(yaffsfs_CheckPath(path) < 0){
2221 yaffsfs_SetError(-ENAMETOOLONG);
2226 dev = yaffsfs_FindMountPoint(path);
2228 if(dev->is_mounted){
2232 yaffs_flush_whole_cache(dev);
2234 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2235 yh = & yaffsfs_handle[i];
2236 if(yh->useCount>0 &&
2237 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2238 inUse = 1; /* the device is in use, can't unmount */
2241 if(!inUse || force){
2243 yaffs_checkpoint_save(dev);
2244 dev->read_only = read_only ? 1 : 0;
2247 yaffsfs_SetError(-EBUSY);
2250 yaffsfs_SetError(-EINVAL);
2254 yaffsfs_SetError(-ENODEV);
2261 int yaffs_unmount2(const YCHAR *path, int force)
2264 struct yaffs_dev *dev=NULL;
2266 if(yaffsfs_CheckPath(path) < 0){
2267 yaffsfs_SetError(-ENAMETOOLONG);
2272 dev = yaffsfs_FindMountPoint(path);
2274 if(dev->is_mounted){
2278 yaffs_flush_whole_cache(dev);
2279 yaffs_checkpoint_save(dev);
2281 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2282 if(yaffsfs_handle[i].useCount > 0 &&
2283 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2284 inUse = 1; /* the device is in use, can't unmount */
2287 if(!inUse || force){
2289 yaffsfs_BreakDeviceHandles(dev);
2290 yaffs_deinitialise(dev);
2294 yaffsfs_SetError(-EBUSY);
2297 yaffsfs_SetError(-EINVAL);
2300 yaffsfs_SetError(-ENODEV);
2307 int yaffs_unmount(const YCHAR *path)
2309 return yaffs_unmount2(path,0);
2312 loff_t yaffs_freespace(const YCHAR *path)
2315 struct yaffs_dev *dev=NULL;
2318 if(yaffsfs_CheckPath(path) < 0){
2319 yaffsfs_SetError(-ENAMETOOLONG);
2324 dev = yaffsfs_FindDevice(path,&dummy);
2325 if(dev && dev->is_mounted){
2326 retVal = yaffs_get_n_free_chunks(dev);
2327 retVal *= dev->data_bytes_per_chunk;
2330 yaffsfs_SetError(-EINVAL);
2336 loff_t yaffs_totalspace(const YCHAR *path)
2339 struct yaffs_dev *dev=NULL;
2342 if(yaffsfs_CheckPath(path) < 0){
2343 yaffsfs_SetError(-ENAMETOOLONG);
2348 dev = yaffsfs_FindDevice(path,&dummy);
2349 if(dev && dev->is_mounted){
2350 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2351 dev->param.n_reserved_blocks;
2352 retVal *= dev->param.chunks_per_block;
2353 retVal *= dev->data_bytes_per_chunk;
2356 yaffsfs_SetError(-EINVAL);
2362 int yaffs_inodecount(const YCHAR *path)
2365 struct yaffs_dev *dev=NULL;
2368 if(yaffsfs_CheckPath(path) < 0){
2369 yaffsfs_SetError(-ENAMETOOLONG);
2374 dev = yaffsfs_FindDevice(path,&dummy);
2375 if(dev && dev->is_mounted) {
2376 int n_obj = dev->n_obj;
2377 if(n_obj > dev->n_hardlinks)
2378 retVal = n_obj - dev->n_hardlinks;
2382 yaffsfs_SetError(-EINVAL);
2389 void yaffs_add_device(struct yaffs_dev *dev)
2391 dev->is_mounted = 0;
2392 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2394 if(!dev->dev_list.next)
2395 INIT_LIST_HEAD(&dev->dev_list);
2397 list_add(&dev->dev_list,&yaffsfs_deviceList);
2400 void yaffs_remove_device(struct yaffs_dev *dev)
2402 list_del_init(&dev->dev_list);
2408 /* Directory search stuff. */
2411 * Directory search context
2413 * NB this is an opaque structure.
2420 yaffs_dirent de; /* directory entry being used by this dsc */
2421 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2422 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2423 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2425 struct list_head others;
2426 } yaffsfs_DirectorySearchContext;
2430 static struct list_head search_contexts;
2433 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2437 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2441 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2442 dsc->nextReturn = NULL;
2444 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2445 struct yaffs_obj,siblings);
2447 /* Hey someone isn't playing nice! */
2451 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2455 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2457 if( dsc->nextReturn == NULL ||
2458 list_empty(&dsc->dirObj->variant.dir_variant.children))
2459 dsc->nextReturn = NULL;
2461 struct list_head *next = dsc->nextReturn->siblings.next;
2463 if( next == &dsc->dirObj->variant.dir_variant.children)
2464 dsc->nextReturn = NULL; /* end of list */
2466 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2469 /* Hey someone isn't playing nice! */
2473 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2476 struct list_head *i;
2477 yaffsfs_DirectorySearchContext *dsc;
2479 /* if search contexts not initilised then skip */
2480 if(!search_contexts.next)
2483 /* Iterate through the directory search contexts.
2484 * If any are the one being removed, then advance the dsc to
2485 * the next one to prevent a hanging ptr.
2487 list_for_each(i, &search_contexts) {
2489 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2490 if(dsc->nextReturn == obj)
2491 yaffsfs_DirAdvance(dsc);
2497 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2499 yaffs_DIR *dir = NULL;
2500 struct yaffs_obj *obj = NULL;
2501 yaffsfs_DirectorySearchContext *dsc = NULL;
2505 if(yaffsfs_CheckPath(dirname) < 0){
2506 yaffsfs_SetError(-ENAMETOOLONG);
2512 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2515 yaffsfs_SetError(-ENOTDIR);
2517 yaffsfs_SetError(-ELOOP);
2519 yaffsfs_SetError(-ENOENT);
2520 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2521 yaffsfs_SetError(-ENOTDIR);
2524 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2525 dir = (yaffs_DIR *)dsc;
2528 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2529 dsc->magic = YAFFS_MAGIC;
2531 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2532 INIT_LIST_HEAD(&dsc->others);
2534 if(!search_contexts.next)
2535 INIT_LIST_HEAD(&search_contexts);
2537 list_add(&dsc->others,&search_contexts);
2538 yaffsfs_SetDirRewound(dsc);
2548 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2550 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2551 struct yaffs_dirent *retVal = NULL;
2555 if(dsc && dsc->magic == YAFFS_MAGIC){
2556 yaffsfs_SetError(0);
2557 if(dsc->nextReturn){
2558 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2559 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2560 dsc->de.d_off = dsc->offset++;
2561 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2562 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2564 /* this should not happen! */
2565 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2567 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2569 yaffsfs_DirAdvance(dsc);
2573 yaffsfs_SetError(-EBADF);
2582 void yaffs_rewinddir(yaffs_DIR *dirp)
2584 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2588 yaffsfs_SetDirRewound(dsc);
2594 int yaffs_closedir(yaffs_DIR *dirp)
2596 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2600 list_del(&dsc->others); /* unhook from list */
2606 /* End of directory stuff */
2609 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2611 struct yaffs_obj *parent = NULL;
2612 struct yaffs_obj *obj;
2615 int mode = 0; /* ignore for now */
2619 if(yaffsfs_CheckPath(newpath) < 0){
2620 yaffsfs_SetError(-ENAMETOOLONG);
2624 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2625 if(!parent && notDir)
2626 yaffsfs_SetError(-ENOTDIR);
2628 yaffsfs_SetError(-ELOOP);
2629 else if( !parent || yaffs_strnlen(name,5) < 1)
2630 yaffsfs_SetError(-ENOENT);
2631 else if(parent->my_dev->read_only)
2632 yaffsfs_SetError(-EROFS);
2634 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2637 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2638 yaffsfs_SetError(-EEXIST);
2640 yaffsfs_SetError(-ENOSPC);
2649 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2651 struct yaffs_obj *obj = NULL;
2652 struct yaffs_obj *dir = NULL;
2659 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2662 yaffsfs_SetError(-ENOTDIR);
2664 yaffsfs_SetError(-ELOOP);
2665 else if(!dir || !obj)
2666 yaffsfs_SetError(-ENOENT);
2667 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2668 yaffsfs_SetError(-EINVAL);
2670 YCHAR *alias = obj->variant.symlink_variant.alias;
2671 memset(buf,0,bufsiz);
2672 yaffs_strncpy(buf,alias,bufsiz - 1);
2679 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2681 /* Creates a link called newpath to existing oldpath */
2682 struct yaffs_obj *obj = NULL;
2683 struct yaffs_obj *lnk = NULL;
2684 struct yaffs_obj *obj_dir = NULL;
2685 struct yaffs_obj *lnk_dir = NULL;
2693 if(yaffsfs_CheckPath(linkpath) < 0){
2694 yaffsfs_SetError(-ENAMETOOLONG);
2700 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
2701 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2702 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
2704 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2705 yaffsfs_SetError(-ENOTDIR);
2706 else if(objLoop || lnkLoop)
2707 yaffsfs_SetError(-ELOOP);
2708 else if(!obj_dir || !lnk_dir || !obj)
2709 yaffsfs_SetError(-ENOENT);
2710 else if(obj->my_dev->read_only)
2711 yaffsfs_SetError(-EINVAL);
2713 yaffsfs_SetError(-EEXIST);
2714 else if(lnk_dir->my_dev != obj->my_dev)
2715 yaffsfs_SetError(-EXDEV);
2717 retVal = yaffsfs_CheckNameLength(newname);
2720 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2724 yaffsfs_SetError(-ENOSPC);
2734 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2740 yaffsfs_SetError(-EINVAL);
2746 * D E B U G F U N C T I O N S
2751 * Returns number of handles attached to the object
2753 int yaffs_n_handles(const YCHAR *path)
2755 struct yaffs_obj *obj;
2757 if(yaffsfs_CheckPath(path) < 0){
2758 yaffsfs_SetError(-ENAMETOOLONG);
2762 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2765 return yaffsfs_CountHandles(obj);
2770 int yaffs_get_error(void)
2772 return yaffsfs_GetLastError();
2775 int yaffs_set_error(int error)
2777 yaffsfs_SetError(error);
2781 int yaffs_dump_dev(const YCHAR *path)
2788 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2791 struct yaffs_dev *dev = obj->my_dev;
2794 "n_page_writes.......... %d\n"
2795 "n_page_reads........... %d\n"
2796 "n_erasures....... %d\n"
2797 "n_gc_copies............ %d\n"
2798 "garbageCollections... %d\n"
2799 "passiveGarbageColl'ns %d\n"
2805 dev->garbageCollections,
2806 dev->passiveGarbageCollections