645db71462691b5037e4dda29447b1ff5c0f3ced
[yaffs2.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2007 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 <string.h> // for memset
18 #include "yportenv.h"
19
20 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
21
22 #ifndef NULL
23 #define NULL ((void *)0)
24 #endif
25
26
27 const char *yaffsfs_c_version="$Id: yaffsfs.c,v 1.30 2010-01-06 04:00:23 charles Exp $";
28
29 // configurationList is the list of devices that are supported
30 static yaffsfs_DeviceConfiguration *yaffsfs_configurationList;
31
32
33 /* Some forward references */
34 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path, int symDepth);
35 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj);
36
37
38 // Handle management.
39 //
40
41
42 unsigned int yaffs_wr_attempts;
43
44 typedef struct {
45         int count;
46         yaffs_Object *iObj;
47 } yaffsfs_Inode;
48
49 typedef struct{
50         __u8  inUse:1;          // this handle is in use
51         __u8  readOnly:1;       // this handle is read only
52         __u8  append:1;         // append only
53         __u8  exclusive:1;      // exclusive
54         int   inodeId:20;       // the object
55         __u32 position;         // current position in file
56 }yaffsfs_Handle;
57
58 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
59 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
60
61 // yaffsfs_InitHandle
62 /// Inilitalise handles on start-up.
63 //
64 static int yaffsfs_InitHandles(void)
65 {
66         int i;
67         memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
68         memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
69         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
70                 yaffsfs_handle[i].inodeId = -1;
71
72         return 0;
73 }
74
75 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
76 {
77         if(h < 0 || h >= YAFFSFS_N_HANDLES)
78                 return NULL;
79
80         return &yaffsfs_handle[h];
81 }
82
83 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
84 {
85         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
86
87         if(h && h->inUse && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
88                 return  &yaffsfs_inode[h->inodeId];
89
90         return NULL;
91 }
92
93 yaffs_Object *yaffsfs_GetHandleObject(int handle)
94 {
95         yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
96
97         if(in)
98                 return in->iObj;
99
100         return NULL;
101 }
102
103 //yaffsfs_GetInodeIdForObject
104 // Grab an inode entry when opening a new inode.
105 //
106
107 static int yaffsfs_GetInodeIdForObject(yaffs_Object *obj)
108 {
109         int i;
110         int ret = -1;
111         yaffsfs_Inode *in = NULL;
112         
113         if(obj)
114                 obj = yaffs_GetEquivalentObject(obj);
115
116         /* Look for it. If we can't find it then make one */
117         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
118                 if(yaffsfs_inode[i].iObj == obj)
119                         ret = i;
120         }
121
122         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
123                 if(!yaffsfs_inode[i].iObj)
124                         ret = i;
125         }
126         
127         
128         if(ret>=0){
129                 in = &yaffsfs_inode[ret];
130                 if(!in->iObj)
131                         in->count = 0;
132                 in->iObj = obj;
133                 in->count++;
134         }
135         
136         
137         return ret;
138 }
139
140 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
141 {
142         yaffs_Object *obj;
143         
144         obj = in->iObj;
145
146         if(obj->unlinked)
147                 yaffs_DeleteObject(obj);
148         
149         obj->myInode = NULL;
150         in->iObj = NULL;
151
152 }
153
154 static void yaffsfs_PutInode(int inodeId)
155 {
156         if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
157                 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
158                 in->count--;
159                 if(in->count <= 0)
160                         yaffsfs_ReleaseInode(in);
161         }       
162 }
163
164
165 //yaffsfs_GetHandle
166 // Grab a handle (when opening a file)
167 //
168
169 static int yaffsfs_GetHandle(void)
170 {
171         int i;
172         yaffsfs_Handle *h;
173
174         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
175                 h = yaffsfs_GetHandlePointer(i);
176                 if(!h){
177                         // todo bug: should never happen
178                 }
179                 if(!h->inUse){
180                         memset(h,0,sizeof(yaffsfs_Handle));
181                         h->inodeId=-1;
182                         h->inUse=1;
183                         return i;
184                 }
185         }
186         return -1;
187 }
188
189 // yaffs_PutHandle
190 // Let go of a handle when closing a file or aborting an open.
191 //
192 static int yaffsfs_PutHandle(int handle)
193 {
194         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
195
196         if(h){
197                 if(h->inodeId >= 0)
198                         yaffsfs_PutInode(h->inodeId);
199                 h->inUse = 0;
200                 h->inodeId = -1;
201         }
202         return 0;
203 }
204
205
206
207
208 // Stuff to search for a directory from a path
209
210
211 int yaffsfs_Match(YCHAR a, YCHAR b)
212 {
213         // case sensitive
214         return (a == b);
215 }
216
217 int yaffsfs_IsPathDivider(YCHAR ch)
218 {
219         YCHAR *str = YAFFS_PATH_DIVIDERS;
220
221         while(*str){
222                 if(*str == ch)
223                         return 1;
224                 str++;
225         }
226
227         return 0;
228 }
229
230 // yaffsfs_FindDevice
231 // yaffsfs_FindRoot
232 // Scan the configuration list to find the root.
233 // Curveballs: Should match paths that end in '/' too
234 // Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
235 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
236 {
237         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
238         const YCHAR *leftOver;
239         const YCHAR *p;
240         yaffs_Device *retval = NULL;
241         int thisMatchLength;
242         int longestMatch = -1;
243         int matching;
244
245         // Check all configs, choose the one that:
246         // 1) Actually matches a prefix (ie /a amd /abc will not match
247         // 2) Matches the longest.
248         while(cfg && cfg->prefix && cfg->dev){
249                 leftOver = path;
250                 p = cfg->prefix;
251                 thisMatchLength = 0;
252                 matching = 1;
253
254
255                 while(matching && *p && *leftOver){
256                         // Skip over any /s
257                         while(yaffsfs_IsPathDivider(*p))
258                               p++;
259
260                         // Skip over any /s
261                         while(yaffsfs_IsPathDivider(*leftOver))
262                               leftOver++;
263
264                         // Now match the text part
265                         while(matching &&
266                               *p && !yaffsfs_IsPathDivider(*p) &&
267                               *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
268                                 if(yaffsfs_Match(*p,*leftOver)){
269                                         p++;
270                                         leftOver++;
271                                         thisMatchLength++;
272                                 } else {
273                                         matching = 0;
274                                 }
275                         }
276                 }
277
278                 // Skip over any /s in leftOver
279                 while(yaffsfs_IsPathDivider(*leftOver))
280                       leftOver++;
281                 
282
283                 if( matching && (thisMatchLength > longestMatch)){
284                         // Matched prefix
285                         *restOfPath = (YCHAR *)leftOver;
286                         retval = cfg->dev;
287                         longestMatch = thisMatchLength;
288                 }
289
290                 cfg++;
291         }
292         return retval;
293 }
294
295 #if 0
296 static yaffs_Device *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
297 {
298         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
299         const YCHAR *leftOver;
300         const YCHAR *p;
301         yaffs_Device *retval = NULL;
302         int thisMatchLength;
303         int longestMatch = -1;
304
305         // Check all configs, choose the one that:
306         // 1) Actually matches a prefix (ie /a amd /abc will not match
307         // 2) Matches the longest.
308         while(cfg && cfg->prefix && cfg->dev){
309                 leftOver = path;
310                 p = cfg->prefix;
311                 thisMatchLength = 0;
312
313                 while(*p &&  //unmatched part of prefix
314                       !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) && // the rest of the prefix is not / (to catch / at end)
315                       *leftOver &&
316                       yaffsfs_Match(*p,*leftOver)){
317                         p++;
318                         leftOver++;
319                         thisMatchLength++;
320                 }
321
322
323                 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) &&      // end of prefix
324                    (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && // no more in this path name part
325                    (thisMatchLength > longestMatch)){
326                         // Matched prefix
327                         *restOfPath = (YCHAR *)leftOver;
328                         retval = cfg->dev;
329                         longestMatch = thisMatchLength;
330                 }
331                 cfg++;
332         }
333         return retval;
334 }
335 #endif
336
337 static yaffs_Object *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
338 {
339
340         yaffs_Device *dev;
341
342         dev= yaffsfs_FindDevice(path,restOfPath);
343         if(dev && dev->isMounted){
344                 return dev->rootDir;
345         }
346         return NULL;
347 }
348
349 static yaffs_Object *yaffsfs_FollowLink(yaffs_Object *obj,int symDepth)
350 {
351
352         while(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
353                 YCHAR *alias = obj->variant.symLinkVariant.alias;
354
355                 if(yaffsfs_IsPathDivider(*alias))
356                         // Starts with a /, need to scan from root up
357                         obj = yaffsfs_FindObject(NULL,alias,symDepth++);
358                 else
359                         // Relative to here, so use the parent of the symlink as a start
360                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++);
361         }
362         return obj;
363 }
364
365
366 // yaffsfs_FindDirectory
367 // Parse a path to determine the directory and the name within the directory.
368 //
369 // eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
370 static yaffs_Object *yaffsfs_DoFindDirectory(yaffs_Object *startDir,const YCHAR *path,YCHAR **name,int symDepth)
371 {
372         yaffs_Object *dir;
373         YCHAR *restOfPath;
374         YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
375         int i;
376
377         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
378                 return NULL;
379
380         if(startDir){
381                 dir = startDir;
382                 restOfPath = (YCHAR *)path;
383         }
384         else
385                 dir = yaffsfs_FindRoot(path,&restOfPath);
386
387         while(dir){
388                 // parse off /.
389                 // curve ball: also throw away surplus '/'
390                 // eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
391                 while(yaffsfs_IsPathDivider(*restOfPath))
392                         restOfPath++; // get rid of '/'
393
394                 *name = restOfPath;
395                 i = 0;
396
397                 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
398                         if (i < YAFFS_MAX_NAME_LENGTH){
399                                 str[i] = *restOfPath;
400                                 str[i+1] = '\0';
401                                 i++;
402                         }
403                         restOfPath++;
404                 }
405
406                 if(!*restOfPath)
407                         // got to the end of the string
408                         return dir;
409                 else{
410                         if(yaffs_strcmp(str,_Y(".")) == 0)
411                         {
412                                 // Do nothing
413                         }
414                         else if(yaffs_strcmp(str,_Y("..")) == 0)
415                                 dir = dir->parent;
416                         else{
417                                 dir = yaffs_FindObjectByName(dir,str);
418
419                                 while(dir && dir->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
420                                         dir = yaffsfs_FollowLink(dir,symDepth);
421
422
423                                 if(dir && dir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
424                                         dir = NULL;
425                         }
426                 }
427         }
428         // directory did not exist.
429         return NULL;
430 }
431
432 static yaffs_Object *yaffsfs_FindDirectory(yaffs_Object *relativeDirectory,const YCHAR *path,YCHAR **name,int symDepth)
433 {
434         return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
435 }
436
437 // yaffsfs_FindObject turns a path for an existing object into the object
438 //
439 static yaffs_Object *yaffsfs_FindObject(yaffs_Object *relativeDirectory, const YCHAR *path,int symDepth)
440 {
441         yaffs_Object *dir;
442         YCHAR *name;
443
444         dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
445
446         if(dir && *name)
447                 return yaffs_FindObjectByName(dir,name);
448
449         return dir;
450 }
451
452
453 int yaffs_dup(int fd)
454 {
455         int newHandle = -1;
456         yaffsfs_Handle *oldPtr = NULL;
457         yaffsfs_Handle *newPtr = NULL;
458
459         yaffsfs_Lock();
460
461         oldPtr = yaffsfs_GetHandlePointer(fd);
462         if(oldPtr && oldPtr->inUse)
463                 newHandle = yaffsfs_GetHandle();
464         if(newHandle >= 0)
465                 newPtr = yaffsfs_GetHandlePointer(newHandle);
466
467         if(newPtr){
468                 *newPtr = *oldPtr;
469                 return newHandle;
470         }
471
472         if(!oldPtr)
473                 yaffsfs_SetError(-EBADF);
474         else
475                 yaffsfs_SetError(-ENOMEM);
476
477         return -1;
478
479 }
480
481 int yaffs_open(const YCHAR *path, int oflag, int mode)
482 {
483         yaffs_Object *obj = NULL;
484         yaffs_Object *dir = NULL;
485         YCHAR *name;
486         int handle = -1;
487         yaffsfs_Handle *h = NULL;
488         int alreadyOpen = 0;
489         int alreadyExclusive = 0;
490         int openDenied = 0;
491         int symDepth = 0;
492         int errorReported = 0;
493
494         int i;
495
496
497         // todo sanity check oflag (eg. can't have O_TRUNC without WRONLY or RDWR
498
499
500         yaffsfs_Lock();
501
502         handle = yaffsfs_GetHandle();
503
504         if(handle >= 0){
505
506                 h = yaffsfs_GetHandlePointer(handle);
507
508
509                 // try to find the exisiting object
510                 obj = yaffsfs_FindObject(NULL,path,0);
511
512                 if(obj && obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
513                         obj = yaffsfs_FollowLink(obj,symDepth++);
514
515                 if(obj)
516                         obj = yaffs_GetEquivalentObject(obj);
517
518                 if(obj && obj->variantType != YAFFS_OBJECT_TYPE_FILE)
519                         obj = NULL;
520
521                 if(obj){
522                         // Check if the object is already in use
523                         alreadyOpen = alreadyExclusive = 0;
524
525                         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
526                                 if(i != handle &&
527                                    yaffsfs_handle[i].inUse &&
528                                     obj == yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj){
529                                         alreadyOpen = 1;
530                                         if(yaffsfs_handle[i].exclusive)
531                                                 alreadyExclusive = 1;
532                                  }
533                         }
534
535                         if(((oflag & O_EXCL) && alreadyOpen) || alreadyExclusive)
536                                 openDenied = 1;
537
538                         // Open should fail if O_CREAT and O_EXCL are specified
539                         if((oflag & O_EXCL) && (oflag & O_CREAT)){
540                                 openDenied = 1;
541                                 yaffsfs_SetError(-EEXIST);
542                                 errorReported = 1;
543                         }
544
545                         // Check file permissions
546                         if( (oflag & (O_RDWR | O_WRONLY)) == 0 &&     // ie O_RDONLY
547                            !(obj->yst_mode & S_IREAD))
548                                 openDenied = 1;
549
550                         if( (oflag & O_RDWR) &&
551                            !(obj->yst_mode & S_IREAD))
552                                 openDenied = 1;
553
554                         if( (oflag & (O_RDWR | O_WRONLY)) &&
555                            !(obj->yst_mode & S_IWRITE))
556                                 openDenied = 1;
557
558                 } else if((oflag & O_CREAT)) {
559                         // Let's see if we can create this file
560                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
561                         if(dir)
562                                 obj = yaffs_MknodFile(dir,name,mode,0,0);
563                         else {
564                                 yaffsfs_SetError(-ENOTDIR);
565                                 errorReported = 1;
566                         }
567                 }
568
569                 if(obj && !openDenied) {
570                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
571
572                         if(inodeId<0) {
573                                 /*
574                                  * Todo: Fix any problem if inodes run out, though that
575                                  * can't happen if the number of inode items >= number of handles. 
576                                  */
577                         }
578                         
579                         h->inodeId = inodeId;
580                         h->inUse = 1;
581                         h->readOnly = (oflag & (O_WRONLY | O_RDWR)) ? 0 : 1;
582                         h->append =  (oflag & O_APPEND) ? 1 : 0;
583                         h->exclusive = (oflag & O_EXCL) ? 1 : 0;
584                         h->position = 0;
585
586                         /* Hook inode to object */
587                         obj->myInode = (void*) &yaffsfs_inode[inodeId];
588
589                         if((oflag & O_TRUNC) && !h->readOnly)
590                                 yaffs_ResizeFile(obj,0);
591                 } else {
592                         yaffsfs_PutHandle(handle);
593                         if(!errorReported) {
594                                 yaffsfs_SetError(-EACCES);
595                                 errorReported = 1;
596                         }
597                         handle = -1;
598                 }
599         }
600
601         yaffsfs_Unlock();
602
603         return handle;
604 }
605
606 int yaffs_Dofsync(int fd,int datasync)
607 {
608         yaffsfs_Handle *h = NULL;
609         int retVal = 0;
610
611         yaffsfs_Lock();
612
613         h = yaffsfs_GetHandlePointer(fd);
614
615         if(h && h->inUse)
616                 // flush the file
617                 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,datasync);
618         else {
619                 // bad handle
620                 yaffsfs_SetError(-EBADF);
621                 retVal = -1;
622         }
623
624         yaffsfs_Unlock();
625
626         return retVal;
627 }
628
629 int yaffs_fsync(int fd)
630 {
631         return yaffs_Dofsync(fd,0);
632 }
633
634 int yaffs_flush(int fd)
635 {
636         return yaffs_fsync(fd);
637 }
638
639 int yaffs_fdatasync(int fd)
640 {
641         return yaffs_Dofsync(fd,1);
642 }
643
644 int yaffs_close(int fd)
645 {
646         yaffsfs_Handle *h = NULL;
647         int retVal = 0;
648
649         yaffsfs_Lock();
650
651         h = yaffsfs_GetHandlePointer(fd);
652
653         if(h && h->inUse) {
654                 // clean up
655                 yaffs_FlushFile(yaffsfs_inode[h->inodeId].iObj,1,0);
656                 yaffsfs_PutHandle(fd);
657                 retVal = 0;
658         } else {
659                 // bad handle
660                 yaffsfs_SetError(-EBADF);
661                 retVal = -1;
662         }
663
664         yaffsfs_Unlock();
665
666         return retVal;
667 }
668
669 int yaffs_read(int fd, void *buf, unsigned int nbyte)
670 {
671         yaffsfs_Handle *h = NULL;
672         yaffs_Object *obj = NULL;
673         int pos = 0;
674         int nRead = -1;
675         unsigned int maxRead;
676
677         yaffsfs_Lock();
678         h = yaffsfs_GetHandlePointer(fd);
679         obj = yaffsfs_GetHandleObject(fd);
680
681         if(!h || !obj)
682                 // bad handle
683                 yaffsfs_SetError(-EBADF);
684
685         else if( h && obj){
686                 pos=  h->position;
687                 if(yaffs_GetObjectFileLength(obj) > pos)
688                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
689                 else
690                         maxRead = 0;
691
692                 if(nbyte > maxRead)
693                         nbyte = maxRead;
694
695
696                 if(nbyte > 0) {
697                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
698                         if(nRead >= 0)
699                                 h->position = pos + nRead;
700                         else {
701                                 //todo error
702                         }
703                 } else
704                         nRead = 0;
705
706         }
707
708         yaffsfs_Unlock();
709
710
711         return (nRead >= 0) ? nRead : -1;
712
713 }
714
715 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
716 {
717         yaffsfs_Handle *h = NULL;
718         yaffs_Object *obj = NULL;
719         int pos = 0;
720         int nRead = -1;
721         unsigned int maxRead;
722
723         yaffsfs_Lock();
724         h = yaffsfs_GetHandlePointer(fd);
725         obj = yaffsfs_GetHandleObject(fd);
726
727         if(!h || !obj)
728                 // bad handle
729                 yaffsfs_SetError(-EBADF);
730         else if( h && obj) {
731                 pos= offset;
732                 if(yaffs_GetObjectFileLength(obj) > pos)
733                         maxRead = yaffs_GetObjectFileLength(obj) - pos;
734                 else
735                         maxRead = 0;
736
737                 if(nbyte > maxRead)
738                         nbyte = maxRead;
739
740
741                 if(nbyte > 0)
742                         nRead = yaffs_ReadDataFromFile(obj,buf,pos,nbyte);
743                 else
744                         nRead = 0;
745         }
746
747         yaffsfs_Unlock();
748
749
750         return (nRead >= 0) ? nRead : -1;
751
752 }
753
754 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
755 {
756         yaffsfs_Handle *h = NULL;
757         yaffs_Object *obj = NULL;
758         int pos = 0;
759         int nWritten = -1;
760         int writeThrough = 0;
761
762         yaffsfs_Lock();
763         h = yaffsfs_GetHandlePointer(fd);
764         obj = yaffsfs_GetHandleObject(fd);
765
766         if(!h || !obj)
767                 // bad handle
768                 yaffsfs_SetError(-EBADF);
769         else if( h && obj && h->readOnly){
770                 // todo error
771         } else if( h && obj){
772                 if(h->append)
773                         pos =  yaffs_GetObjectFileLength(obj);
774                 else
775                         pos = h->position;
776
777                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
778
779                 if(nWritten >= 0)
780                         h->position = pos + nWritten;
781                 else {
782                         //todo error
783                 }
784
785         }
786
787         yaffsfs_Unlock();
788
789
790         return (nWritten >= 0) ? nWritten : -1;
791
792 }
793
794 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
795 {
796         yaffsfs_Handle *h = NULL;
797         yaffs_Object *obj = NULL;
798         int pos = 0;
799         int nWritten = -1;
800         int writeThrough = 0;
801
802         yaffsfs_Lock();
803         h = yaffsfs_GetHandlePointer(fd);
804         obj = yaffsfs_GetHandleObject(fd);
805
806         if(!h || !obj)
807                 // bad handle
808                 yaffsfs_SetError(-EBADF);
809         else if( h && obj && h->readOnly){
810                 // todo error
811         }
812         else if( h && obj){
813                 pos = offset;
814
815                 nWritten = yaffs_WriteDataToFile(obj,buf,pos,nbyte,writeThrough);
816
817                 if(nWritten < 0 || ((unsigned int)nWritten) < nbyte)
818                         yaffsfs_SetError(-ENOSPC);
819
820         }
821
822         yaffsfs_Unlock();
823
824
825         return (nWritten >= 0) ? nWritten : -1;
826
827 }
828
829
830 int yaffs_truncate(const YCHAR *path,off_t newSize)
831 {
832         yaffs_Object *obj = NULL;
833         int result = YAFFS_FAIL;
834
835         yaffsfs_Lock();
836
837         obj = yaffsfs_FindObject(NULL,path,0);
838         if(obj)
839                 obj = yaffs_GetEquivalentObject(obj);
840
841         if(!obj)
842                 yaffsfs_SetError(-ENOENT);
843         else if(obj->variantType != YAFFS_OBJECT_TYPE_FILE)
844                 yaffsfs_SetError(-EISDIR);
845         else
846                 result = yaffs_ResizeFile(obj,newSize);
847
848         yaffsfs_Unlock();
849
850
851         return (result) ? 0 : -1;
852 }
853
854 int yaffs_ftruncate(int fd, off_t newSize)
855 {
856         yaffsfs_Handle *h = NULL;
857         yaffs_Object *obj = NULL;
858         int result = 0;
859
860         yaffsfs_Lock();
861         h = yaffsfs_GetHandlePointer(fd);
862         obj = yaffsfs_GetHandleObject(fd);
863
864         if(!h || !obj)
865                 // bad handle
866                 yaffsfs_SetError(-EBADF);
867         else
868                 // resize the file
869                 result = yaffs_ResizeFile(obj,newSize);
870         yaffsfs_Unlock();
871
872
873         return (result) ? 0 : -1;
874
875 }
876
877 off_t yaffs_lseek(int fd, off_t offset, int whence)
878 {
879         yaffsfs_Handle *h = NULL;
880         yaffs_Object *obj = NULL;
881         int pos = -1;
882         int fSize = -1;
883
884         yaffsfs_Lock();
885         h = yaffsfs_GetHandlePointer(fd);
886         obj = yaffsfs_GetHandleObject(fd);
887
888         if(!h || !obj)
889                 // bad handle
890                 yaffsfs_SetError(-EBADF);
891         else if(whence == SEEK_SET){
892                 if(offset >= 0)
893                         pos = offset;
894         }
895         else if(whence == SEEK_CUR) {
896                 if( (h->position + offset) >= 0)
897                         pos = (h->position + offset);
898         }
899         else if(whence == SEEK_END) {
900                 fSize = yaffs_GetObjectFileLength(obj);
901                 if(fSize >= 0 && (fSize + offset) >= 0)
902                         pos = fSize + offset;
903         }
904
905         if(pos >= 0)
906                 h->position = pos;
907         else {
908                 // todo error
909         }
910
911
912         yaffsfs_Unlock();
913
914         return pos;
915 }
916
917
918 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
919 {
920         yaffs_Object *dir = NULL;
921         yaffs_Object *obj = NULL;
922         YCHAR *name;
923         int result = YAFFS_FAIL;
924
925         yaffsfs_Lock();
926
927         obj = yaffsfs_FindObject(NULL,path,0);
928         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
929         if(!dir)
930                 yaffsfs_SetError(-ENOTDIR);
931         else if(!obj)
932                 yaffsfs_SetError(-ENOENT);
933         else if(!isDirectory && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
934                 yaffsfs_SetError(-EISDIR);
935         else if(isDirectory && obj->variantType != YAFFS_OBJECT_TYPE_DIRECTORY)
936                 yaffsfs_SetError(-ENOTDIR);
937         else {
938                 result = yaffs_Unlink(dir,name);
939
940                 if(result == YAFFS_FAIL && isDirectory)
941                         yaffsfs_SetError(-ENOTEMPTY);
942         }
943
944         yaffsfs_Unlock();
945
946         // todo error
947
948         return (result == YAFFS_FAIL) ? -1 : 0;
949 }
950
951
952 int yaffs_rmdir(const YCHAR *path)
953 {
954         return yaffsfs_DoUnlink(path,1);
955 }
956
957 int yaffs_unlink(const YCHAR *path)
958 {
959         return yaffsfs_DoUnlink(path,0);
960 }
961
962 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
963 {
964         yaffs_Object *olddir = NULL;
965         yaffs_Object *newdir = NULL;
966         yaffs_Object *obj = NULL;
967         YCHAR *oldname;
968         YCHAR *newname;
969         int result= YAFFS_FAIL;
970         int renameAllowed = 1;
971
972         yaffsfs_Lock();
973
974         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
975         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
976         obj = yaffsfs_FindObject(NULL,oldPath,0);
977
978         if(!olddir || !newdir || !obj) {
979                 // bad file
980                 yaffsfs_SetError(-EBADF);
981                 renameAllowed = 0;
982         } else if(olddir->myDev != newdir->myDev) {
983                 // oops must be on same device
984                 // todo error
985                 yaffsfs_SetError(-EXDEV);
986                 renameAllowed = 0;
987         } else if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
988                 // It is a directory, check that it is not being renamed to
989                 // being its own decendent.
990                 // Do this by tracing from the new directory back to the root, checking for obj
991
992                 yaffs_Object *xx = newdir;
993
994                 while( renameAllowed && xx){
995                         if(xx == obj)
996                                 renameAllowed = 0;
997                         xx = xx->parent;
998                 }
999                 if(!renameAllowed)
1000                         yaffsfs_SetError(-EACCES);
1001         }
1002
1003         if(renameAllowed)
1004                 result = yaffs_RenameObject(olddir,oldname,newdir,newname);
1005
1006         yaffsfs_Unlock();
1007
1008         return (result == YAFFS_FAIL) ? -1 : 0;
1009 }
1010
1011
1012 static int yaffsfs_DoStat(yaffs_Object *obj,struct yaffs_stat *buf)
1013 {
1014         int retVal = -1;
1015
1016         if(obj)
1017                 obj = yaffs_GetEquivalentObject(obj);
1018
1019         if(obj && buf){
1020                 buf->st_dev = (int)obj->myDev->genericDevice;
1021                 buf->st_ino = obj->objectId;
1022                 buf->st_mode = obj->yst_mode & ~S_IFMT; // clear out file type bits
1023
1024                 if(obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY)
1025                         buf->st_mode |= S_IFDIR;
1026                 else if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK)
1027                         buf->st_mode |= S_IFLNK;
1028                 else if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
1029                         buf->st_mode |= S_IFREG;
1030
1031                 buf->st_nlink = yaffs_GetObjectLinkCount(obj);
1032                 buf->st_uid = 0;
1033                 buf->st_gid = 0;;
1034                 buf->st_rdev = obj->yst_rdev;
1035                 buf->st_size = yaffs_GetObjectFileLength(obj);
1036                 buf->st_blksize = obj->myDev->nDataBytesPerChunk;
1037                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1038 #if CONFIG_YAFFS_WINCE
1039                 buf->yst_wince_atime[0] = obj->win_atime[0];
1040                 buf->yst_wince_atime[1] = obj->win_atime[1];
1041                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1042                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1043                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1044                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1045 #else
1046                 buf->yst_atime = obj->yst_atime;
1047                 buf->yst_ctime = obj->yst_ctime;
1048                 buf->yst_mtime = obj->yst_mtime;
1049 #endif
1050                 retVal = 0;
1051         }
1052         return retVal;
1053 }
1054
1055 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1056 {
1057         yaffs_Object *obj;
1058
1059         int retVal = -1;
1060
1061         yaffsfs_Lock();
1062         obj = yaffsfs_FindObject(NULL,path,0);
1063
1064         if(!doLStat && obj)
1065                 obj = yaffsfs_FollowLink(obj,0);
1066
1067         if(obj)
1068                 retVal = yaffsfs_DoStat(obj,buf);
1069         else
1070                 // todo error not found
1071                 yaffsfs_SetError(-ENOENT);
1072
1073         yaffsfs_Unlock();
1074
1075         return retVal;
1076
1077 }
1078
1079 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1080 {
1081         return yaffsfs_DoStatOrLStat(path,buf,0);
1082 }
1083
1084 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1085 {
1086         return yaffsfs_DoStatOrLStat(path,buf,1);
1087 }
1088
1089 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1090 {
1091         yaffs_Object *obj;
1092
1093         int retVal = -1;
1094
1095         yaffsfs_Lock();
1096         obj = yaffsfs_GetHandleObject(fd);
1097
1098         if(obj)
1099                 retVal = yaffsfs_DoStat(obj,buf);
1100         else
1101                 // bad handle
1102                 yaffsfs_SetError(-EBADF);
1103
1104         yaffsfs_Unlock();
1105
1106         return retVal;
1107 }
1108
1109 #ifdef CONFIG_YAFFS_WINCE
1110 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1111 {
1112         yaffs_Object *obj;
1113
1114         int retVal = -1;
1115
1116         yaffsfs_Lock();
1117         obj = yaffsfs_GetHandleObject(fd);
1118
1119         if(obj){
1120
1121                 if(wctime){
1122                         wctime[0] = obj->win_ctime[0];
1123                         wctime[1] = obj->win_ctime[1];
1124                 }
1125                 if(watime){
1126                         watime[0] = obj->win_atime[0];
1127                         watime[1] = obj->win_atime[1];
1128                 }
1129                 if(wmtime){
1130                         wmtime[0] = obj->win_mtime[0];
1131                         wmtime[1] = obj->win_mtime[1];
1132                 }
1133
1134
1135                 retVal = 0;
1136         } else
1137                 // bad handle
1138                 yaffsfs_SetError(-EBADF);               
1139         
1140         yaffsfs_Unlock();
1141         
1142         return retVal;
1143 }
1144
1145
1146 int yaffs_set_wince_times(int fd, 
1147                                                   const unsigned *wctime, 
1148                                                   const unsigned *watime, 
1149                                                   const unsigned *wmtime)
1150 {
1151         yaffs_Object *obj;
1152         int result;
1153         int retVal = -1;
1154
1155         yaffsfs_Lock();
1156         obj = yaffsfs_GetHandleObject(fd);
1157
1158         if(obj){
1159
1160                 if(wctime){
1161                         obj->win_ctime[0] = wctime[0];
1162                         obj->win_ctime[1] = wctime[1];
1163                 }
1164                 if(watime){
1165                         obj->win_atime[0] = watime[0];
1166                         obj->win_atime[1] = watime[1];
1167                 }
1168                 if(wmtime){
1169                         obj->win_mtime[0] = wmtime[0];
1170                         obj->win_mtime[1] = wmtime[1];
1171                 }
1172
1173                 obj->dirty = 1;
1174                 result = yaffs_FlushFile(obj,0,0);
1175                 retVal = 0;
1176         } else
1177                 // bad handle
1178                 yaffsfs_SetError(-EBADF);
1179
1180         yaffsfs_Unlock();
1181
1182         return retVal;
1183 }
1184
1185 #endif
1186
1187
1188 static int yaffsfs_DoChMod(yaffs_Object *obj,mode_t mode)
1189 {
1190         int result = -1;
1191
1192         if(obj)
1193                 obj = yaffs_GetEquivalentObject(obj);
1194
1195         if(obj) {
1196                 obj->yst_mode = mode;
1197                 obj->dirty = 1;
1198                 result = yaffs_FlushFile(obj,0,0);
1199         }
1200
1201         return result == YAFFS_OK ? 0 : -1;
1202 }
1203
1204
1205 int yaffs_access(const YCHAR *path, int amode)
1206 {
1207         yaffs_Object *obj;
1208
1209         int retval = 0;
1210
1211         yaffsfs_Lock();
1212         obj = yaffsfs_FindObject(NULL,path,0);
1213
1214         if(obj) {
1215                 int access_ok = 1;
1216
1217                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1218                         access_ok = 0;
1219                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1220                         access_ok = 0;
1221                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1222                         access_ok = 0;
1223
1224                 if(!access_ok) {
1225                         yaffsfs_SetError(-EACCES);
1226                         retval = -1;
1227                 }
1228         } else {
1229                 // todo error not found
1230                 yaffsfs_SetError(-ENOENT);
1231                 retval = -1;
1232         }
1233
1234         yaffsfs_Unlock();
1235
1236         return retval;
1237
1238 }
1239
1240
1241 int yaffs_chmod(const YCHAR *path, mode_t mode)
1242 {
1243         yaffs_Object *obj;
1244
1245         int retVal = -1;
1246
1247         yaffsfs_Lock();
1248         obj = yaffsfs_FindObject(NULL,path,0);
1249
1250         if(obj)
1251                 retVal = yaffsfs_DoChMod(obj,mode);
1252         else
1253                 // todo error not found
1254                 yaffsfs_SetError(-ENOENT);
1255
1256         yaffsfs_Unlock();
1257
1258         return retVal;
1259
1260 }
1261
1262
1263 int yaffs_fchmod(int fd, mode_t mode)
1264 {
1265         yaffs_Object *obj;
1266
1267         int retVal = -1;
1268
1269         yaffsfs_Lock();
1270         obj = yaffsfs_GetHandleObject(fd);
1271
1272         if(obj)
1273                 retVal = yaffsfs_DoChMod(obj,mode);
1274         else
1275                 // bad handle
1276                 yaffsfs_SetError(-EBADF);
1277
1278         yaffsfs_Unlock();
1279
1280         return retVal;
1281 }
1282
1283
1284 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1285 {
1286         yaffs_Object *parent = NULL;
1287         yaffs_Object *dir = NULL;
1288         YCHAR *name;
1289         int retVal= -1;
1290
1291         yaffsfs_Lock();
1292         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1293         if(parent)
1294                 dir = yaffs_MknodDirectory(parent,name,mode,0,0);
1295         if(dir)
1296                 retVal = 0;
1297         else {
1298                 if(!parent)
1299                         yaffsfs_SetError(-ENOENT); // missing path
1300                 else if (yaffs_FindObjectByName(parent,name))
1301                         yaffsfs_SetError(-EEXIST); // the name already exists
1302                 else
1303                         yaffsfs_SetError(-ENOSPC); // just assume no space
1304                 retVal = -1;
1305         }
1306
1307         yaffsfs_Unlock();
1308
1309         return retVal;
1310 }
1311
1312 int yaffs_mount(const YCHAR *path)
1313 {
1314         int retVal=-1;
1315         int result=YAFFS_FAIL;
1316         yaffs_Device *dev=NULL;
1317         YCHAR *dummy;
1318
1319         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1320
1321         yaffsfs_Lock();
1322         dev = yaffsfs_FindDevice(path,&dummy);
1323         if(dev){
1324                 if(!dev->isMounted){
1325                         result = yaffs_GutsInitialise(dev);
1326                         if(result == YAFFS_FAIL)
1327                                 // todo error - mount failed
1328                                 yaffsfs_SetError(-ENOMEM);
1329                         retVal = result ? 0 : -1;
1330
1331                 }
1332                 else
1333                         //todo error - already mounted.
1334                         yaffsfs_SetError(-EBUSY);
1335         } else
1336                 // todo error - no device
1337                 yaffsfs_SetError(-ENODEV);
1338
1339         yaffsfs_Unlock();
1340         return retVal;
1341
1342 }
1343
1344 int yaffs_sync(const YCHAR *path)
1345 {
1346         int retVal=-1;
1347         yaffs_Device *dev=NULL;
1348         YCHAR *dummy;
1349         
1350         yaffsfs_Lock();
1351         dev = yaffsfs_FindDevice(path,&dummy);
1352         if(dev){
1353                 if(dev->isMounted){
1354                         
1355                         yaffs_FlushEntireDeviceCache(dev);
1356                         yaffs_CheckpointSave(dev);
1357                         
1358                         
1359                 } else
1360                         //todo error - not mounted.
1361                         yaffsfs_SetError(-EINVAL);
1362                         
1363         }else
1364                 // todo error - no device
1365                 yaffsfs_SetError(-ENODEV);
1366
1367         yaffsfs_Unlock();
1368         return retVal;  
1369 }
1370
1371
1372 int yaffs_unmount(const YCHAR *path)
1373 {
1374         int retVal=-1;
1375         yaffs_Device *dev=NULL;
1376         YCHAR *dummy;
1377
1378         yaffsfs_Lock();
1379         dev = yaffsfs_FindDevice(path,&dummy);
1380         if(dev){
1381                 if(dev->isMounted){
1382                         int i;
1383                         int inUse;
1384
1385                         yaffs_FlushEntireDeviceCache(dev);
1386                         yaffs_CheckpointSave(dev);
1387
1388                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1389                                 if(yaffsfs_handle[i].inUse && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->myDev == dev)
1390                                         inUse = 1; // the device is in use, can't unmount
1391                         }
1392
1393                         if(!inUse){
1394                                 yaffs_Deinitialise(dev);
1395
1396                                 retVal = 0;
1397                         } else
1398                                 // todo error can't unmount as files are open
1399                                 yaffsfs_SetError(-EBUSY);
1400
1401                 } else
1402                         //todo error - not mounted.
1403                         yaffsfs_SetError(-EINVAL);
1404
1405         }
1406         else
1407                 // todo error - no device
1408                 yaffsfs_SetError(-ENODEV);
1409
1410         yaffsfs_Unlock();
1411         return retVal;
1412
1413 }
1414
1415 loff_t yaffs_freespace(const YCHAR *path)
1416 {
1417         loff_t retVal=-1;
1418         yaffs_Device *dev=NULL;
1419         YCHAR *dummy;
1420
1421         yaffsfs_Lock();
1422         dev = yaffsfs_FindDevice(path,&dummy);
1423         if(dev  && dev->isMounted){
1424                 retVal = yaffs_GetNumberOfFreeChunks(dev);
1425                 retVal *= dev->nDataBytesPerChunk;
1426
1427         } else
1428                 yaffsfs_SetError(-EINVAL);
1429
1430         yaffsfs_Unlock();
1431         return retVal;
1432 }
1433
1434 loff_t yaffs_totalspace(const YCHAR *path)
1435 {
1436         loff_t retVal=-1;
1437         yaffs_Device *dev=NULL;
1438         YCHAR *dummy;
1439
1440         yaffsfs_Lock();
1441         dev = yaffsfs_FindDevice(path,&dummy);
1442         if(dev  && dev->isMounted){
1443                 retVal = (dev->endBlock - dev->startBlock + 1) - dev->nReservedBlocks;
1444                 retVal *= dev->nChunksPerBlock;
1445                 retVal *= dev->nDataBytesPerChunk;
1446
1447         } else
1448                 yaffsfs_SetError(-EINVAL);
1449
1450         yaffsfs_Unlock();
1451         return retVal;
1452 }
1453
1454 int yaffs_inodecount(const YCHAR *path)
1455 {
1456         loff_t retVal= -1;
1457         yaffs_Device *dev=NULL;
1458         YCHAR *dummy;
1459
1460         yaffsfs_Lock();
1461         dev = yaffsfs_FindDevice(path,&dummy);
1462         if(dev  && dev->isMounted) {
1463            int nObjects = dev->nObjectsCreated - dev->nFreeObjects;
1464            if(nObjects > dev->nHardLinks)
1465                 retVal = nObjects - dev->nHardLinks;
1466         }
1467         
1468         if(retVal < 0)
1469                 yaffsfs_SetError(-EINVAL);
1470         
1471         yaffsfs_Unlock();
1472         return retVal;  
1473 }
1474
1475
1476
1477 void yaffs_initialise(yaffsfs_DeviceConfiguration *cfgList)
1478 {
1479
1480         yaffsfs_DeviceConfiguration *cfg;
1481
1482         yaffsfs_configurationList = cfgList;
1483
1484         yaffsfs_InitHandles();
1485
1486         cfg = yaffsfs_configurationList;
1487
1488         while(cfg && cfg->prefix && cfg->dev){
1489                 cfg->dev->isMounted = 0;
1490                 cfg->dev->removeObjectCallback = yaffsfs_RemoveObjectCallback;
1491                 cfg++;
1492         }
1493
1494
1495 }
1496
1497
1498 //
1499 // Directory search stuff.
1500
1501 //
1502 // Directory search context
1503 //
1504 // NB this is an opaque structure.
1505
1506
1507 typedef struct
1508 {
1509         __u32 magic;
1510         yaffs_dirent de;                /* directory entry being used by this dsc */
1511         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
1512         yaffs_Object *dirObj;           /* ptr to directory being searched */
1513         yaffs_Object *nextReturn;       /* obj to be returned by next readddir */
1514         int offset;
1515         struct ylist_head others;       
1516 } yaffsfs_DirectorySearchContext;
1517
1518
1519
1520 static struct ylist_head search_contexts;
1521
1522
1523 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
1524 {
1525         if(dsc &&
1526            dsc->dirObj &&
1527            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1528
1529            dsc->offset = 0;
1530
1531            if( ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1532                 dsc->nextReturn = NULL;
1533            else
1534                 dsc->nextReturn = ylist_entry(dsc->dirObj->variant.directoryVariant.children.next,
1535                                                 yaffs_Object,siblings);
1536         } else {
1537                 /* Hey someone isn't playing nice! */
1538         }
1539 }
1540
1541 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
1542 {
1543         if(dsc &&
1544            dsc->dirObj &&
1545            dsc->dirObj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1546
1547            if( dsc->nextReturn == NULL ||
1548                ylist_empty(&dsc->dirObj->variant.directoryVariant.children))
1549                 dsc->nextReturn = NULL;
1550            else {
1551                    struct ylist_head *next = dsc->nextReturn->siblings.next;
1552
1553                    if( next == &dsc->dirObj->variant.directoryVariant.children)
1554                         dsc->nextReturn = NULL; /* end of list */
1555                    else
1556                         dsc->nextReturn = ylist_entry(next,yaffs_Object,siblings);
1557            }
1558         } else {
1559                 /* Hey someone isn't playing nice! */
1560         }
1561 }
1562
1563 static void yaffsfs_RemoveObjectCallback(yaffs_Object *obj)
1564 {
1565
1566         struct ylist_head *i;
1567         yaffsfs_DirectorySearchContext *dsc;
1568
1569         /* if search contexts not initilised then skip */
1570         if(!search_contexts.next)
1571                 return;
1572
1573         /* Iterate through the directory search contexts.
1574          * If any are the one being removed, then advance the dsc to
1575          * the next one to prevent a hanging ptr.
1576          */
1577          ylist_for_each(i, &search_contexts) {
1578                 if (i) {
1579                         dsc = ylist_entry(i, yaffsfs_DirectorySearchContext,others);
1580                         if(dsc->nextReturn == obj)
1581                                 yaffsfs_DirAdvance(dsc);
1582                 }
1583         }
1584
1585 }
1586
1587 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
1588 {
1589         yaffs_DIR *dir = NULL;
1590         yaffs_Object *obj = NULL;
1591         yaffsfs_DirectorySearchContext *dsc = NULL;
1592
1593         yaffsfs_Lock();
1594
1595         obj = yaffsfs_FindObject(NULL,dirname,0);
1596
1597         if(obj && obj->variantType == YAFFS_OBJECT_TYPE_DIRECTORY){
1598
1599                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
1600                 dir = (yaffs_DIR *)dsc;
1601
1602                 if(dsc){
1603                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
1604                         dsc->magic = YAFFS_MAGIC;
1605                         dsc->dirObj = obj;
1606                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
1607                         YINIT_LIST_HEAD(&dsc->others);
1608
1609                         if(!search_contexts.next)
1610                                 YINIT_LIST_HEAD(&search_contexts);
1611
1612                         ylist_add(&dsc->others,&search_contexts);       
1613                         yaffsfs_SetDirRewound(dsc);
1614                 }
1615
1616         }
1617
1618         yaffsfs_Unlock();
1619
1620         return dir;
1621 }
1622
1623 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
1624 {
1625         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1626         struct yaffs_dirent *retVal = NULL;
1627
1628         yaffsfs_Lock();
1629
1630         if(dsc && dsc->magic == YAFFS_MAGIC){
1631                 yaffsfs_SetError(0);
1632                 if(dsc->nextReturn){
1633                         dsc->de.d_ino = yaffs_GetEquivalentObject(dsc->nextReturn)->objectId;
1634                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
1635                         dsc->de.d_off = dsc->offset++;
1636                         yaffs_GetObjectName(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
1637                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
1638                         {
1639                                 // this should not happen!
1640                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
1641                         }
1642                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
1643                         retVal = &dsc->de;
1644                         yaffsfs_DirAdvance(dsc);
1645                 } else
1646                         retVal = NULL;
1647         } else
1648                 yaffsfs_SetError(-EBADF);
1649
1650         yaffsfs_Unlock();
1651
1652         return retVal;
1653
1654 }
1655
1656
1657 void yaffs_rewinddir(yaffs_DIR *dirp)
1658 {
1659         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1660
1661         yaffsfs_Lock();
1662
1663         yaffsfs_SetDirRewound(dsc);
1664
1665         yaffsfs_Unlock();
1666 }
1667
1668
1669 int yaffs_closedir(yaffs_DIR *dirp)
1670 {
1671         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
1672
1673         yaffsfs_Lock();
1674         dsc->magic = 0;
1675         ylist_del(&dsc->others); /* unhook from list */
1676         YFREE(dsc);
1677         yaffsfs_Unlock();
1678         return 0;
1679 }
1680
1681 // end of directory stuff
1682
1683
1684 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
1685 {
1686         yaffs_Object *parent = NULL;
1687         yaffs_Object *obj;
1688         YCHAR *name;
1689         int retVal= -1;
1690         int mode = 0; // ignore for now
1691
1692         yaffsfs_Lock();
1693         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
1694         if(parent){
1695                 obj = yaffs_MknodSymLink(parent,name,mode,0,0,oldpath);
1696                 if(obj)
1697                         retVal = 0;
1698                 else{
1699                         yaffsfs_SetError(-ENOSPC); // just assume no space for now
1700                         retVal = -1;
1701                 }
1702         } else {
1703                 yaffsfs_SetError(-EINVAL);
1704                 retVal = -1;
1705         }
1706
1707         yaffsfs_Unlock();
1708
1709         return retVal;
1710
1711 }
1712
1713 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
1714 {
1715         yaffs_Object *obj = NULL;
1716         int retVal;
1717
1718
1719         yaffsfs_Lock();
1720
1721         obj = yaffsfs_FindObject(NULL,path,0);
1722
1723         if(!obj) {
1724                 yaffsfs_SetError(-ENOENT);
1725                 retVal = -1;
1726         } else if(obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK) {
1727                 yaffsfs_SetError(-EINVAL);
1728                 retVal = -1;
1729         } else {
1730                 YCHAR *alias = obj->variant.symLinkVariant.alias;
1731                 memset(buf,0,bufsiz);
1732                 yaffs_strncpy(buf,alias,bufsiz - 1);
1733                 retVal = 0;
1734         }
1735         yaffsfs_Unlock();
1736         return retVal;
1737 }
1738
1739 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
1740 {
1741         // Creates a link called newpath to existing oldpath
1742         yaffs_Object *obj = NULL;
1743         yaffs_Object *target = NULL;
1744         int retVal = 0;
1745         int newNameLength = 0;
1746
1747
1748         yaffsfs_Lock();
1749
1750         obj = yaffsfs_FindObject(NULL,oldpath,0);
1751         target = yaffsfs_FindObject(NULL,newpath,0);
1752
1753         if(!obj) {
1754                 yaffsfs_SetError(-ENOENT);
1755                 retVal = -1;
1756         } else if(target) {
1757                 yaffsfs_SetError(-EEXIST);
1758                 retVal = -1;
1759         } else {
1760                 yaffs_Object *newdir = NULL;
1761                 yaffs_Object *link = NULL;
1762
1763                 YCHAR *newname;
1764
1765                 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
1766
1767                 if(!newdir){
1768                         yaffsfs_SetError(-ENOTDIR);
1769                         retVal = -1;
1770                 }else if(newdir->myDev != obj->myDev){
1771                         yaffsfs_SetError(-EXDEV);
1772                         retVal = -1;
1773                 }
1774                 
1775                 newNameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
1776                 
1777                 if(newNameLength == 0){
1778                         yaffsfs_SetError(-ENOENT);
1779                         retVal = -1;
1780                 } else if (newNameLength > YAFFS_MAX_NAME_LENGTH){
1781                         yaffsfs_SetError(-ENAMETOOLONG);
1782                         retVal = -1;
1783                 }
1784                 
1785                 if(retVal == 0) {
1786                         link = yaffs_Link(newdir,newname,obj);
1787                         if(link)
1788                                 retVal = 0;
1789                         else{
1790                                 yaffsfs_SetError(-ENOSPC);
1791                                 retVal = -1;
1792                         }
1793
1794                 }
1795         }
1796         yaffsfs_Unlock();
1797
1798         return retVal;
1799 }
1800
1801 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
1802 {
1803         return -1;
1804 }
1805
1806 int yaffs_DumpDevStruct(const YCHAR *path)
1807 {
1808 #if 0
1809         YCHAR *rest;
1810
1811         yaffs_Object *obj = yaffsfs_FindRoot(path,&rest);
1812
1813         if(obj){
1814                 yaffs_Device *dev = obj->myDev;
1815
1816                 printf("\n"
1817                            "nPageWrites.......... %d\n"
1818                            "nPageReads........... %d\n"
1819                            "nBlockErasures....... %d\n"
1820                            "nGCCopies............ %d\n"
1821                            "garbageCollections... %d\n"
1822                            "passiveGarbageColl'ns %d\n"
1823                            "\n",
1824                                 dev->nPageWrites,
1825                                 dev->nPageReads,
1826                                 dev->nBlockErasures,
1827                                 dev->nGCCopies,
1828                                 dev->garbageCollections,
1829                                 dev->passiveGarbageCollections
1830                 );
1831
1832         }
1833
1834 #endif
1835         return 0;
1836 }
1837