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