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