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