2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
20 #include <string.h> /* for memset */
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
36 int symDepth, int getEquiv,
37 struct yaffs_obj **dirOut,
38 int *notDir, int *loop);
40 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
42 unsigned int yaffs_wr_attempts;
46 * There are open inodes in yaffsfs_Inode.
47 * There are open handles in yaffsfs_Handle.
49 * Things are structured this way to be like the Linux VFS model
50 * so that interactions with the yaffs guts calls are similar.
51 * That means more common code paths and less special code.
52 * That means better testing etc.
56 int count; /* Number of handles accessing this inode */
57 struct yaffs_obj *iObj;
66 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
67 int useCount:10; /* Use count for this handle */
68 u32 position; /* current position in file */
71 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
72 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
73 static int yaffsfs_handlesInitialised;
76 unsigned yaffs_set_trace(unsigned tm)
78 yaffs_trace_mask = tm;
79 return yaffs_trace_mask;
82 unsigned yaffs_get_trace(void)
84 return yaffs_trace_mask;
89 * Inilitalise handle management on start-up.
92 static void yaffsfs_InitHandles(void)
95 if(yaffsfs_handlesInitialised)
98 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
99 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
100 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
101 yaffsfs_handle[i].inodeId = -1;
104 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
106 if(h < 0 || h >= YAFFSFS_N_HANDLES)
109 return &yaffsfs_handle[h];
112 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
114 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
116 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
117 return &yaffsfs_inode[h->inodeId];
122 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
124 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
133 * yaffsfs_FindInodeIdForObject
134 * Find the inode entry for an object, if it exists.
137 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
143 obj = yaffs_get_equivalent_obj(obj);
145 /* Look for it in open inode table*/
146 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
147 if(yaffsfs_inode[i].iObj == obj)
154 * yaffsfs_GetInodeIdForObject
155 * Grab an inode entry when opening a new inode.
157 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
161 yaffsfs_Inode *in = NULL;
164 obj = yaffs_get_equivalent_obj(obj);
166 ret = yaffsfs_FindInodeIdForObject(obj);
168 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
169 if(!yaffsfs_inode[i].iObj)
174 in = &yaffsfs_inode[ret];
186 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
188 int i = yaffsfs_FindInodeIdForObject(obj);
191 return yaffsfs_inode[i].count;
196 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
198 struct yaffs_obj *obj;
205 obj->my_inode = NULL;
210 static void yaffsfs_PutInode(int inodeId)
212 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
213 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
216 yaffsfs_ReleaseInode(in);
225 * Grab a handle (when opening a file)
228 static int yaffsfs_GetNewHandle(void)
233 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
234 h = yaffsfs_GetHandlePointer(i);
236 /* todo bug: should never happen */
239 memset(h,0,sizeof(yaffsfs_Handle));
250 * Increase use of handle when reading/writing a file
252 static int yaffsfs_GetHandle(int handle)
254 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
256 if(h && h->useCount > 0){
264 * Let go of a handle when closing a file or aborting an open or
265 * ending a read or write.
267 static int yaffsfs_PutHandle(int handle)
269 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
271 if(h && h->useCount > 0){
275 yaffsfs_PutInode(h->inodeId);
283 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
286 struct yaffs_obj *obj;
288 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
289 h = yaffsfs_GetHandlePointer(i);
290 obj = yaffsfs_GetHandleObject(i);
291 if(h && h->useCount>0 && obj && obj->my_dev == dev){
293 yaffsfs_PutInode(h->inodeId);
303 * Stuff to handle names.
307 int yaffsfs_Match(YCHAR a, YCHAR b)
313 int yaffsfs_IsPathDivider(YCHAR ch)
315 const YCHAR *str = YAFFS_PATH_DIVIDERS;
326 int yaffsfs_CheckNameLength(const char *name)
330 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
333 yaffsfs_SetError(-ENOENT);
335 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
336 yaffsfs_SetError(-ENAMETOOLONG);
344 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
346 YCHAR *alt_path = NULL;
351 * We don't have a definition for max path length.
352 * We will use 3 * max name length instead.
355 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
357 /* If the last character is a path divider, then we need to
358 * trim it back so that the name look-up works properly.
359 * eg. /foo/new_dir/ -> /foo/newdir
360 * Curveball: Need to handle multiple path dividers:
361 * eg. /foof/sdfse///// -> /foo/sdfse
363 if(path_length > 0 &&
364 yaffsfs_IsPathDivider(path[path_length-1])){
365 alt_path = YMALLOC(path_length + 1);
368 strcpy(alt_path, path);
369 for(i = path_length-1;
370 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
372 alt_path[i] = (YCHAR) 0;
374 *ret_path = alt_path;
379 LIST_HEAD(yaffsfs_deviceList);
384 * Scan the configuration list to find the device
385 * Curveballs: Should match paths that end in '/' too
386 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
388 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
390 struct list_head *cfg;
391 const YCHAR *leftOver;
393 struct yaffs_dev *retval = NULL;
394 struct yaffs_dev *dev = NULL;
396 int longestMatch = -1;
400 * Check all configs, choose the one that:
401 * 1) Actually matches a prefix (ie /a amd /abc will not match
402 * 2) Matches the longest.
404 list_for_each(cfg, &yaffsfs_deviceList){
405 dev = list_entry(cfg, struct yaffs_dev, dev_list);
412 while(matching && *p && *leftOver){
413 /* Skip over any /s */
414 while(yaffsfs_IsPathDivider(*p))
417 /* Skip over any /s */
418 while(yaffsfs_IsPathDivider(*leftOver))
421 /* Now match the text part */
423 *p && !yaffsfs_IsPathDivider(*p) &&
424 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
425 if(yaffsfs_Match(*p,*leftOver)){
435 /* Skip over any /s in leftOver */
436 while(yaffsfs_IsPathDivider(*leftOver))
439 // Skip over any /s in p
440 while(yaffsfs_IsPathDivider(*p))
443 // p should now be at the end of the string (ie. fully matched)
447 if( matching && (thisMatchLength > longestMatch))
450 *restOfPath = (YCHAR *)leftOver;
452 longestMatch = thisMatchLength;
459 static int yaffsfs_CheckPath(const YCHAR *path)
463 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
464 if(yaffsfs_IsPathDivider(*path)){
472 return (*path) ? -1 : 0;
475 /* FindMountPoint only returns a dev entry if the path is a mount point */
476 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
478 struct yaffs_dev *dev;
479 YCHAR *restOfPath=NULL;
480 dev = yaffsfs_FindDevice(path,&restOfPath);
481 if(dev && restOfPath && *restOfPath)
486 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
489 struct yaffs_dev *dev;
491 dev= yaffsfs_FindDevice(path,restOfPath);
492 if(dev && dev->is_mounted){
493 return dev->root_dir;
498 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
499 int symDepth, int *loop)
503 obj = yaffs_get_equivalent_obj(obj);
505 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
506 YCHAR *alias = obj->variant.symlink_variant.alias;
508 if(yaffsfs_IsPathDivider(*alias))
509 /* Starts with a /, need to scan from root up */
510 obj = yaffsfs_FindObject(NULL,alias,symDepth++,
513 /* Relative to here, so use the parent of the symlink as a start */
514 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
522 * yaffsfs_FindDirectory
523 * Parse a path to determine the directory and the name within the directory.
525 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
527 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
528 const YCHAR *path, YCHAR **name, int symDepth,
529 int *notDir,int *loop)
531 struct yaffs_obj *dir;
533 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
536 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
544 restOfPath = (YCHAR *)path;
547 dir = yaffsfs_FindRoot(path,&restOfPath);
553 * curve ball: also throw away surplus '/'
554 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
556 while(yaffsfs_IsPathDivider(*restOfPath))
557 restOfPath++; /* get rid of '/' */
562 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
563 if (i < YAFFS_MAX_NAME_LENGTH){
564 str[i] = *restOfPath;
572 /* got to the end of the string */
575 if(yaffs_strcmp(str,_Y(".")) == 0)
579 else if(yaffs_strcmp(str,_Y("..")) == 0)
582 dir = yaffs_find_by_name(dir,str);
584 dir = yaffsfs_FollowLink(dir,symDepth,loop);
586 if(dir && dir->variant_type !=
587 YAFFS_OBJECT_TYPE_DIRECTORY){
596 /* directory did not exist. */
600 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
607 return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
611 * yaffsfs_FindObject turns a path for an existing object into the object
613 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
614 const YCHAR *path,int symDepth, int getEquiv,
615 struct yaffs_obj **dirOut, int *notDir,int *loop)
617 struct yaffs_obj *dir;
618 struct yaffs_obj *obj;
621 dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
627 obj = yaffs_find_by_name(dir,name);
632 obj = yaffs_get_equivalent_obj(obj);
638 int yaffs_dup(int fd)
641 yaffsfs_Handle *oldPtr = NULL;
642 yaffsfs_Handle *newPtr = NULL;
646 oldPtr = yaffsfs_GetHandlePointer(fd);
647 if(oldPtr && oldPtr->useCount > 0)
648 newHandle = yaffsfs_GetNewHandle();
650 newPtr = yaffsfs_GetHandlePointer(newHandle);
658 yaffsfs_SetError(-EBADF);
660 yaffsfs_SetError(-ENOMEM);
668 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
670 struct yaffs_obj *obj = NULL;
671 struct yaffs_obj *dir = NULL;
674 yaffsfs_Handle *yh = NULL;
677 int errorReported = 0;
678 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
679 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
680 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
681 u8 sharedReadAllowed;
682 u8 sharedWriteAllowed;
690 if(yaffsfs_CheckPath(path) < 0){
691 yaffsfs_SetError(-ENAMETOOLONG);
695 /* O_EXCL only has meaning if O_CREAT is specified */
696 if(!(oflag & O_CREAT))
699 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
700 if( (oflag & O_CREAT) & (oflag & O_EXCL))
703 /* Todo: Are there any more flag combos to sanitise ? */
705 /* Figure out if reading or writing is requested */
707 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
708 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
712 handle = yaffsfs_GetNewHandle();
715 yaffsfs_SetError(-ENFILE);
719 yh = yaffsfs_GetHandlePointer(handle);
721 /* try to find the exisiting object */
722 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
724 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
727 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
728 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
734 /* The file already exists or it might be a directory */
736 /* If it is a directory then we can't open it as a file */
737 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
739 yaffsfs_SetError(-EISDIR);
743 /* Open should fail if O_CREAT and O_EXCL are specified since
746 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
748 yaffsfs_SetError(-EEXIST);
752 /* Check file permissions */
753 if( readRequested && !(obj->yst_mode & S_IREAD))
756 if( writeRequested && !(obj->yst_mode & S_IWRITE))
759 if( !errorReported && writeRequested &&
760 obj->my_dev->read_only){
762 yaffsfs_SetError(-EROFS);
766 if(openDenied && !errorReported ) {
767 /* Error if the file exists but permissions are refused. */
768 yaffsfs_SetError(-EACCES);
772 /* Check sharing of an existing object. */
776 sharedReadAllowed = 1;
777 sharedWriteAllowed = 1;
780 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
781 hx = &yaffsfs_handle[i];
782 if(hx->useCount > 0 &&
784 yaffsfs_inode[hx->inodeId].iObj == obj){
786 sharedReadAllowed = 0;
788 sharedWriteAllowed = 0;
798 if((!sharedReadAllowed && readRequested)||
799 (!shareRead && alreadyReading) ||
800 (!sharedWriteAllowed && writeRequested) ||
801 (!shareWrite && alreadyWriting)){
803 yaffsfs_SetError(-EBUSY);
810 /* If we could not open an existing object, then let's see if
811 * the directory exists. If not, error.
813 if(!obj && !errorReported){
814 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
816 yaffsfs_SetError(-ENOTDIR);
819 yaffsfs_SetError(-ELOOP);
822 yaffsfs_SetError(-ENOENT);
827 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
828 /* Let's see if we can create this file if it does not exist. */
829 if(dir->my_dev->read_only){
830 yaffsfs_SetError(-EROFS);
833 obj = yaffs_create_file(dir,name,mode,0,0);
835 if(!obj && !errorReported){
836 yaffsfs_SetError(-ENOSPC);
841 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
842 /* Error if the file does not exist and CREAT is not set. */
843 yaffsfs_SetError(-ENOENT);
847 if(obj && !openDenied) {
848 int inodeId = yaffsfs_GetInodeIdForObject(obj);
852 * Todo: Fix any problem if inodes run out, though that
853 * can't happen if the number of inode items >= number of handles.
857 yh->inodeId = inodeId;
858 yh->reading = readRequested;
859 yh->writing = writeRequested;
860 yh->append = (oflag & O_APPEND) ? 1 : 0;
862 yh->shareRead = shareRead;
863 yh->shareWrite = shareWrite;
865 /* Hook inode to object */
866 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
868 if((oflag & O_TRUNC) && yh->writing)
869 yaffs_resize_file(obj,0);
871 yaffsfs_PutHandle(handle);
873 yaffsfs_SetError(0); /* Problem */
883 int yaffs_open(const YCHAR *path, int oflag, int mode)
885 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
888 int yaffs_Dofsync(int fd,int datasync)
890 yaffsfs_Handle *h = NULL;
895 h = yaffsfs_GetHandlePointer(fd);
897 if(!h || h->useCount < 1)
898 yaffsfs_SetError(-EBADF);
900 struct yaffs_obj * obj;
901 obj = yaffsfs_inode[h->inodeId].iObj;
902 if(obj->my_dev->read_only)
903 yaffsfs_SetError(-EROFS);
905 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
915 int yaffs_fsync(int fd)
917 return yaffs_Dofsync(fd,0);
920 int yaffs_flush(int fd)
922 return yaffs_fsync(fd);
925 int yaffs_fdatasync(int fd)
927 return yaffs_Dofsync(fd,1);
930 int yaffs_close(int fd)
932 yaffsfs_Handle *h = NULL;
937 h = yaffsfs_GetHandlePointer(fd);
939 if(h && h->useCount > 0) {
941 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
942 yaffsfs_PutHandle(fd);
946 yaffsfs_SetError(-EBADF);
957 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
959 yaffsfs_Handle *h = NULL;
960 struct yaffs_obj *obj = NULL;
967 unsigned int maxRead;
968 u8 *buf = (u8 *)vbuf;
971 h = yaffsfs_GetHandlePointer(fd);
972 obj = yaffsfs_GetHandleObject(fd);
976 yaffsfs_SetError(-EBADF);
978 } else if(!h->reading){
979 /* Not a reading handle */
980 yaffsfs_SetError(-EINVAL);
982 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
983 yaffsfs_SetError(-EINVAL);
989 startPos = h->position;
993 if(yaffs_get_obj_length(obj) > pos)
994 maxRead = yaffs_get_obj_length(obj) - pos;
1002 yaffsfs_GetHandle(fd);
1004 endPos = pos + nbyte;
1006 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1007 nbyte > YAFFS_MAX_FILE_SIZE ||
1008 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1014 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1019 * Need to reverify object in case the device was
1020 * unmounted in another thread.
1022 obj = yaffsfs_GetHandleObject(fd);
1026 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1034 if(nRead == nToRead)
1037 nbyte = 0; /* no more to read */
1047 yaffsfs_PutHandle(fd);
1051 h->position = startPos + totalRead;
1053 yaffsfs_SetError(-EINVAL);
1060 return (totalRead >= 0) ? totalRead : -1;
1064 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1066 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1069 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1071 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1074 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1076 yaffsfs_Handle *h = NULL;
1077 struct yaffs_obj *obj = NULL;
1082 int totalWritten = 0;
1083 int write_trhrough = 0;
1085 const u8 *buf = (const u8 *)vbuf;
1088 h = yaffsfs_GetHandlePointer(fd);
1089 obj = yaffsfs_GetHandleObject(fd);
1093 yaffsfs_SetError(-EBADF);
1095 } else if(!h->writing){
1096 yaffsfs_SetError(-EINVAL);
1098 } else if(obj->my_dev->read_only){
1099 yaffsfs_SetError(-EROFS);
1103 startPos = yaffs_get_obj_length(obj);
1107 startPos = h->position;
1109 yaffsfs_GetHandle(fd);
1111 endPos = pos + nbyte;
1113 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1114 nbyte > YAFFS_MAX_FILE_SIZE ||
1115 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1122 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1123 if(nToWrite > nbyte)
1127 * Need to reverify object in case the device was
1128 * remounted or unmounted in another thread.
1130 obj = yaffsfs_GetHandleObject(fd);
1131 if(!obj || obj->my_dev->read_only)
1134 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1137 totalWritten += nWritten;
1142 if(nWritten == nToWrite)
1147 if(nWritten < 1 && totalWritten < 1){
1148 yaffsfs_SetError(-ENOSPC);
1158 yaffsfs_PutHandle(fd);
1161 if(totalWritten > 0)
1162 h->position = startPos + totalWritten;
1164 yaffsfs_SetError(-EINVAL);
1170 return (totalWritten >= 0) ? totalWritten : -1;
1173 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1175 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1178 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1180 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1184 int yaffs_truncate(const YCHAR *path,off_t new_size)
1186 struct yaffs_obj *obj = NULL;
1187 struct yaffs_obj *dir = NULL;
1188 int result = YAFFS_FAIL;
1192 if(yaffsfs_CheckPath(path) < 0){
1193 yaffsfs_SetError(-ENAMETOOLONG);
1199 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1200 obj = yaffsfs_FollowLink(obj,0,&loop);
1203 yaffsfs_SetError(-ENOTDIR);
1205 yaffsfs_SetError(-ELOOP);
1206 else if(!dir || !obj)
1207 yaffsfs_SetError(-ENOENT);
1208 else if(obj->my_dev->read_only)
1209 yaffsfs_SetError(-EROFS);
1210 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1211 yaffsfs_SetError(-EISDIR);
1212 else if(obj->my_dev->read_only)
1213 yaffsfs_SetError(-EROFS);
1214 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1215 yaffsfs_SetError(-EINVAL);
1217 result = yaffs_resize_file(obj,new_size);
1221 return (result) ? 0 : -1;
1224 int yaffs_ftruncate(int fd, off_t new_size)
1226 yaffsfs_Handle *h = NULL;
1227 struct yaffs_obj *obj = NULL;
1231 h = yaffsfs_GetHandlePointer(fd);
1232 obj = yaffsfs_GetHandleObject(fd);
1236 yaffsfs_SetError(-EBADF);
1237 else if(!h->writing)
1238 yaffsfs_SetError(-EINVAL);
1239 else if(obj->my_dev->read_only)
1240 yaffsfs_SetError(-EROFS);
1241 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1242 yaffsfs_SetError(-EINVAL);
1244 /* resize the file */
1245 result = yaffs_resize_file(obj,new_size);
1248 return (result) ? 0 : -1;
1252 off_t yaffs_lseek(int fd, off_t offset, int whence)
1254 yaffsfs_Handle *h = NULL;
1255 struct yaffs_obj *obj = NULL;
1260 h = yaffsfs_GetHandlePointer(fd);
1261 obj = yaffsfs_GetHandleObject(fd);
1264 yaffsfs_SetError(-EBADF);
1265 else if(offset > YAFFS_MAX_FILE_SIZE)
1266 yaffsfs_SetError(-EINVAL);
1268 if(whence == SEEK_SET){
1271 } else if(whence == SEEK_CUR) {
1272 if( (h->position + offset) >= 0)
1273 pos = (h->position + offset);
1274 } else if(whence == SEEK_END) {
1275 fSize = yaffs_get_obj_length(obj);
1276 if(fSize >= 0 && (fSize + offset) >= 0)
1277 pos = fSize + offset;
1280 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1283 yaffsfs_SetError(-EINVAL);
1294 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1296 struct yaffs_obj *dir = NULL;
1297 struct yaffs_obj *obj = NULL;
1299 int result = YAFFS_FAIL;
1303 if(yaffsfs_CheckPath(path) < 0){
1304 yaffsfs_SetError(-ENAMETOOLONG);
1310 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1311 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
1314 yaffsfs_SetError(-ENOTDIR);
1316 yaffsfs_SetError(-ELOOP);
1318 yaffsfs_SetError(-ENOENT);
1319 else if(yaffs_strncmp(name,_Y("."),2) == 0)
1320 yaffsfs_SetError(-EINVAL);
1322 yaffsfs_SetError(-ENOENT);
1323 else if(obj->my_dev->read_only)
1324 yaffsfs_SetError(-EROFS);
1325 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1326 yaffsfs_SetError(-EISDIR);
1327 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1328 yaffsfs_SetError(-ENOTDIR);
1329 else if(isDirectory && obj == obj->my_dev->root_dir)
1330 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1332 result = yaffs_unlinker(dir,name);
1334 if(result == YAFFS_FAIL && isDirectory)
1335 yaffsfs_SetError(-ENOTEMPTY);
1340 return (result == YAFFS_FAIL) ? -1 : 0;
1344 int yaffs_unlink(const YCHAR *path)
1346 return yaffsfs_DoUnlink(path,0);
1349 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1351 struct yaffs_obj *olddir = NULL;
1352 struct yaffs_obj *newdir = NULL;
1353 struct yaffs_obj *obj = NULL;
1354 struct yaffs_obj *newobj = NULL;
1357 int result= YAFFS_FAIL;
1358 int rename_allowed = 1;
1364 YCHAR *alt_newpath=NULL;
1366 if(yaffsfs_CheckPath(newPath) < 0){
1367 yaffsfs_SetError(-ENAMETOOLONG);
1371 if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1372 yaffsfs_SetError(-ENOMEM);
1376 newPath = alt_newpath;
1381 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir,&oldLoop);
1382 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir,&newLoop);
1383 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1384 newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1386 /* If the object being renamed is a directory and the
1387 * path ended with a "/" then the olddir == obj.
1388 * We pass through NULL for the old name to tell the lower layers
1389 * to use olddir as the object.
1395 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1396 yaffsfs_SetError(-ENOTDIR);
1398 } else if(oldLoop || newLoop) {
1399 yaffsfs_SetError(-ELOOP);
1401 } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1402 yaffsfs_SetError(-EINVAL);
1404 }else if(!olddir || !newdir || !obj) {
1405 yaffsfs_SetError(-ENOENT);
1407 } else if(obj->my_dev->read_only){
1408 yaffsfs_SetError(-EROFS);
1410 } else if(yaffs_is_non_empty_dir(newobj)){
1411 yaffsfs_SetError(-ENOTEMPTY);
1413 } else if(olddir->my_dev != newdir->my_dev) {
1414 /* Rename must be on same device */
1415 yaffsfs_SetError(-EXDEV);
1417 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1419 * It is a directory, check that it is not being renamed to
1420 * being its own decendent.
1421 * Do this by tracing from the new directory back to the root,
1425 struct yaffs_obj *xx = newdir;
1427 while( rename_allowed && xx){
1433 yaffsfs_SetError(-EINVAL);
1437 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1444 return (result == YAFFS_FAIL) ? -1 : 0;
1448 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1452 obj = yaffs_get_equivalent_obj(obj);
1455 buf->st_dev = (int)obj->my_dev->os_context;
1456 buf->st_ino = obj->obj_id;
1457 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1459 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1460 buf->st_mode |= S_IFDIR;
1461 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1462 buf->st_mode |= S_IFLNK;
1463 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1464 buf->st_mode |= S_IFREG;
1466 buf->st_nlink = yaffs_get_obj_link_count(obj);
1469 buf->st_rdev = obj->yst_rdev;
1470 buf->st_size = yaffs_get_obj_length(obj);
1471 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1472 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1473 #if CONFIG_YAFFS_WINCE
1474 buf->yst_wince_atime[0] = obj->win_atime[0];
1475 buf->yst_wince_atime[1] = obj->win_atime[1];
1476 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1477 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1478 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1479 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1481 buf->yst_atime = obj->yst_atime;
1482 buf->yst_ctime = obj->yst_ctime;
1483 buf->yst_mtime = obj->yst_mtime;
1490 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1492 struct yaffs_obj *obj=NULL;
1493 struct yaffs_obj *dir=NULL;
1498 if(yaffsfs_CheckPath(path) < 0){
1499 yaffsfs_SetError(-ENAMETOOLONG);
1505 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1508 obj = yaffsfs_FollowLink(obj,0,&loop);
1511 yaffsfs_SetError(-ENOTDIR);
1513 yaffsfs_SetError(-ELOOP);
1514 else if(!dir || !obj)
1515 yaffsfs_SetError(-ENOENT);
1517 retVal = yaffsfs_DoStat(obj,buf);
1525 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1527 return yaffsfs_DoStatOrLStat(path,buf,0);
1530 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1532 return yaffsfs_DoStatOrLStat(path,buf,1);
1535 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1537 struct yaffs_obj *obj;
1542 obj = yaffsfs_GetHandleObject(fd);
1545 retVal = yaffsfs_DoStat(obj,buf);
1548 yaffsfs_SetError(-EBADF);
1555 #ifndef CONFIG_YAFFS_WINCE
1556 /* xattrib functions */
1559 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1561 struct yaffs_obj *obj;
1562 struct yaffs_obj *dir;
1568 if(yaffsfs_CheckPath(path) < 0){
1569 yaffsfs_SetError(-ENAMETOOLONG);
1575 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1578 obj = yaffsfs_FollowLink(obj,0,&loop);
1581 yaffsfs_SetError(-ENOTDIR);
1583 yaffsfs_SetError(-ELOOP);
1584 else if(!dir || !obj)
1585 yaffsfs_SetError(-ENOENT);
1587 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1589 yaffsfs_SetError(retVal);
1600 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1602 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1605 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1607 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1612 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1614 struct yaffs_obj *obj;
1619 obj = yaffsfs_GetHandleObject(fd);
1622 yaffsfs_SetError(-EBADF);
1624 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1626 yaffsfs_SetError(retVal);
1636 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1638 struct yaffs_obj *obj;
1639 struct yaffs_obj *dir;
1644 if(yaffsfs_CheckPath(path) < 0){
1645 yaffsfs_SetError(-ENAMETOOLONG);
1651 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1654 obj = yaffsfs_FollowLink(obj,0,&loop);
1657 yaffsfs_SetError(-ENOTDIR);
1659 yaffsfs_SetError(-ELOOP);
1660 else if(!dir || !obj)
1661 yaffsfs_SetError(-ENOENT);
1663 retVal = yaffs_get_xattrib(obj,name,data,size);
1665 yaffsfs_SetError(retVal);
1675 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1677 return yaffs_do_getxattr( path, name, data, size, 1);
1679 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1681 return yaffs_do_getxattr( path, name, data, size, 0);
1686 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1688 struct yaffs_obj *obj;
1693 obj = yaffsfs_GetHandleObject(fd);
1696 retVal = yaffs_get_xattrib(obj,name,data,size);
1698 yaffsfs_SetError(retVal);
1703 yaffsfs_SetError(-EBADF);
1710 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1712 struct yaffs_obj *obj=NULL;
1713 struct yaffs_obj *dir=NULL;
1718 if(yaffsfs_CheckPath(path) < 0){
1719 yaffsfs_SetError(-ENAMETOOLONG);
1725 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir,&loop);
1728 obj = yaffsfs_FollowLink(obj,0,&loop);
1731 yaffsfs_SetError(-ENOTDIR);
1733 yaffsfs_SetError(-ELOOP);
1734 else if(!dir || !obj)
1735 yaffsfs_SetError(-ENOENT);
1737 retVal = yaffs_list_xattrib(obj, data,size);
1739 yaffsfs_SetError(retVal);
1750 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1752 return yaffs_do_listxattr(path, data, size, 1);
1755 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1757 return yaffs_do_listxattr(path, data, size, 0);
1760 int yaffs_flistxattr(int fd, char *data, int size)
1762 struct yaffs_obj *obj;
1767 obj = yaffsfs_GetHandleObject(fd);
1770 retVal = yaffs_list_xattrib(obj,data,size);
1772 yaffsfs_SetError(retVal);
1777 yaffsfs_SetError(-EBADF);
1784 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1786 struct yaffs_obj *obj=NULL;
1787 struct yaffs_obj *dir=NULL;
1792 if(yaffsfs_CheckPath(path) < 0){
1793 yaffsfs_SetError(-ENAMETOOLONG);
1799 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1802 obj = yaffsfs_FollowLink(obj,0,&loop);
1805 yaffsfs_SetError(-ENOTDIR);
1807 yaffsfs_SetError(-ELOOP);
1808 else if(!dir || !obj)
1809 yaffsfs_SetError(-ENOENT);
1811 retVal = yaffs_remove_xattrib(obj,name);
1813 yaffsfs_SetError(retVal);
1824 int yaffs_removexattr(const YCHAR *path, const char *name)
1826 return yaffs_do_removexattr(path, name, 1);
1829 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1831 return yaffs_do_removexattr(path, name, 0);
1834 int yaffs_fremovexattr(int fd, const char *name)
1836 struct yaffs_obj *obj;
1841 obj = yaffsfs_GetHandleObject(fd);
1844 retVal = yaffs_remove_xattrib(obj,name);
1846 yaffsfs_SetError(retVal);
1851 yaffsfs_SetError(-EBADF);
1859 #ifdef CONFIG_YAFFS_WINCE
1860 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1862 struct yaffs_obj *obj;
1867 obj = yaffsfs_GetHandleObject(fd);
1872 wctime[0] = obj->win_ctime[0];
1873 wctime[1] = obj->win_ctime[1];
1876 watime[0] = obj->win_atime[0];
1877 watime[1] = obj->win_atime[1];
1880 wmtime[0] = obj->win_mtime[0];
1881 wmtime[1] = obj->win_mtime[1];
1888 yaffsfs_SetError(-EBADF);
1896 int yaffs_set_wince_times(int fd,
1897 const unsigned *wctime,
1898 const unsigned *watime,
1899 const unsigned *wmtime)
1901 struct yaffs_obj *obj;
1906 obj = yaffsfs_GetHandleObject(fd);
1911 obj->win_ctime[0] = wctime[0];
1912 obj->win_ctime[1] = wctime[1];
1915 obj->win_atime[0] = watime[0];
1916 obj->win_atime[1] = watime[1];
1919 obj->win_mtime[0] = wmtime[0];
1920 obj->win_mtime[1] = wmtime[1];
1924 result = yaffs_flush_file(obj,0,0);
1928 yaffsfs_SetError(-EBADF);
1938 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1943 obj = yaffs_get_equivalent_obj(obj);
1946 obj->yst_mode = mode;
1948 result = yaffs_flush_file(obj,0,0);
1951 return result == YAFFS_OK ? 0 : -1;
1955 int yaffs_access(const YCHAR *path, int amode)
1957 struct yaffs_obj *obj=NULL;
1958 struct yaffs_obj *dir=NULL;
1963 if(yaffsfs_CheckPath(path) < 0){
1964 yaffsfs_SetError(-ENAMETOOLONG);
1968 if(amode & ~(R_OK | W_OK | X_OK)){
1969 yaffsfs_SetError(-EINVAL);
1975 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
1976 obj = yaffsfs_FollowLink(obj,0,&loop);
1979 yaffsfs_SetError(-ENOTDIR);
1981 yaffsfs_SetError(-ELOOP);
1982 else if(!dir || !obj)
1983 yaffsfs_SetError(-ENOENT);
1984 else if((amode & W_OK) && obj->my_dev->read_only)
1985 yaffsfs_SetError(-EROFS);
1989 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1991 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1993 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1997 yaffsfs_SetError(-EACCES);
2009 int yaffs_chmod(const YCHAR *path, mode_t mode)
2011 struct yaffs_obj *obj=NULL;
2012 struct yaffs_obj *dir=NULL;
2017 if(yaffsfs_CheckPath(path) < 0){
2018 yaffsfs_SetError(-ENAMETOOLONG);
2023 yaffsfs_SetError(-EINVAL);
2029 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir,&loop);
2030 obj = yaffsfs_FollowLink(obj,0,&loop);
2033 yaffsfs_SetError(-ENOTDIR);
2035 yaffsfs_SetError(-ELOOP);
2036 else if(!dir || !obj)
2037 yaffsfs_SetError(-ENOENT);
2038 else if(obj->my_dev->read_only)
2039 yaffsfs_SetError(-EROFS);
2041 retVal = yaffsfs_DoChMod(obj,mode);
2050 int yaffs_fchmod(int fd, mode_t mode)
2052 struct yaffs_obj *obj;
2056 yaffsfs_SetError(-EINVAL);
2061 obj = yaffsfs_GetHandleObject(fd);
2064 yaffsfs_SetError(-EBADF);
2065 else if(obj->my_dev->read_only)
2066 yaffsfs_SetError(-EROFS);
2068 retVal = yaffsfs_DoChMod(obj,mode);
2075 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2077 struct yaffs_obj *parent = NULL;
2078 struct yaffs_obj *dir = NULL;
2080 YCHAR *alt_path = NULL;
2085 if(yaffsfs_CheckPath(path) < 0){
2086 yaffsfs_SetError(-ENAMETOOLONG);
2090 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2091 yaffsfs_SetError(-ENOMEM);
2098 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir,&loop);
2099 if(!parent && notDir)
2100 yaffsfs_SetError(-ENOTDIR);
2102 yaffsfs_SetError(-ELOOP);
2104 yaffsfs_SetError(-ENOENT);
2105 else if(yaffs_strnlen(name,5) == 0){
2106 /* Trying to make the root itself */
2107 yaffsfs_SetError(-EEXIST);
2108 } else if(parent->my_dev->read_only)
2109 yaffsfs_SetError(-EROFS);
2111 dir = yaffs_create_dir(parent,name,mode,0,0);
2114 else if (yaffs_find_by_name(parent,name))
2115 yaffsfs_SetError(-EEXIST); /* the name already exists */
2117 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2128 int yaffs_rmdir(const YCHAR *path)
2133 if(yaffsfs_CheckPath(path) < 0){
2134 yaffsfs_SetError(-ENAMETOOLONG);
2138 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2139 yaffsfs_SetError(-ENOMEM);
2144 result = yaffsfs_DoUnlink(path,1);
2151 void * yaffs_getdev(const YCHAR *path)
2153 struct yaffs_dev *dev=NULL;
2155 dev = yaffsfs_FindDevice(path,&dummy);
2159 int yaffs_mount2(const YCHAR *path,int read_only)
2162 int result=YAFFS_FAIL;
2163 struct yaffs_dev *dev=NULL;
2165 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2167 if(yaffsfs_CheckPath(path) < 0){
2168 yaffsfs_SetError(-ENAMETOOLONG);
2174 yaffsfs_InitHandles();
2176 dev = yaffsfs_FindMountPoint(path);
2178 if(!dev->is_mounted){
2179 dev->read_only = read_only ? 1 : 0;
2180 result = yaffs_guts_initialise(dev);
2181 if(result == YAFFS_FAIL)
2182 yaffsfs_SetError(-ENOMEM);
2183 retVal = result ? 0 : -1;
2187 yaffsfs_SetError(-EBUSY);
2189 yaffsfs_SetError(-ENODEV);
2196 int yaffs_mount(const YCHAR *path)
2198 return yaffs_mount2(path,0);
2201 int yaffs_sync(const YCHAR *path)
2204 struct yaffs_dev *dev=NULL;
2207 if(yaffsfs_CheckPath(path) < 0){
2208 yaffsfs_SetError(-ENAMETOOLONG);
2213 dev = yaffsfs_FindDevice(path,&dummy);
2215 if(!dev->is_mounted)
2216 yaffsfs_SetError(-EINVAL);
2217 else if(dev->read_only)
2218 yaffsfs_SetError(-EROFS);
2221 yaffs_flush_whole_cache(dev);
2222 yaffs_checkpoint_save(dev);
2227 yaffsfs_SetError(-ENODEV);
2234 int yaffs_remount(const YCHAR *path, int force, int read_only)
2237 struct yaffs_dev *dev=NULL;
2240 if(yaffsfs_CheckPath(path) < 0){
2241 yaffsfs_SetError(-ENAMETOOLONG);
2246 dev = yaffsfs_FindMountPoint(path);
2248 if(dev->is_mounted){
2252 yaffs_flush_whole_cache(dev);
2254 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2255 yh = & yaffsfs_handle[i];
2256 if(yh->useCount>0 &&
2257 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2258 inUse = 1; /* the device is in use, can't unmount */
2261 if(!inUse || force){
2263 yaffs_checkpoint_save(dev);
2264 dev->read_only = read_only ? 1 : 0;
2267 yaffsfs_SetError(-EBUSY);
2270 yaffsfs_SetError(-EINVAL);
2274 yaffsfs_SetError(-ENODEV);
2281 int yaffs_unmount2(const YCHAR *path, int force)
2284 struct yaffs_dev *dev=NULL;
2286 if(yaffsfs_CheckPath(path) < 0){
2287 yaffsfs_SetError(-ENAMETOOLONG);
2292 dev = yaffsfs_FindMountPoint(path);
2294 if(dev->is_mounted){
2298 yaffs_flush_whole_cache(dev);
2299 yaffs_checkpoint_save(dev);
2301 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2302 if(yaffsfs_handle[i].useCount > 0 &&
2303 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2304 inUse = 1; /* the device is in use, can't unmount */
2307 if(!inUse || force){
2309 yaffsfs_BreakDeviceHandles(dev);
2310 yaffs_deinitialise(dev);
2314 yaffsfs_SetError(-EBUSY);
2317 yaffsfs_SetError(-EINVAL);
2320 yaffsfs_SetError(-ENODEV);
2327 int yaffs_unmount(const YCHAR *path)
2329 return yaffs_unmount2(path,0);
2332 loff_t yaffs_freespace(const YCHAR *path)
2335 struct yaffs_dev *dev=NULL;
2338 if(yaffsfs_CheckPath(path) < 0){
2339 yaffsfs_SetError(-ENAMETOOLONG);
2344 dev = yaffsfs_FindDevice(path,&dummy);
2345 if(dev && dev->is_mounted){
2346 retVal = yaffs_get_n_free_chunks(dev);
2347 retVal *= dev->data_bytes_per_chunk;
2350 yaffsfs_SetError(-EINVAL);
2356 loff_t yaffs_totalspace(const YCHAR *path)
2359 struct yaffs_dev *dev=NULL;
2362 if(yaffsfs_CheckPath(path) < 0){
2363 yaffsfs_SetError(-ENAMETOOLONG);
2368 dev = yaffsfs_FindDevice(path,&dummy);
2369 if(dev && dev->is_mounted){
2370 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2371 dev->param.n_reserved_blocks;
2372 retVal *= dev->param.chunks_per_block;
2373 retVal *= dev->data_bytes_per_chunk;
2376 yaffsfs_SetError(-EINVAL);
2382 int yaffs_inodecount(const YCHAR *path)
2385 struct yaffs_dev *dev=NULL;
2388 if(yaffsfs_CheckPath(path) < 0){
2389 yaffsfs_SetError(-ENAMETOOLONG);
2394 dev = yaffsfs_FindDevice(path,&dummy);
2395 if(dev && dev->is_mounted) {
2396 int n_obj = dev->n_obj;
2397 if(n_obj > dev->n_hardlinks)
2398 retVal = n_obj - dev->n_hardlinks;
2402 yaffsfs_SetError(-EINVAL);
2409 void yaffs_add_device(struct yaffs_dev *dev)
2411 dev->is_mounted = 0;
2412 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2414 if(!dev->dev_list.next)
2415 INIT_LIST_HEAD(&dev->dev_list);
2417 list_add(&dev->dev_list,&yaffsfs_deviceList);
2420 void yaffs_remove_device(struct yaffs_dev *dev)
2422 list_del_init(&dev->dev_list);
2428 /* Directory search stuff. */
2431 * Directory search context
2433 * NB this is an opaque structure.
2440 yaffs_dirent de; /* directory entry being used by this dsc */
2441 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2442 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2443 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2445 struct list_head others;
2446 } yaffsfs_DirectorySearchContext;
2450 static struct list_head search_contexts;
2453 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2457 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2461 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2462 dsc->nextReturn = NULL;
2464 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2465 struct yaffs_obj,siblings);
2467 /* Hey someone isn't playing nice! */
2471 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2475 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2477 if( dsc->nextReturn == NULL ||
2478 list_empty(&dsc->dirObj->variant.dir_variant.children))
2479 dsc->nextReturn = NULL;
2481 struct list_head *next = dsc->nextReturn->siblings.next;
2483 if( next == &dsc->dirObj->variant.dir_variant.children)
2484 dsc->nextReturn = NULL; /* end of list */
2486 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2489 /* Hey someone isn't playing nice! */
2493 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2496 struct list_head *i;
2497 yaffsfs_DirectorySearchContext *dsc;
2499 /* if search contexts not initilised then skip */
2500 if(!search_contexts.next)
2503 /* Iterate through the directory search contexts.
2504 * If any are the one being removed, then advance the dsc to
2505 * the next one to prevent a hanging ptr.
2507 list_for_each(i, &search_contexts) {
2509 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2510 if(dsc->nextReturn == obj)
2511 yaffsfs_DirAdvance(dsc);
2517 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2519 yaffs_DIR *dir = NULL;
2520 struct yaffs_obj *obj = NULL;
2521 yaffsfs_DirectorySearchContext *dsc = NULL;
2525 if(yaffsfs_CheckPath(dirname) < 0){
2526 yaffsfs_SetError(-ENAMETOOLONG);
2532 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir,&loop);
2535 yaffsfs_SetError(-ENOTDIR);
2537 yaffsfs_SetError(-ELOOP);
2539 yaffsfs_SetError(-ENOENT);
2540 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2541 yaffsfs_SetError(-ENOTDIR);
2544 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2545 dir = (yaffs_DIR *)dsc;
2548 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2549 dsc->magic = YAFFS_MAGIC;
2551 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2552 INIT_LIST_HEAD(&dsc->others);
2554 if(!search_contexts.next)
2555 INIT_LIST_HEAD(&search_contexts);
2557 list_add(&dsc->others,&search_contexts);
2558 yaffsfs_SetDirRewound(dsc);
2568 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2570 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2571 struct yaffs_dirent *retVal = NULL;
2575 if(dsc && dsc->magic == YAFFS_MAGIC){
2576 yaffsfs_SetError(0);
2577 if(dsc->nextReturn){
2578 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2579 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2580 dsc->de.d_off = dsc->offset++;
2581 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2582 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2584 /* this should not happen! */
2585 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2587 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2589 yaffsfs_DirAdvance(dsc);
2593 yaffsfs_SetError(-EBADF);
2602 void yaffs_rewinddir(yaffs_DIR *dirp)
2604 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2608 yaffsfs_SetDirRewound(dsc);
2614 int yaffs_closedir(yaffs_DIR *dirp)
2616 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2620 list_del(&dsc->others); /* unhook from list */
2626 /* End of directory stuff */
2629 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2631 struct yaffs_obj *parent = NULL;
2632 struct yaffs_obj *obj;
2635 int mode = 0; /* ignore for now */
2639 if(yaffsfs_CheckPath(newpath) < 0){
2640 yaffsfs_SetError(-ENAMETOOLONG);
2644 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir,&loop);
2645 if(!parent && notDir)
2646 yaffsfs_SetError(-ENOTDIR);
2648 yaffsfs_SetError(-ELOOP);
2649 else if( !parent || yaffs_strnlen(name,5) < 1)
2650 yaffsfs_SetError(-ENOENT);
2651 else if(parent->my_dev->read_only)
2652 yaffsfs_SetError(-EROFS);
2654 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2657 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2658 yaffsfs_SetError(-EEXIST);
2660 yaffsfs_SetError(-ENOSPC);
2669 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2671 struct yaffs_obj *obj = NULL;
2672 struct yaffs_obj *dir = NULL;
2679 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir,&loop);
2682 yaffsfs_SetError(-ENOTDIR);
2684 yaffsfs_SetError(-ELOOP);
2685 else if(!dir || !obj)
2686 yaffsfs_SetError(-ENOENT);
2687 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2688 yaffsfs_SetError(-EINVAL);
2690 YCHAR *alias = obj->variant.symlink_variant.alias;
2691 memset(buf,0,bufsiz);
2692 yaffs_strncpy(buf,alias,bufsiz - 1);
2699 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2701 /* Creates a link called newpath to existing oldpath */
2702 struct yaffs_obj *obj = NULL;
2703 struct yaffs_obj *lnk = NULL;
2704 struct yaffs_obj *obj_dir = NULL;
2705 struct yaffs_obj *lnk_dir = NULL;
2713 if(yaffsfs_CheckPath(linkpath) < 0){
2714 yaffsfs_SetError(-ENAMETOOLONG);
2720 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj,&objLoop);
2721 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2722 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk,&lnkLoop);
2724 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2725 yaffsfs_SetError(-ENOTDIR);
2726 else if(objLoop || lnkLoop)
2727 yaffsfs_SetError(-ELOOP);
2728 else if(!obj_dir || !lnk_dir || !obj)
2729 yaffsfs_SetError(-ENOENT);
2730 else if(obj->my_dev->read_only)
2731 yaffsfs_SetError(-EROFS);
2733 yaffsfs_SetError(-EEXIST);
2734 else if(lnk_dir->my_dev != obj->my_dev)
2735 yaffsfs_SetError(-EXDEV);
2737 retVal = yaffsfs_CheckNameLength(newname);
2740 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2744 yaffsfs_SetError(-ENOSPC);
2754 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2760 yaffsfs_SetError(-EINVAL);
2766 * D E B U G F U N C T I O N S
2771 * Returns number of handles attached to the object
2773 int yaffs_n_handles(const YCHAR *path)
2775 struct yaffs_obj *obj;
2777 if(yaffsfs_CheckPath(path) < 0){
2778 yaffsfs_SetError(-ENAMETOOLONG);
2782 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2785 return yaffsfs_CountHandles(obj);
2790 int yaffs_get_error(void)
2792 return yaffsfs_GetLastError();
2795 int yaffs_set_error(int error)
2797 yaffsfs_SetError(error);
2801 int yaffs_dump_dev(const YCHAR *path)
2808 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2811 struct yaffs_dev *dev = obj->my_dev;
2814 "n_page_writes.......... %d\n"
2815 "n_page_reads........... %d\n"
2816 "n_erasures....... %d\n"
2817 "n_gc_copies............ %d\n"
2818 "garbageCollections... %d\n"
2819 "passiveGarbageColl'ns %d\n"
2825 dev->garbageCollections,
2826 dev->passiveGarbageCollections