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);
521 while (matching && *p && *leftOver) {
522 /* Skip over any /s */
523 while (yaffsfs_IsPathDivider(*p))
526 /* Skip over any /s */
527 while (yaffsfs_IsPathDivider(*leftOver))
530 /* Now match the text part */
532 *p && !yaffsfs_IsPathDivider(*p) &&
533 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
534 if (yaffsfs_Match(*p, *leftOver)) {
544 /* Skip over any /s in leftOver */
545 while (yaffsfs_IsPathDivider(*leftOver))
548 /*Skip over any /s in p */
549 while (yaffsfs_IsPathDivider(*p))
552 /* p should now be at the end of the string if fully matched */
556 if (matching && (thisMatchLength > longestMatch)) {
558 *restOfPath = (YCHAR *) leftOver;
560 longestMatch = thisMatchLength;
567 static int yaffsfs_CheckPath(const YCHAR *path)
572 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
573 if (yaffsfs_IsPathDivider(*path)) {
581 return (*path) ? -1 : 0;
584 /* FindMountPoint only returns a dev entry if the path is a mount point */
585 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
587 struct yaffs_dev *dev;
588 YCHAR *restOfPath = NULL;
590 dev = yaffsfs_FindDevice(path, &restOfPath);
591 if (dev && restOfPath && *restOfPath)
596 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
599 struct yaffs_dev *dev;
601 dev = yaffsfs_FindDevice(path, restOfPath);
602 if (dev && dev->is_mounted)
603 return dev->root_dir;
608 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
609 int symDepth, int *loop)
613 obj = yaffs_get_equivalent_obj(obj);
615 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
616 YCHAR *alias = obj->variant.symlink_variant.alias;
618 if (yaffsfs_IsPathDivider(*alias))
620 * Starts with a /, need to scan from root up */
621 /* NB Might not work if this is called with root != NULL
623 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
624 1, NULL, NULL, loop);
627 * Relative to here so use the parent of the
630 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
631 1, NULL, NULL, loop);
637 * yaffsfs_FindDirectory
638 * Parse a path to determine the directory and the name within the directory.
640 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
642 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
644 YCHAR **name, int symDepth,
645 int *notDir, int *loop)
647 struct yaffs_obj *dir;
649 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
652 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
660 restOfPath = (YCHAR *) path;
662 dir = yaffsfs_FindRoot(path, &restOfPath);
667 * curve ball: also throw away surplus '/'
668 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
670 while (yaffsfs_IsPathDivider(*restOfPath))
671 restOfPath++; /* get rid of '/' */
676 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
677 if (i < YAFFS_MAX_NAME_LENGTH) {
678 str[i] = *restOfPath;
686 /* got to the end of the string */
689 if (yaffs_strcmp(str, _Y(".")) == 0) {
691 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
694 dir = yaffs_find_by_name(dir, str);
696 dir = yaffsfs_FollowLink(dir, symDepth, loop);
698 if (dir && dir->variant_type !=
699 YAFFS_OBJECT_TYPE_DIRECTORY) {
708 /* directory did not exist. */
712 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
716 int *notDir, int *loop)
718 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
723 * yaffsfs_FindObject turns a path for an existing object into the object
725 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
726 const YCHAR *path, int symDepth,
728 struct yaffs_obj **dirOut,
729 int *notDir, int *loop)
731 struct yaffs_obj *dir;
732 struct yaffs_obj *obj;
736 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
742 obj = yaffs_find_by_name(dir, name);
747 obj = yaffs_get_equivalent_obj(obj);
752 /*************************************************************************
753 * Start of yaffsfs visible functions.
754 *************************************************************************/
756 int yaffs_dup(int handle)
758 int newHandleNumber = -1;
759 struct yaffsfs_FileDes *existingFD = NULL;
760 struct yaffsfs_Handle *existingHandle = NULL;
761 struct yaffsfs_Handle *newHandle = NULL;
764 existingHandle = yaffsfs_HandleToPointer(handle);
765 existingFD = yaffsfs_HandleToFileDes(handle);
767 newHandleNumber = yaffsfs_NewHandle(&newHandle);
769 newHandle->fdId = existingHandle->fdId;
770 existingFD->handleCount++;
776 yaffsfs_SetError(-EBADF);
778 yaffsfs_SetError(-ENOMEM);
780 return newHandleNumber;
784 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
786 int current_objects = dev->n_obj - dev->n_deleted_files;
788 if (dev->param.max_objects && current_objects > dev->param.max_objects)
794 int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
795 int oflag, int mode, int sharing)
797 struct yaffs_obj *obj = NULL;
798 struct yaffs_obj *dir = NULL;
801 struct yaffsfs_FileDes *fd = NULL;
804 int errorReported = 0;
805 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
806 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
807 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
808 u8 sharedReadAllowed;
809 u8 sharedWriteAllowed;
819 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
820 yaffsfs_SetError(-EFAULT);
824 if (yaffsfs_CheckPath(path) < 0) {
825 yaffsfs_SetError(-ENAMETOOLONG);
829 /* O_EXCL only has meaning if O_CREAT is specified */
830 if (!(oflag & O_CREAT))
833 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
834 if ((oflag & O_CREAT) & (oflag & O_EXCL))
837 /* Todo: Are there any more flag combos to sanitise ? */
839 /* Figure out if reading or writing is requested */
841 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
842 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
846 handle = yaffsfs_NewHandleAndFileDes();
849 yaffsfs_SetError(-ENFILE);
853 fd = yaffsfs_HandleToFileDes(handle);
855 /* try to find the exisiting object */
856 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
858 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
861 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
862 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
867 /* The file already exists or it might be a directory */
868 is_dir = (obj->variant_type ==
869 YAFFS_OBJECT_TYPE_DIRECTORY);
871 /* A directory can't be opened except for read */
873 (writeRequested || !readRequested ||
874 (oflag & ~O_RDONLY))) {
876 yaffsfs_SetError(-EISDIR);
881 dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
884 yaffsfs_SetError(-ENFILE);
889 /* Open should fail if O_CREAT and O_EXCL are specified
890 * for a file that exists.
892 if (!errorReported &&
893 (oflag & O_EXCL) && (oflag & O_CREAT)) {
895 yaffsfs_SetError(-EEXIST);
899 /* Check file permissions */
900 if (readRequested && !(obj->yst_mode & S_IREAD))
903 if (writeRequested && !(obj->yst_mode & S_IWRITE))
906 if (!errorReported && writeRequested &&
907 obj->my_dev->read_only) {
909 yaffsfs_SetError(-EROFS);
913 if (openDenied && !errorReported) {
914 yaffsfs_SetError(-EACCES);
918 /* Check sharing of an existing object. */
920 struct yaffsfs_FileDes *fdx;
923 sharedReadAllowed = 1;
924 sharedWriteAllowed = 1;
927 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
928 fdx = &yaffsfs_fd[i];
929 if (fdx->handleCount > 0 &&
931 yaffsfs_inode[fdx->inodeId].iObj
934 sharedReadAllowed = 0;
935 if (!fdx->shareWrite)
936 sharedWriteAllowed = 0;
944 if ((!sharedReadAllowed && readRequested) ||
945 (!shareRead && alreadyReading) ||
946 (!sharedWriteAllowed && writeRequested) ||
947 (!shareWrite && alreadyWriting)) {
949 yaffsfs_SetError(-EBUSY);
956 /* If we could not open an existing object, then let's see if
957 * the directory exists. If not, error.
959 if (!obj && !errorReported) {
960 dir = yaffsfs_FindDirectory(reldir, path, &name, 0,
962 if (!dir && notDir) {
963 yaffsfs_SetError(-ENOTDIR);
966 yaffsfs_SetError(-ELOOP);
969 yaffsfs_SetError(-ENOENT);
974 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
975 /* Let's see if we can create this file */
976 if (dir->my_dev->read_only) {
977 yaffsfs_SetError(-EROFS);
979 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
980 yaffsfs_SetError(-ENFILE);
983 obj = yaffs_create_file(dir, name, mode, 0, 0);
985 if (!obj && !errorReported) {
986 yaffsfs_SetError(-ENOSPC);
991 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
992 yaffsfs_SetError(-ENOENT);
996 if (obj && !openDenied) {
997 int inodeId = yaffsfs_GetInodeIdForObject(obj);
1001 * Todo: Fix any problem if inodes run out,
1002 * That can't happen if the number of inode
1003 * items >= number of handles.
1007 fd->inodeId = inodeId;
1008 fd->reading = readRequested;
1009 fd->writing = writeRequested;
1010 fd->append = (oflag & O_APPEND) ? 1 : 0;
1011 fd->shareRead = shareRead;
1012 fd->shareWrite = shareWrite;
1020 /* Hook inode to object */
1021 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1023 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1024 yaffs_resize_file(obj, 0);
1026 yaffsfs_PutHandle(handle);
1028 yaffsfs_SetError(0); /* Problem */
1038 int yaffs_open_sharing_reldev(struct yaffs_dev *dev, const YCHAR *path, int oflag,
1039 int mode, int sharing)
1041 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path,
1042 oflag, mode, sharing);
1045 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
1047 return yaffs_open_sharing_reldir(NULL, path, oflag, mode, sharing);
1050 int yaffs_open_reldir(struct yaffs_obj *reldir,const YCHAR *path, int oflag, int mode)
1052 return yaffs_open_sharing_reldir(reldir, path, oflag, mode,
1053 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1056 int yaffs_open_reldev(struct yaffs_dev *dev,const YCHAR *path, int oflag, int mode)
1058 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path, oflag, mode,
1059 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1062 int yaffs_open(const YCHAR *path, int oflag, int mode)
1064 return yaffs_open_reldir(NULL, path, oflag, mode);
1067 static int yaffs_Dofsync(int handle, int datasync)
1070 struct yaffs_obj *obj;
1074 obj = yaffsfs_HandleToObject(handle);
1077 yaffsfs_SetError(-EBADF);
1078 else if (obj->my_dev->read_only)
1079 yaffsfs_SetError(-EROFS);
1081 yaffs_flush_file(obj, 1, datasync, 0);
1090 int yaffs_fsync(int handle)
1092 return yaffs_Dofsync(handle, 0);
1095 int yaffs_flush(int handle)
1097 return yaffs_fsync(handle);
1100 int yaffs_fdatasync(int handle)
1102 return yaffs_Dofsync(handle, 1);
1105 int yaffs_close(int handle)
1107 struct yaffsfs_Handle *h = NULL;
1108 struct yaffsfs_FileDes *f;
1109 struct yaffs_obj *obj = NULL;
1114 h = yaffsfs_HandleToPointer(handle);
1115 f = yaffsfs_HandleToFileDes(handle);
1116 obj = yaffsfs_HandleToObject(handle);
1118 if (!h || !obj || !f)
1119 yaffsfs_SetError(-EBADF);
1123 yaffs_flush_file(obj, 1, 0, 1);
1124 yaffsfs_PutHandle(handle);
1133 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1134 int isPread, Y_LOFF_T offset)
1136 struct yaffsfs_FileDes *fd = NULL;
1137 struct yaffs_obj *obj = NULL;
1139 Y_LOFF_T startPos = 0;
1140 Y_LOFF_T endPos = 0;
1145 u8 *buf = (u8 *) vbuf;
1147 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1148 yaffsfs_SetError(-EFAULT);
1153 fd = yaffsfs_HandleToFileDes(handle);
1154 obj = yaffsfs_HandleToObject(handle);
1158 yaffsfs_SetError(-EBADF);
1160 } else if (!fd->reading) {
1161 /* Not a reading handle */
1162 yaffsfs_SetError(-EINVAL);
1164 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1165 yaffsfs_SetError(-EINVAL);
1171 startPos = fd->v.position;
1175 if (yaffs_get_obj_length(obj) > pos)
1176 maxRead = yaffs_get_obj_length(obj) - pos;
1180 if (nbyte > maxRead)
1183 yaffsfs_GetHandle(handle);
1185 endPos = pos + nbyte;
1187 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1188 nbyte > YAFFS_MAX_FILE_SIZE ||
1189 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1195 nToRead = YAFFSFS_RW_SIZE -
1196 (pos & (YAFFSFS_RW_SIZE - 1));
1197 if (nToRead > nbyte)
1201 * Need to reverify object in case the device was
1202 * unmounted in another thread.
1204 obj = yaffsfs_HandleToObject(handle);
1208 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1216 if (nRead == nToRead)
1219 nbyte = 0; /* no more to read */
1228 yaffsfs_PutHandle(handle);
1232 fd->v.position = startPos + totalRead;
1234 yaffsfs_SetError(-EINVAL);
1241 return (totalRead >= 0) ? totalRead : -1;
1245 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1247 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1250 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1252 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1255 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1256 int isPwrite, Y_LOFF_T offset)
1258 struct yaffsfs_FileDes *fd = NULL;
1259 struct yaffs_obj *obj = NULL;
1261 Y_LOFF_T startPos = 0;
1264 int totalWritten = 0;
1265 int write_trhrough = 0;
1267 const u8 *buf = (const u8 *)vbuf;
1269 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1270 yaffsfs_SetError(-EFAULT);
1275 fd = yaffsfs_HandleToFileDes(handle);
1276 obj = yaffsfs_HandleToObject(handle);
1280 yaffsfs_SetError(-EBADF);
1282 } else if (!fd->writing) {
1283 yaffsfs_SetError(-EINVAL);
1285 } else if (obj->my_dev->read_only) {
1286 yaffsfs_SetError(-EROFS);
1290 startPos = yaffs_get_obj_length(obj);
1294 startPos = fd->v.position;
1296 yaffsfs_GetHandle(handle);
1298 endPos = pos + nbyte;
1300 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1301 nbyte > YAFFS_MAX_FILE_SIZE ||
1302 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1309 nToWrite = YAFFSFS_RW_SIZE -
1310 (pos & (YAFFSFS_RW_SIZE - 1));
1311 if (nToWrite > nbyte)
1315 * Need to reverify object in case the device was
1316 * remounted or unmounted in another thread.
1318 obj = yaffsfs_HandleToObject(handle);
1319 if (!obj || obj->my_dev->read_only)
1323 yaffs_wr_file(obj, buf, pos, nToWrite,
1326 totalWritten += nWritten;
1331 if (nWritten == nToWrite)
1336 if (nWritten < 1 && totalWritten < 1) {
1337 yaffsfs_SetError(-ENOSPC);
1347 yaffsfs_PutHandle(handle);
1350 if (totalWritten > 0)
1351 fd->v.position = startPos + totalWritten;
1353 yaffsfs_SetError(-EINVAL);
1359 return (totalWritten >= 0) ? totalWritten : -1;
1362 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1364 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1367 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1369 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1372 int yaffs_truncate_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1375 struct yaffs_obj *obj = NULL;
1376 struct yaffs_obj *dir = NULL;
1377 int result = YAFFS_FAIL;
1381 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1382 yaffsfs_SetError(-EFAULT);
1386 if (yaffsfs_CheckPath(path) < 0) {
1387 yaffsfs_SetError(-ENAMETOOLONG);
1393 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1394 obj = yaffsfs_FollowLink(obj, 0, &loop);
1397 yaffsfs_SetError(-ENOTDIR);
1399 yaffsfs_SetError(-ELOOP);
1400 else if (!dir || !obj)
1401 yaffsfs_SetError(-ENOENT);
1402 else if (obj->my_dev->read_only)
1403 yaffsfs_SetError(-EROFS);
1404 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1405 yaffsfs_SetError(-EISDIR);
1406 else if (obj->my_dev->read_only)
1407 yaffsfs_SetError(-EROFS);
1408 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1409 yaffsfs_SetError(-EINVAL);
1411 result = yaffs_resize_file(obj, new_size);
1415 return (result) ? 0 : -1;
1418 int yaffs_truncate_reldev(struct yaffs_dev *dev, const YCHAR *path,
1421 return yaffs_truncate_reldir(ROOT_DIR(dev), path, new_size);
1424 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1426 return yaffs_truncate_reldir(NULL, path, new_size);
1429 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1431 struct yaffsfs_FileDes *fd = NULL;
1432 struct yaffs_obj *obj = NULL;
1436 fd = yaffsfs_HandleToFileDes(handle);
1437 obj = yaffsfs_HandleToObject(handle);
1441 yaffsfs_SetError(-EBADF);
1442 else if (!fd->writing)
1443 yaffsfs_SetError(-EINVAL);
1444 else if (obj->my_dev->read_only)
1445 yaffsfs_SetError(-EROFS);
1446 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1447 yaffsfs_SetError(-EINVAL);
1449 /* resize the file */
1450 result = yaffs_resize_file(obj, new_size);
1453 return (result) ? 0 : -1;
1457 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1459 struct yaffsfs_FileDes *fd = NULL;
1460 struct yaffs_obj *obj = NULL;
1462 Y_LOFF_T fSize = -1;
1465 fd = yaffsfs_HandleToFileDes(handle);
1466 obj = yaffsfs_HandleToObject(handle);
1469 yaffsfs_SetError(-EBADF);
1470 else if (offset > YAFFS_MAX_FILE_SIZE)
1471 yaffsfs_SetError(-EINVAL);
1473 if (whence == SEEK_SET) {
1476 } else if (whence == SEEK_CUR) {
1477 if ((fd->v.position + offset) >= 0)
1478 pos = (fd->v.position + offset);
1479 } else if (whence == SEEK_END) {
1480 fSize = yaffs_get_obj_length(obj);
1481 if (fSize >= 0 && (fSize + offset) >= 0)
1482 pos = fSize + offset;
1485 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1486 fd->v.position = pos;
1488 yaffsfs_SetError(-EINVAL);
1498 static int yaffsfs_DoUnlink_reldir(struct yaffs_obj *reldir,
1499 const YCHAR *path, int isDirectory)
1501 struct yaffs_obj *dir = NULL;
1502 struct yaffs_obj *obj = NULL;
1504 int result = YAFFS_FAIL;
1508 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1509 yaffsfs_SetError(-EFAULT);
1513 if (yaffsfs_CheckPath(path) < 0) {
1514 yaffsfs_SetError(-ENAMETOOLONG);
1520 obj = yaffsfs_FindObject(reldir, path, 0, 0, NULL, NULL, NULL);
1521 dir = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
1524 yaffsfs_SetError(-ENOTDIR);
1526 yaffsfs_SetError(-ELOOP);
1528 yaffsfs_SetError(-ENOENT);
1529 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1530 yaffsfs_SetError(-EINVAL);
1532 yaffsfs_SetError(-ENOENT);
1533 else if (obj->my_dev->read_only)
1534 yaffsfs_SetError(-EROFS);
1535 else if (!isDirectory &&
1536 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1537 yaffsfs_SetError(-EISDIR);
1538 else if (isDirectory &&
1539 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1540 yaffsfs_SetError(-ENOTDIR);
1541 else if (isDirectory && obj == obj->my_dev->root_dir)
1542 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1544 result = yaffs_unlinker(dir, name);
1546 if (result == YAFFS_FAIL && isDirectory)
1547 yaffsfs_SetError(-ENOTEMPTY);
1552 return (result == YAFFS_FAIL) ? -1 : 0;
1555 int yaffs_unlink_reldir(struct yaffs_obj *reldir, const YCHAR *path)
1557 return yaffsfs_DoUnlink_reldir(reldir, path, 0);
1560 int yaffs_unlink_reldev(struct yaffs_dev *dev, const YCHAR *path)
1562 return yaffsfs_DoUnlink_reldir(ROOT_DIR(dev), path, 0);
1565 int yaffs_unlink(const YCHAR *path)
1567 return yaffs_unlink_reldir(NULL, path);
1570 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1572 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1573 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1579 static int rename_dir_over_file(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 int yaffs_rename_reldir(struct yaffs_obj *reldir,
1589 const YCHAR *oldPath, const YCHAR *newPath)
1591 struct yaffs_obj *olddir = NULL;
1592 struct yaffs_obj *newdir = NULL;
1593 struct yaffs_obj *obj = NULL;
1594 struct yaffs_obj *newobj = NULL;
1597 int result = YAFFS_FAIL;
1598 int rename_allowed = 1;
1604 YCHAR *alt_newpath = NULL;
1606 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1607 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1608 yaffsfs_SetError(-EFAULT);
1612 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1613 yaffsfs_SetError(-ENAMETOOLONG);
1617 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1618 yaffsfs_SetError(-ENOMEM);
1622 newPath = alt_newpath;
1626 olddir = yaffsfs_FindDirectory(reldir, oldPath, &oldname, 0,
1627 ¬OldDir, &oldLoop);
1628 newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
1629 ¬NewDir, &newLoop);
1630 obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
1631 newobj = yaffsfs_FindObject(reldir, newPath, 0, 0, NULL, NULL, NULL);
1633 /* If the object being renamed is a directory and the
1634 * path ended with a "/" then the olddir == obj.
1635 * We pass through NULL for the old name to tell the lower layers
1636 * to use olddir as the object.
1642 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1643 yaffsfs_SetError(-ENOTDIR);
1645 } else if (oldLoop || newLoop) {
1646 yaffsfs_SetError(-ELOOP);
1648 } else if (olddir && oldname &&
1649 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1650 yaffsfs_SetError(-EINVAL);
1652 } else if (!olddir || !newdir || !obj) {
1653 yaffsfs_SetError(-ENOENT);
1655 } else if (obj->my_dev->read_only) {
1656 yaffsfs_SetError(-EROFS);
1658 } else if (rename_file_over_dir(obj, newobj)) {
1659 yaffsfs_SetError(-EISDIR);
1661 } else if (rename_dir_over_file(obj, newobj)) {
1662 yaffsfs_SetError(-ENOTDIR);
1664 } else if (yaffs_is_non_empty_dir(newobj)) {
1665 yaffsfs_SetError(-ENOTEMPTY);
1667 } else if (olddir->my_dev != newdir->my_dev) {
1668 /* Rename must be on same device */
1669 yaffsfs_SetError(-EXDEV);
1671 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1673 * It is a directory, check that it is not being renamed to
1674 * being its own decendent.
1675 * Do this by tracing from the new directory back to the root,
1679 struct yaffs_obj *xx = newdir;
1681 while (rename_allowed && xx) {
1686 if (!rename_allowed)
1687 yaffsfs_SetError(-EINVAL);
1691 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1697 return (result == YAFFS_FAIL) ? -1 : 0;
1700 int yaffs_rename_reldev(struct yaffs_dev *dev, const YCHAR *oldPath,
1701 const YCHAR *newPath)
1703 return yaffs_rename_reldir(ROOT_DIR(dev), oldPath, newPath);
1705 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1707 return yaffs_rename_reldir(NULL, oldPath, newPath);
1710 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1714 obj = yaffs_get_equivalent_obj(obj);
1717 buf->st_dev = (int)obj->my_dev->os_context;
1718 buf->st_ino = obj->obj_id;
1719 buf->st_mode = obj->yst_mode & ~S_IFMT;
1721 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1722 buf->st_mode |= S_IFDIR;
1723 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1724 buf->st_mode |= S_IFLNK;
1725 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1726 buf->st_mode |= S_IFREG;
1728 buf->st_nlink = yaffs_get_obj_link_count(obj);
1731 buf->st_rdev = obj->yst_rdev;
1732 buf->st_size = yaffs_get_obj_length(obj);
1733 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1734 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1736 #if CONFIG_YAFFS_WINCE
1737 buf->yst_wince_atime[0] = obj->win_atime[0];
1738 buf->yst_wince_atime[1] = obj->win_atime[1];
1739 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1740 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1741 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1742 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1744 buf->yst_atime = obj->yst_atime;
1745 buf->yst_ctime = obj->yst_ctime;
1746 buf->yst_mtime = obj->yst_mtime;
1753 static int yaffsfs_DoStatOrLStat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1754 struct yaffs_stat *buf, int doLStat)
1756 struct yaffs_obj *obj = NULL;
1757 struct yaffs_obj *dir = NULL;
1762 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1763 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1764 yaffsfs_SetError(-EFAULT);
1768 if (yaffsfs_CheckPath(path) < 0) {
1769 yaffsfs_SetError(-ENAMETOOLONG);
1775 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1777 if (!doLStat && obj)
1778 obj = yaffsfs_FollowLink(obj, 0, &loop);
1781 yaffsfs_SetError(-ENOTDIR);
1783 yaffsfs_SetError(-ELOOP);
1784 else if (!dir || !obj)
1785 yaffsfs_SetError(-ENOENT);
1787 retVal = yaffsfs_DoStat(obj, buf);
1795 int yaffs_stat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1796 struct yaffs_stat *buf)
1798 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 0);
1801 int yaffs_lstat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1802 struct yaffs_stat *buf)
1804 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 1);
1807 int yaffs_stat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1808 struct yaffs_stat *buf)
1810 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 0);
1813 int yaffs_lstat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1814 struct yaffs_stat *buf)
1816 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 1);
1819 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1821 return yaffs_stat_reldir(NULL, path, buf);
1824 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1826 return yaffs_lstat_reldir(NULL, path, buf);
1829 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1831 struct yaffs_obj *obj;
1835 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1836 yaffsfs_SetError(-EFAULT);
1841 obj = yaffsfs_HandleToObject(fd);
1844 retVal = yaffsfs_DoStat(obj, buf);
1847 yaffsfs_SetError(-EBADF);
1854 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1855 const struct yaffs_utimbuf *buf)
1858 struct yaffs_utimbuf local;
1860 obj = yaffs_get_equivalent_obj(obj);
1862 if (obj && obj->my_dev->read_only) {
1863 yaffsfs_SetError(-EROFS);
1867 #if !CONFIG_YAFFS_WINCE
1869 local.actime = Y_CURRENT_TIME;
1870 local.modtime = local.actime;
1877 obj->yst_atime = buf->actime;
1878 obj->yst_mtime = buf->modtime;
1880 result = yaffs_flush_file(obj, 0, 0, 0);
1881 retVal = result == YAFFS_OK ? 0 : -1;
1888 int yaffs_utime_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1889 const struct yaffs_utimbuf *buf)
1891 struct yaffs_obj *obj = NULL;
1892 struct yaffs_obj *dir = NULL;
1898 yaffsfs_SetError(-EFAULT);
1902 if (yaffsfs_CheckPath(path) < 0) {
1903 yaffsfs_SetError(-ENAMETOOLONG);
1909 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1912 yaffsfs_SetError(-ENOTDIR);
1914 yaffsfs_SetError(-ELOOP);
1915 else if (!dir || !obj)
1916 yaffsfs_SetError(-ENOENT);
1918 retVal = yaffsfs_DoUtime(obj, buf);
1926 int yaffs_utime_reldev(struct yaffs_dev *dev, const YCHAR *path,
1927 const struct yaffs_utimbuf *buf)
1929 return yaffs_utime_reldir(ROOT_DIR(dev), path, buf);
1932 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1934 return yaffs_utime_reldir(NULL, path, buf);
1937 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1939 struct yaffs_obj *obj;
1944 obj = yaffsfs_HandleToObject(fd);
1947 retVal = yaffsfs_DoUtime(obj, buf);
1950 yaffsfs_SetError(-EBADF);
1957 #ifndef CONFIG_YAFFS_WINCE
1958 /* xattrib functions */
1960 static int yaffs_do_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1961 const char *name, const void *data, int size,
1962 int flags, int follow)
1964 struct yaffs_obj *obj;
1965 struct yaffs_obj *dir;
1971 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1972 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1973 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1974 yaffsfs_SetError(-EFAULT);
1978 if (yaffsfs_CheckPath(path) < 0) {
1979 yaffsfs_SetError(-ENAMETOOLONG);
1985 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1988 obj = yaffsfs_FollowLink(obj, 0, &loop);
1991 yaffsfs_SetError(-ENOTDIR);
1993 yaffsfs_SetError(-ELOOP);
1994 else if (!dir || !obj)
1995 yaffsfs_SetError(-ENOENT);
1997 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1999 yaffsfs_SetError(retVal);
2010 int yaffs_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2011 const char *name, const void *data, int size, int flags)
2013 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 1);
2016 int yaffs_setxattr_reldev(struct yaffs_dev *dev, const YCHAR *path,
2017 const char *name, const void *data, int size, int flags)
2019 return yaffs_setxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2022 int yaffs_setxattr(const YCHAR *path, const char *name,
2023 const void *data, int size, int flags)
2025 return yaffs_setxattr_reldir(NULL, path, name, data, size, flags);
2028 int yaffs_lsetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path, const char *name,
2029 const void *data, int size, int flags)
2031 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 0);
2034 int yaffs_lsetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name,
2035 const void *data, int size, int flags)
2037 return yaffs_lsetxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2040 int yaffs_lsetxattr(const YCHAR *path, const char *name,
2041 const void *data, int size, int flags)
2043 return yaffs_lsetxattr_reldir(NULL, path, name, data, size, flags);
2046 int yaffs_fsetxattr(int fd, const char *name,
2047 const void *data, int size, int flags)
2049 struct yaffs_obj *obj;
2053 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2054 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2055 yaffsfs_SetError(-EFAULT);
2060 obj = yaffsfs_HandleToObject(fd);
2063 yaffsfs_SetError(-EBADF);
2065 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2067 yaffsfs_SetError(retVal);
2077 static int yaffs_do_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2078 const char *name, void *data, int size, int follow)
2080 struct yaffs_obj *obj;
2081 struct yaffs_obj *dir;
2086 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2087 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2088 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2089 yaffsfs_SetError(-EFAULT);
2093 if (yaffsfs_CheckPath(path) < 0) {
2094 yaffsfs_SetError(-ENAMETOOLONG);
2100 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2103 obj = yaffsfs_FollowLink(obj, 0, &loop);
2106 yaffsfs_SetError(-ENOTDIR);
2108 yaffsfs_SetError(-ELOOP);
2109 else if (!dir || !obj)
2110 yaffsfs_SetError(-ENOENT);
2112 retVal = yaffs_get_xattrib(obj, name, data, size);
2114 yaffsfs_SetError(retVal);
2124 int yaffs_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2125 const char *name, void *data, int size)
2127 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 1);
2130 int yaffs_getxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2132 return yaffs_getxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2135 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
2137 return yaffs_getxattr_reldir(NULL, path, name, data, size);
2140 int yaffs_lgetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2141 const char *name, void *data, int size)
2143 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 0);
2146 int yaffs_lgetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2148 return yaffs_lgetxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2151 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2153 return yaffs_lgetxattr_reldir(NULL, path, name, data, size);
2156 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2158 struct yaffs_obj *obj;
2162 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2163 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2164 yaffsfs_SetError(-EFAULT);
2169 obj = yaffsfs_HandleToObject(fd);
2172 retVal = yaffs_get_xattrib(obj, name, data, size);
2174 yaffsfs_SetError(retVal);
2179 yaffsfs_SetError(-EBADF);
2186 static int yaffs_do_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2187 char *data, int size, int follow)
2189 struct yaffs_obj *obj = NULL;
2190 struct yaffs_obj *dir = NULL;
2195 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2196 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2197 yaffsfs_SetError(-EFAULT);
2201 if (yaffsfs_CheckPath(path) < 0) {
2202 yaffsfs_SetError(-ENAMETOOLONG);
2208 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2211 obj = yaffsfs_FollowLink(obj, 0, &loop);
2214 yaffsfs_SetError(-ENOTDIR);
2216 yaffsfs_SetError(-ELOOP);
2217 else if (!dir || !obj)
2218 yaffsfs_SetError(-ENOENT);
2220 retVal = yaffs_list_xattrib(obj, data, size);
2222 yaffsfs_SetError(retVal);
2233 int yaffs_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2234 char *data, int size)
2236 return yaffs_do_listxattr_reldir(reldir, path, data, size, 1);
2239 int yaffs_listxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2241 return yaffs_listxattr_reldir(ROOT_DIR(dev), path, data, size);
2244 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2246 return yaffs_listxattr_reldir(NULL, path, data, size);
2249 int yaffs_llistxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2250 char *data, int size)
2252 return yaffs_do_listxattr_reldir(reldir, path, data, size, 0);
2255 int yaffs_llistxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2257 return yaffs_llistxattr_reldir(ROOT_DIR(dev), path, data, size);
2260 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2262 return yaffs_llistxattr_reldir(NULL, path, data, size);
2265 int yaffs_flistxattr(int fd, char *data, int size)
2267 struct yaffs_obj *obj;
2271 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2272 yaffsfs_SetError(-EFAULT);
2277 obj = yaffsfs_HandleToObject(fd);
2280 retVal = yaffs_list_xattrib(obj, data, size);
2282 yaffsfs_SetError(retVal);
2287 yaffsfs_SetError(-EBADF);
2294 static int yaffs_do_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2295 const char *name, int follow)
2297 struct yaffs_obj *obj = NULL;
2298 struct yaffs_obj *dir = NULL;
2303 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2304 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2305 yaffsfs_SetError(-EFAULT);
2309 if (yaffsfs_CheckPath(path) < 0) {
2310 yaffsfs_SetError(-ENAMETOOLONG);
2316 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2319 obj = yaffsfs_FollowLink(obj, 0, &loop);
2322 yaffsfs_SetError(-ENOTDIR);
2324 yaffsfs_SetError(-ELOOP);
2325 else if (!dir || !obj)
2326 yaffsfs_SetError(-ENOENT);
2328 retVal = yaffs_remove_xattrib(obj, name);
2330 yaffsfs_SetError(retVal);
2341 int yaffs_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2344 return yaffs_do_removexattr_reldir(reldir, path, name, 1);
2347 int yaffs_removexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2349 return yaffs_removexattr_reldir(ROOT_DIR(dev), path, name);
2352 int yaffs_removexattr(const YCHAR *path, const char *name)
2354 return yaffs_removexattr_reldir(NULL, path, name);
2357 int yaffs_lremovexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2360 return yaffs_do_removexattr_reldir(reldir, path, name, 0);
2363 int yaffs_lremovexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2365 return yaffs_lremovexattr_reldir(ROOT_DIR(dev), path, name);
2368 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2370 return yaffs_lremovexattr_reldir(NULL, path, name);
2373 int yaffs_fremovexattr(int fd, const char *name)
2375 struct yaffs_obj *obj;
2379 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2380 yaffsfs_SetError(-EFAULT);
2385 obj = yaffsfs_HandleToObject(fd);
2388 retVal = yaffs_remove_xattrib(obj, name);
2390 yaffsfs_SetError(retVal);
2395 yaffsfs_SetError(-EBADF);
2403 #ifdef CONFIG_YAFFS_WINCE
2404 int yaffs_get_wince_times(int fd, unsigned *wctime,
2405 unsigned *watime, unsigned *wmtime)
2407 struct yaffs_obj *obj;
2412 obj = yaffsfs_HandleToObject(fd);
2417 wctime[0] = obj->win_ctime[0];
2418 wctime[1] = obj->win_ctime[1];
2421 watime[0] = obj->win_atime[0];
2422 watime[1] = obj->win_atime[1];
2425 wmtime[0] = obj->win_mtime[0];
2426 wmtime[1] = obj->win_mtime[1];
2432 yaffsfs_SetError(-EBADF);
2439 int yaffs_set_wince_times(int fd,
2440 const unsigned *wctime,
2441 const unsigned *watime, const unsigned *wmtime)
2443 struct yaffs_obj *obj;
2448 obj = yaffsfs_HandleToObject(fd);
2453 obj->win_ctime[0] = wctime[0];
2454 obj->win_ctime[1] = wctime[1];
2457 obj->win_atime[0] = watime[0];
2458 obj->win_atime[1] = watime[1];
2461 obj->win_mtime[0] = wmtime[0];
2462 obj->win_mtime[1] = wmtime[1];
2466 result = yaffs_flush_file(obj, 0, 0, 0);
2470 yaffsfs_SetError(-EBADF);
2479 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2484 obj = yaffs_get_equivalent_obj(obj);
2487 obj->yst_mode = mode;
2489 result = yaffs_flush_file(obj, 0, 0, 0);
2492 return result == YAFFS_OK ? 0 : -1;
2495 int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
2497 struct yaffs_obj *obj = NULL;
2498 struct yaffs_obj *dir = NULL;
2503 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2504 yaffsfs_SetError(-EFAULT);
2508 if (yaffsfs_CheckPath(path) < 0) {
2509 yaffsfs_SetError(-ENAMETOOLONG);
2513 if (amode & ~(R_OK | W_OK | X_OK)) {
2514 yaffsfs_SetError(-EINVAL);
2520 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2521 obj = yaffsfs_FollowLink(obj, 0, &loop);
2524 yaffsfs_SetError(-ENOTDIR);
2526 yaffsfs_SetError(-ELOOP);
2527 else if (!dir || !obj)
2528 yaffsfs_SetError(-ENOENT);
2529 else if ((amode & W_OK) && obj->my_dev->read_only)
2530 yaffsfs_SetError(-EROFS);
2534 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2536 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2538 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2542 yaffsfs_SetError(-EACCES);
2553 int yaffs_access_reldev(struct yaffs_dev *dev, const YCHAR *path, int amode)
2555 return yaffs_access_reldir(ROOT_DIR(dev), path, amode);
2558 int yaffs_access(const YCHAR *path, int amode)
2560 return yaffs_access_reldir(NULL, path, amode);
2563 int yaffs_chmod_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2565 struct yaffs_obj *obj = NULL;
2566 struct yaffs_obj *dir = NULL;
2571 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2572 yaffsfs_SetError(-EFAULT);
2576 if (yaffsfs_CheckPath(path) < 0) {
2577 yaffsfs_SetError(-ENAMETOOLONG);
2581 if (mode & ~(0777)) {
2582 yaffsfs_SetError(-EINVAL);
2588 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2589 obj = yaffsfs_FollowLink(obj, 0, &loop);
2592 yaffsfs_SetError(-ENOTDIR);
2594 yaffsfs_SetError(-ELOOP);
2595 else if (!dir || !obj)
2596 yaffsfs_SetError(-ENOENT);
2597 else if (obj->my_dev->read_only)
2598 yaffsfs_SetError(-EROFS);
2600 retVal = yaffsfs_DoChMod(obj, mode);
2608 int yaffs_chmod_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2610 return yaffs_chmod_reldir(ROOT_DIR(dev), path, mode);
2613 int yaffs_chmod(const YCHAR *path, mode_t mode)
2615 return yaffs_chmod_reldir(NULL, path, mode);
2618 int yaffs_fchmod(int fd, mode_t mode)
2620 struct yaffs_obj *obj;
2623 if (mode & ~(0777)) {
2624 yaffsfs_SetError(-EINVAL);
2629 obj = yaffsfs_HandleToObject(fd);
2632 yaffsfs_SetError(-EBADF);
2633 else if (obj->my_dev->read_only)
2634 yaffsfs_SetError(-EROFS);
2636 retVal = yaffsfs_DoChMod(obj, mode);
2643 int yaffs_mkdir_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2645 struct yaffs_obj *parent = NULL;
2646 struct yaffs_obj *dir = NULL;
2648 YCHAR *alt_path = NULL;
2653 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2654 yaffsfs_SetError(-EFAULT);
2658 if (yaffsfs_CheckPath(path) < 0) {
2659 yaffsfs_SetError(-ENAMETOOLONG);
2663 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2664 yaffsfs_SetError(-ENOMEM);
2671 parent = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
2672 if (!parent && notDir)
2673 yaffsfs_SetError(-ENOTDIR);
2675 yaffsfs_SetError(-ELOOP);
2677 yaffsfs_SetError(-ENOENT);
2678 else if (yaffsfs_TooManyObjects(parent->my_dev))
2679 yaffsfs_SetError(-ENFILE);
2680 else if (yaffs_strnlen(name, 5) == 0) {
2681 /* Trying to make the root itself */
2682 yaffsfs_SetError(-EEXIST);
2683 } else if (parent->my_dev->read_only)
2684 yaffsfs_SetError(-EROFS);
2686 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2689 else if (yaffs_find_by_name(parent, name))
2690 yaffsfs_SetError(-EEXIST); /* name exists */
2692 yaffsfs_SetError(-ENOSPC); /* assume no space */
2702 int yaffs_mkdir_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2704 return yaffs_mkdir_reldir(ROOT_DIR(dev), path, mode);
2707 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2709 return yaffs_mkdir_reldir(NULL, path, mode);
2712 int yaffs_rmdir_reldir(struct yaffs_obj *reldir, const YCHAR *path)
2717 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2718 yaffsfs_SetError(-EFAULT);
2722 if (yaffsfs_CheckPath(path) < 0) {
2723 yaffsfs_SetError(-ENAMETOOLONG);
2727 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2728 yaffsfs_SetError(-ENOMEM);
2733 result = yaffsfs_DoUnlink_reldir(reldir, path, 1);
2740 int yaffs_rmdir_reldev(struct yaffs_dev *dev, const YCHAR *path)
2742 return yaffs_rmdir_reldir(ROOT_DIR(dev), path);
2745 int yaffs_rmdir(const YCHAR *path)
2747 return yaffs_rmdir_reldir(NULL, path);
2751 * The mount/unmount/sync functions act on devices rather than reldirs.
2753 void *yaffs_getdev(const YCHAR *path)
2755 struct yaffs_dev *dev = NULL;
2757 dev = yaffsfs_FindDevice(path, &dummy);
2761 int yaffs_mount_common(struct yaffs_dev *dev, const YCHAR *path,
2762 int read_only, int skip_checkpt)
2765 int result = YAFFS_FAIL;
2768 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2769 yaffsfs_SetError(-EFAULT);
2773 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2775 if (yaffsfs_CheckPath(path) < 0) {
2776 yaffsfs_SetError(-ENAMETOOLONG);
2783 yaffsfs_InitHandles();
2786 dev = yaffsfs_FindMountPoint(path);
2789 if (!dev->is_mounted) {
2790 dev->read_only = read_only ? 1 : 0;
2792 u8 skip = dev->param.skip_checkpt_rd;
2793 dev->param.skip_checkpt_rd = 1;
2794 result = yaffs_guts_initialise(dev);
2795 dev->param.skip_checkpt_rd = skip;
2797 result = yaffs_guts_initialise(dev);
2800 if (result == YAFFS_FAIL)
2801 yaffsfs_SetError(-ENOMEM);
2802 retVal = result ? 0 : -1;
2805 yaffsfs_SetError(-EBUSY);
2807 yaffsfs_SetError(-ENODEV);
2814 int yaffs_mount3_reldev(struct yaffs_dev *dev, int read_only, int skip_checkpt)
2816 return yaffs_mount_common(dev, NULL, read_only, skip_checkpt);
2819 int yaffs_mount3(const YCHAR *path, int read_only, int skip_checkpt)
2821 return yaffs_mount_common(NULL, path, read_only, skip_checkpt);
2824 int yaffs_mount2_reldev(struct yaffs_dev *dev, int readonly)
2826 return yaffs_mount_common(dev, NULL, readonly, 0);
2829 int yaffs_mount2(const YCHAR *path, int readonly)
2831 return yaffs_mount_common(NULL, path, readonly, 0);
2834 int yaffs_mount_reldev(struct yaffs_dev *dev)
2836 return yaffs_mount_common(dev, NULL, 0, 0);
2839 int yaffs_mount(const YCHAR *path)
2841 return yaffs_mount_common(NULL, path, 0, 0);
2844 int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
2850 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2851 yaffsfs_SetError(-EFAULT);
2855 if (yaffsfs_CheckPath(path) < 0) {
2856 yaffsfs_SetError(-ENAMETOOLONG);
2863 dev = yaffsfs_FindDevice(path, &dummy);
2866 if (!dev->is_mounted)
2867 yaffsfs_SetError(-EINVAL);
2868 else if (dev->read_only)
2869 yaffsfs_SetError(-EROFS);
2872 yaffs_flush_whole_cache(dev, 0);
2873 yaffs_checkpoint_save(dev);
2878 yaffsfs_SetError(-ENODEV);
2884 int yaffs_sync_reldev(struct yaffs_dev *dev)
2886 return yaffs_sync_common(dev, NULL);
2889 int yaffs_sync(const YCHAR *path)
2891 return yaffs_sync_common(NULL, path);
2895 static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
2903 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2904 yaffsfs_SetError(-EFAULT);
2908 if (yaffsfs_CheckPath(path) < 0) {
2909 yaffsfs_SetError(-ENAMETOOLONG);
2916 dev = yaffsfs_FindDevice(path, &dummy);
2919 if (!dev->is_mounted)
2920 yaffsfs_SetError(-EINVAL);
2922 retVal = yaffs_bg_gc(dev, urgency);
2924 yaffsfs_SetError(-ENODEV);
2930 /* Background gc functions.
2931 * These return 0 when bg done or greater than 0 when gc has been
2932 * done and there is still a lot of garbage to be cleaned up.
2935 int yaffs_do_background_gc(const YCHAR *path, int urgency)
2937 return yaffsfs_bg_gc_common(NULL, path, urgency);
2940 int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
2942 return yaffsfs_bg_gc_common(dev, NULL, urgency);
2945 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2948 struct yaffs_obj *obj;
2950 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2951 obj = yaffsfs_HandleToObject(i);
2952 if (obj && obj->my_dev == dev)
2958 int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
2959 int force, int read_only)
2963 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2964 yaffsfs_SetError(-EFAULT);
2968 if (yaffsfs_CheckPath(path) < 0) {
2969 yaffsfs_SetError(-ENAMETOOLONG);
2975 dev = yaffsfs_FindMountPoint(path);
2978 if (dev->is_mounted) {
2979 yaffs_flush_whole_cache(dev, 0);
2981 if (force || !yaffsfs_IsDevBusy(dev)) {
2983 yaffs_checkpoint_save(dev);
2984 dev->read_only = read_only ? 1 : 0;
2987 yaffsfs_SetError(-EBUSY);
2990 yaffsfs_SetError(-EINVAL);
2993 yaffsfs_SetError(-ENODEV);
3000 int yaffs_remount_reldev(struct yaffs_dev *dev, int force, int read_only)
3002 return yaffs_remount_common(dev, NULL, force, read_only);
3004 int yaffs_remount(const YCHAR *path, int force, int read_only)
3006 return yaffs_remount_common(NULL, path, force, read_only);
3009 int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
3013 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3014 yaffsfs_SetError(-EFAULT);
3018 if (yaffsfs_CheckPath(path) < 0) {
3019 yaffsfs_SetError(-ENAMETOOLONG);
3025 dev = yaffsfs_FindMountPoint(path);
3028 if (dev->is_mounted) {
3030 yaffs_flush_whole_cache(dev, 0);
3031 yaffs_checkpoint_save(dev);
3032 inUse = yaffsfs_IsDevBusy(dev);
3033 if (!inUse || force) {
3035 yaffsfs_BreakDeviceHandles(dev);
3036 yaffs_deinitialise(dev);
3040 yaffsfs_SetError(-EBUSY);
3043 yaffsfs_SetError(-EINVAL);
3046 yaffsfs_SetError(-ENODEV);
3053 int yaffs_unmount2_reldev(struct yaffs_dev *dev, int force)
3055 return yaffs_unmount2_common(dev, NULL, force);
3058 int yaffs_unmount2(const YCHAR *path, int force)
3060 return yaffs_unmount2_common(NULL, path, force);
3063 int yaffs_unmount_reldev(struct yaffs_dev *dev)
3065 return yaffs_unmount2_reldev(dev, 0);
3068 int yaffs_unmount(const YCHAR *path)
3070 return yaffs_unmount2(path, 0);
3073 int yaffs_format_common(struct yaffs_dev *dev,
3076 int force_unmount_flag,
3084 yaffsfs_SetError(-EFAULT);
3088 if (yaffsfs_CheckPath(path) < 0) {
3089 yaffsfs_SetError(-ENAMETOOLONG);
3096 dev = yaffsfs_FindMountPoint(path);
3099 int was_mounted = dev->is_mounted;
3101 if (dev->is_mounted && unmount_flag) {
3103 yaffs_flush_whole_cache(dev, 0);
3104 yaffs_checkpoint_save(dev);
3105 inUse = yaffsfs_IsDevBusy(dev);
3106 if (!inUse || force_unmount_flag) {
3108 yaffsfs_BreakDeviceHandles(dev);
3109 yaffs_deinitialise(dev);
3113 if(dev->is_mounted) {
3114 yaffsfs_SetError(-EBUSY);
3117 yaffs_guts_format_dev(dev);
3118 if(was_mounted && remount_flag) {
3119 result = yaffs_guts_initialise(dev);
3120 if (result == YAFFS_FAIL) {
3121 yaffsfs_SetError(-ENOMEM);
3127 yaffsfs_SetError(-ENODEV);
3136 int yaffs_format_reldev(struct yaffs_dev *dev,
3138 int force_unmount_flag,
3141 return yaffs_format_common(dev, NULL, unmount_flag,
3142 force_unmount_flag, remount_flag);
3145 int yaffs_format(const YCHAR *path,
3147 int force_unmount_flag,
3150 return yaffs_format_common(NULL, path, unmount_flag,
3151 force_unmount_flag, remount_flag);
3154 Y_LOFF_T yaffs_freespace_common(struct yaffs_dev *dev, const YCHAR *path)
3156 Y_LOFF_T retVal = -1;
3160 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3161 yaffsfs_SetError(-EFAULT);
3165 if (yaffsfs_CheckPath(path) < 0) {
3166 yaffsfs_SetError(-ENAMETOOLONG);
3173 dev = yaffsfs_FindDevice(path, &dummy);
3174 if (dev && dev->is_mounted) {
3175 retVal = yaffs_get_n_free_chunks(dev);
3176 retVal *= dev->data_bytes_per_chunk;
3179 yaffsfs_SetError(-EINVAL);
3185 Y_LOFF_T yaffs_freespace_reldev(struct yaffs_dev *dev)
3187 return yaffs_freespace_common(dev, NULL);
3190 Y_LOFF_T yaffs_freespace(const YCHAR *path)
3192 return yaffs_freespace_common(NULL, path);
3195 Y_LOFF_T yaffs_totalspace_common(struct yaffs_dev *dev, const YCHAR *path)
3197 Y_LOFF_T retVal = -1;
3201 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3202 yaffsfs_SetError(-EFAULT);
3206 if (yaffsfs_CheckPath(path) < 0) {
3207 yaffsfs_SetError(-ENAMETOOLONG);
3214 dev = yaffsfs_FindDevice(path, &dummy);
3215 if (dev && dev->is_mounted) {
3216 retVal = (dev->param.end_block - dev->param.start_block + 1) -
3217 dev->param.n_reserved_blocks;
3218 retVal *= dev->param.chunks_per_block;
3219 retVal *= dev->data_bytes_per_chunk;
3222 yaffsfs_SetError(-EINVAL);
3228 Y_LOFF_T yaffs_totalspace_reldev(struct yaffs_dev *dev)
3230 return yaffs_totalspace_common(dev, NULL);
3233 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
3235 return yaffs_totalspace_common(NULL, path);
3238 int yaffs_inodecount_common(struct yaffs_dev *dev, const YCHAR *path)
3240 Y_LOFF_T retVal = -1;
3244 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3245 yaffsfs_SetError(-EFAULT);
3249 if (yaffsfs_CheckPath(path) < 0) {
3250 yaffsfs_SetError(-ENAMETOOLONG);
3257 dev = yaffsfs_FindDevice(path, &dummy);
3258 if (dev && dev->is_mounted) {
3259 int n_obj = dev->n_obj;
3260 if (n_obj > dev->n_hardlinks)
3261 retVal = n_obj - dev->n_hardlinks;
3265 yaffsfs_SetError(-EINVAL);
3271 int yaffs_inodecount_reldev(struct yaffs_dev *dev)
3273 return yaffs_inodecount_common(dev, NULL);
3276 int yaffs_inodecount(const YCHAR *path)
3278 return yaffs_inodecount_common(NULL, path);
3281 void yaffs_add_device(struct yaffs_dev *dev)
3283 struct list_head *cfg;
3284 /* First check that the device is not in the list. */
3286 list_for_each(cfg, &yaffsfs_deviceList) {
3287 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
3291 dev->is_mounted = 0;
3292 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
3294 if (!dev->dev_list.next)
3295 INIT_LIST_HEAD(&dev->dev_list);
3297 list_add(&dev->dev_list, &yaffsfs_deviceList);
3302 void yaffs_remove_device(struct yaffs_dev *dev)
3304 list_del_init(&dev->dev_list);
3307 /* Functions to iterate through devices. NB Use with extreme care! */
3309 static struct list_head *dev_iterator;
3310 void yaffs_dev_rewind(void)
3312 dev_iterator = yaffsfs_deviceList.next;
3315 struct yaffs_dev *yaffs_next_dev(void)
3317 struct yaffs_dev *retval;
3321 if (dev_iterator == &yaffsfs_deviceList)
3324 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
3325 dev_iterator = dev_iterator->next;
3329 /* Directory search stuff. */
3331 static struct list_head search_contexts;
3333 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
3337 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3341 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
3342 dsc->nextReturn = NULL;
3345 list_entry(dsc->dirObj->variant.dir_variant.
3346 children.next, struct yaffs_obj,
3349 /* Hey someone isn't playing nice! */
3353 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
3357 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3359 if (dsc->nextReturn == NULL ||
3360 list_empty(&dsc->dirObj->variant.dir_variant.children))
3361 dsc->nextReturn = NULL;
3363 struct list_head *next = dsc->nextReturn->siblings.next;
3365 if (next == &dsc->dirObj->variant.dir_variant.children)
3366 dsc->nextReturn = NULL; /* end of list */
3368 dsc->nextReturn = list_entry(next,
3373 /* Hey someone isn't playing nice! */
3377 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
3380 struct list_head *i;
3381 struct yaffsfs_DirSearchContext *dsc;
3383 /* if search contexts not initilised then skip */
3384 if (!search_contexts.next)
3387 /* Iterate through the directory search contexts.
3388 * If any are the one being removed, then advance the dsc to
3389 * the next one to prevent a hanging ptr.
3391 list_for_each(i, &search_contexts) {
3393 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
3395 if (dsc->nextReturn == obj)
3396 yaffsfs_DirAdvance(dsc);
3402 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
3403 const YCHAR *dirname)
3405 yaffs_DIR *dir = NULL;
3406 struct yaffs_obj *obj = NULL;
3407 struct yaffsfs_DirSearchContext *dsc = NULL;
3411 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3412 yaffsfs_SetError(-EFAULT);
3416 if (yaffsfs_CheckPath(dirname) < 0) {
3417 yaffsfs_SetError(-ENAMETOOLONG);
3421 obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, ¬Dir, &loop);
3424 yaffsfs_SetError(-ENOTDIR);
3426 yaffsfs_SetError(-ELOOP);
3428 yaffsfs_SetError(-ENOENT);
3429 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3430 yaffsfs_SetError(-ENOTDIR);
3434 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3435 if (!yaffsfs_dsc[i].inUse)
3436 dsc = &yaffsfs_dsc[i];
3439 dir = (yaffs_DIR *) dsc;
3442 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3445 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3446 INIT_LIST_HEAD(&dsc->others);
3448 if (!search_contexts.next)
3449 INIT_LIST_HEAD(&search_contexts);
3451 list_add(&dsc->others, &search_contexts);
3452 yaffsfs_SetDirRewound(dsc);
3458 yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
3463 ret = yaffsfs_opendir_reldir_no_lock(reldir, dirname);
3468 yaffs_DIR *yaffs_opendir_reldev(struct yaffs_dev *dev, const YCHAR *dirname)
3470 return yaffs_opendir_reldir(ROOT_DIR(dev), dirname);
3473 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3475 return yaffs_opendir_reldir(NULL, dirname);
3478 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3480 struct yaffsfs_DirSearchContext *dsc;
3481 struct yaffs_dirent *retVal = NULL;
3483 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3486 if (dsc && dsc->inUse) {
3487 yaffsfs_SetError(0);
3488 if (dsc->nextReturn) {
3490 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3491 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3492 dsc->de.d_off = dsc->offset++;
3493 yaffs_get_obj_name(dsc->nextReturn,
3494 dsc->de.d_name, NAME_MAX);
3495 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3496 /* this should not happen! */
3497 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3499 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3501 yaffsfs_DirAdvance(dsc);
3505 yaffsfs_SetError(-EBADF);
3510 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3512 struct yaffs_dirent *ret;
3515 ret = yaffsfs_readdir_no_lock(dirp);
3520 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3522 struct yaffsfs_DirSearchContext *dsc;
3524 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3526 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3529 yaffsfs_SetDirRewound(dsc);
3533 void yaffs_rewinddir(yaffs_DIR *dirp)
3536 yaffsfs_rewinddir_no_lock(dirp);
3540 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3542 struct yaffs_dirent *ret = NULL;
3543 struct yaffsfs_FileDes *f;
3546 f = yaffsfs_HandleToFileDes(fd);
3548 ret = yaffsfs_readdir_no_lock(f->v.dir);
3553 void yaffs_rewinddir_fd(int fd)
3555 struct yaffsfs_FileDes *f;
3558 f = yaffsfs_HandleToFileDes(fd);
3560 yaffsfs_rewinddir_no_lock(f->v.dir);
3565 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3567 struct yaffsfs_DirSearchContext *dsc;
3569 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3571 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3572 yaffsfs_SetError(-EFAULT);
3577 list_del(&dsc->others); /* unhook from list */
3581 int yaffs_closedir(yaffs_DIR *dirp)
3586 ret = yaffsfs_closedir_no_lock(dirp);
3591 /* End of directory stuff */
3593 int yaffs_symlink_reldir(struct yaffs_obj *reldir,
3594 const YCHAR *oldpath, const YCHAR *newpath)
3596 struct yaffs_obj *parent = NULL;
3597 struct yaffs_obj *obj;
3600 int mode = 0; /* ignore for now */
3604 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3605 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3606 yaffsfs_SetError(-EFAULT);
3610 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3611 yaffsfs_SetError(-ENAMETOOLONG);
3616 parent = yaffsfs_FindDirectory(reldir, newpath, &name, 0, ¬Dir, &loop);
3617 if (!parent && notDir)
3618 yaffsfs_SetError(-ENOTDIR);
3620 yaffsfs_SetError(-ELOOP);
3621 else if (!parent || yaffs_strnlen(name, 5) < 1)
3622 yaffsfs_SetError(-ENOENT);
3623 else if (yaffsfs_TooManyObjects(parent->my_dev))
3624 yaffsfs_SetError(-ENFILE);
3625 else if (parent->my_dev->read_only)
3626 yaffsfs_SetError(-EROFS);
3628 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3631 else if (yaffsfs_FindObject(reldir, newpath, 0, 0,
3633 yaffsfs_SetError(-EEXIST);
3635 yaffsfs_SetError(-ENOSPC);
3643 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3645 return yaffs_symlink_reldir(NULL, oldpath, newpath);
3648 int yaffs_readlink_reldir(struct yaffs_obj *reldir,const YCHAR *path,
3649 YCHAR *buf, int bufsiz)
3651 struct yaffs_obj *obj = NULL;
3652 struct yaffs_obj *dir = NULL;
3657 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3658 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3659 yaffsfs_SetError(-EFAULT);
3665 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
3668 yaffsfs_SetError(-ENOTDIR);
3670 yaffsfs_SetError(-ELOOP);
3671 else if (!dir || !obj)
3672 yaffsfs_SetError(-ENOENT);
3673 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3674 yaffsfs_SetError(-EINVAL);
3676 YCHAR *alias = obj->variant.symlink_variant.alias;
3677 memset(buf, 0, bufsiz);
3678 yaffs_strncpy(buf, alias, bufsiz - 1);
3684 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3686 return yaffs_readlink_reldir(NULL, path, buf, bufsiz);
3689 int yaffs_link_reldir(struct yaffs_obj *reldir,
3690 const YCHAR *oldpath, const YCHAR *linkpath)
3692 /* Creates a link called newpath to existing oldpath */
3693 struct yaffs_obj *obj = NULL;
3694 struct yaffs_obj *lnk = NULL;
3695 struct yaffs_obj *obj_dir = NULL;
3696 struct yaffs_obj *lnk_dir = NULL;
3704 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3705 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3706 yaffsfs_SetError(-EFAULT);
3710 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3711 yaffsfs_SetError(-ENAMETOOLONG);
3717 obj = yaffsfs_FindObject(reldir, oldpath, 0, 1,
3718 &obj_dir, ¬DirObj, &objLoop);
3719 lnk = yaffsfs_FindObject(reldir, linkpath, 0, 0, NULL, NULL, NULL);
3720 lnk_dir = yaffsfs_FindDirectory(reldir, linkpath, &newname,
3721 0, ¬DirLnk, &lnkLoop);
3723 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3724 yaffsfs_SetError(-ENOTDIR);
3725 else if (objLoop || lnkLoop)
3726 yaffsfs_SetError(-ELOOP);
3727 else if (!obj_dir || !lnk_dir || !obj)
3728 yaffsfs_SetError(-ENOENT);
3729 else if (obj->my_dev->read_only)
3730 yaffsfs_SetError(-EROFS);
3731 else if (yaffsfs_TooManyObjects(obj->my_dev))
3732 yaffsfs_SetError(-ENFILE);
3734 yaffsfs_SetError(-EEXIST);
3735 else if (lnk_dir->my_dev != obj->my_dev)
3736 yaffsfs_SetError(-EXDEV);
3738 retVal = yaffsfs_CheckNameLength(newname);
3741 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3745 yaffsfs_SetError(-ENOSPC);
3754 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3756 return yaffs_link_reldir(NULL, oldpath, linkpath);
3759 int yaffs_mknod_reldir(struct yaffs_obj *reldir, const YCHAR *pathname,
3760 mode_t mode, dev_t dev_val)
3767 yaffsfs_SetError(-EINVAL);
3771 int yaffs_mknod_reldev(struct yaffs_dev *dev, const YCHAR *pathname, mode_t mode, dev_t dev_val)
3773 return yaffs_mknod_reldir(ROOT_DIR(dev), pathname, mode, dev_val);
3776 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev_val)
3778 return yaffs_mknod_reldir(NULL, pathname, mode, dev_val);
3782 * D E B U G F U N C T I O N S
3787 * Returns number of handles attached to the object
3789 int yaffs_n_handles_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3791 struct yaffs_obj *obj;
3793 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3794 yaffsfs_SetError(-EFAULT);
3798 if (yaffsfs_CheckPath(path) < 0) {
3799 yaffsfs_SetError(-ENAMETOOLONG);
3803 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
3806 return yaffsfs_CountHandles(obj);
3811 int yaffs_n_handles(const YCHAR *path)
3813 return yaffs_n_handles_reldir(NULL, path);
3816 int yaffs_get_error(void)
3818 return yaffsfs_GetLastError();
3821 int yaffs_set_error(int error)
3823 yaffsfs_SetError(error);
3827 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3837 reldir = yaffsfs_FindRoot(path, &rest);
3840 struct yaffs_dev *dev = reldir->my_dev;
3843 "n_page_writes.......... %d\n"
3844 "n_page_reads........... %d\n"
3845 "n_erasures....... %d\n"
3846 "n_gc_copies............ %d\n"
3847 "garbageCollections... %d\n"
3848 "passiveGarbageColl'ns %d\n"
3854 dev->garbageCollections, dev->passiveGarbageCollections);
3861 int yaffs_dump_dev(const YCHAR *path)
3863 return yaffs_dump_dev_reldir(NULL, path);