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);
751 obj = yaffs_find_by_name(dir, name);
756 obj = yaffs_get_equivalent_obj(obj);
761 /*************************************************************************
762 * Start of yaffsfs visible functions.
763 *************************************************************************/
765 int yaffs_dup(int handle)
767 int newHandleNumber = -1;
768 struct yaffsfs_FileDes *existingFD = NULL;
769 struct yaffsfs_Handle *existingHandle = NULL;
770 struct yaffsfs_Handle *newHandle = NULL;
773 existingHandle = yaffsfs_HandleToPointer(handle);
774 existingFD = yaffsfs_HandleToFileDes(handle);
776 newHandleNumber = yaffsfs_NewHandle(&newHandle);
778 newHandle->fdId = existingHandle->fdId;
779 existingFD->handleCount++;
785 yaffsfs_SetError(-EBADF);
787 yaffsfs_SetError(-ENOMEM);
789 return newHandleNumber;
793 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
795 int current_objects = dev->n_obj - dev->n_deleted_files;
797 if (dev->param.max_objects && current_objects > dev->param.max_objects)
803 int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
804 int oflag, int mode, int sharing)
806 struct yaffs_obj *obj = NULL;
807 struct yaffs_obj *dir = NULL;
810 struct yaffsfs_FileDes *fd = NULL;
813 int errorReported = 0;
814 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
815 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
816 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
817 u8 sharedReadAllowed;
818 u8 sharedWriteAllowed;
828 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
829 yaffsfs_SetError(-EFAULT);
833 if (yaffsfs_CheckPath(path) < 0) {
834 yaffsfs_SetError(-ENAMETOOLONG);
838 /* O_EXCL only has meaning if O_CREAT is specified */
839 if (!(oflag & O_CREAT))
842 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
843 if ((oflag & O_CREAT) & (oflag & O_EXCL))
846 /* Todo: Are there any more flag combos to sanitise ? */
848 /* Figure out if reading or writing is requested */
850 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
851 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
855 handle = yaffsfs_NewHandleAndFileDes();
858 yaffsfs_SetError(-ENFILE);
862 fd = yaffsfs_HandleToFileDes(handle);
864 /* try to find the exisiting object */
865 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
867 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
870 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
871 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
876 /* The file already exists or it might be a directory */
877 is_dir = (obj->variant_type ==
878 YAFFS_OBJECT_TYPE_DIRECTORY);
880 /* A directory can't be opened except for read */
882 (writeRequested || !readRequested ||
883 (oflag & ~O_RDONLY))) {
885 yaffsfs_SetError(-EISDIR);
890 dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
893 yaffsfs_SetError(-ENFILE);
898 /* Open should fail if O_CREAT and O_EXCL are specified
899 * for a file that exists.
901 if (!errorReported &&
902 (oflag & O_EXCL) && (oflag & O_CREAT)) {
904 yaffsfs_SetError(-EEXIST);
908 /* Check file permissions */
909 if (readRequested && !(obj->yst_mode & S_IREAD))
912 if (writeRequested && !(obj->yst_mode & S_IWRITE))
915 if (!errorReported && writeRequested &&
916 obj->my_dev->read_only) {
918 yaffsfs_SetError(-EROFS);
922 if (openDenied && !errorReported) {
923 yaffsfs_SetError(-EACCES);
927 /* Check sharing of an existing object. */
929 struct yaffsfs_FileDes *fdx;
932 sharedReadAllowed = 1;
933 sharedWriteAllowed = 1;
936 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
937 fdx = &yaffsfs_fd[i];
938 if (fdx->handleCount > 0 &&
940 yaffsfs_inode[fdx->inodeId].iObj
943 sharedReadAllowed = 0;
944 if (!fdx->shareWrite)
945 sharedWriteAllowed = 0;
953 if ((!sharedReadAllowed && readRequested) ||
954 (!shareRead && alreadyReading) ||
955 (!sharedWriteAllowed && writeRequested) ||
956 (!shareWrite && alreadyWriting)) {
958 yaffsfs_SetError(-EBUSY);
965 /* If we could not open an existing object, then let's see if
966 * the directory exists. If not, error.
968 if (!obj && !errorReported) {
969 dir = yaffsfs_FindDirectory(reldir, path, &name, 0,
971 if (!dir && notDir) {
972 yaffsfs_SetError(-ENOTDIR);
975 yaffsfs_SetError(-ELOOP);
978 yaffsfs_SetError(-ENOENT);
983 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
984 /* Let's see if we can create this file */
985 if (dir->my_dev->read_only) {
986 yaffsfs_SetError(-EROFS);
988 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
989 yaffsfs_SetError(-ENFILE);
992 obj = yaffs_create_file(dir, name, mode, 0, 0);
994 if (!obj && !errorReported) {
995 yaffsfs_SetError(-ENOSPC);
1000 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
1001 yaffsfs_SetError(-ENOENT);
1005 if (obj && !openDenied) {
1006 int inodeId = yaffsfs_GetInodeIdForObject(obj);
1010 * Todo: Fix any problem if inodes run out,
1011 * That can't happen if the number of inode
1012 * items >= number of handles.
1016 fd->inodeId = inodeId;
1017 fd->reading = readRequested;
1018 fd->writing = writeRequested;
1019 fd->append = (oflag & O_APPEND) ? 1 : 0;
1020 fd->shareRead = shareRead;
1021 fd->shareWrite = shareWrite;
1029 /* Hook inode to object */
1030 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1032 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1033 yaffs_resize_file(obj, 0);
1035 yaffsfs_PutHandle(handle);
1037 yaffsfs_SetError(0); /* Problem */
1047 int yaffs_open_sharing_reldev(struct yaffs_dev *dev, const YCHAR *path, int oflag,
1048 int mode, int sharing)
1050 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path,
1051 oflag, mode, sharing);
1054 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
1056 return yaffs_open_sharing_reldir(NULL, path, oflag, mode, sharing);
1059 int yaffs_open_reldir(struct yaffs_obj *reldir,const YCHAR *path, int oflag, int mode)
1061 return yaffs_open_sharing_reldir(reldir, path, oflag, mode,
1062 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1065 int yaffs_open_reldev(struct yaffs_dev *dev,const YCHAR *path, int oflag, int mode)
1067 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path, oflag, mode,
1068 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1071 int yaffs_open(const YCHAR *path, int oflag, int mode)
1073 return yaffs_open_reldir(NULL, path, oflag, mode);
1076 static int yaffs_Dofsync(int handle, int datasync)
1079 struct yaffs_obj *obj;
1083 obj = yaffsfs_HandleToObject(handle);
1086 yaffsfs_SetError(-EBADF);
1087 else if (obj->my_dev->read_only)
1088 yaffsfs_SetError(-EROFS);
1090 yaffs_flush_file(obj, 1, datasync, 0);
1099 int yaffs_fsync(int handle)
1101 return yaffs_Dofsync(handle, 0);
1104 int yaffs_flush(int handle)
1106 return yaffs_fsync(handle);
1109 int yaffs_fdatasync(int handle)
1111 return yaffs_Dofsync(handle, 1);
1114 int yaffs_close(int handle)
1116 struct yaffsfs_Handle *h = NULL;
1117 struct yaffsfs_FileDes *f;
1118 struct yaffs_obj *obj = NULL;
1123 h = yaffsfs_HandleToPointer(handle);
1124 f = yaffsfs_HandleToFileDes(handle);
1125 obj = yaffsfs_HandleToObject(handle);
1127 if (!h || !obj || !f)
1128 yaffsfs_SetError(-EBADF);
1132 yaffs_flush_file(obj, 1, 0, 1);
1133 yaffsfs_PutHandle(handle);
1142 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1143 int isPread, Y_LOFF_T offset)
1145 struct yaffsfs_FileDes *fd = NULL;
1146 struct yaffs_obj *obj = NULL;
1148 Y_LOFF_T startPos = 0;
1149 Y_LOFF_T endPos = 0;
1154 u8 *buf = (u8 *) vbuf;
1156 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1157 yaffsfs_SetError(-EFAULT);
1162 fd = yaffsfs_HandleToFileDes(handle);
1163 obj = yaffsfs_HandleToObject(handle);
1167 yaffsfs_SetError(-EBADF);
1169 } else if (!fd->reading) {
1170 /* Not a reading handle */
1171 yaffsfs_SetError(-EINVAL);
1173 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1174 yaffsfs_SetError(-EINVAL);
1180 startPos = fd->v.position;
1184 if (yaffs_get_obj_length(obj) > pos)
1185 maxRead = yaffs_get_obj_length(obj) - pos;
1189 if (nbyte > maxRead)
1192 yaffsfs_GetHandle(handle);
1194 endPos = pos + nbyte;
1196 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1197 nbyte > YAFFS_MAX_FILE_SIZE ||
1198 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1204 nToRead = YAFFSFS_RW_SIZE -
1205 (pos & (YAFFSFS_RW_SIZE - 1));
1206 if (nToRead > nbyte)
1210 * Need to reverify object in case the device was
1211 * unmounted in another thread.
1213 obj = yaffsfs_HandleToObject(handle);
1217 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1225 if (nRead == nToRead)
1228 nbyte = 0; /* no more to read */
1237 yaffsfs_PutHandle(handle);
1241 fd->v.position = startPos + totalRead;
1243 yaffsfs_SetError(-EINVAL);
1250 return (totalRead >= 0) ? totalRead : -1;
1254 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1256 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1259 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1261 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1264 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1265 int isPwrite, Y_LOFF_T offset)
1267 struct yaffsfs_FileDes *fd = NULL;
1268 struct yaffs_obj *obj = NULL;
1270 Y_LOFF_T startPos = 0;
1273 int totalWritten = 0;
1274 int write_trhrough = 0;
1276 const u8 *buf = (const u8 *)vbuf;
1278 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1279 yaffsfs_SetError(-EFAULT);
1284 fd = yaffsfs_HandleToFileDes(handle);
1285 obj = yaffsfs_HandleToObject(handle);
1289 yaffsfs_SetError(-EBADF);
1291 } else if (!fd->writing) {
1292 yaffsfs_SetError(-EINVAL);
1294 } else if (obj->my_dev->read_only) {
1295 yaffsfs_SetError(-EROFS);
1299 startPos = yaffs_get_obj_length(obj);
1303 startPos = fd->v.position;
1305 yaffsfs_GetHandle(handle);
1307 endPos = pos + nbyte;
1309 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1310 nbyte > YAFFS_MAX_FILE_SIZE ||
1311 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1318 nToWrite = YAFFSFS_RW_SIZE -
1319 (pos & (YAFFSFS_RW_SIZE - 1));
1320 if (nToWrite > nbyte)
1324 * Need to reverify object in case the device was
1325 * remounted or unmounted in another thread.
1327 obj = yaffsfs_HandleToObject(handle);
1328 if (!obj || obj->my_dev->read_only)
1332 yaffs_wr_file(obj, buf, pos, nToWrite,
1335 totalWritten += nWritten;
1340 if (nWritten == nToWrite)
1345 if (nWritten < 1 && totalWritten < 1) {
1346 yaffsfs_SetError(-ENOSPC);
1356 yaffsfs_PutHandle(handle);
1359 if (totalWritten > 0)
1360 fd->v.position = startPos + totalWritten;
1362 yaffsfs_SetError(-EINVAL);
1368 return (totalWritten >= 0) ? totalWritten : -1;
1371 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1373 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1376 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1378 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1381 int yaffs_truncate_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1384 struct yaffs_obj *obj = NULL;
1385 struct yaffs_obj *dir = NULL;
1386 int result = YAFFS_FAIL;
1390 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1391 yaffsfs_SetError(-EFAULT);
1395 if (yaffsfs_CheckPath(path) < 0) {
1396 yaffsfs_SetError(-ENAMETOOLONG);
1402 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1403 obj = yaffsfs_FollowLink(obj, 0, &loop);
1406 yaffsfs_SetError(-ENOTDIR);
1408 yaffsfs_SetError(-ELOOP);
1409 else if (!dir || !obj)
1410 yaffsfs_SetError(-ENOENT);
1411 else if (obj->my_dev->read_only)
1412 yaffsfs_SetError(-EROFS);
1413 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1414 yaffsfs_SetError(-EISDIR);
1415 else if (obj->my_dev->read_only)
1416 yaffsfs_SetError(-EROFS);
1417 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1418 yaffsfs_SetError(-EINVAL);
1420 result = yaffs_resize_file(obj, new_size);
1424 return (result) ? 0 : -1;
1427 int yaffs_truncate_reldev(struct yaffs_dev *dev, const YCHAR *path,
1430 return yaffs_truncate_reldir(ROOT_DIR(dev), path, new_size);
1433 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1435 return yaffs_truncate_reldir(NULL, path, new_size);
1438 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1440 struct yaffsfs_FileDes *fd = NULL;
1441 struct yaffs_obj *obj = NULL;
1445 fd = yaffsfs_HandleToFileDes(handle);
1446 obj = yaffsfs_HandleToObject(handle);
1450 yaffsfs_SetError(-EBADF);
1451 else if (!fd->writing)
1452 yaffsfs_SetError(-EINVAL);
1453 else if (obj->my_dev->read_only)
1454 yaffsfs_SetError(-EROFS);
1455 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1456 yaffsfs_SetError(-EINVAL);
1458 /* resize the file */
1459 result = yaffs_resize_file(obj, new_size);
1462 return (result) ? 0 : -1;
1466 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1468 struct yaffsfs_FileDes *fd = NULL;
1469 struct yaffs_obj *obj = NULL;
1471 Y_LOFF_T fSize = -1;
1474 fd = yaffsfs_HandleToFileDes(handle);
1475 obj = yaffsfs_HandleToObject(handle);
1478 yaffsfs_SetError(-EBADF);
1479 else if (offset > YAFFS_MAX_FILE_SIZE)
1480 yaffsfs_SetError(-EINVAL);
1482 if (whence == SEEK_SET) {
1485 } else if (whence == SEEK_CUR) {
1486 if ((fd->v.position + offset) >= 0)
1487 pos = (fd->v.position + offset);
1488 } else if (whence == SEEK_END) {
1489 fSize = yaffs_get_obj_length(obj);
1490 if (fSize >= 0 && (fSize + offset) >= 0)
1491 pos = fSize + offset;
1494 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1495 fd->v.position = pos;
1497 yaffsfs_SetError(-EINVAL);
1507 static int yaffsfs_DoUnlink_reldir(struct yaffs_obj *reldir,
1508 const YCHAR *path, int isDirectory)
1510 struct yaffs_obj *dir = NULL;
1511 struct yaffs_obj *obj = NULL;
1513 int result = YAFFS_FAIL;
1517 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1518 yaffsfs_SetError(-EFAULT);
1522 if (yaffsfs_CheckPath(path) < 0) {
1523 yaffsfs_SetError(-ENAMETOOLONG);
1529 obj = yaffsfs_FindObject(reldir, path, 0, 0, NULL, NULL, NULL);
1530 dir = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
1533 yaffsfs_SetError(-ENOTDIR);
1535 yaffsfs_SetError(-ELOOP);
1537 yaffsfs_SetError(-ENOENT);
1538 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1539 yaffsfs_SetError(-EINVAL);
1541 yaffsfs_SetError(-ENOENT);
1542 else if (obj->my_dev->read_only)
1543 yaffsfs_SetError(-EROFS);
1544 else if (!isDirectory &&
1545 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1546 yaffsfs_SetError(-EISDIR);
1547 else if (isDirectory &&
1548 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1549 yaffsfs_SetError(-ENOTDIR);
1550 else if (isDirectory && obj == obj->my_dev->root_dir)
1551 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1553 result = yaffs_unlinker(dir, name);
1555 if (result == YAFFS_FAIL && isDirectory)
1556 yaffsfs_SetError(-ENOTEMPTY);
1561 return (result == YAFFS_FAIL) ? -1 : 0;
1564 int yaffs_unlink_reldir(struct yaffs_obj *reldir, const YCHAR *path)
1566 return yaffsfs_DoUnlink_reldir(reldir, path, 0);
1569 int yaffs_unlink_reldev(struct yaffs_dev *dev, const YCHAR *path)
1571 return yaffsfs_DoUnlink_reldir(ROOT_DIR(dev), path, 0);
1574 int yaffs_unlink(const YCHAR *path)
1576 return yaffs_unlink_reldir(NULL, path);
1579 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1581 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1582 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1588 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1590 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1591 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1597 int yaffs_rename_reldir(struct yaffs_obj *reldir,
1598 const YCHAR *oldPath, const YCHAR *newPath)
1600 struct yaffs_obj *olddir = NULL;
1601 struct yaffs_obj *newdir = NULL;
1602 struct yaffs_obj *obj = NULL;
1603 struct yaffs_obj *newobj = NULL;
1606 int result = YAFFS_FAIL;
1607 int rename_allowed = 1;
1613 YCHAR *alt_newpath = NULL;
1615 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1616 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1617 yaffsfs_SetError(-EFAULT);
1621 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1622 yaffsfs_SetError(-ENAMETOOLONG);
1626 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1627 yaffsfs_SetError(-ENOMEM);
1631 newPath = alt_newpath;
1635 olddir = yaffsfs_FindDirectory(reldir, oldPath, &oldname, 0,
1636 ¬OldDir, &oldLoop);
1637 newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
1638 ¬NewDir, &newLoop);
1639 obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
1640 newobj = yaffsfs_FindObject(reldir, newPath, 0, 0, NULL, NULL, NULL);
1642 /* If the object being renamed is a directory and the
1643 * path ended with a "/" then the olddir == obj.
1644 * We pass through NULL for the old name to tell the lower layers
1645 * to use olddir as the object.
1651 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1652 yaffsfs_SetError(-ENOTDIR);
1654 } else if (oldLoop || newLoop) {
1655 yaffsfs_SetError(-ELOOP);
1657 } else if (olddir && oldname &&
1658 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1659 yaffsfs_SetError(-EINVAL);
1661 } else if (!olddir || !newdir || !obj) {
1662 yaffsfs_SetError(-ENOENT);
1664 } else if (obj->my_dev->read_only) {
1665 yaffsfs_SetError(-EROFS);
1667 } else if (rename_file_over_dir(obj, newobj)) {
1668 yaffsfs_SetError(-EISDIR);
1670 } else if (rename_dir_over_file(obj, newobj)) {
1671 yaffsfs_SetError(-ENOTDIR);
1673 } else if (yaffs_is_non_empty_dir(newobj)) {
1674 yaffsfs_SetError(-ENOTEMPTY);
1676 } else if (olddir->my_dev != newdir->my_dev) {
1677 /* Rename must be on same device */
1678 yaffsfs_SetError(-EXDEV);
1680 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1682 * It is a directory, check that it is not being renamed to
1683 * being its own decendent.
1684 * Do this by tracing from the new directory back to the root,
1688 struct yaffs_obj *xx = newdir;
1690 while (rename_allowed && xx) {
1695 if (!rename_allowed)
1696 yaffsfs_SetError(-EINVAL);
1700 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1706 return (result == YAFFS_FAIL) ? -1 : 0;
1709 int yaffs_rename_reldev(struct yaffs_dev *dev, const YCHAR *oldPath,
1710 const YCHAR *newPath)
1712 return yaffs_rename_reldir(ROOT_DIR(dev), oldPath, newPath);
1714 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1716 return yaffs_rename_reldir(NULL, oldPath, newPath);
1719 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1723 obj = yaffs_get_equivalent_obj(obj);
1726 buf->st_dev = (int)obj->my_dev->os_context;
1727 buf->st_ino = obj->obj_id;
1728 buf->st_mode = obj->yst_mode & ~S_IFMT;
1730 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1731 buf->st_mode |= S_IFDIR;
1732 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1733 buf->st_mode |= S_IFLNK;
1734 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1735 buf->st_mode |= S_IFREG;
1737 buf->st_nlink = yaffs_get_obj_link_count(obj);
1740 buf->st_rdev = obj->yst_rdev;
1741 buf->st_size = yaffs_get_obj_length(obj);
1742 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1743 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1745 #if CONFIG_YAFFS_WINCE
1746 buf->yst_wince_atime[0] = obj->win_atime[0];
1747 buf->yst_wince_atime[1] = obj->win_atime[1];
1748 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1749 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1750 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1751 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1753 buf->yst_atime = obj->yst_atime;
1754 buf->yst_ctime = obj->yst_ctime;
1755 buf->yst_mtime = obj->yst_mtime;
1762 static int yaffsfs_DoStatOrLStat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1763 struct yaffs_stat *buf, int doLStat)
1765 struct yaffs_obj *obj = NULL;
1766 struct yaffs_obj *dir = NULL;
1771 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1772 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1773 yaffsfs_SetError(-EFAULT);
1777 if (yaffsfs_CheckPath(path) < 0) {
1778 yaffsfs_SetError(-ENAMETOOLONG);
1784 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1786 if (!doLStat && obj)
1787 obj = yaffsfs_FollowLink(obj, 0, &loop);
1790 yaffsfs_SetError(-ENOTDIR);
1792 yaffsfs_SetError(-ELOOP);
1793 else if (!dir || !obj)
1794 yaffsfs_SetError(-ENOENT);
1796 retVal = yaffsfs_DoStat(obj, buf);
1804 int yaffs_stat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1805 struct yaffs_stat *buf)
1807 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 0);
1810 int yaffs_lstat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1811 struct yaffs_stat *buf)
1813 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 1);
1816 int yaffs_stat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1817 struct yaffs_stat *buf)
1819 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 0);
1822 int yaffs_lstat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1823 struct yaffs_stat *buf)
1825 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 1);
1828 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1830 return yaffs_stat_reldir(NULL, path, buf);
1833 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1835 return yaffs_lstat_reldir(NULL, path, buf);
1838 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1840 struct yaffs_obj *obj;
1844 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1845 yaffsfs_SetError(-EFAULT);
1850 obj = yaffsfs_HandleToObject(fd);
1853 retVal = yaffsfs_DoStat(obj, buf);
1856 yaffsfs_SetError(-EBADF);
1863 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1864 const struct yaffs_utimbuf *buf)
1867 struct yaffs_utimbuf local;
1869 obj = yaffs_get_equivalent_obj(obj);
1871 if (obj && obj->my_dev->read_only) {
1872 yaffsfs_SetError(-EROFS);
1876 #if !CONFIG_YAFFS_WINCE
1878 local.actime = Y_CURRENT_TIME;
1879 local.modtime = local.actime;
1886 obj->yst_atime = buf->actime;
1887 obj->yst_mtime = buf->modtime;
1889 result = yaffs_flush_file(obj, 0, 0, 0);
1890 retVal = result == YAFFS_OK ? 0 : -1;
1897 int yaffs_utime_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1898 const struct yaffs_utimbuf *buf)
1900 struct yaffs_obj *obj = NULL;
1901 struct yaffs_obj *dir = NULL;
1907 yaffsfs_SetError(-EFAULT);
1911 if (yaffsfs_CheckPath(path) < 0) {
1912 yaffsfs_SetError(-ENAMETOOLONG);
1918 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1921 yaffsfs_SetError(-ENOTDIR);
1923 yaffsfs_SetError(-ELOOP);
1924 else if (!dir || !obj)
1925 yaffsfs_SetError(-ENOENT);
1927 retVal = yaffsfs_DoUtime(obj, buf);
1935 int yaffs_utime_reldev(struct yaffs_dev *dev, const YCHAR *path,
1936 const struct yaffs_utimbuf *buf)
1938 return yaffs_utime_reldir(ROOT_DIR(dev), path, buf);
1941 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1943 return yaffs_utime_reldir(NULL, path, buf);
1946 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1948 struct yaffs_obj *obj;
1953 obj = yaffsfs_HandleToObject(fd);
1956 retVal = yaffsfs_DoUtime(obj, buf);
1959 yaffsfs_SetError(-EBADF);
1966 #ifndef CONFIG_YAFFS_WINCE
1967 /* xattrib functions */
1969 static int yaffs_do_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1970 const char *name, const void *data, int size,
1971 int flags, int follow)
1973 struct yaffs_obj *obj;
1974 struct yaffs_obj *dir;
1980 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1981 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1982 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1983 yaffsfs_SetError(-EFAULT);
1987 if (yaffsfs_CheckPath(path) < 0) {
1988 yaffsfs_SetError(-ENAMETOOLONG);
1994 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1997 obj = yaffsfs_FollowLink(obj, 0, &loop);
2000 yaffsfs_SetError(-ENOTDIR);
2002 yaffsfs_SetError(-ELOOP);
2003 else if (!dir || !obj)
2004 yaffsfs_SetError(-ENOENT);
2006 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2008 yaffsfs_SetError(retVal);
2019 int yaffs_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2020 const char *name, const void *data, int size, int flags)
2022 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 1);
2025 int yaffs_setxattr_reldev(struct yaffs_dev *dev, const YCHAR *path,
2026 const char *name, const void *data, int size, int flags)
2028 return yaffs_setxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2031 int yaffs_setxattr(const YCHAR *path, const char *name,
2032 const void *data, int size, int flags)
2034 return yaffs_setxattr_reldir(NULL, path, name, data, size, flags);
2037 int yaffs_lsetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path, const char *name,
2038 const void *data, int size, int flags)
2040 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 0);
2043 int yaffs_lsetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name,
2044 const void *data, int size, int flags)
2046 return yaffs_lsetxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2049 int yaffs_lsetxattr(const YCHAR *path, const char *name,
2050 const void *data, int size, int flags)
2052 return yaffs_lsetxattr_reldir(NULL, path, name, data, size, flags);
2055 int yaffs_fsetxattr(int fd, const char *name,
2056 const void *data, int size, int flags)
2058 struct yaffs_obj *obj;
2062 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2063 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2064 yaffsfs_SetError(-EFAULT);
2069 obj = yaffsfs_HandleToObject(fd);
2072 yaffsfs_SetError(-EBADF);
2074 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2076 yaffsfs_SetError(retVal);
2086 static int yaffs_do_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2087 const char *name, void *data, int size, int follow)
2089 struct yaffs_obj *obj;
2090 struct yaffs_obj *dir;
2095 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2096 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2097 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2098 yaffsfs_SetError(-EFAULT);
2102 if (yaffsfs_CheckPath(path) < 0) {
2103 yaffsfs_SetError(-ENAMETOOLONG);
2109 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2112 obj = yaffsfs_FollowLink(obj, 0, &loop);
2115 yaffsfs_SetError(-ENOTDIR);
2117 yaffsfs_SetError(-ELOOP);
2118 else if (!dir || !obj)
2119 yaffsfs_SetError(-ENOENT);
2121 retVal = yaffs_get_xattrib(obj, name, data, size);
2123 yaffsfs_SetError(retVal);
2133 int yaffs_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2134 const char *name, void *data, int size)
2136 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 1);
2139 int yaffs_getxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2141 return yaffs_getxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2144 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
2146 return yaffs_getxattr_reldir(NULL, path, name, data, size);
2149 int yaffs_lgetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2150 const char *name, void *data, int size)
2152 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 0);
2155 int yaffs_lgetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2157 return yaffs_lgetxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2160 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2162 return yaffs_lgetxattr_reldir(NULL, path, name, data, size);
2165 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2167 struct yaffs_obj *obj;
2171 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2172 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2173 yaffsfs_SetError(-EFAULT);
2178 obj = yaffsfs_HandleToObject(fd);
2181 retVal = yaffs_get_xattrib(obj, name, data, size);
2183 yaffsfs_SetError(retVal);
2188 yaffsfs_SetError(-EBADF);
2195 static int yaffs_do_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2196 char *data, int size, int follow)
2198 struct yaffs_obj *obj = NULL;
2199 struct yaffs_obj *dir = NULL;
2204 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2205 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2206 yaffsfs_SetError(-EFAULT);
2210 if (yaffsfs_CheckPath(path) < 0) {
2211 yaffsfs_SetError(-ENAMETOOLONG);
2217 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2220 obj = yaffsfs_FollowLink(obj, 0, &loop);
2223 yaffsfs_SetError(-ENOTDIR);
2225 yaffsfs_SetError(-ELOOP);
2226 else if (!dir || !obj)
2227 yaffsfs_SetError(-ENOENT);
2229 retVal = yaffs_list_xattrib(obj, data, size);
2231 yaffsfs_SetError(retVal);
2242 int yaffs_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2243 char *data, int size)
2245 return yaffs_do_listxattr_reldir(reldir, path, data, size, 1);
2248 int yaffs_listxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2250 return yaffs_listxattr_reldir(ROOT_DIR(dev), path, data, size);
2253 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2255 return yaffs_listxattr_reldir(NULL, path, data, size);
2258 int yaffs_llistxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2259 char *data, int size)
2261 return yaffs_do_listxattr_reldir(reldir, path, data, size, 0);
2264 int yaffs_llistxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2266 return yaffs_llistxattr_reldir(ROOT_DIR(dev), path, data, size);
2269 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2271 return yaffs_llistxattr_reldir(NULL, path, data, size);
2274 int yaffs_flistxattr(int fd, char *data, int size)
2276 struct yaffs_obj *obj;
2280 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2281 yaffsfs_SetError(-EFAULT);
2286 obj = yaffsfs_HandleToObject(fd);
2289 retVal = yaffs_list_xattrib(obj, data, size);
2291 yaffsfs_SetError(retVal);
2296 yaffsfs_SetError(-EBADF);
2303 static int yaffs_do_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2304 const char *name, int follow)
2306 struct yaffs_obj *obj = NULL;
2307 struct yaffs_obj *dir = NULL;
2312 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2313 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2314 yaffsfs_SetError(-EFAULT);
2318 if (yaffsfs_CheckPath(path) < 0) {
2319 yaffsfs_SetError(-ENAMETOOLONG);
2325 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2328 obj = yaffsfs_FollowLink(obj, 0, &loop);
2331 yaffsfs_SetError(-ENOTDIR);
2333 yaffsfs_SetError(-ELOOP);
2334 else if (!dir || !obj)
2335 yaffsfs_SetError(-ENOENT);
2337 retVal = yaffs_remove_xattrib(obj, name);
2339 yaffsfs_SetError(retVal);
2350 int yaffs_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2353 return yaffs_do_removexattr_reldir(reldir, path, name, 1);
2356 int yaffs_removexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2358 return yaffs_removexattr_reldir(ROOT_DIR(dev), path, name);
2361 int yaffs_removexattr(const YCHAR *path, const char *name)
2363 return yaffs_removexattr_reldir(NULL, path, name);
2366 int yaffs_lremovexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2369 return yaffs_do_removexattr_reldir(reldir, path, name, 0);
2372 int yaffs_lremovexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2374 return yaffs_lremovexattr_reldir(ROOT_DIR(dev), path, name);
2377 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2379 return yaffs_lremovexattr_reldir(NULL, path, name);
2382 int yaffs_fremovexattr(int fd, const char *name)
2384 struct yaffs_obj *obj;
2388 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2389 yaffsfs_SetError(-EFAULT);
2394 obj = yaffsfs_HandleToObject(fd);
2397 retVal = yaffs_remove_xattrib(obj, name);
2399 yaffsfs_SetError(retVal);
2404 yaffsfs_SetError(-EBADF);
2412 #ifdef CONFIG_YAFFS_WINCE
2413 int yaffs_get_wince_times(int fd, unsigned *wctime,
2414 unsigned *watime, unsigned *wmtime)
2416 struct yaffs_obj *obj;
2421 obj = yaffsfs_HandleToObject(fd);
2426 wctime[0] = obj->win_ctime[0];
2427 wctime[1] = obj->win_ctime[1];
2430 watime[0] = obj->win_atime[0];
2431 watime[1] = obj->win_atime[1];
2434 wmtime[0] = obj->win_mtime[0];
2435 wmtime[1] = obj->win_mtime[1];
2441 yaffsfs_SetError(-EBADF);
2448 int yaffs_set_wince_times(int fd,
2449 const unsigned *wctime,
2450 const unsigned *watime, const unsigned *wmtime)
2452 struct yaffs_obj *obj;
2457 obj = yaffsfs_HandleToObject(fd);
2462 obj->win_ctime[0] = wctime[0];
2463 obj->win_ctime[1] = wctime[1];
2466 obj->win_atime[0] = watime[0];
2467 obj->win_atime[1] = watime[1];
2470 obj->win_mtime[0] = wmtime[0];
2471 obj->win_mtime[1] = wmtime[1];
2475 result = yaffs_flush_file(obj, 0, 0, 0);
2479 yaffsfs_SetError(-EBADF);
2488 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2493 obj = yaffs_get_equivalent_obj(obj);
2496 obj->yst_mode = mode;
2498 result = yaffs_flush_file(obj, 0, 0, 0);
2501 return result == YAFFS_OK ? 0 : -1;
2504 int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
2506 struct yaffs_obj *obj = NULL;
2507 struct yaffs_obj *dir = NULL;
2512 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2513 yaffsfs_SetError(-EFAULT);
2517 if (yaffsfs_CheckPath(path) < 0) {
2518 yaffsfs_SetError(-ENAMETOOLONG);
2522 if (amode & ~(R_OK | W_OK | X_OK)) {
2523 yaffsfs_SetError(-EINVAL);
2529 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2530 obj = yaffsfs_FollowLink(obj, 0, &loop);
2533 yaffsfs_SetError(-ENOTDIR);
2535 yaffsfs_SetError(-ELOOP);
2536 else if (!dir || !obj)
2537 yaffsfs_SetError(-ENOENT);
2538 else if ((amode & W_OK) && obj->my_dev->read_only)
2539 yaffsfs_SetError(-EROFS);
2543 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2545 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2547 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2551 yaffsfs_SetError(-EACCES);
2562 int yaffs_access_reldev(struct yaffs_dev *dev, const YCHAR *path, int amode)
2564 return yaffs_access_reldir(ROOT_DIR(dev), path, amode);
2567 int yaffs_access(const YCHAR *path, int amode)
2569 return yaffs_access_reldir(NULL, path, amode);
2572 int yaffs_chmod_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2574 struct yaffs_obj *obj = NULL;
2575 struct yaffs_obj *dir = NULL;
2580 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2581 yaffsfs_SetError(-EFAULT);
2585 if (yaffsfs_CheckPath(path) < 0) {
2586 yaffsfs_SetError(-ENAMETOOLONG);
2590 if (mode & ~(0777)) {
2591 yaffsfs_SetError(-EINVAL);
2597 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2598 obj = yaffsfs_FollowLink(obj, 0, &loop);
2601 yaffsfs_SetError(-ENOTDIR);
2603 yaffsfs_SetError(-ELOOP);
2604 else if (!dir || !obj)
2605 yaffsfs_SetError(-ENOENT);
2606 else if (obj->my_dev->read_only)
2607 yaffsfs_SetError(-EROFS);
2609 retVal = yaffsfs_DoChMod(obj, mode);
2617 int yaffs_chmod_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2619 return yaffs_chmod_reldir(ROOT_DIR(dev), path, mode);
2622 int yaffs_chmod(const YCHAR *path, mode_t mode)
2624 return yaffs_chmod_reldir(NULL, path, mode);
2627 int yaffs_fchmod(int fd, mode_t mode)
2629 struct yaffs_obj *obj;
2632 if (mode & ~(0777)) {
2633 yaffsfs_SetError(-EINVAL);
2638 obj = yaffsfs_HandleToObject(fd);
2641 yaffsfs_SetError(-EBADF);
2642 else if (obj->my_dev->read_only)
2643 yaffsfs_SetError(-EROFS);
2645 retVal = yaffsfs_DoChMod(obj, mode);
2652 int yaffs_mkdir_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2654 struct yaffs_obj *parent = NULL;
2655 struct yaffs_obj *dir = NULL;
2657 YCHAR *alt_path = NULL;
2662 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2663 yaffsfs_SetError(-EFAULT);
2667 if (yaffsfs_CheckPath(path) < 0) {
2668 yaffsfs_SetError(-ENAMETOOLONG);
2672 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2673 yaffsfs_SetError(-ENOMEM);
2680 parent = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
2681 if (!parent && notDir)
2682 yaffsfs_SetError(-ENOTDIR);
2684 yaffsfs_SetError(-ELOOP);
2686 yaffsfs_SetError(-ENOENT);
2687 else if (yaffsfs_TooManyObjects(parent->my_dev))
2688 yaffsfs_SetError(-ENFILE);
2689 else if (yaffs_strnlen(name, 5) == 0) {
2690 /* Trying to make the root itself */
2691 yaffsfs_SetError(-EEXIST);
2692 } else if (parent->my_dev->read_only)
2693 yaffsfs_SetError(-EROFS);
2695 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2698 else if (yaffs_find_by_name(parent, name))
2699 yaffsfs_SetError(-EEXIST); /* name exists */
2701 yaffsfs_SetError(-ENOSPC); /* assume no space */
2711 int yaffs_mkdir_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2713 return yaffs_mkdir_reldir(ROOT_DIR(dev), path, mode);
2716 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2718 return yaffs_mkdir_reldir(NULL, path, mode);
2721 int yaffs_rmdir_reldir(struct yaffs_obj *reldir, const YCHAR *path)
2726 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2727 yaffsfs_SetError(-EFAULT);
2731 if (yaffsfs_CheckPath(path) < 0) {
2732 yaffsfs_SetError(-ENAMETOOLONG);
2736 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2737 yaffsfs_SetError(-ENOMEM);
2742 result = yaffsfs_DoUnlink_reldir(reldir, path, 1);
2749 int yaffs_rmdir_reldev(struct yaffs_dev *dev, const YCHAR *path)
2751 return yaffs_rmdir_reldir(ROOT_DIR(dev), path);
2754 int yaffs_rmdir(const YCHAR *path)
2756 return yaffs_rmdir_reldir(NULL, path);
2760 * The mount/unmount/sync functions act on devices rather than reldirs.
2762 void *yaffs_getdev(const YCHAR *path)
2764 struct yaffs_dev *dev = NULL;
2766 dev = yaffsfs_FindDevice(path, &dummy);
2770 int yaffs_mount_common(struct yaffs_dev *dev, const YCHAR *path,
2771 int read_only, int skip_checkpt)
2774 int result = YAFFS_FAIL;
2777 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2778 yaffsfs_SetError(-EFAULT);
2782 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2784 if (yaffsfs_CheckPath(path) < 0) {
2785 yaffsfs_SetError(-ENAMETOOLONG);
2792 yaffsfs_InitHandles();
2795 dev = yaffsfs_FindMountPoint(path);
2798 if (!dev->is_mounted) {
2799 dev->read_only = read_only ? 1 : 0;
2801 u8 skip = dev->param.skip_checkpt_rd;
2802 dev->param.skip_checkpt_rd = 1;
2803 result = yaffs_guts_initialise(dev);
2804 dev->param.skip_checkpt_rd = skip;
2806 result = yaffs_guts_initialise(dev);
2809 if (result == YAFFS_FAIL)
2810 yaffsfs_SetError(-ENOMEM);
2811 retVal = result ? 0 : -1;
2814 yaffsfs_SetError(-EBUSY);
2816 yaffsfs_SetError(-ENODEV);
2823 int yaffs_mount3_reldev(struct yaffs_dev *dev, int read_only, int skip_checkpt)
2825 return yaffs_mount_common(dev, NULL, read_only, skip_checkpt);
2828 int yaffs_mount3(const YCHAR *path, int read_only, int skip_checkpt)
2830 return yaffs_mount_common(NULL, path, read_only, skip_checkpt);
2833 int yaffs_mount2_reldev(struct yaffs_dev *dev, int readonly)
2835 return yaffs_mount_common(dev, NULL, readonly, 0);
2838 int yaffs_mount2(const YCHAR *path, int readonly)
2840 return yaffs_mount_common(NULL, path, readonly, 0);
2843 int yaffs_mount_reldev(struct yaffs_dev *dev)
2845 return yaffs_mount_common(dev, NULL, 0, 0);
2848 int yaffs_mount(const YCHAR *path)
2850 return yaffs_mount_common(NULL, path, 0, 0);
2853 int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
2859 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2860 yaffsfs_SetError(-EFAULT);
2864 if (yaffsfs_CheckPath(path) < 0) {
2865 yaffsfs_SetError(-ENAMETOOLONG);
2872 dev = yaffsfs_FindDevice(path, &dummy);
2875 if (!dev->is_mounted)
2876 yaffsfs_SetError(-EINVAL);
2877 else if (dev->read_only)
2878 yaffsfs_SetError(-EROFS);
2881 yaffs_flush_whole_cache(dev, 0);
2882 yaffs_checkpoint_save(dev);
2887 yaffsfs_SetError(-ENODEV);
2893 int yaffs_sync_reldev(struct yaffs_dev *dev)
2895 return yaffs_sync_common(dev, NULL);
2898 int yaffs_sync(const YCHAR *path)
2900 return yaffs_sync_common(NULL, path);
2904 static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
2912 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2913 yaffsfs_SetError(-EFAULT);
2917 if (yaffsfs_CheckPath(path) < 0) {
2918 yaffsfs_SetError(-ENAMETOOLONG);
2925 dev = yaffsfs_FindDevice(path, &dummy);
2928 if (!dev->is_mounted)
2929 yaffsfs_SetError(-EINVAL);
2931 retVal = yaffs_bg_gc(dev, urgency);
2933 yaffsfs_SetError(-ENODEV);
2939 /* Background gc functions.
2940 * These return 0 when bg done or greater than 0 when gc has been
2941 * done and there is still a lot of garbage to be cleaned up.
2944 int yaffs_do_background_gc(const YCHAR *path, int urgency)
2946 return yaffsfs_bg_gc_common(NULL, path, urgency);
2949 int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
2951 return yaffsfs_bg_gc_common(dev, NULL, urgency);
2954 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2957 struct yaffs_obj *obj;
2959 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2960 obj = yaffsfs_HandleToObject(i);
2961 if (obj && obj->my_dev == dev)
2967 int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
2968 int force, int read_only)
2972 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2973 yaffsfs_SetError(-EFAULT);
2977 if (yaffsfs_CheckPath(path) < 0) {
2978 yaffsfs_SetError(-ENAMETOOLONG);
2984 dev = yaffsfs_FindMountPoint(path);
2987 if (dev->is_mounted) {
2988 yaffs_flush_whole_cache(dev, 0);
2990 if (force || !yaffsfs_IsDevBusy(dev)) {
2992 yaffs_checkpoint_save(dev);
2993 dev->read_only = read_only ? 1 : 0;
2996 yaffsfs_SetError(-EBUSY);
2999 yaffsfs_SetError(-EINVAL);
3002 yaffsfs_SetError(-ENODEV);
3009 int yaffs_remount_reldev(struct yaffs_dev *dev, int force, int read_only)
3011 return yaffs_remount_common(dev, NULL, force, read_only);
3013 int yaffs_remount(const YCHAR *path, int force, int read_only)
3015 return yaffs_remount_common(NULL, path, force, read_only);
3018 int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
3022 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3023 yaffsfs_SetError(-EFAULT);
3027 if (yaffsfs_CheckPath(path) < 0) {
3028 yaffsfs_SetError(-ENAMETOOLONG);
3034 dev = yaffsfs_FindMountPoint(path);
3037 if (dev->is_mounted) {
3039 yaffs_flush_whole_cache(dev, 0);
3040 yaffs_checkpoint_save(dev);
3041 inUse = yaffsfs_IsDevBusy(dev);
3042 if (!inUse || force) {
3044 yaffsfs_BreakDeviceHandles(dev);
3045 yaffs_deinitialise(dev);
3049 yaffsfs_SetError(-EBUSY);
3052 yaffsfs_SetError(-EINVAL);
3055 yaffsfs_SetError(-ENODEV);
3062 int yaffs_unmount2_reldev(struct yaffs_dev *dev, int force)
3064 return yaffs_unmount2_common(dev, NULL, force);
3067 int yaffs_unmount2(const YCHAR *path, int force)
3069 return yaffs_unmount2_common(NULL, path, force);
3072 int yaffs_unmount_reldev(struct yaffs_dev *dev)
3074 return yaffs_unmount2_reldev(dev, 0);
3077 int yaffs_unmount(const YCHAR *path)
3079 return yaffs_unmount2(path, 0);
3082 int yaffs_format_common(struct yaffs_dev *dev,
3085 int force_unmount_flag,
3093 yaffsfs_SetError(-EFAULT);
3097 if (yaffsfs_CheckPath(path) < 0) {
3098 yaffsfs_SetError(-ENAMETOOLONG);
3105 dev = yaffsfs_FindMountPoint(path);
3108 int was_mounted = dev->is_mounted;
3110 if (dev->is_mounted && unmount_flag) {
3112 yaffs_flush_whole_cache(dev, 0);
3113 yaffs_checkpoint_save(dev);
3114 inUse = yaffsfs_IsDevBusy(dev);
3115 if (!inUse || force_unmount_flag) {
3117 yaffsfs_BreakDeviceHandles(dev);
3118 yaffs_deinitialise(dev);
3122 if(dev->is_mounted) {
3123 yaffsfs_SetError(-EBUSY);
3126 yaffs_guts_format_dev(dev);
3127 if(was_mounted && remount_flag) {
3128 result = yaffs_guts_initialise(dev);
3129 if (result == YAFFS_FAIL) {
3130 yaffsfs_SetError(-ENOMEM);
3136 yaffsfs_SetError(-ENODEV);
3145 int yaffs_format_reldev(struct yaffs_dev *dev,
3147 int force_unmount_flag,
3150 return yaffs_format_common(dev, NULL, unmount_flag,
3151 force_unmount_flag, remount_flag);
3154 int yaffs_format(const YCHAR *path,
3156 int force_unmount_flag,
3159 return yaffs_format_common(NULL, path, unmount_flag,
3160 force_unmount_flag, remount_flag);
3163 Y_LOFF_T yaffs_freespace_common(struct yaffs_dev *dev, const YCHAR *path)
3165 Y_LOFF_T retVal = -1;
3169 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3170 yaffsfs_SetError(-EFAULT);
3174 if (yaffsfs_CheckPath(path) < 0) {
3175 yaffsfs_SetError(-ENAMETOOLONG);
3182 dev = yaffsfs_FindDevice(path, &dummy);
3183 if (dev && dev->is_mounted) {
3184 retVal = yaffs_get_n_free_chunks(dev);
3185 retVal *= dev->data_bytes_per_chunk;
3188 yaffsfs_SetError(-EINVAL);
3194 Y_LOFF_T yaffs_freespace_reldev(struct yaffs_dev *dev)
3196 return yaffs_freespace_common(dev, NULL);
3199 Y_LOFF_T yaffs_freespace(const YCHAR *path)
3201 return yaffs_freespace_common(NULL, path);
3204 Y_LOFF_T yaffs_totalspace_common(struct yaffs_dev *dev, const YCHAR *path)
3206 Y_LOFF_T retVal = -1;
3210 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3211 yaffsfs_SetError(-EFAULT);
3215 if (yaffsfs_CheckPath(path) < 0) {
3216 yaffsfs_SetError(-ENAMETOOLONG);
3223 dev = yaffsfs_FindDevice(path, &dummy);
3224 if (dev && dev->is_mounted) {
3225 retVal = (dev->param.end_block - dev->param.start_block + 1) -
3226 dev->param.n_reserved_blocks;
3227 retVal *= dev->param.chunks_per_block;
3228 retVal *= dev->data_bytes_per_chunk;
3231 yaffsfs_SetError(-EINVAL);
3237 Y_LOFF_T yaffs_totalspace_reldev(struct yaffs_dev *dev)
3239 return yaffs_totalspace_common(dev, NULL);
3242 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
3244 return yaffs_totalspace_common(NULL, path);
3247 int yaffs_inodecount_common(struct yaffs_dev *dev, const YCHAR *path)
3249 Y_LOFF_T retVal = -1;
3253 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3254 yaffsfs_SetError(-EFAULT);
3258 if (yaffsfs_CheckPath(path) < 0) {
3259 yaffsfs_SetError(-ENAMETOOLONG);
3266 dev = yaffsfs_FindDevice(path, &dummy);
3267 if (dev && dev->is_mounted) {
3268 int n_obj = dev->n_obj;
3269 if (n_obj > dev->n_hardlinks)
3270 retVal = n_obj - dev->n_hardlinks;
3274 yaffsfs_SetError(-EINVAL);
3280 int yaffs_inodecount_reldev(struct yaffs_dev *dev)
3282 return yaffs_inodecount_common(dev, NULL);
3285 int yaffs_inodecount(const YCHAR *path)
3287 return yaffs_inodecount_common(NULL, path);
3290 void yaffs_add_device(struct yaffs_dev *dev)
3292 struct list_head *cfg;
3293 /* First check that the device is not in the list. */
3295 list_for_each(cfg, &yaffsfs_deviceList) {
3296 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
3300 dev->is_mounted = 0;
3301 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
3303 if (!dev->dev_list.next)
3304 INIT_LIST_HEAD(&dev->dev_list);
3306 list_add(&dev->dev_list, &yaffsfs_deviceList);
3311 void yaffs_remove_device(struct yaffs_dev *dev)
3313 list_del_init(&dev->dev_list);
3316 /* Functions to iterate through devices. NB Use with extreme care! */
3318 static struct list_head *dev_iterator;
3319 void yaffs_dev_rewind(void)
3321 dev_iterator = yaffsfs_deviceList.next;
3324 struct yaffs_dev *yaffs_next_dev(void)
3326 struct yaffs_dev *retval;
3330 if (dev_iterator == &yaffsfs_deviceList)
3333 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
3334 dev_iterator = dev_iterator->next;
3338 /* Directory search stuff. */
3340 static struct list_head search_contexts;
3342 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
3346 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3350 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
3351 dsc->nextReturn = NULL;
3354 list_entry(dsc->dirObj->variant.dir_variant.
3355 children.next, struct yaffs_obj,
3358 /* Hey someone isn't playing nice! */
3362 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
3366 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3368 if (dsc->nextReturn == NULL ||
3369 list_empty(&dsc->dirObj->variant.dir_variant.children))
3370 dsc->nextReturn = NULL;
3372 struct list_head *next = dsc->nextReturn->siblings.next;
3374 if (next == &dsc->dirObj->variant.dir_variant.children)
3375 dsc->nextReturn = NULL; /* end of list */
3377 dsc->nextReturn = list_entry(next,
3382 /* Hey someone isn't playing nice! */
3386 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
3389 struct list_head *i;
3390 struct yaffsfs_DirSearchContext *dsc;
3392 /* if search contexts not initilised then skip */
3393 if (!search_contexts.next)
3396 /* Iterate through the directory search contexts.
3397 * If any are the one being removed, then advance the dsc to
3398 * the next one to prevent a hanging ptr.
3400 list_for_each(i, &search_contexts) {
3402 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
3404 if (dsc->nextReturn == obj)
3405 yaffsfs_DirAdvance(dsc);
3411 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
3412 const YCHAR *dirname)
3414 yaffs_DIR *dir = NULL;
3415 struct yaffs_obj *obj = NULL;
3416 struct yaffsfs_DirSearchContext *dsc = NULL;
3420 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3421 yaffsfs_SetError(-EFAULT);
3425 if (yaffsfs_CheckPath(dirname) < 0) {
3426 yaffsfs_SetError(-ENAMETOOLONG);
3430 obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, ¬Dir, &loop);
3431 obj = yaffsfs_FollowLink(obj, 0, &loop);
3434 yaffsfs_SetError(-ENOTDIR);
3436 yaffsfs_SetError(-ELOOP);
3438 yaffsfs_SetError(-ENOENT);
3439 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3440 yaffsfs_SetError(-ENOTDIR);
3444 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3445 if (!yaffsfs_dsc[i].inUse)
3446 dsc = &yaffsfs_dsc[i];
3449 dir = (yaffs_DIR *) dsc;
3452 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3455 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3456 INIT_LIST_HEAD(&dsc->others);
3458 if (!search_contexts.next)
3459 INIT_LIST_HEAD(&search_contexts);
3461 list_add(&dsc->others, &search_contexts);
3462 yaffsfs_SetDirRewound(dsc);
3468 yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
3473 ret = yaffsfs_opendir_reldir_no_lock(reldir, dirname);
3478 yaffs_DIR *yaffs_opendir_reldev(struct yaffs_dev *dev, const YCHAR *dirname)
3480 return yaffs_opendir_reldir(ROOT_DIR(dev), dirname);
3483 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3485 return yaffs_opendir_reldir(NULL, dirname);
3488 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3490 struct yaffsfs_DirSearchContext *dsc;
3491 struct yaffs_dirent *retVal = NULL;
3493 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3496 if (dsc && dsc->inUse) {
3497 yaffsfs_SetError(0);
3498 if (dsc->nextReturn) {
3500 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3501 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3502 dsc->de.d_off = dsc->offset++;
3503 yaffs_get_obj_name(dsc->nextReturn,
3504 dsc->de.d_name, NAME_MAX);
3505 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3506 /* this should not happen! */
3507 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3509 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3511 yaffsfs_DirAdvance(dsc);
3515 yaffsfs_SetError(-EBADF);
3520 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3522 struct yaffs_dirent *ret;
3525 ret = yaffsfs_readdir_no_lock(dirp);
3530 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3532 struct yaffsfs_DirSearchContext *dsc;
3534 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3536 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3539 yaffsfs_SetDirRewound(dsc);
3543 void yaffs_rewinddir(yaffs_DIR *dirp)
3546 yaffsfs_rewinddir_no_lock(dirp);
3550 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3552 struct yaffs_dirent *ret = NULL;
3553 struct yaffsfs_FileDes *f;
3556 f = yaffsfs_HandleToFileDes(fd);
3558 ret = yaffsfs_readdir_no_lock(f->v.dir);
3563 void yaffs_rewinddir_fd(int fd)
3565 struct yaffsfs_FileDes *f;
3568 f = yaffsfs_HandleToFileDes(fd);
3570 yaffsfs_rewinddir_no_lock(f->v.dir);
3575 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3577 struct yaffsfs_DirSearchContext *dsc;
3579 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3581 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3582 yaffsfs_SetError(-EFAULT);
3587 list_del(&dsc->others); /* unhook from list */
3591 int yaffs_closedir(yaffs_DIR *dirp)
3596 ret = yaffsfs_closedir_no_lock(dirp);
3601 /* End of directory stuff */
3603 int yaffs_symlink_reldir(struct yaffs_obj *reldir,
3604 const YCHAR *oldpath, const YCHAR *newpath)
3606 struct yaffs_obj *parent = NULL;
3607 struct yaffs_obj *obj;
3610 int mode = 0; /* ignore for now */
3614 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3615 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3616 yaffsfs_SetError(-EFAULT);
3620 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3621 yaffsfs_SetError(-ENAMETOOLONG);
3626 parent = yaffsfs_FindDirectory(reldir, newpath, &name, 0, ¬Dir, &loop);
3627 if (!parent && notDir)
3628 yaffsfs_SetError(-ENOTDIR);
3630 yaffsfs_SetError(-ELOOP);
3631 else if (!parent || yaffs_strnlen(name, 5) < 1)
3632 yaffsfs_SetError(-ENOENT);
3633 else if (yaffsfs_TooManyObjects(parent->my_dev))
3634 yaffsfs_SetError(-ENFILE);
3635 else if (parent->my_dev->read_only)
3636 yaffsfs_SetError(-EROFS);
3638 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3641 else if (yaffsfs_FindObject(reldir, newpath, 0, 0,
3643 yaffsfs_SetError(-EEXIST);
3645 yaffsfs_SetError(-ENOSPC);
3653 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3655 return yaffs_symlink_reldir(NULL, oldpath, newpath);
3658 int yaffs_readlink_reldir(struct yaffs_obj *reldir,const YCHAR *path,
3659 YCHAR *buf, int bufsiz)
3661 struct yaffs_obj *obj = NULL;
3662 struct yaffs_obj *dir = NULL;
3667 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3668 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3669 yaffsfs_SetError(-EFAULT);
3675 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
3678 yaffsfs_SetError(-ENOTDIR);
3680 yaffsfs_SetError(-ELOOP);
3681 else if (!dir || !obj)
3682 yaffsfs_SetError(-ENOENT);
3683 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3684 yaffsfs_SetError(-EINVAL);
3686 YCHAR *alias = obj->variant.symlink_variant.alias;
3687 memset(buf, 0, bufsiz);
3688 yaffs_strncpy(buf, alias, bufsiz - 1);
3694 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3696 return yaffs_readlink_reldir(NULL, path, buf, bufsiz);
3699 int yaffs_link_reldir(struct yaffs_obj *reldir,
3700 const YCHAR *oldpath, const YCHAR *linkpath)
3702 /* Creates a link called newpath to existing oldpath */
3703 struct yaffs_obj *obj = NULL;
3704 struct yaffs_obj *lnk = NULL;
3705 struct yaffs_obj *obj_dir = NULL;
3706 struct yaffs_obj *lnk_dir = NULL;
3714 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3715 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3716 yaffsfs_SetError(-EFAULT);
3720 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3721 yaffsfs_SetError(-ENAMETOOLONG);
3727 obj = yaffsfs_FindObject(reldir, oldpath, 0, 1,
3728 &obj_dir, ¬DirObj, &objLoop);
3729 lnk = yaffsfs_FindObject(reldir, linkpath, 0, 0, NULL, NULL, NULL);
3730 lnk_dir = yaffsfs_FindDirectory(reldir, linkpath, &newname,
3731 0, ¬DirLnk, &lnkLoop);
3733 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3734 yaffsfs_SetError(-ENOTDIR);
3735 else if (objLoop || lnkLoop)
3736 yaffsfs_SetError(-ELOOP);
3737 else if (!obj_dir || !lnk_dir || !obj)
3738 yaffsfs_SetError(-ENOENT);
3739 else if (obj->my_dev->read_only)
3740 yaffsfs_SetError(-EROFS);
3741 else if (yaffsfs_TooManyObjects(obj->my_dev))
3742 yaffsfs_SetError(-ENFILE);
3744 yaffsfs_SetError(-EEXIST);
3745 else if (lnk_dir->my_dev != obj->my_dev)
3746 yaffsfs_SetError(-EXDEV);
3748 retVal = yaffsfs_CheckNameLength(newname);
3751 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3755 yaffsfs_SetError(-ENOSPC);
3764 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3766 return yaffs_link_reldir(NULL, oldpath, linkpath);
3769 int yaffs_mknod_reldir(struct yaffs_obj *reldir, const YCHAR *pathname,
3770 mode_t mode, dev_t dev_val)
3777 yaffsfs_SetError(-EINVAL);
3781 int yaffs_mknod_reldev(struct yaffs_dev *dev, const YCHAR *pathname, mode_t mode, dev_t dev_val)
3783 return yaffs_mknod_reldir(ROOT_DIR(dev), pathname, mode, dev_val);
3786 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev_val)
3788 return yaffs_mknod_reldir(NULL, pathname, mode, dev_val);
3792 * D E B U G F U N C T I O N S
3797 * Returns number of handles attached to the object
3799 int yaffs_n_handles_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3801 struct yaffs_obj *obj;
3803 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3804 yaffsfs_SetError(-EFAULT);
3808 if (yaffsfs_CheckPath(path) < 0) {
3809 yaffsfs_SetError(-ENAMETOOLONG);
3813 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
3816 return yaffsfs_CountHandles(obj);
3821 int yaffs_n_handles(const YCHAR *path)
3823 return yaffs_n_handles_reldir(NULL, path);
3826 int yaffs_get_error(void)
3828 return yaffsfs_GetLastError();
3831 int yaffs_set_error(int error)
3833 yaffsfs_SetError(error);
3837 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3847 reldir = yaffsfs_FindRoot(path, &rest);
3850 struct yaffs_dev *dev = reldir->my_dev;
3853 "n_page_writes.......... %d\n"
3854 "n_page_reads........... %d\n"
3855 "n_erasures....... %d\n"
3856 "n_gc_copies............ %d\n"
3857 "garbageCollections... %d\n"
3858 "passiveGarbageColl'ns %d\n"
3864 dev->garbageCollections, dev->passiveGarbageCollections);
3871 int yaffs_dump_dev(const YCHAR *path)
3873 return yaffs_dump_dev_reldir(NULL, path);