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)
646 /* The file already exists or it might be a directory */
648 /* If it is a directory then we can't open it as a file */
649 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
651 yaffsfs_SetError(-EISDIR);
655 /* Open should fail if O_CREAT and O_EXCL are specified since
658 if((oflag & O_EXCL) && (oflag & O_CREAT)){
660 yaffsfs_SetError(-EEXIST);
664 /* Check file permissions */
665 if( readRequested && !(obj->yst_mode & S_IREAD))
668 if( writeRequested && !(obj->yst_mode & S_IWRITE))
671 /* Check sharing of an existing object. */
675 sharedReadAllowed = 1;
676 sharedWriteAllowed = 1;
679 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
680 hx = &yaffsfs_handle[i];
681 if(hx->useCount > 0 &&
683 yaffsfs_inode[hx->inodeId].iObj == obj){
685 sharedReadAllowed = 0;
687 sharedWriteAllowed = 0;
697 if((!sharedReadAllowed && readRequested)||
698 (!shareRead && alreadyReading) ||
699 (!sharedWriteAllowed && writeRequested) ||
700 (!shareWrite && alreadyWriting)){
702 yaffsfs_SetError(-EBUSY);
707 } else if((oflag & O_CREAT)) {
708 /* Let's see if we can create this file */
709 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
710 if(dir && dir->my_dev->read_only){
711 yaffsfs_SetError(-EINVAL);
714 obj = yaffs_create_file(dir,name,mode,0,0);
716 yaffsfs_SetError(-ENOTDIR);
721 if(obj && !openDenied) {
722 int inodeId = yaffsfs_GetInodeIdForObject(obj);
726 * Todo: Fix any problem if inodes run out, though that
727 * can't happen if the number of inode items >= number of handles.
731 yh->inodeId = inodeId;
732 yh->reading = readRequested;
733 yh->writing = writeRequested;
734 yh->append = (oflag & O_APPEND) ? 1 : 0;
736 yh->shareRead = shareRead;
737 yh->shareWrite = shareWrite;
739 /* Hook inode to object */
740 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
742 if((oflag & O_TRUNC) && yh->writing)
743 yaffs_resize_file(obj,0);
745 yaffsfs_PutHandle(handle);
747 yaffsfs_SetError(!obj ? -ENOSPC : -EACCES);
759 int yaffs_open(const YCHAR *path, int oflag, int mode)
761 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
764 int yaffs_Dofsync(int fd,int datasync)
766 yaffsfs_Handle *h = NULL;
771 h = yaffsfs_GetHandlePointer(fd);
773 if(h && h->useCount > 0)
775 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
778 yaffsfs_SetError(-EBADF);
787 int yaffs_fsync(int fd)
789 return yaffs_Dofsync(fd,0);
792 int yaffs_flush(int fd)
794 return yaffs_fsync(fd);
797 int yaffs_fdatasync(int fd)
799 return yaffs_Dofsync(fd,1);
802 int yaffs_close(int fd)
804 yaffsfs_Handle *h = NULL;
809 h = yaffsfs_GetHandlePointer(fd);
811 if(h && h->useCount > 0) {
813 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
814 yaffsfs_PutHandle(fd);
818 yaffsfs_SetError(-EBADF);
829 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
831 yaffsfs_Handle *h = NULL;
832 struct yaffs_obj *obj = NULL;
838 unsigned int maxRead;
839 u8 *buf = (u8 *)vbuf;
842 h = yaffsfs_GetHandlePointer(fd);
843 obj = yaffsfs_GetHandleObject(fd);
847 yaffsfs_SetError(-EBADF);
849 } else if(!h->reading){
850 /* Not a reading handle */
851 yaffsfs_SetError(-EINVAL);
857 startPos = h->position;
861 if(yaffs_get_obj_length(obj) > pos)
862 maxRead = yaffs_get_obj_length(obj) - pos;
870 yaffsfs_GetHandle(fd);
873 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
876 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
887 nbyte = 0; /* no more to read */
897 yaffsfs_PutHandle(fd);
901 h->position = startPos + totalRead;
911 return (totalRead >= 0) ? totalRead : -1;
915 int yaffs_read(int fd, void *buf, unsigned int nbyte)
917 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
920 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
922 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
925 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
927 yaffsfs_Handle *h = NULL;
928 struct yaffs_obj *obj = NULL;
932 int totalWritten = 0;
933 int write_trhrough = 0;
935 const u8 *buf = (const u8 *)vbuf;
938 h = yaffsfs_GetHandlePointer(fd);
939 obj = yaffsfs_GetHandleObject(fd);
943 yaffsfs_SetError(-EBADF);
945 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
946 yaffsfs_SetError(-EINVAL);
950 startPos = yaffs_get_obj_length(obj);
954 startPos = h->position;
956 yaffsfs_GetHandle(fd);
959 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
963 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,write_trhrough);
965 totalWritten += nWritten;
970 if(nWritten == nToWrite)
975 if(nWritten < 1 && totalWritten < 1){
976 yaffsfs_SetError(-ENOSPC);
986 yaffsfs_PutHandle(fd);
990 h->position = startPos + totalWritten;
999 return (totalWritten >= 0) ? totalWritten : -1;
1002 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1004 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1007 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1009 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1013 int yaffs_truncate(const YCHAR *path,off_t new_size)
1015 struct yaffs_obj *obj = NULL;
1016 int result = YAFFS_FAIL;
1020 obj = yaffsfs_FindObject(NULL,path,0,1);
1023 yaffsfs_SetError(-ENOENT);
1024 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1025 yaffsfs_SetError(-EISDIR);
1026 else if(obj->my_dev->read_only)
1027 yaffsfs_SetError(-EINVAL);
1029 result = yaffs_resize_file(obj,new_size);
1034 return (result) ? 0 : -1;
1037 int yaffs_ftruncate(int fd, off_t new_size)
1039 yaffsfs_Handle *h = NULL;
1040 struct yaffs_obj *obj = NULL;
1044 h = yaffsfs_GetHandlePointer(fd);
1045 obj = yaffsfs_GetHandleObject(fd);
1049 yaffsfs_SetError(-EBADF);
1050 else if(obj->my_dev->read_only)
1051 yaffsfs_SetError(-EINVAL);
1053 /* resize the file */
1054 result = yaffs_resize_file(obj,new_size);
1058 return (result) ? 0 : -1;
1062 off_t yaffs_lseek(int fd, off_t offset, int whence)
1064 yaffsfs_Handle *h = NULL;
1065 struct yaffs_obj *obj = NULL;
1070 h = yaffsfs_GetHandlePointer(fd);
1071 obj = yaffsfs_GetHandleObject(fd);
1075 yaffsfs_SetError(-EBADF);
1077 if(whence == SEEK_SET){
1080 } else if(whence == SEEK_CUR) {
1081 if( (h->position + offset) >= 0)
1082 pos = (h->position + offset);
1083 } else if(whence == SEEK_END) {
1084 fSize = yaffs_get_obj_length(obj);
1085 if(fSize >= 0 && (fSize + offset) >= 0)
1086 pos = fSize + offset;
1092 yaffsfs_SetError(-EINVAL);
1101 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1103 struct yaffs_obj *dir = NULL;
1104 struct yaffs_obj *obj = NULL;
1106 int result = YAFFS_FAIL;
1110 obj = yaffsfs_FindObject(NULL,path,0,0);
1111 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1113 yaffsfs_SetError(-ENOTDIR);
1115 yaffsfs_SetError(-ENOENT);
1116 else if(obj->my_dev->read_only)
1117 yaffsfs_SetError(-EINVAL);
1118 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1119 yaffsfs_SetError(-EISDIR);
1120 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1121 yaffsfs_SetError(-ENOTDIR);
1123 result = yaffs_unlinker(dir,name);
1125 if(result == YAFFS_FAIL && isDirectory)
1126 yaffsfs_SetError(-ENOTEMPTY);
1133 return (result == YAFFS_FAIL) ? -1 : 0;
1137 int yaffs_rmdir(const YCHAR *path)
1139 return yaffsfs_DoUnlink(path,1);
1142 int yaffs_unlink(const YCHAR *path)
1144 return yaffsfs_DoUnlink(path,0);
1147 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1149 struct yaffs_obj *olddir = NULL;
1150 struct yaffs_obj *newdir = NULL;
1151 struct yaffs_obj *obj = NULL;
1154 int result= YAFFS_FAIL;
1155 int rename_allowed = 1;
1159 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1160 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1161 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1163 if(!olddir || !newdir || !obj) {
1165 yaffsfs_SetError(-EBADF);
1167 } else if(obj->my_dev->read_only){
1168 yaffsfs_SetError(-EINVAL);
1170 } else if(olddir->my_dev != newdir->my_dev) {
1171 /* oops must be on same device */
1173 yaffsfs_SetError(-EXDEV);
1175 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1177 * It is a directory, check that it is not being renamed to
1178 * being its own decendent.
1179 * Do this by tracing from the new directory back to the root, checking for obj
1182 struct yaffs_obj *xx = newdir;
1184 while( rename_allowed && xx){
1190 yaffsfs_SetError(-EACCES);
1194 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1198 return (result == YAFFS_FAIL) ? -1 : 0;
1202 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1206 obj = yaffs_get_equivalent_obj(obj);
1209 buf->st_dev = (int)obj->my_dev->os_context;
1210 buf->st_ino = obj->obj_id;
1211 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1213 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1214 buf->st_mode |= S_IFDIR;
1215 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1216 buf->st_mode |= S_IFLNK;
1217 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1218 buf->st_mode |= S_IFREG;
1220 buf->st_nlink = yaffs_get_obj_link_count(obj);
1223 buf->st_rdev = obj->yst_rdev;
1224 buf->st_size = yaffs_get_obj_length(obj);
1225 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1226 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1227 #if CONFIG_YAFFS_WINCE
1228 buf->yst_wince_atime[0] = obj->win_atime[0];
1229 buf->yst_wince_atime[1] = obj->win_atime[1];
1230 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1231 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1232 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1233 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1235 buf->yst_atime = obj->yst_atime;
1236 buf->yst_ctime = obj->yst_ctime;
1237 buf->yst_mtime = obj->yst_mtime;
1244 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1246 struct yaffs_obj *obj;
1252 obj = yaffsfs_FindObject(NULL,path,0,1);
1255 obj = yaffsfs_FollowLink(obj,0);
1258 retVal = yaffsfs_DoStat(obj,buf);
1260 /* todo error not found */
1261 yaffsfs_SetError(-ENOENT);
1269 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1271 return yaffsfs_DoStatOrLStat(path,buf,0);
1274 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1276 return yaffsfs_DoStatOrLStat(path,buf,1);
1279 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1281 struct yaffs_obj *obj;
1286 obj = yaffsfs_GetHandleObject(fd);
1289 retVal = yaffsfs_DoStat(obj,buf);
1292 yaffsfs_SetError(-EBADF);
1299 #ifndef CONFIG_YAFFS_WINCE
1300 /* xattrib functions */
1303 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1305 struct yaffs_obj *obj;
1311 obj = yaffsfs_FindObject(NULL,path,0,1);
1314 obj = yaffsfs_FollowLink(obj,0);
1317 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1319 yaffsfs_SetError(retVal);
1323 /* todo error not found */
1324 yaffsfs_SetError(-ENOENT);
1332 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1334 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1337 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1339 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1344 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1346 struct yaffs_obj *obj;
1351 obj = yaffsfs_GetHandleObject(fd);
1354 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1356 yaffsfs_SetError(retVal);
1361 yaffsfs_SetError(-EBADF);
1368 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1370 struct yaffs_obj *obj;
1376 obj = yaffsfs_FindObject(NULL,path,0,1);
1379 obj = yaffsfs_FollowLink(obj,0);
1382 retVal = yaffs_get_xattrib(obj,name,data,size);
1384 yaffsfs_SetError(retVal);
1388 /* todo error not found */
1389 yaffsfs_SetError(-ENOENT);
1397 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1399 return yaffs_do_getxattr( path, name, data, size, 1);
1401 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1403 return yaffs_do_getxattr( path, name, data, size, 0);
1408 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1410 struct yaffs_obj *obj;
1415 obj = yaffsfs_GetHandleObject(fd);
1418 retVal = yaffs_get_xattrib(obj,name,data,size);
1420 yaffsfs_SetError(retVal);
1425 yaffsfs_SetError(-EBADF);
1432 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1434 struct yaffs_obj *obj;
1440 obj = yaffsfs_FindObject(NULL,path,0,1);
1443 obj = yaffsfs_FollowLink(obj,0);
1446 retVal = yaffs_list_xattrib(obj, data,size);
1448 yaffsfs_SetError(retVal);
1452 /* todo error not found */
1453 yaffsfs_SetError(-ENOENT);
1461 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1463 return yaffs_do_listxattr(path, data, size, 1);
1466 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1468 return yaffs_do_listxattr(path, data, size, 0);
1471 int yaffs_flistxattr(int fd, char *data, int size)
1473 struct yaffs_obj *obj;
1478 obj = yaffsfs_GetHandleObject(fd);
1481 retVal = yaffs_list_xattrib(obj,data,size);
1483 yaffsfs_SetError(retVal);
1488 yaffsfs_SetError(-EBADF);
1495 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1497 struct yaffs_obj *obj;
1503 obj = yaffsfs_FindObject(NULL,path,0,1);
1506 obj = yaffsfs_FollowLink(obj,0);
1509 retVal = yaffs_remove_xattrib(obj,name);
1511 yaffsfs_SetError(retVal);
1515 /* todo error not found */
1516 yaffsfs_SetError(-ENOENT);
1524 int yaffs_removexattr(const YCHAR *path, const char *name)
1526 return yaffs_do_removexattr(path, name, 1);
1529 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1531 return yaffs_do_removexattr(path, name, 0);
1534 int yaffs_fremovexattr(int fd, const char *name)
1536 struct yaffs_obj *obj;
1541 obj = yaffsfs_GetHandleObject(fd);
1544 retVal = yaffs_remove_xattrib(obj,name);
1546 yaffsfs_SetError(retVal);
1551 yaffsfs_SetError(-EBADF);
1559 #ifdef CONFIG_YAFFS_WINCE
1560 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1562 struct yaffs_obj *obj;
1567 obj = yaffsfs_GetHandleObject(fd);
1572 wctime[0] = obj->win_ctime[0];
1573 wctime[1] = obj->win_ctime[1];
1576 watime[0] = obj->win_atime[0];
1577 watime[1] = obj->win_atime[1];
1580 wmtime[0] = obj->win_mtime[0];
1581 wmtime[1] = obj->win_mtime[1];
1588 yaffsfs_SetError(-EBADF);
1596 int yaffs_set_wince_times(int fd,
1597 const unsigned *wctime,
1598 const unsigned *watime,
1599 const unsigned *wmtime)
1601 struct yaffs_obj *obj;
1606 obj = yaffsfs_GetHandleObject(fd);
1611 obj->win_ctime[0] = wctime[0];
1612 obj->win_ctime[1] = wctime[1];
1615 obj->win_atime[0] = watime[0];
1616 obj->win_atime[1] = watime[1];
1619 obj->win_mtime[0] = wmtime[0];
1620 obj->win_mtime[1] = wmtime[1];
1624 result = yaffs_flush_file(obj,0,0);
1628 yaffsfs_SetError(-EBADF);
1638 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1643 obj = yaffs_get_equivalent_obj(obj);
1646 obj->yst_mode = mode;
1648 result = yaffs_flush_file(obj,0,0);
1651 return result == YAFFS_OK ? 0 : -1;
1655 int yaffs_access(const YCHAR *path, int amode)
1657 struct yaffs_obj *obj;
1663 obj = yaffsfs_FindObject(NULL,path,0,1);
1668 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1670 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1672 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1676 yaffsfs_SetError(-EACCES);
1680 /* todo error not found */
1681 yaffsfs_SetError(-ENOENT);
1692 int yaffs_chmod(const YCHAR *path, mode_t mode)
1694 struct yaffs_obj *obj;
1700 obj = yaffsfs_FindObject(NULL,path,0,1);
1703 yaffsfs_SetError(-ENOENT);
1704 else if(obj->my_dev->read_only)
1705 yaffsfs_SetError(-EINVAL);
1707 retVal = yaffsfs_DoChMod(obj,mode);
1716 int yaffs_fchmod(int fd, mode_t mode)
1718 struct yaffs_obj *obj;
1723 obj = yaffsfs_GetHandleObject(fd);
1726 yaffsfs_SetError(-ENOENT);
1727 else if(obj->my_dev->read_only)
1728 yaffsfs_SetError(-EINVAL);
1730 retVal = yaffsfs_DoChMod(obj,mode);
1738 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1740 struct yaffs_obj *parent = NULL;
1741 struct yaffs_obj *dir = NULL;
1743 YCHAR *use_path = NULL;
1744 int path_length = 0;
1750 * We don't have a definition for max path length.
1751 * We will use 3 * max name length instead.
1754 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1756 /* If the last character is a path divider, then we need to
1757 * trim it back so that the name look-up works properly.
1758 * eg. /foo/new_dir/ -> /foo/newdir
1759 * Curveball: Need to handle multiple path dividers:
1760 * eg. /foof/sdfse///// -> /foo/sdfse
1762 if(path_length > 0 &&
1763 yaffsfs_IsPathDivider(path[path_length-1])){
1764 use_path = YMALLOC(path_length + 1);
1766 yaffsfs_SetError(-ENOMEM);
1769 strcpy(use_path, path);
1770 for(i = path_length-1;
1771 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1773 use_path[i] = (YCHAR) 0;
1778 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1779 if(parent && yaffs_strnlen(name,5) == 0){
1780 /* Trying to make the root itself */
1781 yaffsfs_SetError(-EEXIST);
1782 } else if(parent && parent->my_dev->read_only){
1783 yaffsfs_SetError(-EINVAL);
1786 dir = yaffs_create_dir(parent,name,mode,0,0);
1791 yaffsfs_SetError(-ENOENT); /* missing path */
1792 else if (yaffs_find_by_name(parent,name))
1793 yaffsfs_SetError(-EEXIST); /* the name already exists */
1795 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1808 void * yaffs_getdev(const YCHAR *path)
1810 struct yaffs_dev *dev=NULL;
1812 dev = yaffsfs_FindDevice(path,&dummy);
1816 int yaffs_mount2(const YCHAR *path,int read_only)
1819 int result=YAFFS_FAIL;
1820 struct yaffs_dev *dev=NULL;
1823 T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1827 yaffsfs_InitHandles();
1829 dev = yaffsfs_FindDevice(path,&dummy);
1831 if(!dev->is_mounted){
1832 dev->read_only = read_only ? 1 : 0;
1833 result = yaffs_guts_initialise(dev);
1834 if(result == YAFFS_FAIL)
1835 /* todo error - mount failed */
1836 yaffsfs_SetError(-ENOMEM);
1837 retVal = result ? 0 : -1;
1841 /* todo error - already mounted. */
1842 yaffsfs_SetError(-EBUSY);
1844 /* todo error - no device */
1845 yaffsfs_SetError(-ENODEV);
1852 int yaffs_mount(const YCHAR *path)
1854 return yaffs_mount2(path,0);
1857 int yaffs_sync(const YCHAR *path)
1860 struct yaffs_dev *dev=NULL;
1864 dev = yaffsfs_FindDevice(path,&dummy);
1866 if(dev->is_mounted){
1868 yaffs_flush_whole_cache(dev);
1869 yaffs_checkpoint_save(dev);
1873 /* todo error - not mounted. */
1874 yaffsfs_SetError(-EINVAL);
1877 /* todo error - no device */
1878 yaffsfs_SetError(-ENODEV);
1885 int yaffs_remount(const YCHAR *path, int force, int read_only)
1888 struct yaffs_dev *dev=NULL;
1892 dev = yaffsfs_FindDevice(path,&dummy);
1894 if(dev->is_mounted){
1898 yaffs_flush_whole_cache(dev);
1900 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1901 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1902 inUse = 1; /* the device is in use, can't unmount */
1905 if(!inUse || force){
1907 yaffs_checkpoint_save(dev);
1908 dev->read_only = read_only ? 1 : 0;
1911 yaffsfs_SetError(-EBUSY);
1914 yaffsfs_SetError(-EINVAL);
1918 yaffsfs_SetError(-ENODEV);
1925 int yaffs_unmount2(const YCHAR *path, int force)
1928 struct yaffs_dev *dev=NULL;
1932 dev = yaffsfs_FindDevice(path,&dummy);
1934 if(dev->is_mounted){
1938 yaffs_flush_whole_cache(dev);
1939 yaffs_checkpoint_save(dev);
1941 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1942 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1943 inUse = 1; /* the device is in use, can't unmount */
1946 if(!inUse || force){
1947 yaffs_deinitialise(dev);
1951 /* todo error can't unmount as files are open */
1952 yaffsfs_SetError(-EBUSY);
1955 /* todo error - not mounted. */
1956 yaffsfs_SetError(-EINVAL);
1960 /* todo error - no device */
1961 yaffsfs_SetError(-ENODEV);
1968 int yaffs_unmount(const YCHAR *path)
1970 return yaffs_unmount2(path,0);
1973 loff_t yaffs_freespace(const YCHAR *path)
1976 struct yaffs_dev *dev=NULL;
1980 dev = yaffsfs_FindDevice(path,&dummy);
1981 if(dev && dev->is_mounted){
1982 retVal = yaffs_get_n_free_chunks(dev);
1983 retVal *= dev->data_bytes_per_chunk;
1986 yaffsfs_SetError(-EINVAL);
1992 loff_t yaffs_totalspace(const YCHAR *path)
1995 struct yaffs_dev *dev=NULL;
1999 dev = yaffsfs_FindDevice(path,&dummy);
2000 if(dev && dev->is_mounted){
2001 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2002 retVal *= dev->param.chunks_per_block;
2003 retVal *= dev->data_bytes_per_chunk;
2006 yaffsfs_SetError(-EINVAL);
2012 int yaffs_inodecount(const YCHAR *path)
2015 struct yaffs_dev *dev=NULL;
2019 dev = yaffsfs_FindDevice(path,&dummy);
2020 if(dev && dev->is_mounted) {
2021 int n_obj = dev->n_obj;
2022 if(n_obj > dev->n_hardlinks)
2023 retVal = n_obj - dev->n_hardlinks;
2027 yaffsfs_SetError(-EINVAL);
2034 void yaffs_add_device(struct yaffs_dev *dev)
2036 dev->is_mounted = 0;
2037 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2039 if(!dev->dev_list.next)
2040 INIT_LIST_HEAD(&dev->dev_list);
2042 list_add(&dev->dev_list,&yaffsfs_deviceList);
2045 void yaffs_remove_device(struct yaffs_dev *dev)
2047 list_del_init(&dev->dev_list);
2053 /* Directory search stuff. */
2056 * Directory search context
2058 * NB this is an opaque structure.
2065 yaffs_dirent de; /* directory entry being used by this dsc */
2066 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2067 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2068 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2070 struct list_head others;
2071 } yaffsfs_DirectorySearchContext;
2075 static struct list_head search_contexts;
2078 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2082 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2086 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2087 dsc->nextReturn = NULL;
2089 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2090 struct yaffs_obj,siblings);
2092 /* Hey someone isn't playing nice! */
2096 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2100 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2102 if( dsc->nextReturn == NULL ||
2103 list_empty(&dsc->dirObj->variant.dir_variant.children))
2104 dsc->nextReturn = NULL;
2106 struct list_head *next = dsc->nextReturn->siblings.next;
2108 if( next == &dsc->dirObj->variant.dir_variant.children)
2109 dsc->nextReturn = NULL; /* end of list */
2111 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2114 /* Hey someone isn't playing nice! */
2118 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2121 struct list_head *i;
2122 yaffsfs_DirectorySearchContext *dsc;
2124 /* if search contexts not initilised then skip */
2125 if(!search_contexts.next)
2128 /* Iterate through the directory search contexts.
2129 * If any are the one being removed, then advance the dsc to
2130 * the next one to prevent a hanging ptr.
2132 list_for_each(i, &search_contexts) {
2134 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2135 if(dsc->nextReturn == obj)
2136 yaffsfs_DirAdvance(dsc);
2142 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2144 yaffs_DIR *dir = NULL;
2145 struct yaffs_obj *obj = NULL;
2146 yaffsfs_DirectorySearchContext *dsc = NULL;
2150 obj = yaffsfs_FindObject(NULL,dirname,0,1);
2152 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2154 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2155 dir = (yaffs_DIR *)dsc;
2158 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2159 dsc->magic = YAFFS_MAGIC;
2161 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2162 INIT_LIST_HEAD(&dsc->others);
2164 if(!search_contexts.next)
2165 INIT_LIST_HEAD(&search_contexts);
2167 list_add(&dsc->others,&search_contexts);
2168 yaffsfs_SetDirRewound(dsc);
2178 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2180 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2181 struct yaffs_dirent *retVal = NULL;
2185 if(dsc && dsc->magic == YAFFS_MAGIC){
2186 yaffsfs_SetError(0);
2187 if(dsc->nextReturn){
2188 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2189 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2190 dsc->de.d_off = dsc->offset++;
2191 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2192 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2194 /* this should not happen! */
2195 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2197 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2199 yaffsfs_DirAdvance(dsc);
2203 yaffsfs_SetError(-EBADF);
2212 void yaffs_rewinddir(yaffs_DIR *dirp)
2214 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2218 yaffsfs_SetDirRewound(dsc);
2224 int yaffs_closedir(yaffs_DIR *dirp)
2226 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2230 list_del(&dsc->others); /* unhook from list */
2236 /* End of directory stuff */
2239 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2241 struct yaffs_obj *parent = NULL;
2242 struct yaffs_obj *obj;
2245 int mode = 0; /* ignore for now */
2248 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2249 if(parent && parent->my_dev->read_only)
2250 yaffsfs_SetError(-EINVAL);
2252 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2256 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2260 yaffsfs_SetError(-EINVAL);
2270 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2272 struct yaffs_obj *obj = NULL;
2278 obj = yaffsfs_FindObject(NULL,path,0,1);
2281 yaffsfs_SetError(-ENOENT);
2283 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2284 yaffsfs_SetError(-EINVAL);
2287 YCHAR *alias = obj->variant.symlink_variant.alias;
2288 memset(buf,0,bufsiz);
2289 yaffs_strncpy(buf,alias,bufsiz - 1);
2296 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2298 /* Creates a link called newpath to existing oldpath */
2299 struct yaffs_obj *obj = NULL;
2300 struct yaffs_obj *target = NULL;
2302 int new_nameLength = 0;
2307 obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2308 target = yaffsfs_FindObject(NULL,newpath,0,0);
2311 yaffsfs_SetError(-ENOENT);
2313 } else if(obj->my_dev->read_only){
2314 yaffsfs_SetError(-EINVAL);
2317 yaffsfs_SetError(-EEXIST);
2320 struct yaffs_obj *newdir = NULL;
2321 struct yaffs_obj *link = NULL;
2325 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2328 yaffsfs_SetError(-ENOTDIR);
2330 }else if(newdir->my_dev != obj->my_dev){
2331 yaffsfs_SetError(-EXDEV);
2335 new_nameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2337 if(new_nameLength == 0){
2338 yaffsfs_SetError(-ENOENT);
2340 } else if (new_nameLength > YAFFS_MAX_NAME_LENGTH){
2341 yaffsfs_SetError(-ENAMETOOLONG);
2346 link = yaffs_link_obj(newdir,newname,obj);
2350 yaffsfs_SetError(-ENOSPC);
2361 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2370 * Returns number of handles attached to the object
2372 int yaffs_n_handles(const YCHAR *path)
2374 struct yaffs_obj *obj;
2376 obj = yaffsfs_FindObject(NULL,path,0,1);
2378 return yaffsfs_CountHandles(obj);
2381 int yaffs_get_error(void)
2383 return yaffsfs_GetLastError();
2386 int yaffs_dump_dev(const YCHAR *path)
2391 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2394 struct yaffs_dev *dev = obj->my_dev;
2397 "n_page_writes.......... %d\n"
2398 "n_page_reads........... %d\n"
2399 "n_erasures....... %d\n"
2400 "n_gc_copies............ %d\n"
2401 "garbageCollections... %d\n"
2402 "passiveGarbageColl'ns %d\n"
2408 dev->garbageCollections,
2409 dev->passiveGarbageCollections