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