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