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 /*************************************************************************
639 * Start of yaffsfs visible functions.
640 *************************************************************************/
642 int yaffs_dup(int fd)
645 yaffsfs_Handle *oldPtr = NULL;
646 yaffsfs_Handle *newPtr = NULL;
650 oldPtr = yaffsfs_GetHandlePointer(fd);
651 if(oldPtr && oldPtr->useCount > 0)
652 newHandle = yaffsfs_GetNewHandle();
654 newPtr = yaffsfs_GetHandlePointer(newHandle);
662 yaffsfs_SetError(-EBADF);
664 yaffsfs_SetError(-ENOMEM);
672 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
674 struct yaffs_obj *obj = NULL;
675 struct yaffs_obj *dir = NULL;
678 yaffsfs_Handle *yh = NULL;
681 int errorReported = 0;
682 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
683 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
684 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
685 u8 sharedReadAllowed;
686 u8 sharedWriteAllowed;
695 yaffsfs_SetError(-EFAULT);
699 if(yaffsfs_CheckPath(path) < 0){
700 yaffsfs_SetError(-ENAMETOOLONG);
704 /* O_EXCL only has meaning if O_CREAT is specified */
705 if(!(oflag & O_CREAT))
708 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
709 if( (oflag & O_CREAT) & (oflag & O_EXCL))
712 /* Todo: Are there any more flag combos to sanitise ? */
714 /* Figure out if reading or writing is requested */
716 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
717 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
721 handle = yaffsfs_GetNewHandle();
724 yaffsfs_SetError(-ENFILE);
728 yh = yaffsfs_GetHandlePointer(handle);
730 /* try to find the exisiting object */
731 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
733 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
736 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
737 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
743 /* The file already exists or it might be a directory */
745 /* If it is a directory then we can't open it as a file */
746 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
748 yaffsfs_SetError(-EISDIR);
752 /* Open should fail if O_CREAT and O_EXCL are specified since
755 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
757 yaffsfs_SetError(-EEXIST);
761 /* Check file permissions */
762 if( readRequested && !(obj->yst_mode & S_IREAD))
765 if( writeRequested && !(obj->yst_mode & S_IWRITE))
768 if( !errorReported && writeRequested &&
769 obj->my_dev->read_only){
771 yaffsfs_SetError(-EROFS);
775 if(openDenied && !errorReported ) {
776 /* Error if the file exists but permissions are refused. */
777 yaffsfs_SetError(-EACCES);
781 /* Check sharing of an existing object. */
785 sharedReadAllowed = 1;
786 sharedWriteAllowed = 1;
789 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
790 hx = &yaffsfs_handle[i];
791 if(hx->useCount > 0 &&
793 yaffsfs_inode[hx->inodeId].iObj == obj){
795 sharedReadAllowed = 0;
797 sharedWriteAllowed = 0;
807 if((!sharedReadAllowed && readRequested)||
808 (!shareRead && alreadyReading) ||
809 (!sharedWriteAllowed && writeRequested) ||
810 (!shareWrite && alreadyWriting)){
812 yaffsfs_SetError(-EBUSY);
819 /* If we could not open an existing object, then let's see if
820 * the directory exists. If not, error.
822 if(!obj && !errorReported){
823 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
825 yaffsfs_SetError(-ENOTDIR);
828 yaffsfs_SetError(-ELOOP);
831 yaffsfs_SetError(-ENOENT);
836 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
837 /* Let's see if we can create this file if it does not exist. */
838 if(dir->my_dev->read_only){
839 yaffsfs_SetError(-EROFS);
842 obj = yaffs_create_file(dir,name,mode,0,0);
844 if(!obj && !errorReported){
845 yaffsfs_SetError(-ENOSPC);
850 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
851 /* Error if the file does not exist and CREAT is not set. */
852 yaffsfs_SetError(-ENOENT);
856 if(obj && !openDenied) {
857 int inodeId = yaffsfs_GetInodeIdForObject(obj);
861 * Todo: Fix any problem if inodes run out, though that
862 * can't happen if the number of inode items >= number of handles.
866 yh->inodeId = inodeId;
867 yh->reading = readRequested;
868 yh->writing = writeRequested;
869 yh->append = (oflag & O_APPEND) ? 1 : 0;
871 yh->shareRead = shareRead;
872 yh->shareWrite = shareWrite;
874 /* Hook inode to object */
875 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
877 if((oflag & O_TRUNC) && yh->writing)
878 yaffs_resize_file(obj,0);
880 yaffsfs_PutHandle(handle);
882 yaffsfs_SetError(0); /* Problem */
892 int yaffs_open(const YCHAR *path, int oflag, int mode)
894 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
897 int yaffs_Dofsync(int fd,int datasync)
899 yaffsfs_Handle *h = NULL;
904 h = yaffsfs_GetHandlePointer(fd);
906 if(!h || h->useCount < 1)
907 yaffsfs_SetError(-EBADF);
909 struct yaffs_obj * obj;
910 obj = yaffsfs_inode[h->inodeId].iObj;
911 if(obj->my_dev->read_only)
912 yaffsfs_SetError(-EROFS);
914 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
924 int yaffs_fsync(int fd)
926 return yaffs_Dofsync(fd,0);
929 int yaffs_flush(int fd)
931 return yaffs_fsync(fd);
934 int yaffs_fdatasync(int fd)
936 return yaffs_Dofsync(fd,1);
939 int yaffs_close(int fd)
941 yaffsfs_Handle *h = NULL;
946 h = yaffsfs_GetHandlePointer(fd);
948 if(h && h->useCount > 0) {
950 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
951 yaffsfs_PutHandle(fd);
955 yaffsfs_SetError(-EBADF);
966 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
968 yaffsfs_Handle *h = NULL;
969 struct yaffs_obj *obj = NULL;
976 unsigned int maxRead;
977 u8 *buf = (u8 *)vbuf;
980 yaffsfs_SetError(-EFAULT);
985 h = yaffsfs_GetHandlePointer(fd);
986 obj = yaffsfs_GetHandleObject(fd);
990 yaffsfs_SetError(-EBADF);
992 } else if(!h->reading){
993 /* Not a reading handle */
994 yaffsfs_SetError(-EINVAL);
996 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
997 yaffsfs_SetError(-EINVAL);
1003 startPos = h->position;
1007 if(yaffs_get_obj_length(obj) > pos)
1008 maxRead = yaffs_get_obj_length(obj) - pos;
1016 yaffsfs_GetHandle(fd);
1018 endPos = pos + nbyte;
1020 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1021 nbyte > YAFFS_MAX_FILE_SIZE ||
1022 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1028 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1033 * Need to reverify object in case the device was
1034 * unmounted in another thread.
1036 obj = yaffsfs_GetHandleObject(fd);
1040 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1048 if(nRead == nToRead)
1051 nbyte = 0; /* no more to read */
1061 yaffsfs_PutHandle(fd);
1065 h->position = startPos + totalRead;
1067 yaffsfs_SetError(-EINVAL);
1074 return (totalRead >= 0) ? totalRead : -1;
1078 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1080 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1083 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1085 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1088 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1090 yaffsfs_Handle *h = NULL;
1091 struct yaffs_obj *obj = NULL;
1096 int totalWritten = 0;
1097 int write_trhrough = 0;
1099 const u8 *buf = (const u8 *)vbuf;
1102 yaffsfs_SetError(-EFAULT);
1107 h = yaffsfs_GetHandlePointer(fd);
1108 obj = yaffsfs_GetHandleObject(fd);
1112 yaffsfs_SetError(-EBADF);
1114 } else if(!h->writing){
1115 yaffsfs_SetError(-EINVAL);
1117 } else if(obj->my_dev->read_only){
1118 yaffsfs_SetError(-EROFS);
1122 startPos = yaffs_get_obj_length(obj);
1126 startPos = h->position;
1128 yaffsfs_GetHandle(fd);
1130 endPos = pos + nbyte;
1132 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1133 nbyte > YAFFS_MAX_FILE_SIZE ||
1134 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1141 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1142 if(nToWrite > nbyte)
1146 * Need to reverify object in case the device was
1147 * remounted or unmounted in another thread.
1149 obj = yaffsfs_GetHandleObject(fd);
1150 if(!obj || obj->my_dev->read_only)
1153 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1156 totalWritten += nWritten;
1161 if(nWritten == nToWrite)
1166 if(nWritten < 1 && totalWritten < 1){
1167 yaffsfs_SetError(-ENOSPC);
1177 yaffsfs_PutHandle(fd);
1180 if(totalWritten > 0)
1181 h->position = startPos + totalWritten;
1183 yaffsfs_SetError(-EINVAL);
1189 return (totalWritten >= 0) ? totalWritten : -1;
1192 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1194 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1197 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1199 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1203 int yaffs_truncate(const YCHAR *path,off_t new_size)
1205 struct yaffs_obj *obj = NULL;
1206 struct yaffs_obj *dir = NULL;
1207 int result = YAFFS_FAIL;
1212 yaffsfs_SetError(-EFAULT);
1216 if(yaffsfs_CheckPath(path) < 0){
1217 yaffsfs_SetError(-ENAMETOOLONG);
1223 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1224 obj = yaffsfs_FollowLink(obj,0,&loop);
1227 yaffsfs_SetError(-ENOTDIR);
1229 yaffsfs_SetError(-ELOOP);
1230 else if(!dir || !obj)
1231 yaffsfs_SetError(-ENOENT);
1232 else if(obj->my_dev->read_only)
1233 yaffsfs_SetError(-EROFS);
1234 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1235 yaffsfs_SetError(-EISDIR);
1236 else if(obj->my_dev->read_only)
1237 yaffsfs_SetError(-EROFS);
1238 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1239 yaffsfs_SetError(-EINVAL);
1241 result = yaffs_resize_file(obj,new_size);
1245 return (result) ? 0 : -1;
1248 int yaffs_ftruncate(int fd, off_t new_size)
1250 yaffsfs_Handle *h = NULL;
1251 struct yaffs_obj *obj = NULL;
1255 h = yaffsfs_GetHandlePointer(fd);
1256 obj = yaffsfs_GetHandleObject(fd);
1260 yaffsfs_SetError(-EBADF);
1261 else if(!h->writing)
1262 yaffsfs_SetError(-EINVAL);
1263 else if(obj->my_dev->read_only)
1264 yaffsfs_SetError(-EROFS);
1265 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1266 yaffsfs_SetError(-EINVAL);
1268 /* resize the file */
1269 result = yaffs_resize_file(obj,new_size);
1272 return (result) ? 0 : -1;
1276 off_t yaffs_lseek(int fd, off_t offset, int whence)
1278 yaffsfs_Handle *h = NULL;
1279 struct yaffs_obj *obj = NULL;
1284 h = yaffsfs_GetHandlePointer(fd);
1285 obj = yaffsfs_GetHandleObject(fd);
1288 yaffsfs_SetError(-EBADF);
1289 else if(offset > YAFFS_MAX_FILE_SIZE)
1290 yaffsfs_SetError(-EINVAL);
1292 if(whence == SEEK_SET){
1295 } else if(whence == SEEK_CUR) {
1296 if( (h->position + offset) >= 0)
1297 pos = (h->position + offset);
1298 } else if(whence == SEEK_END) {
1299 fSize = yaffs_get_obj_length(obj);
1300 if(fSize >= 0 && (fSize + offset) >= 0)
1301 pos = fSize + offset;
1304 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1307 yaffsfs_SetError(-EINVAL);
1318 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1320 struct yaffs_obj *dir = NULL;
1321 struct yaffs_obj *obj = NULL;
1323 int result = YAFFS_FAIL;
1328 yaffsfs_SetError(-EFAULT);
1332 if(yaffsfs_CheckPath(path) < 0){
1333 yaffsfs_SetError(-ENAMETOOLONG);
1339 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1340 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1343 yaffsfs_SetError(-ENOTDIR);
1345 yaffsfs_SetError(-ELOOP);
1347 yaffsfs_SetError(-ENOENT);
1348 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1349 yaffsfs_SetError(-EINVAL);
1351 yaffsfs_SetError(-ENOENT);
1352 else if(obj->my_dev->read_only)
1353 yaffsfs_SetError(-EROFS);
1354 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1355 yaffsfs_SetError(-EISDIR);
1356 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1357 yaffsfs_SetError(-ENOTDIR);
1358 else if(isDirectory && obj == obj->my_dev->root_dir)
1359 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1361 result = yaffs_unlinker(dir,name);
1363 if(result == YAFFS_FAIL && isDirectory)
1364 yaffsfs_SetError(-ENOTEMPTY);
1369 return (result == YAFFS_FAIL) ? -1 : 0;
1373 int yaffs_unlink(const YCHAR *path)
1375 return yaffsfs_DoUnlink(path,0);
1378 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1380 struct yaffs_obj *olddir = NULL;
1381 struct yaffs_obj *newdir = NULL;
1382 struct yaffs_obj *obj = NULL;
1383 struct yaffs_obj *newobj = NULL;
1386 int result= YAFFS_FAIL;
1387 int rename_allowed = 1;
1393 YCHAR *alt_newpath=NULL;
1395 if(!oldPath || !newPath){
1396 yaffsfs_SetError(-EFAULT);
1400 if(yaffsfs_CheckPath(oldPath) < 0 ||
1401 yaffsfs_CheckPath(newPath) < 0){
1402 yaffsfs_SetError(-ENAMETOOLONG);
1406 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1407 yaffsfs_SetError(-ENOMEM);
1411 newPath = alt_newpath;
1416 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1417 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1418 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1419 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1421 /* If the object being renamed is a directory and the
1422 * path ended with a "/" then the olddir == obj.
1423 * We pass through NULL for the old name to tell the lower layers
1424 * to use olddir as the object.
1430 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1431 yaffsfs_SetError(-ENOTDIR);
1433 } else if(oldLoop || newLoop) {
1434 yaffsfs_SetError(-ELOOP);
1436 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1437 yaffsfs_SetError(-EINVAL);
1439 }else if(!olddir || !newdir || !obj) {
1440 yaffsfs_SetError(-ENOENT);
1442 } else if(obj->my_dev->read_only){
1443 yaffsfs_SetError(-EROFS);
1445 } else if(yaffs_is_non_empty_dir(newobj)){
1446 yaffsfs_SetError(-ENOTEMPTY);
1448 } else if(olddir->my_dev != newdir->my_dev) {
1449 /* Rename must be on same device */
1450 yaffsfs_SetError(-EXDEV);
1452 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1454 * It is a directory, check that it is not being renamed to
1455 * being its own decendent.
1456 * Do this by tracing from the new directory back to the root,
1460 struct yaffs_obj *xx = newdir;
1462 while( rename_allowed && xx){
1468 yaffsfs_SetError(-EINVAL);
1472 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1479 return (result == YAFFS_FAIL) ? -1 : 0;
1483 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1487 obj = yaffs_get_equivalent_obj(obj);
1490 buf->st_dev = (int)obj->my_dev->os_context;
1491 buf->st_ino = obj->obj_id;
1492 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1494 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1495 buf->st_mode |= S_IFDIR;
1496 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1497 buf->st_mode |= S_IFLNK;
1498 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1499 buf->st_mode |= S_IFREG;
1501 buf->st_nlink = yaffs_get_obj_link_count(obj);
1504 buf->st_rdev = obj->yst_rdev;
1505 buf->st_size = yaffs_get_obj_length(obj);
1506 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1507 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1508 #if CONFIG_YAFFS_WINCE
1509 buf->yst_wince_atime[0] = obj->win_atime[0];
1510 buf->yst_wince_atime[1] = obj->win_atime[1];
1511 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1512 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1513 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1514 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1516 buf->yst_atime = obj->yst_atime;
1517 buf->yst_ctime = obj->yst_ctime;
1518 buf->yst_mtime = obj->yst_mtime;
1525 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1527 struct yaffs_obj *obj=NULL;
1528 struct yaffs_obj *dir=NULL;
1534 yaffsfs_SetError(-EFAULT);
1538 if(yaffsfs_CheckPath(path) < 0){
1539 yaffsfs_SetError(-ENAMETOOLONG);
1545 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1548 obj = yaffsfs_FollowLink(obj,0,&loop);
1551 yaffsfs_SetError(-ENOTDIR);
1553 yaffsfs_SetError(-ELOOP);
1554 else if(!dir || !obj)
1555 yaffsfs_SetError(-ENOENT);
1557 retVal = yaffsfs_DoStat(obj,buf);
1565 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1567 return yaffsfs_DoStatOrLStat(path,buf,0);
1570 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1572 return yaffsfs_DoStatOrLStat(path,buf,1);
1575 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1577 struct yaffs_obj *obj;
1582 yaffsfs_SetError(-EFAULT);
1587 obj = yaffsfs_GetHandleObject(fd);
1590 retVal = yaffsfs_DoStat(obj,buf);
1593 yaffsfs_SetError(-EBADF);
1600 #ifndef CONFIG_YAFFS_WINCE
1601 /* xattrib functions */
1604 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1605 const void *data, int size, int flags, int follow)
1607 struct yaffs_obj *obj;
1608 struct yaffs_obj *dir;
1614 if(!path || !name || !data){
1615 yaffsfs_SetError(-EFAULT);
1619 if(yaffsfs_CheckPath(path) < 0){
1620 yaffsfs_SetError(-ENAMETOOLONG);
1626 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1629 obj = yaffsfs_FollowLink(obj,0,&loop);
1632 yaffsfs_SetError(-ENOTDIR);
1634 yaffsfs_SetError(-ELOOP);
1635 else if(!dir || !obj)
1636 yaffsfs_SetError(-ENOENT);
1638 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1640 yaffsfs_SetError(retVal);
1651 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1653 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1656 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1658 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1663 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1665 struct yaffs_obj *obj;
1670 yaffsfs_SetError(-EFAULT);
1675 obj = yaffsfs_GetHandleObject(fd);
1678 yaffsfs_SetError(-EBADF);
1680 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1682 yaffsfs_SetError(retVal);
1692 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1694 struct yaffs_obj *obj;
1695 struct yaffs_obj *dir;
1700 if(!path || !name || !data ){
1701 yaffsfs_SetError(-EFAULT);
1705 if(yaffsfs_CheckPath(path) < 0){
1706 yaffsfs_SetError(-ENAMETOOLONG);
1712 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1715 obj = yaffsfs_FollowLink(obj,0,&loop);
1718 yaffsfs_SetError(-ENOTDIR);
1720 yaffsfs_SetError(-ELOOP);
1721 else if(!dir || !obj)
1722 yaffsfs_SetError(-ENOENT);
1724 retVal = yaffs_get_xattrib(obj,name,data,size);
1726 yaffsfs_SetError(retVal);
1736 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1738 return yaffs_do_getxattr( path, name, data, size, 1);
1740 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1742 return yaffs_do_getxattr( path, name, data, size, 0);
1747 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1749 struct yaffs_obj *obj;
1753 if(!name || !data ){
1754 yaffsfs_SetError(-EFAULT);
1759 obj = yaffsfs_GetHandleObject(fd);
1762 retVal = yaffs_get_xattrib(obj,name,data,size);
1764 yaffsfs_SetError(retVal);
1769 yaffsfs_SetError(-EBADF);
1776 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1778 struct yaffs_obj *obj=NULL;
1779 struct yaffs_obj *dir=NULL;
1784 if(!path || !data ){
1785 yaffsfs_SetError(-EFAULT);
1789 if(yaffsfs_CheckPath(path) < 0){
1790 yaffsfs_SetError(-ENAMETOOLONG);
1796 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1799 obj = yaffsfs_FollowLink(obj,0,&loop);
1802 yaffsfs_SetError(-ENOTDIR);
1804 yaffsfs_SetError(-ELOOP);
1805 else if(!dir || !obj)
1806 yaffsfs_SetError(-ENOENT);
1808 retVal = yaffs_list_xattrib(obj, data,size);
1810 yaffsfs_SetError(retVal);
1821 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1823 return yaffs_do_listxattr(path, data, size, 1);
1826 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1828 return yaffs_do_listxattr(path, data, size, 0);
1831 int yaffs_flistxattr(int fd, char *data, int size)
1833 struct yaffs_obj *obj;
1838 yaffsfs_SetError(-EFAULT);
1843 obj = yaffsfs_GetHandleObject(fd);
1846 retVal = yaffs_list_xattrib(obj,data,size);
1848 yaffsfs_SetError(retVal);
1853 yaffsfs_SetError(-EBADF);
1860 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1862 struct yaffs_obj *obj=NULL;
1863 struct yaffs_obj *dir=NULL;
1869 yaffsfs_SetError(-EFAULT);
1873 if(yaffsfs_CheckPath(path) < 0){
1874 yaffsfs_SetError(-ENAMETOOLONG);
1880 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1883 obj = yaffsfs_FollowLink(obj,0,&loop);
1886 yaffsfs_SetError(-ENOTDIR);
1888 yaffsfs_SetError(-ELOOP);
1889 else if(!dir || !obj)
1890 yaffsfs_SetError(-ENOENT);
1892 retVal = yaffs_remove_xattrib(obj,name);
1894 yaffsfs_SetError(retVal);
1905 int yaffs_removexattr(const YCHAR *path, const char *name)
1907 return yaffs_do_removexattr(path, name, 1);
1910 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1912 return yaffs_do_removexattr(path, name, 0);
1915 int yaffs_fremovexattr(int fd, const char *name)
1917 struct yaffs_obj *obj;
1922 yaffsfs_SetError(-EFAULT);
1927 obj = yaffsfs_GetHandleObject(fd);
1930 retVal = yaffs_remove_xattrib(obj,name);
1932 yaffsfs_SetError(retVal);
1937 yaffsfs_SetError(-EBADF);
1945 #ifdef CONFIG_YAFFS_WINCE
1946 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1948 struct yaffs_obj *obj;
1953 obj = yaffsfs_GetHandleObject(fd);
1958 wctime[0] = obj->win_ctime[0];
1959 wctime[1] = obj->win_ctime[1];
1962 watime[0] = obj->win_atime[0];
1963 watime[1] = obj->win_atime[1];
1966 wmtime[0] = obj->win_mtime[0];
1967 wmtime[1] = obj->win_mtime[1];
1974 yaffsfs_SetError(-EBADF);
1982 int yaffs_set_wince_times(int fd,
1983 const unsigned *wctime,
1984 const unsigned *watime,
1985 const unsigned *wmtime)
1987 struct yaffs_obj *obj;
1992 obj = yaffsfs_GetHandleObject(fd);
1997 obj->win_ctime[0] = wctime[0];
1998 obj->win_ctime[1] = wctime[1];
2001 obj->win_atime[0] = watime[0];
2002 obj->win_atime[1] = watime[1];
2005 obj->win_mtime[0] = wmtime[0];
2006 obj->win_mtime[1] = wmtime[1];
2010 result = yaffs_flush_file(obj,0,0);
2014 yaffsfs_SetError(-EBADF);
2024 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2029 obj = yaffs_get_equivalent_obj(obj);
2032 obj->yst_mode = mode;
2034 result = yaffs_flush_file(obj,0,0);
2037 return result == YAFFS_OK ? 0 : -1;
2041 int yaffs_access(const YCHAR *path, int amode)
2043 struct yaffs_obj *obj=NULL;
2044 struct yaffs_obj *dir=NULL;
2050 yaffsfs_SetError(-EFAULT);
2054 if(yaffsfs_CheckPath(path) < 0){
2055 yaffsfs_SetError(-ENAMETOOLONG);
2059 if(amode & ~(R_OK | W_OK | X_OK)){
2060 yaffsfs_SetError(-EINVAL);
2066 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2067 obj = yaffsfs_FollowLink(obj,0,&loop);
2070 yaffsfs_SetError(-ENOTDIR);
2072 yaffsfs_SetError(-ELOOP);
2073 else if(!dir || !obj)
2074 yaffsfs_SetError(-ENOENT);
2075 else if((amode & W_OK) && obj->my_dev->read_only)
2076 yaffsfs_SetError(-EROFS);
2080 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2082 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2084 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2088 yaffsfs_SetError(-EACCES);
2100 int yaffs_chmod(const YCHAR *path, mode_t mode)
2102 struct yaffs_obj *obj=NULL;
2103 struct yaffs_obj *dir=NULL;
2109 yaffsfs_SetError(-EFAULT);
2113 if(yaffsfs_CheckPath(path) < 0){
2114 yaffsfs_SetError(-ENAMETOOLONG);
2119 yaffsfs_SetError(-EINVAL);
2125 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2126 obj = yaffsfs_FollowLink(obj,0,&loop);
2129 yaffsfs_SetError(-ENOTDIR);
2131 yaffsfs_SetError(-ELOOP);
2132 else if(!dir || !obj)
2133 yaffsfs_SetError(-ENOENT);
2134 else if(obj->my_dev->read_only)
2135 yaffsfs_SetError(-EROFS);
2137 retVal = yaffsfs_DoChMod(obj,mode);
2146 int yaffs_fchmod(int fd, mode_t mode)
2148 struct yaffs_obj *obj;
2152 yaffsfs_SetError(-EINVAL);
2157 obj = yaffsfs_GetHandleObject(fd);
2160 yaffsfs_SetError(-EBADF);
2161 else if(obj->my_dev->read_only)
2162 yaffsfs_SetError(-EROFS);
2164 retVal = yaffsfs_DoChMod(obj,mode);
2171 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2173 struct yaffs_obj *parent = NULL;
2174 struct yaffs_obj *dir = NULL;
2176 YCHAR *alt_path = NULL;
2182 yaffsfs_SetError(-EFAULT);
2186 if(yaffsfs_CheckPath(path) < 0){
2187 yaffsfs_SetError(-ENAMETOOLONG);
2191 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2192 yaffsfs_SetError(-ENOMEM);
2199 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2200 if(!parent && notDir)
2201 yaffsfs_SetError(-ENOTDIR);
2203 yaffsfs_SetError(-ELOOP);
2205 yaffsfs_SetError(-ENOENT);
2206 else if(yaffs_strnlen(name,5) == 0){
2207 /* Trying to make the root itself */
2208 yaffsfs_SetError(-EEXIST);
2209 } else if(parent->my_dev->read_only)
2210 yaffsfs_SetError(-EROFS);
2212 dir = yaffs_create_dir(parent,name,mode,0,0);
2215 else if (yaffs_find_by_name(parent,name))
2216 yaffsfs_SetError(-EEXIST); /* the name already exists */
2218 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2229 int yaffs_rmdir(const YCHAR *path)
2235 yaffsfs_SetError(-EFAULT);
2239 if(yaffsfs_CheckPath(path) < 0){
2240 yaffsfs_SetError(-ENAMETOOLONG);
2244 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2245 yaffsfs_SetError(-ENOMEM);
2250 result = yaffsfs_DoUnlink(path,1);
2257 void * yaffs_getdev(const YCHAR *path)
2259 struct yaffs_dev *dev=NULL;
2261 dev = yaffsfs_FindDevice(path,&dummy);
2265 int yaffs_mount2(const YCHAR *path,int read_only)
2268 int result=YAFFS_FAIL;
2269 struct yaffs_dev *dev=NULL;
2272 yaffsfs_SetError(-EFAULT);
2276 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2278 if(yaffsfs_CheckPath(path) < 0){
2279 yaffsfs_SetError(-ENAMETOOLONG);
2285 yaffsfs_InitHandles();
2287 dev = yaffsfs_FindMountPoint(path);
2289 if(!dev->is_mounted){
2290 dev->read_only = read_only ? 1 : 0;
2291 result = yaffs_guts_initialise(dev);
2292 if(result == YAFFS_FAIL)
2293 yaffsfs_SetError(-ENOMEM);
2294 retVal = result ? 0 : -1;
2298 yaffsfs_SetError(-EBUSY);
2300 yaffsfs_SetError(-ENODEV);
2307 int yaffs_mount(const YCHAR *path)
2309 return yaffs_mount2(path,0);
2312 int yaffs_sync(const YCHAR *path)
2315 struct yaffs_dev *dev=NULL;
2319 yaffsfs_SetError(-EFAULT);
2323 if(yaffsfs_CheckPath(path) < 0){
2324 yaffsfs_SetError(-ENAMETOOLONG);
2329 dev = yaffsfs_FindDevice(path,&dummy);
2331 if(!dev->is_mounted)
2332 yaffsfs_SetError(-EINVAL);
2333 else if(dev->read_only)
2334 yaffsfs_SetError(-EROFS);
2337 yaffs_flush_whole_cache(dev);
2338 yaffs_checkpoint_save(dev);
2343 yaffsfs_SetError(-ENODEV);
2350 int yaffs_remount(const YCHAR *path, int force, int read_only)
2353 struct yaffs_dev *dev=NULL;
2357 yaffsfs_SetError(-EFAULT);
2361 if(yaffsfs_CheckPath(path) < 0){
2362 yaffsfs_SetError(-ENAMETOOLONG);
2367 dev = yaffsfs_FindMountPoint(path);
2369 if(dev->is_mounted){
2373 yaffs_flush_whole_cache(dev);
2375 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2376 yh = & yaffsfs_handle[i];
2377 if(yh->useCount>0 &&
2378 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2379 inUse = 1; /* the device is in use, can't unmount */
2382 if(!inUse || force){
2384 yaffs_checkpoint_save(dev);
2385 dev->read_only = read_only ? 1 : 0;
2388 yaffsfs_SetError(-EBUSY);
2391 yaffsfs_SetError(-EINVAL);
2395 yaffsfs_SetError(-ENODEV);
2402 int yaffs_unmount2(const YCHAR *path, int force)
2405 struct yaffs_dev *dev=NULL;
2408 yaffsfs_SetError(-EFAULT);
2412 if(yaffsfs_CheckPath(path) < 0){
2413 yaffsfs_SetError(-ENAMETOOLONG);
2418 dev = yaffsfs_FindMountPoint(path);
2420 if(dev->is_mounted){
2424 yaffs_flush_whole_cache(dev);
2425 yaffs_checkpoint_save(dev);
2427 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2428 if(yaffsfs_handle[i].useCount > 0 &&
2429 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2430 inUse = 1; /* the device is in use, can't unmount */
2433 if(!inUse || force){
2435 yaffsfs_BreakDeviceHandles(dev);
2436 yaffs_deinitialise(dev);
2440 yaffsfs_SetError(-EBUSY);
2443 yaffsfs_SetError(-EINVAL);
2446 yaffsfs_SetError(-ENODEV);
2453 int yaffs_unmount(const YCHAR *path)
2455 return yaffs_unmount2(path,0);
2458 loff_t yaffs_freespace(const YCHAR *path)
2461 struct yaffs_dev *dev=NULL;
2465 yaffsfs_SetError(-EFAULT);
2469 if(yaffsfs_CheckPath(path) < 0){
2470 yaffsfs_SetError(-ENAMETOOLONG);
2475 dev = yaffsfs_FindDevice(path,&dummy);
2476 if(dev && dev->is_mounted){
2477 retVal = yaffs_get_n_free_chunks(dev);
2478 retVal *= dev->data_bytes_per_chunk;
2481 yaffsfs_SetError(-EINVAL);
2487 loff_t yaffs_totalspace(const YCHAR *path)
2490 struct yaffs_dev *dev=NULL;
2494 yaffsfs_SetError(-EFAULT);
2498 if(yaffsfs_CheckPath(path) < 0){
2499 yaffsfs_SetError(-ENAMETOOLONG);
2504 dev = yaffsfs_FindDevice(path,&dummy);
2505 if(dev && dev->is_mounted){
2506 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2507 dev->param.n_reserved_blocks;
2508 retVal *= dev->param.chunks_per_block;
2509 retVal *= dev->data_bytes_per_chunk;
2512 yaffsfs_SetError(-EINVAL);
2518 int yaffs_inodecount(const YCHAR *path)
2521 struct yaffs_dev *dev=NULL;
2525 yaffsfs_SetError(-EFAULT);
2529 if(yaffsfs_CheckPath(path) < 0){
2530 yaffsfs_SetError(-ENAMETOOLONG);
2535 dev = yaffsfs_FindDevice(path,&dummy);
2536 if(dev && dev->is_mounted) {
2537 int n_obj = dev->n_obj;
2538 if(n_obj > dev->n_hardlinks)
2539 retVal = n_obj - dev->n_hardlinks;
2543 yaffsfs_SetError(-EINVAL);
2550 void yaffs_add_device(struct yaffs_dev *dev)
2552 dev->is_mounted = 0;
2553 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2555 if(!dev->dev_list.next)
2556 INIT_LIST_HEAD(&dev->dev_list);
2558 list_add(&dev->dev_list,&yaffsfs_deviceList);
2561 void yaffs_remove_device(struct yaffs_dev *dev)
2563 list_del_init(&dev->dev_list);
2569 /* Directory search stuff. */
2572 * Directory search context
2574 * NB this is an opaque structure.
2581 yaffs_dirent de; /* directory entry being used by this dsc */
2582 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2583 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2584 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2586 struct list_head others;
2587 } yaffsfs_DirectorySearchContext;
2591 static struct list_head search_contexts;
2594 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2598 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2602 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2603 dsc->nextReturn = NULL;
2605 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2606 struct yaffs_obj,siblings);
2608 /* Hey someone isn't playing nice! */
2612 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2616 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2618 if( dsc->nextReturn == NULL ||
2619 list_empty(&dsc->dirObj->variant.dir_variant.children))
2620 dsc->nextReturn = NULL;
2622 struct list_head *next = dsc->nextReturn->siblings.next;
2624 if( next == &dsc->dirObj->variant.dir_variant.children)
2625 dsc->nextReturn = NULL; /* end of list */
2627 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2630 /* Hey someone isn't playing nice! */
2634 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2637 struct list_head *i;
2638 yaffsfs_DirectorySearchContext *dsc;
2640 /* if search contexts not initilised then skip */
2641 if(!search_contexts.next)
2644 /* Iterate through the directory search contexts.
2645 * If any are the one being removed, then advance the dsc to
2646 * the next one to prevent a hanging ptr.
2648 list_for_each(i, &search_contexts) {
2650 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2651 if(dsc->nextReturn == obj)
2652 yaffsfs_DirAdvance(dsc);
2658 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2660 yaffs_DIR *dir = NULL;
2661 struct yaffs_obj *obj = NULL;
2662 yaffsfs_DirectorySearchContext *dsc = NULL;
2667 yaffsfs_SetError(-EFAULT);
2671 if(yaffsfs_CheckPath(dirname) < 0){
2672 yaffsfs_SetError(-ENAMETOOLONG);
2678 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2681 yaffsfs_SetError(-ENOTDIR);
2683 yaffsfs_SetError(-ELOOP);
2685 yaffsfs_SetError(-ENOENT);
2686 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2687 yaffsfs_SetError(-ENOTDIR);
2690 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2691 dir = (yaffs_DIR *)dsc;
2694 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2695 dsc->magic = YAFFS_MAGIC;
2697 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2698 INIT_LIST_HEAD(&dsc->others);
2700 if(!search_contexts.next)
2701 INIT_LIST_HEAD(&search_contexts);
2703 list_add(&dsc->others,&search_contexts);
2704 yaffsfs_SetDirRewound(dsc);
2714 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2716 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2717 struct yaffs_dirent *retVal = NULL;
2721 if(dsc && dsc->magic == YAFFS_MAGIC){
2722 yaffsfs_SetError(0);
2723 if(dsc->nextReturn){
2724 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2725 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2726 dsc->de.d_off = dsc->offset++;
2727 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2728 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2730 /* this should not happen! */
2731 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2733 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2735 yaffsfs_DirAdvance(dsc);
2739 yaffsfs_SetError(-EBADF);
2748 void yaffs_rewinddir(yaffs_DIR *dirp)
2750 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2754 yaffsfs_SetDirRewound(dsc);
2760 int yaffs_closedir(yaffs_DIR *dirp)
2762 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2765 yaffsfs_SetError(-EFAULT);
2771 list_del(&dsc->others); /* unhook from list */
2777 /* End of directory stuff */
2780 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2782 struct yaffs_obj *parent = NULL;
2783 struct yaffs_obj *obj;
2786 int mode = 0; /* ignore for now */
2790 if(!oldpath || !newpath){
2791 yaffsfs_SetError(-EFAULT);
2795 if(yaffsfs_CheckPath(newpath) < 0 ||
2796 yaffsfs_CheckPath(oldpath) < 0){
2797 yaffsfs_SetError(-ENAMETOOLONG);
2802 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2803 if(!parent && notDir)
2804 yaffsfs_SetError(-ENOTDIR);
2806 yaffsfs_SetError(-ELOOP);
2807 else if( !parent || yaffs_strnlen(name,5) < 1)
2808 yaffsfs_SetError(-ENOENT);
2809 else if(parent->my_dev->read_only)
2810 yaffsfs_SetError(-EROFS);
2812 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2815 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2816 yaffsfs_SetError(-EEXIST);
2818 yaffsfs_SetError(-ENOSPC);
2827 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2829 struct yaffs_obj *obj = NULL;
2830 struct yaffs_obj *dir = NULL;
2836 yaffsfs_SetError(-EFAULT);
2842 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2845 yaffsfs_SetError(-ENOTDIR);
2847 yaffsfs_SetError(-ELOOP);
2848 else if(!dir || !obj)
2849 yaffsfs_SetError(-ENOENT);
2850 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2851 yaffsfs_SetError(-EINVAL);
2853 YCHAR *alias = obj->variant.symlink_variant.alias;
2854 memset(buf,0,bufsiz);
2855 yaffs_strncpy(buf,alias,bufsiz - 1);
2862 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2864 /* Creates a link called newpath to existing oldpath */
2865 struct yaffs_obj *obj = NULL;
2866 struct yaffs_obj *lnk = NULL;
2867 struct yaffs_obj *obj_dir = NULL;
2868 struct yaffs_obj *lnk_dir = NULL;
2876 if(!oldpath || !linkpath){
2877 yaffsfs_SetError(-EFAULT);
2881 if(yaffsfs_CheckPath(linkpath) < 0 ||
2882 yaffsfs_CheckPath(oldpath) < 0){
2883 yaffsfs_SetError(-ENAMETOOLONG);
2889 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
2890 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2891 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
2893 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2894 yaffsfs_SetError(-ENOTDIR);
2895 else if(objLoop || lnkLoop)
2896 yaffsfs_SetError(-ELOOP);
2897 else if(!obj_dir || !lnk_dir || !obj)
2898 yaffsfs_SetError(-ENOENT);
2899 else if(obj->my_dev->read_only)
2900 yaffsfs_SetError(-EROFS);
2902 yaffsfs_SetError(-EEXIST);
2903 else if(lnk_dir->my_dev != obj->my_dev)
2904 yaffsfs_SetError(-EXDEV);
2906 retVal = yaffsfs_CheckNameLength(newname);
2909 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2913 yaffsfs_SetError(-ENOSPC);
2923 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2929 yaffsfs_SetError(-EINVAL);
2935 * D E B U G F U N C T I O N S
2940 * Returns number of handles attached to the object
2942 int yaffs_n_handles(const YCHAR *path)
2944 struct yaffs_obj *obj;
2947 yaffsfs_SetError(-EFAULT);
2951 if(yaffsfs_CheckPath(path) < 0){
2952 yaffsfs_SetError(-ENAMETOOLONG);
2956 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2959 return yaffsfs_CountHandles(obj);
2964 int yaffs_get_error(void)
2966 return yaffsfs_GetLastError();
2969 int yaffs_set_error(int error)
2971 yaffsfs_SetError(error);
2975 int yaffs_dump_dev(const YCHAR *path)
2982 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2985 struct yaffs_dev *dev = obj->my_dev;
2988 "n_page_writes.......... %d\n"
2989 "n_page_reads........... %d\n"
2990 "n_erasures....... %d\n"
2991 "n_gc_copies............ %d\n"
2992 "garbageCollections... %d\n"
2993 "passiveGarbageColl'ns %d\n"
2999 dev->garbageCollections,
3000 dev->passiveGarbageCollections