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