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, const YCHAR *path, int symDepth, int getEquiv);
35 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
37 unsigned int yaffs_wr_attempts;
41 * There are open inodes in yaffsfs_Inode.
42 * There are open handles in yaffsfs_Handle.
44 * Things are structured this way to be like the Linux VFS model
45 * so that interactions with the yaffs guts calls are similar.
46 * That means more common code paths and less special code.
47 * That means better testing etc.
51 int count; /* Number of handles accessing this inode */
52 struct yaffs_obj *iObj;
61 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
62 int useCount:10; /* Use count for this handle */
63 u32 position; /* current position in file */
66 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
67 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
68 static int yaffsfs_handlesInitialised;
70 unsigned int yaffs_trace_mask;
72 int yaffs_set_trace(unsigned int tm)
74 return yaffs_trace_mask=tm;
77 unsigned int yaffs_get_trace(void)
79 return yaffs_trace_mask;
84 * Inilitalise handle management on start-up.
87 static void yaffsfs_InitHandles(void)
90 if(yaffsfs_handlesInitialised)
93 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
94 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
95 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
96 yaffsfs_handle[i].inodeId = -1;
99 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
101 if(h < 0 || h >= YAFFSFS_N_HANDLES)
104 return &yaffsfs_handle[h];
107 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
109 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
111 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
112 return &yaffsfs_inode[h->inodeId];
117 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
119 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
128 * yaffsfs_FindInodeIdForObject
129 * Find the inode entry for an object, if it exists.
132 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
138 obj = yaffs_get_equivalent_obj(obj);
140 /* Look for it in open inode table*/
141 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
142 if(yaffsfs_inode[i].iObj == obj)
149 * yaffsfs_GetInodeIdForObject
150 * Grab an inode entry when opening a new inode.
152 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
156 yaffsfs_Inode *in = NULL;
159 obj = yaffs_get_equivalent_obj(obj);
161 ret = yaffsfs_FindInodeIdForObject(obj);
163 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
164 if(!yaffsfs_inode[i].iObj)
169 in = &yaffsfs_inode[ret];
181 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
183 int i = yaffsfs_FindInodeIdForObject(obj);
186 return yaffsfs_inode[i].count;
191 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
193 struct yaffs_obj *obj;
200 obj->my_inode = NULL;
205 static void yaffsfs_PutInode(int inodeId)
207 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
208 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
211 yaffsfs_ReleaseInode(in);
218 * Grab a handle (when opening a file)
221 static int yaffsfs_GetNewHandle(void)
226 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
227 h = yaffsfs_GetHandlePointer(i);
229 /* todo bug: should never happen */
232 memset(h,0,sizeof(yaffsfs_Handle));
243 * Increase use of handle when reading/writing a file
245 static int yaffsfs_GetHandle(int handle)
247 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
249 if(h && h->useCount > 0){
257 * Let go of a handle when closing a file or aborting an open or
258 * ending a read or write.
260 static int yaffsfs_PutHandle(int handle)
262 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
264 if(h && h->useCount > 0){
268 yaffsfs_PutInode(h->inodeId);
280 * Stuff to search for a directory from a path
284 int yaffsfs_Match(YCHAR a, YCHAR b)
290 int yaffsfs_IsPathDivider(YCHAR ch)
292 const YCHAR *str = YAFFS_PATH_DIVIDERS;
305 LIST_HEAD(yaffsfs_deviceList);
310 * Scan the configuration list to find the root.
311 * Curveballs: Should match paths that end in '/' too
312 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
314 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
316 struct list_head *cfg;
317 const YCHAR *leftOver;
319 struct yaffs_dev *retval = NULL;
320 struct yaffs_dev *dev = NULL;
322 int longestMatch = -1;
326 * Check all configs, choose the one that:
327 * 1) Actually matches a prefix (ie /a amd /abc will not match
328 * 2) Matches the longest.
330 list_for_each(cfg, &yaffsfs_deviceList){
331 dev = list_entry(cfg, struct yaffs_dev, dev_list);
338 while(matching && *p && *leftOver){
339 /* Skip over any /s */
340 while(yaffsfs_IsPathDivider(*p))
343 /* Skip over any /s */
344 while(yaffsfs_IsPathDivider(*leftOver))
347 /* Now match the text part */
349 *p && !yaffsfs_IsPathDivider(*p) &&
350 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
351 if(yaffsfs_Match(*p,*leftOver)){
361 /* Skip over any /s in leftOver */
362 while(yaffsfs_IsPathDivider(*leftOver))
365 // Skip over any /s in p
366 while(yaffsfs_IsPathDivider(*p))
369 // p should now be at the end of the string (ie. fully matched)
373 if( matching && (thisMatchLength > longestMatch))
376 *restOfPath = (YCHAR *)leftOver;
378 longestMatch = thisMatchLength;
386 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
388 yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
389 const YCHAR *leftOver;
391 struct yaffs_dev *retval = NULL;
393 int longestMatch = -1;
396 * Check all configs, choose the one that:
397 * 1) Actually matches a prefix (ie /a amd /abc will not match
398 * 2) Matches the longest.
400 while(cfg && cfg->prefix && cfg->dev){
405 while(*p && /* unmatched part of prefix */
406 !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) &&
407 *leftOver && yaffsfs_Match(*p,*leftOver)){
414 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) && /* end of prefix */
415 (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && /* no more in this path name part */
416 (thisMatchLength > longestMatch)){
418 *restOfPath = (YCHAR *)leftOver;
420 longestMatch = thisMatchLength;
428 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
431 struct yaffs_dev *dev;
433 dev= yaffsfs_FindDevice(path,restOfPath);
434 if(dev && dev->is_mounted){
435 return dev->root_dir;
440 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
444 obj = yaffs_get_equivalent_obj(obj);
446 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
447 YCHAR *alias = obj->variant.symlink_variant.alias;
449 if(yaffsfs_IsPathDivider(*alias))
450 /* Starts with a /, need to scan from root up */
451 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
453 /* Relative to here, so use the parent of the symlink as a start */
454 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
461 * yaffsfs_FindDirectory
462 * Parse a path to determine the directory and the name within the directory.
464 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
466 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
467 const YCHAR *path, YCHAR **name, int symDepth)
469 struct yaffs_obj *dir;
471 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
474 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
479 restOfPath = (YCHAR *)path;
482 dir = yaffsfs_FindRoot(path,&restOfPath);
487 * curve ball: also throw away surplus '/'
488 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
490 while(yaffsfs_IsPathDivider(*restOfPath))
491 restOfPath++; /* get rid of '/' */
496 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
497 if (i < YAFFS_MAX_NAME_LENGTH){
498 str[i] = *restOfPath;
506 /* got to the end of the string */
509 if(yaffs_strcmp(str,_Y(".")) == 0)
513 else if(yaffs_strcmp(str,_Y("..")) == 0)
516 dir = yaffs_find_by_name(dir,str);
518 dir = yaffsfs_FollowLink(dir,symDepth);
520 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
525 /* directory did not exist. */
529 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
530 const YCHAR *path,YCHAR **name,int symDepth)
532 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
536 * yaffsfs_FindObject turns a path for an existing object into the object
538 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
540 struct yaffs_obj *dir;
541 struct yaffs_obj *obj;
544 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
547 obj = yaffs_find_by_name(dir,name);
552 obj = yaffs_get_equivalent_obj(obj);
558 int yaffs_dup(int fd)
561 yaffsfs_Handle *oldPtr = NULL;
562 yaffsfs_Handle *newPtr = NULL;
566 oldPtr = yaffsfs_GetHandlePointer(fd);
567 if(oldPtr && oldPtr->useCount > 0)
568 newHandle = yaffsfs_GetNewHandle();
570 newPtr = yaffsfs_GetHandlePointer(newHandle);
578 yaffsfs_SetError(-EBADF);
580 yaffsfs_SetError(-ENOMEM);
588 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
590 struct yaffs_obj *obj = NULL;
591 struct yaffs_obj *dir = NULL;
594 yaffsfs_Handle *yh = NULL;
597 int errorReported = 0;
598 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
599 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
600 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
601 u8 sharedReadAllowed;
602 u8 sharedWriteAllowed;
608 /* O_EXCL only has meaning if O_CREAT is specified */
609 if(!(oflag & O_CREAT))
612 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
613 if( (oflag & O_CREAT) & (oflag & O_EXCL))
616 /* Todo: Are there any more flag combos to sanitise ? */
618 /* Figure out if reading or writing is requested */
620 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
621 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
625 handle = yaffsfs_GetNewHandle();
628 yaffsfs_SetError(-ENFILE);
632 yh = yaffsfs_GetHandlePointer(handle);
634 /* try to find the exisiting object */
635 obj = yaffsfs_FindObject(NULL,path,0,1);
637 obj = yaffsfs_FollowLink(obj,symDepth++);
640 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
641 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
647 /* The file already exists or it might be a directory */
649 /* If it is a directory then we can't open it as a file */
650 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
652 yaffsfs_SetError(-EISDIR);
656 /* Open should fail if O_CREAT and O_EXCL are specified since
659 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
661 yaffsfs_SetError(-EEXIST);
665 /* Check file permissions */
666 if( readRequested && !(obj->yst_mode & S_IREAD))
669 if( writeRequested && !(obj->yst_mode & S_IWRITE))
672 if(openDenied && !errorReported ) {
673 /* Error if the file exists but permissions are refused. */
674 yaffsfs_SetError(-EACCES);
678 /* Check sharing of an existing object. */
682 sharedReadAllowed = 1;
683 sharedWriteAllowed = 1;
686 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
687 hx = &yaffsfs_handle[i];
688 if(hx->useCount > 0 &&
690 yaffsfs_inode[hx->inodeId].iObj == obj){
692 sharedReadAllowed = 0;
694 sharedWriteAllowed = 0;
704 if((!sharedReadAllowed && readRequested)||
705 (!shareRead && alreadyReading) ||
706 (!sharedWriteAllowed && writeRequested) ||
707 (!shareWrite && alreadyWriting)){
709 yaffsfs_SetError(-EBUSY);
716 /* If we could not open an existing object, then let's see if
717 * the directory exists. If not, error.
719 if(!obj && !errorReported){
720 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
722 yaffsfs_SetError(-ENOTDIR);
727 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
728 /* Let's see if we can create this file if it does not exist. */
729 if(dir->my_dev->read_only){
730 yaffsfs_SetError(-EINVAL);
733 obj = yaffs_create_file(dir,name,mode,0,0);
735 if(!obj && !errorReported){
736 yaffsfs_SetError(-ENOSPC);
741 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
742 /* Error if the file does not exist and CREAT is not set. */
743 yaffsfs_SetError(-ENOENT);
747 if(obj && !openDenied) {
748 int inodeId = yaffsfs_GetInodeIdForObject(obj);
752 * Todo: Fix any problem if inodes run out, though that
753 * can't happen if the number of inode items >= number of handles.
757 yh->inodeId = inodeId;
758 yh->reading = readRequested;
759 yh->writing = writeRequested;
760 yh->append = (oflag & O_APPEND) ? 1 : 0;
762 yh->shareRead = shareRead;
763 yh->shareWrite = shareWrite;
765 /* Hook inode to object */
766 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
768 if((oflag & O_TRUNC) && yh->writing)
769 yaffs_resize_file(obj,0);
771 yaffsfs_PutHandle(handle);
773 yaffsfs_SetError(0); /* Problem */
783 int yaffs_open(const YCHAR *path, int oflag, int mode)
785 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
788 int yaffs_Dofsync(int fd,int datasync)
790 yaffsfs_Handle *h = NULL;
795 h = yaffsfs_GetHandlePointer(fd);
797 if(h && h->useCount > 0)
799 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
802 yaffsfs_SetError(-EBADF);
811 int yaffs_fsync(int fd)
813 return yaffs_Dofsync(fd,0);
816 int yaffs_flush(int fd)
818 return yaffs_fsync(fd);
821 int yaffs_fdatasync(int fd)
823 return yaffs_Dofsync(fd,1);
826 int yaffs_close(int fd)
828 yaffsfs_Handle *h = NULL;
833 h = yaffsfs_GetHandlePointer(fd);
835 if(h && h->useCount > 0) {
837 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
838 yaffsfs_PutHandle(fd);
842 yaffsfs_SetError(-EBADF);
853 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
855 yaffsfs_Handle *h = NULL;
856 struct yaffs_obj *obj = NULL;
862 unsigned int maxRead;
863 u8 *buf = (u8 *)vbuf;
866 h = yaffsfs_GetHandlePointer(fd);
867 obj = yaffsfs_GetHandleObject(fd);
871 yaffsfs_SetError(-EBADF);
873 } else if(!h->reading){
874 /* Not a reading handle */
875 yaffsfs_SetError(-EINVAL);
881 startPos = h->position;
885 if(yaffs_get_obj_length(obj) > pos)
886 maxRead = yaffs_get_obj_length(obj) - pos;
894 yaffsfs_GetHandle(fd);
897 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
900 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
911 nbyte = 0; /* no more to read */
921 yaffsfs_PutHandle(fd);
925 h->position = startPos + totalRead;
935 return (totalRead >= 0) ? totalRead : -1;
939 int yaffs_read(int fd, void *buf, unsigned int nbyte)
941 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
944 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
946 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
949 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
951 yaffsfs_Handle *h = NULL;
952 struct yaffs_obj *obj = NULL;
956 int totalWritten = 0;
957 int write_trhrough = 0;
959 const u8 *buf = (const u8 *)vbuf;
962 h = yaffsfs_GetHandlePointer(fd);
963 obj = yaffsfs_GetHandleObject(fd);
967 yaffsfs_SetError(-EBADF);
969 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
970 yaffsfs_SetError(-EINVAL);
974 startPos = yaffs_get_obj_length(obj);
978 startPos = h->position;
980 yaffsfs_GetHandle(fd);
983 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
987 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,write_trhrough);
989 totalWritten += nWritten;
994 if(nWritten == nToWrite)
999 if(nWritten < 1 && totalWritten < 1){
1000 yaffsfs_SetError(-ENOSPC);
1010 yaffsfs_PutHandle(fd);
1013 if(totalWritten > 0)
1014 h->position = startPos + totalWritten;
1023 return (totalWritten >= 0) ? totalWritten : -1;
1026 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1028 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1031 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1033 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1037 int yaffs_truncate(const YCHAR *path,off_t new_size)
1039 struct yaffs_obj *obj = NULL;
1040 int result = YAFFS_FAIL;
1044 obj = yaffsfs_FindObject(NULL,path,0,1);
1047 yaffsfs_SetError(-ENOENT);
1048 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1049 yaffsfs_SetError(-EISDIR);
1050 else if(obj->my_dev->read_only)
1051 yaffsfs_SetError(-EINVAL);
1053 result = yaffs_resize_file(obj,new_size);
1058 return (result) ? 0 : -1;
1061 int yaffs_ftruncate(int fd, off_t new_size)
1063 yaffsfs_Handle *h = NULL;
1064 struct yaffs_obj *obj = NULL;
1068 h = yaffsfs_GetHandlePointer(fd);
1069 obj = yaffsfs_GetHandleObject(fd);
1073 yaffsfs_SetError(-EBADF);
1074 else if(obj->my_dev->read_only)
1075 yaffsfs_SetError(-EINVAL);
1077 /* resize the file */
1078 result = yaffs_resize_file(obj,new_size);
1082 return (result) ? 0 : -1;
1086 off_t yaffs_lseek(int fd, off_t offset, int whence)
1088 yaffsfs_Handle *h = NULL;
1089 struct yaffs_obj *obj = NULL;
1094 h = yaffsfs_GetHandlePointer(fd);
1095 obj = yaffsfs_GetHandleObject(fd);
1099 yaffsfs_SetError(-EBADF);
1101 if(whence == SEEK_SET){
1104 } else if(whence == SEEK_CUR) {
1105 if( (h->position + offset) >= 0)
1106 pos = (h->position + offset);
1107 } else if(whence == SEEK_END) {
1108 fSize = yaffs_get_obj_length(obj);
1109 if(fSize >= 0 && (fSize + offset) >= 0)
1110 pos = fSize + offset;
1116 yaffsfs_SetError(-EINVAL);
1125 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1127 struct yaffs_obj *dir = NULL;
1128 struct yaffs_obj *obj = NULL;
1130 int result = YAFFS_FAIL;
1134 obj = yaffsfs_FindObject(NULL,path,0,0);
1135 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1137 yaffsfs_SetError(-ENOTDIR);
1139 yaffsfs_SetError(-ENOENT);
1140 else if(obj->my_dev->read_only)
1141 yaffsfs_SetError(-EINVAL);
1142 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1143 yaffsfs_SetError(-EISDIR);
1144 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1145 yaffsfs_SetError(-ENOTDIR);
1147 result = yaffs_unlinker(dir,name);
1149 if(result == YAFFS_FAIL && isDirectory)
1150 yaffsfs_SetError(-ENOTEMPTY);
1157 return (result == YAFFS_FAIL) ? -1 : 0;
1161 int yaffs_rmdir(const YCHAR *path)
1163 return yaffsfs_DoUnlink(path,1);
1166 int yaffs_unlink(const YCHAR *path)
1168 return yaffsfs_DoUnlink(path,0);
1171 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1173 struct yaffs_obj *olddir = NULL;
1174 struct yaffs_obj *newdir = NULL;
1175 struct yaffs_obj *obj = NULL;
1178 int result= YAFFS_FAIL;
1179 int rename_allowed = 1;
1183 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1184 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1185 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1187 if(!olddir || !newdir || !obj) {
1189 yaffsfs_SetError(-EBADF);
1191 } else if(obj->my_dev->read_only){
1192 yaffsfs_SetError(-EINVAL);
1194 } else if(olddir->my_dev != newdir->my_dev) {
1195 /* oops must be on same device */
1197 yaffsfs_SetError(-EXDEV);
1199 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1201 * It is a directory, check that it is not being renamed to
1202 * being its own decendent.
1203 * Do this by tracing from the new directory back to the root, checking for obj
1206 struct yaffs_obj *xx = newdir;
1208 while( rename_allowed && xx){
1214 yaffsfs_SetError(-EACCES);
1218 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1222 return (result == YAFFS_FAIL) ? -1 : 0;
1226 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1230 obj = yaffs_get_equivalent_obj(obj);
1233 buf->st_dev = (int)obj->my_dev->os_context;
1234 buf->st_ino = obj->obj_id;
1235 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1237 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1238 buf->st_mode |= S_IFDIR;
1239 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1240 buf->st_mode |= S_IFLNK;
1241 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1242 buf->st_mode |= S_IFREG;
1244 buf->st_nlink = yaffs_get_obj_link_count(obj);
1247 buf->st_rdev = obj->yst_rdev;
1248 buf->st_size = yaffs_get_obj_length(obj);
1249 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1250 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1251 #if CONFIG_YAFFS_WINCE
1252 buf->yst_wince_atime[0] = obj->win_atime[0];
1253 buf->yst_wince_atime[1] = obj->win_atime[1];
1254 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1255 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1256 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1257 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1259 buf->yst_atime = obj->yst_atime;
1260 buf->yst_ctime = obj->yst_ctime;
1261 buf->yst_mtime = obj->yst_mtime;
1268 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1270 struct yaffs_obj *obj;
1276 obj = yaffsfs_FindObject(NULL,path,0,1);
1279 obj = yaffsfs_FollowLink(obj,0);
1282 retVal = yaffsfs_DoStat(obj,buf);
1284 /* todo error not found */
1285 yaffsfs_SetError(-ENOENT);
1293 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1295 return yaffsfs_DoStatOrLStat(path,buf,0);
1298 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1300 return yaffsfs_DoStatOrLStat(path,buf,1);
1303 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1305 struct yaffs_obj *obj;
1310 obj = yaffsfs_GetHandleObject(fd);
1313 retVal = yaffsfs_DoStat(obj,buf);
1316 yaffsfs_SetError(-EBADF);
1323 #ifndef CONFIG_YAFFS_WINCE
1324 /* xattrib functions */
1327 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1329 struct yaffs_obj *obj;
1335 obj = yaffsfs_FindObject(NULL,path,0,1);
1338 obj = yaffsfs_FollowLink(obj,0);
1341 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1343 yaffsfs_SetError(retVal);
1347 /* todo error not found */
1348 yaffsfs_SetError(-ENOENT);
1356 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1358 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1361 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1363 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1368 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1370 struct yaffs_obj *obj;
1375 obj = yaffsfs_GetHandleObject(fd);
1378 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1380 yaffsfs_SetError(retVal);
1385 yaffsfs_SetError(-EBADF);
1392 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1394 struct yaffs_obj *obj;
1400 obj = yaffsfs_FindObject(NULL,path,0,1);
1403 obj = yaffsfs_FollowLink(obj,0);
1406 retVal = yaffs_get_xattrib(obj,name,data,size);
1408 yaffsfs_SetError(retVal);
1412 /* todo error not found */
1413 yaffsfs_SetError(-ENOENT);
1421 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1423 return yaffs_do_getxattr( path, name, data, size, 1);
1425 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1427 return yaffs_do_getxattr( path, name, data, size, 0);
1432 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1434 struct yaffs_obj *obj;
1439 obj = yaffsfs_GetHandleObject(fd);
1442 retVal = yaffs_get_xattrib(obj,name,data,size);
1444 yaffsfs_SetError(retVal);
1449 yaffsfs_SetError(-EBADF);
1456 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1458 struct yaffs_obj *obj;
1464 obj = yaffsfs_FindObject(NULL,path,0,1);
1467 obj = yaffsfs_FollowLink(obj,0);
1470 retVal = yaffs_list_xattrib(obj, data,size);
1472 yaffsfs_SetError(retVal);
1476 /* todo error not found */
1477 yaffsfs_SetError(-ENOENT);
1485 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1487 return yaffs_do_listxattr(path, data, size, 1);
1490 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1492 return yaffs_do_listxattr(path, data, size, 0);
1495 int yaffs_flistxattr(int fd, char *data, int size)
1497 struct yaffs_obj *obj;
1502 obj = yaffsfs_GetHandleObject(fd);
1505 retVal = yaffs_list_xattrib(obj,data,size);
1507 yaffsfs_SetError(retVal);
1512 yaffsfs_SetError(-EBADF);
1519 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1521 struct yaffs_obj *obj;
1527 obj = yaffsfs_FindObject(NULL,path,0,1);
1530 obj = yaffsfs_FollowLink(obj,0);
1533 retVal = yaffs_remove_xattrib(obj,name);
1535 yaffsfs_SetError(retVal);
1539 /* todo error not found */
1540 yaffsfs_SetError(-ENOENT);
1548 int yaffs_removexattr(const YCHAR *path, const char *name)
1550 return yaffs_do_removexattr(path, name, 1);
1553 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1555 return yaffs_do_removexattr(path, name, 0);
1558 int yaffs_fremovexattr(int fd, const char *name)
1560 struct yaffs_obj *obj;
1565 obj = yaffsfs_GetHandleObject(fd);
1568 retVal = yaffs_remove_xattrib(obj,name);
1570 yaffsfs_SetError(retVal);
1575 yaffsfs_SetError(-EBADF);
1583 #ifdef CONFIG_YAFFS_WINCE
1584 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1586 struct yaffs_obj *obj;
1591 obj = yaffsfs_GetHandleObject(fd);
1596 wctime[0] = obj->win_ctime[0];
1597 wctime[1] = obj->win_ctime[1];
1600 watime[0] = obj->win_atime[0];
1601 watime[1] = obj->win_atime[1];
1604 wmtime[0] = obj->win_mtime[0];
1605 wmtime[1] = obj->win_mtime[1];
1612 yaffsfs_SetError(-EBADF);
1620 int yaffs_set_wince_times(int fd,
1621 const unsigned *wctime,
1622 const unsigned *watime,
1623 const unsigned *wmtime)
1625 struct yaffs_obj *obj;
1630 obj = yaffsfs_GetHandleObject(fd);
1635 obj->win_ctime[0] = wctime[0];
1636 obj->win_ctime[1] = wctime[1];
1639 obj->win_atime[0] = watime[0];
1640 obj->win_atime[1] = watime[1];
1643 obj->win_mtime[0] = wmtime[0];
1644 obj->win_mtime[1] = wmtime[1];
1648 result = yaffs_flush_file(obj,0,0);
1652 yaffsfs_SetError(-EBADF);
1662 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1667 obj = yaffs_get_equivalent_obj(obj);
1670 obj->yst_mode = mode;
1672 result = yaffs_flush_file(obj,0,0);
1675 return result == YAFFS_OK ? 0 : -1;
1679 int yaffs_access(const YCHAR *path, int amode)
1681 struct yaffs_obj *obj;
1687 obj = yaffsfs_FindObject(NULL,path,0,1);
1692 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1694 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1696 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1700 yaffsfs_SetError(-EACCES);
1704 /* todo error not found */
1705 yaffsfs_SetError(-ENOENT);
1716 int yaffs_chmod(const YCHAR *path, mode_t mode)
1718 struct yaffs_obj *obj;
1724 obj = yaffsfs_FindObject(NULL,path,0,1);
1727 yaffsfs_SetError(-ENOENT);
1728 else if(obj->my_dev->read_only)
1729 yaffsfs_SetError(-EINVAL);
1731 retVal = yaffsfs_DoChMod(obj,mode);
1740 int yaffs_fchmod(int fd, mode_t mode)
1742 struct yaffs_obj *obj;
1747 obj = yaffsfs_GetHandleObject(fd);
1750 yaffsfs_SetError(-ENOENT);
1751 else if(obj->my_dev->read_only)
1752 yaffsfs_SetError(-EINVAL);
1754 retVal = yaffsfs_DoChMod(obj,mode);
1762 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1764 struct yaffs_obj *parent = NULL;
1765 struct yaffs_obj *dir = NULL;
1767 YCHAR *use_path = NULL;
1768 int path_length = 0;
1774 * We don't have a definition for max path length.
1775 * We will use 3 * max name length instead.
1778 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1780 /* If the last character is a path divider, then we need to
1781 * trim it back so that the name look-up works properly.
1782 * eg. /foo/new_dir/ -> /foo/newdir
1783 * Curveball: Need to handle multiple path dividers:
1784 * eg. /foof/sdfse///// -> /foo/sdfse
1786 if(path_length > 0 &&
1787 yaffsfs_IsPathDivider(path[path_length-1])){
1788 use_path = YMALLOC(path_length + 1);
1790 yaffsfs_SetError(-ENOMEM);
1793 strcpy(use_path, path);
1794 for(i = path_length-1;
1795 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1797 use_path[i] = (YCHAR) 0;
1802 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1803 if(parent && yaffs_strnlen(name,5) == 0){
1804 /* Trying to make the root itself */
1805 yaffsfs_SetError(-EEXIST);
1806 } else if(parent && parent->my_dev->read_only){
1807 yaffsfs_SetError(-EINVAL);
1810 dir = yaffs_create_dir(parent,name,mode,0,0);
1815 yaffsfs_SetError(-ENOENT); /* missing path */
1816 else if (yaffs_find_by_name(parent,name))
1817 yaffsfs_SetError(-EEXIST); /* the name already exists */
1819 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1832 void * yaffs_getdev(const YCHAR *path)
1834 struct yaffs_dev *dev=NULL;
1836 dev = yaffsfs_FindDevice(path,&dummy);
1840 int yaffs_mount2(const YCHAR *path,int read_only)
1843 int result=YAFFS_FAIL;
1844 struct yaffs_dev *dev=NULL;
1847 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1851 yaffsfs_InitHandles();
1853 dev = yaffsfs_FindDevice(path,&dummy);
1855 if(!dev->is_mounted){
1856 dev->read_only = read_only ? 1 : 0;
1857 result = yaffs_guts_initialise(dev);
1858 if(result == YAFFS_FAIL)
1859 /* todo error - mount failed */
1860 yaffsfs_SetError(-ENOMEM);
1861 retVal = result ? 0 : -1;
1865 /* todo error - already mounted. */
1866 yaffsfs_SetError(-EBUSY);
1868 /* todo error - no device */
1869 yaffsfs_SetError(-ENODEV);
1876 int yaffs_mount(const YCHAR *path)
1878 return yaffs_mount2(path,0);
1881 int yaffs_sync(const YCHAR *path)
1884 struct yaffs_dev *dev=NULL;
1888 dev = yaffsfs_FindDevice(path,&dummy);
1890 if(dev->is_mounted){
1892 yaffs_flush_whole_cache(dev);
1893 yaffs_checkpoint_save(dev);
1897 /* todo error - not mounted. */
1898 yaffsfs_SetError(-EINVAL);
1901 /* todo error - no device */
1902 yaffsfs_SetError(-ENODEV);
1909 int yaffs_remount(const YCHAR *path, int force, int read_only)
1912 struct yaffs_dev *dev=NULL;
1916 dev = yaffsfs_FindDevice(path,&dummy);
1918 if(dev->is_mounted){
1922 yaffs_flush_whole_cache(dev);
1924 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1925 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1926 inUse = 1; /* the device is in use, can't unmount */
1929 if(!inUse || force){
1931 yaffs_checkpoint_save(dev);
1932 dev->read_only = read_only ? 1 : 0;
1935 yaffsfs_SetError(-EBUSY);
1938 yaffsfs_SetError(-EINVAL);
1942 yaffsfs_SetError(-ENODEV);
1949 int yaffs_unmount2(const YCHAR *path, int force)
1952 struct yaffs_dev *dev=NULL;
1956 dev = yaffsfs_FindDevice(path,&dummy);
1958 if(dev->is_mounted){
1962 yaffs_flush_whole_cache(dev);
1963 yaffs_checkpoint_save(dev);
1965 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1966 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1967 inUse = 1; /* the device is in use, can't unmount */
1970 if(!inUse || force){
1971 yaffs_deinitialise(dev);
1975 /* todo error can't unmount as files are open */
1976 yaffsfs_SetError(-EBUSY);
1979 /* todo error - not mounted. */
1980 yaffsfs_SetError(-EINVAL);
1984 /* todo error - no device */
1985 yaffsfs_SetError(-ENODEV);
1992 int yaffs_unmount(const YCHAR *path)
1994 return yaffs_unmount2(path,0);
1997 loff_t yaffs_freespace(const YCHAR *path)
2000 struct yaffs_dev *dev=NULL;
2004 dev = yaffsfs_FindDevice(path,&dummy);
2005 if(dev && dev->is_mounted){
2006 retVal = yaffs_get_n_free_chunks(dev);
2007 retVal *= dev->data_bytes_per_chunk;
2010 yaffsfs_SetError(-EINVAL);
2016 loff_t yaffs_totalspace(const YCHAR *path)
2019 struct yaffs_dev *dev=NULL;
2023 dev = yaffsfs_FindDevice(path,&dummy);
2024 if(dev && dev->is_mounted){
2025 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2026 retVal *= dev->param.chunks_per_block;
2027 retVal *= dev->data_bytes_per_chunk;
2030 yaffsfs_SetError(-EINVAL);
2036 int yaffs_inodecount(const YCHAR *path)
2039 struct yaffs_dev *dev=NULL;
2043 dev = yaffsfs_FindDevice(path,&dummy);
2044 if(dev && dev->is_mounted) {
2045 int n_obj = dev->n_obj;
2046 if(n_obj > dev->n_hardlinks)
2047 retVal = n_obj - dev->n_hardlinks;
2051 yaffsfs_SetError(-EINVAL);
2058 void yaffs_add_device(struct yaffs_dev *dev)
2060 dev->is_mounted = 0;
2061 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2063 if(!dev->dev_list.next)
2064 INIT_LIST_HEAD(&dev->dev_list);
2066 list_add(&dev->dev_list,&yaffsfs_deviceList);
2069 void yaffs_remove_device(struct yaffs_dev *dev)
2071 list_del_init(&dev->dev_list);
2077 /* Directory search stuff. */
2080 * Directory search context
2082 * NB this is an opaque structure.
2089 yaffs_dirent de; /* directory entry being used by this dsc */
2090 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2091 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2092 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2094 struct list_head others;
2095 } yaffsfs_DirectorySearchContext;
2099 static struct list_head search_contexts;
2102 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2106 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2110 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2111 dsc->nextReturn = NULL;
2113 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2114 struct yaffs_obj,siblings);
2116 /* Hey someone isn't playing nice! */
2120 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2124 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2126 if( dsc->nextReturn == NULL ||
2127 list_empty(&dsc->dirObj->variant.dir_variant.children))
2128 dsc->nextReturn = NULL;
2130 struct list_head *next = dsc->nextReturn->siblings.next;
2132 if( next == &dsc->dirObj->variant.dir_variant.children)
2133 dsc->nextReturn = NULL; /* end of list */
2135 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2138 /* Hey someone isn't playing nice! */
2142 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2145 struct list_head *i;
2146 yaffsfs_DirectorySearchContext *dsc;
2148 /* if search contexts not initilised then skip */
2149 if(!search_contexts.next)
2152 /* Iterate through the directory search contexts.
2153 * If any are the one being removed, then advance the dsc to
2154 * the next one to prevent a hanging ptr.
2156 list_for_each(i, &search_contexts) {
2158 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2159 if(dsc->nextReturn == obj)
2160 yaffsfs_DirAdvance(dsc);
2166 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2168 yaffs_DIR *dir = NULL;
2169 struct yaffs_obj *obj = NULL;
2170 yaffsfs_DirectorySearchContext *dsc = NULL;
2174 obj = yaffsfs_FindObject(NULL,dirname,0,1);
2176 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2178 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2179 dir = (yaffs_DIR *)dsc;
2182 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2183 dsc->magic = YAFFS_MAGIC;
2185 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2186 INIT_LIST_HEAD(&dsc->others);
2188 if(!search_contexts.next)
2189 INIT_LIST_HEAD(&search_contexts);
2191 list_add(&dsc->others,&search_contexts);
2192 yaffsfs_SetDirRewound(dsc);
2202 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2204 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2205 struct yaffs_dirent *retVal = NULL;
2209 if(dsc && dsc->magic == YAFFS_MAGIC){
2210 yaffsfs_SetError(0);
2211 if(dsc->nextReturn){
2212 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2213 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2214 dsc->de.d_off = dsc->offset++;
2215 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2216 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2218 /* this should not happen! */
2219 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2221 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2223 yaffsfs_DirAdvance(dsc);
2227 yaffsfs_SetError(-EBADF);
2236 void yaffs_rewinddir(yaffs_DIR *dirp)
2238 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2242 yaffsfs_SetDirRewound(dsc);
2248 int yaffs_closedir(yaffs_DIR *dirp)
2250 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2254 list_del(&dsc->others); /* unhook from list */
2260 /* End of directory stuff */
2263 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2265 struct yaffs_obj *parent = NULL;
2266 struct yaffs_obj *obj;
2269 int mode = 0; /* ignore for now */
2272 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2273 if(parent && parent->my_dev->read_only)
2274 yaffsfs_SetError(-EINVAL);
2276 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2280 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2284 yaffsfs_SetError(-EINVAL);
2294 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2296 struct yaffs_obj *obj = NULL;
2302 obj = yaffsfs_FindObject(NULL,path,0,1);
2305 yaffsfs_SetError(-ENOENT);
2307 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2308 yaffsfs_SetError(-EINVAL);
2311 YCHAR *alias = obj->variant.symlink_variant.alias;
2312 memset(buf,0,bufsiz);
2313 yaffs_strncpy(buf,alias,bufsiz - 1);
2320 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2322 /* Creates a link called newpath to existing oldpath */
2323 struct yaffs_obj *obj = NULL;
2324 struct yaffs_obj *target = NULL;
2326 int new_nameLength = 0;
2331 obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2332 target = yaffsfs_FindObject(NULL,newpath,0,0);
2335 yaffsfs_SetError(-ENOENT);
2337 } else if(obj->my_dev->read_only){
2338 yaffsfs_SetError(-EINVAL);
2341 yaffsfs_SetError(-EEXIST);
2344 struct yaffs_obj *newdir = NULL;
2345 struct yaffs_obj *link = NULL;
2349 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2352 yaffsfs_SetError(-ENOTDIR);
2354 }else if(newdir->my_dev != obj->my_dev){
2355 yaffsfs_SetError(-EXDEV);
2359 new_nameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2361 if(new_nameLength == 0){
2362 yaffsfs_SetError(-ENOENT);
2364 } else if (new_nameLength > YAFFS_MAX_NAME_LENGTH){
2365 yaffsfs_SetError(-ENAMETOOLONG);
2370 link = yaffs_link_obj(newdir,newname,obj);
2374 yaffsfs_SetError(-ENOSPC);
2385 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2394 * Returns number of handles attached to the object
2396 int yaffs_n_handles(const YCHAR *path)
2398 struct yaffs_obj *obj;
2400 obj = yaffsfs_FindObject(NULL,path,0,1);
2402 return yaffsfs_CountHandles(obj);
2405 int yaffs_get_error(void)
2407 return yaffsfs_GetLastError();
2410 int yaffs_set_error(int error)
2412 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2413 yaffsfs_SetError(error);
2417 int yaffs_dump_dev(const YCHAR *path)
2422 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2425 struct yaffs_dev *dev = obj->my_dev;
2428 "n_page_writes.......... %d\n"
2429 "n_page_reads........... %d\n"
2430 "n_erasures....... %d\n"
2431 "n_gc_copies............ %d\n"
2432 "garbageCollections... %d\n"
2433 "passiveGarbageColl'ns %d\n"
2439 dev->garbageCollections,
2440 dev->passiveGarbageCollections