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