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