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);
41 unsigned int yaffs_wr_attempts;
45 * There are open inodes in struct yaffsfs_Inode.
46 * There are open file descriptors in yaffsfs_FileDes.
47 * There are open handles in yaffsfs_FileDes.
49 * Things are structured this way to be like the Linux VFS model
50 * so that interactions with the yaffs guts calls are similar.
51 * That means more common code paths and less special code.
52 * That means better testing etc.
54 * We have 3 layers because:
55 * A handle is different than an fd because you can use dup()
56 * to create a new handle that accesses the *same* fd. The two
57 * handles will use the same offset (part of the fd). We only close
58 * down the fd when there are no more handles accessing it.
60 * More than one fd can currently access one file, but each fd
61 * has its own permsiions and offset.
64 struct yaffsfs_Inode {
65 int count; /* Number of handles accessing this inode */
66 struct yaffs_obj *iObj;
69 struct yaffsfs_FileDes {
75 int inodeId:12; /* Index to corresponding yaffsfs_Inode */
76 int handleCount:10; /* Number of handles for this fd */
77 Y_LOFF_T position; /* current position in file */
80 struct yaffsfs_Handle {
86 struct yaffsfs_DirSearchContxt {
87 struct yaffs_dirent de; /* directory entry */
88 YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
89 struct yaffs_obj *dirObj; /* ptr to directory being searched */
90 struct yaffs_obj *nextReturn; /* obj returned by next readddir */
91 struct list_head others;
96 static struct yaffsfs_DirSearchContxt yaffsfs_dsc[YAFFSFS_N_DSC];
97 static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
98 static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
99 static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
101 static int yaffsfs_handlesInitialised;
103 unsigned yaffs_set_trace(unsigned tm)
105 yaffs_trace_mask = tm;
106 return yaffs_trace_mask;
109 unsigned yaffs_get_trace(void)
111 return yaffs_trace_mask;
116 * Inilitalise handle management on start-up.
119 static void yaffsfs_InitHandles(void)
122 if (yaffsfs_handlesInitialised)
125 yaffsfs_handlesInitialised = 1;
127 memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
128 memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
129 memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
130 memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
132 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
133 yaffsfs_fd[i].inodeId = -1;
134 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
135 yaffsfs_handle[i].fdId = -1;
138 static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
140 if (h >= 0 && h < YAFFSFS_N_HANDLES)
141 return &yaffsfs_handle[h];
145 static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
147 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
149 if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
150 return &yaffsfs_fd[h->fdId];
155 static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
157 struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
159 if (fd && fd->handleCount > 0 &&
160 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
161 return &yaffsfs_inode[fd->inodeId];
166 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
168 struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
177 * yaffsfs_FindInodeIdForObject
178 * Find the inode entry for an object, if it exists.
181 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
187 obj = yaffs_get_equivalent_obj(obj);
189 /* Look for it in open inode table */
190 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
191 if (yaffsfs_inode[i].iObj == obj)
198 * yaffsfs_GetInodeIdForObject
199 * Grab an inode entry when opening a new inode.
201 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
205 struct yaffsfs_Inode *in = NULL;
208 obj = yaffs_get_equivalent_obj(obj);
210 ret = yaffsfs_FindInodeIdForObject(obj);
212 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
213 if (!yaffsfs_inode[i].iObj)
218 in = &yaffsfs_inode[ret];
228 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
230 int i = yaffsfs_FindInodeIdForObject(obj);
233 return yaffsfs_inode[i].count;
238 static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
240 struct yaffs_obj *obj;
247 obj->my_inode = NULL;
252 static void yaffsfs_PutInode(int inodeId)
254 if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
255 struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
257 if (in->count <= 0) {
258 yaffsfs_ReleaseInode(in);
264 static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
267 struct yaffsfs_Handle *h;
269 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
270 h = &yaffsfs_handle[i];
271 if (h->useCount < 1) {
272 memset(h, 0, sizeof(struct yaffsfs_Handle));
283 static int yaffsfs_NewHandleAndFileDes(void)
286 struct yaffsfs_FileDes *fd;
287 struct yaffsfs_Handle *h = NULL;
288 int handle = yaffsfs_NewHandle(&h);
293 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
295 if (fd->handleCount < 1) {
296 memset(fd, 0, sizeof(struct yaffsfs_FileDes));
304 /* Dump the handle because we could not get a fd */
311 * Increase use of handle when reading/writing a file
312 * Also gets the file descriptor.
315 static int yaffsfs_GetHandle(int handle)
317 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
319 if (h && h->useCount > 0) {
328 * Let go of a handle when closing a file or aborting an open or
329 * ending a read or write.
332 static int yaffsfs_PutFileDes(int fdId)
334 struct yaffsfs_FileDes *fd;
336 if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
337 fd = &yaffsfs_fd[fdId];
339 if (fd->handleCount < 1) {
340 if (fd->inodeId >= 0) {
341 yaffsfs_PutInode(fd->inodeId);
349 static int yaffsfs_PutHandle(int handle)
351 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
353 if (h && h->useCount > 0) {
355 if (h->useCount < 1) {
356 yaffsfs_PutFileDes(h->fdId);
364 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
366 struct yaffsfs_FileDes *fd;
367 struct yaffsfs_Handle *h;
368 struct yaffs_obj *obj;
370 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
371 h = yaffsfs_HandleToPointer(i);
372 fd = yaffsfs_HandleToFileDes(i);
373 obj = yaffsfs_HandleToObject(i);
374 if (h && h->useCount > 0) {
378 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
380 yaffsfs_PutInode(fd->inodeId);
387 * Stuff to handle names.
389 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
391 static int yaffs_toupper(YCHAR a)
393 if (a >= 'a' && a <= 'z')
394 return (a - 'a') + 'A';
399 static int yaffsfs_Match(YCHAR a, YCHAR b)
401 return (yaffs_toupper(a) == yaffs_toupper(b));
404 static int yaffsfs_Match(YCHAR a, YCHAR b)
411 static int yaffsfs_IsPathDivider(YCHAR ch)
413 const YCHAR *str = YAFFS_PATH_DIVIDERS;
424 static int yaffsfs_CheckNameLength(const char *name)
428 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
430 if (nameLength == 0) {
431 yaffsfs_SetError(-ENOENT);
433 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
434 yaffsfs_SetError(-ENAMETOOLONG);
441 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
443 YCHAR *alt_path = NULL;
448 * We don't have a definition for max path length.
449 * We will use 3 * max name length instead.
452 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
454 /* If the last character is a path divider, then we need to
455 * trim it back so that the name look-up works properly.
456 * eg. /foo/new_dir/ -> /foo/newdir
457 * Curveball: Need to handle multiple path dividers:
458 * eg. /foof/sdfse///// -> /foo/sdfse
460 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
461 alt_path = kmalloc(path_length + 1, 0);
464 yaffs_strcpy(alt_path, path);
465 for (i = path_length - 1;
466 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
467 alt_path[i] = (YCHAR) 0;
469 *ret_path = alt_path;
473 LIST_HEAD(yaffsfs_deviceList);
478 * Scan the configuration list to find the device
479 * Curveballs: Should match paths that end in '/' too
480 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
482 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
485 struct list_head *cfg;
486 const YCHAR *leftOver;
488 struct yaffs_dev *retval = NULL;
489 struct yaffs_dev *dev = NULL;
491 int longestMatch = -1;
495 * Check all configs, choose the one that:
496 * 1) Actually matches a prefix (ie /a amd /abc will not match
497 * 2) Matches the longest.
499 list_for_each(cfg, &yaffsfs_deviceList) {
500 dev = list_entry(cfg, struct yaffs_dev, dev_list);
509 while (matching && *p && *leftOver) {
510 /* Skip over any /s */
511 while (yaffsfs_IsPathDivider(*p))
514 /* Skip over any /s */
515 while (yaffsfs_IsPathDivider(*leftOver))
518 /* Now match the text part */
520 *p && !yaffsfs_IsPathDivider(*p) &&
521 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
522 if (yaffsfs_Match(*p, *leftOver)) {
532 /* Skip over any /s in leftOver */
533 while (yaffsfs_IsPathDivider(*leftOver))
536 /*Skip over any /s in p */
537 while (yaffsfs_IsPathDivider(*p))
540 /* p should now be at the end of the string if fully matched */
544 if (matching && (thisMatchLength > longestMatch)) {
546 *restOfPath = (YCHAR *) leftOver;
548 longestMatch = thisMatchLength;
555 static int yaffsfs_CheckPath(const YCHAR *path)
560 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
561 if (yaffsfs_IsPathDivider(*path)) {
569 return (*path) ? -1 : 0;
572 /* FindMountPoint only returns a dev entry if the path is a mount point */
573 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
575 struct yaffs_dev *dev;
576 YCHAR *restOfPath = NULL;
578 dev = yaffsfs_FindDevice(path, &restOfPath);
579 if (dev && restOfPath && *restOfPath)
584 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
587 struct yaffs_dev *dev;
589 dev = yaffsfs_FindDevice(path, restOfPath);
590 if (dev && dev->is_mounted)
591 return dev->root_dir;
596 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
597 int symDepth, int *loop)
601 obj = yaffs_get_equivalent_obj(obj);
603 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
604 YCHAR *alias = obj->variant.symlink_variant.alias;
606 if (yaffsfs_IsPathDivider(*alias))
607 /* Starts with a /, need to scan from root up */
608 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
609 1, NULL, NULL, loop);
612 * Relative to here so use the parent of the
615 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
616 1, NULL, NULL, loop);
622 * yaffsfs_FindDirectory
623 * Parse a path to determine the directory and the name within the directory.
625 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
627 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
629 YCHAR **name, int symDepth,
630 int *notDir, int *loop)
632 struct yaffs_obj *dir;
634 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
637 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
645 restOfPath = (YCHAR *) path;
647 dir = yaffsfs_FindRoot(path, &restOfPath);
652 * curve ball: also throw away surplus '/'
653 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
655 while (yaffsfs_IsPathDivider(*restOfPath))
656 restOfPath++; /* get rid of '/' */
661 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
662 if (i < YAFFS_MAX_NAME_LENGTH) {
663 str[i] = *restOfPath;
671 /* got to the end of the string */
674 if (yaffs_strcmp(str, _Y(".")) == 0) {
676 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
679 dir = yaffs_find_by_name(dir, str);
681 dir = yaffsfs_FollowLink(dir, symDepth, loop);
683 if (dir && dir->variant_type !=
684 YAFFS_OBJECT_TYPE_DIRECTORY) {
693 /* directory did not exist. */
697 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
701 int *notDir, int *loop)
703 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
708 * yaffsfs_FindObject turns a path for an existing object into the object
710 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
711 const YCHAR *path, int symDepth,
713 struct yaffs_obj **dirOut,
714 int *notDir, int *loop)
716 struct yaffs_obj *dir;
717 struct yaffs_obj *obj;
721 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
727 obj = yaffs_find_by_name(dir, name);
732 obj = yaffs_get_equivalent_obj(obj);
737 /*************************************************************************
738 * Start of yaffsfs visible functions.
739 *************************************************************************/
741 int yaffs_dup(int handle)
743 int newHandleNumber = -1;
744 struct yaffsfs_FileDes *existingFD = NULL;
745 struct yaffsfs_Handle *existingHandle = NULL;
746 struct yaffsfs_Handle *newHandle = NULL;
749 existingHandle = yaffsfs_HandleToPointer(handle);
750 existingFD = yaffsfs_HandleToFileDes(handle);
752 newHandleNumber = yaffsfs_NewHandle(&newHandle);
754 newHandle->fdId = existingHandle->fdId;
755 existingFD->handleCount++;
761 yaffsfs_SetError(-EBADF);
763 yaffsfs_SetError(-ENOMEM);
765 return newHandleNumber;
769 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
771 int current_objects = dev->n_obj - dev->n_deleted_files;
773 if (dev->param.max_objects && current_objects > dev->param.max_objects)
779 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
781 struct yaffs_obj *obj = NULL;
782 struct yaffs_obj *dir = NULL;
785 struct yaffsfs_FileDes *fd = NULL;
788 int errorReported = 0;
789 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
790 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
791 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
792 u8 sharedReadAllowed;
793 u8 sharedWriteAllowed;
801 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
802 yaffsfs_SetError(-EFAULT);
806 if (yaffsfs_CheckPath(path) < 0) {
807 yaffsfs_SetError(-ENAMETOOLONG);
811 /* O_EXCL only has meaning if O_CREAT is specified */
812 if (!(oflag & O_CREAT))
815 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
816 if ((oflag & O_CREAT) & (oflag & O_EXCL))
819 /* Todo: Are there any more flag combos to sanitise ? */
821 /* Figure out if reading or writing is requested */
823 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
824 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
828 handle = yaffsfs_NewHandleAndFileDes();
831 yaffsfs_SetError(-ENFILE);
835 fd = yaffsfs_HandleToFileDes(handle);
837 /* try to find the exisiting object */
838 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
840 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
843 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
844 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
849 /* The file already exists or it might be a directory */
851 /* A directory can't be opened as a file */
852 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
854 yaffsfs_SetError(-EISDIR);
858 /* Open should fail if O_CREAT and O_EXCL are specified
859 * for a file that exists.
861 if (!errorReported &&
862 (oflag & O_EXCL) && (oflag & O_CREAT)) {
864 yaffsfs_SetError(-EEXIST);
868 /* Check file permissions */
869 if (readRequested && !(obj->yst_mode & S_IREAD))
872 if (writeRequested && !(obj->yst_mode & S_IWRITE))
875 if (!errorReported && writeRequested &&
876 obj->my_dev->read_only) {
878 yaffsfs_SetError(-EROFS);
882 if (openDenied && !errorReported) {
883 yaffsfs_SetError(-EACCES);
887 /* Check sharing of an existing object. */
889 struct yaffsfs_FileDes *fdx;
892 sharedReadAllowed = 1;
893 sharedWriteAllowed = 1;
896 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
897 fdx = &yaffsfs_fd[i];
898 if (fdx->handleCount > 0 &&
900 yaffsfs_inode[fdx->inodeId].iObj
903 sharedReadAllowed = 0;
904 if (!fdx->shareWrite)
905 sharedWriteAllowed = 0;
913 if ((!sharedReadAllowed && readRequested) ||
914 (!shareRead && alreadyReading) ||
915 (!sharedWriteAllowed && writeRequested) ||
916 (!shareWrite && alreadyWriting)) {
918 yaffsfs_SetError(-EBUSY);
925 /* If we could not open an existing object, then let's see if
926 * the directory exists. If not, error.
928 if (!obj && !errorReported) {
929 dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
931 if (!dir && notDir) {
932 yaffsfs_SetError(-ENOTDIR);
935 yaffsfs_SetError(-ELOOP);
938 yaffsfs_SetError(-ENOENT);
943 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
944 /* Let's see if we can create this file */
945 if (dir->my_dev->read_only) {
946 yaffsfs_SetError(-EROFS);
948 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
949 yaffsfs_SetError(-ENFILE);
952 obj = yaffs_create_file(dir, name, mode, 0, 0);
954 if (!obj && !errorReported) {
955 yaffsfs_SetError(-ENOSPC);
960 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
961 yaffsfs_SetError(-ENOENT);
965 if (obj && !openDenied) {
966 int inodeId = yaffsfs_GetInodeIdForObject(obj);
970 * Todo: Fix any problem if inodes run out,
971 * That can't happen if the number of inode
972 * items >= number of handles.
976 fd->inodeId = inodeId;
977 fd->reading = readRequested;
978 fd->writing = writeRequested;
979 fd->append = (oflag & O_APPEND) ? 1 : 0;
981 fd->shareRead = shareRead;
982 fd->shareWrite = shareWrite;
984 /* Hook inode to object */
985 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
987 if ((oflag & O_TRUNC) && fd->writing)
988 yaffs_resize_file(obj, 0);
990 yaffsfs_PutHandle(handle);
992 yaffsfs_SetError(0); /* Problem */
1002 int yaffs_open(const YCHAR *path, int oflag, int mode)
1004 return yaffs_open_sharing(path, oflag, mode,
1005 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1008 static int yaffs_Dofsync(int handle, int datasync)
1011 struct yaffs_obj *obj;
1015 obj = yaffsfs_HandleToObject(handle);
1018 yaffsfs_SetError(-EBADF);
1019 else if (obj->my_dev->read_only)
1020 yaffsfs_SetError(-EROFS);
1022 yaffs_flush_file(obj, 1, datasync);
1031 int yaffs_fsync(int handle)
1033 return yaffs_Dofsync(handle, 0);
1036 int yaffs_flush(int handle)
1038 return yaffs_fsync(handle);
1041 int yaffs_fdatasync(int handle)
1043 return yaffs_Dofsync(handle, 1);
1046 int yaffs_close(int handle)
1048 struct yaffsfs_Handle *h = NULL;
1049 struct yaffs_obj *obj = NULL;
1054 h = yaffsfs_HandleToPointer(handle);
1055 obj = yaffsfs_HandleToObject(handle);
1058 yaffsfs_SetError(-EBADF);
1061 yaffs_flush_file(obj, 1, 0);
1062 yaffsfs_PutHandle(handle);
1071 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1072 int isPread, Y_LOFF_T offset)
1074 struct yaffsfs_FileDes *fd = NULL;
1075 struct yaffs_obj *obj = NULL;
1077 Y_LOFF_T startPos = 0;
1078 Y_LOFF_T endPos = 0;
1083 u8 *buf = (u8 *) vbuf;
1085 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1086 yaffsfs_SetError(-EFAULT);
1091 fd = yaffsfs_HandleToFileDes(handle);
1092 obj = yaffsfs_HandleToObject(handle);
1096 yaffsfs_SetError(-EBADF);
1098 } else if (!fd->reading) {
1099 /* Not a reading handle */
1100 yaffsfs_SetError(-EINVAL);
1102 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1103 yaffsfs_SetError(-EINVAL);
1109 startPos = fd->position;
1113 if (yaffs_get_obj_length(obj) > pos)
1114 maxRead = yaffs_get_obj_length(obj) - pos;
1118 if (nbyte > maxRead)
1121 yaffsfs_GetHandle(handle);
1123 endPos = pos + nbyte;
1125 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1126 nbyte > YAFFS_MAX_FILE_SIZE ||
1127 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1133 nToRead = YAFFSFS_RW_SIZE -
1134 (pos & (YAFFSFS_RW_SIZE - 1));
1135 if (nToRead > nbyte)
1139 * Need to reverify object in case the device was
1140 * unmounted in another thread.
1142 obj = yaffsfs_HandleToObject(handle);
1146 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1154 if (nRead == nToRead)
1157 nbyte = 0; /* no more to read */
1166 yaffsfs_PutHandle(handle);
1170 fd->position = startPos + totalRead;
1172 yaffsfs_SetError(-EINVAL);
1179 return (totalRead >= 0) ? totalRead : -1;
1183 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1185 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1188 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1190 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1193 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1194 int isPwrite, Y_LOFF_T offset)
1196 struct yaffsfs_FileDes *fd = NULL;
1197 struct yaffs_obj *obj = NULL;
1199 Y_LOFF_T startPos = 0;
1202 int totalWritten = 0;
1203 int write_trhrough = 0;
1205 const u8 *buf = (const u8 *)vbuf;
1207 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1208 yaffsfs_SetError(-EFAULT);
1213 fd = yaffsfs_HandleToFileDes(handle);
1214 obj = yaffsfs_HandleToObject(handle);
1218 yaffsfs_SetError(-EBADF);
1220 } else if (!fd->writing) {
1221 yaffsfs_SetError(-EINVAL);
1223 } else if (obj->my_dev->read_only) {
1224 yaffsfs_SetError(-EROFS);
1228 startPos = yaffs_get_obj_length(obj);
1232 startPos = fd->position;
1234 yaffsfs_GetHandle(handle);
1236 endPos = pos + nbyte;
1238 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1239 nbyte > YAFFS_MAX_FILE_SIZE ||
1240 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1247 nToWrite = YAFFSFS_RW_SIZE -
1248 (pos & (YAFFSFS_RW_SIZE - 1));
1249 if (nToWrite > nbyte)
1253 * Need to reverify object in case the device was
1254 * remounted or unmounted in another thread.
1256 obj = yaffsfs_HandleToObject(handle);
1257 if (!obj || obj->my_dev->read_only)
1261 yaffs_wr_file(obj, buf, pos, nToWrite,
1264 totalWritten += nWritten;
1269 if (nWritten == nToWrite)
1274 if (nWritten < 1 && totalWritten < 1) {
1275 yaffsfs_SetError(-ENOSPC);
1285 yaffsfs_PutHandle(handle);
1288 if (totalWritten > 0)
1289 fd->position = startPos + totalWritten;
1291 yaffsfs_SetError(-EINVAL);
1297 return (totalWritten >= 0) ? totalWritten : -1;
1300 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1302 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1305 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1307 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1310 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1312 struct yaffs_obj *obj = NULL;
1313 struct yaffs_obj *dir = NULL;
1314 int result = YAFFS_FAIL;
1318 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1319 yaffsfs_SetError(-EFAULT);
1323 if (yaffsfs_CheckPath(path) < 0) {
1324 yaffsfs_SetError(-ENAMETOOLONG);
1330 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1331 obj = yaffsfs_FollowLink(obj, 0, &loop);
1334 yaffsfs_SetError(-ENOTDIR);
1336 yaffsfs_SetError(-ELOOP);
1337 else if (!dir || !obj)
1338 yaffsfs_SetError(-ENOENT);
1339 else if (obj->my_dev->read_only)
1340 yaffsfs_SetError(-EROFS);
1341 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1342 yaffsfs_SetError(-EISDIR);
1343 else if (obj->my_dev->read_only)
1344 yaffsfs_SetError(-EROFS);
1345 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1346 yaffsfs_SetError(-EINVAL);
1348 result = yaffs_resize_file(obj, new_size);
1352 return (result) ? 0 : -1;
1355 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1357 struct yaffsfs_FileDes *fd = NULL;
1358 struct yaffs_obj *obj = NULL;
1362 fd = yaffsfs_HandleToFileDes(handle);
1363 obj = yaffsfs_HandleToObject(handle);
1367 yaffsfs_SetError(-EBADF);
1368 else if (!fd->writing)
1369 yaffsfs_SetError(-EINVAL);
1370 else if (obj->my_dev->read_only)
1371 yaffsfs_SetError(-EROFS);
1372 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1373 yaffsfs_SetError(-EINVAL);
1375 /* resize the file */
1376 result = yaffs_resize_file(obj, new_size);
1379 return (result) ? 0 : -1;
1383 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1385 struct yaffsfs_FileDes *fd = NULL;
1386 struct yaffs_obj *obj = NULL;
1388 Y_LOFF_T fSize = -1;
1391 fd = yaffsfs_HandleToFileDes(handle);
1392 obj = yaffsfs_HandleToObject(handle);
1395 yaffsfs_SetError(-EBADF);
1396 else if (offset > YAFFS_MAX_FILE_SIZE)
1397 yaffsfs_SetError(-EINVAL);
1399 if (whence == SEEK_SET) {
1402 } else if (whence == SEEK_CUR) {
1403 if ((fd->position + offset) >= 0)
1404 pos = (fd->position + offset);
1405 } else if (whence == SEEK_END) {
1406 fSize = yaffs_get_obj_length(obj);
1407 if (fSize >= 0 && (fSize + offset) >= 0)
1408 pos = fSize + offset;
1411 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1414 yaffsfs_SetError(-EINVAL);
1424 static int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
1426 struct yaffs_obj *dir = NULL;
1427 struct yaffs_obj *obj = NULL;
1429 int result = YAFFS_FAIL;
1433 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1434 yaffsfs_SetError(-EFAULT);
1438 if (yaffsfs_CheckPath(path) < 0) {
1439 yaffsfs_SetError(-ENAMETOOLONG);
1445 obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
1446 dir = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
1449 yaffsfs_SetError(-ENOTDIR);
1451 yaffsfs_SetError(-ELOOP);
1453 yaffsfs_SetError(-ENOENT);
1454 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1455 yaffsfs_SetError(-EINVAL);
1457 yaffsfs_SetError(-ENOENT);
1458 else if (obj->my_dev->read_only)
1459 yaffsfs_SetError(-EROFS);
1460 else if (!isDirectory &&
1461 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1462 yaffsfs_SetError(-EISDIR);
1463 else if (isDirectory &&
1464 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1465 yaffsfs_SetError(-ENOTDIR);
1466 else if (isDirectory && obj == obj->my_dev->root_dir)
1467 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1469 result = yaffs_unlinker(dir, name);
1471 if (result == YAFFS_FAIL && isDirectory)
1472 yaffsfs_SetError(-ENOTEMPTY);
1477 return (result == YAFFS_FAIL) ? -1 : 0;
1480 int yaffs_unlink(const YCHAR *path)
1482 return yaffsfs_DoUnlink(path, 0);
1485 static int rename_file_over_dir(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1487 if (obj && obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY &&
1488 newobj && newobj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1494 static int rename_dir_over_file(struct yaffs_obj *obj, struct yaffs_obj *newobj)
1496 if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY &&
1497 newobj && newobj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1503 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1505 struct yaffs_obj *olddir = NULL;
1506 struct yaffs_obj *newdir = NULL;
1507 struct yaffs_obj *obj = NULL;
1508 struct yaffs_obj *newobj = NULL;
1511 int result = YAFFS_FAIL;
1512 int rename_allowed = 1;
1518 YCHAR *alt_newpath = NULL;
1520 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1521 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1522 yaffsfs_SetError(-EFAULT);
1526 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1527 yaffsfs_SetError(-ENAMETOOLONG);
1531 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1532 yaffsfs_SetError(-ENOMEM);
1536 newPath = alt_newpath;
1540 olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
1541 ¬OldDir, &oldLoop);
1542 newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
1543 ¬NewDir, &newLoop);
1544 obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
1545 newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
1547 /* If the object being renamed is a directory and the
1548 * path ended with a "/" then the olddir == obj.
1549 * We pass through NULL for the old name to tell the lower layers
1550 * to use olddir as the object.
1556 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1557 yaffsfs_SetError(-ENOTDIR);
1559 } else if (oldLoop || newLoop) {
1560 yaffsfs_SetError(-ELOOP);
1562 } else if (olddir && oldname &&
1563 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1564 yaffsfs_SetError(-EINVAL);
1566 } else if (!olddir || !newdir || !obj) {
1567 yaffsfs_SetError(-ENOENT);
1569 } else if (obj->my_dev->read_only) {
1570 yaffsfs_SetError(-EROFS);
1572 } else if (rename_file_over_dir(obj, newobj)) {
1573 yaffsfs_SetError(-EISDIR);
1575 } else if (rename_dir_over_file(obj, newobj)) {
1576 yaffsfs_SetError(-ENOTDIR);
1578 } else if (yaffs_is_non_empty_dir(newobj)) {
1579 yaffsfs_SetError(-ENOTEMPTY);
1581 } else if (olddir->my_dev != newdir->my_dev) {
1582 /* Rename must be on same device */
1583 yaffsfs_SetError(-EXDEV);
1585 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1587 * It is a directory, check that it is not being renamed to
1588 * being its own decendent.
1589 * Do this by tracing from the new directory back to the root,
1593 struct yaffs_obj *xx = newdir;
1595 while (rename_allowed && xx) {
1600 if (!rename_allowed)
1601 yaffsfs_SetError(-EINVAL);
1605 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1611 return (result == YAFFS_FAIL) ? -1 : 0;
1614 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1618 obj = yaffs_get_equivalent_obj(obj);
1621 buf->st_dev = (int)obj->my_dev->os_context;
1622 buf->st_ino = obj->obj_id;
1623 buf->st_mode = obj->yst_mode & ~S_IFMT;
1625 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1626 buf->st_mode |= S_IFDIR;
1627 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1628 buf->st_mode |= S_IFLNK;
1629 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1630 buf->st_mode |= S_IFREG;
1632 buf->st_nlink = yaffs_get_obj_link_count(obj);
1635 buf->st_rdev = obj->yst_rdev;
1636 buf->st_size = yaffs_get_obj_length(obj);
1637 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1638 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1640 #if CONFIG_YAFFS_WINCE
1641 buf->yst_wince_atime[0] = obj->win_atime[0];
1642 buf->yst_wince_atime[1] = obj->win_atime[1];
1643 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1644 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1645 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1646 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1648 buf->yst_atime = obj->yst_atime;
1649 buf->yst_ctime = obj->yst_ctime;
1650 buf->yst_mtime = obj->yst_mtime;
1657 static int yaffsfs_DoStatOrLStat(const YCHAR *path,
1658 struct yaffs_stat *buf, int doLStat)
1660 struct yaffs_obj *obj = NULL;
1661 struct yaffs_obj *dir = NULL;
1666 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1667 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1668 yaffsfs_SetError(-EFAULT);
1672 if (yaffsfs_CheckPath(path) < 0) {
1673 yaffsfs_SetError(-ENAMETOOLONG);
1679 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1681 if (!doLStat && obj)
1682 obj = yaffsfs_FollowLink(obj, 0, &loop);
1685 yaffsfs_SetError(-ENOTDIR);
1687 yaffsfs_SetError(-ELOOP);
1688 else if (!dir || !obj)
1689 yaffsfs_SetError(-ENOENT);
1691 retVal = yaffsfs_DoStat(obj, buf);
1699 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1701 return yaffsfs_DoStatOrLStat(path, buf, 0);
1704 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1706 return yaffsfs_DoStatOrLStat(path, buf, 1);
1709 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1711 struct yaffs_obj *obj;
1715 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1716 yaffsfs_SetError(-EFAULT);
1721 obj = yaffsfs_HandleToObject(fd);
1724 retVal = yaffsfs_DoStat(obj, buf);
1727 yaffsfs_SetError(-EBADF);
1734 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1735 const struct yaffs_utimbuf *buf)
1740 struct yaffs_utimbuf local;
1742 obj = yaffs_get_equivalent_obj(obj);
1744 if (obj && obj->my_dev->read_only) {
1745 yaffsfs_SetError(-EROFS);
1750 local.actime = Y_CURRENT_TIME;
1751 local.modtime = local.actime;
1756 obj->yst_atime = buf->actime;
1757 obj->yst_mtime = buf->modtime;
1759 result = yaffs_flush_file(obj, 0, 0);
1760 retVal = result == YAFFS_OK ? 0 : -1;
1766 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1768 struct yaffs_obj *obj = NULL;
1769 struct yaffs_obj *dir = NULL;
1775 yaffsfs_SetError(-EFAULT);
1779 if (yaffsfs_CheckPath(path) < 0) {
1780 yaffsfs_SetError(-ENAMETOOLONG);
1786 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1789 yaffsfs_SetError(-ENOTDIR);
1791 yaffsfs_SetError(-ELOOP);
1792 else if (!dir || !obj)
1793 yaffsfs_SetError(-ENOENT);
1795 retVal = yaffsfs_DoUtime(obj, buf);
1803 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1805 struct yaffs_obj *obj;
1810 obj = yaffsfs_HandleToObject(fd);
1813 retVal = yaffsfs_DoUtime(obj, buf);
1816 yaffsfs_SetError(-EBADF);
1823 #ifndef CONFIG_YAFFS_WINCE
1824 /* xattrib functions */
1826 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1827 const void *data, int size, int flags, int follow)
1829 struct yaffs_obj *obj;
1830 struct yaffs_obj *dir;
1836 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1837 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1838 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1839 yaffsfs_SetError(-EFAULT);
1843 if (yaffsfs_CheckPath(path) < 0) {
1844 yaffsfs_SetError(-ENAMETOOLONG);
1850 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1853 obj = yaffsfs_FollowLink(obj, 0, &loop);
1856 yaffsfs_SetError(-ENOTDIR);
1858 yaffsfs_SetError(-ELOOP);
1859 else if (!dir || !obj)
1860 yaffsfs_SetError(-ENOENT);
1862 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1864 yaffsfs_SetError(retVal);
1875 int yaffs_setxattr(const YCHAR *path, const char *name,
1876 const void *data, int size, int flags)
1878 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1881 int yaffs_lsetxattr(const YCHAR *path, const char *name,
1882 const void *data, int size, int flags)
1884 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1887 int yaffs_fsetxattr(int fd, const char *name,
1888 const void *data, int size, int flags)
1890 struct yaffs_obj *obj;
1894 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1895 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1896 yaffsfs_SetError(-EFAULT);
1901 obj = yaffsfs_HandleToObject(fd);
1904 yaffsfs_SetError(-EBADF);
1906 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1908 yaffsfs_SetError(retVal);
1918 static int yaffs_do_getxattr(const YCHAR *path, const char *name,
1919 void *data, int size, int follow)
1921 struct yaffs_obj *obj;
1922 struct yaffs_obj *dir;
1927 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1928 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1929 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1930 yaffsfs_SetError(-EFAULT);
1934 if (yaffsfs_CheckPath(path) < 0) {
1935 yaffsfs_SetError(-ENAMETOOLONG);
1941 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1944 obj = yaffsfs_FollowLink(obj, 0, &loop);
1947 yaffsfs_SetError(-ENOTDIR);
1949 yaffsfs_SetError(-ELOOP);
1950 else if (!dir || !obj)
1951 yaffsfs_SetError(-ENOENT);
1953 retVal = yaffs_get_xattrib(obj, name, data, size);
1955 yaffsfs_SetError(retVal);
1965 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1967 return yaffs_do_getxattr(path, name, data, size, 1);
1970 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1972 return yaffs_do_getxattr(path, name, data, size, 0);
1975 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1977 struct yaffs_obj *obj;
1981 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1982 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1983 yaffsfs_SetError(-EFAULT);
1988 obj = yaffsfs_HandleToObject(fd);
1991 retVal = yaffs_get_xattrib(obj, name, data, size);
1993 yaffsfs_SetError(retVal);
1998 yaffsfs_SetError(-EBADF);
2005 static int yaffs_do_listxattr(const YCHAR *path, char *data,
2006 int size, int follow)
2008 struct yaffs_obj *obj = NULL;
2009 struct yaffs_obj *dir = NULL;
2014 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2015 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2016 yaffsfs_SetError(-EFAULT);
2020 if (yaffsfs_CheckPath(path) < 0) {
2021 yaffsfs_SetError(-ENAMETOOLONG);
2027 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2030 obj = yaffsfs_FollowLink(obj, 0, &loop);
2033 yaffsfs_SetError(-ENOTDIR);
2035 yaffsfs_SetError(-ELOOP);
2036 else if (!dir || !obj)
2037 yaffsfs_SetError(-ENOENT);
2039 retVal = yaffs_list_xattrib(obj, data, size);
2041 yaffsfs_SetError(retVal);
2052 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2054 return yaffs_do_listxattr(path, data, size, 1);
2057 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2059 return yaffs_do_listxattr(path, data, size, 0);
2062 int yaffs_flistxattr(int fd, char *data, int size)
2064 struct yaffs_obj *obj;
2068 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2069 yaffsfs_SetError(-EFAULT);
2074 obj = yaffsfs_HandleToObject(fd);
2077 retVal = yaffs_list_xattrib(obj, data, size);
2079 yaffsfs_SetError(retVal);
2084 yaffsfs_SetError(-EBADF);
2091 static int yaffs_do_removexattr(const YCHAR *path, const char *name,
2094 struct yaffs_obj *obj = NULL;
2095 struct yaffs_obj *dir = NULL;
2100 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2101 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2102 yaffsfs_SetError(-EFAULT);
2106 if (yaffsfs_CheckPath(path) < 0) {
2107 yaffsfs_SetError(-ENAMETOOLONG);
2113 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2116 obj = yaffsfs_FollowLink(obj, 0, &loop);
2119 yaffsfs_SetError(-ENOTDIR);
2121 yaffsfs_SetError(-ELOOP);
2122 else if (!dir || !obj)
2123 yaffsfs_SetError(-ENOENT);
2125 retVal = yaffs_remove_xattrib(obj, name);
2127 yaffsfs_SetError(retVal);
2138 int yaffs_removexattr(const YCHAR *path, const char *name)
2140 return yaffs_do_removexattr(path, name, 1);
2143 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2145 return yaffs_do_removexattr(path, name, 0);
2148 int yaffs_fremovexattr(int fd, const char *name)
2150 struct yaffs_obj *obj;
2154 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2155 yaffsfs_SetError(-EFAULT);
2160 obj = yaffsfs_HandleToObject(fd);
2163 retVal = yaffs_remove_xattrib(obj, name);
2165 yaffsfs_SetError(retVal);
2170 yaffsfs_SetError(-EBADF);
2178 #ifdef CONFIG_YAFFS_WINCE
2179 int yaffs_get_wince_times(int fd, unsigned *wctime,
2180 unsigned *watime, unsigned *wmtime)
2182 struct yaffs_obj *obj;
2187 obj = yaffsfs_HandleToObject(fd);
2192 wctime[0] = obj->win_ctime[0];
2193 wctime[1] = obj->win_ctime[1];
2196 watime[0] = obj->win_atime[0];
2197 watime[1] = obj->win_atime[1];
2200 wmtime[0] = obj->win_mtime[0];
2201 wmtime[1] = obj->win_mtime[1];
2207 yaffsfs_SetError(-EBADF);
2214 int yaffs_set_wince_times(int fd,
2215 const unsigned *wctime,
2216 const unsigned *watime, const unsigned *wmtime)
2218 struct yaffs_obj *obj;
2223 obj = yaffsfs_HandleToObject(fd);
2228 obj->win_ctime[0] = wctime[0];
2229 obj->win_ctime[1] = wctime[1];
2232 obj->win_atime[0] = watime[0];
2233 obj->win_atime[1] = watime[1];
2236 obj->win_mtime[0] = wmtime[0];
2237 obj->win_mtime[1] = wmtime[1];
2241 result = yaffs_flush_file(obj, 0, 0);
2245 yaffsfs_SetError(-EBADF);
2254 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2259 obj = yaffs_get_equivalent_obj(obj);
2262 obj->yst_mode = mode;
2264 result = yaffs_flush_file(obj, 0, 0);
2267 return result == YAFFS_OK ? 0 : -1;
2270 int yaffs_access(const YCHAR *path, int amode)
2272 struct yaffs_obj *obj = NULL;
2273 struct yaffs_obj *dir = NULL;
2278 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2279 yaffsfs_SetError(-EFAULT);
2283 if (yaffsfs_CheckPath(path) < 0) {
2284 yaffsfs_SetError(-ENAMETOOLONG);
2288 if (amode & ~(R_OK | W_OK | X_OK)) {
2289 yaffsfs_SetError(-EINVAL);
2295 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2296 obj = yaffsfs_FollowLink(obj, 0, &loop);
2299 yaffsfs_SetError(-ENOTDIR);
2301 yaffsfs_SetError(-ELOOP);
2302 else if (!dir || !obj)
2303 yaffsfs_SetError(-ENOENT);
2304 else if ((amode & W_OK) && obj->my_dev->read_only)
2305 yaffsfs_SetError(-EROFS);
2309 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2311 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2313 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2317 yaffsfs_SetError(-EACCES);
2328 int yaffs_chmod(const YCHAR *path, mode_t mode)
2330 struct yaffs_obj *obj = NULL;
2331 struct yaffs_obj *dir = NULL;
2336 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2337 yaffsfs_SetError(-EFAULT);
2341 if (yaffsfs_CheckPath(path) < 0) {
2342 yaffsfs_SetError(-ENAMETOOLONG);
2346 if (mode & ~(0777)) {
2347 yaffsfs_SetError(-EINVAL);
2353 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2354 obj = yaffsfs_FollowLink(obj, 0, &loop);
2357 yaffsfs_SetError(-ENOTDIR);
2359 yaffsfs_SetError(-ELOOP);
2360 else if (!dir || !obj)
2361 yaffsfs_SetError(-ENOENT);
2362 else if (obj->my_dev->read_only)
2363 yaffsfs_SetError(-EROFS);
2365 retVal = yaffsfs_DoChMod(obj, mode);
2373 int yaffs_fchmod(int fd, mode_t mode)
2375 struct yaffs_obj *obj;
2378 if (mode & ~(0777)) {
2379 yaffsfs_SetError(-EINVAL);
2384 obj = yaffsfs_HandleToObject(fd);
2387 yaffsfs_SetError(-EBADF);
2388 else if (obj->my_dev->read_only)
2389 yaffsfs_SetError(-EROFS);
2391 retVal = yaffsfs_DoChMod(obj, mode);
2398 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2400 struct yaffs_obj *parent = NULL;
2401 struct yaffs_obj *dir = NULL;
2403 YCHAR *alt_path = NULL;
2408 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2409 yaffsfs_SetError(-EFAULT);
2413 if (yaffsfs_CheckPath(path) < 0) {
2414 yaffsfs_SetError(-ENAMETOOLONG);
2418 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2419 yaffsfs_SetError(-ENOMEM);
2426 parent = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
2427 if (!parent && notDir)
2428 yaffsfs_SetError(-ENOTDIR);
2430 yaffsfs_SetError(-ELOOP);
2432 yaffsfs_SetError(-ENOENT);
2433 else if (yaffsfs_TooManyObjects(parent->my_dev))
2434 yaffsfs_SetError(-ENFILE);
2435 else if (yaffs_strnlen(name, 5) == 0) {
2436 /* Trying to make the root itself */
2437 yaffsfs_SetError(-EEXIST);
2438 } else if (parent->my_dev->read_only)
2439 yaffsfs_SetError(-EROFS);
2441 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2444 else if (yaffs_find_by_name(parent, name))
2445 yaffsfs_SetError(-EEXIST); /* name exists */
2447 yaffsfs_SetError(-ENOSPC); /* assume no space */
2457 int yaffs_rmdir(const YCHAR *path)
2462 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2463 yaffsfs_SetError(-EFAULT);
2467 if (yaffsfs_CheckPath(path) < 0) {
2468 yaffsfs_SetError(-ENAMETOOLONG);
2472 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2473 yaffsfs_SetError(-ENOMEM);
2478 result = yaffsfs_DoUnlink(path, 1);
2485 void *yaffs_getdev(const YCHAR *path)
2487 struct yaffs_dev *dev = NULL;
2489 dev = yaffsfs_FindDevice(path, &dummy);
2493 int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
2496 int result = YAFFS_FAIL;
2497 struct yaffs_dev *dev = NULL;
2499 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2500 yaffsfs_SetError(-EFAULT);
2504 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2506 if (yaffsfs_CheckPath(path) < 0) {
2507 yaffsfs_SetError(-ENAMETOOLONG);
2513 yaffsfs_InitHandles();
2515 dev = yaffsfs_FindMountPoint(path);
2517 if (!dev->is_mounted) {
2518 dev->read_only = read_only ? 1 : 0;
2520 u8 skip = dev->param.skip_checkpt_rd;
2521 dev->param.skip_checkpt_rd = 1;
2522 result = yaffs_guts_initialise(dev);
2523 dev->param.skip_checkpt_rd = skip;
2525 result = yaffs_guts_initialise(dev);
2528 if (result == YAFFS_FAIL)
2529 yaffsfs_SetError(-ENOMEM);
2530 retVal = result ? 0 : -1;
2533 yaffsfs_SetError(-EBUSY);
2535 yaffsfs_SetError(-ENODEV);
2542 int yaffs_mount2(const YCHAR *path, int readonly)
2544 return yaffs_mount_common(path, readonly, 0);
2547 int yaffs_mount(const YCHAR *path)
2549 return yaffs_mount_common(path, 0, 0);
2552 int yaffs_sync(const YCHAR *path)
2555 struct yaffs_dev *dev = NULL;
2558 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2559 yaffsfs_SetError(-EFAULT);
2563 if (yaffsfs_CheckPath(path) < 0) {
2564 yaffsfs_SetError(-ENAMETOOLONG);
2569 dev = yaffsfs_FindDevice(path, &dummy);
2571 if (!dev->is_mounted)
2572 yaffsfs_SetError(-EINVAL);
2573 else if (dev->read_only)
2574 yaffsfs_SetError(-EROFS);
2577 yaffs_flush_whole_cache(dev);
2578 yaffs_checkpoint_save(dev);
2583 yaffsfs_SetError(-ENODEV);
2589 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2592 struct yaffs_obj *obj;
2594 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2595 obj = yaffsfs_HandleToObject(i);
2596 if (obj && obj->my_dev == dev)
2602 int yaffs_remount(const YCHAR *path, int force, int read_only)
2605 struct yaffs_dev *dev = NULL;
2607 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2608 yaffsfs_SetError(-EFAULT);
2612 if (yaffsfs_CheckPath(path) < 0) {
2613 yaffsfs_SetError(-ENAMETOOLONG);
2618 dev = yaffsfs_FindMountPoint(path);
2620 if (dev->is_mounted) {
2621 yaffs_flush_whole_cache(dev);
2623 if (force || !yaffsfs_IsDevBusy(dev)) {
2625 yaffs_checkpoint_save(dev);
2626 dev->read_only = read_only ? 1 : 0;
2629 yaffsfs_SetError(-EBUSY);
2632 yaffsfs_SetError(-EINVAL);
2635 yaffsfs_SetError(-ENODEV);
2642 int yaffs_unmount2(const YCHAR *path, int force)
2645 struct yaffs_dev *dev = NULL;
2647 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2648 yaffsfs_SetError(-EFAULT);
2652 if (yaffsfs_CheckPath(path) < 0) {
2653 yaffsfs_SetError(-ENAMETOOLONG);
2658 dev = yaffsfs_FindMountPoint(path);
2660 if (dev->is_mounted) {
2662 yaffs_flush_whole_cache(dev);
2663 yaffs_checkpoint_save(dev);
2664 inUse = yaffsfs_IsDevBusy(dev);
2665 if (!inUse || force) {
2667 yaffsfs_BreakDeviceHandles(dev);
2668 yaffs_deinitialise(dev);
2672 yaffsfs_SetError(-EBUSY);
2675 yaffsfs_SetError(-EINVAL);
2678 yaffsfs_SetError(-ENODEV);
2685 int yaffs_unmount(const YCHAR *path)
2687 return yaffs_unmount2(path, 0);
2690 int yaffs_format(const YCHAR *path,
2692 int force_unmount_flag,
2696 struct yaffs_dev *dev = NULL;
2700 yaffsfs_SetError(-EFAULT);
2704 if (yaffsfs_CheckPath(path) < 0) {
2705 yaffsfs_SetError(-ENAMETOOLONG);
2710 dev = yaffsfs_FindMountPoint(path);
2713 int was_mounted = dev->is_mounted;
2715 if (dev->is_mounted && unmount_flag) {
2717 yaffs_flush_whole_cache(dev);
2718 yaffs_checkpoint_save(dev);
2719 inUse = yaffsfs_IsDevBusy(dev);
2720 if (!inUse || force_unmount_flag) {
2722 yaffsfs_BreakDeviceHandles(dev);
2723 yaffs_deinitialise(dev);
2727 if(dev->is_mounted) {
2728 yaffsfs_SetError(-EBUSY);
2731 yaffs_format_dev(dev);
2732 if(was_mounted && remount_flag) {
2733 result = yaffs_guts_initialise(dev);
2734 if (result == YAFFS_FAIL) {
2735 yaffsfs_SetError(-ENOMEM);
2741 yaffsfs_SetError(-ENODEV);
2751 Y_LOFF_T yaffs_freespace(const YCHAR *path)
2753 Y_LOFF_T retVal = -1;
2754 struct yaffs_dev *dev = NULL;
2757 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2758 yaffsfs_SetError(-EFAULT);
2762 if (yaffsfs_CheckPath(path) < 0) {
2763 yaffsfs_SetError(-ENAMETOOLONG);
2768 dev = yaffsfs_FindDevice(path, &dummy);
2769 if (dev && dev->is_mounted) {
2770 retVal = yaffs_get_n_free_chunks(dev);
2771 retVal *= dev->data_bytes_per_chunk;
2774 yaffsfs_SetError(-EINVAL);
2780 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
2782 Y_LOFF_T retVal = -1;
2783 struct yaffs_dev *dev = NULL;
2786 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2787 yaffsfs_SetError(-EFAULT);
2791 if (yaffsfs_CheckPath(path) < 0) {
2792 yaffsfs_SetError(-ENAMETOOLONG);
2797 dev = yaffsfs_FindDevice(path, &dummy);
2798 if (dev && dev->is_mounted) {
2799 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2800 dev->param.n_reserved_blocks;
2801 retVal *= dev->param.chunks_per_block;
2802 retVal *= dev->data_bytes_per_chunk;
2805 yaffsfs_SetError(-EINVAL);
2811 int yaffs_inodecount(const YCHAR *path)
2813 Y_LOFF_T retVal = -1;
2814 struct yaffs_dev *dev = NULL;
2817 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2818 yaffsfs_SetError(-EFAULT);
2822 if (yaffsfs_CheckPath(path) < 0) {
2823 yaffsfs_SetError(-ENAMETOOLONG);
2828 dev = yaffsfs_FindDevice(path, &dummy);
2829 if (dev && dev->is_mounted) {
2830 int n_obj = dev->n_obj;
2831 if (n_obj > dev->n_hardlinks)
2832 retVal = n_obj - dev->n_hardlinks;
2836 yaffsfs_SetError(-EINVAL);
2842 void yaffs_add_device(struct yaffs_dev *dev)
2844 struct list_head *cfg;
2845 /* First check that the device is not in the list. */
2847 list_for_each(cfg, &yaffsfs_deviceList) {
2848 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
2852 dev->is_mounted = 0;
2853 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2855 if (!dev->dev_list.next)
2856 INIT_LIST_HEAD(&dev->dev_list);
2858 list_add(&dev->dev_list, &yaffsfs_deviceList);
2861 void yaffs_remove_device(struct yaffs_dev *dev)
2863 list_del_init(&dev->dev_list);
2866 /* Functions to iterate through devices. NB Use with extreme care! */
2868 static struct list_head *dev_iterator;
2869 void yaffs_dev_rewind(void)
2871 dev_iterator = yaffsfs_deviceList.next;
2874 struct yaffs_dev *yaffs_next_dev(void)
2876 struct yaffs_dev *retval;
2880 if (dev_iterator == &yaffsfs_deviceList)
2883 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2884 dev_iterator = dev_iterator->next;
2888 /* Directory search stuff. */
2890 static struct list_head search_contexts;
2892 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContxt *dsc)
2896 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2900 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
2901 dsc->nextReturn = NULL;
2904 list_entry(dsc->dirObj->variant.dir_variant.
2905 children.next, struct yaffs_obj,
2908 /* Hey someone isn't playing nice! */
2912 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContxt *dsc)
2916 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2918 if (dsc->nextReturn == NULL ||
2919 list_empty(&dsc->dirObj->variant.dir_variant.children))
2920 dsc->nextReturn = NULL;
2922 struct list_head *next = dsc->nextReturn->siblings.next;
2924 if (next == &dsc->dirObj->variant.dir_variant.children)
2925 dsc->nextReturn = NULL; /* end of list */
2927 dsc->nextReturn = list_entry(next,
2932 /* Hey someone isn't playing nice! */
2936 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2939 struct list_head *i;
2940 struct yaffsfs_DirSearchContxt *dsc;
2942 /* if search contexts not initilised then skip */
2943 if (!search_contexts.next)
2946 /* Iterate through the directory search contexts.
2947 * If any are the one being removed, then advance the dsc to
2948 * the next one to prevent a hanging ptr.
2950 list_for_each(i, &search_contexts) {
2952 dsc = list_entry(i, struct yaffsfs_DirSearchContxt,
2954 if (dsc->nextReturn == obj)
2955 yaffsfs_DirAdvance(dsc);
2961 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2963 yaffs_DIR *dir = NULL;
2964 struct yaffs_obj *obj = NULL;
2965 struct yaffsfs_DirSearchContxt *dsc = NULL;
2969 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
2970 yaffsfs_SetError(-EFAULT);
2974 if (yaffsfs_CheckPath(dirname) < 0) {
2975 yaffsfs_SetError(-ENAMETOOLONG);
2981 obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, ¬Dir, &loop);
2984 yaffsfs_SetError(-ENOTDIR);
2986 yaffsfs_SetError(-ELOOP);
2988 yaffsfs_SetError(-ENOENT);
2989 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2990 yaffsfs_SetError(-ENOTDIR);
2994 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
2995 if (!yaffsfs_dsc[i].inUse)
2996 dsc = &yaffsfs_dsc[i];
2999 dir = (yaffs_DIR *) dsc;
3002 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContxt));
3005 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
3006 INIT_LIST_HEAD(&dsc->others);
3008 if (!search_contexts.next)
3009 INIT_LIST_HEAD(&search_contexts);
3011 list_add(&dsc->others, &search_contexts);
3012 yaffsfs_SetDirRewound(dsc);
3022 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3024 struct yaffsfs_DirSearchContxt *dsc;
3025 struct yaffs_dirent *retVal = NULL;
3027 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3030 if (dsc && dsc->inUse) {
3031 yaffsfs_SetError(0);
3032 if (dsc->nextReturn) {
3034 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3035 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3036 dsc->de.d_off = dsc->offset++;
3037 yaffs_get_obj_name(dsc->nextReturn,
3038 dsc->de.d_name, NAME_MAX);
3039 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3040 /* this should not happen! */
3041 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3043 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3045 yaffsfs_DirAdvance(dsc);
3049 yaffsfs_SetError(-EBADF);
3057 void yaffs_rewinddir(yaffs_DIR *dirp)
3059 struct yaffsfs_DirSearchContxt *dsc;
3061 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3063 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3068 yaffsfs_SetDirRewound(dsc);
3073 int yaffs_closedir(yaffs_DIR *dirp)
3075 struct yaffsfs_DirSearchContxt *dsc;
3077 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3079 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3080 yaffsfs_SetError(-EFAULT);
3086 list_del(&dsc->others); /* unhook from list */
3091 /* End of directory stuff */
3093 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3095 struct yaffs_obj *parent = NULL;
3096 struct yaffs_obj *obj;
3099 int mode = 0; /* ignore for now */
3103 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3104 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3105 yaffsfs_SetError(-EFAULT);
3109 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3110 yaffsfs_SetError(-ENAMETOOLONG);
3115 parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, ¬Dir, &loop);
3116 if (!parent && notDir)
3117 yaffsfs_SetError(-ENOTDIR);
3119 yaffsfs_SetError(-ELOOP);
3120 else if (!parent || yaffs_strnlen(name, 5) < 1)
3121 yaffsfs_SetError(-ENOENT);
3122 else if (yaffsfs_TooManyObjects(parent->my_dev))
3123 yaffsfs_SetError(-ENFILE);
3124 else if (parent->my_dev->read_only)
3125 yaffsfs_SetError(-EROFS);
3127 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3130 else if (yaffsfs_FindObject
3131 (NULL, newpath, 0, 0, NULL, NULL, NULL))
3132 yaffsfs_SetError(-EEXIST);
3134 yaffsfs_SetError(-ENOSPC);
3143 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3145 struct yaffs_obj *obj = NULL;
3146 struct yaffs_obj *dir = NULL;
3151 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3152 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3153 yaffsfs_SetError(-EFAULT);
3159 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
3162 yaffsfs_SetError(-ENOTDIR);
3164 yaffsfs_SetError(-ELOOP);
3165 else if (!dir || !obj)
3166 yaffsfs_SetError(-ENOENT);
3167 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3168 yaffsfs_SetError(-EINVAL);
3170 YCHAR *alias = obj->variant.symlink_variant.alias;
3171 memset(buf, 0, bufsiz);
3172 yaffs_strncpy(buf, alias, bufsiz - 1);
3179 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3181 /* Creates a link called newpath to existing oldpath */
3182 struct yaffs_obj *obj = NULL;
3183 struct yaffs_obj *lnk = NULL;
3184 struct yaffs_obj *obj_dir = NULL;
3185 struct yaffs_obj *lnk_dir = NULL;
3193 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3194 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3195 yaffsfs_SetError(-EFAULT);
3199 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3200 yaffsfs_SetError(-ENAMETOOLONG);
3206 obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
3207 &obj_dir, ¬DirObj, &objLoop);
3208 lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
3209 lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
3210 0, ¬DirLnk, &lnkLoop);
3212 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3213 yaffsfs_SetError(-ENOTDIR);
3214 else if (objLoop || lnkLoop)
3215 yaffsfs_SetError(-ELOOP);
3216 else if (!obj_dir || !lnk_dir || !obj)
3217 yaffsfs_SetError(-ENOENT);
3218 else if (obj->my_dev->read_only)
3219 yaffsfs_SetError(-EROFS);
3220 else if (yaffsfs_TooManyObjects(obj->my_dev))
3221 yaffsfs_SetError(-ENFILE);
3223 yaffsfs_SetError(-EEXIST);
3224 else if (lnk_dir->my_dev != obj->my_dev)
3225 yaffsfs_SetError(-EXDEV);
3227 retVal = yaffsfs_CheckNameLength(newname);
3230 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3234 yaffsfs_SetError(-ENOSPC);
3244 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3250 yaffsfs_SetError(-EINVAL);
3255 * D E B U G F U N C T I O N S
3260 * Returns number of handles attached to the object
3262 int yaffs_n_handles(const YCHAR *path)
3264 struct yaffs_obj *obj;
3266 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3267 yaffsfs_SetError(-EFAULT);
3271 if (yaffsfs_CheckPath(path) < 0) {
3272 yaffsfs_SetError(-ENAMETOOLONG);
3276 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
3279 return yaffsfs_CountHandles(obj);
3284 int yaffs_get_error(void)
3286 return yaffsfs_GetLastError();
3289 int yaffs_set_error(int error)
3291 yaffsfs_SetError(error);
3295 int yaffs_dump_dev(const YCHAR *path)
3302 struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
3305 struct yaffs_dev *dev = obj->my_dev;
3308 "n_page_writes.......... %d\n"
3309 "n_page_reads........... %d\n"
3310 "n_erasures....... %d\n"
3311 "n_gc_copies............ %d\n"
3312 "garbageCollections... %d\n"
3313 "passiveGarbageColl'ns %d\n"
3319 dev->garbageCollections, dev->passiveGarbageCollections);