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