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;
71 unsigned yaffs_set_trace(unsigned tm)
73 yaffs_trace_mask = tm;
74 return yaffs_trace_mask;
77 unsigned 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 handle names.
284 int yaffsfs_Match(YCHAR a, YCHAR b)
290 int yaffsfs_IsPathDivider(YCHAR ch)
292 const YCHAR *str = YAFFS_PATH_DIVIDERS;
303 int yaffsfs_CheckNameLength(const char *name)
307 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
310 yaffsfs_SetError(-ENOENT);
312 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
313 yaffsfs_SetError(-ENAMETOOLONG);
320 LIST_HEAD(yaffsfs_deviceList);
325 * Scan the configuration list to find the root.
326 * Curveballs: Should match paths that end in '/' too
327 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
329 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
331 struct list_head *cfg;
332 const YCHAR *leftOver;
334 struct yaffs_dev *retval = NULL;
335 struct yaffs_dev *dev = NULL;
337 int longestMatch = -1;
341 * Check all configs, choose the one that:
342 * 1) Actually matches a prefix (ie /a amd /abc will not match
343 * 2) Matches the longest.
345 list_for_each(cfg, &yaffsfs_deviceList){
346 dev = list_entry(cfg, struct yaffs_dev, dev_list);
353 while(matching && *p && *leftOver){
354 /* Skip over any /s */
355 while(yaffsfs_IsPathDivider(*p))
358 /* Skip over any /s */
359 while(yaffsfs_IsPathDivider(*leftOver))
362 /* Now match the text part */
364 *p && !yaffsfs_IsPathDivider(*p) &&
365 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
366 if(yaffsfs_Match(*p,*leftOver)){
376 /* Skip over any /s in leftOver */
377 while(yaffsfs_IsPathDivider(*leftOver))
380 // Skip over any /s in p
381 while(yaffsfs_IsPathDivider(*p))
384 // p should now be at the end of the string (ie. fully matched)
388 if( matching && (thisMatchLength > longestMatch))
391 *restOfPath = (YCHAR *)leftOver;
393 longestMatch = thisMatchLength;
401 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
404 struct yaffs_dev *dev;
406 dev= yaffsfs_FindDevice(path,restOfPath);
407 if(dev && dev->is_mounted){
408 return dev->root_dir;
413 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
417 obj = yaffs_get_equivalent_obj(obj);
419 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
420 YCHAR *alias = obj->variant.symlink_variant.alias;
422 if(yaffsfs_IsPathDivider(*alias))
423 /* Starts with a /, need to scan from root up */
424 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
426 /* Relative to here, so use the parent of the symlink as a start */
427 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
434 * yaffsfs_FindDirectory
435 * Parse a path to determine the directory and the name within the directory.
437 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
439 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
440 const YCHAR *path, YCHAR **name, int symDepth)
442 struct yaffs_obj *dir;
444 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
447 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
452 restOfPath = (YCHAR *)path;
455 dir = yaffsfs_FindRoot(path,&restOfPath);
460 * curve ball: also throw away surplus '/'
461 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
463 while(yaffsfs_IsPathDivider(*restOfPath))
464 restOfPath++; /* get rid of '/' */
469 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
470 if (i < YAFFS_MAX_NAME_LENGTH){
471 str[i] = *restOfPath;
479 /* got to the end of the string */
482 if(yaffs_strcmp(str,_Y(".")) == 0)
486 else if(yaffs_strcmp(str,_Y("..")) == 0)
489 dir = yaffs_find_by_name(dir,str);
491 dir = yaffsfs_FollowLink(dir,symDepth);
493 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
498 /* directory did not exist. */
502 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
503 const YCHAR *path,YCHAR **name,int symDepth)
505 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
509 * yaffsfs_FindObject turns a path for an existing object into the object
511 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
513 struct yaffs_obj *dir;
514 struct yaffs_obj *obj;
517 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
520 obj = yaffs_find_by_name(dir,name);
525 obj = yaffs_get_equivalent_obj(obj);
531 int yaffs_dup(int fd)
534 yaffsfs_Handle *oldPtr = NULL;
535 yaffsfs_Handle *newPtr = NULL;
539 oldPtr = yaffsfs_GetHandlePointer(fd);
540 if(oldPtr && oldPtr->useCount > 0)
541 newHandle = yaffsfs_GetNewHandle();
543 newPtr = yaffsfs_GetHandlePointer(newHandle);
551 yaffsfs_SetError(-EBADF);
553 yaffsfs_SetError(-ENOMEM);
561 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
563 struct yaffs_obj *obj = NULL;
564 struct yaffs_obj *dir = NULL;
567 yaffsfs_Handle *yh = NULL;
570 int errorReported = 0;
571 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
572 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
573 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
574 u8 sharedReadAllowed;
575 u8 sharedWriteAllowed;
581 /* O_EXCL only has meaning if O_CREAT is specified */
582 if(!(oflag & O_CREAT))
585 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
586 if( (oflag & O_CREAT) & (oflag & O_EXCL))
589 /* Todo: Are there any more flag combos to sanitise ? */
591 /* Figure out if reading or writing is requested */
593 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
594 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
598 handle = yaffsfs_GetNewHandle();
601 yaffsfs_SetError(-ENFILE);
605 yh = yaffsfs_GetHandlePointer(handle);
607 /* try to find the exisiting object */
608 obj = yaffsfs_FindObject(NULL,path,0,1);
610 obj = yaffsfs_FollowLink(obj,symDepth++);
613 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
614 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
620 /* The file already exists or it might be a directory */
622 /* If it is a directory then we can't open it as a file */
623 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
625 yaffsfs_SetError(-EISDIR);
629 /* Open should fail if O_CREAT and O_EXCL are specified since
632 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
634 yaffsfs_SetError(-EEXIST);
638 /* Check file permissions */
639 if( readRequested && !(obj->yst_mode & S_IREAD))
642 if( writeRequested && !(obj->yst_mode & S_IWRITE))
645 if(openDenied && !errorReported ) {
646 /* Error if the file exists but permissions are refused. */
647 yaffsfs_SetError(-EACCES);
651 /* Check sharing of an existing object. */
655 sharedReadAllowed = 1;
656 sharedWriteAllowed = 1;
659 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
660 hx = &yaffsfs_handle[i];
661 if(hx->useCount > 0 &&
663 yaffsfs_inode[hx->inodeId].iObj == obj){
665 sharedReadAllowed = 0;
667 sharedWriteAllowed = 0;
677 if((!sharedReadAllowed && readRequested)||
678 (!shareRead && alreadyReading) ||
679 (!sharedWriteAllowed && writeRequested) ||
680 (!shareWrite && alreadyWriting)){
682 yaffsfs_SetError(-EBUSY);
689 /* If we could not open an existing object, then let's see if
690 * the directory exists. If not, error.
692 if(!obj && !errorReported){
693 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
695 yaffsfs_SetError(-ENOTDIR);
700 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
701 /* Let's see if we can create this file if it does not exist. */
702 if(dir->my_dev->read_only){
703 yaffsfs_SetError(-EINVAL);
706 obj = yaffs_create_file(dir,name,mode,0,0);
708 if(!obj && !errorReported){
709 yaffsfs_SetError(-ENOSPC);
714 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
715 /* Error if the file does not exist and CREAT is not set. */
716 yaffsfs_SetError(-ENOENT);
720 if(obj && !openDenied) {
721 int inodeId = yaffsfs_GetInodeIdForObject(obj);
725 * Todo: Fix any problem if inodes run out, though that
726 * can't happen if the number of inode items >= number of handles.
730 yh->inodeId = inodeId;
731 yh->reading = readRequested;
732 yh->writing = writeRequested;
733 yh->append = (oflag & O_APPEND) ? 1 : 0;
735 yh->shareRead = shareRead;
736 yh->shareWrite = shareWrite;
738 /* Hook inode to object */
739 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
741 if((oflag & O_TRUNC) && yh->writing)
742 yaffs_resize_file(obj,0);
744 yaffsfs_PutHandle(handle);
746 yaffsfs_SetError(0); /* Problem */
756 int yaffs_open(const YCHAR *path, int oflag, int mode)
758 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
761 int yaffs_Dofsync(int fd,int datasync)
763 yaffsfs_Handle *h = NULL;
768 h = yaffsfs_GetHandlePointer(fd);
770 if(h && h->useCount > 0)
772 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
775 yaffsfs_SetError(-EBADF);
784 int yaffs_fsync(int fd)
786 return yaffs_Dofsync(fd,0);
789 int yaffs_flush(int fd)
791 return yaffs_fsync(fd);
794 int yaffs_fdatasync(int fd)
796 return yaffs_Dofsync(fd,1);
799 int yaffs_close(int fd)
801 yaffsfs_Handle *h = NULL;
806 h = yaffsfs_GetHandlePointer(fd);
808 if(h && h->useCount > 0) {
810 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
811 yaffsfs_PutHandle(fd);
815 yaffsfs_SetError(-EBADF);
826 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
828 yaffsfs_Handle *h = NULL;
829 struct yaffs_obj *obj = NULL;
835 unsigned int maxRead;
836 u8 *buf = (u8 *)vbuf;
839 h = yaffsfs_GetHandlePointer(fd);
840 obj = yaffsfs_GetHandleObject(fd);
844 yaffsfs_SetError(-EBADF);
846 } else if(!h->reading){
847 /* Not a reading handle */
848 yaffsfs_SetError(-EINVAL);
854 startPos = h->position;
858 if(yaffs_get_obj_length(obj) > pos)
859 maxRead = yaffs_get_obj_length(obj) - pos;
867 yaffsfs_GetHandle(fd);
870 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
873 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
884 nbyte = 0; /* no more to read */
894 yaffsfs_PutHandle(fd);
898 h->position = startPos + totalRead;
908 return (totalRead >= 0) ? totalRead : -1;
912 int yaffs_read(int fd, void *buf, unsigned int nbyte)
914 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
917 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
919 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
922 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
924 yaffsfs_Handle *h = NULL;
925 struct yaffs_obj *obj = NULL;
929 int totalWritten = 0;
930 int write_trhrough = 0;
932 const u8 *buf = (const u8 *)vbuf;
935 h = yaffsfs_GetHandlePointer(fd);
936 obj = yaffsfs_GetHandleObject(fd);
940 yaffsfs_SetError(-EBADF);
942 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
943 yaffsfs_SetError(-EINVAL);
947 startPos = yaffs_get_obj_length(obj);
951 startPos = h->position;
953 yaffsfs_GetHandle(fd);
956 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
960 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,write_trhrough);
962 totalWritten += nWritten;
967 if(nWritten == nToWrite)
972 if(nWritten < 1 && totalWritten < 1){
973 yaffsfs_SetError(-ENOSPC);
983 yaffsfs_PutHandle(fd);
987 h->position = startPos + totalWritten;
996 return (totalWritten >= 0) ? totalWritten : -1;
999 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1001 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1004 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1006 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1010 int yaffs_truncate(const YCHAR *path,off_t new_size)
1012 struct yaffs_obj *obj = NULL;
1013 int result = YAFFS_FAIL;
1017 obj = yaffsfs_FindObject(NULL,path,0,1);
1020 yaffsfs_SetError(-ENOENT);
1021 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1022 yaffsfs_SetError(-EISDIR);
1023 else if(obj->my_dev->read_only)
1024 yaffsfs_SetError(-EACCES);
1025 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1026 yaffsfs_SetError(-EINVAL);
1028 result = yaffs_resize_file(obj,new_size);
1032 return (result) ? 0 : -1;
1035 int yaffs_ftruncate(int fd, off_t new_size)
1037 yaffsfs_Handle *h = NULL;
1038 struct yaffs_obj *obj = NULL;
1042 h = yaffsfs_GetHandlePointer(fd);
1043 obj = yaffsfs_GetHandleObject(fd);
1047 yaffsfs_SetError(-EBADF);
1048 else if(obj->my_dev->read_only)
1049 yaffsfs_SetError(-EACCES);
1050 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1051 yaffsfs_SetError(-EINVAL);
1053 /* resize the file */
1054 result = yaffs_resize_file(obj,new_size);
1057 return (result) ? 0 : -1;
1061 off_t yaffs_lseek(int fd, off_t offset, int whence)
1063 yaffsfs_Handle *h = NULL;
1064 struct yaffs_obj *obj = NULL;
1069 h = yaffsfs_GetHandlePointer(fd);
1070 obj = yaffsfs_GetHandleObject(fd);
1074 yaffsfs_SetError(-EBADF);
1076 if(whence == SEEK_SET){
1079 } else if(whence == SEEK_CUR) {
1080 if( (h->position + offset) >= 0)
1081 pos = (h->position + offset);
1082 } else if(whence == SEEK_END) {
1083 fSize = yaffs_get_obj_length(obj);
1084 if(fSize >= 0 && (fSize + offset) >= 0)
1085 pos = fSize + offset;
1091 yaffsfs_SetError(-EINVAL);
1100 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1102 struct yaffs_obj *dir = NULL;
1103 struct yaffs_obj *obj = NULL;
1105 int result = YAFFS_FAIL;
1109 obj = yaffsfs_FindObject(NULL,path,0,0);
1110 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1112 yaffsfs_SetError(-ENOTDIR);
1114 yaffsfs_SetError(-ENOENT);
1115 else if(obj->my_dev->read_only)
1116 yaffsfs_SetError(-EINVAL);
1117 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1118 yaffsfs_SetError(-EISDIR);
1119 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1120 yaffsfs_SetError(-ENOTDIR);
1122 result = yaffs_unlinker(dir,name);
1124 if(result == YAFFS_FAIL && isDirectory)
1125 yaffsfs_SetError(-ENOTEMPTY);
1132 return (result == YAFFS_FAIL) ? -1 : 0;
1136 int yaffs_rmdir(const YCHAR *path)
1138 return yaffsfs_DoUnlink(path,1);
1141 int yaffs_unlink(const YCHAR *path)
1143 return yaffsfs_DoUnlink(path,0);
1146 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1148 struct yaffs_obj *olddir = NULL;
1149 struct yaffs_obj *newdir = NULL;
1150 struct yaffs_obj *obj = NULL;
1153 int result= YAFFS_FAIL;
1154 int rename_allowed = 1;
1158 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1159 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1160 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1162 if(!olddir || !newdir || !obj) {
1164 yaffsfs_SetError(-EBADF);
1166 } else if(obj->my_dev->read_only){
1167 yaffsfs_SetError(-EINVAL);
1169 } else if(olddir->my_dev != newdir->my_dev) {
1170 /* Rename must be on same device */
1171 yaffsfs_SetError(-EXDEV);
1173 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1175 * It is a directory, check that it is not being renamed to
1176 * being its own decendent.
1177 * Do this by tracing from the new directory back to the root, checking for obj
1180 struct yaffs_obj *xx = newdir;
1182 while( rename_allowed && xx){
1188 yaffsfs_SetError(-EACCES);
1192 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1196 return (result == YAFFS_FAIL) ? -1 : 0;
1200 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1204 obj = yaffs_get_equivalent_obj(obj);
1207 buf->st_dev = (int)obj->my_dev->os_context;
1208 buf->st_ino = obj->obj_id;
1209 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1211 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1212 buf->st_mode |= S_IFDIR;
1213 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1214 buf->st_mode |= S_IFLNK;
1215 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1216 buf->st_mode |= S_IFREG;
1218 buf->st_nlink = yaffs_get_obj_link_count(obj);
1221 buf->st_rdev = obj->yst_rdev;
1222 buf->st_size = yaffs_get_obj_length(obj);
1223 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1224 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1225 #if CONFIG_YAFFS_WINCE
1226 buf->yst_wince_atime[0] = obj->win_atime[0];
1227 buf->yst_wince_atime[1] = obj->win_atime[1];
1228 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1229 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1230 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1231 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1233 buf->yst_atime = obj->yst_atime;
1234 buf->yst_ctime = obj->yst_ctime;
1235 buf->yst_mtime = obj->yst_mtime;
1242 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1244 struct yaffs_obj *obj;
1250 obj = yaffsfs_FindObject(NULL,path,0,1);
1253 obj = yaffsfs_FollowLink(obj,0);
1256 retVal = yaffsfs_DoStat(obj,buf);
1258 /* todo error not found */
1259 yaffsfs_SetError(-ENOENT);
1267 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1269 return yaffsfs_DoStatOrLStat(path,buf,0);
1272 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1274 return yaffsfs_DoStatOrLStat(path,buf,1);
1277 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1279 struct yaffs_obj *obj;
1284 obj = yaffsfs_GetHandleObject(fd);
1287 retVal = yaffsfs_DoStat(obj,buf);
1290 yaffsfs_SetError(-EBADF);
1297 #ifndef CONFIG_YAFFS_WINCE
1298 /* xattrib functions */
1301 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1303 struct yaffs_obj *obj;
1309 obj = yaffsfs_FindObject(NULL,path,0,1);
1312 obj = yaffsfs_FollowLink(obj,0);
1315 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1317 yaffsfs_SetError(retVal);
1321 /* todo error not found */
1322 yaffsfs_SetError(-ENOENT);
1330 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1332 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1335 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1337 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1342 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1344 struct yaffs_obj *obj;
1349 obj = yaffsfs_GetHandleObject(fd);
1352 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1354 yaffsfs_SetError(retVal);
1359 yaffsfs_SetError(-EBADF);
1366 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1368 struct yaffs_obj *obj;
1374 obj = yaffsfs_FindObject(NULL,path,0,1);
1377 obj = yaffsfs_FollowLink(obj,0);
1380 retVal = yaffs_get_xattrib(obj,name,data,size);
1382 yaffsfs_SetError(retVal);
1386 /* todo error not found */
1387 yaffsfs_SetError(-ENOENT);
1395 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1397 return yaffs_do_getxattr( path, name, data, size, 1);
1399 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1401 return yaffs_do_getxattr( path, name, data, size, 0);
1406 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1408 struct yaffs_obj *obj;
1413 obj = yaffsfs_GetHandleObject(fd);
1416 retVal = yaffs_get_xattrib(obj,name,data,size);
1418 yaffsfs_SetError(retVal);
1423 yaffsfs_SetError(-EBADF);
1430 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1432 struct yaffs_obj *obj;
1438 obj = yaffsfs_FindObject(NULL,path,0,1);
1441 obj = yaffsfs_FollowLink(obj,0);
1444 retVal = yaffs_list_xattrib(obj, data,size);
1446 yaffsfs_SetError(retVal);
1450 /* todo error not found */
1451 yaffsfs_SetError(-ENOENT);
1459 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1461 return yaffs_do_listxattr(path, data, size, 1);
1464 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1466 return yaffs_do_listxattr(path, data, size, 0);
1469 int yaffs_flistxattr(int fd, char *data, int size)
1471 struct yaffs_obj *obj;
1476 obj = yaffsfs_GetHandleObject(fd);
1479 retVal = yaffs_list_xattrib(obj,data,size);
1481 yaffsfs_SetError(retVal);
1486 yaffsfs_SetError(-EBADF);
1493 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1495 struct yaffs_obj *obj;
1501 obj = yaffsfs_FindObject(NULL,path,0,1);
1504 obj = yaffsfs_FollowLink(obj,0);
1507 retVal = yaffs_remove_xattrib(obj,name);
1509 yaffsfs_SetError(retVal);
1513 /* todo error not found */
1514 yaffsfs_SetError(-ENOENT);
1522 int yaffs_removexattr(const YCHAR *path, const char *name)
1524 return yaffs_do_removexattr(path, name, 1);
1527 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1529 return yaffs_do_removexattr(path, name, 0);
1532 int yaffs_fremovexattr(int fd, const char *name)
1534 struct yaffs_obj *obj;
1539 obj = yaffsfs_GetHandleObject(fd);
1542 retVal = yaffs_remove_xattrib(obj,name);
1544 yaffsfs_SetError(retVal);
1549 yaffsfs_SetError(-EBADF);
1557 #ifdef CONFIG_YAFFS_WINCE
1558 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1560 struct yaffs_obj *obj;
1565 obj = yaffsfs_GetHandleObject(fd);
1570 wctime[0] = obj->win_ctime[0];
1571 wctime[1] = obj->win_ctime[1];
1574 watime[0] = obj->win_atime[0];
1575 watime[1] = obj->win_atime[1];
1578 wmtime[0] = obj->win_mtime[0];
1579 wmtime[1] = obj->win_mtime[1];
1586 yaffsfs_SetError(-EBADF);
1594 int yaffs_set_wince_times(int fd,
1595 const unsigned *wctime,
1596 const unsigned *watime,
1597 const unsigned *wmtime)
1599 struct yaffs_obj *obj;
1604 obj = yaffsfs_GetHandleObject(fd);
1609 obj->win_ctime[0] = wctime[0];
1610 obj->win_ctime[1] = wctime[1];
1613 obj->win_atime[0] = watime[0];
1614 obj->win_atime[1] = watime[1];
1617 obj->win_mtime[0] = wmtime[0];
1618 obj->win_mtime[1] = wmtime[1];
1622 result = yaffs_flush_file(obj,0,0);
1626 yaffsfs_SetError(-EBADF);
1636 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1641 obj = yaffs_get_equivalent_obj(obj);
1644 obj->yst_mode = mode;
1646 result = yaffs_flush_file(obj,0,0);
1649 return result == YAFFS_OK ? 0 : -1;
1653 int yaffs_access(const YCHAR *path, int amode)
1655 struct yaffs_obj *obj;
1659 if(amode & ~(R_OK | W_OK | X_OK)){
1660 yaffsfs_SetError(-EINVAL);
1666 obj = yaffsfs_FindObject(NULL,path,0,1);
1671 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1673 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1675 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1679 yaffsfs_SetError(-EACCES);
1683 /* todo error not found */
1684 yaffsfs_SetError(-ENOENT);
1695 int yaffs_chmod(const YCHAR *path, mode_t mode)
1697 struct yaffs_obj *obj;
1703 obj = yaffsfs_FindObject(NULL,path,0,1);
1706 yaffsfs_SetError(-ENOENT);
1707 else if(obj->my_dev->read_only)
1708 yaffsfs_SetError(-EINVAL);
1710 retVal = yaffsfs_DoChMod(obj,mode);
1719 int yaffs_fchmod(int fd, mode_t mode)
1721 struct yaffs_obj *obj;
1726 obj = yaffsfs_GetHandleObject(fd);
1729 yaffsfs_SetError(-ENOENT);
1730 else if(obj->my_dev->read_only)
1731 yaffsfs_SetError(-EINVAL);
1733 retVal = yaffsfs_DoChMod(obj,mode);
1741 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1743 struct yaffs_obj *parent = NULL;
1744 struct yaffs_obj *dir = NULL;
1746 YCHAR *use_path = NULL;
1747 int path_length = 0;
1753 * We don't have a definition for max path length.
1754 * We will use 3 * max name length instead.
1757 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1759 /* If the last character is a path divider, then we need to
1760 * trim it back so that the name look-up works properly.
1761 * eg. /foo/new_dir/ -> /foo/newdir
1762 * Curveball: Need to handle multiple path dividers:
1763 * eg. /foof/sdfse///// -> /foo/sdfse
1765 if(path_length > 0 &&
1766 yaffsfs_IsPathDivider(path[path_length-1])){
1767 use_path = YMALLOC(path_length + 1);
1769 yaffsfs_SetError(-ENOMEM);
1772 strcpy(use_path, path);
1773 for(i = path_length-1;
1774 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1776 use_path[i] = (YCHAR) 0;
1781 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1782 if(parent && yaffs_strnlen(name,5) == 0){
1783 /* Trying to make the root itself */
1784 yaffsfs_SetError(-EEXIST);
1785 } else if(parent && parent->my_dev->read_only){
1786 yaffsfs_SetError(-EINVAL);
1789 dir = yaffs_create_dir(parent,name,mode,0,0);
1794 yaffsfs_SetError(-ENOENT); /* missing path */
1795 else if (yaffs_find_by_name(parent,name))
1796 yaffsfs_SetError(-EEXIST); /* the name already exists */
1798 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1811 void * yaffs_getdev(const YCHAR *path)
1813 struct yaffs_dev *dev=NULL;
1815 dev = yaffsfs_FindDevice(path,&dummy);
1819 int yaffs_mount2(const YCHAR *path,int read_only)
1822 int result=YAFFS_FAIL;
1823 struct yaffs_dev *dev=NULL;
1826 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1830 yaffsfs_InitHandles();
1832 dev = yaffsfs_FindDevice(path,&dummy);
1834 if(!dev->is_mounted){
1835 dev->read_only = read_only ? 1 : 0;
1836 result = yaffs_guts_initialise(dev);
1837 if(result == YAFFS_FAIL)
1838 /* todo error - mount failed */
1839 yaffsfs_SetError(-ENOMEM);
1840 retVal = result ? 0 : -1;
1844 /* todo error - already mounted. */
1845 yaffsfs_SetError(-EBUSY);
1847 /* todo error - no device */
1848 yaffsfs_SetError(-ENODEV);
1855 int yaffs_mount(const YCHAR *path)
1857 return yaffs_mount2(path,0);
1860 int yaffs_sync(const YCHAR *path)
1863 struct yaffs_dev *dev=NULL;
1867 dev = yaffsfs_FindDevice(path,&dummy);
1869 if(dev->is_mounted){
1871 yaffs_flush_whole_cache(dev);
1872 yaffs_checkpoint_save(dev);
1876 /* todo error - not mounted. */
1877 yaffsfs_SetError(-EINVAL);
1880 /* todo error - no device */
1881 yaffsfs_SetError(-ENODEV);
1888 int yaffs_remount(const YCHAR *path, int force, int read_only)
1891 struct yaffs_dev *dev=NULL;
1895 dev = yaffsfs_FindDevice(path,&dummy);
1897 if(dev->is_mounted){
1901 yaffs_flush_whole_cache(dev);
1903 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1904 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1905 inUse = 1; /* the device is in use, can't unmount */
1908 if(!inUse || force){
1910 yaffs_checkpoint_save(dev);
1911 dev->read_only = read_only ? 1 : 0;
1914 yaffsfs_SetError(-EBUSY);
1917 yaffsfs_SetError(-EINVAL);
1921 yaffsfs_SetError(-ENODEV);
1928 int yaffs_unmount2(const YCHAR *path, int force)
1931 struct yaffs_dev *dev=NULL;
1935 dev = yaffsfs_FindDevice(path,&dummy);
1937 if(dev->is_mounted){
1941 yaffs_flush_whole_cache(dev);
1942 yaffs_checkpoint_save(dev);
1944 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1945 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1946 inUse = 1; /* the device is in use, can't unmount */
1949 if(!inUse || force){
1950 yaffs_deinitialise(dev);
1954 /* todo error can't unmount as files are open */
1955 yaffsfs_SetError(-EBUSY);
1958 /* todo error - not mounted. */
1959 yaffsfs_SetError(-EINVAL);
1963 /* todo error - no device */
1964 yaffsfs_SetError(-ENODEV);
1971 int yaffs_unmount(const YCHAR *path)
1973 return yaffs_unmount2(path,0);
1976 loff_t yaffs_freespace(const YCHAR *path)
1979 struct yaffs_dev *dev=NULL;
1983 dev = yaffsfs_FindDevice(path,&dummy);
1984 if(dev && dev->is_mounted){
1985 retVal = yaffs_get_n_free_chunks(dev);
1986 retVal *= dev->data_bytes_per_chunk;
1989 yaffsfs_SetError(-EINVAL);
1995 loff_t yaffs_totalspace(const YCHAR *path)
1998 struct yaffs_dev *dev=NULL;
2002 dev = yaffsfs_FindDevice(path,&dummy);
2003 if(dev && dev->is_mounted){
2004 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2005 retVal *= dev->param.chunks_per_block;
2006 retVal *= dev->data_bytes_per_chunk;
2009 yaffsfs_SetError(-EINVAL);
2015 int yaffs_inodecount(const YCHAR *path)
2018 struct yaffs_dev *dev=NULL;
2022 dev = yaffsfs_FindDevice(path,&dummy);
2023 if(dev && dev->is_mounted) {
2024 int n_obj = dev->n_obj;
2025 if(n_obj > dev->n_hardlinks)
2026 retVal = n_obj - dev->n_hardlinks;
2030 yaffsfs_SetError(-EINVAL);
2037 void yaffs_add_device(struct yaffs_dev *dev)
2039 dev->is_mounted = 0;
2040 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2042 if(!dev->dev_list.next)
2043 INIT_LIST_HEAD(&dev->dev_list);
2045 list_add(&dev->dev_list,&yaffsfs_deviceList);
2048 void yaffs_remove_device(struct yaffs_dev *dev)
2050 list_del_init(&dev->dev_list);
2056 /* Directory search stuff. */
2059 * Directory search context
2061 * NB this is an opaque structure.
2068 yaffs_dirent de; /* directory entry being used by this dsc */
2069 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2070 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2071 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2073 struct list_head others;
2074 } yaffsfs_DirectorySearchContext;
2078 static struct list_head search_contexts;
2081 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2085 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2089 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2090 dsc->nextReturn = NULL;
2092 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2093 struct yaffs_obj,siblings);
2095 /* Hey someone isn't playing nice! */
2099 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2103 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2105 if( dsc->nextReturn == NULL ||
2106 list_empty(&dsc->dirObj->variant.dir_variant.children))
2107 dsc->nextReturn = NULL;
2109 struct list_head *next = dsc->nextReturn->siblings.next;
2111 if( next == &dsc->dirObj->variant.dir_variant.children)
2112 dsc->nextReturn = NULL; /* end of list */
2114 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2117 /* Hey someone isn't playing nice! */
2121 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2124 struct list_head *i;
2125 yaffsfs_DirectorySearchContext *dsc;
2127 /* if search contexts not initilised then skip */
2128 if(!search_contexts.next)
2131 /* Iterate through the directory search contexts.
2132 * If any are the one being removed, then advance the dsc to
2133 * the next one to prevent a hanging ptr.
2135 list_for_each(i, &search_contexts) {
2137 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2138 if(dsc->nextReturn == obj)
2139 yaffsfs_DirAdvance(dsc);
2145 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2147 yaffs_DIR *dir = NULL;
2148 struct yaffs_obj *obj = NULL;
2149 yaffsfs_DirectorySearchContext *dsc = NULL;
2153 obj = yaffsfs_FindObject(NULL,dirname,0,1);
2155 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2157 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2158 dir = (yaffs_DIR *)dsc;
2161 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2162 dsc->magic = YAFFS_MAGIC;
2164 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2165 INIT_LIST_HEAD(&dsc->others);
2167 if(!search_contexts.next)
2168 INIT_LIST_HEAD(&search_contexts);
2170 list_add(&dsc->others,&search_contexts);
2171 yaffsfs_SetDirRewound(dsc);
2181 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2183 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2184 struct yaffs_dirent *retVal = NULL;
2188 if(dsc && dsc->magic == YAFFS_MAGIC){
2189 yaffsfs_SetError(0);
2190 if(dsc->nextReturn){
2191 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2192 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2193 dsc->de.d_off = dsc->offset++;
2194 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2195 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2197 /* this should not happen! */
2198 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2200 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2202 yaffsfs_DirAdvance(dsc);
2206 yaffsfs_SetError(-EBADF);
2215 void yaffs_rewinddir(yaffs_DIR *dirp)
2217 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2221 yaffsfs_SetDirRewound(dsc);
2227 int yaffs_closedir(yaffs_DIR *dirp)
2229 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2233 list_del(&dsc->others); /* unhook from list */
2239 /* End of directory stuff */
2242 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2244 struct yaffs_obj *parent = NULL;
2245 struct yaffs_obj *obj;
2248 int mode = 0; /* ignore for now */
2251 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2252 if(parent && parent->my_dev->read_only)
2253 yaffsfs_SetError(-EINVAL);
2255 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2259 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2263 yaffsfs_SetError(-EINVAL);
2273 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2275 struct yaffs_obj *obj = NULL;
2280 obj = yaffsfs_FindObject(NULL,path,0,1);
2283 yaffsfs_SetError(-ENOENT);
2285 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2286 yaffsfs_SetError(-EINVAL);
2289 YCHAR *alias = obj->variant.symlink_variant.alias;
2290 memset(buf,0,bufsiz);
2291 yaffs_strncpy(buf,alias,bufsiz - 1);
2298 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2300 /* Creates a link called newpath to existing oldpath */
2301 struct yaffs_obj *obj = NULL;
2302 struct yaffs_obj *target = NULL;
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 retVal = yaffsfs_CheckNameLength(newname);
2338 link = yaffs_link_obj(newdir,newname,obj);
2342 yaffsfs_SetError(-ENOSPC);
2353 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2362 * Returns number of handles attached to the object
2364 int yaffs_n_handles(const YCHAR *path)
2366 struct yaffs_obj *obj;
2368 obj = yaffsfs_FindObject(NULL,path,0,1);
2370 return yaffsfs_CountHandles(obj);
2373 int yaffs_get_error(void)
2375 return yaffsfs_GetLastError();
2378 int yaffs_set_error(int error)
2380 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2381 yaffsfs_SetError(error);
2385 int yaffs_dump_dev(const YCHAR *path)
2390 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2393 struct yaffs_dev *dev = obj->my_dev;
2396 "n_page_writes.......... %d\n"
2397 "n_page_reads........... %d\n"
2398 "n_erasures....... %d\n"
2399 "n_gc_copies............ %d\n"
2400 "garbageCollections... %d\n"
2401 "passiveGarbageColl'ns %d\n"
2407 dev->garbageCollections,
2408 dev->passiveGarbageCollections