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 /* YAFFSFS_RW_SIZE must be a power of 2 */
29 #define YAFFSFS_RW_SHIFT (13)
30 #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
32 /* Some forward references */
33 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
35 int symDepth, int getEquiv,
36 struct yaffs_obj **dirOut,
37 int *notDir, int *loop);
39 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
40 static yaffs_DIR *yaffsfs_opendir_no_lock(const YCHAR *dirname);
41 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirent);
43 unsigned int yaffs_wr_attempts;
47 * There are open inodes in struct yaffsfs_Inode.
48 * There are open file descriptors in yaffsfs_FileDes.
49 * There are open handles in yaffsfs_FileDes.
51 * Things are structured this way to be like the Linux VFS model
52 * so that interactions with the yaffs guts calls are similar.
53 * That means more common code paths and less special code.
54 * That means better testing etc.
56 * We have 3 layers because:
57 * A handle is different than an fd because you can use dup()
58 * to create a new handle that accesses the *same* fd. The two
59 * handles will use the same offset (part of the fd). We only close
60 * down the fd when there are no more handles accessing it.
62 * More than one fd can currently access one file, but each fd
63 * has its own permsiions and offset.
66 struct yaffsfs_Inode {
67 int count; /* Number of handles accessing this inode */
68 struct yaffs_obj *iObj;
72 struct yaffsfs_DirSearchContext {
73 struct yaffs_dirent de; /* directory entry */
74 YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
75 struct yaffs_obj *dirObj; /* ptr to directory being searched */
76 struct yaffs_obj *nextReturn; /* obj returned by next readddir */
77 struct list_head others;
82 struct yaffsfs_FileDes {
83 u8 isDir:1; /* This s a directory */
89 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
90 int handleCount:10; /* Number of handles for this fd */
92 Y_LOFF_T position; /* current position in file */
97 struct yaffsfs_Handle {
103 static struct yaffsfs_DirSearchContext yaffsfs_dsc[YAFFSFS_N_DSC];
104 static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
105 static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
106 static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
108 static int yaffsfs_handlesInitialised;
110 unsigned yaffs_set_trace(unsigned tm)
112 yaffs_trace_mask = tm;
113 return yaffs_trace_mask;
116 unsigned yaffs_get_trace(void)
118 return yaffs_trace_mask;
123 * Inilitalise handle management on start-up.
126 static void yaffsfs_InitHandles(void)
129 if (yaffsfs_handlesInitialised)
132 yaffsfs_handlesInitialised = 1;
134 memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
135 memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
136 memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
137 memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
139 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
140 yaffsfs_fd[i].inodeId = -1;
141 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
142 yaffsfs_handle[i].fdId = -1;
145 static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
147 if (h >= 0 && h < YAFFSFS_N_HANDLES)
148 return &yaffsfs_handle[h];
152 static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
154 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
156 if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
157 return &yaffsfs_fd[h->fdId];
162 static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
164 struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
166 if (fd && fd->handleCount > 0 &&
167 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
168 return &yaffsfs_inode[fd->inodeId];
173 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
175 struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
184 * yaffsfs_FindInodeIdForObject
185 * Find the inode entry for an object, if it exists.
188 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
194 obj = yaffs_get_equivalent_obj(obj);
196 /* Look for it in open inode table */
197 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
198 if (yaffsfs_inode[i].iObj == obj)
205 * yaffsfs_GetInodeIdForObject
206 * Grab an inode entry when opening a new inode.
208 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
212 struct yaffsfs_Inode *in = NULL;
215 obj = yaffs_get_equivalent_obj(obj);
217 ret = yaffsfs_FindInodeIdForObject(obj);
219 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
220 if (!yaffsfs_inode[i].iObj)
225 in = &yaffsfs_inode[ret];
235 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
237 int i = yaffsfs_FindInodeIdForObject(obj);
240 return yaffsfs_inode[i].count;
245 static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
247 struct yaffs_obj *obj;
250 obj->my_inode = NULL;
257 static void yaffsfs_PutInode(int inodeId)
259 if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
260 struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
262 if (in->count <= 0) {
263 yaffsfs_ReleaseInode(in);
269 static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
272 struct yaffsfs_Handle *h;
274 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
275 h = &yaffsfs_handle[i];
276 if (h->useCount < 1) {
277 memset(h, 0, sizeof(struct yaffsfs_Handle));
288 static int yaffsfs_NewHandleAndFileDes(void)
291 struct yaffsfs_FileDes *fd;
292 struct yaffsfs_Handle *h = NULL;
293 int handle = yaffsfs_NewHandle(&h);
298 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
300 if (fd->handleCount < 1) {
301 memset(fd, 0, sizeof(struct yaffsfs_FileDes));
309 /* Dump the handle because we could not get a fd */
316 * Increase use of handle when reading/writing a file
317 * Also gets the file descriptor.
320 static int yaffsfs_GetHandle(int handle)
322 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
324 if (h && h->useCount > 0) {
333 * Let go of a handle when closing a file or aborting an open or
334 * ending a read or write.
337 static int yaffsfs_PutFileDes(int fdId)
339 struct yaffsfs_FileDes *fd;
341 if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
342 fd = &yaffsfs_fd[fdId];
344 if (fd->handleCount < 1) {
346 yaffsfs_closedir_no_lock(fd->v.dir);
347 if (fd->inodeId >= 0) {
348 yaffsfs_PutInode(fd->inodeId);
356 static int yaffsfs_PutHandle(int handle)
358 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
360 if (h && h->useCount > 0) {
362 if (h->useCount < 1) {
363 yaffsfs_PutFileDes(h->fdId);
371 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
373 struct yaffsfs_FileDes *fd;
374 struct yaffsfs_Handle *h;
375 struct yaffs_obj *obj;
377 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
378 h = yaffsfs_HandleToPointer(i);
379 fd = yaffsfs_HandleToFileDes(i);
380 obj = yaffsfs_HandleToObject(i);
381 if (h && h->useCount > 0) {
385 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
387 yaffsfs_PutInode(fd->inodeId);
394 * Stuff to handle names.
396 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
398 static int yaffs_toupper(YCHAR a)
400 if (a >= 'a' && a <= 'z')
401 return (a - 'a') + 'A';
406 static int yaffsfs_Match(YCHAR a, YCHAR b)
408 return (yaffs_toupper(a) == yaffs_toupper(b));
411 static int yaffsfs_Match(YCHAR a, YCHAR b)
418 static int yaffsfs_IsPathDivider(YCHAR ch)
420 const YCHAR *str = YAFFS_PATH_DIVIDERS;
431 static int yaffsfs_CheckNameLength(const char *name)
435 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
437 if (nameLength == 0) {
438 yaffsfs_SetError(-ENOENT);
440 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
441 yaffsfs_SetError(-ENAMETOOLONG);
448 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
450 YCHAR *alt_path = NULL;
455 * We don't have a definition for max path length.
456 * We will use 3 * max name length instead.
459 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
461 /* If the last character is a path divider, then we need to
462 * trim it back so that the name look-up works properly.
463 * eg. /foo/new_dir/ -> /foo/newdir
464 * Curveball: Need to handle multiple path dividers:
465 * eg. /foof/sdfse///// -> /foo/sdfse
467 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
468 alt_path = kmalloc(path_length + 1, 0);
471 yaffs_strcpy(alt_path, path);
472 for (i = path_length - 1;
473 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
474 alt_path[i] = (YCHAR) 0;
476 *ret_path = alt_path;
480 LIST_HEAD(yaffsfs_deviceList);
485 * Scan the configuration list to find the device
486 * Curveballs: Should match paths that end in '/' too
487 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
489 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
492 struct list_head *cfg;
493 const YCHAR *leftOver;
495 struct yaffs_dev *retval = NULL;
496 struct yaffs_dev *dev = NULL;
498 int longestMatch = -1;
502 * Check all configs, choose the one that:
503 * 1) Actually matches a prefix (ie /a amd /abc will not match
504 * 2) Matches the longest.
506 list_for_each(cfg, &yaffsfs_deviceList) {
507 dev = list_entry(cfg, struct yaffs_dev, dev_list);
516 while (matching && *p && *leftOver) {
517 /* Skip over any /s */
518 while (yaffsfs_IsPathDivider(*p))
521 /* Skip over any /s */
522 while (yaffsfs_IsPathDivider(*leftOver))
525 /* Now match the text part */
527 *p && !yaffsfs_IsPathDivider(*p) &&
528 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
529 if (yaffsfs_Match(*p, *leftOver)) {
539 /* Skip over any /s in leftOver */
540 while (yaffsfs_IsPathDivider(*leftOver))
543 /*Skip over any /s in p */
544 while (yaffsfs_IsPathDivider(*p))
547 /* p should now be at the end of the string if fully matched */
551 if (matching && (thisMatchLength > longestMatch)) {
553 *restOfPath = (YCHAR *) leftOver;
555 longestMatch = thisMatchLength;
562 static int yaffsfs_CheckPath(const YCHAR *path)
567 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
568 if (yaffsfs_IsPathDivider(*path)) {
576 return (*path) ? -1 : 0;
579 /* FindMountPoint only returns a dev entry if the path is a mount point */
580 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
582 struct yaffs_dev *dev;
583 YCHAR *restOfPath = NULL;
585 dev = yaffsfs_FindDevice(path, &restOfPath);
586 if (dev && restOfPath && *restOfPath)
591 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
594 struct yaffs_dev *dev;
596 dev = yaffsfs_FindDevice(path, restOfPath);
597 if (dev && dev->is_mounted)
598 return dev->root_dir;
603 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
604 int symDepth, int *loop)
608 obj = yaffs_get_equivalent_obj(obj);
610 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
611 YCHAR *alias = obj->variant.symlink_variant.alias;
613 if (yaffsfs_IsPathDivider(*alias))
614 /* Starts with a /, need to scan from root up */
615 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
616 1, NULL, NULL, loop);
619 * Relative to here so use the parent of the
622 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
623 1, NULL, NULL, loop);
629 * yaffsfs_FindDirectory
630 * Parse a path to determine the directory and the name within the directory.
632 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
634 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
636 YCHAR **name, int symDepth,
637 int *notDir, int *loop)
639 struct yaffs_obj *dir;
641 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
644 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
652 restOfPath = (YCHAR *) path;
654 dir = yaffsfs_FindRoot(path, &restOfPath);
659 * curve ball: also throw away surplus '/'
660 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
662 while (yaffsfs_IsPathDivider(*restOfPath))
663 restOfPath++; /* get rid of '/' */
668 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
669 if (i < YAFFS_MAX_NAME_LENGTH) {
670 str[i] = *restOfPath;
678 /* got to the end of the string */
681 if (yaffs_strcmp(str, _Y(".")) == 0) {
683 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
686 dir = yaffs_find_by_name(dir, str);
688 dir = yaffsfs_FollowLink(dir, symDepth, loop);
690 if (dir && dir->variant_type !=
691 YAFFS_OBJECT_TYPE_DIRECTORY) {
700 /* directory did not exist. */
704 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
708 int *notDir, int *loop)
710 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
715 * yaffsfs_FindObject turns a path for an existing object into the object
717 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
718 const YCHAR *path, int symDepth,
720 struct yaffs_obj **dirOut,
721 int *notDir, int *loop)
723 struct yaffs_obj *dir;
724 struct yaffs_obj *obj;
728 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
734 obj = yaffs_find_by_name(dir, name);
739 obj = yaffs_get_equivalent_obj(obj);
744 /*************************************************************************
745 * Start of yaffsfs visible functions.
746 *************************************************************************/
748 int yaffs_dup(int handle)
750 int newHandleNumber = -1;
751 struct yaffsfs_FileDes *existingFD = NULL;
752 struct yaffsfs_Handle *existingHandle = NULL;
753 struct yaffsfs_Handle *newHandle = NULL;
756 existingHandle = yaffsfs_HandleToPointer(handle);
757 existingFD = yaffsfs_HandleToFileDes(handle);
759 newHandleNumber = yaffsfs_NewHandle(&newHandle);
761 newHandle->fdId = existingHandle->fdId;
762 existingFD->handleCount++;
768 yaffsfs_SetError(-EBADF);
770 yaffsfs_SetError(-ENOMEM);
772 return newHandleNumber;
776 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
778 int current_objects = dev->n_obj - dev->n_deleted_files;
780 if (dev->param.max_objects && current_objects > dev->param.max_objects)
786 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
788 struct yaffs_obj *obj = NULL;
789 struct yaffs_obj *dir = NULL;
792 struct yaffsfs_FileDes *fd = NULL;
795 int errorReported = 0;
796 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
797 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
798 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
799 u8 sharedReadAllowed;
800 u8 sharedWriteAllowed;
810 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
811 yaffsfs_SetError(-EFAULT);
815 if (yaffsfs_CheckPath(path) < 0) {
816 yaffsfs_SetError(-ENAMETOOLONG);
820 /* O_EXCL only has meaning if O_CREAT is specified */
821 if (!(oflag & O_CREAT))
824 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
825 if ((oflag & O_CREAT) & (oflag & O_EXCL))
828 /* Todo: Are there any more flag combos to sanitise ? */
830 /* Figure out if reading or writing is requested */
832 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
833 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
837 handle = yaffsfs_NewHandleAndFileDes();
840 yaffsfs_SetError(-ENFILE);
844 fd = yaffsfs_HandleToFileDes(handle);
846 /* try to find the exisiting object */
847 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
849 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
852 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
853 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
858 /* The file already exists or it might be a directory */
859 is_dir = (obj->variant_type ==
860 YAFFS_OBJECT_TYPE_DIRECTORY);
862 /* A directory can't be opened except for read */
864 (writeRequested || !readRequested ||
865 (oflag & ~O_RDONLY))) {
867 yaffsfs_SetError(-EISDIR);
872 dsc = yaffsfs_opendir_no_lock(path);
875 yaffsfs_SetError(-ENFILE);
880 /* Open should fail if O_CREAT and O_EXCL are specified
881 * for a file that exists.
883 if (!errorReported &&
884 (oflag & O_EXCL) && (oflag & O_CREAT)) {
886 yaffsfs_SetError(-EEXIST);
890 /* Check file permissions */
891 if (readRequested && !(obj->yst_mode & S_IREAD))
894 if (writeRequested && !(obj->yst_mode & S_IWRITE))
897 if (!errorReported && writeRequested &&
898 obj->my_dev->read_only) {
900 yaffsfs_SetError(-EROFS);
904 if (openDenied && !errorReported) {
905 yaffsfs_SetError(-EACCES);
909 /* Check sharing of an existing object. */
911 struct yaffsfs_FileDes *fdx;
914 sharedReadAllowed = 1;
915 sharedWriteAllowed = 1;
918 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
919 fdx = &yaffsfs_fd[i];
920 if (fdx->handleCount > 0 &&
922 yaffsfs_inode[fdx->inodeId].iObj
925 sharedReadAllowed = 0;
926 if (!fdx->shareWrite)
927 sharedWriteAllowed = 0;
935 if ((!sharedReadAllowed && readRequested) ||
936 (!shareRead && alreadyReading) ||
937 (!sharedWriteAllowed && writeRequested) ||
938 (!shareWrite && alreadyWriting)) {
940 yaffsfs_SetError(-EBUSY);
947 /* If we could not open an existing object, then let's see if
948 * the directory exists. If not, error.
950 if (!obj && !errorReported) {
951 dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
953 if (!dir && notDir) {
954 yaffsfs_SetError(-ENOTDIR);
957 yaffsfs_SetError(-ELOOP);
960 yaffsfs_SetError(-ENOENT);
965 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
966 /* Let's see if we can create this file */
967 if (dir->my_dev->read_only) {
968 yaffsfs_SetError(-EROFS);
970 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
971 yaffsfs_SetError(-ENFILE);
974 obj = yaffs_create_file(dir, name, mode, 0, 0);
976 if (!obj && !errorReported) {
977 yaffsfs_SetError(-ENOSPC);
982 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
983 yaffsfs_SetError(-ENOENT);
987 if (obj && !openDenied) {
988 int inodeId = yaffsfs_GetInodeIdForObject(obj);
992 * Todo: Fix any problem if inodes run out,
993 * That can't happen if the number of inode
994 * items >= number of handles.
998 fd->inodeId = inodeId;
999 fd->reading = readRequested;
1000 fd->writing = writeRequested;
1001 fd->append = (oflag & O_APPEND) ? 1 : 0;
1002 fd->shareRead = shareRead;
1003 fd->shareWrite = shareWrite;
1011 /* Hook inode to object */
1012 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
1014 if (!is_dir && (oflag & O_TRUNC) && fd->writing)
1015 yaffs_resize_file(obj, 0);
1017 yaffsfs_PutHandle(handle);
1019 yaffsfs_SetError(0); /* Problem */
1029 int yaffs_open(const YCHAR *path, int oflag, int mode)
1031 return yaffs_open_sharing(path, oflag, mode,
1032 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1035 static int yaffs_Dofsync(int handle, int datasync)
1038 struct yaffs_obj *obj;
1042 obj = yaffsfs_HandleToObject(handle);
1045 yaffsfs_SetError(-EBADF);
1046 else if (obj->my_dev->read_only)
1047 yaffsfs_SetError(-EROFS);
1049 yaffs_flush_file(obj, 1, datasync);
1058 int yaffs_fsync(int handle)
1060 return yaffs_Dofsync(handle, 0);
1063 int yaffs_flush(int handle)
1065 return yaffs_fsync(handle);
1068 int yaffs_fdatasync(int handle)
1070 return yaffs_Dofsync(handle, 1);
1073 int yaffs_close(int handle)
1075 struct yaffsfs_Handle *h = NULL;
1076 struct yaffsfs_FileDes *f;
1077 struct yaffs_obj *obj = NULL;
1082 h = yaffsfs_HandleToPointer(handle);
1083 f = yaffsfs_HandleToFileDes(handle);
1084 obj = yaffsfs_HandleToObject(handle);
1086 if (!h || !obj || !f)
1087 yaffsfs_SetError(-EBADF);
1091 yaffs_flush_file(obj, 1, 0);
1092 yaffsfs_PutHandle(handle);
1101 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1102 int isPread, Y_LOFF_T offset)
1104 struct yaffsfs_FileDes *fd = NULL;
1105 struct yaffs_obj *obj = NULL;
1107 Y_LOFF_T startPos = 0;
1108 Y_LOFF_T endPos = 0;
1113 u8 *buf = (u8 *) vbuf;
1115 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1116 yaffsfs_SetError(-EFAULT);
1121 fd = yaffsfs_HandleToFileDes(handle);
1122 obj = yaffsfs_HandleToObject(handle);
1126 yaffsfs_SetError(-EBADF);
1128 } else if (!fd->reading) {
1129 /* Not a reading handle */
1130 yaffsfs_SetError(-EINVAL);
1132 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1133 yaffsfs_SetError(-EINVAL);
1139 startPos = fd->v.position;
1143 if (yaffs_get_obj_length(obj) > pos)
1144 maxRead = yaffs_get_obj_length(obj) - pos;
1148 if (nbyte > maxRead)
1151 yaffsfs_GetHandle(handle);
1153 endPos = pos + nbyte;
1155 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1156 nbyte > YAFFS_MAX_FILE_SIZE ||
1157 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1163 nToRead = YAFFSFS_RW_SIZE -
1164 (pos & (YAFFSFS_RW_SIZE - 1));
1165 if (nToRead > nbyte)
1169 * Need to reverify object in case the device was
1170 * unmounted in another thread.
1172 obj = yaffsfs_HandleToObject(handle);
1176 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1184 if (nRead == nToRead)
1187 nbyte = 0; /* no more to read */
1196 yaffsfs_PutHandle(handle);
1200 fd->v.position = startPos + totalRead;
1202 yaffsfs_SetError(-EINVAL);
1209 return (totalRead >= 0) ? totalRead : -1;
1213 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1215 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1218 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1220 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1223 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1224 int isPwrite, Y_LOFF_T offset)
1226 struct yaffsfs_FileDes *fd = NULL;
1227 struct yaffs_obj *obj = NULL;
1229 Y_LOFF_T startPos = 0;
1232 int totalWritten = 0;
1233 int write_trhrough = 0;
1235 const u8 *buf = (const u8 *)vbuf;
1237 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1238 yaffsfs_SetError(-EFAULT);
1243 fd = yaffsfs_HandleToFileDes(handle);
1244 obj = yaffsfs_HandleToObject(handle);
1248 yaffsfs_SetError(-EBADF);
1250 } else if (!fd->writing) {
1251 yaffsfs_SetError(-EINVAL);
1253 } else if (obj->my_dev->read_only) {
1254 yaffsfs_SetError(-EROFS);
1258 startPos = yaffs_get_obj_length(obj);
1262 startPos = fd->v.position;
1264 yaffsfs_GetHandle(handle);
1266 endPos = pos + nbyte;
1268 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1269 nbyte > YAFFS_MAX_FILE_SIZE ||
1270 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1277 nToWrite = YAFFSFS_RW_SIZE -
1278 (pos & (YAFFSFS_RW_SIZE - 1));
1279 if (nToWrite > nbyte)
1283 * Need to reverify object in case the device was
1284 * remounted or unmounted in another thread.
1286 obj = yaffsfs_HandleToObject(handle);
1287 if (!obj || obj->my_dev->read_only)
1291 yaffs_wr_file(obj, buf, pos, nToWrite,
1294 totalWritten += nWritten;
1299 if (nWritten == nToWrite)
1304 if (nWritten < 1 && totalWritten < 1) {
1305 yaffsfs_SetError(-ENOSPC);
1315 yaffsfs_PutHandle(handle);
1318 if (totalWritten > 0)
1319 fd->v.position = startPos + totalWritten;
1321 yaffsfs_SetError(-EINVAL);
1327 return (totalWritten >= 0) ? totalWritten : -1;
1330 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1332 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1335 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1337 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1340 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1342 struct yaffs_obj *obj = NULL;
1343 struct yaffs_obj *dir = NULL;
1344 int result = YAFFS_FAIL;
1348 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1349 yaffsfs_SetError(-EFAULT);
1353 if (yaffsfs_CheckPath(path) < 0) {
1354 yaffsfs_SetError(-ENAMETOOLONG);
1360 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1361 obj = yaffsfs_FollowLink(obj, 0, &loop);
1364 yaffsfs_SetError(-ENOTDIR);
1366 yaffsfs_SetError(-ELOOP);
1367 else if (!dir || !obj)
1368 yaffsfs_SetError(-ENOENT);
1369 else if (obj->my_dev->read_only)
1370 yaffsfs_SetError(-EROFS);
1371 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1372 yaffsfs_SetError(-EISDIR);
1373 else if (obj->my_dev->read_only)
1374 yaffsfs_SetError(-EROFS);
1375 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1376 yaffsfs_SetError(-EINVAL);
1378 result = yaffs_resize_file(obj, new_size);
1382 return (result) ? 0 : -1;
1385 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1387 struct yaffsfs_FileDes *fd = NULL;
1388 struct yaffs_obj *obj = NULL;
1392 fd = yaffsfs_HandleToFileDes(handle);
1393 obj = yaffsfs_HandleToObject(handle);
1397 yaffsfs_SetError(-EBADF);
1398 else if (!fd->writing)
1399 yaffsfs_SetError(-EINVAL);
1400 else if (obj->my_dev->read_only)
1401 yaffsfs_SetError(-EROFS);
1402 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1403 yaffsfs_SetError(-EINVAL);
1405 /* resize the file */
1406 result = yaffs_resize_file(obj, new_size);
1409 return (result) ? 0 : -1;
1413 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1415 struct yaffsfs_FileDes *fd = NULL;
1416 struct yaffs_obj *obj = NULL;
1418 Y_LOFF_T fSize = -1;
1421 fd = yaffsfs_HandleToFileDes(handle);
1422 obj = yaffsfs_HandleToObject(handle);
1425 yaffsfs_SetError(-EBADF);
1426 else if (offset > YAFFS_MAX_FILE_SIZE)
1427 yaffsfs_SetError(-EINVAL);
1429 if (whence == SEEK_SET) {
1432 } else if (whence == SEEK_CUR) {
1433 if ((fd->v.position + offset) >= 0)
1434 pos = (fd->v.position + offset);
1435 } else if (whence == SEEK_END) {
1436 fSize = yaffs_get_obj_length(obj);
1437 if (fSize >= 0 && (fSize + offset) >= 0)
1438 pos = fSize + offset;
1441 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1442 fd->v.position = pos;
1444 yaffsfs_SetError(-EINVAL);
1454 static int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
1456 struct yaffs_obj *dir = NULL;
1457 struct yaffs_obj *obj = NULL;
1459 int result = YAFFS_FAIL;
1463 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1464 yaffsfs_SetError(-EFAULT);
1468 if (yaffsfs_CheckPath(path) < 0) {
1469 yaffsfs_SetError(-ENAMETOOLONG);
1475 obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
1476 dir = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
1479 yaffsfs_SetError(-ENOTDIR);
1481 yaffsfs_SetError(-ELOOP);
1483 yaffsfs_SetError(-ENOENT);
1484 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1485 yaffsfs_SetError(-EINVAL);
1487 yaffsfs_SetError(-ENOENT);
1488 else if (obj->my_dev->read_only)
1489 yaffsfs_SetError(-EROFS);
1490 else if (!isDirectory &&
1491 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1492 yaffsfs_SetError(-EISDIR);
1493 else if (isDirectory &&
1494 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1495 yaffsfs_SetError(-ENOTDIR);
1496 else if (isDirectory && obj == obj->my_dev->root_dir)
1497 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1499 result = yaffs_unlinker(dir, name);
1501 if (result == YAFFS_FAIL && isDirectory)
1502 yaffsfs_SetError(-ENOTEMPTY);
1507 return (result == YAFFS_FAIL) ? -1 : 0;
1510 int yaffs_unlink(const YCHAR *path)
1512 return yaffsfs_DoUnlink(path, 0);
1515 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1517 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1518 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1524 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1526 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1527 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1533 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1535 struct yaffs_obj *olddir = NULL;
1536 struct yaffs_obj *newdir = NULL;
1537 struct yaffs_obj *obj = NULL;
1538 struct yaffs_obj *newobj = NULL;
1541 int result = YAFFS_FAIL;
1542 int rename_allowed = 1;
1548 YCHAR *alt_newpath = NULL;
1550 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1551 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1552 yaffsfs_SetError(-EFAULT);
1556 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1557 yaffsfs_SetError(-ENAMETOOLONG);
1561 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1562 yaffsfs_SetError(-ENOMEM);
1566 newPath = alt_newpath;
1570 olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
1571 ¬OldDir, &oldLoop);
1572 newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
1573 ¬NewDir, &newLoop);
1574 obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
1575 newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
1577 /* If the object being renamed is a directory and the
1578 * path ended with a "/" then the olddir == obj.
1579 * We pass through NULL for the old name to tell the lower layers
1580 * to use olddir as the object.
1586 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1587 yaffsfs_SetError(-ENOTDIR);
1589 } else if (oldLoop || newLoop) {
1590 yaffsfs_SetError(-ELOOP);
1592 } else if (olddir && oldname &&
1593 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1594 yaffsfs_SetError(-EINVAL);
1596 } else if (!olddir || !newdir || !obj) {
1597 yaffsfs_SetError(-ENOENT);
1599 } else if (obj->my_dev->read_only) {
1600 yaffsfs_SetError(-EROFS);
1602 } else if (rename_file_over_dir(obj, newobj)) {
1603 yaffsfs_SetError(-EISDIR);
1605 } else if (rename_dir_over_file(obj, newobj)) {
1606 yaffsfs_SetError(-ENOTDIR);
1608 } else if (yaffs_is_non_empty_dir(newobj)) {
1609 yaffsfs_SetError(-ENOTEMPTY);
1611 } else if (olddir->my_dev != newdir->my_dev) {
1612 /* Rename must be on same device */
1613 yaffsfs_SetError(-EXDEV);
1615 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1617 * It is a directory, check that it is not being renamed to
1618 * being its own decendent.
1619 * Do this by tracing from the new directory back to the root,
1623 struct yaffs_obj *xx = newdir;
1625 while (rename_allowed && xx) {
1630 if (!rename_allowed)
1631 yaffsfs_SetError(-EINVAL);
1635 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1641 return (result == YAFFS_FAIL) ? -1 : 0;
1644 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1648 obj = yaffs_get_equivalent_obj(obj);
1651 buf->st_dev = (int)obj->my_dev->os_context;
1652 buf->st_ino = obj->obj_id;
1653 buf->st_mode = obj->yst_mode & ~S_IFMT;
1655 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1656 buf->st_mode |= S_IFDIR;
1657 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1658 buf->st_mode |= S_IFLNK;
1659 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1660 buf->st_mode |= S_IFREG;
1662 buf->st_nlink = yaffs_get_obj_link_count(obj);
1665 buf->st_rdev = obj->yst_rdev;
1666 buf->st_size = yaffs_get_obj_length(obj);
1667 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1668 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1670 #if CONFIG_YAFFS_WINCE
1671 buf->yst_wince_atime[0] = obj->win_atime[0];
1672 buf->yst_wince_atime[1] = obj->win_atime[1];
1673 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1674 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1675 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1676 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1678 buf->yst_atime = obj->yst_atime;
1679 buf->yst_ctime = obj->yst_ctime;
1680 buf->yst_mtime = obj->yst_mtime;
1687 static int yaffsfs_DoStatOrLStat(const YCHAR *path,
1688 struct yaffs_stat *buf, int doLStat)
1690 struct yaffs_obj *obj = NULL;
1691 struct yaffs_obj *dir = NULL;
1696 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1697 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1698 yaffsfs_SetError(-EFAULT);
1702 if (yaffsfs_CheckPath(path) < 0) {
1703 yaffsfs_SetError(-ENAMETOOLONG);
1709 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1711 if (!doLStat && obj)
1712 obj = yaffsfs_FollowLink(obj, 0, &loop);
1715 yaffsfs_SetError(-ENOTDIR);
1717 yaffsfs_SetError(-ELOOP);
1718 else if (!dir || !obj)
1719 yaffsfs_SetError(-ENOENT);
1721 retVal = yaffsfs_DoStat(obj, buf);
1729 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1731 return yaffsfs_DoStatOrLStat(path, buf, 0);
1734 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1736 return yaffsfs_DoStatOrLStat(path, buf, 1);
1739 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1741 struct yaffs_obj *obj;
1745 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1746 yaffsfs_SetError(-EFAULT);
1751 obj = yaffsfs_HandleToObject(fd);
1754 retVal = yaffsfs_DoStat(obj, buf);
1757 yaffsfs_SetError(-EBADF);
1764 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1765 const struct yaffs_utimbuf *buf)
1770 struct yaffs_utimbuf local;
1772 obj = yaffs_get_equivalent_obj(obj);
1774 if (obj && obj->my_dev->read_only) {
1775 yaffsfs_SetError(-EROFS);
1780 local.actime = Y_CURRENT_TIME;
1781 local.modtime = local.actime;
1786 obj->yst_atime = buf->actime;
1787 obj->yst_mtime = buf->modtime;
1789 result = yaffs_flush_file(obj, 0, 0);
1790 retVal = result == YAFFS_OK ? 0 : -1;
1796 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1798 struct yaffs_obj *obj = NULL;
1799 struct yaffs_obj *dir = NULL;
1805 yaffsfs_SetError(-EFAULT);
1809 if (yaffsfs_CheckPath(path) < 0) {
1810 yaffsfs_SetError(-ENAMETOOLONG);
1816 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1819 yaffsfs_SetError(-ENOTDIR);
1821 yaffsfs_SetError(-ELOOP);
1822 else if (!dir || !obj)
1823 yaffsfs_SetError(-ENOENT);
1825 retVal = yaffsfs_DoUtime(obj, buf);
1833 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1835 struct yaffs_obj *obj;
1840 obj = yaffsfs_HandleToObject(fd);
1843 retVal = yaffsfs_DoUtime(obj, buf);
1846 yaffsfs_SetError(-EBADF);
1853 #ifndef CONFIG_YAFFS_WINCE
1854 /* xattrib functions */
1856 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1857 const void *data, int size, int flags, int follow)
1859 struct yaffs_obj *obj;
1860 struct yaffs_obj *dir;
1866 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1867 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1868 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1869 yaffsfs_SetError(-EFAULT);
1873 if (yaffsfs_CheckPath(path) < 0) {
1874 yaffsfs_SetError(-ENAMETOOLONG);
1880 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1883 obj = yaffsfs_FollowLink(obj, 0, &loop);
1886 yaffsfs_SetError(-ENOTDIR);
1888 yaffsfs_SetError(-ELOOP);
1889 else if (!dir || !obj)
1890 yaffsfs_SetError(-ENOENT);
1892 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1894 yaffsfs_SetError(retVal);
1905 int yaffs_setxattr(const YCHAR *path, const char *name,
1906 const void *data, int size, int flags)
1908 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1911 int yaffs_lsetxattr(const YCHAR *path, const char *name,
1912 const void *data, int size, int flags)
1914 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1917 int yaffs_fsetxattr(int fd, const char *name,
1918 const void *data, int size, int flags)
1920 struct yaffs_obj *obj;
1924 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1925 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1926 yaffsfs_SetError(-EFAULT);
1931 obj = yaffsfs_HandleToObject(fd);
1934 yaffsfs_SetError(-EBADF);
1936 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1938 yaffsfs_SetError(retVal);
1948 static int yaffs_do_getxattr(const YCHAR *path, const char *name,
1949 void *data, int size, int follow)
1951 struct yaffs_obj *obj;
1952 struct yaffs_obj *dir;
1957 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1958 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1959 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1960 yaffsfs_SetError(-EFAULT);
1964 if (yaffsfs_CheckPath(path) < 0) {
1965 yaffsfs_SetError(-ENAMETOOLONG);
1971 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1974 obj = yaffsfs_FollowLink(obj, 0, &loop);
1977 yaffsfs_SetError(-ENOTDIR);
1979 yaffsfs_SetError(-ELOOP);
1980 else if (!dir || !obj)
1981 yaffsfs_SetError(-ENOENT);
1983 retVal = yaffs_get_xattrib(obj, name, data, size);
1985 yaffsfs_SetError(retVal);
1995 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1997 return yaffs_do_getxattr(path, name, data, size, 1);
2000 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
2002 return yaffs_do_getxattr(path, name, data, size, 0);
2005 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
2007 struct yaffs_obj *obj;
2011 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
2012 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2013 yaffsfs_SetError(-EFAULT);
2018 obj = yaffsfs_HandleToObject(fd);
2021 retVal = yaffs_get_xattrib(obj, name, data, size);
2023 yaffsfs_SetError(retVal);
2028 yaffsfs_SetError(-EBADF);
2035 static int yaffs_do_listxattr(const YCHAR *path, char *data,
2036 int size, int follow)
2038 struct yaffs_obj *obj = NULL;
2039 struct yaffs_obj *dir = NULL;
2044 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2045 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2046 yaffsfs_SetError(-EFAULT);
2050 if (yaffsfs_CheckPath(path) < 0) {
2051 yaffsfs_SetError(-ENAMETOOLONG);
2057 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2060 obj = yaffsfs_FollowLink(obj, 0, &loop);
2063 yaffsfs_SetError(-ENOTDIR);
2065 yaffsfs_SetError(-ELOOP);
2066 else if (!dir || !obj)
2067 yaffsfs_SetError(-ENOENT);
2069 retVal = yaffs_list_xattrib(obj, data, size);
2071 yaffsfs_SetError(retVal);
2082 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2084 return yaffs_do_listxattr(path, data, size, 1);
2087 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2089 return yaffs_do_listxattr(path, data, size, 0);
2092 int yaffs_flistxattr(int fd, char *data, int size)
2094 struct yaffs_obj *obj;
2098 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2099 yaffsfs_SetError(-EFAULT);
2104 obj = yaffsfs_HandleToObject(fd);
2107 retVal = yaffs_list_xattrib(obj, data, size);
2109 yaffsfs_SetError(retVal);
2114 yaffsfs_SetError(-EBADF);
2121 static int yaffs_do_removexattr(const YCHAR *path, const char *name,
2124 struct yaffs_obj *obj = NULL;
2125 struct yaffs_obj *dir = NULL;
2130 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2131 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2132 yaffsfs_SetError(-EFAULT);
2136 if (yaffsfs_CheckPath(path) < 0) {
2137 yaffsfs_SetError(-ENAMETOOLONG);
2143 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2146 obj = yaffsfs_FollowLink(obj, 0, &loop);
2149 yaffsfs_SetError(-ENOTDIR);
2151 yaffsfs_SetError(-ELOOP);
2152 else if (!dir || !obj)
2153 yaffsfs_SetError(-ENOENT);
2155 retVal = yaffs_remove_xattrib(obj, name);
2157 yaffsfs_SetError(retVal);
2168 int yaffs_removexattr(const YCHAR *path, const char *name)
2170 return yaffs_do_removexattr(path, name, 1);
2173 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2175 return yaffs_do_removexattr(path, name, 0);
2178 int yaffs_fremovexattr(int fd, const char *name)
2180 struct yaffs_obj *obj;
2184 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2185 yaffsfs_SetError(-EFAULT);
2190 obj = yaffsfs_HandleToObject(fd);
2193 retVal = yaffs_remove_xattrib(obj, name);
2195 yaffsfs_SetError(retVal);
2200 yaffsfs_SetError(-EBADF);
2208 #ifdef CONFIG_YAFFS_WINCE
2209 int yaffs_get_wince_times(int fd, unsigned *wctime,
2210 unsigned *watime, unsigned *wmtime)
2212 struct yaffs_obj *obj;
2217 obj = yaffsfs_HandleToObject(fd);
2222 wctime[0] = obj->win_ctime[0];
2223 wctime[1] = obj->win_ctime[1];
2226 watime[0] = obj->win_atime[0];
2227 watime[1] = obj->win_atime[1];
2230 wmtime[0] = obj->win_mtime[0];
2231 wmtime[1] = obj->win_mtime[1];
2237 yaffsfs_SetError(-EBADF);
2244 int yaffs_set_wince_times(int fd,
2245 const unsigned *wctime,
2246 const unsigned *watime, const unsigned *wmtime)
2248 struct yaffs_obj *obj;
2253 obj = yaffsfs_HandleToObject(fd);
2258 obj->win_ctime[0] = wctime[0];
2259 obj->win_ctime[1] = wctime[1];
2262 obj->win_atime[0] = watime[0];
2263 obj->win_atime[1] = watime[1];
2266 obj->win_mtime[0] = wmtime[0];
2267 obj->win_mtime[1] = wmtime[1];
2271 result = yaffs_flush_file(obj, 0, 0);
2275 yaffsfs_SetError(-EBADF);
2284 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2289 obj = yaffs_get_equivalent_obj(obj);
2292 obj->yst_mode = mode;
2294 result = yaffs_flush_file(obj, 0, 0);
2297 return result == YAFFS_OK ? 0 : -1;
2300 int yaffs_access(const YCHAR *path, int amode)
2302 struct yaffs_obj *obj = NULL;
2303 struct yaffs_obj *dir = NULL;
2308 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2309 yaffsfs_SetError(-EFAULT);
2313 if (yaffsfs_CheckPath(path) < 0) {
2314 yaffsfs_SetError(-ENAMETOOLONG);
2318 if (amode & ~(R_OK | W_OK | X_OK)) {
2319 yaffsfs_SetError(-EINVAL);
2325 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2326 obj = yaffsfs_FollowLink(obj, 0, &loop);
2329 yaffsfs_SetError(-ENOTDIR);
2331 yaffsfs_SetError(-ELOOP);
2332 else if (!dir || !obj)
2333 yaffsfs_SetError(-ENOENT);
2334 else if ((amode & W_OK) && obj->my_dev->read_only)
2335 yaffsfs_SetError(-EROFS);
2339 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2341 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2343 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2347 yaffsfs_SetError(-EACCES);
2358 int yaffs_chmod(const YCHAR *path, mode_t mode)
2360 struct yaffs_obj *obj = NULL;
2361 struct yaffs_obj *dir = NULL;
2366 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2367 yaffsfs_SetError(-EFAULT);
2371 if (yaffsfs_CheckPath(path) < 0) {
2372 yaffsfs_SetError(-ENAMETOOLONG);
2376 if (mode & ~(0777)) {
2377 yaffsfs_SetError(-EINVAL);
2383 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2384 obj = yaffsfs_FollowLink(obj, 0, &loop);
2387 yaffsfs_SetError(-ENOTDIR);
2389 yaffsfs_SetError(-ELOOP);
2390 else if (!dir || !obj)
2391 yaffsfs_SetError(-ENOENT);
2392 else if (obj->my_dev->read_only)
2393 yaffsfs_SetError(-EROFS);
2395 retVal = yaffsfs_DoChMod(obj, mode);
2403 int yaffs_fchmod(int fd, mode_t mode)
2405 struct yaffs_obj *obj;
2408 if (mode & ~(0777)) {
2409 yaffsfs_SetError(-EINVAL);
2414 obj = yaffsfs_HandleToObject(fd);
2417 yaffsfs_SetError(-EBADF);
2418 else if (obj->my_dev->read_only)
2419 yaffsfs_SetError(-EROFS);
2421 retVal = yaffsfs_DoChMod(obj, mode);
2428 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2430 struct yaffs_obj *parent = NULL;
2431 struct yaffs_obj *dir = NULL;
2433 YCHAR *alt_path = NULL;
2438 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2439 yaffsfs_SetError(-EFAULT);
2443 if (yaffsfs_CheckPath(path) < 0) {
2444 yaffsfs_SetError(-ENAMETOOLONG);
2448 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2449 yaffsfs_SetError(-ENOMEM);
2456 parent = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
2457 if (!parent && notDir)
2458 yaffsfs_SetError(-ENOTDIR);
2460 yaffsfs_SetError(-ELOOP);
2462 yaffsfs_SetError(-ENOENT);
2463 else if (yaffsfs_TooManyObjects(parent->my_dev))
2464 yaffsfs_SetError(-ENFILE);
2465 else if (yaffs_strnlen(name, 5) == 0) {
2466 /* Trying to make the root itself */
2467 yaffsfs_SetError(-EEXIST);
2468 } else if (parent->my_dev->read_only)
2469 yaffsfs_SetError(-EROFS);
2471 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2474 else if (yaffs_find_by_name(parent, name))
2475 yaffsfs_SetError(-EEXIST); /* name exists */
2477 yaffsfs_SetError(-ENOSPC); /* assume no space */
2487 int yaffs_rmdir(const YCHAR *path)
2492 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2493 yaffsfs_SetError(-EFAULT);
2497 if (yaffsfs_CheckPath(path) < 0) {
2498 yaffsfs_SetError(-ENAMETOOLONG);
2502 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2503 yaffsfs_SetError(-ENOMEM);
2508 result = yaffsfs_DoUnlink(path, 1);
2515 void *yaffs_getdev(const YCHAR *path)
2517 struct yaffs_dev *dev = NULL;
2519 dev = yaffsfs_FindDevice(path, &dummy);
2523 int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
2526 int result = YAFFS_FAIL;
2527 struct yaffs_dev *dev = NULL;
2529 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2530 yaffsfs_SetError(-EFAULT);
2534 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2536 if (yaffsfs_CheckPath(path) < 0) {
2537 yaffsfs_SetError(-ENAMETOOLONG);
2543 yaffsfs_InitHandles();
2545 dev = yaffsfs_FindMountPoint(path);
2547 if (!dev->is_mounted) {
2548 dev->read_only = read_only ? 1 : 0;
2550 u8 skip = dev->param.skip_checkpt_rd;
2551 dev->param.skip_checkpt_rd = 1;
2552 result = yaffs_guts_initialise(dev);
2553 dev->param.skip_checkpt_rd = skip;
2555 result = yaffs_guts_initialise(dev);
2558 if (result == YAFFS_FAIL)
2559 yaffsfs_SetError(-ENOMEM);
2560 retVal = result ? 0 : -1;
2563 yaffsfs_SetError(-EBUSY);
2565 yaffsfs_SetError(-ENODEV);
2572 int yaffs_mount2(const YCHAR *path, int readonly)
2574 return yaffs_mount_common(path, readonly, 0);
2577 int yaffs_mount(const YCHAR *path)
2579 return yaffs_mount_common(path, 0, 0);
2582 int yaffs_sync(const YCHAR *path)
2585 struct yaffs_dev *dev = NULL;
2588 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2589 yaffsfs_SetError(-EFAULT);
2593 if (yaffsfs_CheckPath(path) < 0) {
2594 yaffsfs_SetError(-ENAMETOOLONG);
2599 dev = yaffsfs_FindDevice(path, &dummy);
2601 if (!dev->is_mounted)
2602 yaffsfs_SetError(-EINVAL);
2603 else if (dev->read_only)
2604 yaffsfs_SetError(-EROFS);
2607 yaffs_flush_whole_cache(dev);
2608 yaffs_checkpoint_save(dev);
2613 yaffsfs_SetError(-ENODEV);
2619 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2622 struct yaffs_obj *obj;
2624 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2625 obj = yaffsfs_HandleToObject(i);
2626 if (obj && obj->my_dev == dev)
2632 int yaffs_remount(const YCHAR *path, int force, int read_only)
2635 struct yaffs_dev *dev = NULL;
2637 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2638 yaffsfs_SetError(-EFAULT);
2642 if (yaffsfs_CheckPath(path) < 0) {
2643 yaffsfs_SetError(-ENAMETOOLONG);
2648 dev = yaffsfs_FindMountPoint(path);
2650 if (dev->is_mounted) {
2651 yaffs_flush_whole_cache(dev);
2653 if (force || !yaffsfs_IsDevBusy(dev)) {
2655 yaffs_checkpoint_save(dev);
2656 dev->read_only = read_only ? 1 : 0;
2659 yaffsfs_SetError(-EBUSY);
2662 yaffsfs_SetError(-EINVAL);
2665 yaffsfs_SetError(-ENODEV);
2672 int yaffs_unmount2(const YCHAR *path, int force)
2675 struct yaffs_dev *dev = NULL;
2677 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2678 yaffsfs_SetError(-EFAULT);
2682 if (yaffsfs_CheckPath(path) < 0) {
2683 yaffsfs_SetError(-ENAMETOOLONG);
2688 dev = yaffsfs_FindMountPoint(path);
2690 if (dev->is_mounted) {
2692 yaffs_flush_whole_cache(dev);
2693 yaffs_checkpoint_save(dev);
2694 inUse = yaffsfs_IsDevBusy(dev);
2695 if (!inUse || force) {
2697 yaffsfs_BreakDeviceHandles(dev);
2698 yaffs_deinitialise(dev);
2702 yaffsfs_SetError(-EBUSY);
2705 yaffsfs_SetError(-EINVAL);
2708 yaffsfs_SetError(-ENODEV);
2715 int yaffs_unmount(const YCHAR *path)
2717 return yaffs_unmount2(path, 0);
2720 int yaffs_format(const YCHAR *path,
2722 int force_unmount_flag,
2726 struct yaffs_dev *dev = NULL;
2730 yaffsfs_SetError(-EFAULT);
2734 if (yaffsfs_CheckPath(path) < 0) {
2735 yaffsfs_SetError(-ENAMETOOLONG);
2740 dev = yaffsfs_FindMountPoint(path);
2743 int was_mounted = dev->is_mounted;
2745 if (dev->is_mounted && unmount_flag) {
2747 yaffs_flush_whole_cache(dev);
2748 yaffs_checkpoint_save(dev);
2749 inUse = yaffsfs_IsDevBusy(dev);
2750 if (!inUse || force_unmount_flag) {
2752 yaffsfs_BreakDeviceHandles(dev);
2753 yaffs_deinitialise(dev);
2757 if(dev->is_mounted) {
2758 yaffsfs_SetError(-EBUSY);
2761 yaffs_format_dev(dev);
2762 if(was_mounted && remount_flag) {
2763 result = yaffs_guts_initialise(dev);
2764 if (result == YAFFS_FAIL) {
2765 yaffsfs_SetError(-ENOMEM);
2771 yaffsfs_SetError(-ENODEV);
2781 Y_LOFF_T yaffs_freespace(const YCHAR *path)
2783 Y_LOFF_T retVal = -1;
2784 struct yaffs_dev *dev = NULL;
2787 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2788 yaffsfs_SetError(-EFAULT);
2792 if (yaffsfs_CheckPath(path) < 0) {
2793 yaffsfs_SetError(-ENAMETOOLONG);
2798 dev = yaffsfs_FindDevice(path, &dummy);
2799 if (dev && dev->is_mounted) {
2800 retVal = yaffs_get_n_free_chunks(dev);
2801 retVal *= dev->data_bytes_per_chunk;
2804 yaffsfs_SetError(-EINVAL);
2810 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
2812 Y_LOFF_T retVal = -1;
2813 struct yaffs_dev *dev = NULL;
2816 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2817 yaffsfs_SetError(-EFAULT);
2821 if (yaffsfs_CheckPath(path) < 0) {
2822 yaffsfs_SetError(-ENAMETOOLONG);
2827 dev = yaffsfs_FindDevice(path, &dummy);
2828 if (dev && dev->is_mounted) {
2829 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2830 dev->param.n_reserved_blocks;
2831 retVal *= dev->param.chunks_per_block;
2832 retVal *= dev->data_bytes_per_chunk;
2835 yaffsfs_SetError(-EINVAL);
2841 int yaffs_inodecount(const YCHAR *path)
2843 Y_LOFF_T retVal = -1;
2844 struct yaffs_dev *dev = NULL;
2847 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2848 yaffsfs_SetError(-EFAULT);
2852 if (yaffsfs_CheckPath(path) < 0) {
2853 yaffsfs_SetError(-ENAMETOOLONG);
2858 dev = yaffsfs_FindDevice(path, &dummy);
2859 if (dev && dev->is_mounted) {
2860 int n_obj = dev->n_obj;
2861 if (n_obj > dev->n_hardlinks)
2862 retVal = n_obj - dev->n_hardlinks;
2866 yaffsfs_SetError(-EINVAL);
2872 void yaffs_add_device(struct yaffs_dev *dev)
2874 struct list_head *cfg;
2875 /* First check that the device is not in the list. */
2877 list_for_each(cfg, &yaffsfs_deviceList) {
2878 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
2882 dev->is_mounted = 0;
2883 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2885 if (!dev->dev_list.next)
2886 INIT_LIST_HEAD(&dev->dev_list);
2888 list_add(&dev->dev_list, &yaffsfs_deviceList);
2893 void yaffs_remove_device(struct yaffs_dev *dev)
2895 list_del_init(&dev->dev_list);
2898 /* Functions to iterate through devices. NB Use with extreme care! */
2900 static struct list_head *dev_iterator;
2901 void yaffs_dev_rewind(void)
2903 dev_iterator = yaffsfs_deviceList.next;
2906 struct yaffs_dev *yaffs_next_dev(void)
2908 struct yaffs_dev *retval;
2912 if (dev_iterator == &yaffsfs_deviceList)
2915 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2916 dev_iterator = dev_iterator->next;
2920 /* Directory search stuff. */
2922 static struct list_head search_contexts;
2924 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContext *dsc)
2928 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2932 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
2933 dsc->nextReturn = NULL;
2936 list_entry(dsc->dirObj->variant.dir_variant.
2937 children.next, struct yaffs_obj,
2940 /* Hey someone isn't playing nice! */
2944 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContext *dsc)
2948 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2950 if (dsc->nextReturn == NULL ||
2951 list_empty(&dsc->dirObj->variant.dir_variant.children))
2952 dsc->nextReturn = NULL;
2954 struct list_head *next = dsc->nextReturn->siblings.next;
2956 if (next == &dsc->dirObj->variant.dir_variant.children)
2957 dsc->nextReturn = NULL; /* end of list */
2959 dsc->nextReturn = list_entry(next,
2964 /* Hey someone isn't playing nice! */
2968 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2971 struct list_head *i;
2972 struct yaffsfs_DirSearchContext *dsc;
2974 /* if search contexts not initilised then skip */
2975 if (!search_contexts.next)
2978 /* Iterate through the directory search contexts.
2979 * If any are the one being removed, then advance the dsc to
2980 * the next one to prevent a hanging ptr.
2982 list_for_each(i, &search_contexts) {
2984 dsc = list_entry(i, struct yaffsfs_DirSearchContext,
2986 if (dsc->nextReturn == obj)
2987 yaffsfs_DirAdvance(dsc);
2993 static yaffs_DIR *yaffsfs_opendir_no_lock(const YCHAR *dirname)
2995 yaffs_DIR *dir = NULL;
2996 struct yaffs_obj *obj = NULL;
2997 struct yaffsfs_DirSearchContext *dsc = NULL;
3001 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
3002 yaffsfs_SetError(-EFAULT);
3006 if (yaffsfs_CheckPath(dirname) < 0) {
3007 yaffsfs_SetError(-ENAMETOOLONG);
3011 obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, ¬Dir, &loop);
3014 yaffsfs_SetError(-ENOTDIR);
3016 yaffsfs_SetError(-ELOOP);
3018 yaffsfs_SetError(-ENOENT);
3019 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
3020 yaffsfs_SetError(-ENOTDIR);
3024 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
3025 if (!yaffsfs_dsc[i].inUse)
3026 dsc = &yaffsfs_dsc[i];
3029 dir = (yaffs_DIR *) dsc;
3032 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContext));
3035 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3036 INIT_LIST_HEAD(&dsc->others);
3038 if (!search_contexts.next)
3039 INIT_LIST_HEAD(&search_contexts);
3041 list_add(&dsc->others, &search_contexts);
3042 yaffsfs_SetDirRewound(dsc);
3048 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
3053 ret = yaffsfs_opendir_no_lock(dirname);
3058 struct yaffs_dirent *yaffsfs_readdir_no_lock(yaffs_DIR * dirp)
3060 struct yaffsfs_DirSearchContext *dsc;
3061 struct yaffs_dirent *retVal = NULL;
3063 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3066 if (dsc && dsc->inUse) {
3067 yaffsfs_SetError(0);
3068 if (dsc->nextReturn) {
3070 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3071 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3072 dsc->de.d_off = dsc->offset++;
3073 yaffs_get_obj_name(dsc->nextReturn,
3074 dsc->de.d_name, NAME_MAX);
3075 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3076 /* this should not happen! */
3077 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3079 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3081 yaffsfs_DirAdvance(dsc);
3085 yaffsfs_SetError(-EBADF);
3090 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3092 struct yaffs_dirent *ret;
3095 ret = yaffsfs_readdir_no_lock(dirp);
3100 static void yaffsfs_rewinddir_no_lock(yaffs_DIR *dirp)
3102 struct yaffsfs_DirSearchContext *dsc;
3104 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3106 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3109 yaffsfs_SetDirRewound(dsc);
3113 void yaffs_rewinddir(yaffs_DIR *dirp)
3116 yaffsfs_rewinddir_no_lock(dirp);
3120 struct yaffs_dirent *yaffs_readdir_fd(int fd)
3122 struct yaffs_dirent *ret = NULL;
3123 struct yaffsfs_FileDes *f;
3126 f = yaffsfs_HandleToFileDes(fd);
3128 ret = yaffsfs_readdir_no_lock(f->v.dir);
3133 void yaffs_rewinddir_fd(int fd)
3135 struct yaffsfs_FileDes *f;
3138 f = yaffsfs_HandleToFileDes(fd);
3140 yaffsfs_rewinddir_no_lock(f->v.dir);
3145 static int yaffsfs_closedir_no_lock(yaffs_DIR *dirp)
3147 struct yaffsfs_DirSearchContext *dsc;
3149 dsc = (struct yaffsfs_DirSearchContext *) dirp;
3151 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3152 yaffsfs_SetError(-EFAULT);
3157 list_del(&dsc->others); /* unhook from list */
3161 int yaffs_closedir(yaffs_DIR *dirp)
3166 ret = yaffsfs_closedir_no_lock(dirp);
3171 /* End of directory stuff */
3173 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3175 struct yaffs_obj *parent = NULL;
3176 struct yaffs_obj *obj;
3179 int mode = 0; /* ignore for now */
3183 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3184 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3185 yaffsfs_SetError(-EFAULT);
3189 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3190 yaffsfs_SetError(-ENAMETOOLONG);
3195 parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, ¬Dir, &loop);
3196 if (!parent && notDir)
3197 yaffsfs_SetError(-ENOTDIR);
3199 yaffsfs_SetError(-ELOOP);
3200 else if (!parent || yaffs_strnlen(name, 5) < 1)
3201 yaffsfs_SetError(-ENOENT);
3202 else if (yaffsfs_TooManyObjects(parent->my_dev))
3203 yaffsfs_SetError(-ENFILE);
3204 else if (parent->my_dev->read_only)
3205 yaffsfs_SetError(-EROFS);
3207 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3210 else if (yaffsfs_FindObject
3211 (NULL, newpath, 0, 0, NULL, NULL, NULL))
3212 yaffsfs_SetError(-EEXIST);
3214 yaffsfs_SetError(-ENOSPC);
3223 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3225 struct yaffs_obj *obj = NULL;
3226 struct yaffs_obj *dir = NULL;
3231 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3232 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3233 yaffsfs_SetError(-EFAULT);
3239 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
3242 yaffsfs_SetError(-ENOTDIR);
3244 yaffsfs_SetError(-ELOOP);
3245 else if (!dir || !obj)
3246 yaffsfs_SetError(-ENOENT);
3247 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3248 yaffsfs_SetError(-EINVAL);
3250 YCHAR *alias = obj->variant.symlink_variant.alias;
3251 memset(buf, 0, bufsiz);
3252 yaffs_strncpy(buf, alias, bufsiz - 1);
3259 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3261 /* Creates a link called newpath to existing oldpath */
3262 struct yaffs_obj *obj = NULL;
3263 struct yaffs_obj *lnk = NULL;
3264 struct yaffs_obj *obj_dir = NULL;
3265 struct yaffs_obj *lnk_dir = NULL;
3273 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3274 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3275 yaffsfs_SetError(-EFAULT);
3279 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3280 yaffsfs_SetError(-ENAMETOOLONG);
3286 obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
3287 &obj_dir, ¬DirObj, &objLoop);
3288 lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
3289 lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
3290 0, ¬DirLnk, &lnkLoop);
3292 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3293 yaffsfs_SetError(-ENOTDIR);
3294 else if (objLoop || lnkLoop)
3295 yaffsfs_SetError(-ELOOP);
3296 else if (!obj_dir || !lnk_dir || !obj)
3297 yaffsfs_SetError(-ENOENT);
3298 else if (obj->my_dev->read_only)
3299 yaffsfs_SetError(-EROFS);
3300 else if (yaffsfs_TooManyObjects(obj->my_dev))
3301 yaffsfs_SetError(-ENFILE);
3303 yaffsfs_SetError(-EEXIST);
3304 else if (lnk_dir->my_dev != obj->my_dev)
3305 yaffsfs_SetError(-EXDEV);
3307 retVal = yaffsfs_CheckNameLength(newname);
3310 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3314 yaffsfs_SetError(-ENOSPC);
3324 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3330 yaffsfs_SetError(-EINVAL);
3335 * D E B U G F U N C T I O N S
3340 * Returns number of handles attached to the object
3342 int yaffs_n_handles(const YCHAR *path)
3344 struct yaffs_obj *obj;
3346 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3347 yaffsfs_SetError(-EFAULT);
3351 if (yaffsfs_CheckPath(path) < 0) {
3352 yaffsfs_SetError(-ENAMETOOLONG);
3356 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
3359 return yaffsfs_CountHandles(obj);
3364 int yaffs_get_error(void)
3366 return yaffsfs_GetLastError();
3369 int yaffs_set_error(int error)
3371 yaffsfs_SetError(error);
3375 int yaffs_dump_dev(const YCHAR *path)
3382 struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
3385 struct yaffs_dev *dev = obj->my_dev;
3388 "n_page_writes.......... %d\n"
3389 "n_page_reads........... %d\n"
3390 "n_erasures....... %d\n"
3391 "n_gc_copies............ %d\n"
3392 "garbageCollections... %d\n"
3393 "passiveGarbageColl'ns %d\n"
3399 dev->garbageCollections, dev->passiveGarbageCollections);