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