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