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