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,
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,int symDepth)
502 obj = yaffs_get_equivalent_obj(obj);
504 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
505 YCHAR *alias = obj->variant.symlink_variant.alias;
507 if(yaffsfs_IsPathDivider(*alias))
508 /* Starts with a /, need to scan from root up */
509 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL,NULL);
511 /* Relative to here, so use the parent of the symlink as a start */
512 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL,NULL);
519 * yaffsfs_FindDirectory
520 * Parse a path to determine the directory and the name within the directory.
522 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
524 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
525 const YCHAR *path, YCHAR **name, int symDepth,
528 struct yaffs_obj *dir;
530 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
536 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
541 restOfPath = (YCHAR *)path;
544 dir = yaffsfs_FindRoot(path,&restOfPath);
550 * curve ball: also throw away surplus '/'
551 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
553 while(yaffsfs_IsPathDivider(*restOfPath))
554 restOfPath++; /* get rid of '/' */
559 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
560 if (i < YAFFS_MAX_NAME_LENGTH){
561 str[i] = *restOfPath;
569 /* got to the end of the string */
572 if(yaffs_strcmp(str,_Y(".")) == 0)
576 else if(yaffs_strcmp(str,_Y("..")) == 0)
579 dir = yaffs_find_by_name(dir,str);
581 dir = yaffsfs_FollowLink(dir,symDepth);
583 if(dir && dir->variant_type !=
584 YAFFS_OBJECT_TYPE_DIRECTORY){
593 /* directory did not exist. */
597 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDirectory,
603 return yaffsfs_DoFindDirectory(relDirectory,path,name,symDepth,notDir);
607 * yaffsfs_FindObject turns a path for an existing object into the object
609 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
610 const YCHAR *path,int symDepth, int getEquiv,
611 struct yaffs_obj **dirOut, int *notDir)
613 struct yaffs_obj *dir;
614 struct yaffs_obj *obj;
617 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth,notDir);
623 obj = yaffs_find_by_name(dir,name);
628 obj = yaffs_get_equivalent_obj(obj);
634 int yaffs_dup(int fd)
637 yaffsfs_Handle *oldPtr = NULL;
638 yaffsfs_Handle *newPtr = NULL;
642 oldPtr = yaffsfs_GetHandlePointer(fd);
643 if(oldPtr && oldPtr->useCount > 0)
644 newHandle = yaffsfs_GetNewHandle();
646 newPtr = yaffsfs_GetHandlePointer(newHandle);
654 yaffsfs_SetError(-EBADF);
656 yaffsfs_SetError(-ENOMEM);
664 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
666 struct yaffs_obj *obj = NULL;
667 struct yaffs_obj *dir = NULL;
670 yaffsfs_Handle *yh = NULL;
673 int errorReported = 0;
674 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
675 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
676 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
677 u8 sharedReadAllowed;
678 u8 sharedWriteAllowed;
684 if(yaffsfs_CheckPath(path) < 0){
685 yaffsfs_SetError(-ENAMETOOLONG);
689 /* O_EXCL only has meaning if O_CREAT is specified */
690 if(!(oflag & O_CREAT))
693 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
694 if( (oflag & O_CREAT) & (oflag & O_EXCL))
697 /* Todo: Are there any more flag combos to sanitise ? */
699 /* Figure out if reading or writing is requested */
701 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
702 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
706 handle = yaffsfs_GetNewHandle();
709 yaffsfs_SetError(-ENFILE);
713 yh = yaffsfs_GetHandlePointer(handle);
715 /* try to find the exisiting object */
716 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL);
718 obj = yaffsfs_FollowLink(obj,symDepth++);
721 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
722 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
728 /* The file already exists or it might be a directory */
730 /* If it is a directory then we can't open it as a file */
731 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
733 yaffsfs_SetError(-EISDIR);
737 /* Open should fail if O_CREAT and O_EXCL are specified since
740 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
742 yaffsfs_SetError(-EEXIST);
746 /* Check file permissions */
747 if( readRequested && !(obj->yst_mode & S_IREAD))
750 if( writeRequested && !(obj->yst_mode & S_IWRITE))
753 if(openDenied && !errorReported ) {
754 /* Error if the file exists but permissions are refused. */
755 yaffsfs_SetError(-EACCES);
759 /* Check sharing of an existing object. */
763 sharedReadAllowed = 1;
764 sharedWriteAllowed = 1;
767 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
768 hx = &yaffsfs_handle[i];
769 if(hx->useCount > 0 &&
771 yaffsfs_inode[hx->inodeId].iObj == obj){
773 sharedReadAllowed = 0;
775 sharedWriteAllowed = 0;
785 if((!sharedReadAllowed && readRequested)||
786 (!shareRead && alreadyReading) ||
787 (!sharedWriteAllowed && writeRequested) ||
788 (!shareWrite && alreadyWriting)){
790 yaffsfs_SetError(-EBUSY);
797 /* If we could not open an existing object, then let's see if
798 * the directory exists. If not, error.
800 if(!obj && !errorReported){
801 dir = yaffsfs_FindDirectory(NULL,path,&name,0,NULL);
803 yaffsfs_SetError(-ENOTDIR);
808 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
809 /* Let's see if we can create this file if it does not exist. */
810 if(dir->my_dev->read_only){
811 yaffsfs_SetError(-EINVAL);
814 obj = yaffs_create_file(dir,name,mode,0,0);
816 if(!obj && !errorReported){
817 yaffsfs_SetError(-ENOSPC);
822 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
823 /* Error if the file does not exist and CREAT is not set. */
824 yaffsfs_SetError(-ENOENT);
828 if(obj && !openDenied) {
829 int inodeId = yaffsfs_GetInodeIdForObject(obj);
833 * Todo: Fix any problem if inodes run out, though that
834 * can't happen if the number of inode items >= number of handles.
838 yh->inodeId = inodeId;
839 yh->reading = readRequested;
840 yh->writing = writeRequested;
841 yh->append = (oflag & O_APPEND) ? 1 : 0;
843 yh->shareRead = shareRead;
844 yh->shareWrite = shareWrite;
846 /* Hook inode to object */
847 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
849 if((oflag & O_TRUNC) && yh->writing)
850 yaffs_resize_file(obj,0);
852 yaffsfs_PutHandle(handle);
854 yaffsfs_SetError(0); /* Problem */
864 int yaffs_open(const YCHAR *path, int oflag, int mode)
866 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
869 int yaffs_Dofsync(int fd,int datasync)
871 yaffsfs_Handle *h = NULL;
876 h = yaffsfs_GetHandlePointer(fd);
878 if(h && h->useCount > 0)
880 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
883 yaffsfs_SetError(-EBADF);
892 int yaffs_fsync(int fd)
894 return yaffs_Dofsync(fd,0);
897 int yaffs_flush(int fd)
899 return yaffs_fsync(fd);
902 int yaffs_fdatasync(int fd)
904 return yaffs_Dofsync(fd,1);
907 int yaffs_close(int fd)
909 yaffsfs_Handle *h = NULL;
914 h = yaffsfs_GetHandlePointer(fd);
916 if(h && h->useCount > 0) {
918 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
919 yaffsfs_PutHandle(fd);
923 yaffsfs_SetError(-EBADF);
934 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
936 yaffsfs_Handle *h = NULL;
937 struct yaffs_obj *obj = NULL;
944 unsigned int maxRead;
945 u8 *buf = (u8 *)vbuf;
948 h = yaffsfs_GetHandlePointer(fd);
949 obj = yaffsfs_GetHandleObject(fd);
953 yaffsfs_SetError(-EBADF);
955 } else if(!h->reading){
956 /* Not a reading handle */
957 yaffsfs_SetError(-EINVAL);
959 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
960 yaffsfs_SetError(-EINVAL);
966 startPos = h->position;
970 if(yaffs_get_obj_length(obj) > pos)
971 maxRead = yaffs_get_obj_length(obj) - pos;
979 yaffsfs_GetHandle(fd);
981 endPos = pos + nbyte;
983 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
984 nbyte > YAFFS_MAX_FILE_SIZE ||
985 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
991 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
996 * Need to reverify object in case the device was
997 * unmounted in another thread.
999 obj = yaffsfs_GetHandleObject(fd);
1003 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1011 if(nRead == nToRead)
1014 nbyte = 0; /* no more to read */
1024 yaffsfs_PutHandle(fd);
1028 h->position = startPos + totalRead;
1030 yaffsfs_SetError(-EINVAL);
1037 return (totalRead >= 0) ? totalRead : -1;
1041 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1043 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1046 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1048 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1051 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1053 yaffsfs_Handle *h = NULL;
1054 struct yaffs_obj *obj = NULL;
1059 int totalWritten = 0;
1060 int write_trhrough = 0;
1062 const u8 *buf = (const u8 *)vbuf;
1065 h = yaffsfs_GetHandlePointer(fd);
1066 obj = yaffsfs_GetHandleObject(fd);
1070 yaffsfs_SetError(-EBADF);
1072 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1073 yaffsfs_SetError(-EINVAL);
1077 startPos = yaffs_get_obj_length(obj);
1081 startPos = h->position;
1083 yaffsfs_GetHandle(fd);
1085 endPos = pos + nbyte;
1087 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1088 nbyte > YAFFS_MAX_FILE_SIZE ||
1089 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1096 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1097 if(nToWrite > nbyte)
1101 * Need to reverify object in case the device was
1102 * remounted or unmounted in another thread.
1104 obj = yaffsfs_GetHandleObject(fd);
1105 if(!obj || obj->my_dev->read_only)
1108 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1111 totalWritten += nWritten;
1116 if(nWritten == nToWrite)
1121 if(nWritten < 1 && totalWritten < 1){
1122 yaffsfs_SetError(-ENOSPC);
1132 yaffsfs_PutHandle(fd);
1135 if(totalWritten > 0)
1136 h->position = startPos + totalWritten;
1138 yaffsfs_SetError(-EINVAL);
1144 return (totalWritten >= 0) ? totalWritten : -1;
1147 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1149 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1152 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1154 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1158 int yaffs_truncate(const YCHAR *path,off_t new_size)
1160 struct yaffs_obj *obj = NULL;
1161 struct yaffs_obj *dir = NULL;
1162 int result = YAFFS_FAIL;
1165 if(yaffsfs_CheckPath(path) < 0){
1166 yaffsfs_SetError(-ENAMETOOLONG);
1172 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1175 yaffsfs_SetError(-ENOTDIR);
1176 else if(!dir || !obj)
1177 yaffsfs_SetError(-ENOENT);
1178 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1179 yaffsfs_SetError(-EISDIR);
1180 else if(obj->my_dev->read_only)
1181 yaffsfs_SetError(-EACCES);
1182 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1183 yaffsfs_SetError(-EINVAL);
1185 result = yaffs_resize_file(obj,new_size);
1189 return (result) ? 0 : -1;
1192 int yaffs_ftruncate(int fd, off_t new_size)
1194 yaffsfs_Handle *h = NULL;
1195 struct yaffs_obj *obj = NULL;
1199 h = yaffsfs_GetHandlePointer(fd);
1200 obj = yaffsfs_GetHandleObject(fd);
1204 yaffsfs_SetError(-EBADF);
1205 else if(obj->my_dev->read_only)
1206 yaffsfs_SetError(-EACCES);
1207 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1208 yaffsfs_SetError(-EINVAL);
1210 /* resize the file */
1211 result = yaffs_resize_file(obj,new_size);
1214 return (result) ? 0 : -1;
1218 off_t yaffs_lseek(int fd, off_t offset, int whence)
1220 yaffsfs_Handle *h = NULL;
1221 struct yaffs_obj *obj = NULL;
1226 h = yaffsfs_GetHandlePointer(fd);
1227 obj = yaffsfs_GetHandleObject(fd);
1230 yaffsfs_SetError(-EBADF);
1231 else if(offset > YAFFS_MAX_FILE_SIZE)
1232 yaffsfs_SetError(-EINVAL);
1234 if(whence == SEEK_SET){
1237 } else if(whence == SEEK_CUR) {
1238 if( (h->position + offset) >= 0)
1239 pos = (h->position + offset);
1240 } else if(whence == SEEK_END) {
1241 fSize = yaffs_get_obj_length(obj);
1242 if(fSize >= 0 && (fSize + offset) >= 0)
1243 pos = fSize + offset;
1246 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1249 yaffsfs_SetError(-EINVAL);
1260 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1262 struct yaffs_obj *dir = NULL;
1263 struct yaffs_obj *obj = NULL;
1265 int result = YAFFS_FAIL;
1268 if(yaffsfs_CheckPath(path) < 0){
1269 yaffsfs_SetError(-ENAMETOOLONG);
1275 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL);
1276 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir);
1279 yaffsfs_SetError(-ENOTDIR);
1281 yaffsfs_SetError(-ENOENT);
1282 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1283 yaffsfs_SetError(-EINVAL);
1285 yaffsfs_SetError(-ENOENT);
1286 else if(obj->my_dev->read_only)
1287 yaffsfs_SetError(-EINVAL);
1288 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1289 yaffsfs_SetError(-EISDIR);
1290 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1291 yaffsfs_SetError(-ENOTDIR);
1292 else if(isDirectory && obj == obj->my_dev->root_dir)
1293 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1295 result = yaffs_unlinker(dir,name);
1297 if(result == YAFFS_FAIL && isDirectory)
1298 yaffsfs_SetError(-ENOTEMPTY);
1303 return (result == YAFFS_FAIL) ? -1 : 0;
1307 int yaffs_unlink(const YCHAR *path)
1309 return yaffsfs_DoUnlink(path,0);
1312 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1314 struct yaffs_obj *olddir = NULL;
1315 struct yaffs_obj *newdir = NULL;
1316 struct yaffs_obj *obj = NULL;
1317 struct yaffs_obj *newobj = NULL;
1320 int result= YAFFS_FAIL;
1321 int rename_allowed = 1;
1324 YCHAR *alt_newpath=NULL;
1326 if(yaffsfs_CheckPath(newPath) < 0){
1327 yaffsfs_SetError(-ENAMETOOLONG);
1331 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1332 yaffsfs_SetError(-ENOMEM);
1336 newPath = alt_newpath;
1341 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir);
1342 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir);
1343 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL);
1344 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL);
1346 /* If the object being renamed is a directory and the
1347 * path ended with a "/" then the olddir == obj.
1348 * We pass through NULL for the old name to tell the lower layers
1349 * to use olddir as the object.
1355 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1356 yaffsfs_SetError(-ENOTDIR);
1358 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1359 yaffsfs_SetError(-EINVAL);
1361 }else if(!olddir || !newdir || !obj) {
1362 yaffsfs_SetError(-ENOENT);
1364 } else if(obj->my_dev->read_only){
1365 yaffsfs_SetError(-EROFS);
1367 } else if(yaffs_is_non_empty_dir(newobj)){
1368 yaffsfs_SetError(-ENOTEMPTY);
1370 } else if(olddir->my_dev != newdir->my_dev) {
1371 /* Rename must be on same device */
1372 yaffsfs_SetError(-EXDEV);
1374 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1376 * It is a directory, check that it is not being renamed to
1377 * being its own decendent.
1378 * Do this by tracing from the new directory back to the root,
1382 struct yaffs_obj *xx = newdir;
1384 while( rename_allowed && xx){
1390 yaffsfs_SetError(-EINVAL);
1394 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1401 return (result == YAFFS_FAIL) ? -1 : 0;
1405 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1409 obj = yaffs_get_equivalent_obj(obj);
1412 buf->st_dev = (int)obj->my_dev->os_context;
1413 buf->st_ino = obj->obj_id;
1414 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1416 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1417 buf->st_mode |= S_IFDIR;
1418 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1419 buf->st_mode |= S_IFLNK;
1420 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1421 buf->st_mode |= S_IFREG;
1423 buf->st_nlink = yaffs_get_obj_link_count(obj);
1426 buf->st_rdev = obj->yst_rdev;
1427 buf->st_size = yaffs_get_obj_length(obj);
1428 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1429 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1430 #if CONFIG_YAFFS_WINCE
1431 buf->yst_wince_atime[0] = obj->win_atime[0];
1432 buf->yst_wince_atime[1] = obj->win_atime[1];
1433 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1434 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1435 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1436 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1438 buf->yst_atime = obj->yst_atime;
1439 buf->yst_ctime = obj->yst_ctime;
1440 buf->yst_mtime = obj->yst_mtime;
1447 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1449 struct yaffs_obj *obj=NULL;
1450 struct yaffs_obj *dir=NULL;
1454 if(yaffsfs_CheckPath(path) < 0){
1455 yaffsfs_SetError(-ENAMETOOLONG);
1461 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1464 obj = yaffsfs_FollowLink(obj,0);
1467 yaffsfs_SetError(-ENOTDIR);
1468 else if(!dir || !obj)
1469 yaffsfs_SetError(-ENOENT);
1471 retVal = yaffsfs_DoStat(obj,buf);
1479 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1481 return yaffsfs_DoStatOrLStat(path,buf,0);
1484 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1486 return yaffsfs_DoStatOrLStat(path,buf,1);
1489 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1491 struct yaffs_obj *obj;
1496 obj = yaffsfs_GetHandleObject(fd);
1499 retVal = yaffsfs_DoStat(obj,buf);
1502 yaffsfs_SetError(-EBADF);
1509 #ifndef CONFIG_YAFFS_WINCE
1510 /* xattrib functions */
1513 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1515 struct yaffs_obj *obj;
1516 struct yaffs_obj *dir;
1521 if(yaffsfs_CheckPath(path) < 0){
1522 yaffsfs_SetError(-ENAMETOOLONG);
1528 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1531 obj = yaffsfs_FollowLink(obj,0);
1534 yaffsfs_SetError(-ENOTDIR);
1535 else if(!dir || !obj)
1536 yaffsfs_SetError(-ENOENT);
1538 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1540 yaffsfs_SetError(retVal);
1551 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1553 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1556 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1558 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1563 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1565 struct yaffs_obj *obj;
1570 obj = yaffsfs_GetHandleObject(fd);
1573 yaffsfs_SetError(-EBADF);
1575 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1577 yaffsfs_SetError(retVal);
1587 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1589 struct yaffs_obj *obj;
1590 struct yaffs_obj *dir;
1594 if(yaffsfs_CheckPath(path) < 0){
1595 yaffsfs_SetError(-ENAMETOOLONG);
1601 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1604 obj = yaffsfs_FollowLink(obj,0);
1607 yaffsfs_SetError(-ENOTDIR);
1608 else if(!dir || !obj)
1609 yaffsfs_SetError(-ENOENT);
1611 retVal = yaffs_get_xattrib(obj,name,data,size);
1613 yaffsfs_SetError(retVal);
1623 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1625 return yaffs_do_getxattr( path, name, data, size, 1);
1627 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1629 return yaffs_do_getxattr( path, name, data, size, 0);
1634 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1636 struct yaffs_obj *obj;
1641 obj = yaffsfs_GetHandleObject(fd);
1644 retVal = yaffs_get_xattrib(obj,name,data,size);
1646 yaffsfs_SetError(retVal);
1651 yaffsfs_SetError(-EBADF);
1658 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1660 struct yaffs_obj *obj=NULL;
1661 struct yaffs_obj *dir=NULL;
1665 if(yaffsfs_CheckPath(path) < 0){
1666 yaffsfs_SetError(-ENAMETOOLONG);
1672 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1675 obj = yaffsfs_FollowLink(obj,0);
1678 yaffsfs_SetError(-ENOTDIR);
1679 else if(!dir || !obj)
1680 yaffsfs_SetError(-ENOENT);
1682 retVal = yaffs_list_xattrib(obj, data,size);
1684 yaffsfs_SetError(retVal);
1695 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1697 return yaffs_do_listxattr(path, data, size, 1);
1700 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1702 return yaffs_do_listxattr(path, data, size, 0);
1705 int yaffs_flistxattr(int fd, char *data, int size)
1707 struct yaffs_obj *obj;
1712 obj = yaffsfs_GetHandleObject(fd);
1715 retVal = yaffs_list_xattrib(obj,data,size);
1717 yaffsfs_SetError(retVal);
1722 yaffsfs_SetError(-EBADF);
1729 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1731 struct yaffs_obj *obj=NULL;
1732 struct yaffs_obj *dir=NULL;
1736 if(yaffsfs_CheckPath(path) < 0){
1737 yaffsfs_SetError(-ENAMETOOLONG);
1743 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
1746 obj = yaffsfs_FollowLink(obj,0);
1749 yaffsfs_SetError(-ENOTDIR);
1750 else if(!dir || !obj)
1751 yaffsfs_SetError(-ENOENT);
1753 retVal = yaffs_remove_xattrib(obj,name);
1755 yaffsfs_SetError(retVal);
1766 int yaffs_removexattr(const YCHAR *path, const char *name)
1768 return yaffs_do_removexattr(path, name, 1);
1771 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1773 return yaffs_do_removexattr(path, name, 0);
1776 int yaffs_fremovexattr(int fd, const char *name)
1778 struct yaffs_obj *obj;
1783 obj = yaffsfs_GetHandleObject(fd);
1786 retVal = yaffs_remove_xattrib(obj,name);
1788 yaffsfs_SetError(retVal);
1793 yaffsfs_SetError(-EBADF);
1801 #ifdef CONFIG_YAFFS_WINCE
1802 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1804 struct yaffs_obj *obj;
1809 obj = yaffsfs_GetHandleObject(fd);
1814 wctime[0] = obj->win_ctime[0];
1815 wctime[1] = obj->win_ctime[1];
1818 watime[0] = obj->win_atime[0];
1819 watime[1] = obj->win_atime[1];
1822 wmtime[0] = obj->win_mtime[0];
1823 wmtime[1] = obj->win_mtime[1];
1830 yaffsfs_SetError(-EBADF);
1838 int yaffs_set_wince_times(int fd,
1839 const unsigned *wctime,
1840 const unsigned *watime,
1841 const unsigned *wmtime)
1843 struct yaffs_obj *obj;
1848 obj = yaffsfs_GetHandleObject(fd);
1853 obj->win_ctime[0] = wctime[0];
1854 obj->win_ctime[1] = wctime[1];
1857 obj->win_atime[0] = watime[0];
1858 obj->win_atime[1] = watime[1];
1861 obj->win_mtime[0] = wmtime[0];
1862 obj->win_mtime[1] = wmtime[1];
1866 result = yaffs_flush_file(obj,0,0);
1870 yaffsfs_SetError(-EBADF);
1880 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1885 obj = yaffs_get_equivalent_obj(obj);
1888 obj->yst_mode = mode;
1890 result = yaffs_flush_file(obj,0,0);
1893 return result == YAFFS_OK ? 0 : -1;
1897 int yaffs_access(const YCHAR *path, int amode)
1899 struct yaffs_obj *obj=NULL;
1900 struct yaffs_obj *dir=NULL;
1904 if(yaffsfs_CheckPath(path) < 0){
1905 yaffsfs_SetError(-ENAMETOOLONG);
1909 if(amode & ~(R_OK | W_OK | X_OK)){
1910 yaffsfs_SetError(-EINVAL);
1916 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
1917 obj = yaffsfs_FollowLink(obj,0);
1920 yaffsfs_SetError(-ENOTDIR);
1921 else if(!dir || !obj)
1922 yaffsfs_SetError(-ENOENT);
1926 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1928 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1930 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1934 yaffsfs_SetError(-EACCES);
1946 int yaffs_chmod(const YCHAR *path, mode_t mode)
1948 struct yaffs_obj *obj=NULL;
1949 struct yaffs_obj *dir=NULL;
1953 if(yaffsfs_CheckPath(path) < 0){
1954 yaffsfs_SetError(-ENAMETOOLONG);
1959 yaffsfs_SetError(-EINVAL);
1965 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir);
1968 yaffsfs_SetError(-ENOTDIR);
1969 else if(!dir || !obj)
1970 yaffsfs_SetError(-ENOENT);
1971 else if(obj->my_dev->read_only)
1972 yaffsfs_SetError(-EROFS);
1974 retVal = yaffsfs_DoChMod(obj,mode);
1983 int yaffs_fchmod(int fd, mode_t mode)
1985 struct yaffs_obj *obj;
1989 yaffsfs_SetError(-EINVAL);
1994 obj = yaffsfs_GetHandleObject(fd);
1997 yaffsfs_SetError(-EBADF);
1998 else if(obj->my_dev->read_only)
1999 yaffsfs_SetError(-EROFS);
2001 retVal = yaffsfs_DoChMod(obj,mode);
2008 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2010 struct yaffs_obj *parent = NULL;
2011 struct yaffs_obj *dir = NULL;
2013 YCHAR *alt_path = NULL;
2017 if(yaffsfs_CheckPath(path) < 0){
2018 yaffsfs_SetError(-ENAMETOOLONG);
2022 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2023 yaffsfs_SetError(-ENOMEM);
2030 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir);
2031 if(!parent && notDir)
2032 yaffsfs_SetError(-ENOTDIR);
2034 yaffsfs_SetError(-ENOENT);
2035 else if(yaffs_strnlen(name,5) == 0){
2036 /* Trying to make the root itself */
2037 yaffsfs_SetError(-EEXIST);
2038 } else if(parent->my_dev->read_only)
2039 yaffsfs_SetError(-EROFS);
2041 dir = yaffs_create_dir(parent,name,mode,0,0);
2044 else if (yaffs_find_by_name(parent,name))
2045 yaffsfs_SetError(-EEXIST); /* the name already exists */
2047 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2058 int yaffs_rmdir(const YCHAR *path)
2063 if(yaffsfs_CheckPath(path) < 0){
2064 yaffsfs_SetError(-ENAMETOOLONG);
2068 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2069 yaffsfs_SetError(-ENOMEM);
2074 result = yaffsfs_DoUnlink(path,1);
2081 void * yaffs_getdev(const YCHAR *path)
2083 struct yaffs_dev *dev=NULL;
2085 dev = yaffsfs_FindDevice(path,&dummy);
2089 int yaffs_mount2(const YCHAR *path,int read_only)
2092 int result=YAFFS_FAIL;
2093 struct yaffs_dev *dev=NULL;
2095 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2097 if(yaffsfs_CheckPath(path) < 0){
2098 yaffsfs_SetError(-ENAMETOOLONG);
2104 yaffsfs_InitHandles();
2106 dev = yaffsfs_FindMountPoint(path);
2108 if(!dev->is_mounted){
2109 dev->read_only = read_only ? 1 : 0;
2110 result = yaffs_guts_initialise(dev);
2111 if(result == YAFFS_FAIL)
2112 yaffsfs_SetError(-ENOMEM);
2113 retVal = result ? 0 : -1;
2117 yaffsfs_SetError(-EBUSY);
2119 yaffsfs_SetError(-ENODEV);
2126 int yaffs_mount(const YCHAR *path)
2128 return yaffs_mount2(path,0);
2131 int yaffs_sync(const YCHAR *path)
2134 struct yaffs_dev *dev=NULL;
2137 if(yaffsfs_CheckPath(path) < 0){
2138 yaffsfs_SetError(-ENAMETOOLONG);
2143 dev = yaffsfs_FindDevice(path,&dummy);
2145 if(dev->is_mounted){
2147 yaffs_flush_whole_cache(dev);
2148 yaffs_checkpoint_save(dev);
2152 yaffsfs_SetError(-EINVAL);
2155 yaffsfs_SetError(-ENODEV);
2162 int yaffs_remount(const YCHAR *path, int force, int read_only)
2165 struct yaffs_dev *dev=NULL;
2168 if(yaffsfs_CheckPath(path) < 0){
2169 yaffsfs_SetError(-ENAMETOOLONG);
2174 dev = yaffsfs_FindMountPoint(path);
2176 if(dev->is_mounted){
2180 yaffs_flush_whole_cache(dev);
2182 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2183 yh = & yaffsfs_handle[i];
2184 if(yh->useCount>0 &&
2185 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2186 inUse = 1; /* the device is in use, can't unmount */
2189 if(!inUse || force){
2191 yaffs_checkpoint_save(dev);
2192 dev->read_only = read_only ? 1 : 0;
2195 yaffsfs_SetError(-EBUSY);
2198 yaffsfs_SetError(-EINVAL);
2202 yaffsfs_SetError(-ENODEV);
2209 int yaffs_unmount2(const YCHAR *path, int force)
2212 struct yaffs_dev *dev=NULL;
2214 if(yaffsfs_CheckPath(path) < 0){
2215 yaffsfs_SetError(-ENAMETOOLONG);
2220 dev = yaffsfs_FindMountPoint(path);
2222 if(dev->is_mounted){
2226 yaffs_flush_whole_cache(dev);
2227 yaffs_checkpoint_save(dev);
2229 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2230 if(yaffsfs_handle[i].useCount > 0 &&
2231 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2232 inUse = 1; /* the device is in use, can't unmount */
2235 if(!inUse || force){
2237 yaffsfs_BreakDeviceHandles(dev);
2238 yaffs_deinitialise(dev);
2242 yaffsfs_SetError(-EBUSY);
2245 yaffsfs_SetError(-EINVAL);
2248 yaffsfs_SetError(-ENODEV);
2255 int yaffs_unmount(const YCHAR *path)
2257 return yaffs_unmount2(path,0);
2260 loff_t yaffs_freespace(const YCHAR *path)
2263 struct yaffs_dev *dev=NULL;
2266 if(yaffsfs_CheckPath(path) < 0){
2267 yaffsfs_SetError(-ENAMETOOLONG);
2272 dev = yaffsfs_FindDevice(path,&dummy);
2273 if(dev && dev->is_mounted){
2274 retVal = yaffs_get_n_free_chunks(dev);
2275 retVal *= dev->data_bytes_per_chunk;
2278 yaffsfs_SetError(-EINVAL);
2284 loff_t yaffs_totalspace(const YCHAR *path)
2287 struct yaffs_dev *dev=NULL;
2290 if(yaffsfs_CheckPath(path) < 0){
2291 yaffsfs_SetError(-ENAMETOOLONG);
2296 dev = yaffsfs_FindDevice(path,&dummy);
2297 if(dev && dev->is_mounted){
2298 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2299 dev->param.n_reserved_blocks;
2300 retVal *= dev->param.chunks_per_block;
2301 retVal *= dev->data_bytes_per_chunk;
2304 yaffsfs_SetError(-EINVAL);
2310 int yaffs_inodecount(const YCHAR *path)
2313 struct yaffs_dev *dev=NULL;
2316 if(yaffsfs_CheckPath(path) < 0){
2317 yaffsfs_SetError(-ENAMETOOLONG);
2322 dev = yaffsfs_FindDevice(path,&dummy);
2323 if(dev && dev->is_mounted) {
2324 int n_obj = dev->n_obj;
2325 if(n_obj > dev->n_hardlinks)
2326 retVal = n_obj - dev->n_hardlinks;
2330 yaffsfs_SetError(-EINVAL);
2337 void yaffs_add_device(struct yaffs_dev *dev)
2339 dev->is_mounted = 0;
2340 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2342 if(!dev->dev_list.next)
2343 INIT_LIST_HEAD(&dev->dev_list);
2345 list_add(&dev->dev_list,&yaffsfs_deviceList);
2348 void yaffs_remove_device(struct yaffs_dev *dev)
2350 list_del_init(&dev->dev_list);
2356 /* Directory search stuff. */
2359 * Directory search context
2361 * NB this is an opaque structure.
2368 yaffs_dirent de; /* directory entry being used by this dsc */
2369 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2370 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2371 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2373 struct list_head others;
2374 } yaffsfs_DirectorySearchContext;
2378 static struct list_head search_contexts;
2381 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2385 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2389 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2390 dsc->nextReturn = NULL;
2392 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2393 struct yaffs_obj,siblings);
2395 /* Hey someone isn't playing nice! */
2399 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2403 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2405 if( dsc->nextReturn == NULL ||
2406 list_empty(&dsc->dirObj->variant.dir_variant.children))
2407 dsc->nextReturn = NULL;
2409 struct list_head *next = dsc->nextReturn->siblings.next;
2411 if( next == &dsc->dirObj->variant.dir_variant.children)
2412 dsc->nextReturn = NULL; /* end of list */
2414 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2417 /* Hey someone isn't playing nice! */
2421 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2424 struct list_head *i;
2425 yaffsfs_DirectorySearchContext *dsc;
2427 /* if search contexts not initilised then skip */
2428 if(!search_contexts.next)
2431 /* Iterate through the directory search contexts.
2432 * If any are the one being removed, then advance the dsc to
2433 * the next one to prevent a hanging ptr.
2435 list_for_each(i, &search_contexts) {
2437 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2438 if(dsc->nextReturn == obj)
2439 yaffsfs_DirAdvance(dsc);
2445 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2447 yaffs_DIR *dir = NULL;
2448 struct yaffs_obj *obj = NULL;
2449 yaffsfs_DirectorySearchContext *dsc = NULL;
2452 if(yaffsfs_CheckPath(dirname) < 0){
2453 yaffsfs_SetError(-ENAMETOOLONG);
2459 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir);
2462 yaffsfs_SetError(-ENOTDIR);
2464 yaffsfs_SetError(-ENOENT);
2465 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2466 yaffsfs_SetError(-ENOTDIR);
2469 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2470 dir = (yaffs_DIR *)dsc;
2473 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2474 dsc->magic = YAFFS_MAGIC;
2476 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2477 INIT_LIST_HEAD(&dsc->others);
2479 if(!search_contexts.next)
2480 INIT_LIST_HEAD(&search_contexts);
2482 list_add(&dsc->others,&search_contexts);
2483 yaffsfs_SetDirRewound(dsc);
2493 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2495 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2496 struct yaffs_dirent *retVal = NULL;
2500 if(dsc && dsc->magic == YAFFS_MAGIC){
2501 yaffsfs_SetError(0);
2502 if(dsc->nextReturn){
2503 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2504 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2505 dsc->de.d_off = dsc->offset++;
2506 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2507 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2509 /* this should not happen! */
2510 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2512 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2514 yaffsfs_DirAdvance(dsc);
2518 yaffsfs_SetError(-EBADF);
2527 void yaffs_rewinddir(yaffs_DIR *dirp)
2529 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2533 yaffsfs_SetDirRewound(dsc);
2539 int yaffs_closedir(yaffs_DIR *dirp)
2541 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2545 list_del(&dsc->others); /* unhook from list */
2551 /* End of directory stuff */
2554 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2556 struct yaffs_obj *parent = NULL;
2557 struct yaffs_obj *obj;
2560 int mode = 0; /* ignore for now */
2563 if(yaffsfs_CheckPath(newpath) < 0){
2564 yaffsfs_SetError(-ENAMETOOLONG);
2568 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir);
2569 if(!parent && notDir)
2570 yaffsfs_SetError(-ENOTDIR);
2571 else if( !parent || yaffs_strnlen(name,5) < 1)
2572 yaffsfs_SetError(-ENOENT);
2573 else if(parent->my_dev->read_only)
2574 yaffsfs_SetError(-EROFS);
2576 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2579 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL))
2580 yaffsfs_SetError(-EEXIST);
2582 yaffsfs_SetError(-ENOSPC);
2591 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2593 struct yaffs_obj *obj = NULL;
2594 struct yaffs_obj *dir = NULL;
2600 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
2603 yaffsfs_SetError(-ENOTDIR);
2604 else if(!dir || !obj)
2605 yaffsfs_SetError(-ENOENT);
2606 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2607 yaffsfs_SetError(-EINVAL);
2609 YCHAR *alias = obj->variant.symlink_variant.alias;
2610 memset(buf,0,bufsiz);
2611 yaffs_strncpy(buf,alias,bufsiz - 1);
2618 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2620 /* Creates a link called newpath to existing oldpath */
2621 struct yaffs_obj *obj = NULL;
2622 struct yaffs_obj *lnk = NULL;
2623 struct yaffs_obj *obj_dir = NULL;
2624 struct yaffs_obj *lnk_dir = NULL;
2630 if(yaffsfs_CheckPath(linkpath) < 0){
2631 yaffsfs_SetError(-ENAMETOOLONG);
2637 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj);
2638 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL);
2639 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk);
2641 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2642 yaffsfs_SetError(-ENOTDIR);
2643 else if(!obj_dir || !lnk_dir || !obj)
2644 yaffsfs_SetError(-ENOENT);
2645 else if(obj->my_dev->read_only)
2646 yaffsfs_SetError(-EINVAL);
2648 yaffsfs_SetError(-EEXIST);
2649 else if(lnk_dir->my_dev != obj->my_dev)
2650 yaffsfs_SetError(-EXDEV);
2652 retVal = yaffsfs_CheckNameLength(newname);
2655 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2659 yaffsfs_SetError(-ENOSPC);
2669 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2675 yaffsfs_SetError(-EINVAL);
2681 * D E B U G F U N C T I O N S
2686 * Returns number of handles attached to the object
2688 int yaffs_n_handles(const YCHAR *path)
2690 struct yaffs_obj *obj;
2692 if(yaffsfs_CheckPath(path) < 0){
2693 yaffsfs_SetError(-ENAMETOOLONG);
2697 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL);
2700 return yaffsfs_CountHandles(obj);
2705 int yaffs_get_error(void)
2707 return yaffsfs_GetLastError();
2710 int yaffs_set_error(int error)
2712 yaffsfs_SetError(error);
2716 int yaffs_dump_dev(const YCHAR *path)
2723 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2726 struct yaffs_dev *dev = obj->my_dev;
2729 "n_page_writes.......... %d\n"
2730 "n_page_reads........... %d\n"
2731 "n_erasures....... %d\n"
2732 "n_gc_copies............ %d\n"
2733 "garbageCollections... %d\n"
2734 "passiveGarbageColl'ns %d\n"
2740 dev->garbageCollections,
2741 dev->passiveGarbageCollections