yaffs direct: Modify lseek value checking
[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         /* todo error */
1246
1247         return (result == YAFFS_FAIL) ? -1 : 0;
1248 }
1249
1250
1251 int yaffs_unlink(const YCHAR *path)
1252 {
1253         return yaffsfs_DoUnlink(path,0);
1254 }
1255
1256 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1257 {
1258         struct yaffs_obj *olddir = NULL;
1259         struct yaffs_obj *newdir = NULL;
1260         struct yaffs_obj *obj = NULL;
1261         YCHAR *oldname;
1262         YCHAR *newname;
1263         int result= YAFFS_FAIL;
1264         int rename_allowed = 1;
1265
1266         yaffsfs_Lock();
1267
1268         if(yaffsfs_CheckPath(newPath) < 0){
1269                 yaffsfs_SetError(-ENAMETOOLONG);
1270                 return -1;
1271         }
1272
1273         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1274         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1275         obj = yaffsfs_FindObject(NULL,oldPath,0,0,NULL);
1276
1277         if(!olddir || !newdir) {
1278                 yaffsfs_SetError(-ENOTDIR);
1279                 rename_allowed = 0;
1280         } else if(!obj) {
1281                 yaffsfs_SetError(-ENOENT);
1282                 rename_allowed = 0;
1283         } else if(obj->my_dev->read_only){
1284                 yaffsfs_SetError(-EROFS);
1285                 rename_allowed = 0;
1286         } else if(olddir->my_dev != newdir->my_dev) {
1287                 /* Rename must be on same device */
1288                 yaffsfs_SetError(-EXDEV);
1289                 rename_allowed = 0;
1290         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1291                 /*
1292                  * It is a directory, check that it is not being renamed to
1293                  * being its own decendent.
1294                  * Do this by tracing from the new directory back to the root, checking for obj
1295                  */
1296
1297                 struct yaffs_obj *xx = newdir;
1298
1299                 while( rename_allowed && xx){
1300                         if(xx == obj)
1301                                 rename_allowed = 0;
1302                         xx = xx->parent;
1303                 }
1304                 if(!rename_allowed)
1305                         yaffsfs_SetError(-EINVAL);
1306         }
1307
1308         if(rename_allowed)
1309                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1310
1311         yaffsfs_Unlock();
1312
1313         return (result == YAFFS_FAIL) ? -1 : 0;
1314 }
1315
1316
1317 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1318 {
1319         int retVal = -1;
1320
1321         obj = yaffs_get_equivalent_obj(obj);
1322
1323         if(obj && buf){
1324                 buf->st_dev = (int)obj->my_dev->os_context;
1325                 buf->st_ino = obj->obj_id;
1326                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1327
1328                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1329                         buf->st_mode |= S_IFDIR;
1330                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1331                         buf->st_mode |= S_IFLNK;
1332                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1333                         buf->st_mode |= S_IFREG;
1334
1335                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1336                 buf->st_uid = 0;
1337                 buf->st_gid = 0;;
1338                 buf->st_rdev = obj->yst_rdev;
1339                 buf->st_size = yaffs_get_obj_length(obj);
1340                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1341                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1342 #if CONFIG_YAFFS_WINCE
1343                 buf->yst_wince_atime[0] = obj->win_atime[0];
1344                 buf->yst_wince_atime[1] = obj->win_atime[1];
1345                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1346                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1347                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1348                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1349 #else
1350                 buf->yst_atime = obj->yst_atime;
1351                 buf->yst_ctime = obj->yst_ctime;
1352                 buf->yst_mtime = obj->yst_mtime;
1353 #endif
1354                 retVal = 0;
1355         }
1356         return retVal;
1357 }
1358
1359 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1360 {
1361         struct yaffs_obj *obj=NULL;
1362         struct yaffs_obj *dir=NULL;
1363
1364         int retVal = -1;
1365
1366         if(yaffsfs_CheckPath(path) < 0){
1367                 yaffsfs_SetError(-ENAMETOOLONG);
1368                 return -1;
1369         }
1370
1371         yaffsfs_Lock();
1372
1373         obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1374
1375         if(!doLStat && obj)
1376                 obj = yaffsfs_FollowLink(obj,0);
1377
1378         if(!dir)
1379                 yaffsfs_SetError(-ENOTDIR);
1380         else if(!obj)
1381                 yaffsfs_SetError(-ENOENT);
1382         else
1383                 retVal = yaffsfs_DoStat(obj,buf);
1384
1385         yaffsfs_Unlock();
1386
1387         return retVal;
1388
1389 }
1390
1391 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1392 {
1393         return yaffsfs_DoStatOrLStat(path,buf,0);
1394 }
1395
1396 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1397 {
1398         return yaffsfs_DoStatOrLStat(path,buf,1);
1399 }
1400
1401 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1402 {
1403         struct yaffs_obj *obj;
1404
1405         int retVal = -1;
1406
1407         yaffsfs_Lock();
1408         obj = yaffsfs_GetHandleObject(fd);
1409
1410         if(obj)
1411                 retVal = yaffsfs_DoStat(obj,buf);
1412         else
1413                 /* bad handle */
1414                 yaffsfs_SetError(-EBADF);
1415
1416         yaffsfs_Unlock();
1417
1418         return retVal;
1419 }
1420
1421 #ifndef CONFIG_YAFFS_WINCE
1422 /* xattrib functions */
1423
1424
1425 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1426 {
1427         struct yaffs_obj *obj;
1428         struct yaffs_obj *dir;
1429
1430         int retVal = -1;
1431
1432         if(yaffsfs_CheckPath(path) < 0){
1433                 yaffsfs_SetError(-ENAMETOOLONG);
1434                 return -1;
1435         }
1436
1437         yaffsfs_Lock();
1438
1439         obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1440
1441         if(follow)
1442                 obj = yaffsfs_FollowLink(obj,0);
1443
1444         if(!dir) 
1445                 yaffsfs_SetError(-ENOTDIR);
1446         else if(!obj) 
1447                 yaffsfs_SetError(-ENOENT);
1448         else {
1449                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1450                 if(retVal< 0){
1451                         yaffsfs_SetError(retVal);
1452                         retVal = -1;
1453                 }
1454         }
1455
1456         yaffsfs_Unlock();
1457
1458         return retVal;
1459
1460 }
1461
1462 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1463 {
1464         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1465 }
1466
1467 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1468 {
1469         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1470 }
1471
1472
1473
1474 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1475 {
1476         struct yaffs_obj *obj;
1477
1478         int retVal = -1;
1479
1480         yaffsfs_Lock();
1481         obj = yaffsfs_GetHandleObject(fd);
1482
1483         if(!obj) 
1484                 yaffsfs_SetError(-EBADF);
1485         else {
1486                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1487                 if(retVal< 0){
1488                         yaffsfs_SetError(retVal);
1489                         retVal = -1;
1490                 }
1491         }
1492
1493         yaffsfs_Unlock();
1494
1495         return retVal;
1496 }
1497
1498 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1499 {
1500         struct yaffs_obj *obj;
1501         struct yaffs_obj *dir;
1502
1503         int retVal = -1;
1504
1505         if(yaffsfs_CheckPath(path) < 0){
1506                 yaffsfs_SetError(-ENAMETOOLONG);
1507                 return -1;
1508         }
1509
1510         yaffsfs_Lock();
1511
1512         obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1513
1514         if(follow)
1515                 obj = yaffsfs_FollowLink(obj,0);
1516
1517         if(!dir) 
1518                 yaffsfs_SetError(-ENOTDIR);
1519         else if(!obj) 
1520                 yaffsfs_SetError(-ENOENT);
1521         else {
1522                 retVal = yaffs_get_xattrib(obj,name,data,size);
1523                 if(retVal< 0){
1524                         yaffsfs_SetError(retVal);
1525                         retVal = -1;
1526                 }
1527         }
1528         yaffsfs_Unlock();
1529
1530         return retVal;
1531
1532 }
1533
1534 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1535 {
1536         return yaffs_do_getxattr( path, name, data, size, 1);
1537 }
1538 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1539 {
1540         return yaffs_do_getxattr( path, name, data, size, 0);
1541 }
1542
1543
1544
1545 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1546 {
1547         struct yaffs_obj *obj;
1548
1549         int retVal = -1;
1550
1551         yaffsfs_Lock();
1552         obj = yaffsfs_GetHandleObject(fd);
1553
1554         if(obj) {
1555                 retVal = yaffs_get_xattrib(obj,name,data,size);
1556                 if(retVal< 0){
1557                         yaffsfs_SetError(retVal);
1558                         retVal = -1;
1559                 }
1560         } else
1561                 /* bad handle */
1562                 yaffsfs_SetError(-EBADF);
1563
1564         yaffsfs_Unlock();
1565
1566         return retVal;
1567 }
1568
1569 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1570 {
1571         struct yaffs_obj *obj=NULL;
1572         struct yaffs_obj *dir=NULL;
1573
1574         int retVal = -1;
1575
1576         if(yaffsfs_CheckPath(path) < 0){
1577                 yaffsfs_SetError(-ENAMETOOLONG);
1578                 return -1;
1579         }
1580
1581         yaffsfs_Lock();
1582
1583         obj = yaffsfs_FindObject(NULL,path,0,1,&dir);
1584
1585         if(follow)
1586                 obj = yaffsfs_FollowLink(obj,0);
1587
1588         if(!dir) 
1589                 yaffsfs_SetError(-ENOTDIR);
1590         else if(!obj) 
1591                 yaffsfs_SetError(-ENOENT);
1592         else {
1593                 retVal = yaffs_list_xattrib(obj, data,size);
1594                 if(retVal< 0){
1595                         yaffsfs_SetError(retVal);
1596                         retVal = -1;
1597                 }
1598         }
1599
1600         yaffsfs_Unlock();
1601
1602         return retVal;
1603
1604 }
1605
1606 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1607 {
1608         return yaffs_do_listxattr(path, data, size, 1);
1609 }
1610
1611 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1612 {
1613         return yaffs_do_listxattr(path, data, size, 0);
1614 }
1615
1616 int yaffs_flistxattr(int fd, char *data, int size)
1617 {
1618         struct yaffs_obj *obj;
1619
1620         int retVal = -1;
1621
1622         yaffsfs_Lock();
1623         obj = yaffsfs_GetHandleObject(fd);
1624
1625         if(obj) {
1626                 retVal = yaffs_list_xattrib(obj,data,size);
1627                 if(retVal< 0){
1628                         yaffsfs_SetError(retVal);
1629                         retVal = -1;
1630                 }
1631         } else
1632                 /* bad handle */
1633                 yaffsfs_SetError(-EBADF);
1634
1635         yaffsfs_Unlock();
1636
1637         return retVal;
1638 }
1639
1640 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1641 {
1642         struct yaffs_obj *obj=NULL;
1643         struct yaffs_obj *dir=NULL;
1644
1645         int retVal = -1;
1646
1647         if(yaffsfs_CheckPath(path) < 0){
1648                 yaffsfs_SetError(-ENAMETOOLONG);
1649                 return -1;
1650         }
1651
1652         yaffsfs_Lock();
1653
1654         obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1655
1656         if(follow)
1657                 obj = yaffsfs_FollowLink(obj,0);
1658
1659         if(!dir) 
1660                 yaffsfs_SetError(-ENOTDIR);
1661         else if(!obj) 
1662                 yaffsfs_SetError(-ENOENT);
1663         else {
1664                 retVal = yaffs_remove_xattrib(obj,name);
1665                 if(retVal< 0){
1666                         yaffsfs_SetError(retVal);
1667                         retVal = -1;
1668                 }
1669         }
1670
1671         yaffsfs_Unlock();
1672
1673         return retVal;
1674
1675 }
1676
1677 int yaffs_removexattr(const YCHAR *path, const char *name)
1678 {
1679         return yaffs_do_removexattr(path, name, 1);
1680 }
1681
1682 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1683 {
1684         return yaffs_do_removexattr(path, name, 0);
1685 }
1686
1687 int yaffs_fremovexattr(int fd, const char *name)
1688 {
1689         struct yaffs_obj *obj;
1690
1691         int retVal = -1;
1692
1693         yaffsfs_Lock();
1694         obj = yaffsfs_GetHandleObject(fd);
1695
1696         if(obj){
1697                 retVal = yaffs_remove_xattrib(obj,name);
1698                 if(retVal< 0){
1699                         yaffsfs_SetError(retVal);
1700                         retVal = -1;
1701                 }
1702         }else
1703                 /* bad handle */
1704                 yaffsfs_SetError(-EBADF);
1705
1706         yaffsfs_Unlock();
1707
1708         return retVal;
1709 }
1710 #endif
1711
1712 #ifdef CONFIG_YAFFS_WINCE
1713 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1714 {
1715         struct yaffs_obj *obj;
1716
1717         int retVal = -1;
1718
1719         yaffsfs_Lock();
1720         obj = yaffsfs_GetHandleObject(fd);
1721
1722         if(obj){
1723
1724                 if(wctime){
1725                         wctime[0] = obj->win_ctime[0];
1726                         wctime[1] = obj->win_ctime[1];
1727                 }
1728                 if(watime){
1729                         watime[0] = obj->win_atime[0];
1730                         watime[1] = obj->win_atime[1];
1731                 }
1732                 if(wmtime){
1733                         wmtime[0] = obj->win_mtime[0];
1734                         wmtime[1] = obj->win_mtime[1];
1735                 }
1736
1737
1738                 retVal = 0;
1739         } else
1740                 /*  bad handle */
1741                 yaffsfs_SetError(-EBADF);               
1742         
1743         yaffsfs_Unlock();
1744         
1745         return retVal;
1746 }
1747
1748
1749 int yaffs_set_wince_times(int fd, 
1750                                                   const unsigned *wctime, 
1751                                                   const unsigned *watime, 
1752                                                   const unsigned *wmtime)
1753 {
1754         struct yaffs_obj *obj;
1755         int result;
1756         int retVal = -1;
1757
1758         yaffsfs_Lock();
1759         obj = yaffsfs_GetHandleObject(fd);
1760
1761         if(obj){
1762
1763                 if(wctime){
1764                         obj->win_ctime[0] = wctime[0];
1765                         obj->win_ctime[1] = wctime[1];
1766                 }
1767                 if(watime){
1768                         obj->win_atime[0] = watime[0];
1769                         obj->win_atime[1] = watime[1];
1770                 }
1771                 if(wmtime){
1772                         obj->win_mtime[0] = wmtime[0];
1773                         obj->win_mtime[1] = wmtime[1];
1774                 }
1775
1776                 obj->dirty = 1;
1777                 result = yaffs_flush_file(obj,0,0);
1778                 retVal = 0;
1779         } else
1780                 /* bad handle */
1781                 yaffsfs_SetError(-EBADF);
1782
1783         yaffsfs_Unlock();
1784
1785         return retVal;
1786 }
1787
1788 #endif
1789
1790
1791 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1792 {
1793         int result = -1;
1794
1795         if(obj)
1796                 obj = yaffs_get_equivalent_obj(obj);
1797
1798         if(obj) {
1799                 obj->yst_mode = mode;
1800                 obj->dirty = 1;
1801                 result = yaffs_flush_file(obj,0,0);
1802         }
1803
1804         return result == YAFFS_OK ? 0 : -1;
1805 }
1806
1807
1808 int yaffs_access(const YCHAR *path, int amode)
1809 {
1810         struct yaffs_obj *obj=NULL;
1811         struct yaffs_obj *dir=NULL;
1812
1813         int retval = -1;
1814
1815         if(yaffsfs_CheckPath(path) < 0){
1816                 yaffsfs_SetError(-ENAMETOOLONG);
1817                 return -1;
1818         }
1819
1820         if(amode & ~(R_OK | W_OK | X_OK)){
1821                 yaffsfs_SetError(-EINVAL);
1822                 return -1;
1823         }
1824
1825         yaffsfs_Lock();
1826
1827         obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1828
1829         if(!dir) 
1830                 yaffsfs_SetError(-ENOTDIR);
1831         else if(!obj) 
1832                 yaffsfs_SetError(-ENOENT);
1833         else {
1834                 int access_ok = 1;
1835
1836                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1837                         access_ok = 0;
1838                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1839                         access_ok = 0;
1840                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1841                         access_ok = 0;
1842
1843                 if(!access_ok)
1844                         yaffsfs_SetError(-EACCES);
1845                 else
1846                         retval = 0;
1847         }
1848
1849         yaffsfs_Unlock();
1850
1851         return retval;
1852
1853 }
1854
1855
1856 int yaffs_chmod(const YCHAR *path, mode_t mode)
1857 {
1858         struct yaffs_obj *obj=NULL;
1859         struct yaffs_obj *dir=NULL;
1860         int retVal = -1;
1861
1862         if(yaffsfs_CheckPath(path) < 0){
1863                 yaffsfs_SetError(-ENAMETOOLONG);
1864                 return -1;
1865         }
1866
1867         if(mode & ~(0777)){
1868                 yaffsfs_SetError(-EINVAL);
1869                 return -1;
1870         }
1871
1872         yaffsfs_Lock();
1873
1874         obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
1875
1876         if(!dir) 
1877                 yaffsfs_SetError(-ENOTDIR);
1878         else if(!obj) 
1879                 yaffsfs_SetError(-ENOENT);
1880         else if(obj->my_dev->read_only)
1881                 yaffsfs_SetError(-EROFS);
1882         else
1883                 retVal = yaffsfs_DoChMod(obj,mode);
1884
1885         yaffsfs_Unlock();
1886
1887         return retVal;
1888
1889 }
1890
1891
1892 int yaffs_fchmod(int fd, mode_t mode)
1893 {
1894         struct yaffs_obj *obj;
1895         int retVal = -1;
1896
1897         if(mode & ~(0777)){
1898                 yaffsfs_SetError(-EINVAL);
1899                 return -1;
1900         }
1901
1902         yaffsfs_Lock();
1903         obj = yaffsfs_GetHandleObject(fd);
1904
1905         if(!obj)
1906                 yaffsfs_SetError(-EBADF);
1907         else if(obj->my_dev->read_only)
1908                 yaffsfs_SetError(-EROFS);
1909         else
1910                 retVal = yaffsfs_DoChMod(obj,mode);
1911
1912         yaffsfs_Unlock();
1913
1914         return retVal;
1915 }
1916
1917
1918 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
1919 {
1920         YCHAR *alt_path = NULL;
1921         int path_length;
1922         int i;
1923
1924         /*
1925          * We don't have a definition for max path length.
1926          * We will use 3 * max name length instead.
1927          */
1928         *ret_path = NULL;
1929         path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1930
1931         /* If the last character is a path divider, then we need to
1932          * trim it back so that the name look-up works properly.
1933          * eg. /foo/new_dir/ -> /foo/newdir
1934          * Curveball: Need to handle multiple path dividers:
1935          * eg. /foof/sdfse///// -> /foo/sdfse
1936          */
1937         if(path_length > 0 && 
1938                 yaffsfs_IsPathDivider(path[path_length-1])){
1939                 alt_path = YMALLOC(path_length + 1);
1940                 if(!alt_path)
1941                         return -1;
1942                 strcpy(alt_path, path);
1943                 for(i = path_length-1;
1944                         i >= 0 && yaffsfs_IsPathDivider(alt_path[i]);
1945                         i--)
1946                         alt_path[i] = (YCHAR) 0;
1947         }
1948         *ret_path = alt_path;
1949         return 0;
1950 }
1951
1952 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1953 {
1954         struct yaffs_obj *parent = NULL;
1955         struct yaffs_obj *dir = NULL;
1956         YCHAR *name;
1957         YCHAR *alt_path = NULL;
1958         int retVal= -1;
1959
1960         if(yaffsfs_CheckPath(path) < 0){
1961                 yaffsfs_SetError(-ENAMETOOLONG);
1962                 return -1;
1963         }
1964
1965         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
1966                 yaffsfs_SetError(-ENOMEM);
1967                 return -1;
1968         }
1969         if(alt_path)
1970                 path = alt_path;
1971         
1972         yaffsfs_Lock();
1973         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1974         if(!parent)
1975                 yaffsfs_SetError(-ENOTDIR);
1976         else if(parent && yaffs_strnlen(name,5) == 0){
1977                 /* Trying to make the root itself */
1978                 yaffsfs_SetError(-EEXIST);
1979         } else if(parent && parent->my_dev->read_only)
1980                 yaffsfs_SetError(-EROFS);
1981         else {
1982                 dir = yaffs_create_dir(parent,name,mode,0,0);
1983                 if(dir)
1984                         retVal = 0;
1985                 else if (yaffs_find_by_name(parent,name))
1986                         yaffsfs_SetError(-EEXIST); /* the name already exists */
1987                 else
1988                         yaffsfs_SetError(-ENOSPC); /* just assume no space */
1989         }
1990
1991         yaffsfs_Unlock();
1992
1993         if(alt_path)
1994                 YFREE(alt_path);
1995
1996         return retVal;
1997 }
1998
1999 int yaffs_rmdir(const YCHAR *path)
2000 {
2001         int result;
2002         YCHAR *alt_path;
2003
2004         if(yaffsfs_CheckPath(path) < 0){
2005                 yaffsfs_SetError(-ENAMETOOLONG);
2006                 return -1;
2007         }
2008
2009         if(yaffsfs_alt_dir_path(path, &alt_path) < 0){
2010                 yaffsfs_SetError(-ENOMEM);
2011                 return -1;
2012         }
2013         if(alt_path)
2014                 path = alt_path;
2015         result =  yaffsfs_DoUnlink(path,1);
2016         if(alt_path)
2017                 YFREE(alt_path);
2018         return result;
2019 }
2020
2021
2022 void * yaffs_getdev(const YCHAR *path)
2023 {
2024         struct yaffs_dev *dev=NULL;
2025         YCHAR *dummy;
2026         dev = yaffsfs_FindDevice(path,&dummy);
2027         return (void *)dev;
2028 }
2029
2030 int yaffs_mount2(const YCHAR *path,int read_only)
2031 {
2032         int retVal=-1;
2033         int result=YAFFS_FAIL;
2034         struct yaffs_dev *dev=NULL;
2035
2036         T(YAFFS_TRACE_MOUNT,(TSTR("yaffs: Mounting %s" TENDSTR),path));
2037
2038         if(yaffsfs_CheckPath(path) < 0){
2039                 yaffsfs_SetError(-ENAMETOOLONG);
2040                 return -1;
2041         }
2042
2043         yaffsfs_Lock();
2044
2045         yaffsfs_InitHandles();
2046
2047         dev = yaffsfs_FindMountPoint(path);
2048         if(dev){
2049                 if(!dev->is_mounted){
2050                         dev->read_only = read_only ? 1 : 0;
2051                         result = yaffs_guts_initialise(dev);
2052                         if(result == YAFFS_FAIL)
2053                                 /* todo error - mount failed */
2054                                 yaffsfs_SetError(-ENOMEM);
2055                         retVal = result ? 0 : -1;
2056
2057                 }
2058                 else
2059                         /* todo error - already mounted. */
2060                         yaffsfs_SetError(-EBUSY);
2061         } else
2062                 /* todo error - no device */
2063                 yaffsfs_SetError(-ENODEV);
2064
2065         yaffsfs_Unlock();
2066         return retVal;
2067
2068 }
2069
2070 int yaffs_mount(const YCHAR *path)
2071 {
2072         return yaffs_mount2(path,0);
2073 }
2074
2075 int yaffs_sync(const YCHAR *path)
2076 {
2077         int retVal=-1;
2078         struct yaffs_dev *dev=NULL;
2079         YCHAR *dummy;
2080
2081         if(yaffsfs_CheckPath(path) < 0){
2082                 yaffsfs_SetError(-ENAMETOOLONG);
2083                 return -1;
2084         }
2085         
2086         yaffsfs_Lock();
2087         dev = yaffsfs_FindDevice(path,&dummy);
2088         if(dev){
2089                 if(dev->is_mounted){
2090                         
2091                         yaffs_flush_whole_cache(dev);
2092                         yaffs_checkpoint_save(dev);
2093                         retVal = 0;
2094                         
2095                 } else
2096                         /* todo error - not mounted. */
2097                         yaffsfs_SetError(-EINVAL);
2098                         
2099         }else
2100                 /* todo error - no device */
2101                 yaffsfs_SetError(-ENODEV);
2102
2103         yaffsfs_Unlock();
2104         return retVal;  
2105 }
2106
2107
2108 int yaffs_remount(const YCHAR *path, int force, int read_only)
2109 {
2110         int retVal=-1;
2111         struct yaffs_dev *dev=NULL;
2112         yaffsfs_Handle *yh;
2113
2114         if(yaffsfs_CheckPath(path) < 0){
2115                 yaffsfs_SetError(-ENAMETOOLONG);
2116                 return -1;
2117         }
2118
2119         yaffsfs_Lock();
2120         dev = yaffsfs_FindMountPoint(path);
2121         if(dev){
2122                 if(dev->is_mounted){
2123                         int i;
2124                         int inUse;
2125
2126                         yaffs_flush_whole_cache(dev);
2127
2128                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
2129                                 yh = & yaffsfs_handle[i];
2130                                 if(yh->useCount>0 && 
2131                                         yaffsfs_inode[yh->inodeId].iObj->my_dev == dev)
2132                                         inUse = 1; /* the device is in use, can't unmount */
2133                         }
2134
2135                         if(!inUse || force){
2136                                 if(read_only)
2137                                         yaffs_checkpoint_save(dev);
2138                                 dev->read_only =  read_only ? 1 : 0;
2139                                 retVal = 0;
2140                         } else
2141                                 yaffsfs_SetError(-EBUSY);
2142
2143                 } else
2144                         yaffsfs_SetError(-EINVAL);
2145
2146         }
2147         else
2148                 yaffsfs_SetError(-ENODEV);
2149
2150         yaffsfs_Unlock();
2151         return retVal;
2152
2153 }
2154
2155 int yaffs_unmount2(const YCHAR *path, int force)
2156 {
2157         int retVal=-1;
2158         struct yaffs_dev *dev=NULL;
2159
2160         if(yaffsfs_CheckPath(path) < 0){
2161                 yaffsfs_SetError(-ENAMETOOLONG);
2162                 return -1;
2163         }
2164
2165         yaffsfs_Lock();
2166         dev = yaffsfs_FindMountPoint(path);
2167         if(dev){
2168                 if(dev->is_mounted){
2169                         int i;
2170                         int inUse;
2171
2172                         yaffs_flush_whole_cache(dev);
2173                         yaffs_checkpoint_save(dev);
2174
2175                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
2176                                 if(yaffsfs_handle[i].useCount > 0 && 
2177                                 yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
2178                                         inUse = 1; /* the device is in use, can't unmount */
2179                         }
2180
2181                         if(!inUse || force){
2182                                 if(inUse)
2183                                         yaffsfs_BreakDeviceHandles(dev);
2184                                 yaffs_deinitialise(dev);
2185
2186                                 retVal = 0;
2187                         } else
2188                                 /* todo error can't unmount as files are open */
2189                                 yaffsfs_SetError(-EBUSY);
2190
2191                 } else
2192                         /* todo error - not mounted. */
2193                         yaffsfs_SetError(-EINVAL);
2194
2195         } else
2196                 /* todo error - no device */
2197                 yaffsfs_SetError(-ENODEV);
2198
2199         yaffsfs_Unlock();
2200         return retVal;
2201
2202 }
2203
2204 int yaffs_unmount(const YCHAR *path)
2205 {
2206         return yaffs_unmount2(path,0);
2207 }
2208
2209 loff_t yaffs_freespace(const YCHAR *path)
2210 {
2211         loff_t retVal=-1;
2212         struct yaffs_dev *dev=NULL;
2213         YCHAR *dummy;
2214
2215         if(yaffsfs_CheckPath(path) < 0){
2216                 yaffsfs_SetError(-ENAMETOOLONG);
2217                 return -1;
2218         }
2219
2220         yaffsfs_Lock();
2221         dev = yaffsfs_FindDevice(path,&dummy);
2222         if(dev  && dev->is_mounted){
2223                 retVal = yaffs_get_n_free_chunks(dev);
2224                 retVal *= dev->data_bytes_per_chunk;
2225
2226         } else
2227                 yaffsfs_SetError(-EINVAL);
2228
2229         yaffsfs_Unlock();
2230         return retVal;
2231 }
2232
2233 loff_t yaffs_totalspace(const YCHAR *path)
2234 {
2235         loff_t retVal=-1;
2236         struct yaffs_dev *dev=NULL;
2237         YCHAR *dummy;
2238
2239         if(yaffsfs_CheckPath(path) < 0){
2240                 yaffsfs_SetError(-ENAMETOOLONG);
2241                 return -1;
2242         }
2243
2244         yaffsfs_Lock();
2245         dev = yaffsfs_FindDevice(path,&dummy);
2246         if(dev  && dev->is_mounted){
2247                 retVal = (dev->param.end_block - dev->param.start_block + 1) - 
2248                         dev->param.n_reserved_blocks;
2249                 retVal *= dev->param.chunks_per_block;
2250                 retVal *= dev->data_bytes_per_chunk;
2251
2252         } else
2253                 yaffsfs_SetError(-EINVAL);
2254
2255         yaffsfs_Unlock();
2256         return retVal;
2257 }
2258
2259 int yaffs_inodecount(const YCHAR *path)
2260 {
2261         loff_t retVal= -1;
2262         struct yaffs_dev *dev=NULL;
2263         YCHAR *dummy;
2264
2265         if(yaffsfs_CheckPath(path) < 0){
2266                 yaffsfs_SetError(-ENAMETOOLONG);
2267                 return -1;
2268         }
2269
2270         yaffsfs_Lock();
2271         dev = yaffsfs_FindDevice(path,&dummy);
2272         if(dev  && dev->is_mounted) {
2273            int n_obj = dev->n_obj;
2274            if(n_obj > dev->n_hardlinks)
2275                 retVal = n_obj - dev->n_hardlinks;
2276         }
2277         
2278         if(retVal < 0)
2279                 yaffsfs_SetError(-EINVAL);
2280         
2281         yaffsfs_Unlock();
2282         return retVal;  
2283 }
2284
2285
2286 void yaffs_add_device(struct yaffs_dev *dev)
2287 {
2288         dev->is_mounted = 0;
2289         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2290
2291         if(!dev->dev_list.next)
2292                 INIT_LIST_HEAD(&dev->dev_list);
2293
2294         list_add(&dev->dev_list,&yaffsfs_deviceList);
2295 }
2296
2297 void yaffs_remove_device(struct yaffs_dev *dev)
2298 {
2299         list_del_init(&dev->dev_list);
2300 }
2301
2302
2303
2304
2305 /* Directory search stuff. */
2306
2307 /*
2308  * Directory search context
2309  *
2310  * NB this is an opaque structure.
2311  */
2312
2313
2314 typedef struct
2315 {
2316         u32 magic;
2317         yaffs_dirent de;                /* directory entry being used by this dsc */
2318         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2319         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2320         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2321         int offset;
2322         struct list_head others;       
2323 } yaffsfs_DirectorySearchContext;
2324
2325
2326
2327 static struct list_head search_contexts;
2328
2329
2330 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2331 {
2332         if(dsc &&
2333            dsc->dirObj &&
2334            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2335
2336            dsc->offset = 0;
2337
2338            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2339                 dsc->nextReturn = NULL;
2340            else
2341                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2342                                                 struct yaffs_obj,siblings);
2343         } else {
2344                 /* Hey someone isn't playing nice! */
2345         }
2346 }
2347
2348 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2349 {
2350         if(dsc &&
2351            dsc->dirObj &&
2352            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2353
2354            if( dsc->nextReturn == NULL ||
2355                list_empty(&dsc->dirObj->variant.dir_variant.children))
2356                 dsc->nextReturn = NULL;
2357            else {
2358                    struct list_head *next = dsc->nextReturn->siblings.next;
2359
2360                    if( next == &dsc->dirObj->variant.dir_variant.children)
2361                         dsc->nextReturn = NULL; /* end of list */
2362                    else
2363                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2364            }
2365         } else {
2366                 /* Hey someone isn't playing nice! */
2367         }
2368 }
2369
2370 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2371 {
2372
2373         struct list_head *i;
2374         yaffsfs_DirectorySearchContext *dsc;
2375
2376         /* if search contexts not initilised then skip */
2377         if(!search_contexts.next)
2378                 return;
2379
2380         /* Iterate through the directory search contexts.
2381          * If any are the one being removed, then advance the dsc to
2382          * the next one to prevent a hanging ptr.
2383          */
2384          list_for_each(i, &search_contexts) {
2385                 if (i) {
2386                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2387                         if(dsc->nextReturn == obj)
2388                                 yaffsfs_DirAdvance(dsc);
2389                 }
2390         }
2391
2392 }
2393
2394 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2395 {
2396         yaffs_DIR *dir = NULL;
2397         struct yaffs_obj *obj = NULL;
2398         yaffsfs_DirectorySearchContext *dsc = NULL;
2399
2400         if(yaffsfs_CheckPath(dirname) < 0){
2401                 yaffsfs_SetError(-ENAMETOOLONG);
2402                 return -1;
2403         }
2404
2405         yaffsfs_Lock();
2406
2407         obj = yaffsfs_FindObject(NULL,dirname,0,1,NULL);
2408
2409         if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2410
2411                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2412                 dir = (yaffs_DIR *)dsc;
2413
2414                 if(dsc){
2415                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2416                         dsc->magic = YAFFS_MAGIC;
2417                         dsc->dirObj = obj;
2418                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2419                         INIT_LIST_HEAD(&dsc->others);
2420
2421                         if(!search_contexts.next)
2422                                 INIT_LIST_HEAD(&search_contexts);
2423
2424                         list_add(&dsc->others,&search_contexts);       
2425                         yaffsfs_SetDirRewound(dsc);
2426                 }
2427
2428         }
2429
2430         yaffsfs_Unlock();
2431
2432         return dir;
2433 }
2434
2435 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2436 {
2437         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2438         struct yaffs_dirent *retVal = NULL;
2439
2440         yaffsfs_Lock();
2441
2442         if(dsc && dsc->magic == YAFFS_MAGIC){
2443                 yaffsfs_SetError(0);
2444                 if(dsc->nextReturn){
2445                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2446                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2447                         dsc->de.d_off = dsc->offset++;
2448                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2449                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2450                         {
2451                                 /* this should not happen! */
2452                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2453                         }
2454                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2455                         retVal = &dsc->de;
2456                         yaffsfs_DirAdvance(dsc);
2457                 } else
2458                         retVal = NULL;
2459         } else
2460                 yaffsfs_SetError(-EBADF);
2461
2462         yaffsfs_Unlock();
2463
2464         return retVal;
2465
2466 }
2467
2468
2469 void yaffs_rewinddir(yaffs_DIR *dirp)
2470 {
2471         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2472
2473         yaffsfs_Lock();
2474
2475         yaffsfs_SetDirRewound(dsc);
2476
2477         yaffsfs_Unlock();
2478 }
2479
2480
2481 int yaffs_closedir(yaffs_DIR *dirp)
2482 {
2483         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2484
2485         yaffsfs_Lock();
2486         dsc->magic = 0;
2487         list_del(&dsc->others); /* unhook from list */
2488         YFREE(dsc);
2489         yaffsfs_Unlock();
2490         return 0;
2491 }
2492
2493 /* End of directory stuff */
2494
2495
2496 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2497 {
2498         struct yaffs_obj *parent = NULL;
2499         struct yaffs_obj *obj;
2500         YCHAR *name;
2501         int retVal= -1;
2502         int mode = 0; /* ignore for now */
2503
2504         if(yaffsfs_CheckPath(newpath) < 0){
2505                 yaffsfs_SetError(-ENAMETOOLONG);
2506                 return -1;
2507         }
2508         yaffsfs_Lock();
2509         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2510         if(!parent)
2511                 yaffsfs_SetError(-ENOTDIR);
2512         else if( strlen(name) < 1)
2513                 yaffsfs_SetError(-ENOENT);
2514         else if(parent->my_dev->read_only)
2515                 yaffsfs_SetError(-EROFS);
2516         else if(parent){
2517                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2518                 if(obj)
2519                         retVal = 0;
2520                 else if (yaffsfs_FindObject(NULL,newpath,0,0, NULL))
2521                         yaffsfs_SetError(-EEXIST);
2522                 else
2523                         yaffsfs_SetError(-ENOSPC);
2524         }
2525
2526         yaffsfs_Unlock();
2527
2528         return retVal;
2529
2530 }
2531
2532 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2533 {
2534         struct yaffs_obj *obj = NULL;
2535         struct yaffs_obj *dir = NULL;
2536         int retVal= -1;
2537
2538         yaffsfs_Lock();
2539
2540         obj = yaffsfs_FindObject(NULL,path,0,1, &dir);
2541
2542         if(!dir) 
2543                 yaffsfs_SetError(-ENOTDIR);
2544         else if(!obj) 
2545                 yaffsfs_SetError(-ENOENT);
2546         else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
2547                 yaffsfs_SetError(-EINVAL);
2548         else {
2549                 YCHAR *alias = obj->variant.symlink_variant.alias;
2550                 memset(buf,0,bufsiz);
2551                 yaffs_strncpy(buf,alias,bufsiz - 1);
2552                 retVal = 0;
2553         }
2554         yaffsfs_Unlock();
2555         return retVal;
2556 }
2557
2558 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
2559 {
2560         /* Creates a link called newpath to existing oldpath */
2561         struct yaffs_obj *obj = NULL;
2562         struct yaffs_obj *lnk = NULL;
2563         int retVal = -1;
2564
2565         if(yaffsfs_CheckPath(linkpath) < 0){
2566                 yaffsfs_SetError(-ENAMETOOLONG);
2567                 return -1;
2568         }
2569
2570         yaffsfs_Lock();
2571
2572         obj = yaffsfs_FindObject(NULL,oldpath,0,1,NULL);
2573         lnk = yaffsfs_FindObject(NULL,linkpath,0,0,NULL);
2574
2575         if(!obj)
2576                 yaffsfs_SetError(-ENOENT);
2577         else if(obj->my_dev->read_only)
2578                 yaffsfs_SetError(-EINVAL);
2579         else if(lnk)
2580                 yaffsfs_SetError(-EEXIST);
2581         else {
2582                 struct yaffs_obj *newdir = NULL;
2583                 struct yaffs_obj *link = NULL;
2584
2585                 YCHAR *newname;
2586
2587                 newdir = yaffsfs_FindDirectory(NULL,linkpath,&newname,0);
2588
2589                 if(!newdir)
2590                         yaffsfs_SetError(-ENOTDIR);
2591                 else if(newdir->my_dev != obj->my_dev)
2592                         yaffsfs_SetError(-EXDEV);
2593                 
2594                 retVal = yaffsfs_CheckNameLength(newname);
2595                 
2596                 if(retVal == 0) {
2597                         link = yaffs_link_obj(newdir,newname,obj);
2598                         if(link)
2599                                 retVal = 0;
2600                         else{
2601                                 yaffsfs_SetError(-ENOSPC);
2602                                 retVal = -1;
2603                         }
2604
2605                 }
2606         }
2607         yaffsfs_Unlock();
2608
2609         return retVal;
2610 }
2611
2612 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2613 {
2614         pathname=pathname;
2615         mode=mode;
2616         dev=dev;
2617
2618         yaffsfs_SetError(-EINVAL);
2619         return -1;
2620 }
2621
2622
2623
2624 /*
2625  * yaffs_n_handles()
2626  * Returns number of handles attached to the object
2627  */
2628 int yaffs_n_handles(const YCHAR *path)
2629 {
2630         struct yaffs_obj *obj;
2631
2632         if(yaffsfs_CheckPath(path) < 0){
2633                 yaffsfs_SetError(-ENAMETOOLONG);
2634                 return -1;
2635         }
2636
2637         obj = yaffsfs_FindObject(NULL,path,0,1,NULL);
2638
2639         if(obj)
2640                 return yaffsfs_CountHandles(obj);
2641         else
2642                 return -1;
2643 }
2644
2645 int yaffs_get_error(void)
2646 {
2647         return yaffsfs_GetLastError();
2648 }
2649
2650 int yaffs_set_error(int error)
2651 {
2652         /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2653         yaffsfs_SetError(error);
2654         return 0;
2655 }
2656
2657 int yaffs_dump_dev(const YCHAR *path)
2658 {
2659 #if 1
2660         path=path;
2661 #else
2662         YCHAR *rest;
2663
2664         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2665
2666         if(obj){
2667                 struct yaffs_dev *dev = obj->my_dev;
2668
2669                 printf("\n"
2670                            "n_page_writes.......... %d\n"
2671                            "n_page_reads........... %d\n"
2672                            "n_erasures....... %d\n"
2673                            "n_gc_copies............ %d\n"
2674                            "garbageCollections... %d\n"
2675                            "passiveGarbageColl'ns %d\n"
2676                            "\n",
2677                                 dev->n_page_writes,
2678                                 dev->n_page_reads,
2679                                 dev->n_erasures,
2680                                 dev->n_gc_copies,
2681                                 dev->garbageCollections,
2682                                 dev->passiveGarbageCollections
2683                 );
2684
2685         }
2686
2687 #endif
2688         return 0;
2689 }
2690