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