yaffs Added more tests to direct/timothy_tests/quick_tests
[yaffs2.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2010 Aleph One Ltd.
5  *   for Toby Churchill Ltd and Brightstar Engineering
6  *
7  * Created by Charles Manning <charles@aleph1.co.uk>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include "yaffsfs.h"
15 #include "yaffs_guts.h"
16 #include "yaffscfg.h"
17 #include "yportenv.h"
18 #include "yaffs_trace.h"
19
20 #include <string.h> /* for memset */
21
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
23
24 #ifndef NULL
25 #define NULL ((void *)0)
26 #endif
27
28
29 /* YAFFSFS_RW_SIZE must be a power of 2 */
30 #define YAFFSFS_RW_SHIFT (13)
31 #define YAFFSFS_RW_SIZE  (1<<YAFFSFS_RW_SHIFT)
32
33 /* Some forward references */
34 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
35                         const YCHAR *path,
36                         int symDepth, int getEquiv,
37                         struct yaffs_obj **dirOut,
38                         int *notDir, int *loop);
39
40 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
41
42 unsigned int yaffs_wr_attempts;
43
44 /*
45  * Handle management.
46  * There are open inodes in yaffsfs_Inode.
47  * There are open handles in yaffsfs_Handle.
48  *
49  * Things are structured this way to be like the Linux VFS model
50  * so that interactions with the yaffs guts calls are similar.
51  * That means more common code paths and less special code.
52  * That means better testing etc.
53  */
54
55 typedef struct {
56         int count;      /* Number of handles accessing this inode */
57         struct yaffs_obj *iObj;
58 } yaffsfs_Inode;
59
60 typedef struct{
61         u8      reading:1;
62         u8      writing:1;
63         u8      append:1;
64         u8      shareRead:1;
65         u8      shareWrite:1;
66         int     inodeId:12;     /* Index to corresponding yaffsfs_Inode */
67         int     useCount:10;    /* Use count for this handle */
68         u32 position;           /* current position in file */
69 }yaffsfs_Handle;
70
71 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
72 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
73 static int yaffsfs_handlesInitialised;
74
75
76 unsigned yaffs_set_trace(unsigned  tm) 
77 {
78         yaffs_trace_mask = tm;
79         return yaffs_trace_mask;
80 }
81
82 unsigned yaffs_get_trace(void)
83 {
84         return yaffs_trace_mask;
85 }
86
87 /*
88  * yaffsfs_InitHandle
89  * Inilitalise handle management on start-up.
90  */
91
92 static void yaffsfs_InitHandles(void)
93 {
94         int i;
95         if(yaffsfs_handlesInitialised)
96                 return;
97
98         memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
99         memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
100         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
101                 yaffsfs_handle[i].inodeId = -1;
102 }
103
104 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
105 {
106         if(h < 0 || h >= YAFFSFS_N_HANDLES)
107                 return NULL;
108
109         return &yaffsfs_handle[h];
110 }
111
112 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
113 {
114         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
115
116         if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
117                 return  &yaffsfs_inode[h->inodeId];
118
119         return NULL;
120 }
121
122 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
123 {
124         yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
125
126         if(in)
127                 return in->iObj;
128
129         return NULL;
130 }
131
132 /*
133  * yaffsfs_FindInodeIdForObject
134  * Find the inode entry for an object, if it exists.
135  */
136
137 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
138 {
139         int i;
140         int ret = -1;
141         
142         if(obj)
143                 obj = yaffs_get_equivalent_obj(obj);
144
145         /* Look for it in open inode table*/
146         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
147                 if(yaffsfs_inode[i].iObj == obj)
148                         ret = i;
149         }
150         return ret;
151 }
152
153 /*
154  * yaffsfs_GetInodeIdForObject
155  * Grab an inode entry when opening a new inode.
156  */
157 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
158 {
159         int i;
160         int ret;
161         yaffsfs_Inode *in = NULL;
162         
163         if(obj)
164                 obj = yaffs_get_equivalent_obj(obj);
165
166         ret = yaffsfs_FindInodeIdForObject(obj);
167
168         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
169                 if(!yaffsfs_inode[i].iObj)
170                         ret = i;
171         }
172
173         if(ret>=0){
174                 in = &yaffsfs_inode[ret];
175                 if(!in->iObj)
176                         in->count = 0;
177                 in->iObj = obj;
178                 in->count++;
179         }
180         
181         
182         return ret;
183 }
184
185
186 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
187 {
188         int i = yaffsfs_FindInodeIdForObject(obj);
189
190         if(i >= 0)
191                 return yaffsfs_inode[i].count;
192         else
193                 return 0;
194 }
195
196 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
197 {
198         struct yaffs_obj *obj;
199         
200         obj = in->iObj;
201
202         if(obj->unlinked)
203                 yaffs_del_obj(obj);
204         
205         obj->my_inode = NULL;
206         in->iObj = NULL;
207
208 }
209
210 static void yaffsfs_PutInode(int inodeId)
211 {
212         if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
213                 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
214                 in->count--;
215                 if(in->count <= 0){
216                         yaffsfs_ReleaseInode(in);
217                         in->count = 0;
218                 }
219         }       
220 }
221
222
223 /*
224  * yaffsfs_GetHandle
225  * Grab a handle (when opening a file)
226  */
227
228 static int yaffsfs_GetNewHandle(void)
229 {
230         int i;
231         yaffsfs_Handle *h;
232
233         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
234                 h = yaffsfs_GetHandlePointer(i);
235                 if(!h){
236                         /* todo bug: should never happen */
237                 }
238                 if(h->useCount < 1){
239                         memset(h,0,sizeof(yaffsfs_Handle));
240                         h->inodeId=-1;
241                         h->useCount=1;
242                         return i;
243                 }
244         }
245         return -1;
246 }
247
248 /*
249  * yaffs_get_handle
250  * Increase use of handle when reading/writing a file
251  */
252 static int yaffsfs_GetHandle(int handle)
253 {
254         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
255
256         if(h && h->useCount > 0){       
257                 h->useCount++;
258         }
259         return 0;
260 }
261
262 /*
263  * yaffs_put_handle
264  * Let go of a handle when closing a file or aborting an open or
265  * ending a read or write.
266  */
267 static int yaffsfs_PutHandle(int handle)
268 {
269         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
270
271         if(h && h->useCount > 0){       
272                 h->useCount--;
273                 if(h->useCount < 1){
274                         if(h->inodeId >= 0){
275                                 yaffsfs_PutInode(h->inodeId);
276                                 h->inodeId = -1;
277                         }
278                 }
279         }
280         return 0;
281 }
282
283 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
284 {
285         yaffsfs_Handle *h;
286         struct yaffs_obj *obj;
287         int i;
288         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
289                 h = yaffsfs_GetHandlePointer(i);
290                 obj = yaffsfs_GetHandleObject(i);
291                 if(h && h->useCount>0 && obj && obj->my_dev == dev){
292                         h->useCount = 0;
293                         yaffsfs_PutInode(h->inodeId);
294                         h->inodeId = -1;
295                 }
296         }
297 }
298
299
300
301
302 /*
303  *  Stuff to handle names.
304  */
305
306
307 int yaffsfs_Match(YCHAR a, YCHAR b)
308 {
309         /* case sensitive */
310         return (a == b);
311 }
312
313 int yaffsfs_IsPathDivider(YCHAR ch)
314 {
315         const YCHAR *str = YAFFS_PATH_DIVIDERS;
316
317         while(*str){
318                 if(*str == ch)
319                         return 1;
320                 str++;
321         }
322
323         return 0;
324 }
325
326 int yaffsfs_CheckNameLength(const char *name)
327 {
328         int retVal = 0;         
329
330         int nameLength = yaffs_strnlen(name,YAFFS_MAX_NAME_LENGTH+1);
331                 
332         if(nameLength == 0){
333                 yaffsfs_SetError(-ENOENT);
334                 retVal = -1;
335         } else if (nameLength > YAFFS_MAX_NAME_LENGTH){
336                 yaffsfs_SetError(-ENAMETOOLONG);
337                 retVal = -1;
338         }
339
340         return retVal;  
341 }
342
343
344 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
345 {
346         YCHAR *alt_path = NULL;
347         int path_length;
348         int i;
349
350         /*
351          * We don't have a definition for max path length.
352          * We will use 3 * max name length instead.
353          */
354         *ret_path = NULL;
355         path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
356
357         /* If the last character is a path divider, then we need to
358          * trim it back so that the name look-up works properly.
359          * eg. /foo/new_dir/ -> /foo/newdir
360          * Curveball: Need to handle multiple path dividers:
361          * eg. /foof/sdfse///// -> /foo/sdfse
362          */
363         if(path_length > 0 && 
364                 yaffsfs_IsPathDivider(path[path_length-1])){
365                 alt_path = YMALLOC(path_length + 1);
366                 if(!alt_path)
367                         return -1;
368                 strcpy(alt_path, path);
369                 for(i = path_length-1;
370                         i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
371                         i--)
372                         alt_path[i] = (YCHAR) 0;
373         }
374         *ret_path = alt_path;
375         return 0;
376 }
377
378
379 LIST_HEAD(yaffsfs_deviceList);
380
381 /*
382  * yaffsfs_FindDevice
383  * yaffsfs_FindRoot
384  * Scan the configuration list to find the device
385  * Curveballs: Should match paths that end in '/' too
386  * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
387  */
388 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
389 {
390         struct list_head *cfg;
391         const YCHAR *leftOver;
392         const YCHAR *p;
393         struct yaffs_dev *retval = NULL;
394         struct yaffs_dev *dev = NULL;
395         int thisMatchLength;
396         int longestMatch = -1;
397         int matching;
398
399         /*
400          * Check all configs, choose the one that:
401          * 1) Actually matches a prefix (ie /a amd /abc will not match
402          * 2) Matches the longest.
403          */
404         list_for_each(cfg, &yaffsfs_deviceList){
405                 dev = list_entry(cfg, struct yaffs_dev, dev_list);
406                 leftOver = path;
407                 p = dev->param.name;
408                 thisMatchLength = 0;
409                 matching = 1;
410
411
412                 while(matching && *p && *leftOver){
413                         /* Skip over any /s */
414                         while(yaffsfs_IsPathDivider(*p))
415                               p++;
416
417                         /* Skip over any /s */
418                         while(yaffsfs_IsPathDivider(*leftOver))
419                               leftOver++;
420
421                         /* Now match the text part */
422                         while(matching &&
423                               *p && !yaffsfs_IsPathDivider(*p) &&
424                               *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
425                                 if(yaffsfs_Match(*p,*leftOver)){
426                                         p++;
427                                         leftOver++;
428                                         thisMatchLength++;
429                                 } else {
430                                         matching = 0;
431                                 }
432                         }
433                 }
434
435                 /* Skip over any /s in leftOver */
436                 while(yaffsfs_IsPathDivider(*leftOver))
437                       leftOver++;
438
439                 // Skip over any /s in p
440                 while(yaffsfs_IsPathDivider(*p))
441                       p++;
442
443                 // p should now be at the end of the string (ie. fully matched)
444                 if(*p)
445                         matching = 0;
446
447                 if( matching && (thisMatchLength > longestMatch))
448                 {
449                         // Matched prefix
450                         *restOfPath = (YCHAR *)leftOver;
451                         retval = dev;
452                         longestMatch = thisMatchLength;
453                 }
454
455         }
456         return retval;
457 }
458
459 static int yaffsfs_CheckPath(const YCHAR *path)
460 {
461         int n=0;
462         int divs=0;
463         while(*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100){
464                 if(yaffsfs_IsPathDivider(*path)){
465                         n=0;
466                         divs++;
467                 } else
468                         n++;
469                 path++;
470         }
471         
472         return (*path) ? -1 : 0;
473 }
474
475 /* FindMountPoint only returns a dev entry if the path is a mount point */
476 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
477 {
478         struct yaffs_dev *dev;
479         YCHAR *restOfPath=NULL;
480         dev = yaffsfs_FindDevice(path,&restOfPath);
481         if(dev && restOfPath && *restOfPath)
482                 dev = NULL;
483         return dev;
484 }
485
486 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
487 {
488
489         struct yaffs_dev *dev;
490
491         dev= yaffsfs_FindDevice(path,restOfPath);
492         if(dev && dev->is_mounted){
493                 return dev->root_dir;
494         }
495         return NULL;
496 }
497
498 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
499                                         int symDepth, int *loop)
500 {
501
502         if(obj)
503                 obj = yaffs_get_equivalent_obj(obj);
504
505         while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
506                 YCHAR *alias = obj->variant.symlink_variant.alias;
507
508                 if(yaffsfs_IsPathDivider(*alias))
509                         /* Starts with a /, need to scan from root up */
510                         obj = yaffsfs_FindObject(NULL,alias,symDepth++,
511                                                 1,NULL,NULL,loop);
512                 else
513                         /* Relative to here, so use the parent of the symlink as a start */
514                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,
515                                                 1,NULL,NULL,loop);
516         }
517         return obj;
518 }
519
520
521 /*
522  * yaffsfs_FindDirectory
523  * Parse a path to determine the directory and the name within the directory.
524  *
525  * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
526  */
527 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
528                                 const YCHAR *path, YCHAR **name, int symDepth,
529                                 int *notDir,int *loop)
530 {
531         struct yaffs_obj *dir;
532         YCHAR *restOfPath;
533         YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
534         int i;
535
536         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES){
537                 if(loop)
538                         *loop = 1;
539                 return NULL;
540         }
541
542         if(startDir){
543                 dir = startDir;
544                 restOfPath = (YCHAR *)path;
545         }
546         else
547                 dir = yaffsfs_FindRoot(path,&restOfPath);
548
549
550         while(dir){
551                 /*
552                  * parse off /.
553                  * curve ball: also throw away surplus '/'
554                  * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
555                  */
556                 while(yaffsfs_IsPathDivider(*restOfPath))
557                         restOfPath++; /* get rid of '/' */
558
559                 *name = restOfPath;
560                 i = 0;
561
562                 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
563                         if (i < YAFFS_MAX_NAME_LENGTH){
564                                 str[i] = *restOfPath;
565                                 str[i+1] = '\0';
566                                 i++;
567                         }
568                         restOfPath++;
569                 }
570
571                 if(!*restOfPath)
572                         /* got to the end of the string */
573                         return dir;
574                 else{
575                         if(yaffs_strcmp(str,_Y(".")) == 0)
576                         {
577                                 /* Do nothing */
578                         }
579                         else if(yaffs_strcmp(str,_Y("..")) == 0)
580                                 dir = dir->parent;
581                         else{
582                                 dir = yaffs_find_by_name(dir,str);
583
584                                 dir = yaffsfs_FollowLink(dir,symDepth,loop);
585
586                                 if(dir && dir->variant_type != 
587                                         YAFFS_OBJECT_TYPE_DIRECTORY){
588                                         if(notDir)
589                                                 *notDir = 1;
590                                         dir = NULL;
591                                 }
592                                 
593                         }
594                 }
595         }
596         /* directory did not exist. */
597         return NULL;
598 }
599
600 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
601                                         const YCHAR *path,
602                                         YCHAR **name,
603                                         int symDepth,
604                                         int *notDir,
605                                         int *loop)
606 {
607         return yaffsfs_DoFindDirectory(relDir,path,name,symDepth,notDir,loop);
608 }
609
610 /*
611  * yaffsfs_FindObject turns a path for an existing object into the object
612  */
613 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
614                         const YCHAR *path,int symDepth, int getEquiv,
615                         struct yaffs_obj **dirOut, int *notDir,int *loop)
616 {
617         struct yaffs_obj *dir;
618         struct yaffs_obj *obj;
619         YCHAR *name;
620
621         dir = yaffsfs_FindDirectory(relDir,path,&name,symDepth,notDir,loop);
622
623         if(dirOut)
624                 *dirOut =  dir;
625
626         if(dir && *name)
627                 obj = yaffs_find_by_name(dir,name);
628         else
629                 obj = dir;
630
631         if(getEquiv)
632                 obj = yaffs_get_equivalent_obj(obj);
633
634         return obj;
635 }
636
637
638 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
1187         if(!dir && notDir)
1188                 yaffsfs_SetError(-ENOTDIR);
1189         else if(loop)
1190                 yaffsfs_SetError(-ELOOP);
1191         else if(!dir || !obj)
1192                 yaffsfs_SetError(-ENOENT);
1193         else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1194                 yaffsfs_SetError(-EISDIR);
1195         else if(obj->my_dev->read_only)
1196                 yaffsfs_SetError(-EACCES);
1197         else if(new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1198                 yaffsfs_SetError(-EINVAL);
1199         else
1200                 result = yaffs_resize_file(obj,new_size);
1201
1202         yaffsfs_Unlock();
1203
1204         return (result) ? 0 : -1;
1205 }
1206
1207 int yaffs_ftruncate(int fd, off_t new_size)
1208 {
1209         yaffsfs_Handle *h = NULL;
1210         struct yaffs_obj *obj = NULL;
1211         int result = 0;
1212
1213         yaffsfs_Lock();
1214         h = yaffsfs_GetHandlePointer(fd);
1215         obj = yaffsfs_GetHandleObject(fd);
1216
1217         if(!h || !obj)
1218                 /* bad handle */
1219                 yaffsfs_SetError(-EBADF);
1220         else if(obj->my_dev->read_only)
1221                 yaffsfs_SetError(-EACCES);
1222         else if( new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1223                 yaffsfs_SetError(-EINVAL);
1224         else
1225                 /* resize the file */
1226                 result = yaffs_resize_file(obj,new_size);
1227         yaffsfs_Unlock();
1228
1229         return (result) ? 0 : -1;
1230
1231 }
1232
1233 off_t yaffs_lseek(int fd, off_t offset, int whence)
1234 {
1235         yaffsfs_Handle *h = NULL;
1236         struct yaffs_obj *obj = NULL;
1237         int pos = -1;
1238         int fSize = -1;
1239
1240         yaffsfs_Lock();
1241         h = yaffsfs_GetHandlePointer(fd);
1242         obj = yaffsfs_GetHandleObject(fd);
1243
1244         if(!h || !obj)
1245                 yaffsfs_SetError(-EBADF);
1246         else if(offset > YAFFS_MAX_FILE_SIZE)
1247                 yaffsfs_SetError(-EINVAL);
1248         else {
1249                 if(whence == SEEK_SET){
1250                         if(offset >= 0)
1251                                 pos = offset;
1252                 } else if(whence == SEEK_CUR) {
1253                         if( (h->position + offset) >= 0)
1254                                 pos = (h->position + offset);
1255                 } else if(whence == SEEK_END) {
1256                         fSize = yaffs_get_obj_length(obj);
1257                         if(fSize >= 0 && (fSize + offset) >= 0)
1258                                 pos = fSize + offset;
1259                 } 
1260
1261                 if(pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1262                         h->position = pos;
1263                 else{
1264                         yaffsfs_SetError(-EINVAL);
1265                         pos = -1;
1266                 }
1267         }
1268
1269         yaffsfs_Unlock();
1270
1271         return pos;
1272 }
1273
1274
1275 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1276 {
1277         struct yaffs_obj *dir = NULL;
1278         struct yaffs_obj *obj = NULL;
1279         YCHAR *name;
1280         int result = YAFFS_FAIL;
1281         int notDir = 0;
1282         int loop = 0;
1283
1284         if(yaffsfs_CheckPath(path) < 0){
1285                 yaffsfs_SetError(-ENAMETOOLONG);
1286                 return -1;
1287         }
1288
1289         yaffsfs_Lock();
1290
1291         obj = yaffsfs_FindObject(NULL,path,0,0,NULL,NULL,NULL);
1292         dir = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
1293
1294         if(!dir && notDir)
1295                 yaffsfs_SetError(-ENOTDIR);
1296         else if(loop)
1297                 yaffsfs_SetError(-ELOOP);
1298         else if(!dir)
1299                 yaffsfs_SetError(-ENOENT);
1300         else if(yaffs_strncmp(name,_Y("."),2) == 0)
1301                 yaffsfs_SetError(-EINVAL);
1302         else if(!obj)
1303                 yaffsfs_SetError(-ENOENT);
1304         else if(obj->my_dev->read_only)
1305                 yaffsfs_SetError(-EINVAL);
1306         else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1307                 yaffsfs_SetError(-EISDIR);
1308         else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1309                 yaffsfs_SetError(-ENOTDIR);
1310         else if(isDirectory && obj == obj->my_dev->root_dir)
1311                 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1312         else {
1313                 result = yaffs_unlinker(dir,name);
1314
1315                 if(result == YAFFS_FAIL && isDirectory)
1316                         yaffsfs_SetError(-ENOTEMPTY);
1317         }
1318
1319         yaffsfs_Unlock();
1320
1321         return (result == YAFFS_FAIL) ? -1 : 0;
1322 }
1323
1324
1325 int yaffs_unlink(const YCHAR *path)
1326 {
1327         return yaffsfs_DoUnlink(path,0);
1328 }
1329
1330 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1331 {
1332         struct yaffs_obj *olddir = NULL;
1333         struct yaffs_obj *newdir = NULL;
1334         struct yaffs_obj *obj = NULL;
1335         struct yaffs_obj *newobj = NULL;
1336         YCHAR *oldname;
1337         YCHAR *newname;
1338         int result= YAFFS_FAIL;
1339         int rename_allowed = 1;
1340         int notOldDir = 0;
1341         int notNewDir = 0;
1342         int oldLoop = 0;
1343         int newLoop = 0;
1344
1345         YCHAR *alt_newpath=NULL;
1346
1347         if(yaffsfs_CheckPath(newPath) < 0){
1348                 yaffsfs_SetError(-ENAMETOOLONG);
1349                 return -1;
1350         }
1351
1352         if(yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0){
1353                 yaffsfs_SetError(-ENOMEM);
1354                 return -1;
1355         }
1356         if(alt_newpath)
1357                 newPath = alt_newpath;
1358
1359         yaffsfs_Lock();
1360
1361
1362         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0,&notOldDir,&oldLoop);
1363         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0,&notNewDir,&newLoop);
1364         obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL,NULL,NULL);
1365         newobj = yaffsfs_FindObject(NULL,newPath,0,0,NULL,NULL,NULL);
1366
1367         /* If the object being renamed is a directory and the 
1368          * path ended with a "/" then the olddir == obj.
1369          * We pass through NULL for the old name to tell the lower layers
1370          * to use olddir as the object.
1371          */
1372
1373         if(olddir == obj)
1374                 oldname = NULL;
1375
1376         if((!olddir && notOldDir) || (!newdir && notNewDir)) {
1377                 yaffsfs_SetError(-ENOTDIR);
1378                 rename_allowed = 0;
1379         } else if(oldLoop || newLoop) {
1380                 yaffsfs_SetError(-ELOOP);
1381                 rename_allowed = 0;
1382         } else if (olddir && oldname && yaffs_strncmp(oldname, _Y("."),2) == 0){
1383                 yaffsfs_SetError(-EINVAL);
1384                 rename_allowed = 0;
1385         }else if(!olddir || !newdir || !obj) {
1386                 yaffsfs_SetError(-ENOENT);
1387                 rename_allowed = 0;
1388         } else if(obj->my_dev->read_only){
1389                 yaffsfs_SetError(-EROFS);
1390                 rename_allowed = 0;
1391         } else if(yaffs_is_non_empty_dir(newobj)){
1392                 yaffsfs_SetError(-ENOTEMPTY);
1393                 rename_allowed = 0;
1394         } else if(olddir->my_dev != newdir->my_dev) {
1395                 /* Rename must be on same device */
1396                 yaffsfs_SetError(-EXDEV);
1397                 rename_allowed = 0;
1398         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1399                 /*
1400                  * It is a directory, check that it is not being renamed to
1401                  * being its own decendent.
1402                  * Do this by tracing from the new directory back to the root, 
1403                  * checking for obj
1404                  */
1405
1406                 struct yaffs_obj *xx = newdir;
1407
1408                 while( rename_allowed && xx){
1409                         if(xx == obj)
1410                                 rename_allowed = 0;
1411                         xx = xx->parent;
1412                 }
1413                 if(!rename_allowed)
1414                         yaffsfs_SetError(-EINVAL);
1415         }
1416
1417         if(rename_allowed)
1418                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1419
1420         yaffsfs_Unlock();
1421
1422         if(alt_newpath)
1423                 YFREE(alt_newpath);
1424
1425         return (result == YAFFS_FAIL) ? -1 : 0;
1426 }
1427
1428
1429 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1430 {
1431         int retVal = -1;
1432
1433         obj = yaffs_get_equivalent_obj(obj);
1434
1435         if(obj && buf){
1436                 buf->st_dev = (int)obj->my_dev->os_context;
1437                 buf->st_ino = obj->obj_id;
1438                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1439
1440                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1441                         buf->st_mode |= S_IFDIR;
1442                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1443                         buf->st_mode |= S_IFLNK;
1444                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1445                         buf->st_mode |= S_IFREG;
1446
1447                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1448                 buf->st_uid = 0;
1449                 buf->st_gid = 0;;
1450                 buf->st_rdev = obj->yst_rdev;
1451                 buf->st_size = yaffs_get_obj_length(obj);
1452                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1453                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1454 #if CONFIG_YAFFS_WINCE
1455                 buf->yst_wince_atime[0] = obj->win_atime[0];
1456                 buf->yst_wince_atime[1] = obj->win_atime[1];
1457                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1458                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1459                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1460                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1461 #else
1462                 buf->yst_atime = obj->yst_atime;
1463                 buf->yst_ctime = obj->yst_ctime;
1464                 buf->yst_mtime = obj->yst_mtime;
1465 #endif
1466                 retVal = 0;
1467         }
1468         return retVal;
1469 }
1470
1471 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1472 {
1473         struct yaffs_obj *obj=NULL;
1474         struct yaffs_obj *dir=NULL;
1475         int retVal = -1;
1476         int notDir = 0;
1477         int loop = 0;
1478
1479         if(yaffsfs_CheckPath(path) < 0){
1480                 yaffsfs_SetError(-ENAMETOOLONG);
1481                 return -1;
1482         }
1483
1484         yaffsfs_Lock();
1485
1486         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1487
1488         if(!doLStat && obj)
1489                 obj = yaffsfs_FollowLink(obj,0,&loop);
1490
1491         if(!dir && notDir)
1492                 yaffsfs_SetError(-ENOTDIR);
1493         else if(loop)
1494                 yaffsfs_SetError(-ELOOP);
1495         else if(!dir || !obj)
1496                 yaffsfs_SetError(-ENOENT);
1497         else
1498                 retVal = yaffsfs_DoStat(obj,buf);
1499
1500         yaffsfs_Unlock();
1501
1502         return retVal;
1503
1504 }
1505
1506 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1507 {
1508         return yaffsfs_DoStatOrLStat(path,buf,0);
1509 }
1510
1511 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1512 {
1513         return yaffsfs_DoStatOrLStat(path,buf,1);
1514 }
1515
1516 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1517 {
1518         struct yaffs_obj *obj;
1519
1520         int retVal = -1;
1521
1522         yaffsfs_Lock();
1523         obj = yaffsfs_GetHandleObject(fd);
1524
1525         if(obj)
1526                 retVal = yaffsfs_DoStat(obj,buf);
1527         else
1528                 /* bad handle */
1529                 yaffsfs_SetError(-EBADF);
1530
1531         yaffsfs_Unlock();
1532
1533         return retVal;
1534 }
1535
1536 #ifndef CONFIG_YAFFS_WINCE
1537 /* xattrib functions */
1538
1539
1540 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1541 {
1542         struct yaffs_obj *obj;
1543         struct yaffs_obj *dir;
1544         int notDir = 0;
1545         int loop = 0;
1546
1547         int retVal = -1;
1548
1549         if(yaffsfs_CheckPath(path) < 0){
1550                 yaffsfs_SetError(-ENAMETOOLONG);
1551                 return -1;
1552         }
1553
1554         yaffsfs_Lock();
1555
1556         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1557
1558         if(follow)
1559                 obj = yaffsfs_FollowLink(obj,0,&loop);
1560
1561         if(!dir && notDir) 
1562                 yaffsfs_SetError(-ENOTDIR);
1563         else if(loop) 
1564                 yaffsfs_SetError(-ELOOP);
1565         else if(!dir || !obj) 
1566                 yaffsfs_SetError(-ENOENT);
1567         else {
1568                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1569                 if(retVal< 0){
1570                         yaffsfs_SetError(retVal);
1571                         retVal = -1;
1572                 }
1573         }
1574
1575         yaffsfs_Unlock();
1576
1577         return retVal;
1578
1579 }
1580
1581 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1582 {
1583         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1584 }
1585
1586 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1587 {
1588         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1589 }
1590
1591
1592
1593 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1594 {
1595         struct yaffs_obj *obj;
1596
1597         int retVal = -1;
1598
1599         yaffsfs_Lock();
1600         obj = yaffsfs_GetHandleObject(fd);
1601
1602         if(!obj) 
1603                 yaffsfs_SetError(-EBADF);
1604         else {
1605                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1606                 if(retVal< 0){
1607                         yaffsfs_SetError(retVal);
1608                         retVal = -1;
1609                 }
1610         }
1611
1612         yaffsfs_Unlock();
1613
1614         return retVal;
1615 }
1616
1617 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1618 {
1619         struct yaffs_obj *obj;
1620         struct yaffs_obj *dir;
1621         int retVal = -1;
1622         int notDir = 0;
1623         int loop = 0;
1624
1625         if(yaffsfs_CheckPath(path) < 0){
1626                 yaffsfs_SetError(-ENAMETOOLONG);
1627                 return -1;
1628         }
1629
1630         yaffsfs_Lock();
1631
1632         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1633
1634         if(follow)
1635                 obj = yaffsfs_FollowLink(obj,0,&loop);
1636
1637         if(!dir && notDir) 
1638                 yaffsfs_SetError(-ENOTDIR);
1639         else if(loop) 
1640                 yaffsfs_SetError(-ELOOP);
1641         else if(!dir || !obj) 
1642                 yaffsfs_SetError(-ENOENT);
1643         else {
1644                 retVal = yaffs_get_xattrib(obj,name,data,size);
1645                 if(retVal< 0){
1646                         yaffsfs_SetError(retVal);
1647                         retVal = -1;
1648                 }
1649         }
1650         yaffsfs_Unlock();
1651
1652         return retVal;
1653
1654 }
1655
1656 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1657 {
1658         return yaffs_do_getxattr( path, name, data, size, 1);
1659 }
1660 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1661 {
1662         return yaffs_do_getxattr( path, name, data, size, 0);
1663 }
1664
1665
1666
1667 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1668 {
1669         struct yaffs_obj *obj;
1670
1671         int retVal = -1;
1672
1673         yaffsfs_Lock();
1674         obj = yaffsfs_GetHandleObject(fd);
1675
1676         if(obj) {
1677                 retVal = yaffs_get_xattrib(obj,name,data,size);
1678                 if(retVal< 0){
1679                         yaffsfs_SetError(retVal);
1680                         retVal = -1;
1681                 }
1682         } else
1683                 /* bad handle */
1684                 yaffsfs_SetError(-EBADF);
1685
1686         yaffsfs_Unlock();
1687
1688         return retVal;
1689 }
1690
1691 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1692 {
1693         struct yaffs_obj *obj=NULL;
1694         struct yaffs_obj *dir=NULL;
1695         int retVal = -1;
1696         int notDir = 0;
1697         int loop = 0;
1698
1699         if(yaffsfs_CheckPath(path) < 0){
1700                 yaffsfs_SetError(-ENAMETOOLONG);
1701                 return -1;
1702         }
1703
1704         yaffsfs_Lock();
1705
1706         obj = yaffsfs_FindObject(NULL,path,0,1,&dir,&notDir,&loop);
1707
1708         if(follow)
1709                 obj = yaffsfs_FollowLink(obj,0,&loop);
1710
1711         if(!dir && notDir) 
1712                 yaffsfs_SetError(-ENOTDIR);
1713         else if(loop) 
1714                 yaffsfs_SetError(-ELOOP);
1715         else if(!dir || !obj) 
1716                 yaffsfs_SetError(-ENOENT);
1717         else {
1718                 retVal = yaffs_list_xattrib(obj, data,size);
1719                 if(retVal< 0){
1720                         yaffsfs_SetError(retVal);
1721                         retVal = -1;
1722                 }
1723         }
1724
1725         yaffsfs_Unlock();
1726
1727         return retVal;
1728
1729 }
1730
1731 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1732 {
1733         return yaffs_do_listxattr(path, data, size, 1);
1734 }
1735
1736 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1737 {
1738         return yaffs_do_listxattr(path, data, size, 0);
1739 }
1740
1741 int yaffs_flistxattr(int fd, char *data, int size)
1742 {
1743         struct yaffs_obj *obj;
1744
1745         int retVal = -1;
1746
1747         yaffsfs_Lock();
1748         obj = yaffsfs_GetHandleObject(fd);
1749
1750         if(obj) {
1751                 retVal = yaffs_list_xattrib(obj,data,size);
1752                 if(retVal< 0){
1753                         yaffsfs_SetError(retVal);
1754                         retVal = -1;
1755                 }
1756         } else
1757                 /* bad handle */
1758                 yaffsfs_SetError(-EBADF);
1759
1760         yaffsfs_Unlock();
1761
1762         return retVal;
1763 }
1764
1765 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1766 {
1767         struct yaffs_obj *obj=NULL;
1768         struct yaffs_obj *dir=NULL;
1769         int notDir = 0;
1770         int loop = 0;
1771         int retVal = -1;
1772
1773         if(yaffsfs_CheckPath(path) < 0){
1774                 yaffsfs_SetError(-ENAMETOOLONG);
1775                 return -1;
1776         }
1777
1778         yaffsfs_Lock();
1779
1780         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
1781
1782         if(follow)
1783                 obj = yaffsfs_FollowLink(obj,0,&loop);
1784
1785         if(!dir && notDir) 
1786                 yaffsfs_SetError(-ENOTDIR);
1787         else if(loop) 
1788                 yaffsfs_SetError(-ELOOP);
1789         else if(!dir || !obj) 
1790                 yaffsfs_SetError(-ENOENT);
1791         else {
1792                 retVal = yaffs_remove_xattrib(obj,name);
1793                 if(retVal< 0){
1794                         yaffsfs_SetError(retVal);
1795                         retVal = -1;
1796                 }
1797         }
1798
1799         yaffsfs_Unlock();
1800
1801         return retVal;
1802
1803 }
1804
1805 int yaffs_removexattr(const YCHAR *path, const char *name)
1806 {
1807         return yaffs_do_removexattr(path, name, 1);
1808 }
1809
1810 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1811 {
1812         return yaffs_do_removexattr(path, name, 0);
1813 }
1814
1815 int yaffs_fremovexattr(int fd, const char *name)
1816 {
1817         struct yaffs_obj *obj;
1818
1819         int retVal = -1;
1820
1821         yaffsfs_Lock();
1822         obj = yaffsfs_GetHandleObject(fd);
1823
1824         if(obj){
1825                 retVal = yaffs_remove_xattrib(obj,name);
1826                 if(retVal< 0){
1827                         yaffsfs_SetError(retVal);
1828                         retVal = -1;
1829                 }
1830         }else
1831                 /* bad handle */
1832                 yaffsfs_SetError(-EBADF);
1833
1834         yaffsfs_Unlock();
1835
1836         return retVal;
1837 }
1838 #endif
1839
1840 #ifdef CONFIG_YAFFS_WINCE
1841 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1842 {
1843         struct yaffs_obj *obj;
1844
1845         int retVal = -1;
1846
1847         yaffsfs_Lock();
1848         obj = yaffsfs_GetHandleObject(fd);
1849
1850         if(obj){
1851
1852                 if(wctime){
1853                         wctime[0] = obj->win_ctime[0];
1854                         wctime[1] = obj->win_ctime[1];
1855                 }
1856                 if(watime){
1857                         watime[0] = obj->win_atime[0];
1858                         watime[1] = obj->win_atime[1];
1859                 }
1860                 if(wmtime){
1861                         wmtime[0] = obj->win_mtime[0];
1862                         wmtime[1] = obj->win_mtime[1];
1863                 }
1864
1865
1866                 retVal = 0;
1867         } else
1868                 /*  bad handle */
1869                 yaffsfs_SetError(-EBADF);               
1870         
1871         yaffsfs_Unlock();
1872         
1873         return retVal;
1874 }
1875
1876
1877 int yaffs_set_wince_times(int fd, 
1878                                                   const unsigned *wctime, 
1879                                                   const unsigned *watime, 
1880                                                   const unsigned *wmtime)
1881 {
1882         struct yaffs_obj *obj;
1883         int result;
1884         int retVal = -1;
1885
1886         yaffsfs_Lock();
1887         obj = yaffsfs_GetHandleObject(fd);
1888
1889         if(obj){
1890
1891                 if(wctime){
1892                         obj->win_ctime[0] = wctime[0];
1893                         obj->win_ctime[1] = wctime[1];
1894                 }
1895                 if(watime){
1896                         obj->win_atime[0] = watime[0];
1897                         obj->win_atime[1] = watime[1];
1898                 }
1899                 if(wmtime){
1900                         obj->win_mtime[0] = wmtime[0];
1901                         obj->win_mtime[1] = wmtime[1];
1902                 }
1903
1904                 obj->dirty = 1;
1905                 result = yaffs_flush_file(obj,0,0);
1906                 retVal = 0;
1907         } else
1908                 /* bad handle */
1909                 yaffsfs_SetError(-EBADF);
1910
1911         yaffsfs_Unlock();
1912
1913         return retVal;
1914 }
1915
1916 #endif
1917
1918
1919 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1920 {
1921         int result = -1;
1922
1923         if(obj)
1924                 obj = yaffs_get_equivalent_obj(obj);
1925
1926         if(obj) {
1927                 obj->yst_mode = mode;
1928                 obj->dirty = 1;
1929                 result = yaffs_flush_file(obj,0,0);
1930         }
1931
1932         return result == YAFFS_OK ? 0 : -1;
1933 }
1934
1935
1936 int yaffs_access(const YCHAR *path, int amode)
1937 {
1938         struct yaffs_obj *obj=NULL;
1939         struct yaffs_obj *dir=NULL;
1940         int notDir = 0;
1941         int loop = 0;
1942         int retval = -1;
1943
1944         if(yaffsfs_CheckPath(path) < 0){
1945                 yaffsfs_SetError(-ENAMETOOLONG);
1946                 return -1;
1947         }
1948
1949         if(amode & ~(R_OK | W_OK | X_OK)){
1950                 yaffsfs_SetError(-EINVAL);
1951                 return -1;
1952         }
1953
1954         yaffsfs_Lock();
1955
1956         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
1957         obj = yaffsfs_FollowLink(obj,0,&loop);
1958
1959         if(!dir && notDir) 
1960                 yaffsfs_SetError(-ENOTDIR);
1961         else if(loop) 
1962                 yaffsfs_SetError(-ELOOP);
1963         else if(!dir || !obj) 
1964                 yaffsfs_SetError(-ENOENT);
1965         else {
1966                 int access_ok = 1;
1967
1968                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1969                         access_ok = 0;
1970                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1971                         access_ok = 0;
1972                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1973                         access_ok = 0;
1974
1975                 if(!access_ok)
1976                         yaffsfs_SetError(-EACCES);
1977                 else
1978                         retval = 0;
1979         }
1980
1981         yaffsfs_Unlock();
1982
1983         return retval;
1984
1985 }
1986
1987
1988 int yaffs_chmod(const YCHAR *path, mode_t mode)
1989 {
1990         struct yaffs_obj *obj=NULL;
1991         struct yaffs_obj *dir=NULL;
1992         int retVal = -1;
1993         int notDir = 0;
1994         int loop = 0;
1995
1996         if(yaffsfs_CheckPath(path) < 0){
1997                 yaffsfs_SetError(-ENAMETOOLONG);
1998                 return -1;
1999         }
2000
2001         if(mode & ~(0777)){
2002                 yaffsfs_SetError(-EINVAL);
2003                 return -1;
2004         }
2005
2006         yaffsfs_Lock();
2007
2008         obj = yaffsfs_FindObject(NULL,path,0,1, &dir, &notDir,&loop);
2009
2010         if(!dir && notDir) 
2011                 yaffsfs_SetError(-ENOTDIR);
2012         else if(loop) 
2013                 yaffsfs_SetError(-ELOOP);
2014         else if(!dir || !obj) 
2015                 yaffsfs_SetError(-ENOENT);
2016         else if(obj->my_dev->read_only)
2017                 yaffsfs_SetError(-EROFS);
2018         else
2019                 retVal = yaffsfs_DoChMod(obj,mode);
2020
2021         yaffsfs_Unlock();
2022
2023         return retVal;
2024
2025 }
2026
2027
2028 int yaffs_fchmod(int fd, mode_t mode)
2029 {
2030         struct yaffs_obj *obj;
2031         int retVal = -1;
2032
2033         if(mode & ~(0777)){
2034                 yaffsfs_SetError(-EINVAL);
2035                 return -1;
2036         }
2037
2038         yaffsfs_Lock();
2039         obj = yaffsfs_GetHandleObject(fd);
2040
2041         if(!obj)
2042                 yaffsfs_SetError(-EBADF);
2043         else if(obj->my_dev->read_only)
2044                 yaffsfs_SetError(-EROFS);
2045         else
2046                 retVal = yaffsfs_DoChMod(obj,mode);
2047
2048         yaffsfs_Unlock();
2049
2050         return retVal;
2051 }
2052
2053 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2054 {
2055         struct yaffs_obj *parent = NULL;
2056         struct yaffs_obj *dir = NULL;
2057         YCHAR *name;
2058         YCHAR *alt_path = NULL;
2059         int retVal= -1;
2060         int notDir = 0;
2061         int loop = 0;
2062
2063         if(yaffsfs_CheckPath(path) < 0){
2064                 yaffsfs_SetError(-ENAMETOOLONG);
2065                 return -1;
2066         }
2067
2068         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2069                 yaffsfs_SetError(-ENOMEM);
2070                 return -1;
2071         }
2072         if(alt_path)
2073                 path = alt_path;
2074         
2075         yaffsfs_Lock();
2076         parent = yaffsfs_FindDirectory(NULL,path,&name,0,&notDir,&loop);
2077         if(!parent && notDir)
2078                 yaffsfs_SetError(-ENOTDIR);
2079         else if(loop)
2080                 yaffsfs_SetError(-ELOOP);
2081         else if(!parent)
2082                 yaffsfs_SetError(-ENOENT);
2083         else if(yaffs_strnlen(name,5) == 0){
2084                 /* Trying to make the root itself */
2085                 yaffsfs_SetError(-EEXIST);
2086         } else if(parent->my_dev->read_only)
2087                 yaffsfs_SetError(-EROFS);
2088         else {
2089                 dir = yaffs_create_dir(parent,name,mode,0,0);
2090                 if(dir)
2091                         retVal = 0;
2092                 else if (yaffs_find_by_name(parent,name))
2093                         yaffsfs_SetError(-EEXIST); /* the name already exists */
2094                 else
2095                         yaffsfs_SetError(-ENOSPC); /* just assume no space */
2096         }
2097
2098         yaffsfs_Unlock();
2099
2100         if(alt_path)
2101                 YFREE(alt_path);
2102
2103         return retVal;
2104 }
2105
2106 int yaffs_rmdir(const YCHAR *path)
2107 {
2108         int result;
2109         YCHAR *alt_path;
2110
2111         if(yaffsfs_CheckPath(path) < 0){
2112                 yaffsfs_SetError(-ENAMETOOLONG);
2113                 return -1;
2114         }
2115
2116         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2117                 yaffsfs_SetError(-ENOMEM);
2118                 return -1;
2119         }
2120         if(alt_path)
2121                 path = alt_path;
2122         result =  yaffsfs_DoUnlink(path,1);
2123         if(alt_path)
2124                 YFREE(alt_path);
2125         return result;
2126 }
2127
2128
2129 void * yaffs_getdev(const YCHAR *path)
2130 {
2131         struct yaffs_dev *dev=NULL;
2132         YCHAR *dummy;
2133         dev = yaffsfs_FindDevice(path,&dummy);
2134         return (void *)dev;
2135 }
2136
2137 int yaffs_mount2(const YCHAR *path,int read_only)
2138 {
2139         int retVal=-1;
2140         int result=YAFFS_FAIL;
2141         struct yaffs_dev *dev=NULL;
2142
2143         T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2144
2145         if(yaffsfs_CheckPath(path) < 0){
2146                 yaffsfs_SetError(-ENAMETOOLONG);
2147                 return -1;
2148         }
2149
2150         yaffsfs_Lock();
2151
2152         yaffsfs_InitHandles();
2153
2154         dev = yaffsfs_FindMountPoint(path);
2155         if(dev){
2156                 if(!dev->is_mounted){
2157                         dev->read_only = read_only ? 1 : 0;
2158                         result = yaffs_guts_initialise(dev);
2159                         if(result == YAFFS_FAIL)
2160                                 yaffsfs_SetError(-ENOMEM);
2161                         retVal = result ? 0 : -1;
2162
2163                 }
2164                 else
2165                         yaffsfs_SetError(-EBUSY);
2166         } else
2167                 yaffsfs_SetError(-ENODEV);
2168
2169         yaffsfs_Unlock();
2170         return retVal;
2171
2172 }
2173
2174 int yaffs_mount(const YCHAR *path)
2175 {
2176         return yaffs_mount2(path,0);
2177 }
2178
2179 int yaffs_sync(const YCHAR *path)
2180 {
2181         int retVal=-1;
2182         struct yaffs_dev *dev=NULL;
2183         YCHAR *dummy;
2184
2185         if(yaffsfs_CheckPath(path) < 0){
2186                 yaffsfs_SetError(-ENAMETOOLONG);
2187                 return -1;
2188         }
2189         
2190         yaffsfs_Lock();
2191         dev = yaffsfs_FindDevice(path,&dummy);
2192         if(dev){
2193                 if(dev->is_mounted){
2194                         
2195                         yaffs_flush_whole_cache(dev);
2196                         yaffs_checkpoint_save(dev);
2197                         retVal = 0;
2198                         
2199                 } else
2200                         yaffsfs_SetError(-EINVAL);
2201                         
2202         }else
2203                 yaffsfs_SetError(-ENODEV);
2204
2205         yaffsfs_Unlock();
2206         return retVal;  
2207 }
2208
2209
2210 int yaffs_remount(const YCHAR *path, int force, int read_only)
2211 {
2212         int retVal=-1;
2213         struct yaffs_dev *dev=NULL;
2214         yaffsfs_Handle *yh;
2215
2216         if(yaffsfs_CheckPath(path) < 0){
2217                 yaffsfs_SetError(-ENAMETOOLONG);
2218                 return -1;
2219         }
2220
2221         yaffsfs_Lock();
2222         dev = yaffsfs_FindMountPoint(path);
2223         if(dev){
2224                 if(dev->is_mounted){
2225                         int i;
2226                         int inUse;
2227
2228                         yaffs_flush_whole_cache(dev);
2229
2230                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2231                                 yh = & yaffsfs_handle[i];
2232                                 if(yh->useCount>0 && 
2233                                         yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2234                                         inUse = 1; /* the device is in use, can't unmount */
2235                         }
2236
2237                         if(!inUse || force){
2238                                 if(read_only)
2239                                         yaffs_checkpoint_save(dev);
2240                                 dev->read_only =  read_only ? 1 : 0;
2241                                 retVal = 0;
2242                         } else
2243                                 yaffsfs_SetError(-EBUSY);
2244
2245                 } else
2246                         yaffsfs_SetError(-EINVAL);
2247
2248         }
2249         else
2250                 yaffsfs_SetError(-ENODEV);
2251
2252         yaffsfs_Unlock();
2253         return retVal;
2254
2255 }
2256
2257 int yaffs_unmount2(const YCHAR *path, int force)
2258 {
2259         int retVal=-1;
2260         struct yaffs_dev *dev=NULL;
2261
2262         if(yaffsfs_CheckPath(path) < 0){
2263                 yaffsfs_SetError(-ENAMETOOLONG);
2264                 return -1;
2265         }
2266
2267         yaffsfs_Lock();
2268         dev = yaffsfs_FindMountPoint(path);
2269         if(dev){
2270                 if(dev->is_mounted){
2271                         int i;
2272                         int inUse;
2273
2274                         yaffs_flush_whole_cache(dev);
2275                         yaffs_checkpoint_save(dev);
2276
2277                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2278                                 if(yaffsfs_handle[i].useCount > 0 && 
2279                                 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2280                                         inUse = 1; /* the device is in use, can't unmount */
2281                         }
2282
2283                         if(!inUse || force){
2284                                 if(inUse)
2285                                         yaffsfs_BreakDeviceHandles(dev);
2286                                 yaffs_deinitialise(dev);
2287
2288                                 retVal = 0;
2289                         } else
2290                                 yaffsfs_SetError(-EBUSY);
2291
2292                 } else
2293                         yaffsfs_SetError(-EINVAL);
2294
2295         } else
2296                 yaffsfs_SetError(-ENODEV);
2297
2298         yaffsfs_Unlock();
2299         return retVal;
2300
2301 }
2302
2303 int yaffs_unmount(const YCHAR *path)
2304 {
2305         return yaffs_unmount2(path,0);
2306 }
2307
2308 loff_t yaffs_freespace(const YCHAR *path)
2309 {
2310         loff_t retVal=-1;
2311         struct yaffs_dev *dev=NULL;
2312         YCHAR *dummy;
2313
2314         if(yaffsfs_CheckPath(path) < 0){
2315                 yaffsfs_SetError(-ENAMETOOLONG);
2316                 return -1;
2317         }
2318
2319         yaffsfs_Lock();
2320         dev = yaffsfs_FindDevice(path,&dummy);
2321         if(dev  && dev->is_mounted){
2322                 retVal = yaffs_get_n_free_chunks(dev);
2323                 retVal *= dev->data_bytes_per_chunk;
2324
2325         } else
2326                 yaffsfs_SetError(-EINVAL);
2327
2328         yaffsfs_Unlock();
2329         return retVal;
2330 }
2331
2332 loff_t yaffs_totalspace(const YCHAR *path)
2333 {
2334         loff_t retVal=-1;
2335         struct yaffs_dev *dev=NULL;
2336         YCHAR *dummy;
2337
2338         if(yaffsfs_CheckPath(path) < 0){
2339                 yaffsfs_SetError(-ENAMETOOLONG);
2340                 return -1;
2341         }
2342
2343         yaffsfs_Lock();
2344         dev = yaffsfs_FindDevice(path,&dummy);
2345         if(dev  && dev->is_mounted){
2346                 retVal = (dev->param.end_block - dev->param.start_block + 1) - 
2347                         dev->param.n_reserved_blocks;
2348                 retVal *= dev->param.chunks_per_block;
2349                 retVal *= dev->data_bytes_per_chunk;
2350
2351         } else
2352                 yaffsfs_SetError(-EINVAL);
2353
2354         yaffsfs_Unlock();
2355         return retVal;
2356 }
2357
2358 int yaffs_inodecount(const YCHAR *path)
2359 {
2360         loff_t retVal= -1;
2361         struct yaffs_dev *dev=NULL;
2362         YCHAR *dummy;
2363
2364         if(yaffsfs_CheckPath(path) < 0){
2365                 yaffsfs_SetError(-ENAMETOOLONG);
2366                 return -1;
2367         }
2368
2369         yaffsfs_Lock();
2370         dev = yaffsfs_FindDevice(path,&dummy);
2371         if(dev  && dev->is_mounted) {
2372            int n_obj = dev->n_obj;
2373            if(n_obj > dev->n_hardlinks)
2374                 retVal = n_obj - dev->n_hardlinks;
2375         }
2376         
2377         if(retVal < 0)
2378                 yaffsfs_SetError(-EINVAL);
2379         
2380         yaffsfs_Unlock();
2381         return retVal;  
2382 }
2383
2384
2385 void yaffs_add_device(struct yaffs_dev *dev)
2386 {
2387         dev->is_mounted = 0;
2388         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2389
2390         if(!dev->dev_list.next)
2391                 INIT_LIST_HEAD(&dev->dev_list);
2392
2393         list_add(&dev->dev_list,&yaffsfs_deviceList);
2394 }
2395
2396 void yaffs_remove_device(struct yaffs_dev *dev)
2397 {
2398         list_del_init(&dev->dev_list);
2399 }
2400
2401
2402
2403
2404 /* Directory search stuff. */
2405
2406 /*
2407  * Directory search context
2408  *
2409  * NB this is an opaque structure.
2410  */
2411
2412
2413 typedef struct
2414 {
2415         u32 magic;
2416         yaffs_dirent de;                /* directory entry being used by this dsc */
2417         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2418         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2419         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2420         int offset;
2421         struct list_head others;       
2422 } yaffsfs_DirectorySearchContext;
2423
2424
2425
2426 static struct list_head search_contexts;
2427
2428
2429 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2430 {
2431         if(dsc &&
2432            dsc->dirObj &&
2433            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2434
2435            dsc->offset = 0;
2436
2437            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2438                 dsc->nextReturn = NULL;
2439            else
2440                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2441                                                 struct yaffs_obj,siblings);
2442         } else {
2443                 /* Hey someone isn't playing nice! */
2444         }
2445 }
2446
2447 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2448 {
2449         if(dsc &&
2450            dsc->dirObj &&
2451            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2452
2453            if( dsc->nextReturn == NULL ||
2454                list_empty(&dsc->dirObj->variant.dir_variant.children))
2455                 dsc->nextReturn = NULL;
2456            else {
2457                    struct list_head *next = dsc->nextReturn->siblings.next;
2458
2459                    if( next == &dsc->dirObj->variant.dir_variant.children)
2460                         dsc->nextReturn = NULL; /* end of list */
2461                    else
2462                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2463            }
2464         } else {
2465                 /* Hey someone isn't playing nice! */
2466         }
2467 }
2468
2469 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2470 {
2471
2472         struct list_head *i;
2473         yaffsfs_DirectorySearchContext *dsc;
2474
2475         /* if search contexts not initilised then skip */
2476         if(!search_contexts.next)
2477                 return;
2478
2479         /* Iterate through the directory search contexts.
2480          * If any are the one being removed, then advance the dsc to
2481          * the next one to prevent a hanging ptr.
2482          */
2483          list_for_each(i, &search_contexts) {
2484                 if (i) {
2485                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2486                         if(dsc->nextReturn == obj)
2487                                 yaffsfs_DirAdvance(dsc);
2488                 }
2489         }
2490
2491 }
2492
2493 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2494 {
2495         yaffs_DIR *dir = NULL;
2496         struct yaffs_obj *obj = NULL;
2497         yaffsfs_DirectorySearchContext *dsc = NULL;
2498         int notDir = 0;
2499         int loop = 0;
2500
2501         if(yaffsfs_CheckPath(dirname) < 0){
2502                 yaffsfs_SetError(-ENAMETOOLONG);
2503                 return NULL;
2504         }
2505
2506         yaffsfs_Lock();
2507
2508         obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL,&notDir,&loop);
2509
2510         if(!obj && notDir)
2511                 yaffsfs_SetError(-ENOTDIR);
2512         else if(loop)
2513                 yaffsfs_SetError(-ELOOP);
2514         else if(!obj)
2515                 yaffsfs_SetError(-ENOENT);
2516         else if(obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2517                 yaffsfs_SetError(-ENOTDIR);
2518         else {
2519
2520                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2521                 dir = (yaffs_DIR *)dsc;
2522
2523                 if(dsc){
2524                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2525                         dsc->magic = YAFFS_MAGIC;
2526                         dsc->dirObj = obj;
2527                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2528                         INIT_LIST_HEAD(&dsc->others);
2529
2530                         if(!search_contexts.next)
2531                                 INIT_LIST_HEAD(&search_contexts);
2532
2533                         list_add(&dsc->others,&search_contexts);       
2534                         yaffsfs_SetDirRewound(dsc);
2535                 }
2536
2537         }
2538
2539         yaffsfs_Unlock();
2540
2541         return dir;
2542 }
2543
2544 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2545 {
2546         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2547         struct yaffs_dirent *retVal = NULL;
2548
2549         yaffsfs_Lock();
2550
2551         if(dsc && dsc->magic == YAFFS_MAGIC){
2552                 yaffsfs_SetError(0);
2553                 if(dsc->nextReturn){
2554                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2555                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2556                         dsc->de.d_off = dsc->offset++;
2557                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2558                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2559                         {
2560                                 /* this should not happen! */
2561                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2562                         }
2563                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2564                         retVal = &dsc->de;
2565                         yaffsfs_DirAdvance(dsc);
2566                 } else
2567                         retVal = NULL;
2568         } else
2569                 yaffsfs_SetError(-EBADF);
2570
2571         yaffsfs_Unlock();
2572
2573         return retVal;
2574
2575 }
2576
2577
2578 void yaffs_rewinddir(yaffs_DIR *dirp)
2579 {
2580         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2581
2582         yaffsfs_Lock();
2583
2584         yaffsfs_SetDirRewound(dsc);
2585
2586         yaffsfs_Unlock();
2587 }
2588
2589
2590 int yaffs_closedir(yaffs_DIR *dirp)
2591 {
2592         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2593
2594         yaffsfs_Lock();
2595         dsc->magic = 0;
2596         list_del(&dsc->others); /* unhook from list */
2597         YFREE(dsc);
2598         yaffsfs_Unlock();
2599         return 0;
2600 }
2601
2602 /* End of directory stuff */
2603
2604
2605 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2606 {
2607         struct yaffs_obj *parent = NULL;
2608         struct yaffs_obj *obj;
2609         YCHAR *name;
2610         int retVal= -1;
2611         int mode = 0; /* ignore for now */
2612         int notDir = 0;
2613         int loop = 0;
2614
2615         if(yaffsfs_CheckPath(newpath) < 0){
2616                 yaffsfs_SetError(-ENAMETOOLONG);
2617                 return -1;
2618         }
2619         yaffsfs_Lock();
2620         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0,&notDir,&loop);
2621         if(!parent && notDir)
2622                 yaffsfs_SetError(-ENOTDIR);
2623         else if(loop)
2624                 yaffsfs_SetError(-ELOOP);
2625         else if( !parent || yaffs_strnlen(name,5) < 1)
2626                 yaffsfs_SetError(-ENOENT);
2627         else if(parent->my_dev->read_only)
2628                 yaffsfs_SetError(-EROFS);
2629         else if(parent){
2630                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2631                 if(obj)
2632                         retVal = 0;
2633                 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL,NULL,NULL))
2634                         yaffsfs_SetError(-EEXIST);
2635                 else
2636                         yaffsfs_SetError(-ENOSPC);
2637         }
2638
2639         yaffsfs_Unlock();
2640
2641         return retVal;
2642
2643 }
2644
2645 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2646 {
2647         struct yaffs_obj *obj = NULL;
2648         struct yaffs_obj *dir = NULL;
2649         int retVal= -1;
2650         int notDir = 0;
2651         int loop = 0;
2652
2653         yaffsfs_Lock();
2654
2655         obj = yaffsfs_FindObject(NULL,path,0,1, &dir,&notDir,&loop);
2656
2657         if(!dir && notDir) 
2658                 yaffsfs_SetError(-ENOTDIR);
2659         else if(loop) 
2660                 yaffsfs_SetError(-ELOOP);
2661         else if(!dir || !obj) 
2662                 yaffsfs_SetError(-ENOENT);
2663         else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2664                 yaffsfs_SetError(-EINVAL);
2665         else {
2666                 YCHAR *alias = obj->variant.symlink_variant.alias;
2667                 memset(buf,0,bufsiz);
2668                 yaffs_strncpy(buf,alias,bufsiz - 1);
2669                 retVal = 0;
2670         }
2671         yaffsfs_Unlock();
2672         return retVal;
2673 }
2674
2675 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2676 {
2677         /* Creates a link called newpath to existing oldpath */
2678         struct yaffs_obj *obj = NULL;
2679         struct yaffs_obj *lnk = NULL;
2680         struct yaffs_obj *obj_dir = NULL;
2681         struct yaffs_obj *lnk_dir = NULL;
2682         int retVal = -1;
2683         int notDirObj = 0;
2684         int notDirLnk = 0;
2685         int objLoop = 0;
2686         int lnkLoop = 0;
2687         YCHAR *newname;
2688
2689         if(yaffsfs_CheckPath(linkpath) < 0){
2690                 yaffsfs_SetError(-ENAMETOOLONG);
2691                 return -1;
2692         }
2693
2694         yaffsfs_Lock();
2695
2696         obj = yaffsfs_FindObject(NULL,oldpath,0,1,&obj_dir,&notDirObj,&objLoop);
2697         lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL,NULL,NULL);
2698         lnk_dir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0,&notDirLnk,&lnkLoop);
2699
2700         if((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
2701                 yaffsfs_SetError(-ENOTDIR);
2702         else if(objLoop || lnkLoop)
2703                 yaffsfs_SetError(-ELOOP);
2704         else if(!obj_dir || !lnk_dir || !obj)
2705                 yaffsfs_SetError(-ENOENT);
2706         else if(obj->my_dev->read_only)
2707                 yaffsfs_SetError(-EINVAL);
2708         else if(lnk)
2709                 yaffsfs_SetError(-EEXIST);
2710         else if(lnk_dir->my_dev != obj->my_dev)
2711                 yaffsfs_SetError(-EXDEV);
2712         else {          
2713                 retVal = yaffsfs_CheckNameLength(newname);
2714                 
2715                 if(retVal == 0) {
2716                         lnk = yaffs_link_obj(lnk_dir,newname,obj);
2717                         if(lnk)
2718                                 retVal = 0;
2719                         else{
2720                                 yaffsfs_SetError(-ENOSPC);
2721                                 retVal = -1;
2722                         }
2723                 }
2724         }
2725         yaffsfs_Unlock();
2726
2727         return retVal;
2728 }
2729
2730 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2731 {
2732         pathname=pathname;
2733         mode=mode;
2734         dev=dev;
2735
2736         yaffsfs_SetError(-EINVAL);
2737         return -1;
2738 }
2739
2740
2741 /*
2742  * D E B U G   F U N C T I O N S
2743  */
2744  
2745 /*
2746  * yaffs_n_handles()
2747  * Returns number of handles attached to the object
2748  */
2749 int yaffs_n_handles(const YCHAR *path)
2750 {
2751         struct yaffs_obj *obj;
2752
2753         if(yaffsfs_CheckPath(path) < 0){
2754                 yaffsfs_SetError(-ENAMETOOLONG);
2755                 return -1;
2756         }
2757
2758         obj = yaffsfs_FindObject(NULL,path,0,1,NULL,NULL,NULL);
2759
2760         if(obj)
2761                 return yaffsfs_CountHandles(obj);
2762         else
2763                 return -1;
2764 }
2765
2766 int yaffs_get_error(void)
2767 {
2768         return yaffsfs_GetLastError();
2769 }
2770
2771 int yaffs_set_error(int error)
2772 {
2773         yaffsfs_SetError(error);
2774         return 0;
2775 }
2776
2777 int yaffs_dump_dev(const YCHAR *path)
2778 {
2779 #if 1
2780         path=path;
2781 #else
2782         YCHAR *rest;
2783
2784         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2785
2786         if(obj){
2787                 struct yaffs_dev *dev = obj->my_dev;
2788
2789                 printf("\n"
2790                            "n_page_writes.......... %d\n"
2791                            "n_page_reads........... %d\n"
2792                            "n_erasures....... %d\n"
2793                            "n_gc_copies............ %d\n"
2794                            "garbageCollections... %d\n"
2795                            "passiveGarbageColl'ns %d\n"
2796                            "\n",
2797                                 dev->n_page_writes,
2798                                 dev->n_page_reads,
2799                                 dev->n_erasures,
2800                                 dev->n_gc_copies,
2801                                 dev->garbageCollections,
2802                                 dev->passiveGarbageCollections
2803                 );
2804
2805         }
2806
2807 #endif
2808         return 0;
2809 }
2810