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 < 1)
891 yaffsfs_SetError(-EBADF);
893 struct yaffs_obj * obj;
894 obj = yaffsfs_inode[h->inodeId].iObj;
895 if(obj->my_dev->read_only)
896 yaffsfs_SetError(-EROFS);
898 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
908 int yaffs_fsync(int fd)
910 return yaffs_Dofsync(fd,0);
913 int yaffs_flush(int fd)
915 return yaffs_fsync(fd);
918 int yaffs_fdatasync(int fd)
920 return yaffs_Dofsync(fd,1);
923 int yaffs_close(int fd)
925 yaffsfs_Handle *h = NULL;
930 h = yaffsfs_GetHandlePointer(fd);
932 if(h && h->useCount > 0) {
934 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
935 yaffsfs_PutHandle(fd);
939 yaffsfs_SetError(-EBADF);
950 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
952 yaffsfs_Handle *h = NULL;
953 struct yaffs_obj *obj = NULL;
960 unsigned int maxRead;
961 u8 *buf = (u8 *)vbuf;
964 h = yaffsfs_GetHandlePointer(fd);
965 obj = yaffsfs_GetHandleObject(fd);
969 yaffsfs_SetError(-EBADF);
971 } else if(!h->reading){
972 /* Not a reading handle */
973 yaffsfs_SetError(-EINVAL);
975 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
976 yaffsfs_SetError(-EINVAL);
982 startPos = h->position;
986 if(yaffs_get_obj_length(obj) > pos)
987 maxRead = yaffs_get_obj_length(obj) - pos;
995 yaffsfs_GetHandle(fd);
997 endPos = pos + nbyte;
999 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1000 nbyte > YAFFS_MAX_FILE_SIZE ||
1001 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1007 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1012 * Need to reverify object in case the device was
1013 * unmounted in another thread.
1015 obj = yaffsfs_GetHandleObject(fd);
1019 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1027 if(nRead == nToRead)
1030 nbyte = 0; /* no more to read */
1040 yaffsfs_PutHandle(fd);
1044 h->position = startPos + totalRead;
1046 yaffsfs_SetError(-EINVAL);
1053 return (totalRead >= 0) ? totalRead : -1;
1057 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1059 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1062 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1064 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1067 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1069 yaffsfs_Handle *h = NULL;
1070 struct yaffs_obj *obj = NULL;
1075 int totalWritten = 0;
1076 int write_trhrough = 0;
1078 const u8 *buf = (const u8 *)vbuf;
1081 h = yaffsfs_GetHandlePointer(fd);
1082 obj = yaffsfs_GetHandleObject(fd);
1086 yaffsfs_SetError(-EBADF);
1088 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1089 yaffsfs_SetError(-EINVAL);
1093 startPos = yaffs_get_obj_length(obj);
1097 startPos = h->position;
1099 yaffsfs_GetHandle(fd);
1101 endPos = pos + nbyte;
1103 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1104 nbyte > YAFFS_MAX_FILE_SIZE ||
1105 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1112 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1113 if(nToWrite > nbyte)
1117 * Need to reverify object in case the device was
1118 * remounted or unmounted in another thread.
1120 obj = yaffsfs_GetHandleObject(fd);
1121 if(!obj || obj->my_dev->read_only)
1124 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1127 totalWritten += nWritten;
1132 if(nWritten == nToWrite)
1137 if(nWritten < 1 && totalWritten < 1){
1138 yaffsfs_SetError(-ENOSPC);
1148 yaffsfs_PutHandle(fd);
1151 if(totalWritten > 0)
1152 h->position = startPos + totalWritten;
1154 yaffsfs_SetError(-EINVAL);
1160 return (totalWritten >= 0) ? totalWritten : -1;
1163 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1165 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1168 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1170 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1174 int yaffs_truncate(const YCHAR *path,off_t new_size)
1176 struct yaffs_obj *obj = NULL;
1177 struct yaffs_obj *dir = NULL;
1178 int result = YAFFS_FAIL;
1182 if(yaffsfs_CheckPath(path) < 0){
1183 yaffsfs_SetError(-ENAMETOOLONG);
1189 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1190 obj = yaffsfs_FollowLink(obj,0,&loop);
1193 yaffsfs_SetError(-ENOTDIR);
1195 yaffsfs_SetError(-ELOOP);
1196 else if(!dir || !obj)
1197 yaffsfs_SetError(-ENOENT);
1198 else if(obj->my_dev->read_only)
1199 yaffsfs_SetError(-EROFS);
1200 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1201 yaffsfs_SetError(-EISDIR);
1202 else if(obj->my_dev->read_only)
1203 yaffsfs_SetError(-EACCES);
1204 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1205 yaffsfs_SetError(-EINVAL);
1207 result = yaffs_resize_file(obj,new_size);
1211 return (result) ? 0 : -1;
1214 int yaffs_ftruncate(int fd, off_t new_size)
1216 yaffsfs_Handle *h = NULL;
1217 struct yaffs_obj *obj = NULL;
1221 h = yaffsfs_GetHandlePointer(fd);
1222 obj = yaffsfs_GetHandleObject(fd);
1226 yaffsfs_SetError(-EBADF);
1227 else if(obj->my_dev->read_only)
1228 yaffsfs_SetError(-EROFS);
1229 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1230 yaffsfs_SetError(-EINVAL);
1232 /* resize the file */
1233 result = yaffs_resize_file(obj,new_size);
1236 return (result) ? 0 : -1;
1240 off_t yaffs_lseek(int fd, off_t offset, int whence)
1242 yaffsfs_Handle *h = NULL;
1243 struct yaffs_obj *obj = NULL;
1248 h = yaffsfs_GetHandlePointer(fd);
1249 obj = yaffsfs_GetHandleObject(fd);
1252 yaffsfs_SetError(-EBADF);
1253 else if(offset > YAFFS_MAX_FILE_SIZE)
1254 yaffsfs_SetError(-EINVAL);
1256 if(whence == SEEK_SET){
1259 } else if(whence == SEEK_CUR) {
1260 if( (h->position + offset) >= 0)
1261 pos = (h->position + offset);
1262 } else if(whence == SEEK_END) {
1263 fSize = yaffs_get_obj_length(obj);
1264 if(fSize >= 0 && (fSize + offset) >= 0)
1265 pos = fSize + offset;
1268 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1271 yaffsfs_SetError(-EINVAL);
1282 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1284 struct yaffs_obj *dir = NULL;
1285 struct yaffs_obj *obj = NULL;
1287 int result = YAFFS_FAIL;
1291 if(yaffsfs_CheckPath(path) < 0){
1292 yaffsfs_SetError(-ENAMETOOLONG);
1298 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1299 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1302 yaffsfs_SetError(-ENOTDIR);
1304 yaffsfs_SetError(-ELOOP);
1306 yaffsfs_SetError(-ENOENT);
1307 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1308 yaffsfs_SetError(-EINVAL);
1310 yaffsfs_SetError(-ENOENT);
1311 else if(obj->my_dev->read_only)
1312 yaffsfs_SetError(-EINVAL);
1313 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1314 yaffsfs_SetError(-EISDIR);
1315 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1316 yaffsfs_SetError(-ENOTDIR);
1317 else if(isDirectory && obj == obj->my_dev->root_dir)
1318 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1320 result = yaffs_unlinker(dir,name);
1322 if(result == YAFFS_FAIL && isDirectory)
1323 yaffsfs_SetError(-ENOTEMPTY);
1328 return (result == YAFFS_FAIL) ? -1 : 0;
1332 int yaffs_unlink(const YCHAR *path)
1334 return yaffsfs_DoUnlink(path,0);
1337 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1339 struct yaffs_obj *olddir = NULL;
1340 struct yaffs_obj *newdir = NULL;
1341 struct yaffs_obj *obj = NULL;
1342 struct yaffs_obj *newobj = NULL;
1345 int result= YAFFS_FAIL;
1346 int rename_allowed = 1;
1352 YCHAR *alt_newpath=NULL;
1354 if(yaffsfs_CheckPath(newPath) < 0){
1355 yaffsfs_SetError(-ENAMETOOLONG);
1359 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1360 yaffsfs_SetError(-ENOMEM);
1364 newPath = alt_newpath;
1369 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1370 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1371 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1372 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1374 /* If the object being renamed is a directory and the
1375 * path ended with a "/" then the olddir == obj.
1376 * We pass through NULL for the old name to tell the lower layers
1377 * to use olddir as the object.
1383 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1384 yaffsfs_SetError(-ENOTDIR);
1386 } else if(oldLoop || newLoop) {
1387 yaffsfs_SetError(-ELOOP);
1389 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1390 yaffsfs_SetError(-EINVAL);
1392 }else if(!olddir || !newdir || !obj) {
1393 yaffsfs_SetError(-ENOENT);
1395 } else if(obj->my_dev->read_only){
1396 yaffsfs_SetError(-EROFS);
1398 } else if(yaffs_is_non_empty_dir(newobj)){
1399 yaffsfs_SetError(-ENOTEMPTY);
1401 } else if(olddir->my_dev != newdir->my_dev) {
1402 /* Rename must be on same device */
1403 yaffsfs_SetError(-EXDEV);
1405 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1407 * It is a directory, check that it is not being renamed to
1408 * being its own decendent.
1409 * Do this by tracing from the new directory back to the root,
1413 struct yaffs_obj *xx = newdir;
1415 while( rename_allowed && xx){
1421 yaffsfs_SetError(-EINVAL);
1425 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1432 return (result == YAFFS_FAIL) ? -1 : 0;
1436 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1440 obj = yaffs_get_equivalent_obj(obj);
1443 buf->st_dev = (int)obj->my_dev->os_context;
1444 buf->st_ino = obj->obj_id;
1445 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1447 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1448 buf->st_mode |= S_IFDIR;
1449 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1450 buf->st_mode |= S_IFLNK;
1451 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1452 buf->st_mode |= S_IFREG;
1454 buf->st_nlink = yaffs_get_obj_link_count(obj);
1457 buf->st_rdev = obj->yst_rdev;
1458 buf->st_size = yaffs_get_obj_length(obj);
1459 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1460 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1461 #if CONFIG_YAFFS_WINCE
1462 buf->yst_wince_atime[0] = obj->win_atime[0];
1463 buf->yst_wince_atime[1] = obj->win_atime[1];
1464 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1465 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1466 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1467 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1469 buf->yst_atime = obj->yst_atime;
1470 buf->yst_ctime = obj->yst_ctime;
1471 buf->yst_mtime = obj->yst_mtime;
1478 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1480 struct yaffs_obj *obj=NULL;
1481 struct yaffs_obj *dir=NULL;
1486 if(yaffsfs_CheckPath(path) < 0){
1487 yaffsfs_SetError(-ENAMETOOLONG);
1493 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1496 obj = yaffsfs_FollowLink(obj,0,&loop);
1499 yaffsfs_SetError(-ENOTDIR);
1501 yaffsfs_SetError(-ELOOP);
1502 else if(!dir || !obj)
1503 yaffsfs_SetError(-ENOENT);
1505 retVal = yaffsfs_DoStat(obj,buf);
1513 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1515 return yaffsfs_DoStatOrLStat(path,buf,0);
1518 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1520 return yaffsfs_DoStatOrLStat(path,buf,1);
1523 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1525 struct yaffs_obj *obj;
1530 obj = yaffsfs_GetHandleObject(fd);
1533 retVal = yaffsfs_DoStat(obj,buf);
1536 yaffsfs_SetError(-EBADF);
1543 #ifndef CONFIG_YAFFS_WINCE
1544 /* xattrib functions */
1547 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1549 struct yaffs_obj *obj;
1550 struct yaffs_obj *dir;
1556 if(yaffsfs_CheckPath(path) < 0){
1557 yaffsfs_SetError(-ENAMETOOLONG);
1563 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1566 obj = yaffsfs_FollowLink(obj,0,&loop);
1569 yaffsfs_SetError(-ENOTDIR);
1571 yaffsfs_SetError(-ELOOP);
1572 else if(!dir || !obj)
1573 yaffsfs_SetError(-ENOENT);
1575 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1577 yaffsfs_SetError(retVal);
1588 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1590 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1593 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1595 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1600 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1602 struct yaffs_obj *obj;
1607 obj = yaffsfs_GetHandleObject(fd);
1610 yaffsfs_SetError(-EBADF);
1612 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1614 yaffsfs_SetError(retVal);
1624 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1626 struct yaffs_obj *obj;
1627 struct yaffs_obj *dir;
1632 if(yaffsfs_CheckPath(path) < 0){
1633 yaffsfs_SetError(-ENAMETOOLONG);
1639 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1642 obj = yaffsfs_FollowLink(obj,0,&loop);
1645 yaffsfs_SetError(-ENOTDIR);
1647 yaffsfs_SetError(-ELOOP);
1648 else if(!dir || !obj)
1649 yaffsfs_SetError(-ENOENT);
1651 retVal = yaffs_get_xattrib(obj,name,data,size);
1653 yaffsfs_SetError(retVal);
1663 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1665 return yaffs_do_getxattr( path, name, data, size, 1);
1667 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1669 return yaffs_do_getxattr( path, name, data, size, 0);
1674 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1676 struct yaffs_obj *obj;
1681 obj = yaffsfs_GetHandleObject(fd);
1684 retVal = yaffs_get_xattrib(obj,name,data,size);
1686 yaffsfs_SetError(retVal);
1691 yaffsfs_SetError(-EBADF);
1698 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1700 struct yaffs_obj *obj=NULL;
1701 struct yaffs_obj *dir=NULL;
1706 if(yaffsfs_CheckPath(path) < 0){
1707 yaffsfs_SetError(-ENAMETOOLONG);
1713 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1716 obj = yaffsfs_FollowLink(obj,0,&loop);
1719 yaffsfs_SetError(-ENOTDIR);
1721 yaffsfs_SetError(-ELOOP);
1722 else if(!dir || !obj)
1723 yaffsfs_SetError(-ENOENT);
1725 retVal = yaffs_list_xattrib(obj, data,size);
1727 yaffsfs_SetError(retVal);
1738 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1740 return yaffs_do_listxattr(path, data, size, 1);
1743 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1745 return yaffs_do_listxattr(path, data, size, 0);
1748 int yaffs_flistxattr(int fd, char *data, int size)
1750 struct yaffs_obj *obj;
1755 obj = yaffsfs_GetHandleObject(fd);
1758 retVal = yaffs_list_xattrib(obj,data,size);
1760 yaffsfs_SetError(retVal);
1765 yaffsfs_SetError(-EBADF);
1772 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1774 struct yaffs_obj *obj=NULL;
1775 struct yaffs_obj *dir=NULL;
1780 if(yaffsfs_CheckPath(path) < 0){
1781 yaffsfs_SetError(-ENAMETOOLONG);
1787 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1790 obj = yaffsfs_FollowLink(obj,0,&loop);
1793 yaffsfs_SetError(-ENOTDIR);
1795 yaffsfs_SetError(-ELOOP);
1796 else if(!dir || !obj)
1797 yaffsfs_SetError(-ENOENT);
1799 retVal = yaffs_remove_xattrib(obj,name);
1801 yaffsfs_SetError(retVal);
1812 int yaffs_removexattr(const YCHAR *path, const char *name)
1814 return yaffs_do_removexattr(path, name, 1);
1817 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1819 return yaffs_do_removexattr(path, name, 0);
1822 int yaffs_fremovexattr(int fd, const char *name)
1824 struct yaffs_obj *obj;
1829 obj = yaffsfs_GetHandleObject(fd);
1832 retVal = yaffs_remove_xattrib(obj,name);
1834 yaffsfs_SetError(retVal);
1839 yaffsfs_SetError(-EBADF);
1847 #ifdef CONFIG_YAFFS_WINCE
1848 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1850 struct yaffs_obj *obj;
1855 obj = yaffsfs_GetHandleObject(fd);
1860 wctime[0] = obj->win_ctime[0];
1861 wctime[1] = obj->win_ctime[1];
1864 watime[0] = obj->win_atime[0];
1865 watime[1] = obj->win_atime[1];
1868 wmtime[0] = obj->win_mtime[0];
1869 wmtime[1] = obj->win_mtime[1];
1876 yaffsfs_SetError(-EBADF);
1884 int yaffs_set_wince_times(int fd,
1885 const unsigned *wctime,
1886 const unsigned *watime,
1887 const unsigned *wmtime)
1889 struct yaffs_obj *obj;
1894 obj = yaffsfs_GetHandleObject(fd);
1899 obj->win_ctime[0] = wctime[0];
1900 obj->win_ctime[1] = wctime[1];
1903 obj->win_atime[0] = watime[0];
1904 obj->win_atime[1] = watime[1];
1907 obj->win_mtime[0] = wmtime[0];
1908 obj->win_mtime[1] = wmtime[1];
1912 result = yaffs_flush_file(obj,0,0);
1916 yaffsfs_SetError(-EBADF);
1926 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1931 obj = yaffs_get_equivalent_obj(obj);
1934 obj->yst_mode = mode;
1936 result = yaffs_flush_file(obj,0,0);
1939 return result == YAFFS_OK ? 0 : -1;
1943 int yaffs_access(const YCHAR *path, int amode)
1945 struct yaffs_obj *obj=NULL;
1946 struct yaffs_obj *dir=NULL;
1951 if(yaffsfs_CheckPath(path) < 0){
1952 yaffsfs_SetError(-ENAMETOOLONG);
1956 if(amode & ~(R_OK | W_OK | X_OK)){
1957 yaffsfs_SetError(-EINVAL);
1963 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1964 obj = yaffsfs_FollowLink(obj,0,&loop);
1967 yaffsfs_SetError(-ENOTDIR);
1969 yaffsfs_SetError(-ELOOP);
1970 else if(!dir || !obj)
1971 yaffsfs_SetError(-ENOENT);
1972 else if((amode & W_OK) && obj->my_dev->read_only)
1973 yaffsfs_SetError(-EROFS);
1977 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1979 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1981 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1985 yaffsfs_SetError(-EACCES);
1997 int yaffs_chmod(const YCHAR *path, mode_t mode)
1999 struct yaffs_obj *obj=NULL;
2000 struct yaffs_obj *dir=NULL;
2005 if(yaffsfs_CheckPath(path) < 0){
2006 yaffsfs_SetError(-ENAMETOOLONG);
2011 yaffsfs_SetError(-EINVAL);
2017 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2018 obj = yaffsfs_FollowLink(obj,0,&loop);
2021 yaffsfs_SetError(-ENOTDIR);
2023 yaffsfs_SetError(-ELOOP);
2024 else if(!dir || !obj)
2025 yaffsfs_SetError(-ENOENT);
2026 else if(obj->my_dev->read_only)
2027 yaffsfs_SetError(-EROFS);
2029 retVal = yaffsfs_DoChMod(obj,mode);
2038 int yaffs_fchmod(int fd, mode_t mode)
2040 struct yaffs_obj *obj;
2044 yaffsfs_SetError(-EINVAL);
2049 obj = yaffsfs_GetHandleObject(fd);
2052 yaffsfs_SetError(-EBADF);
2053 else if(obj->my_dev->read_only)
2054 yaffsfs_SetError(-EROFS);
2056 retVal = yaffsfs_DoChMod(obj,mode);
2063 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2065 struct yaffs_obj *parent = NULL;
2066 struct yaffs_obj *dir = NULL;
2068 YCHAR *alt_path = NULL;
2073 if(yaffsfs_CheckPath(path) < 0){
2074 yaffsfs_SetError(-ENAMETOOLONG);
2078 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2079 yaffsfs_SetError(-ENOMEM);
2086 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2087 if(!parent && notDir)
2088 yaffsfs_SetError(-ENOTDIR);
2090 yaffsfs_SetError(-ELOOP);
2092 yaffsfs_SetError(-ENOENT);
2093 else if(yaffs_strnlen(name,5) == 0){
2094 /* Trying to make the root itself */
2095 yaffsfs_SetError(-EEXIST);
2096 } else if(parent->my_dev->read_only)
2097 yaffsfs_SetError(-EROFS);
2099 dir = yaffs_create_dir(parent,name,mode,0,0);
2102 else if (yaffs_find_by_name(parent,name))
2103 yaffsfs_SetError(-EEXIST); /* the name already exists */
2105 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2116 int yaffs_rmdir(const YCHAR *path)
2121 if(yaffsfs_CheckPath(path) < 0){
2122 yaffsfs_SetError(-ENAMETOOLONG);
2126 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2127 yaffsfs_SetError(-ENOMEM);
2132 result = yaffsfs_DoUnlink(path,1);
2139 void * yaffs_getdev(const YCHAR *path)
2141 struct yaffs_dev *dev=NULL;
2143 dev = yaffsfs_FindDevice(path,&dummy);
2147 int yaffs_mount2(const YCHAR *path,int read_only)
2150 int result=YAFFS_FAIL;
2151 struct yaffs_dev *dev=NULL;
2153 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2155 if(yaffsfs_CheckPath(path) < 0){
2156 yaffsfs_SetError(-ENAMETOOLONG);
2162 yaffsfs_InitHandles();
2164 dev = yaffsfs_FindMountPoint(path);
2166 if(!dev->is_mounted){
2167 dev->read_only = read_only ? 1 : 0;
2168 result = yaffs_guts_initialise(dev);
2169 if(result == YAFFS_FAIL)
2170 yaffsfs_SetError(-ENOMEM);
2171 retVal = result ? 0 : -1;
2175 yaffsfs_SetError(-EBUSY);
2177 yaffsfs_SetError(-ENODEV);
2184 int yaffs_mount(const YCHAR *path)
2186 return yaffs_mount2(path,0);
2189 int yaffs_sync(const YCHAR *path)
2192 struct yaffs_dev *dev=NULL;
2195 if(yaffsfs_CheckPath(path) < 0){
2196 yaffsfs_SetError(-ENAMETOOLONG);
2201 dev = yaffsfs_FindDevice(path,&dummy);
2203 if(!dev->is_mounted)
2204 yaffsfs_SetError(-EINVAL);
2205 else if(dev->read_only)
2206 yaffsfs_SetError(-EROFS);
2209 yaffs_flush_whole_cache(dev);
2210 yaffs_checkpoint_save(dev);
2215 yaffsfs_SetError(-ENODEV);
2222 int yaffs_remount(const YCHAR *path, int force, int read_only)
2225 struct yaffs_dev *dev=NULL;
2228 if(yaffsfs_CheckPath(path) < 0){
2229 yaffsfs_SetError(-ENAMETOOLONG);
2234 dev = yaffsfs_FindMountPoint(path);
2236 if(dev->is_mounted){
2240 yaffs_flush_whole_cache(dev);
2242 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2243 yh = & yaffsfs_handle[i];
2244 if(yh->useCount>0 &&
2245 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2246 inUse = 1; /* the device is in use, can't unmount */
2249 if(!inUse || force){
2251 yaffs_checkpoint_save(dev);
2252 dev->read_only = read_only ? 1 : 0;
2255 yaffsfs_SetError(-EBUSY);
2258 yaffsfs_SetError(-EINVAL);
2262 yaffsfs_SetError(-ENODEV);
2269 int yaffs_unmount2(const YCHAR *path, int force)
2272 struct yaffs_dev *dev=NULL;
2274 if(yaffsfs_CheckPath(path) < 0){
2275 yaffsfs_SetError(-ENAMETOOLONG);
2280 dev = yaffsfs_FindMountPoint(path);
2282 if(dev->is_mounted){
2286 yaffs_flush_whole_cache(dev);
2287 yaffs_checkpoint_save(dev);
2289 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2290 if(yaffsfs_handle[i].useCount > 0 &&
2291 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2292 inUse = 1; /* the device is in use, can't unmount */
2295 if(!inUse || force){
2297 yaffsfs_BreakDeviceHandles(dev);
2298 yaffs_deinitialise(dev);
2302 yaffsfs_SetError(-EBUSY);
2305 yaffsfs_SetError(-EINVAL);
2308 yaffsfs_SetError(-ENODEV);
2315 int yaffs_unmount(const YCHAR *path)
2317 return yaffs_unmount2(path,0);
2320 loff_t yaffs_freespace(const YCHAR *path)
2323 struct yaffs_dev *dev=NULL;
2326 if(yaffsfs_CheckPath(path) < 0){
2327 yaffsfs_SetError(-ENAMETOOLONG);
2332 dev = yaffsfs_FindDevice(path,&dummy);
2333 if(dev && dev->is_mounted){
2334 retVal = yaffs_get_n_free_chunks(dev);
2335 retVal *= dev->data_bytes_per_chunk;
2338 yaffsfs_SetError(-EINVAL);
2344 loff_t yaffs_totalspace(const YCHAR *path)
2347 struct yaffs_dev *dev=NULL;
2350 if(yaffsfs_CheckPath(path) < 0){
2351 yaffsfs_SetError(-ENAMETOOLONG);
2356 dev = yaffsfs_FindDevice(path,&dummy);
2357 if(dev && dev->is_mounted){
2358 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2359 dev->param.n_reserved_blocks;
2360 retVal *= dev->param.chunks_per_block;
2361 retVal *= dev->data_bytes_per_chunk;
2364 yaffsfs_SetError(-EINVAL);
2370 int yaffs_inodecount(const YCHAR *path)
2373 struct yaffs_dev *dev=NULL;
2376 if(yaffsfs_CheckPath(path) < 0){
2377 yaffsfs_SetError(-ENAMETOOLONG);
2382 dev = yaffsfs_FindDevice(path,&dummy);
2383 if(dev && dev->is_mounted) {
2384 int n_obj = dev->n_obj;
2385 if(n_obj > dev->n_hardlinks)
2386 retVal = n_obj - dev->n_hardlinks;
2390 yaffsfs_SetError(-EINVAL);
2397 void yaffs_add_device(struct yaffs_dev *dev)
2399 dev->is_mounted = 0;
2400 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2402 if(!dev->dev_list.next)
2403 INIT_LIST_HEAD(&dev->dev_list);
2405 list_add(&dev->dev_list,&yaffsfs_deviceList);
2408 void yaffs_remove_device(struct yaffs_dev *dev)
2410 list_del_init(&dev->dev_list);
2416 /* Directory search stuff. */
2419 * Directory search context
2421 * NB this is an opaque structure.
2428 yaffs_dirent de; /* directory entry being used by this dsc */
2429 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2430 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2431 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2433 struct list_head others;
2434 } yaffsfs_DirectorySearchContext;
2438 static struct list_head search_contexts;
2441 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2445 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2449 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2450 dsc->nextReturn = NULL;
2452 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2453 struct yaffs_obj,siblings);
2455 /* Hey someone isn't playing nice! */
2459 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2463 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2465 if( dsc->nextReturn == NULL ||
2466 list_empty(&dsc->dirObj->variant.dir_variant.children))
2467 dsc->nextReturn = NULL;
2469 struct list_head *next = dsc->nextReturn->siblings.next;
2471 if( next == &dsc->dirObj->variant.dir_variant.children)
2472 dsc->nextReturn = NULL; /* end of list */
2474 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2477 /* Hey someone isn't playing nice! */
2481 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2484 struct list_head *i;
2485 yaffsfs_DirectorySearchContext *dsc;
2487 /* if search contexts not initilised then skip */
2488 if(!search_contexts.next)
2491 /* Iterate through the directory search contexts.
2492 * If any are the one being removed, then advance the dsc to
2493 * the next one to prevent a hanging ptr.
2495 list_for_each(i, &search_contexts) {
2497 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2498 if(dsc->nextReturn == obj)
2499 yaffsfs_DirAdvance(dsc);
2505 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2507 yaffs_DIR *dir = NULL;
2508 struct yaffs_obj *obj = NULL;
2509 yaffsfs_DirectorySearchContext *dsc = NULL;
2513 if(yaffsfs_CheckPath(dirname) < 0){
2514 yaffsfs_SetError(-ENAMETOOLONG);
2520 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2523 yaffsfs_SetError(-ENOTDIR);
2525 yaffsfs_SetError(-ELOOP);
2527 yaffsfs_SetError(-ENOENT);
2528 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2529 yaffsfs_SetError(-ENOTDIR);
2532 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2533 dir = (yaffs_DIR *)dsc;
2536 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2537 dsc->magic = YAFFS_MAGIC;
2539 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2540 INIT_LIST_HEAD(&dsc->others);
2542 if(!search_contexts.next)
2543 INIT_LIST_HEAD(&search_contexts);
2545 list_add(&dsc->others,&search_contexts);
2546 yaffsfs_SetDirRewound(dsc);
2556 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2558 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2559 struct yaffs_dirent *retVal = NULL;
2563 if(dsc && dsc->magic == YAFFS_MAGIC){
2564 yaffsfs_SetError(0);
2565 if(dsc->nextReturn){
2566 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2567 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2568 dsc->de.d_off = dsc->offset++;
2569 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2570 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2572 /* this should not happen! */
2573 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2575 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2577 yaffsfs_DirAdvance(dsc);
2581 yaffsfs_SetError(-EBADF);
2590 void yaffs_rewinddir(yaffs_DIR *dirp)
2592 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2596 yaffsfs_SetDirRewound(dsc);
2602 int yaffs_closedir(yaffs_DIR *dirp)
2604 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2608 list_del(&dsc->others); /* unhook from list */
2614 /* End of directory stuff */
2617 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2619 struct yaffs_obj *parent = NULL;
2620 struct yaffs_obj *obj;
2623 int mode = 0; /* ignore for now */
2627 if(yaffsfs_CheckPath(newpath) < 0){
2628 yaffsfs_SetError(-ENAMETOOLONG);
2632 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2633 if(!parent && notDir)
2634 yaffsfs_SetError(-ENOTDIR);
2636 yaffsfs_SetError(-ELOOP);
2637 else if( !parent || yaffs_strnlen(name,5) < 1)
2638 yaffsfs_SetError(-ENOENT);
2639 else if(parent->my_dev->read_only)
2640 yaffsfs_SetError(-EROFS);
2642 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2645 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2646 yaffsfs_SetError(-EEXIST);
2648 yaffsfs_SetError(-ENOSPC);
2657 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2659 struct yaffs_obj *obj = NULL;
2660 struct yaffs_obj *dir = NULL;
2667 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2670 yaffsfs_SetError(-ENOTDIR);
2672 yaffsfs_SetError(-ELOOP);
2673 else if(!dir || !obj)
2674 yaffsfs_SetError(-ENOENT);
2675 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2676 yaffsfs_SetError(-EINVAL);
2678 YCHAR *alias = obj->variant.symlink_variant.alias;
2679 memset(buf,0,bufsiz);
2680 yaffs_strncpy(buf,alias,bufsiz - 1);
2687 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2689 /* Creates a link called newpath to existing oldpath */
2690 struct yaffs_obj *obj = NULL;
2691 struct yaffs_obj *lnk = NULL;
2692 struct yaffs_obj *obj_dir = NULL;
2693 struct yaffs_obj *lnk_dir = NULL;
2701 if(yaffsfs_CheckPath(linkpath) < 0){
2702 yaffsfs_SetError(-ENAMETOOLONG);
2708 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
2709 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2710 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
2712 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2713 yaffsfs_SetError(-ENOTDIR);
2714 else if(objLoop || lnkLoop)
2715 yaffsfs_SetError(-ELOOP);
2716 else if(!obj_dir || !lnk_dir || !obj)
2717 yaffsfs_SetError(-ENOENT);
2718 else if(obj->my_dev->read_only)
2719 yaffsfs_SetError(-EINVAL);
2721 yaffsfs_SetError(-EEXIST);
2722 else if(lnk_dir->my_dev != obj->my_dev)
2723 yaffsfs_SetError(-EXDEV);
2725 retVal = yaffsfs_CheckNameLength(newname);
2728 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2732 yaffsfs_SetError(-ENOSPC);
2742 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2748 yaffsfs_SetError(-EINVAL);
2754 * D E B U G F U N C T I O N S
2759 * Returns number of handles attached to the object
2761 int yaffs_n_handles(const YCHAR *path)
2763 struct yaffs_obj *obj;
2765 if(yaffsfs_CheckPath(path) < 0){
2766 yaffsfs_SetError(-ENAMETOOLONG);
2770 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2773 return yaffsfs_CountHandles(obj);
2778 int yaffs_get_error(void)
2780 return yaffsfs_GetLastError();
2783 int yaffs_set_error(int error)
2785 yaffsfs_SetError(error);
2789 int yaffs_dump_dev(const YCHAR *path)
2796 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2799 struct yaffs_dev *dev = obj->my_dev;
2802 "n_page_writes.......... %d\n"
2803 "n_page_reads........... %d\n"
2804 "n_erasures....... %d\n"
2805 "n_gc_copies............ %d\n"
2806 "garbageCollections... %d\n"
2807 "passiveGarbageColl'ns %d\n"
2813 dev->garbageCollections,
2814 dev->passiveGarbageCollections