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