2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2010 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
20 #include <string.h> /* for memset */
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
35 const YCHAR *path, int symDepth, int getEquiv);
36 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
38 unsigned int yaffs_wr_attempts;
42 * There are open inodes in yaffsfs_Inode.
43 * There are open handles in yaffsfs_Handle.
45 * Things are structured this way to be like the Linux VFS model
46 * so that interactions with the yaffs guts calls are similar.
47 * That means more common code paths and less special code.
48 * That means better testing etc.
52 int count; /* Number of handles accessing this inode */
53 struct yaffs_obj *iObj;
62 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
63 int useCount:10; /* Use count for this handle */
64 u32 position; /* current position in file */
67 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
68 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
69 static int yaffsfs_handlesInitialised;
72 unsigned yaffs_set_trace(unsigned tm)
74 yaffs_trace_mask = tm;
75 return yaffs_trace_mask;
78 unsigned yaffs_get_trace(void)
80 return yaffs_trace_mask;
85 * Inilitalise handle management on start-up.
88 static void yaffsfs_InitHandles(void)
91 if(yaffsfs_handlesInitialised)
94 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
95 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
96 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
97 yaffsfs_handle[i].inodeId = -1;
100 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
102 if(h < 0 || h >= YAFFSFS_N_HANDLES)
105 return &yaffsfs_handle[h];
108 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
110 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
112 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
113 return &yaffsfs_inode[h->inodeId];
118 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
120 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
129 * yaffsfs_FindInodeIdForObject
130 * Find the inode entry for an object, if it exists.
133 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
139 obj = yaffs_get_equivalent_obj(obj);
141 /* Look for it in open inode table*/
142 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
143 if(yaffsfs_inode[i].iObj == obj)
150 * yaffsfs_GetInodeIdForObject
151 * Grab an inode entry when opening a new inode.
153 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
157 yaffsfs_Inode *in = NULL;
160 obj = yaffs_get_equivalent_obj(obj);
162 ret = yaffsfs_FindInodeIdForObject(obj);
164 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
165 if(!yaffsfs_inode[i].iObj)
170 in = &yaffsfs_inode[ret];
182 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
184 int i = yaffsfs_FindInodeIdForObject(obj);
187 return yaffsfs_inode[i].count;
192 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
194 struct yaffs_obj *obj;
201 obj->my_inode = NULL;
206 static void yaffsfs_PutInode(int inodeId)
208 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
209 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
212 yaffsfs_ReleaseInode(in);
219 * Grab a handle (when opening a file)
222 static int yaffsfs_GetNewHandle(void)
227 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
228 h = yaffsfs_GetHandlePointer(i);
230 /* todo bug: should never happen */
233 memset(h,0,sizeof(yaffsfs_Handle));
244 * Increase use of handle when reading/writing a file
246 static int yaffsfs_GetHandle(int handle)
248 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
250 if(h && h->useCount > 0){
258 * Let go of a handle when closing a file or aborting an open or
259 * ending a read or write.
261 static int yaffsfs_PutHandle(int handle)
263 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
265 if(h && h->useCount > 0){
269 yaffsfs_PutInode(h->inodeId);
281 * Stuff to handle names.
285 int yaffsfs_Match(YCHAR a, YCHAR b)
291 int yaffsfs_IsPathDivider(YCHAR ch)
293 const YCHAR *str = YAFFS_PATH_DIVIDERS;
304 int yaffsfs_CheckNameLength(const char *name)
308 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
311 yaffsfs_SetError(-ENOENT);
313 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
314 yaffsfs_SetError(-ENAMETOOLONG);
321 LIST_HEAD(yaffsfs_deviceList);
326 * Scan the configuration list to find the root.
327 * Curveballs: Should match paths that end in '/' too
328 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
330 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
332 struct list_head *cfg;
333 const YCHAR *leftOver;
335 struct yaffs_dev *retval = NULL;
336 struct yaffs_dev *dev = NULL;
338 int longestMatch = -1;
342 * Check all configs, choose the one that:
343 * 1) Actually matches a prefix (ie /a amd /abc will not match
344 * 2) Matches the longest.
346 list_for_each(cfg, &yaffsfs_deviceList){
347 dev = list_entry(cfg, struct yaffs_dev, dev_list);
354 while(matching && *p && *leftOver){
355 /* Skip over any /s */
356 while(yaffsfs_IsPathDivider(*p))
359 /* Skip over any /s */
360 while(yaffsfs_IsPathDivider(*leftOver))
363 /* Now match the text part */
365 *p && !yaffsfs_IsPathDivider(*p) &&
366 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
367 if(yaffsfs_Match(*p,*leftOver)){
377 /* Skip over any /s in leftOver */
378 while(yaffsfs_IsPathDivider(*leftOver))
381 // Skip over any /s in p
382 while(yaffsfs_IsPathDivider(*p))
385 // p should now be at the end of the string (ie. fully matched)
389 if( matching && (thisMatchLength > longestMatch))
392 *restOfPath = (YCHAR *)leftOver;
394 longestMatch = thisMatchLength;
402 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
405 struct yaffs_dev *dev;
407 dev= yaffsfs_FindDevice(path,restOfPath);
408 if(dev && dev->is_mounted){
409 return dev->root_dir;
414 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
418 obj = yaffs_get_equivalent_obj(obj);
420 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
421 YCHAR *alias = obj->variant.symlink_variant.alias;
423 if(yaffsfs_IsPathDivider(*alias))
424 /* Starts with a /, need to scan from root up */
425 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
427 /* Relative to here, so use the parent of the symlink as a start */
428 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
435 * yaffsfs_FindDirectory
436 * Parse a path to determine the directory and the name within the directory.
438 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
440 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
441 const YCHAR *path, YCHAR **name, int symDepth)
443 struct yaffs_obj *dir;
445 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
448 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
453 restOfPath = (YCHAR *)path;
456 dir = yaffsfs_FindRoot(path,&restOfPath);
461 * curve ball: also throw away surplus '/'
462 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
464 while(yaffsfs_IsPathDivider(*restOfPath))
465 restOfPath++; /* get rid of '/' */
470 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
471 if (i < YAFFS_MAX_NAME_LENGTH){
472 str[i] = *restOfPath;
480 /* got to the end of the string */
483 if(yaffs_strcmp(str,_Y(".")) == 0)
487 else if(yaffs_strcmp(str,_Y("..")) == 0)
490 dir = yaffs_find_by_name(dir,str);
492 dir = yaffsfs_FollowLink(dir,symDepth);
494 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
499 /* directory did not exist. */
503 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
504 const YCHAR *path,YCHAR **name,int symDepth)
506 return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
510 * yaffsfs_FindObject turns a path for an existing object into the object
512 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
514 struct yaffs_obj *dir;
515 struct yaffs_obj *obj;
518 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
521 obj = yaffs_find_by_name(dir,name);
526 obj = yaffs_get_equivalent_obj(obj);
532 int yaffs_dup(int fd)
535 yaffsfs_Handle *oldPtr = NULL;
536 yaffsfs_Handle *newPtr = NULL;
540 oldPtr = yaffsfs_GetHandlePointer(fd);
541 if(oldPtr && oldPtr->useCount > 0)
542 newHandle = yaffsfs_GetNewHandle();
544 newPtr = yaffsfs_GetHandlePointer(newHandle);
552 yaffsfs_SetError(-EBADF);
554 yaffsfs_SetError(-ENOMEM);
562 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
564 struct yaffs_obj *obj = NULL;
565 struct yaffs_obj *dir = NULL;
568 yaffsfs_Handle *yh = NULL;
571 int errorReported = 0;
572 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
573 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
574 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
575 u8 sharedReadAllowed;
576 u8 sharedWriteAllowed;
582 /* O_EXCL only has meaning if O_CREAT is specified */
583 if(!(oflag & O_CREAT))
586 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
587 if( (oflag & O_CREAT) & (oflag & O_EXCL))
590 /* Todo: Are there any more flag combos to sanitise ? */
592 /* Figure out if reading or writing is requested */
594 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
595 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
599 handle = yaffsfs_GetNewHandle();
602 yaffsfs_SetError(-ENFILE);
606 yh = yaffsfs_GetHandlePointer(handle);
608 /* try to find the exisiting object */
609 obj = yaffsfs_FindObject(NULL,path,0,1);
611 obj = yaffsfs_FollowLink(obj,symDepth++);
614 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
615 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
621 /* The file already exists or it might be a directory */
623 /* If it is a directory then we can't open it as a file */
624 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
626 yaffsfs_SetError(-EISDIR);
630 /* Open should fail if O_CREAT and O_EXCL are specified since
633 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
635 yaffsfs_SetError(-EEXIST);
639 /* Check file permissions */
640 if( readRequested && !(obj->yst_mode & S_IREAD))
643 if( writeRequested && !(obj->yst_mode & S_IWRITE))
646 if(openDenied && !errorReported ) {
647 /* Error if the file exists but permissions are refused. */
648 yaffsfs_SetError(-EACCES);
652 /* Check sharing of an existing object. */
656 sharedReadAllowed = 1;
657 sharedWriteAllowed = 1;
660 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
661 hx = &yaffsfs_handle[i];
662 if(hx->useCount > 0 &&
664 yaffsfs_inode[hx->inodeId].iObj == obj){
666 sharedReadAllowed = 0;
668 sharedWriteAllowed = 0;
678 if((!sharedReadAllowed && readRequested)||
679 (!shareRead && alreadyReading) ||
680 (!sharedWriteAllowed && writeRequested) ||
681 (!shareWrite && alreadyWriting)){
683 yaffsfs_SetError(-EBUSY);
690 /* If we could not open an existing object, then let's see if
691 * the directory exists. If not, error.
693 if(!obj && !errorReported){
694 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
696 yaffsfs_SetError(-ENOTDIR);
701 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
702 /* Let's see if we can create this file if it does not exist. */
703 if(dir->my_dev->read_only){
704 yaffsfs_SetError(-EINVAL);
707 obj = yaffs_create_file(dir,name,mode,0,0);
709 if(!obj && !errorReported){
710 yaffsfs_SetError(-ENOSPC);
715 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
716 /* Error if the file does not exist and CREAT is not set. */
717 yaffsfs_SetError(-ENOENT);
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(0); /* Problem */
757 int yaffs_open(const YCHAR *path, int oflag, int mode)
759 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
762 int yaffs_Dofsync(int fd,int datasync)
764 yaffsfs_Handle *h = NULL;
769 h = yaffsfs_GetHandlePointer(fd);
771 if(h && h->useCount > 0)
773 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
776 yaffsfs_SetError(-EBADF);
785 int yaffs_fsync(int fd)
787 return yaffs_Dofsync(fd,0);
790 int yaffs_flush(int fd)
792 return yaffs_fsync(fd);
795 int yaffs_fdatasync(int fd)
797 return yaffs_Dofsync(fd,1);
800 int yaffs_close(int fd)
802 yaffsfs_Handle *h = NULL;
807 h = yaffsfs_GetHandlePointer(fd);
809 if(h && h->useCount > 0) {
811 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
812 yaffsfs_PutHandle(fd);
816 yaffsfs_SetError(-EBADF);
827 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
829 yaffsfs_Handle *h = NULL;
830 struct yaffs_obj *obj = NULL;
836 unsigned int maxRead;
837 u8 *buf = (u8 *)vbuf;
840 h = yaffsfs_GetHandlePointer(fd);
841 obj = yaffsfs_GetHandleObject(fd);
845 yaffsfs_SetError(-EBADF);
847 } else if(!h->reading){
848 /* Not a reading handle */
849 yaffsfs_SetError(-EINVAL);
855 startPos = h->position;
859 if(yaffs_get_obj_length(obj) > pos)
860 maxRead = yaffs_get_obj_length(obj) - pos;
868 yaffsfs_GetHandle(fd);
871 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
876 * Need to reverify object in case the device was
877 * unmounted in another thread.
879 obj = yaffsfs_GetHandleObject(fd);
883 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
894 nbyte = 0; /* no more to read */
904 yaffsfs_PutHandle(fd);
908 h->position = startPos + totalRead;
918 return (totalRead >= 0) ? totalRead : -1;
922 int yaffs_read(int fd, void *buf, unsigned int nbyte)
924 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
927 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
929 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
932 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
934 yaffsfs_Handle *h = NULL;
935 struct yaffs_obj *obj = NULL;
939 int totalWritten = 0;
940 int write_trhrough = 0;
942 const u8 *buf = (const u8 *)vbuf;
945 h = yaffsfs_GetHandlePointer(fd);
946 obj = yaffsfs_GetHandleObject(fd);
950 yaffsfs_SetError(-EBADF);
952 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
953 yaffsfs_SetError(-EINVAL);
957 startPos = yaffs_get_obj_length(obj);
961 startPos = h->position;
963 yaffsfs_GetHandle(fd);
967 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
972 * Need to reverify object in case the device was
973 * remounted or unmounted in another thread.
975 obj = yaffsfs_GetHandleObject(fd);
976 if(!obj || obj->my_dev->read_only)
979 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
982 totalWritten += nWritten;
987 if(nWritten == nToWrite)
992 if(nWritten < 1 && totalWritten < 1){
993 yaffsfs_SetError(-ENOSPC);
1003 yaffsfs_PutHandle(fd);
1006 if(totalWritten > 0)
1007 h->position = startPos + totalWritten;
1016 return (totalWritten >= 0) ? totalWritten : -1;
1019 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1021 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1024 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1026 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1030 int yaffs_truncate(const YCHAR *path,off_t new_size)
1032 struct yaffs_obj *obj = NULL;
1033 int result = YAFFS_FAIL;
1037 obj = yaffsfs_FindObject(NULL,path,0,1);
1040 yaffsfs_SetError(-ENOENT);
1041 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1042 yaffsfs_SetError(-EISDIR);
1043 else if(obj->my_dev->read_only)
1044 yaffsfs_SetError(-EACCES);
1045 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1046 yaffsfs_SetError(-EINVAL);
1048 result = yaffs_resize_file(obj,new_size);
1052 return (result) ? 0 : -1;
1055 int yaffs_ftruncate(int fd, off_t new_size)
1057 yaffsfs_Handle *h = NULL;
1058 struct yaffs_obj *obj = NULL;
1062 h = yaffsfs_GetHandlePointer(fd);
1063 obj = yaffsfs_GetHandleObject(fd);
1067 yaffsfs_SetError(-EBADF);
1068 else if(obj->my_dev->read_only)
1069 yaffsfs_SetError(-EACCES);
1070 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1071 yaffsfs_SetError(-EINVAL);
1073 /* resize the file */
1074 result = yaffs_resize_file(obj,new_size);
1077 return (result) ? 0 : -1;
1081 off_t yaffs_lseek(int fd, off_t offset, int whence)
1083 yaffsfs_Handle *h = NULL;
1084 struct yaffs_obj *obj = NULL;
1089 h = yaffsfs_GetHandlePointer(fd);
1090 obj = yaffsfs_GetHandleObject(fd);
1094 yaffsfs_SetError(-EBADF);
1096 if(whence == SEEK_SET){
1099 } else if(whence == SEEK_CUR) {
1100 if( (h->position + offset) >= 0)
1101 pos = (h->position + offset);
1102 } else if(whence == SEEK_END) {
1103 fSize = yaffs_get_obj_length(obj);
1104 if(fSize >= 0 && (fSize + offset) >= 0)
1105 pos = fSize + offset;
1111 yaffsfs_SetError(-EINVAL);
1120 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1122 struct yaffs_obj *dir = NULL;
1123 struct yaffs_obj *obj = NULL;
1125 int result = YAFFS_FAIL;
1129 obj = yaffsfs_FindObject(NULL,path,0,0);
1130 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1132 yaffsfs_SetError(-ENOTDIR);
1134 yaffsfs_SetError(-ENOENT);
1135 else if(obj->my_dev->read_only)
1136 yaffsfs_SetError(-EINVAL);
1137 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1138 yaffsfs_SetError(-EISDIR);
1139 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1140 yaffsfs_SetError(-ENOTDIR);
1142 result = yaffs_unlinker(dir,name);
1144 if(result == YAFFS_FAIL && isDirectory)
1145 yaffsfs_SetError(-ENOTEMPTY);
1152 return (result == YAFFS_FAIL) ? -1 : 0;
1156 int yaffs_rmdir(const YCHAR *path)
1158 return yaffsfs_DoUnlink(path,1);
1161 int yaffs_unlink(const YCHAR *path)
1163 return yaffsfs_DoUnlink(path,0);
1166 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1168 struct yaffs_obj *olddir = NULL;
1169 struct yaffs_obj *newdir = NULL;
1170 struct yaffs_obj *obj = NULL;
1173 int result= YAFFS_FAIL;
1174 int rename_allowed = 1;
1178 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1179 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1180 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1182 if(!olddir || !newdir || !obj) {
1184 yaffsfs_SetError(-EBADF);
1186 } else if(obj->my_dev->read_only){
1187 yaffsfs_SetError(-EINVAL);
1189 } else if(olddir->my_dev != newdir->my_dev) {
1190 /* Rename must be on same device */
1191 yaffsfs_SetError(-EXDEV);
1193 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1195 * It is a directory, check that it is not being renamed to
1196 * being its own decendent.
1197 * Do this by tracing from the new directory back to the root, checking for obj
1200 struct yaffs_obj *xx = newdir;
1202 while( rename_allowed && xx){
1208 yaffsfs_SetError(-EACCES);
1212 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1216 return (result == YAFFS_FAIL) ? -1 : 0;
1220 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1224 obj = yaffs_get_equivalent_obj(obj);
1227 buf->st_dev = (int)obj->my_dev->os_context;
1228 buf->st_ino = obj->obj_id;
1229 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1231 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1232 buf->st_mode |= S_IFDIR;
1233 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1234 buf->st_mode |= S_IFLNK;
1235 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1236 buf->st_mode |= S_IFREG;
1238 buf->st_nlink = yaffs_get_obj_link_count(obj);
1241 buf->st_rdev = obj->yst_rdev;
1242 buf->st_size = yaffs_get_obj_length(obj);
1243 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1244 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1245 #if CONFIG_YAFFS_WINCE
1246 buf->yst_wince_atime[0] = obj->win_atime[0];
1247 buf->yst_wince_atime[1] = obj->win_atime[1];
1248 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1249 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1250 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1251 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1253 buf->yst_atime = obj->yst_atime;
1254 buf->yst_ctime = obj->yst_ctime;
1255 buf->yst_mtime = obj->yst_mtime;
1262 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1264 struct yaffs_obj *obj;
1270 obj = yaffsfs_FindObject(NULL,path,0,1);
1273 obj = yaffsfs_FollowLink(obj,0);
1276 retVal = yaffsfs_DoStat(obj,buf);
1278 /* todo error not found */
1279 yaffsfs_SetError(-ENOENT);
1287 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1289 return yaffsfs_DoStatOrLStat(path,buf,0);
1292 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1294 return yaffsfs_DoStatOrLStat(path,buf,1);
1297 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1299 struct yaffs_obj *obj;
1304 obj = yaffsfs_GetHandleObject(fd);
1307 retVal = yaffsfs_DoStat(obj,buf);
1310 yaffsfs_SetError(-EBADF);
1317 #ifndef CONFIG_YAFFS_WINCE
1318 /* xattrib functions */
1321 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1323 struct yaffs_obj *obj;
1329 obj = yaffsfs_FindObject(NULL,path,0,1);
1332 obj = yaffsfs_FollowLink(obj,0);
1335 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1337 yaffsfs_SetError(retVal);
1341 /* todo error not found */
1342 yaffsfs_SetError(-ENOENT);
1350 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1352 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1355 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1357 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1362 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1364 struct yaffs_obj *obj;
1369 obj = yaffsfs_GetHandleObject(fd);
1372 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1374 yaffsfs_SetError(retVal);
1379 yaffsfs_SetError(-EBADF);
1386 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1388 struct yaffs_obj *obj;
1394 obj = yaffsfs_FindObject(NULL,path,0,1);
1397 obj = yaffsfs_FollowLink(obj,0);
1400 retVal = yaffs_get_xattrib(obj,name,data,size);
1402 yaffsfs_SetError(retVal);
1406 /* todo error not found */
1407 yaffsfs_SetError(-ENOENT);
1415 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1417 return yaffs_do_getxattr( path, name, data, size, 1);
1419 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1421 return yaffs_do_getxattr( path, name, data, size, 0);
1426 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1428 struct yaffs_obj *obj;
1433 obj = yaffsfs_GetHandleObject(fd);
1436 retVal = yaffs_get_xattrib(obj,name,data,size);
1438 yaffsfs_SetError(retVal);
1443 yaffsfs_SetError(-EBADF);
1450 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1452 struct yaffs_obj *obj;
1458 obj = yaffsfs_FindObject(NULL,path,0,1);
1461 obj = yaffsfs_FollowLink(obj,0);
1464 retVal = yaffs_list_xattrib(obj, data,size);
1466 yaffsfs_SetError(retVal);
1470 /* todo error not found */
1471 yaffsfs_SetError(-ENOENT);
1479 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1481 return yaffs_do_listxattr(path, data, size, 1);
1484 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1486 return yaffs_do_listxattr(path, data, size, 0);
1489 int yaffs_flistxattr(int fd, char *data, int size)
1491 struct yaffs_obj *obj;
1496 obj = yaffsfs_GetHandleObject(fd);
1499 retVal = yaffs_list_xattrib(obj,data,size);
1501 yaffsfs_SetError(retVal);
1506 yaffsfs_SetError(-EBADF);
1513 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1515 struct yaffs_obj *obj;
1521 obj = yaffsfs_FindObject(NULL,path,0,1);
1524 obj = yaffsfs_FollowLink(obj,0);
1527 retVal = yaffs_remove_xattrib(obj,name);
1529 yaffsfs_SetError(retVal);
1533 /* todo error not found */
1534 yaffsfs_SetError(-ENOENT);
1542 int yaffs_removexattr(const YCHAR *path, const char *name)
1544 return yaffs_do_removexattr(path, name, 1);
1547 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1549 return yaffs_do_removexattr(path, name, 0);
1552 int yaffs_fremovexattr(int fd, const char *name)
1554 struct yaffs_obj *obj;
1559 obj = yaffsfs_GetHandleObject(fd);
1562 retVal = yaffs_remove_xattrib(obj,name);
1564 yaffsfs_SetError(retVal);
1569 yaffsfs_SetError(-EBADF);
1577 #ifdef CONFIG_YAFFS_WINCE
1578 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1580 struct yaffs_obj *obj;
1585 obj = yaffsfs_GetHandleObject(fd);
1590 wctime[0] = obj->win_ctime[0];
1591 wctime[1] = obj->win_ctime[1];
1594 watime[0] = obj->win_atime[0];
1595 watime[1] = obj->win_atime[1];
1598 wmtime[0] = obj->win_mtime[0];
1599 wmtime[1] = obj->win_mtime[1];
1606 yaffsfs_SetError(-EBADF);
1614 int yaffs_set_wince_times(int fd,
1615 const unsigned *wctime,
1616 const unsigned *watime,
1617 const unsigned *wmtime)
1619 struct yaffs_obj *obj;
1624 obj = yaffsfs_GetHandleObject(fd);
1629 obj->win_ctime[0] = wctime[0];
1630 obj->win_ctime[1] = wctime[1];
1633 obj->win_atime[0] = watime[0];
1634 obj->win_atime[1] = watime[1];
1637 obj->win_mtime[0] = wmtime[0];
1638 obj->win_mtime[1] = wmtime[1];
1642 result = yaffs_flush_file(obj,0,0);
1646 yaffsfs_SetError(-EBADF);
1656 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1661 obj = yaffs_get_equivalent_obj(obj);
1664 obj->yst_mode = mode;
1666 result = yaffs_flush_file(obj,0,0);
1669 return result == YAFFS_OK ? 0 : -1;
1673 int yaffs_access(const YCHAR *path, int amode)
1675 struct yaffs_obj *obj;
1679 if(amode & ~(R_OK | W_OK | X_OK)){
1680 yaffsfs_SetError(-EINVAL);
1686 obj = yaffsfs_FindObject(NULL,path,0,1);
1691 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1693 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1695 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1699 yaffsfs_SetError(-EACCES);
1703 /* todo error not found */
1704 yaffsfs_SetError(-ENOENT);
1715 int yaffs_chmod(const YCHAR *path, mode_t mode)
1717 struct yaffs_obj *obj;
1723 obj = yaffsfs_FindObject(NULL,path,0,1);
1726 yaffsfs_SetError(-ENOENT);
1727 else if(obj->my_dev->read_only)
1728 yaffsfs_SetError(-EINVAL);
1730 retVal = yaffsfs_DoChMod(obj,mode);
1739 int yaffs_fchmod(int fd, mode_t mode)
1741 struct yaffs_obj *obj;
1746 obj = yaffsfs_GetHandleObject(fd);
1749 yaffsfs_SetError(-ENOENT);
1750 else if(obj->my_dev->read_only)
1751 yaffsfs_SetError(-EINVAL);
1753 retVal = yaffsfs_DoChMod(obj,mode);
1761 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1763 struct yaffs_obj *parent = NULL;
1764 struct yaffs_obj *dir = NULL;
1766 YCHAR *use_path = NULL;
1767 int path_length = 0;
1773 * We don't have a definition for max path length.
1774 * We will use 3 * max name length instead.
1777 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1779 /* If the last character is a path divider, then we need to
1780 * trim it back so that the name look-up works properly.
1781 * eg. /foo/new_dir/ -> /foo/newdir
1782 * Curveball: Need to handle multiple path dividers:
1783 * eg. /foof/sdfse///// -> /foo/sdfse
1785 if(path_length > 0 &&
1786 yaffsfs_IsPathDivider(path[path_length-1])){
1787 use_path = YMALLOC(path_length + 1);
1789 yaffsfs_SetError(-ENOMEM);
1792 strcpy(use_path, path);
1793 for(i = path_length-1;
1794 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1796 use_path[i] = (YCHAR) 0;
1801 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1802 if(parent && yaffs_strnlen(name,5) == 0){
1803 /* Trying to make the root itself */
1804 yaffsfs_SetError(-EEXIST);
1805 } else if(parent && parent->my_dev->read_only){
1806 yaffsfs_SetError(-EINVAL);
1809 dir = yaffs_create_dir(parent,name,mode,0,0);
1814 yaffsfs_SetError(-ENOENT); /* missing path */
1815 else if (yaffs_find_by_name(parent,name))
1816 yaffsfs_SetError(-EEXIST); /* the name already exists */
1818 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1831 void * yaffs_getdev(const YCHAR *path)
1833 struct yaffs_dev *dev=NULL;
1835 dev = yaffsfs_FindDevice(path,&dummy);
1839 int yaffs_mount2(const YCHAR *path,int read_only)
1842 int result=YAFFS_FAIL;
1843 struct yaffs_dev *dev=NULL;
1846 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1850 yaffsfs_InitHandles();
1852 dev = yaffsfs_FindDevice(path,&restOfPath);
1853 if(dev && !(*restOfPath)){
1854 if(!dev->is_mounted){
1855 dev->read_only = read_only ? 1 : 0;
1856 result = yaffs_guts_initialise(dev);
1857 if(result == YAFFS_FAIL)
1858 /* todo error - mount failed */
1859 yaffsfs_SetError(-ENOMEM);
1860 retVal = result ? 0 : -1;
1864 /* todo error - already mounted. */
1865 yaffsfs_SetError(-EBUSY);
1867 /* todo error - no device */
1868 yaffsfs_SetError(-ENODEV);
1875 int yaffs_mount(const YCHAR *path)
1877 return yaffs_mount2(path,0);
1880 int yaffs_sync(const YCHAR *path)
1883 struct yaffs_dev *dev=NULL;
1887 dev = yaffsfs_FindDevice(path,&dummy);
1889 if(dev->is_mounted){
1891 yaffs_flush_whole_cache(dev);
1892 yaffs_checkpoint_save(dev);
1896 /* todo error - not mounted. */
1897 yaffsfs_SetError(-EINVAL);
1900 /* todo error - no device */
1901 yaffsfs_SetError(-ENODEV);
1908 int yaffs_remount(const YCHAR *path, int force, int read_only)
1911 struct yaffs_dev *dev=NULL;
1915 dev = yaffsfs_FindDevice(path,&dummy);
1917 if(dev->is_mounted){
1921 yaffs_flush_whole_cache(dev);
1923 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1924 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1925 inUse = 1; /* the device is in use, can't unmount */
1928 if(!inUse || force){
1930 yaffs_checkpoint_save(dev);
1931 dev->read_only = read_only ? 1 : 0;
1934 yaffsfs_SetError(-EBUSY);
1937 yaffsfs_SetError(-EINVAL);
1941 yaffsfs_SetError(-ENODEV);
1948 int yaffs_unmount2(const YCHAR *path, int force)
1951 struct yaffs_dev *dev=NULL;
1955 dev = yaffsfs_FindDevice(path,&dummy);
1957 if(dev->is_mounted){
1961 yaffs_flush_whole_cache(dev);
1962 yaffs_checkpoint_save(dev);
1964 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1965 if(yaffsfs_handle[i].useCount > 0 &&
1966 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;
2301 obj = yaffsfs_FindObject(NULL,path,0,1);
2304 yaffsfs_SetError(-ENOENT);
2306 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2307 yaffsfs_SetError(-EINVAL);
2310 YCHAR *alias = obj->variant.symlink_variant.alias;
2311 memset(buf,0,bufsiz);
2312 yaffs_strncpy(buf,alias,bufsiz - 1);
2319 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2321 /* Creates a link called newpath to existing oldpath */
2322 struct yaffs_obj *obj = NULL;
2323 struct yaffs_obj *target = NULL;
2328 obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2329 target = yaffsfs_FindObject(NULL,newpath,0,0);
2332 yaffsfs_SetError(-ENOENT);
2334 } else if(obj->my_dev->read_only){
2335 yaffsfs_SetError(-EINVAL);
2338 yaffsfs_SetError(-EEXIST);
2341 struct yaffs_obj *newdir = NULL;
2342 struct yaffs_obj *link = NULL;
2346 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2349 yaffsfs_SetError(-ENOTDIR);
2351 }else if(newdir->my_dev != obj->my_dev){
2352 yaffsfs_SetError(-EXDEV);
2356 retVal = yaffsfs_CheckNameLength(newname);
2359 link = yaffs_link_obj(newdir,newname,obj);
2363 yaffsfs_SetError(-ENOSPC);
2374 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2383 * Returns number of handles attached to the object
2385 int yaffs_n_handles(const YCHAR *path)
2387 struct yaffs_obj *obj;
2389 obj = yaffsfs_FindObject(NULL,path,0,1);
2391 return yaffsfs_CountHandles(obj);
2394 int yaffs_get_error(void)
2396 return yaffsfs_GetLastError();
2399 int yaffs_set_error(int error)
2401 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2402 yaffsfs_SetError(error);
2406 int yaffs_dump_dev(const YCHAR *path)
2411 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2414 struct yaffs_dev *dev = obj->my_dev;
2417 "n_page_writes.......... %d\n"
2418 "n_page_reads........... %d\n"
2419 "n_erasures....... %d\n"
2420 "n_gc_copies............ %d\n"
2421 "garbageCollections... %d\n"
2422 "passiveGarbageColl'ns %d\n"
2428 dev->garbageCollections,
2429 dev->passiveGarbageCollections