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
402 static int yaffs_toupper(YCHAR a)
404 if (a >= 'a' && a <= 'z')
405 return (a - 'a') + 'A';
410 static int yaffsfs_Match(YCHAR a, YCHAR b)
412 return (yaffs_toupper(a) == yaffs_toupper(b));
415 static int yaffsfs_Match(YCHAR a, YCHAR b)
422 static int yaffsfs_IsPathDivider(YCHAR ch)
424 const YCHAR *str = YAFFS_PATH_DIVIDERS;
435 static int yaffsfs_CheckNameLength(const char *name)
439 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
441 if (nameLength == 0) {
442 yaffsfs_SetError(-ENOENT);
444 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
445 yaffsfs_SetError(-ENAMETOOLONG);
452 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
454 YCHAR *alt_path = NULL;
459 * We don't have a definition for max path length.
460 * We will use 3 * max name length instead.
463 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
465 /* If the last character is a path divider, then we need to
466 * trim it back so that the name look-up works properly.
467 * eg. /foo/new_dir/ -> /foo/newdir
468 * Curveball: Need to handle multiple path dividers:
469 * eg. /foof/sdfse///// -> /foo/sdfse
471 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
472 alt_path = kmalloc(path_length + 1, 0);
475 yaffs_strcpy(alt_path, path);
476 for (i = path_length - 1;
477 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
478 alt_path[i] = (YCHAR) 0;
480 *ret_path = alt_path;
484 LIST_HEAD(yaffsfs_deviceList);
489 * Scan the configuration list to find the device
490 * Curveballs: Should match paths that end in '/' too
491 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
493 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
496 struct list_head *cfg;
497 const YCHAR *leftOver;
499 struct yaffs_dev *retval = NULL;
500 struct yaffs_dev *dev = NULL;
502 int longestMatch = -1;
506 * Check all configs, choose the one that:
507 * 1) Actually matches a prefix (ie /a amd /abc will not match
508 * 2) Matches the longest.
510 list_for_each(cfg, &yaffsfs_deviceList) {
511 dev = list_entry(cfg, struct yaffs_dev, dev_list);
520 while (matching && *p && *leftOver) {
521 /* Skip over any /s */
522 while (yaffsfs_IsPathDivider(*p))
525 /* Skip over any /s */
526 while (yaffsfs_IsPathDivider(*leftOver))
529 /* Now match the text part */
531 *p && !yaffsfs_IsPathDivider(*p) &&
532 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
533 if (yaffsfs_Match(*p, *leftOver)) {
543 /* Skip over any /s in leftOver */
544 while (yaffsfs_IsPathDivider(*leftOver))
547 /*Skip over any /s in p */
548 while (yaffsfs_IsPathDivider(*p))
551 /* p should now be at the end of the string if fully matched */
555 if (matching && (thisMatchLength > longestMatch)) {
557 *restOfPath = (YCHAR *) leftOver;
559 longestMatch = thisMatchLength;
566 static int yaffsfs_CheckPath(const YCHAR *path)
571 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
572 if (yaffsfs_IsPathDivider(*path)) {
580 return (*path) ? -1 : 0;
583 /* FindMountPoint only returns a dev entry if the path is a mount point */
584 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
586 struct yaffs_dev *dev;
587 YCHAR *restOfPath = NULL;
589 dev = yaffsfs_FindDevice(path, &restOfPath);
590 if (dev && restOfPath && *restOfPath)
595 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
598 struct yaffs_dev *dev;
600 dev = yaffsfs_FindDevice(path, restOfPath);
601 if (dev && dev->is_mounted)
602 return dev->root_dir;
607 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
608 int symDepth, int *loop)
612 obj = yaffs_get_equivalent_obj(obj);
614 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
615 YCHAR *alias = obj->variant.symlink_variant.alias;
617 if (yaffsfs_IsPathDivider(*alias))
619 * Starts with a /, need to scan from root up */
620 /* NB Might not work if this is called with root != NULL
622 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
623 1, NULL, NULL, loop);
626 * Relative to here so use the parent of the
629 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
630 1, NULL, NULL, loop);
636 * yaffsfs_FindDirectory
637 * Parse a path to determine the directory and the name within the directory.
639 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
641 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
643 YCHAR **name, int symDepth,
644 int *notDir, int *loop)
646 struct yaffs_obj *dir;
648 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
651 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
659 restOfPath = (YCHAR *) path;
661 dir = yaffsfs_FindRoot(path, &restOfPath);
666 * curve ball: also throw away surplus '/'
667 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
669 while (yaffsfs_IsPathDivider(*restOfPath))
670 restOfPath++; /* get rid of '/' */
675 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
676 if (i < YAFFS_MAX_NAME_LENGTH) {
677 str[i] = *restOfPath;
685 /* got to the end of the string */
688 if (yaffs_strcmp(str, _Y(".")) == 0) {
690 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
693 dir = yaffs_find_by_name(dir, str);
695 dir = yaffsfs_FollowLink(dir, symDepth, loop);
697 if (dir && dir->variant_type !=
698 YAFFS_OBJECT_TYPE_DIRECTORY) {
707 /* directory did not exist. */
711 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
715 int *notDir, int *loop)
717 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
722 * yaffsfs_FindObject turns a path for an existing object into the object
724 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
725 const YCHAR *path, int symDepth,
727 struct yaffs_obj **dirOut,
728 int *notDir, int *loop)
730 struct yaffs_obj *dir;
731 struct yaffs_obj *obj;
735 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
741 obj = yaffs_find_by_name(dir, name);
746 obj = yaffs_get_equivalent_obj(obj);
751 /*************************************************************************
752 * Start of yaffsfs visible functions.
753 *************************************************************************/
755 int yaffs_dup(int handle)
757 int newHandleNumber = -1;
758 struct yaffsfs_FileDes *existingFD = NULL;
759 struct yaffsfs_Handle *existingHandle = NULL;
760 struct yaffsfs_Handle *newHandle = NULL;
763 existingHandle = yaffsfs_HandleToPointer(handle);
764 existingFD = yaffsfs_HandleToFileDes(handle);
766 newHandleNumber = yaffsfs_NewHandle(&newHandle);
768 newHandle->fdId = existingHandle->fdId;
769 existingFD->handleCount++;
775 yaffsfs_SetError(-EBADF);
777 yaffsfs_SetError(-ENOMEM);
779 return newHandleNumber;
783 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
785 int current_objects = dev->n_obj - dev->n_deleted_files;
787 if (dev->param.max_objects && current_objects > dev->param.max_objects)
793 int yaffs_open_sharing_reldir(struct yaffs_obj *reldir, const YCHAR *path,
794 int oflag, int mode, int sharing)
796 struct yaffs_obj *obj = NULL;
797 struct yaffs_obj *dir = NULL;
800 struct yaffsfs_FileDes *fd = NULL;
803 int errorReported = 0;
804 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
805 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
806 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
807 u8 sharedReadAllowed;
808 u8 sharedWriteAllowed;
818 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
819 yaffsfs_SetError(-EFAULT);
823 if (yaffsfs_CheckPath(path) < 0) {
824 yaffsfs_SetError(-ENAMETOOLONG);
828 /* O_EXCL only has meaning if O_CREAT is specified */
829 if (!(oflag & O_CREAT))
832 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
833 if ((oflag & O_CREAT) & (oflag & O_EXCL))
836 /* Todo: Are there any more flag combos to sanitise ? */
838 /* Figure out if reading or writing is requested */
840 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
841 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
845 handle = yaffsfs_NewHandleAndFileDes();
848 yaffsfs_SetError(-ENFILE);
852 fd = yaffsfs_HandleToFileDes(handle);
854 /* try to find the exisiting object */
855 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
857 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
860 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
861 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
866 /* The file already exists or it might be a directory */
867 is_dir = (obj->variant_type ==
868 YAFFS_OBJECT_TYPE_DIRECTORY);
870 /* A directory can't be opened except for read */
872 (writeRequested || !readRequested ||
873 (oflag & ~O_RDONLY))) {
875 yaffsfs_SetError(-EISDIR);
880 dsc = yaffsfs_opendir_reldir_no_lock(reldir, path);
883 yaffsfs_SetError(-ENFILE);
888 /* Open should fail if O_CREAT and O_EXCL are specified
889 * for a file that exists.
891 if (!errorReported &&
892 (oflag & O_EXCL) && (oflag & O_CREAT)) {
894 yaffsfs_SetError(-EEXIST);
898 /* Check file permissions */
899 if (readRequested && !(obj->yst_mode & S_IREAD))
902 if (writeRequested && !(obj->yst_mode & S_IWRITE))
905 if (!errorReported && writeRequested &&
906 obj->my_dev->read_only) {
908 yaffsfs_SetError(-EROFS);
912 if (openDenied && !errorReported) {
913 yaffsfs_SetError(-EACCES);
917 /* Check sharing of an existing object. */
919 struct yaffsfs_FileDes *fdx;
922 sharedReadAllowed = 1;
923 sharedWriteAllowed = 1;
926 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
927 fdx = &yaffsfs_fd[i];
928 if (fdx->handleCount > 0 &&
930 yaffsfs_inode[fdx->inodeId].iObj
933 sharedReadAllowed = 0;
934 if (!fdx->shareWrite)
935 sharedWriteAllowed = 0;
943 if ((!sharedReadAllowed && readRequested) ||
944 (!shareRead && alreadyReading) ||
945 (!sharedWriteAllowed && writeRequested) ||
946 (!shareWrite && alreadyWriting)) {
948 yaffsfs_SetError(-EBUSY);
955 /* If we could not open an existing object, then let's see if
956 * the directory exists. If not, error.
958 if (!obj && !errorReported) {
959 dir = yaffsfs_FindDirectory(reldir, path, &name, 0,
961 if (!dir && notDir) {
962 yaffsfs_SetError(-ENOTDIR);
965 yaffsfs_SetError(-ELOOP);
968 yaffsfs_SetError(-ENOENT);
973 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
974 /* Let's see if we can create this file */
975 if (dir->my_dev->read_only) {
976 yaffsfs_SetError(-EROFS);
978 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
979 yaffsfs_SetError(-ENFILE);
982 obj = yaffs_create_file(dir, name, mode, 0, 0);
984 if (!obj && !errorReported) {
985 yaffsfs_SetError(-ENOSPC);
990 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
991 yaffsfs_SetError(-ENOENT);
995 if (obj && !openDenied) {
996 int inodeId = yaffsfs_GetInodeIdForObject(obj);
1000 * Todo: Fix any problem if inodes run out,
1001 * That can't happen if the number of inode
1002 * items >= number of handles.
1006 fd->inodeId = inodeId;
1007 fd->reading = readRequested;
1008 fd->writing = writeRequested;
1009 fd->append = (oflag & O_APPEND) ? 1 : 0;
1010 fd->shareRead = shareRead;
1011 fd->shareWrite = shareWrite;
1019 /* Hook inode to object */
1020 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1022 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1023 yaffs_resize_file(obj, 0);
1025 yaffsfs_PutHandle(handle);
1027 yaffsfs_SetError(0); /* Problem */
1037 int yaffs_open_sharing_reldev(struct yaffs_dev *dev, const YCHAR *path, int oflag,
1038 int mode, int sharing)
1040 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path,
1041 oflag, mode, sharing);
1044 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
1046 return yaffs_open_sharing_reldir(NULL, path, oflag, mode, sharing);
1049 int yaffs_open_reldir(struct yaffs_obj *reldir,const YCHAR *path, int oflag, int mode)
1051 return yaffs_open_sharing_reldir(reldir, path, oflag, mode,
1052 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1055 int yaffs_open_reldev(struct yaffs_dev *dev,const YCHAR *path, int oflag, int mode)
1057 return yaffs_open_sharing_reldir(ROOT_DIR(dev), path, oflag, mode,
1058 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1061 int yaffs_open(const YCHAR *path, int oflag, int mode)
1063 return yaffs_open_reldir(NULL, path, oflag, mode);
1066 static int yaffs_Dofsync(int handle, int datasync)
1069 struct yaffs_obj *obj;
1073 obj = yaffsfs_HandleToObject(handle);
1076 yaffsfs_SetError(-EBADF);
1077 else if (obj->my_dev->read_only)
1078 yaffsfs_SetError(-EROFS);
1080 yaffs_flush_file(obj, 1, datasync);
1089 int yaffs_fsync(int handle)
1091 return yaffs_Dofsync(handle, 0);
1094 int yaffs_flush(int handle)
1096 return yaffs_fsync(handle);
1099 int yaffs_fdatasync(int handle)
1101 return yaffs_Dofsync(handle, 1);
1104 int yaffs_close(int handle)
1106 struct yaffsfs_Handle *h = NULL;
1107 struct yaffsfs_FileDes *f;
1108 struct yaffs_obj *obj = NULL;
1113 h = yaffsfs_HandleToPointer(handle);
1114 f = yaffsfs_HandleToFileDes(handle);
1115 obj = yaffsfs_HandleToObject(handle);
1117 if (!h || !obj || !f)
1118 yaffsfs_SetError(-EBADF);
1122 yaffs_flush_file(obj, 1, 0);
1123 yaffsfs_PutHandle(handle);
1132 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1133 int isPread, Y_LOFF_T offset)
1135 struct yaffsfs_FileDes *fd = NULL;
1136 struct yaffs_obj *obj = NULL;
1138 Y_LOFF_T startPos = 0;
1139 Y_LOFF_T endPos = 0;
1144 u8 *buf = (u8 *) vbuf;
1146 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1147 yaffsfs_SetError(-EFAULT);
1152 fd = yaffsfs_HandleToFileDes(handle);
1153 obj = yaffsfs_HandleToObject(handle);
1157 yaffsfs_SetError(-EBADF);
1159 } else if (!fd->reading) {
1160 /* Not a reading handle */
1161 yaffsfs_SetError(-EINVAL);
1163 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1164 yaffsfs_SetError(-EINVAL);
1170 startPos = fd->v.position;
1174 if (yaffs_get_obj_length(obj) > pos)
1175 maxRead = yaffs_get_obj_length(obj) - pos;
1179 if (nbyte > maxRead)
1182 yaffsfs_GetHandle(handle);
1184 endPos = pos + nbyte;
1186 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1187 nbyte > YAFFS_MAX_FILE_SIZE ||
1188 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1194 nToRead = YAFFSFS_RW_SIZE -
1195 (pos & (YAFFSFS_RW_SIZE - 1));
1196 if (nToRead > nbyte)
1200 * Need to reverify object in case the device was
1201 * unmounted in another thread.
1203 obj = yaffsfs_HandleToObject(handle);
1207 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1215 if (nRead == nToRead)
1218 nbyte = 0; /* no more to read */
1227 yaffsfs_PutHandle(handle);
1231 fd->v.position = startPos + totalRead;
1233 yaffsfs_SetError(-EINVAL);
1240 return (totalRead >= 0) ? totalRead : -1;
1244 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1246 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1249 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1251 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1254 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1255 int isPwrite, Y_LOFF_T offset)
1257 struct yaffsfs_FileDes *fd = NULL;
1258 struct yaffs_obj *obj = NULL;
1260 Y_LOFF_T startPos = 0;
1263 int totalWritten = 0;
1264 int write_trhrough = 0;
1266 const u8 *buf = (const u8 *)vbuf;
1268 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1269 yaffsfs_SetError(-EFAULT);
1274 fd = yaffsfs_HandleToFileDes(handle);
1275 obj = yaffsfs_HandleToObject(handle);
1279 yaffsfs_SetError(-EBADF);
1281 } else if (!fd->writing) {
1282 yaffsfs_SetError(-EINVAL);
1284 } else if (obj->my_dev->read_only) {
1285 yaffsfs_SetError(-EROFS);
1289 startPos = yaffs_get_obj_length(obj);
1293 startPos = fd->v.position;
1295 yaffsfs_GetHandle(handle);
1297 endPos = pos + nbyte;
1299 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1300 nbyte > YAFFS_MAX_FILE_SIZE ||
1301 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1308 nToWrite = YAFFSFS_RW_SIZE -
1309 (pos & (YAFFSFS_RW_SIZE - 1));
1310 if (nToWrite > nbyte)
1314 * Need to reverify object in case the device was
1315 * remounted or unmounted in another thread.
1317 obj = yaffsfs_HandleToObject(handle);
1318 if (!obj || obj->my_dev->read_only)
1322 yaffs_wr_file(obj, buf, pos, nToWrite,
1325 totalWritten += nWritten;
1330 if (nWritten == nToWrite)
1335 if (nWritten < 1 && totalWritten < 1) {
1336 yaffsfs_SetError(-ENOSPC);
1346 yaffsfs_PutHandle(handle);
1349 if (totalWritten > 0)
1350 fd->v.position = startPos + totalWritten;
1352 yaffsfs_SetError(-EINVAL);
1358 return (totalWritten >= 0) ? totalWritten : -1;
1361 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1363 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1366 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1368 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1371 int yaffs_truncate_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1374 struct yaffs_obj *obj = NULL;
1375 struct yaffs_obj *dir = NULL;
1376 int result = YAFFS_FAIL;
1380 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1381 yaffsfs_SetError(-EFAULT);
1385 if (yaffsfs_CheckPath(path) < 0) {
1386 yaffsfs_SetError(-ENAMETOOLONG);
1392 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1393 obj = yaffsfs_FollowLink(obj, 0, &loop);
1396 yaffsfs_SetError(-ENOTDIR);
1398 yaffsfs_SetError(-ELOOP);
1399 else if (!dir || !obj)
1400 yaffsfs_SetError(-ENOENT);
1401 else if (obj->my_dev->read_only)
1402 yaffsfs_SetError(-EROFS);
1403 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1404 yaffsfs_SetError(-EISDIR);
1405 else if (obj->my_dev->read_only)
1406 yaffsfs_SetError(-EROFS);
1407 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1408 yaffsfs_SetError(-EINVAL);
1410 result = yaffs_resize_file(obj, new_size);
1414 return (result) ? 0 : -1;
1417 int yaffs_truncate_reldev(struct yaffs_dev *dev, const YCHAR *path,
1420 return yaffs_truncate_reldir(ROOT_DIR(dev), path, new_size);
1423 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1425 return yaffs_truncate_reldir(NULL, path, new_size);
1428 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1430 struct yaffsfs_FileDes *fd = NULL;
1431 struct yaffs_obj *obj = NULL;
1435 fd = yaffsfs_HandleToFileDes(handle);
1436 obj = yaffsfs_HandleToObject(handle);
1440 yaffsfs_SetError(-EBADF);
1441 else if (!fd->writing)
1442 yaffsfs_SetError(-EINVAL);
1443 else if (obj->my_dev->read_only)
1444 yaffsfs_SetError(-EROFS);
1445 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1446 yaffsfs_SetError(-EINVAL);
1448 /* resize the file */
1449 result = yaffs_resize_file(obj, new_size);
1452 return (result) ? 0 : -1;
1456 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1458 struct yaffsfs_FileDes *fd = NULL;
1459 struct yaffs_obj *obj = NULL;
1461 Y_LOFF_T fSize = -1;
1464 fd = yaffsfs_HandleToFileDes(handle);
1465 obj = yaffsfs_HandleToObject(handle);
1468 yaffsfs_SetError(-EBADF);
1469 else if (offset > YAFFS_MAX_FILE_SIZE)
1470 yaffsfs_SetError(-EINVAL);
1472 if (whence == SEEK_SET) {
1475 } else if (whence == SEEK_CUR) {
1476 if ((fd->v.position + offset) >= 0)
1477 pos = (fd->v.position + offset);
1478 } else if (whence == SEEK_END) {
1479 fSize = yaffs_get_obj_length(obj);
1480 if (fSize >= 0 && (fSize + offset) >= 0)
1481 pos = fSize + offset;
1484 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1485 fd->v.position = pos;
1487 yaffsfs_SetError(-EINVAL);
1497 static int yaffsfs_DoUnlink_reldir(struct yaffs_obj *reldir,
1498 const YCHAR *path, int isDirectory)
1500 struct yaffs_obj *dir = NULL;
1501 struct yaffs_obj *obj = NULL;
1503 int result = YAFFS_FAIL;
1507 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1508 yaffsfs_SetError(-EFAULT);
1512 if (yaffsfs_CheckPath(path) < 0) {
1513 yaffsfs_SetError(-ENAMETOOLONG);
1519 obj = yaffsfs_FindObject(reldir, path, 0, 0, NULL, NULL, NULL);
1520 dir = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
1523 yaffsfs_SetError(-ENOTDIR);
1525 yaffsfs_SetError(-ELOOP);
1527 yaffsfs_SetError(-ENOENT);
1528 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1529 yaffsfs_SetError(-EINVAL);
1531 yaffsfs_SetError(-ENOENT);
1532 else if (obj->my_dev->read_only)
1533 yaffsfs_SetError(-EROFS);
1534 else if (!isDirectory &&
1535 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1536 yaffsfs_SetError(-EISDIR);
1537 else if (isDirectory &&
1538 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1539 yaffsfs_SetError(-ENOTDIR);
1540 else if (isDirectory && obj == obj->my_dev->root_dir)
1541 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1543 result = yaffs_unlinker(dir, name);
1545 if (result == YAFFS_FAIL && isDirectory)
1546 yaffsfs_SetError(-ENOTEMPTY);
1551 return (result == YAFFS_FAIL) ? -1 : 0;
1554 int yaffs_unlink_reldir(struct yaffs_obj *reldir, const YCHAR *path)
1556 return yaffsfs_DoUnlink_reldir(reldir, path, 0);
1559 int yaffs_unlink_reldev(struct yaffs_dev *dev, const YCHAR *path)
1561 return yaffsfs_DoUnlink_reldir(ROOT_DIR(dev), path, 0);
1564 int yaffs_unlink(const YCHAR *path)
1566 return yaffs_unlink_reldir(NULL, path);
1569 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1571 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1572 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1578 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1580 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1581 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1587 int yaffs_rename_reldir(struct yaffs_obj *reldir,
1588 const YCHAR *oldPath, const YCHAR *newPath)
1590 struct yaffs_obj *olddir = NULL;
1591 struct yaffs_obj *newdir = NULL;
1592 struct yaffs_obj *obj = NULL;
1593 struct yaffs_obj *newobj = NULL;
1596 int result = YAFFS_FAIL;
1597 int rename_allowed = 1;
1603 YCHAR *alt_newpath = NULL;
1605 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1606 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1607 yaffsfs_SetError(-EFAULT);
1611 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1612 yaffsfs_SetError(-ENAMETOOLONG);
1616 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1617 yaffsfs_SetError(-ENOMEM);
1621 newPath = alt_newpath;
1625 olddir = yaffsfs_FindDirectory(reldir, oldPath, &oldname, 0,
1626 ¬OldDir, &oldLoop);
1627 newdir = yaffsfs_FindDirectory(reldir, newPath, &newname, 0,
1628 ¬NewDir, &newLoop);
1629 obj = yaffsfs_FindObject(reldir, oldPath, 0, 0, NULL, NULL, NULL);
1630 newobj = yaffsfs_FindObject(reldir, newPath, 0, 0, NULL, NULL, NULL);
1632 /* If the object being renamed is a directory and the
1633 * path ended with a "/" then the olddir == obj.
1634 * We pass through NULL for the old name to tell the lower layers
1635 * to use olddir as the object.
1641 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1642 yaffsfs_SetError(-ENOTDIR);
1644 } else if (oldLoop || newLoop) {
1645 yaffsfs_SetError(-ELOOP);
1647 } else if (olddir && oldname &&
1648 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1649 yaffsfs_SetError(-EINVAL);
1651 } else if (!olddir || !newdir || !obj) {
1652 yaffsfs_SetError(-ENOENT);
1654 } else if (obj->my_dev->read_only) {
1655 yaffsfs_SetError(-EROFS);
1657 } else if (rename_file_over_dir(obj, newobj)) {
1658 yaffsfs_SetError(-EISDIR);
1660 } else if (rename_dir_over_file(obj, newobj)) {
1661 yaffsfs_SetError(-ENOTDIR);
1663 } else if (yaffs_is_non_empty_dir(newobj)) {
1664 yaffsfs_SetError(-ENOTEMPTY);
1666 } else if (olddir->my_dev != newdir->my_dev) {
1667 /* Rename must be on same device */
1668 yaffsfs_SetError(-EXDEV);
1670 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1672 * It is a directory, check that it is not being renamed to
1673 * being its own decendent.
1674 * Do this by tracing from the new directory back to the root,
1678 struct yaffs_obj *xx = newdir;
1680 while (rename_allowed && xx) {
1685 if (!rename_allowed)
1686 yaffsfs_SetError(-EINVAL);
1690 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1696 return (result == YAFFS_FAIL) ? -1 : 0;
1699 int yaffs_rename_reldev(struct yaffs_dev *dev, const YCHAR *oldPath,
1700 const YCHAR *newPath)
1702 return yaffs_rename_reldir(ROOT_DIR(dev), oldPath, newPath);
1704 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1706 return yaffs_rename_reldir(NULL, oldPath, newPath);
1709 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1713 obj = yaffs_get_equivalent_obj(obj);
1716 buf->st_dev = (int)obj->my_dev->os_context;
1717 buf->st_ino = obj->obj_id;
1718 buf->st_mode = obj->yst_mode & ~S_IFMT;
1720 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1721 buf->st_mode |= S_IFDIR;
1722 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1723 buf->st_mode |= S_IFLNK;
1724 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1725 buf->st_mode |= S_IFREG;
1727 buf->st_nlink = yaffs_get_obj_link_count(obj);
1730 buf->st_rdev = obj->yst_rdev;
1731 buf->st_size = yaffs_get_obj_length(obj);
1732 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1733 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1735 #if CONFIG_YAFFS_WINCE
1736 buf->yst_wince_atime[0] = obj->win_atime[0];
1737 buf->yst_wince_atime[1] = obj->win_atime[1];
1738 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1739 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1740 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1741 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1743 buf->yst_atime = obj->yst_atime;
1744 buf->yst_ctime = obj->yst_ctime;
1745 buf->yst_mtime = obj->yst_mtime;
1752 static int yaffsfs_DoStatOrLStat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1753 struct yaffs_stat *buf, int doLStat)
1755 struct yaffs_obj *obj = NULL;
1756 struct yaffs_obj *dir = NULL;
1761 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1762 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1763 yaffsfs_SetError(-EFAULT);
1767 if (yaffsfs_CheckPath(path) < 0) {
1768 yaffsfs_SetError(-ENAMETOOLONG);
1774 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1776 if (!doLStat && obj)
1777 obj = yaffsfs_FollowLink(obj, 0, &loop);
1780 yaffsfs_SetError(-ENOTDIR);
1782 yaffsfs_SetError(-ELOOP);
1783 else if (!dir || !obj)
1784 yaffsfs_SetError(-ENOENT);
1786 retVal = yaffsfs_DoStat(obj, buf);
1794 int yaffs_stat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1795 struct yaffs_stat *buf)
1797 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 0);
1800 int yaffs_lstat_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1801 struct yaffs_stat *buf)
1803 return yaffsfs_DoStatOrLStat_reldir(reldir, path, buf, 1);
1806 int yaffs_stat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1807 struct yaffs_stat *buf)
1809 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 0);
1812 int yaffs_lstat_reldev(struct yaffs_dev *dev, const YCHAR *path,
1813 struct yaffs_stat *buf)
1815 return yaffsfs_DoStatOrLStat_reldir(ROOT_DIR(dev), path, buf, 1);
1818 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1820 return yaffs_stat_reldir(NULL, path, buf);
1823 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1825 return yaffs_lstat_reldir(NULL, path, buf);
1828 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1830 struct yaffs_obj *obj;
1834 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1835 yaffsfs_SetError(-EFAULT);
1840 obj = yaffsfs_HandleToObject(fd);
1843 retVal = yaffsfs_DoStat(obj, buf);
1846 yaffsfs_SetError(-EBADF);
1853 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1854 const struct yaffs_utimbuf *buf)
1859 struct yaffs_utimbuf local;
1861 obj = yaffs_get_equivalent_obj(obj);
1863 if (obj && obj->my_dev->read_only) {
1864 yaffsfs_SetError(-EROFS);
1869 local.actime = Y_CURRENT_TIME;
1870 local.modtime = local.actime;
1875 obj->yst_atime = buf->actime;
1876 obj->yst_mtime = buf->modtime;
1878 result = yaffs_flush_file(obj, 0, 0);
1879 retVal = result == YAFFS_OK ? 0 : -1;
1885 int yaffs_utime_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1886 const struct yaffs_utimbuf *buf)
1888 struct yaffs_obj *obj = NULL;
1889 struct yaffs_obj *dir = NULL;
1895 yaffsfs_SetError(-EFAULT);
1899 if (yaffsfs_CheckPath(path) < 0) {
1900 yaffsfs_SetError(-ENAMETOOLONG);
1906 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1909 yaffsfs_SetError(-ENOTDIR);
1911 yaffsfs_SetError(-ELOOP);
1912 else if (!dir || !obj)
1913 yaffsfs_SetError(-ENOENT);
1915 retVal = yaffsfs_DoUtime(obj, buf);
1923 int yaffs_utime_reldev(struct yaffs_dev *dev, const YCHAR *path,
1924 const struct yaffs_utimbuf *buf)
1926 return yaffs_utime_reldir(ROOT_DIR(dev), path, buf);
1929 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1931 return yaffs_utime_reldir(NULL, path, buf);
1934 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1936 struct yaffs_obj *obj;
1941 obj = yaffsfs_HandleToObject(fd);
1944 retVal = yaffsfs_DoUtime(obj, buf);
1947 yaffsfs_SetError(-EBADF);
1954 #ifndef CONFIG_YAFFS_WINCE
1955 /* xattrib functions */
1957 static int yaffs_do_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
1958 const char *name, const void *data, int size,
1959 int flags, int follow)
1961 struct yaffs_obj *obj;
1962 struct yaffs_obj *dir;
1968 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1969 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1970 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1971 yaffsfs_SetError(-EFAULT);
1975 if (yaffsfs_CheckPath(path) < 0) {
1976 yaffsfs_SetError(-ENAMETOOLONG);
1982 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
1985 obj = yaffsfs_FollowLink(obj, 0, &loop);
1988 yaffsfs_SetError(-ENOTDIR);
1990 yaffsfs_SetError(-ELOOP);
1991 else if (!dir || !obj)
1992 yaffsfs_SetError(-ENOENT);
1994 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1996 yaffsfs_SetError(retVal);
2007 int yaffs_setxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2008 const char *name, const void *data, int size, int flags)
2010 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 1);
2013 int yaffs_setxattr_reldev(struct yaffs_dev *dev, const YCHAR *path,
2014 const char *name, const void *data, int size, int flags)
2016 return yaffs_setxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2019 int yaffs_setxattr(const YCHAR *path, const char *name,
2020 const void *data, int size, int flags)
2022 return yaffs_setxattr_reldir(NULL, path, name, data, size, flags);
2025 int yaffs_lsetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path, const char *name,
2026 const void *data, int size, int flags)
2028 return yaffs_do_setxattr_reldir(reldir, path, name, data, size, flags, 0);
2031 int yaffs_lsetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name,
2032 const void *data, int size, int flags)
2034 return yaffs_lsetxattr_reldir(ROOT_DIR(dev), path, name, data, size, flags);
2037 int yaffs_lsetxattr(const YCHAR *path, const char *name,
2038 const void *data, int size, int flags)
2040 return yaffs_lsetxattr_reldir(NULL, path, name, data, size, flags);
2043 int yaffs_fsetxattr(int fd, const char *name,
2044 const void *data, int size, int flags)
2046 struct yaffs_obj *obj;
2050 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2051 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
2052 yaffsfs_SetError(-EFAULT);
2057 obj = yaffsfs_HandleToObject(fd);
2060 yaffsfs_SetError(-EBADF);
2062 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
2064 yaffsfs_SetError(retVal);
2074 static int yaffs_do_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2075 const char *name, void *data, int size, int follow)
2077 struct yaffs_obj *obj;
2078 struct yaffs_obj *dir;
2083 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2084 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2085 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2086 yaffsfs_SetError(-EFAULT);
2090 if (yaffsfs_CheckPath(path) < 0) {
2091 yaffsfs_SetError(-ENAMETOOLONG);
2097 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2100 obj = yaffsfs_FollowLink(obj, 0, &loop);
2103 yaffsfs_SetError(-ENOTDIR);
2105 yaffsfs_SetError(-ELOOP);
2106 else if (!dir || !obj)
2107 yaffsfs_SetError(-ENOENT);
2109 retVal = yaffs_get_xattrib(obj, name, data, size);
2111 yaffsfs_SetError(retVal);
2121 int yaffs_getxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2122 const char *name, void *data, int size)
2124 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 1);
2127 int yaffs_getxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2129 return yaffs_getxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2132 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
2134 return yaffs_getxattr_reldir(NULL, path, name, data, size);
2137 int yaffs_lgetxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2138 const char *name, void *data, int size)
2140 return yaffs_do_getxattr_reldir(reldir, path, name, data, size, 0);
2143 int yaffs_lgetxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name, void *data, int size)
2145 return yaffs_lgetxattr_reldir(ROOT_DIR(dev), path, name, data, size);
2148 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2150 return yaffs_lgetxattr_reldir(NULL, path, name, data, size);
2153 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2155 struct yaffs_obj *obj;
2159 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2160 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2161 yaffsfs_SetError(-EFAULT);
2166 obj = yaffsfs_HandleToObject(fd);
2169 retVal = yaffs_get_xattrib(obj, name, data, size);
2171 yaffsfs_SetError(retVal);
2176 yaffsfs_SetError(-EBADF);
2183 static int yaffs_do_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2184 char *data, int size, int follow)
2186 struct yaffs_obj *obj = NULL;
2187 struct yaffs_obj *dir = NULL;
2192 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2193 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2194 yaffsfs_SetError(-EFAULT);
2198 if (yaffsfs_CheckPath(path) < 0) {
2199 yaffsfs_SetError(-ENAMETOOLONG);
2205 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2208 obj = yaffsfs_FollowLink(obj, 0, &loop);
2211 yaffsfs_SetError(-ENOTDIR);
2213 yaffsfs_SetError(-ELOOP);
2214 else if (!dir || !obj)
2215 yaffsfs_SetError(-ENOENT);
2217 retVal = yaffs_list_xattrib(obj, data, size);
2219 yaffsfs_SetError(retVal);
2230 int yaffs_listxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2231 char *data, int size)
2233 return yaffs_do_listxattr_reldir(reldir, path, data, size, 1);
2236 int yaffs_listxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2238 return yaffs_listxattr_reldir(ROOT_DIR(dev), path, data, size);
2241 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2243 return yaffs_listxattr_reldir(NULL, path, data, size);
2246 int yaffs_llistxattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2247 char *data, int size)
2249 return yaffs_do_listxattr_reldir(reldir, path, data, size, 0);
2252 int yaffs_llistxattr_reldev(struct yaffs_dev *dev, const YCHAR *path, char *data, int size)
2254 return yaffs_llistxattr_reldir(ROOT_DIR(dev), path, data, size);
2257 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2259 return yaffs_llistxattr_reldir(NULL, path, data, size);
2262 int yaffs_flistxattr(int fd, char *data, int size)
2264 struct yaffs_obj *obj;
2268 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2269 yaffsfs_SetError(-EFAULT);
2274 obj = yaffsfs_HandleToObject(fd);
2277 retVal = yaffs_list_xattrib(obj, data, size);
2279 yaffsfs_SetError(retVal);
2284 yaffsfs_SetError(-EBADF);
2291 static int yaffs_do_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2292 const char *name, int follow)
2294 struct yaffs_obj *obj = NULL;
2295 struct yaffs_obj *dir = NULL;
2300 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2301 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2302 yaffsfs_SetError(-EFAULT);
2306 if (yaffsfs_CheckPath(path) < 0) {
2307 yaffsfs_SetError(-ENAMETOOLONG);
2313 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2316 obj = yaffsfs_FollowLink(obj, 0, &loop);
2319 yaffsfs_SetError(-ENOTDIR);
2321 yaffsfs_SetError(-ELOOP);
2322 else if (!dir || !obj)
2323 yaffsfs_SetError(-ENOENT);
2325 retVal = yaffs_remove_xattrib(obj, name);
2327 yaffsfs_SetError(retVal);
2338 int yaffs_removexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2341 return yaffs_do_removexattr_reldir(reldir, path, name, 1);
2344 int yaffs_removexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2346 return yaffs_removexattr_reldir(ROOT_DIR(dev), path, name);
2349 int yaffs_removexattr(const YCHAR *path, const char *name)
2351 return yaffs_removexattr_reldir(NULL, path, name);
2354 int yaffs_lremovexattr_reldir(struct yaffs_obj *reldir, const YCHAR *path,
2357 return yaffs_do_removexattr_reldir(reldir, path, name, 0);
2360 int yaffs_lremovexattr_reldev(struct yaffs_dev *dev, const YCHAR *path, const char *name)
2362 return yaffs_lremovexattr_reldir(ROOT_DIR(dev), path, name);
2365 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2367 return yaffs_lremovexattr_reldir(NULL, path, name);
2370 int yaffs_fremovexattr(int fd, const char *name)
2372 struct yaffs_obj *obj;
2376 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2377 yaffsfs_SetError(-EFAULT);
2382 obj = yaffsfs_HandleToObject(fd);
2385 retVal = yaffs_remove_xattrib(obj, name);
2387 yaffsfs_SetError(retVal);
2392 yaffsfs_SetError(-EBADF);
2400 #ifdef CONFIG_YAFFS_WINCE
2401 int yaffs_get_wince_times(int fd, unsigned *wctime,
2402 unsigned *watime, unsigned *wmtime)
2404 struct yaffs_obj *obj;
2409 obj = yaffsfs_HandleToObject(fd);
2414 wctime[0] = obj->win_ctime[0];
2415 wctime[1] = obj->win_ctime[1];
2418 watime[0] = obj->win_atime[0];
2419 watime[1] = obj->win_atime[1];
2422 wmtime[0] = obj->win_mtime[0];
2423 wmtime[1] = obj->win_mtime[1];
2429 yaffsfs_SetError(-EBADF);
2436 int yaffs_set_wince_times(int fd,
2437 const unsigned *wctime,
2438 const unsigned *watime, const unsigned *wmtime)
2440 struct yaffs_obj *obj;
2445 obj = yaffsfs_HandleToObject(fd);
2450 obj->win_ctime[0] = wctime[0];
2451 obj->win_ctime[1] = wctime[1];
2454 obj->win_atime[0] = watime[0];
2455 obj->win_atime[1] = watime[1];
2458 obj->win_mtime[0] = wmtime[0];
2459 obj->win_mtime[1] = wmtime[1];
2463 result = yaffs_flush_file(obj, 0, 0);
2467 yaffsfs_SetError(-EBADF);
2476 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2481 obj = yaffs_get_equivalent_obj(obj);
2484 obj->yst_mode = mode;
2486 result = yaffs_flush_file(obj, 0, 0);
2489 return result == YAFFS_OK ? 0 : -1;
2492 int yaffs_access_reldir(struct yaffs_obj *reldir, const YCHAR *path, int amode)
2494 struct yaffs_obj *obj = NULL;
2495 struct yaffs_obj *dir = NULL;
2500 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2501 yaffsfs_SetError(-EFAULT);
2505 if (yaffsfs_CheckPath(path) < 0) {
2506 yaffsfs_SetError(-ENAMETOOLONG);
2510 if (amode & ~(R_OK | W_OK | X_OK)) {
2511 yaffsfs_SetError(-EINVAL);
2517 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2518 obj = yaffsfs_FollowLink(obj, 0, &loop);
2521 yaffsfs_SetError(-ENOTDIR);
2523 yaffsfs_SetError(-ELOOP);
2524 else if (!dir || !obj)
2525 yaffsfs_SetError(-ENOENT);
2526 else if ((amode & W_OK) && obj->my_dev->read_only)
2527 yaffsfs_SetError(-EROFS);
2531 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2533 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2535 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2539 yaffsfs_SetError(-EACCES);
2550 int yaffs_access_reldev(struct yaffs_dev *dev, const YCHAR *path, int amode)
2552 return yaffs_access_reldir(ROOT_DIR(dev), path, amode);
2555 int yaffs_access(const YCHAR *path, int amode)
2557 return yaffs_access_reldir(NULL, path, amode);
2560 int yaffs_chmod_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2562 struct yaffs_obj *obj = NULL;
2563 struct yaffs_obj *dir = NULL;
2568 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2569 yaffsfs_SetError(-EFAULT);
2573 if (yaffsfs_CheckPath(path) < 0) {
2574 yaffsfs_SetError(-ENAMETOOLONG);
2578 if (mode & ~(0777)) {
2579 yaffsfs_SetError(-EINVAL);
2585 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
2586 obj = yaffsfs_FollowLink(obj, 0, &loop);
2589 yaffsfs_SetError(-ENOTDIR);
2591 yaffsfs_SetError(-ELOOP);
2592 else if (!dir || !obj)
2593 yaffsfs_SetError(-ENOENT);
2594 else if (obj->my_dev->read_only)
2595 yaffsfs_SetError(-EROFS);
2597 retVal = yaffsfs_DoChMod(obj, mode);
2605 int yaffs_chmod_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2607 return yaffs_chmod_reldir(ROOT_DIR(dev), path, mode);
2610 int yaffs_chmod(const YCHAR *path, mode_t mode)
2612 return yaffs_chmod_reldir(NULL, path, mode);
2615 int yaffs_fchmod(int fd, mode_t mode)
2617 struct yaffs_obj *obj;
2620 if (mode & ~(0777)) {
2621 yaffsfs_SetError(-EINVAL);
2626 obj = yaffsfs_HandleToObject(fd);
2629 yaffsfs_SetError(-EBADF);
2630 else if (obj->my_dev->read_only)
2631 yaffsfs_SetError(-EROFS);
2633 retVal = yaffsfs_DoChMod(obj, mode);
2640 int yaffs_mkdir_reldir(struct yaffs_obj *reldir, const YCHAR *path, mode_t mode)
2642 struct yaffs_obj *parent = NULL;
2643 struct yaffs_obj *dir = NULL;
2645 YCHAR *alt_path = NULL;
2650 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2651 yaffsfs_SetError(-EFAULT);
2655 if (yaffsfs_CheckPath(path) < 0) {
2656 yaffsfs_SetError(-ENAMETOOLONG);
2660 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2661 yaffsfs_SetError(-ENOMEM);
2668 parent = yaffsfs_FindDirectory(reldir, path, &name, 0, ¬Dir, &loop);
2669 if (!parent && notDir)
2670 yaffsfs_SetError(-ENOTDIR);
2672 yaffsfs_SetError(-ELOOP);
2674 yaffsfs_SetError(-ENOENT);
2675 else if (yaffsfs_TooManyObjects(parent->my_dev))
2676 yaffsfs_SetError(-ENFILE);
2677 else if (yaffs_strnlen(name, 5) == 0) {
2678 /* Trying to make the root itself */
2679 yaffsfs_SetError(-EEXIST);
2680 } else if (parent->my_dev->read_only)
2681 yaffsfs_SetError(-EROFS);
2683 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2686 else if (yaffs_find_by_name(parent, name))
2687 yaffsfs_SetError(-EEXIST); /* name exists */
2689 yaffsfs_SetError(-ENOSPC); /* assume no space */
2699 int yaffs_mkdir_reldev(struct yaffs_dev *dev, const YCHAR *path, mode_t mode)
2701 return yaffs_mkdir_reldir(ROOT_DIR(dev), path, mode);
2704 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2706 return yaffs_mkdir_reldir(NULL, path, mode);
2709 int yaffs_rmdir_reldir(struct yaffs_obj *reldir, const YCHAR *path)
2714 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2715 yaffsfs_SetError(-EFAULT);
2719 if (yaffsfs_CheckPath(path) < 0) {
2720 yaffsfs_SetError(-ENAMETOOLONG);
2724 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2725 yaffsfs_SetError(-ENOMEM);
2730 result = yaffsfs_DoUnlink_reldir(reldir, path, 1);
2737 int yaffs_rmdir_reldev(struct yaffs_dev *dev, const YCHAR *path)
2739 return yaffs_rmdir_reldir(ROOT_DIR(dev), path);
2742 int yaffs_rmdir(const YCHAR *path)
2744 return yaffs_rmdir_reldir(NULL, path);
2748 * The mount/unmount/sync functions act on devices rather than reldirs.
2750 void *yaffs_getdev(const YCHAR *path)
2752 struct yaffs_dev *dev = NULL;
2754 dev = yaffsfs_FindDevice(path, &dummy);
2758 int yaffs_mount_common(struct yaffs_dev *dev, const YCHAR *path,
2759 int read_only, int skip_checkpt)
2762 int result = YAFFS_FAIL;
2765 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2766 yaffsfs_SetError(-EFAULT);
2770 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2772 if (yaffsfs_CheckPath(path) < 0) {
2773 yaffsfs_SetError(-ENAMETOOLONG);
2780 yaffsfs_InitHandles();
2783 dev = yaffsfs_FindMountPoint(path);
2786 if (!dev->is_mounted) {
2787 dev->read_only = read_only ? 1 : 0;
2789 u8 skip = dev->param.skip_checkpt_rd;
2790 dev->param.skip_checkpt_rd = 1;
2791 result = yaffs_guts_initialise(dev);
2792 dev->param.skip_checkpt_rd = skip;
2794 result = yaffs_guts_initialise(dev);
2797 if (result == YAFFS_FAIL)
2798 yaffsfs_SetError(-ENOMEM);
2799 retVal = result ? 0 : -1;
2802 yaffsfs_SetError(-EBUSY);
2804 yaffsfs_SetError(-ENODEV);
2811 int yaffs_mount3_reldev(struct yaffs_dev *dev, int read_only, int skip_checkpt)
2813 return yaffs_mount_common(dev, NULL, read_only, skip_checkpt);
2816 int yaffs_mount3(const YCHAR *path, int read_only, int skip_checkpt)
2818 return yaffs_mount_common(NULL, path, read_only, skip_checkpt);
2821 int yaffs_mount2_reldev(struct yaffs_dev *dev, int readonly)
2823 return yaffs_mount_common(dev, NULL, readonly, 0);
2826 int yaffs_mount2(const YCHAR *path, int readonly)
2828 return yaffs_mount_common(NULL, path, readonly, 0);
2831 int yaffs_mount_reldev(struct yaffs_dev *dev)
2833 return yaffs_mount_common(dev, NULL, 0, 0);
2836 int yaffs_mount(const YCHAR *path)
2838 return yaffs_mount_common(NULL, path, 0, 0);
2841 int yaffs_sync_common(struct yaffs_dev *dev, const YCHAR *path)
2847 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2848 yaffsfs_SetError(-EFAULT);
2852 if (yaffsfs_CheckPath(path) < 0) {
2853 yaffsfs_SetError(-ENAMETOOLONG);
2860 dev = yaffsfs_FindDevice(path, &dummy);
2863 if (!dev->is_mounted)
2864 yaffsfs_SetError(-EINVAL);
2865 else if (dev->read_only)
2866 yaffsfs_SetError(-EROFS);
2869 yaffs_flush_whole_cache(dev);
2870 yaffs_checkpoint_save(dev);
2875 yaffsfs_SetError(-ENODEV);
2881 int yaffs_sync_reldev(struct yaffs_dev *dev)
2883 return yaffs_sync_common(dev, NULL);
2886 int yaffs_sync(const YCHAR *path)
2888 return yaffs_sync_common(NULL, path);
2892 static int yaffsfs_bg_gc_common(struct yaffs_dev *dev,
2900 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2901 yaffsfs_SetError(-EFAULT);
2905 if (yaffsfs_CheckPath(path) < 0) {
2906 yaffsfs_SetError(-ENAMETOOLONG);
2913 dev = yaffsfs_FindDevice(path, &dummy);
2916 if (!dev->is_mounted)
2917 yaffsfs_SetError(-EINVAL);
2919 retVal = yaffs_bg_gc(dev, urgency);
2921 yaffsfs_SetError(-ENODEV);
2927 /* Background gc functions.
2928 * These return 0 when bg done or greater than 0 when gc has been
2929 * done and there is still a lot of garbage to be cleaned up.
2932 int yaffs_do_background_gc(const YCHAR *path, int urgency)
2934 return yaffsfs_bg_gc_common(NULL, path, urgency);
2937 int yaffs_do_background_gc_reldev(struct yaffs_dev *dev, int urgency)
2939 return yaffsfs_bg_gc_common(dev, NULL, urgency);
2942 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2945 struct yaffs_obj *obj;
2947 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2948 obj = yaffsfs_HandleToObject(i);
2949 if (obj && obj->my_dev == dev)
2955 int yaffs_remount_common(struct yaffs_dev *dev, const YCHAR *path,
2956 int force, int read_only)
2960 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2961 yaffsfs_SetError(-EFAULT);
2965 if (yaffsfs_CheckPath(path) < 0) {
2966 yaffsfs_SetError(-ENAMETOOLONG);
2972 dev = yaffsfs_FindMountPoint(path);
2975 if (dev->is_mounted) {
2976 yaffs_flush_whole_cache(dev);
2978 if (force || !yaffsfs_IsDevBusy(dev)) {
2980 yaffs_checkpoint_save(dev);
2981 dev->read_only = read_only ? 1 : 0;
2984 yaffsfs_SetError(-EBUSY);
2987 yaffsfs_SetError(-EINVAL);
2990 yaffsfs_SetError(-ENODEV);
2997 int yaffs_remount_reldev(struct yaffs_dev *dev, int force, int read_only)
2999 return yaffs_remount_common(dev, NULL, force, read_only);
3001 int yaffs_remount(const YCHAR *path, int force, int read_only)
3003 return yaffs_remount_common(NULL, path, force, read_only);
3006 int yaffs_unmount2_common(struct yaffs_dev *dev, const YCHAR *path, int force)
3010 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3011 yaffsfs_SetError(-EFAULT);
3015 if (yaffsfs_CheckPath(path) < 0) {
3016 yaffsfs_SetError(-ENAMETOOLONG);
3022 dev = yaffsfs_FindMountPoint(path);
3025 if (dev->is_mounted) {
3027 yaffs_flush_whole_cache(dev);
3028 yaffs_checkpoint_save(dev);
3029 inUse = yaffsfs_IsDevBusy(dev);
3030 if (!inUse || force) {
3032 yaffsfs_BreakDeviceHandles(dev);
3033 yaffs_deinitialise(dev);
3037 yaffsfs_SetError(-EBUSY);
3040 yaffsfs_SetError(-EINVAL);
3043 yaffsfs_SetError(-ENODEV);
3050 int yaffs_unmount2_reldev(struct yaffs_dev *dev, int force)
3052 return yaffs_unmount2_common(dev, NULL, force);
3055 int yaffs_unmount2(const YCHAR *path, int force)
3057 return yaffs_unmount2_common(NULL, path, force);
3060 int yaffs_unmount_reldev(struct yaffs_dev *dev)
3062 return yaffs_unmount2_reldev(dev, 0);
3065 int yaffs_unmount(const YCHAR *path)
3067 return yaffs_unmount2(path, 0);
3070 int yaffs_format_common(struct yaffs_dev *dev,
3073 int force_unmount_flag,
3081 yaffsfs_SetError(-EFAULT);
3085 if (yaffsfs_CheckPath(path) < 0) {
3086 yaffsfs_SetError(-ENAMETOOLONG);
3093 dev = yaffsfs_FindMountPoint(path);
3096 int was_mounted = dev->is_mounted;
3098 if (dev->is_mounted && unmount_flag) {
3100 yaffs_flush_whole_cache(dev);
3101 yaffs_checkpoint_save(dev);
3102 inUse = yaffsfs_IsDevBusy(dev);
3103 if (!inUse || force_unmount_flag) {
3105 yaffsfs_BreakDeviceHandles(dev);
3106 yaffs_deinitialise(dev);
3110 if(dev->is_mounted) {
3111 yaffsfs_SetError(-EBUSY);
3114 yaffs_guts_format_dev(dev);
3115 if(was_mounted && remount_flag) {
3116 result = yaffs_guts_initialise(dev);
3117 if (result == YAFFS_FAIL) {
3118 yaffsfs_SetError(-ENOMEM);
3124 yaffsfs_SetError(-ENODEV);
3133 int yaffs_format_reldev(struct yaffs_dev *dev,
3135 int force_unmount_flag,
3138 return yaffs_format_common(dev, NULL, unmount_flag,
3139 force_unmount_flag, remount_flag);
3142 int yaffs_format(const YCHAR *path,
3144 int force_unmount_flag,
3147 return yaffs_format_common(NULL, path, unmount_flag,
3148 force_unmount_flag, remount_flag);
3151 Y_LOFF_T yaffs_freespace_common(struct yaffs_dev *dev, const YCHAR *path)
3153 Y_LOFF_T retVal = -1;
3157 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3158 yaffsfs_SetError(-EFAULT);
3162 if (yaffsfs_CheckPath(path) < 0) {
3163 yaffsfs_SetError(-ENAMETOOLONG);
3170 dev = yaffsfs_FindDevice(path, &dummy);
3171 if (dev && dev->is_mounted) {
3172 retVal = yaffs_get_n_free_chunks(dev);
3173 retVal *= dev->data_bytes_per_chunk;
3176 yaffsfs_SetError(-EINVAL);
3182 Y_LOFF_T yaffs_freespace_reldev(struct yaffs_dev *dev)
3184 return yaffs_freespace_common(dev, NULL);
3187 Y_LOFF_T yaffs_freespace(const YCHAR *path)
3189 return yaffs_freespace_common(NULL, path);
3192 Y_LOFF_T yaffs_totalspace_common(struct yaffs_dev *dev, const YCHAR *path)
3194 Y_LOFF_T retVal = -1;
3198 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3199 yaffsfs_SetError(-EFAULT);
3203 if (yaffsfs_CheckPath(path) < 0) {
3204 yaffsfs_SetError(-ENAMETOOLONG);
3211 dev = yaffsfs_FindDevice(path, &dummy);
3212 if (dev && dev->is_mounted) {
3213 retVal = (dev->param.end_block - dev->param.start_block + 1) -
3214 dev->param.n_reserved_blocks;
3215 retVal *= dev->param.chunks_per_block;
3216 retVal *= dev->data_bytes_per_chunk;
3219 yaffsfs_SetError(-EINVAL);
3225 Y_LOFF_T yaffs_totalspace_reldev(struct yaffs_dev *dev)
3227 return yaffs_totalspace_common(dev, NULL);
3230 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
3232 return yaffs_totalspace_common(NULL, path);
3235 int yaffs_inodecount_common(struct yaffs_dev *dev, const YCHAR *path)
3237 Y_LOFF_T retVal = -1;
3241 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3242 yaffsfs_SetError(-EFAULT);
3246 if (yaffsfs_CheckPath(path) < 0) {
3247 yaffsfs_SetError(-ENAMETOOLONG);
3254 dev = yaffsfs_FindDevice(path, &dummy);
3255 if (dev && dev->is_mounted) {
3256 int n_obj = dev->n_obj;
3257 if (n_obj > dev->n_hardlinks)
3258 retVal = n_obj - dev->n_hardlinks;
3262 yaffsfs_SetError(-EINVAL);
3268 int yaffs_inodecount_reldev(struct yaffs_dev *dev)
3270 return yaffs_inodecount_common(dev, NULL);
3273 int yaffs_inodecount(const YCHAR *path)
3275 return yaffs_inodecount_common(NULL, path);
3278 void yaffs_add_device(struct yaffs_dev *dev)
3280 struct list_head *cfg;
3281 /* First check that the device is not in the list. */
3283 list_for_each(cfg, &yaffsfs_deviceList) {
3284 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
3288 dev->is_mounted = 0;
3289 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
3291 if (!dev->dev_list.next)
3292 INIT_LIST_HEAD(&dev->dev_list);
3294 list_add(&dev->dev_list, &yaffsfs_deviceList);
3299 void yaffs_remove_device(struct yaffs_dev *dev)
3301 list_del_init(&dev->dev_list);
3304 /* Functions to iterate through devices. NB Use with extreme care! */
3306 static struct list_head *dev_iterator;
3307 void yaffs_dev_rewind(void)
3309 dev_iterator = yaffsfs_deviceList.next;
3312 struct yaffs_dev *yaffs_next_dev(void)
3314 struct yaffs_dev *retval;
3318 if (dev_iterator == &yaffsfs_deviceList)
3321 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
3322 dev_iterator = dev_iterator->next;
3326 /* Directory search stuff. */
3328 static struct list_head search_contexts;
3330 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
3334 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3338 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
3339 dsc->nextReturn = NULL;
3342 list_entry(dsc->dirObj->variant.dir_variant.
3343 children.next, struct yaffs_obj,
3346 /* Hey someone isn't playing nice! */
3350 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
3354 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
3356 if (dsc->nextReturn == NULL ||
3357 list_empty(&dsc->dirObj->variant.dir_variant.children))
3358 dsc->nextReturn = NULL;
3360 struct list_head *next = dsc->nextReturn->siblings.next;
3362 if (next == &dsc->dirObj->variant.dir_variant.children)
3363 dsc->nextReturn = NULL; /* end of list */
3365 dsc->nextReturn = list_entry(next,
3370 /* Hey someone isn't playing nice! */
3374 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
3377 struct list_head *i;
3378 struct yaffsfs_DirSearchContext *dsc;
3380 /* if search contexts not initilised then skip */
3381 if (!search_contexts.next)
3384 /* Iterate through the directory search contexts.
3385 * If any are the one being removed, then advance the dsc to
3386 * the next one to prevent a hanging ptr.
3388 list_for_each(i, &search_contexts) {
3390 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
3392 if (dsc->nextReturn == obj)
3393 yaffsfs_DirAdvance(dsc);
3399 static yaffs_DIR *yaffsfs_opendir_reldir_no_lock(struct yaffs_obj *reldir,
3400 const YCHAR *dirname)
3402 yaffs_DIR *dir = NULL;
3403 struct yaffs_obj *obj = NULL;
3404 struct yaffsfs_DirSearchContext *dsc = NULL;
3408 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3409 yaffsfs_SetError(-EFAULT);
3413 if (yaffsfs_CheckPath(dirname) < 0) {
3414 yaffsfs_SetError(-ENAMETOOLONG);
3418 obj = yaffsfs_FindObject(reldir, dirname, 0, 1, NULL, ¬Dir, &loop);
3421 yaffsfs_SetError(-ENOTDIR);
3423 yaffsfs_SetError(-ELOOP);
3425 yaffsfs_SetError(-ENOENT);
3426 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3427 yaffsfs_SetError(-ENOTDIR);
3431 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3432 if (!yaffsfs_dsc[i].inUse)
3433 dsc = &yaffsfs_dsc[i];
3436 dir = (yaffs_DIR *) dsc;
3439 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3442 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3443 INIT_LIST_HEAD(&dsc->others);
3445 if (!search_contexts.next)
3446 INIT_LIST_HEAD(&search_contexts);
3448 list_add(&dsc->others, &search_contexts);
3449 yaffsfs_SetDirRewound(dsc);
3455 yaffs_DIR *yaffs_opendir_reldir(struct yaffs_obj *reldir, const YCHAR *dirname)
3460 ret = yaffsfs_opendir_reldir_no_lock(reldir, dirname);
3464 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3466 return yaffs_opendir_reldir(NULL, dirname);
3469 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3471 struct yaffsfs_DirSearchContext *dsc;
3472 struct yaffs_dirent *retVal = NULL;
3474 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3477 if (dsc && dsc->inUse) {
3478 yaffsfs_SetError(0);
3479 if (dsc->nextReturn) {
3481 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3482 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3483 dsc->de.d_off = dsc->offset++;
3484 yaffs_get_obj_name(dsc->nextReturn,
3485 dsc->de.d_name, NAME_MAX);
3486 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3487 /* this should not happen! */
3488 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3490 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3492 yaffsfs_DirAdvance(dsc);
3496 yaffsfs_SetError(-EBADF);
3501 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3503 struct yaffs_dirent *ret;
3506 ret = yaffsfs_readdir_no_lock(dirp);
3511 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3513 struct yaffsfs_DirSearchContext *dsc;
3515 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3517 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3520 yaffsfs_SetDirRewound(dsc);
3524 void yaffs_rewinddir(yaffs_DIR *dirp)
3527 yaffsfs_rewinddir_no_lock(dirp);
3531 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3533 struct yaffs_dirent *ret = NULL;
3534 struct yaffsfs_FileDes *f;
3537 f = yaffsfs_HandleToFileDes(fd);
3539 ret = yaffsfs_readdir_no_lock(f->v.dir);
3544 void yaffs_rewinddir_fd(int fd)
3546 struct yaffsfs_FileDes *f;
3549 f = yaffsfs_HandleToFileDes(fd);
3551 yaffsfs_rewinddir_no_lock(f->v.dir);
3556 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3558 struct yaffsfs_DirSearchContext *dsc;
3560 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3562 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3563 yaffsfs_SetError(-EFAULT);
3568 list_del(&dsc->others); /* unhook from list */
3572 int yaffs_closedir(yaffs_DIR *dirp)
3577 ret = yaffsfs_closedir_no_lock(dirp);
3582 /* End of directory stuff */
3584 int yaffs_symlink_reldir(struct yaffs_obj *reldir,
3585 const YCHAR *oldpath, const YCHAR *newpath)
3587 struct yaffs_obj *parent = NULL;
3588 struct yaffs_obj *obj;
3591 int mode = 0; /* ignore for now */
3595 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3596 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3597 yaffsfs_SetError(-EFAULT);
3601 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3602 yaffsfs_SetError(-ENAMETOOLONG);
3607 parent = yaffsfs_FindDirectory(reldir, newpath, &name, 0, ¬Dir, &loop);
3608 if (!parent && notDir)
3609 yaffsfs_SetError(-ENOTDIR);
3611 yaffsfs_SetError(-ELOOP);
3612 else if (!parent || yaffs_strnlen(name, 5) < 1)
3613 yaffsfs_SetError(-ENOENT);
3614 else if (yaffsfs_TooManyObjects(parent->my_dev))
3615 yaffsfs_SetError(-ENFILE);
3616 else if (parent->my_dev->read_only)
3617 yaffsfs_SetError(-EROFS);
3619 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3622 else if (yaffsfs_FindObject(reldir, newpath, 0, 0,
3624 yaffsfs_SetError(-EEXIST);
3626 yaffsfs_SetError(-ENOSPC);
3634 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3636 return yaffs_symlink_reldir(NULL, oldpath, newpath);
3639 int yaffs_readlink_reldir(struct yaffs_obj *reldir,const YCHAR *path,
3640 YCHAR *buf, int bufsiz)
3642 struct yaffs_obj *obj = NULL;
3643 struct yaffs_obj *dir = NULL;
3648 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3649 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3650 yaffsfs_SetError(-EFAULT);
3656 obj = yaffsfs_FindObject(reldir, path, 0, 1, &dir, ¬Dir, &loop);
3659 yaffsfs_SetError(-ENOTDIR);
3661 yaffsfs_SetError(-ELOOP);
3662 else if (!dir || !obj)
3663 yaffsfs_SetError(-ENOENT);
3664 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3665 yaffsfs_SetError(-EINVAL);
3667 YCHAR *alias = obj->variant.symlink_variant.alias;
3668 memset(buf, 0, bufsiz);
3669 yaffs_strncpy(buf, alias, bufsiz - 1);
3675 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3677 return yaffs_readlink_reldir(NULL, path, buf, bufsiz);
3680 int yaffs_link_reldir(struct yaffs_obj *reldir,
3681 const YCHAR *oldpath, const YCHAR *linkpath)
3683 /* Creates a link called newpath to existing oldpath */
3684 struct yaffs_obj *obj = NULL;
3685 struct yaffs_obj *lnk = NULL;
3686 struct yaffs_obj *obj_dir = NULL;
3687 struct yaffs_obj *lnk_dir = NULL;
3695 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3696 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3697 yaffsfs_SetError(-EFAULT);
3701 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3702 yaffsfs_SetError(-ENAMETOOLONG);
3708 obj = yaffsfs_FindObject(reldir, oldpath, 0, 1,
3709 &obj_dir, ¬DirObj, &objLoop);
3710 lnk = yaffsfs_FindObject(reldir, linkpath, 0, 0, NULL, NULL, NULL);
3711 lnk_dir = yaffsfs_FindDirectory(reldir, linkpath, &newname,
3712 0, ¬DirLnk, &lnkLoop);
3714 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3715 yaffsfs_SetError(-ENOTDIR);
3716 else if (objLoop || lnkLoop)
3717 yaffsfs_SetError(-ELOOP);
3718 else if (!obj_dir || !lnk_dir || !obj)
3719 yaffsfs_SetError(-ENOENT);
3720 else if (obj->my_dev->read_only)
3721 yaffsfs_SetError(-EROFS);
3722 else if (yaffsfs_TooManyObjects(obj->my_dev))
3723 yaffsfs_SetError(-ENFILE);
3725 yaffsfs_SetError(-EEXIST);
3726 else if (lnk_dir->my_dev != obj->my_dev)
3727 yaffsfs_SetError(-EXDEV);
3729 retVal = yaffsfs_CheckNameLength(newname);
3732 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3736 yaffsfs_SetError(-ENOSPC);
3745 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3747 return yaffs_link_reldir(NULL, oldpath, linkpath);
3750 int yaffs_mknod_reldir(struct yaffs_obj *reldir, const YCHAR *pathname,
3751 mode_t mode, dev_t dev_val)
3758 yaffsfs_SetError(-EINVAL);
3762 int yaffs_mknod_reldev(struct yaffs_dev *dev, const YCHAR *pathname, mode_t mode, dev_t dev_val)
3764 return yaffs_mknod_reldir(ROOT_DIR(dev), pathname, mode, dev_val);
3767 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev_val)
3769 return yaffs_mknod_reldir(NULL, pathname, mode, dev_val);
3773 * D E B U G F U N C T I O N S
3778 * Returns number of handles attached to the object
3780 int yaffs_n_handles_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3782 struct yaffs_obj *obj;
3784 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3785 yaffsfs_SetError(-EFAULT);
3789 if (yaffsfs_CheckPath(path) < 0) {
3790 yaffsfs_SetError(-ENAMETOOLONG);
3794 obj = yaffsfs_FindObject(reldir, path, 0, 1, NULL, NULL, NULL);
3797 return yaffsfs_CountHandles(obj);
3802 int yaffs_n_handles(const YCHAR *path)
3804 return yaffs_n_handles_reldir(NULL, path);
3807 int yaffs_get_error(void)
3809 return yaffsfs_GetLastError();
3812 int yaffs_set_error(int error)
3814 yaffsfs_SetError(error);
3818 int yaffs_dump_dev_reldir(struct yaffs_obj *reldir, const YCHAR *path)
3828 reldir = yaffsfs_FindRoot(path, &rest);
3831 struct yaffs_dev *dev = reldir->my_dev;
3834 "n_page_writes.......... %d\n"
3835 "n_page_reads........... %d\n"
3836 "n_erasures....... %d\n"
3837 "n_gc_copies............ %d\n"
3838 "garbageCollections... %d\n"
3839 "passiveGarbageColl'ns %d\n"
3845 dev->garbageCollections, dev->passiveGarbageCollections);
3852 int yaffs_dump_dev(const YCHAR *path)
3854 return yaffs_dump_dev_reldir(NULL, path);