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,
35 const YCHAR *path, int symDepth, int getEquiv);
36 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
38 unsigned int yaffs_wr_attempts;
42 * There are open inodes in yaffsfs_Inode.
43 * There are open handles in yaffsfs_Handle.
45 * Things are structured this way to be like the Linux VFS model
46 * so that interactions with the yaffs guts calls are similar.
47 * That means more common code paths and less special code.
48 * That means better testing etc.
52 int count; /* Number of handles accessing this inode */
53 struct yaffs_obj *iObj;
62 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
63 int useCount:10; /* Use count for this handle */
64 u32 position; /* current position in file */
67 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
68 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
69 static int yaffsfs_handlesInitialised;
72 unsigned yaffs_set_trace(unsigned tm)
74 yaffs_trace_mask = tm;
75 return yaffs_trace_mask;
78 unsigned yaffs_get_trace(void)
80 return yaffs_trace_mask;
85 * Inilitalise handle management on start-up.
88 static void yaffsfs_InitHandles(void)
91 if(yaffsfs_handlesInitialised)
94 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
95 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
96 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
97 yaffsfs_handle[i].inodeId = -1;
100 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
102 if(h < 0 || h >= YAFFSFS_N_HANDLES)
105 return &yaffsfs_handle[h];
108 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
110 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
112 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
113 return &yaffsfs_inode[h->inodeId];
118 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
120 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
129 * yaffsfs_FindInodeIdForObject
130 * Find the inode entry for an object, if it exists.
133 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
139 obj = yaffs_get_equivalent_obj(obj);
141 /* Look for it in open inode table*/
142 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
143 if(yaffsfs_inode[i].iObj == obj)
150 * yaffsfs_GetInodeIdForObject
151 * Grab an inode entry when opening a new inode.
153 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
157 yaffsfs_Inode *in = NULL;
160 obj = yaffs_get_equivalent_obj(obj);
162 ret = yaffsfs_FindInodeIdForObject(obj);
164 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
165 if(!yaffsfs_inode[i].iObj)
170 in = &yaffsfs_inode[ret];
182 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
184 int i = yaffsfs_FindInodeIdForObject(obj);
187 return yaffsfs_inode[i].count;
192 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
194 struct yaffs_obj *obj;
201 obj->my_inode = NULL;
206 static void yaffsfs_PutInode(int inodeId)
208 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
209 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
212 yaffsfs_ReleaseInode(in);
219 * Grab a handle (when opening a file)
222 static int yaffsfs_GetNewHandle(void)
227 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
228 h = yaffsfs_GetHandlePointer(i);
230 /* todo bug: should never happen */
233 memset(h,0,sizeof(yaffsfs_Handle));
244 * Increase use of handle when reading/writing a file
246 static int yaffsfs_GetHandle(int handle)
248 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
250 if(h && h->useCount > 0){
258 * Let go of a handle when closing a file or aborting an open or
259 * ending a read or write.
261 static int yaffsfs_PutHandle(int handle)
263 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
265 if(h && h->useCount > 0){
269 yaffsfs_PutInode(h->inodeId);
277 static void yaffsfs_PutDeviceHandles(struct yaffs_dev *dev)
281 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
282 yh = & yaffsfs_handle[i];
284 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
285 yaffsfs_PutHandle(i);
293 * Stuff to handle names.
297 int yaffsfs_Match(YCHAR a, YCHAR b)
303 int yaffsfs_IsPathDivider(YCHAR ch)
305 const YCHAR *str = YAFFS_PATH_DIVIDERS;
316 int yaffsfs_CheckNameLength(const char *name)
320 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
323 yaffsfs_SetError(-ENOENT);
325 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
326 yaffsfs_SetError(-ENAMETOOLONG);
333 LIST_HEAD(yaffsfs_deviceList);
338 * Scan the configuration list to find the device
339 * Curveballs: Should match paths that end in '/' too
340 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
342 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
344 struct list_head *cfg;
345 const YCHAR *leftOver;
347 struct yaffs_dev *retval = NULL;
348 struct yaffs_dev *dev = NULL;
350 int longestMatch = -1;
354 * Check all configs, choose the one that:
355 * 1) Actually matches a prefix (ie /a amd /abc will not match
356 * 2) Matches the longest.
358 list_for_each(cfg, &yaffsfs_deviceList){
359 dev = list_entry(cfg, struct yaffs_dev, dev_list);
366 while(matching && *p && *leftOver){
367 /* Skip over any /s */
368 while(yaffsfs_IsPathDivider(*p))
371 /* Skip over any /s */
372 while(yaffsfs_IsPathDivider(*leftOver))
375 /* Now match the text part */
377 *p && !yaffsfs_IsPathDivider(*p) &&
378 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
379 if(yaffsfs_Match(*p,*leftOver)){
389 /* Skip over any /s in leftOver */
390 while(yaffsfs_IsPathDivider(*leftOver))
393 // Skip over any /s in p
394 while(yaffsfs_IsPathDivider(*p))
397 // p should now be at the end of the string (ie. fully matched)
401 if( matching && (thisMatchLength > longestMatch))
404 *restOfPath = (YCHAR *)leftOver;
406 longestMatch = thisMatchLength;
413 /* FindMountPoint only returns a dev entry if the path is a mount point */
414 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
416 struct yaffs_dev *dev;
417 YCHAR *restOfPath=NULL;
418 dev = yaffsfs_FindDevice(path,&restOfPath);
419 if(dev && restOfPath && *restOfPath)
424 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
427 struct yaffs_dev *dev;
429 dev= yaffsfs_FindDevice(path,restOfPath);
430 if(dev && dev->is_mounted){
431 return dev->root_dir;
436 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
440 obj = yaffs_get_equivalent_obj(obj);
442 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
443 YCHAR *alias = obj->variant.symlink_variant.alias;
445 if(yaffsfs_IsPathDivider(*alias))
446 /* Starts with a /, need to scan from root up */
447 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
449 /* Relative to here, so use the parent of the symlink as a start */
450 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
457 * yaffsfs_FindDirectory
458 * Parse a path to determine the directory and the name within the directory.
460 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
462 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
463 const YCHAR *path, YCHAR **name, int symDepth)
465 struct yaffs_obj *dir;
467 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
470 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
475 restOfPath = (YCHAR *)path;
478 dir = yaffsfs_FindRoot(path,&restOfPath);
483 * curve ball: also throw away surplus '/'
484 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
486 while(yaffsfs_IsPathDivider(*restOfPath))
487 restOfPath++; /* get rid of '/' */
492 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
493 if (i < YAFFS_MAX_NAME_LENGTH){
494 str[i] = *restOfPath;
502 /* got to the end of the string */
505 if(yaffs_strcmp(str,_Y(".")) == 0)
509 else if(yaffs_strcmp(str,_Y("..")) == 0)
512 dir = yaffs_find_by_name(dir,str);
514 dir = yaffsfs_FollowLink(dir,symDepth);
516 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
521 /* directory did not exist. */
525 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
526 const YCHAR *path,YCHAR **name,int symDepth)
528 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
532 * yaffsfs_FindObject turns a path for an existing object into the object
534 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
536 struct yaffs_obj *dir;
537 struct yaffs_obj *obj;
540 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
543 obj = yaffs_find_by_name(dir,name);
548 obj = yaffs_get_equivalent_obj(obj);
554 int yaffs_dup(int fd)
557 yaffsfs_Handle *oldPtr = NULL;
558 yaffsfs_Handle *newPtr = NULL;
562 oldPtr = yaffsfs_GetHandlePointer(fd);
563 if(oldPtr && oldPtr->useCount > 0)
564 newHandle = yaffsfs_GetNewHandle();
566 newPtr = yaffsfs_GetHandlePointer(newHandle);
574 yaffsfs_SetError(-EBADF);
576 yaffsfs_SetError(-ENOMEM);
584 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
586 struct yaffs_obj *obj = NULL;
587 struct yaffs_obj *dir = NULL;
590 yaffsfs_Handle *yh = NULL;
593 int errorReported = 0;
594 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
595 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
596 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
597 u8 sharedReadAllowed;
598 u8 sharedWriteAllowed;
604 /* O_EXCL only has meaning if O_CREAT is specified */
605 if(!(oflag & O_CREAT))
608 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
609 if( (oflag & O_CREAT) & (oflag & O_EXCL))
612 /* Todo: Are there any more flag combos to sanitise ? */
614 /* Figure out if reading or writing is requested */
616 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
617 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
621 handle = yaffsfs_GetNewHandle();
624 yaffsfs_SetError(-ENFILE);
628 yh = yaffsfs_GetHandlePointer(handle);
630 /* try to find the exisiting object */
631 obj = yaffsfs_FindObject(NULL,path,0,1);
633 obj = yaffsfs_FollowLink(obj,symDepth++);
636 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
637 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
643 /* The file already exists or it might be a directory */
645 /* If it is a directory then we can't open it as a file */
646 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
648 yaffsfs_SetError(-EISDIR);
652 /* Open should fail if O_CREAT and O_EXCL are specified since
655 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
657 yaffsfs_SetError(-EEXIST);
661 /* Check file permissions */
662 if( readRequested && !(obj->yst_mode & S_IREAD))
665 if( writeRequested && !(obj->yst_mode & S_IWRITE))
668 if(openDenied && !errorReported ) {
669 /* Error if the file exists but permissions are refused. */
670 yaffsfs_SetError(-EACCES);
674 /* Check sharing of an existing object. */
678 sharedReadAllowed = 1;
679 sharedWriteAllowed = 1;
682 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
683 hx = &yaffsfs_handle[i];
684 if(hx->useCount > 0 &&
686 yaffsfs_inode[hx->inodeId].iObj == obj){
688 sharedReadAllowed = 0;
690 sharedWriteAllowed = 0;
700 if((!sharedReadAllowed && readRequested)||
701 (!shareRead && alreadyReading) ||
702 (!sharedWriteAllowed && writeRequested) ||
703 (!shareWrite && alreadyWriting)){
705 yaffsfs_SetError(-EBUSY);
712 /* If we could not open an existing object, then let's see if
713 * the directory exists. If not, error.
715 if(!obj && !errorReported){
716 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
718 yaffsfs_SetError(-ENOTDIR);
723 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
724 /* Let's see if we can create this file if it does not exist. */
725 if(dir->my_dev->read_only){
726 yaffsfs_SetError(-EINVAL);
729 obj = yaffs_create_file(dir,name,mode,0,0);
731 if(!obj && !errorReported){
732 yaffsfs_SetError(-ENOSPC);
737 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
738 /* Error if the file does not exist and CREAT is not set. */
739 yaffsfs_SetError(-ENOENT);
743 if(obj && !openDenied) {
744 int inodeId = yaffsfs_GetInodeIdForObject(obj);
748 * Todo: Fix any problem if inodes run out, though that
749 * can't happen if the number of inode items >= number of handles.
753 yh->inodeId = inodeId;
754 yh->reading = readRequested;
755 yh->writing = writeRequested;
756 yh->append = (oflag & O_APPEND) ? 1 : 0;
758 yh->shareRead = shareRead;
759 yh->shareWrite = shareWrite;
761 /* Hook inode to object */
762 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
764 if((oflag & O_TRUNC) && yh->writing)
765 yaffs_resize_file(obj,0);
767 yaffsfs_PutHandle(handle);
769 yaffsfs_SetError(0); /* Problem */
779 int yaffs_open(const YCHAR *path, int oflag, int mode)
781 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
784 int yaffs_Dofsync(int fd,int datasync)
786 yaffsfs_Handle *h = NULL;
791 h = yaffsfs_GetHandlePointer(fd);
793 if(h && h->useCount > 0)
795 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
798 yaffsfs_SetError(-EBADF);
807 int yaffs_fsync(int fd)
809 return yaffs_Dofsync(fd,0);
812 int yaffs_flush(int fd)
814 return yaffs_fsync(fd);
817 int yaffs_fdatasync(int fd)
819 return yaffs_Dofsync(fd,1);
822 int yaffs_close(int fd)
824 yaffsfs_Handle *h = NULL;
829 h = yaffsfs_GetHandlePointer(fd);
831 if(h && h->useCount > 0) {
833 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
834 yaffsfs_PutHandle(fd);
838 yaffsfs_SetError(-EBADF);
849 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
851 yaffsfs_Handle *h = NULL;
852 struct yaffs_obj *obj = NULL;
858 unsigned int maxRead;
859 u8 *buf = (u8 *)vbuf;
862 h = yaffsfs_GetHandlePointer(fd);
863 obj = yaffsfs_GetHandleObject(fd);
867 yaffsfs_SetError(-EBADF);
869 } else if(!h->reading){
870 /* Not a reading handle */
871 yaffsfs_SetError(-EINVAL);
877 startPos = h->position;
881 if(yaffs_get_obj_length(obj) > pos)
882 maxRead = yaffs_get_obj_length(obj) - pos;
890 yaffsfs_GetHandle(fd);
893 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
898 * Need to reverify object in case the device was
899 * unmounted in another thread.
901 obj = yaffsfs_GetHandleObject(fd);
905 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
916 nbyte = 0; /* no more to read */
926 yaffsfs_PutHandle(fd);
930 h->position = startPos + totalRead;
940 return (totalRead >= 0) ? totalRead : -1;
944 int yaffs_read(int fd, void *buf, unsigned int nbyte)
946 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
949 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
951 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
954 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
956 yaffsfs_Handle *h = NULL;
957 struct yaffs_obj *obj = NULL;
961 int totalWritten = 0;
962 int write_trhrough = 0;
964 const u8 *buf = (const u8 *)vbuf;
967 h = yaffsfs_GetHandlePointer(fd);
968 obj = yaffsfs_GetHandleObject(fd);
972 yaffsfs_SetError(-EBADF);
974 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
975 yaffsfs_SetError(-EINVAL);
979 startPos = yaffs_get_obj_length(obj);
983 startPos = h->position;
985 yaffsfs_GetHandle(fd);
989 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
994 * Need to reverify object in case the device was
995 * remounted or unmounted in another thread.
997 obj = yaffsfs_GetHandleObject(fd);
998 if(!obj || obj->my_dev->read_only)
1001 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1004 totalWritten += nWritten;
1009 if(nWritten == nToWrite)
1014 if(nWritten < 1 && totalWritten < 1){
1015 yaffsfs_SetError(-ENOSPC);
1025 yaffsfs_PutHandle(fd);
1028 if(totalWritten > 0)
1029 h->position = startPos + totalWritten;
1038 return (totalWritten >= 0) ? totalWritten : -1;
1041 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1043 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1046 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1048 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1052 int yaffs_truncate(const YCHAR *path,off_t new_size)
1054 struct yaffs_obj *obj = NULL;
1055 int result = YAFFS_FAIL;
1059 obj = yaffsfs_FindObject(NULL,path,0,1);
1062 yaffsfs_SetError(-ENOENT);
1063 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1064 yaffsfs_SetError(-EISDIR);
1065 else if(obj->my_dev->read_only)
1066 yaffsfs_SetError(-EACCES);
1067 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1068 yaffsfs_SetError(-EINVAL);
1070 result = yaffs_resize_file(obj,new_size);
1074 return (result) ? 0 : -1;
1077 int yaffs_ftruncate(int fd, off_t new_size)
1079 yaffsfs_Handle *h = NULL;
1080 struct yaffs_obj *obj = NULL;
1084 h = yaffsfs_GetHandlePointer(fd);
1085 obj = yaffsfs_GetHandleObject(fd);
1089 yaffsfs_SetError(-EBADF);
1090 else if(obj->my_dev->read_only)
1091 yaffsfs_SetError(-EACCES);
1092 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1093 yaffsfs_SetError(-EINVAL);
1095 /* resize the file */
1096 result = yaffs_resize_file(obj,new_size);
1099 return (result) ? 0 : -1;
1103 off_t yaffs_lseek(int fd, off_t offset, int whence)
1105 yaffsfs_Handle *h = NULL;
1106 struct yaffs_obj *obj = NULL;
1111 h = yaffsfs_GetHandlePointer(fd);
1112 obj = yaffsfs_GetHandleObject(fd);
1116 yaffsfs_SetError(-EBADF);
1118 if(whence == SEEK_SET){
1121 } else if(whence == SEEK_CUR) {
1122 if( (h->position + offset) >= 0)
1123 pos = (h->position + offset);
1124 } else if(whence == SEEK_END) {
1125 fSize = yaffs_get_obj_length(obj);
1126 if(fSize >= 0 && (fSize + offset) >= 0)
1127 pos = fSize + offset;
1133 yaffsfs_SetError(-EINVAL);
1142 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1144 struct yaffs_obj *dir = NULL;
1145 struct yaffs_obj *obj = NULL;
1147 int result = YAFFS_FAIL;
1151 obj = yaffsfs_FindObject(NULL,path,0,0);
1152 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1154 yaffsfs_SetError(-ENOTDIR);
1156 yaffsfs_SetError(-ENOENT);
1157 else if(obj->my_dev->read_only)
1158 yaffsfs_SetError(-EINVAL);
1159 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1160 yaffsfs_SetError(-EISDIR);
1161 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1162 yaffsfs_SetError(-ENOTDIR);
1164 result = yaffs_unlinker(dir,name);
1166 if(result == YAFFS_FAIL && isDirectory)
1167 yaffsfs_SetError(-ENOTEMPTY);
1174 return (result == YAFFS_FAIL) ? -1 : 0;
1178 int yaffs_rmdir(const YCHAR *path)
1180 return yaffsfs_DoUnlink(path,1);
1183 int yaffs_unlink(const YCHAR *path)
1185 return yaffsfs_DoUnlink(path,0);
1188 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1190 struct yaffs_obj *olddir = NULL;
1191 struct yaffs_obj *newdir = NULL;
1192 struct yaffs_obj *obj = NULL;
1195 int result= YAFFS_FAIL;
1196 int rename_allowed = 1;
1200 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1201 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1202 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1204 if(!olddir || !newdir || !obj) {
1206 yaffsfs_SetError(-EBADF);
1208 } else if(obj->my_dev->read_only){
1209 yaffsfs_SetError(-EINVAL);
1211 } else if(olddir->my_dev != newdir->my_dev) {
1212 /* Rename must be on same device */
1213 yaffsfs_SetError(-EXDEV);
1215 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1217 * It is a directory, check that it is not being renamed to
1218 * being its own decendent.
1219 * Do this by tracing from the new directory back to the root, checking for obj
1222 struct yaffs_obj *xx = newdir;
1224 while( rename_allowed && xx){
1230 yaffsfs_SetError(-EACCES);
1234 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1238 return (result == YAFFS_FAIL) ? -1 : 0;
1242 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1246 obj = yaffs_get_equivalent_obj(obj);
1249 buf->st_dev = (int)obj->my_dev->os_context;
1250 buf->st_ino = obj->obj_id;
1251 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1253 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1254 buf->st_mode |= S_IFDIR;
1255 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1256 buf->st_mode |= S_IFLNK;
1257 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1258 buf->st_mode |= S_IFREG;
1260 buf->st_nlink = yaffs_get_obj_link_count(obj);
1263 buf->st_rdev = obj->yst_rdev;
1264 buf->st_size = yaffs_get_obj_length(obj);
1265 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1266 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1267 #if CONFIG_YAFFS_WINCE
1268 buf->yst_wince_atime[0] = obj->win_atime[0];
1269 buf->yst_wince_atime[1] = obj->win_atime[1];
1270 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1271 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1272 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1273 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1275 buf->yst_atime = obj->yst_atime;
1276 buf->yst_ctime = obj->yst_ctime;
1277 buf->yst_mtime = obj->yst_mtime;
1284 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1286 struct yaffs_obj *obj;
1292 obj = yaffsfs_FindObject(NULL,path,0,1);
1295 obj = yaffsfs_FollowLink(obj,0);
1298 retVal = yaffsfs_DoStat(obj,buf);
1300 /* todo error not found */
1301 yaffsfs_SetError(-ENOENT);
1309 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1311 return yaffsfs_DoStatOrLStat(path,buf,0);
1314 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1316 return yaffsfs_DoStatOrLStat(path,buf,1);
1319 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1321 struct yaffs_obj *obj;
1326 obj = yaffsfs_GetHandleObject(fd);
1329 retVal = yaffsfs_DoStat(obj,buf);
1332 yaffsfs_SetError(-EBADF);
1339 #ifndef CONFIG_YAFFS_WINCE
1340 /* xattrib functions */
1343 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1345 struct yaffs_obj *obj;
1351 obj = yaffsfs_FindObject(NULL,path,0,1);
1354 obj = yaffsfs_FollowLink(obj,0);
1357 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1359 yaffsfs_SetError(retVal);
1363 /* todo error not found */
1364 yaffsfs_SetError(-ENOENT);
1372 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1374 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1377 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1379 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1384 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1386 struct yaffs_obj *obj;
1391 obj = yaffsfs_GetHandleObject(fd);
1394 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1396 yaffsfs_SetError(retVal);
1401 yaffsfs_SetError(-EBADF);
1408 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1410 struct yaffs_obj *obj;
1416 obj = yaffsfs_FindObject(NULL,path,0,1);
1419 obj = yaffsfs_FollowLink(obj,0);
1422 retVal = yaffs_get_xattrib(obj,name,data,size);
1424 yaffsfs_SetError(retVal);
1428 /* todo error not found */
1429 yaffsfs_SetError(-ENOENT);
1437 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1439 return yaffs_do_getxattr( path, name, data, size, 1);
1441 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1443 return yaffs_do_getxattr( path, name, data, size, 0);
1448 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1450 struct yaffs_obj *obj;
1455 obj = yaffsfs_GetHandleObject(fd);
1458 retVal = yaffs_get_xattrib(obj,name,data,size);
1460 yaffsfs_SetError(retVal);
1465 yaffsfs_SetError(-EBADF);
1472 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1474 struct yaffs_obj *obj;
1480 obj = yaffsfs_FindObject(NULL,path,0,1);
1483 obj = yaffsfs_FollowLink(obj,0);
1486 retVal = yaffs_list_xattrib(obj, data,size);
1488 yaffsfs_SetError(retVal);
1492 /* todo error not found */
1493 yaffsfs_SetError(-ENOENT);
1501 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1503 return yaffs_do_listxattr(path, data, size, 1);
1506 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1508 return yaffs_do_listxattr(path, data, size, 0);
1511 int yaffs_flistxattr(int fd, char *data, int size)
1513 struct yaffs_obj *obj;
1518 obj = yaffsfs_GetHandleObject(fd);
1521 retVal = yaffs_list_xattrib(obj,data,size);
1523 yaffsfs_SetError(retVal);
1528 yaffsfs_SetError(-EBADF);
1535 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1537 struct yaffs_obj *obj;
1543 obj = yaffsfs_FindObject(NULL,path,0,1);
1546 obj = yaffsfs_FollowLink(obj,0);
1549 retVal = yaffs_remove_xattrib(obj,name);
1551 yaffsfs_SetError(retVal);
1555 /* todo error not found */
1556 yaffsfs_SetError(-ENOENT);
1564 int yaffs_removexattr(const YCHAR *path, const char *name)
1566 return yaffs_do_removexattr(path, name, 1);
1569 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1571 return yaffs_do_removexattr(path, name, 0);
1574 int yaffs_fremovexattr(int fd, const char *name)
1576 struct yaffs_obj *obj;
1581 obj = yaffsfs_GetHandleObject(fd);
1584 retVal = yaffs_remove_xattrib(obj,name);
1586 yaffsfs_SetError(retVal);
1591 yaffsfs_SetError(-EBADF);
1599 #ifdef CONFIG_YAFFS_WINCE
1600 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1602 struct yaffs_obj *obj;
1607 obj = yaffsfs_GetHandleObject(fd);
1612 wctime[0] = obj->win_ctime[0];
1613 wctime[1] = obj->win_ctime[1];
1616 watime[0] = obj->win_atime[0];
1617 watime[1] = obj->win_atime[1];
1620 wmtime[0] = obj->win_mtime[0];
1621 wmtime[1] = obj->win_mtime[1];
1628 yaffsfs_SetError(-EBADF);
1636 int yaffs_set_wince_times(int fd,
1637 const unsigned *wctime,
1638 const unsigned *watime,
1639 const unsigned *wmtime)
1641 struct yaffs_obj *obj;
1646 obj = yaffsfs_GetHandleObject(fd);
1651 obj->win_ctime[0] = wctime[0];
1652 obj->win_ctime[1] = wctime[1];
1655 obj->win_atime[0] = watime[0];
1656 obj->win_atime[1] = watime[1];
1659 obj->win_mtime[0] = wmtime[0];
1660 obj->win_mtime[1] = wmtime[1];
1664 result = yaffs_flush_file(obj,0,0);
1668 yaffsfs_SetError(-EBADF);
1678 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1683 obj = yaffs_get_equivalent_obj(obj);
1686 obj->yst_mode = mode;
1688 result = yaffs_flush_file(obj,0,0);
1691 return result == YAFFS_OK ? 0 : -1;
1695 int yaffs_access(const YCHAR *path, int amode)
1697 struct yaffs_obj *obj;
1701 if(amode & ~(R_OK | W_OK | X_OK)){
1702 yaffsfs_SetError(-EINVAL);
1708 obj = yaffsfs_FindObject(NULL,path,0,1);
1713 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1715 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1717 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1721 yaffsfs_SetError(-EACCES);
1725 /* todo error not found */
1726 yaffsfs_SetError(-ENOENT);
1737 int yaffs_chmod(const YCHAR *path, mode_t mode)
1739 struct yaffs_obj *obj;
1743 yaffsfs_SetError(-EINVAL);
1749 obj = yaffsfs_FindObject(NULL,path,0,1);
1752 yaffsfs_SetError(-ENOENT);
1753 else if(obj->my_dev->read_only)
1754 yaffsfs_SetError(-EROFS);
1756 retVal = yaffsfs_DoChMod(obj,mode);
1765 int yaffs_fchmod(int fd, mode_t mode)
1767 struct yaffs_obj *obj;
1771 yaffsfs_SetError(-EINVAL);
1776 obj = yaffsfs_GetHandleObject(fd);
1779 yaffsfs_SetError(-EBADF);
1780 else if(obj->my_dev->read_only)
1781 yaffsfs_SetError(-EROFS);
1783 retVal = yaffsfs_DoChMod(obj,mode);
1791 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1793 struct yaffs_obj *parent = NULL;
1794 struct yaffs_obj *dir = NULL;
1796 YCHAR *use_path = NULL;
1797 int path_length = 0;
1803 * We don't have a definition for max path length.
1804 * We will use 3 * max name length instead.
1807 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1809 /* If the last character is a path divider, then we need to
1810 * trim it back so that the name look-up works properly.
1811 * eg. /foo/new_dir/ -> /foo/newdir
1812 * Curveball: Need to handle multiple path dividers:
1813 * eg. /foof/sdfse///// -> /foo/sdfse
1815 if(path_length > 0 &&
1816 yaffsfs_IsPathDivider(path[path_length-1])){
1817 use_path = YMALLOC(path_length + 1);
1819 yaffsfs_SetError(-ENOMEM);
1822 strcpy(use_path, path);
1823 for(i = path_length-1;
1824 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1826 use_path[i] = (YCHAR) 0;
1831 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1832 if(parent && yaffs_strnlen(name,5) == 0){
1833 /* Trying to make the root itself */
1834 yaffsfs_SetError(-EEXIST);
1835 } else if(parent && parent->my_dev->read_only){
1836 yaffsfs_SetError(-EINVAL);
1839 dir = yaffs_create_dir(parent,name,mode,0,0);
1844 yaffsfs_SetError(-ENOENT); /* missing path */
1845 else if (yaffs_find_by_name(parent,name))
1846 yaffsfs_SetError(-EEXIST); /* the name already exists */
1848 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1861 void * yaffs_getdev(const YCHAR *path)
1863 struct yaffs_dev *dev=NULL;
1865 dev = yaffsfs_FindDevice(path,&dummy);
1869 int yaffs_mount2(const YCHAR *path,int read_only)
1872 int result=YAFFS_FAIL;
1873 struct yaffs_dev *dev=NULL;
1875 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1879 yaffsfs_InitHandles();
1881 dev = yaffsfs_FindMountPoint(path);
1883 if(!dev->is_mounted){
1884 dev->read_only = read_only ? 1 : 0;
1885 result = yaffs_guts_initialise(dev);
1886 if(result == YAFFS_FAIL)
1887 /* todo error - mount failed */
1888 yaffsfs_SetError(-ENOMEM);
1889 retVal = result ? 0 : -1;
1893 /* todo error - already mounted. */
1894 yaffsfs_SetError(-EBUSY);
1896 /* todo error - no device */
1897 yaffsfs_SetError(-ENODEV);
1904 int yaffs_mount(const YCHAR *path)
1906 return yaffs_mount2(path,0);
1909 int yaffs_sync(const YCHAR *path)
1912 struct yaffs_dev *dev=NULL;
1916 dev = yaffsfs_FindDevice(path,&dummy);
1918 if(dev->is_mounted){
1920 yaffs_flush_whole_cache(dev);
1921 yaffs_checkpoint_save(dev);
1925 /* todo error - not mounted. */
1926 yaffsfs_SetError(-EINVAL);
1929 /* todo error - no device */
1930 yaffsfs_SetError(-ENODEV);
1937 int yaffs_remount(const YCHAR *path, int force, int read_only)
1940 struct yaffs_dev *dev=NULL;
1944 dev = yaffsfs_FindMountPoint(path);
1946 if(dev->is_mounted){
1950 yaffs_flush_whole_cache(dev);
1952 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1953 yh = & yaffsfs_handle[i];
1954 if(yh->useCount>0 &&
1955 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
1956 inUse = 1; /* the device is in use, can't unmount */
1959 if(!inUse || force){
1961 yaffs_checkpoint_save(dev);
1962 dev->read_only = read_only ? 1 : 0;
1965 yaffsfs_SetError(-EBUSY);
1968 yaffsfs_SetError(-EINVAL);
1972 yaffsfs_SetError(-ENODEV);
1979 int yaffs_unmount2(const YCHAR *path, int force)
1982 struct yaffs_dev *dev=NULL;
1985 dev = yaffsfs_FindMountPoint(path);
1987 if(dev->is_mounted){
1991 yaffs_flush_whole_cache(dev);
1992 yaffs_checkpoint_save(dev);
1994 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1995 if(yaffsfs_handle[i].useCount > 0 &&
1996 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1997 inUse = 1; /* the device is in use, can't unmount */
2000 if(!inUse || force){
2002 yaffsfs_PutDeviceHandles(dev);
2003 yaffs_deinitialise(dev);
2007 /* todo error can't unmount as files are open */
2008 yaffsfs_SetError(-EBUSY);
2011 /* todo error - not mounted. */
2012 yaffsfs_SetError(-EINVAL);
2015 /* todo error - no device */
2016 yaffsfs_SetError(-ENODEV);
2023 int yaffs_unmount(const YCHAR *path)
2025 return yaffs_unmount2(path,0);
2028 loff_t yaffs_freespace(const YCHAR *path)
2031 struct yaffs_dev *dev=NULL;
2035 dev = yaffsfs_FindDevice(path,&dummy);
2036 if(dev && dev->is_mounted){
2037 retVal = yaffs_get_n_free_chunks(dev);
2038 retVal *= dev->data_bytes_per_chunk;
2041 yaffsfs_SetError(-EINVAL);
2047 loff_t yaffs_totalspace(const YCHAR *path)
2050 struct yaffs_dev *dev=NULL;
2054 dev = yaffsfs_FindDevice(path,&dummy);
2055 if(dev && dev->is_mounted){
2056 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2057 retVal *= dev->param.chunks_per_block;
2058 retVal *= dev->data_bytes_per_chunk;
2061 yaffsfs_SetError(-EINVAL);
2067 int yaffs_inodecount(const YCHAR *path)
2070 struct yaffs_dev *dev=NULL;
2074 dev = yaffsfs_FindDevice(path,&dummy);
2075 if(dev && dev->is_mounted) {
2076 int n_obj = dev->n_obj;
2077 if(n_obj > dev->n_hardlinks)
2078 retVal = n_obj - dev->n_hardlinks;
2082 yaffsfs_SetError(-EINVAL);
2089 void yaffs_add_device(struct yaffs_dev *dev)
2091 dev->is_mounted = 0;
2092 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2094 if(!dev->dev_list.next)
2095 INIT_LIST_HEAD(&dev->dev_list);
2097 list_add(&dev->dev_list,&yaffsfs_deviceList);
2100 void yaffs_remove_device(struct yaffs_dev *dev)
2102 list_del_init(&dev->dev_list);
2108 /* Directory search stuff. */
2111 * Directory search context
2113 * NB this is an opaque structure.
2120 yaffs_dirent de; /* directory entry being used by this dsc */
2121 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2122 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2123 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2125 struct list_head others;
2126 } yaffsfs_DirectorySearchContext;
2130 static struct list_head search_contexts;
2133 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2137 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2141 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2142 dsc->nextReturn = NULL;
2144 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2145 struct yaffs_obj,siblings);
2147 /* Hey someone isn't playing nice! */
2151 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2155 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2157 if( dsc->nextReturn == NULL ||
2158 list_empty(&dsc->dirObj->variant.dir_variant.children))
2159 dsc->nextReturn = NULL;
2161 struct list_head *next = dsc->nextReturn->siblings.next;
2163 if( next == &dsc->dirObj->variant.dir_variant.children)
2164 dsc->nextReturn = NULL; /* end of list */
2166 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2169 /* Hey someone isn't playing nice! */
2173 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2176 struct list_head *i;
2177 yaffsfs_DirectorySearchContext *dsc;
2179 /* if search contexts not initilised then skip */
2180 if(!search_contexts.next)
2183 /* Iterate through the directory search contexts.
2184 * If any are the one being removed, then advance the dsc to
2185 * the next one to prevent a hanging ptr.
2187 list_for_each(i, &search_contexts) {
2189 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2190 if(dsc->nextReturn == obj)
2191 yaffsfs_DirAdvance(dsc);
2197 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2199 yaffs_DIR *dir = NULL;
2200 struct yaffs_obj *obj = NULL;
2201 yaffsfs_DirectorySearchContext *dsc = NULL;
2205 obj = yaffsfs_FindObject(NULL,dirname,0,1);
2207 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2209 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2210 dir = (yaffs_DIR *)dsc;
2213 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2214 dsc->magic = YAFFS_MAGIC;
2216 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2217 INIT_LIST_HEAD(&dsc->others);
2219 if(!search_contexts.next)
2220 INIT_LIST_HEAD(&search_contexts);
2222 list_add(&dsc->others,&search_contexts);
2223 yaffsfs_SetDirRewound(dsc);
2233 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2235 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2236 struct yaffs_dirent *retVal = NULL;
2240 if(dsc && dsc->magic == YAFFS_MAGIC){
2241 yaffsfs_SetError(0);
2242 if(dsc->nextReturn){
2243 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2244 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2245 dsc->de.d_off = dsc->offset++;
2246 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2247 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2249 /* this should not happen! */
2250 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2252 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2254 yaffsfs_DirAdvance(dsc);
2258 yaffsfs_SetError(-EBADF);
2267 void yaffs_rewinddir(yaffs_DIR *dirp)
2269 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2273 yaffsfs_SetDirRewound(dsc);
2279 int yaffs_closedir(yaffs_DIR *dirp)
2281 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2285 list_del(&dsc->others); /* unhook from list */
2291 /* End of directory stuff */
2294 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2296 struct yaffs_obj *parent = NULL;
2297 struct yaffs_obj *obj;
2300 int mode = 0; /* ignore for now */
2303 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2304 if(parent && parent->my_dev->read_only)
2305 yaffsfs_SetError(-EINVAL);
2307 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2311 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2315 yaffsfs_SetError(-EINVAL);
2325 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2327 struct yaffs_obj *obj = NULL;
2332 obj = yaffsfs_FindObject(NULL,path,0,1);
2335 yaffsfs_SetError(-ENOENT);
2337 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2338 yaffsfs_SetError(-EINVAL);
2341 YCHAR *alias = obj->variant.symlink_variant.alias;
2342 memset(buf,0,bufsiz);
2343 yaffs_strncpy(buf,alias,bufsiz - 1);
2350 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2352 /* Creates a link called newpath to existing oldpath */
2353 struct yaffs_obj *obj = NULL;
2354 struct yaffs_obj *target = NULL;
2359 obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2360 target = yaffsfs_FindObject(NULL,newpath,0,0);
2363 yaffsfs_SetError(-ENOENT);
2365 } else if(obj->my_dev->read_only){
2366 yaffsfs_SetError(-EINVAL);
2369 yaffsfs_SetError(-EEXIST);
2372 struct yaffs_obj *newdir = NULL;
2373 struct yaffs_obj *link = NULL;
2377 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2380 yaffsfs_SetError(-ENOTDIR);
2382 }else if(newdir->my_dev != obj->my_dev){
2383 yaffsfs_SetError(-EXDEV);
2387 retVal = yaffsfs_CheckNameLength(newname);
2390 link = yaffs_link_obj(newdir,newname,obj);
2394 yaffsfs_SetError(-ENOSPC);
2405 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2417 * Returns number of handles attached to the object
2419 int yaffs_n_handles(const YCHAR *path)
2421 struct yaffs_obj *obj;
2423 obj = yaffsfs_FindObject(NULL,path,0,1);
2425 return yaffsfs_CountHandles(obj);
2428 int yaffs_get_error(void)
2430 return yaffsfs_GetLastError();
2433 int yaffs_set_error(int error)
2435 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2436 yaffsfs_SetError(error);
2440 int yaffs_dump_dev(const YCHAR *path)
2447 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2450 struct yaffs_dev *dev = obj->my_dev;
2453 "n_page_writes.......... %d\n"
2454 "n_page_reads........... %d\n"
2455 "n_erasures....... %d\n"
2456 "n_gc_copies............ %d\n"
2457 "garbageCollections... %d\n"
2458 "passiveGarbageColl'ns %d\n"
2464 dev->garbageCollections,
2465 dev->passiveGarbageCollections