2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2018 Aleph One Ltd.
6 * Created by Charles Manning <charles@aleph1.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
14 #include "yaffs_guts.h"
17 #include "yaffs_trace.h"
21 #define YAFFS_MAX_RW_SIZE 0x70000000
22 #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
25 #define NULL ((void *)0)
28 #define ROOT_DIR(dev) (((dev) && (dev)->is_mounted) ? (dev)->root_dir : NULL)
30 /* YAFFSFS_RW_SIZE must be a power of 2 */
31 #define YAFFSFS_RW_SHIFT (13)
32 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
34 /* Some forward references */
35 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
37 int symDepth, int getEquiv,
38 struct yaffs_obj **dirOut,
39 int *notDir, int *loop);
41 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
42 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(
43 struct yaffs_obj *reldir, const YCHAR *dirname);
44 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirent);
46 unsigned int yaffs_wr_attempts;
50 * There are open inodes in struct yaffsfs_Inode.
51 * There are open file descriptors in yaffsfs_FileDes.
52 * There are open handles in yaffsfs_FileDes.
54 * Things are structured this way to be like the Linux VFS model
55 * so that interactions with the yaffs guts calls are similar.
56 * That means more common code paths and less special code.
57 * That means better testing etc.
59 * We have 3 layers because:
60 * A handle is different than an fd because you can use dup()
61 * to create a new handle that accesses the *same* fd. The two
62 * handles will use the same offset (part of the fd). We only close
63 * down the fd when there are no more handles accessing it.
65 * More than one fd can currently access one file, but each fd
66 * has its own permsiions and offset.
69 struct yaffsfs_Inode {
70 int count; /* Number of handles accessing this inode */
71 struct yaffs_obj *iObj;
75 struct yaffsfs_DirSearchContext {
76 struct yaffs_dirent de; /* directory entry */
77 YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
78 struct yaffs_obj *dirObj; /* ptr to directory being searched */
79 struct yaffs_obj *nextReturn; /* obj returned by next readddir */
80 struct list_head others;
85 struct yaffsfs_FileDes {
86 u8 isDir:1; /* This s a directory */
92 s32 inodeId:12; /* Index to corresponding yaffsfs_Inode */
93 s32 handleCount:10; /* Number of handles for this fd */
95 Y_LOFF_T position; /* current position in file */
100 struct yaffsfs_Handle {
106 static struct yaffsfs_DirSearchContext yaffsfs_dsc[YAFFSFS_N_DSC];
107 static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
108 static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
109 static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
111 static int yaffsfs_handlesInitialised;
113 unsigned yaffs_set_trace(unsigned tm)
115 yaffs_trace_mask = tm;
116 return yaffs_trace_mask;
119 unsigned yaffs_get_trace(void)
121 return yaffs_trace_mask;
126 * Inilitalise handle management on start-up.
129 static void yaffsfs_InitHandles(void)
133 if (yaffsfs_handlesInitialised)
136 yaffsfs_handlesInitialised = 1;
138 memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
139 memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
140 memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
141 memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
143 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
144 yaffsfs_fd[i].inodeId = -1;
145 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
146 yaffsfs_handle[i].fdId = -1;
149 static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
151 if (h >= 0 && h < YAFFSFS_N_HANDLES)
152 return &yaffsfs_handle[h];
156 static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
158 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
160 if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
161 return &yaffsfs_fd[h->fdId];
166 static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
168 struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
170 if (fd && fd->handleCount > 0 &&
171 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
172 return &yaffsfs_inode[fd->inodeId];
177 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
179 struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
188 * yaffsfs_FindInodeIdForObject
189 * Find the inode entry for an object, if it exists.
192 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
198 obj = yaffs_get_equivalent_obj(obj);
200 /* Look for it in open inode table */
201 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
202 if (yaffsfs_inode[i].iObj == obj)
209 * yaffsfs_GetInodeIdForObject
210 * Grab an inode entry when opening a new inode.
212 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
216 struct yaffsfs_Inode *in = NULL;
219 obj = yaffs_get_equivalent_obj(obj);
221 ret = yaffsfs_FindInodeIdForObject(obj);
223 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
224 if (!yaffsfs_inode[i].iObj)
229 in = &yaffsfs_inode[ret];
239 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
241 int i = yaffsfs_FindInodeIdForObject(obj);
244 return yaffsfs_inode[i].count;
249 static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
251 struct yaffs_obj *obj;
254 obj->my_inode = NULL;
261 static void yaffsfs_PutInode(int inodeId)
263 if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
264 struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
266 if (in->count <= 0) {
267 yaffsfs_ReleaseInode(in);
273 static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
276 struct yaffsfs_Handle *h;
278 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
279 h = &yaffsfs_handle[i];
280 if (h->useCount < 1) {
281 memset(h, 0, sizeof(struct yaffsfs_Handle));
292 static int yaffsfs_NewHandleAndFileDes(void)
295 struct yaffsfs_FileDes *fd;
296 struct yaffsfs_Handle *h = NULL;
297 int handle = yaffsfs_NewHandle(&h);
302 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
304 if (fd->handleCount < 1) {
305 memset(fd, 0, sizeof(struct yaffsfs_FileDes));
313 /* Dump the handle because we could not get a fd */
320 * Increase use of handle when reading/writing a file
321 * Also gets the file descriptor.
324 static int yaffsfs_GetHandle(int handle)
326 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
328 if (h && h->useCount > 0) {
337 * Let go of a handle when closing a file or aborting an open or
338 * ending a read or write.
341 static int yaffsfs_PutFileDes(int fdId)
343 struct yaffsfs_FileDes *fd;
345 if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
346 fd = &yaffsfs_fd[fdId];
348 if (fd->handleCount < 1) {
350 yaffsfs_closedir_no_lock(fd->v.dir);
353 if (fd->inodeId >= 0) {
354 yaffsfs_PutInode(fd->inodeId);
362 static int yaffsfs_PutHandle(int handle)
364 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
366 if (h && h->useCount > 0) {
368 if (h->useCount < 1) {
369 yaffsfs_PutFileDes(h->fdId);
377 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
379 struct yaffsfs_FileDes *fd;
380 struct yaffsfs_Handle *h;
381 struct yaffs_obj *obj;
383 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
384 h = yaffsfs_HandleToPointer(i);
385 fd = yaffsfs_HandleToFileDes(i);
386 obj = yaffsfs_HandleToObject(i);
387 if (h && h->useCount > 0) {
391 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
393 yaffsfs_PutInode(fd->inodeId);
400 * Stuff to handle names.
402 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
403 #ifndef CONFIG_YAFFS_WINCE
404 static int yaffs_toupper(YCHAR a)
406 if (a >= 'a' && a <= 'z')
407 return (a - 'a') + 'A';
413 static int yaffsfs_Match(YCHAR a, YCHAR b)
415 return (yaffs_toupper(a) == yaffs_toupper(b));
418 static int yaffsfs_Match(YCHAR a, YCHAR b)
425 static int yaffsfs_IsPathDivider(YCHAR ch)
427 const YCHAR *str = YAFFS_PATH_DIVIDERS;
438 static int yaffsfs_CheckNameLength(const YCHAR *name)
442 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
444 if (nameLength == 0) {
445 yaffsfs_SetError(-ENOENT);
447 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
448 yaffsfs_SetError(-ENAMETOOLONG);
455 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
457 YCHAR *alt_path = NULL;
462 * We don't have a definition for max path length.
463 * We will use 3 * max name length instead.
466 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
468 /* If the last character is a path divider, then we need to
469 * trim it back so that the name look-up works properly.
470 * eg. /foo/new_dir/ -> /foo/newdir
471 * Curveball: Need to handle multiple path dividers:
472 * eg. /foof/sdfse///// -> /foo/sdfse
474 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
475 alt_path = kmalloc(path_length + 1, 0);
478 yaffs_strcpy(alt_path, path);
479 for (i = path_length - 1;
480 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
481 alt_path[i] = (YCHAR) 0;
483 *ret_path = alt_path;
487 LIST_HEAD(yaffsfs_deviceList);
492 * Scan the configuration list to find the device
493 * Curveballs: Should match paths that end in '/' too
494 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
496 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
499 struct list_head *cfg;
500 const YCHAR *leftOver;
502 struct yaffs_dev *retval = NULL;
503 struct yaffs_dev *dev = NULL;
505 int longestMatch = -1;
509 * Check all configs, choose the one that:
510 * 1) Actually matches a prefix (ie /a amd /abc will not match
511 * 2) Matches the longest.
513 list_for_each(cfg, &yaffsfs_deviceList) {
514 dev = list_entry(cfg, struct yaffs_dev, dev_list);
524 /* Skip over any leading /s */
525 while (yaffsfs_IsPathDivider(*p))
527 while (yaffsfs_IsPathDivider(*leftOver))
530 while (matching && *p && *leftOver) {
532 /* Now match the text part */
534 *p && !yaffsfs_IsPathDivider(*p) &&
535 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
536 if (yaffsfs_Match(*p, *leftOver)) {
545 if ((*p && !yaffsfs_IsPathDivider(*p)) ||
546 (*leftOver && !yaffsfs_IsPathDivider(*leftOver)))
549 while (yaffsfs_IsPathDivider(*p))
551 while (yaffsfs_IsPathDivider(*leftOver))
556 /* Skip over any /s in leftOver */
557 while (yaffsfs_IsPathDivider(*leftOver))
560 /*Skip over any /s in p */
561 while (yaffsfs_IsPathDivider(*p))
564 /* p should now be at the end of the string if fully matched */
568 if (matching && (thisMatchLength > longestMatch)) {
570 *restOfPath = (YCHAR *) leftOver;
572 longestMatch = thisMatchLength;
578 static int yaffsfs_CheckPath(const YCHAR *path)
583 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
584 if (yaffsfs_IsPathDivider(*path)) {
592 return (*path) ? -1 : 0;
595 /* FindMountPoint only returns a dev entry if the path is a mount point */
596 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
598 struct yaffs_dev *dev;
599 YCHAR *restOfPath = NULL;
601 dev = yaffsfs_FindDevice(path, &restOfPath);
602 if (dev && restOfPath && *restOfPath)
607 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
610 struct yaffs_dev *dev;
612 dev = yaffsfs_FindDevice(path, restOfPath);
613 if (dev && dev->is_mounted)
614 return dev->root_dir;
619 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
620 int symDepth, int *loop)
624 obj = yaffs_get_equivalent_obj(obj);
626 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
627 YCHAR *alias = obj->variant.symlink_variant.alias;
629 if (yaffsfs_IsPathDivider(*alias))
631 * Starts with a /, need to scan from root up */
632 /* NB Might not work if this is called with root != NULL
634 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
635 1, NULL, NULL, loop);
638 * Relative to here so use the parent of the
641 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
642 1, NULL, NULL, loop);
648 * yaffsfs_FindDirectory
649 * Parse a path to determine the directory and the name within the directory.
651 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
653 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
655 YCHAR **name, int symDepth,
656 int *notDir, int *loop)
658 struct yaffs_obj *dir;
660 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
663 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
671 restOfPath = (YCHAR *) path;
673 dir = yaffsfs_FindRoot(path, &restOfPath);
678 * curve ball: also throw away surplus '/'
679 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
681 while (yaffsfs_IsPathDivider(*restOfPath))
682 restOfPath++; /* get rid of '/' */
687 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
688 if (i < YAFFS_MAX_NAME_LENGTH) {
689 str[i] = *restOfPath;
697 /* got to the end of the string */
700 if (yaffs_strcmp(str, _Y(".")) == 0) {
702 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
705 dir = yaffs_find_by_name(dir, str);
707 dir = yaffsfs_FollowLink(dir, symDepth, loop);
709 if (dir && dir->variant_type !=
710 YAFFS_OBJECT_TYPE_DIRECTORY) {
719 /* directory did not exist. */
723 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
727 int *notDir, int *loop)
729 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
734 * yaffsfs_FindObject turns a path for an existing object into the object
736 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
737 const YCHAR *path, int symDepth,
739 struct yaffs_obj **dirOut,
740 int *notDir, int *loop)
742 struct yaffs_obj *dir;
743 struct yaffs_obj *obj;
747 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
752 /* At this stage we have looked up directory part and have the name part
753 * in name if there is one.
755 * eg /nand/x/ will give us a name of ""
756 * /nand/x will give us a name of "x"
758 * Since the name part might be "." or ".." which need to be fixed.
760 if (dir && (yaffs_strcmp(name, _Y("..")) == 0)) {
763 } else if (dir && (yaffs_strcmp(name, _Y(".")) == 0))
765 else if (dir && *name)
766 obj = yaffs_find_by_name(dir, name);
771 obj = yaffs_get_equivalent_obj(obj);
776 /*************************************************************************
777 * Start of yaffsfs visible functions.
778 *************************************************************************/
780 int yaffs_dup(int handle)
782 int newHandleNumber = -1;
783 struct yaffsfs_FileDes *existingFD = NULL;
784 struct yaffsfs_Handle *existingHandle = NULL;
785 struct yaffsfs_Handle *newHandle = NULL;
788 existingHandle = yaffsfs_HandleToPointer(handle);
789 existingFD = yaffsfs_HandleToFileDes(handle);
791 newHandleNumber = yaffsfs_NewHandle(&newHandle);
793 newHandle->fdId = existingHandle->fdId;
794 existingFD->handleCount++;
800 yaffsfs_SetError(-EBADF);
802 yaffsfs_SetError(-ENOMEM);
804 return newHandleNumber;
808 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
810 int current_objects = dev->n_obj - dev->n_deleted_files;
812 if (dev->param.max_objects && current_objects > dev->param.max_objects)
818 int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
819 int oflag, int mode, int sharing)
821 struct yaffs_obj *obj = NULL;
822 struct yaffs_obj *dir = NULL;
825 struct yaffsfs_FileDes *fd = NULL;
828 int errorReported = 0;
829 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
830 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
831 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
832 u8 sharedReadAllowed;
833 u8 sharedWriteAllowed;
841 yaffs_DIR *dsc = NULL;
843 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
844 yaffsfs_SetError(-EFAULT);
848 if (yaffsfs_CheckPath(path) < 0) {
849 yaffsfs_SetError(-ENAMETOOLONG);
853 /* O_EXCL only has meaning if O_CREAT is specified */
854 if (!(oflag & O_CREAT))
857 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
858 if ((oflag & O_CREAT) && (oflag & O_EXCL))
861 /* Todo: Are there any more flag combos to sanitise ? */
863 /* Figure out if reading or writing is requested */
865 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
866 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
870 handle = yaffsfs_NewHandleAndFileDes();
873 yaffsfs_SetError(-ENFILE);
874 errorReported = __LINE__;
877 fd = yaffsfs_HandleToFileDes(handle);
879 /* try to find the exisiting object */
880 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
882 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
885 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
886 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
891 /* The file already exists or it might be a directory */
892 is_dir = (obj->variant_type ==
893 YAFFS_OBJECT_TYPE_DIRECTORY);
896 * A directory can't be opened except for read, so we
906 dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
908 openDenied = __LINE__;
909 yaffsfs_SetError(-ENFILE);
910 errorReported = __LINE__;
914 /* Open should fail if O_CREAT and O_EXCL are specified
915 * for a file that exists.
917 if (!errorReported &&
918 (oflag & O_EXCL) && (oflag & O_CREAT)) {
919 openDenied = __LINE__;
920 yaffsfs_SetError(-EEXIST);
921 errorReported = __LINE__;
924 /* Check file permissions */
925 if (readRequested && !(obj->yst_mode & S_IRUSR))
926 openDenied = __LINE__;
928 if (writeRequested && !(obj->yst_mode & S_IWUSR))
929 openDenied = __LINE__;
931 if (!errorReported && writeRequested &&
932 obj->my_dev->read_only) {
933 openDenied = __LINE__;
934 yaffsfs_SetError(-EROFS);
935 errorReported = __LINE__;
938 if (openDenied && !errorReported) {
939 yaffsfs_SetError(-EACCES);
940 errorReported = __LINE__;
943 /* Check sharing of an existing object. */
945 struct yaffsfs_FileDes *fdx;
948 sharedReadAllowed = 1;
949 sharedWriteAllowed = 1;
952 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
953 fdx = &yaffsfs_fd[i];
954 if (fdx->handleCount > 0 &&
956 yaffsfs_inode[fdx->inodeId].iObj
959 sharedReadAllowed = 0;
960 if (!fdx->shareWrite)
961 sharedWriteAllowed = 0;
969 if ((!sharedReadAllowed && readRequested) ||
970 (!shareRead && alreadyReading) ||
971 (!sharedWriteAllowed && writeRequested) ||
972 (!shareWrite && alreadyWriting)) {
973 openDenied = __LINE__;
974 yaffsfs_SetError(-EBUSY);
975 errorReported = __LINE__;
981 /* If we could not open an existing object, then let's see if
982 * the directory exists. If not, error.
984 if (!obj && !errorReported) {
985 dir = yaffsfs_FindDirectory(reldir, path, &name, 0,
987 if (!dir && notDir) {
988 yaffsfs_SetError(-ENOTDIR);
989 errorReported = __LINE__;
991 yaffsfs_SetError(-ELOOP);
992 errorReported = __LINE__;
994 yaffsfs_SetError(-ENOENT);
995 errorReported = __LINE__;
999 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
1000 /* Let's see if we can create this file */
1001 if (dir->my_dev->read_only) {
1002 yaffsfs_SetError(-EROFS);
1003 errorReported = __LINE__;
1004 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
1005 yaffsfs_SetError(-ENFILE);
1006 errorReported = __LINE__;
1008 obj = yaffs_create_file(dir, name, mode, 0, 0);
1010 if (!obj && !errorReported) {
1011 yaffsfs_SetError(-ENOSPC);
1012 errorReported = __LINE__;
1016 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
1017 yaffsfs_SetError(-ENOENT);
1018 errorReported = __LINE__;
1021 if (obj && !openDenied) {
1022 int inodeId = yaffsfs_GetInodeIdForObject(obj);
1026 * Todo: Fix any problem if inodes run out,
1027 * That can't happen if the number of inode
1028 * items >= number of handles.
1032 fd->inodeId = inodeId;
1033 fd->reading = readRequested;
1034 fd->writing = writeRequested;
1035 fd->append = (oflag & O_APPEND) ? 1 : 0;
1036 fd->shareRead = shareRead;
1037 fd->shareWrite = shareWrite;
1045 /* Hook inode to object */
1046 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1048 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1049 yaffs_resize_file(obj, 0);
1052 yaffsfs_closedir_no_lock(dsc);
1054 yaffsfs_PutHandle(handle);
1056 yaffsfs_SetError(0); /* Problem */
1066 int yaffs_open_sharing_reldev(struct yaffs_dev *dev, const YCHAR *path, int oflag,
1067 int mode, int sharing)
1069 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path,
1070 oflag, mode, sharing);
1073 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
1075 return yaffs_open_sharing_reldir(NULL, path, oflag, mode, sharing);
1078 int yaffs_open_reldir(struct yaffs_obj *reldir,const YCHAR *path, int oflag, int mode)
1080 return yaffs_open_sharing_reldir(reldir, path, oflag, mode,
1081 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1084 int yaffs_open_reldev(struct yaffs_dev *dev,const YCHAR *path, int oflag, int mode)
1086 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path, oflag, mode,
1087 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1090 int yaffs_open(const YCHAR *path, int oflag, int mode)
1092 return yaffs_open_reldir(NULL, path, oflag, mode);
1095 static int yaffs_Dofsync(int handle, int datasync)
1098 struct yaffs_obj *obj;
1102 obj = yaffsfs_HandleToObject(handle);
1105 yaffsfs_SetError(-EBADF);
1106 else if (obj->my_dev->read_only)
1107 yaffsfs_SetError(-EROFS);
1109 yaffs_flush_file(obj, 1, datasync, 0);
1118 int yaffs_fsync(int handle)
1120 return yaffs_Dofsync(handle, 0);
1123 int yaffs_flush(int handle)
1125 return yaffs_fsync(handle);
1128 int yaffs_fdatasync(int handle)
1130 return yaffs_Dofsync(handle, 1);
1133 int yaffs_close(int handle)
1135 struct yaffsfs_Handle *h = NULL;
1136 struct yaffsfs_FileDes *f;
1137 struct yaffs_obj *obj = NULL;
1142 h = yaffsfs_HandleToPointer(handle);
1143 f = yaffsfs_HandleToFileDes(handle);
1144 obj = yaffsfs_HandleToObject(handle);
1146 if (!h || !obj || !f)
1147 yaffsfs_SetError(-EBADF);
1151 yaffs_flush_file(obj, 1, 0, 1);
1152 yaffsfs_PutHandle(handle);
1161 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1162 int isPread, Y_LOFF_T offset)
1164 struct yaffsfs_FileDes *fd = NULL;
1165 struct yaffs_obj *obj = NULL;
1167 Y_LOFF_T startPos = 0;
1168 Y_LOFF_T endPos = 0;
1173 u8 *buf = (u8 *) vbuf;
1175 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1176 yaffsfs_SetError(-EFAULT);
1181 fd = yaffsfs_HandleToFileDes(handle);
1182 obj = yaffsfs_HandleToObject(handle);
1186 yaffsfs_SetError(-EBADF);
1188 } else if (!fd->reading) {
1189 /* Not a reading handle */
1190 yaffsfs_SetError(-EINVAL);
1192 } else if (nbyte > YAFFS_MAX_RW_SIZE) {
1193 yaffsfs_SetError(-EINVAL);
1199 startPos = fd->v.position;
1203 if (yaffs_get_obj_length(obj) > pos)
1204 maxRead = yaffs_get_obj_length(obj) - pos;
1208 if (nbyte > maxRead)
1211 yaffsfs_GetHandle(handle);
1213 endPos = pos + nbyte;
1215 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1216 nbyte > YAFFS_MAX_RW_SIZE ||
1217 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1223 nToRead = YAFFSFS_RW_SIZE -
1224 (pos & (YAFFSFS_RW_SIZE - 1));
1225 if (nToRead > (int)nbyte)
1229 * Need to reverify object in case the device was
1230 * unmounted in another thread.
1232 obj = yaffsfs_HandleToObject(handle);
1236 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1244 if (nRead == nToRead)
1247 nbyte = 0; /* no more to read */
1256 yaffsfs_PutHandle(handle);
1260 fd->v.position = startPos + totalRead;
1262 yaffsfs_SetError(-EINVAL);
1269 return (totalRead >= 0) ? totalRead : -1;
1273 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1275 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1278 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1280 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1283 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1284 int isPwrite, Y_LOFF_T offset)
1286 struct yaffsfs_FileDes *fd = NULL;
1287 struct yaffs_obj *obj = NULL;
1289 Y_LOFF_T startPos = 0;
1292 int totalWritten = 0;
1293 int write_trhrough = 0;
1295 const u8 *buf = (const u8 *)vbuf;
1297 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1298 yaffsfs_SetError(-EFAULT);
1303 fd = yaffsfs_HandleToFileDes(handle);
1304 obj = yaffsfs_HandleToObject(handle);
1308 yaffsfs_SetError(-EBADF);
1310 } else if (!fd->writing) {
1311 yaffsfs_SetError(-EINVAL);
1313 } else if (obj->my_dev->read_only) {
1314 yaffsfs_SetError(-EROFS);
1318 startPos = yaffs_get_obj_length(obj);
1322 startPos = fd->v.position;
1324 yaffsfs_GetHandle(handle);
1326 endPos = pos + nbyte;
1328 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1329 nbyte > YAFFS_MAX_RW_SIZE ||
1330 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1337 nToWrite = YAFFSFS_RW_SIZE -
1338 (pos & (YAFFSFS_RW_SIZE - 1));
1339 if (nToWrite > (int)nbyte)
1343 * Need to reverify object in case the device was
1344 * remounted or unmounted in another thread.
1346 obj = yaffsfs_HandleToObject(handle);
1347 if (!obj || obj->my_dev->read_only)
1351 yaffs_wr_file(obj, buf, pos, nToWrite,
1354 totalWritten += nWritten;
1359 if (nWritten == nToWrite)
1364 if (nWritten < 1 && totalWritten < 1) {
1365 yaffsfs_SetError(-ENOSPC);
1375 yaffsfs_PutHandle(handle);
1378 if (totalWritten > 0)
1379 fd->v.position = startPos + totalWritten;
1381 yaffsfs_SetError(-EINVAL);
1387 return (totalWritten >= 0) ? totalWritten : -1;
1390 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1392 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1395 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1397 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1400 int yaffs_truncate_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1403 struct yaffs_obj *obj = NULL;
1404 struct yaffs_obj *dir = NULL;
1405 int result = YAFFS_FAIL;
1409 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1410 yaffsfs_SetError(-EFAULT);
1414 if (yaffsfs_CheckPath(path) < 0) {
1415 yaffsfs_SetError(-ENAMETOOLONG);
1421 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1422 obj = yaffsfs_FollowLink(obj, 0, &loop);
1425 yaffsfs_SetError(-ENOTDIR);
1427 yaffsfs_SetError(-ELOOP);
1428 else if (!dir || !obj)
1429 yaffsfs_SetError(-ENOENT);
1430 else if (obj->my_dev->read_only)
1431 yaffsfs_SetError(-EROFS);
1432 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1433 yaffsfs_SetError(-EISDIR);
1434 else if (obj->my_dev->read_only)
1435 yaffsfs_SetError(-EROFS);
1436 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1437 yaffsfs_SetError(-EINVAL);
1439 result = yaffs_resize_file(obj, new_size);
1443 return (result) ? 0 : -1;
1446 int yaffs_truncate_reldev(struct yaffs_dev *dev, const YCHAR *path,
1449 return yaffs_truncate_reldir(ROOT_DIR(dev), path, new_size);
1452 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1454 return yaffs_truncate_reldir(NULL, path, new_size);
1457 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1459 struct yaffsfs_FileDes *fd = NULL;
1460 struct yaffs_obj *obj = NULL;
1464 fd = yaffsfs_HandleToFileDes(handle);
1465 obj = yaffsfs_HandleToObject(handle);
1469 yaffsfs_SetError(-EBADF);
1470 else if (!fd->writing)
1471 yaffsfs_SetError(-EINVAL);
1472 else if (obj->my_dev->read_only)
1473 yaffsfs_SetError(-EROFS);
1474 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1475 yaffsfs_SetError(-EINVAL);
1477 /* resize the file */
1478 result = yaffs_resize_file(obj, new_size);
1481 return (result) ? 0 : -1;
1485 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1487 struct yaffsfs_FileDes *fd = NULL;
1488 struct yaffs_obj *obj = NULL;
1490 Y_LOFF_T fSize = -1;
1493 fd = yaffsfs_HandleToFileDes(handle);
1494 obj = yaffsfs_HandleToObject(handle);
1497 yaffsfs_SetError(-EBADF);
1498 else if (offset > YAFFS_MAX_FILE_SIZE)
1499 yaffsfs_SetError(-EINVAL);
1501 if (whence == SEEK_SET) {
1504 } else if (whence == SEEK_CUR) {
1505 if ((fd->v.position + offset) >= 0)
1506 pos = (fd->v.position + offset);
1507 } else if (whence == SEEK_END) {
1508 fSize = yaffs_get_obj_length(obj);
1509 if (fSize >= 0 && (fSize + offset) >= 0)
1510 pos = fSize + offset;
1513 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1514 fd->v.position = pos;
1516 yaffsfs_SetError(-EINVAL);
1526 static int yaffsfs_DoUnlink_reldir(struct yaffs_obj *reldir,
1527 const YCHAR *path, int isDirectory)
1529 struct yaffs_obj *dir = NULL;
1530 struct yaffs_obj *obj = NULL;
1532 int result = YAFFS_FAIL;
1536 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1537 yaffsfs_SetError(-EFAULT);
1541 if (yaffsfs_CheckPath(path) < 0) {
1542 yaffsfs_SetError(-ENAMETOOLONG);
1548 obj = yaffsfs_FindObject(reldir, path, 0, 0, NULL, NULL, NULL);
1549 dir = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
1552 yaffsfs_SetError(-ENOTDIR);
1554 yaffsfs_SetError(-ELOOP);
1556 yaffsfs_SetError(-ENOENT);
1557 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1558 yaffsfs_SetError(-EINVAL);
1560 yaffsfs_SetError(-ENOENT);
1561 else if (obj->my_dev->read_only)
1562 yaffsfs_SetError(-EROFS);
1563 else if (!isDirectory &&
1564 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1565 yaffsfs_SetError(-EISDIR);
1566 else if (isDirectory &&
1567 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1568 yaffsfs_SetError(-ENOTDIR);
1569 else if (isDirectory && obj == obj->my_dev->root_dir)
1570 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1572 result = yaffs_unlinker(dir, name);
1574 if (result == YAFFS_FAIL && isDirectory)
1575 yaffsfs_SetError(-ENOTEMPTY);
1580 return (result == YAFFS_FAIL) ? -1 : 0;
1583 int yaffs_unlink_reldir(struct yaffs_obj *reldir, const YCHAR *path)
1585 return yaffsfs_DoUnlink_reldir(reldir, path, 0);
1588 int yaffs_unlink_reldev(struct yaffs_dev *dev, const YCHAR *path)
1590 return yaffsfs_DoUnlink_reldir(ROOT_DIR(dev), path, 0);
1593 int yaffs_unlink(const YCHAR *path)
1595 return yaffs_unlink_reldir(NULL, path);
1598 int yaffs_funlink(int fd)
1600 struct yaffs_obj *obj;
1604 obj = yaffsfs_HandleToObject(fd);
1607 yaffsfs_SetError(-EBADF);
1608 else if (obj->my_dev->read_only)
1609 yaffsfs_SetError(-EROFS);
1610 else if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1611 !(list_empty(&obj->variant.dir_variant.children)))
1612 yaffsfs_SetError(-ENOTEMPTY);
1613 else if (obj == obj->my_dev->root_dir)
1614 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1615 else if (yaffs_unlink_obj(obj) == YAFFS_OK)
1623 int yaffs_fgetfl(int fd, int *flags)
1625 struct yaffsfs_FileDes *fdp = yaffsfs_HandleToFileDes(fd);
1630 if(!flags || !fdp) {
1631 yaffsfs_SetError(-EINVAL);
1634 if (fdp->reading && fdp->writing)
1636 else if (fdp->writing)
1648 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1650 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1651 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1657 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1659 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1660 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1666 int yaffs_rename_reldir(struct yaffs_obj *reldir,
1667 const YCHAR *oldPath, const YCHAR *newPath)
1669 struct yaffs_obj *olddir = NULL;
1670 struct yaffs_obj *newdir = NULL;
1671 struct yaffs_obj *obj = NULL;
1672 struct yaffs_obj *newobj = NULL;
1675 int result = YAFFS_FAIL;
1676 int rename_allowed = 1;
1682 YCHAR *alt_newpath = NULL;
1684 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1685 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1686 yaffsfs_SetError(-EFAULT);
1690 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1691 yaffsfs_SetError(-ENAMETOOLONG);
1695 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1696 yaffsfs_SetError(-ENOMEM);
1700 newPath = alt_newpath;
1704 olddir = yaffsfs_FindDirectory(reldir, oldPath, &oldname, 0,
1705 ¬OldDir, &oldLoop);
1706 newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
1707 ¬NewDir, &newLoop);
1708 obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
1709 newobj = yaffsfs_FindObject(reldir, newPath, 0, 0, NULL, NULL, NULL);
1711 /* If the object being renamed is a directory and the
1712 * path ended with a "/" then the olddir == obj.
1713 * We pass through NULL for the old name to tell the lower layers
1714 * to use olddir as the object.
1720 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1721 yaffsfs_SetError(-ENOTDIR);
1723 } else if (oldLoop || newLoop) {
1724 yaffsfs_SetError(-ELOOP);
1726 } else if (olddir && oldname &&
1727 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1728 yaffsfs_SetError(-EINVAL);
1730 } else if (!olddir || !newdir || !obj) {
1731 yaffsfs_SetError(-ENOENT);
1733 } else if (obj->my_dev->read_only) {
1734 yaffsfs_SetError(-EROFS);
1736 } else if (rename_file_over_dir(obj, newobj)) {
1737 yaffsfs_SetError(-EISDIR);
1739 } else if (rename_dir_over_file(obj, newobj)) {
1740 yaffsfs_SetError(-ENOTDIR);
1742 } else if (yaffs_is_non_empty_dir(newobj)) {
1743 yaffsfs_SetError(-ENOTEMPTY);
1745 } else if (olddir->my_dev != newdir->my_dev) {
1746 /* Rename must be on same device */
1747 yaffsfs_SetError(-EXDEV);
1749 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1751 * It is a directory, check that it is not being renamed to
1752 * being its own decendent.
1753 * Do this by tracing from the new directory back to the root,
1757 struct yaffs_obj *xx = newdir;
1759 while (rename_allowed && xx) {
1764 if (!rename_allowed)
1765 yaffsfs_SetError(-EINVAL);
1769 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1775 return (result == YAFFS_FAIL) ? -1 : 0;
1778 int yaffs_rename_reldev(struct yaffs_dev *dev, const YCHAR *oldPath,
1779 const YCHAR *newPath)
1781 return yaffs_rename_reldir(ROOT_DIR(dev), oldPath, newPath);
1783 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1785 return yaffs_rename_reldir(NULL, oldPath, newPath);
1788 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1792 obj = yaffs_get_equivalent_obj(obj);
1796 buf->st_ino = obj->obj_id;
1797 buf->st_mode = obj->yst_mode & ~S_IFMT;
1799 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1800 buf->st_mode |= S_IFDIR;
1801 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1802 buf->st_mode |= S_IFLNK;
1803 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1804 buf->st_mode |= S_IFREG;
1806 buf->st_nlink = yaffs_get_obj_link_count(obj);
1809 buf->st_rdev = obj->yst_rdev;
1810 buf->st_size = yaffs_get_obj_length(obj);
1811 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1812 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1814 #if CONFIG_YAFFS_WINCE
1815 buf->yst_wince_atime[0] = obj->win_atime[0];
1816 buf->yst_wince_atime[1] = obj->win_atime[1];
1817 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1818 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1819 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1820 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1822 buf->yst_atime = obj->yst_atime;
1823 buf->yst_ctime = obj->yst_ctime;
1824 buf->yst_mtime = obj->yst_mtime;
1831 static int yaffsfs_DoStatOrLStat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1832 struct yaffs_stat *buf, int doLStat)
1834 struct yaffs_obj *obj = NULL;
1835 struct yaffs_obj *dir = NULL;
1840 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1841 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1842 yaffsfs_SetError(-EFAULT);
1846 if (yaffsfs_CheckPath(path) < 0) {
1847 yaffsfs_SetError(-ENAMETOOLONG);
1853 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1855 if (!doLStat && obj)
1856 obj = yaffsfs_FollowLink(obj, 0, &loop);
1859 yaffsfs_SetError(-ENOTDIR);
1861 yaffsfs_SetError(-ELOOP);
1862 else if (!dir || !obj)
1863 yaffsfs_SetError(-ENOENT);
1865 retVal = yaffsfs_DoStat(obj, buf);
1873 int yaffs_stat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1874 struct yaffs_stat *buf)
1876 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 0);
1879 int yaffs_lstat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1880 struct yaffs_stat *buf)
1882 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 1);
1885 int yaffs_stat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1886 struct yaffs_stat *buf)
1888 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 0);
1891 int yaffs_lstat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1892 struct yaffs_stat *buf)
1894 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 1);
1897 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1899 return yaffs_stat_reldir(NULL, path, buf);
1902 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1904 return yaffs_lstat_reldir(NULL, path, buf);
1907 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1909 struct yaffs_obj *obj;
1913 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1914 yaffsfs_SetError(-EFAULT);
1919 obj = yaffsfs_HandleToObject(fd);
1922 retVal = yaffsfs_DoStat(obj, buf);
1925 yaffsfs_SetError(-EBADF);
1932 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1933 const struct yaffs_utimbuf *buf)
1936 struct yaffs_utimbuf local;
1938 obj = yaffs_get_equivalent_obj(obj);
1940 if (obj && obj->my_dev->read_only) {
1941 yaffsfs_SetError(-EROFS);
1945 #if !CONFIG_YAFFS_WINCE
1946 // if the the buffer is null then create one with the fields set to the current time.
1948 local.actime = Y_CURRENT_TIME;
1949 local.modtime = local.actime;
1953 // copy the buffer's time into the obj.
1957 obj->yst_atime = buf->actime;
1958 obj->yst_mtime = buf->modtime;
1960 // set the obj to dirty to cause it to be written to flash during the next flush operation.
1962 result = yaffs_flush_file(obj, 0, 0, 0);
1963 retVal = result == YAFFS_OK ? 0 : -1;
1970 int yaffs_utime_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1971 const struct yaffs_utimbuf *buf)
1973 struct yaffs_obj *obj = NULL;
1974 struct yaffs_obj *dir = NULL;
1980 yaffsfs_SetError(-EFAULT);
1984 if (yaffsfs_CheckPath(path) < 0) {
1985 yaffsfs_SetError(-ENAMETOOLONG);
1991 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1994 yaffsfs_SetError(-ENOTDIR);
1996 yaffsfs_SetError(-ELOOP);
1997 else if (!dir || !obj)
1998 yaffsfs_SetError(-ENOENT);
2000 retVal = yaffsfs_DoUtime(obj, buf);
2008 int yaffs_utime_reldev(struct yaffs_dev *dev, const YCHAR *path,
2009 const struct yaffs_utimbuf *buf)
2011 return yaffs_utime_reldir(ROOT_DIR(dev), path, buf);
2014 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
2016 return yaffs_utime_reldir(NULL, path, buf);
2019 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
2021 struct yaffs_obj *obj;
2026 obj = yaffsfs_HandleToObject(fd);
2029 retVal = yaffsfs_DoUtime(obj, buf);
2032 yaffsfs_SetError(-EBADF);
2039 #ifndef CONFIG_YAFFS_WINCE
2040 /* xattrib functions */
2042 static int yaffs_do_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2043 const char *name, const void *data, int size,
2044 int flags, int follow)
2046 struct yaffs_obj *obj;
2047 struct yaffs_obj *dir;
2053 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2054 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2055 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2056 yaffsfs_SetError(-EFAULT);
2060 if (yaffsfs_CheckPath(path) < 0) {
2061 yaffsfs_SetError(-ENAMETOOLONG);
2067 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2070 obj = yaffsfs_FollowLink(obj, 0, &loop);
2073 yaffsfs_SetError(-ENOTDIR);
2075 yaffsfs_SetError(-ELOOP);
2076 else if (!dir || !obj)
2077 yaffsfs_SetError(-ENOENT);
2079 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2081 yaffsfs_SetError(retVal);
2092 int yaffs_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2093 const char *name, const void *data, int size, int flags)
2095 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 1);
2098 int yaffs_setxattr_reldev(struct yaffs_dev *dev, const YCHAR *path,
2099 const char *name, const void *data, int size, int flags)
2101 return yaffs_setxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2104 int yaffs_setxattr(const YCHAR *path, const char *name,
2105 const void *data, int size, int flags)
2107 return yaffs_setxattr_reldir(NULL, path, name, data, size, flags);
2110 int yaffs_lsetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path, const char *name,
2111 const void *data, int size, int flags)
2113 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 0);
2116 int yaffs_lsetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name,
2117 const void *data, int size, int flags)
2119 return yaffs_lsetxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2122 int yaffs_lsetxattr(const YCHAR *path, const char *name,
2123 const void *data, int size, int flags)
2125 return yaffs_lsetxattr_reldir(NULL, path, name, data, size, flags);
2128 int yaffs_fsetxattr(int fd, const char *name,
2129 const void *data, int size, int flags)
2131 struct yaffs_obj *obj;
2135 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2136 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2137 yaffsfs_SetError(-EFAULT);
2142 obj = yaffsfs_HandleToObject(fd);
2145 yaffsfs_SetError(-EBADF);
2147 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2149 yaffsfs_SetError(retVal);
2159 static int yaffs_do_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2160 const char *name, void *data, int size, int follow)
2162 struct yaffs_obj *obj;
2163 struct yaffs_obj *dir;
2168 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2169 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2170 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2171 yaffsfs_SetError(-EFAULT);
2175 if (yaffsfs_CheckPath(path) < 0) {
2176 yaffsfs_SetError(-ENAMETOOLONG);
2182 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2185 obj = yaffsfs_FollowLink(obj, 0, &loop);
2188 yaffsfs_SetError(-ENOTDIR);
2190 yaffsfs_SetError(-ELOOP);
2191 else if (!dir || !obj)
2192 yaffsfs_SetError(-ENOENT);
2194 retVal = yaffs_get_xattrib(obj, name, data, size);
2196 yaffsfs_SetError(retVal);
2206 int yaffs_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2207 const char *name, void *data, int size)
2209 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 1);
2212 int yaffs_getxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2214 return yaffs_getxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2217 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
2219 return yaffs_getxattr_reldir(NULL, path, name, data, size);
2222 int yaffs_lgetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2223 const char *name, void *data, int size)
2225 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 0);
2228 int yaffs_lgetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2230 return yaffs_lgetxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2233 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2235 return yaffs_lgetxattr_reldir(NULL, path, name, data, size);
2238 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2240 struct yaffs_obj *obj;
2244 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2245 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2246 yaffsfs_SetError(-EFAULT);
2251 obj = yaffsfs_HandleToObject(fd);
2254 retVal = yaffs_get_xattrib(obj, name, data, size);
2256 yaffsfs_SetError(retVal);
2261 yaffsfs_SetError(-EBADF);
2268 static int yaffs_do_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2269 char *data, int size, int follow)
2271 struct yaffs_obj *obj = NULL;
2272 struct yaffs_obj *dir = NULL;
2277 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2278 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2279 yaffsfs_SetError(-EFAULT);
2283 if (yaffsfs_CheckPath(path) < 0) {
2284 yaffsfs_SetError(-ENAMETOOLONG);
2290 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2293 obj = yaffsfs_FollowLink(obj, 0, &loop);
2296 yaffsfs_SetError(-ENOTDIR);
2298 yaffsfs_SetError(-ELOOP);
2299 else if (!dir || !obj)
2300 yaffsfs_SetError(-ENOENT);
2302 retVal = yaffs_list_xattrib(obj, data, size);
2304 yaffsfs_SetError(retVal);
2315 int yaffs_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2316 char *data, int size)
2318 return yaffs_do_listxattr_reldir(reldir, path, data, size, 1);
2321 int yaffs_listxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2323 return yaffs_listxattr_reldir(ROOT_DIR(dev), path, data, size);
2326 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2328 return yaffs_listxattr_reldir(NULL, path, data, size);
2331 int yaffs_llistxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2332 char *data, int size)
2334 return yaffs_do_listxattr_reldir(reldir, path, data, size, 0);
2337 int yaffs_llistxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2339 return yaffs_llistxattr_reldir(ROOT_DIR(dev), path, data, size);
2342 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2344 return yaffs_llistxattr_reldir(NULL, path, data, size);
2347 int yaffs_flistxattr(int fd, char *data, int size)
2349 struct yaffs_obj *obj;
2353 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2354 yaffsfs_SetError(-EFAULT);
2359 obj = yaffsfs_HandleToObject(fd);
2362 retVal = yaffs_list_xattrib(obj, data, size);
2364 yaffsfs_SetError(retVal);
2369 yaffsfs_SetError(-EBADF);
2376 static int yaffs_do_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2377 const char *name, int follow)
2379 struct yaffs_obj *obj = NULL;
2380 struct yaffs_obj *dir = NULL;
2385 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2386 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2387 yaffsfs_SetError(-EFAULT);
2391 if (yaffsfs_CheckPath(path) < 0) {
2392 yaffsfs_SetError(-ENAMETOOLONG);
2398 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2401 obj = yaffsfs_FollowLink(obj, 0, &loop);
2404 yaffsfs_SetError(-ENOTDIR);
2406 yaffsfs_SetError(-ELOOP);
2407 else if (!dir || !obj)
2408 yaffsfs_SetError(-ENOENT);
2410 retVal = yaffs_remove_xattrib(obj, name);
2412 yaffsfs_SetError(retVal);
2423 int yaffs_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2426 return yaffs_do_removexattr_reldir(reldir, path, name, 1);
2429 int yaffs_removexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2431 return yaffs_removexattr_reldir(ROOT_DIR(dev), path, name);
2434 int yaffs_removexattr(const YCHAR *path, const char *name)
2436 return yaffs_removexattr_reldir(NULL, path, name);
2439 int yaffs_lremovexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2442 return yaffs_do_removexattr_reldir(reldir, path, name, 0);
2445 int yaffs_lremovexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2447 return yaffs_lremovexattr_reldir(ROOT_DIR(dev), path, name);
2450 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2452 return yaffs_lremovexattr_reldir(NULL, path, name);
2455 int yaffs_fremovexattr(int fd, const char *name)
2457 struct yaffs_obj *obj;
2461 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2462 yaffsfs_SetError(-EFAULT);
2467 obj = yaffsfs_HandleToObject(fd);
2470 retVal = yaffs_remove_xattrib(obj, name);
2472 yaffsfs_SetError(retVal);
2477 yaffsfs_SetError(-EBADF);
2485 #ifdef CONFIG_YAFFS_WINCE
2486 int yaffs_get_wince_times(int fd, unsigned *wctime,
2487 unsigned *watime, unsigned *wmtime)
2489 struct yaffs_obj *obj;
2494 obj = yaffsfs_HandleToObject(fd);
2499 wctime[0] = obj->win_ctime[0];
2500 wctime[1] = obj->win_ctime[1];
2503 watime[0] = obj->win_atime[0];
2504 watime[1] = obj->win_atime[1];
2507 wmtime[0] = obj->win_mtime[0];
2508 wmtime[1] = obj->win_mtime[1];
2514 yaffsfs_SetError(-EBADF);
2521 int yaffs_set_wince_times(int fd,
2522 const unsigned *wctime,
2523 const unsigned *watime, const unsigned *wmtime)
2525 struct yaffs_obj *obj;
2530 obj = yaffsfs_HandleToObject(fd);
2535 obj->win_ctime[0] = wctime[0];
2536 obj->win_ctime[1] = wctime[1];
2539 obj->win_atime[0] = watime[0];
2540 obj->win_atime[1] = watime[1];
2543 obj->win_mtime[0] = wmtime[0];
2544 obj->win_mtime[1] = wmtime[1];
2548 result = yaffs_flush_file(obj, 0, 0, 0);
2552 yaffsfs_SetError(-EBADF);
2561 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2566 obj = yaffs_get_equivalent_obj(obj);
2569 obj->yst_mode = mode;
2571 result = yaffs_flush_file(obj, 0, 0, 0);
2574 return result == YAFFS_OK ? 0 : -1;
2577 int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
2579 struct yaffs_obj *obj = NULL;
2580 struct yaffs_obj *dir = NULL;
2585 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2586 yaffsfs_SetError(-EFAULT);
2590 if (yaffsfs_CheckPath(path) < 0) {
2591 yaffsfs_SetError(-ENAMETOOLONG);
2595 if (amode & ~(R_OK | W_OK | X_OK)) {
2596 yaffsfs_SetError(-EINVAL);
2602 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2603 obj = yaffsfs_FollowLink(obj, 0, &loop);
2606 yaffsfs_SetError(-ENOTDIR);
2608 yaffsfs_SetError(-ELOOP);
2609 else if (!dir || !obj)
2610 yaffsfs_SetError(-ENOENT);
2611 else if ((amode & W_OK) && obj->my_dev->read_only)
2612 yaffsfs_SetError(-EROFS);
2616 if ((amode & R_OK) && !(obj->yst_mode & S_IRUSR))
2618 if ((amode & W_OK) && !(obj->yst_mode & S_IWUSR))
2620 if ((amode & X_OK) && !(obj->yst_mode & S_IXUSR))
2624 yaffsfs_SetError(-EACCES);
2635 int yaffs_access_reldev(struct yaffs_dev *dev, const YCHAR *path, int amode)
2637 return yaffs_access_reldir(ROOT_DIR(dev), path, amode);
2640 int yaffs_access(const YCHAR *path, int amode)
2642 return yaffs_access_reldir(NULL, path, amode);
2645 int yaffs_chmod_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2647 struct yaffs_obj *obj = NULL;
2648 struct yaffs_obj *dir = NULL;
2653 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2654 yaffsfs_SetError(-EFAULT);
2658 if (yaffsfs_CheckPath(path) < 0) {
2659 yaffsfs_SetError(-ENAMETOOLONG);
2663 if (mode & ~(0777)) {
2664 yaffsfs_SetError(-EINVAL);
2670 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2671 obj = yaffsfs_FollowLink(obj, 0, &loop);
2674 yaffsfs_SetError(-ENOTDIR);
2676 yaffsfs_SetError(-ELOOP);
2677 else if (!dir || !obj)
2678 yaffsfs_SetError(-ENOENT);
2679 else if (obj->my_dev->read_only)
2680 yaffsfs_SetError(-EROFS);
2682 retVal = yaffsfs_DoChMod(obj, mode);
2690 int yaffs_chmod_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2692 return yaffs_chmod_reldir(ROOT_DIR(dev), path, mode);
2695 int yaffs_chmod(const YCHAR *path, mode_t mode)
2697 return yaffs_chmod_reldir(NULL, path, mode);
2700 int yaffs_fchmod(int fd, mode_t mode)
2702 struct yaffs_obj *obj;
2705 if (mode & ~(0777)) {
2706 yaffsfs_SetError(-EINVAL);
2711 obj = yaffsfs_HandleToObject(fd);
2714 yaffsfs_SetError(-EBADF);
2715 else if (obj->my_dev->read_only)
2716 yaffsfs_SetError(-EROFS);
2718 retVal = yaffsfs_DoChMod(obj, mode);
2725 int yaffs_mkdir_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2727 struct yaffs_obj *parent = NULL;
2728 struct yaffs_obj *dir = NULL;
2730 YCHAR *alt_path = NULL;
2735 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2736 yaffsfs_SetError(-EFAULT);
2740 if (yaffsfs_CheckPath(path) < 0) {
2741 yaffsfs_SetError(-ENAMETOOLONG);
2745 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2746 yaffsfs_SetError(-ENOMEM);
2753 parent = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
2754 if (!parent && notDir)
2755 yaffsfs_SetError(-ENOTDIR);
2757 yaffsfs_SetError(-ELOOP);
2759 yaffsfs_SetError(-ENOENT);
2760 else if (yaffsfs_TooManyObjects(parent->my_dev))
2761 yaffsfs_SetError(-ENFILE);
2762 else if (yaffs_strnlen(name, 5) == 0) {
2763 /* Trying to make the root itself */
2764 yaffsfs_SetError(-EEXIST);
2765 } else if (parent->my_dev->read_only)
2766 yaffsfs_SetError(-EROFS);
2768 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2771 else if (yaffs_find_by_name(parent, name))
2772 yaffsfs_SetError(-EEXIST); /* name exists */
2774 yaffsfs_SetError(-ENOSPC); /* assume no space */
2784 int yaffs_mkdir_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2786 return yaffs_mkdir_reldir(ROOT_DIR(dev), path, mode);
2789 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2791 return yaffs_mkdir_reldir(NULL, path, mode);
2794 int yaffs_rmdir_reldir(struct yaffs_obj *reldir, const YCHAR *path)
2799 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2800 yaffsfs_SetError(-EFAULT);
2804 if (yaffsfs_CheckPath(path) < 0) {
2805 yaffsfs_SetError(-ENAMETOOLONG);
2809 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2810 yaffsfs_SetError(-ENOMEM);
2815 result = yaffsfs_DoUnlink_reldir(reldir, path, 1);
2822 int yaffs_rmdir_reldev(struct yaffs_dev *dev, const YCHAR *path)
2824 return yaffs_rmdir_reldir(ROOT_DIR(dev), path);
2827 int yaffs_rmdir(const YCHAR *path)
2829 return yaffs_rmdir_reldir(NULL, path);
2833 * The mount/unmount/sync functions act on devices rather than reldirs.
2835 void *yaffs_getdev(const YCHAR *path)
2837 struct yaffs_dev *dev = NULL;
2839 dev = yaffsfs_FindDevice(path, &dummy);
2843 int yaffs_mount_common(struct yaffs_dev *dev, const YCHAR *path,
2844 int read_only, int skip_checkpt)
2847 int result = YAFFS_FAIL;
2850 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2851 yaffsfs_SetError(-EFAULT);
2855 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2857 if (yaffsfs_CheckPath(path) < 0) {
2858 yaffsfs_SetError(-ENAMETOOLONG);
2865 yaffsfs_InitHandles();
2868 dev = yaffsfs_FindMountPoint(path);
2871 if (!dev->is_mounted) {
2872 dev->read_only = read_only ? 1 : 0;
2874 u8 skip = dev->param.skip_checkpt_rd;
2875 dev->param.skip_checkpt_rd = 1;
2876 result = yaffs_guts_initialise(dev);
2877 dev->param.skip_checkpt_rd = skip;
2879 result = yaffs_guts_initialise(dev);
2882 if (result == YAFFS_FAIL)
2883 yaffsfs_SetError(-ENOMEM);
2884 retVal = result ? 0 : -1;
2887 yaffsfs_SetError(-EBUSY);
2889 yaffsfs_SetError(-ENODEV);
2896 int yaffs_mount3_reldev(struct yaffs_dev *dev, int read_only, int skip_checkpt)
2898 return yaffs_mount_common(dev, NULL, read_only, skip_checkpt);
2901 int yaffs_mount3(const YCHAR *path, int read_only, int skip_checkpt)
2903 return yaffs_mount_common(NULL, path, read_only, skip_checkpt);
2906 int yaffs_mount2_reldev(struct yaffs_dev *dev, int readonly)
2908 return yaffs_mount_common(dev, NULL, readonly, 0);
2911 int yaffs_mount2(const YCHAR *path, int readonly)
2913 return yaffs_mount_common(NULL, path, readonly, 0);
2916 int yaffs_mount_reldev(struct yaffs_dev *dev)
2918 return yaffs_mount_common(dev, NULL, 0, 0);
2921 int yaffs_mount(const YCHAR *path)
2923 return yaffs_mount_common(NULL, path, 0, 0);
2926 static int yaffs_sync_common(struct yaffs_dev *dev,
2934 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2935 yaffsfs_SetError(-EFAULT);
2939 if (yaffsfs_CheckPath(path) < 0) {
2940 yaffsfs_SetError(-ENAMETOOLONG);
2947 dev = yaffsfs_FindDevice(path, &dummy);
2950 if (!dev->is_mounted)
2951 yaffsfs_SetError(-EINVAL);
2952 else if (dev->read_only)
2953 yaffsfs_SetError(-EROFS);
2956 yaffs_flush_whole_cache(dev, 0);
2958 yaffs_checkpoint_save(dev);
2963 yaffsfs_SetError(-ENODEV);
2969 int yaffs_sync_files_reldev(struct yaffs_dev *dev)
2971 return yaffs_sync_common(dev, NULL, 0);
2974 int yaffs_sync_files(const YCHAR *path)
2976 return yaffs_sync_common(NULL, path, 0);
2979 int yaffs_sync_reldev(struct yaffs_dev *dev)
2981 return yaffs_sync_common(dev, NULL, 1);
2984 int yaffs_sync(const YCHAR *path)
2986 return yaffs_sync_common(NULL, path, 1);
2990 static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
2998 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2999 yaffsfs_SetError(-EFAULT);
3003 if (yaffsfs_CheckPath(path) < 0) {
3004 yaffsfs_SetError(-ENAMETOOLONG);
3011 dev = yaffsfs_FindDevice(path, &dummy);
3014 if (!dev->is_mounted)
3015 yaffsfs_SetError(-EINVAL);
3017 retVal = yaffs_bg_gc(dev, urgency);
3019 yaffsfs_SetError(-ENODEV);
3025 /* Background gc functions.
3026 * These return 0 when bg done or greater than 0 when gc has been
3027 * done and there is still a lot of garbage to be cleaned up.
3030 int yaffs_do_background_gc(const YCHAR *path, int urgency)
3032 return yaffsfs_bg_gc_common(NULL, path, urgency);
3035 int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
3037 return yaffsfs_bg_gc_common(dev, NULL, urgency);
3040 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
3043 struct yaffs_obj *obj;
3045 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
3046 obj = yaffsfs_HandleToObject(i);
3047 if (obj && obj->my_dev == dev)
3053 int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
3054 int force, int read_only)
3059 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3060 yaffsfs_SetError(-EFAULT);
3064 if (yaffsfs_CheckPath(path) < 0) {
3065 yaffsfs_SetError(-ENAMETOOLONG);
3071 dev = yaffsfs_FindMountPoint(path);
3074 if (dev->is_mounted) {
3075 yaffs_flush_whole_cache(dev, 0);
3077 if (force || !yaffsfs_IsDevBusy(dev)) {
3079 yaffs_checkpoint_save(dev);
3080 was_read_only = dev->read_only;
3081 dev->read_only = read_only ? 1 : 0;
3082 if (was_read_only && !read_only) {
3083 yaffs_guts_cleanup(dev);
3087 yaffsfs_SetError(-EBUSY);
3090 yaffsfs_SetError(-EINVAL);
3093 yaffsfs_SetError(-ENODEV);
3100 int yaffs_remount_reldev(struct yaffs_dev *dev, int force, int read_only)
3102 return yaffs_remount_common(dev, NULL, force, read_only);
3104 int yaffs_remount(const YCHAR *path, int force, int read_only)
3106 return yaffs_remount_common(NULL, path, force, read_only);
3109 int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
3115 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3116 yaffsfs_SetError(-EFAULT);
3120 if (yaffsfs_CheckPath(path) < 0) {
3121 yaffsfs_SetError(-ENAMETOOLONG);
3128 dev = yaffsfs_FindMountPoint(path);
3131 if (dev->is_mounted) {
3133 yaffs_flush_whole_cache(dev, 0);
3134 yaffs_checkpoint_save(dev);
3135 inUse = yaffsfs_IsDevBusy(dev);
3136 if (!inUse || force) {
3138 yaffsfs_BreakDeviceHandles(dev);
3139 yaffs_deinitialise(dev);
3143 yaffsfs_SetError(-EBUSY);
3146 yaffsfs_SetError(-EINVAL);
3149 yaffsfs_SetError(-ENODEV);
3156 int yaffs_unmount2_reldev(struct yaffs_dev *dev, int force)
3158 return yaffs_unmount2_common(dev, NULL, force);
3161 int yaffs_unmount2(const YCHAR *path, int force)
3163 return yaffs_unmount2_common(NULL, path, force);
3166 int yaffs_unmount_reldev(struct yaffs_dev *dev)
3168 return yaffs_unmount2_reldev(dev, 0);
3171 int yaffs_unmount(const YCHAR *path)
3173 return yaffs_unmount2(path, 0);
3176 int yaffs_format_common(struct yaffs_dev *dev,
3179 int force_unmount_flag,
3187 yaffsfs_SetError(-EFAULT);
3191 if (yaffsfs_CheckPath(path) < 0) {
3192 yaffsfs_SetError(-ENAMETOOLONG);
3199 dev = yaffsfs_FindMountPoint(path);
3202 int was_mounted = dev->is_mounted;
3204 if (dev->is_mounted && unmount_flag) {
3206 yaffs_flush_whole_cache(dev, 0);
3207 yaffs_checkpoint_save(dev);
3208 inUse = yaffsfs_IsDevBusy(dev);
3209 if (!inUse || force_unmount_flag) {
3211 yaffsfs_BreakDeviceHandles(dev);
3212 yaffs_deinitialise(dev);
3216 if(dev->is_mounted) {
3217 yaffsfs_SetError(-EBUSY);
3220 yaffs_guts_format_dev(dev);
3221 if(was_mounted && remount_flag) {
3222 result = yaffs_guts_initialise(dev);
3223 if (result == YAFFS_FAIL) {
3224 yaffsfs_SetError(-ENOMEM);
3230 yaffsfs_SetError(-ENODEV);
3239 int yaffs_format_reldev(struct yaffs_dev *dev,
3241 int force_unmount_flag,
3244 return yaffs_format_common(dev, NULL, unmount_flag,
3245 force_unmount_flag, remount_flag);
3248 int yaffs_format(const YCHAR *path,
3250 int force_unmount_flag,
3253 return yaffs_format_common(NULL, path, unmount_flag,
3254 force_unmount_flag, remount_flag);
3257 Y_LOFF_T yaffs_freespace_common(struct yaffs_dev *dev, const YCHAR *path)
3259 Y_LOFF_T retVal = -1;
3263 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3264 yaffsfs_SetError(-EFAULT);
3268 if (yaffsfs_CheckPath(path) < 0) {
3269 yaffsfs_SetError(-ENAMETOOLONG);
3276 dev = yaffsfs_FindDevice(path, &dummy);
3277 if (dev && dev->is_mounted) {
3278 retVal = yaffs_get_n_free_chunks(dev);
3279 retVal *= dev->data_bytes_per_chunk;
3282 yaffsfs_SetError(-EINVAL);
3288 Y_LOFF_T yaffs_freespace_reldev(struct yaffs_dev *dev)
3290 return yaffs_freespace_common(dev, NULL);
3293 Y_LOFF_T yaffs_freespace(const YCHAR *path)
3295 return yaffs_freespace_common(NULL, path);
3298 Y_LOFF_T yaffs_totalspace_common(struct yaffs_dev *dev, const YCHAR *path)
3300 Y_LOFF_T retVal = -1;
3304 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3305 yaffsfs_SetError(-EFAULT);
3309 if (yaffsfs_CheckPath(path) < 0) {
3310 yaffsfs_SetError(-ENAMETOOLONG);
3317 dev = yaffsfs_FindDevice(path, &dummy);
3318 if (dev && dev->is_mounted) {
3319 retVal = (dev->param.end_block - dev->param.start_block + 1) -
3320 dev->param.n_reserved_blocks;
3321 retVal *= dev->param.chunks_per_block;
3322 retVal *= dev->data_bytes_per_chunk;
3325 yaffsfs_SetError(-EINVAL);
3331 Y_LOFF_T yaffs_totalspace_reldev(struct yaffs_dev *dev)
3333 return yaffs_totalspace_common(dev, NULL);
3336 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
3338 return yaffs_totalspace_common(NULL, path);
3341 int yaffs_inodecount_common(struct yaffs_dev *dev, const YCHAR *path)
3343 Y_LOFF_T retVal = -1;
3347 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3348 yaffsfs_SetError(-EFAULT);
3352 if (yaffsfs_CheckPath(path) < 0) {
3353 yaffsfs_SetError(-ENAMETOOLONG);
3360 dev = yaffsfs_FindDevice(path, &dummy);
3361 if (dev && dev->is_mounted) {
3362 int n_obj = dev->n_obj;
3363 if (n_obj > dev->n_hardlinks)
3364 retVal = n_obj - dev->n_hardlinks;
3368 yaffsfs_SetError(-EINVAL);
3374 int yaffs_inodecount_reldev(struct yaffs_dev *dev)
3376 return yaffs_inodecount_common(dev, NULL);
3379 int yaffs_inodecount(const YCHAR *path)
3381 return yaffs_inodecount_common(NULL, path);
3384 void yaffs_add_device(struct yaffs_dev *dev)
3386 struct list_head *cfg;
3387 /* First check that the device is not in the list. */
3389 list_for_each(cfg, &yaffsfs_deviceList) {
3390 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
3394 dev->is_mounted = 0;
3395 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
3397 if (!dev->dev_list.next)
3398 INIT_LIST_HEAD(&dev->dev_list);
3400 list_add(&dev->dev_list, &yaffsfs_deviceList);
3405 void yaffs_remove_device(struct yaffs_dev *dev)
3407 list_del_init(&dev->dev_list);
3410 /* Functions to iterate through devices. NB Use with extreme care! */
3412 static struct list_head *dev_iterator;
3413 void yaffs_dev_rewind(void)
3415 dev_iterator = yaffsfs_deviceList.next;
3418 struct yaffs_dev *yaffs_next_dev(void)
3420 struct yaffs_dev *retval;
3424 if (dev_iterator == &yaffsfs_deviceList)
3427 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
3428 dev_iterator = dev_iterator->next;
3432 /* Directory search stuff. */
3434 static struct list_head search_contexts;
3436 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
3440 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3444 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
3445 dsc->nextReturn = NULL;
3448 list_entry(dsc->dirObj->variant.dir_variant.
3449 children.next, struct yaffs_obj,
3452 /* Hey someone isn't playing nice! */
3456 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
3460 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3462 if (dsc->nextReturn == NULL ||
3463 list_empty(&dsc->dirObj->variant.dir_variant.children))
3464 dsc->nextReturn = NULL;
3466 struct list_head *next = dsc->nextReturn->siblings.next;
3468 if (next == &dsc->dirObj->variant.dir_variant.children)
3469 dsc->nextReturn = NULL; /* end of list */
3471 dsc->nextReturn = list_entry(next,
3476 /* Hey someone isn't playing nice! */
3480 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
3483 struct list_head *i;
3484 struct yaffsfs_DirSearchContext *dsc;
3486 /* if search contexts not initilised then skip */
3487 if (!search_contexts.next)
3490 /* Iterate through the directory search contexts.
3491 * If any are the one being removed, then advance the dsc to
3492 * the next one to prevent a hanging ptr.
3494 list_for_each(i, &search_contexts) {
3496 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
3498 if (dsc->nextReturn == obj)
3499 yaffsfs_DirAdvance(dsc);
3505 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
3506 const YCHAR *dirname)
3508 yaffs_DIR *dir = NULL;
3509 struct yaffs_obj *obj = NULL;
3510 struct yaffsfs_DirSearchContext *dsc = NULL;
3514 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3515 yaffsfs_SetError(-EFAULT);
3519 if (yaffsfs_CheckPath(dirname) < 0) {
3520 yaffsfs_SetError(-ENAMETOOLONG);
3524 obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, ¬Dir, &loop);
3525 obj = yaffsfs_FollowLink(obj, 0, &loop);
3528 yaffsfs_SetError(-ENOTDIR);
3530 yaffsfs_SetError(-ELOOP);
3532 yaffsfs_SetError(-ENOENT);
3533 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3534 yaffsfs_SetError(-ENOTDIR);
3538 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3539 if (!yaffsfs_dsc[i].inUse)
3540 dsc = &yaffsfs_dsc[i];
3543 dir = (yaffs_DIR *) dsc;
3546 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3549 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3550 INIT_LIST_HEAD(&dsc->others);
3552 if (!search_contexts.next)
3553 INIT_LIST_HEAD(&search_contexts);
3555 list_add(&dsc->others, &search_contexts);
3556 yaffsfs_SetDirRewound(dsc);
3562 yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
3567 ret = yaffsfs_opendir_reldir_no_lock(reldir, dirname);
3572 yaffs_DIR *yaffs_opendir_reldev(struct yaffs_dev *dev, const YCHAR *dirname)
3574 return yaffs_opendir_reldir(ROOT_DIR(dev), dirname);
3577 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3579 return yaffs_opendir_reldir(NULL, dirname);
3582 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3584 struct yaffsfs_DirSearchContext *dsc;
3585 struct yaffs_dirent *retVal = NULL;
3587 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3590 if (dsc && dsc->inUse) {
3591 yaffsfs_SetError(0);
3592 if (dsc->nextReturn) {
3594 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3595 dsc->de.d_dont_use = 0;
3596 dsc->de.d_off = dsc->offset++;
3597 yaffs_get_obj_name(dsc->nextReturn,
3598 dsc->de.d_name, NAME_MAX);
3599 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3600 /* this should not happen! */
3601 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3603 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3605 yaffsfs_DirAdvance(dsc);
3609 yaffsfs_SetError(-EBADF);
3614 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3616 struct yaffs_dirent *ret;
3619 ret = yaffsfs_readdir_no_lock(dirp);
3624 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3626 struct yaffsfs_DirSearchContext *dsc;
3628 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3630 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3633 yaffsfs_SetDirRewound(dsc);
3637 void yaffs_rewinddir(yaffs_DIR *dirp)
3640 yaffsfs_rewinddir_no_lock(dirp);
3644 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3646 struct yaffs_dirent *ret = NULL;
3647 struct yaffsfs_FileDes *f;
3650 f = yaffsfs_HandleToFileDes(fd);
3651 if(f && f->isDir && f->v.dir)
3652 ret = yaffsfs_readdir_no_lock(f->v.dir);
3657 void yaffs_rewinddir_fd(int fd)
3659 struct yaffsfs_FileDes *f;
3662 f = yaffsfs_HandleToFileDes(fd);
3664 yaffsfs_rewinddir_no_lock(f->v.dir);
3669 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3671 struct yaffsfs_DirSearchContext *dsc;
3673 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3675 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3676 yaffsfs_SetError(-EFAULT);
3681 list_del(&dsc->others); /* unhook from list */
3686 int yaffs_closedir(yaffs_DIR *dirp)
3691 ret = yaffsfs_closedir_no_lock(dirp);
3696 /* End of directory stuff */
3698 int yaffs_symlink_reldir(struct yaffs_obj *reldir,
3699 const YCHAR *oldpath, const YCHAR *newpath)
3701 struct yaffs_obj *parent = NULL;
3702 struct yaffs_obj *obj;
3705 int mode = 0; /* ignore for now */
3709 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3710 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3711 yaffsfs_SetError(-EFAULT);
3715 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3716 yaffsfs_SetError(-ENAMETOOLONG);
3721 parent = yaffsfs_FindDirectory(reldir, newpath, &name, 0, ¬Dir, &loop);
3722 if (!parent && notDir)
3723 yaffsfs_SetError(-ENOTDIR);
3725 yaffsfs_SetError(-ELOOP);
3726 else if (!parent || yaffs_strnlen(name, 5) < 1)
3727 yaffsfs_SetError(-ENOENT);
3728 else if (yaffsfs_TooManyObjects(parent->my_dev))
3729 yaffsfs_SetError(-ENFILE);
3730 else if (parent->my_dev->read_only)
3731 yaffsfs_SetError(-EROFS);
3733 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3736 else if (yaffsfs_FindObject(reldir, newpath, 0, 0,
3738 yaffsfs_SetError(-EEXIST);
3740 yaffsfs_SetError(-ENOSPC);
3748 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3750 return yaffs_symlink_reldir(NULL, oldpath, newpath);
3753 int yaffs_readlink_reldir(struct yaffs_obj *reldir,const YCHAR *path,
3754 YCHAR *buf, int bufsiz)
3756 struct yaffs_obj *obj = NULL;
3757 struct yaffs_obj *dir = NULL;
3762 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3763 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3764 yaffsfs_SetError(-EFAULT);
3770 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
3773 yaffsfs_SetError(-ENOTDIR);
3775 yaffsfs_SetError(-ELOOP);
3776 else if (!dir || !obj)
3777 yaffsfs_SetError(-ENOENT);
3778 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3779 yaffsfs_SetError(-EINVAL);
3781 YCHAR *alias = obj->variant.symlink_variant.alias;
3782 memset(buf, 0, bufsiz);
3783 yaffs_strncpy(buf, alias, bufsiz - 1);
3789 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3791 return yaffs_readlink_reldir(NULL, path, buf, bufsiz);
3794 int yaffs_link_reldir(struct yaffs_obj *reldir,
3795 const YCHAR *oldpath, const YCHAR *linkpath)
3797 /* Creates a link called newpath to existing oldpath */
3798 struct yaffs_obj *obj = NULL;
3799 struct yaffs_obj *lnk = NULL;
3800 struct yaffs_obj *obj_dir = NULL;
3801 struct yaffs_obj *lnk_dir = NULL;
3809 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3810 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3811 yaffsfs_SetError(-EFAULT);
3815 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3816 yaffsfs_SetError(-ENAMETOOLONG);
3822 obj = yaffsfs_FindObject(reldir, oldpath, 0, 1,
3823 &obj_dir, ¬DirObj, &objLoop);
3824 lnk = yaffsfs_FindObject(reldir, linkpath, 0, 0, NULL, NULL, NULL);
3825 lnk_dir = yaffsfs_FindDirectory(reldir, linkpath, &newname,
3826 0, ¬DirLnk, &lnkLoop);
3828 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3829 yaffsfs_SetError(-ENOTDIR);
3830 else if (objLoop || lnkLoop)
3831 yaffsfs_SetError(-ELOOP);
3832 else if (!obj_dir || !lnk_dir || !obj)
3833 yaffsfs_SetError(-ENOENT);
3834 else if (obj->my_dev->read_only)
3835 yaffsfs_SetError(-EROFS);
3836 else if (yaffsfs_TooManyObjects(obj->my_dev))
3837 yaffsfs_SetError(-ENFILE);
3839 yaffsfs_SetError(-EEXIST);
3840 else if (lnk_dir->my_dev != obj->my_dev)
3841 yaffsfs_SetError(-EXDEV);
3843 retVal = yaffsfs_CheckNameLength(newname);
3846 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3850 yaffsfs_SetError(-ENOSPC);
3859 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3861 return yaffs_link_reldir(NULL, oldpath, linkpath);
3864 int yaffs_mknod_reldir(struct yaffs_obj *reldir, const YCHAR *pathname,
3865 mode_t mode, dev_t dev_val)
3872 yaffsfs_SetError(-EINVAL);
3876 int yaffs_mknod_reldev(struct yaffs_dev *dev, const YCHAR *pathname, mode_t mode, dev_t dev_val)
3878 return yaffs_mknod_reldir(ROOT_DIR(dev), pathname, mode, dev_val);
3881 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev_val)
3883 return yaffs_mknod_reldir(NULL, pathname, mode, dev_val);
3887 * D E B U G F U N C T I O N S
3892 * Returns number of handles attached to the object
3894 int yaffs_n_handles_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3896 struct yaffs_obj *obj;
3898 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3899 yaffsfs_SetError(-EFAULT);
3903 if (yaffsfs_CheckPath(path) < 0) {
3904 yaffsfs_SetError(-ENAMETOOLONG);
3908 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
3911 return yaffsfs_CountHandles(obj);
3916 int yaffs_n_handles(const YCHAR *path)
3918 return yaffs_n_handles_reldir(NULL, path);
3921 int yaffs_get_error(void)
3923 return yaffsfs_GetLastError();
3926 int yaffs_set_error(int error)
3928 yaffsfs_SetError(error);
3932 struct yaffs_obj * yaffs_get_obj_from_fd(int handle)
3934 return yaffsfs_HandleToObject(handle);
3937 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3947 reldir = yaffsfs_FindRoot(path, &rest);
3950 struct yaffs_dev *dev = reldir->my_dev;
3953 "n_page_writes.......... %d\n"
3954 "n_page_reads........... %d\n"
3955 "n_erasures....... %d\n"
3956 "n_gc_copies............ %d\n"
3957 "garbageCollections... %d\n"
3958 "passiveGarbageColl'ns %d\n"
3964 dev->garbageCollections, dev->passiveGarbageCollections);
3971 int yaffs_dump_dev(const YCHAR *path)
3973 return yaffs_dump_dev_reldir(NULL, path);