yaffs Changed the logical error handling paths in yaffsfs.c
[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, const YCHAR *path, int symDepth, int getEquiv);
35 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
36
37 unsigned int yaffs_wr_attempts;
38
39 /*
40  * Handle management.
41  * There are open inodes in yaffsfs_Inode.
42  * There are open handles in yaffsfs_Handle.
43  *
44  * Things are structured this way to be like the Linux VFS model
45  * so that interactions with the yaffs guts calls are similar.
46  * That means more common code paths and less special code.
47  * That means better testing etc.
48  */
49
50 typedef struct {
51         int count;      /* Number of handles accessing this inode */
52         struct yaffs_obj *iObj;
53 } yaffsfs_Inode;
54
55 typedef struct{
56         u8      reading:1;
57         u8      writing:1;
58         u8      append:1;
59         u8      shareRead:1;
60         u8      shareWrite:1;
61         int     inodeId:12;     /* Index to corresponding yaffsfs_Inode */
62         int     useCount:10;    /* Use count for this handle */
63         u32 position;           /* current position in file */
64 }yaffsfs_Handle;
65
66 static yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
67 static yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
68 static int yaffsfs_handlesInitialised;
69
70 unsigned int yaffs_trace_mask;
71
72 int yaffs_set_trace(unsigned int tm) 
73 {
74         return yaffs_trace_mask=tm;
75 }
76
77 unsigned int yaffs_get_trace(void)
78 {
79         return yaffs_trace_mask;
80 }
81
82 /*
83  * yaffsfs_InitHandle
84  * Inilitalise handle management on start-up.
85  */
86
87 static void yaffsfs_InitHandles(void)
88 {
89         int i;
90         if(yaffsfs_handlesInitialised)
91                 return;
92
93         memset(yaffsfs_inode,0,sizeof(yaffsfs_inode));
94         memset(yaffsfs_handle,0,sizeof(yaffsfs_handle));
95         for(i = 0; i < YAFFSFS_N_HANDLES; i++)
96                 yaffsfs_handle[i].inodeId = -1;
97 }
98
99 yaffsfs_Handle *yaffsfs_GetHandlePointer(int h)
100 {
101         if(h < 0 || h >= YAFFSFS_N_HANDLES)
102                 return NULL;
103
104         return &yaffsfs_handle[h];
105 }
106
107 yaffsfs_Inode *yaffsfs_GetInodePointer(int handle)
108 {
109         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
110
111         if(h && h->useCount > 0 && h->inodeId >= 0 && h->inodeId < YAFFSFS_N_HANDLES)
112                 return  &yaffsfs_inode[h->inodeId];
113
114         return NULL;
115 }
116
117 struct yaffs_obj *yaffsfs_GetHandleObject(int handle)
118 {
119         yaffsfs_Inode *in = yaffsfs_GetInodePointer(handle);
120
121         if(in)
122                 return in->iObj;
123
124         return NULL;
125 }
126
127 /*
128  * yaffsfs_FindInodeIdForObject
129  * Find the inode entry for an object, if it exists.
130  */
131
132 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
133 {
134         int i;
135         int ret = -1;
136         
137         if(obj)
138                 obj = yaffs_get_equivalent_obj(obj);
139
140         /* Look for it in open inode table*/
141         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
142                 if(yaffsfs_inode[i].iObj == obj)
143                         ret = i;
144         }
145         return ret;
146 }
147
148 /*
149  * yaffsfs_GetInodeIdForObject
150  * Grab an inode entry when opening a new inode.
151  */
152 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
153 {
154         int i;
155         int ret;
156         yaffsfs_Inode *in = NULL;
157         
158         if(obj)
159                 obj = yaffs_get_equivalent_obj(obj);
160
161         ret = yaffsfs_FindInodeIdForObject(obj);
162
163         for(i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++){
164                 if(!yaffsfs_inode[i].iObj)
165                         ret = i;
166         }
167
168         if(ret>=0){
169                 in = &yaffsfs_inode[ret];
170                 if(!in->iObj)
171                         in->count = 0;
172                 in->iObj = obj;
173                 in->count++;
174         }
175         
176         
177         return ret;
178 }
179
180
181 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
182 {
183         int i = yaffsfs_FindInodeIdForObject(obj);
184
185         if(i >= 0)
186                 return yaffsfs_inode[i].count;
187         else
188                 return 0;
189 }
190
191 static void yaffsfs_ReleaseInode(yaffsfs_Inode *in)
192 {
193         struct yaffs_obj *obj;
194         
195         obj = in->iObj;
196
197         if(obj->unlinked)
198                 yaffs_del_obj(obj);
199         
200         obj->my_inode = NULL;
201         in->iObj = NULL;
202
203 }
204
205 static void yaffsfs_PutInode(int inodeId)
206 {
207         if(inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES){
208                 yaffsfs_Inode *in = & yaffsfs_inode[inodeId];
209                 in->count--;
210                 if(in->count <= 0)
211                         yaffsfs_ReleaseInode(in);
212         }       
213 }
214
215
216 /*
217  * yaffsfs_GetHandle
218  * Grab a handle (when opening a file)
219  */
220
221 static int yaffsfs_GetNewHandle(void)
222 {
223         int i;
224         yaffsfs_Handle *h;
225
226         for(i = 0; i < YAFFSFS_N_HANDLES; i++){
227                 h = yaffsfs_GetHandlePointer(i);
228                 if(!h){
229                         /* todo bug: should never happen */
230                 }
231                 if(h->useCount < 1){
232                         memset(h,0,sizeof(yaffsfs_Handle));
233                         h->inodeId=-1;
234                         h->useCount=1;
235                         return i;
236                 }
237         }
238         return -1;
239 }
240
241 /*
242  * yaffs_get_handle
243  * Increase use of handle when reading/writing a file
244  */
245 static int yaffsfs_GetHandle(int handle)
246 {
247         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
248
249         if(h && h->useCount > 0){       
250                 h->useCount++;
251         }
252         return 0;
253 }
254
255 /*
256  * yaffs_put_handle
257  * Let go of a handle when closing a file or aborting an open or
258  * ending a read or write.
259  */
260 static int yaffsfs_PutHandle(int handle)
261 {
262         yaffsfs_Handle *h = yaffsfs_GetHandlePointer(handle);
263
264         if(h && h->useCount > 0){       
265                 h->useCount--;
266                 if(h->useCount < 1){
267                         if(h->inodeId >= 0){
268                                 yaffsfs_PutInode(h->inodeId);
269                                 h->inodeId = -1;
270                         }
271                 }
272         }
273         return 0;
274 }
275
276
277
278
279 /*
280  *  Stuff to search for a directory from a path
281  */
282
283
284 int yaffsfs_Match(YCHAR a, YCHAR b)
285 {
286         /* case sensitive */
287         return (a == b);
288 }
289
290 int yaffsfs_IsPathDivider(YCHAR ch)
291 {
292         const YCHAR *str = YAFFS_PATH_DIVIDERS;
293
294         while(*str){
295                 if(*str == ch)
296                         return 1;
297                 str++;
298         }
299
300         return 0;
301 }
302
303
304
305 LIST_HEAD(yaffsfs_deviceList);
306
307 /*
308  * yaffsfs_FindDevice
309  * yaffsfs_FindRoot
310  * Scan the configuration list to find the root.
311  * Curveballs: Should match paths that end in '/' too
312  * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
313  */
314 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
315 {
316         struct list_head *cfg;
317         const YCHAR *leftOver;
318         const YCHAR *p;
319         struct yaffs_dev *retval = NULL;
320         struct yaffs_dev *dev = NULL;
321         int thisMatchLength;
322         int longestMatch = -1;
323         int matching;
324
325         /*
326          * Check all configs, choose the one that:
327          * 1) Actually matches a prefix (ie /a amd /abc will not match
328          * 2) Matches the longest.
329          */
330         list_for_each(cfg, &yaffsfs_deviceList){
331                 dev = list_entry(cfg, struct yaffs_dev, dev_list);
332                 leftOver = path;
333                 p = dev->param.name;
334                 thisMatchLength = 0;
335                 matching = 1;
336
337
338                 while(matching && *p && *leftOver){
339                         /* Skip over any /s */
340                         while(yaffsfs_IsPathDivider(*p))
341                               p++;
342
343                         /* Skip over any /s */
344                         while(yaffsfs_IsPathDivider(*leftOver))
345                               leftOver++;
346
347                         /* Now match the text part */
348                         while(matching &&
349                               *p && !yaffsfs_IsPathDivider(*p) &&
350                               *leftOver && !yaffsfs_IsPathDivider(*leftOver)){
351                                 if(yaffsfs_Match(*p,*leftOver)){
352                                         p++;
353                                         leftOver++;
354                                         thisMatchLength++;
355                                 } else {
356                                         matching = 0;
357                                 }
358                         }
359                 }
360
361                 /* Skip over any /s in leftOver */
362                 while(yaffsfs_IsPathDivider(*leftOver))
363                       leftOver++;
364
365                 // Skip over any /s in p
366                 while(yaffsfs_IsPathDivider(*p))
367                       p++;
368
369                 // p should now be at the end of the string (ie. fully matched)
370                 if(*p)
371                         matching = 0;
372
373                 if( matching && (thisMatchLength > longestMatch))
374                 {
375                         // Matched prefix
376                         *restOfPath = (YCHAR *)leftOver;
377                         retval = dev;
378                         longestMatch = thisMatchLength;
379                 }
380
381         }
382         return retval;
383 }
384
385 #if 0
386 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path, YCHAR **restOfPath)
387 {
388         yaffsfs_DeviceConfiguration *cfg = yaffsfs_configurationList;
389         const YCHAR *leftOver;
390         const YCHAR *p;
391         struct yaffs_dev *retval = NULL;
392         int thisMatchLength;
393         int longestMatch = -1;
394
395         /*
396          * Check all configs, choose the one that:
397          * 1) Actually matches a prefix (ie /a amd /abc will not match
398          * 2) Matches the longest.
399          */
400         while(cfg && cfg->prefix && cfg->dev){
401                 leftOver = path;
402                 p = cfg->prefix;
403                 thisMatchLength = 0;
404
405                 while(*p &&  /* unmatched part of prefix */
406                       !(yaffsfs_IsPathDivider(*p) && (p[1] == 0)) &&
407                       *leftOver && yaffsfs_Match(*p,*leftOver)){
408                         p++;
409                         leftOver++;
410                         thisMatchLength++;
411                 }
412
413
414                 if((!*p || (yaffsfs_IsPathDivider(*p) && (p[1] == 0))) &&  /* end of prefix */
415                    (!*leftOver || yaffsfs_IsPathDivider(*leftOver)) && /* no more in this path name part */
416                    (thisMatchLength > longestMatch)){
417                         /* Matched prefix */
418                         *restOfPath = (YCHAR *)leftOver;
419                         retval = cfg->dev;
420                         longestMatch = thisMatchLength;
421                 }
422                 cfg++;
423         }
424         return retval;
425 }
426 #endif
427
428 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path, YCHAR **restOfPath)
429 {
430
431         struct yaffs_dev *dev;
432
433         dev= yaffsfs_FindDevice(path,restOfPath);
434         if(dev && dev->is_mounted){
435                 return dev->root_dir;
436         }
437         return NULL;
438 }
439
440 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,int symDepth)
441 {
442
443         if(obj)
444                 obj = yaffs_get_equivalent_obj(obj);
445
446         while(obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK){
447                 YCHAR *alias = obj->variant.symlink_variant.alias;
448
449                 if(yaffsfs_IsPathDivider(*alias))
450                         /* Starts with a /, need to scan from root up */
451                         obj = yaffsfs_FindObject(NULL,alias,symDepth++,1);
452                 else
453                         /* Relative to here, so use the parent of the symlink as a start */
454                         obj = yaffsfs_FindObject(obj->parent,alias,symDepth++,1);
455         }
456         return obj;
457 }
458
459
460 /*
461  * yaffsfs_FindDirectory
462  * Parse a path to determine the directory and the name within the directory.
463  *
464  * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
465  */
466 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
467                                 const YCHAR *path, YCHAR **name, int symDepth)
468 {
469         struct yaffs_obj *dir;
470         YCHAR *restOfPath;
471         YCHAR str[YAFFS_MAX_NAME_LENGTH+1];
472         int i;
473
474         if(symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES)
475                 return NULL;
476
477         if(startDir){
478                 dir = startDir;
479                 restOfPath = (YCHAR *)path;
480         }
481         else
482                 dir = yaffsfs_FindRoot(path,&restOfPath);
483
484         while(dir){
485                 /*
486                  * parse off /.
487                  * curve ball: also throw away surplus '/'
488                  * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
489                  */
490                 while(yaffsfs_IsPathDivider(*restOfPath))
491                         restOfPath++; /* get rid of '/' */
492
493                 *name = restOfPath;
494                 i = 0;
495
496                 while(*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)){
497                         if (i < YAFFS_MAX_NAME_LENGTH){
498                                 str[i] = *restOfPath;
499                                 str[i+1] = '\0';
500                                 i++;
501                         }
502                         restOfPath++;
503                 }
504
505                 if(!*restOfPath)
506                         /* got to the end of the string */
507                         return dir;
508                 else{
509                         if(yaffs_strcmp(str,_Y(".")) == 0)
510                         {
511                                 /* Do nothing */
512                         }
513                         else if(yaffs_strcmp(str,_Y("..")) == 0)
514                                 dir = dir->parent;
515                         else{
516                                 dir = yaffs_find_by_name(dir,str);
517
518                                 dir = yaffsfs_FollowLink(dir,symDepth);
519
520                                 if(dir && dir->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
521                                         dir = NULL;
522                         }
523                 }
524         }
525         /* directory did not exist. */
526         return NULL;
527 }
528
529 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relativeDirectory,
530                                         const YCHAR *path,YCHAR **name,int symDepth)
531 {
532         return yaffsfs_DoFindDirectory(relativeDirectory,path,name,symDepth);
533 }
534
535 /*
536  * yaffsfs_FindObject turns a path for an existing object into the object
537  */
538 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory, const YCHAR *path,int symDepth, int getEquiv)
539 {
540         struct yaffs_obj *dir;
541         struct yaffs_obj *obj;
542         YCHAR *name;
543
544         dir = yaffsfs_FindDirectory(relativeDirectory,path,&name,symDepth);
545
546         if(dir && *name)
547                 obj = yaffs_find_by_name(dir,name);
548         else
549                 obj = dir;
550
551         if(getEquiv)
552                 obj = yaffs_get_equivalent_obj(obj);
553
554         return obj;
555 }
556
557
558 int yaffs_dup(int fd)
559 {
560         int newHandle = -1;
561         yaffsfs_Handle *oldPtr = NULL;
562         yaffsfs_Handle *newPtr = NULL;
563
564         yaffsfs_Lock();
565
566         oldPtr = yaffsfs_GetHandlePointer(fd);
567         if(oldPtr && oldPtr->useCount > 0)
568                 newHandle = yaffsfs_GetNewHandle();
569         if(newHandle >= 0)
570                 newPtr = yaffsfs_GetHandlePointer(newHandle);
571
572         if(newPtr){
573                 *newPtr = *oldPtr;
574                 return newHandle;
575         }
576
577         if(!oldPtr)
578                 yaffsfs_SetError(-EBADF);
579         else
580                 yaffsfs_SetError(-ENOMEM);
581
582         return -1;
583
584 }
585
586
587
588 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
589 {
590         struct yaffs_obj *obj = NULL;
591         struct yaffs_obj *dir = NULL;
592         YCHAR *name;
593         int handle = -1;
594         yaffsfs_Handle *yh = NULL;
595         int openDenied = 0;
596         int symDepth = 0;
597         int errorReported = 0;
598         int rwflags = oflag & ( O_RDWR | O_RDONLY | O_WRONLY);
599         u8 shareRead = (sharing & YAFFS_SHARE_READ) ?  1 : 0;
600         u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
601         u8 sharedReadAllowed;
602         u8 sharedWriteAllowed;
603         u8 alreadyReading;
604         u8 alreadyWriting;
605         u8 readRequested;
606         u8 writeRequested;
607
608         /* O_EXCL only has meaning if O_CREAT is specified */
609         if(!(oflag & O_CREAT))
610                 oflag &= ~(O_EXCL);
611
612         /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
613         if( (oflag & O_CREAT) & (oflag & O_EXCL))
614                 oflag &= ~(O_TRUNC);
615
616         /* Todo: Are there any more flag combos to sanitise ? */
617
618         /* Figure out if reading or writing is requested */
619
620         readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
621         writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
622
623         yaffsfs_Lock();
624
625         handle = yaffsfs_GetNewHandle();
626
627         if(handle < 0){
628                 yaffsfs_SetError(-ENFILE);
629                 errorReported = 1;
630         } else {
631
632                 yh = yaffsfs_GetHandlePointer(handle);
633
634                 /* try to find the exisiting object */
635                 obj = yaffsfs_FindObject(NULL,path,0,1);
636
637                 obj = yaffsfs_FollowLink(obj,symDepth++);
638
639                 if(obj &&
640                         obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
641                         obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
642                         obj = NULL;
643
644
645                 if(obj){
646
647                         /* The file already exists or it might be a directory */
648
649                         /* If it is a directory then we can't open it as a file */
650                         if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
651                                 openDenied = 1;
652                                 yaffsfs_SetError(-EISDIR);
653                                 errorReported = 1;
654                         }
655
656                         /* Open should fail if O_CREAT and O_EXCL are specified since
657                          * the file exists
658                          */
659                         if(!errorReported && (oflag & O_EXCL) && (oflag & O_CREAT)){
660                                 openDenied = 1;
661                                 yaffsfs_SetError(-EEXIST);
662                                 errorReported = 1;
663                         }
664
665                         /* Check file permissions */
666                         if( readRequested && !(obj->yst_mode & S_IREAD))
667                                 openDenied = 1;
668
669                         if( writeRequested && !(obj->yst_mode & S_IWRITE))
670                                 openDenied = 1;
671
672                         if(openDenied && !errorReported ) {
673                                 /* Error if the file exists but permissions are refused. */
674                                 yaffsfs_SetError(-EACCES);
675                                 errorReported = 1;
676                         }
677
678                         /* Check sharing of an existing object. */
679                         if(!openDenied){
680                                 yaffsfs_Handle *hx;
681                                 int i;
682                                 sharedReadAllowed = 1;
683                                 sharedWriteAllowed = 1;
684                                 alreadyReading = 0;
685                                 alreadyWriting = 0;
686                                 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
687                                         hx = &yaffsfs_handle[i];
688                                         if(hx->useCount > 0 &&
689                                                 hx->inodeId >= 0 &&
690                                                 yaffsfs_inode[hx->inodeId].iObj == obj){
691                                                 if(!hx->shareRead)
692                                                         sharedReadAllowed = 0;
693                                                 if(!hx->shareWrite)
694                                                         sharedWriteAllowed = 0;
695                                                 if(hx->reading)
696                                                         alreadyReading = 1;
697                                                 if(hx->writing)
698                                                         alreadyWriting = 1;
699                                         }
700                                 }
701
702
703
704                                 if((!sharedReadAllowed && readRequested)|| 
705                                         (!shareRead  && alreadyReading) ||
706                                         (!sharedWriteAllowed && writeRequested) ||
707                                         (!shareWrite && alreadyWriting)){
708                                         openDenied = 1;
709                                         yaffsfs_SetError(-EBUSY);
710                                         errorReported=1;
711                                 }
712                         }
713
714                 }
715
716                 /* If we could not open an existing object, then let's see if
717                  * the directory exists. If not, error.
718                  */
719                 if(!obj && !errorReported){
720                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
721                         if(!dir){
722                                 yaffsfs_SetError(-ENOTDIR);
723                                 errorReported = 1;
724                         }
725                 }
726
727                 if(!obj && dir && !errorReported && (oflag & O_CREAT)) {
728                         /* Let's see if we can create this file if it does not exist. */
729                         if(dir->my_dev->read_only){
730                                 yaffsfs_SetError(-EINVAL);
731                                 errorReported = 1;
732                         } else
733                                 obj = yaffs_create_file(dir,name,mode,0,0);
734
735                         if(!obj && !errorReported){
736                                 yaffsfs_SetError(-ENOSPC);
737                                 errorReported = 1;
738                         }
739                 }
740
741                 if(!obj && dir && !errorReported && !(oflag & O_CREAT)) {
742                         /* Error if the file does not exist and CREAT is not set. */
743                         yaffsfs_SetError(-ENOENT);
744                         errorReported = 1;
745                 }
746
747                 if(obj && !openDenied) {
748                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
749
750                         if(inodeId<0) {
751                                 /*
752                                  * Todo: Fix any problem if inodes run out, though that
753                                  * can't happen if the number of inode items >= number of handles. 
754                                  */
755                         }
756                         
757                         yh->inodeId = inodeId;
758                         yh->reading = readRequested;
759                         yh->writing = writeRequested;
760                         yh->append =  (oflag & O_APPEND) ? 1 : 0;
761                         yh->position = 0;
762                         yh->shareRead = shareRead;
763                         yh->shareWrite = shareWrite;
764
765                         /* Hook inode to object */
766                         obj->my_inode = (void*) &yaffsfs_inode[inodeId];
767
768                         if((oflag & O_TRUNC) && yh->writing)
769                                 yaffs_resize_file(obj,0);
770                 } else {
771                         yaffsfs_PutHandle(handle);
772                         if(!errorReported) 
773                                 yaffsfs_SetError(0); /* Problem */
774                         handle = -1;
775                 }
776         }
777
778         yaffsfs_Unlock();
779
780         return handle;
781 }
782
783 int yaffs_open(const YCHAR *path, int oflag, int mode)
784 {
785         return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
786 }
787
788 int yaffs_Dofsync(int fd,int datasync)
789 {
790         yaffsfs_Handle *h = NULL;
791         int retVal = 0;
792
793         yaffsfs_Lock();
794
795         h = yaffsfs_GetHandlePointer(fd);
796
797         if(h && h->useCount > 0)
798                 /* flush the file */
799                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
800         else {
801                 /* bad handle */
802                 yaffsfs_SetError(-EBADF);
803                 retVal = -1;
804         }
805
806         yaffsfs_Unlock();
807
808         return retVal;
809 }
810
811 int yaffs_fsync(int fd)
812 {
813         return yaffs_Dofsync(fd,0);
814 }
815
816 int yaffs_flush(int fd)
817 {
818         return yaffs_fsync(fd);
819 }
820
821 int yaffs_fdatasync(int fd)
822 {
823         return yaffs_Dofsync(fd,1);
824 }
825
826 int yaffs_close(int fd)
827 {
828         yaffsfs_Handle *h = NULL;
829         int retVal = 0;
830
831         yaffsfs_Lock();
832
833         h = yaffsfs_GetHandlePointer(fd);
834
835         if(h && h->useCount > 0) {
836                 /* clean up */
837                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
838                 yaffsfs_PutHandle(fd);
839                 retVal = 0;
840         } else {
841                 /* bad handle */
842                 yaffsfs_SetError(-EBADF);
843                 retVal = -1;
844         }
845
846         yaffsfs_Unlock();
847
848         return retVal;
849 }
850
851
852
853 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
854 {
855         yaffsfs_Handle *h = NULL;
856         struct yaffs_obj *obj = NULL;
857         int pos = 0;
858         int startPos = 0;
859         int nRead = 0;
860         int nToRead = 0;
861         int totalRead = 0;
862         unsigned int maxRead;
863         u8 *buf = (u8 *)vbuf;
864
865         yaffsfs_Lock();
866         h = yaffsfs_GetHandlePointer(fd);
867         obj = yaffsfs_GetHandleObject(fd);
868
869         if(!h || !obj){
870                 /* bad handle */
871                 yaffsfs_SetError(-EBADF);
872                 totalRead = -1;
873         } else if(!h->reading){
874                 /* Not a reading handle */
875                 yaffsfs_SetError(-EINVAL);
876                 totalRead = -1;
877         } else {
878                 if(isPread)
879                         startPos = offset;
880                 else
881                         startPos = h->position;
882
883                 pos = startPos;
884                                         
885                 if(yaffs_get_obj_length(obj) > pos)
886                         maxRead = yaffs_get_obj_length(obj) - pos;
887                 else
888                         maxRead = 0;
889
890                 if(nbyte > maxRead)
891                         nbyte = maxRead;
892
893
894                 yaffsfs_GetHandle(fd);
895
896                 while(nbyte > 0) {
897                         nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
898                         if(nToRead > nbyte)
899                                 nToRead = nbyte;
900                         nRead = yaffs_file_rd(obj,buf,pos,nToRead);
901
902                         if(nRead > 0){
903                                 totalRead += nRead;
904                                 pos += nRead;
905                                 buf += nRead;
906                         }
907
908                         if(nRead == nToRead)
909                                 nbyte-=nRead;
910                         else
911                                 nbyte = 0; /* no more to read */
912                                         
913                                         
914                         if(nbyte > 0){
915                                 yaffsfs_Unlock();
916                                 yaffsfs_Lock();
917                         }
918
919                 }
920
921                 yaffsfs_PutHandle(fd);
922
923                 if(!isPread) {
924                         if(totalRead >= 0)
925                                 h->position = startPos + totalRead;
926                         else {
927                                         /* todo error */
928                         }
929                 }
930
931         }
932
933         yaffsfs_Unlock();
934
935         return (totalRead >= 0) ? totalRead : -1;
936
937 }
938
939 int yaffs_read(int fd, void *buf, unsigned int nbyte)
940 {
941         return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
942 }
943
944 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
945 {
946         return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
947 }
948
949 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
950 {
951         yaffsfs_Handle *h = NULL;
952         struct yaffs_obj *obj = NULL;
953         int pos = 0;
954         int startPos = 0;
955         int nWritten = 0;
956         int totalWritten = 0;
957         int write_trhrough = 0;
958         int nToWrite = 0;
959         const u8 *buf = (const u8 *)vbuf;
960
961         yaffsfs_Lock();
962         h = yaffsfs_GetHandlePointer(fd);
963         obj = yaffsfs_GetHandleObject(fd);
964
965         if(!h || !obj){
966                 /* bad handle */
967                 yaffsfs_SetError(-EBADF);
968                 totalWritten = -1;
969         } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
970                 yaffsfs_SetError(-EINVAL);
971                 totalWritten=-1;
972         } else {
973                 if(h->append)
974                         startPos = yaffs_get_obj_length(obj);
975                 else if(isPwrite)
976                         startPos = offset;
977                 else
978                         startPos = h->position;
979
980                 yaffsfs_GetHandle(fd);
981                 pos = startPos;
982                 while(nbyte > 0) {
983                         nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
984                         if(nToWrite > nbyte)
985                                 nToWrite = nbyte;
986
987                         nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,write_trhrough);
988                         if(nWritten > 0){
989                                 totalWritten += nWritten;
990                                 pos += nWritten;
991                                 buf += nWritten;
992                         }
993
994                         if(nWritten == nToWrite)
995                                 nbyte -= nToWrite;
996                         else
997                                 nbyte = 0;
998
999                         if(nWritten < 1 && totalWritten < 1){
1000                                 yaffsfs_SetError(-ENOSPC);
1001                                 totalWritten = -1;
1002                         }
1003
1004                         if(nbyte > 0){
1005                                 yaffsfs_Unlock();
1006                                 yaffsfs_Lock();
1007                         }
1008                 }
1009
1010                 yaffsfs_PutHandle(fd);
1011
1012                 if(!isPwrite){
1013                         if(totalWritten > 0)
1014                                 h->position = startPos + totalWritten;
1015                         else {
1016                                 /* todo error */
1017                         }
1018                 }
1019         }
1020
1021         yaffsfs_Unlock();
1022
1023         return (totalWritten >= 0) ? totalWritten : -1;
1024 }
1025
1026 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1027 {
1028         return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1029 }
1030
1031 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1032 {
1033         return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1034 }
1035
1036
1037 int yaffs_truncate(const YCHAR *path,off_t new_size)
1038 {
1039         struct yaffs_obj *obj = NULL;
1040         int result = YAFFS_FAIL;
1041
1042         yaffsfs_Lock();
1043
1044         obj = yaffsfs_FindObject(NULL,path,0,1);
1045
1046         if(!obj)
1047                 yaffsfs_SetError(-ENOENT);
1048         else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1049                 yaffsfs_SetError(-EISDIR);
1050         else if(obj->my_dev->read_only)
1051                 yaffsfs_SetError(-EINVAL);
1052         else
1053                 result = yaffs_resize_file(obj,new_size);
1054
1055         yaffsfs_Unlock();
1056
1057
1058         return (result) ? 0 : -1;
1059 }
1060
1061 int yaffs_ftruncate(int fd, off_t new_size)
1062 {
1063         yaffsfs_Handle *h = NULL;
1064         struct yaffs_obj *obj = NULL;
1065         int result = 0;
1066
1067         yaffsfs_Lock();
1068         h = yaffsfs_GetHandlePointer(fd);
1069         obj = yaffsfs_GetHandleObject(fd);
1070
1071         if(!h || !obj)
1072                 /* bad handle */
1073                 yaffsfs_SetError(-EBADF);
1074         else if(obj->my_dev->read_only)
1075                 yaffsfs_SetError(-EINVAL);
1076         else
1077                 /* resize the file */
1078                 result = yaffs_resize_file(obj,new_size);
1079         yaffsfs_Unlock();
1080
1081
1082         return (result) ? 0 : -1;
1083
1084 }
1085
1086 off_t yaffs_lseek(int fd, off_t offset, int whence)
1087 {
1088         yaffsfs_Handle *h = NULL;
1089         struct yaffs_obj *obj = NULL;
1090         int pos = -1;
1091         int fSize = -1;
1092
1093         yaffsfs_Lock();
1094         h = yaffsfs_GetHandlePointer(fd);
1095         obj = yaffsfs_GetHandleObject(fd);
1096
1097         if(!h || !obj){
1098                 /* bad handle */
1099                 yaffsfs_SetError(-EBADF);
1100         } else {
1101                 if(whence == SEEK_SET){
1102                         if(offset >= 0)
1103                                 pos = offset;
1104                 } else if(whence == SEEK_CUR) {
1105                         if( (h->position + offset) >= 0)
1106                                 pos = (h->position + offset);
1107                 } else if(whence == SEEK_END) {
1108                         fSize = yaffs_get_obj_length(obj);
1109                         if(fSize >= 0 && (fSize + offset) >= 0)
1110                                 pos = fSize + offset;
1111                 } 
1112
1113                 if(pos >= 0)
1114                         h->position = pos;
1115                 else
1116                         yaffsfs_SetError(-EINVAL);
1117         }
1118
1119         yaffsfs_Unlock();
1120
1121         return pos;
1122 }
1123
1124
1125 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1126 {
1127         struct yaffs_obj *dir = NULL;
1128         struct yaffs_obj *obj = NULL;
1129         YCHAR *name;
1130         int result = YAFFS_FAIL;
1131
1132         yaffsfs_Lock();
1133
1134         obj = yaffsfs_FindObject(NULL,path,0,0);
1135         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1136         if(!dir)
1137                 yaffsfs_SetError(-ENOTDIR);
1138         else if(!obj)
1139                 yaffsfs_SetError(-ENOENT);
1140         else if(obj->my_dev->read_only)
1141                 yaffsfs_SetError(-EINVAL);
1142         else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1143                 yaffsfs_SetError(-EISDIR);
1144         else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1145                 yaffsfs_SetError(-ENOTDIR);
1146         else {
1147                 result = yaffs_unlinker(dir,name);
1148
1149                 if(result == YAFFS_FAIL && isDirectory)
1150                         yaffsfs_SetError(-ENOTEMPTY);
1151         }
1152
1153         yaffsfs_Unlock();
1154
1155         /* todo error */
1156
1157         return (result == YAFFS_FAIL) ? -1 : 0;
1158 }
1159
1160
1161 int yaffs_rmdir(const YCHAR *path)
1162 {
1163         return yaffsfs_DoUnlink(path,1);
1164 }
1165
1166 int yaffs_unlink(const YCHAR *path)
1167 {
1168         return yaffsfs_DoUnlink(path,0);
1169 }
1170
1171 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1172 {
1173         struct yaffs_obj *olddir = NULL;
1174         struct yaffs_obj *newdir = NULL;
1175         struct yaffs_obj *obj = NULL;
1176         YCHAR *oldname;
1177         YCHAR *newname;
1178         int result= YAFFS_FAIL;
1179         int rename_allowed = 1;
1180
1181         yaffsfs_Lock();
1182
1183         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1184         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1185         obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1186
1187         if(!olddir || !newdir || !obj) {
1188                 /* bad file */
1189                 yaffsfs_SetError(-EBADF);
1190                 rename_allowed = 0;
1191         } else if(obj->my_dev->read_only){
1192                 yaffsfs_SetError(-EINVAL);
1193                 rename_allowed = 0;
1194         } else if(olddir->my_dev != newdir->my_dev) {
1195                 /* oops must be on same device */
1196                 /* todo error */
1197                 yaffsfs_SetError(-EXDEV);
1198                 rename_allowed = 0;
1199         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1200                 /*
1201                  * It is a directory, check that it is not being renamed to
1202                  * being its own decendent.
1203                  * Do this by tracing from the new directory back to the root, checking for obj
1204                  */
1205
1206                 struct yaffs_obj *xx = newdir;
1207
1208                 while( rename_allowed && xx){
1209                         if(xx == obj)
1210                                 rename_allowed = 0;
1211                         xx = xx->parent;
1212                 }
1213                 if(!rename_allowed)
1214                         yaffsfs_SetError(-EACCES);
1215         }
1216
1217         if(rename_allowed)
1218                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1219
1220         yaffsfs_Unlock();
1221
1222         return (result == YAFFS_FAIL) ? -1 : 0;
1223 }
1224
1225
1226 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1227 {
1228         int retVal = -1;
1229
1230         obj = yaffs_get_equivalent_obj(obj);
1231
1232         if(obj && buf){
1233                 buf->st_dev = (int)obj->my_dev->os_context;
1234                 buf->st_ino = obj->obj_id;
1235                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1236
1237                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1238                         buf->st_mode |= S_IFDIR;
1239                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1240                         buf->st_mode |= S_IFLNK;
1241                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1242                         buf->st_mode |= S_IFREG;
1243
1244                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1245                 buf->st_uid = 0;
1246                 buf->st_gid = 0;;
1247                 buf->st_rdev = obj->yst_rdev;
1248                 buf->st_size = yaffs_get_obj_length(obj);
1249                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1250                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1251 #if CONFIG_YAFFS_WINCE
1252                 buf->yst_wince_atime[0] = obj->win_atime[0];
1253                 buf->yst_wince_atime[1] = obj->win_atime[1];
1254                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1255                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1256                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1257                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1258 #else
1259                 buf->yst_atime = obj->yst_atime;
1260                 buf->yst_ctime = obj->yst_ctime;
1261                 buf->yst_mtime = obj->yst_mtime;
1262 #endif
1263                 retVal = 0;
1264         }
1265         return retVal;
1266 }
1267
1268 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1269 {
1270         struct yaffs_obj *obj;
1271
1272         int retVal = -1;
1273
1274         yaffsfs_Lock();
1275
1276         obj = yaffsfs_FindObject(NULL,path,0,1);
1277
1278         if(!doLStat && obj)
1279                 obj = yaffsfs_FollowLink(obj,0);
1280
1281         if(obj)
1282                 retVal = yaffsfs_DoStat(obj,buf);
1283         else
1284                 /* todo error not found */
1285                 yaffsfs_SetError(-ENOENT);
1286
1287         yaffsfs_Unlock();
1288
1289         return retVal;
1290
1291 }
1292
1293 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1294 {
1295         return yaffsfs_DoStatOrLStat(path,buf,0);
1296 }
1297
1298 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1299 {
1300         return yaffsfs_DoStatOrLStat(path,buf,1);
1301 }
1302
1303 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1304 {
1305         struct yaffs_obj *obj;
1306
1307         int retVal = -1;
1308
1309         yaffsfs_Lock();
1310         obj = yaffsfs_GetHandleObject(fd);
1311
1312         if(obj)
1313                 retVal = yaffsfs_DoStat(obj,buf);
1314         else
1315                 /* bad handle */
1316                 yaffsfs_SetError(-EBADF);
1317
1318         yaffsfs_Unlock();
1319
1320         return retVal;
1321 }
1322
1323 #ifndef CONFIG_YAFFS_WINCE
1324 /* xattrib functions */
1325
1326
1327 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1328 {
1329         struct yaffs_obj *obj;
1330
1331         int retVal = -1;
1332
1333         yaffsfs_Lock();
1334
1335         obj = yaffsfs_FindObject(NULL,path,0,1);
1336
1337         if(follow)
1338                 obj = yaffsfs_FollowLink(obj,0);
1339
1340         if(obj) {
1341                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1342                 if(retVal< 0){
1343                         yaffsfs_SetError(retVal);
1344                         retVal = -1;
1345                 }
1346         } else
1347                 /* todo error not found */
1348                 yaffsfs_SetError(-ENOENT);
1349
1350         yaffsfs_Unlock();
1351
1352         return retVal;
1353
1354 }
1355
1356 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1357 {
1358         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1359 }
1360
1361 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1362 {
1363         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1364 }
1365
1366
1367
1368 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1369 {
1370         struct yaffs_obj *obj;
1371
1372         int retVal = -1;
1373
1374         yaffsfs_Lock();
1375         obj = yaffsfs_GetHandleObject(fd);
1376
1377         if(obj) {
1378                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1379                 if(retVal< 0){
1380                         yaffsfs_SetError(retVal);
1381                         retVal = -1;
1382                 }
1383         } else
1384                 /* bad handle */
1385                 yaffsfs_SetError(-EBADF);
1386
1387         yaffsfs_Unlock();
1388
1389         return retVal;
1390 }
1391
1392 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1393 {
1394         struct yaffs_obj *obj;
1395
1396         int retVal = -1;
1397
1398         yaffsfs_Lock();
1399
1400         obj = yaffsfs_FindObject(NULL,path,0,1);
1401
1402         if(follow)
1403                 obj = yaffsfs_FollowLink(obj,0);
1404
1405         if(obj) {
1406                 retVal = yaffs_get_xattrib(obj,name,data,size);
1407                 if(retVal< 0){
1408                         yaffsfs_SetError(retVal);
1409                         retVal = -1;
1410                 }
1411         } else
1412                 /* todo error not found */
1413                 yaffsfs_SetError(-ENOENT);
1414
1415         yaffsfs_Unlock();
1416
1417         return retVal;
1418
1419 }
1420
1421 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1422 {
1423         return yaffs_do_getxattr( path, name, data, size, 1);
1424 }
1425 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1426 {
1427         return yaffs_do_getxattr( path, name, data, size, 0);
1428 }
1429
1430
1431
1432 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1433 {
1434         struct yaffs_obj *obj;
1435
1436         int retVal = -1;
1437
1438         yaffsfs_Lock();
1439         obj = yaffsfs_GetHandleObject(fd);
1440
1441         if(obj) {
1442                 retVal = yaffs_get_xattrib(obj,name,data,size);
1443                 if(retVal< 0){
1444                         yaffsfs_SetError(retVal);
1445                         retVal = -1;
1446                 }
1447         } else
1448                 /* bad handle */
1449                 yaffsfs_SetError(-EBADF);
1450
1451         yaffsfs_Unlock();
1452
1453         return retVal;
1454 }
1455
1456 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1457 {
1458         struct yaffs_obj *obj;
1459
1460         int retVal = -1;
1461
1462         yaffsfs_Lock();
1463
1464         obj = yaffsfs_FindObject(NULL,path,0,1);
1465
1466         if(follow)
1467                 obj = yaffsfs_FollowLink(obj,0);
1468
1469         if(obj) {
1470                 retVal = yaffs_list_xattrib(obj, data,size);
1471                 if(retVal< 0){
1472                         yaffsfs_SetError(retVal);
1473                         retVal = -1;
1474                 }
1475         } else
1476                 /* todo error not found */
1477                 yaffsfs_SetError(-ENOENT);
1478
1479         yaffsfs_Unlock();
1480
1481         return retVal;
1482
1483 }
1484
1485 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1486 {
1487         return yaffs_do_listxattr(path, data, size, 1);
1488 }
1489
1490 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1491 {
1492         return yaffs_do_listxattr(path, data, size, 0);
1493 }
1494
1495 int yaffs_flistxattr(int fd, char *data, int size)
1496 {
1497         struct yaffs_obj *obj;
1498
1499         int retVal = -1;
1500
1501         yaffsfs_Lock();
1502         obj = yaffsfs_GetHandleObject(fd);
1503
1504         if(obj) {
1505                 retVal = yaffs_list_xattrib(obj,data,size);
1506                 if(retVal< 0){
1507                         yaffsfs_SetError(retVal);
1508                         retVal = -1;
1509                 }
1510         } else
1511                 /* bad handle */
1512                 yaffsfs_SetError(-EBADF);
1513
1514         yaffsfs_Unlock();
1515
1516         return retVal;
1517 }
1518
1519 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1520 {
1521         struct yaffs_obj *obj;
1522
1523         int retVal = -1;
1524
1525         yaffsfs_Lock();
1526
1527         obj = yaffsfs_FindObject(NULL,path,0,1);
1528
1529         if(follow)
1530                 obj = yaffsfs_FollowLink(obj,0);
1531
1532         if(obj) {
1533                 retVal = yaffs_remove_xattrib(obj,name);
1534                 if(retVal< 0){
1535                         yaffsfs_SetError(retVal);
1536                         retVal = -1;
1537                 }
1538         } else
1539                 /* todo error not found */
1540                 yaffsfs_SetError(-ENOENT);
1541
1542         yaffsfs_Unlock();
1543
1544         return retVal;
1545
1546 }
1547
1548 int yaffs_removexattr(const YCHAR *path, const char *name)
1549 {
1550         return yaffs_do_removexattr(path, name, 1);
1551 }
1552
1553 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1554 {
1555         return yaffs_do_removexattr(path, name, 0);
1556 }
1557
1558 int yaffs_fremovexattr(int fd, const char *name)
1559 {
1560         struct yaffs_obj *obj;
1561
1562         int retVal = -1;
1563
1564         yaffsfs_Lock();
1565         obj = yaffsfs_GetHandleObject(fd);
1566
1567         if(obj){
1568                 retVal = yaffs_remove_xattrib(obj,name);
1569                 if(retVal< 0){
1570                         yaffsfs_SetError(retVal);
1571                         retVal = -1;
1572                 }
1573         }else
1574                 /* bad handle */
1575                 yaffsfs_SetError(-EBADF);
1576
1577         yaffsfs_Unlock();
1578
1579         return retVal;
1580 }
1581 #endif
1582
1583 #ifdef CONFIG_YAFFS_WINCE
1584 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1585 {
1586         struct yaffs_obj *obj;
1587
1588         int retVal = -1;
1589
1590         yaffsfs_Lock();
1591         obj = yaffsfs_GetHandleObject(fd);
1592
1593         if(obj){
1594
1595                 if(wctime){
1596                         wctime[0] = obj->win_ctime[0];
1597                         wctime[1] = obj->win_ctime[1];
1598                 }
1599                 if(watime){
1600                         watime[0] = obj->win_atime[0];
1601                         watime[1] = obj->win_atime[1];
1602                 }
1603                 if(wmtime){
1604                         wmtime[0] = obj->win_mtime[0];
1605                         wmtime[1] = obj->win_mtime[1];
1606                 }
1607
1608
1609                 retVal = 0;
1610         } else
1611                 /*  bad handle */
1612                 yaffsfs_SetError(-EBADF);               
1613         
1614         yaffsfs_Unlock();
1615         
1616         return retVal;
1617 }
1618
1619
1620 int yaffs_set_wince_times(int fd, 
1621                                                   const unsigned *wctime, 
1622                                                   const unsigned *watime, 
1623                                                   const unsigned *wmtime)
1624 {
1625         struct yaffs_obj *obj;
1626         int result;
1627         int retVal = -1;
1628
1629         yaffsfs_Lock();
1630         obj = yaffsfs_GetHandleObject(fd);
1631
1632         if(obj){
1633
1634                 if(wctime){
1635                         obj->win_ctime[0] = wctime[0];
1636                         obj->win_ctime[1] = wctime[1];
1637                 }
1638                 if(watime){
1639                         obj->win_atime[0] = watime[0];
1640                         obj->win_atime[1] = watime[1];
1641                 }
1642                 if(wmtime){
1643                         obj->win_mtime[0] = wmtime[0];
1644                         obj->win_mtime[1] = wmtime[1];
1645                 }
1646
1647                 obj->dirty = 1;
1648                 result = yaffs_flush_file(obj,0,0);
1649                 retVal = 0;
1650         } else
1651                 /* bad handle */
1652                 yaffsfs_SetError(-EBADF);
1653
1654         yaffsfs_Unlock();
1655
1656         return retVal;
1657 }
1658
1659 #endif
1660
1661
1662 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1663 {
1664         int result = -1;
1665
1666         if(obj)
1667                 obj = yaffs_get_equivalent_obj(obj);
1668
1669         if(obj) {
1670                 obj->yst_mode = mode;
1671                 obj->dirty = 1;
1672                 result = yaffs_flush_file(obj,0,0);
1673         }
1674
1675         return result == YAFFS_OK ? 0 : -1;
1676 }
1677
1678
1679 int yaffs_access(const YCHAR *path, int amode)
1680 {
1681         struct yaffs_obj *obj;
1682
1683         int retval = 0;
1684
1685         yaffsfs_Lock();
1686
1687         obj = yaffsfs_FindObject(NULL,path,0,1);
1688
1689         if(obj) {
1690                 int access_ok = 1;
1691
1692                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1693                         access_ok = 0;
1694                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1695                         access_ok = 0;
1696                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1697                         access_ok = 0;
1698
1699                 if(!access_ok) {
1700                         yaffsfs_SetError(-EACCES);
1701                         retval = -1;
1702                 }
1703         } else {
1704                 /* todo error not found */
1705                 yaffsfs_SetError(-ENOENT);
1706                 retval = -1;
1707         }
1708
1709         yaffsfs_Unlock();
1710
1711         return retval;
1712
1713 }
1714
1715
1716 int yaffs_chmod(const YCHAR *path, mode_t mode)
1717 {
1718         struct yaffs_obj *obj;
1719
1720         int retVal = -1;
1721
1722         yaffsfs_Lock();
1723
1724         obj = yaffsfs_FindObject(NULL,path,0,1);
1725
1726         if(!obj)
1727                 yaffsfs_SetError(-ENOENT);
1728         else if(obj->my_dev->read_only)
1729                 yaffsfs_SetError(-EINVAL);
1730         else
1731                 retVal = yaffsfs_DoChMod(obj,mode);
1732
1733         yaffsfs_Unlock();
1734
1735         return retVal;
1736
1737 }
1738
1739
1740 int yaffs_fchmod(int fd, mode_t mode)
1741 {
1742         struct yaffs_obj *obj;
1743
1744         int retVal = -1;
1745
1746         yaffsfs_Lock();
1747         obj = yaffsfs_GetHandleObject(fd);
1748
1749         if(!obj)
1750                 yaffsfs_SetError(-ENOENT);
1751         else if(obj->my_dev->read_only)
1752                 yaffsfs_SetError(-EINVAL);
1753         else
1754                 retVal = yaffsfs_DoChMod(obj,mode);
1755
1756         yaffsfs_Unlock();
1757
1758         return retVal;
1759 }
1760
1761
1762 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1763 {
1764         struct yaffs_obj *parent = NULL;
1765         struct yaffs_obj *dir = NULL;
1766         YCHAR *name;
1767         YCHAR *use_path = NULL;
1768         int path_length = 0;
1769         int retVal= -1;
1770         int i;
1771
1772
1773         /*
1774          * We don't have a definition for max path length.
1775          * We will use 3 * max name length instead.
1776          */
1777         
1778         path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1779
1780         /* If the last character is a path divider, then we need to
1781          * trim it back so that the name look-up works properly.
1782          * eg. /foo/new_dir/ -> /foo/newdir
1783          * Curveball: Need to handle multiple path dividers:
1784          * eg. /foof/sdfse///// -> /foo/sdfse
1785          */
1786         if(path_length > 0 && 
1787                 yaffsfs_IsPathDivider(path[path_length-1])){
1788                 use_path = YMALLOC(path_length + 1);
1789                 if(!use_path){
1790                         yaffsfs_SetError(-ENOMEM);
1791                         return -1;
1792                 }
1793                 strcpy(use_path, path);
1794                 for(i = path_length-1;
1795                         i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1796                         i--)
1797                         use_path[i] = (YCHAR) 0;
1798                 path = use_path;
1799         }
1800         
1801         yaffsfs_Lock();
1802         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1803         if(parent && yaffs_strnlen(name,5) == 0){
1804                 /* Trying to make the root itself */
1805                 yaffsfs_SetError(-EEXIST);
1806         } else if(parent && parent->my_dev->read_only){
1807                 yaffsfs_SetError(-EINVAL);
1808         } else {
1809                 if(parent)
1810                         dir = yaffs_create_dir(parent,name,mode,0,0);
1811                 if(dir)
1812                         retVal = 0;
1813                 else {
1814                         if(!parent)
1815                                 yaffsfs_SetError(-ENOENT); /* missing path */
1816                         else if (yaffs_find_by_name(parent,name))
1817                                 yaffsfs_SetError(-EEXIST); /* the name already exists */
1818                         else
1819                                 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1820                         retVal = -1;
1821                 }
1822         }
1823
1824         yaffsfs_Unlock();
1825
1826         if(use_path)
1827                 YFREE(use_path);
1828
1829         return retVal;
1830 }
1831
1832 void * yaffs_getdev(const YCHAR *path)
1833 {
1834         struct yaffs_dev *dev=NULL;
1835         YCHAR *dummy;
1836         dev = yaffsfs_FindDevice(path,&dummy);
1837         return (void *)dev;
1838 }
1839
1840 int yaffs_mount2(const YCHAR *path,int read_only)
1841 {
1842         int retVal=-1;
1843         int result=YAFFS_FAIL;
1844         struct yaffs_dev *dev=NULL;
1845         YCHAR *dummy;
1846
1847         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1848
1849         yaffsfs_Lock();
1850
1851         yaffsfs_InitHandles();
1852
1853         dev = yaffsfs_FindDevice(path,&dummy);
1854         if(dev){
1855                 if(!dev->is_mounted){
1856                         dev->read_only = read_only ? 1 : 0;
1857                         result = yaffs_guts_initialise(dev);
1858                         if(result == YAFFS_FAIL)
1859                                 /* todo error - mount failed */
1860                                 yaffsfs_SetError(-ENOMEM);
1861                         retVal = result ? 0 : -1;
1862
1863                 }
1864                 else
1865                         /* todo error - already mounted. */
1866                         yaffsfs_SetError(-EBUSY);
1867         } else
1868                 /* todo error - no device */
1869                 yaffsfs_SetError(-ENODEV);
1870
1871         yaffsfs_Unlock();
1872         return retVal;
1873
1874 }
1875
1876 int yaffs_mount(const YCHAR *path)
1877 {
1878         return yaffs_mount2(path,0);
1879 }
1880
1881 int yaffs_sync(const YCHAR *path)
1882 {
1883         int retVal=-1;
1884         struct yaffs_dev *dev=NULL;
1885         YCHAR *dummy;
1886         
1887         yaffsfs_Lock();
1888         dev = yaffsfs_FindDevice(path,&dummy);
1889         if(dev){
1890                 if(dev->is_mounted){
1891                         
1892                         yaffs_flush_whole_cache(dev);
1893                         yaffs_checkpoint_save(dev);
1894                         retVal = 0;
1895                         
1896                 } else
1897                         /* todo error - not mounted. */
1898                         yaffsfs_SetError(-EINVAL);
1899                         
1900         }else
1901                 /* todo error - no device */
1902                 yaffsfs_SetError(-ENODEV);
1903
1904         yaffsfs_Unlock();
1905         return retVal;  
1906 }
1907
1908
1909 int yaffs_remount(const YCHAR *path, int force, int read_only)
1910 {
1911         int retVal=-1;
1912         struct yaffs_dev *dev=NULL;
1913         YCHAR *dummy;
1914
1915         yaffsfs_Lock();
1916         dev = yaffsfs_FindDevice(path,&dummy);
1917         if(dev){
1918                 if(dev->is_mounted){
1919                         int i;
1920                         int inUse;
1921
1922                         yaffs_flush_whole_cache(dev);
1923
1924                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1925                                 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1926                                         inUse = 1; /* the device is in use, can't unmount */
1927                         }
1928
1929                         if(!inUse || force){
1930                                 if(read_only)
1931                                         yaffs_checkpoint_save(dev);
1932                                 dev->read_only =  read_only ? 1 : 0;
1933                                 retVal = 0;
1934                         } else
1935                                 yaffsfs_SetError(-EBUSY);
1936
1937                 } else
1938                         yaffsfs_SetError(-EINVAL);
1939
1940         }
1941         else
1942                 yaffsfs_SetError(-ENODEV);
1943
1944         yaffsfs_Unlock();
1945         return retVal;
1946
1947 }
1948
1949 int yaffs_unmount2(const YCHAR *path, int force)
1950 {
1951         int retVal=-1;
1952         struct yaffs_dev *dev=NULL;
1953         YCHAR *dummy;
1954
1955         yaffsfs_Lock();
1956         dev = yaffsfs_FindDevice(path,&dummy);
1957         if(dev){
1958                 if(dev->is_mounted){
1959                         int i;
1960                         int inUse;
1961
1962                         yaffs_flush_whole_cache(dev);
1963                         yaffs_checkpoint_save(dev);
1964
1965                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1966                                 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1967                                         inUse = 1; /* the device is in use, can't unmount */
1968                         }
1969
1970                         if(!inUse || force){
1971                                 yaffs_deinitialise(dev);
1972
1973                                 retVal = 0;
1974                         } else
1975                                 /* todo error can't unmount as files are open */
1976                                 yaffsfs_SetError(-EBUSY);
1977
1978                 } else
1979                         /* todo error - not mounted. */
1980                         yaffsfs_SetError(-EINVAL);
1981
1982         }
1983         else
1984                 /* todo error - no device */
1985                 yaffsfs_SetError(-ENODEV);
1986
1987         yaffsfs_Unlock();
1988         return retVal;
1989
1990 }
1991
1992 int yaffs_unmount(const YCHAR *path)
1993 {
1994         return yaffs_unmount2(path,0);
1995 }
1996
1997 loff_t yaffs_freespace(const YCHAR *path)
1998 {
1999         loff_t retVal=-1;
2000         struct yaffs_dev *dev=NULL;
2001         YCHAR *dummy;
2002
2003         yaffsfs_Lock();
2004         dev = yaffsfs_FindDevice(path,&dummy);
2005         if(dev  && dev->is_mounted){
2006                 retVal = yaffs_get_n_free_chunks(dev);
2007                 retVal *= dev->data_bytes_per_chunk;
2008
2009         } else
2010                 yaffsfs_SetError(-EINVAL);
2011
2012         yaffsfs_Unlock();
2013         return retVal;
2014 }
2015
2016 loff_t yaffs_totalspace(const YCHAR *path)
2017 {
2018         loff_t retVal=-1;
2019         struct yaffs_dev *dev=NULL;
2020         YCHAR *dummy;
2021
2022         yaffsfs_Lock();
2023         dev = yaffsfs_FindDevice(path,&dummy);
2024         if(dev  && dev->is_mounted){
2025                 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2026                 retVal *= dev->param.chunks_per_block;
2027                 retVal *= dev->data_bytes_per_chunk;
2028
2029         } else
2030                 yaffsfs_SetError(-EINVAL);
2031
2032         yaffsfs_Unlock();
2033         return retVal;
2034 }
2035
2036 int yaffs_inodecount(const YCHAR *path)
2037 {
2038         loff_t retVal= -1;
2039         struct yaffs_dev *dev=NULL;
2040         YCHAR *dummy;
2041
2042         yaffsfs_Lock();
2043         dev = yaffsfs_FindDevice(path,&dummy);
2044         if(dev  && dev->is_mounted) {
2045            int n_obj = dev->n_obj;
2046            if(n_obj > dev->n_hardlinks)
2047                 retVal = n_obj - dev->n_hardlinks;
2048         }
2049         
2050         if(retVal < 0)
2051                 yaffsfs_SetError(-EINVAL);
2052         
2053         yaffsfs_Unlock();
2054         return retVal;  
2055 }
2056
2057
2058 void yaffs_add_device(struct yaffs_dev *dev)
2059 {
2060         dev->is_mounted = 0;
2061         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2062
2063         if(!dev->dev_list.next)
2064                 INIT_LIST_HEAD(&dev->dev_list);
2065
2066         list_add(&dev->dev_list,&yaffsfs_deviceList);
2067 }
2068
2069 void yaffs_remove_device(struct yaffs_dev *dev)
2070 {
2071         list_del_init(&dev->dev_list);
2072 }
2073
2074
2075
2076
2077 /* Directory search stuff. */
2078
2079 /*
2080  * Directory search context
2081  *
2082  * NB this is an opaque structure.
2083  */
2084
2085
2086 typedef struct
2087 {
2088         u32 magic;
2089         yaffs_dirent de;                /* directory entry being used by this dsc */
2090         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2091         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2092         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2093         int offset;
2094         struct list_head others;       
2095 } yaffsfs_DirectorySearchContext;
2096
2097
2098
2099 static struct list_head search_contexts;
2100
2101
2102 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2103 {
2104         if(dsc &&
2105            dsc->dirObj &&
2106            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2107
2108            dsc->offset = 0;
2109
2110            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2111                 dsc->nextReturn = NULL;
2112            else
2113                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2114                                                 struct yaffs_obj,siblings);
2115         } else {
2116                 /* Hey someone isn't playing nice! */
2117         }
2118 }
2119
2120 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2121 {
2122         if(dsc &&
2123            dsc->dirObj &&
2124            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2125
2126            if( dsc->nextReturn == NULL ||
2127                list_empty(&dsc->dirObj->variant.dir_variant.children))
2128                 dsc->nextReturn = NULL;
2129            else {
2130                    struct list_head *next = dsc->nextReturn->siblings.next;
2131
2132                    if( next == &dsc->dirObj->variant.dir_variant.children)
2133                         dsc->nextReturn = NULL; /* end of list */
2134                    else
2135                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2136            }
2137         } else {
2138                 /* Hey someone isn't playing nice! */
2139         }
2140 }
2141
2142 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2143 {
2144
2145         struct list_head *i;
2146         yaffsfs_DirectorySearchContext *dsc;
2147
2148         /* if search contexts not initilised then skip */
2149         if(!search_contexts.next)
2150                 return;
2151
2152         /* Iterate through the directory search contexts.
2153          * If any are the one being removed, then advance the dsc to
2154          * the next one to prevent a hanging ptr.
2155          */
2156          list_for_each(i, &search_contexts) {
2157                 if (i) {
2158                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2159                         if(dsc->nextReturn == obj)
2160                                 yaffsfs_DirAdvance(dsc);
2161                 }
2162         }
2163
2164 }
2165
2166 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2167 {
2168         yaffs_DIR *dir = NULL;
2169         struct yaffs_obj *obj = NULL;
2170         yaffsfs_DirectorySearchContext *dsc = NULL;
2171
2172         yaffsfs_Lock();
2173
2174         obj = yaffsfs_FindObject(NULL,dirname,0,1);
2175
2176         if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2177
2178                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2179                 dir = (yaffs_DIR *)dsc;
2180
2181                 if(dsc){
2182                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2183                         dsc->magic = YAFFS_MAGIC;
2184                         dsc->dirObj = obj;
2185                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2186                         INIT_LIST_HEAD(&dsc->others);
2187
2188                         if(!search_contexts.next)
2189                                 INIT_LIST_HEAD(&search_contexts);
2190
2191                         list_add(&dsc->others,&search_contexts);       
2192                         yaffsfs_SetDirRewound(dsc);
2193                 }
2194
2195         }
2196
2197         yaffsfs_Unlock();
2198
2199         return dir;
2200 }
2201
2202 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2203 {
2204         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2205         struct yaffs_dirent *retVal = NULL;
2206
2207         yaffsfs_Lock();
2208
2209         if(dsc && dsc->magic == YAFFS_MAGIC){
2210                 yaffsfs_SetError(0);
2211                 if(dsc->nextReturn){
2212                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2213                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2214                         dsc->de.d_off = dsc->offset++;
2215                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2216                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2217                         {
2218                                 /* this should not happen! */
2219                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2220                         }
2221                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2222                         retVal = &dsc->de;
2223                         yaffsfs_DirAdvance(dsc);
2224                 } else
2225                         retVal = NULL;
2226         } else
2227                 yaffsfs_SetError(-EBADF);
2228
2229         yaffsfs_Unlock();
2230
2231         return retVal;
2232
2233 }
2234
2235
2236 void yaffs_rewinddir(yaffs_DIR *dirp)
2237 {
2238         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2239
2240         yaffsfs_Lock();
2241
2242         yaffsfs_SetDirRewound(dsc);
2243
2244         yaffsfs_Unlock();
2245 }
2246
2247
2248 int yaffs_closedir(yaffs_DIR *dirp)
2249 {
2250         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2251
2252         yaffsfs_Lock();
2253         dsc->magic = 0;
2254         list_del(&dsc->others); /* unhook from list */
2255         YFREE(dsc);
2256         yaffsfs_Unlock();
2257         return 0;
2258 }
2259
2260 /* End of directory stuff */
2261
2262
2263 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2264 {
2265         struct yaffs_obj *parent = NULL;
2266         struct yaffs_obj *obj;
2267         YCHAR *name;
2268         int retVal= -1;
2269         int mode = 0; /* ignore for now */
2270
2271         yaffsfs_Lock();
2272         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2273         if(parent && parent->my_dev->read_only)
2274                 yaffsfs_SetError(-EINVAL);
2275         else if(parent){
2276                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2277                 if(obj)
2278                         retVal = 0;
2279                 else{
2280                         yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2281                         retVal = -1;
2282                 }
2283         } else {
2284                 yaffsfs_SetError(-EINVAL);
2285                 retVal = -1;
2286         }
2287
2288         yaffsfs_Unlock();
2289
2290         return retVal;
2291
2292 }
2293
2294 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2295 {
2296         struct yaffs_obj *obj = NULL;
2297         int retVal;
2298
2299
2300         yaffsfs_Lock();
2301
2302         obj = yaffsfs_FindObject(NULL,path,0,1);
2303
2304         if(!obj) {
2305                 yaffsfs_SetError(-ENOENT);
2306                 retVal = -1;
2307         } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2308                 yaffsfs_SetError(-EINVAL);
2309                 retVal = -1;
2310         } else {
2311                 YCHAR *alias = obj->variant.symlink_variant.alias;
2312                 memset(buf,0,bufsiz);
2313                 yaffs_strncpy(buf,alias,bufsiz - 1);
2314                 retVal = 0;
2315         }
2316         yaffsfs_Unlock();
2317         return retVal;
2318 }
2319
2320 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2321 {
2322         /* Creates a link called newpath to existing oldpath */
2323         struct yaffs_obj *obj = NULL;
2324         struct yaffs_obj *target = NULL;
2325         int retVal = 0;
2326         int new_nameLength = 0;
2327
2328
2329         yaffsfs_Lock();
2330
2331         obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2332         target = yaffsfs_FindObject(NULL,newpath,0,0);
2333
2334         if(!obj) {
2335                 yaffsfs_SetError(-ENOENT);
2336                 retVal = -1;
2337         } else if(obj->my_dev->read_only){
2338                 yaffsfs_SetError(-EINVAL);
2339                 retVal= -1;
2340         } else if(target) {
2341                 yaffsfs_SetError(-EEXIST);
2342                 retVal = -1;
2343         } else {
2344                 struct yaffs_obj *newdir = NULL;
2345                 struct yaffs_obj *link = NULL;
2346
2347                 YCHAR *newname;
2348
2349                 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2350
2351                 if(!newdir){
2352                         yaffsfs_SetError(-ENOTDIR);
2353                         retVal = -1;
2354                 }else if(newdir->my_dev != obj->my_dev){
2355                         yaffsfs_SetError(-EXDEV);
2356                         retVal = -1;
2357                 }
2358                 
2359                 new_nameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2360                 
2361                 if(new_nameLength == 0){
2362                         yaffsfs_SetError(-ENOENT);
2363                         retVal = -1;
2364                 } else if (new_nameLength > YAFFS_MAX_NAME_LENGTH){
2365                         yaffsfs_SetError(-ENAMETOOLONG);
2366                         retVal = -1;
2367                 }
2368                 
2369                 if(retVal == 0) {
2370                         link = yaffs_link_obj(newdir,newname,obj);
2371                         if(link)
2372                                 retVal = 0;
2373                         else{
2374                                 yaffsfs_SetError(-ENOSPC);
2375                                 retVal = -1;
2376                         }
2377
2378                 }
2379         }
2380         yaffsfs_Unlock();
2381
2382         return retVal;
2383 }
2384
2385 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2386 {
2387         return -1;
2388 }
2389
2390
2391
2392 /*
2393  * yaffs_n_handles()
2394  * Returns number of handles attached to the object
2395  */
2396 int yaffs_n_handles(const YCHAR *path)
2397 {
2398         struct yaffs_obj *obj;
2399
2400         obj = yaffsfs_FindObject(NULL,path,0,1);
2401
2402         return yaffsfs_CountHandles(obj);
2403 }
2404
2405 int yaffs_get_error(void)
2406 {
2407         return yaffsfs_GetLastError();
2408 }
2409
2410 int yaffs_set_error(int error)
2411 {
2412         /*yaffsfs_SetError does not return. So the program is assumed to have worked. */
2413         yaffsfs_SetError(error);
2414         return 0;
2415 }
2416
2417 int yaffs_dump_dev(const YCHAR *path)
2418 {
2419 #if 0
2420         YCHAR *rest;
2421
2422         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2423
2424         if(obj){
2425                 struct yaffs_dev *dev = obj->my_dev;
2426
2427                 printf("\n"
2428                            "n_page_writes.......... %d\n"
2429                            "n_page_reads........... %d\n"
2430                            "n_erasures....... %d\n"
2431                            "n_gc_copies............ %d\n"
2432                            "garbageCollections... %d\n"
2433                            "passiveGarbageColl'ns %d\n"
2434                            "\n",
2435                                 dev->n_page_writes,
2436                                 dev->n_page_reads,
2437                                 dev->n_erasures,
2438                                 dev->n_gc_copies,
2439                                 dev->garbageCollections,
2440                                 dev->passiveGarbageCollections
2441                 );
2442
2443         }
2444
2445 #endif
2446         return 0;
2447 }
2448