yaffs Moving timothy_tests and updating the makefiles to work with the new test-framework
[yaffs2.git] / direct / yaffsfs.c
1 /*
2  * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3  *
4  * Copyright (C) 2002-2011 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"
21
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
23
24 #ifndef NULL
25 #define NULL ((void *)0)
26 #endif
27
28 /* YAFFSFS_RW_SIZE must be a power of 2 */
29 #define YAFFSFS_RW_SHIFT (13)
30 #define YAFFSFS_RW_SIZE  (1<<YAFFSFS_RW_SHIFT)
31
32 /* Some forward references */
33 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
34                                             const YCHAR *path,
35                                             int symDepth, int getEquiv,
36                                             struct yaffs_obj **dirOut,
37                                             int *notDir, int *loop);
38
39 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
40
41 unsigned int yaffs_wr_attempts;
42
43 /*
44  * Handle management.
45  * There are open inodes in struct yaffsfs_Inode.
46  * There are open file descriptors in yaffsfs_FileDes.
47  * There are open handles in yaffsfs_FileDes.
48  *
49  * Things are structured this way to be like the Linux VFS model
50  * so that interactions with the yaffs guts calls are similar.
51  * That means more common code paths and less special code.
52  * That means better testing etc.
53  *
54  * We have 3 layers because:
55  * A handle is different than an fd because you can use dup()
56  * to create a new handle that accesses the *same* fd. The two
57  * handles will use the same offset (part of the fd). We only close
58  * down the fd when there are no more handles accessing it.
59  *
60  * More than one fd can currently access one file, but each fd
61  * has its own permsiions and offset.
62  */
63
64 struct yaffsfs_Inode {
65         int count;              /* Number of handles accessing this inode */
66         struct yaffs_obj *iObj;
67 };
68
69 struct yaffsfs_FileDes {
70         u8 reading:1;
71         u8 writing:1;
72         u8 append:1;
73         u8 shareRead:1;
74         u8 shareWrite:1;
75         int inodeId:12;         /* Index to corresponding yaffsfs_Inode */
76         int handleCount:10;     /* Number of handles for this fd */
77         Y_LOFF_T position;      /* current position in file */
78 };
79
80 struct yaffsfs_Handle {
81         short int fdId;
82         short int useCount;
83 };
84
85
86 struct yaffsfs_DirSearchContxt {
87         struct yaffs_dirent de; /* directory entry */
88         YCHAR name[NAME_MAX + 1];       /* name of directory being searched */
89         struct yaffs_obj *dirObj;       /* ptr to directory being searched */
90         struct yaffs_obj *nextReturn;   /* obj  returned by next readddir */
91         struct list_head others;
92         int offset:20;
93         unsigned inUse:1;
94 };
95
96 static struct yaffsfs_DirSearchContxt yaffsfs_dsc[YAFFSFS_N_DSC];
97 static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
98 static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
99 static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
100
101 static int yaffsfs_handlesInitialised;
102
103 unsigned yaffs_set_trace(unsigned tm)
104 {
105         yaffs_trace_mask = tm;
106         return yaffs_trace_mask;
107 }
108
109 unsigned yaffs_get_trace(void)
110 {
111         return yaffs_trace_mask;
112 }
113
114 /*
115  * yaffsfs_InitHandle
116  * Inilitalise handle management on start-up.
117  */
118
119 static void yaffsfs_InitHandles(void)
120 {
121         int i;
122         if (yaffsfs_handlesInitialised)
123                 return;
124
125         yaffsfs_handlesInitialised = 1;
126
127         memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
128         memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
129         memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
130         memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
131
132         for (i = 0; i < YAFFSFS_N_HANDLES; i++)
133                 yaffsfs_fd[i].inodeId = -1;
134         for (i = 0; i < YAFFSFS_N_HANDLES; i++)
135                 yaffsfs_handle[i].fdId = -1;
136 }
137
138 static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
139 {
140         if (h >= 0 && h < YAFFSFS_N_HANDLES)
141                 return &yaffsfs_handle[h];
142         return NULL;
143 }
144
145 static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
146 {
147         struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
148
149         if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
150                 return &yaffsfs_fd[h->fdId];
151
152         return NULL;
153 }
154
155 static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
156 {
157         struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
158
159         if (fd && fd->handleCount > 0 &&
160             fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
161                 return &yaffsfs_inode[fd->inodeId];
162
163         return NULL;
164 }
165
166 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
167 {
168         struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
169
170         if (in)
171                 return in->iObj;
172
173         return NULL;
174 }
175
176 /*
177  * yaffsfs_FindInodeIdForObject
178  * Find the inode entry for an object, if it exists.
179  */
180
181 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
182 {
183         int i;
184         int ret = -1;
185
186         if (obj)
187                 obj = yaffs_get_equivalent_obj(obj);
188
189         /* Look for it in open inode table */
190         for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
191                 if (yaffsfs_inode[i].iObj == obj)
192                         ret = i;
193         }
194         return ret;
195 }
196
197 /*
198  * yaffsfs_GetInodeIdForObject
199  * Grab an inode entry when opening a new inode.
200  */
201 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
202 {
203         int i;
204         int ret;
205         struct yaffsfs_Inode *in = NULL;
206
207         if (obj)
208                 obj = yaffs_get_equivalent_obj(obj);
209
210         ret = yaffsfs_FindInodeIdForObject(obj);
211
212         for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
213                 if (!yaffsfs_inode[i].iObj)
214                         ret = i;
215         }
216
217         if (ret >= 0) {
218                 in = &yaffsfs_inode[ret];
219                 if (!in->iObj)
220                         in->count = 0;
221                 in->iObj = obj;
222                 in->count++;
223         }
224
225         return ret;
226 }
227
228 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
229 {
230         int i = yaffsfs_FindInodeIdForObject(obj);
231
232         if (i >= 0)
233                 return yaffsfs_inode[i].count;
234         else
235                 return 0;
236 }
237
238 static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
239 {
240         struct yaffs_obj *obj;
241
242         obj = in->iObj;
243
244         if (obj->unlinked)
245                 yaffs_del_obj(obj);
246
247         obj->my_inode = NULL;
248         in->iObj = NULL;
249
250 }
251
252 static void yaffsfs_PutInode(int inodeId)
253 {
254         if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
255                 struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
256                 in->count--;
257                 if (in->count <= 0) {
258                         yaffsfs_ReleaseInode(in);
259                         in->count = 0;
260                 }
261         }
262 }
263
264 static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
265 {
266         int i;
267         struct yaffsfs_Handle *h;
268
269         for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
270                 h = &yaffsfs_handle[i];
271                 if (h->useCount < 1) {
272                         memset(h, 0, sizeof(struct yaffsfs_Handle));
273                         h->fdId = -1;
274                         h->useCount = 1;
275                         if (hptr)
276                                 *hptr = h;
277                         return i;
278                 }
279         }
280         return -1;
281 }
282
283 static int yaffsfs_NewHandleAndFileDes(void)
284 {
285         int i;
286         struct yaffsfs_FileDes *fd;
287         struct yaffsfs_Handle *h = NULL;
288         int handle = yaffsfs_NewHandle(&h);
289
290         if (handle < 0)
291                 return -1;
292
293         for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
294                 fd = &yaffsfs_fd[i];
295                 if (fd->handleCount < 1) {
296                         memset(fd, 0, sizeof(struct yaffsfs_FileDes));
297                         fd->inodeId = -1;
298                         fd->handleCount = 1;
299                         h->fdId = i;
300                         return handle;
301                 }
302         }
303
304         /* Dump the handle because we could not get a fd */
305         h->useCount = 0;
306         return -1;
307 }
308
309 /*
310  * yaffs_get_handle
311  * Increase use of handle when reading/writing a file
312  * Also gets the file descriptor.
313  */
314
315 static int yaffsfs_GetHandle(int handle)
316 {
317         struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
318
319         if (h && h->useCount > 0) {
320                 h->useCount++;
321                 return 0;
322         }
323         return -1;
324 }
325
326 /*
327  * yaffs_put_handle
328  * Let go of a handle when closing a file or aborting an open or
329  * ending a read or write.
330  */
331
332 static int yaffsfs_PutFileDes(int fdId)
333 {
334         struct yaffsfs_FileDes *fd;
335
336         if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
337                 fd = &yaffsfs_fd[fdId];
338                 fd->handleCount--;
339                 if (fd->handleCount < 1) {
340                         if (fd->inodeId >= 0) {
341                                 yaffsfs_PutInode(fd->inodeId);
342                                 fd->inodeId = -1;
343                         }
344                 }
345         }
346         return 0;
347 }
348
349 static int yaffsfs_PutHandle(int handle)
350 {
351         struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
352
353         if (h && h->useCount > 0) {
354                 h->useCount--;
355                 if (h->useCount < 1) {
356                         yaffsfs_PutFileDes(h->fdId);
357                         h->fdId = -1;
358                 }
359         }
360
361         return 0;
362 }
363
364 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
365 {
366         struct yaffsfs_FileDes *fd;
367         struct yaffsfs_Handle *h;
368         struct yaffs_obj *obj;
369         int i;
370         for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
371                 h = yaffsfs_HandleToPointer(i);
372                 fd = yaffsfs_HandleToFileDes(i);
373                 obj = yaffsfs_HandleToObject(i);
374                 if (h && h->useCount > 0) {
375                         h->useCount = 0;
376                         h->fdId = 0;
377                 }
378                 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
379                         fd->handleCount = 0;
380                         yaffsfs_PutInode(fd->inodeId);
381                         fd->inodeId = -1;
382                 }
383         }
384 }
385
386 /*
387  *  Stuff to handle names.
388  */
389 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
390
391 static int yaffs_toupper(YCHAR a)
392 {
393         if (a >= 'a' && a <= 'z')
394                 return (a - 'a') + 'A';
395         else
396                 return a;
397 }
398
399 static int yaffsfs_Match(YCHAR a, YCHAR b)
400 {
401         return (yaffs_toupper(a) == yaffs_toupper(b));
402 }
403 #else
404 static int yaffsfs_Match(YCHAR a, YCHAR b)
405 {
406         /* case sensitive */
407         return (a == b);
408 }
409 #endif
410
411 static int yaffsfs_IsPathDivider(YCHAR ch)
412 {
413         const YCHAR *str = YAFFS_PATH_DIVIDERS;
414
415         while (*str) {
416                 if (*str == ch)
417                         return 1;
418                 str++;
419         }
420
421         return 0;
422 }
423
424 static int yaffsfs_CheckNameLength(const char *name)
425 {
426         int retVal = 0;
427
428         int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
429
430         if (nameLength == 0) {
431                 yaffsfs_SetError(-ENOENT);
432                 retVal = -1;
433         } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
434                 yaffsfs_SetError(-ENAMETOOLONG);
435                 retVal = -1;
436         }
437
438         return retVal;
439 }
440
441 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
442 {
443         YCHAR *alt_path = NULL;
444         int path_length;
445         int i;
446
447         /*
448          * We don't have a definition for max path length.
449          * We will use 3 * max name length instead.
450          */
451         *ret_path = NULL;
452         path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
453
454         /* If the last character is a path divider, then we need to
455          * trim it back so that the name look-up works properly.
456          * eg. /foo/new_dir/ -> /foo/newdir
457          * Curveball: Need to handle multiple path dividers:
458          * eg. /foof/sdfse///// -> /foo/sdfse
459          */
460         if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
461                 alt_path = kmalloc(path_length + 1, 0);
462                 if (!alt_path)
463                         return -1;
464                 yaffs_strcpy(alt_path, path);
465                 for (i = path_length - 1;
466                      i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
467                         alt_path[i] = (YCHAR) 0;
468         }
469         *ret_path = alt_path;
470         return 0;
471 }
472
473 LIST_HEAD(yaffsfs_deviceList);
474
475 /*
476  * yaffsfs_FindDevice
477  * yaffsfs_FindRoot
478  * Scan the configuration list to find the device
479  * Curveballs: Should match paths that end in '/' too
480  * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
481  */
482 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
483                                             YCHAR **restOfPath)
484 {
485         struct list_head *cfg;
486         const YCHAR *leftOver;
487         const YCHAR *p;
488         struct yaffs_dev *retval = NULL;
489         struct yaffs_dev *dev = NULL;
490         int thisMatchLength;
491         int longestMatch = -1;
492         int matching;
493
494         /*
495          * Check all configs, choose the one that:
496          * 1) Actually matches a prefix (ie /a amd /abc will not match
497          * 2) Matches the longest.
498          */
499         list_for_each(cfg, &yaffsfs_deviceList) {
500                 dev = list_entry(cfg, struct yaffs_dev, dev_list);
501                 leftOver = path;
502                 p = dev->param.name;
503                 thisMatchLength = 0;
504                 matching = 1;
505
506                 if(!p)
507                         continue;
508
509                 while (matching && *p && *leftOver) {
510                         /* Skip over any /s */
511                         while (yaffsfs_IsPathDivider(*p))
512                                 p++;
513
514                         /* Skip over any /s */
515                         while (yaffsfs_IsPathDivider(*leftOver))
516                                 leftOver++;
517
518                         /* Now match the text part */
519                         while (matching &&
520                                *p && !yaffsfs_IsPathDivider(*p) &&
521                                *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
522                                 if (yaffsfs_Match(*p, *leftOver)) {
523                                         p++;
524                                         leftOver++;
525                                         thisMatchLength++;
526                                 } else {
527                                         matching = 0;
528                                 }
529                         }
530                 }
531
532                 /* Skip over any /s in leftOver */
533                 while (yaffsfs_IsPathDivider(*leftOver))
534                         leftOver++;
535
536                 /*Skip over any /s in p */
537                 while (yaffsfs_IsPathDivider(*p))
538                         p++;
539
540                 /* p should now be at the end of the string if fully matched */
541                 if (*p)
542                         matching = 0;
543
544                 if (matching && (thisMatchLength > longestMatch)) {
545                         /* Matched prefix */
546                         *restOfPath = (YCHAR *) leftOver;
547                         retval = dev;
548                         longestMatch = thisMatchLength;
549                 }
550
551         }
552         return retval;
553 }
554
555 static int yaffsfs_CheckPath(const YCHAR *path)
556 {
557         int n = 0;
558         int divs = 0;
559
560         while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
561                 if (yaffsfs_IsPathDivider(*path)) {
562                         n = 0;
563                         divs++;
564                 } else
565                         n++;
566                 path++;
567         }
568
569         return (*path) ? -1 : 0;
570 }
571
572 /* FindMountPoint only returns a dev entry if the path is a mount point */
573 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
574 {
575         struct yaffs_dev *dev;
576         YCHAR *restOfPath = NULL;
577
578         dev = yaffsfs_FindDevice(path, &restOfPath);
579         if (dev && restOfPath && *restOfPath)
580                 dev = NULL;
581         return dev;
582 }
583
584 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
585                                           YCHAR **restOfPath)
586 {
587         struct yaffs_dev *dev;
588
589         dev = yaffsfs_FindDevice(path, restOfPath);
590         if (dev && dev->is_mounted)
591                 return dev->root_dir;
592
593         return NULL;
594 }
595
596 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
597                                             int symDepth, int *loop)
598 {
599
600         if (obj)
601                 obj = yaffs_get_equivalent_obj(obj);
602
603         while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
604                 YCHAR *alias = obj->variant.symlink_variant.alias;
605
606                 if (yaffsfs_IsPathDivider(*alias))
607                         /* Starts with a /, need to scan from root up */
608                         obj = yaffsfs_FindObject(NULL, alias, symDepth++,
609                                                  1, NULL, NULL, loop);
610                 else
611                         /*
612                          * Relative to here so use the parent of the
613                          * symlink as a start
614                          */
615                         obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
616                                                  1, NULL, NULL, loop);
617         }
618         return obj;
619 }
620
621 /*
622  * yaffsfs_FindDirectory
623  * Parse a path to determine the directory and the name within the directory.
624  *
625  * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
626  */
627 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
628                                                  const YCHAR *path,
629                                                  YCHAR **name, int symDepth,
630                                                  int *notDir, int *loop)
631 {
632         struct yaffs_obj *dir;
633         YCHAR *restOfPath;
634         YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
635         int i;
636
637         if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
638                 if (loop)
639                         *loop = 1;
640                 return NULL;
641         }
642
643         if (startDir) {
644                 dir = startDir;
645                 restOfPath = (YCHAR *) path;
646         } else
647                 dir = yaffsfs_FindRoot(path, &restOfPath);
648
649         while (dir) {
650                 /*
651                  * parse off /.
652                  * curve ball: also throw away surplus '/'
653                  * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
654                  */
655                 while (yaffsfs_IsPathDivider(*restOfPath))
656                         restOfPath++;   /* get rid of '/' */
657
658                 *name = restOfPath;
659                 i = 0;
660
661                 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
662                         if (i < YAFFS_MAX_NAME_LENGTH) {
663                                 str[i] = *restOfPath;
664                                 str[i + 1] = '\0';
665                                 i++;
666                         }
667                         restOfPath++;
668                 }
669
670                 if (!*restOfPath)
671                         /* got to the end of the string */
672                         return dir;
673                 else {
674                         if (yaffs_strcmp(str, _Y(".")) == 0) {
675                                 /* Do nothing */
676                         } else if (yaffs_strcmp(str, _Y("..")) == 0) {
677                                 dir = dir->parent;
678                         } else {
679                                 dir = yaffs_find_by_name(dir, str);
680
681                                 dir = yaffsfs_FollowLink(dir, symDepth, loop);
682
683                                 if (dir && dir->variant_type !=
684                                     YAFFS_OBJECT_TYPE_DIRECTORY) {
685                                         if (notDir)
686                                                 *notDir = 1;
687                                         dir = NULL;
688                                 }
689
690                         }
691                 }
692         }
693         /* directory did not exist. */
694         return NULL;
695 }
696
697 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
698                                                const YCHAR *path,
699                                                YCHAR **name,
700                                                int symDepth,
701                                                int *notDir, int *loop)
702 {
703         return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
704                                                 loop);
705 }
706
707 /*
708  * yaffsfs_FindObject turns a path for an existing object into the object
709  */
710 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
711                                             const YCHAR *path, int symDepth,
712                                             int getEquiv,
713                                             struct yaffs_obj **dirOut,
714                                             int *notDir, int *loop)
715 {
716         struct yaffs_obj *dir;
717         struct yaffs_obj *obj;
718         YCHAR *name;
719
720         dir =
721             yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
722
723         if (dirOut)
724                 *dirOut = dir;
725
726         if (dir && *name)
727                 obj = yaffs_find_by_name(dir, name);
728         else
729                 obj = dir;
730
731         if (getEquiv)
732                 obj = yaffs_get_equivalent_obj(obj);
733
734         return obj;
735 }
736
737 /*************************************************************************
738  *      Start of yaffsfs visible functions.
739  *************************************************************************/
740
741 int yaffs_dup(int handle)
742 {
743         int newHandleNumber = -1;
744         struct yaffsfs_FileDes *existingFD = NULL;
745         struct yaffsfs_Handle *existingHandle = NULL;
746         struct yaffsfs_Handle *newHandle = NULL;
747
748         yaffsfs_Lock();
749         existingHandle = yaffsfs_HandleToPointer(handle);
750         existingFD = yaffsfs_HandleToFileDes(handle);
751         if (existingFD)
752                 newHandleNumber = yaffsfs_NewHandle(&newHandle);
753         if (newHandle) {
754                 newHandle->fdId = existingHandle->fdId;
755                 existingFD->handleCount++;
756         }
757
758         yaffsfs_Unlock();
759
760         if (!existingFD)
761                 yaffsfs_SetError(-EBADF);
762         else if (!newHandle)
763                 yaffsfs_SetError(-ENOMEM);
764
765         return newHandleNumber;
766
767 }
768
769 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
770 {
771         int current_objects = dev->n_obj - dev->n_deleted_files;
772
773         if (dev->param.max_objects && current_objects > dev->param.max_objects)
774                 return 1;
775         else
776                 return 0;
777 }
778
779 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
780 {
781         struct yaffs_obj *obj = NULL;
782         struct yaffs_obj *dir = NULL;
783         YCHAR *name;
784         int handle = -1;
785         struct yaffsfs_FileDes *fd = NULL;
786         int openDenied = 0;
787         int symDepth = 0;
788         int errorReported = 0;
789         int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
790         u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
791         u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
792         u8 sharedReadAllowed;
793         u8 sharedWriteAllowed;
794         u8 alreadyReading;
795         u8 alreadyWriting;
796         u8 readRequested;
797         u8 writeRequested;
798         int notDir = 0;
799         int loop = 0;
800
801         if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
802                 yaffsfs_SetError(-EFAULT);
803                 return -1;
804         }
805
806         if (yaffsfs_CheckPath(path) < 0) {
807                 yaffsfs_SetError(-ENAMETOOLONG);
808                 return -1;
809         }
810
811         /* O_EXCL only has meaning if O_CREAT is specified */
812         if (!(oflag & O_CREAT))
813                 oflag &= ~(O_EXCL);
814
815         /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
816         if ((oflag & O_CREAT) & (oflag & O_EXCL))
817                 oflag &= ~(O_TRUNC);
818
819         /* Todo: Are there any more flag combos to sanitise ? */
820
821         /* Figure out if reading or writing is requested */
822
823         readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
824         writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
825
826         yaffsfs_Lock();
827
828         handle = yaffsfs_NewHandleAndFileDes();
829
830         if (handle < 0) {
831                 yaffsfs_SetError(-ENFILE);
832                 errorReported = 1;
833         } else {
834
835                 fd = yaffsfs_HandleToFileDes(handle);
836
837                 /* try to find the exisiting object */
838                 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
839
840                 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
841
842                 if (obj &&
843                     obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
844                     obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
845                         obj = NULL;
846
847                 if (obj) {
848
849                         /* The file already exists or it might be a directory */
850
851                         /* A directory can't be opened as a file */
852                         if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
853                                 openDenied = 1;
854                                 yaffsfs_SetError(-EISDIR);
855                                 errorReported = 1;
856                         }
857
858                         /* Open should fail if O_CREAT and O_EXCL are specified
859                          * for a file that exists.
860                          */
861                         if (!errorReported &&
862                             (oflag & O_EXCL) && (oflag & O_CREAT)) {
863                                 openDenied = 1;
864                                 yaffsfs_SetError(-EEXIST);
865                                 errorReported = 1;
866                         }
867
868                         /* Check file permissions */
869                         if (readRequested && !(obj->yst_mode & S_IREAD))
870                                 openDenied = 1;
871
872                         if (writeRequested && !(obj->yst_mode & S_IWRITE))
873                                 openDenied = 1;
874
875                         if (!errorReported && writeRequested &&
876                             obj->my_dev->read_only) {
877                                 openDenied = 1;
878                                 yaffsfs_SetError(-EROFS);
879                                 errorReported = 1;
880                         }
881
882                         if (openDenied && !errorReported) {
883                                 yaffsfs_SetError(-EACCES);
884                                 errorReported = 1;
885                         }
886
887                         /* Check sharing of an existing object. */
888                         if (!openDenied) {
889                                 struct yaffsfs_FileDes *fdx;
890                                 int i;
891
892                                 sharedReadAllowed = 1;
893                                 sharedWriteAllowed = 1;
894                                 alreadyReading = 0;
895                                 alreadyWriting = 0;
896                                 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
897                                         fdx = &yaffsfs_fd[i];
898                                         if (fdx->handleCount > 0 &&
899                                             fdx->inodeId >= 0 &&
900                                             yaffsfs_inode[fdx->inodeId].iObj
901                                             == obj) {
902                                                 if (!fdx->shareRead)
903                                                         sharedReadAllowed = 0;
904                                                 if (!fdx->shareWrite)
905                                                         sharedWriteAllowed = 0;
906                                                 if (fdx->reading)
907                                                         alreadyReading = 1;
908                                                 if (fdx->writing)
909                                                         alreadyWriting = 1;
910                                         }
911                                 }
912
913                                 if ((!sharedReadAllowed && readRequested) ||
914                                     (!shareRead && alreadyReading) ||
915                                     (!sharedWriteAllowed && writeRequested) ||
916                                     (!shareWrite && alreadyWriting)) {
917                                         openDenied = 1;
918                                         yaffsfs_SetError(-EBUSY);
919                                         errorReported = 1;
920                                 }
921                         }
922
923                 }
924
925                 /* If we could not open an existing object, then let's see if
926                  * the directory exists. If not, error.
927                  */
928                 if (!obj && !errorReported) {
929                         dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
930                                                     &notDir, &loop);
931                         if (!dir && notDir) {
932                                 yaffsfs_SetError(-ENOTDIR);
933                                 errorReported = 1;
934                         } else if (loop) {
935                                 yaffsfs_SetError(-ELOOP);
936                                 errorReported = 1;
937                         } else if (!dir) {
938                                 yaffsfs_SetError(-ENOENT);
939                                 errorReported = 1;
940                         }
941                 }
942
943                 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
944                         /* Let's see if we can create this file */
945                         if (dir->my_dev->read_only) {
946                                 yaffsfs_SetError(-EROFS);
947                                 errorReported = 1;
948                         } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
949                                 yaffsfs_SetError(-ENFILE);
950                                 errorReported = 1;
951                         } else
952                                 obj = yaffs_create_file(dir, name, mode, 0, 0);
953
954                         if (!obj && !errorReported) {
955                                 yaffsfs_SetError(-ENOSPC);
956                                 errorReported = 1;
957                         }
958                 }
959
960                 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
961                         yaffsfs_SetError(-ENOENT);
962                         errorReported = 1;
963                 }
964
965                 if (obj && !openDenied) {
966                         int inodeId = yaffsfs_GetInodeIdForObject(obj);
967
968                         if (inodeId < 0) {
969                                 /*
970                                  * Todo: Fix any problem if inodes run out,
971                                  * That can't happen if the number of inode
972                                  * items >= number of handles.
973                                  */
974                         }
975
976                         fd->inodeId = inodeId;
977                         fd->reading = readRequested;
978                         fd->writing = writeRequested;
979                         fd->append = (oflag & O_APPEND) ? 1 : 0;
980                         fd->position = 0;
981                         fd->shareRead = shareRead;
982                         fd->shareWrite = shareWrite;
983
984                         /* Hook inode to object */
985                         obj->my_inode = (void *)&yaffsfs_inode[inodeId];
986
987                         if ((oflag & O_TRUNC) && fd->writing)
988                                 yaffs_resize_file(obj, 0);
989                 } else {
990                         yaffsfs_PutHandle(handle);
991                         if (!errorReported)
992                                 yaffsfs_SetError(0);    /* Problem */
993                         handle = -1;
994                 }
995         }
996
997         yaffsfs_Unlock();
998
999         return handle;
1000 }
1001
1002 int yaffs_open(const YCHAR *path, int oflag, int mode)
1003 {
1004         return yaffs_open_sharing(path, oflag, mode,
1005                                   YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1006 }
1007
1008 static int yaffs_Dofsync(int handle, int datasync)
1009 {
1010         int retVal = -1;
1011         struct yaffs_obj *obj;
1012
1013         yaffsfs_Lock();
1014
1015         obj = yaffsfs_HandleToObject(handle);
1016
1017         if (!obj)
1018                 yaffsfs_SetError(-EBADF);
1019         else if (obj->my_dev->read_only)
1020                 yaffsfs_SetError(-EROFS);
1021         else {
1022                 yaffs_flush_file(obj, 1, datasync);
1023                 retVal = 0;
1024         }
1025
1026         yaffsfs_Unlock();
1027
1028         return retVal;
1029 }
1030
1031 int yaffs_fsync(int handle)
1032 {
1033         return yaffs_Dofsync(handle, 0);
1034 }
1035
1036 int yaffs_flush(int handle)
1037 {
1038         return yaffs_fsync(handle);
1039 }
1040
1041 int yaffs_fdatasync(int handle)
1042 {
1043         return yaffs_Dofsync(handle, 1);
1044 }
1045
1046 int yaffs_close(int handle)
1047 {
1048         struct yaffsfs_Handle *h = NULL;
1049         struct yaffs_obj *obj = NULL;
1050         int retVal = -1;
1051
1052         yaffsfs_Lock();
1053
1054         h = yaffsfs_HandleToPointer(handle);
1055         obj = yaffsfs_HandleToObject(handle);
1056
1057         if (!h || !obj)
1058                 yaffsfs_SetError(-EBADF);
1059         else {
1060                 /* clean up */
1061                 yaffs_flush_file(obj, 1, 0);
1062                 yaffsfs_PutHandle(handle);
1063                 retVal = 0;
1064         }
1065
1066         yaffsfs_Unlock();
1067
1068         return retVal;
1069 }
1070
1071 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1072                     int isPread, Y_LOFF_T offset)
1073 {
1074         struct yaffsfs_FileDes *fd = NULL;
1075         struct yaffs_obj *obj = NULL;
1076         Y_LOFF_T pos = 0;
1077         Y_LOFF_T startPos = 0;
1078         Y_LOFF_T endPos = 0;
1079         int nRead = 0;
1080         int nToRead = 0;
1081         int totalRead = 0;
1082         Y_LOFF_T maxRead;
1083         u8 *buf = (u8 *) vbuf;
1084
1085         if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1086                 yaffsfs_SetError(-EFAULT);
1087                 return -1;
1088         }
1089
1090         yaffsfs_Lock();
1091         fd = yaffsfs_HandleToFileDes(handle);
1092         obj = yaffsfs_HandleToObject(handle);
1093
1094         if (!fd || !obj) {
1095                 /* bad handle */
1096                 yaffsfs_SetError(-EBADF);
1097                 totalRead = -1;
1098         } else if (!fd->reading) {
1099                 /* Not a reading handle */
1100                 yaffsfs_SetError(-EINVAL);
1101                 totalRead = -1;
1102         } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1103                 yaffsfs_SetError(-EINVAL);
1104                 totalRead = -1;
1105         } else {
1106                 if (isPread)
1107                         startPos = offset;
1108                 else
1109                         startPos = fd->position;
1110
1111                 pos = startPos;
1112
1113                 if (yaffs_get_obj_length(obj) > pos)
1114                         maxRead = yaffs_get_obj_length(obj) - pos;
1115                 else
1116                         maxRead = 0;
1117
1118                 if (nbyte > maxRead)
1119                         nbyte = maxRead;
1120
1121                 yaffsfs_GetHandle(handle);
1122
1123                 endPos = pos + nbyte;
1124
1125                 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1126                     nbyte > YAFFS_MAX_FILE_SIZE ||
1127                     endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1128                         totalRead = -1;
1129                         nbyte = 0;
1130                 }
1131
1132                 while (nbyte > 0) {
1133                         nToRead = YAFFSFS_RW_SIZE -
1134                             (pos & (YAFFSFS_RW_SIZE - 1));
1135                         if (nToRead > nbyte)
1136                                 nToRead = nbyte;
1137
1138                         /* Tricky bit...
1139                          * Need to reverify object in case the device was
1140                          * unmounted in another thread.
1141                          */
1142                         obj = yaffsfs_HandleToObject(handle);
1143                         if (!obj)
1144                                 nRead = 0;
1145                         else
1146                                 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1147
1148                         if (nRead > 0) {
1149                                 totalRead += nRead;
1150                                 pos += nRead;
1151                                 buf += nRead;
1152                         }
1153
1154                         if (nRead == nToRead)
1155                                 nbyte -= nRead;
1156                         else
1157                                 nbyte = 0;      /* no more to read */
1158
1159                         if (nbyte > 0) {
1160                                 yaffsfs_Unlock();
1161                                 yaffsfs_Lock();
1162                         }
1163
1164                 }
1165
1166                 yaffsfs_PutHandle(handle);
1167
1168                 if (!isPread) {
1169                         if (totalRead >= 0)
1170                                 fd->position = startPos + totalRead;
1171                         else
1172                                 yaffsfs_SetError(-EINVAL);
1173                 }
1174
1175         }
1176
1177         yaffsfs_Unlock();
1178
1179         return (totalRead >= 0) ? totalRead : -1;
1180
1181 }
1182
1183 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1184 {
1185         return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1186 }
1187
1188 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1189 {
1190         return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1191 }
1192
1193 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1194                      int isPwrite, Y_LOFF_T offset)
1195 {
1196         struct yaffsfs_FileDes *fd = NULL;
1197         struct yaffs_obj *obj = NULL;
1198         Y_LOFF_T pos = 0;
1199         Y_LOFF_T startPos = 0;
1200         Y_LOFF_T endPos;
1201         int nWritten = 0;
1202         int totalWritten = 0;
1203         int write_trhrough = 0;
1204         int nToWrite = 0;
1205         const u8 *buf = (const u8 *)vbuf;
1206
1207         if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1208                 yaffsfs_SetError(-EFAULT);
1209                 return -1;
1210         }
1211
1212         yaffsfs_Lock();
1213         fd = yaffsfs_HandleToFileDes(handle);
1214         obj = yaffsfs_HandleToObject(handle);
1215
1216         if (!fd || !obj) {
1217                 /* bad handle */
1218                 yaffsfs_SetError(-EBADF);
1219                 totalWritten = -1;
1220         } else if (!fd->writing) {
1221                 yaffsfs_SetError(-EINVAL);
1222                 totalWritten = -1;
1223         } else if (obj->my_dev->read_only) {
1224                 yaffsfs_SetError(-EROFS);
1225                 totalWritten = -1;
1226         } else {
1227                 if (fd->append)
1228                         startPos = yaffs_get_obj_length(obj);
1229                 else if (isPwrite)
1230                         startPos = offset;
1231                 else
1232                         startPos = fd->position;
1233
1234                 yaffsfs_GetHandle(handle);
1235                 pos = startPos;
1236                 endPos = pos + nbyte;
1237
1238                 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1239                     nbyte > YAFFS_MAX_FILE_SIZE ||
1240                     endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1241                         totalWritten = -1;
1242                         nbyte = 0;
1243                 }
1244
1245                 while (nbyte > 0) {
1246
1247                         nToWrite = YAFFSFS_RW_SIZE -
1248                             (pos & (YAFFSFS_RW_SIZE - 1));
1249                         if (nToWrite > nbyte)
1250                                 nToWrite = nbyte;
1251
1252                         /* Tricky bit...
1253                          * Need to reverify object in case the device was
1254                          * remounted or unmounted in another thread.
1255                          */
1256                         obj = yaffsfs_HandleToObject(handle);
1257                         if (!obj || obj->my_dev->read_only)
1258                                 nWritten = 0;
1259                         else
1260                                 nWritten =
1261                                     yaffs_wr_file(obj, buf, pos, nToWrite,
1262                                                   write_trhrough);
1263                         if (nWritten > 0) {
1264                                 totalWritten += nWritten;
1265                                 pos += nWritten;
1266                                 buf += nWritten;
1267                         }
1268
1269                         if (nWritten == nToWrite)
1270                                 nbyte -= nToWrite;
1271                         else
1272                                 nbyte = 0;
1273
1274                         if (nWritten < 1 && totalWritten < 1) {
1275                                 yaffsfs_SetError(-ENOSPC);
1276                                 totalWritten = -1;
1277                         }
1278
1279                         if (nbyte > 0) {
1280                                 yaffsfs_Unlock();
1281                                 yaffsfs_Lock();
1282                         }
1283                 }
1284
1285                 yaffsfs_PutHandle(handle);
1286
1287                 if (!isPwrite) {
1288                         if (totalWritten > 0)
1289                                 fd->position = startPos + totalWritten;
1290                         else
1291                                 yaffsfs_SetError(-EINVAL);
1292                 }
1293         }
1294
1295         yaffsfs_Unlock();
1296
1297         return (totalWritten >= 0) ? totalWritten : -1;
1298 }
1299
1300 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1301 {
1302         return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1303 }
1304
1305 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1306 {
1307         return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1308 }
1309
1310 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1311 {
1312         struct yaffs_obj *obj = NULL;
1313         struct yaffs_obj *dir = NULL;
1314         int result = YAFFS_FAIL;
1315         int notDir = 0;
1316         int loop = 0;
1317
1318         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1319                 yaffsfs_SetError(-EFAULT);
1320                 return -1;
1321         }
1322
1323         if (yaffsfs_CheckPath(path) < 0) {
1324                 yaffsfs_SetError(-ENAMETOOLONG);
1325                 return -1;
1326         }
1327
1328         yaffsfs_Lock();
1329
1330         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
1331         obj = yaffsfs_FollowLink(obj, 0, &loop);
1332
1333         if (!dir && notDir)
1334                 yaffsfs_SetError(-ENOTDIR);
1335         else if (loop)
1336                 yaffsfs_SetError(-ELOOP);
1337         else if (!dir || !obj)
1338                 yaffsfs_SetError(-ENOENT);
1339         else if (obj->my_dev->read_only)
1340                 yaffsfs_SetError(-EROFS);
1341         else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1342                 yaffsfs_SetError(-EISDIR);
1343         else if (obj->my_dev->read_only)
1344                 yaffsfs_SetError(-EROFS);
1345         else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1346                 yaffsfs_SetError(-EINVAL);
1347         else
1348                 result = yaffs_resize_file(obj, new_size);
1349
1350         yaffsfs_Unlock();
1351
1352         return (result) ? 0 : -1;
1353 }
1354
1355 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1356 {
1357         struct yaffsfs_FileDes *fd = NULL;
1358         struct yaffs_obj *obj = NULL;
1359         int result = 0;
1360
1361         yaffsfs_Lock();
1362         fd = yaffsfs_HandleToFileDes(handle);
1363         obj = yaffsfs_HandleToObject(handle);
1364
1365         if (!fd || !obj)
1366                 /* bad handle */
1367                 yaffsfs_SetError(-EBADF);
1368         else if (!fd->writing)
1369                 yaffsfs_SetError(-EINVAL);
1370         else if (obj->my_dev->read_only)
1371                 yaffsfs_SetError(-EROFS);
1372         else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1373                 yaffsfs_SetError(-EINVAL);
1374         else
1375                 /* resize the file */
1376                 result = yaffs_resize_file(obj, new_size);
1377         yaffsfs_Unlock();
1378
1379         return (result) ? 0 : -1;
1380
1381 }
1382
1383 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1384 {
1385         struct yaffsfs_FileDes *fd = NULL;
1386         struct yaffs_obj *obj = NULL;
1387         Y_LOFF_T pos = -1;
1388         Y_LOFF_T fSize = -1;
1389
1390         yaffsfs_Lock();
1391         fd = yaffsfs_HandleToFileDes(handle);
1392         obj = yaffsfs_HandleToObject(handle);
1393
1394         if (!fd || !obj)
1395                 yaffsfs_SetError(-EBADF);
1396         else if (offset > YAFFS_MAX_FILE_SIZE)
1397                 yaffsfs_SetError(-EINVAL);
1398         else {
1399                 if (whence == SEEK_SET) {
1400                         if (offset >= 0)
1401                                 pos = offset;
1402                 } else if (whence == SEEK_CUR) {
1403                         if ((fd->position + offset) >= 0)
1404                                 pos = (fd->position + offset);
1405                 } else if (whence == SEEK_END) {
1406                         fSize = yaffs_get_obj_length(obj);
1407                         if (fSize >= 0 && (fSize + offset) >= 0)
1408                                 pos = fSize + offset;
1409                 }
1410
1411                 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1412                         fd->position = pos;
1413                 else {
1414                         yaffsfs_SetError(-EINVAL);
1415                         pos = -1;
1416                 }
1417         }
1418
1419         yaffsfs_Unlock();
1420
1421         return pos;
1422 }
1423
1424 static int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
1425 {
1426         struct yaffs_obj *dir = NULL;
1427         struct yaffs_obj *obj = NULL;
1428         YCHAR *name;
1429         int result = YAFFS_FAIL;
1430         int notDir = 0;
1431         int loop = 0;
1432
1433         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1434                 yaffsfs_SetError(-EFAULT);
1435                 return -1;
1436         }
1437
1438         if (yaffsfs_CheckPath(path) < 0) {
1439                 yaffsfs_SetError(-ENAMETOOLONG);
1440                 return -1;
1441         }
1442
1443         yaffsfs_Lock();
1444
1445         obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
1446         dir = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
1447
1448         if (!dir && notDir)
1449                 yaffsfs_SetError(-ENOTDIR);
1450         else if (loop)
1451                 yaffsfs_SetError(-ELOOP);
1452         else if (!dir)
1453                 yaffsfs_SetError(-ENOENT);
1454         else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1455                 yaffsfs_SetError(-EINVAL);
1456         else if (!obj)
1457                 yaffsfs_SetError(-ENOENT);
1458         else if (obj->my_dev->read_only)
1459                 yaffsfs_SetError(-EROFS);
1460         else if (!isDirectory &&
1461                  obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1462                 yaffsfs_SetError(-EISDIR);
1463         else if (isDirectory &&
1464                  obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1465                 yaffsfs_SetError(-ENOTDIR);
1466         else if (isDirectory && obj == obj->my_dev->root_dir)
1467                 yaffsfs_SetError(-EBUSY);       /* Can't rmdir a root */
1468         else {
1469                 result = yaffs_unlinker(dir, name);
1470
1471                 if (result == YAFFS_FAIL && isDirectory)
1472                         yaffsfs_SetError(-ENOTEMPTY);
1473         }
1474
1475         yaffsfs_Unlock();
1476
1477         return (result == YAFFS_FAIL) ? -1 : 0;
1478 }
1479
1480 int yaffs_unlink(const YCHAR *path)
1481 {
1482         return yaffsfs_DoUnlink(path, 0);
1483 }
1484
1485 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1486 {
1487         struct yaffs_obj *olddir = NULL;
1488         struct yaffs_obj *newdir = NULL;
1489         struct yaffs_obj *obj = NULL;
1490         struct yaffs_obj *newobj = NULL;
1491         YCHAR *oldname;
1492         YCHAR *newname;
1493         int result = YAFFS_FAIL;
1494         int rename_allowed = 1;
1495         int notOldDir = 0;
1496         int notNewDir = 0;
1497         int oldLoop = 0;
1498         int newLoop = 0;
1499
1500         YCHAR *alt_newpath = NULL;
1501
1502         if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1503             yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1504                 yaffsfs_SetError(-EFAULT);
1505                 return -1;
1506         }
1507
1508         if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1509                 yaffsfs_SetError(-ENAMETOOLONG);
1510                 return -1;
1511         }
1512
1513         if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1514                 yaffsfs_SetError(-ENOMEM);
1515                 return -1;
1516         }
1517         if (alt_newpath)
1518                 newPath = alt_newpath;
1519
1520         yaffsfs_Lock();
1521
1522         olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
1523                                        &notOldDir, &oldLoop);
1524         newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
1525                                        &notNewDir, &newLoop);
1526         obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
1527         newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
1528
1529         /* If the object being renamed is a directory and the
1530          * path ended with a "/" then the olddir == obj.
1531          * We pass through NULL for the old name to tell the lower layers
1532          * to use olddir as the object.
1533          */
1534
1535         if (olddir == obj)
1536                 oldname = NULL;
1537
1538         if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1539                 yaffsfs_SetError(-ENOTDIR);
1540                 rename_allowed = 0;
1541         } else if (oldLoop || newLoop) {
1542                 yaffsfs_SetError(-ELOOP);
1543                 rename_allowed = 0;
1544         } else if (olddir && oldname &&
1545                         yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1546                 yaffsfs_SetError(-EINVAL);
1547                 rename_allowed = 0;
1548         } else if (!olddir || !newdir || !obj) {
1549                 yaffsfs_SetError(-ENOENT);
1550                 rename_allowed = 0;
1551         } else if (obj->my_dev->read_only) {
1552                 yaffsfs_SetError(-EROFS);
1553                 rename_allowed = 0;
1554         } else if (yaffs_is_non_empty_dir(newobj)) {
1555                 yaffsfs_SetError(-ENOTEMPTY);
1556                 rename_allowed = 0;
1557         } else if (olddir->my_dev != newdir->my_dev) {
1558                 /* Rename must be on same device */
1559                 yaffsfs_SetError(-EXDEV);
1560                 rename_allowed = 0;
1561         } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1562                 /*
1563                  * It is a directory, check that it is not being renamed to
1564                  * being its own decendent.
1565                  * Do this by tracing from the new directory back to the root,
1566                  * checking for obj
1567                  */
1568
1569                 struct yaffs_obj *xx = newdir;
1570
1571                 while (rename_allowed && xx) {
1572                         if (xx == obj)
1573                                 rename_allowed = 0;
1574                         xx = xx->parent;
1575                 }
1576                 if (!rename_allowed)
1577                         yaffsfs_SetError(-EINVAL);
1578         }
1579
1580         if (rename_allowed)
1581                 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1582
1583         yaffsfs_Unlock();
1584
1585         kfree(alt_newpath);
1586
1587         return (result == YAFFS_FAIL) ? -1 : 0;
1588 }
1589
1590 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1591 {
1592         int retVal = -1;
1593
1594         obj = yaffs_get_equivalent_obj(obj);
1595
1596         if (obj && buf) {
1597                 buf->st_dev = (int)obj->my_dev->os_context;
1598                 buf->st_ino = obj->obj_id;
1599                 buf->st_mode = obj->yst_mode & ~S_IFMT;
1600
1601                 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1602                         buf->st_mode |= S_IFDIR;
1603                 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1604                         buf->st_mode |= S_IFLNK;
1605                 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1606                         buf->st_mode |= S_IFREG;
1607
1608                 buf->st_nlink = yaffs_get_obj_link_count(obj);
1609                 buf->st_uid = 0;
1610                 buf->st_gid = 0;
1611                 buf->st_rdev = obj->yst_rdev;
1612                 buf->st_size = yaffs_get_obj_length(obj);
1613                 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1614                 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1615                     buf->st_blksize;
1616 #if CONFIG_YAFFS_WINCE
1617                 buf->yst_wince_atime[0] = obj->win_atime[0];
1618                 buf->yst_wince_atime[1] = obj->win_atime[1];
1619                 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1620                 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1621                 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1622                 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1623 #else
1624                 buf->yst_atime = obj->yst_atime;
1625                 buf->yst_ctime = obj->yst_ctime;
1626                 buf->yst_mtime = obj->yst_mtime;
1627 #endif
1628                 retVal = 0;
1629         }
1630         return retVal;
1631 }
1632
1633 static int yaffsfs_DoStatOrLStat(const YCHAR *path,
1634                                  struct yaffs_stat *buf, int doLStat)
1635 {
1636         struct yaffs_obj *obj = NULL;
1637         struct yaffs_obj *dir = NULL;
1638         int retVal = -1;
1639         int notDir = 0;
1640         int loop = 0;
1641
1642         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1643             yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1644                 yaffsfs_SetError(-EFAULT);
1645                 return -1;
1646         }
1647
1648         if (yaffsfs_CheckPath(path) < 0) {
1649                 yaffsfs_SetError(-ENAMETOOLONG);
1650                 return -1;
1651         }
1652
1653         yaffsfs_Lock();
1654
1655         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
1656
1657         if (!doLStat && obj)
1658                 obj = yaffsfs_FollowLink(obj, 0, &loop);
1659
1660         if (!dir && notDir)
1661                 yaffsfs_SetError(-ENOTDIR);
1662         else if (loop)
1663                 yaffsfs_SetError(-ELOOP);
1664         else if (!dir || !obj)
1665                 yaffsfs_SetError(-ENOENT);
1666         else
1667                 retVal = yaffsfs_DoStat(obj, buf);
1668
1669         yaffsfs_Unlock();
1670
1671         return retVal;
1672
1673 }
1674
1675 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1676 {
1677         return yaffsfs_DoStatOrLStat(path, buf, 0);
1678 }
1679
1680 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1681 {
1682         return yaffsfs_DoStatOrLStat(path, buf, 1);
1683 }
1684
1685 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1686 {
1687         struct yaffs_obj *obj;
1688
1689         int retVal = -1;
1690
1691         if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1692                 yaffsfs_SetError(-EFAULT);
1693                 return -1;
1694         }
1695
1696         yaffsfs_Lock();
1697         obj = yaffsfs_HandleToObject(fd);
1698
1699         if (obj)
1700                 retVal = yaffsfs_DoStat(obj, buf);
1701         else
1702                 /* bad handle */
1703                 yaffsfs_SetError(-EBADF);
1704
1705         yaffsfs_Unlock();
1706
1707         return retVal;
1708 }
1709
1710 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1711                            const struct yaffs_utimbuf *buf)
1712 {
1713         int retVal = -1;
1714         int result;
1715
1716         struct yaffs_utimbuf local;
1717
1718         obj = yaffs_get_equivalent_obj(obj);
1719
1720         if (obj && obj->my_dev->read_only) {
1721                 yaffsfs_SetError(-EROFS);
1722                 return -1;
1723         }
1724
1725         if (!buf) {
1726                 local.actime = Y_CURRENT_TIME;
1727                 local.modtime = local.actime;
1728                 buf = &local;
1729         }
1730
1731         if (obj) {
1732                 obj->yst_atime = buf->actime;
1733                 obj->yst_mtime = buf->modtime;
1734                 obj->dirty = 1;
1735                 result = yaffs_flush_file(obj, 0, 0);
1736                 retVal = result == YAFFS_OK ? 0 : -1;
1737         }
1738
1739         return retVal;
1740 }
1741
1742 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1743 {
1744         struct yaffs_obj *obj = NULL;
1745         struct yaffs_obj *dir = NULL;
1746         int retVal = -1;
1747         int notDir = 0;
1748         int loop = 0;
1749
1750         if (!path) {
1751                 yaffsfs_SetError(-EFAULT);
1752                 return -1;
1753         }
1754
1755         if (yaffsfs_CheckPath(path) < 0) {
1756                 yaffsfs_SetError(-ENAMETOOLONG);
1757                 return -1;
1758         }
1759
1760         yaffsfs_Lock();
1761
1762         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
1763
1764         if (!dir && notDir)
1765                 yaffsfs_SetError(-ENOTDIR);
1766         else if (loop)
1767                 yaffsfs_SetError(-ELOOP);
1768         else if (!dir || !obj)
1769                 yaffsfs_SetError(-ENOENT);
1770         else
1771                 retVal = yaffsfs_DoUtime(obj, buf);
1772
1773         yaffsfs_Unlock();
1774
1775         return retVal;
1776
1777 }
1778
1779 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1780 {
1781         struct yaffs_obj *obj;
1782
1783         int retVal = -1;
1784
1785         yaffsfs_Lock();
1786         obj = yaffsfs_HandleToObject(fd);
1787
1788         if (obj)
1789                 retVal = yaffsfs_DoUtime(obj, buf);
1790         else
1791                 /* bad handle */
1792                 yaffsfs_SetError(-EBADF);
1793
1794         yaffsfs_Unlock();
1795
1796         return retVal;
1797 }
1798
1799 #ifndef CONFIG_YAFFS_WINCE
1800 /* xattrib functions */
1801
1802 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1803                              const void *data, int size, int flags, int follow)
1804 {
1805         struct yaffs_obj *obj;
1806         struct yaffs_obj *dir;
1807         int notDir = 0;
1808         int loop = 0;
1809
1810         int retVal = -1;
1811
1812         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1813             yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1814             yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1815                 yaffsfs_SetError(-EFAULT);
1816                 return -1;
1817         }
1818
1819         if (yaffsfs_CheckPath(path) < 0) {
1820                 yaffsfs_SetError(-ENAMETOOLONG);
1821                 return -1;
1822         }
1823
1824         yaffsfs_Lock();
1825
1826         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
1827
1828         if (follow)
1829                 obj = yaffsfs_FollowLink(obj, 0, &loop);
1830
1831         if (!dir && notDir)
1832                 yaffsfs_SetError(-ENOTDIR);
1833         else if (loop)
1834                 yaffsfs_SetError(-ELOOP);
1835         else if (!dir || !obj)
1836                 yaffsfs_SetError(-ENOENT);
1837         else {
1838                 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1839                 if (retVal < 0) {
1840                         yaffsfs_SetError(retVal);
1841                         retVal = -1;
1842                 }
1843         }
1844
1845         yaffsfs_Unlock();
1846
1847         return retVal;
1848
1849 }
1850
1851 int yaffs_setxattr(const YCHAR *path, const char *name,
1852                    const void *data, int size, int flags)
1853 {
1854         return yaffs_do_setxattr(path, name, data, size, flags, 1);
1855 }
1856
1857 int yaffs_lsetxattr(const YCHAR *path, const char *name,
1858                     const void *data, int size, int flags)
1859 {
1860         return yaffs_do_setxattr(path, name, data, size, flags, 0);
1861 }
1862
1863 int yaffs_fsetxattr(int fd, const char *name,
1864                     const void *data, int size, int flags)
1865 {
1866         struct yaffs_obj *obj;
1867
1868         int retVal = -1;
1869
1870         if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1871             yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1872                 yaffsfs_SetError(-EFAULT);
1873                 return -1;
1874         }
1875
1876         yaffsfs_Lock();
1877         obj = yaffsfs_HandleToObject(fd);
1878
1879         if (!obj)
1880                 yaffsfs_SetError(-EBADF);
1881         else {
1882                 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1883                 if (retVal < 0) {
1884                         yaffsfs_SetError(retVal);
1885                         retVal = -1;
1886                 }
1887         }
1888
1889         yaffsfs_Unlock();
1890
1891         return retVal;
1892 }
1893
1894 static int yaffs_do_getxattr(const YCHAR *path, const char *name,
1895                              void *data, int size, int follow)
1896 {
1897         struct yaffs_obj *obj;
1898         struct yaffs_obj *dir;
1899         int retVal = -1;
1900         int notDir = 0;
1901         int loop = 0;
1902
1903         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1904             yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1905             yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1906                 yaffsfs_SetError(-EFAULT);
1907                 return -1;
1908         }
1909
1910         if (yaffsfs_CheckPath(path) < 0) {
1911                 yaffsfs_SetError(-ENAMETOOLONG);
1912                 return -1;
1913         }
1914
1915         yaffsfs_Lock();
1916
1917         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
1918
1919         if (follow)
1920                 obj = yaffsfs_FollowLink(obj, 0, &loop);
1921
1922         if (!dir && notDir)
1923                 yaffsfs_SetError(-ENOTDIR);
1924         else if (loop)
1925                 yaffsfs_SetError(-ELOOP);
1926         else if (!dir || !obj)
1927                 yaffsfs_SetError(-ENOENT);
1928         else {
1929                 retVal = yaffs_get_xattrib(obj, name, data, size);
1930                 if (retVal < 0) {
1931                         yaffsfs_SetError(retVal);
1932                         retVal = -1;
1933                 }
1934         }
1935         yaffsfs_Unlock();
1936
1937         return retVal;
1938
1939 }
1940
1941 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1942 {
1943         return yaffs_do_getxattr(path, name, data, size, 1);
1944 }
1945
1946 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1947 {
1948         return yaffs_do_getxattr(path, name, data, size, 0);
1949 }
1950
1951 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1952 {
1953         struct yaffs_obj *obj;
1954
1955         int retVal = -1;
1956
1957         if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1958             yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1959                 yaffsfs_SetError(-EFAULT);
1960                 return -1;
1961         }
1962
1963         yaffsfs_Lock();
1964         obj = yaffsfs_HandleToObject(fd);
1965
1966         if (obj) {
1967                 retVal = yaffs_get_xattrib(obj, name, data, size);
1968                 if (retVal < 0) {
1969                         yaffsfs_SetError(retVal);
1970                         retVal = -1;
1971                 }
1972         } else
1973                 /* bad handle */
1974                 yaffsfs_SetError(-EBADF);
1975
1976         yaffsfs_Unlock();
1977
1978         return retVal;
1979 }
1980
1981 static int yaffs_do_listxattr(const YCHAR *path, char *data,
1982                               int size, int follow)
1983 {
1984         struct yaffs_obj *obj = NULL;
1985         struct yaffs_obj *dir = NULL;
1986         int retVal = -1;
1987         int notDir = 0;
1988         int loop = 0;
1989
1990         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1991             yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1992                 yaffsfs_SetError(-EFAULT);
1993                 return -1;
1994         }
1995
1996         if (yaffsfs_CheckPath(path) < 0) {
1997                 yaffsfs_SetError(-ENAMETOOLONG);
1998                 return -1;
1999         }
2000
2001         yaffsfs_Lock();
2002
2003         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
2004
2005         if (follow)
2006                 obj = yaffsfs_FollowLink(obj, 0, &loop);
2007
2008         if (!dir && notDir)
2009                 yaffsfs_SetError(-ENOTDIR);
2010         else if (loop)
2011                 yaffsfs_SetError(-ELOOP);
2012         else if (!dir || !obj)
2013                 yaffsfs_SetError(-ENOENT);
2014         else {
2015                 retVal = yaffs_list_xattrib(obj, data, size);
2016                 if (retVal < 0) {
2017                         yaffsfs_SetError(retVal);
2018                         retVal = -1;
2019                 }
2020         }
2021
2022         yaffsfs_Unlock();
2023
2024         return retVal;
2025
2026 }
2027
2028 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2029 {
2030         return yaffs_do_listxattr(path, data, size, 1);
2031 }
2032
2033 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2034 {
2035         return yaffs_do_listxattr(path, data, size, 0);
2036 }
2037
2038 int yaffs_flistxattr(int fd, char *data, int size)
2039 {
2040         struct yaffs_obj *obj;
2041
2042         int retVal = -1;
2043
2044         if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2045                 yaffsfs_SetError(-EFAULT);
2046                 return -1;
2047         }
2048
2049         yaffsfs_Lock();
2050         obj = yaffsfs_HandleToObject(fd);
2051
2052         if (obj) {
2053                 retVal = yaffs_list_xattrib(obj, data, size);
2054                 if (retVal < 0) {
2055                         yaffsfs_SetError(retVal);
2056                         retVal = -1;
2057                 }
2058         } else
2059                 /* bad handle */
2060                 yaffsfs_SetError(-EBADF);
2061
2062         yaffsfs_Unlock();
2063
2064         return retVal;
2065 }
2066
2067 static int yaffs_do_removexattr(const YCHAR *path, const char *name,
2068                                 int follow)
2069 {
2070         struct yaffs_obj *obj = NULL;
2071         struct yaffs_obj *dir = NULL;
2072         int notDir = 0;
2073         int loop = 0;
2074         int retVal = -1;
2075
2076         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2077             yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2078                 yaffsfs_SetError(-EFAULT);
2079                 return -1;
2080         }
2081
2082         if (yaffsfs_CheckPath(path) < 0) {
2083                 yaffsfs_SetError(-ENAMETOOLONG);
2084                 return -1;
2085         }
2086
2087         yaffsfs_Lock();
2088
2089         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
2090
2091         if (follow)
2092                 obj = yaffsfs_FollowLink(obj, 0, &loop);
2093
2094         if (!dir && notDir)
2095                 yaffsfs_SetError(-ENOTDIR);
2096         else if (loop)
2097                 yaffsfs_SetError(-ELOOP);
2098         else if (!dir || !obj)
2099                 yaffsfs_SetError(-ENOENT);
2100         else {
2101                 retVal = yaffs_remove_xattrib(obj, name);
2102                 if (retVal < 0) {
2103                         yaffsfs_SetError(retVal);
2104                         retVal = -1;
2105                 }
2106         }
2107
2108         yaffsfs_Unlock();
2109
2110         return retVal;
2111
2112 }
2113
2114 int yaffs_removexattr(const YCHAR *path, const char *name)
2115 {
2116         return yaffs_do_removexattr(path, name, 1);
2117 }
2118
2119 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2120 {
2121         return yaffs_do_removexattr(path, name, 0);
2122 }
2123
2124 int yaffs_fremovexattr(int fd, const char *name)
2125 {
2126         struct yaffs_obj *obj;
2127
2128         int retVal = -1;
2129
2130         if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2131                 yaffsfs_SetError(-EFAULT);
2132                 return -1;
2133         }
2134
2135         yaffsfs_Lock();
2136         obj = yaffsfs_HandleToObject(fd);
2137
2138         if (obj) {
2139                 retVal = yaffs_remove_xattrib(obj, name);
2140                 if (retVal < 0) {
2141                         yaffsfs_SetError(retVal);
2142                         retVal = -1;
2143                 }
2144         } else
2145                 /* bad handle */
2146                 yaffsfs_SetError(-EBADF);
2147
2148         yaffsfs_Unlock();
2149
2150         return retVal;
2151 }
2152 #endif
2153
2154 #ifdef CONFIG_YAFFS_WINCE
2155 int yaffs_get_wince_times(int fd, unsigned *wctime,
2156                           unsigned *watime, unsigned *wmtime)
2157 {
2158         struct yaffs_obj *obj;
2159
2160         int retVal = -1;
2161
2162         yaffsfs_Lock();
2163         obj = yaffsfs_HandleToObject(fd);
2164
2165         if (obj) {
2166
2167                 if (wctime) {
2168                         wctime[0] = obj->win_ctime[0];
2169                         wctime[1] = obj->win_ctime[1];
2170                 }
2171                 if (watime) {
2172                         watime[0] = obj->win_atime[0];
2173                         watime[1] = obj->win_atime[1];
2174                 }
2175                 if (wmtime) {
2176                         wmtime[0] = obj->win_mtime[0];
2177                         wmtime[1] = obj->win_mtime[1];
2178                 }
2179
2180                 retVal = 0;
2181         } else
2182                 /*  bad handle */
2183                 yaffsfs_SetError(-EBADF);
2184
2185         yaffsfs_Unlock();
2186
2187         return retVal;
2188 }
2189
2190 int yaffs_set_wince_times(int fd,
2191                           const unsigned *wctime,
2192                           const unsigned *watime, const unsigned *wmtime)
2193 {
2194         struct yaffs_obj *obj;
2195         int result;
2196         int retVal = -1;
2197
2198         yaffsfs_Lock();
2199         obj = yaffsfs_HandleToObject(fd);
2200
2201         if (obj) {
2202
2203                 if (wctime) {
2204                         obj->win_ctime[0] = wctime[0];
2205                         obj->win_ctime[1] = wctime[1];
2206                 }
2207                 if (watime) {
2208                         obj->win_atime[0] = watime[0];
2209                         obj->win_atime[1] = watime[1];
2210                 }
2211                 if (wmtime) {
2212                         obj->win_mtime[0] = wmtime[0];
2213                         obj->win_mtime[1] = wmtime[1];
2214                 }
2215
2216                 obj->dirty = 1;
2217                 result = yaffs_flush_file(obj, 0, 0);
2218                 retVal = 0;
2219         } else
2220                 /* bad handle */
2221                 yaffsfs_SetError(-EBADF);
2222
2223         yaffsfs_Unlock();
2224
2225         return retVal;
2226 }
2227
2228 #endif
2229
2230 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2231 {
2232         int result = -1;
2233
2234         if (obj)
2235                 obj = yaffs_get_equivalent_obj(obj);
2236
2237         if (obj) {
2238                 obj->yst_mode = mode;
2239                 obj->dirty = 1;
2240                 result = yaffs_flush_file(obj, 0, 0);
2241         }
2242
2243         return result == YAFFS_OK ? 0 : -1;
2244 }
2245
2246 int yaffs_access(const YCHAR *path, int amode)
2247 {
2248         struct yaffs_obj *obj = NULL;
2249         struct yaffs_obj *dir = NULL;
2250         int notDir = 0;
2251         int loop = 0;
2252         int retval = -1;
2253
2254         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2255                 yaffsfs_SetError(-EFAULT);
2256                 return -1;
2257         }
2258
2259         if (yaffsfs_CheckPath(path) < 0) {
2260                 yaffsfs_SetError(-ENAMETOOLONG);
2261                 return -1;
2262         }
2263
2264         if (amode & ~(R_OK | W_OK | X_OK)) {
2265                 yaffsfs_SetError(-EINVAL);
2266                 return -1;
2267         }
2268
2269         yaffsfs_Lock();
2270
2271         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
2272         obj = yaffsfs_FollowLink(obj, 0, &loop);
2273
2274         if (!dir && notDir)
2275                 yaffsfs_SetError(-ENOTDIR);
2276         else if (loop)
2277                 yaffsfs_SetError(-ELOOP);
2278         else if (!dir || !obj)
2279                 yaffsfs_SetError(-ENOENT);
2280         else if ((amode & W_OK) && obj->my_dev->read_only)
2281                 yaffsfs_SetError(-EROFS);
2282         else {
2283                 int access_ok = 1;
2284
2285                 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2286                         access_ok = 0;
2287                 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2288                         access_ok = 0;
2289                 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2290                         access_ok = 0;
2291
2292                 if (!access_ok)
2293                         yaffsfs_SetError(-EACCES);
2294                 else
2295                         retval = 0;
2296         }
2297
2298         yaffsfs_Unlock();
2299
2300         return retval;
2301
2302 }
2303
2304 int yaffs_chmod(const YCHAR *path, mode_t mode)
2305 {
2306         struct yaffs_obj *obj = NULL;
2307         struct yaffs_obj *dir = NULL;
2308         int retVal = -1;
2309         int notDir = 0;
2310         int loop = 0;
2311
2312         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2313                 yaffsfs_SetError(-EFAULT);
2314                 return -1;
2315         }
2316
2317         if (yaffsfs_CheckPath(path) < 0) {
2318                 yaffsfs_SetError(-ENAMETOOLONG);
2319                 return -1;
2320         }
2321
2322         if (mode & ~(0777)) {
2323                 yaffsfs_SetError(-EINVAL);
2324                 return -1;
2325         }
2326
2327         yaffsfs_Lock();
2328
2329         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
2330         obj = yaffsfs_FollowLink(obj, 0, &loop);
2331
2332         if (!dir && notDir)
2333                 yaffsfs_SetError(-ENOTDIR);
2334         else if (loop)
2335                 yaffsfs_SetError(-ELOOP);
2336         else if (!dir || !obj)
2337                 yaffsfs_SetError(-ENOENT);
2338         else if (obj->my_dev->read_only)
2339                 yaffsfs_SetError(-EROFS);
2340         else
2341                 retVal = yaffsfs_DoChMod(obj, mode);
2342
2343         yaffsfs_Unlock();
2344
2345         return retVal;
2346
2347 }
2348
2349 int yaffs_fchmod(int fd, mode_t mode)
2350 {
2351         struct yaffs_obj *obj;
2352         int retVal = -1;
2353
2354         if (mode & ~(0777)) {
2355                 yaffsfs_SetError(-EINVAL);
2356                 return -1;
2357         }
2358
2359         yaffsfs_Lock();
2360         obj = yaffsfs_HandleToObject(fd);
2361
2362         if (!obj)
2363                 yaffsfs_SetError(-EBADF);
2364         else if (obj->my_dev->read_only)
2365                 yaffsfs_SetError(-EROFS);
2366         else
2367                 retVal = yaffsfs_DoChMod(obj, mode);
2368
2369         yaffsfs_Unlock();
2370
2371         return retVal;
2372 }
2373
2374 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2375 {
2376         struct yaffs_obj *parent = NULL;
2377         struct yaffs_obj *dir = NULL;
2378         YCHAR *name;
2379         YCHAR *alt_path = NULL;
2380         int retVal = -1;
2381         int notDir = 0;
2382         int loop = 0;
2383
2384         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2385                 yaffsfs_SetError(-EFAULT);
2386                 return -1;
2387         }
2388
2389         if (yaffsfs_CheckPath(path) < 0) {
2390                 yaffsfs_SetError(-ENAMETOOLONG);
2391                 return -1;
2392         }
2393
2394         if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2395                 yaffsfs_SetError(-ENOMEM);
2396                 return -1;
2397         }
2398         if (alt_path)
2399                 path = alt_path;
2400
2401         yaffsfs_Lock();
2402         parent = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
2403         if (!parent && notDir)
2404                 yaffsfs_SetError(-ENOTDIR);
2405         else if (loop)
2406                 yaffsfs_SetError(-ELOOP);
2407         else if (!parent)
2408                 yaffsfs_SetError(-ENOENT);
2409         else if (yaffsfs_TooManyObjects(parent->my_dev))
2410                 yaffsfs_SetError(-ENFILE);
2411         else if (yaffs_strnlen(name, 5) == 0) {
2412                 /* Trying to make the root itself */
2413                 yaffsfs_SetError(-EEXIST);
2414         } else if (parent->my_dev->read_only)
2415                 yaffsfs_SetError(-EROFS);
2416         else {
2417                 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2418                 if (dir)
2419                         retVal = 0;
2420                 else if (yaffs_find_by_name(parent, name))
2421                         yaffsfs_SetError(-EEXIST);      /* name exists */
2422                 else
2423                         yaffsfs_SetError(-ENOSPC);      /* assume no space */
2424         }
2425
2426         yaffsfs_Unlock();
2427
2428         kfree(alt_path);
2429
2430         return retVal;
2431 }
2432
2433 int yaffs_rmdir(const YCHAR *path)
2434 {
2435         int result;
2436         YCHAR *alt_path;
2437
2438         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2439                 yaffsfs_SetError(-EFAULT);
2440                 return -1;
2441         }
2442
2443         if (yaffsfs_CheckPath(path) < 0) {
2444                 yaffsfs_SetError(-ENAMETOOLONG);
2445                 return -1;
2446         }
2447
2448         if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2449                 yaffsfs_SetError(-ENOMEM);
2450                 return -1;
2451         }
2452         if (alt_path)
2453                 path = alt_path;
2454         result = yaffsfs_DoUnlink(path, 1);
2455
2456         kfree(alt_path);
2457
2458         return result;
2459 }
2460
2461 void *yaffs_getdev(const YCHAR *path)
2462 {
2463         struct yaffs_dev *dev = NULL;
2464         YCHAR *dummy;
2465         dev = yaffsfs_FindDevice(path, &dummy);
2466         return (void *)dev;
2467 }
2468
2469 int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
2470 {
2471         int retVal = -1;
2472         int result = YAFFS_FAIL;
2473         struct yaffs_dev *dev = NULL;
2474
2475         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2476                 yaffsfs_SetError(-EFAULT);
2477                 return -1;
2478         }
2479
2480         yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2481
2482         if (yaffsfs_CheckPath(path) < 0) {
2483                 yaffsfs_SetError(-ENAMETOOLONG);
2484                 return -1;
2485         }
2486
2487         yaffsfs_Lock();
2488
2489         yaffsfs_InitHandles();
2490
2491         dev = yaffsfs_FindMountPoint(path);
2492         if (dev) {
2493                 if (!dev->is_mounted) {
2494                         dev->read_only = read_only ? 1 : 0;
2495                         if (skip_checkpt) {
2496                                 u8 skip = dev->param.skip_checkpt_rd;
2497                                 dev->param.skip_checkpt_rd = 1;
2498                                 result = yaffs_guts_initialise(dev);
2499                                 dev->param.skip_checkpt_rd = skip;
2500                         } else {
2501                                 result = yaffs_guts_initialise(dev);
2502                         }
2503
2504                         if (result == YAFFS_FAIL)
2505                                 yaffsfs_SetError(-ENOMEM);
2506                         retVal = result ? 0 : -1;
2507
2508                 } else
2509                         yaffsfs_SetError(-EBUSY);
2510         } else
2511                 yaffsfs_SetError(-ENODEV);
2512
2513         yaffsfs_Unlock();
2514         return retVal;
2515
2516 }
2517
2518 int yaffs_mount2(const YCHAR *path, int readonly)
2519 {
2520         return yaffs_mount_common(path, readonly, 0);
2521 }
2522
2523 int yaffs_mount(const YCHAR *path)
2524 {
2525         return yaffs_mount_common(path, 0, 0);
2526 }
2527
2528 int yaffs_sync(const YCHAR *path)
2529 {
2530         int retVal = -1;
2531         struct yaffs_dev *dev = NULL;
2532         YCHAR *dummy;
2533
2534         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2535                 yaffsfs_SetError(-EFAULT);
2536                 return -1;
2537         }
2538
2539         if (yaffsfs_CheckPath(path) < 0) {
2540                 yaffsfs_SetError(-ENAMETOOLONG);
2541                 return -1;
2542         }
2543
2544         yaffsfs_Lock();
2545         dev = yaffsfs_FindDevice(path, &dummy);
2546         if (dev) {
2547                 if (!dev->is_mounted)
2548                         yaffsfs_SetError(-EINVAL);
2549                 else if (dev->read_only)
2550                         yaffsfs_SetError(-EROFS);
2551                 else {
2552
2553                         yaffs_flush_whole_cache(dev);
2554                         yaffs_checkpoint_save(dev);
2555                         retVal = 0;
2556
2557                 }
2558         } else
2559                 yaffsfs_SetError(-ENODEV);
2560
2561         yaffsfs_Unlock();
2562         return retVal;
2563 }
2564
2565 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2566 {
2567         int i;
2568         struct yaffs_obj *obj;
2569
2570         for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2571                 obj = yaffsfs_HandleToObject(i);
2572                 if (obj && obj->my_dev == dev)
2573                         return 1;
2574         }
2575         return 0;
2576 }
2577
2578 int yaffs_remount(const YCHAR *path, int force, int read_only)
2579 {
2580         int retVal = -1;
2581         struct yaffs_dev *dev = NULL;
2582
2583         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2584                 yaffsfs_SetError(-EFAULT);
2585                 return -1;
2586         }
2587
2588         if (yaffsfs_CheckPath(path) < 0) {
2589                 yaffsfs_SetError(-ENAMETOOLONG);
2590                 return -1;
2591         }
2592
2593         yaffsfs_Lock();
2594         dev = yaffsfs_FindMountPoint(path);
2595         if (dev) {
2596                 if (dev->is_mounted) {
2597                         yaffs_flush_whole_cache(dev);
2598
2599                         if (force || !yaffsfs_IsDevBusy(dev)) {
2600                                 if (read_only)
2601                                         yaffs_checkpoint_save(dev);
2602                                 dev->read_only = read_only ? 1 : 0;
2603                                 retVal = 0;
2604                         } else
2605                                 yaffsfs_SetError(-EBUSY);
2606
2607                 } else
2608                         yaffsfs_SetError(-EINVAL);
2609
2610         } else
2611                 yaffsfs_SetError(-ENODEV);
2612
2613         yaffsfs_Unlock();
2614         return retVal;
2615
2616 }
2617
2618 int yaffs_unmount2(const YCHAR *path, int force)
2619 {
2620         int retVal = -1;
2621         struct yaffs_dev *dev = NULL;
2622
2623         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2624                 yaffsfs_SetError(-EFAULT);
2625                 return -1;
2626         }
2627
2628         if (yaffsfs_CheckPath(path) < 0) {
2629                 yaffsfs_SetError(-ENAMETOOLONG);
2630                 return -1;
2631         }
2632
2633         yaffsfs_Lock();
2634         dev = yaffsfs_FindMountPoint(path);
2635         if (dev) {
2636                 if (dev->is_mounted) {
2637                         int inUse;
2638                         yaffs_flush_whole_cache(dev);
2639                         yaffs_checkpoint_save(dev);
2640                         inUse = yaffsfs_IsDevBusy(dev);
2641                         if (!inUse || force) {
2642                                 if (inUse)
2643                                         yaffsfs_BreakDeviceHandles(dev);
2644                                 yaffs_deinitialise(dev);
2645
2646                                 retVal = 0;
2647                         } else
2648                                 yaffsfs_SetError(-EBUSY);
2649
2650                 } else
2651                         yaffsfs_SetError(-EINVAL);
2652
2653         } else
2654                 yaffsfs_SetError(-ENODEV);
2655
2656         yaffsfs_Unlock();
2657         return retVal;
2658
2659 }
2660
2661 int yaffs_unmount(const YCHAR *path)
2662 {
2663         return yaffs_unmount2(path, 0);
2664 }
2665
2666 int yaffs_format(const YCHAR *path,
2667                 int unmount_flag,
2668                 int force_unmount_flag,
2669                 int remount_flag)
2670 {
2671         int retVal = 0;
2672         struct yaffs_dev *dev = NULL;
2673         int result;
2674
2675         if (!path) {
2676                 yaffsfs_SetError(-EFAULT);
2677                 return -1;
2678         }
2679
2680         if (yaffsfs_CheckPath(path) < 0) {
2681                 yaffsfs_SetError(-ENAMETOOLONG);
2682                 return -1;
2683         }
2684
2685         yaffsfs_Lock();
2686         dev = yaffsfs_FindMountPoint(path);
2687
2688         if (dev) {
2689                 int was_mounted = dev->is_mounted;
2690
2691                 if (dev->is_mounted && unmount_flag) {
2692                         int inUse;
2693                         yaffs_flush_whole_cache(dev);
2694                         yaffs_checkpoint_save(dev);
2695                         inUse = yaffsfs_IsDevBusy(dev);
2696                         if (!inUse || force_unmount_flag) {
2697                                 if (inUse)
2698                                         yaffsfs_BreakDeviceHandles(dev);
2699                                 yaffs_deinitialise(dev);
2700                         }
2701                 }
2702
2703                 if(dev->is_mounted) {
2704                                 yaffsfs_SetError(-EBUSY);
2705                                 retVal = -1;
2706                 } else {
2707                         yaffs_format_dev(dev);
2708                         if(was_mounted && remount_flag) {
2709                                 result = yaffs_guts_initialise(dev);
2710                                 if (result == YAFFS_FAIL) {
2711                                         yaffsfs_SetError(-ENOMEM);
2712                                         retVal = -1;
2713                                 }
2714                         }
2715                 }
2716         } else {
2717                 yaffsfs_SetError(-ENODEV);
2718                 retVal = -1;
2719         }
2720
2721         yaffsfs_Unlock();
2722         return retVal;
2723
2724 }
2725
2726
2727 Y_LOFF_T yaffs_freespace(const YCHAR *path)
2728 {
2729         Y_LOFF_T retVal = -1;
2730         struct yaffs_dev *dev = NULL;
2731         YCHAR *dummy;
2732
2733         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2734                 yaffsfs_SetError(-EFAULT);
2735                 return -1;
2736         }
2737
2738         if (yaffsfs_CheckPath(path) < 0) {
2739                 yaffsfs_SetError(-ENAMETOOLONG);
2740                 return -1;
2741         }
2742
2743         yaffsfs_Lock();
2744         dev = yaffsfs_FindDevice(path, &dummy);
2745         if (dev && dev->is_mounted) {
2746                 retVal = yaffs_get_n_free_chunks(dev);
2747                 retVal *= dev->data_bytes_per_chunk;
2748
2749         } else
2750                 yaffsfs_SetError(-EINVAL);
2751
2752         yaffsfs_Unlock();
2753         return retVal;
2754 }
2755
2756 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
2757 {
2758         Y_LOFF_T retVal = -1;
2759         struct yaffs_dev *dev = NULL;
2760         YCHAR *dummy;
2761
2762         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2763                 yaffsfs_SetError(-EFAULT);
2764                 return -1;
2765         }
2766
2767         if (yaffsfs_CheckPath(path) < 0) {
2768                 yaffsfs_SetError(-ENAMETOOLONG);
2769                 return -1;
2770         }
2771
2772         yaffsfs_Lock();
2773         dev = yaffsfs_FindDevice(path, &dummy);
2774         if (dev && dev->is_mounted) {
2775                 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2776                     dev->param.n_reserved_blocks;
2777                 retVal *= dev->param.chunks_per_block;
2778                 retVal *= dev->data_bytes_per_chunk;
2779
2780         } else
2781                 yaffsfs_SetError(-EINVAL);
2782
2783         yaffsfs_Unlock();
2784         return retVal;
2785 }
2786
2787 int yaffs_inodecount(const YCHAR *path)
2788 {
2789         Y_LOFF_T retVal = -1;
2790         struct yaffs_dev *dev = NULL;
2791         YCHAR *dummy;
2792
2793         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2794                 yaffsfs_SetError(-EFAULT);
2795                 return -1;
2796         }
2797
2798         if (yaffsfs_CheckPath(path) < 0) {
2799                 yaffsfs_SetError(-ENAMETOOLONG);
2800                 return -1;
2801         }
2802
2803         yaffsfs_Lock();
2804         dev = yaffsfs_FindDevice(path, &dummy);
2805         if (dev && dev->is_mounted) {
2806                 int n_obj = dev->n_obj;
2807                 if (n_obj > dev->n_hardlinks)
2808                         retVal = n_obj - dev->n_hardlinks;
2809         }
2810
2811         if (retVal < 0)
2812                 yaffsfs_SetError(-EINVAL);
2813
2814         yaffsfs_Unlock();
2815         return retVal;
2816 }
2817
2818 void yaffs_add_device(struct yaffs_dev *dev)
2819 {
2820         struct list_head *cfg;
2821         /* First check that the device is not in the list. */
2822
2823         list_for_each(cfg, &yaffsfs_deviceList) {
2824                 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
2825                         return;
2826         }
2827
2828         dev->is_mounted = 0;
2829         dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2830
2831         if (!dev->dev_list.next)
2832                 INIT_LIST_HEAD(&dev->dev_list);
2833
2834         list_add(&dev->dev_list, &yaffsfs_deviceList);
2835 }
2836
2837 void yaffs_remove_device(struct yaffs_dev *dev)
2838 {
2839         list_del_init(&dev->dev_list);
2840 }
2841
2842 /* Functions to iterate through devices. NB Use with extreme care! */
2843
2844 static struct list_head *dev_iterator;
2845 void yaffs_dev_rewind(void)
2846 {
2847         dev_iterator = yaffsfs_deviceList.next;
2848 }
2849
2850 struct yaffs_dev *yaffs_next_dev(void)
2851 {
2852         struct yaffs_dev *retval;
2853
2854         if (!dev_iterator)
2855                 return NULL;
2856         if (dev_iterator == &yaffsfs_deviceList)
2857                 return NULL;
2858
2859         retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2860         dev_iterator = dev_iterator->next;
2861         return retval;
2862 }
2863
2864 /* Directory search stuff. */
2865
2866 static struct list_head search_contexts;
2867
2868 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContxt *dsc)
2869 {
2870         if (dsc &&
2871             dsc->dirObj &&
2872             dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2873
2874                 dsc->offset = 0;
2875
2876                 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
2877                         dsc->nextReturn = NULL;
2878                 else
2879                         dsc->nextReturn =
2880                             list_entry(dsc->dirObj->variant.dir_variant.
2881                                        children.next, struct yaffs_obj,
2882                                        siblings);
2883         } else {
2884                 /* Hey someone isn't playing nice! */
2885         }
2886 }
2887
2888 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContxt *dsc)
2889 {
2890         if (dsc &&
2891             dsc->dirObj &&
2892             dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2893
2894                 if (dsc->nextReturn == NULL ||
2895                     list_empty(&dsc->dirObj->variant.dir_variant.children))
2896                         dsc->nextReturn = NULL;
2897                 else {
2898                         struct list_head *next = dsc->nextReturn->siblings.next;
2899
2900                         if (next == &dsc->dirObj->variant.dir_variant.children)
2901                                 dsc->nextReturn = NULL; /* end of list */
2902                         else
2903                                 dsc->nextReturn = list_entry(next,
2904                                                              struct yaffs_obj,
2905                                                              siblings);
2906                 }
2907         } else {
2908                 /* Hey someone isn't playing nice! */
2909         }
2910 }
2911
2912 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2913 {
2914
2915         struct list_head *i;
2916         struct yaffsfs_DirSearchContxt *dsc;
2917
2918         /* if search contexts not initilised then skip */
2919         if (!search_contexts.next)
2920                 return;
2921
2922         /* Iterate through the directory search contexts.
2923          * If any are the one being removed, then advance the dsc to
2924          * the next one to prevent a hanging ptr.
2925          */
2926         list_for_each(i, &search_contexts) {
2927                 if (i) {
2928                         dsc = list_entry(i, struct yaffsfs_DirSearchContxt,
2929                                          others);
2930                         if (dsc->nextReturn == obj)
2931                                 yaffsfs_DirAdvance(dsc);
2932                 }
2933         }
2934
2935 }
2936
2937 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2938 {
2939         yaffs_DIR *dir = NULL;
2940         struct yaffs_obj *obj = NULL;
2941         struct yaffsfs_DirSearchContxt *dsc = NULL;
2942         int notDir = 0;
2943         int loop = 0;
2944
2945         if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
2946                 yaffsfs_SetError(-EFAULT);
2947                 return NULL;
2948         }
2949
2950         if (yaffsfs_CheckPath(dirname) < 0) {
2951                 yaffsfs_SetError(-ENAMETOOLONG);
2952                 return NULL;
2953         }
2954
2955         yaffsfs_Lock();
2956
2957         obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, &notDir, &loop);
2958
2959         if (!obj && notDir)
2960                 yaffsfs_SetError(-ENOTDIR);
2961         else if (loop)
2962                 yaffsfs_SetError(-ELOOP);
2963         else if (!obj)
2964                 yaffsfs_SetError(-ENOENT);
2965         else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2966                 yaffsfs_SetError(-ENOTDIR);
2967         else {
2968                 int i;
2969
2970                 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
2971                         if (!yaffsfs_dsc[i].inUse)
2972                                 dsc = &yaffsfs_dsc[i];
2973                 }
2974
2975                 dir = (yaffs_DIR *) dsc;
2976
2977                 if (dsc) {
2978                         memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContxt));
2979                         dsc->inUse = 1;
2980                         dsc->dirObj = obj;
2981                         yaffs_strncpy(dsc->name, dirname, NAME_MAX);
2982                         INIT_LIST_HEAD(&dsc->others);
2983
2984                         if (!search_contexts.next)
2985                                 INIT_LIST_HEAD(&search_contexts);
2986
2987                         list_add(&dsc->others, &search_contexts);
2988                         yaffsfs_SetDirRewound(dsc);
2989                 }
2990
2991         }
2992
2993         yaffsfs_Unlock();
2994
2995         return dir;
2996 }
2997
2998 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
2999 {
3000         struct yaffsfs_DirSearchContxt *dsc;
3001         struct yaffs_dirent *retVal = NULL;
3002
3003         dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3004         yaffsfs_Lock();
3005
3006         if (dsc && dsc->inUse) {
3007                 yaffsfs_SetError(0);
3008                 if (dsc->nextReturn) {
3009                         dsc->de.d_ino =
3010                             yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3011                         dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3012                         dsc->de.d_off = dsc->offset++;
3013                         yaffs_get_obj_name(dsc->nextReturn,
3014                                            dsc->de.d_name, NAME_MAX);
3015                         if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3016                                 /* this should not happen! */
3017                                 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3018                         }
3019                         dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3020                         retVal = &dsc->de;
3021                         yaffsfs_DirAdvance(dsc);
3022                 } else
3023                         retVal = NULL;
3024         } else
3025                 yaffsfs_SetError(-EBADF);
3026
3027         yaffsfs_Unlock();
3028
3029         return retVal;
3030
3031 }
3032
3033 void yaffs_rewinddir(yaffs_DIR *dirp)
3034 {
3035         struct yaffsfs_DirSearchContxt *dsc;
3036
3037         dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3038
3039         if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3040                 return;
3041
3042         yaffsfs_Lock();
3043
3044         yaffsfs_SetDirRewound(dsc);
3045
3046         yaffsfs_Unlock();
3047 }
3048
3049 int yaffs_closedir(yaffs_DIR *dirp)
3050 {
3051         struct yaffsfs_DirSearchContxt *dsc;
3052
3053         dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3054
3055         if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3056                 yaffsfs_SetError(-EFAULT);
3057                 return -1;
3058         }
3059
3060         yaffsfs_Lock();
3061         dsc->inUse = 0;
3062         list_del(&dsc->others); /* unhook from list */
3063         yaffsfs_Unlock();
3064         return 0;
3065 }
3066
3067 /* End of directory stuff */
3068
3069 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3070 {
3071         struct yaffs_obj *parent = NULL;
3072         struct yaffs_obj *obj;
3073         YCHAR *name;
3074         int retVal = -1;
3075         int mode = 0;           /* ignore for now */
3076         int notDir = 0;
3077         int loop = 0;
3078
3079         if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3080             yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3081                 yaffsfs_SetError(-EFAULT);
3082                 return -1;
3083         }
3084
3085         if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3086                 yaffsfs_SetError(-ENAMETOOLONG);
3087                 return -1;
3088         }
3089
3090         yaffsfs_Lock();
3091         parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, &notDir, &loop);
3092         if (!parent && notDir)
3093                 yaffsfs_SetError(-ENOTDIR);
3094         else if (loop)
3095                 yaffsfs_SetError(-ELOOP);
3096         else if (!parent || yaffs_strnlen(name, 5) < 1)
3097                 yaffsfs_SetError(-ENOENT);
3098         else if (yaffsfs_TooManyObjects(parent->my_dev))
3099                 yaffsfs_SetError(-ENFILE);
3100         else if (parent->my_dev->read_only)
3101                 yaffsfs_SetError(-EROFS);
3102         else if (parent) {
3103                 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3104                 if (obj)
3105                         retVal = 0;
3106                 else if (yaffsfs_FindObject
3107                          (NULL, newpath, 0, 0, NULL, NULL, NULL))
3108                         yaffsfs_SetError(-EEXIST);
3109                 else
3110                         yaffsfs_SetError(-ENOSPC);
3111         }
3112
3113         yaffsfs_Unlock();
3114
3115         return retVal;
3116
3117 }
3118
3119 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3120 {
3121         struct yaffs_obj *obj = NULL;
3122         struct yaffs_obj *dir = NULL;
3123         int retVal = -1;
3124         int notDir = 0;
3125         int loop = 0;
3126
3127         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3128             yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3129                 yaffsfs_SetError(-EFAULT);
3130                 return -1;
3131         }
3132
3133         yaffsfs_Lock();
3134
3135         obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
3136
3137         if (!dir && notDir)
3138                 yaffsfs_SetError(-ENOTDIR);
3139         else if (loop)
3140                 yaffsfs_SetError(-ELOOP);
3141         else if (!dir || !obj)
3142                 yaffsfs_SetError(-ENOENT);
3143         else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3144                 yaffsfs_SetError(-EINVAL);
3145         else {
3146                 YCHAR *alias = obj->variant.symlink_variant.alias;
3147                 memset(buf, 0, bufsiz);
3148                 yaffs_strncpy(buf, alias, bufsiz - 1);
3149                 retVal = 0;
3150         }
3151         yaffsfs_Unlock();
3152         return retVal;
3153 }
3154
3155 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3156 {
3157         /* Creates a link called newpath to existing oldpath */
3158         struct yaffs_obj *obj = NULL;
3159         struct yaffs_obj *lnk = NULL;
3160         struct yaffs_obj *obj_dir = NULL;
3161         struct yaffs_obj *lnk_dir = NULL;
3162         int retVal = -1;
3163         int notDirObj = 0;
3164         int notDirLnk = 0;
3165         int objLoop = 0;
3166         int lnkLoop = 0;
3167         YCHAR *newname;
3168
3169         if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3170             yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3171                 yaffsfs_SetError(-EFAULT);
3172                 return -1;
3173         }
3174
3175         if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3176                 yaffsfs_SetError(-ENAMETOOLONG);
3177                 return -1;
3178         }
3179
3180         yaffsfs_Lock();
3181
3182         obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
3183                                  &obj_dir, &notDirObj, &objLoop);
3184         lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
3185         lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
3186                                         0, &notDirLnk, &lnkLoop);
3187
3188         if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3189                 yaffsfs_SetError(-ENOTDIR);
3190         else if (objLoop || lnkLoop)
3191                 yaffsfs_SetError(-ELOOP);
3192         else if (!obj_dir || !lnk_dir || !obj)
3193                 yaffsfs_SetError(-ENOENT);
3194         else if (obj->my_dev->read_only)
3195                 yaffsfs_SetError(-EROFS);
3196         else if (yaffsfs_TooManyObjects(obj->my_dev))
3197                 yaffsfs_SetError(-ENFILE);
3198         else if (lnk)
3199                 yaffsfs_SetError(-EEXIST);
3200         else if (lnk_dir->my_dev != obj->my_dev)
3201                 yaffsfs_SetError(-EXDEV);
3202         else {
3203                 retVal = yaffsfs_CheckNameLength(newname);
3204
3205                 if (retVal == 0) {
3206                         lnk = yaffs_link_obj(lnk_dir, newname, obj);
3207                         if (lnk)
3208                                 retVal = 0;
3209                         else {
3210                                 yaffsfs_SetError(-ENOSPC);
3211                                 retVal = -1;
3212                         }
3213                 }
3214         }
3215         yaffsfs_Unlock();
3216
3217         return retVal;
3218 }
3219
3220 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3221 {
3222         (void) pathname;
3223         (void) mode;
3224         (void) dev;
3225
3226         yaffsfs_SetError(-EINVAL);
3227         return -1;
3228 }
3229
3230 /*
3231  * D E B U G   F U N C T I O N S
3232  */
3233
3234 /*
3235  * yaffs_n_handles()
3236  * Returns number of handles attached to the object
3237  */
3238 int yaffs_n_handles(const YCHAR *path)
3239 {
3240         struct yaffs_obj *obj;
3241
3242         if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3243                 yaffsfs_SetError(-EFAULT);
3244                 return -1;
3245         }
3246
3247         if (yaffsfs_CheckPath(path) < 0) {
3248                 yaffsfs_SetError(-ENAMETOOLONG);
3249                 return -1;
3250         }
3251
3252         obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
3253
3254         if (obj)
3255                 return yaffsfs_CountHandles(obj);
3256         else
3257                 return -1;
3258 }
3259
3260 int yaffs_get_error(void)
3261 {
3262         return yaffsfs_GetLastError();
3263 }
3264
3265 int yaffs_set_error(int error)
3266 {
3267         yaffsfs_SetError(error);
3268         return 0;
3269 }
3270
3271 int yaffs_dump_dev(const YCHAR *path)
3272 {
3273 #if 1
3274         (void) path;
3275 #else
3276         YCHAR *rest;
3277
3278         struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
3279
3280         if (obj) {
3281                 struct yaffs_dev *dev = obj->my_dev;
3282
3283                 printf("\n"
3284                        "n_page_writes.......... %d\n"
3285                        "n_page_reads........... %d\n"
3286                        "n_erasures....... %d\n"
3287                        "n_gc_copies............ %d\n"
3288                        "garbageCollections... %d\n"
3289                        "passiveGarbageColl'ns %d\n"
3290                        "\n",
3291                        dev->n_page_writes,
3292                        dev->n_page_reads,
3293                        dev->n_erasures,
3294                        dev->n_gc_copies,
3295                        dev->garbageCollections, dev->passiveGarbageCollections);
3296
3297         }
3298 #endif
3299         return 0;
3300 }