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