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