2 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
4 * Copyright (C) 2002-2011 Aleph One Ltd.
5 * for Toby Churchill Ltd and Brightstar Engineering
7 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 #include "yaffs_guts.h"
18 #include "yaffs_trace.h"
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);
351 if (fd->inodeId >= 0) {
352 yaffsfs_PutInode(fd->inodeId);
360 static int yaffsfs_PutHandle(int handle)
362 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
364 if (h && h->useCount > 0) {
366 if (h->useCount < 1) {
367 yaffsfs_PutFileDes(h->fdId);
375 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
377 struct yaffsfs_FileDes *fd;
378 struct yaffsfs_Handle *h;
379 struct yaffs_obj *obj;
381 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
382 h = yaffsfs_HandleToPointer(i);
383 fd = yaffsfs_HandleToFileDes(i);
384 obj = yaffsfs_HandleToObject(i);
385 if (h && h->useCount > 0) {
389 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
391 yaffsfs_PutInode(fd->inodeId);
398 * Stuff to handle names.
400 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
401 #ifndef CONFIG_YAFFS_WINCE
402 static int yaffs_toupper(YCHAR a)
404 if (a >= 'a' && a <= 'z')
405 return (a - 'a') + 'A';
411 static int yaffsfs_Match(YCHAR a, YCHAR b)
413 return (yaffs_toupper(a) == yaffs_toupper(b));
416 static int yaffsfs_Match(YCHAR a, YCHAR b)
423 static int yaffsfs_IsPathDivider(YCHAR ch)
425 const YCHAR *str = YAFFS_PATH_DIVIDERS;
436 static int yaffsfs_CheckNameLength(const YCHAR *name)
440 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
442 if (nameLength == 0) {
443 yaffsfs_SetError(-ENOENT);
445 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
446 yaffsfs_SetError(-ENAMETOOLONG);
453 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
455 YCHAR *alt_path = NULL;
460 * We don't have a definition for max path length.
461 * We will use 3 * max name length instead.
464 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
466 /* If the last character is a path divider, then we need to
467 * trim it back so that the name look-up works properly.
468 * eg. /foo/new_dir/ -> /foo/newdir
469 * Curveball: Need to handle multiple path dividers:
470 * eg. /foof/sdfse///// -> /foo/sdfse
472 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
473 alt_path = kmalloc(path_length + 1, 0);
476 yaffs_strcpy(alt_path, path);
477 for (i = path_length - 1;
478 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
479 alt_path[i] = (YCHAR) 0;
481 *ret_path = alt_path;
485 LIST_HEAD(yaffsfs_deviceList);
490 * Scan the configuration list to find the device
491 * Curveballs: Should match paths that end in '/' too
492 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
494 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
497 struct list_head *cfg;
498 const YCHAR *leftOver;
500 struct yaffs_dev *retval = NULL;
501 struct yaffs_dev *dev = NULL;
503 int longestMatch = -1;
507 * Check all configs, choose the one that:
508 * 1) Actually matches a prefix (ie /a amd /abc will not match
509 * 2) Matches the longest.
511 list_for_each(cfg, &yaffsfs_deviceList) {
512 dev = list_entry(cfg, struct yaffs_dev, dev_list);
522 /* Skip over any leading /s */
523 while (yaffsfs_IsPathDivider(*p))
525 while (yaffsfs_IsPathDivider(*leftOver))
528 while (matching && *p && *leftOver) {
530 /* Now match the text part */
532 *p && !yaffsfs_IsPathDivider(*p) &&
533 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
534 if (yaffsfs_Match(*p, *leftOver)) {
543 if ((*p && !yaffsfs_IsPathDivider(*p)) ||
544 (*leftOver && !yaffsfs_IsPathDivider(*leftOver)))
547 while (yaffsfs_IsPathDivider(*p))
549 while (yaffsfs_IsPathDivider(*leftOver))
554 /* Skip over any /s in leftOver */
555 while (yaffsfs_IsPathDivider(*leftOver))
558 /*Skip over any /s in p */
559 while (yaffsfs_IsPathDivider(*p))
562 /* p should now be at the end of the string if fully matched */
566 if (matching && (thisMatchLength > longestMatch)) {
568 *restOfPath = (YCHAR *) leftOver;
570 longestMatch = thisMatchLength;
576 static int yaffsfs_CheckPath(const YCHAR *path)
581 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
582 if (yaffsfs_IsPathDivider(*path)) {
590 return (*path) ? -1 : 0;
593 /* FindMountPoint only returns a dev entry if the path is a mount point */
594 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
596 struct yaffs_dev *dev;
597 YCHAR *restOfPath = NULL;
599 dev = yaffsfs_FindDevice(path, &restOfPath);
600 if (dev && restOfPath && *restOfPath)
605 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
608 struct yaffs_dev *dev;
610 dev = yaffsfs_FindDevice(path, restOfPath);
611 if (dev && dev->is_mounted)
612 return dev->root_dir;
617 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
618 int symDepth, int *loop)
622 obj = yaffs_get_equivalent_obj(obj);
624 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
625 YCHAR *alias = obj->variant.symlink_variant.alias;
627 if (yaffsfs_IsPathDivider(*alias))
629 * Starts with a /, need to scan from root up */
630 /* NB Might not work if this is called with root != NULL
632 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
633 1, NULL, NULL, loop);
636 * Relative to here so use the parent of the
639 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
640 1, NULL, NULL, loop);
646 * yaffsfs_FindDirectory
647 * Parse a path to determine the directory and the name within the directory.
649 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
651 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
653 YCHAR **name, int symDepth,
654 int *notDir, int *loop)
656 struct yaffs_obj *dir;
658 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
661 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
669 restOfPath = (YCHAR *) path;
671 dir = yaffsfs_FindRoot(path, &restOfPath);
676 * curve ball: also throw away surplus '/'
677 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
679 while (yaffsfs_IsPathDivider(*restOfPath))
680 restOfPath++; /* get rid of '/' */
685 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
686 if (i < YAFFS_MAX_NAME_LENGTH) {
687 str[i] = *restOfPath;
695 /* got to the end of the string */
698 if (yaffs_strcmp(str, _Y(".")) == 0) {
700 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
703 dir = yaffs_find_by_name(dir, str);
705 dir = yaffsfs_FollowLink(dir, symDepth, loop);
707 if (dir && dir->variant_type !=
708 YAFFS_OBJECT_TYPE_DIRECTORY) {
717 /* directory did not exist. */
721 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
725 int *notDir, int *loop)
727 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
732 * yaffsfs_FindObject turns a path for an existing object into the object
734 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
735 const YCHAR *path, int symDepth,
737 struct yaffs_obj **dirOut,
738 int *notDir, int *loop)
740 struct yaffs_obj *dir;
741 struct yaffs_obj *obj;
745 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
750 /* At this stage we have looked up directory part and have the name part
751 * in name if there is one.
753 * eg /nand/x/ will give us a name of ""
754 * /nand/x will give us a name of "x"
756 * Since the name part might be "." or ".." which need to be fixed.
758 if (dir && (yaffs_strcmp(name, _Y("..")) == 0)) {
761 } else if (dir && (yaffs_strcmp(name, _Y(".")) == 0))
763 else if (dir && *name)
764 obj = yaffs_find_by_name(dir, name);
769 obj = yaffs_get_equivalent_obj(obj);
774 /*************************************************************************
775 * Start of yaffsfs visible functions.
776 *************************************************************************/
778 int yaffs_dup(int handle)
780 int newHandleNumber = -1;
781 struct yaffsfs_FileDes *existingFD = NULL;
782 struct yaffsfs_Handle *existingHandle = NULL;
783 struct yaffsfs_Handle *newHandle = NULL;
786 existingHandle = yaffsfs_HandleToPointer(handle);
787 existingFD = yaffsfs_HandleToFileDes(handle);
789 newHandleNumber = yaffsfs_NewHandle(&newHandle);
791 newHandle->fdId = existingHandle->fdId;
792 existingFD->handleCount++;
798 yaffsfs_SetError(-EBADF);
800 yaffsfs_SetError(-ENOMEM);
802 return newHandleNumber;
806 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
808 int current_objects = dev->n_obj - dev->n_deleted_files;
810 if (dev->param.max_objects && current_objects > dev->param.max_objects)
816 int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
817 int oflag, int mode, int sharing)
819 struct yaffs_obj *obj = NULL;
820 struct yaffs_obj *dir = NULL;
823 struct yaffsfs_FileDes *fd = NULL;
826 int errorReported = 0;
827 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
828 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
829 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
830 u8 sharedReadAllowed;
831 u8 sharedWriteAllowed;
841 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
842 yaffsfs_SetError(-EFAULT);
846 if (yaffsfs_CheckPath(path) < 0) {
847 yaffsfs_SetError(-ENAMETOOLONG);
851 /* O_EXCL only has meaning if O_CREAT is specified */
852 if (!(oflag & O_CREAT))
855 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
856 if ((oflag & O_CREAT) & (oflag & O_EXCL))
859 /* Todo: Are there any more flag combos to sanitise ? */
861 /* Figure out if reading or writing is requested */
863 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
864 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
868 handle = yaffsfs_NewHandleAndFileDes();
871 yaffsfs_SetError(-ENFILE);
875 fd = yaffsfs_HandleToFileDes(handle);
877 /* try to find the exisiting object */
878 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
880 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
883 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
884 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
889 /* The file already exists or it might be a directory */
890 is_dir = (obj->variant_type ==
891 YAFFS_OBJECT_TYPE_DIRECTORY);
893 /* A directory can't be opened except for read */
895 (writeRequested || !readRequested ||
896 (oflag & ~O_RDONLY))) {
898 yaffsfs_SetError(-EISDIR);
903 dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
906 yaffsfs_SetError(-ENFILE);
911 /* Open should fail if O_CREAT and O_EXCL are specified
912 * for a file that exists.
914 if (!errorReported &&
915 (oflag & O_EXCL) && (oflag & O_CREAT)) {
917 yaffsfs_SetError(-EEXIST);
921 /* Check file permissions */
922 if (readRequested && !(obj->yst_mode & S_IREAD))
925 if (writeRequested && !(obj->yst_mode & S_IWRITE))
928 if (!errorReported && writeRequested &&
929 obj->my_dev->read_only) {
931 yaffsfs_SetError(-EROFS);
935 if (openDenied && !errorReported) {
936 yaffsfs_SetError(-EACCES);
940 /* Check sharing of an existing object. */
942 struct yaffsfs_FileDes *fdx;
945 sharedReadAllowed = 1;
946 sharedWriteAllowed = 1;
949 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
950 fdx = &yaffsfs_fd[i];
951 if (fdx->handleCount > 0 &&
953 yaffsfs_inode[fdx->inodeId].iObj
956 sharedReadAllowed = 0;
957 if (!fdx->shareWrite)
958 sharedWriteAllowed = 0;
966 if ((!sharedReadAllowed && readRequested) ||
967 (!shareRead && alreadyReading) ||
968 (!sharedWriteAllowed && writeRequested) ||
969 (!shareWrite && alreadyWriting)) {
971 yaffsfs_SetError(-EBUSY);
978 /* If we could not open an existing object, then let's see if
979 * the directory exists. If not, error.
981 if (!obj && !errorReported) {
982 dir = yaffsfs_FindDirectory(reldir, path, &name, 0,
984 if (!dir && notDir) {
985 yaffsfs_SetError(-ENOTDIR);
988 yaffsfs_SetError(-ELOOP);
991 yaffsfs_SetError(-ENOENT);
996 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
997 /* Let's see if we can create this file */
998 if (dir->my_dev->read_only) {
999 yaffsfs_SetError(-EROFS);
1001 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
1002 yaffsfs_SetError(-ENFILE);
1005 obj = yaffs_create_file(dir, name, mode, 0, 0);
1007 if (!obj && !errorReported) {
1008 yaffsfs_SetError(-ENOSPC);
1013 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
1014 yaffsfs_SetError(-ENOENT);
1018 if (obj && !openDenied) {
1019 int inodeId = yaffsfs_GetInodeIdForObject(obj);
1023 * Todo: Fix any problem if inodes run out,
1024 * That can't happen if the number of inode
1025 * items >= number of handles.
1029 fd->inodeId = inodeId;
1030 fd->reading = readRequested;
1031 fd->writing = writeRequested;
1032 fd->append = (oflag & O_APPEND) ? 1 : 0;
1033 fd->shareRead = shareRead;
1034 fd->shareWrite = shareWrite;
1042 /* Hook inode to object */
1043 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1045 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1046 yaffs_resize_file(obj, 0);
1048 yaffsfs_PutHandle(handle);
1050 yaffsfs_SetError(0); /* Problem */
1060 int yaffs_open_sharing_reldev(struct yaffs_dev *dev, const YCHAR *path, int oflag,
1061 int mode, int sharing)
1063 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path,
1064 oflag, mode, sharing);
1067 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
1069 return yaffs_open_sharing_reldir(NULL, path, oflag, mode, sharing);
1072 int yaffs_open_reldir(struct yaffs_obj *reldir,const YCHAR *path, int oflag, int mode)
1074 return yaffs_open_sharing_reldir(reldir, path, oflag, mode,
1075 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1078 int yaffs_open_reldev(struct yaffs_dev *dev,const YCHAR *path, int oflag, int mode)
1080 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path, oflag, mode,
1081 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1084 int yaffs_open(const YCHAR *path, int oflag, int mode)
1086 return yaffs_open_reldir(NULL, path, oflag, mode);
1089 static int yaffs_Dofsync(int handle, int datasync)
1092 struct yaffs_obj *obj;
1096 obj = yaffsfs_HandleToObject(handle);
1099 yaffsfs_SetError(-EBADF);
1100 else if (obj->my_dev->read_only)
1101 yaffsfs_SetError(-EROFS);
1103 yaffs_flush_file(obj, 1, datasync, 0);
1112 int yaffs_fsync(int handle)
1114 return yaffs_Dofsync(handle, 0);
1117 int yaffs_flush(int handle)
1119 return yaffs_fsync(handle);
1122 int yaffs_fdatasync(int handle)
1124 return yaffs_Dofsync(handle, 1);
1127 int yaffs_close(int handle)
1129 struct yaffsfs_Handle *h = NULL;
1130 struct yaffsfs_FileDes *f;
1131 struct yaffs_obj *obj = NULL;
1136 h = yaffsfs_HandleToPointer(handle);
1137 f = yaffsfs_HandleToFileDes(handle);
1138 obj = yaffsfs_HandleToObject(handle);
1140 if (!h || !obj || !f)
1141 yaffsfs_SetError(-EBADF);
1145 yaffs_flush_file(obj, 1, 0, 1);
1146 yaffsfs_PutHandle(handle);
1155 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1156 int isPread, Y_LOFF_T offset)
1158 struct yaffsfs_FileDes *fd = NULL;
1159 struct yaffs_obj *obj = NULL;
1161 Y_LOFF_T startPos = 0;
1162 Y_LOFF_T endPos = 0;
1167 u8 *buf = (u8 *) vbuf;
1169 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1170 yaffsfs_SetError(-EFAULT);
1175 fd = yaffsfs_HandleToFileDes(handle);
1176 obj = yaffsfs_HandleToObject(handle);
1180 yaffsfs_SetError(-EBADF);
1182 } else if (!fd->reading) {
1183 /* Not a reading handle */
1184 yaffsfs_SetError(-EINVAL);
1186 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1187 yaffsfs_SetError(-EINVAL);
1193 startPos = fd->v.position;
1197 if (yaffs_get_obj_length(obj) > pos)
1198 maxRead = yaffs_get_obj_length(obj) - pos;
1202 if (nbyte > maxRead)
1205 yaffsfs_GetHandle(handle);
1207 endPos = pos + nbyte;
1209 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1210 nbyte > YAFFS_MAX_FILE_SIZE ||
1211 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1217 nToRead = YAFFSFS_RW_SIZE -
1218 (pos & (YAFFSFS_RW_SIZE - 1));
1219 if (nToRead > nbyte)
1223 * Need to reverify object in case the device was
1224 * unmounted in another thread.
1226 obj = yaffsfs_HandleToObject(handle);
1230 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1238 if (nRead == nToRead)
1241 nbyte = 0; /* no more to read */
1250 yaffsfs_PutHandle(handle);
1254 fd->v.position = startPos + totalRead;
1256 yaffsfs_SetError(-EINVAL);
1263 return (totalRead >= 0) ? totalRead : -1;
1267 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1269 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1272 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1274 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1277 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1278 int isPwrite, Y_LOFF_T offset)
1280 struct yaffsfs_FileDes *fd = NULL;
1281 struct yaffs_obj *obj = NULL;
1283 Y_LOFF_T startPos = 0;
1286 int totalWritten = 0;
1287 int write_trhrough = 0;
1289 const u8 *buf = (const u8 *)vbuf;
1291 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1292 yaffsfs_SetError(-EFAULT);
1297 fd = yaffsfs_HandleToFileDes(handle);
1298 obj = yaffsfs_HandleToObject(handle);
1302 yaffsfs_SetError(-EBADF);
1304 } else if (!fd->writing) {
1305 yaffsfs_SetError(-EINVAL);
1307 } else if (obj->my_dev->read_only) {
1308 yaffsfs_SetError(-EROFS);
1312 startPos = yaffs_get_obj_length(obj);
1316 startPos = fd->v.position;
1318 yaffsfs_GetHandle(handle);
1320 endPos = pos + nbyte;
1322 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1323 nbyte > YAFFS_MAX_FILE_SIZE ||
1324 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1331 nToWrite = YAFFSFS_RW_SIZE -
1332 (pos & (YAFFSFS_RW_SIZE - 1));
1333 if (nToWrite > nbyte)
1337 * Need to reverify object in case the device was
1338 * remounted or unmounted in another thread.
1340 obj = yaffsfs_HandleToObject(handle);
1341 if (!obj || obj->my_dev->read_only)
1345 yaffs_wr_file(obj, buf, pos, nToWrite,
1348 totalWritten += nWritten;
1353 if (nWritten == nToWrite)
1358 if (nWritten < 1 && totalWritten < 1) {
1359 yaffsfs_SetError(-ENOSPC);
1369 yaffsfs_PutHandle(handle);
1372 if (totalWritten > 0)
1373 fd->v.position = startPos + totalWritten;
1375 yaffsfs_SetError(-EINVAL);
1381 return (totalWritten >= 0) ? totalWritten : -1;
1384 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1386 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1389 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1391 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1394 int yaffs_truncate_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1397 struct yaffs_obj *obj = NULL;
1398 struct yaffs_obj *dir = NULL;
1399 int result = YAFFS_FAIL;
1403 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1404 yaffsfs_SetError(-EFAULT);
1408 if (yaffsfs_CheckPath(path) < 0) {
1409 yaffsfs_SetError(-ENAMETOOLONG);
1415 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1416 obj = yaffsfs_FollowLink(obj, 0, &loop);
1419 yaffsfs_SetError(-ENOTDIR);
1421 yaffsfs_SetError(-ELOOP);
1422 else if (!dir || !obj)
1423 yaffsfs_SetError(-ENOENT);
1424 else if (obj->my_dev->read_only)
1425 yaffsfs_SetError(-EROFS);
1426 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1427 yaffsfs_SetError(-EISDIR);
1428 else if (obj->my_dev->read_only)
1429 yaffsfs_SetError(-EROFS);
1430 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1431 yaffsfs_SetError(-EINVAL);
1433 result = yaffs_resize_file(obj, new_size);
1437 return (result) ? 0 : -1;
1440 int yaffs_truncate_reldev(struct yaffs_dev *dev, const YCHAR *path,
1443 return yaffs_truncate_reldir(ROOT_DIR(dev), path, new_size);
1446 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1448 return yaffs_truncate_reldir(NULL, path, new_size);
1451 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1453 struct yaffsfs_FileDes *fd = NULL;
1454 struct yaffs_obj *obj = NULL;
1458 fd = yaffsfs_HandleToFileDes(handle);
1459 obj = yaffsfs_HandleToObject(handle);
1463 yaffsfs_SetError(-EBADF);
1464 else if (!fd->writing)
1465 yaffsfs_SetError(-EINVAL);
1466 else if (obj->my_dev->read_only)
1467 yaffsfs_SetError(-EROFS);
1468 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1469 yaffsfs_SetError(-EINVAL);
1471 /* resize the file */
1472 result = yaffs_resize_file(obj, new_size);
1475 return (result) ? 0 : -1;
1479 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1481 struct yaffsfs_FileDes *fd = NULL;
1482 struct yaffs_obj *obj = NULL;
1484 Y_LOFF_T fSize = -1;
1487 fd = yaffsfs_HandleToFileDes(handle);
1488 obj = yaffsfs_HandleToObject(handle);
1491 yaffsfs_SetError(-EBADF);
1492 else if (offset > YAFFS_MAX_FILE_SIZE)
1493 yaffsfs_SetError(-EINVAL);
1495 if (whence == SEEK_SET) {
1498 } else if (whence == SEEK_CUR) {
1499 if ((fd->v.position + offset) >= 0)
1500 pos = (fd->v.position + offset);
1501 } else if (whence == SEEK_END) {
1502 fSize = yaffs_get_obj_length(obj);
1503 if (fSize >= 0 && (fSize + offset) >= 0)
1504 pos = fSize + offset;
1507 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1508 fd->v.position = pos;
1510 yaffsfs_SetError(-EINVAL);
1520 static int yaffsfs_DoUnlink_reldir(struct yaffs_obj *reldir,
1521 const YCHAR *path, int isDirectory)
1523 struct yaffs_obj *dir = NULL;
1524 struct yaffs_obj *obj = NULL;
1526 int result = YAFFS_FAIL;
1530 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1531 yaffsfs_SetError(-EFAULT);
1535 if (yaffsfs_CheckPath(path) < 0) {
1536 yaffsfs_SetError(-ENAMETOOLONG);
1542 obj = yaffsfs_FindObject(reldir, path, 0, 0, NULL, NULL, NULL);
1543 dir = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
1546 yaffsfs_SetError(-ENOTDIR);
1548 yaffsfs_SetError(-ELOOP);
1550 yaffsfs_SetError(-ENOENT);
1551 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1552 yaffsfs_SetError(-EINVAL);
1554 yaffsfs_SetError(-ENOENT);
1555 else if (obj->my_dev->read_only)
1556 yaffsfs_SetError(-EROFS);
1557 else if (!isDirectory &&
1558 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1559 yaffsfs_SetError(-EISDIR);
1560 else if (isDirectory &&
1561 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1562 yaffsfs_SetError(-ENOTDIR);
1563 else if (isDirectory && obj == obj->my_dev->root_dir)
1564 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1566 result = yaffs_unlinker(dir, name);
1568 if (result == YAFFS_FAIL && isDirectory)
1569 yaffsfs_SetError(-ENOTEMPTY);
1574 return (result == YAFFS_FAIL) ? -1 : 0;
1577 int yaffs_unlink_reldir(struct yaffs_obj *reldir, const YCHAR *path)
1579 return yaffsfs_DoUnlink_reldir(reldir, path, 0);
1582 int yaffs_unlink_reldev(struct yaffs_dev *dev, const YCHAR *path)
1584 return yaffsfs_DoUnlink_reldir(ROOT_DIR(dev), path, 0);
1587 int yaffs_unlink(const YCHAR *path)
1589 return yaffs_unlink_reldir(NULL, path);
1592 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1594 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1595 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1601 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1603 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1604 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1610 int yaffs_rename_reldir(struct yaffs_obj *reldir,
1611 const YCHAR *oldPath, const YCHAR *newPath)
1613 struct yaffs_obj *olddir = NULL;
1614 struct yaffs_obj *newdir = NULL;
1615 struct yaffs_obj *obj = NULL;
1616 struct yaffs_obj *newobj = NULL;
1619 int result = YAFFS_FAIL;
1620 int rename_allowed = 1;
1626 YCHAR *alt_newpath = NULL;
1628 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1629 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1630 yaffsfs_SetError(-EFAULT);
1634 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1635 yaffsfs_SetError(-ENAMETOOLONG);
1639 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1640 yaffsfs_SetError(-ENOMEM);
1644 newPath = alt_newpath;
1648 olddir = yaffsfs_FindDirectory(reldir, oldPath, &oldname, 0,
1649 ¬OldDir, &oldLoop);
1650 newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
1651 ¬NewDir, &newLoop);
1652 obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
1653 newobj = yaffsfs_FindObject(reldir, newPath, 0, 0, NULL, NULL, NULL);
1655 /* If the object being renamed is a directory and the
1656 * path ended with a "/" then the olddir == obj.
1657 * We pass through NULL for the old name to tell the lower layers
1658 * to use olddir as the object.
1664 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1665 yaffsfs_SetError(-ENOTDIR);
1667 } else if (oldLoop || newLoop) {
1668 yaffsfs_SetError(-ELOOP);
1670 } else if (olddir && oldname &&
1671 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1672 yaffsfs_SetError(-EINVAL);
1674 } else if (!olddir || !newdir || !obj) {
1675 yaffsfs_SetError(-ENOENT);
1677 } else if (obj->my_dev->read_only) {
1678 yaffsfs_SetError(-EROFS);
1680 } else if (rename_file_over_dir(obj, newobj)) {
1681 yaffsfs_SetError(-EISDIR);
1683 } else if (rename_dir_over_file(obj, newobj)) {
1684 yaffsfs_SetError(-ENOTDIR);
1686 } else if (yaffs_is_non_empty_dir(newobj)) {
1687 yaffsfs_SetError(-ENOTEMPTY);
1689 } else if (olddir->my_dev != newdir->my_dev) {
1690 /* Rename must be on same device */
1691 yaffsfs_SetError(-EXDEV);
1693 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1695 * It is a directory, check that it is not being renamed to
1696 * being its own decendent.
1697 * Do this by tracing from the new directory back to the root,
1701 struct yaffs_obj *xx = newdir;
1703 while (rename_allowed && xx) {
1708 if (!rename_allowed)
1709 yaffsfs_SetError(-EINVAL);
1713 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1719 return (result == YAFFS_FAIL) ? -1 : 0;
1722 int yaffs_rename_reldev(struct yaffs_dev *dev, const YCHAR *oldPath,
1723 const YCHAR *newPath)
1725 return yaffs_rename_reldir(ROOT_DIR(dev), oldPath, newPath);
1727 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1729 return yaffs_rename_reldir(NULL, oldPath, newPath);
1732 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1736 obj = yaffs_get_equivalent_obj(obj);
1739 buf->st_dev = (int)obj->my_dev->os_context;
1740 buf->st_ino = obj->obj_id;
1741 buf->st_mode = obj->yst_mode & ~S_IFMT;
1743 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1744 buf->st_mode |= S_IFDIR;
1745 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1746 buf->st_mode |= S_IFLNK;
1747 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1748 buf->st_mode |= S_IFREG;
1750 buf->st_nlink = yaffs_get_obj_link_count(obj);
1753 buf->st_rdev = obj->yst_rdev;
1754 buf->st_size = yaffs_get_obj_length(obj);
1755 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1756 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1758 #if CONFIG_YAFFS_WINCE
1759 buf->yst_wince_atime[0] = obj->win_atime[0];
1760 buf->yst_wince_atime[1] = obj->win_atime[1];
1761 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1762 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1763 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1764 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1766 buf->yst_atime = obj->yst_atime;
1767 buf->yst_ctime = obj->yst_ctime;
1768 buf->yst_mtime = obj->yst_mtime;
1775 static int yaffsfs_DoStatOrLStat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1776 struct yaffs_stat *buf, int doLStat)
1778 struct yaffs_obj *obj = NULL;
1779 struct yaffs_obj *dir = NULL;
1784 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1785 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1786 yaffsfs_SetError(-EFAULT);
1790 if (yaffsfs_CheckPath(path) < 0) {
1791 yaffsfs_SetError(-ENAMETOOLONG);
1797 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1799 if (!doLStat && obj)
1800 obj = yaffsfs_FollowLink(obj, 0, &loop);
1803 yaffsfs_SetError(-ENOTDIR);
1805 yaffsfs_SetError(-ELOOP);
1806 else if (!dir || !obj)
1807 yaffsfs_SetError(-ENOENT);
1809 retVal = yaffsfs_DoStat(obj, buf);
1817 int yaffs_stat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1818 struct yaffs_stat *buf)
1820 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 0);
1823 int yaffs_lstat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1824 struct yaffs_stat *buf)
1826 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 1);
1829 int yaffs_stat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1830 struct yaffs_stat *buf)
1832 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 0);
1835 int yaffs_lstat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1836 struct yaffs_stat *buf)
1838 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 1);
1841 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1843 return yaffs_stat_reldir(NULL, path, buf);
1846 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1848 return yaffs_lstat_reldir(NULL, path, buf);
1851 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1853 struct yaffs_obj *obj;
1857 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1858 yaffsfs_SetError(-EFAULT);
1863 obj = yaffsfs_HandleToObject(fd);
1866 retVal = yaffsfs_DoStat(obj, buf);
1869 yaffsfs_SetError(-EBADF);
1876 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1877 const struct yaffs_utimbuf *buf)
1880 struct yaffs_utimbuf local;
1882 obj = yaffs_get_equivalent_obj(obj);
1884 if (obj && obj->my_dev->read_only) {
1885 yaffsfs_SetError(-EROFS);
1889 #if !CONFIG_YAFFS_WINCE
1891 local.actime = Y_CURRENT_TIME;
1892 local.modtime = local.actime;
1899 obj->yst_atime = buf->actime;
1900 obj->yst_mtime = buf->modtime;
1902 result = yaffs_flush_file(obj, 0, 0, 0);
1903 retVal = result == YAFFS_OK ? 0 : -1;
1910 int yaffs_utime_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1911 const struct yaffs_utimbuf *buf)
1913 struct yaffs_obj *obj = NULL;
1914 struct yaffs_obj *dir = NULL;
1920 yaffsfs_SetError(-EFAULT);
1924 if (yaffsfs_CheckPath(path) < 0) {
1925 yaffsfs_SetError(-ENAMETOOLONG);
1931 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1934 yaffsfs_SetError(-ENOTDIR);
1936 yaffsfs_SetError(-ELOOP);
1937 else if (!dir || !obj)
1938 yaffsfs_SetError(-ENOENT);
1940 retVal = yaffsfs_DoUtime(obj, buf);
1948 int yaffs_utime_reldev(struct yaffs_dev *dev, const YCHAR *path,
1949 const struct yaffs_utimbuf *buf)
1951 return yaffs_utime_reldir(ROOT_DIR(dev), path, buf);
1954 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1956 return yaffs_utime_reldir(NULL, path, buf);
1959 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1961 struct yaffs_obj *obj;
1966 obj = yaffsfs_HandleToObject(fd);
1969 retVal = yaffsfs_DoUtime(obj, buf);
1972 yaffsfs_SetError(-EBADF);
1979 #ifndef CONFIG_YAFFS_WINCE
1980 /* xattrib functions */
1982 static int yaffs_do_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1983 const char *name, const void *data, int size,
1984 int flags, int follow)
1986 struct yaffs_obj *obj;
1987 struct yaffs_obj *dir;
1993 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1994 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1995 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1996 yaffsfs_SetError(-EFAULT);
2000 if (yaffsfs_CheckPath(path) < 0) {
2001 yaffsfs_SetError(-ENAMETOOLONG);
2007 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2010 obj = yaffsfs_FollowLink(obj, 0, &loop);
2013 yaffsfs_SetError(-ENOTDIR);
2015 yaffsfs_SetError(-ELOOP);
2016 else if (!dir || !obj)
2017 yaffsfs_SetError(-ENOENT);
2019 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2021 yaffsfs_SetError(retVal);
2032 int yaffs_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2033 const char *name, const void *data, int size, int flags)
2035 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 1);
2038 int yaffs_setxattr_reldev(struct yaffs_dev *dev, const YCHAR *path,
2039 const char *name, const void *data, int size, int flags)
2041 return yaffs_setxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2044 int yaffs_setxattr(const YCHAR *path, const char *name,
2045 const void *data, int size, int flags)
2047 return yaffs_setxattr_reldir(NULL, path, name, data, size, flags);
2050 int yaffs_lsetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path, const char *name,
2051 const void *data, int size, int flags)
2053 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 0);
2056 int yaffs_lsetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name,
2057 const void *data, int size, int flags)
2059 return yaffs_lsetxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2062 int yaffs_lsetxattr(const YCHAR *path, const char *name,
2063 const void *data, int size, int flags)
2065 return yaffs_lsetxattr_reldir(NULL, path, name, data, size, flags);
2068 int yaffs_fsetxattr(int fd, const char *name,
2069 const void *data, int size, int flags)
2071 struct yaffs_obj *obj;
2075 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2076 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2077 yaffsfs_SetError(-EFAULT);
2082 obj = yaffsfs_HandleToObject(fd);
2085 yaffsfs_SetError(-EBADF);
2087 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2089 yaffsfs_SetError(retVal);
2099 static int yaffs_do_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2100 const char *name, void *data, int size, int follow)
2102 struct yaffs_obj *obj;
2103 struct yaffs_obj *dir;
2108 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2109 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2110 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2111 yaffsfs_SetError(-EFAULT);
2115 if (yaffsfs_CheckPath(path) < 0) {
2116 yaffsfs_SetError(-ENAMETOOLONG);
2122 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2125 obj = yaffsfs_FollowLink(obj, 0, &loop);
2128 yaffsfs_SetError(-ENOTDIR);
2130 yaffsfs_SetError(-ELOOP);
2131 else if (!dir || !obj)
2132 yaffsfs_SetError(-ENOENT);
2134 retVal = yaffs_get_xattrib(obj, name, data, size);
2136 yaffsfs_SetError(retVal);
2146 int yaffs_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2147 const char *name, void *data, int size)
2149 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 1);
2152 int yaffs_getxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2154 return yaffs_getxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2157 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
2159 return yaffs_getxattr_reldir(NULL, path, name, data, size);
2162 int yaffs_lgetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2163 const char *name, void *data, int size)
2165 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 0);
2168 int yaffs_lgetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2170 return yaffs_lgetxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2173 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2175 return yaffs_lgetxattr_reldir(NULL, path, name, data, size);
2178 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2180 struct yaffs_obj *obj;
2184 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2185 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2186 yaffsfs_SetError(-EFAULT);
2191 obj = yaffsfs_HandleToObject(fd);
2194 retVal = yaffs_get_xattrib(obj, name, data, size);
2196 yaffsfs_SetError(retVal);
2201 yaffsfs_SetError(-EBADF);
2208 static int yaffs_do_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2209 char *data, int size, int follow)
2211 struct yaffs_obj *obj = NULL;
2212 struct yaffs_obj *dir = NULL;
2217 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2218 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2219 yaffsfs_SetError(-EFAULT);
2223 if (yaffsfs_CheckPath(path) < 0) {
2224 yaffsfs_SetError(-ENAMETOOLONG);
2230 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2233 obj = yaffsfs_FollowLink(obj, 0, &loop);
2236 yaffsfs_SetError(-ENOTDIR);
2238 yaffsfs_SetError(-ELOOP);
2239 else if (!dir || !obj)
2240 yaffsfs_SetError(-ENOENT);
2242 retVal = yaffs_list_xattrib(obj, data, size);
2244 yaffsfs_SetError(retVal);
2255 int yaffs_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2256 char *data, int size)
2258 return yaffs_do_listxattr_reldir(reldir, path, data, size, 1);
2261 int yaffs_listxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2263 return yaffs_listxattr_reldir(ROOT_DIR(dev), path, data, size);
2266 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2268 return yaffs_listxattr_reldir(NULL, path, data, size);
2271 int yaffs_llistxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2272 char *data, int size)
2274 return yaffs_do_listxattr_reldir(reldir, path, data, size, 0);
2277 int yaffs_llistxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2279 return yaffs_llistxattr_reldir(ROOT_DIR(dev), path, data, size);
2282 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2284 return yaffs_llistxattr_reldir(NULL, path, data, size);
2287 int yaffs_flistxattr(int fd, char *data, int size)
2289 struct yaffs_obj *obj;
2293 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2294 yaffsfs_SetError(-EFAULT);
2299 obj = yaffsfs_HandleToObject(fd);
2302 retVal = yaffs_list_xattrib(obj, data, size);
2304 yaffsfs_SetError(retVal);
2309 yaffsfs_SetError(-EBADF);
2316 static int yaffs_do_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2317 const char *name, int follow)
2319 struct yaffs_obj *obj = NULL;
2320 struct yaffs_obj *dir = NULL;
2325 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2326 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2327 yaffsfs_SetError(-EFAULT);
2331 if (yaffsfs_CheckPath(path) < 0) {
2332 yaffsfs_SetError(-ENAMETOOLONG);
2338 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2341 obj = yaffsfs_FollowLink(obj, 0, &loop);
2344 yaffsfs_SetError(-ENOTDIR);
2346 yaffsfs_SetError(-ELOOP);
2347 else if (!dir || !obj)
2348 yaffsfs_SetError(-ENOENT);
2350 retVal = yaffs_remove_xattrib(obj, name);
2352 yaffsfs_SetError(retVal);
2363 int yaffs_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2366 return yaffs_do_removexattr_reldir(reldir, path, name, 1);
2369 int yaffs_removexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2371 return yaffs_removexattr_reldir(ROOT_DIR(dev), path, name);
2374 int yaffs_removexattr(const YCHAR *path, const char *name)
2376 return yaffs_removexattr_reldir(NULL, path, name);
2379 int yaffs_lremovexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2382 return yaffs_do_removexattr_reldir(reldir, path, name, 0);
2385 int yaffs_lremovexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2387 return yaffs_lremovexattr_reldir(ROOT_DIR(dev), path, name);
2390 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2392 return yaffs_lremovexattr_reldir(NULL, path, name);
2395 int yaffs_fremovexattr(int fd, const char *name)
2397 struct yaffs_obj *obj;
2401 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2402 yaffsfs_SetError(-EFAULT);
2407 obj = yaffsfs_HandleToObject(fd);
2410 retVal = yaffs_remove_xattrib(obj, name);
2412 yaffsfs_SetError(retVal);
2417 yaffsfs_SetError(-EBADF);
2425 #ifdef CONFIG_YAFFS_WINCE
2426 int yaffs_get_wince_times(int fd, unsigned *wctime,
2427 unsigned *watime, unsigned *wmtime)
2429 struct yaffs_obj *obj;
2434 obj = yaffsfs_HandleToObject(fd);
2439 wctime[0] = obj->win_ctime[0];
2440 wctime[1] = obj->win_ctime[1];
2443 watime[0] = obj->win_atime[0];
2444 watime[1] = obj->win_atime[1];
2447 wmtime[0] = obj->win_mtime[0];
2448 wmtime[1] = obj->win_mtime[1];
2454 yaffsfs_SetError(-EBADF);
2461 int yaffs_set_wince_times(int fd,
2462 const unsigned *wctime,
2463 const unsigned *watime, const unsigned *wmtime)
2465 struct yaffs_obj *obj;
2470 obj = yaffsfs_HandleToObject(fd);
2475 obj->win_ctime[0] = wctime[0];
2476 obj->win_ctime[1] = wctime[1];
2479 obj->win_atime[0] = watime[0];
2480 obj->win_atime[1] = watime[1];
2483 obj->win_mtime[0] = wmtime[0];
2484 obj->win_mtime[1] = wmtime[1];
2488 result = yaffs_flush_file(obj, 0, 0, 0);
2492 yaffsfs_SetError(-EBADF);
2501 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2506 obj = yaffs_get_equivalent_obj(obj);
2509 obj->yst_mode = mode;
2511 result = yaffs_flush_file(obj, 0, 0, 0);
2514 return result == YAFFS_OK ? 0 : -1;
2517 int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
2519 struct yaffs_obj *obj = NULL;
2520 struct yaffs_obj *dir = NULL;
2525 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2526 yaffsfs_SetError(-EFAULT);
2530 if (yaffsfs_CheckPath(path) < 0) {
2531 yaffsfs_SetError(-ENAMETOOLONG);
2535 if (amode & ~(R_OK | W_OK | X_OK)) {
2536 yaffsfs_SetError(-EINVAL);
2542 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2543 obj = yaffsfs_FollowLink(obj, 0, &loop);
2546 yaffsfs_SetError(-ENOTDIR);
2548 yaffsfs_SetError(-ELOOP);
2549 else if (!dir || !obj)
2550 yaffsfs_SetError(-ENOENT);
2551 else if ((amode & W_OK) && obj->my_dev->read_only)
2552 yaffsfs_SetError(-EROFS);
2556 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2558 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2560 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2564 yaffsfs_SetError(-EACCES);
2575 int yaffs_access_reldev(struct yaffs_dev *dev, const YCHAR *path, int amode)
2577 return yaffs_access_reldir(ROOT_DIR(dev), path, amode);
2580 int yaffs_access(const YCHAR *path, int amode)
2582 return yaffs_access_reldir(NULL, path, amode);
2585 int yaffs_chmod_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2587 struct yaffs_obj *obj = NULL;
2588 struct yaffs_obj *dir = NULL;
2593 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2594 yaffsfs_SetError(-EFAULT);
2598 if (yaffsfs_CheckPath(path) < 0) {
2599 yaffsfs_SetError(-ENAMETOOLONG);
2603 if (mode & ~(0777)) {
2604 yaffsfs_SetError(-EINVAL);
2610 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2611 obj = yaffsfs_FollowLink(obj, 0, &loop);
2614 yaffsfs_SetError(-ENOTDIR);
2616 yaffsfs_SetError(-ELOOP);
2617 else if (!dir || !obj)
2618 yaffsfs_SetError(-ENOENT);
2619 else if (obj->my_dev->read_only)
2620 yaffsfs_SetError(-EROFS);
2622 retVal = yaffsfs_DoChMod(obj, mode);
2630 int yaffs_chmod_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2632 return yaffs_chmod_reldir(ROOT_DIR(dev), path, mode);
2635 int yaffs_chmod(const YCHAR *path, mode_t mode)
2637 return yaffs_chmod_reldir(NULL, path, mode);
2640 int yaffs_fchmod(int fd, mode_t mode)
2642 struct yaffs_obj *obj;
2645 if (mode & ~(0777)) {
2646 yaffsfs_SetError(-EINVAL);
2651 obj = yaffsfs_HandleToObject(fd);
2654 yaffsfs_SetError(-EBADF);
2655 else if (obj->my_dev->read_only)
2656 yaffsfs_SetError(-EROFS);
2658 retVal = yaffsfs_DoChMod(obj, mode);
2665 int yaffs_mkdir_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2667 struct yaffs_obj *parent = NULL;
2668 struct yaffs_obj *dir = NULL;
2670 YCHAR *alt_path = NULL;
2675 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2676 yaffsfs_SetError(-EFAULT);
2680 if (yaffsfs_CheckPath(path) < 0) {
2681 yaffsfs_SetError(-ENAMETOOLONG);
2685 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2686 yaffsfs_SetError(-ENOMEM);
2693 parent = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
2694 if (!parent && notDir)
2695 yaffsfs_SetError(-ENOTDIR);
2697 yaffsfs_SetError(-ELOOP);
2699 yaffsfs_SetError(-ENOENT);
2700 else if (yaffsfs_TooManyObjects(parent->my_dev))
2701 yaffsfs_SetError(-ENFILE);
2702 else if (yaffs_strnlen(name, 5) == 0) {
2703 /* Trying to make the root itself */
2704 yaffsfs_SetError(-EEXIST);
2705 } else if (parent->my_dev->read_only)
2706 yaffsfs_SetError(-EROFS);
2708 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2711 else if (yaffs_find_by_name(parent, name))
2712 yaffsfs_SetError(-EEXIST); /* name exists */
2714 yaffsfs_SetError(-ENOSPC); /* assume no space */
2724 int yaffs_mkdir_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2726 return yaffs_mkdir_reldir(ROOT_DIR(dev), path, mode);
2729 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2731 return yaffs_mkdir_reldir(NULL, path, mode);
2734 int yaffs_rmdir_reldir(struct yaffs_obj *reldir, const YCHAR *path)
2739 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2740 yaffsfs_SetError(-EFAULT);
2744 if (yaffsfs_CheckPath(path) < 0) {
2745 yaffsfs_SetError(-ENAMETOOLONG);
2749 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2750 yaffsfs_SetError(-ENOMEM);
2755 result = yaffsfs_DoUnlink_reldir(reldir, path, 1);
2762 int yaffs_rmdir_reldev(struct yaffs_dev *dev, const YCHAR *path)
2764 return yaffs_rmdir_reldir(ROOT_DIR(dev), path);
2767 int yaffs_rmdir(const YCHAR *path)
2769 return yaffs_rmdir_reldir(NULL, path);
2773 * The mount/unmount/sync functions act on devices rather than reldirs.
2775 void *yaffs_getdev(const YCHAR *path)
2777 struct yaffs_dev *dev = NULL;
2779 dev = yaffsfs_FindDevice(path, &dummy);
2783 int yaffs_mount_common(struct yaffs_dev *dev, const YCHAR *path,
2784 int read_only, int skip_checkpt)
2787 int result = YAFFS_FAIL;
2790 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2791 yaffsfs_SetError(-EFAULT);
2795 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2797 if (yaffsfs_CheckPath(path) < 0) {
2798 yaffsfs_SetError(-ENAMETOOLONG);
2805 yaffsfs_InitHandles();
2808 dev = yaffsfs_FindMountPoint(path);
2811 if (!dev->is_mounted) {
2812 dev->read_only = read_only ? 1 : 0;
2814 u8 skip = dev->param.skip_checkpt_rd;
2815 dev->param.skip_checkpt_rd = 1;
2816 result = yaffs_guts_initialise(dev);
2817 dev->param.skip_checkpt_rd = skip;
2819 result = yaffs_guts_initialise(dev);
2822 if (result == YAFFS_FAIL)
2823 yaffsfs_SetError(-ENOMEM);
2824 retVal = result ? 0 : -1;
2827 yaffsfs_SetError(-EBUSY);
2829 yaffsfs_SetError(-ENODEV);
2836 int yaffs_mount3_reldev(struct yaffs_dev *dev, int read_only, int skip_checkpt)
2838 return yaffs_mount_common(dev, NULL, read_only, skip_checkpt);
2841 int yaffs_mount3(const YCHAR *path, int read_only, int skip_checkpt)
2843 return yaffs_mount_common(NULL, path, read_only, skip_checkpt);
2846 int yaffs_mount2_reldev(struct yaffs_dev *dev, int readonly)
2848 return yaffs_mount_common(dev, NULL, readonly, 0);
2851 int yaffs_mount2(const YCHAR *path, int readonly)
2853 return yaffs_mount_common(NULL, path, readonly, 0);
2856 int yaffs_mount_reldev(struct yaffs_dev *dev)
2858 return yaffs_mount_common(dev, NULL, 0, 0);
2861 int yaffs_mount(const YCHAR *path)
2863 return yaffs_mount_common(NULL, path, 0, 0);
2866 int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
2872 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2873 yaffsfs_SetError(-EFAULT);
2877 if (yaffsfs_CheckPath(path) < 0) {
2878 yaffsfs_SetError(-ENAMETOOLONG);
2885 dev = yaffsfs_FindDevice(path, &dummy);
2888 if (!dev->is_mounted)
2889 yaffsfs_SetError(-EINVAL);
2890 else if (dev->read_only)
2891 yaffsfs_SetError(-EROFS);
2894 yaffs_flush_whole_cache(dev, 0);
2895 yaffs_checkpoint_save(dev);
2900 yaffsfs_SetError(-ENODEV);
2906 int yaffs_sync_reldev(struct yaffs_dev *dev)
2908 return yaffs_sync_common(dev, NULL);
2911 int yaffs_sync(const YCHAR *path)
2913 return yaffs_sync_common(NULL, path);
2917 static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
2925 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2926 yaffsfs_SetError(-EFAULT);
2930 if (yaffsfs_CheckPath(path) < 0) {
2931 yaffsfs_SetError(-ENAMETOOLONG);
2938 dev = yaffsfs_FindDevice(path, &dummy);
2941 if (!dev->is_mounted)
2942 yaffsfs_SetError(-EINVAL);
2944 retVal = yaffs_bg_gc(dev, urgency);
2946 yaffsfs_SetError(-ENODEV);
2952 /* Background gc functions.
2953 * These return 0 when bg done or greater than 0 when gc has been
2954 * done and there is still a lot of garbage to be cleaned up.
2957 int yaffs_do_background_gc(const YCHAR *path, int urgency)
2959 return yaffsfs_bg_gc_common(NULL, path, urgency);
2962 int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
2964 return yaffsfs_bg_gc_common(dev, NULL, urgency);
2967 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2970 struct yaffs_obj *obj;
2972 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2973 obj = yaffsfs_HandleToObject(i);
2974 if (obj && obj->my_dev == dev)
2980 int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
2981 int force, int read_only)
2985 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2986 yaffsfs_SetError(-EFAULT);
2990 if (yaffsfs_CheckPath(path) < 0) {
2991 yaffsfs_SetError(-ENAMETOOLONG);
2997 dev = yaffsfs_FindMountPoint(path);
3000 if (dev->is_mounted) {
3001 yaffs_flush_whole_cache(dev, 0);
3003 if (force || !yaffsfs_IsDevBusy(dev)) {
3005 yaffs_checkpoint_save(dev);
3006 dev->read_only = read_only ? 1 : 0;
3009 yaffsfs_SetError(-EBUSY);
3012 yaffsfs_SetError(-EINVAL);
3015 yaffsfs_SetError(-ENODEV);
3022 int yaffs_remount_reldev(struct yaffs_dev *dev, int force, int read_only)
3024 return yaffs_remount_common(dev, NULL, force, read_only);
3026 int yaffs_remount(const YCHAR *path, int force, int read_only)
3028 return yaffs_remount_common(NULL, path, force, read_only);
3031 int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
3035 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3036 yaffsfs_SetError(-EFAULT);
3040 if (yaffsfs_CheckPath(path) < 0) {
3041 yaffsfs_SetError(-ENAMETOOLONG);
3047 dev = yaffsfs_FindMountPoint(path);
3050 if (dev->is_mounted) {
3052 yaffs_flush_whole_cache(dev, 0);
3053 yaffs_checkpoint_save(dev);
3054 inUse = yaffsfs_IsDevBusy(dev);
3055 if (!inUse || force) {
3057 yaffsfs_BreakDeviceHandles(dev);
3058 yaffs_deinitialise(dev);
3062 yaffsfs_SetError(-EBUSY);
3065 yaffsfs_SetError(-EINVAL);
3068 yaffsfs_SetError(-ENODEV);
3075 int yaffs_unmount2_reldev(struct yaffs_dev *dev, int force)
3077 return yaffs_unmount2_common(dev, NULL, force);
3080 int yaffs_unmount2(const YCHAR *path, int force)
3082 return yaffs_unmount2_common(NULL, path, force);
3085 int yaffs_unmount_reldev(struct yaffs_dev *dev)
3087 return yaffs_unmount2_reldev(dev, 0);
3090 int yaffs_unmount(const YCHAR *path)
3092 return yaffs_unmount2(path, 0);
3095 int yaffs_format_common(struct yaffs_dev *dev,
3098 int force_unmount_flag,
3106 yaffsfs_SetError(-EFAULT);
3110 if (yaffsfs_CheckPath(path) < 0) {
3111 yaffsfs_SetError(-ENAMETOOLONG);
3118 dev = yaffsfs_FindMountPoint(path);
3121 int was_mounted = dev->is_mounted;
3123 if (dev->is_mounted && unmount_flag) {
3125 yaffs_flush_whole_cache(dev, 0);
3126 yaffs_checkpoint_save(dev);
3127 inUse = yaffsfs_IsDevBusy(dev);
3128 if (!inUse || force_unmount_flag) {
3130 yaffsfs_BreakDeviceHandles(dev);
3131 yaffs_deinitialise(dev);
3135 if(dev->is_mounted) {
3136 yaffsfs_SetError(-EBUSY);
3139 yaffs_guts_format_dev(dev);
3140 if(was_mounted && remount_flag) {
3141 result = yaffs_guts_initialise(dev);
3142 if (result == YAFFS_FAIL) {
3143 yaffsfs_SetError(-ENOMEM);
3149 yaffsfs_SetError(-ENODEV);
3158 int yaffs_format_reldev(struct yaffs_dev *dev,
3160 int force_unmount_flag,
3163 return yaffs_format_common(dev, NULL, unmount_flag,
3164 force_unmount_flag, remount_flag);
3167 int yaffs_format(const YCHAR *path,
3169 int force_unmount_flag,
3172 return yaffs_format_common(NULL, path, unmount_flag,
3173 force_unmount_flag, remount_flag);
3176 Y_LOFF_T yaffs_freespace_common(struct yaffs_dev *dev, const YCHAR *path)
3178 Y_LOFF_T retVal = -1;
3182 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3183 yaffsfs_SetError(-EFAULT);
3187 if (yaffsfs_CheckPath(path) < 0) {
3188 yaffsfs_SetError(-ENAMETOOLONG);
3195 dev = yaffsfs_FindDevice(path, &dummy);
3196 if (dev && dev->is_mounted) {
3197 retVal = yaffs_get_n_free_chunks(dev);
3198 retVal *= dev->data_bytes_per_chunk;
3201 yaffsfs_SetError(-EINVAL);
3207 Y_LOFF_T yaffs_freespace_reldev(struct yaffs_dev *dev)
3209 return yaffs_freespace_common(dev, NULL);
3212 Y_LOFF_T yaffs_freespace(const YCHAR *path)
3214 return yaffs_freespace_common(NULL, path);
3217 Y_LOFF_T yaffs_totalspace_common(struct yaffs_dev *dev, const YCHAR *path)
3219 Y_LOFF_T retVal = -1;
3223 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3224 yaffsfs_SetError(-EFAULT);
3228 if (yaffsfs_CheckPath(path) < 0) {
3229 yaffsfs_SetError(-ENAMETOOLONG);
3236 dev = yaffsfs_FindDevice(path, &dummy);
3237 if (dev && dev->is_mounted) {
3238 retVal = (dev->param.end_block - dev->param.start_block + 1) -
3239 dev->param.n_reserved_blocks;
3240 retVal *= dev->param.chunks_per_block;
3241 retVal *= dev->data_bytes_per_chunk;
3244 yaffsfs_SetError(-EINVAL);
3250 Y_LOFF_T yaffs_totalspace_reldev(struct yaffs_dev *dev)
3252 return yaffs_totalspace_common(dev, NULL);
3255 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
3257 return yaffs_totalspace_common(NULL, path);
3260 int yaffs_inodecount_common(struct yaffs_dev *dev, const YCHAR *path)
3262 Y_LOFF_T retVal = -1;
3266 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3267 yaffsfs_SetError(-EFAULT);
3271 if (yaffsfs_CheckPath(path) < 0) {
3272 yaffsfs_SetError(-ENAMETOOLONG);
3279 dev = yaffsfs_FindDevice(path, &dummy);
3280 if (dev && dev->is_mounted) {
3281 int n_obj = dev->n_obj;
3282 if (n_obj > dev->n_hardlinks)
3283 retVal = n_obj - dev->n_hardlinks;
3287 yaffsfs_SetError(-EINVAL);
3293 int yaffs_inodecount_reldev(struct yaffs_dev *dev)
3295 return yaffs_inodecount_common(dev, NULL);
3298 int yaffs_inodecount(const YCHAR *path)
3300 return yaffs_inodecount_common(NULL, path);
3303 void yaffs_add_device(struct yaffs_dev *dev)
3305 struct list_head *cfg;
3306 /* First check that the device is not in the list. */
3308 list_for_each(cfg, &yaffsfs_deviceList) {
3309 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
3313 dev->is_mounted = 0;
3314 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
3316 if (!dev->dev_list.next)
3317 INIT_LIST_HEAD(&dev->dev_list);
3319 list_add(&dev->dev_list, &yaffsfs_deviceList);
3324 void yaffs_remove_device(struct yaffs_dev *dev)
3326 list_del_init(&dev->dev_list);
3329 /* Functions to iterate through devices. NB Use with extreme care! */
3331 static struct list_head *dev_iterator;
3332 void yaffs_dev_rewind(void)
3334 dev_iterator = yaffsfs_deviceList.next;
3337 struct yaffs_dev *yaffs_next_dev(void)
3339 struct yaffs_dev *retval;
3343 if (dev_iterator == &yaffsfs_deviceList)
3346 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
3347 dev_iterator = dev_iterator->next;
3351 /* Directory search stuff. */
3353 static struct list_head search_contexts;
3355 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
3359 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3363 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
3364 dsc->nextReturn = NULL;
3367 list_entry(dsc->dirObj->variant.dir_variant.
3368 children.next, struct yaffs_obj,
3371 /* Hey someone isn't playing nice! */
3375 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
3379 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3381 if (dsc->nextReturn == NULL ||
3382 list_empty(&dsc->dirObj->variant.dir_variant.children))
3383 dsc->nextReturn = NULL;
3385 struct list_head *next = dsc->nextReturn->siblings.next;
3387 if (next == &dsc->dirObj->variant.dir_variant.children)
3388 dsc->nextReturn = NULL; /* end of list */
3390 dsc->nextReturn = list_entry(next,
3395 /* Hey someone isn't playing nice! */
3399 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
3402 struct list_head *i;
3403 struct yaffsfs_DirSearchContext *dsc;
3405 /* if search contexts not initilised then skip */
3406 if (!search_contexts.next)
3409 /* Iterate through the directory search contexts.
3410 * If any are the one being removed, then advance the dsc to
3411 * the next one to prevent a hanging ptr.
3413 list_for_each(i, &search_contexts) {
3415 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
3417 if (dsc->nextReturn == obj)
3418 yaffsfs_DirAdvance(dsc);
3424 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
3425 const YCHAR *dirname)
3427 yaffs_DIR *dir = NULL;
3428 struct yaffs_obj *obj = NULL;
3429 struct yaffsfs_DirSearchContext *dsc = NULL;
3433 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3434 yaffsfs_SetError(-EFAULT);
3438 if (yaffsfs_CheckPath(dirname) < 0) {
3439 yaffsfs_SetError(-ENAMETOOLONG);
3443 obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, ¬Dir, &loop);
3444 obj = yaffsfs_FollowLink(obj, 0, &loop);
3447 yaffsfs_SetError(-ENOTDIR);
3449 yaffsfs_SetError(-ELOOP);
3451 yaffsfs_SetError(-ENOENT);
3452 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3453 yaffsfs_SetError(-ENOTDIR);
3457 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3458 if (!yaffsfs_dsc[i].inUse)
3459 dsc = &yaffsfs_dsc[i];
3462 dir = (yaffs_DIR *) dsc;
3465 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3468 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3469 INIT_LIST_HEAD(&dsc->others);
3471 if (!search_contexts.next)
3472 INIT_LIST_HEAD(&search_contexts);
3474 list_add(&dsc->others, &search_contexts);
3475 yaffsfs_SetDirRewound(dsc);
3481 yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
3486 ret = yaffsfs_opendir_reldir_no_lock(reldir, dirname);
3491 yaffs_DIR *yaffs_opendir_reldev(struct yaffs_dev *dev, const YCHAR *dirname)
3493 return yaffs_opendir_reldir(ROOT_DIR(dev), dirname);
3496 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3498 return yaffs_opendir_reldir(NULL, dirname);
3501 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3503 struct yaffsfs_DirSearchContext *dsc;
3504 struct yaffs_dirent *retVal = NULL;
3506 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3509 if (dsc && dsc->inUse) {
3510 yaffsfs_SetError(0);
3511 if (dsc->nextReturn) {
3513 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3514 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3515 dsc->de.d_off = dsc->offset++;
3516 yaffs_get_obj_name(dsc->nextReturn,
3517 dsc->de.d_name, NAME_MAX);
3518 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3519 /* this should not happen! */
3520 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3522 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3524 yaffsfs_DirAdvance(dsc);
3528 yaffsfs_SetError(-EBADF);
3533 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3535 struct yaffs_dirent *ret;
3538 ret = yaffsfs_readdir_no_lock(dirp);
3543 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3545 struct yaffsfs_DirSearchContext *dsc;
3547 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3549 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3552 yaffsfs_SetDirRewound(dsc);
3556 void yaffs_rewinddir(yaffs_DIR *dirp)
3559 yaffsfs_rewinddir_no_lock(dirp);
3563 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3565 struct yaffs_dirent *ret = NULL;
3566 struct yaffsfs_FileDes *f;
3569 f = yaffsfs_HandleToFileDes(fd);
3571 ret = yaffsfs_readdir_no_lock(f->v.dir);
3576 void yaffs_rewinddir_fd(int fd)
3578 struct yaffsfs_FileDes *f;
3581 f = yaffsfs_HandleToFileDes(fd);
3583 yaffsfs_rewinddir_no_lock(f->v.dir);
3588 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3590 struct yaffsfs_DirSearchContext *dsc;
3592 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3594 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3595 yaffsfs_SetError(-EFAULT);
3600 list_del(&dsc->others); /* unhook from list */
3604 int yaffs_closedir(yaffs_DIR *dirp)
3609 ret = yaffsfs_closedir_no_lock(dirp);
3614 /* End of directory stuff */
3616 int yaffs_symlink_reldir(struct yaffs_obj *reldir,
3617 const YCHAR *oldpath, const YCHAR *newpath)
3619 struct yaffs_obj *parent = NULL;
3620 struct yaffs_obj *obj;
3623 int mode = 0; /* ignore for now */
3627 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3628 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3629 yaffsfs_SetError(-EFAULT);
3633 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3634 yaffsfs_SetError(-ENAMETOOLONG);
3639 parent = yaffsfs_FindDirectory(reldir, newpath, &name, 0, ¬Dir, &loop);
3640 if (!parent && notDir)
3641 yaffsfs_SetError(-ENOTDIR);
3643 yaffsfs_SetError(-ELOOP);
3644 else if (!parent || yaffs_strnlen(name, 5) < 1)
3645 yaffsfs_SetError(-ENOENT);
3646 else if (yaffsfs_TooManyObjects(parent->my_dev))
3647 yaffsfs_SetError(-ENFILE);
3648 else if (parent->my_dev->read_only)
3649 yaffsfs_SetError(-EROFS);
3651 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3654 else if (yaffsfs_FindObject(reldir, newpath, 0, 0,
3656 yaffsfs_SetError(-EEXIST);
3658 yaffsfs_SetError(-ENOSPC);
3666 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3668 return yaffs_symlink_reldir(NULL, oldpath, newpath);
3671 int yaffs_readlink_reldir(struct yaffs_obj *reldir,const YCHAR *path,
3672 YCHAR *buf, int bufsiz)
3674 struct yaffs_obj *obj = NULL;
3675 struct yaffs_obj *dir = NULL;
3680 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3681 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3682 yaffsfs_SetError(-EFAULT);
3688 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
3691 yaffsfs_SetError(-ENOTDIR);
3693 yaffsfs_SetError(-ELOOP);
3694 else if (!dir || !obj)
3695 yaffsfs_SetError(-ENOENT);
3696 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3697 yaffsfs_SetError(-EINVAL);
3699 YCHAR *alias = obj->variant.symlink_variant.alias;
3700 memset(buf, 0, bufsiz);
3701 yaffs_strncpy(buf, alias, bufsiz - 1);
3707 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3709 return yaffs_readlink_reldir(NULL, path, buf, bufsiz);
3712 int yaffs_link_reldir(struct yaffs_obj *reldir,
3713 const YCHAR *oldpath, const YCHAR *linkpath)
3715 /* Creates a link called newpath to existing oldpath */
3716 struct yaffs_obj *obj = NULL;
3717 struct yaffs_obj *lnk = NULL;
3718 struct yaffs_obj *obj_dir = NULL;
3719 struct yaffs_obj *lnk_dir = NULL;
3727 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3728 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3729 yaffsfs_SetError(-EFAULT);
3733 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3734 yaffsfs_SetError(-ENAMETOOLONG);
3740 obj = yaffsfs_FindObject(reldir, oldpath, 0, 1,
3741 &obj_dir, ¬DirObj, &objLoop);
3742 lnk = yaffsfs_FindObject(reldir, linkpath, 0, 0, NULL, NULL, NULL);
3743 lnk_dir = yaffsfs_FindDirectory(reldir, linkpath, &newname,
3744 0, ¬DirLnk, &lnkLoop);
3746 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3747 yaffsfs_SetError(-ENOTDIR);
3748 else if (objLoop || lnkLoop)
3749 yaffsfs_SetError(-ELOOP);
3750 else if (!obj_dir || !lnk_dir || !obj)
3751 yaffsfs_SetError(-ENOENT);
3752 else if (obj->my_dev->read_only)
3753 yaffsfs_SetError(-EROFS);
3754 else if (yaffsfs_TooManyObjects(obj->my_dev))
3755 yaffsfs_SetError(-ENFILE);
3757 yaffsfs_SetError(-EEXIST);
3758 else if (lnk_dir->my_dev != obj->my_dev)
3759 yaffsfs_SetError(-EXDEV);
3761 retVal = yaffsfs_CheckNameLength(newname);
3764 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3768 yaffsfs_SetError(-ENOSPC);
3777 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3779 return yaffs_link_reldir(NULL, oldpath, linkpath);
3782 int yaffs_mknod_reldir(struct yaffs_obj *reldir, const YCHAR *pathname,
3783 mode_t mode, dev_t dev_val)
3790 yaffsfs_SetError(-EINVAL);
3794 int yaffs_mknod_reldev(struct yaffs_dev *dev, const YCHAR *pathname, mode_t mode, dev_t dev_val)
3796 return yaffs_mknod_reldir(ROOT_DIR(dev), pathname, mode, dev_val);
3799 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev_val)
3801 return yaffs_mknod_reldir(NULL, pathname, mode, dev_val);
3805 * D E B U G F U N C T I O N S
3810 * Returns number of handles attached to the object
3812 int yaffs_n_handles_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3814 struct yaffs_obj *obj;
3816 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3817 yaffsfs_SetError(-EFAULT);
3821 if (yaffsfs_CheckPath(path) < 0) {
3822 yaffsfs_SetError(-ENAMETOOLONG);
3826 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
3829 return yaffsfs_CountHandles(obj);
3834 int yaffs_n_handles(const YCHAR *path)
3836 return yaffs_n_handles_reldir(NULL, path);
3839 int yaffs_get_error(void)
3841 return yaffsfs_GetLastError();
3844 int yaffs_set_error(int error)
3846 yaffsfs_SetError(error);
3850 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3860 reldir = yaffsfs_FindRoot(path, &rest);
3863 struct yaffs_dev *dev = reldir->my_dev;
3866 "n_page_writes.......... %d\n"
3867 "n_page_reads........... %d\n"
3868 "n_erasures....... %d\n"
3869 "n_gc_copies............ %d\n"
3870 "garbageCollections... %d\n"
3871 "passiveGarbageColl'ns %d\n"
3877 dev->garbageCollections, dev->passiveGarbageCollections);
3884 int yaffs_dump_dev(const YCHAR *path)
3886 return yaffs_dump_dev_reldir(NULL, path);