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