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