yaffs Added some more tests to direct/timothy_tests/quick_tests
[yaffs2.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
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.
12  */
13
14 #include "yaffsfs.h"
15 #include "yaffs_guts.h"
16 #include "yaffscfg.h"
17 #include "yportenv.h"
18 #include "yaffs_trace.h"
19
20 #include <string.h> /* for memset */
21
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
23
24 #ifndef NULL
25 #define NULL ((void *)0)
26 #endif
27
28
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)
32
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
35                         const YCHAR *path,
36                         int symDepth, int getEquiv,
37                         struct yaffs_obj **dirOut,
38                         int *notDir, int *loop);
39
40 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
41
42 unsigned int yaffs_wr_attempts;
43
44 /*
45  * Handle management.
46  * There are open inodes in yaffsfs_Inode.
47  * There are open handles in yaffsfs_Handle.
48  *
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.
53  */
54
55 typedef struct {
56         int count;      /* Number of handles accessing this inode */
57         struct yaffs_obj *iObj;
58 } yaffsfs_Inode;
59
60 typedef struct{
61         u8      reading:1;
62         u8      writing:1;
63         u8      append:1;
64         u8      shareRead:1;
65         u8      shareWrite:1;
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 */
69 }yaffsfs_Handle;
70
71 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
72 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
73 static int yaffsfs_handlesInitialised;
74
75
76 unsigned yaffs_set_trace(unsigned  tm) 
77 {
78         yaffs_trace_mask = tm;
79         return yaffs_trace_mask;
80 }
81
82 unsigned yaffs_get_trace(void)
83 {
84         return yaffs_trace_mask;
85 }
86
87 /*
88  * yaffsfs_InitHandle
89  * Inilitalise handle management on start-up.
90  */
91
92 static void yaffsfs_InitHandles(void)
93 {
94         int i;
95         if(yaffsfs_handlesInitialised)
96                 return;
97
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;
102 }
103
104 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
105 {
106         if(h < 0 || h >= YAFFSFS_N_HANDLES)
107                 return NULL;
108
109         return &yaffsfs_handle[h];
110 }
111
112 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
113 {
114         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
115
116         if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
117                 return  &yaffsfs_inode[h->inodeId];
118
119         return NULL;
120 }
121
122 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
123 {
124         yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
125
126         if(in)
127                 return in->iObj;
128
129         return NULL;
130 }
131
132 /*
133  * yaffsfs_FindInodeIdForObject
134  * Find the inode entry for an object, if it exists.
135  */
136
137 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
138 {
139         int i;
140         int ret = -1;
141         
142         if(obj)
143                 obj = yaffs_get_equivalent_obj(obj);
144
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)
148                         ret = i;
149         }
150         return ret;
151 }
152
153 /*
154  * yaffsfs_GetInodeIdForObject
155  * Grab an inode entry when opening a new inode.
156  */
157 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
158 {
159         int i;
160         int ret;
161         yaffsfs_Inode *in = NULL;
162         
163         if(obj)
164                 obj = yaffs_get_equivalent_obj(obj);
165
166         ret = yaffsfs_FindInodeIdForObject(obj);
167
168         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
169                 if(!yaffsfs_inode[i].iObj)
170                         ret = i;
171         }
172
173         if(ret>=0){
174                 in = &yaffsfs_inode[ret];
175                 if(!in->iObj)
176                         in->count = 0;
177                 in->iObj = obj;
178                 in->count++;
179         }
180         
181         
182         return ret;
183 }
184
185
186 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
187 {
188         int i = yaffsfs_FindInodeIdForObject(obj);
189
190         if(i >= 0)
191                 return yaffsfs_inode[i].count;
192         else
193                 return 0;
194 }
195
196 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
197 {
198         struct yaffs_obj *obj;
199         
200         obj = in->iObj;
201
202         if(obj->unlinked)
203                 yaffs_del_obj(obj);
204         
205         obj->my_inode = NULL;
206         in->iObj = NULL;
207
208 }
209
210 static void yaffsfs_PutInode(int inodeId)
211 {
212         if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
213                 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
214                 in->count--;
215                 if(in->count <= 0){
216                         yaffsfs_ReleaseInode(in);
217                         in->count = 0;
218                 }
219         }       
220 }
221
222
223 /*
224  * yaffsfs_GetHandle
225  * Grab a handle (when opening a file)
226  */
227
228 static int yaffsfs_GetNewHandle(void)
229 {
230         int i;
231         yaffsfs_Handle *h;
232
233         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
234                 h = yaffsfs_GetHandlePointer(i);
235                 if(!h){
236                         /* todo bug: should never happen */
237                 }
238                 if(h->useCount < 1){
239                         memset(h,0,sizeof(yaffsfs_Handle));
240                         h->inodeId=-1;
241                         h->useCount=1;
242                         return i;
243                 }
244         }
245         return -1;
246 }
247
248 /*
249  * yaffs_get_handle
250  * Increase use of handle when reading/writing a file
251  */
252 static int yaffsfs_GetHandle(int handle)
253 {
254         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
255
256         if(h && h->useCount > 0){       
257                 h->useCount++;
258         }
259         return 0;
260 }
261
262 /*
263  * yaffs_put_handle
264  * Let go of a handle when closing a file or aborting an open or
265  * ending a read or write.
266  */
267 static int yaffsfs_PutHandle(int handle)
268 {
269         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
270
271         if(h && h->useCount > 0){       
272                 h->useCount--;
273                 if(h->useCount < 1){
274                         if(h->inodeId >= 0){
275                                 yaffsfs_PutInode(h->inodeId);
276                                 h->inodeId = -1;
277                         }
278                 }
279         }
280         return 0;
281 }
282
283 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
284 {
285         yaffsfs_Handle *h;
286         struct yaffs_obj *obj;
287         int i;
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){
292                         h->useCount = 0;
293                         yaffsfs_PutInode(h->inodeId);
294                         h->inodeId = -1;
295                 }
296         }
297 }
298
299
300
301
302 /*
303  *  Stuff to handle names.
304  */
305
306
307 int yaffsfs_Match(YCHAR a, YCHAR b)
308 {
309         /* case sensitive */
310         return (a == b);
311 }
312
313 int yaffsfs_IsPathDivider(YCHAR ch)
314 {
315         const YCHAR *str = YAFFS_PATH_DIVIDERS;
316
317         while(*str){
318                 if(*str == ch)
319                         return 1;
320                 str++;
321         }
322
323         return 0;
324 }
325
326 int yaffsfs_CheckNameLength(const char *name)
327 {
328         int retVal = 0;         
329
330         int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
331                 
332         if(nameLength == 0){
333                 yaffsfs_SetError(-ENOENT);
334                 retVal = -1;
335         } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
336                 yaffsfs_SetError(-ENAMETOOLONG);
337                 retVal = -1;
338         }
339
340         return retVal;  
341 }
342
343
344 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
345 {
346         YCHAR *alt_path = NULL;
347         int path_length;
348         int i;
349
350         /*
351          * We don't have a definition for max path length.
352          * We will use 3 * max name length instead.
353          */
354         *ret_path = NULL;
355         path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
356
357         /* If the last character is a path divider, then we need to
358          * trim it back so that the name look-up works properly.
359          * eg. /foo/new_dir/ -> /foo/newdir
360          * Curveball: Need to handle multiple path dividers:
361          * eg. /foof/sdfse///// -> /foo/sdfse
362          */
363         if(path_length > 0 && 
364                 yaffsfs_IsPathDivider(path[path_length-1])){
365                 alt_path = YMALLOC(path_length + 1);
366                 if(!alt_path)
367                         return -1;
368                 strcpy(alt_path, path);
369                 for(i = path_length-1;
370                         i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
371                         i--)
372                         alt_path[i] = (YCHAR) 0;
373         }
374         *ret_path = alt_path;
375         return 0;
376 }
377
378
379 LIST_HEAD(yaffsfs_deviceList);
380
381 /*
382  * yaffsfs_FindDevice
383  * yaffsfs_FindRoot
384  * Scan the configuration list to find the device
385  * Curveballs: Should match paths that end in '/' too
386  * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
387  */
388 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
389 {
390         struct list_head *cfg;
391         const YCHAR *leftOver;
392         const YCHAR *p;
393         struct yaffs_dev *retval = NULL;
394         struct yaffs_dev *dev = NULL;
395         int thisMatchLength;
396         int longestMatch = -1;
397         int matching;
398
399         /*
400          * Check all configs, choose the one that:
401          * 1) Actually matches a prefix (ie /a amd /abc will not match
402          * 2) Matches the longest.
403          */
404         list_for_each(cfg, &yaffsfs_deviceList){
405                 dev = list_entry(cfg, struct yaffs_dev, dev_list);
406                 leftOver = path;
407                 p = dev->param.name;
408                 thisMatchLength = 0;
409                 matching = 1;
410
411
412                 while(matching && *p && *leftOver){
413                         /* Skip over any /s */
414                         while(yaffsfs_IsPathDivider(*p))
415                               p++;
416
417                         /* Skip over any /s */
418                         while(yaffsfs_IsPathDivider(*leftOver))
419                               leftOver++;
420
421                         /* Now match the text part */
422                         while(matching &&
423                               *p && !yaffsfs_IsPathDivider(*p) &&
424                               *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
425                                 if(yaffsfs_Match(*p,*leftOver)){
426                                         p++;
427                                         leftOver++;
428                                         thisMatchLength++;
429                                 } else {
430                                         matching = 0;
431                                 }
432                         }
433                 }
434
435                 /* Skip over any /s in leftOver */
436                 while(yaffsfs_IsPathDivider(*leftOver))
437                       leftOver++;
438
439                 // Skip over any /s in p
440                 while(yaffsfs_IsPathDivider(*p))
441                       p++;
442
443                 // p should now be at the end of the string (ie. fully matched)
444                 if(*p)
445                         matching = 0;
446
447                 if( matching && (thisMatchLength > longestMatch))
448                 {
449                         // Matched prefix
450                         *restOfPath = (YCHAR *)leftOver;
451                         retval = dev;
452                         longestMatch = thisMatchLength;
453                 }
454
455         }
456         return retval;
457 }
458
459 static int yaffsfs_CheckPath(const YCHAR *path)
460 {
461         int n=0;
462         int divs=0;
463         while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
464                 if(yaffsfs_IsPathDivider(*path)){
465                         n=0;
466                         divs++;
467                 } else
468                         n++;
469                 path++;
470         }
471         
472         return (*path) ? -1 : 0;
473 }
474
475 /* FindMountPoint only returns a dev entry if the path is a mount point */
476 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
477 {
478         struct yaffs_dev *dev;
479         YCHAR *restOfPath=NULL;
480         dev = yaffsfs_FindDevice(path,&restOfPath);
481         if(dev && restOfPath && *restOfPath)
482                 dev = NULL;
483         return dev;
484 }
485
486 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
487 {
488
489         struct yaffs_dev *dev;
490
491         dev= yaffsfs_FindDevice(path,restOfPath);
492         if(dev && dev->is_mounted){
493                 return dev->root_dir;
494         }
495         return NULL;
496 }
497
498 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
499                                         int symDepth, int *loop)
500 {
501
502         if(obj)
503                 obj = yaffs_get_equivalent_obj(obj);
504
505         while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
506                 YCHAR *alias = obj->variant.symlink_variant.alias;
507
508                 if(yaffsfs_IsPathDivider(*alias))
509                         /* Starts with a /, need to scan from root up */
510                         obj = yaffsfs_FindObject(NULL,alias,symDepth++,
511                                                 1,NULL,NULL,loop);
512                 else
513                         /* Relative to here, so use the parent of the symlink as a start */
514                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
515                                                 1,NULL,NULL,loop);
516         }
517         return obj;
518 }
519
520
521 /*
522  * yaffsfs_FindDirectory
523  * Parse a path to determine the directory and the name within the directory.
524  *
525  * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
526  */
527 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
528                                 const YCHAR *path, YCHAR **name, int symDepth,
529                                 int *notDir,int *loop)
530 {
531         struct yaffs_obj *dir;
532         YCHAR *restOfPath;
533         YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
534         int i;
535
536         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
537                 if(loop)
538                         *loop = 1;
539                 return NULL;
540         }
541
542         if(startDir){
543                 dir = startDir;
544                 restOfPath = (YCHAR *)path;
545         }
546         else
547                 dir = yaffsfs_FindRoot(path,&restOfPath);
548
549
550         while(dir){
551                 /*
552                  * parse off /.
553                  * curve ball: also throw away surplus '/'
554                  * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
555                  */
556                 while(yaffsfs_IsPathDivider(*restOfPath))
557                         restOfPath++; /* get rid of '/' */
558
559                 *name = restOfPath;
560                 i = 0;
561
562                 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
563                         if (i < YAFFS_MAX_NAME_LENGTH){
564                                 str[i] = *restOfPath;
565                                 str[i+1] = '\0';
566                                 i++;
567                         }
568                         restOfPath++;
569                 }
570
571                 if(!*restOfPath)
572                         /* got to the end of the string */
573                         return dir;
574                 else{
575                         if(yaffs_strcmp(str,_Y(".")) == 0)
576                         {
577                                 /* Do nothing */
578                         }
579                         else if(yaffs_strcmp(str,_Y("..")) == 0)
580                                 dir = dir->parent;
581                         else{
582                                 dir = yaffs_find_by_name(dir,str);
583
584                                 dir = yaffsfs_FollowLink(dir,symDepth,loop);
585
586                                 if(dir && dir->variant_type != 
587                                         YAFFS_OBJECT_TYPE_DIRECTORY){
588                                         if(notDir)
589                                                 *notDir = 1;
590                                         dir = NULL;
591                                 }
592                                 
593                         }
594                 }
595         }
596         /* directory did not exist. */
597         return NULL;
598 }
599
600 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
601                                         const YCHAR *path,
602                                         YCHAR **name,
603                                         int symDepth,
604                                         int *notDir,
605                                         int *loop)
606 {
607         return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
608 }
609
610 /*
611  * yaffsfs_FindObject turns a path for an existing object into the object
612  */
613 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
614                         const YCHAR *path,int symDepth, int getEquiv,
615                         struct yaffs_obj **dirOut, int *notDir,int *loop)
616 {
617         struct yaffs_obj *dir;
618         struct yaffs_obj *obj;
619         YCHAR *name;
620
621         dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
622
623         if(dirOut)
624                 *dirOut =  dir;
625
626         if(dir && *name)
627                 obj = yaffs_find_by_name(dir,name);
628         else
629                 obj = dir;
630
631         if(getEquiv)
632                 obj = yaffs_get_equivalent_obj(obj);
633
634         return obj;
635 }
636
637
638 /*************************************************************************
639  *      Start of yaffsfs visible functions. 
640  *************************************************************************/
641
642 int yaffs_dup(int fd)
643 {
644         int newHandle = -1;
645         yaffsfs_Handle *oldPtr = NULL;
646         yaffsfs_Handle *newPtr = NULL;
647
648         yaffsfs_Lock();
649
650         oldPtr = yaffsfs_GetHandlePointer(fd);
651         if(oldPtr && oldPtr->useCount > 0)
652                 newHandle = yaffsfs_GetNewHandle();
653         if(newHandle >= 0)
654                 newPtr = yaffsfs_GetHandlePointer(newHandle);
655
656         if(newPtr)
657                 *newPtr = *oldPtr;
658
659         yaffsfs_Unlock();
660
661         if(!oldPtr)
662                 yaffsfs_SetError(-EBADF);
663         else if (!newPtr)
664                 yaffsfs_SetError(-ENOMEM);
665
666         return newHandle;
667
668 }
669
670
671
672 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
673 {
674         struct yaffs_obj *obj = NULL;
675         struct yaffs_obj *dir = NULL;
676         YCHAR *name;
677         int handle = -1;
678         yaffsfs_Handle *yh = NULL;
679         int openDenied = 0;
680         int symDepth = 0;
681         int errorReported = 0;
682         int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
683         u8 shareRead = (sharing & YAFFS_SHARE_READ) ?  1 : 0;
684         u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
685         u8 sharedReadAllowed;
686         u8 sharedWriteAllowed;
687         u8 alreadyReading;
688         u8 alreadyWriting;
689         u8 readRequested;
690         u8 writeRequested;
691         int notDir = 0;
692         int loop = 0;
693
694         if(!path) {
695                 yaffsfs_SetError(-EFAULT);
696                 return -1;
697         }
698
699         if(yaffsfs_CheckPath(path) < 0){
700                 yaffsfs_SetError(-ENAMETOOLONG);
701                 return -1;
702         }
703
704         /* O_EXCL only has meaning if O_CREAT is specified */
705         if(!(oflag & O_CREAT))
706                 oflag &= ~(O_EXCL);
707
708         /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
709         if( (oflag & O_CREAT) & (oflag & O_EXCL))
710                 oflag &= ~(O_TRUNC);
711
712         /* Todo: Are there any more flag combos to sanitise ? */
713
714         /* Figure out if reading or writing is requested */
715
716         readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
717         writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
718
719         yaffsfs_Lock();
720
721         handle = yaffsfs_GetNewHandle();
722
723         if(handle < 0){
724                 yaffsfs_SetError(-ENFILE);
725                 errorReported = 1;
726         } else {
727
728                 yh = yaffsfs_GetHandlePointer(handle);
729
730                 /* try to find the exisiting object */
731                 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
732
733                 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
734
735                 if(obj &&
736                         obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
737                         obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
738                         obj = NULL;
739
740
741                 if(obj){
742
743                         /* The file already exists or it might be a directory */
744
745                         /* If it is a directory then we can't open it as a file */
746                         if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
747                                 openDenied = 1;
748                                 yaffsfs_SetError(-EISDIR);
749                                 errorReported = 1;
750                         }
751
752                         /* Open should fail if O_CREAT and O_EXCL are specified since
753                          * the file exists
754                          */
755                         if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
756                                 openDenied = 1;
757                                 yaffsfs_SetError(-EEXIST);
758                                 errorReported = 1;
759                         }
760
761                         /* Check file permissions */
762                         if( readRequested && !(obj->yst_mode & S_IREAD))
763                                 openDenied = 1;
764
765                         if( writeRequested && !(obj->yst_mode & S_IWRITE))
766                                 openDenied = 1;
767
768                         if( !errorReported && writeRequested && 
769                                 obj->my_dev->read_only){
770                                 openDenied = 1;
771                                 yaffsfs_SetError(-EROFS);
772                                 errorReported = 1;
773                         }
774
775                         if(openDenied && !errorReported ) {
776                                 /* Error if the file exists but permissions are refused. */
777                                 yaffsfs_SetError(-EACCES);
778                                 errorReported = 1;
779                         }
780
781                         /* Check sharing of an existing object. */
782                         if(!openDenied){
783                                 yaffsfs_Handle *hx;
784                                 int i;
785                                 sharedReadAllowed = 1;
786                                 sharedWriteAllowed = 1;
787                                 alreadyReading = 0;
788                                 alreadyWriting = 0;
789                                 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
790                                         hx = &yaffsfs_handle[i];
791                                         if(hx->useCount > 0 &&
792                                                 hx->inodeId >= 0 &&
793                                                 yaffsfs_inode[hx->inodeId].iObj == obj){
794                                                 if(!hx->shareRead)
795                                                         sharedReadAllowed = 0;
796                                                 if(!hx->shareWrite)
797                                                         sharedWriteAllowed = 0;
798                                                 if(hx->reading)
799                                                         alreadyReading = 1;
800                                                 if(hx->writing)
801                                                         alreadyWriting = 1;
802                                         }
803                                 }
804
805
806
807                                 if((!sharedReadAllowed && readRequested)|| 
808                                         (!shareRead  && alreadyReading) ||
809                                         (!sharedWriteAllowed && writeRequested) ||
810                                         (!shareWrite && alreadyWriting)){
811                                         openDenied = 1;
812                                         yaffsfs_SetError(-EBUSY);
813                                         errorReported=1;
814                                 }
815                         }
816
817                 }
818
819                 /* If we could not open an existing object, then let's see if
820                  * the directory exists. If not, error.
821                  */
822                 if(!obj && !errorReported){
823                         dir = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
824                         if(!dir && notDir){
825                                 yaffsfs_SetError(-ENOTDIR);
826                                 errorReported = 1;
827                         } else if(loop){
828                                 yaffsfs_SetError(-ELOOP);
829                                 errorReported = 1;
830                         } else  if(!dir){
831                                 yaffsfs_SetError(-ENOENT);
832                                 errorReported = 1;
833                         }
834                 }
835
836                 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
837                         /* Let's see if we can create this file if it does not exist. */
838                         if(dir->my_dev->read_only){
839                                 yaffsfs_SetError(-EROFS);
840                                 errorReported = 1;
841                         } else
842                                 obj = yaffs_create_file(dir,name,mode,0,0);
843
844                         if(!obj && !errorReported){
845                                 yaffsfs_SetError(-ENOSPC);
846                                 errorReported = 1;
847                         }
848                 }
849
850                 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
851                         /* Error if the file does not exist and CREAT is not set. */
852                         yaffsfs_SetError(-ENOENT);
853                         errorReported = 1;
854                 }
855
856                 if(obj && !openDenied) {
857                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
858
859                         if(inodeId<0) {
860                                 /*
861                                  * Todo: Fix any problem if inodes run out, though that
862                                  * can't happen if the number of inode items >= number of handles. 
863                                  */
864                         }
865                         
866                         yh->inodeId = inodeId;
867                         yh->reading = readRequested;
868                         yh->writing = writeRequested;
869                         yh->append =  (oflag & O_APPEND) ? 1 : 0;
870                         yh->position = 0;
871                         yh->shareRead = shareRead;
872                         yh->shareWrite = shareWrite;
873
874                         /* Hook inode to object */
875                         obj->my_inode = (void*) &yaffsfs_inode[inodeId];
876
877                         if((oflag & O_TRUNC) && yh->writing)
878                                 yaffs_resize_file(obj,0);
879                 } else {
880                         yaffsfs_PutHandle(handle);
881                         if(!errorReported) 
882                                 yaffsfs_SetError(0); /* Problem */
883                         handle = -1;
884                 }
885         }
886
887         yaffsfs_Unlock();
888
889         return handle;
890 }
891
892 int yaffs_open(const YCHAR *path, int oflag, int mode)
893 {
894         return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
895 }
896
897 int yaffs_Dofsync(int fd,int datasync)
898 {
899         yaffsfs_Handle *h = NULL;
900         int retVal = -1;
901
902         yaffsfs_Lock();
903
904         h = yaffsfs_GetHandlePointer(fd);
905
906         if(!h || h->useCount < 1)
907                 yaffsfs_SetError(-EBADF);
908         else {
909                 struct yaffs_obj * obj;
910                 obj = yaffsfs_inode[h->inodeId].iObj;
911                 if(obj->my_dev->read_only)
912                         yaffsfs_SetError(-EROFS);
913                 else {          
914                         yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
915                         retVal = 0;
916                 }
917         }
918
919         yaffsfs_Unlock();
920
921         return retVal;
922 }
923
924 int yaffs_fsync(int fd)
925 {
926         return yaffs_Dofsync(fd,0);
927 }
928
929 int yaffs_flush(int fd)
930 {
931         return yaffs_fsync(fd);
932 }
933
934 int yaffs_fdatasync(int fd)
935 {
936         return yaffs_Dofsync(fd,1);
937 }
938
939 int yaffs_close(int fd)
940 {
941         yaffsfs_Handle *h = NULL;
942         int retVal = 0;
943
944         yaffsfs_Lock();
945
946         h = yaffsfs_GetHandlePointer(fd);
947
948         if(h && h->useCount > 0) {
949                 /* clean up */
950                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
951                 yaffsfs_PutHandle(fd);
952                 retVal = 0;
953         } else {
954                 /* bad handle */
955                 yaffsfs_SetError(-EBADF);
956                 retVal = -1;
957         }
958
959         yaffsfs_Unlock();
960
961         return retVal;
962 }
963
964
965
966 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
967 {
968         yaffsfs_Handle *h = NULL;
969         struct yaffs_obj *obj = NULL;
970         int pos = 0;
971         int startPos = 0;
972         int endPos = 0;
973         int nRead = 0;
974         int nToRead = 0;
975         int totalRead = 0;
976         unsigned int maxRead;
977         u8 *buf = (u8 *)vbuf;
978
979         if(!vbuf){
980                 yaffsfs_SetError(-EFAULT);
981                 return -1;
982         }
983
984         yaffsfs_Lock();
985         h = yaffsfs_GetHandlePointer(fd);
986         obj = yaffsfs_GetHandleObject(fd);
987
988         if(!h || !obj){
989                 /* bad handle */
990                 yaffsfs_SetError(-EBADF);
991                 totalRead = -1;
992         } else if(!h->reading){
993                 /* Not a reading handle */
994                 yaffsfs_SetError(-EINVAL);
995                 totalRead = -1;
996         } else if(nbyte > YAFFS_MAX_FILE_SIZE){
997                 yaffsfs_SetError(-EINVAL);
998                 totalRead = -1;
999         } else {
1000                 if(isPread)
1001                         startPos = offset;
1002                 else
1003                         startPos = h->position;
1004
1005                 pos = startPos;
1006                                         
1007                 if(yaffs_get_obj_length(obj) > pos)
1008                         maxRead = yaffs_get_obj_length(obj) - pos;
1009                 else
1010                         maxRead = 0;
1011
1012                 if(nbyte > maxRead)
1013                         nbyte = maxRead;
1014
1015
1016                 yaffsfs_GetHandle(fd);
1017
1018                 endPos = pos + nbyte;
1019
1020                 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1021                         nbyte > YAFFS_MAX_FILE_SIZE ||
1022                         endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1023                         totalRead = -1;
1024                         nbyte = 0;
1025                 }
1026
1027                 while(nbyte > 0) {
1028                         nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1029                         if(nToRead > nbyte)
1030                                 nToRead = nbyte;
1031
1032                         /* Tricky bit... 
1033                          * Need to reverify object in case the device was
1034                          * unmounted in another thread.
1035                          */
1036                         obj = yaffsfs_GetHandleObject(fd);
1037                         if(!obj)
1038                                 nRead = 0;
1039                         else
1040                                 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1041
1042                         if(nRead > 0){
1043                                 totalRead += nRead;
1044                                 pos += nRead;
1045                                 buf += nRead;
1046                         }
1047
1048                         if(nRead == nToRead)
1049                                 nbyte-=nRead;
1050                         else
1051                                 nbyte = 0; /* no more to read */
1052                                         
1053                                         
1054                         if(nbyte > 0){
1055                                 yaffsfs_Unlock();
1056                                 yaffsfs_Lock();
1057                         }
1058
1059                 }
1060
1061                 yaffsfs_PutHandle(fd);
1062
1063                 if(!isPread) {
1064                         if(totalRead >= 0)
1065                                 h->position = startPos + totalRead;
1066                         else
1067                                 yaffsfs_SetError(-EINVAL);
1068                 }
1069
1070         }
1071
1072         yaffsfs_Unlock();
1073
1074         return (totalRead >= 0) ? totalRead : -1;
1075
1076 }
1077
1078 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1079 {
1080         return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1081 }
1082
1083 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1084 {
1085         return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1086 }
1087
1088 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1089 {
1090         yaffsfs_Handle *h = NULL;
1091         struct yaffs_obj *obj = NULL;
1092         int pos = 0;
1093         int startPos = 0;
1094         int endPos;
1095         int nWritten = 0;
1096         int totalWritten = 0;
1097         int write_trhrough = 0;
1098         int nToWrite = 0;
1099         const u8 *buf = (const u8 *)vbuf;
1100
1101         if(!vbuf){
1102                 yaffsfs_SetError(-EFAULT);
1103                 return -1;
1104         }
1105
1106         yaffsfs_Lock();
1107         h = yaffsfs_GetHandlePointer(fd);
1108         obj = yaffsfs_GetHandleObject(fd);
1109
1110         if(!h || !obj){
1111                 /* bad handle */
1112                 yaffsfs_SetError(-EBADF);
1113                 totalWritten = -1;
1114         } else if(!h->writing){
1115                 yaffsfs_SetError(-EINVAL);
1116                 totalWritten=-1;
1117         } else if(obj->my_dev->read_only){
1118                 yaffsfs_SetError(-EROFS);
1119                 totalWritten=-1;
1120         } else {
1121                 if(h->append)
1122                         startPos = yaffs_get_obj_length(obj);
1123                 else if(isPwrite)
1124                         startPos = offset;
1125                 else
1126                         startPos = h->position;
1127
1128                 yaffsfs_GetHandle(fd);
1129                 pos = startPos;
1130                 endPos = pos + nbyte;
1131
1132                 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1133                         nbyte > YAFFS_MAX_FILE_SIZE ||
1134                         endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1135                         totalWritten = -1;
1136                         nbyte = 0;
1137                 }
1138
1139                 while(nbyte > 0) {
1140
1141                         nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1142                         if(nToWrite > nbyte)
1143                                 nToWrite = nbyte;
1144
1145                         /* Tricky bit... 
1146                          * Need to reverify object in case the device was
1147                          * remounted or unmounted in another thread.
1148                          */
1149                         obj = yaffsfs_GetHandleObject(fd);
1150                         if(!obj || obj->my_dev->read_only)
1151                                 nWritten = 0;
1152                         else
1153                                 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1154                                                         write_trhrough);
1155                         if(nWritten > 0){
1156                                 totalWritten += nWritten;
1157                                 pos += nWritten;
1158                                 buf += nWritten;
1159                         }
1160
1161                         if(nWritten == nToWrite)
1162                                 nbyte -= nToWrite;
1163                         else
1164                                 nbyte = 0;
1165
1166                         if(nWritten < 1 && totalWritten < 1){
1167                                 yaffsfs_SetError(-ENOSPC);
1168                                 totalWritten = -1;
1169                         }
1170
1171                         if(nbyte > 0){
1172                                 yaffsfs_Unlock();
1173                                 yaffsfs_Lock();
1174                         }
1175                 }
1176
1177                 yaffsfs_PutHandle(fd);
1178
1179                 if(!isPwrite){
1180                         if(totalWritten > 0)
1181                                 h->position = startPos + totalWritten;
1182                         else
1183                                 yaffsfs_SetError(-EINVAL);
1184                 }
1185         }
1186
1187         yaffsfs_Unlock();
1188
1189         return (totalWritten >= 0) ? totalWritten : -1;
1190 }
1191
1192 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1193 {
1194         return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1195 }
1196
1197 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1198 {
1199         return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1200 }
1201
1202
1203 int yaffs_truncate(const YCHAR *path,off_t new_size)
1204 {
1205         struct yaffs_obj *obj = NULL;
1206         struct yaffs_obj *dir = NULL;
1207         int result = YAFFS_FAIL;
1208         int notDir = 0;
1209         int loop = 0;
1210
1211         if(!path){
1212                 yaffsfs_SetError(-EFAULT);
1213                 return -1;
1214         }
1215
1216         if(yaffsfs_CheckPath(path) < 0){
1217                 yaffsfs_SetError(-ENAMETOOLONG);
1218                 return -1;
1219         }
1220
1221         yaffsfs_Lock();
1222
1223         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1224         obj = yaffsfs_FollowLink(obj,0,&loop);
1225
1226         if(!dir && notDir)
1227                 yaffsfs_SetError(-ENOTDIR);
1228         else if(loop)
1229                 yaffsfs_SetError(-ELOOP);
1230         else if(!dir || !obj)
1231                 yaffsfs_SetError(-ENOENT);
1232         else if(obj->my_dev->read_only)
1233                 yaffsfs_SetError(-EROFS);
1234         else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1235                 yaffsfs_SetError(-EISDIR);
1236         else if(obj->my_dev->read_only)
1237                 yaffsfs_SetError(-EROFS);
1238         else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1239                 yaffsfs_SetError(-EINVAL);
1240         else
1241                 result = yaffs_resize_file(obj,new_size);
1242
1243         yaffsfs_Unlock();
1244
1245         return (result) ? 0 : -1;
1246 }
1247
1248 int yaffs_ftruncate(int fd, off_t new_size)
1249 {
1250         yaffsfs_Handle *h = NULL;
1251         struct yaffs_obj *obj = NULL;
1252         int result = 0;
1253
1254         yaffsfs_Lock();
1255         h = yaffsfs_GetHandlePointer(fd);
1256         obj = yaffsfs_GetHandleObject(fd);
1257
1258         if(!h || !obj)
1259                 /* bad handle */
1260                 yaffsfs_SetError(-EBADF);
1261         else if(!h->writing)
1262                 yaffsfs_SetError(-EINVAL);
1263         else if(obj->my_dev->read_only)
1264                 yaffsfs_SetError(-EROFS);
1265         else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1266                 yaffsfs_SetError(-EINVAL);
1267         else
1268                 /* resize the file */
1269                 result = yaffs_resize_file(obj,new_size);
1270         yaffsfs_Unlock();
1271
1272         return (result) ? 0 : -1;
1273
1274 }
1275
1276 off_t yaffs_lseek(int fd, off_t offset, int whence)
1277 {
1278         yaffsfs_Handle *h = NULL;
1279         struct yaffs_obj *obj = NULL;
1280         int pos = -1;
1281         int fSize = -1;
1282
1283         yaffsfs_Lock();
1284         h = yaffsfs_GetHandlePointer(fd);
1285         obj = yaffsfs_GetHandleObject(fd);
1286
1287         if(!h || !obj)
1288                 yaffsfs_SetError(-EBADF);
1289         else if(offset > YAFFS_MAX_FILE_SIZE)
1290                 yaffsfs_SetError(-EINVAL);
1291         else {
1292                 if(whence == SEEK_SET){
1293                         if(offset >= 0)
1294                                 pos = offset;
1295                 } else if(whence == SEEK_CUR) {
1296                         if( (h->position + offset) >= 0)
1297                                 pos = (h->position + offset);
1298                 } else if(whence == SEEK_END) {
1299                         fSize = yaffs_get_obj_length(obj);
1300                         if(fSize >= 0 && (fSize + offset) >= 0)
1301                                 pos = fSize + offset;
1302                 } 
1303
1304                 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1305                         h->position = pos;
1306                 else{
1307                         yaffsfs_SetError(-EINVAL);
1308                         pos = -1;
1309                 }
1310         }
1311
1312         yaffsfs_Unlock();
1313
1314         return pos;
1315 }
1316
1317
1318 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1319 {
1320         struct yaffs_obj *dir = NULL;
1321         struct yaffs_obj *obj = NULL;
1322         YCHAR *name;
1323         int result = YAFFS_FAIL;
1324         int notDir = 0;
1325         int loop = 0;
1326
1327         if(!path){
1328                 yaffsfs_SetError(-EFAULT);
1329                 return -1;
1330         }
1331
1332         if(yaffsfs_CheckPath(path) < 0){
1333                 yaffsfs_SetError(-ENAMETOOLONG);
1334                 return -1;
1335         }
1336
1337         yaffsfs_Lock();
1338
1339         obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1340         dir = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
1341
1342         if(!dir && notDir)
1343                 yaffsfs_SetError(-ENOTDIR);
1344         else if(loop)
1345                 yaffsfs_SetError(-ELOOP);
1346         else if(!dir)
1347                 yaffsfs_SetError(-ENOENT);
1348         else if(yaffs_strncmp(name,_Y("."),2) == 0)
1349                 yaffsfs_SetError(-EINVAL);
1350         else if(!obj)
1351                 yaffsfs_SetError(-ENOENT);
1352         else if(obj->my_dev->read_only)
1353                 yaffsfs_SetError(-EROFS);
1354         else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1355                 yaffsfs_SetError(-EISDIR);
1356         else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1357                 yaffsfs_SetError(-ENOTDIR);
1358         else if(isDirectory && obj == obj->my_dev->root_dir)
1359                 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1360         else {
1361                 result = yaffs_unlinker(dir,name);
1362
1363                 if(result == YAFFS_FAIL && isDirectory)
1364                         yaffsfs_SetError(-ENOTEMPTY);
1365         }
1366
1367         yaffsfs_Unlock();
1368
1369         return (result == YAFFS_FAIL) ? -1 : 0;
1370 }
1371
1372
1373 int yaffs_unlink(const YCHAR *path)
1374 {
1375         return yaffsfs_DoUnlink(path,0);
1376 }
1377
1378 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1379 {
1380         struct yaffs_obj *olddir = NULL;
1381         struct yaffs_obj *newdir = NULL;
1382         struct yaffs_obj *obj = NULL;
1383         struct yaffs_obj *newobj = NULL;
1384         YCHAR *oldname;
1385         YCHAR *newname;
1386         int result= YAFFS_FAIL;
1387         int rename_allowed = 1;
1388         int notOldDir = 0;
1389         int notNewDir = 0;
1390         int oldLoop = 0;
1391         int newLoop = 0;
1392
1393         YCHAR *alt_newpath=NULL;
1394
1395         if(!oldPath || !newPath){
1396                 yaffsfs_SetError(-EFAULT);
1397                 return -1;
1398         }
1399
1400         if(yaffsfs_CheckPath(oldPath) < 0 ||
1401                 yaffsfs_CheckPath(newPath) < 0){
1402                 yaffsfs_SetError(-ENAMETOOLONG);
1403                 return -1;
1404         }
1405
1406         if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1407                 yaffsfs_SetError(-ENOMEM);
1408                 return -1;
1409         }
1410         if(alt_newpath)
1411                 newPath = alt_newpath;
1412
1413         yaffsfs_Lock();
1414
1415
1416         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,&notOldDir,&oldLoop);
1417         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,&notNewDir,&newLoop);
1418         obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1419         newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1420
1421         /* If the object being renamed is a directory and the 
1422          * path ended with a "/" then the olddir == obj.
1423          * We pass through NULL for the old name to tell the lower layers
1424          * to use olddir as the object.
1425          */
1426
1427         if(olddir == obj)
1428                 oldname = NULL;
1429
1430         if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1431                 yaffsfs_SetError(-ENOTDIR);
1432                 rename_allowed = 0;
1433         } else if(oldLoop || newLoop) {
1434                 yaffsfs_SetError(-ELOOP);
1435                 rename_allowed = 0;
1436         } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1437                 yaffsfs_SetError(-EINVAL);
1438                 rename_allowed = 0;
1439         }else if(!olddir || !newdir || !obj) {
1440                 yaffsfs_SetError(-ENOENT);
1441                 rename_allowed = 0;
1442         } else if(obj->my_dev->read_only){
1443                 yaffsfs_SetError(-EROFS);
1444                 rename_allowed = 0;
1445         } else if(yaffs_is_non_empty_dir(newobj)){
1446                 yaffsfs_SetError(-ENOTEMPTY);
1447                 rename_allowed = 0;
1448         } else if(olddir->my_dev != newdir->my_dev) {
1449                 /* Rename must be on same device */
1450                 yaffsfs_SetError(-EXDEV);
1451                 rename_allowed = 0;
1452         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1453                 /*
1454                  * It is a directory, check that it is not being renamed to
1455                  * being its own decendent.
1456                  * Do this by tracing from the new directory back to the root, 
1457                  * checking for obj
1458                  */
1459
1460                 struct yaffs_obj *xx = newdir;
1461
1462                 while( rename_allowed && xx){
1463                         if(xx == obj)
1464                                 rename_allowed = 0;
1465                         xx = xx->parent;
1466                 }
1467                 if(!rename_allowed)
1468                         yaffsfs_SetError(-EINVAL);
1469         }
1470
1471         if(rename_allowed)
1472                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1473
1474         yaffsfs_Unlock();
1475
1476         if(alt_newpath)
1477                 YFREE(alt_newpath);
1478
1479         return (result == YAFFS_FAIL) ? -1 : 0;
1480 }
1481
1482
1483 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1484 {
1485         int retVal = -1;
1486
1487         obj = yaffs_get_equivalent_obj(obj);
1488
1489         if(obj && buf){
1490                 buf->st_dev = (int)obj->my_dev->os_context;
1491                 buf->st_ino = obj->obj_id;
1492                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1493
1494                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1495                         buf->st_mode |= S_IFDIR;
1496                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1497                         buf->st_mode |= S_IFLNK;
1498                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1499                         buf->st_mode |= S_IFREG;
1500
1501                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1502                 buf->st_uid = 0;
1503                 buf->st_gid = 0;;
1504                 buf->st_rdev = obj->yst_rdev;
1505                 buf->st_size = yaffs_get_obj_length(obj);
1506                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1507                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1508 #if CONFIG_YAFFS_WINCE
1509                 buf->yst_wince_atime[0] = obj->win_atime[0];
1510                 buf->yst_wince_atime[1] = obj->win_atime[1];
1511                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1512                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1513                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1514                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1515 #else
1516                 buf->yst_atime = obj->yst_atime;
1517                 buf->yst_ctime = obj->yst_ctime;
1518                 buf->yst_mtime = obj->yst_mtime;
1519 #endif
1520                 retVal = 0;
1521         }
1522         return retVal;
1523 }
1524
1525 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1526 {
1527         struct yaffs_obj *obj=NULL;
1528         struct yaffs_obj *dir=NULL;
1529         int retVal = -1;
1530         int notDir = 0;
1531         int loop = 0;
1532
1533         if(!path || !buf){
1534                 yaffsfs_SetError(-EFAULT);
1535                 return -1;
1536         }
1537
1538         if(yaffsfs_CheckPath(path) < 0){
1539                 yaffsfs_SetError(-ENAMETOOLONG);
1540                 return -1;
1541         }
1542
1543         yaffsfs_Lock();
1544
1545         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1546
1547         if(!doLStat && obj)
1548                 obj = yaffsfs_FollowLink(obj,0,&loop);
1549
1550         if(!dir && notDir)
1551                 yaffsfs_SetError(-ENOTDIR);
1552         else if(loop)
1553                 yaffsfs_SetError(-ELOOP);
1554         else if(!dir || !obj)
1555                 yaffsfs_SetError(-ENOENT);
1556         else
1557                 retVal = yaffsfs_DoStat(obj,buf);
1558
1559         yaffsfs_Unlock();
1560
1561         return retVal;
1562
1563 }
1564
1565 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1566 {
1567         return yaffsfs_DoStatOrLStat(path,buf,0);
1568 }
1569
1570 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1571 {
1572         return yaffsfs_DoStatOrLStat(path,buf,1);
1573 }
1574
1575 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1576 {
1577         struct yaffs_obj *obj;
1578
1579         int retVal = -1;
1580
1581         if(!buf){
1582                 yaffsfs_SetError(-EFAULT);
1583                 return -1;
1584         }
1585
1586         yaffsfs_Lock();
1587         obj = yaffsfs_GetHandleObject(fd);
1588
1589         if(obj)
1590                 retVal = yaffsfs_DoStat(obj,buf);
1591         else
1592                 /* bad handle */
1593                 yaffsfs_SetError(-EBADF);
1594
1595         yaffsfs_Unlock();
1596
1597         return retVal;
1598 }
1599
1600 #ifndef CONFIG_YAFFS_WINCE
1601 /* xattrib functions */
1602
1603
1604 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1605                         const void *data, int size, int flags, int follow)
1606 {
1607         struct yaffs_obj *obj;
1608         struct yaffs_obj *dir;
1609         int notDir = 0;
1610         int loop = 0;
1611
1612         int retVal = -1;
1613
1614         if(!path || !name || !data){
1615                 yaffsfs_SetError(-EFAULT);
1616                 return -1;
1617         }
1618
1619         if(yaffsfs_CheckPath(path) < 0){
1620                 yaffsfs_SetError(-ENAMETOOLONG);
1621                 return -1;
1622         }
1623
1624         yaffsfs_Lock();
1625
1626         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1627
1628         if(follow)
1629                 obj = yaffsfs_FollowLink(obj,0,&loop);
1630
1631         if(!dir && notDir) 
1632                 yaffsfs_SetError(-ENOTDIR);
1633         else if(loop) 
1634                 yaffsfs_SetError(-ELOOP);
1635         else if(!dir || !obj) 
1636                 yaffsfs_SetError(-ENOENT);
1637         else {
1638                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1639                 if(retVal< 0){
1640                         yaffsfs_SetError(retVal);
1641                         retVal = -1;
1642                 }
1643         }
1644
1645         yaffsfs_Unlock();
1646
1647         return retVal;
1648
1649 }
1650
1651 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1652 {
1653         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1654 }
1655
1656 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1657 {
1658         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1659 }
1660
1661
1662
1663 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1664 {
1665         struct yaffs_obj *obj;
1666
1667         int retVal = -1;
1668
1669         if(!name || !data){
1670                 yaffsfs_SetError(-EFAULT);
1671                 return -1;
1672         }
1673
1674         yaffsfs_Lock();
1675         obj = yaffsfs_GetHandleObject(fd);
1676
1677         if(!obj) 
1678                 yaffsfs_SetError(-EBADF);
1679         else {
1680                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1681                 if(retVal< 0){
1682                         yaffsfs_SetError(retVal);
1683                         retVal = -1;
1684                 }
1685         }
1686
1687         yaffsfs_Unlock();
1688
1689         return retVal;
1690 }
1691
1692 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1693 {
1694         struct yaffs_obj *obj;
1695         struct yaffs_obj *dir;
1696         int retVal = -1;
1697         int notDir = 0;
1698         int loop = 0;
1699
1700         if(!path || !name || !data ){
1701                 yaffsfs_SetError(-EFAULT);
1702                 return -1;
1703         }
1704
1705         if(yaffsfs_CheckPath(path) < 0){
1706                 yaffsfs_SetError(-ENAMETOOLONG);
1707                 return -1;
1708         }
1709
1710         yaffsfs_Lock();
1711
1712         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1713
1714         if(follow)
1715                 obj = yaffsfs_FollowLink(obj,0,&loop);
1716
1717         if(!dir && notDir) 
1718                 yaffsfs_SetError(-ENOTDIR);
1719         else if(loop) 
1720                 yaffsfs_SetError(-ELOOP);
1721         else if(!dir || !obj) 
1722                 yaffsfs_SetError(-ENOENT);
1723         else {
1724                 retVal = yaffs_get_xattrib(obj,name,data,size);
1725                 if(retVal< 0){
1726                         yaffsfs_SetError(retVal);
1727                         retVal = -1;
1728                 }
1729         }
1730         yaffsfs_Unlock();
1731
1732         return retVal;
1733
1734 }
1735
1736 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1737 {
1738         return yaffs_do_getxattr( path, name, data, size, 1);
1739 }
1740 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1741 {
1742         return yaffs_do_getxattr( path, name, data, size, 0);
1743 }
1744
1745
1746
1747 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1748 {
1749         struct yaffs_obj *obj;
1750
1751         int retVal = -1;
1752
1753         if(!name || !data ){
1754                 yaffsfs_SetError(-EFAULT);
1755                 return -1;
1756         }
1757
1758         yaffsfs_Lock();
1759         obj = yaffsfs_GetHandleObject(fd);
1760
1761         if(obj) {
1762                 retVal = yaffs_get_xattrib(obj,name,data,size);
1763                 if(retVal< 0){
1764                         yaffsfs_SetError(retVal);
1765                         retVal = -1;
1766                 }
1767         } else
1768                 /* bad handle */
1769                 yaffsfs_SetError(-EBADF);
1770
1771         yaffsfs_Unlock();
1772
1773         return retVal;
1774 }
1775
1776 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1777 {
1778         struct yaffs_obj *obj=NULL;
1779         struct yaffs_obj *dir=NULL;
1780         int retVal = -1;
1781         int notDir = 0;
1782         int loop = 0;
1783
1784         if(!path || !data ){
1785                 yaffsfs_SetError(-EFAULT);
1786                 return -1;
1787         }
1788
1789         if(yaffsfs_CheckPath(path) < 0){
1790                 yaffsfs_SetError(-ENAMETOOLONG);
1791                 return -1;
1792         }
1793
1794         yaffsfs_Lock();
1795
1796         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1797
1798         if(follow)
1799                 obj = yaffsfs_FollowLink(obj,0,&loop);
1800
1801         if(!dir && notDir) 
1802                 yaffsfs_SetError(-ENOTDIR);
1803         else if(loop) 
1804                 yaffsfs_SetError(-ELOOP);
1805         else if(!dir || !obj) 
1806                 yaffsfs_SetError(-ENOENT);
1807         else {
1808                 retVal = yaffs_list_xattrib(obj, data,size);
1809                 if(retVal< 0){
1810                         yaffsfs_SetError(retVal);
1811                         retVal = -1;
1812                 }
1813         }
1814
1815         yaffsfs_Unlock();
1816
1817         return retVal;
1818
1819 }
1820
1821 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1822 {
1823         return yaffs_do_listxattr(path, data, size, 1);
1824 }
1825
1826 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1827 {
1828         return yaffs_do_listxattr(path, data, size, 0);
1829 }
1830
1831 int yaffs_flistxattr(int fd, char *data, int size)
1832 {
1833         struct yaffs_obj *obj;
1834
1835         int retVal = -1;
1836
1837         if(!data ){
1838                 yaffsfs_SetError(-EFAULT);
1839                 return -1;
1840         }
1841
1842         yaffsfs_Lock();
1843         obj = yaffsfs_GetHandleObject(fd);
1844
1845         if(obj) {
1846                 retVal = yaffs_list_xattrib(obj,data,size);
1847                 if(retVal< 0){
1848                         yaffsfs_SetError(retVal);
1849                         retVal = -1;
1850                 }
1851         } else
1852                 /* bad handle */
1853                 yaffsfs_SetError(-EBADF);
1854
1855         yaffsfs_Unlock();
1856
1857         return retVal;
1858 }
1859
1860 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1861 {
1862         struct yaffs_obj *obj=NULL;
1863         struct yaffs_obj *dir=NULL;
1864         int notDir = 0;
1865         int loop = 0;
1866         int retVal = -1;
1867
1868         if(!path || !name){
1869                 yaffsfs_SetError(-EFAULT);
1870                 return -1;
1871         }
1872
1873         if(yaffsfs_CheckPath(path) < 0){
1874                 yaffsfs_SetError(-ENAMETOOLONG);
1875                 return -1;
1876         }
1877
1878         yaffsfs_Lock();
1879
1880         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
1881
1882         if(follow)
1883                 obj = yaffsfs_FollowLink(obj,0,&loop);
1884
1885         if(!dir && notDir) 
1886                 yaffsfs_SetError(-ENOTDIR);
1887         else if(loop) 
1888                 yaffsfs_SetError(-ELOOP);
1889         else if(!dir || !obj) 
1890                 yaffsfs_SetError(-ENOENT);
1891         else {
1892                 retVal = yaffs_remove_xattrib(obj,name);
1893                 if(retVal< 0){
1894                         yaffsfs_SetError(retVal);
1895                         retVal = -1;
1896                 }
1897         }
1898
1899         yaffsfs_Unlock();
1900
1901         return retVal;
1902
1903 }
1904
1905 int yaffs_removexattr(const YCHAR *path, const char *name)
1906 {
1907         return yaffs_do_removexattr(path, name, 1);
1908 }
1909
1910 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1911 {
1912         return yaffs_do_removexattr(path, name, 0);
1913 }
1914
1915 int yaffs_fremovexattr(int fd, const char *name)
1916 {
1917         struct yaffs_obj *obj;
1918
1919         int retVal = -1;
1920
1921         if(!name){
1922                 yaffsfs_SetError(-EFAULT);
1923                 return -1;
1924         }
1925
1926         yaffsfs_Lock();
1927         obj = yaffsfs_GetHandleObject(fd);
1928
1929         if(obj){
1930                 retVal = yaffs_remove_xattrib(obj,name);
1931                 if(retVal< 0){
1932                         yaffsfs_SetError(retVal);
1933                         retVal = -1;
1934                 }
1935         }else
1936                 /* bad handle */
1937                 yaffsfs_SetError(-EBADF);
1938
1939         yaffsfs_Unlock();
1940
1941         return retVal;
1942 }
1943 #endif
1944
1945 #ifdef CONFIG_YAFFS_WINCE
1946 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1947 {
1948         struct yaffs_obj *obj;
1949
1950         int retVal = -1;
1951
1952         yaffsfs_Lock();
1953         obj = yaffsfs_GetHandleObject(fd);
1954
1955         if(obj){
1956
1957                 if(wctime){
1958                         wctime[0] = obj->win_ctime[0];
1959                         wctime[1] = obj->win_ctime[1];
1960                 }
1961                 if(watime){
1962                         watime[0] = obj->win_atime[0];
1963                         watime[1] = obj->win_atime[1];
1964                 }
1965                 if(wmtime){
1966                         wmtime[0] = obj->win_mtime[0];
1967                         wmtime[1] = obj->win_mtime[1];
1968                 }
1969
1970
1971                 retVal = 0;
1972         } else
1973                 /*  bad handle */
1974                 yaffsfs_SetError(-EBADF);               
1975         
1976         yaffsfs_Unlock();
1977         
1978         return retVal;
1979 }
1980
1981
1982 int yaffs_set_wince_times(int fd, 
1983                                                   const unsigned *wctime, 
1984                                                   const unsigned *watime, 
1985                                                   const unsigned *wmtime)
1986 {
1987         struct yaffs_obj *obj;
1988         int result;
1989         int retVal = -1;
1990
1991         yaffsfs_Lock();
1992         obj = yaffsfs_GetHandleObject(fd);
1993
1994         if(obj){
1995
1996                 if(wctime){
1997                         obj->win_ctime[0] = wctime[0];
1998                         obj->win_ctime[1] = wctime[1];
1999                 }
2000                 if(watime){
2001                         obj->win_atime[0] = watime[0];
2002                         obj->win_atime[1] = watime[1];
2003                 }
2004                 if(wmtime){
2005                         obj->win_mtime[0] = wmtime[0];
2006                         obj->win_mtime[1] = wmtime[1];
2007                 }
2008
2009                 obj->dirty = 1;
2010                 result = yaffs_flush_file(obj,0,0);
2011                 retVal = 0;
2012         } else
2013                 /* bad handle */
2014                 yaffsfs_SetError(-EBADF);
2015
2016         yaffsfs_Unlock();
2017
2018         return retVal;
2019 }
2020
2021 #endif
2022
2023
2024 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
2025 {
2026         int result = -1;
2027
2028         if(obj)
2029                 obj = yaffs_get_equivalent_obj(obj);
2030
2031         if(obj) {
2032                 obj->yst_mode = mode;
2033                 obj->dirty = 1;
2034                 result = yaffs_flush_file(obj,0,0);
2035         }
2036
2037         return result == YAFFS_OK ? 0 : -1;
2038 }
2039
2040
2041 int yaffs_access(const YCHAR *path, int amode)
2042 {
2043         struct yaffs_obj *obj=NULL;
2044         struct yaffs_obj *dir=NULL;
2045         int notDir = 0;
2046         int loop = 0;
2047         int retval = -1;
2048
2049         if(!path){
2050                 yaffsfs_SetError(-EFAULT);
2051                 return -1;
2052         }
2053
2054         if(yaffsfs_CheckPath(path) < 0){
2055                 yaffsfs_SetError(-ENAMETOOLONG);
2056                 return -1;
2057         }
2058
2059         if(amode & ~(R_OK | W_OK | X_OK)){
2060                 yaffsfs_SetError(-EINVAL);
2061                 return -1;
2062         }
2063
2064         yaffsfs_Lock();
2065
2066         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
2067         obj = yaffsfs_FollowLink(obj,0,&loop);
2068
2069         if(!dir && notDir) 
2070                 yaffsfs_SetError(-ENOTDIR);
2071         else if(loop) 
2072                 yaffsfs_SetError(-ELOOP);
2073         else if(!dir || !obj) 
2074                 yaffsfs_SetError(-ENOENT);
2075         else if((amode & W_OK) && obj->my_dev->read_only)
2076                 yaffsfs_SetError(-EROFS);
2077         else{
2078                 int access_ok = 1;
2079
2080                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2081                         access_ok = 0;
2082                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2083                         access_ok = 0;
2084                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2085                         access_ok = 0;
2086
2087                 if(!access_ok)
2088                         yaffsfs_SetError(-EACCES);
2089                 else
2090                         retval = 0;
2091         }
2092
2093         yaffsfs_Unlock();
2094
2095         return retval;
2096
2097 }
2098
2099
2100 int yaffs_chmod(const YCHAR *path, mode_t mode)
2101 {
2102         struct yaffs_obj *obj=NULL;
2103         struct yaffs_obj *dir=NULL;
2104         int retVal = -1;
2105         int notDir = 0;
2106         int loop = 0;
2107
2108         if(!path){
2109                 yaffsfs_SetError(-EFAULT);
2110                 return -1;
2111         }
2112
2113         if(yaffsfs_CheckPath(path) < 0){
2114                 yaffsfs_SetError(-ENAMETOOLONG);
2115                 return -1;
2116         }
2117
2118         if(mode & ~(0777)){
2119                 yaffsfs_SetError(-EINVAL);
2120                 return -1;
2121         }
2122
2123         yaffsfs_Lock();
2124
2125         obj = yaffsfs_FindObject(NULL,path,0,1, &dir, &notDir,&loop);
2126         obj = yaffsfs_FollowLink(obj,0,&loop);
2127
2128         if(!dir && notDir) 
2129                 yaffsfs_SetError(-ENOTDIR);
2130         else if(loop) 
2131                 yaffsfs_SetError(-ELOOP);
2132         else if(!dir || !obj) 
2133                 yaffsfs_SetError(-ENOENT);
2134         else if(obj->my_dev->read_only)
2135                 yaffsfs_SetError(-EROFS);
2136         else
2137                 retVal = yaffsfs_DoChMod(obj,mode);
2138
2139         yaffsfs_Unlock();
2140
2141         return retVal;
2142
2143 }
2144
2145
2146 int yaffs_fchmod(int fd, mode_t mode)
2147 {
2148         struct yaffs_obj *obj;
2149         int retVal = -1;
2150
2151         if(mode & ~(0777)){
2152                 yaffsfs_SetError(-EINVAL);
2153                 return -1;
2154         }
2155
2156         yaffsfs_Lock();
2157         obj = yaffsfs_GetHandleObject(fd);
2158
2159         if(!obj)
2160                 yaffsfs_SetError(-EBADF);
2161         else if(obj->my_dev->read_only)
2162                 yaffsfs_SetError(-EROFS);
2163         else
2164                 retVal = yaffsfs_DoChMod(obj,mode);
2165
2166         yaffsfs_Unlock();
2167
2168         return retVal;
2169 }
2170
2171 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2172 {
2173         struct yaffs_obj *parent = NULL;
2174         struct yaffs_obj *dir = NULL;
2175         YCHAR *name;
2176         YCHAR *alt_path = NULL;
2177         int retVal= -1;
2178         int notDir = 0;
2179         int loop = 0;
2180
2181         if(!path){
2182                 yaffsfs_SetError(-EFAULT);
2183                 return -1;
2184         }
2185
2186         if(yaffsfs_CheckPath(path) < 0){
2187                 yaffsfs_SetError(-ENAMETOOLONG);
2188                 return -1;
2189         }
2190
2191         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2192                 yaffsfs_SetError(-ENOMEM);
2193                 return -1;
2194         }
2195         if(alt_path)
2196                 path = alt_path;
2197         
2198         yaffsfs_Lock();
2199         parent = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
2200         if(!parent && notDir)
2201                 yaffsfs_SetError(-ENOTDIR);
2202         else if(loop)
2203                 yaffsfs_SetError(-ELOOP);
2204         else if(!parent)
2205                 yaffsfs_SetError(-ENOENT);
2206         else if(yaffs_strnlen(name,5) == 0){
2207                 /* Trying to make the root itself */
2208                 yaffsfs_SetError(-EEXIST);
2209         } else if(parent->my_dev->read_only)
2210                 yaffsfs_SetError(-EROFS);
2211         else {
2212                 dir = yaffs_create_dir(parent,name,mode,0,0);
2213                 if(dir)
2214                         retVal = 0;
2215                 else if (yaffs_find_by_name(parent,name))
2216                         yaffsfs_SetError(-EEXIST); /* the name already exists */
2217                 else
2218                         yaffsfs_SetError(-ENOSPC); /* just assume no space */
2219         }
2220
2221         yaffsfs_Unlock();
2222
2223         if(alt_path)
2224                 YFREE(alt_path);
2225
2226         return retVal;
2227 }
2228
2229 int yaffs_rmdir(const YCHAR *path)
2230 {
2231         int result;
2232         YCHAR *alt_path;
2233
2234         if(!path){
2235                 yaffsfs_SetError(-EFAULT);
2236                 return -1;
2237         }
2238
2239         if(yaffsfs_CheckPath(path) < 0){
2240                 yaffsfs_SetError(-ENAMETOOLONG);
2241                 return -1;
2242         }
2243
2244         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2245                 yaffsfs_SetError(-ENOMEM);
2246                 return -1;
2247         }
2248         if(alt_path)
2249                 path = alt_path;
2250         result =  yaffsfs_DoUnlink(path,1);
2251         if(alt_path)
2252                 YFREE(alt_path);
2253         return result;
2254 }
2255
2256
2257 void * yaffs_getdev(const YCHAR *path)
2258 {
2259         struct yaffs_dev *dev=NULL;
2260         YCHAR *dummy;
2261         dev = yaffsfs_FindDevice(path,&dummy);
2262         return (void *)dev;
2263 }
2264
2265 int yaffs_mount2(const YCHAR *path,int read_only)
2266 {
2267         int retVal=-1;
2268         int result=YAFFS_FAIL;
2269         struct yaffs_dev *dev=NULL;
2270
2271         if(!path){
2272                 yaffsfs_SetError(-EFAULT);
2273                 return -1;
2274         }
2275
2276         T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2277
2278         if(yaffsfs_CheckPath(path) < 0){
2279                 yaffsfs_SetError(-ENAMETOOLONG);
2280                 return -1;
2281         }
2282
2283         yaffsfs_Lock();
2284
2285         yaffsfs_InitHandles();
2286
2287         dev = yaffsfs_FindMountPoint(path);
2288         if(dev){
2289                 if(!dev->is_mounted){
2290                         dev->read_only = read_only ? 1 : 0;
2291                         result = yaffs_guts_initialise(dev);
2292                         if(result == YAFFS_FAIL)
2293                                 yaffsfs_SetError(-ENOMEM);
2294                         retVal = result ? 0 : -1;
2295
2296                 }
2297                 else
2298                         yaffsfs_SetError(-EBUSY);
2299         } else
2300                 yaffsfs_SetError(-ENODEV);
2301
2302         yaffsfs_Unlock();
2303         return retVal;
2304
2305 }
2306
2307 int yaffs_mount(const YCHAR *path)
2308 {
2309         return yaffs_mount2(path,0);
2310 }
2311
2312 int yaffs_sync(const YCHAR *path)
2313 {
2314         int retVal=-1;
2315         struct yaffs_dev *dev=NULL;
2316         YCHAR *dummy;
2317
2318         if(!path){
2319                 yaffsfs_SetError(-EFAULT);
2320                 return -1;
2321         }
2322
2323         if(yaffsfs_CheckPath(path) < 0){
2324                 yaffsfs_SetError(-ENAMETOOLONG);
2325                 return -1;
2326         }
2327         
2328         yaffsfs_Lock();
2329         dev = yaffsfs_FindDevice(path,&dummy);
2330         if(dev){
2331                 if(!dev->is_mounted)
2332                         yaffsfs_SetError(-EINVAL);
2333                 else if(dev->read_only)
2334                         yaffsfs_SetError(-EROFS);
2335                 else {
2336                         
2337                         yaffs_flush_whole_cache(dev);
2338                         yaffs_checkpoint_save(dev);
2339                         retVal = 0;
2340                         
2341                 }      
2342         }else
2343                 yaffsfs_SetError(-ENODEV);
2344
2345         yaffsfs_Unlock();
2346         return retVal;  
2347 }
2348
2349
2350 int yaffs_remount(const YCHAR *path, int force, int read_only)
2351 {
2352         int retVal=-1;
2353         struct yaffs_dev *dev=NULL;
2354         yaffsfs_Handle *yh;
2355
2356         if(!path){
2357                 yaffsfs_SetError(-EFAULT);
2358                 return -1;
2359         }
2360
2361         if(yaffsfs_CheckPath(path) < 0){
2362                 yaffsfs_SetError(-ENAMETOOLONG);
2363                 return -1;
2364         }
2365
2366         yaffsfs_Lock();
2367         dev = yaffsfs_FindMountPoint(path);
2368         if(dev){
2369                 if(dev->is_mounted){
2370                         int i;
2371                         int inUse;
2372
2373                         yaffs_flush_whole_cache(dev);
2374
2375                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2376                                 yh = & yaffsfs_handle[i];
2377                                 if(yh->useCount>0 && 
2378                                         yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2379                                         inUse = 1; /* the device is in use, can't unmount */
2380                         }
2381
2382                         if(!inUse || force){
2383                                 if(read_only)
2384                                         yaffs_checkpoint_save(dev);
2385                                 dev->read_only =  read_only ? 1 : 0;
2386                                 retVal = 0;
2387                         } else
2388                                 yaffsfs_SetError(-EBUSY);
2389
2390                 } else
2391                         yaffsfs_SetError(-EINVAL);
2392
2393         }
2394         else
2395                 yaffsfs_SetError(-ENODEV);
2396
2397         yaffsfs_Unlock();
2398         return retVal;
2399
2400 }
2401
2402 int yaffs_unmount2(const YCHAR *path, int force)
2403 {
2404         int retVal=-1;
2405         struct yaffs_dev *dev=NULL;
2406
2407         if(!path){
2408                 yaffsfs_SetError(-EFAULT);
2409                 return -1;
2410         }
2411
2412         if(yaffsfs_CheckPath(path) < 0){
2413                 yaffsfs_SetError(-ENAMETOOLONG);
2414                 return -1;
2415         }
2416
2417         yaffsfs_Lock();
2418         dev = yaffsfs_FindMountPoint(path);
2419         if(dev){
2420                 if(dev->is_mounted){
2421                         int i;
2422                         int inUse;
2423
2424                         yaffs_flush_whole_cache(dev);
2425                         yaffs_checkpoint_save(dev);
2426
2427                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2428                                 if(yaffsfs_handle[i].useCount > 0 && 
2429                                 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2430                                         inUse = 1; /* the device is in use, can't unmount */
2431                         }
2432
2433                         if(!inUse || force){
2434                                 if(inUse)
2435                                         yaffsfs_BreakDeviceHandles(dev);
2436                                 yaffs_deinitialise(dev);
2437
2438                                 retVal = 0;
2439                         } else
2440                                 yaffsfs_SetError(-EBUSY);
2441
2442                 } else
2443                         yaffsfs_SetError(-EINVAL);
2444
2445         } else
2446                 yaffsfs_SetError(-ENODEV);
2447
2448         yaffsfs_Unlock();
2449         return retVal;
2450
2451 }
2452
2453 int yaffs_unmount(const YCHAR *path)
2454 {
2455         return yaffs_unmount2(path,0);
2456 }
2457
2458 loff_t yaffs_freespace(const YCHAR *path)
2459 {
2460         loff_t retVal=-1;
2461         struct yaffs_dev *dev=NULL;
2462         YCHAR *dummy;
2463
2464         if(!path){
2465                 yaffsfs_SetError(-EFAULT);
2466                 return -1;
2467         }
2468
2469         if(yaffsfs_CheckPath(path) < 0){
2470                 yaffsfs_SetError(-ENAMETOOLONG);
2471                 return -1;
2472         }
2473
2474         yaffsfs_Lock();
2475         dev = yaffsfs_FindDevice(path,&dummy);
2476         if(dev  && dev->is_mounted){
2477                 retVal = yaffs_get_n_free_chunks(dev);
2478                 retVal *= dev->data_bytes_per_chunk;
2479
2480         } else
2481                 yaffsfs_SetError(-EINVAL);
2482
2483         yaffsfs_Unlock();
2484         return retVal;
2485 }
2486
2487 loff_t yaffs_totalspace(const YCHAR *path)
2488 {
2489         loff_t retVal=-1;
2490         struct yaffs_dev *dev=NULL;
2491         YCHAR *dummy;
2492
2493         if(!path){
2494                 yaffsfs_SetError(-EFAULT);
2495                 return -1;
2496         }
2497
2498         if(yaffsfs_CheckPath(path) < 0){
2499                 yaffsfs_SetError(-ENAMETOOLONG);
2500                 return -1;
2501         }
2502
2503         yaffsfs_Lock();
2504         dev = yaffsfs_FindDevice(path,&dummy);
2505         if(dev  && dev->is_mounted){
2506                 retVal = (dev->param.end_block - dev->param.start_block + 1) - 
2507                         dev->param.n_reserved_blocks;
2508                 retVal *= dev->param.chunks_per_block;
2509                 retVal *= dev->data_bytes_per_chunk;
2510
2511         } else
2512                 yaffsfs_SetError(-EINVAL);
2513
2514         yaffsfs_Unlock();
2515         return retVal;
2516 }
2517
2518 int yaffs_inodecount(const YCHAR *path)
2519 {
2520         loff_t retVal= -1;
2521         struct yaffs_dev *dev=NULL;
2522         YCHAR *dummy;
2523
2524         if(!path){
2525                 yaffsfs_SetError(-EFAULT);
2526                 return -1;
2527         }
2528
2529         if(yaffsfs_CheckPath(path) < 0){
2530                 yaffsfs_SetError(-ENAMETOOLONG);
2531                 return -1;
2532         }
2533
2534         yaffsfs_Lock();
2535         dev = yaffsfs_FindDevice(path,&dummy);
2536         if(dev  && dev->is_mounted) {
2537            int n_obj = dev->n_obj;
2538            if(n_obj > dev->n_hardlinks)
2539                 retVal = n_obj - dev->n_hardlinks;
2540         }
2541         
2542         if(retVal < 0)
2543                 yaffsfs_SetError(-EINVAL);
2544         
2545         yaffsfs_Unlock();
2546         return retVal;  
2547 }
2548
2549
2550 void yaffs_add_device(struct yaffs_dev *dev)
2551 {
2552         dev->is_mounted = 0;
2553         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2554
2555         if(!dev->dev_list.next)
2556                 INIT_LIST_HEAD(&dev->dev_list);
2557
2558         list_add(&dev->dev_list,&yaffsfs_deviceList);
2559 }
2560
2561 void yaffs_remove_device(struct yaffs_dev *dev)
2562 {
2563         list_del_init(&dev->dev_list);
2564 }
2565
2566
2567
2568
2569 /* Directory search stuff. */
2570
2571 /*
2572  * Directory search context
2573  *
2574  * NB this is an opaque structure.
2575  */
2576
2577
2578 typedef struct
2579 {
2580         u32 magic;
2581         yaffs_dirent de;                /* directory entry being used by this dsc */
2582         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2583         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2584         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2585         int offset;
2586         struct list_head others;       
2587 } yaffsfs_DirectorySearchContext;
2588
2589
2590
2591 static struct list_head search_contexts;
2592
2593
2594 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2595 {
2596         if(dsc &&
2597            dsc->dirObj &&
2598            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2599
2600            dsc->offset = 0;
2601
2602            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2603                 dsc->nextReturn = NULL;
2604            else
2605                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2606                                                 struct yaffs_obj,siblings);
2607         } else {
2608                 /* Hey someone isn't playing nice! */
2609         }
2610 }
2611
2612 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2613 {
2614         if(dsc &&
2615            dsc->dirObj &&
2616            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2617
2618            if( dsc->nextReturn == NULL ||
2619                list_empty(&dsc->dirObj->variant.dir_variant.children))
2620                 dsc->nextReturn = NULL;
2621            else {
2622                    struct list_head *next = dsc->nextReturn->siblings.next;
2623
2624                    if( next == &dsc->dirObj->variant.dir_variant.children)
2625                         dsc->nextReturn = NULL; /* end of list */
2626                    else
2627                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2628            }
2629         } else {
2630                 /* Hey someone isn't playing nice! */
2631         }
2632 }
2633
2634 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2635 {
2636
2637         struct list_head *i;
2638         yaffsfs_DirectorySearchContext *dsc;
2639
2640         /* if search contexts not initilised then skip */
2641         if(!search_contexts.next)
2642                 return;
2643
2644         /* Iterate through the directory search contexts.
2645          * If any are the one being removed, then advance the dsc to
2646          * the next one to prevent a hanging ptr.
2647          */
2648          list_for_each(i, &search_contexts) {
2649                 if (i) {
2650                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2651                         if(dsc->nextReturn == obj)
2652                                 yaffsfs_DirAdvance(dsc);
2653                 }
2654         }
2655
2656 }
2657
2658 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2659 {
2660         yaffs_DIR *dir = NULL;
2661         struct yaffs_obj *obj = NULL;
2662         yaffsfs_DirectorySearchContext *dsc = NULL;
2663         int notDir = 0;
2664         int loop = 0;
2665
2666         if(!dirname){
2667                 yaffsfs_SetError(-EFAULT);
2668                 return NULL;
2669         }
2670
2671         if(yaffsfs_CheckPath(dirname) < 0){
2672                 yaffsfs_SetError(-ENAMETOOLONG);
2673                 return NULL;
2674         }
2675
2676         yaffsfs_Lock();
2677
2678         obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,&notDir,&loop);
2679
2680         if(!obj && notDir)
2681                 yaffsfs_SetError(-ENOTDIR);
2682         else if(loop)
2683                 yaffsfs_SetError(-ELOOP);
2684         else if(!obj)
2685                 yaffsfs_SetError(-ENOENT);
2686         else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2687                 yaffsfs_SetError(-ENOTDIR);
2688         else {
2689
2690                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2691                 dir = (yaffs_DIR *)dsc;
2692
2693                 if(dsc){
2694                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2695                         dsc->magic = YAFFS_MAGIC;
2696                         dsc->dirObj = obj;
2697                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2698                         INIT_LIST_HEAD(&dsc->others);
2699
2700                         if(!search_contexts.next)
2701                                 INIT_LIST_HEAD(&search_contexts);
2702
2703                         list_add(&dsc->others,&search_contexts);       
2704                         yaffsfs_SetDirRewound(dsc);
2705                 }
2706
2707         }
2708
2709         yaffsfs_Unlock();
2710
2711         return dir;
2712 }
2713
2714 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2715 {
2716         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2717         struct yaffs_dirent *retVal = NULL;
2718
2719         yaffsfs_Lock();
2720
2721         if(dsc && dsc->magic == YAFFS_MAGIC){
2722                 yaffsfs_SetError(0);
2723                 if(dsc->nextReturn){
2724                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2725                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2726                         dsc->de.d_off = dsc->offset++;
2727                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2728                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2729                         {
2730                                 /* this should not happen! */
2731                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2732                         }
2733                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2734                         retVal = &dsc->de;
2735                         yaffsfs_DirAdvance(dsc);
2736                 } else
2737                         retVal = NULL;
2738         } else
2739                 yaffsfs_SetError(-EBADF);
2740
2741         yaffsfs_Unlock();
2742
2743         return retVal;
2744
2745 }
2746
2747
2748 void yaffs_rewinddir(yaffs_DIR *dirp)
2749 {
2750         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2751
2752         yaffsfs_Lock();
2753
2754         yaffsfs_SetDirRewound(dsc);
2755
2756         yaffsfs_Unlock();
2757 }
2758
2759
2760 int yaffs_closedir(yaffs_DIR *dirp)
2761 {
2762         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2763
2764         if(!dsc){
2765                 yaffsfs_SetError(-EFAULT);
2766                 return -1;
2767         }
2768
2769         yaffsfs_Lock();
2770         dsc->magic = 0;
2771         list_del(&dsc->others); /* unhook from list */
2772         YFREE(dsc);
2773         yaffsfs_Unlock();
2774         return 0;
2775 }
2776
2777 /* End of directory stuff */
2778
2779
2780 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2781 {
2782         struct yaffs_obj *parent = NULL;
2783         struct yaffs_obj *obj;
2784         YCHAR *name;
2785         int retVal= -1;
2786         int mode = 0; /* ignore for now */
2787         int notDir = 0;
2788         int loop = 0;
2789
2790         if(!oldpath || !newpath){
2791                 yaffsfs_SetError(-EFAULT);
2792                 return -1;
2793         }
2794
2795         if(yaffsfs_CheckPath(newpath) < 0 ||
2796                 yaffsfs_CheckPath(oldpath) < 0){
2797                 yaffsfs_SetError(-ENAMETOOLONG);
2798                 return -1;
2799         }
2800
2801         yaffsfs_Lock();
2802         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,&notDir,&loop);
2803         if(!parent && notDir)
2804                 yaffsfs_SetError(-ENOTDIR);
2805         else if(loop)
2806                 yaffsfs_SetError(-ELOOP);
2807         else if( !parent || yaffs_strnlen(name,5) < 1)
2808                 yaffsfs_SetError(-ENOENT);
2809         else if(parent->my_dev->read_only)
2810                 yaffsfs_SetError(-EROFS);
2811         else if(parent){
2812                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2813                 if(obj)
2814                         retVal = 0;
2815                 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2816                         yaffsfs_SetError(-EEXIST);
2817                 else
2818                         yaffsfs_SetError(-ENOSPC);
2819         }
2820
2821         yaffsfs_Unlock();
2822
2823         return retVal;
2824
2825 }
2826
2827 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2828 {
2829         struct yaffs_obj *obj = NULL;
2830         struct yaffs_obj *dir = NULL;
2831         int retVal= -1;
2832         int notDir = 0;
2833         int loop = 0;
2834
2835         if(!path || !buf){
2836                 yaffsfs_SetError(-EFAULT);
2837                 return -1;
2838         }
2839
2840         yaffsfs_Lock();
2841
2842         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
2843
2844         if(!dir && notDir) 
2845                 yaffsfs_SetError(-ENOTDIR);
2846         else if(loop) 
2847                 yaffsfs_SetError(-ELOOP);
2848         else if(!dir || !obj) 
2849                 yaffsfs_SetError(-ENOENT);
2850         else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2851                 yaffsfs_SetError(-EINVAL);
2852         else {
2853                 YCHAR *alias = obj->variant.symlink_variant.alias;
2854                 memset(buf,0,bufsiz);
2855                 yaffs_strncpy(buf,alias,bufsiz - 1);
2856                 retVal = 0;
2857         }
2858         yaffsfs_Unlock();
2859         return retVal;
2860 }
2861
2862 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2863 {
2864         /* Creates a link called newpath to existing oldpath */
2865         struct yaffs_obj *obj = NULL;
2866         struct yaffs_obj *lnk = NULL;
2867         struct yaffs_obj *obj_dir = NULL;
2868         struct yaffs_obj *lnk_dir = NULL;
2869         int retVal = -1;
2870         int notDirObj = 0;
2871         int notDirLnk = 0;
2872         int objLoop = 0;
2873         int lnkLoop = 0;
2874         YCHAR *newname;
2875
2876         if(!oldpath || !linkpath){
2877                 yaffsfs_SetError(-EFAULT);
2878                 return -1;
2879         }
2880
2881         if(yaffsfs_CheckPath(linkpath) < 0 ||
2882                 yaffsfs_CheckPath(oldpath) < 0){
2883                 yaffsfs_SetError(-ENAMETOOLONG);
2884                 return -1;
2885         }
2886
2887         yaffsfs_Lock();
2888
2889         obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,&notDirObj,&objLoop);
2890         lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2891         lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,&notDirLnk,&lnkLoop);
2892
2893         if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2894                 yaffsfs_SetError(-ENOTDIR);
2895         else if(objLoop || lnkLoop)
2896                 yaffsfs_SetError(-ELOOP);
2897         else if(!obj_dir || !lnk_dir || !obj)
2898                 yaffsfs_SetError(-ENOENT);
2899         else if(obj->my_dev->read_only)
2900                 yaffsfs_SetError(-EROFS);
2901         else if(lnk)
2902                 yaffsfs_SetError(-EEXIST);
2903         else if(lnk_dir->my_dev != obj->my_dev)
2904                 yaffsfs_SetError(-EXDEV);
2905         else {          
2906                 retVal = yaffsfs_CheckNameLength(newname);
2907                 
2908                 if(retVal == 0) {
2909                         lnk = yaffs_link_obj(lnk_dir,newname,obj);
2910                         if(lnk)
2911                                 retVal = 0;
2912                         else{
2913                                 yaffsfs_SetError(-ENOSPC);
2914                                 retVal = -1;
2915                         }
2916                 }
2917         }
2918         yaffsfs_Unlock();
2919
2920         return retVal;
2921 }
2922
2923 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2924 {
2925         pathname=pathname;
2926         mode=mode;
2927         dev=dev;
2928
2929         yaffsfs_SetError(-EINVAL);
2930         return -1;
2931 }
2932
2933
2934 /*
2935  * D E B U G   F U N C T I O N S
2936  */
2937  
2938 /*
2939  * yaffs_n_handles()
2940  * Returns number of handles attached to the object
2941  */
2942 int yaffs_n_handles(const YCHAR *path)
2943 {
2944         struct yaffs_obj *obj;
2945
2946         if(!path){
2947                 yaffsfs_SetError(-EFAULT);
2948                 return -1;
2949         }
2950
2951         if(yaffsfs_CheckPath(path) < 0){
2952                 yaffsfs_SetError(-ENAMETOOLONG);
2953                 return -1;
2954         }
2955
2956         obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2957
2958         if(obj)
2959                 return yaffsfs_CountHandles(obj);
2960         else
2961                 return -1;
2962 }
2963
2964 int yaffs_get_error(void)
2965 {
2966         return yaffsfs_GetLastError();
2967 }
2968
2969 int yaffs_set_error(int error)
2970 {
2971         yaffsfs_SetError(error);
2972         return 0;
2973 }
2974
2975 int yaffs_dump_dev(const YCHAR *path)
2976 {
2977 #if 1
2978         path=path;
2979 #else
2980         YCHAR *rest;
2981
2982         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2983
2984         if(obj){
2985                 struct yaffs_dev *dev = obj->my_dev;
2986
2987                 printf("\n"
2988                            "n_page_writes.......... %d\n"
2989                            "n_page_reads........... %d\n"
2990                            "n_erasures....... %d\n"
2991                            "n_gc_copies............ %d\n"
2992                            "garbageCollections... %d\n"
2993                            "passiveGarbageColl'ns %d\n"
2994                            "\n",
2995                                 dev->n_page_writes,
2996                                 dev->n_page_reads,
2997                                 dev->n_erasures,
2998                                 dev->n_gc_copies,
2999                                 dev->garbageCollections,
3000                                 dev->passiveGarbageCollections
3001                 );
3002
3003         }
3004
3005 #endif
3006         return 0;
3007 }
3008