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