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 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 new_nameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
309 if(new_nameLength == 0){
310 yaffsfs_SetError(-ENOENT);
312 } else if (new_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(-EINVAL);
1026 result = yaffs_resize_file(obj,new_size);
1031 return (result) ? 0 : -1;
1034 int yaffs_ftruncate(int fd, off_t new_size)
1036 yaffsfs_Handle *h = NULL;
1037 struct yaffs_obj *obj = NULL;
1041 h = yaffsfs_GetHandlePointer(fd);
1042 obj = yaffsfs_GetHandleObject(fd);
1046 yaffsfs_SetError(-EBADF);
1047 else if(obj->my_dev->read_only)
1048 yaffsfs_SetError(-EINVAL);
1050 /* resize the file */
1051 result = yaffs_resize_file(obj,new_size);
1055 return (result) ? 0 : -1;
1059 off_t yaffs_lseek(int fd, off_t offset, int whence)
1061 yaffsfs_Handle *h = NULL;
1062 struct yaffs_obj *obj = NULL;
1067 h = yaffsfs_GetHandlePointer(fd);
1068 obj = yaffsfs_GetHandleObject(fd);
1072 yaffsfs_SetError(-EBADF);
1074 if(whence == SEEK_SET){
1077 } else if(whence == SEEK_CUR) {
1078 if( (h->position + offset) >= 0)
1079 pos = (h->position + offset);
1080 } else if(whence == SEEK_END) {
1081 fSize = yaffs_get_obj_length(obj);
1082 if(fSize >= 0 && (fSize + offset) >= 0)
1083 pos = fSize + offset;
1089 yaffsfs_SetError(-EINVAL);
1098 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1100 struct yaffs_obj *dir = NULL;
1101 struct yaffs_obj *obj = NULL;
1103 int result = YAFFS_FAIL;
1107 obj = yaffsfs_FindObject(NULL,path,0,0);
1108 dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1110 yaffsfs_SetError(-ENOTDIR);
1112 yaffsfs_SetError(-ENOENT);
1113 else if(obj->my_dev->read_only)
1114 yaffsfs_SetError(-EINVAL);
1115 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1116 yaffsfs_SetError(-EISDIR);
1117 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1118 yaffsfs_SetError(-ENOTDIR);
1120 result = yaffs_unlinker(dir,name);
1122 if(result == YAFFS_FAIL && isDirectory)
1123 yaffsfs_SetError(-ENOTEMPTY);
1130 return (result == YAFFS_FAIL) ? -1 : 0;
1134 int yaffs_rmdir(const YCHAR *path)
1136 return yaffsfs_DoUnlink(path,1);
1139 int yaffs_unlink(const YCHAR *path)
1141 return yaffsfs_DoUnlink(path,0);
1144 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1146 struct yaffs_obj *olddir = NULL;
1147 struct yaffs_obj *newdir = NULL;
1148 struct yaffs_obj *obj = NULL;
1151 int result= YAFFS_FAIL;
1152 int rename_allowed = 1;
1156 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1157 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1158 obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1160 if(!olddir || !newdir || !obj) {
1162 yaffsfs_SetError(-EBADF);
1164 } else if(obj->my_dev->read_only){
1165 yaffsfs_SetError(-EINVAL);
1167 } else if(olddir->my_dev != newdir->my_dev) {
1168 /* oops must be on same device */
1170 yaffsfs_SetError(-EXDEV);
1172 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1174 * It is a directory, check that it is not being renamed to
1175 * being its own decendent.
1176 * Do this by tracing from the new directory back to the root, checking for obj
1179 struct yaffs_obj *xx = newdir;
1181 while( rename_allowed && xx){
1187 yaffsfs_SetError(-EACCES);
1191 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1195 return (result == YAFFS_FAIL) ? -1 : 0;
1199 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1203 obj = yaffs_get_equivalent_obj(obj);
1206 buf->st_dev = (int)obj->my_dev->os_context;
1207 buf->st_ino = obj->obj_id;
1208 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1210 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1211 buf->st_mode |= S_IFDIR;
1212 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1213 buf->st_mode |= S_IFLNK;
1214 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1215 buf->st_mode |= S_IFREG;
1217 buf->st_nlink = yaffs_get_obj_link_count(obj);
1220 buf->st_rdev = obj->yst_rdev;
1221 buf->st_size = yaffs_get_obj_length(obj);
1222 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1223 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1224 #if CONFIG_YAFFS_WINCE
1225 buf->yst_wince_atime[0] = obj->win_atime[0];
1226 buf->yst_wince_atime[1] = obj->win_atime[1];
1227 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1228 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1229 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1230 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1232 buf->yst_atime = obj->yst_atime;
1233 buf->yst_ctime = obj->yst_ctime;
1234 buf->yst_mtime = obj->yst_mtime;
1241 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1243 struct yaffs_obj *obj;
1249 obj = yaffsfs_FindObject(NULL,path,0,1);
1252 obj = yaffsfs_FollowLink(obj,0);
1255 retVal = yaffsfs_DoStat(obj,buf);
1257 /* todo error not found */
1258 yaffsfs_SetError(-ENOENT);
1266 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1268 return yaffsfs_DoStatOrLStat(path,buf,0);
1271 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1273 return yaffsfs_DoStatOrLStat(path,buf,1);
1276 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1278 struct yaffs_obj *obj;
1283 obj = yaffsfs_GetHandleObject(fd);
1286 retVal = yaffsfs_DoStat(obj,buf);
1289 yaffsfs_SetError(-EBADF);
1296 #ifndef CONFIG_YAFFS_WINCE
1297 /* xattrib functions */
1300 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1302 struct yaffs_obj *obj;
1308 obj = yaffsfs_FindObject(NULL,path,0,1);
1311 obj = yaffsfs_FollowLink(obj,0);
1314 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1316 yaffsfs_SetError(retVal);
1320 /* todo error not found */
1321 yaffsfs_SetError(-ENOENT);
1329 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1331 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1334 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1336 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1341 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1343 struct yaffs_obj *obj;
1348 obj = yaffsfs_GetHandleObject(fd);
1351 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1353 yaffsfs_SetError(retVal);
1358 yaffsfs_SetError(-EBADF);
1365 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1367 struct yaffs_obj *obj;
1373 obj = yaffsfs_FindObject(NULL,path,0,1);
1376 obj = yaffsfs_FollowLink(obj,0);
1379 retVal = yaffs_get_xattrib(obj,name,data,size);
1381 yaffsfs_SetError(retVal);
1385 /* todo error not found */
1386 yaffsfs_SetError(-ENOENT);
1394 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1396 return yaffs_do_getxattr( path, name, data, size, 1);
1398 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1400 return yaffs_do_getxattr( path, name, data, size, 0);
1405 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1407 struct yaffs_obj *obj;
1412 obj = yaffsfs_GetHandleObject(fd);
1415 retVal = yaffs_get_xattrib(obj,name,data,size);
1417 yaffsfs_SetError(retVal);
1422 yaffsfs_SetError(-EBADF);
1429 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1431 struct yaffs_obj *obj;
1437 obj = yaffsfs_FindObject(NULL,path,0,1);
1440 obj = yaffsfs_FollowLink(obj,0);
1443 retVal = yaffs_list_xattrib(obj, data,size);
1445 yaffsfs_SetError(retVal);
1449 /* todo error not found */
1450 yaffsfs_SetError(-ENOENT);
1458 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1460 return yaffs_do_listxattr(path, data, size, 1);
1463 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1465 return yaffs_do_listxattr(path, data, size, 0);
1468 int yaffs_flistxattr(int fd, char *data, int size)
1470 struct yaffs_obj *obj;
1475 obj = yaffsfs_GetHandleObject(fd);
1478 retVal = yaffs_list_xattrib(obj,data,size);
1480 yaffsfs_SetError(retVal);
1485 yaffsfs_SetError(-EBADF);
1492 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1494 struct yaffs_obj *obj;
1500 obj = yaffsfs_FindObject(NULL,path,0,1);
1503 obj = yaffsfs_FollowLink(obj,0);
1506 retVal = yaffs_remove_xattrib(obj,name);
1508 yaffsfs_SetError(retVal);
1512 /* todo error not found */
1513 yaffsfs_SetError(-ENOENT);
1521 int yaffs_removexattr(const YCHAR *path, const char *name)
1523 return yaffs_do_removexattr(path, name, 1);
1526 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1528 return yaffs_do_removexattr(path, name, 0);
1531 int yaffs_fremovexattr(int fd, const char *name)
1533 struct yaffs_obj *obj;
1538 obj = yaffsfs_GetHandleObject(fd);
1541 retVal = yaffs_remove_xattrib(obj,name);
1543 yaffsfs_SetError(retVal);
1548 yaffsfs_SetError(-EBADF);
1556 #ifdef CONFIG_YAFFS_WINCE
1557 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1559 struct yaffs_obj *obj;
1564 obj = yaffsfs_GetHandleObject(fd);
1569 wctime[0] = obj->win_ctime[0];
1570 wctime[1] = obj->win_ctime[1];
1573 watime[0] = obj->win_atime[0];
1574 watime[1] = obj->win_atime[1];
1577 wmtime[0] = obj->win_mtime[0];
1578 wmtime[1] = obj->win_mtime[1];
1585 yaffsfs_SetError(-EBADF);
1593 int yaffs_set_wince_times(int fd,
1594 const unsigned *wctime,
1595 const unsigned *watime,
1596 const unsigned *wmtime)
1598 struct yaffs_obj *obj;
1603 obj = yaffsfs_GetHandleObject(fd);
1608 obj->win_ctime[0] = wctime[0];
1609 obj->win_ctime[1] = wctime[1];
1612 obj->win_atime[0] = watime[0];
1613 obj->win_atime[1] = watime[1];
1616 obj->win_mtime[0] = wmtime[0];
1617 obj->win_mtime[1] = wmtime[1];
1621 result = yaffs_flush_file(obj,0,0);
1625 yaffsfs_SetError(-EBADF);
1635 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1640 obj = yaffs_get_equivalent_obj(obj);
1643 obj->yst_mode = mode;
1645 result = yaffs_flush_file(obj,0,0);
1648 return result == YAFFS_OK ? 0 : -1;
1652 int yaffs_access(const YCHAR *path, int amode)
1654 struct yaffs_obj *obj;
1660 obj = yaffsfs_FindObject(NULL,path,0,1);
1665 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1667 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1669 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1673 yaffsfs_SetError(-EACCES);
1677 /* todo error not found */
1678 yaffsfs_SetError(-ENOENT);
1689 int yaffs_chmod(const YCHAR *path, mode_t mode)
1691 struct yaffs_obj *obj;
1697 obj = yaffsfs_FindObject(NULL,path,0,1);
1700 yaffsfs_SetError(-ENOENT);
1701 else if(obj->my_dev->read_only)
1702 yaffsfs_SetError(-EINVAL);
1704 retVal = yaffsfs_DoChMod(obj,mode);
1713 int yaffs_fchmod(int fd, mode_t mode)
1715 struct yaffs_obj *obj;
1720 obj = yaffsfs_GetHandleObject(fd);
1723 yaffsfs_SetError(-ENOENT);
1724 else if(obj->my_dev->read_only)
1725 yaffsfs_SetError(-EINVAL);
1727 retVal = yaffsfs_DoChMod(obj,mode);
1735 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1737 struct yaffs_obj *parent = NULL;
1738 struct yaffs_obj *dir = NULL;
1740 YCHAR *use_path = NULL;
1741 int path_length = 0;
1747 * We don't have a definition for max path length.
1748 * We will use 3 * max name length instead.
1751 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1753 /* If the last character is a path divider, then we need to
1754 * trim it back so that the name look-up works properly.
1755 * eg. /foo/new_dir/ -> /foo/newdir
1756 * Curveball: Need to handle multiple path dividers:
1757 * eg. /foof/sdfse///// -> /foo/sdfse
1759 if(path_length > 0 &&
1760 yaffsfs_IsPathDivider(path[path_length-1])){
1761 use_path = YMALLOC(path_length + 1);
1763 yaffsfs_SetError(-ENOMEM);
1766 strcpy(use_path, path);
1767 for(i = path_length-1;
1768 i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1770 use_path[i] = (YCHAR) 0;
1775 parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1776 if(parent && yaffs_strnlen(name,5) == 0){
1777 /* Trying to make the root itself */
1778 yaffsfs_SetError(-EEXIST);
1779 } else if(parent && parent->my_dev->read_only){
1780 yaffsfs_SetError(-EINVAL);
1783 dir = yaffs_create_dir(parent,name,mode,0,0);
1788 yaffsfs_SetError(-ENOENT); /* missing path */
1789 else if (yaffs_find_by_name(parent,name))
1790 yaffsfs_SetError(-EEXIST); /* the name already exists */
1792 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1805 void * yaffs_getdev(const YCHAR *path)
1807 struct yaffs_dev *dev=NULL;
1809 dev = yaffsfs_FindDevice(path,&dummy);
1813 int yaffs_mount2(const YCHAR *path,int read_only)
1816 int result=YAFFS_FAIL;
1817 struct yaffs_dev *dev=NULL;
1820 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1824 yaffsfs_InitHandles();
1826 dev = yaffsfs_FindDevice(path,&dummy);
1828 if(!dev->is_mounted){
1829 dev->read_only = read_only ? 1 : 0;
1830 result = yaffs_guts_initialise(dev);
1831 if(result == YAFFS_FAIL)
1832 /* todo error - mount failed */
1833 yaffsfs_SetError(-ENOMEM);
1834 retVal = result ? 0 : -1;
1838 /* todo error - already mounted. */
1839 yaffsfs_SetError(-EBUSY);
1841 /* todo error - no device */
1842 yaffsfs_SetError(-ENODEV);
1849 int yaffs_mount(const YCHAR *path)
1851 return yaffs_mount2(path,0);
1854 int yaffs_sync(const YCHAR *path)
1857 struct yaffs_dev *dev=NULL;
1861 dev = yaffsfs_FindDevice(path,&dummy);
1863 if(dev->is_mounted){
1865 yaffs_flush_whole_cache(dev);
1866 yaffs_checkpoint_save(dev);
1870 /* todo error - not mounted. */
1871 yaffsfs_SetError(-EINVAL);
1874 /* todo error - no device */
1875 yaffsfs_SetError(-ENODEV);
1882 int yaffs_remount(const YCHAR *path, int force, int read_only)
1885 struct yaffs_dev *dev=NULL;
1889 dev = yaffsfs_FindDevice(path,&dummy);
1891 if(dev->is_mounted){
1895 yaffs_flush_whole_cache(dev);
1897 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1898 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1899 inUse = 1; /* the device is in use, can't unmount */
1902 if(!inUse || force){
1904 yaffs_checkpoint_save(dev);
1905 dev->read_only = read_only ? 1 : 0;
1908 yaffsfs_SetError(-EBUSY);
1911 yaffsfs_SetError(-EINVAL);
1915 yaffsfs_SetError(-ENODEV);
1922 int yaffs_unmount2(const YCHAR *path, int force)
1925 struct yaffs_dev *dev=NULL;
1929 dev = yaffsfs_FindDevice(path,&dummy);
1931 if(dev->is_mounted){
1935 yaffs_flush_whole_cache(dev);
1936 yaffs_checkpoint_save(dev);
1938 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1939 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1940 inUse = 1; /* the device is in use, can't unmount */
1943 if(!inUse || force){
1944 yaffs_deinitialise(dev);
1948 /* todo error can't unmount as files are open */
1949 yaffsfs_SetError(-EBUSY);
1952 /* todo error - not mounted. */
1953 yaffsfs_SetError(-EINVAL);
1957 /* todo error - no device */
1958 yaffsfs_SetError(-ENODEV);
1965 int yaffs_unmount(const YCHAR *path)
1967 return yaffs_unmount2(path,0);
1970 loff_t yaffs_freespace(const YCHAR *path)
1973 struct yaffs_dev *dev=NULL;
1977 dev = yaffsfs_FindDevice(path,&dummy);
1978 if(dev && dev->is_mounted){
1979 retVal = yaffs_get_n_free_chunks(dev);
1980 retVal *= dev->data_bytes_per_chunk;
1983 yaffsfs_SetError(-EINVAL);
1989 loff_t yaffs_totalspace(const YCHAR *path)
1992 struct yaffs_dev *dev=NULL;
1996 dev = yaffsfs_FindDevice(path,&dummy);
1997 if(dev && dev->is_mounted){
1998 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
1999 retVal *= dev->param.chunks_per_block;
2000 retVal *= dev->data_bytes_per_chunk;
2003 yaffsfs_SetError(-EINVAL);
2009 int yaffs_inodecount(const YCHAR *path)
2012 struct yaffs_dev *dev=NULL;
2016 dev = yaffsfs_FindDevice(path,&dummy);
2017 if(dev && dev->is_mounted) {
2018 int n_obj = dev->n_obj;
2019 if(n_obj > dev->n_hardlinks)
2020 retVal = n_obj - dev->n_hardlinks;
2024 yaffsfs_SetError(-EINVAL);
2031 void yaffs_add_device(struct yaffs_dev *dev)
2033 dev->is_mounted = 0;
2034 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2036 if(!dev->dev_list.next)
2037 INIT_LIST_HEAD(&dev->dev_list);
2039 list_add(&dev->dev_list,&yaffsfs_deviceList);
2042 void yaffs_remove_device(struct yaffs_dev *dev)
2044 list_del_init(&dev->dev_list);
2050 /* Directory search stuff. */
2053 * Directory search context
2055 * NB this is an opaque structure.
2062 yaffs_dirent de; /* directory entry being used by this dsc */
2063 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2064 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2065 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2067 struct list_head others;
2068 } yaffsfs_DirectorySearchContext;
2072 static struct list_head search_contexts;
2075 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2079 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2083 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2084 dsc->nextReturn = NULL;
2086 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2087 struct yaffs_obj,siblings);
2089 /* Hey someone isn't playing nice! */
2093 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2097 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2099 if( dsc->nextReturn == NULL ||
2100 list_empty(&dsc->dirObj->variant.dir_variant.children))
2101 dsc->nextReturn = NULL;
2103 struct list_head *next = dsc->nextReturn->siblings.next;
2105 if( next == &dsc->dirObj->variant.dir_variant.children)
2106 dsc->nextReturn = NULL; /* end of list */
2108 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2111 /* Hey someone isn't playing nice! */
2115 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2118 struct list_head *i;
2119 yaffsfs_DirectorySearchContext *dsc;
2121 /* if search contexts not initilised then skip */
2122 if(!search_contexts.next)
2125 /* Iterate through the directory search contexts.
2126 * If any are the one being removed, then advance the dsc to
2127 * the next one to prevent a hanging ptr.
2129 list_for_each(i, &search_contexts) {
2131 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2132 if(dsc->nextReturn == obj)
2133 yaffsfs_DirAdvance(dsc);
2139 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2141 yaffs_DIR *dir = NULL;
2142 struct yaffs_obj *obj = NULL;
2143 yaffsfs_DirectorySearchContext *dsc = NULL;
2147 obj = yaffsfs_FindObject(NULL,dirname,0,1);
2149 if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2151 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2152 dir = (yaffs_DIR *)dsc;
2155 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2156 dsc->magic = YAFFS_MAGIC;
2158 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2159 INIT_LIST_HEAD(&dsc->others);
2161 if(!search_contexts.next)
2162 INIT_LIST_HEAD(&search_contexts);
2164 list_add(&dsc->others,&search_contexts);
2165 yaffsfs_SetDirRewound(dsc);
2175 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2177 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2178 struct yaffs_dirent *retVal = NULL;
2182 if(dsc && dsc->magic == YAFFS_MAGIC){
2183 yaffsfs_SetError(0);
2184 if(dsc->nextReturn){
2185 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2186 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2187 dsc->de.d_off = dsc->offset++;
2188 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2189 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2191 /* this should not happen! */
2192 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2194 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2196 yaffsfs_DirAdvance(dsc);
2200 yaffsfs_SetError(-EBADF);
2209 void yaffs_rewinddir(yaffs_DIR *dirp)
2211 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2215 yaffsfs_SetDirRewound(dsc);
2221 int yaffs_closedir(yaffs_DIR *dirp)
2223 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2227 list_del(&dsc->others); /* unhook from list */
2233 /* End of directory stuff */
2236 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2238 struct yaffs_obj *parent = NULL;
2239 struct yaffs_obj *obj;
2242 int mode = 0; /* ignore for now */
2245 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2246 if(parent && parent->my_dev->read_only)
2247 yaffsfs_SetError(-EINVAL);
2249 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2253 yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2257 yaffsfs_SetError(-EINVAL);
2267 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2269 struct yaffs_obj *obj = NULL;
2275 obj = yaffsfs_FindObject(NULL,path,0,1);
2278 yaffsfs_SetError(-ENOENT);
2280 } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2281 yaffsfs_SetError(-EINVAL);
2284 YCHAR *alias = obj->variant.symlink_variant.alias;
2285 memset(buf,0,bufsiz);
2286 yaffs_strncpy(buf,alias,bufsiz - 1);
2293 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2295 /* Creates a link called newpath to existing oldpath */
2296 struct yaffs_obj *obj = NULL;
2297 struct yaffs_obj *target = NULL;
2299 int new_nameLength = 0;
2304 obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2305 target = yaffsfs_FindObject(NULL,newpath,0,0);
2308 yaffsfs_SetError(-ENOENT);
2310 } else if(obj->my_dev->read_only){
2311 yaffsfs_SetError(-EINVAL);
2314 yaffsfs_SetError(-EEXIST);
2317 struct yaffs_obj *newdir = NULL;
2318 struct yaffs_obj *link = NULL;
2322 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2325 yaffsfs_SetError(-ENOTDIR);
2327 }else if(newdir->my_dev != obj->my_dev){
2328 yaffsfs_SetError(-EXDEV);
2332 retVal = yaffsfs_CheckNameLength(newname);
2335 link = yaffs_link_obj(newdir,newname,obj);
2339 yaffsfs_SetError(-ENOSPC);
2350 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2359 * Returns number of handles attached to the object
2361 int yaffs_n_handles(const YCHAR *path)
2363 struct yaffs_obj *obj;
2365 obj = yaffsfs_FindObject(NULL,path,0,1);
2367 return yaffsfs_CountHandles(obj);
2370 int yaffs_get_error(void)
2372 return yaffsfs_GetLastError();
2375 int yaffs_set_error(int error)
2377 /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2378 yaffsfs_SetError(error);
2382 int yaffs_dump_dev(const YCHAR *path)
2387 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2390 struct yaffs_dev *dev = obj->my_dev;
2393 "n_page_writes.......... %d\n"
2394 "n_page_reads........... %d\n"
2395 "n_erasures....... %d\n"
2396 "n_gc_copies............ %d\n"
2397 "garbageCollections... %d\n"
2398 "passiveGarbageColl'ns %d\n"
2404 dev->garbageCollections,
2405 dev->passiveGarbageCollections