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