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