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