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,
36 int symDepth, int getEquiv,
37 struct yaffs_obj **dirOut,
40 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
42 unsigned int yaffs_wr_attempts;
46 * There are open inodes in yaffsfs_Inode.
47 * There are open handles in yaffsfs_Handle.
49 * Things are structured this way to be like the Linux VFS model
50 * so that interactions with the yaffs guts calls are similar.
51 * That means more common code paths and less special code.
52 * That means better testing etc.
56 int count; /* Number of handles accessing this inode */
57 struct yaffs_obj *iObj;
66 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
67 int useCount:10; /* Use count for this handle */
68 u32 position; /* current position in file */
71 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
72 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
73 static int yaffsfs_handlesInitialised;
76 unsigned yaffs_set_trace(unsigned tm)
78 yaffs_trace_mask = tm;
79 return yaffs_trace_mask;
82 unsigned yaffs_get_trace(void)
84 return yaffs_trace_mask;
89 * Inilitalise handle management on start-up.
92 static void yaffsfs_InitHandles(void)
95 if(yaffsfs_handlesInitialised)
98 memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
99 memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
100 for(i = 0; i < YAFFSFS_N_HANDLES; i++)
101 yaffsfs_handle[i].inodeId = -1;
104 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
106 if(h < 0 || h >= YAFFSFS_N_HANDLES)
109 return &yaffsfs_handle[h];
112 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
114 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
116 if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
117 return &yaffsfs_inode[h->inodeId];
122 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
124 yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
133 * yaffsfs_FindInodeIdForObject
134 * Find the inode entry for an object, if it exists.
137 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
143 obj = yaffs_get_equivalent_obj(obj);
145 /* Look for it in open inode table*/
146 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
147 if(yaffsfs_inode[i].iObj == obj)
154 * yaffsfs_GetInodeIdForObject
155 * Grab an inode entry when opening a new inode.
157 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
161 yaffsfs_Inode *in = NULL;
164 obj = yaffs_get_equivalent_obj(obj);
166 ret = yaffsfs_FindInodeIdForObject(obj);
168 for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
169 if(!yaffsfs_inode[i].iObj)
174 in = &yaffsfs_inode[ret];
186 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
188 int i = yaffsfs_FindInodeIdForObject(obj);
191 return yaffsfs_inode[i].count;
196 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
198 struct yaffs_obj *obj;
205 obj->my_inode = NULL;
210 static void yaffsfs_PutInode(int inodeId)
212 if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
213 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
216 yaffsfs_ReleaseInode(in);
225 * Grab a handle (when opening a file)
228 static int yaffsfs_GetNewHandle(void)
233 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
234 h = yaffsfs_GetHandlePointer(i);
236 /* todo bug: should never happen */
239 memset(h,0,sizeof(yaffsfs_Handle));
250 * Increase use of handle when reading/writing a file
252 static int yaffsfs_GetHandle(int handle)
254 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
256 if(h && h->useCount > 0){
264 * Let go of a handle when closing a file or aborting an open or
265 * ending a read or write.
267 static int yaffsfs_PutHandle(int handle)
269 yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
271 if(h && h->useCount > 0){
275 yaffsfs_PutInode(h->inodeId);
283 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
286 struct yaffs_obj *obj;
288 for(i = 0; i < YAFFSFS_N_HANDLES; i++){
289 h = yaffsfs_GetHandlePointer(i);
290 obj = yaffsfs_GetHandleObject(i);
291 if(h && h->useCount>0 && obj && obj->my_dev == dev){
293 yaffsfs_PutInode(h->inodeId);
303 * Stuff to handle names.
307 int yaffsfs_Match(YCHAR a, YCHAR b)
313 int yaffsfs_IsPathDivider(YCHAR ch)
315 const YCHAR *str = YAFFS_PATH_DIVIDERS;
326 int yaffsfs_CheckNameLength(const char *name)
330 int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
333 yaffsfs_SetError(-ENOENT);
335 } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
336 yaffsfs_SetError(-ENAMETOOLONG);
343 LIST_HEAD(yaffsfs_deviceList);
348 * Scan the configuration list to find the device
349 * Curveballs: Should match paths that end in '/' too
350 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
352 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
354 struct list_head *cfg;
355 const YCHAR *leftOver;
357 struct yaffs_dev *retval = NULL;
358 struct yaffs_dev *dev = NULL;
360 int longestMatch = -1;
364 * Check all configs, choose the one that:
365 * 1) Actually matches a prefix (ie /a amd /abc will not match
366 * 2) Matches the longest.
368 list_for_each(cfg, &yaffsfs_deviceList){
369 dev = list_entry(cfg, struct yaffs_dev, dev_list);
376 while(matching && *p && *leftOver){
377 /* Skip over any /s */
378 while(yaffsfs_IsPathDivider(*p))
381 /* Skip over any /s */
382 while(yaffsfs_IsPathDivider(*leftOver))
385 /* Now match the text part */
387 *p && !yaffsfs_IsPathDivider(*p) &&
388 *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
389 if(yaffsfs_Match(*p,*leftOver)){
399 /* Skip over any /s in leftOver */
400 while(yaffsfs_IsPathDivider(*leftOver))
403 // Skip over any /s in p
404 while(yaffsfs_IsPathDivider(*p))
407 // p should now be at the end of the string (ie. fully matched)
411 if( matching && (thisMatchLength > longestMatch))
414 *restOfPath = (YCHAR *)leftOver;
416 longestMatch = thisMatchLength;
423 static int yaffsfs_CheckPath(const YCHAR *path)
427 while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
428 if(yaffsfs_IsPathDivider(*path)){
436 return (*path) ? -1 : 0;
439 /* FindMountPoint only returns a dev entry if the path is a mount point */
440 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
442 struct yaffs_dev *dev;
443 YCHAR *restOfPath=NULL;
444 dev = yaffsfs_FindDevice(path,&restOfPath);
445 if(dev && restOfPath && *restOfPath)
450 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
453 struct yaffs_dev *dev;
455 dev= yaffsfs_FindDevice(path,restOfPath);
456 if(dev && dev->is_mounted){
457 return dev->root_dir;
462 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
466 obj = yaffs_get_equivalent_obj(obj);
468 while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
469 YCHAR *alias = obj->variant.symlink_variant.alias;
471 if(yaffsfs_IsPathDivider(*alias))
472 /* Starts with a /, need to scan from root up */
473 obj = yaffsfs_FindObject(NULL,alias,symDepth++,1,NULL,NULL);
475 /* Relative to here, so use the parent of the symlink as a start */
476 obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1,NULL,NULL);
483 * yaffsfs_FindDirectory
484 * Parse a path to determine the directory and the name within the directory.
486 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
488 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
489 const YCHAR *path, YCHAR **name, int symDepth,
492 struct yaffs_obj *dir;
494 YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
500 if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
505 restOfPath = (YCHAR *)path;
508 dir = yaffsfs_FindRoot(path,&restOfPath);
514 * curve ball: also throw away surplus '/'
515 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
517 while(yaffsfs_IsPathDivider(*restOfPath))
518 restOfPath++; /* get rid of '/' */
523 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
524 if (i < YAFFS_MAX_NAME_LENGTH){
525 str[i] = *restOfPath;
533 /* got to the end of the string */
536 if(yaffs_strcmp(str,_Y(".")) == 0)
540 else if(yaffs_strcmp(str,_Y("..")) == 0)
543 dir = yaffs_find_by_name(dir,str);
545 dir = yaffsfs_FollowLink(dir,symDepth);
547 if(dir && dir->variant_type !=
548 YAFFS_OBJECT_TYPE_DIRECTORY){
557 /* directory did not exist. */
561 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDirectory,
567 return yaffsfs_DoFindDirectory(relDirectory,path,name,symDepth,notDir);
571 * yaffsfs_FindObject turns a path for an existing object into the object
573 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
574 const YCHAR *path,int symDepth, int getEquiv,
575 struct yaffs_obj **dirOut, int *notDir)
577 struct yaffs_obj *dir;
578 struct yaffs_obj *obj;
581 dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth,notDir);
587 obj = yaffs_find_by_name(dir,name);
592 obj = yaffs_get_equivalent_obj(obj);
598 int yaffs_dup(int fd)
601 yaffsfs_Handle *oldPtr = NULL;
602 yaffsfs_Handle *newPtr = NULL;
606 oldPtr = yaffsfs_GetHandlePointer(fd);
607 if(oldPtr && oldPtr->useCount > 0)
608 newHandle = yaffsfs_GetNewHandle();
610 newPtr = yaffsfs_GetHandlePointer(newHandle);
618 yaffsfs_SetError(-EBADF);
620 yaffsfs_SetError(-ENOMEM);
628 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
630 struct yaffs_obj *obj = NULL;
631 struct yaffs_obj *dir = NULL;
634 yaffsfs_Handle *yh = NULL;
637 int errorReported = 0;
638 int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
639 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
640 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
641 u8 sharedReadAllowed;
642 u8 sharedWriteAllowed;
648 if(yaffsfs_CheckPath(path) < 0){
649 yaffsfs_SetError(-ENAMETOOLONG);
653 /* O_EXCL only has meaning if O_CREAT is specified */
654 if(!(oflag & O_CREAT))
657 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
658 if( (oflag & O_CREAT) & (oflag & O_EXCL))
661 /* Todo: Are there any more flag combos to sanitise ? */
663 /* Figure out if reading or writing is requested */
665 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
666 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
670 handle = yaffsfs_GetNewHandle();
673 yaffsfs_SetError(-ENFILE);
677 yh = yaffsfs_GetHandlePointer(handle);
679 /* try to find the exisiting object */
680 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL);
682 obj = yaffsfs_FollowLink(obj,symDepth++);
685 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
686 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
692 /* The file already exists or it might be a directory */
694 /* If it is a directory then we can't open it as a file */
695 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
697 yaffsfs_SetError(-EISDIR);
701 /* Open should fail if O_CREAT and O_EXCL are specified since
704 if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
706 yaffsfs_SetError(-EEXIST);
710 /* Check file permissions */
711 if( readRequested && !(obj->yst_mode & S_IREAD))
714 if( writeRequested && !(obj->yst_mode & S_IWRITE))
717 if(openDenied && !errorReported ) {
718 /* Error if the file exists but permissions are refused. */
719 yaffsfs_SetError(-EACCES);
723 /* Check sharing of an existing object. */
727 sharedReadAllowed = 1;
728 sharedWriteAllowed = 1;
731 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
732 hx = &yaffsfs_handle[i];
733 if(hx->useCount > 0 &&
735 yaffsfs_inode[hx->inodeId].iObj == obj){
737 sharedReadAllowed = 0;
739 sharedWriteAllowed = 0;
749 if((!sharedReadAllowed && readRequested)||
750 (!shareRead && alreadyReading) ||
751 (!sharedWriteAllowed && writeRequested) ||
752 (!shareWrite && alreadyWriting)){
754 yaffsfs_SetError(-EBUSY);
761 /* If we could not open an existing object, then let's see if
762 * the directory exists. If not, error.
764 if(!obj && !errorReported){
765 dir = yaffsfs_FindDirectory(NULL,path,&name,0,NULL);
767 yaffsfs_SetError(-ENOTDIR);
772 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
773 /* Let's see if we can create this file if it does not exist. */
774 if(dir->my_dev->read_only){
775 yaffsfs_SetError(-EINVAL);
778 obj = yaffs_create_file(dir,name,mode,0,0);
780 if(!obj && !errorReported){
781 yaffsfs_SetError(-ENOSPC);
786 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
787 /* Error if the file does not exist and CREAT is not set. */
788 yaffsfs_SetError(-ENOENT);
792 if(obj && !openDenied) {
793 int inodeId = yaffsfs_GetInodeIdForObject(obj);
797 * Todo: Fix any problem if inodes run out, though that
798 * can't happen if the number of inode items >= number of handles.
802 yh->inodeId = inodeId;
803 yh->reading = readRequested;
804 yh->writing = writeRequested;
805 yh->append = (oflag & O_APPEND) ? 1 : 0;
807 yh->shareRead = shareRead;
808 yh->shareWrite = shareWrite;
810 /* Hook inode to object */
811 obj->my_inode = (void*) &yaffsfs_inode[inodeId];
813 if((oflag & O_TRUNC) && yh->writing)
814 yaffs_resize_file(obj,0);
816 yaffsfs_PutHandle(handle);
818 yaffsfs_SetError(0); /* Problem */
828 int yaffs_open(const YCHAR *path, int oflag, int mode)
830 return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
833 int yaffs_Dofsync(int fd,int datasync)
835 yaffsfs_Handle *h = NULL;
840 h = yaffsfs_GetHandlePointer(fd);
842 if(h && h->useCount > 0)
844 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
847 yaffsfs_SetError(-EBADF);
856 int yaffs_fsync(int fd)
858 return yaffs_Dofsync(fd,0);
861 int yaffs_flush(int fd)
863 return yaffs_fsync(fd);
866 int yaffs_fdatasync(int fd)
868 return yaffs_Dofsync(fd,1);
871 int yaffs_close(int fd)
873 yaffsfs_Handle *h = NULL;
878 h = yaffsfs_GetHandlePointer(fd);
880 if(h && h->useCount > 0) {
882 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
883 yaffsfs_PutHandle(fd);
887 yaffsfs_SetError(-EBADF);
898 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
900 yaffsfs_Handle *h = NULL;
901 struct yaffs_obj *obj = NULL;
908 unsigned int maxRead;
909 u8 *buf = (u8 *)vbuf;
912 h = yaffsfs_GetHandlePointer(fd);
913 obj = yaffsfs_GetHandleObject(fd);
917 yaffsfs_SetError(-EBADF);
919 } else if(!h->reading){
920 /* Not a reading handle */
921 yaffsfs_SetError(-EINVAL);
923 } else if(nbyte > YAFFS_MAX_FILE_SIZE){
924 yaffsfs_SetError(-EINVAL);
930 startPos = h->position;
934 if(yaffs_get_obj_length(obj) > pos)
935 maxRead = yaffs_get_obj_length(obj) - pos;
943 yaffsfs_GetHandle(fd);
945 endPos = pos + nbyte;
947 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
948 nbyte > YAFFS_MAX_FILE_SIZE ||
949 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
955 nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
960 * Need to reverify object in case the device was
961 * unmounted in another thread.
963 obj = yaffsfs_GetHandleObject(fd);
967 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
978 nbyte = 0; /* no more to read */
988 yaffsfs_PutHandle(fd);
992 h->position = startPos + totalRead;
994 yaffsfs_SetError(-EINVAL);
1001 return (totalRead >= 0) ? totalRead : -1;
1005 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1007 return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1010 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1012 return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1015 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1017 yaffsfs_Handle *h = NULL;
1018 struct yaffs_obj *obj = NULL;
1023 int totalWritten = 0;
1024 int write_trhrough = 0;
1026 const u8 *buf = (const u8 *)vbuf;
1029 h = yaffsfs_GetHandlePointer(fd);
1030 obj = yaffsfs_GetHandleObject(fd);
1034 yaffsfs_SetError(-EBADF);
1036 } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1037 yaffsfs_SetError(-EINVAL);
1041 startPos = yaffs_get_obj_length(obj);
1045 startPos = h->position;
1047 yaffsfs_GetHandle(fd);
1049 endPos = pos + nbyte;
1051 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1052 nbyte > YAFFS_MAX_FILE_SIZE ||
1053 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1060 nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1061 if(nToWrite > nbyte)
1065 * Need to reverify object in case the device was
1066 * remounted or unmounted in another thread.
1068 obj = yaffsfs_GetHandleObject(fd);
1069 if(!obj || obj->my_dev->read_only)
1072 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1075 totalWritten += nWritten;
1080 if(nWritten == nToWrite)
1085 if(nWritten < 1 && totalWritten < 1){
1086 yaffsfs_SetError(-ENOSPC);
1096 yaffsfs_PutHandle(fd);
1099 if(totalWritten > 0)
1100 h->position = startPos + totalWritten;
1102 yaffsfs_SetError(-EINVAL);
1108 return (totalWritten >= 0) ? totalWritten : -1;
1111 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1113 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1116 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1118 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1122 int yaffs_truncate(const YCHAR *path,off_t new_size)
1124 struct yaffs_obj *obj = NULL;
1125 struct yaffs_obj *dir = NULL;
1126 int result = YAFFS_FAIL;
1129 if(yaffsfs_CheckPath(path) < 0){
1130 yaffsfs_SetError(-ENAMETOOLONG);
1136 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1139 yaffsfs_SetError(-ENOTDIR);
1140 else if(!dir || !obj)
1141 yaffsfs_SetError(-ENOENT);
1142 else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1143 yaffsfs_SetError(-EISDIR);
1144 else if(obj->my_dev->read_only)
1145 yaffsfs_SetError(-EACCES);
1146 else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1147 yaffsfs_SetError(-EINVAL);
1149 result = yaffs_resize_file(obj,new_size);
1153 return (result) ? 0 : -1;
1156 int yaffs_ftruncate(int fd, off_t new_size)
1158 yaffsfs_Handle *h = NULL;
1159 struct yaffs_obj *obj = NULL;
1163 h = yaffsfs_GetHandlePointer(fd);
1164 obj = yaffsfs_GetHandleObject(fd);
1168 yaffsfs_SetError(-EBADF);
1169 else if(obj->my_dev->read_only)
1170 yaffsfs_SetError(-EACCES);
1171 else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1172 yaffsfs_SetError(-EINVAL);
1174 /* resize the file */
1175 result = yaffs_resize_file(obj,new_size);
1178 return (result) ? 0 : -1;
1182 off_t yaffs_lseek(int fd, off_t offset, int whence)
1184 yaffsfs_Handle *h = NULL;
1185 struct yaffs_obj *obj = NULL;
1190 h = yaffsfs_GetHandlePointer(fd);
1191 obj = yaffsfs_GetHandleObject(fd);
1194 yaffsfs_SetError(-EBADF);
1195 else if(offset > YAFFS_MAX_FILE_SIZE)
1196 yaffsfs_SetError(-EINVAL);
1198 if(whence == SEEK_SET){
1201 } else if(whence == SEEK_CUR) {
1202 if( (h->position + offset) >= 0)
1203 pos = (h->position + offset);
1204 } else if(whence == SEEK_END) {
1205 fSize = yaffs_get_obj_length(obj);
1206 if(fSize >= 0 && (fSize + offset) >= 0)
1207 pos = fSize + offset;
1210 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1213 yaffsfs_SetError(-EINVAL);
1224 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1226 struct yaffs_obj *dir = NULL;
1227 struct yaffs_obj *obj = NULL;
1229 int result = YAFFS_FAIL;
1232 if(yaffsfs_CheckPath(path) < 0){
1233 yaffsfs_SetError(-ENAMETOOLONG);
1239 obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL);
1240 dir = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir);
1243 yaffsfs_SetError(-ENOTDIR);
1244 else if(!dir || !obj)
1245 yaffsfs_SetError(-ENOENT);
1246 else if(obj->my_dev->read_only)
1247 yaffsfs_SetError(-EINVAL);
1248 else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1249 yaffsfs_SetError(-EISDIR);
1250 else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1251 yaffsfs_SetError(-ENOTDIR);
1253 result = yaffs_unlinker(dir,name);
1255 if(result == YAFFS_FAIL && isDirectory)
1256 yaffsfs_SetError(-ENOTEMPTY);
1261 return (result == YAFFS_FAIL) ? -1 : 0;
1265 int yaffs_unlink(const YCHAR *path)
1267 return yaffsfs_DoUnlink(path,0);
1270 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1272 struct yaffs_obj *olddir = NULL;
1273 struct yaffs_obj *newdir = NULL;
1274 struct yaffs_obj *obj = NULL;
1277 int result= YAFFS_FAIL;
1278 int rename_allowed = 1;
1284 if(yaffsfs_CheckPath(newPath) < 0){
1285 yaffsfs_SetError(-ENAMETOOLONG);
1289 olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,¬OldDir);
1290 newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,¬NewDir);
1291 obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL);
1293 if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1294 yaffsfs_SetError(-ENOTDIR);
1296 } else if(!olddir || !newdir || !obj) {
1297 yaffsfs_SetError(-ENOENT);
1299 } else if(obj->my_dev->read_only){
1300 yaffsfs_SetError(-EROFS);
1302 } else if(olddir->my_dev != newdir->my_dev) {
1303 /* Rename must be on same device */
1304 yaffsfs_SetError(-EXDEV);
1306 } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1308 * It is a directory, check that it is not being renamed to
1309 * being its own decendent.
1310 * Do this by tracing from the new directory back to the root, checking for obj
1313 struct yaffs_obj *xx = newdir;
1315 while( rename_allowed && xx){
1321 yaffsfs_SetError(-EINVAL);
1325 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1329 return (result == YAFFS_FAIL) ? -1 : 0;
1333 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1337 obj = yaffs_get_equivalent_obj(obj);
1340 buf->st_dev = (int)obj->my_dev->os_context;
1341 buf->st_ino = obj->obj_id;
1342 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1344 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1345 buf->st_mode |= S_IFDIR;
1346 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1347 buf->st_mode |= S_IFLNK;
1348 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1349 buf->st_mode |= S_IFREG;
1351 buf->st_nlink = yaffs_get_obj_link_count(obj);
1354 buf->st_rdev = obj->yst_rdev;
1355 buf->st_size = yaffs_get_obj_length(obj);
1356 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1357 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1358 #if CONFIG_YAFFS_WINCE
1359 buf->yst_wince_atime[0] = obj->win_atime[0];
1360 buf->yst_wince_atime[1] = obj->win_atime[1];
1361 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1362 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1363 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1364 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1366 buf->yst_atime = obj->yst_atime;
1367 buf->yst_ctime = obj->yst_ctime;
1368 buf->yst_mtime = obj->yst_mtime;
1375 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1377 struct yaffs_obj *obj=NULL;
1378 struct yaffs_obj *dir=NULL;
1382 if(yaffsfs_CheckPath(path) < 0){
1383 yaffsfs_SetError(-ENAMETOOLONG);
1389 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1392 obj = yaffsfs_FollowLink(obj,0);
1395 yaffsfs_SetError(-ENOTDIR);
1396 else if(!dir || !obj)
1397 yaffsfs_SetError(-ENOENT);
1399 retVal = yaffsfs_DoStat(obj,buf);
1407 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1409 return yaffsfs_DoStatOrLStat(path,buf,0);
1412 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1414 return yaffsfs_DoStatOrLStat(path,buf,1);
1417 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1419 struct yaffs_obj *obj;
1424 obj = yaffsfs_GetHandleObject(fd);
1427 retVal = yaffsfs_DoStat(obj,buf);
1430 yaffsfs_SetError(-EBADF);
1437 #ifndef CONFIG_YAFFS_WINCE
1438 /* xattrib functions */
1441 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1443 struct yaffs_obj *obj;
1444 struct yaffs_obj *dir;
1449 if(yaffsfs_CheckPath(path) < 0){
1450 yaffsfs_SetError(-ENAMETOOLONG);
1456 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1459 obj = yaffsfs_FollowLink(obj,0);
1462 yaffsfs_SetError(-ENOTDIR);
1463 else if(!dir || !obj)
1464 yaffsfs_SetError(-ENOENT);
1466 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1468 yaffsfs_SetError(retVal);
1479 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1481 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1484 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1486 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1491 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1493 struct yaffs_obj *obj;
1498 obj = yaffsfs_GetHandleObject(fd);
1501 yaffsfs_SetError(-EBADF);
1503 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1505 yaffsfs_SetError(retVal);
1515 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1517 struct yaffs_obj *obj;
1518 struct yaffs_obj *dir;
1522 if(yaffsfs_CheckPath(path) < 0){
1523 yaffsfs_SetError(-ENAMETOOLONG);
1529 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1532 obj = yaffsfs_FollowLink(obj,0);
1535 yaffsfs_SetError(-ENOTDIR);
1536 else if(!dir || !obj)
1537 yaffsfs_SetError(-ENOENT);
1539 retVal = yaffs_get_xattrib(obj,name,data,size);
1541 yaffsfs_SetError(retVal);
1551 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1553 return yaffs_do_getxattr( path, name, data, size, 1);
1555 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1557 return yaffs_do_getxattr( path, name, data, size, 0);
1562 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1564 struct yaffs_obj *obj;
1569 obj = yaffsfs_GetHandleObject(fd);
1572 retVal = yaffs_get_xattrib(obj,name,data,size);
1574 yaffsfs_SetError(retVal);
1579 yaffsfs_SetError(-EBADF);
1586 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1588 struct yaffs_obj *obj=NULL;
1589 struct yaffs_obj *dir=NULL;
1593 if(yaffsfs_CheckPath(path) < 0){
1594 yaffsfs_SetError(-ENAMETOOLONG);
1600 obj = yaffsfs_FindObject(NULL,path,0,1,&dir,¬Dir);
1603 obj = yaffsfs_FollowLink(obj,0);
1606 yaffsfs_SetError(-ENOTDIR);
1607 else if(!dir || !obj)
1608 yaffsfs_SetError(-ENOENT);
1610 retVal = yaffs_list_xattrib(obj, data,size);
1612 yaffsfs_SetError(retVal);
1623 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1625 return yaffs_do_listxattr(path, data, size, 1);
1628 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1630 return yaffs_do_listxattr(path, data, size, 0);
1633 int yaffs_flistxattr(int fd, char *data, int size)
1635 struct yaffs_obj *obj;
1640 obj = yaffsfs_GetHandleObject(fd);
1643 retVal = yaffs_list_xattrib(obj,data,size);
1645 yaffsfs_SetError(retVal);
1650 yaffsfs_SetError(-EBADF);
1657 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1659 struct yaffs_obj *obj=NULL;
1660 struct yaffs_obj *dir=NULL;
1664 if(yaffsfs_CheckPath(path) < 0){
1665 yaffsfs_SetError(-ENAMETOOLONG);
1671 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
1674 obj = yaffsfs_FollowLink(obj,0);
1677 yaffsfs_SetError(-ENOTDIR);
1678 else if(!dir || !obj)
1679 yaffsfs_SetError(-ENOENT);
1681 retVal = yaffs_remove_xattrib(obj,name);
1683 yaffsfs_SetError(retVal);
1694 int yaffs_removexattr(const YCHAR *path, const char *name)
1696 return yaffs_do_removexattr(path, name, 1);
1699 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1701 return yaffs_do_removexattr(path, name, 0);
1704 int yaffs_fremovexattr(int fd, const char *name)
1706 struct yaffs_obj *obj;
1711 obj = yaffsfs_GetHandleObject(fd);
1714 retVal = yaffs_remove_xattrib(obj,name);
1716 yaffsfs_SetError(retVal);
1721 yaffsfs_SetError(-EBADF);
1729 #ifdef CONFIG_YAFFS_WINCE
1730 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1732 struct yaffs_obj *obj;
1737 obj = yaffsfs_GetHandleObject(fd);
1742 wctime[0] = obj->win_ctime[0];
1743 wctime[1] = obj->win_ctime[1];
1746 watime[0] = obj->win_atime[0];
1747 watime[1] = obj->win_atime[1];
1750 wmtime[0] = obj->win_mtime[0];
1751 wmtime[1] = obj->win_mtime[1];
1758 yaffsfs_SetError(-EBADF);
1766 int yaffs_set_wince_times(int fd,
1767 const unsigned *wctime,
1768 const unsigned *watime,
1769 const unsigned *wmtime)
1771 struct yaffs_obj *obj;
1776 obj = yaffsfs_GetHandleObject(fd);
1781 obj->win_ctime[0] = wctime[0];
1782 obj->win_ctime[1] = wctime[1];
1785 obj->win_atime[0] = watime[0];
1786 obj->win_atime[1] = watime[1];
1789 obj->win_mtime[0] = wmtime[0];
1790 obj->win_mtime[1] = wmtime[1];
1794 result = yaffs_flush_file(obj,0,0);
1798 yaffsfs_SetError(-EBADF);
1808 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1813 obj = yaffs_get_equivalent_obj(obj);
1816 obj->yst_mode = mode;
1818 result = yaffs_flush_file(obj,0,0);
1821 return result == YAFFS_OK ? 0 : -1;
1825 int yaffs_access(const YCHAR *path, int amode)
1827 struct yaffs_obj *obj=NULL;
1828 struct yaffs_obj *dir=NULL;
1832 if(yaffsfs_CheckPath(path) < 0){
1833 yaffsfs_SetError(-ENAMETOOLONG);
1837 if(amode & ~(R_OK | W_OK | X_OK)){
1838 yaffsfs_SetError(-EINVAL);
1844 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
1847 yaffsfs_SetError(-ENOTDIR);
1848 else if(!dir || !obj)
1849 yaffsfs_SetError(-ENOENT);
1853 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1855 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1857 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1861 yaffsfs_SetError(-EACCES);
1873 int yaffs_chmod(const YCHAR *path, mode_t mode)
1875 struct yaffs_obj *obj=NULL;
1876 struct yaffs_obj *dir=NULL;
1880 if(yaffsfs_CheckPath(path) < 0){
1881 yaffsfs_SetError(-ENAMETOOLONG);
1886 yaffsfs_SetError(-EINVAL);
1892 obj = yaffsfs_FindObject(NULL,path,0,1, &dir, ¬Dir);
1895 yaffsfs_SetError(-ENOTDIR);
1896 else if(!dir || !obj)
1897 yaffsfs_SetError(-ENOENT);
1898 else if(obj->my_dev->read_only)
1899 yaffsfs_SetError(-EROFS);
1901 retVal = yaffsfs_DoChMod(obj,mode);
1910 int yaffs_fchmod(int fd, mode_t mode)
1912 struct yaffs_obj *obj;
1916 yaffsfs_SetError(-EINVAL);
1921 obj = yaffsfs_GetHandleObject(fd);
1924 yaffsfs_SetError(-EBADF);
1925 else if(obj->my_dev->read_only)
1926 yaffsfs_SetError(-EROFS);
1928 retVal = yaffsfs_DoChMod(obj,mode);
1936 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1938 YCHAR *alt_path = NULL;
1943 * We don't have a definition for max path length.
1944 * We will use 3 * max name length instead.
1947 path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1949 /* If the last character is a path divider, then we need to
1950 * trim it back so that the name look-up works properly.
1951 * eg. /foo/new_dir/ -> /foo/newdir
1952 * Curveball: Need to handle multiple path dividers:
1953 * eg. /foof/sdfse///// -> /foo/sdfse
1955 if(path_length > 0 &&
1956 yaffsfs_IsPathDivider(path[path_length-1])){
1957 alt_path = YMALLOC(path_length + 1);
1960 strcpy(alt_path, path);
1961 for(i = path_length-1;
1962 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1964 alt_path[i] = (YCHAR) 0;
1966 *ret_path = alt_path;
1970 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1972 struct yaffs_obj *parent = NULL;
1973 struct yaffs_obj *dir = NULL;
1975 YCHAR *alt_path = NULL;
1979 if(yaffsfs_CheckPath(path) < 0){
1980 yaffsfs_SetError(-ENAMETOOLONG);
1984 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1985 yaffsfs_SetError(-ENOMEM);
1992 parent = yaffsfs_FindDirectory(NULL,path,&name,0,¬Dir);
1993 if(!parent && notDir)
1994 yaffsfs_SetError(-ENOTDIR);
1996 yaffsfs_SetError(-ENOENT);
1997 else if(yaffs_strnlen(name,5) == 0){
1998 /* Trying to make the root itself */
1999 yaffsfs_SetError(-EEXIST);
2000 } else if(parent->my_dev->read_only)
2001 yaffsfs_SetError(-EROFS);
2003 dir = yaffs_create_dir(parent,name,mode,0,0);
2006 else if (yaffs_find_by_name(parent,name))
2007 yaffsfs_SetError(-EEXIST); /* the name already exists */
2009 yaffsfs_SetError(-ENOSPC); /* just assume no space */
2020 int yaffs_rmdir(const YCHAR *path)
2025 if(yaffsfs_CheckPath(path) < 0){
2026 yaffsfs_SetError(-ENAMETOOLONG);
2030 if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2031 yaffsfs_SetError(-ENOMEM);
2036 result = yaffsfs_DoUnlink(path,1);
2043 void * yaffs_getdev(const YCHAR *path)
2045 struct yaffs_dev *dev=NULL;
2047 dev = yaffsfs_FindDevice(path,&dummy);
2051 int yaffs_mount2(const YCHAR *path,int read_only)
2054 int result=YAFFS_FAIL;
2055 struct yaffs_dev *dev=NULL;
2057 T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2059 if(yaffsfs_CheckPath(path) < 0){
2060 yaffsfs_SetError(-ENAMETOOLONG);
2066 yaffsfs_InitHandles();
2068 dev = yaffsfs_FindMountPoint(path);
2070 if(!dev->is_mounted){
2071 dev->read_only = read_only ? 1 : 0;
2072 result = yaffs_guts_initialise(dev);
2073 if(result == YAFFS_FAIL)
2074 yaffsfs_SetError(-ENOMEM);
2075 retVal = result ? 0 : -1;
2079 yaffsfs_SetError(-EBUSY);
2081 yaffsfs_SetError(-ENODEV);
2088 int yaffs_mount(const YCHAR *path)
2090 return yaffs_mount2(path,0);
2093 int yaffs_sync(const YCHAR *path)
2096 struct yaffs_dev *dev=NULL;
2099 if(yaffsfs_CheckPath(path) < 0){
2100 yaffsfs_SetError(-ENAMETOOLONG);
2105 dev = yaffsfs_FindDevice(path,&dummy);
2107 if(dev->is_mounted){
2109 yaffs_flush_whole_cache(dev);
2110 yaffs_checkpoint_save(dev);
2114 yaffsfs_SetError(-EINVAL);
2117 yaffsfs_SetError(-ENODEV);
2124 int yaffs_remount(const YCHAR *path, int force, int read_only)
2127 struct yaffs_dev *dev=NULL;
2130 if(yaffsfs_CheckPath(path) < 0){
2131 yaffsfs_SetError(-ENAMETOOLONG);
2136 dev = yaffsfs_FindMountPoint(path);
2138 if(dev->is_mounted){
2142 yaffs_flush_whole_cache(dev);
2144 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2145 yh = & yaffsfs_handle[i];
2146 if(yh->useCount>0 &&
2147 yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2148 inUse = 1; /* the device is in use, can't unmount */
2151 if(!inUse || force){
2153 yaffs_checkpoint_save(dev);
2154 dev->read_only = read_only ? 1 : 0;
2157 yaffsfs_SetError(-EBUSY);
2160 yaffsfs_SetError(-EINVAL);
2164 yaffsfs_SetError(-ENODEV);
2171 int yaffs_unmount2(const YCHAR *path, int force)
2174 struct yaffs_dev *dev=NULL;
2176 if(yaffsfs_CheckPath(path) < 0){
2177 yaffsfs_SetError(-ENAMETOOLONG);
2182 dev = yaffsfs_FindMountPoint(path);
2184 if(dev->is_mounted){
2188 yaffs_flush_whole_cache(dev);
2189 yaffs_checkpoint_save(dev);
2191 for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2192 if(yaffsfs_handle[i].useCount > 0 &&
2193 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2194 inUse = 1; /* the device is in use, can't unmount */
2197 if(!inUse || force){
2199 yaffsfs_BreakDeviceHandles(dev);
2200 yaffs_deinitialise(dev);
2204 yaffsfs_SetError(-EBUSY);
2207 yaffsfs_SetError(-EINVAL);
2210 yaffsfs_SetError(-ENODEV);
2217 int yaffs_unmount(const YCHAR *path)
2219 return yaffs_unmount2(path,0);
2222 loff_t yaffs_freespace(const YCHAR *path)
2225 struct yaffs_dev *dev=NULL;
2228 if(yaffsfs_CheckPath(path) < 0){
2229 yaffsfs_SetError(-ENAMETOOLONG);
2234 dev = yaffsfs_FindDevice(path,&dummy);
2235 if(dev && dev->is_mounted){
2236 retVal = yaffs_get_n_free_chunks(dev);
2237 retVal *= dev->data_bytes_per_chunk;
2240 yaffsfs_SetError(-EINVAL);
2246 loff_t yaffs_totalspace(const YCHAR *path)
2249 struct yaffs_dev *dev=NULL;
2252 if(yaffsfs_CheckPath(path) < 0){
2253 yaffsfs_SetError(-ENAMETOOLONG);
2258 dev = yaffsfs_FindDevice(path,&dummy);
2259 if(dev && dev->is_mounted){
2260 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2261 dev->param.n_reserved_blocks;
2262 retVal *= dev->param.chunks_per_block;
2263 retVal *= dev->data_bytes_per_chunk;
2266 yaffsfs_SetError(-EINVAL);
2272 int yaffs_inodecount(const YCHAR *path)
2275 struct yaffs_dev *dev=NULL;
2278 if(yaffsfs_CheckPath(path) < 0){
2279 yaffsfs_SetError(-ENAMETOOLONG);
2284 dev = yaffsfs_FindDevice(path,&dummy);
2285 if(dev && dev->is_mounted) {
2286 int n_obj = dev->n_obj;
2287 if(n_obj > dev->n_hardlinks)
2288 retVal = n_obj - dev->n_hardlinks;
2292 yaffsfs_SetError(-EINVAL);
2299 void yaffs_add_device(struct yaffs_dev *dev)
2301 dev->is_mounted = 0;
2302 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2304 if(!dev->dev_list.next)
2305 INIT_LIST_HEAD(&dev->dev_list);
2307 list_add(&dev->dev_list,&yaffsfs_deviceList);
2310 void yaffs_remove_device(struct yaffs_dev *dev)
2312 list_del_init(&dev->dev_list);
2318 /* Directory search stuff. */
2321 * Directory search context
2323 * NB this is an opaque structure.
2330 yaffs_dirent de; /* directory entry being used by this dsc */
2331 YCHAR name[NAME_MAX+1]; /* name of directory being searched */
2332 struct yaffs_obj *dirObj; /* ptr to directory being searched */
2333 struct yaffs_obj *nextReturn; /* obj to be returned by next readddir */
2335 struct list_head others;
2336 } yaffsfs_DirectorySearchContext;
2340 static struct list_head search_contexts;
2343 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2347 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2351 if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2352 dsc->nextReturn = NULL;
2354 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2355 struct yaffs_obj,siblings);
2357 /* Hey someone isn't playing nice! */
2361 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2365 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2367 if( dsc->nextReturn == NULL ||
2368 list_empty(&dsc->dirObj->variant.dir_variant.children))
2369 dsc->nextReturn = NULL;
2371 struct list_head *next = dsc->nextReturn->siblings.next;
2373 if( next == &dsc->dirObj->variant.dir_variant.children)
2374 dsc->nextReturn = NULL; /* end of list */
2376 dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2379 /* Hey someone isn't playing nice! */
2383 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2386 struct list_head *i;
2387 yaffsfs_DirectorySearchContext *dsc;
2389 /* if search contexts not initilised then skip */
2390 if(!search_contexts.next)
2393 /* Iterate through the directory search contexts.
2394 * If any are the one being removed, then advance the dsc to
2395 * the next one to prevent a hanging ptr.
2397 list_for_each(i, &search_contexts) {
2399 dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2400 if(dsc->nextReturn == obj)
2401 yaffsfs_DirAdvance(dsc);
2407 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2409 yaffs_DIR *dir = NULL;
2410 struct yaffs_obj *obj = NULL;
2411 yaffsfs_DirectorySearchContext *dsc = NULL;
2414 if(yaffsfs_CheckPath(dirname) < 0){
2415 yaffsfs_SetError(-ENAMETOOLONG);
2421 obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,¬Dir);
2424 yaffsfs_SetError(-ENOTDIR);
2426 yaffsfs_SetError(-ENOENT);
2427 else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2428 yaffsfs_SetError(-ENOTDIR);
2431 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2432 dir = (yaffs_DIR *)dsc;
2435 memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2436 dsc->magic = YAFFS_MAGIC;
2438 yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2439 INIT_LIST_HEAD(&dsc->others);
2441 if(!search_contexts.next)
2442 INIT_LIST_HEAD(&search_contexts);
2444 list_add(&dsc->others,&search_contexts);
2445 yaffsfs_SetDirRewound(dsc);
2455 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2457 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2458 struct yaffs_dirent *retVal = NULL;
2462 if(dsc && dsc->magic == YAFFS_MAGIC){
2463 yaffsfs_SetError(0);
2464 if(dsc->nextReturn){
2465 dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2466 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2467 dsc->de.d_off = dsc->offset++;
2468 yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2469 if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2471 /* this should not happen! */
2472 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2474 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2476 yaffsfs_DirAdvance(dsc);
2480 yaffsfs_SetError(-EBADF);
2489 void yaffs_rewinddir(yaffs_DIR *dirp)
2491 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2495 yaffsfs_SetDirRewound(dsc);
2501 int yaffs_closedir(yaffs_DIR *dirp)
2503 yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2507 list_del(&dsc->others); /* unhook from list */
2513 /* End of directory stuff */
2516 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2518 struct yaffs_obj *parent = NULL;
2519 struct yaffs_obj *obj;
2522 int mode = 0; /* ignore for now */
2525 if(yaffsfs_CheckPath(newpath) < 0){
2526 yaffsfs_SetError(-ENAMETOOLONG);
2530 parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,¬Dir);
2531 if(!parent && notDir)
2532 yaffsfs_SetError(-ENOTDIR);
2533 else if( !parent || yaffs_strnlen(name,5) < 1)
2534 yaffsfs_SetError(-ENOENT);
2535 else if(parent->my_dev->read_only)
2536 yaffsfs_SetError(-EROFS);
2538 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2541 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL))
2542 yaffsfs_SetError(-EEXIST);
2544 yaffsfs_SetError(-ENOSPC);
2553 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2555 struct yaffs_obj *obj = NULL;
2556 struct yaffs_obj *dir = NULL;
2562 obj = yaffsfs_FindObject(NULL,path,0,1, &dir,¬Dir);
2565 yaffsfs_SetError(-ENOTDIR);
2566 else if(!dir || !obj)
2567 yaffsfs_SetError(-ENOENT);
2568 else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2569 yaffsfs_SetError(-EINVAL);
2571 YCHAR *alias = obj->variant.symlink_variant.alias;
2572 memset(buf,0,bufsiz);
2573 yaffs_strncpy(buf,alias,bufsiz - 1);
2580 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2582 /* Creates a link called newpath to existing oldpath */
2583 struct yaffs_obj *obj = NULL;
2584 struct yaffs_obj *lnk = NULL;
2585 struct yaffs_obj *obj_dir = NULL;
2586 struct yaffs_obj *lnk_dir = NULL;
2592 if(yaffsfs_CheckPath(linkpath) < 0){
2593 yaffsfs_SetError(-ENAMETOOLONG);
2599 obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,¬DirObj);
2600 lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL);
2601 lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,¬DirLnk);
2603 if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2604 yaffsfs_SetError(-ENOTDIR);
2605 else if(!obj_dir || !lnk_dir || !obj)
2606 yaffsfs_SetError(-ENOENT);
2607 else if(obj->my_dev->read_only)
2608 yaffsfs_SetError(-EINVAL);
2610 yaffsfs_SetError(-EEXIST);
2611 else if(lnk_dir->my_dev != obj->my_dev)
2612 yaffsfs_SetError(-EXDEV);
2614 retVal = yaffsfs_CheckNameLength(newname);
2617 lnk = yaffs_link_obj(lnk_dir,newname,obj);
2621 yaffsfs_SetError(-ENOSPC);
2631 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2637 yaffsfs_SetError(-EINVAL);
2643 * D E B U G F U N C T I O N S
2648 * Returns number of handles attached to the object
2650 int yaffs_n_handles(const YCHAR *path)
2652 struct yaffs_obj *obj;
2654 if(yaffsfs_CheckPath(path) < 0){
2655 yaffsfs_SetError(-ENAMETOOLONG);
2659 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL);
2662 return yaffsfs_CountHandles(obj);
2667 int yaffs_get_error(void)
2669 return yaffsfs_GetLastError();
2672 int yaffs_set_error(int error)
2674 yaffsfs_SetError(error);
2678 int yaffs_dump_dev(const YCHAR *path)
2685 struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2688 struct yaffs_dev *dev = obj->my_dev;
2691 "n_page_writes.......... %d\n"
2692 "n_page_reads........... %d\n"
2693 "n_erasures....... %d\n"
2694 "n_gc_copies............ %d\n"
2695 "garbageCollections... %d\n"
2696 "passiveGarbageColl'ns %d\n"
2702 dev->garbageCollections,
2703 dev->passiveGarbageCollections