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