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