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