yaffs Found another bug.
[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                 if(obj){
645
646                         /* The file already exists or it might be a directory */
647
648                         /* If it is a directory then we can't open it as a file */
649                         if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
650                                 openDenied = 1;
651                                 yaffsfs_SetError(-EISDIR);
652                                 errorReported = 1;
653                         }
654
655                         /* Open should fail if O_CREAT and O_EXCL are specified since
656                          * the file exists
657                          */
658                         if((oflag & O_EXCL) && (oflag & O_CREAT)){
659                                 openDenied = 1;
660                                 yaffsfs_SetError(-EEXIST);
661                                 errorReported = 1;
662                         }
663
664                         /* Check file permissions */
665                         if( readRequested && !(obj->yst_mode & S_IREAD))
666                                 openDenied = 1;
667
668                         if( writeRequested && !(obj->yst_mode & S_IWRITE))
669                                 openDenied = 1;
670
671                         /* Check sharing of an existing object. */
672                         {
673                                 yaffsfs_Handle *hx;
674                                 int i;
675                                 sharedReadAllowed = 1;
676                                 sharedWriteAllowed = 1;
677                                 alreadyReading = 0;
678                                 alreadyWriting = 0;
679                                 for( i = 0; i < YAFFSFS_N_HANDLES; i++){
680                                         hx = &yaffsfs_handle[i];
681                                         if(hx->useCount > 0 &&
682                                                 hx->inodeId >= 0 &&
683                                                 yaffsfs_inode[hx->inodeId].iObj == obj){
684                                                 if(!hx->shareRead)
685                                                         sharedReadAllowed = 0;
686                                                 if(!hx->shareWrite)
687                                                         sharedWriteAllowed = 0;
688                                                 if(hx->reading)
689                                                         alreadyReading = 1;
690                                                 if(hx->writing)
691                                                         alreadyWriting = 1;
692                                         }
693                                 }
694
695
696
697                                 if((!sharedReadAllowed && readRequested)|| 
698                                         (!shareRead  && alreadyReading) ||
699                                         (!sharedWriteAllowed && writeRequested) ||
700                                         (!shareWrite && alreadyWriting)){
701                                         openDenied = 1;
702                                         yaffsfs_SetError(-EBUSY);
703                                         errorReported=1;
704                                 }
705                         }
706
707                 } else if((oflag & O_CREAT)) {
708                         /* Let's see if we can create this file */
709                         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
710                         if(dir  && dir->my_dev->read_only){
711                                 yaffsfs_SetError(-EINVAL);
712                                 errorReported = 1;
713                         } else if(dir)
714                                 obj = yaffs_create_file(dir,name,mode,0,0);
715                         else {
716                                 yaffsfs_SetError(-ENOTDIR);
717                                 errorReported = 1;
718                         }
719                 }
720
721                 if(obj && !openDenied) {
722                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
723
724                         if(inodeId<0) {
725                                 /*
726                                  * Todo: Fix any problem if inodes run out, though that
727                                  * can't happen if the number of inode items >= number of handles. 
728                                  */
729                         }
730                         
731                         yh->inodeId = inodeId;
732                         yh->reading = readRequested;
733                         yh->writing = writeRequested;
734                         yh->append =  (oflag & O_APPEND) ? 1 : 0;
735                         yh->position = 0;
736                         yh->shareRead = shareRead;
737                         yh->shareWrite = shareWrite;
738
739                         /* Hook inode to object */
740                         obj->my_inode = (void*) &yaffsfs_inode[inodeId];
741
742                         if((oflag & O_TRUNC) && yh->writing)
743                                 yaffs_resize_file(obj,0);
744                 } else {
745                         yaffsfs_PutHandle(handle);
746                         if(!errorReported) {
747                                 yaffsfs_SetError(!obj ? -ENOSPC : -EACCES);
748                                 errorReported = 1;
749                         }
750                         handle = -1;
751                 }
752         }
753
754         yaffsfs_Unlock();
755
756         return handle;
757 }
758
759 int yaffs_open(const YCHAR *path, int oflag, int mode)
760 {
761         return yaffs_open_sharing(path, oflag, mode, YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
762 }
763
764 int yaffs_Dofsync(int fd,int datasync)
765 {
766         yaffsfs_Handle *h = NULL;
767         int retVal = 0;
768
769         yaffsfs_Lock();
770
771         h = yaffsfs_GetHandlePointer(fd);
772
773         if(h && h->useCount > 0)
774                 /* flush the file */
775                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,datasync);
776         else {
777                 /* bad handle */
778                 yaffsfs_SetError(-EBADF);
779                 retVal = -1;
780         }
781
782         yaffsfs_Unlock();
783
784         return retVal;
785 }
786
787 int yaffs_fsync(int fd)
788 {
789         return yaffs_Dofsync(fd,0);
790 }
791
792 int yaffs_flush(int fd)
793 {
794         return yaffs_fsync(fd);
795 }
796
797 int yaffs_fdatasync(int fd)
798 {
799         return yaffs_Dofsync(fd,1);
800 }
801
802 int yaffs_close(int fd)
803 {
804         yaffsfs_Handle *h = NULL;
805         int retVal = 0;
806
807         yaffsfs_Lock();
808
809         h = yaffsfs_GetHandlePointer(fd);
810
811         if(h && h->useCount > 0) {
812                 /* clean up */
813                 yaffs_flush_file(yaffsfs_inode[h->inodeId].iObj,1,0);
814                 yaffsfs_PutHandle(fd);
815                 retVal = 0;
816         } else {
817                 /* bad handle */
818                 yaffsfs_SetError(-EBADF);
819                 retVal = -1;
820         }
821
822         yaffsfs_Unlock();
823
824         return retVal;
825 }
826
827
828
829 int yaffsfs_do_read(int fd, void *vbuf, unsigned int nbyte, int isPread, int offset)
830 {
831         yaffsfs_Handle *h = NULL;
832         struct yaffs_obj *obj = NULL;
833         int pos = 0;
834         int startPos = 0;
835         int nRead = 0;
836         int nToRead = 0;
837         int totalRead = 0;
838         unsigned int maxRead;
839         u8 *buf = (u8 *)vbuf;
840
841         yaffsfs_Lock();
842         h = yaffsfs_GetHandlePointer(fd);
843         obj = yaffsfs_GetHandleObject(fd);
844
845         if(!h || !obj){
846                 /* bad handle */
847                 yaffsfs_SetError(-EBADF);
848                 totalRead = -1;
849         } else if(!h->reading){
850                 /* Not a reading handle */
851                 yaffsfs_SetError(-EINVAL);
852                 totalRead = -1;
853         } else {
854                 if(isPread)
855                         startPos = offset;
856                 else
857                         startPos = h->position;
858
859                 pos = startPos;
860                                         
861                 if(yaffs_get_obj_length(obj) > pos)
862                         maxRead = yaffs_get_obj_length(obj) - pos;
863                 else
864                         maxRead = 0;
865
866                 if(nbyte > maxRead)
867                         nbyte = maxRead;
868
869
870                 yaffsfs_GetHandle(fd);
871
872                 while(nbyte > 0) {
873                         nToRead = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
874                         if(nToRead > nbyte)
875                                 nToRead = nbyte;
876                         nRead = yaffs_file_rd(obj,buf,pos,nToRead);
877
878                         if(nRead > 0){
879                                 totalRead += nRead;
880                                 pos += nRead;
881                                 buf += nRead;
882                         }
883
884                         if(nRead == nToRead)
885                                 nbyte-=nRead;
886                         else
887                                 nbyte = 0; /* no more to read */
888                                         
889                                         
890                         if(nbyte > 0){
891                                 yaffsfs_Unlock();
892                                 yaffsfs_Lock();
893                         }
894
895                 }
896
897                 yaffsfs_PutHandle(fd);
898
899                 if(!isPread) {
900                         if(totalRead >= 0)
901                                 h->position = startPos + totalRead;
902                         else {
903                                         /* todo error */
904                         }
905                 }
906
907         }
908
909         yaffsfs_Unlock();
910
911         return (totalRead >= 0) ? totalRead : -1;
912
913 }
914
915 int yaffs_read(int fd, void *buf, unsigned int nbyte)
916 {
917         return yaffsfs_do_read(fd, buf, nbyte, 0, 0);
918 }
919
920 int yaffs_pread(int fd, void *buf, unsigned int nbyte, unsigned int offset)
921 {
922         return yaffsfs_do_read(fd, buf, nbyte, 1, offset);
923 }
924
925 int yaffsfs_do_write(int fd, const void *vbuf, unsigned int nbyte, int isPwrite, int offset)
926 {
927         yaffsfs_Handle *h = NULL;
928         struct yaffs_obj *obj = NULL;
929         int pos = 0;
930         int startPos = 0;
931         int nWritten = 0;
932         int totalWritten = 0;
933         int write_trhrough = 0;
934         int nToWrite = 0;
935         const u8 *buf = (const u8 *)vbuf;
936
937         yaffsfs_Lock();
938         h = yaffsfs_GetHandlePointer(fd);
939         obj = yaffsfs_GetHandleObject(fd);
940
941         if(!h || !obj){
942                 /* bad handle */
943                 yaffsfs_SetError(-EBADF);
944                 totalWritten = -1;
945         } else if( h && obj && (!h->writing || obj->my_dev->read_only)){
946                 yaffsfs_SetError(-EINVAL);
947                 totalWritten=-1;
948         } else {
949                 if(h->append)
950                         startPos = yaffs_get_obj_length(obj);
951                 else if(isPwrite)
952                         startPos = offset;
953                 else
954                         startPos = h->position;
955
956                 yaffsfs_GetHandle(fd);
957                 pos = startPos;
958                 while(nbyte > 0) {
959                         nToWrite = YAFFSFS_RW_SIZE - (pos & (YAFFSFS_RW_SIZE -1));
960                         if(nToWrite > nbyte)
961                                 nToWrite = nbyte;
962
963                         nWritten = yaffs_wr_file(obj,buf,pos,nToWrite,write_trhrough);
964                         if(nWritten > 0){
965                                 totalWritten += nWritten;
966                                 pos += nWritten;
967                                 buf += nWritten;
968                         }
969
970                         if(nWritten == nToWrite)
971                                 nbyte -= nToWrite;
972                         else
973                                 nbyte = 0;
974
975                         if(nWritten < 1 && totalWritten < 1){
976                                 yaffsfs_SetError(-ENOSPC);
977                                 totalWritten = -1;
978                         }
979
980                         if(nbyte > 0){
981                                 yaffsfs_Unlock();
982                                 yaffsfs_Lock();
983                         }
984                 }
985
986                 yaffsfs_PutHandle(fd);
987
988                 if(!isPwrite){
989                         if(totalWritten > 0)
990                                 h->position = startPos + totalWritten;
991                         else {
992                                 /* todo error */
993                         }
994                 }
995         }
996
997         yaffsfs_Unlock();
998
999         return (totalWritten >= 0) ? totalWritten : -1;
1000 }
1001
1002 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1003 {
1004         return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1005 }
1006
1007 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, unsigned int offset)
1008 {
1009         return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1010 }
1011
1012
1013 int yaffs_truncate(const YCHAR *path,off_t new_size)
1014 {
1015         struct yaffs_obj *obj = NULL;
1016         int result = YAFFS_FAIL;
1017
1018         yaffsfs_Lock();
1019
1020         obj = yaffsfs_FindObject(NULL,path,0,1);
1021
1022         if(!obj)
1023                 yaffsfs_SetError(-ENOENT);
1024         else if(obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1025                 yaffsfs_SetError(-EISDIR);
1026         else if(obj->my_dev->read_only)
1027                 yaffsfs_SetError(-EINVAL);
1028         else
1029                 result = yaffs_resize_file(obj,new_size);
1030
1031         yaffsfs_Unlock();
1032
1033
1034         return (result) ? 0 : -1;
1035 }
1036
1037 int yaffs_ftruncate(int fd, off_t new_size)
1038 {
1039         yaffsfs_Handle *h = NULL;
1040         struct yaffs_obj *obj = NULL;
1041         int result = 0;
1042
1043         yaffsfs_Lock();
1044         h = yaffsfs_GetHandlePointer(fd);
1045         obj = yaffsfs_GetHandleObject(fd);
1046
1047         if(!h || !obj)
1048                 /* bad handle */
1049                 yaffsfs_SetError(-EBADF);
1050         else if(obj->my_dev->read_only)
1051                 yaffsfs_SetError(-EINVAL);
1052         else
1053                 /* resize the file */
1054                 result = yaffs_resize_file(obj,new_size);
1055         yaffsfs_Unlock();
1056
1057
1058         return (result) ? 0 : -1;
1059
1060 }
1061
1062 off_t yaffs_lseek(int fd, off_t offset, int whence)
1063 {
1064         yaffsfs_Handle *h = NULL;
1065         struct yaffs_obj *obj = NULL;
1066         int pos = -1;
1067         int fSize = -1;
1068
1069         yaffsfs_Lock();
1070         h = yaffsfs_GetHandlePointer(fd);
1071         obj = yaffsfs_GetHandleObject(fd);
1072
1073         if(!h || !obj){
1074                 /* bad handle */
1075                 yaffsfs_SetError(-EBADF);
1076         } else {
1077                 if(whence == SEEK_SET){
1078                         if(offset >= 0)
1079                                 pos = offset;
1080                 } else if(whence == SEEK_CUR) {
1081                         if( (h->position + offset) >= 0)
1082                                 pos = (h->position + offset);
1083                 } else if(whence == SEEK_END) {
1084                         fSize = yaffs_get_obj_length(obj);
1085                         if(fSize >= 0 && (fSize + offset) >= 0)
1086                                 pos = fSize + offset;
1087                 } 
1088
1089                 if(pos >= 0)
1090                         h->position = pos;
1091                 else
1092                         yaffsfs_SetError(-EINVAL);
1093         }
1094
1095         yaffsfs_Unlock();
1096
1097         return pos;
1098 }
1099
1100
1101 int yaffsfs_DoUnlink(const YCHAR *path,int isDirectory)
1102 {
1103         struct yaffs_obj *dir = NULL;
1104         struct yaffs_obj *obj = NULL;
1105         YCHAR *name;
1106         int result = YAFFS_FAIL;
1107
1108         yaffsfs_Lock();
1109
1110         obj = yaffsfs_FindObject(NULL,path,0,0);
1111         dir = yaffsfs_FindDirectory(NULL,path,&name,0);
1112         if(!dir)
1113                 yaffsfs_SetError(-ENOTDIR);
1114         else if(!obj)
1115                 yaffsfs_SetError(-ENOENT);
1116         else if(obj->my_dev->read_only)
1117                 yaffsfs_SetError(-EINVAL);
1118         else if(!isDirectory && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1119                 yaffsfs_SetError(-EISDIR);
1120         else if(isDirectory && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1121                 yaffsfs_SetError(-ENOTDIR);
1122         else {
1123                 result = yaffs_unlinker(dir,name);
1124
1125                 if(result == YAFFS_FAIL && isDirectory)
1126                         yaffsfs_SetError(-ENOTEMPTY);
1127         }
1128
1129         yaffsfs_Unlock();
1130
1131         /* todo error */
1132
1133         return (result == YAFFS_FAIL) ? -1 : 0;
1134 }
1135
1136
1137 int yaffs_rmdir(const YCHAR *path)
1138 {
1139         return yaffsfs_DoUnlink(path,1);
1140 }
1141
1142 int yaffs_unlink(const YCHAR *path)
1143 {
1144         return yaffsfs_DoUnlink(path,0);
1145 }
1146
1147 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1148 {
1149         struct yaffs_obj *olddir = NULL;
1150         struct yaffs_obj *newdir = NULL;
1151         struct yaffs_obj *obj = NULL;
1152         YCHAR *oldname;
1153         YCHAR *newname;
1154         int result= YAFFS_FAIL;
1155         int rename_allowed = 1;
1156
1157         yaffsfs_Lock();
1158
1159         olddir = yaffsfs_FindDirectory(NULL,oldPath,&oldname,0);
1160         newdir = yaffsfs_FindDirectory(NULL,newPath,&newname,0);
1161         obj = yaffsfs_FindObject(NULL,oldPath,0,0);
1162
1163         if(!olddir || !newdir || !obj) {
1164                 /* bad file */
1165                 yaffsfs_SetError(-EBADF);
1166                 rename_allowed = 0;
1167         } else if(obj->my_dev->read_only){
1168                 yaffsfs_SetError(-EINVAL);
1169                 rename_allowed = 0;
1170         } else if(olddir->my_dev != newdir->my_dev) {
1171                 /* oops must be on same device */
1172                 /* todo error */
1173                 yaffsfs_SetError(-EXDEV);
1174                 rename_allowed = 0;
1175         } else if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1176                 /*
1177                  * It is a directory, check that it is not being renamed to
1178                  * being its own decendent.
1179                  * Do this by tracing from the new directory back to the root, checking for obj
1180                  */
1181
1182                 struct yaffs_obj *xx = newdir;
1183
1184                 while( rename_allowed && xx){
1185                         if(xx == obj)
1186                                 rename_allowed = 0;
1187                         xx = xx->parent;
1188                 }
1189                 if(!rename_allowed)
1190                         yaffsfs_SetError(-EACCES);
1191         }
1192
1193         if(rename_allowed)
1194                 result = yaffs_rename_obj(olddir,oldname,newdir,newname);
1195
1196         yaffsfs_Unlock();
1197
1198         return (result == YAFFS_FAIL) ? -1 : 0;
1199 }
1200
1201
1202 static int yaffsfs_DoStat(struct yaffs_obj *obj,struct yaffs_stat *buf)
1203 {
1204         int retVal = -1;
1205
1206         obj = yaffs_get_equivalent_obj(obj);
1207
1208         if(obj && buf){
1209                 buf->st_dev = (int)obj->my_dev->os_context;
1210                 buf->st_ino = obj->obj_id;
1211                 buf->st_mode = obj->yst_mode & ~S_IFMT; /* clear out file type bits */
1212
1213                 if(obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1214                         buf->st_mode |= S_IFDIR;
1215                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1216                         buf->st_mode |= S_IFLNK;
1217                 else if(obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1218                         buf->st_mode |= S_IFREG;
1219
1220                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1221                 buf->st_uid = 0;
1222                 buf->st_gid = 0;;
1223                 buf->st_rdev = obj->yst_rdev;
1224                 buf->st_size = yaffs_get_obj_length(obj);
1225                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1226                 buf->st_blocks = (buf->st_size + buf->st_blksize -1)/buf->st_blksize;
1227 #if CONFIG_YAFFS_WINCE
1228                 buf->yst_wince_atime[0] = obj->win_atime[0];
1229                 buf->yst_wince_atime[1] = obj->win_atime[1];
1230                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1231                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1232                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1233                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1234 #else
1235                 buf->yst_atime = obj->yst_atime;
1236                 buf->yst_ctime = obj->yst_ctime;
1237                 buf->yst_mtime = obj->yst_mtime;
1238 #endif
1239                 retVal = 0;
1240         }
1241         return retVal;
1242 }
1243
1244 static int yaffsfs_DoStatOrLStat(const YCHAR *path, struct yaffs_stat *buf,int doLStat)
1245 {
1246         struct yaffs_obj *obj;
1247
1248         int retVal = -1;
1249
1250         yaffsfs_Lock();
1251
1252         obj = yaffsfs_FindObject(NULL,path,0,1);
1253
1254         if(!doLStat && obj)
1255                 obj = yaffsfs_FollowLink(obj,0);
1256
1257         if(obj)
1258                 retVal = yaffsfs_DoStat(obj,buf);
1259         else
1260                 /* todo error not found */
1261                 yaffsfs_SetError(-ENOENT);
1262
1263         yaffsfs_Unlock();
1264
1265         return retVal;
1266
1267 }
1268
1269 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1270 {
1271         return yaffsfs_DoStatOrLStat(path,buf,0);
1272 }
1273
1274 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1275 {
1276         return yaffsfs_DoStatOrLStat(path,buf,1);
1277 }
1278
1279 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1280 {
1281         struct yaffs_obj *obj;
1282
1283         int retVal = -1;
1284
1285         yaffsfs_Lock();
1286         obj = yaffsfs_GetHandleObject(fd);
1287
1288         if(obj)
1289                 retVal = yaffsfs_DoStat(obj,buf);
1290         else
1291                 /* bad handle */
1292                 yaffsfs_SetError(-EBADF);
1293
1294         yaffsfs_Unlock();
1295
1296         return retVal;
1297 }
1298
1299 #ifndef CONFIG_YAFFS_WINCE
1300 /* xattrib functions */
1301
1302
1303 static int yaffs_do_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags, int follow)
1304 {
1305         struct yaffs_obj *obj;
1306
1307         int retVal = -1;
1308
1309         yaffsfs_Lock();
1310
1311         obj = yaffsfs_FindObject(NULL,path,0,1);
1312
1313         if(follow)
1314                 obj = yaffsfs_FollowLink(obj,0);
1315
1316         if(obj) {
1317                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1318                 if(retVal< 0){
1319                         yaffsfs_SetError(retVal);
1320                         retVal = -1;
1321                 }
1322         } else
1323                 /* todo error not found */
1324                 yaffsfs_SetError(-ENOENT);
1325
1326         yaffsfs_Unlock();
1327
1328         return retVal;
1329
1330 }
1331
1332 int yaffs_setxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1333 {
1334         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1335 }
1336
1337 int yaffs_lsetxattr(const YCHAR *path, const char *name, const void *data, int size, int flags)
1338 {
1339         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1340 }
1341
1342
1343
1344 int yaffs_fsetxattr(int fd, const char *name, const void *data, int size, int flags)
1345 {
1346         struct yaffs_obj *obj;
1347
1348         int retVal = -1;
1349
1350         yaffsfs_Lock();
1351         obj = yaffsfs_GetHandleObject(fd);
1352
1353         if(obj) {
1354                 retVal = yaffs_set_xattrib(obj,name,data,size,flags);
1355                 if(retVal< 0){
1356                         yaffsfs_SetError(retVal);
1357                         retVal = -1;
1358                 }
1359         } else
1360                 /* bad handle */
1361                 yaffsfs_SetError(-EBADF);
1362
1363         yaffsfs_Unlock();
1364
1365         return retVal;
1366 }
1367
1368 static int yaffs_do_getxattr(const YCHAR *path, const char *name, void *data, int size, int follow)
1369 {
1370         struct yaffs_obj *obj;
1371
1372         int retVal = -1;
1373
1374         yaffsfs_Lock();
1375
1376         obj = yaffsfs_FindObject(NULL,path,0,1);
1377
1378         if(follow)
1379                 obj = yaffsfs_FollowLink(obj,0);
1380
1381         if(obj) {
1382                 retVal = yaffs_get_xattrib(obj,name,data,size);
1383                 if(retVal< 0){
1384                         yaffsfs_SetError(retVal);
1385                         retVal = -1;
1386                 }
1387         } else
1388                 /* todo error not found */
1389                 yaffsfs_SetError(-ENOENT);
1390
1391         yaffsfs_Unlock();
1392
1393         return retVal;
1394
1395 }
1396
1397 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1398 {
1399         return yaffs_do_getxattr( path, name, data, size, 1);
1400 }
1401 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1402 {
1403         return yaffs_do_getxattr( path, name, data, size, 0);
1404 }
1405
1406
1407
1408 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1409 {
1410         struct yaffs_obj *obj;
1411
1412         int retVal = -1;
1413
1414         yaffsfs_Lock();
1415         obj = yaffsfs_GetHandleObject(fd);
1416
1417         if(obj) {
1418                 retVal = yaffs_get_xattrib(obj,name,data,size);
1419                 if(retVal< 0){
1420                         yaffsfs_SetError(retVal);
1421                         retVal = -1;
1422                 }
1423         } else
1424                 /* bad handle */
1425                 yaffsfs_SetError(-EBADF);
1426
1427         yaffsfs_Unlock();
1428
1429         return retVal;
1430 }
1431
1432 static int yaffs_do_listxattr(const YCHAR *path, char *data, int size, int follow)
1433 {
1434         struct yaffs_obj *obj;
1435
1436         int retVal = -1;
1437
1438         yaffsfs_Lock();
1439
1440         obj = yaffsfs_FindObject(NULL,path,0,1);
1441
1442         if(follow)
1443                 obj = yaffsfs_FollowLink(obj,0);
1444
1445         if(obj) {
1446                 retVal = yaffs_list_xattrib(obj, data,size);
1447                 if(retVal< 0){
1448                         yaffsfs_SetError(retVal);
1449                         retVal = -1;
1450                 }
1451         } else
1452                 /* todo error not found */
1453                 yaffsfs_SetError(-ENOENT);
1454
1455         yaffsfs_Unlock();
1456
1457         return retVal;
1458
1459 }
1460
1461 int yaffs_listxattr(const YCHAR *path, char *data, int size)
1462 {
1463         return yaffs_do_listxattr(path, data, size, 1);
1464 }
1465
1466 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
1467 {
1468         return yaffs_do_listxattr(path, data, size, 0);
1469 }
1470
1471 int yaffs_flistxattr(int fd, char *data, int size)
1472 {
1473         struct yaffs_obj *obj;
1474
1475         int retVal = -1;
1476
1477         yaffsfs_Lock();
1478         obj = yaffsfs_GetHandleObject(fd);
1479
1480         if(obj) {
1481                 retVal = yaffs_list_xattrib(obj,data,size);
1482                 if(retVal< 0){
1483                         yaffsfs_SetError(retVal);
1484                         retVal = -1;
1485                 }
1486         } else
1487                 /* bad handle */
1488                 yaffsfs_SetError(-EBADF);
1489
1490         yaffsfs_Unlock();
1491
1492         return retVal;
1493 }
1494
1495 static int yaffs_do_removexattr(const YCHAR *path, const char *name, int follow)
1496 {
1497         struct yaffs_obj *obj;
1498
1499         int retVal = -1;
1500
1501         yaffsfs_Lock();
1502
1503         obj = yaffsfs_FindObject(NULL,path,0,1);
1504
1505         if(follow)
1506                 obj = yaffsfs_FollowLink(obj,0);
1507
1508         if(obj) {
1509                 retVal = yaffs_remove_xattrib(obj,name);
1510                 if(retVal< 0){
1511                         yaffsfs_SetError(retVal);
1512                         retVal = -1;
1513                 }
1514         } else
1515                 /* todo error not found */
1516                 yaffsfs_SetError(-ENOENT);
1517
1518         yaffsfs_Unlock();
1519
1520         return retVal;
1521
1522 }
1523
1524 int yaffs_removexattr(const YCHAR *path, const char *name)
1525 {
1526         return yaffs_do_removexattr(path, name, 1);
1527 }
1528
1529 int yaffs_lremovexattr(const YCHAR *path, const char *name)
1530 {
1531         return yaffs_do_removexattr(path, name, 0);
1532 }
1533
1534 int yaffs_fremovexattr(int fd, const char *name)
1535 {
1536         struct yaffs_obj *obj;
1537
1538         int retVal = -1;
1539
1540         yaffsfs_Lock();
1541         obj = yaffsfs_GetHandleObject(fd);
1542
1543         if(obj){
1544                 retVal = yaffs_remove_xattrib(obj,name);
1545                 if(retVal< 0){
1546                         yaffsfs_SetError(retVal);
1547                         retVal = -1;
1548                 }
1549         }else
1550                 /* bad handle */
1551                 yaffsfs_SetError(-EBADF);
1552
1553         yaffsfs_Unlock();
1554
1555         return retVal;
1556 }
1557 #endif
1558
1559 #ifdef CONFIG_YAFFS_WINCE
1560 int yaffs_get_wince_times(int fd, unsigned *wctime, unsigned *watime, unsigned *wmtime)
1561 {
1562         struct yaffs_obj *obj;
1563
1564         int retVal = -1;
1565
1566         yaffsfs_Lock();
1567         obj = yaffsfs_GetHandleObject(fd);
1568
1569         if(obj){
1570
1571                 if(wctime){
1572                         wctime[0] = obj->win_ctime[0];
1573                         wctime[1] = obj->win_ctime[1];
1574                 }
1575                 if(watime){
1576                         watime[0] = obj->win_atime[0];
1577                         watime[1] = obj->win_atime[1];
1578                 }
1579                 if(wmtime){
1580                         wmtime[0] = obj->win_mtime[0];
1581                         wmtime[1] = obj->win_mtime[1];
1582                 }
1583
1584
1585                 retVal = 0;
1586         } else
1587                 /*  bad handle */
1588                 yaffsfs_SetError(-EBADF);               
1589         
1590         yaffsfs_Unlock();
1591         
1592         return retVal;
1593 }
1594
1595
1596 int yaffs_set_wince_times(int fd, 
1597                                                   const unsigned *wctime, 
1598                                                   const unsigned *watime, 
1599                                                   const unsigned *wmtime)
1600 {
1601         struct yaffs_obj *obj;
1602         int result;
1603         int retVal = -1;
1604
1605         yaffsfs_Lock();
1606         obj = yaffsfs_GetHandleObject(fd);
1607
1608         if(obj){
1609
1610                 if(wctime){
1611                         obj->win_ctime[0] = wctime[0];
1612                         obj->win_ctime[1] = wctime[1];
1613                 }
1614                 if(watime){
1615                         obj->win_atime[0] = watime[0];
1616                         obj->win_atime[1] = watime[1];
1617                 }
1618                 if(wmtime){
1619                         obj->win_mtime[0] = wmtime[0];
1620                         obj->win_mtime[1] = wmtime[1];
1621                 }
1622
1623                 obj->dirty = 1;
1624                 result = yaffs_flush_file(obj,0,0);
1625                 retVal = 0;
1626         } else
1627                 /* bad handle */
1628                 yaffsfs_SetError(-EBADF);
1629
1630         yaffsfs_Unlock();
1631
1632         return retVal;
1633 }
1634
1635 #endif
1636
1637
1638 static int yaffsfs_DoChMod(struct yaffs_obj *obj,mode_t mode)
1639 {
1640         int result = -1;
1641
1642         if(obj)
1643                 obj = yaffs_get_equivalent_obj(obj);
1644
1645         if(obj) {
1646                 obj->yst_mode = mode;
1647                 obj->dirty = 1;
1648                 result = yaffs_flush_file(obj,0,0);
1649         }
1650
1651         return result == YAFFS_OK ? 0 : -1;
1652 }
1653
1654
1655 int yaffs_access(const YCHAR *path, int amode)
1656 {
1657         struct yaffs_obj *obj;
1658
1659         int retval = 0;
1660
1661         yaffsfs_Lock();
1662
1663         obj = yaffsfs_FindObject(NULL,path,0,1);
1664
1665         if(obj) {
1666                 int access_ok = 1;
1667
1668                 if((amode & R_OK) && !(obj->yst_mode & S_IREAD))
1669                         access_ok = 0;
1670                 if((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
1671                         access_ok = 0;
1672                 if((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
1673                         access_ok = 0;
1674
1675                 if(!access_ok) {
1676                         yaffsfs_SetError(-EACCES);
1677                         retval = -1;
1678                 }
1679         } else {
1680                 /* todo error not found */
1681                 yaffsfs_SetError(-ENOENT);
1682                 retval = -1;
1683         }
1684
1685         yaffsfs_Unlock();
1686
1687         return retval;
1688
1689 }
1690
1691
1692 int yaffs_chmod(const YCHAR *path, mode_t mode)
1693 {
1694         struct yaffs_obj *obj;
1695
1696         int retVal = -1;
1697
1698         yaffsfs_Lock();
1699
1700         obj = yaffsfs_FindObject(NULL,path,0,1);
1701
1702         if(!obj)
1703                 yaffsfs_SetError(-ENOENT);
1704         else if(obj->my_dev->read_only)
1705                 yaffsfs_SetError(-EINVAL);
1706         else
1707                 retVal = yaffsfs_DoChMod(obj,mode);
1708
1709         yaffsfs_Unlock();
1710
1711         return retVal;
1712
1713 }
1714
1715
1716 int yaffs_fchmod(int fd, mode_t mode)
1717 {
1718         struct yaffs_obj *obj;
1719
1720         int retVal = -1;
1721
1722         yaffsfs_Lock();
1723         obj = yaffsfs_GetHandleObject(fd);
1724
1725         if(!obj)
1726                 yaffsfs_SetError(-ENOENT);
1727         else if(obj->my_dev->read_only)
1728                 yaffsfs_SetError(-EINVAL);
1729         else
1730                 retVal = yaffsfs_DoChMod(obj,mode);
1731
1732         yaffsfs_Unlock();
1733
1734         return retVal;
1735 }
1736
1737
1738 int yaffs_mkdir(const YCHAR *path, mode_t mode)
1739 {
1740         struct yaffs_obj *parent = NULL;
1741         struct yaffs_obj *dir = NULL;
1742         YCHAR *name;
1743         YCHAR *use_path = NULL;
1744         int path_length = 0;
1745         int retVal= -1;
1746         int i;
1747
1748
1749         /*
1750          * We don't have a definition for max path length.
1751          * We will use 3 * max name length instead.
1752          */
1753         
1754         path_length = strnlen(path,(YAFFS_MAX_NAME_LENGTH+1)*3 +1);
1755
1756         /* If the last character is a path divider, then we need to
1757          * trim it back so that the name look-up works properly.
1758          * eg. /foo/new_dir/ -> /foo/newdir
1759          * Curveball: Need to handle multiple path dividers:
1760          * eg. /foof/sdfse///// -> /foo/sdfse
1761          */
1762         if(path_length > 0 && 
1763                 yaffsfs_IsPathDivider(path[path_length-1])){
1764                 use_path = YMALLOC(path_length + 1);
1765                 if(!use_path){
1766                         yaffsfs_SetError(-ENOMEM);
1767                         return -1;
1768                 }
1769                 strcpy(use_path, path);
1770                 for(i = path_length-1;
1771                         i >= 0 && yaffsfs_IsPathDivider(use_path[i]);
1772                         i--)
1773                         use_path[i] = (YCHAR) 0;
1774                 path = use_path;
1775         }
1776         
1777         yaffsfs_Lock();
1778         parent = yaffsfs_FindDirectory(NULL,path,&name,0);
1779         if(parent && yaffs_strnlen(name,5) == 0){
1780                 /* Trying to make the root itself */
1781                 yaffsfs_SetError(-EEXIST);
1782         } else if(parent && parent->my_dev->read_only){
1783                 yaffsfs_SetError(-EINVAL);
1784         } else {
1785                 if(parent)
1786                         dir = yaffs_create_dir(parent,name,mode,0,0);
1787                 if(dir)
1788                         retVal = 0;
1789                 else {
1790                         if(!parent)
1791                                 yaffsfs_SetError(-ENOENT); /* missing path */
1792                         else if (yaffs_find_by_name(parent,name))
1793                                 yaffsfs_SetError(-EEXIST); /* the name already exists */
1794                         else
1795                                 yaffsfs_SetError(-ENOSPC); /* just assume no space */
1796                         retVal = -1;
1797                 }
1798         }
1799
1800         yaffsfs_Unlock();
1801
1802         if(use_path)
1803                 YFREE(use_path);
1804
1805         return retVal;
1806 }
1807
1808 void * yaffs_getdev(const YCHAR *path)
1809 {
1810         struct yaffs_dev *dev=NULL;
1811         YCHAR *dummy;
1812         dev = yaffsfs_FindDevice(path,&dummy);
1813         return (void *)dev;
1814 }
1815
1816 int yaffs_mount2(const YCHAR *path,int read_only)
1817 {
1818         int retVal=-1;
1819         int result=YAFFS_FAIL;
1820         struct yaffs_dev *dev=NULL;
1821         YCHAR *dummy;
1822
1823         T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Mounting %s" TENDSTR),path));
1824
1825         yaffsfs_Lock();
1826
1827         yaffsfs_InitHandles();
1828
1829         dev = yaffsfs_FindDevice(path,&dummy);
1830         if(dev){
1831                 if(!dev->is_mounted){
1832                         dev->read_only = read_only ? 1 : 0;
1833                         result = yaffs_guts_initialise(dev);
1834                         if(result == YAFFS_FAIL)
1835                                 /* todo error - mount failed */
1836                                 yaffsfs_SetError(-ENOMEM);
1837                         retVal = result ? 0 : -1;
1838
1839                 }
1840                 else
1841                         /* todo error - already mounted. */
1842                         yaffsfs_SetError(-EBUSY);
1843         } else
1844                 /* todo error - no device */
1845                 yaffsfs_SetError(-ENODEV);
1846
1847         yaffsfs_Unlock();
1848         return retVal;
1849
1850 }
1851
1852 int yaffs_mount(const YCHAR *path)
1853 {
1854         return yaffs_mount2(path,0);
1855 }
1856
1857 int yaffs_sync(const YCHAR *path)
1858 {
1859         int retVal=-1;
1860         struct yaffs_dev *dev=NULL;
1861         YCHAR *dummy;
1862         
1863         yaffsfs_Lock();
1864         dev = yaffsfs_FindDevice(path,&dummy);
1865         if(dev){
1866                 if(dev->is_mounted){
1867                         
1868                         yaffs_flush_whole_cache(dev);
1869                         yaffs_checkpoint_save(dev);
1870                         retVal = 0;
1871                         
1872                 } else
1873                         /* todo error - not mounted. */
1874                         yaffsfs_SetError(-EINVAL);
1875                         
1876         }else
1877                 /* todo error - no device */
1878                 yaffsfs_SetError(-ENODEV);
1879
1880         yaffsfs_Unlock();
1881         return retVal;  
1882 }
1883
1884
1885 int yaffs_remount(const YCHAR *path, int force, int read_only)
1886 {
1887         int retVal=-1;
1888         struct yaffs_dev *dev=NULL;
1889         YCHAR *dummy;
1890
1891         yaffsfs_Lock();
1892         dev = yaffsfs_FindDevice(path,&dummy);
1893         if(dev){
1894                 if(dev->is_mounted){
1895                         int i;
1896                         int inUse;
1897
1898                         yaffs_flush_whole_cache(dev);
1899
1900                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse && !force; i++){
1901                                 if(yaffsfs_handle[i].useCount>0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1902                                         inUse = 1; /* the device is in use, can't unmount */
1903                         }
1904
1905                         if(!inUse || force){
1906                                 if(read_only)
1907                                         yaffs_checkpoint_save(dev);
1908                                 dev->read_only =  read_only ? 1 : 0;
1909                                 retVal = 0;
1910                         } else
1911                                 yaffsfs_SetError(-EBUSY);
1912
1913                 } else
1914                         yaffsfs_SetError(-EINVAL);
1915
1916         }
1917         else
1918                 yaffsfs_SetError(-ENODEV);
1919
1920         yaffsfs_Unlock();
1921         return retVal;
1922
1923 }
1924
1925 int yaffs_unmount2(const YCHAR *path, int force)
1926 {
1927         int retVal=-1;
1928         struct yaffs_dev *dev=NULL;
1929         YCHAR *dummy;
1930
1931         yaffsfs_Lock();
1932         dev = yaffsfs_FindDevice(path,&dummy);
1933         if(dev){
1934                 if(dev->is_mounted){
1935                         int i;
1936                         int inUse;
1937
1938                         yaffs_flush_whole_cache(dev);
1939                         yaffs_checkpoint_save(dev);
1940
1941                         for(i = inUse = 0; i < YAFFSFS_N_HANDLES && !inUse; i++){
1942                                 if(yaffsfs_handle[i].useCount > 0 && yaffsfs_inode[yaffsfs_handle[i].inodeId].iObj->my_dev == dev)
1943                                         inUse = 1; /* the device is in use, can't unmount */
1944                         }
1945
1946                         if(!inUse || force){
1947                                 yaffs_deinitialise(dev);
1948
1949                                 retVal = 0;
1950                         } else
1951                                 /* todo error can't unmount as files are open */
1952                                 yaffsfs_SetError(-EBUSY);
1953
1954                 } else
1955                         /* todo error - not mounted. */
1956                         yaffsfs_SetError(-EINVAL);
1957
1958         }
1959         else
1960                 /* todo error - no device */
1961                 yaffsfs_SetError(-ENODEV);
1962
1963         yaffsfs_Unlock();
1964         return retVal;
1965
1966 }
1967
1968 int yaffs_unmount(const YCHAR *path)
1969 {
1970         return yaffs_unmount2(path,0);
1971 }
1972
1973 loff_t yaffs_freespace(const YCHAR *path)
1974 {
1975         loff_t retVal=-1;
1976         struct yaffs_dev *dev=NULL;
1977         YCHAR *dummy;
1978
1979         yaffsfs_Lock();
1980         dev = yaffsfs_FindDevice(path,&dummy);
1981         if(dev  && dev->is_mounted){
1982                 retVal = yaffs_get_n_free_chunks(dev);
1983                 retVal *= dev->data_bytes_per_chunk;
1984
1985         } else
1986                 yaffsfs_SetError(-EINVAL);
1987
1988         yaffsfs_Unlock();
1989         return retVal;
1990 }
1991
1992 loff_t yaffs_totalspace(const YCHAR *path)
1993 {
1994         loff_t retVal=-1;
1995         struct yaffs_dev *dev=NULL;
1996         YCHAR *dummy;
1997
1998         yaffsfs_Lock();
1999         dev = yaffsfs_FindDevice(path,&dummy);
2000         if(dev  && dev->is_mounted){
2001                 retVal = (dev->param.end_block - dev->param.start_block + 1) - dev->param.n_reserved_blocks;
2002                 retVal *= dev->param.chunks_per_block;
2003                 retVal *= dev->data_bytes_per_chunk;
2004
2005         } else
2006                 yaffsfs_SetError(-EINVAL);
2007
2008         yaffsfs_Unlock();
2009         return retVal;
2010 }
2011
2012 int yaffs_inodecount(const YCHAR *path)
2013 {
2014         loff_t retVal= -1;
2015         struct yaffs_dev *dev=NULL;
2016         YCHAR *dummy;
2017
2018         yaffsfs_Lock();
2019         dev = yaffsfs_FindDevice(path,&dummy);
2020         if(dev  && dev->is_mounted) {
2021            int n_obj = dev->n_obj;
2022            if(n_obj > dev->n_hardlinks)
2023                 retVal = n_obj - dev->n_hardlinks;
2024         }
2025         
2026         if(retVal < 0)
2027                 yaffsfs_SetError(-EINVAL);
2028         
2029         yaffsfs_Unlock();
2030         return retVal;  
2031 }
2032
2033
2034 void yaffs_add_device(struct yaffs_dev *dev)
2035 {
2036         dev->is_mounted = 0;
2037         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2038
2039         if(!dev->dev_list.next)
2040                 INIT_LIST_HEAD(&dev->dev_list);
2041
2042         list_add(&dev->dev_list,&yaffsfs_deviceList);
2043 }
2044
2045 void yaffs_remove_device(struct yaffs_dev *dev)
2046 {
2047         list_del_init(&dev->dev_list);
2048 }
2049
2050
2051
2052
2053 /* Directory search stuff. */
2054
2055 /*
2056  * Directory search context
2057  *
2058  * NB this is an opaque structure.
2059  */
2060
2061
2062 typedef struct
2063 {
2064         u32 magic;
2065         yaffs_dirent de;                /* directory entry being used by this dsc */
2066         YCHAR name[NAME_MAX+1];         /* name of directory being searched */
2067         struct yaffs_obj *dirObj;           /* ptr to directory being searched */
2068         struct yaffs_obj *nextReturn;       /* obj to be returned by next readddir */
2069         int offset;
2070         struct list_head others;       
2071 } yaffsfs_DirectorySearchContext;
2072
2073
2074
2075 static struct list_head search_contexts;
2076
2077
2078 static void yaffsfs_SetDirRewound(yaffsfs_DirectorySearchContext *dsc)
2079 {
2080         if(dsc &&
2081            dsc->dirObj &&
2082            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2083
2084            dsc->offset = 0;
2085
2086            if( list_empty(&dsc->dirObj->variant.dir_variant.children))
2087                 dsc->nextReturn = NULL;
2088            else
2089                 dsc->nextReturn = list_entry(dsc->dirObj->variant.dir_variant.children.next,
2090                                                 struct yaffs_obj,siblings);
2091         } else {
2092                 /* Hey someone isn't playing nice! */
2093         }
2094 }
2095
2096 static void yaffsfs_DirAdvance(yaffsfs_DirectorySearchContext *dsc)
2097 {
2098         if(dsc &&
2099            dsc->dirObj &&
2100            dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2101
2102            if( dsc->nextReturn == NULL ||
2103                list_empty(&dsc->dirObj->variant.dir_variant.children))
2104                 dsc->nextReturn = NULL;
2105            else {
2106                    struct list_head *next = dsc->nextReturn->siblings.next;
2107
2108                    if( next == &dsc->dirObj->variant.dir_variant.children)
2109                         dsc->nextReturn = NULL; /* end of list */
2110                    else
2111                         dsc->nextReturn = list_entry(next,struct yaffs_obj,siblings);
2112            }
2113         } else {
2114                 /* Hey someone isn't playing nice! */
2115         }
2116 }
2117
2118 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2119 {
2120
2121         struct list_head *i;
2122         yaffsfs_DirectorySearchContext *dsc;
2123
2124         /* if search contexts not initilised then skip */
2125         if(!search_contexts.next)
2126                 return;
2127
2128         /* Iterate through the directory search contexts.
2129          * If any are the one being removed, then advance the dsc to
2130          * the next one to prevent a hanging ptr.
2131          */
2132          list_for_each(i, &search_contexts) {
2133                 if (i) {
2134                         dsc = list_entry(i, yaffsfs_DirectorySearchContext,others);
2135                         if(dsc->nextReturn == obj)
2136                                 yaffsfs_DirAdvance(dsc);
2137                 }
2138         }
2139
2140 }
2141
2142 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2143 {
2144         yaffs_DIR *dir = NULL;
2145         struct yaffs_obj *obj = NULL;
2146         yaffsfs_DirectorySearchContext *dsc = NULL;
2147
2148         yaffsfs_Lock();
2149
2150         obj = yaffsfs_FindObject(NULL,dirname,0,1);
2151
2152         if(obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY){
2153
2154                 dsc = YMALLOC(sizeof(yaffsfs_DirectorySearchContext));
2155                 dir = (yaffs_DIR *)dsc;
2156
2157                 if(dsc){
2158                         memset(dsc,0,sizeof(yaffsfs_DirectorySearchContext));
2159                         dsc->magic = YAFFS_MAGIC;
2160                         dsc->dirObj = obj;
2161                         yaffs_strncpy(dsc->name,dirname,NAME_MAX);
2162                         INIT_LIST_HEAD(&dsc->others);
2163
2164                         if(!search_contexts.next)
2165                                 INIT_LIST_HEAD(&search_contexts);
2166
2167                         list_add(&dsc->others,&search_contexts);       
2168                         yaffsfs_SetDirRewound(dsc);
2169                 }
2170
2171         }
2172
2173         yaffsfs_Unlock();
2174
2175         return dir;
2176 }
2177
2178 struct yaffs_dirent *yaffs_readdir(yaffs_DIR *dirp)
2179 {
2180         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2181         struct yaffs_dirent *retVal = NULL;
2182
2183         yaffsfs_Lock();
2184
2185         if(dsc && dsc->magic == YAFFS_MAGIC){
2186                 yaffsfs_SetError(0);
2187                 if(dsc->nextReturn){
2188                         dsc->de.d_ino = yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2189                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2190                         dsc->de.d_off = dsc->offset++;
2191                         yaffs_get_obj_name(dsc->nextReturn,dsc->de.d_name,NAME_MAX);
2192                         if(yaffs_strnlen(dsc->de.d_name,NAME_MAX+1) == 0)
2193                         {
2194                                 /* this should not happen! */
2195                                 yaffs_strcpy(dsc->de.d_name,_Y("zz"));
2196                         }
2197                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2198                         retVal = &dsc->de;
2199                         yaffsfs_DirAdvance(dsc);
2200                 } else
2201                         retVal = NULL;
2202         } else
2203                 yaffsfs_SetError(-EBADF);
2204
2205         yaffsfs_Unlock();
2206
2207         return retVal;
2208
2209 }
2210
2211
2212 void yaffs_rewinddir(yaffs_DIR *dirp)
2213 {
2214         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2215
2216         yaffsfs_Lock();
2217
2218         yaffsfs_SetDirRewound(dsc);
2219
2220         yaffsfs_Unlock();
2221 }
2222
2223
2224 int yaffs_closedir(yaffs_DIR *dirp)
2225 {
2226         yaffsfs_DirectorySearchContext *dsc = (yaffsfs_DirectorySearchContext *)dirp;
2227
2228         yaffsfs_Lock();
2229         dsc->magic = 0;
2230         list_del(&dsc->others); /* unhook from list */
2231         YFREE(dsc);
2232         yaffsfs_Unlock();
2233         return 0;
2234 }
2235
2236 /* End of directory stuff */
2237
2238
2239 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
2240 {
2241         struct yaffs_obj *parent = NULL;
2242         struct yaffs_obj *obj;
2243         YCHAR *name;
2244         int retVal= -1;
2245         int mode = 0; /* ignore for now */
2246
2247         yaffsfs_Lock();
2248         parent = yaffsfs_FindDirectory(NULL,newpath,&name,0);
2249         if(parent && parent->my_dev->read_only)
2250                 yaffsfs_SetError(-EINVAL);
2251         else if(parent){
2252                 obj = yaffs_create_symlink(parent,name,mode,0,0,oldpath);
2253                 if(obj)
2254                         retVal = 0;
2255                 else{
2256                         yaffsfs_SetError(-ENOSPC); /* just assume no space for now */
2257                         retVal = -1;
2258                 }
2259         } else {
2260                 yaffsfs_SetError(-EINVAL);
2261                 retVal = -1;
2262         }
2263
2264         yaffsfs_Unlock();
2265
2266         return retVal;
2267
2268 }
2269
2270 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
2271 {
2272         struct yaffs_obj *obj = NULL;
2273         int retVal;
2274
2275
2276         yaffsfs_Lock();
2277
2278         obj = yaffsfs_FindObject(NULL,path,0,1);
2279
2280         if(!obj) {
2281                 yaffsfs_SetError(-ENOENT);
2282                 retVal = -1;
2283         } else if(obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK) {
2284                 yaffsfs_SetError(-EINVAL);
2285                 retVal = -1;
2286         } else {
2287                 YCHAR *alias = obj->variant.symlink_variant.alias;
2288                 memset(buf,0,bufsiz);
2289                 yaffs_strncpy(buf,alias,bufsiz - 1);
2290                 retVal = 0;
2291         }
2292         yaffsfs_Unlock();
2293         return retVal;
2294 }
2295
2296 int yaffs_link(const YCHAR *oldpath, const YCHAR *newpath)
2297 {
2298         /* Creates a link called newpath to existing oldpath */
2299         struct yaffs_obj *obj = NULL;
2300         struct yaffs_obj *target = NULL;
2301         int retVal = 0;
2302         int new_nameLength = 0;
2303
2304
2305         yaffsfs_Lock();
2306
2307         obj = yaffsfs_FindObject(NULL,oldpath,0,1);
2308         target = yaffsfs_FindObject(NULL,newpath,0,0);
2309
2310         if(!obj) {
2311                 yaffsfs_SetError(-ENOENT);
2312                 retVal = -1;
2313         } else if(obj->my_dev->read_only){
2314                 yaffsfs_SetError(-EINVAL);
2315                 retVal= -1;
2316         } else if(target) {
2317                 yaffsfs_SetError(-EEXIST);
2318                 retVal = -1;
2319         } else {
2320                 struct yaffs_obj *newdir = NULL;
2321                 struct yaffs_obj *link = NULL;
2322
2323                 YCHAR *newname;
2324
2325                 newdir = yaffsfs_FindDirectory(NULL,newpath,&newname,0);
2326
2327                 if(!newdir){
2328                         yaffsfs_SetError(-ENOTDIR);
2329                         retVal = -1;
2330                 }else if(newdir->my_dev != obj->my_dev){
2331                         yaffsfs_SetError(-EXDEV);
2332                         retVal = -1;
2333                 }
2334                 
2335                 new_nameLength = yaffs_strnlen(newname,YAFFS_MAX_NAME_LENGTH+1);
2336                 
2337                 if(new_nameLength == 0){
2338                         yaffsfs_SetError(-ENOENT);
2339                         retVal = -1;
2340                 } else if (new_nameLength > YAFFS_MAX_NAME_LENGTH){
2341                         yaffsfs_SetError(-ENAMETOOLONG);
2342                         retVal = -1;
2343                 }
2344                 
2345                 if(retVal == 0) {
2346                         link = yaffs_link_obj(newdir,newname,obj);
2347                         if(link)
2348                                 retVal = 0;
2349                         else{
2350                                 yaffsfs_SetError(-ENOSPC);
2351                                 retVal = -1;
2352                         }
2353
2354                 }
2355         }
2356         yaffsfs_Unlock();
2357
2358         return retVal;
2359 }
2360
2361 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
2362 {
2363         return -1;
2364 }
2365
2366
2367
2368 /*
2369  * yaffs_n_handles()
2370  * Returns number of handles attached to the object
2371  */
2372 int yaffs_n_handles(const YCHAR *path)
2373 {
2374         struct yaffs_obj *obj;
2375
2376         obj = yaffsfs_FindObject(NULL,path,0,1);
2377
2378         return yaffsfs_CountHandles(obj);
2379 }
2380
2381 int yaffs_get_error(void)
2382 {
2383         return yaffsfs_GetLastError();
2384 }
2385
2386 int yaffs_dump_dev(const YCHAR *path)
2387 {
2388 #if 0
2389         YCHAR *rest;
2390
2391         struct yaffs_obj *obj = yaffsfs_FindRoot(path,&rest);
2392
2393         if(obj){
2394                 struct yaffs_dev *dev = obj->my_dev;
2395
2396                 printf("\n"
2397                            "n_page_writes.......... %d\n"
2398                            "n_page_reads........... %d\n"
2399                            "n_erasures....... %d\n"
2400                            "n_gc_copies............ %d\n"
2401                            "garbageCollections... %d\n"
2402                            "passiveGarbageColl'ns %d\n"
2403                            "\n",
2404                                 dev->n_page_writes,
2405                                 dev->n_page_reads,
2406                                 dev->n_erasures,
2407                                 dev->n_gc_copies,
2408                                 dev->garbageCollections,
2409                                 dev->passiveGarbageCollections
2410                 );
2411
2412         }
2413
2414 #endif
2415         return 0;
2416 }
2417