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