yaffs Fixed some problems with 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 int yaffs_dup(int fd)
639 {
640         int newHandle = -1;
641         yaffsfs_Handle *oldPtr = NULL;
642         yaffsfs_Handle *newPtr = NULL;
643
644         yaffsfs_Lock();
645
646         oldPtr = yaffsfs_GetHandlePointer(fd);
647         if(oldPtr && oldPtr->useCount > 0)
648                 newHandle = yaffsfs_GetNewHandle();
649         if(newHandle >= 0)
650                 newPtr = yaffsfs_GetHandlePointer(newHandle);
651
652         if(newPtr){
653                 *newPtr = *oldPtr;
654                 return newHandle;
655         }
656
657         if(!oldPtr)
658                 yaffsfs_SetError(-EBADF);
659         else
660                 yaffsfs_SetError(-ENOMEM);
661
662         return -1;
663
664 }
665
666
667
668 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
669 {
670         struct yaffs_obj *obj = NULL;
671         struct yaffs_obj *dir = NULL;
672         YCHAR *name;
673         int handle = -1;
674         yaffsfs_Handle *yh = NULL;
675         int openDenied = 0;
676         int symDepth = 0;
677         int errorReported = 0;
678         int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
679         u8 shareRead = (sharing & YAFFS_SHARE_READ) ?  1 : 0;
680         u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
681         u8 sharedReadAllowed;
682         u8 sharedWriteAllowed;
683         u8 alreadyReading;
684         u8 alreadyWriting;
685         u8 readRequested;
686         u8 writeRequested;
687         int notDir = 0;
688         int loop = 0;
689
690         if(yaffsfs_CheckPath(path) < 0){
691                 yaffsfs_SetError(-ENAMETOOLONG);
692                 return -1;
693         }
694
695         /* O_EXCL only has meaning if O_CREAT is specified */
696         if(!(oflag & O_CREAT))
697                 oflag &= ~(O_EXCL);
698
699         /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
700         if( (oflag & O_CREAT) & (oflag & O_EXCL))
701                 oflag &= ~(O_TRUNC);
702
703         /* Todo: Are there any more flag combos to sanitise ? */
704
705         /* Figure out if reading or writing is requested */
706
707         readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
708         writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
709
710         yaffsfs_Lock();
711
712         handle = yaffsfs_GetNewHandle();
713
714         if(handle < 0){
715                 yaffsfs_SetError(-ENFILE);
716                 errorReported = 1;
717         } else {
718
719                 yh = yaffsfs_GetHandlePointer(handle);
720
721                 /* try to find the exisiting object */
722                 obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
723
724                 obj = yaffsfs_FollowLink(obj,symDepth++,&loop);
725
726                 if(obj &&
727                         obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
728                         obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
729                         obj = NULL;
730
731
732                 if(obj){
733
734                         /* The file already exists or it might be a directory */
735
736                         /* If it is a directory then we can't open it as a file */
737                         if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
738                                 openDenied = 1;
739                                 yaffsfs_SetError(-EISDIR);
740                                 errorReported = 1;
741                         }
742
743                         /* Open should fail if O_CREAT and O_EXCL are specified since
744                          * the file exists
745                          */
746                         if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
747                                 openDenied = 1;
748                                 yaffsfs_SetError(-EEXIST);
749                                 errorReported = 1;
750                         }
751
752                         /* Check file permissions */
753                         if( readRequested && !(obj->yst_mode & S_IREAD))
754                                 openDenied = 1;
755
756                         if( writeRequested && !(obj->yst_mode & S_IWRITE))
757                                 openDenied = 1;
758
759                         if(openDenied && !errorReported ) {
760                                 /* Error if the file exists but permissions are refused. */
761                                 yaffsfs_SetError(-EACCES);
762                                 errorReported = 1;
763                         }
764
765                         /* Check sharing of an existing object. */
766                         if(!openDenied){
767                                 yaffsfs_Handle *hx;
768                                 int i;
769                                 sharedReadAllowed = 1;
770                                 sharedWriteAllowed = 1;
771                                 alreadyReading = 0;
772                                 alreadyWriting = 0;
773                                 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
774                                         hx = &yaffsfs_handle[i];
775                                         if(hx->useCount > 0 &&
776                                                 hx->inodeId >= 0 &&
777                                                 yaffsfs_inode[hx->inodeId].iObj == obj){
778                                                 if(!hx->shareRead)
779                                                         sharedReadAllowed = 0;
780                                                 if(!hx->shareWrite)
781                                                         sharedWriteAllowed = 0;
782                                                 if(hx->reading)
783                                                         alreadyReading = 1;
784                                                 if(hx->writing)
785                                                         alreadyWriting = 1;
786                                         }
787                                 }
788
789
790
791                                 if((!sharedReadAllowed && readRequested)|| 
792                                         (!shareRead  && alreadyReading) ||
793                                         (!sharedWriteAllowed && writeRequested) ||
794                                         (!shareWrite && alreadyWriting)){
795                                         openDenied = 1;
796                                         yaffsfs_SetError(-EBUSY);
797                                         errorReported=1;
798                                 }
799                         }
800
801                 }
802
803                 /* If we could not open an existing object, then let's see if
804                  * the directory exists. If not, error.
805                  */
806                 if(!obj && !errorReported){
807                         dir = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
808                         if(!dir && notDir){
809                                 yaffsfs_SetError(-ENOTDIR);
810                                 errorReported = 1;
811                         } else if(loop){
812                                 yaffsfs_SetError(-ELOOP);
813                                 errorReported = 1;
814                         } else  if(!dir){
815                                 yaffsfs_SetError(-ENOENT);
816                                 errorReported = 1;
817                         }
818                 }
819
820                 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
821                         /* Let's see if we can create this file if it does not exist. */
822                         if(dir->my_dev->read_only){
823                                 yaffsfs_SetError(-EINVAL);
824                                 errorReported = 1;
825                         } else
826                                 obj = yaffs_create_file(dir,name,mode,0,0);
827
828                         if(!obj && !errorReported){
829                                 yaffsfs_SetError(-ENOSPC);
830                                 errorReported = 1;
831                         }
832                 }
833
834                 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
835                         /* Error if the file does not exist and CREAT is not set. */
836                         yaffsfs_SetError(-ENOENT);
837                         errorReported = 1;
838                 }
839
840                 if(obj && !openDenied) {
841                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
842
843                         if(inodeId<0) {
844                                 /*
845                                  * Todo: Fix any problem if inodes run out, though that
846                                  * can't happen if the number of inode items >= number of handles. 
847                                  */
848                         }
849                         
850                         yh->inodeId = inodeId;
851                         yh->reading = readRequested;
852                         yh->writing = writeRequested;
853                         yh->append =  (oflag & O_APPEND) ? 1 : 0;
854                         yh->position = 0;
855                         yh->shareRead = shareRead;
856                         yh->shareWrite = shareWrite;
857
858                         /* Hook inode to object */
859                         obj->my_inode = (void*) &yaffsfs_inode[inodeId];
860
861                         if((oflag & O_TRUNC) && yh->writing)
862                                 yaffs_resize_file(obj,0);
863                 } else {
864                         yaffsfs_PutHandle(handle);
865                         if(!errorReported) 
866                                 yaffsfs_SetError(0); /* Problem */
867                         handle = -1;
868                 }
869         }
870
871         yaffsfs_Unlock();
872
873         return handle;
874 }
875
876 int yaffs_open(const YCHAR *path, int oflag, int mode)
877 {
878         return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
879 }
880
881 int yaffs_Dofsync(int fd,int datasync)
882 {
883         yaffsfs_Handle *h = NULL;
884         int retVal = 0;
885
886         yaffsfs_Lock();
887
888         h = yaffsfs_GetHandlePointer(fd);
889
890         if(h && h->useCount > 0)
891                 /* flush the file */
892                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
893         else {
894                 /* bad handle */
895                 yaffsfs_SetError(-EBADF);
896                 retVal = -1;
897         }
898
899         yaffsfs_Unlock();
900
901         return retVal;
902 }
903
904 int yaffs_fsync(int fd)
905 {
906         return yaffs_Dofsync(fd,0);
907 }
908
909 int yaffs_flush(int fd)
910 {
911         return yaffs_fsync(fd);
912 }
913
914 int yaffs_fdatasync(int fd)
915 {
916         return yaffs_Dofsync(fd,1);
917 }
918
919 int yaffs_close(int fd)
920 {
921         yaffsfs_Handle *h = NULL;
922         int retVal = 0;
923
924         yaffsfs_Lock();
925
926         h = yaffsfs_GetHandlePointer(fd);
927
928         if(h && h->useCount > 0) {
929                 /* clean up */
930                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
931                 yaffsfs_PutHandle(fd);
932                 retVal = 0;
933         } else {
934                 /* bad handle */
935                 yaffsfs_SetError(-EBADF);
936                 retVal = -1;
937         }
938
939         yaffsfs_Unlock();
940
941         return retVal;
942 }
943
944
945
946 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
947 {
948         yaffsfs_Handle *h = NULL;
949         struct yaffs_obj *obj = NULL;
950         int pos = 0;
951         int startPos = 0;
952         int endPos = 0;
953         int nRead = 0;
954         int nToRead = 0;
955         int totalRead = 0;
956         unsigned int maxRead;
957         u8 *buf = (u8 *)vbuf;
958
959         yaffsfs_Lock();
960         h = yaffsfs_GetHandlePointer(fd);
961         obj = yaffsfs_GetHandleObject(fd);
962
963         if(!h || !obj){
964                 /* bad handle */
965                 yaffsfs_SetError(-EBADF);
966                 totalRead = -1;
967         } else if(!h->reading){
968                 /* Not a reading handle */
969                 yaffsfs_SetError(-EINVAL);
970                 totalRead = -1;
971         } else if(nbyte > YAFFS_MAX_FILE_SIZE){
972                 yaffsfs_SetError(-EINVAL);
973                 totalRead = -1;
974         } else {
975                 if(isPread)
976                         startPos = offset;
977                 else
978                         startPos = h->position;
979
980                 pos = startPos;
981                                         
982                 if(yaffs_get_obj_length(obj) > pos)
983                         maxRead = yaffs_get_obj_length(obj) - pos;
984                 else
985                         maxRead = 0;
986
987                 if(nbyte > maxRead)
988                         nbyte = maxRead;
989
990
991                 yaffsfs_GetHandle(fd);
992
993                 endPos = pos + nbyte;
994
995                 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
996                         nbyte > YAFFS_MAX_FILE_SIZE ||
997                         endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
998                         totalRead = -1;
999                         nbyte = 0;
1000                 }
1001
1002                 while(nbyte > 0) {
1003                         nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1004                         if(nToRead > nbyte)
1005                                 nToRead = nbyte;
1006
1007                         /* Tricky bit... 
1008                          * Need to reverify object in case the device was
1009                          * unmounted in another thread.
1010                          */
1011                         obj = yaffsfs_GetHandleObject(fd);
1012                         if(!obj)
1013                                 nRead = 0;
1014                         else
1015                                 nRead = yaffs_file_rd(obj,buf,pos,nToRead);
1016
1017                         if(nRead > 0){
1018                                 totalRead += nRead;
1019                                 pos += nRead;
1020                                 buf += nRead;
1021                         }
1022
1023                         if(nRead == nToRead)
1024                                 nbyte-=nRead;
1025                         else
1026                                 nbyte = 0; /* no more to read */
1027                                         
1028                                         
1029                         if(nbyte > 0){
1030                                 yaffsfs_Unlock();
1031                                 yaffsfs_Lock();
1032                         }
1033
1034                 }
1035
1036                 yaffsfs_PutHandle(fd);
1037
1038                 if(!isPread) {
1039                         if(totalRead >= 0)
1040                                 h->position = startPos + totalRead;
1041                         else
1042                                 yaffsfs_SetError(-EINVAL);
1043                 }
1044
1045         }
1046
1047         yaffsfs_Unlock();
1048
1049         return (totalRead >= 0) ? totalRead : -1;
1050
1051 }
1052
1053 int yaffs_read(int fd, void *buf, unsigned int nbyte)
1054 {
1055         return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
1056 }
1057
1058 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
1059 {
1060         return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
1061 }
1062
1063 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
1064 {
1065         yaffsfs_Handle *h = NULL;
1066         struct yaffs_obj *obj = NULL;
1067         int pos = 0;
1068         int startPos = 0;
1069         int endPos;
1070         int nWritten = 0;
1071         int totalWritten = 0;
1072         int write_trhrough = 0;
1073         int nToWrite = 0;
1074         const u8 *buf = (const u8 *)vbuf;
1075
1076         yaffsfs_Lock();
1077         h = yaffsfs_GetHandlePointer(fd);
1078         obj = yaffsfs_GetHandleObject(fd);
1079
1080         if(!h || !obj){
1081                 /* bad handle */
1082                 yaffsfs_SetError(-EBADF);
1083                 totalWritten = -1;
1084         } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
1085                 yaffsfs_SetError(-EINVAL);
1086                 totalWritten=-1;
1087         } else {
1088                 if(h->append)
1089                         startPos = yaffs_get_obj_length(obj);
1090                 else if(isPwrite)
1091                         startPos = offset;
1092                 else
1093                         startPos = h->position;
1094
1095                 yaffsfs_GetHandle(fd);
1096                 pos = startPos;
1097                 endPos = pos + nbyte;
1098
1099                 if(pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1100                         nbyte > YAFFS_MAX_FILE_SIZE ||
1101                         endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE){
1102                         totalWritten = -1;
1103                         nbyte = 0;
1104                 }
1105
1106                 while(nbyte > 0) {
1107
1108                         nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
1109                         if(nToWrite > nbyte)
1110                                 nToWrite = nbyte;
1111
1112                         /* Tricky bit... 
1113                          * Need to reverify object in case the device was
1114                          * remounted or unmounted in another thread.
1115                          */
1116                         obj = yaffsfs_GetHandleObject(fd);
1117                         if(!obj || obj->my_dev->read_only)
1118                                 nWritten = 0;
1119                         else
1120                                 nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,
1121                                                         write_trhrough);
1122                         if(nWritten > 0){
1123                                 totalWritten += nWritten;
1124                                 pos += nWritten;
1125                                 buf += nWritten;
1126                         }
1127
1128                         if(nWritten == nToWrite)
1129                                 nbyte -= nToWrite;
1130                         else
1131                                 nbyte = 0;
1132
1133                         if(nWritten < 1 && totalWritten < 1){
1134                                 yaffsfs_SetError(-ENOSPC);
1135                                 totalWritten = -1;
1136                         }
1137
1138                         if(nbyte > 0){
1139                                 yaffsfs_Unlock();
1140                                 yaffsfs_Lock();
1141                         }
1142                 }
1143
1144                 yaffsfs_PutHandle(fd);
1145
1146                 if(!isPwrite){
1147                         if(totalWritten > 0)
1148                                 h->position = startPos + totalWritten;
1149                         else
1150                                 yaffsfs_SetError(-EINVAL);
1151                 }
1152         }
1153
1154         yaffsfs_Unlock();
1155
1156         return (totalWritten >= 0) ? totalWritten : -1;
1157 }
1158
1159 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1160 {
1161         return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1162 }
1163
1164 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1165 {
1166         return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1167 }
1168
1169
1170 int yaffs_truncate(const YCHAR *path,off_t new_size)
1171 {
1172         struct yaffs_obj *obj = NULL;
1173         struct yaffs_obj *dir = NULL;
1174         int result = YAFFS_FAIL;
1175         int notDir = 0;
1176         int loop = 0;
1177
1178         if(yaffsfs_CheckPath(path) < 0){
1179                 yaffsfs_SetError(-ENAMETOOLONG);
1180                 return -1;
1181         }
1182
1183         yaffsfs_Lock();
1184
1185         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1186         obj = yaffsfs_FollowLink(obj,0,&loop);
1187
1188         if(!dir && notDir)
1189                 yaffsfs_SetError(-ENOTDIR);
1190         else if(loop)
1191                 yaffsfs_SetError(-ELOOP);
1192         else if(!dir || !obj)
1193                 yaffsfs_SetError(-ENOENT);
1194         else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1195                 yaffsfs_SetError(-EISDIR);
1196         else if(obj->my_dev->read_only)
1197                 yaffsfs_SetError(-EACCES);
1198         else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1199                 yaffsfs_SetError(-EINVAL);
1200         else
1201                 result = yaffs_resize_file(obj,new_size);
1202
1203         yaffsfs_Unlock();
1204
1205         return (result) ? 0 : -1;
1206 }
1207
1208 int yaffs_ftruncate(int fd, off_t new_size)
1209 {
1210         yaffsfs_Handle *h = NULL;
1211         struct yaffs_obj *obj = NULL;
1212         int result = 0;
1213
1214         yaffsfs_Lock();
1215         h = yaffsfs_GetHandlePointer(fd);
1216         obj = yaffsfs_GetHandleObject(fd);
1217
1218         if(!h || !obj)
1219                 /* bad handle */
1220                 yaffsfs_SetError(-EBADF);
1221         else if(obj->my_dev->read_only)
1222                 yaffsfs_SetError(-EACCES);
1223         else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1224                 yaffsfs_SetError(-EINVAL);
1225         else
1226                 /* resize the file */
1227                 result = yaffs_resize_file(obj,new_size);
1228         yaffsfs_Unlock();
1229
1230         return (result) ? 0 : -1;
1231
1232 }
1233
1234 off_t yaffs_lseek(int fd, off_t offset, int whence)
1235 {
1236         yaffsfs_Handle *h = NULL;
1237         struct yaffs_obj *obj = NULL;
1238         int pos = -1;
1239         int fSize = -1;
1240
1241         yaffsfs_Lock();
1242         h = yaffsfs_GetHandlePointer(fd);
1243         obj = yaffsfs_GetHandleObject(fd);
1244
1245         if(!h || !obj)
1246                 yaffsfs_SetError(-EBADF);
1247         else if(offset > YAFFS_MAX_FILE_SIZE)
1248                 yaffsfs_SetError(-EINVAL);
1249         else {
1250                 if(whence == SEEK_SET){
1251                         if(offset >= 0)
1252                                 pos = offset;
1253                 } else if(whence == SEEK_CUR) {
1254                         if( (h->position + offset) >= 0)
1255                                 pos = (h->position + offset);
1256                 } else if(whence == SEEK_END) {
1257                         fSize = yaffs_get_obj_length(obj);
1258                         if(fSize >= 0 && (fSize + offset) >= 0)
1259                                 pos = fSize + offset;
1260                 } 
1261
1262                 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1263                         h->position = pos;
1264                 else{
1265                         yaffsfs_SetError(-EINVAL);
1266                         pos = -1;
1267                 }
1268         }
1269
1270         yaffsfs_Unlock();
1271
1272         return pos;
1273 }
1274
1275
1276 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1277 {
1278         struct yaffs_obj *dir = NULL;
1279         struct yaffs_obj *obj = NULL;
1280         YCHAR *name;
1281         int result = YAFFS_FAIL;
1282         int notDir = 0;
1283         int loop = 0;
1284
1285         if(yaffsfs_CheckPath(path) < 0){
1286                 yaffsfs_SetError(-ENAMETOOLONG);
1287                 return -1;
1288         }
1289
1290         yaffsfs_Lock();
1291
1292         obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1293         dir = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
1294
1295         if(!dir && notDir)
1296                 yaffsfs_SetError(-ENOTDIR);
1297         else if(loop)
1298                 yaffsfs_SetError(-ELOOP);
1299         else if(!dir)
1300                 yaffsfs_SetError(-ENOENT);
1301         else if(yaffs_strncmp(name,_Y("."),2) == 0)
1302                 yaffsfs_SetError(-EINVAL);
1303         else if(!obj)
1304                 yaffsfs_SetError(-ENOENT);
1305         else if(obj->my_dev->read_only)
1306                 yaffsfs_SetError(-EINVAL);
1307         else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1308                 yaffsfs_SetError(-EISDIR);
1309         else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1310                 yaffsfs_SetError(-ENOTDIR);
1311         else if(isDirectory && obj == obj->my_dev->root_dir)
1312                 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1313         else {
1314                 result = yaffs_unlinker(dir,name);
1315
1316                 if(result == YAFFS_FAIL && isDirectory)
1317                         yaffsfs_SetError(-ENOTEMPTY);
1318         }
1319
1320         yaffsfs_Unlock();
1321
1322         return (result == YAFFS_FAIL) ? -1 : 0;
1323 }
1324
1325
1326 int yaffs_unlink(const YCHAR *path)
1327 {
1328         return yaffsfs_DoUnlink(path,0);
1329 }
1330
1331 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1332 {
1333         struct yaffs_obj *olddir = NULL;
1334         struct yaffs_obj *newdir = NULL;
1335         struct yaffs_obj *obj = NULL;
1336         struct yaffs_obj *newobj = NULL;
1337         YCHAR *oldname;
1338         YCHAR *newname;
1339         int result= YAFFS_FAIL;
1340         int rename_allowed = 1;
1341         int notOldDir = 0;
1342         int notNewDir = 0;
1343         int oldLoop = 0;
1344         int newLoop = 0;
1345
1346         YCHAR *alt_newpath=NULL;
1347
1348         if(yaffsfs_CheckPath(newPath) < 0){
1349                 yaffsfs_SetError(-ENAMETOOLONG);
1350                 return -1;
1351         }
1352
1353         if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1354                 yaffsfs_SetError(-ENOMEM);
1355                 return -1;
1356         }
1357         if(alt_newpath)
1358                 newPath = alt_newpath;
1359
1360         yaffsfs_Lock();
1361
1362
1363         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,&notOldDir,&oldLoop);
1364         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,&notNewDir,&newLoop);
1365         obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1366         newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1367
1368         /* If the object being renamed is a directory and the 
1369          * path ended with a "/" then the olddir == obj.
1370          * We pass through NULL for the old name to tell the lower layers
1371          * to use olddir as the object.
1372          */
1373
1374         if(olddir == obj)
1375                 oldname = NULL;
1376
1377         if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1378                 yaffsfs_SetError(-ENOTDIR);
1379                 rename_allowed = 0;
1380         } else if(oldLoop || newLoop) {
1381                 yaffsfs_SetError(-ELOOP);
1382                 rename_allowed = 0;
1383         } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1384                 yaffsfs_SetError(-EINVAL);
1385                 rename_allowed = 0;
1386         }else if(!olddir || !newdir || !obj) {
1387                 yaffsfs_SetError(-ENOENT);
1388                 rename_allowed = 0;
1389         } else if(obj->my_dev->read_only){
1390                 yaffsfs_SetError(-EROFS);
1391                 rename_allowed = 0;
1392         } else if(yaffs_is_non_empty_dir(newobj)){
1393                 yaffsfs_SetError(-ENOTEMPTY);
1394                 rename_allowed = 0;
1395         } else if(olddir->my_dev != newdir->my_dev) {
1396                 /* Rename must be on same device */
1397                 yaffsfs_SetError(-EXDEV);
1398                 rename_allowed = 0;
1399         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1400                 /*
1401                  * It is a directory, check that it is not being renamed to
1402                  * being its own decendent.
1403                  * Do this by tracing from the new directory back to the root, 
1404                  * checking for obj
1405                  */
1406
1407                 struct yaffs_obj *xx = newdir;
1408
1409                 while( rename_allowed && xx){
1410                         if(xx == obj)
1411                                 rename_allowed = 0;
1412                         xx = xx->parent;
1413                 }
1414                 if(!rename_allowed)
1415                         yaffsfs_SetError(-EINVAL);
1416         }
1417
1418         if(rename_allowed)
1419                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1420
1421         yaffsfs_Unlock();
1422
1423         if(alt_newpath)
1424                 YFREE(alt_newpath);
1425
1426         return (result == YAFFS_FAIL) ? -1 : 0;
1427 }
1428
1429
1430 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1431 {
1432         int retVal = -1;
1433
1434         obj = yaffs_get_equivalent_obj(obj);
1435
1436         if(obj && buf){
1437                 buf->st_dev = (int)obj->my_dev->os_context;
1438                 buf->st_ino = obj->obj_id;
1439                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1440
1441                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1442                         buf->st_mode |= S_IFDIR;
1443                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1444                         buf->st_mode |= S_IFLNK;
1445                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1446                         buf->st_mode |= S_IFREG;
1447
1448                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1449                 buf->st_uid = 0;
1450                 buf->st_gid = 0;;
1451                 buf->st_rdev = obj->yst_rdev;
1452                 buf->st_size = yaffs_get_obj_length(obj);
1453                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1454                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1455 #if CONFIG_YAFFS_WINCE
1456                 buf->yst_wince_atime[0] = obj->win_atime[0];
1457                 buf->yst_wince_atime[1] = obj->win_atime[1];
1458                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1459                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1460                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1461                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1462 #else
1463                 buf->yst_atime = obj->yst_atime;
1464                 buf->yst_ctime = obj->yst_ctime;
1465                 buf->yst_mtime = obj->yst_mtime;
1466 #endif
1467                 retVal = 0;
1468         }
1469         return retVal;
1470 }
1471
1472 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1473 {
1474         struct yaffs_obj *obj=NULL;
1475         struct yaffs_obj *dir=NULL;
1476         int retVal = -1;
1477         int notDir = 0;
1478         int loop = 0;
1479
1480         if(yaffsfs_CheckPath(path) < 0){
1481                 yaffsfs_SetError(-ENAMETOOLONG);
1482                 return -1;
1483         }
1484
1485         yaffsfs_Lock();
1486
1487         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1488
1489         if(!doLStat && obj)
1490                 obj = yaffsfs_FollowLink(obj,0,&loop);
1491
1492         if(!dir && notDir)
1493                 yaffsfs_SetError(-ENOTDIR);
1494         else if(loop)
1495                 yaffsfs_SetError(-ELOOP);
1496         else if(!dir || !obj)
1497                 yaffsfs_SetError(-ENOENT);
1498         else
1499                 retVal = yaffsfs_DoStat(obj,buf);
1500
1501         yaffsfs_Unlock();
1502
1503         return retVal;
1504
1505 }
1506
1507 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1508 {
1509         return yaffsfs_DoStatOrLStat(path,buf,0);
1510 }
1511
1512 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1513 {
1514         return yaffsfs_DoStatOrLStat(path,buf,1);
1515 }
1516
1517 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1518 {
1519         struct yaffs_obj *obj;
1520
1521         int retVal = -1;
1522
1523         yaffsfs_Lock();
1524         obj = yaffsfs_GetHandleObject(fd);
1525
1526         if(obj)
1527                 retVal = yaffsfs_DoStat(obj,buf);
1528         else
1529                 /* bad handle */
1530                 yaffsfs_SetError(-EBADF);
1531
1532         yaffsfs_Unlock();
1533
1534         return retVal;
1535 }
1536
1537 #ifndef CONFIG_YAFFS_WINCE
1538 /* xattrib functions */
1539
1540
1541 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1542 {
1543         struct yaffs_obj *obj;
1544         struct yaffs_obj *dir;
1545         int notDir = 0;
1546         int loop = 0;
1547
1548         int retVal = -1;
1549
1550         if(yaffsfs_CheckPath(path) < 0){
1551                 yaffsfs_SetError(-ENAMETOOLONG);
1552                 return -1;
1553         }
1554
1555         yaffsfs_Lock();
1556
1557         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1558
1559         if(follow)
1560                 obj = yaffsfs_FollowLink(obj,0,&loop);
1561
1562         if(!dir && notDir) 
1563                 yaffsfs_SetError(-ENOTDIR);
1564         else if(loop) 
1565                 yaffsfs_SetError(-ELOOP);
1566         else if(!dir || !obj) 
1567                 yaffsfs_SetError(-ENOENT);
1568         else {
1569                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1570                 if(retVal< 0){
1571                         yaffsfs_SetError(retVal);
1572                         retVal = -1;
1573                 }
1574         }
1575
1576         yaffsfs_Unlock();
1577
1578         return retVal;
1579
1580 }
1581
1582 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1583 {
1584         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1585 }
1586
1587 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1588 {
1589         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1590 }
1591
1592
1593
1594 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1595 {
1596         struct yaffs_obj *obj;
1597
1598         int retVal = -1;
1599
1600         yaffsfs_Lock();
1601         obj = yaffsfs_GetHandleObject(fd);
1602
1603         if(!obj) 
1604                 yaffsfs_SetError(-EBADF);
1605         else {
1606                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1607                 if(retVal< 0){
1608                         yaffsfs_SetError(retVal);
1609                         retVal = -1;
1610                 }
1611         }
1612
1613         yaffsfs_Unlock();
1614
1615         return retVal;
1616 }
1617
1618 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1619 {
1620         struct yaffs_obj *obj;
1621         struct yaffs_obj *dir;
1622         int retVal = -1;
1623         int notDir = 0;
1624         int loop = 0;
1625
1626         if(yaffsfs_CheckPath(path) < 0){
1627                 yaffsfs_SetError(-ENAMETOOLONG);
1628                 return -1;
1629         }
1630
1631         yaffsfs_Lock();
1632
1633         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1634
1635         if(follow)
1636                 obj = yaffsfs_FollowLink(obj,0,&loop);
1637
1638         if(!dir && notDir) 
1639                 yaffsfs_SetError(-ENOTDIR);
1640         else if(loop) 
1641                 yaffsfs_SetError(-ELOOP);
1642         else if(!dir || !obj) 
1643                 yaffsfs_SetError(-ENOENT);
1644         else {
1645                 retVal = yaffs_get_xattrib(obj,name,data,size);
1646                 if(retVal< 0){
1647                         yaffsfs_SetError(retVal);
1648                         retVal = -1;
1649                 }
1650         }
1651         yaffsfs_Unlock();
1652
1653         return retVal;
1654
1655 }
1656
1657 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1658 {
1659         return yaffs_do_getxattr( path, name, data, size, 1);
1660 }
1661 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1662 {
1663         return yaffs_do_getxattr( path, name, data, size, 0);
1664 }
1665
1666
1667
1668 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1669 {
1670         struct yaffs_obj *obj;
1671
1672         int retVal = -1;
1673
1674         yaffsfs_Lock();
1675         obj = yaffsfs_GetHandleObject(fd);
1676
1677         if(obj) {
1678                 retVal = yaffs_get_xattrib(obj,name,data,size);
1679                 if(retVal< 0){
1680                         yaffsfs_SetError(retVal);
1681                         retVal = -1;
1682                 }
1683         } else
1684                 /* bad handle */
1685                 yaffsfs_SetError(-EBADF);
1686
1687         yaffsfs_Unlock();
1688
1689         return retVal;
1690 }
1691
1692 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1693 {
1694         struct yaffs_obj *obj=NULL;
1695         struct yaffs_obj *dir=NULL;
1696         int retVal = -1;
1697         int notDir = 0;
1698         int loop = 0;
1699
1700         if(yaffsfs_CheckPath(path) < 0){
1701                 yaffsfs_SetError(-ENAMETOOLONG);
1702                 return -1;
1703         }
1704
1705         yaffsfs_Lock();
1706
1707         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1708
1709         if(follow)
1710                 obj = yaffsfs_FollowLink(obj,0,&loop);
1711
1712         if(!dir && notDir) 
1713                 yaffsfs_SetError(-ENOTDIR);
1714         else if(loop) 
1715                 yaffsfs_SetError(-ELOOP);
1716         else if(!dir || !obj) 
1717                 yaffsfs_SetError(-ENOENT);
1718         else {
1719                 retVal = yaffs_list_xattrib(obj, data,size);
1720                 if(retVal< 0){
1721                         yaffsfs_SetError(retVal);
1722                         retVal = -1;
1723                 }
1724         }
1725
1726         yaffsfs_Unlock();
1727
1728         return retVal;
1729
1730 }
1731
1732 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1733 {
1734         return yaffs_do_listxattr(path, data, size, 1);
1735 }
1736
1737 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1738 {
1739         return yaffs_do_listxattr(path, data, size, 0);
1740 }
1741
1742 int yaffs_flistxattr(int fd, char *data, int size)
1743 {
1744         struct yaffs_obj *obj;
1745
1746         int retVal = -1;
1747
1748         yaffsfs_Lock();
1749         obj = yaffsfs_GetHandleObject(fd);
1750
1751         if(obj) {
1752                 retVal = yaffs_list_xattrib(obj,data,size);
1753                 if(retVal< 0){
1754                         yaffsfs_SetError(retVal);
1755                         retVal = -1;
1756                 }
1757         } else
1758                 /* bad handle */
1759                 yaffsfs_SetError(-EBADF);
1760
1761         yaffsfs_Unlock();
1762
1763         return retVal;
1764 }
1765
1766 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1767 {
1768         struct yaffs_obj *obj=NULL;
1769         struct yaffs_obj *dir=NULL;
1770         int notDir = 0;
1771         int loop = 0;
1772         int retVal = -1;
1773
1774         if(yaffsfs_CheckPath(path) < 0){
1775                 yaffsfs_SetError(-ENAMETOOLONG);
1776                 return -1;
1777         }
1778
1779         yaffsfs_Lock();
1780
1781         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
1782
1783         if(follow)
1784                 obj = yaffsfs_FollowLink(obj,0,&loop);
1785
1786         if(!dir && notDir) 
1787                 yaffsfs_SetError(-ENOTDIR);
1788         else if(loop) 
1789                 yaffsfs_SetError(-ELOOP);
1790         else if(!dir || !obj) 
1791                 yaffsfs_SetError(-ENOENT);
1792         else {
1793                 retVal = yaffs_remove_xattrib(obj,name);
1794                 if(retVal< 0){
1795                         yaffsfs_SetError(retVal);
1796                         retVal = -1;
1797                 }
1798         }
1799
1800         yaffsfs_Unlock();
1801
1802         return retVal;
1803
1804 }
1805
1806 int yaffs_removexattr(const YCHAR *path, const char *name)
1807 {
1808         return yaffs_do_removexattr(path, name, 1);
1809 }
1810
1811 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1812 {
1813         return yaffs_do_removexattr(path, name, 0);
1814 }
1815
1816 int yaffs_fremovexattr(int fd, const char *name)
1817 {
1818         struct yaffs_obj *obj;
1819
1820         int retVal = -1;
1821
1822         yaffsfs_Lock();
1823         obj = yaffsfs_GetHandleObject(fd);
1824
1825         if(obj){
1826                 retVal = yaffs_remove_xattrib(obj,name);
1827                 if(retVal< 0){
1828                         yaffsfs_SetError(retVal);
1829                         retVal = -1;
1830                 }
1831         }else
1832                 /* bad handle */
1833                 yaffsfs_SetError(-EBADF);
1834
1835         yaffsfs_Unlock();
1836
1837         return retVal;
1838 }
1839 #endif
1840
1841 #ifdef CONFIG_YAFFS_WINCE
1842 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1843 {
1844         struct yaffs_obj *obj;
1845
1846         int retVal = -1;
1847
1848         yaffsfs_Lock();
1849         obj = yaffsfs_GetHandleObject(fd);
1850
1851         if(obj){
1852
1853                 if(wctime){
1854                         wctime[0] = obj->win_ctime[0];
1855                         wctime[1] = obj->win_ctime[1];
1856                 }
1857                 if(watime){
1858                         watime[0] = obj->win_atime[0];
1859                         watime[1] = obj->win_atime[1];
1860                 }
1861                 if(wmtime){
1862                         wmtime[0] = obj->win_mtime[0];
1863                         wmtime[1] = obj->win_mtime[1];
1864                 }
1865
1866
1867                 retVal = 0;
1868         } else
1869                 /*  bad handle */
1870                 yaffsfs_SetError(-EBADF);               
1871         
1872         yaffsfs_Unlock();
1873         
1874         return retVal;
1875 }
1876
1877
1878 int yaffs_set_wince_times(int fd, 
1879                                                   const unsigned *wctime, 
1880                                                   const unsigned *watime, 
1881                                                   const unsigned *wmtime)
1882 {
1883         struct yaffs_obj *obj;
1884         int result;
1885         int retVal = -1;
1886
1887         yaffsfs_Lock();
1888         obj = yaffsfs_GetHandleObject(fd);
1889
1890         if(obj){
1891
1892                 if(wctime){
1893                         obj->win_ctime[0] = wctime[0];
1894                         obj->win_ctime[1] = wctime[1];
1895                 }
1896                 if(watime){
1897                         obj->win_atime[0] = watime[0];
1898                         obj->win_atime[1] = watime[1];
1899                 }
1900                 if(wmtime){
1901                         obj->win_mtime[0] = wmtime[0];
1902                         obj->win_mtime[1] = wmtime[1];
1903                 }
1904
1905                 obj->dirty = 1;
1906                 result = yaffs_flush_file(obj,0,0);
1907                 retVal = 0;
1908         } else
1909                 /* bad handle */
1910                 yaffsfs_SetError(-EBADF);
1911
1912         yaffsfs_Unlock();
1913
1914         return retVal;
1915 }
1916
1917 #endif
1918
1919
1920 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1921 {
1922         int result = -1;
1923
1924         if(obj)
1925                 obj = yaffs_get_equivalent_obj(obj);
1926
1927         if(obj) {
1928                 obj->yst_mode = mode;
1929                 obj->dirty = 1;
1930                 result = yaffs_flush_file(obj,0,0);
1931         }
1932
1933         return result == YAFFS_OK ? 0 : -1;
1934 }
1935
1936
1937 int yaffs_access(const YCHAR *path, int amode)
1938 {
1939         struct yaffs_obj *obj=NULL;
1940         struct yaffs_obj *dir=NULL;
1941         int notDir = 0;
1942         int loop = 0;
1943         int retval = -1;
1944
1945         if(yaffsfs_CheckPath(path) < 0){
1946                 yaffsfs_SetError(-ENAMETOOLONG);
1947                 return -1;
1948         }
1949
1950         if(amode & ~(R_OK | W_OK | X_OK)){
1951                 yaffsfs_SetError(-EINVAL);
1952                 return -1;
1953         }
1954
1955         yaffsfs_Lock();
1956
1957         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
1958         obj = yaffsfs_FollowLink(obj,0,&loop);
1959
1960         if(!dir && notDir) 
1961                 yaffsfs_SetError(-ENOTDIR);
1962         else if(loop) 
1963                 yaffsfs_SetError(-ELOOP);
1964         else if(!dir || !obj) 
1965                 yaffsfs_SetError(-ENOENT);
1966         else if((amode & W_OK) && obj->my_dev->read_only)
1967                 yaffsfs_SetError(-EROFS);
1968         else{
1969                 int access_ok = 1;
1970
1971                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1972                         access_ok = 0;
1973                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1974                         access_ok = 0;
1975                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1976                         access_ok = 0;
1977
1978                 if(!access_ok)
1979                         yaffsfs_SetError(-EACCES);
1980                 else
1981                         retval = 0;
1982         }
1983
1984         yaffsfs_Unlock();
1985
1986         return retval;
1987
1988 }
1989
1990
1991 int yaffs_chmod(const YCHAR *path, mode_t mode)
1992 {
1993         struct yaffs_obj *obj=NULL;
1994         struct yaffs_obj *dir=NULL;
1995         int retVal = -1;
1996         int notDir = 0;
1997         int loop = 0;
1998
1999         if(yaffsfs_CheckPath(path) < 0){
2000                 yaffsfs_SetError(-ENAMETOOLONG);
2001                 return -1;
2002         }
2003
2004         if(mode & ~(0777)){
2005                 yaffsfs_SetError(-EINVAL);
2006                 return -1;
2007         }
2008
2009         yaffsfs_Lock();
2010
2011         obj = yaffsfs_FindObject(NULL,path,0,1, &dir, &notDir,&loop);
2012
2013         if(!dir && notDir) 
2014                 yaffsfs_SetError(-ENOTDIR);
2015         else if(loop) 
2016                 yaffsfs_SetError(-ELOOP);
2017         else if(!dir || !obj) 
2018                 yaffsfs_SetError(-ENOENT);
2019         else if(obj->my_dev->read_only)
2020                 yaffsfs_SetError(-EROFS);
2021         else
2022                 retVal = yaffsfs_DoChMod(obj,mode);
2023
2024         yaffsfs_Unlock();
2025
2026         return retVal;
2027
2028 }
2029
2030
2031 int yaffs_fchmod(int fd, mode_t mode)
2032 {
2033         struct yaffs_obj *obj;
2034         int retVal = -1;
2035
2036         if(mode & ~(0777)){
2037                 yaffsfs_SetError(-EINVAL);
2038                 return -1;
2039         }
2040
2041         yaffsfs_Lock();
2042         obj = yaffsfs_GetHandleObject(fd);
2043
2044         if(!obj)
2045                 yaffsfs_SetError(-EBADF);
2046         else if(obj->my_dev->read_only)
2047                 yaffsfs_SetError(-EROFS);
2048         else
2049                 retVal = yaffsfs_DoChMod(obj,mode);
2050
2051         yaffsfs_Unlock();
2052
2053         return retVal;
2054 }
2055
2056 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2057 {
2058         struct yaffs_obj *parent = NULL;
2059         struct yaffs_obj *dir = NULL;
2060         YCHAR *name;
2061         YCHAR *alt_path = NULL;
2062         int retVal= -1;
2063         int notDir = 0;
2064         int loop = 0;
2065
2066         if(yaffsfs_CheckPath(path) < 0){
2067                 yaffsfs_SetError(-ENAMETOOLONG);
2068                 return -1;
2069         }
2070
2071         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2072                 yaffsfs_SetError(-ENOMEM);
2073                 return -1;
2074         }
2075         if(alt_path)
2076                 path = alt_path;
2077         
2078         yaffsfs_Lock();
2079         parent = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
2080         if(!parent && notDir)
2081                 yaffsfs_SetError(-ENOTDIR);
2082         else if(loop)
2083                 yaffsfs_SetError(-ELOOP);
2084         else if(!parent)
2085                 yaffsfs_SetError(-ENOENT);
2086         else if(yaffs_strnlen(name,5) == 0){
2087                 /* Trying to make the root itself */
2088                 yaffsfs_SetError(-EEXIST);
2089         } else if(parent->my_dev->read_only)
2090                 yaffsfs_SetError(-EROFS);
2091         else {
2092                 dir = yaffs_create_dir(parent,name,mode,0,0);
2093                 if(dir)
2094                         retVal = 0;
2095                 else if (yaffs_find_by_name(parent,name))
2096                         yaffsfs_SetError(-EEXIST); /* the name already exists */
2097                 else
2098                         yaffsfs_SetError(-ENOSPC); /* just assume no space */
2099         }
2100
2101         yaffsfs_Unlock();
2102
2103         if(alt_path)
2104                 YFREE(alt_path);
2105
2106         return retVal;
2107 }
2108
2109 int yaffs_rmdir(const YCHAR *path)
2110 {
2111         int result;
2112         YCHAR *alt_path;
2113
2114         if(yaffsfs_CheckPath(path) < 0){
2115                 yaffsfs_SetError(-ENAMETOOLONG);
2116                 return -1;
2117         }
2118
2119         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2120                 yaffsfs_SetError(-ENOMEM);
2121                 return -1;
2122         }
2123         if(alt_path)
2124                 path = alt_path;
2125         result =  yaffsfs_DoUnlink(path,1);
2126         if(alt_path)
2127                 YFREE(alt_path);
2128         return result;
2129 }
2130
2131
2132 void * yaffs_getdev(const YCHAR *path)
2133 {
2134         struct yaffs_dev *dev=NULL;
2135         YCHAR *dummy;
2136         dev = yaffsfs_FindDevice(path,&dummy);
2137         return (void *)dev;
2138 }
2139
2140 int yaffs_mount2(const YCHAR *path,int read_only)
2141 {
2142         int retVal=-1;
2143         int result=YAFFS_FAIL;
2144         struct yaffs_dev *dev=NULL;
2145
2146         T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2147
2148         if(yaffsfs_CheckPath(path) < 0){
2149                 yaffsfs_SetError(-ENAMETOOLONG);
2150                 return -1;
2151         }
2152
2153         yaffsfs_Lock();
2154
2155         yaffsfs_InitHandles();
2156
2157         dev = yaffsfs_FindMountPoint(path);
2158         if(dev){
2159                 if(!dev->is_mounted){
2160                         dev->read_only = read_only ? 1 : 0;
2161                         result = yaffs_guts_initialise(dev);
2162                         if(result == YAFFS_FAIL)
2163                                 yaffsfs_SetError(-ENOMEM);
2164                         retVal = result ? 0 : -1;
2165
2166                 }
2167                 else
2168                         yaffsfs_SetError(-EBUSY);
2169         } else
2170                 yaffsfs_SetError(-ENODEV);
2171
2172         yaffsfs_Unlock();
2173         return retVal;
2174
2175 }
2176
2177 int yaffs_mount(const YCHAR *path)
2178 {
2179         return yaffs_mount2(path,0);
2180 }
2181
2182 int yaffs_sync(const YCHAR *path)
2183 {
2184         int retVal=-1;
2185         struct yaffs_dev *dev=NULL;
2186         YCHAR *dummy;
2187
2188         if(yaffsfs_CheckPath(path) < 0){
2189                 yaffsfs_SetError(-ENAMETOOLONG);
2190                 return -1;
2191         }
2192         
2193         yaffsfs_Lock();
2194         dev = yaffsfs_FindDevice(path,&dummy);
2195         if(dev){
2196                 if(dev->is_mounted){
2197                         
2198                         yaffs_flush_whole_cache(dev);
2199                         yaffs_checkpoint_save(dev);
2200                         retVal = 0;
2201                         
2202                 } else
2203                         yaffsfs_SetError(-EINVAL);
2204                         
2205         }else
2206                 yaffsfs_SetError(-ENODEV);
2207
2208         yaffsfs_Unlock();
2209         return retVal;  
2210 }
2211
2212
2213 int yaffs_remount(const YCHAR *path, int force, int read_only)
2214 {
2215         int retVal=-1;
2216         struct yaffs_dev *dev=NULL;
2217         yaffsfs_Handle *yh;
2218
2219         if(yaffsfs_CheckPath(path) < 0){
2220                 yaffsfs_SetError(-ENAMETOOLONG);
2221                 return -1;
2222         }
2223
2224         yaffsfs_Lock();
2225         dev = yaffsfs_FindMountPoint(path);
2226         if(dev){
2227                 if(dev->is_mounted){
2228                         int i;
2229                         int inUse;
2230
2231                         yaffs_flush_whole_cache(dev);
2232
2233                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2234                                 yh = & yaffsfs_handle[i];
2235                                 if(yh->useCount>0 && 
2236                                         yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2237                                         inUse = 1; /* the device is in use, can't unmount */
2238                         }
2239
2240                         if(!inUse || force){
2241                                 if(read_only)
2242                                         yaffs_checkpoint_save(dev);
2243                                 dev->read_only =  read_only ? 1 : 0;
2244                                 retVal = 0;
2245                         } else
2246                                 yaffsfs_SetError(-EBUSY);
2247
2248                 } else
2249                         yaffsfs_SetError(-EINVAL);
2250
2251         }
2252         else
2253                 yaffsfs_SetError(-ENODEV);
2254
2255         yaffsfs_Unlock();
2256         return retVal;
2257
2258 }
2259
2260 int yaffs_unmount2(const YCHAR *path, int force)
2261 {
2262         int retVal=-1;
2263         struct yaffs_dev *dev=NULL;
2264
2265         if(yaffsfs_CheckPath(path) < 0){
2266                 yaffsfs_SetError(-ENAMETOOLONG);
2267                 return -1;
2268         }
2269
2270         yaffsfs_Lock();
2271         dev = yaffsfs_FindMountPoint(path);
2272         if(dev){
2273                 if(dev->is_mounted){
2274                         int i;
2275                         int inUse;
2276
2277                         yaffs_flush_whole_cache(dev);
2278                         yaffs_checkpoint_save(dev);
2279
2280                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2281                                 if(yaffsfs_handle[i].useCount > 0 && 
2282                                 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2283                                         inUse = 1; /* the device is in use, can't unmount */
2284                         }
2285
2286                         if(!inUse || force){
2287                                 if(inUse)
2288                                         yaffsfs_BreakDeviceHandles(dev);
2289                                 yaffs_deinitialise(dev);
2290
2291                                 retVal = 0;
2292                         } else
2293                                 yaffsfs_SetError(-EBUSY);
2294
2295                 } else
2296                         yaffsfs_SetError(-EINVAL);
2297
2298         } else
2299                 yaffsfs_SetError(-ENODEV);
2300
2301         yaffsfs_Unlock();
2302         return retVal;
2303
2304 }
2305
2306 int yaffs_unmount(const YCHAR *path)
2307 {
2308         return yaffs_unmount2(path,0);
2309 }
2310
2311 loff_t yaffs_freespace(const YCHAR *path)
2312 {
2313         loff_t retVal=-1;
2314         struct yaffs_dev *dev=NULL;
2315         YCHAR *dummy;
2316
2317         if(yaffsfs_CheckPath(path) < 0){
2318                 yaffsfs_SetError(-ENAMETOOLONG);
2319                 return -1;
2320         }
2321
2322         yaffsfs_Lock();
2323         dev = yaffsfs_FindDevice(path,&dummy);
2324         if(dev  && dev->is_mounted){
2325                 retVal = yaffs_get_n_free_chunks(dev);
2326                 retVal *= dev->data_bytes_per_chunk;
2327
2328         } else
2329                 yaffsfs_SetError(-EINVAL);
2330
2331         yaffsfs_Unlock();
2332         return retVal;
2333 }
2334
2335 loff_t yaffs_totalspace(const YCHAR *path)
2336 {
2337         loff_t retVal=-1;
2338         struct yaffs_dev *dev=NULL;
2339         YCHAR *dummy;
2340
2341         if(yaffsfs_CheckPath(path) < 0){
2342                 yaffsfs_SetError(-ENAMETOOLONG);
2343                 return -1;
2344         }
2345
2346         yaffsfs_Lock();
2347         dev = yaffsfs_FindDevice(path,&dummy);
2348         if(dev  && dev->is_mounted){
2349                 retVal = (dev->param.end_block - dev->param.start_block + 1) - 
2350                         dev->param.n_reserved_blocks;
2351                 retVal *= dev->param.chunks_per_block;
2352                 retVal *= dev->data_bytes_per_chunk;
2353
2354         } else
2355                 yaffsfs_SetError(-EINVAL);
2356
2357         yaffsfs_Unlock();
2358         return retVal;
2359 }
2360
2361 int yaffs_inodecount(const YCHAR *path)
2362 {
2363         loff_t retVal= -1;
2364         struct yaffs_dev *dev=NULL;
2365         YCHAR *dummy;
2366
2367         if(yaffsfs_CheckPath(path) < 0){
2368                 yaffsfs_SetError(-ENAMETOOLONG);
2369                 return -1;
2370         }
2371
2372         yaffsfs_Lock();
2373         dev = yaffsfs_FindDevice(path,&dummy);
2374         if(dev  && dev->is_mounted) {
2375            int n_obj = dev->n_obj;
2376            if(n_obj > dev->n_hardlinks)
2377                 retVal = n_obj - dev->n_hardlinks;
2378         }
2379         
2380         if(retVal < 0)
2381                 yaffsfs_SetError(-EINVAL);
2382         
2383         yaffsfs_Unlock();
2384         return retVal;  
2385 }
2386
2387
2388 void yaffs_add_device(struct yaffs_dev *dev)
2389 {
2390         dev->is_mounted = 0;
2391         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2392
2393         if(!dev->dev_list.next)
2394                 INIT_LIST_HEAD(&dev->dev_list);
2395
2396         list_add(&dev->dev_list,&yaffsfs_deviceList);
2397 }
2398
2399 void yaffs_remove_device(struct yaffs_dev *dev)
2400 {
2401         list_del_init(&dev->dev_list);
2402 }
2403
2404
2405
2406
2407 /* Directory search stuff. */
2408
2409 /*
2410  * Directory search context
2411  *
2412  * NB this is an opaque structure.
2413  */
2414
2415
2416 typedef struct
2417 {
2418         u32 magic;
2419         yaffs_dirent de;                /* directory entry being used by this dsc */
2420         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2421         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2422         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2423         int offset;
2424         struct list_head others;       
2425 } yaffsfs_DirectorySearchContext;
2426
2427
2428
2429 static struct list_head search_contexts;
2430
2431
2432 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2433 {
2434         if(dsc &&
2435            dsc->dirObj &&
2436            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2437
2438            dsc->offset = 0;
2439
2440            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2441                 dsc->nextReturn = NULL;
2442            else
2443                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2444                                                 struct yaffs_obj,siblings);
2445         } else {
2446                 /* Hey someone isn't playing nice! */
2447         }
2448 }
2449
2450 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2451 {
2452         if(dsc &&
2453            dsc->dirObj &&
2454            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2455
2456            if( dsc->nextReturn == NULL ||
2457                list_empty(&dsc->dirObj->variant.dir_variant.children))
2458                 dsc->nextReturn = NULL;
2459            else {
2460                    struct list_head *next = dsc->nextReturn->siblings.next;
2461
2462                    if( next == &dsc->dirObj->variant.dir_variant.children)
2463                         dsc->nextReturn = NULL; /* end of list */
2464                    else
2465                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2466            }
2467         } else {
2468                 /* Hey someone isn't playing nice! */
2469         }
2470 }
2471
2472 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2473 {
2474
2475         struct list_head *i;
2476         yaffsfs_DirectorySearchContext *dsc;
2477
2478         /* if search contexts not initilised then skip */
2479         if(!search_contexts.next)
2480                 return;
2481
2482         /* Iterate through the directory search contexts.
2483          * If any are the one being removed, then advance the dsc to
2484          * the next one to prevent a hanging ptr.
2485          */
2486          list_for_each(i, &search_contexts) {
2487                 if (i) {
2488                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2489                         if(dsc->nextReturn == obj)
2490                                 yaffsfs_DirAdvance(dsc);
2491                 }
2492         }
2493
2494 }
2495
2496 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2497 {
2498         yaffs_DIR *dir = NULL;
2499         struct yaffs_obj *obj = NULL;
2500         yaffsfs_DirectorySearchContext *dsc = NULL;
2501         int notDir = 0;
2502         int loop = 0;
2503
2504         if(yaffsfs_CheckPath(dirname) < 0){
2505                 yaffsfs_SetError(-ENAMETOOLONG);
2506                 return NULL;
2507         }
2508
2509         yaffsfs_Lock();
2510
2511         obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,&notDir,&loop);
2512
2513         if(!obj && notDir)
2514                 yaffsfs_SetError(-ENOTDIR);
2515         else if(loop)
2516                 yaffsfs_SetError(-ELOOP);
2517         else if(!obj)
2518                 yaffsfs_SetError(-ENOENT);
2519         else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2520                 yaffsfs_SetError(-ENOTDIR);
2521         else {
2522
2523                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2524                 dir = (yaffs_DIR *)dsc;
2525
2526                 if(dsc){
2527                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2528                         dsc->magic = YAFFS_MAGIC;
2529                         dsc->dirObj = obj;
2530                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2531                         INIT_LIST_HEAD(&dsc->others);
2532
2533                         if(!search_contexts.next)
2534                                 INIT_LIST_HEAD(&search_contexts);
2535
2536                         list_add(&dsc->others,&search_contexts);       
2537                         yaffsfs_SetDirRewound(dsc);
2538                 }
2539
2540         }
2541
2542         yaffsfs_Unlock();
2543
2544         return dir;
2545 }
2546
2547 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2548 {
2549         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2550         struct yaffs_dirent *retVal = NULL;
2551
2552         yaffsfs_Lock();
2553
2554         if(dsc && dsc->magic == YAFFS_MAGIC){
2555                 yaffsfs_SetError(0);
2556                 if(dsc->nextReturn){
2557                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2558                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2559                         dsc->de.d_off = dsc->offset++;
2560                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2561                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2562                         {
2563                                 /* this should not happen! */
2564                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2565                         }
2566                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2567                         retVal = &dsc->de;
2568                         yaffsfs_DirAdvance(dsc);
2569                 } else
2570                         retVal = NULL;
2571         } else
2572                 yaffsfs_SetError(-EBADF);
2573
2574         yaffsfs_Unlock();
2575
2576         return retVal;
2577
2578 }
2579
2580
2581 void yaffs_rewinddir(yaffs_DIR *dirp)
2582 {
2583         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2584
2585         yaffsfs_Lock();
2586
2587         yaffsfs_SetDirRewound(dsc);
2588
2589         yaffsfs_Unlock();
2590 }
2591
2592
2593 int yaffs_closedir(yaffs_DIR *dirp)
2594 {
2595         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2596
2597         yaffsfs_Lock();
2598         dsc->magic = 0;
2599         list_del(&dsc->others); /* unhook from list */
2600         YFREE(dsc);
2601         yaffsfs_Unlock();
2602         return 0;
2603 }
2604
2605 /* End of directory stuff */
2606
2607
2608 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2609 {
2610         struct yaffs_obj *parent = NULL;
2611         struct yaffs_obj *obj;
2612         YCHAR *name;
2613         int retVal= -1;
2614         int mode = 0; /* ignore for now */
2615         int notDir = 0;
2616         int loop = 0;
2617
2618         if(yaffsfs_CheckPath(newpath) < 0){
2619                 yaffsfs_SetError(-ENAMETOOLONG);
2620                 return -1;
2621         }
2622         yaffsfs_Lock();
2623         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,&notDir,&loop);
2624         if(!parent && notDir)
2625                 yaffsfs_SetError(-ENOTDIR);
2626         else if(loop)
2627                 yaffsfs_SetError(-ELOOP);
2628         else if( !parent || yaffs_strnlen(name,5) < 1)
2629                 yaffsfs_SetError(-ENOENT);
2630         else if(parent->my_dev->read_only)
2631                 yaffsfs_SetError(-EROFS);
2632         else if(parent){
2633                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2634                 if(obj)
2635                         retVal = 0;
2636                 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2637                         yaffsfs_SetError(-EEXIST);
2638                 else
2639                         yaffsfs_SetError(-ENOSPC);
2640         }
2641
2642         yaffsfs_Unlock();
2643
2644         return retVal;
2645
2646 }
2647
2648 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2649 {
2650         struct yaffs_obj *obj = NULL;
2651         struct yaffs_obj *dir = NULL;
2652         int retVal= -1;
2653         int notDir = 0;
2654         int loop = 0;
2655
2656         yaffsfs_Lock();
2657
2658         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
2659
2660         if(!dir && notDir) 
2661                 yaffsfs_SetError(-ENOTDIR);
2662         else if(loop) 
2663                 yaffsfs_SetError(-ELOOP);
2664         else if(!dir || !obj) 
2665                 yaffsfs_SetError(-ENOENT);
2666         else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2667                 yaffsfs_SetError(-EINVAL);
2668         else {
2669                 YCHAR *alias = obj->variant.symlink_variant.alias;
2670                 memset(buf,0,bufsiz);
2671                 yaffs_strncpy(buf,alias,bufsiz - 1);
2672                 retVal = 0;
2673         }
2674         yaffsfs_Unlock();
2675         return retVal;
2676 }
2677
2678 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2679 {
2680         /* Creates a link called newpath to existing oldpath */
2681         struct yaffs_obj *obj = NULL;
2682         struct yaffs_obj *lnk = NULL;
2683         struct yaffs_obj *obj_dir = NULL;
2684         struct yaffs_obj *lnk_dir = NULL;
2685         int retVal = -1;
2686         int notDirObj = 0;
2687         int notDirLnk = 0;
2688         int objLoop = 0;
2689         int lnkLoop = 0;
2690         YCHAR *newname;
2691
2692         if(yaffsfs_CheckPath(linkpath) < 0){
2693                 yaffsfs_SetError(-ENAMETOOLONG);
2694                 return -1;
2695         }
2696
2697         yaffsfs_Lock();
2698
2699         obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,&notDirObj,&objLoop);
2700         lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2701         lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,&notDirLnk,&lnkLoop);
2702
2703         if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2704                 yaffsfs_SetError(-ENOTDIR);
2705         else if(objLoop || lnkLoop)
2706                 yaffsfs_SetError(-ELOOP);
2707         else if(!obj_dir || !lnk_dir || !obj)
2708                 yaffsfs_SetError(-ENOENT);
2709         else if(obj->my_dev->read_only)
2710                 yaffsfs_SetError(-EINVAL);
2711         else if(lnk)
2712                 yaffsfs_SetError(-EEXIST);
2713         else if(lnk_dir->my_dev != obj->my_dev)
2714                 yaffsfs_SetError(-EXDEV);
2715         else {          
2716                 retVal = yaffsfs_CheckNameLength(newname);
2717                 
2718                 if(retVal == 0) {
2719                         lnk = yaffs_link_obj(lnk_dir,newname,obj);
2720                         if(lnk)
2721                                 retVal = 0;
2722                         else{
2723                                 yaffsfs_SetError(-ENOSPC);
2724                                 retVal = -1;
2725                         }
2726                 }
2727         }
2728         yaffsfs_Unlock();
2729
2730         return retVal;
2731 }
2732
2733 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2734 {
2735         pathname=pathname;
2736         mode=mode;
2737         dev=dev;
2738
2739         yaffsfs_SetError(-EINVAL);
2740         return -1;
2741 }
2742
2743
2744 /*
2745  * D E B U G   F U N C T I O N S
2746  */
2747  
2748 /*
2749  * yaffs_n_handles()
2750  * Returns number of handles attached to the object
2751  */
2752 int yaffs_n_handles(const YCHAR *path)
2753 {
2754         struct yaffs_obj *obj;
2755
2756         if(yaffsfs_CheckPath(path) < 0){
2757                 yaffsfs_SetError(-ENAMETOOLONG);
2758                 return -1;
2759         }
2760
2761         obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2762
2763         if(obj)
2764                 return yaffsfs_CountHandles(obj);
2765         else
2766                 return -1;
2767 }
2768
2769 int yaffs_get_error(void)
2770 {
2771         return yaffsfs_GetLastError();
2772 }
2773
2774 int yaffs_set_error(int error)
2775 {
2776         yaffsfs_SetError(error);
2777         return 0;
2778 }
2779
2780 int yaffs_dump_dev(const YCHAR *path)
2781 {
2782 #if 1
2783         path=path;
2784 #else
2785         YCHAR *rest;
2786
2787         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2788
2789         if(obj){
2790                 struct yaffs_dev *dev = obj->my_dev;
2791
2792                 printf("\n"
2793                            "n_page_writes.......... %d\n"
2794                            "n_page_reads........... %d\n"
2795                            "n_erasures....... %d\n"
2796                            "n_gc_copies............ %d\n"
2797                            "garbageCollections... %d\n"
2798                            "passiveGarbageColl'ns %d\n"
2799                            "\n",
2800                                 dev->n_page_writes,
2801                                 dev->n_page_reads,
2802                                 dev->n_erasures,
2803                                 dev->n_gc_copies,
2804                                 dev->garbageCollections,
2805                                 dev->passiveGarbageCollections
2806                 );
2807
2808         }
2809
2810 #endif
2811         return 0;
2812 }
2813