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