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