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 memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
126 memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
127 memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
128 memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
130 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
131 yaffsfs_fd[i].inodeId = -1;
132 for (i = 0; i < YAFFSFS_N_HANDLES; i++)
133 yaffsfs_handle[i].fdId = -1;
136 static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
138 if (h >= 0 && h < YAFFSFS_N_HANDLES)
139 return &yaffsfs_handle[h];
143 static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
145 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
147 if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
148 return &yaffsfs_fd[h->fdId];
153 static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
155 struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
157 if (fd && fd->handleCount > 0 &&
158 fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
159 return &yaffsfs_inode[fd->inodeId];
164 static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
166 struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
175 * yaffsfs_FindInodeIdForObject
176 * Find the inode entry for an object, if it exists.
179 static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
185 obj = yaffs_get_equivalent_obj(obj);
187 /* Look for it in open inode table */
188 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
189 if (yaffsfs_inode[i].iObj == obj)
196 * yaffsfs_GetInodeIdForObject
197 * Grab an inode entry when opening a new inode.
199 static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
203 struct yaffsfs_Inode *in = NULL;
206 obj = yaffs_get_equivalent_obj(obj);
208 ret = yaffsfs_FindInodeIdForObject(obj);
210 for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
211 if (!yaffsfs_inode[i].iObj)
216 in = &yaffsfs_inode[ret];
226 static int yaffsfs_CountHandles(struct yaffs_obj *obj)
228 int i = yaffsfs_FindInodeIdForObject(obj);
231 return yaffsfs_inode[i].count;
236 static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
238 struct yaffs_obj *obj;
245 obj->my_inode = NULL;
250 static void yaffsfs_PutInode(int inodeId)
252 if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
253 struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
255 if (in->count <= 0) {
256 yaffsfs_ReleaseInode(in);
262 static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
265 struct yaffsfs_Handle *h;
267 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
268 h = &yaffsfs_handle[i];
269 if (h->useCount < 1) {
270 memset(h, 0, sizeof(struct yaffsfs_Handle));
281 static int yaffsfs_NewHandleAndFileDes(void)
284 struct yaffsfs_FileDes *fd;
285 struct yaffsfs_Handle *h = NULL;
286 int handle = yaffsfs_NewHandle(&h);
291 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
293 if (fd->handleCount < 1) {
294 memset(fd, 0, sizeof(struct yaffsfs_FileDes));
302 /* Dump the handle because we could not get a fd */
309 * Increase use of handle when reading/writing a file
310 * Also gets the file descriptor.
313 static int yaffsfs_GetHandle(int handle)
315 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
317 if (h && h->useCount > 0) {
326 * Let go of a handle when closing a file or aborting an open or
327 * ending a read or write.
330 static int yaffsfs_PutFileDes(int fdId)
332 struct yaffsfs_FileDes *fd;
334 if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
335 fd = &yaffsfs_fd[fdId];
337 if (fd->handleCount < 1) {
338 if (fd->inodeId >= 0) {
339 yaffsfs_PutInode(fd->inodeId);
347 static int yaffsfs_PutHandle(int handle)
349 struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
351 if (h && h->useCount > 0) {
353 if (h->useCount < 1) {
354 yaffsfs_PutFileDes(h->fdId);
362 static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
364 struct yaffsfs_FileDes *fd;
365 struct yaffsfs_Handle *h;
366 struct yaffs_obj *obj;
368 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
369 h = yaffsfs_HandleToPointer(i);
370 fd = yaffsfs_HandleToFileDes(i);
371 obj = yaffsfs_HandleToObject(i);
372 if (h && h->useCount > 0) {
376 if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
378 yaffsfs_PutInode(fd->inodeId);
385 * Stuff to handle names.
387 #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
389 static int yaffs_toupper(YCHAR a)
391 if (a >= 'a' && a <= 'z')
392 return (a - 'a') + 'A';
397 static int yaffsfs_Match(YCHAR a, YCHAR b)
399 return (yaffs_toupper(a) == yaffs_toupper(b));
402 static int yaffsfs_Match(YCHAR a, YCHAR b)
409 static int yaffsfs_IsPathDivider(YCHAR ch)
411 const YCHAR *str = YAFFS_PATH_DIVIDERS;
422 static int yaffsfs_CheckNameLength(const char *name)
426 int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
428 if (nameLength == 0) {
429 yaffsfs_SetError(-ENOENT);
431 } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
432 yaffsfs_SetError(-ENAMETOOLONG);
439 static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
441 YCHAR *alt_path = NULL;
446 * We don't have a definition for max path length.
447 * We will use 3 * max name length instead.
450 path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
452 /* If the last character is a path divider, then we need to
453 * trim it back so that the name look-up works properly.
454 * eg. /foo/new_dir/ -> /foo/newdir
455 * Curveball: Need to handle multiple path dividers:
456 * eg. /foof/sdfse///// -> /foo/sdfse
458 if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
459 alt_path = kmalloc(path_length + 1, 0);
462 yaffs_strcpy(alt_path, path);
463 for (i = path_length - 1;
464 i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
465 alt_path[i] = (YCHAR) 0;
467 *ret_path = alt_path;
471 LIST_HEAD(yaffsfs_deviceList);
476 * Scan the configuration list to find the device
477 * Curveballs: Should match paths that end in '/' too
478 * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
480 static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
483 struct list_head *cfg;
484 const YCHAR *leftOver;
486 struct yaffs_dev *retval = NULL;
487 struct yaffs_dev *dev = NULL;
489 int longestMatch = -1;
493 * Check all configs, choose the one that:
494 * 1) Actually matches a prefix (ie /a amd /abc will not match
495 * 2) Matches the longest.
497 list_for_each(cfg, &yaffsfs_deviceList) {
498 dev = list_entry(cfg, struct yaffs_dev, dev_list);
507 while (matching && *p && *leftOver) {
508 /* Skip over any /s */
509 while (yaffsfs_IsPathDivider(*p))
512 /* Skip over any /s */
513 while (yaffsfs_IsPathDivider(*leftOver))
516 /* Now match the text part */
518 *p && !yaffsfs_IsPathDivider(*p) &&
519 *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
520 if (yaffsfs_Match(*p, *leftOver)) {
530 /* Skip over any /s in leftOver */
531 while (yaffsfs_IsPathDivider(*leftOver))
534 /*Skip over any /s in p */
535 while (yaffsfs_IsPathDivider(*p))
538 /* p should now be at the end of the string if fully matched */
542 if (matching && (thisMatchLength > longestMatch)) {
544 *restOfPath = (YCHAR *) leftOver;
546 longestMatch = thisMatchLength;
553 static int yaffsfs_CheckPath(const YCHAR *path)
558 while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
559 if (yaffsfs_IsPathDivider(*path)) {
567 return (*path) ? -1 : 0;
570 /* FindMountPoint only returns a dev entry if the path is a mount point */
571 static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
573 struct yaffs_dev *dev;
574 YCHAR *restOfPath = NULL;
576 dev = yaffsfs_FindDevice(path, &restOfPath);
577 if (dev && restOfPath && *restOfPath)
582 static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
585 struct yaffs_dev *dev;
587 dev = yaffsfs_FindDevice(path, restOfPath);
588 if (dev && dev->is_mounted)
589 return dev->root_dir;
594 static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
595 int symDepth, int *loop)
599 obj = yaffs_get_equivalent_obj(obj);
601 while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
602 YCHAR *alias = obj->variant.symlink_variant.alias;
604 if (yaffsfs_IsPathDivider(*alias))
605 /* Starts with a /, need to scan from root up */
606 obj = yaffsfs_FindObject(NULL, alias, symDepth++,
607 1, NULL, NULL, loop);
610 * Relative to here so use the parent of the
613 obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
614 1, NULL, NULL, loop);
620 * yaffsfs_FindDirectory
621 * Parse a path to determine the directory and the name within the directory.
623 * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
625 static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
627 YCHAR **name, int symDepth,
628 int *notDir, int *loop)
630 struct yaffs_obj *dir;
632 YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
635 if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
643 restOfPath = (YCHAR *) path;
645 dir = yaffsfs_FindRoot(path, &restOfPath);
650 * curve ball: also throw away surplus '/'
651 * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
653 while (yaffsfs_IsPathDivider(*restOfPath))
654 restOfPath++; /* get rid of '/' */
659 while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
660 if (i < YAFFS_MAX_NAME_LENGTH) {
661 str[i] = *restOfPath;
669 /* got to the end of the string */
672 if (yaffs_strcmp(str, _Y(".")) == 0) {
674 } else if (yaffs_strcmp(str, _Y("..")) == 0) {
677 dir = yaffs_find_by_name(dir, str);
679 dir = yaffsfs_FollowLink(dir, symDepth, loop);
681 if (dir && dir->variant_type !=
682 YAFFS_OBJECT_TYPE_DIRECTORY) {
691 /* directory did not exist. */
695 static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
699 int *notDir, int *loop)
701 return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
706 * yaffsfs_FindObject turns a path for an existing object into the object
708 static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
709 const YCHAR *path, int symDepth,
711 struct yaffs_obj **dirOut,
712 int *notDir, int *loop)
714 struct yaffs_obj *dir;
715 struct yaffs_obj *obj;
719 yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
725 obj = yaffs_find_by_name(dir, name);
730 obj = yaffs_get_equivalent_obj(obj);
735 /*************************************************************************
736 * Start of yaffsfs visible functions.
737 *************************************************************************/
739 int yaffs_dup(int handle)
741 int newHandleNumber = -1;
742 struct yaffsfs_FileDes *existingFD = NULL;
743 struct yaffsfs_Handle *existingHandle = NULL;
744 struct yaffsfs_Handle *newHandle = NULL;
747 existingHandle = yaffsfs_HandleToPointer(handle);
748 existingFD = yaffsfs_HandleToFileDes(handle);
750 newHandleNumber = yaffsfs_NewHandle(&newHandle);
752 newHandle->fdId = existingHandle->fdId;
753 existingFD->handleCount++;
759 yaffsfs_SetError(-EBADF);
761 yaffsfs_SetError(-ENOMEM);
763 return newHandleNumber;
767 static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
769 int current_objects = dev->n_obj - dev->n_deleted_files;
771 if (dev->param.max_objects && current_objects > dev->param.max_objects)
777 int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
779 struct yaffs_obj *obj = NULL;
780 struct yaffs_obj *dir = NULL;
783 struct yaffsfs_FileDes *fd = NULL;
786 int errorReported = 0;
787 int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
788 u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
789 u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
790 u8 sharedReadAllowed;
791 u8 sharedWriteAllowed;
799 if (yaffsfs_CheckMemRegion(path, 0, 0)< 0) {
800 yaffsfs_SetError(-EFAULT);
804 if (yaffsfs_CheckPath(path) < 0) {
805 yaffsfs_SetError(-ENAMETOOLONG);
809 /* O_EXCL only has meaning if O_CREAT is specified */
810 if (!(oflag & O_CREAT))
813 /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
814 if ((oflag & O_CREAT) & (oflag & O_EXCL))
817 /* Todo: Are there any more flag combos to sanitise ? */
819 /* Figure out if reading or writing is requested */
821 readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
822 writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
826 handle = yaffsfs_NewHandleAndFileDes();
829 yaffsfs_SetError(-ENFILE);
833 fd = yaffsfs_HandleToFileDes(handle);
835 /* try to find the exisiting object */
836 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
838 obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
841 obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
842 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
847 /* The file already exists or it might be a directory */
849 /* A directory can't be opened as a file */
850 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
852 yaffsfs_SetError(-EISDIR);
856 /* Open should fail if O_CREAT and O_EXCL are specified
857 * for a file that exists.
859 if (!errorReported &&
860 (oflag & O_EXCL) && (oflag & O_CREAT)) {
862 yaffsfs_SetError(-EEXIST);
866 /* Check file permissions */
867 if (readRequested && !(obj->yst_mode & S_IREAD))
870 if (writeRequested && !(obj->yst_mode & S_IWRITE))
873 if (!errorReported && writeRequested &&
874 obj->my_dev->read_only) {
876 yaffsfs_SetError(-EROFS);
880 if (openDenied && !errorReported) {
881 yaffsfs_SetError(-EACCES);
885 /* Check sharing of an existing object. */
887 struct yaffsfs_FileDes *fdx;
890 sharedReadAllowed = 1;
891 sharedWriteAllowed = 1;
894 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
895 fdx = &yaffsfs_fd[i];
896 if (fdx->handleCount > 0 &&
898 yaffsfs_inode[fdx->inodeId].iObj
901 sharedReadAllowed = 0;
902 if (!fdx->shareWrite)
903 sharedWriteAllowed = 0;
911 if ((!sharedReadAllowed && readRequested) ||
912 (!shareRead && alreadyReading) ||
913 (!sharedWriteAllowed && writeRequested) ||
914 (!shareWrite && alreadyWriting)) {
916 yaffsfs_SetError(-EBUSY);
923 /* If we could not open an existing object, then let's see if
924 * the directory exists. If not, error.
926 if (!obj && !errorReported) {
927 dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
929 if (!dir && notDir) {
930 yaffsfs_SetError(-ENOTDIR);
933 yaffsfs_SetError(-ELOOP);
936 yaffsfs_SetError(-ENOENT);
941 if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
942 /* Let's see if we can create this file */
943 if (dir->my_dev->read_only) {
944 yaffsfs_SetError(-EROFS);
946 } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
947 yaffsfs_SetError(-ENFILE);
950 obj = yaffs_create_file(dir, name, mode, 0, 0);
952 if (!obj && !errorReported) {
953 yaffsfs_SetError(-ENOSPC);
958 if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
959 yaffsfs_SetError(-ENOENT);
963 if (obj && !openDenied) {
964 int inodeId = yaffsfs_GetInodeIdForObject(obj);
968 * Todo: Fix any problem if inodes run out,
969 * That can't happen if the number of inode
970 * items >= number of handles.
974 fd->inodeId = inodeId;
975 fd->reading = readRequested;
976 fd->writing = writeRequested;
977 fd->append = (oflag & O_APPEND) ? 1 : 0;
979 fd->shareRead = shareRead;
980 fd->shareWrite = shareWrite;
982 /* Hook inode to object */
983 obj->my_inode = (void *)&yaffsfs_inode[inodeId];
985 if ((oflag & O_TRUNC) && fd->writing)
986 yaffs_resize_file(obj, 0);
988 yaffsfs_PutHandle(handle);
990 yaffsfs_SetError(0); /* Problem */
1000 int yaffs_open(const YCHAR *path, int oflag, int mode)
1002 return yaffs_open_sharing(path, oflag, mode,
1003 YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
1006 static int yaffs_Dofsync(int handle, int datasync)
1009 struct yaffs_obj *obj;
1013 obj = yaffsfs_HandleToObject(handle);
1016 yaffsfs_SetError(-EBADF);
1017 else if (obj->my_dev->read_only)
1018 yaffsfs_SetError(-EROFS);
1020 yaffs_flush_file(obj, 1, datasync);
1029 int yaffs_fsync(int handle)
1031 return yaffs_Dofsync(handle, 0);
1034 int yaffs_flush(int handle)
1036 return yaffs_fsync(handle);
1039 int yaffs_fdatasync(int handle)
1041 return yaffs_Dofsync(handle, 1);
1044 int yaffs_close(int handle)
1046 struct yaffsfs_Handle *h = NULL;
1047 struct yaffs_obj *obj = NULL;
1052 h = yaffsfs_HandleToPointer(handle);
1053 obj = yaffsfs_HandleToObject(handle);
1056 yaffsfs_SetError(-EBADF);
1059 yaffs_flush_file(obj, 1, 0);
1060 yaffsfs_PutHandle(handle);
1069 static int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
1070 int isPread, Y_LOFF_T offset)
1072 struct yaffsfs_FileDes *fd = NULL;
1073 struct yaffs_obj *obj = NULL;
1075 Y_LOFF_T startPos = 0;
1076 Y_LOFF_T endPos = 0;
1081 u8 *buf = (u8 *) vbuf;
1083 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 1) < 0) {
1084 yaffsfs_SetError(-EFAULT);
1089 fd = yaffsfs_HandleToFileDes(handle);
1090 obj = yaffsfs_HandleToObject(handle);
1094 yaffsfs_SetError(-EBADF);
1096 } else if (!fd->reading) {
1097 /* Not a reading handle */
1098 yaffsfs_SetError(-EINVAL);
1100 } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
1101 yaffsfs_SetError(-EINVAL);
1107 startPos = fd->position;
1111 if (yaffs_get_obj_length(obj) > pos)
1112 maxRead = yaffs_get_obj_length(obj) - pos;
1116 if (nbyte > maxRead)
1119 yaffsfs_GetHandle(handle);
1121 endPos = pos + nbyte;
1123 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1124 nbyte > YAFFS_MAX_FILE_SIZE ||
1125 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1131 nToRead = YAFFSFS_RW_SIZE -
1132 (pos & (YAFFSFS_RW_SIZE - 1));
1133 if (nToRead > nbyte)
1137 * Need to reverify object in case the device was
1138 * unmounted in another thread.
1140 obj = yaffsfs_HandleToObject(handle);
1144 nRead = yaffs_file_rd(obj, buf, pos, nToRead);
1152 if (nRead == nToRead)
1155 nbyte = 0; /* no more to read */
1164 yaffsfs_PutHandle(handle);
1168 fd->position = startPos + totalRead;
1170 yaffsfs_SetError(-EINVAL);
1177 return (totalRead >= 0) ? totalRead : -1;
1181 int yaffs_read(int handle, void *buf, unsigned int nbyte)
1183 return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
1186 int yaffs_pread(int handle, void *buf, unsigned int nbyte, Y_LOFF_T offset)
1188 return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
1191 static int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
1192 int isPwrite, Y_LOFF_T offset)
1194 struct yaffsfs_FileDes *fd = NULL;
1195 struct yaffs_obj *obj = NULL;
1197 Y_LOFF_T startPos = 0;
1200 int totalWritten = 0;
1201 int write_trhrough = 0;
1203 const u8 *buf = (const u8 *)vbuf;
1205 if (yaffsfs_CheckMemRegion(vbuf, nbyte, 0) < 0) {
1206 yaffsfs_SetError(-EFAULT);
1211 fd = yaffsfs_HandleToFileDes(handle);
1212 obj = yaffsfs_HandleToObject(handle);
1216 yaffsfs_SetError(-EBADF);
1218 } else if (!fd->writing) {
1219 yaffsfs_SetError(-EINVAL);
1221 } else if (obj->my_dev->read_only) {
1222 yaffsfs_SetError(-EROFS);
1226 startPos = yaffs_get_obj_length(obj);
1230 startPos = fd->position;
1232 yaffsfs_GetHandle(handle);
1234 endPos = pos + nbyte;
1236 if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
1237 nbyte > YAFFS_MAX_FILE_SIZE ||
1238 endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
1245 nToWrite = YAFFSFS_RW_SIZE -
1246 (pos & (YAFFSFS_RW_SIZE - 1));
1247 if (nToWrite > nbyte)
1251 * Need to reverify object in case the device was
1252 * remounted or unmounted in another thread.
1254 obj = yaffsfs_HandleToObject(handle);
1255 if (!obj || obj->my_dev->read_only)
1259 yaffs_wr_file(obj, buf, pos, nToWrite,
1262 totalWritten += nWritten;
1267 if (nWritten == nToWrite)
1272 if (nWritten < 1 && totalWritten < 1) {
1273 yaffsfs_SetError(-ENOSPC);
1283 yaffsfs_PutHandle(handle);
1286 if (totalWritten > 0)
1287 fd->position = startPos + totalWritten;
1289 yaffsfs_SetError(-EINVAL);
1295 return (totalWritten >= 0) ? totalWritten : -1;
1298 int yaffs_write(int fd, const void *buf, unsigned int nbyte)
1300 return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
1303 int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, Y_LOFF_T offset)
1305 return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
1308 int yaffs_truncate(const YCHAR *path, Y_LOFF_T new_size)
1310 struct yaffs_obj *obj = NULL;
1311 struct yaffs_obj *dir = NULL;
1312 int result = YAFFS_FAIL;
1316 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1317 yaffsfs_SetError(-EFAULT);
1321 if (yaffsfs_CheckPath(path) < 0) {
1322 yaffsfs_SetError(-ENAMETOOLONG);
1328 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1329 obj = yaffsfs_FollowLink(obj, 0, &loop);
1332 yaffsfs_SetError(-ENOTDIR);
1334 yaffsfs_SetError(-ELOOP);
1335 else if (!dir || !obj)
1336 yaffsfs_SetError(-ENOENT);
1337 else if (obj->my_dev->read_only)
1338 yaffsfs_SetError(-EROFS);
1339 else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
1340 yaffsfs_SetError(-EISDIR);
1341 else if (obj->my_dev->read_only)
1342 yaffsfs_SetError(-EROFS);
1343 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1344 yaffsfs_SetError(-EINVAL);
1346 result = yaffs_resize_file(obj, new_size);
1350 return (result) ? 0 : -1;
1353 int yaffs_ftruncate(int handle, Y_LOFF_T new_size)
1355 struct yaffsfs_FileDes *fd = NULL;
1356 struct yaffs_obj *obj = NULL;
1360 fd = yaffsfs_HandleToFileDes(handle);
1361 obj = yaffsfs_HandleToObject(handle);
1365 yaffsfs_SetError(-EBADF);
1366 else if (!fd->writing)
1367 yaffsfs_SetError(-EINVAL);
1368 else if (obj->my_dev->read_only)
1369 yaffsfs_SetError(-EROFS);
1370 else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
1371 yaffsfs_SetError(-EINVAL);
1373 /* resize the file */
1374 result = yaffs_resize_file(obj, new_size);
1377 return (result) ? 0 : -1;
1381 Y_LOFF_T yaffs_lseek(int handle, Y_LOFF_T offset, int whence)
1383 struct yaffsfs_FileDes *fd = NULL;
1384 struct yaffs_obj *obj = NULL;
1386 Y_LOFF_T fSize = -1;
1389 fd = yaffsfs_HandleToFileDes(handle);
1390 obj = yaffsfs_HandleToObject(handle);
1393 yaffsfs_SetError(-EBADF);
1394 else if (offset > YAFFS_MAX_FILE_SIZE)
1395 yaffsfs_SetError(-EINVAL);
1397 if (whence == SEEK_SET) {
1400 } else if (whence == SEEK_CUR) {
1401 if ((fd->position + offset) >= 0)
1402 pos = (fd->position + offset);
1403 } else if (whence == SEEK_END) {
1404 fSize = yaffs_get_obj_length(obj);
1405 if (fSize >= 0 && (fSize + offset) >= 0)
1406 pos = fSize + offset;
1409 if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
1412 yaffsfs_SetError(-EINVAL);
1422 static int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
1424 struct yaffs_obj *dir = NULL;
1425 struct yaffs_obj *obj = NULL;
1427 int result = YAFFS_FAIL;
1431 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
1432 yaffsfs_SetError(-EFAULT);
1436 if (yaffsfs_CheckPath(path) < 0) {
1437 yaffsfs_SetError(-ENAMETOOLONG);
1443 obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
1444 dir = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
1447 yaffsfs_SetError(-ENOTDIR);
1449 yaffsfs_SetError(-ELOOP);
1451 yaffsfs_SetError(-ENOENT);
1452 else if (yaffs_strncmp(name, _Y("."), 2) == 0)
1453 yaffsfs_SetError(-EINVAL);
1455 yaffsfs_SetError(-ENOENT);
1456 else if (obj->my_dev->read_only)
1457 yaffsfs_SetError(-EROFS);
1458 else if (!isDirectory &&
1459 obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1460 yaffsfs_SetError(-EISDIR);
1461 else if (isDirectory &&
1462 obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
1463 yaffsfs_SetError(-ENOTDIR);
1464 else if (isDirectory && obj == obj->my_dev->root_dir)
1465 yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
1467 result = yaffs_unlinker(dir, name);
1469 if (result == YAFFS_FAIL && isDirectory)
1470 yaffsfs_SetError(-ENOTEMPTY);
1475 return (result == YAFFS_FAIL) ? -1 : 0;
1478 int yaffs_unlink(const YCHAR *path)
1480 return yaffsfs_DoUnlink(path, 0);
1483 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1485 struct yaffs_obj *olddir = NULL;
1486 struct yaffs_obj *newdir = NULL;
1487 struct yaffs_obj *obj = NULL;
1488 struct yaffs_obj *newobj = NULL;
1491 int result = YAFFS_FAIL;
1492 int rename_allowed = 1;
1498 YCHAR *alt_newpath = NULL;
1500 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1501 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1502 yaffsfs_SetError(-EFAULT);
1506 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1507 yaffsfs_SetError(-ENAMETOOLONG);
1511 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1512 yaffsfs_SetError(-ENOMEM);
1516 newPath = alt_newpath;
1520 olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
1521 ¬OldDir, &oldLoop);
1522 newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
1523 ¬NewDir, &newLoop);
1524 obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
1525 newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
1527 /* If the object being renamed is a directory and the
1528 * path ended with a "/" then the olddir == obj.
1529 * We pass through NULL for the old name to tell the lower layers
1530 * to use olddir as the object.
1536 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1537 yaffsfs_SetError(-ENOTDIR);
1539 } else if (oldLoop || newLoop) {
1540 yaffsfs_SetError(-ELOOP);
1542 } else if (olddir && oldname &&
1543 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1544 yaffsfs_SetError(-EINVAL);
1546 } else if (!olddir || !newdir || !obj) {
1547 yaffsfs_SetError(-ENOENT);
1549 } else if (obj->my_dev->read_only) {
1550 yaffsfs_SetError(-EROFS);
1552 } else if (yaffs_is_non_empty_dir(newobj)) {
1553 yaffsfs_SetError(-ENOTEMPTY);
1555 } else if (olddir->my_dev != newdir->my_dev) {
1556 /* Rename must be on same device */
1557 yaffsfs_SetError(-EXDEV);
1559 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1561 * It is a directory, check that it is not being renamed to
1562 * being its own decendent.
1563 * Do this by tracing from the new directory back to the root,
1567 struct yaffs_obj *xx = newdir;
1569 while (rename_allowed && xx) {
1574 if (!rename_allowed)
1575 yaffsfs_SetError(-EINVAL);
1579 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1585 return (result == YAFFS_FAIL) ? -1 : 0;
1588 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1592 obj = yaffs_get_equivalent_obj(obj);
1595 buf->st_dev = (int)obj->my_dev->os_context;
1596 buf->st_ino = obj->obj_id;
1597 buf->st_mode = obj->yst_mode & ~S_IFMT;
1599 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1600 buf->st_mode |= S_IFDIR;
1601 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1602 buf->st_mode |= S_IFLNK;
1603 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1604 buf->st_mode |= S_IFREG;
1606 buf->st_nlink = yaffs_get_obj_link_count(obj);
1609 buf->st_rdev = obj->yst_rdev;
1610 buf->st_size = yaffs_get_obj_length(obj);
1611 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1612 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1614 #if CONFIG_YAFFS_WINCE
1615 buf->yst_wince_atime[0] = obj->win_atime[0];
1616 buf->yst_wince_atime[1] = obj->win_atime[1];
1617 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1618 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1619 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1620 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1622 buf->yst_atime = obj->yst_atime;
1623 buf->yst_ctime = obj->yst_ctime;
1624 buf->yst_mtime = obj->yst_mtime;
1631 static int yaffsfs_DoStatOrLStat(const YCHAR *path,
1632 struct yaffs_stat *buf, int doLStat)
1634 struct yaffs_obj *obj = NULL;
1635 struct yaffs_obj *dir = NULL;
1640 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1641 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1642 yaffsfs_SetError(-EFAULT);
1646 if (yaffsfs_CheckPath(path) < 0) {
1647 yaffsfs_SetError(-ENAMETOOLONG);
1653 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1655 if (!doLStat && obj)
1656 obj = yaffsfs_FollowLink(obj, 0, &loop);
1659 yaffsfs_SetError(-ENOTDIR);
1661 yaffsfs_SetError(-ELOOP);
1662 else if (!dir || !obj)
1663 yaffsfs_SetError(-ENOENT);
1665 retVal = yaffsfs_DoStat(obj, buf);
1673 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1675 return yaffsfs_DoStatOrLStat(path, buf, 0);
1678 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1680 return yaffsfs_DoStatOrLStat(path, buf, 1);
1683 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1685 struct yaffs_obj *obj;
1689 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1690 yaffsfs_SetError(-EFAULT);
1695 obj = yaffsfs_HandleToObject(fd);
1698 retVal = yaffsfs_DoStat(obj, buf);
1701 yaffsfs_SetError(-EBADF);
1708 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1709 const struct yaffs_utimbuf *buf)
1714 struct yaffs_utimbuf local;
1716 obj = yaffs_get_equivalent_obj(obj);
1718 if (obj && obj->my_dev->read_only) {
1719 yaffsfs_SetError(-EROFS);
1724 local.actime = Y_CURRENT_TIME;
1725 local.modtime = local.actime;
1730 obj->yst_atime = buf->actime;
1731 obj->yst_mtime = buf->modtime;
1733 result = yaffs_flush_file(obj, 0, 0);
1734 retVal = result == YAFFS_OK ? 0 : -1;
1740 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1742 struct yaffs_obj *obj = NULL;
1743 struct yaffs_obj *dir = NULL;
1749 yaffsfs_SetError(-EFAULT);
1753 if (yaffsfs_CheckPath(path) < 0) {
1754 yaffsfs_SetError(-ENAMETOOLONG);
1760 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1763 yaffsfs_SetError(-ENOTDIR);
1765 yaffsfs_SetError(-ELOOP);
1766 else if (!dir || !obj)
1767 yaffsfs_SetError(-ENOENT);
1769 retVal = yaffsfs_DoUtime(obj, buf);
1777 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1779 struct yaffs_obj *obj;
1784 obj = yaffsfs_HandleToObject(fd);
1787 retVal = yaffsfs_DoUtime(obj, buf);
1790 yaffsfs_SetError(-EBADF);
1797 #ifndef CONFIG_YAFFS_WINCE
1798 /* xattrib functions */
1800 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1801 const void *data, int size, int flags, int follow)
1803 struct yaffs_obj *obj;
1804 struct yaffs_obj *dir;
1810 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1811 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1812 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1813 yaffsfs_SetError(-EFAULT);
1817 if (yaffsfs_CheckPath(path) < 0) {
1818 yaffsfs_SetError(-ENAMETOOLONG);
1824 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1827 obj = yaffsfs_FollowLink(obj, 0, &loop);
1830 yaffsfs_SetError(-ENOTDIR);
1832 yaffsfs_SetError(-ELOOP);
1833 else if (!dir || !obj)
1834 yaffsfs_SetError(-ENOENT);
1836 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1838 yaffsfs_SetError(retVal);
1849 int yaffs_setxattr(const YCHAR *path, const char *name,
1850 const void *data, int size, int flags)
1852 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1855 int yaffs_lsetxattr(const YCHAR *path, const char *name,
1856 const void *data, int size, int flags)
1858 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1861 int yaffs_fsetxattr(int fd, const char *name,
1862 const void *data, int size, int flags)
1864 struct yaffs_obj *obj;
1868 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1869 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1870 yaffsfs_SetError(-EFAULT);
1875 obj = yaffsfs_HandleToObject(fd);
1878 yaffsfs_SetError(-EBADF);
1880 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1882 yaffsfs_SetError(retVal);
1892 static int yaffs_do_getxattr(const YCHAR *path, const char *name,
1893 void *data, int size, int follow)
1895 struct yaffs_obj *obj;
1896 struct yaffs_obj *dir;
1901 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1902 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1903 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1904 yaffsfs_SetError(-EFAULT);
1908 if (yaffsfs_CheckPath(path) < 0) {
1909 yaffsfs_SetError(-ENAMETOOLONG);
1915 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1918 obj = yaffsfs_FollowLink(obj, 0, &loop);
1921 yaffsfs_SetError(-ENOTDIR);
1923 yaffsfs_SetError(-ELOOP);
1924 else if (!dir || !obj)
1925 yaffsfs_SetError(-ENOENT);
1927 retVal = yaffs_get_xattrib(obj, name, data, size);
1929 yaffsfs_SetError(retVal);
1939 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1941 return yaffs_do_getxattr(path, name, data, size, 1);
1944 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1946 return yaffs_do_getxattr(path, name, data, size, 0);
1949 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1951 struct yaffs_obj *obj;
1955 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1956 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1957 yaffsfs_SetError(-EFAULT);
1962 obj = yaffsfs_HandleToObject(fd);
1965 retVal = yaffs_get_xattrib(obj, name, data, size);
1967 yaffsfs_SetError(retVal);
1972 yaffsfs_SetError(-EBADF);
1979 static int yaffs_do_listxattr(const YCHAR *path, char *data,
1980 int size, int follow)
1982 struct yaffs_obj *obj = NULL;
1983 struct yaffs_obj *dir = NULL;
1988 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1989 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1990 yaffsfs_SetError(-EFAULT);
1994 if (yaffsfs_CheckPath(path) < 0) {
1995 yaffsfs_SetError(-ENAMETOOLONG);
2001 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2004 obj = yaffsfs_FollowLink(obj, 0, &loop);
2007 yaffsfs_SetError(-ENOTDIR);
2009 yaffsfs_SetError(-ELOOP);
2010 else if (!dir || !obj)
2011 yaffsfs_SetError(-ENOENT);
2013 retVal = yaffs_list_xattrib(obj, data, size);
2015 yaffsfs_SetError(retVal);
2026 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2028 return yaffs_do_listxattr(path, data, size, 1);
2031 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2033 return yaffs_do_listxattr(path, data, size, 0);
2036 int yaffs_flistxattr(int fd, char *data, int size)
2038 struct yaffs_obj *obj;
2042 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2043 yaffsfs_SetError(-EFAULT);
2048 obj = yaffsfs_HandleToObject(fd);
2051 retVal = yaffs_list_xattrib(obj, data, size);
2053 yaffsfs_SetError(retVal);
2058 yaffsfs_SetError(-EBADF);
2065 static int yaffs_do_removexattr(const YCHAR *path, const char *name,
2068 struct yaffs_obj *obj = NULL;
2069 struct yaffs_obj *dir = NULL;
2074 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2075 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2076 yaffsfs_SetError(-EFAULT);
2080 if (yaffsfs_CheckPath(path) < 0) {
2081 yaffsfs_SetError(-ENAMETOOLONG);
2087 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2090 obj = yaffsfs_FollowLink(obj, 0, &loop);
2093 yaffsfs_SetError(-ENOTDIR);
2095 yaffsfs_SetError(-ELOOP);
2096 else if (!dir || !obj)
2097 yaffsfs_SetError(-ENOENT);
2099 retVal = yaffs_remove_xattrib(obj, name);
2101 yaffsfs_SetError(retVal);
2112 int yaffs_removexattr(const YCHAR *path, const char *name)
2114 return yaffs_do_removexattr(path, name, 1);
2117 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2119 return yaffs_do_removexattr(path, name, 0);
2122 int yaffs_fremovexattr(int fd, const char *name)
2124 struct yaffs_obj *obj;
2128 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2129 yaffsfs_SetError(-EFAULT);
2134 obj = yaffsfs_HandleToObject(fd);
2137 retVal = yaffs_remove_xattrib(obj, name);
2139 yaffsfs_SetError(retVal);
2144 yaffsfs_SetError(-EBADF);
2152 #ifdef CONFIG_YAFFS_WINCE
2153 int yaffs_get_wince_times(int fd, unsigned *wctime,
2154 unsigned *watime, unsigned *wmtime)
2156 struct yaffs_obj *obj;
2161 obj = yaffsfs_HandleToObject(fd);
2166 wctime[0] = obj->win_ctime[0];
2167 wctime[1] = obj->win_ctime[1];
2170 watime[0] = obj->win_atime[0];
2171 watime[1] = obj->win_atime[1];
2174 wmtime[0] = obj->win_mtime[0];
2175 wmtime[1] = obj->win_mtime[1];
2181 yaffsfs_SetError(-EBADF);
2188 int yaffs_set_wince_times(int fd,
2189 const unsigned *wctime,
2190 const unsigned *watime, const unsigned *wmtime)
2192 struct yaffs_obj *obj;
2197 obj = yaffsfs_HandleToObject(fd);
2202 obj->win_ctime[0] = wctime[0];
2203 obj->win_ctime[1] = wctime[1];
2206 obj->win_atime[0] = watime[0];
2207 obj->win_atime[1] = watime[1];
2210 obj->win_mtime[0] = wmtime[0];
2211 obj->win_mtime[1] = wmtime[1];
2215 result = yaffs_flush_file(obj, 0, 0);
2219 yaffsfs_SetError(-EBADF);
2228 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2233 obj = yaffs_get_equivalent_obj(obj);
2236 obj->yst_mode = mode;
2238 result = yaffs_flush_file(obj, 0, 0);
2241 return result == YAFFS_OK ? 0 : -1;
2244 int yaffs_access(const YCHAR *path, int amode)
2246 struct yaffs_obj *obj = NULL;
2247 struct yaffs_obj *dir = NULL;
2252 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2253 yaffsfs_SetError(-EFAULT);
2257 if (yaffsfs_CheckPath(path) < 0) {
2258 yaffsfs_SetError(-ENAMETOOLONG);
2262 if (amode & ~(R_OK | W_OK | X_OK)) {
2263 yaffsfs_SetError(-EINVAL);
2269 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2270 obj = yaffsfs_FollowLink(obj, 0, &loop);
2273 yaffsfs_SetError(-ENOTDIR);
2275 yaffsfs_SetError(-ELOOP);
2276 else if (!dir || !obj)
2277 yaffsfs_SetError(-ENOENT);
2278 else if ((amode & W_OK) && obj->my_dev->read_only)
2279 yaffsfs_SetError(-EROFS);
2283 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2285 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2287 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2291 yaffsfs_SetError(-EACCES);
2302 int yaffs_chmod(const YCHAR *path, mode_t mode)
2304 struct yaffs_obj *obj = NULL;
2305 struct yaffs_obj *dir = NULL;
2310 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2311 yaffsfs_SetError(-EFAULT);
2315 if (yaffsfs_CheckPath(path) < 0) {
2316 yaffsfs_SetError(-ENAMETOOLONG);
2320 if (mode & ~(0777)) {
2321 yaffsfs_SetError(-EINVAL);
2327 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2328 obj = yaffsfs_FollowLink(obj, 0, &loop);
2331 yaffsfs_SetError(-ENOTDIR);
2333 yaffsfs_SetError(-ELOOP);
2334 else if (!dir || !obj)
2335 yaffsfs_SetError(-ENOENT);
2336 else if (obj->my_dev->read_only)
2337 yaffsfs_SetError(-EROFS);
2339 retVal = yaffsfs_DoChMod(obj, mode);
2347 int yaffs_fchmod(int fd, mode_t mode)
2349 struct yaffs_obj *obj;
2352 if (mode & ~(0777)) {
2353 yaffsfs_SetError(-EINVAL);
2358 obj = yaffsfs_HandleToObject(fd);
2361 yaffsfs_SetError(-EBADF);
2362 else if (obj->my_dev->read_only)
2363 yaffsfs_SetError(-EROFS);
2365 retVal = yaffsfs_DoChMod(obj, mode);
2372 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2374 struct yaffs_obj *parent = NULL;
2375 struct yaffs_obj *dir = NULL;
2377 YCHAR *alt_path = NULL;
2382 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2383 yaffsfs_SetError(-EFAULT);
2387 if (yaffsfs_CheckPath(path) < 0) {
2388 yaffsfs_SetError(-ENAMETOOLONG);
2392 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2393 yaffsfs_SetError(-ENOMEM);
2400 parent = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
2401 if (!parent && notDir)
2402 yaffsfs_SetError(-ENOTDIR);
2404 yaffsfs_SetError(-ELOOP);
2406 yaffsfs_SetError(-ENOENT);
2407 else if (yaffsfs_TooManyObjects(parent->my_dev))
2408 yaffsfs_SetError(-ENFILE);
2409 else if (yaffs_strnlen(name, 5) == 0) {
2410 /* Trying to make the root itself */
2411 yaffsfs_SetError(-EEXIST);
2412 } else if (parent->my_dev->read_only)
2413 yaffsfs_SetError(-EROFS);
2415 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2418 else if (yaffs_find_by_name(parent, name))
2419 yaffsfs_SetError(-EEXIST); /* name exists */
2421 yaffsfs_SetError(-ENOSPC); /* assume no space */
2431 int yaffs_rmdir(const YCHAR *path)
2436 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2437 yaffsfs_SetError(-EFAULT);
2441 if (yaffsfs_CheckPath(path) < 0) {
2442 yaffsfs_SetError(-ENAMETOOLONG);
2446 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2447 yaffsfs_SetError(-ENOMEM);
2452 result = yaffsfs_DoUnlink(path, 1);
2459 void *yaffs_getdev(const YCHAR *path)
2461 struct yaffs_dev *dev = NULL;
2463 dev = yaffsfs_FindDevice(path, &dummy);
2467 int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
2470 int result = YAFFS_FAIL;
2471 struct yaffs_dev *dev = NULL;
2473 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2474 yaffsfs_SetError(-EFAULT);
2478 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2480 if (yaffsfs_CheckPath(path) < 0) {
2481 yaffsfs_SetError(-ENAMETOOLONG);
2487 yaffsfs_InitHandles();
2489 dev = yaffsfs_FindMountPoint(path);
2491 if (!dev->is_mounted) {
2492 dev->read_only = read_only ? 1 : 0;
2494 u8 skip = dev->param.skip_checkpt_rd;
2495 dev->param.skip_checkpt_rd = 1;
2496 result = yaffs_guts_initialise(dev);
2497 dev->param.skip_checkpt_rd = skip;
2499 result = yaffs_guts_initialise(dev);
2502 if (result == YAFFS_FAIL)
2503 yaffsfs_SetError(-ENOMEM);
2504 retVal = result ? 0 : -1;
2507 yaffsfs_SetError(-EBUSY);
2509 yaffsfs_SetError(-ENODEV);
2516 int yaffs_mount2(const YCHAR *path, int readonly)
2518 return yaffs_mount_common(path, readonly, 0);
2521 int yaffs_mount(const YCHAR *path)
2523 return yaffs_mount_common(path, 0, 0);
2526 int yaffs_sync(const YCHAR *path)
2529 struct yaffs_dev *dev = NULL;
2532 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2533 yaffsfs_SetError(-EFAULT);
2537 if (yaffsfs_CheckPath(path) < 0) {
2538 yaffsfs_SetError(-ENAMETOOLONG);
2543 dev = yaffsfs_FindDevice(path, &dummy);
2545 if (!dev->is_mounted)
2546 yaffsfs_SetError(-EINVAL);
2547 else if (dev->read_only)
2548 yaffsfs_SetError(-EROFS);
2551 yaffs_flush_whole_cache(dev);
2552 yaffs_checkpoint_save(dev);
2557 yaffsfs_SetError(-ENODEV);
2563 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2566 struct yaffs_obj *obj;
2568 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2569 obj = yaffsfs_HandleToObject(i);
2570 if (obj && obj->my_dev == dev)
2576 int yaffs_remount(const YCHAR *path, int force, int read_only)
2579 struct yaffs_dev *dev = NULL;
2581 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2582 yaffsfs_SetError(-EFAULT);
2586 if (yaffsfs_CheckPath(path) < 0) {
2587 yaffsfs_SetError(-ENAMETOOLONG);
2592 dev = yaffsfs_FindMountPoint(path);
2594 if (dev->is_mounted) {
2595 yaffs_flush_whole_cache(dev);
2597 if (force || !yaffsfs_IsDevBusy(dev)) {
2599 yaffs_checkpoint_save(dev);
2600 dev->read_only = read_only ? 1 : 0;
2603 yaffsfs_SetError(-EBUSY);
2606 yaffsfs_SetError(-EINVAL);
2609 yaffsfs_SetError(-ENODEV);
2616 int yaffs_unmount2(const YCHAR *path, int force)
2619 struct yaffs_dev *dev = NULL;
2621 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2622 yaffsfs_SetError(-EFAULT);
2626 if (yaffsfs_CheckPath(path) < 0) {
2627 yaffsfs_SetError(-ENAMETOOLONG);
2632 dev = yaffsfs_FindMountPoint(path);
2634 if (dev->is_mounted) {
2636 yaffs_flush_whole_cache(dev);
2637 yaffs_checkpoint_save(dev);
2638 inUse = yaffsfs_IsDevBusy(dev);
2639 if (!inUse || force) {
2641 yaffsfs_BreakDeviceHandles(dev);
2642 yaffs_deinitialise(dev);
2646 yaffsfs_SetError(-EBUSY);
2649 yaffsfs_SetError(-EINVAL);
2652 yaffsfs_SetError(-ENODEV);
2659 int yaffs_unmount(const YCHAR *path)
2661 return yaffs_unmount2(path, 0);
2664 Y_LOFF_T yaffs_freespace(const YCHAR *path)
2666 Y_LOFF_T retVal = -1;
2667 struct yaffs_dev *dev = NULL;
2670 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2671 yaffsfs_SetError(-EFAULT);
2675 if (yaffsfs_CheckPath(path) < 0) {
2676 yaffsfs_SetError(-ENAMETOOLONG);
2681 dev = yaffsfs_FindDevice(path, &dummy);
2682 if (dev && dev->is_mounted) {
2683 retVal = yaffs_get_n_free_chunks(dev);
2684 retVal *= dev->data_bytes_per_chunk;
2687 yaffsfs_SetError(-EINVAL);
2693 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
2695 Y_LOFF_T retVal = -1;
2696 struct yaffs_dev *dev = NULL;
2699 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2700 yaffsfs_SetError(-EFAULT);
2704 if (yaffsfs_CheckPath(path) < 0) {
2705 yaffsfs_SetError(-ENAMETOOLONG);
2710 dev = yaffsfs_FindDevice(path, &dummy);
2711 if (dev && dev->is_mounted) {
2712 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2713 dev->param.n_reserved_blocks;
2714 retVal *= dev->param.chunks_per_block;
2715 retVal *= dev->data_bytes_per_chunk;
2718 yaffsfs_SetError(-EINVAL);
2724 int yaffs_inodecount(const YCHAR *path)
2726 Y_LOFF_T retVal = -1;
2727 struct yaffs_dev *dev = NULL;
2730 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2731 yaffsfs_SetError(-EFAULT);
2735 if (yaffsfs_CheckPath(path) < 0) {
2736 yaffsfs_SetError(-ENAMETOOLONG);
2741 dev = yaffsfs_FindDevice(path, &dummy);
2742 if (dev && dev->is_mounted) {
2743 int n_obj = dev->n_obj;
2744 if (n_obj > dev->n_hardlinks)
2745 retVal = n_obj - dev->n_hardlinks;
2749 yaffsfs_SetError(-EINVAL);
2755 void yaffs_add_device(struct yaffs_dev *dev)
2757 struct list_head *cfg;
2758 /* First check that the device is not in the list. */
2760 list_for_each(cfg, &yaffsfs_deviceList) {
2761 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
2765 dev->is_mounted = 0;
2766 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2768 if (!dev->dev_list.next)
2769 INIT_LIST_HEAD(&dev->dev_list);
2771 list_add(&dev->dev_list, &yaffsfs_deviceList);
2774 void yaffs_remove_device(struct yaffs_dev *dev)
2776 list_del_init(&dev->dev_list);
2779 /* Functions to iterate through devices. NB Use with extreme care! */
2781 static struct list_head *dev_iterator;
2782 void yaffs_dev_rewind(void)
2784 dev_iterator = yaffsfs_deviceList.next;
2787 struct yaffs_dev *yaffs_next_dev(void)
2789 struct yaffs_dev *retval;
2793 if (dev_iterator == &yaffsfs_deviceList)
2796 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2797 dev_iterator = dev_iterator->next;
2801 /* Directory search stuff. */
2803 static struct list_head search_contexts;
2805 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContxt *dsc)
2809 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2813 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
2814 dsc->nextReturn = NULL;
2817 list_entry(dsc->dirObj->variant.dir_variant.
2818 children.next, struct yaffs_obj,
2821 /* Hey someone isn't playing nice! */
2825 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContxt *dsc)
2829 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2831 if (dsc->nextReturn == NULL ||
2832 list_empty(&dsc->dirObj->variant.dir_variant.children))
2833 dsc->nextReturn = NULL;
2835 struct list_head *next = dsc->nextReturn->siblings.next;
2837 if (next == &dsc->dirObj->variant.dir_variant.children)
2838 dsc->nextReturn = NULL; /* end of list */
2840 dsc->nextReturn = list_entry(next,
2845 /* Hey someone isn't playing nice! */
2849 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2852 struct list_head *i;
2853 struct yaffsfs_DirSearchContxt *dsc;
2855 /* if search contexts not initilised then skip */
2856 if (!search_contexts.next)
2859 /* Iterate through the directory search contexts.
2860 * If any are the one being removed, then advance the dsc to
2861 * the next one to prevent a hanging ptr.
2863 list_for_each(i, &search_contexts) {
2865 dsc = list_entry(i, struct yaffsfs_DirSearchContxt,
2867 if (dsc->nextReturn == obj)
2868 yaffsfs_DirAdvance(dsc);
2874 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2876 yaffs_DIR *dir = NULL;
2877 struct yaffs_obj *obj = NULL;
2878 struct yaffsfs_DirSearchContxt *dsc = NULL;
2882 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
2883 yaffsfs_SetError(-EFAULT);
2887 if (yaffsfs_CheckPath(dirname) < 0) {
2888 yaffsfs_SetError(-ENAMETOOLONG);
2894 obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, ¬Dir, &loop);
2897 yaffsfs_SetError(-ENOTDIR);
2899 yaffsfs_SetError(-ELOOP);
2901 yaffsfs_SetError(-ENOENT);
2902 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2903 yaffsfs_SetError(-ENOTDIR);
2907 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
2908 if (!yaffsfs_dsc[i].inUse)
2909 dsc = &yaffsfs_dsc[i];
2912 dir = (yaffs_DIR *) dsc;
2915 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContxt));
2918 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
2919 INIT_LIST_HEAD(&dsc->others);
2921 if (!search_contexts.next)
2922 INIT_LIST_HEAD(&search_contexts);
2924 list_add(&dsc->others, &search_contexts);
2925 yaffsfs_SetDirRewound(dsc);
2935 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
2937 struct yaffsfs_DirSearchContxt *dsc;
2938 struct yaffs_dirent *retVal = NULL;
2940 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
2943 if (dsc && dsc->inUse) {
2944 yaffsfs_SetError(0);
2945 if (dsc->nextReturn) {
2947 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
2948 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
2949 dsc->de.d_off = dsc->offset++;
2950 yaffs_get_obj_name(dsc->nextReturn,
2951 dsc->de.d_name, NAME_MAX);
2952 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
2953 /* this should not happen! */
2954 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
2956 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
2958 yaffsfs_DirAdvance(dsc);
2962 yaffsfs_SetError(-EBADF);
2970 void yaffs_rewinddir(yaffs_DIR *dirp)
2972 struct yaffsfs_DirSearchContxt *dsc;
2974 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
2976 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
2981 yaffsfs_SetDirRewound(dsc);
2986 int yaffs_closedir(yaffs_DIR *dirp)
2988 struct yaffsfs_DirSearchContxt *dsc;
2990 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
2992 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
2993 yaffsfs_SetError(-EFAULT);
2999 list_del(&dsc->others); /* unhook from list */
3004 /* End of directory stuff */
3006 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3008 struct yaffs_obj *parent = NULL;
3009 struct yaffs_obj *obj;
3012 int mode = 0; /* ignore for now */
3016 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3017 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3018 yaffsfs_SetError(-EFAULT);
3022 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3023 yaffsfs_SetError(-ENAMETOOLONG);
3028 parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, ¬Dir, &loop);
3029 if (!parent && notDir)
3030 yaffsfs_SetError(-ENOTDIR);
3032 yaffsfs_SetError(-ELOOP);
3033 else if (!parent || yaffs_strnlen(name, 5) < 1)
3034 yaffsfs_SetError(-ENOENT);
3035 else if (yaffsfs_TooManyObjects(parent->my_dev))
3036 yaffsfs_SetError(-ENFILE);
3037 else if (parent->my_dev->read_only)
3038 yaffsfs_SetError(-EROFS);
3040 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3043 else if (yaffsfs_FindObject
3044 (NULL, newpath, 0, 0, NULL, NULL, NULL))
3045 yaffsfs_SetError(-EEXIST);
3047 yaffsfs_SetError(-ENOSPC);
3056 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3058 struct yaffs_obj *obj = NULL;
3059 struct yaffs_obj *dir = NULL;
3064 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3065 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3066 yaffsfs_SetError(-EFAULT);
3072 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
3075 yaffsfs_SetError(-ENOTDIR);
3077 yaffsfs_SetError(-ELOOP);
3078 else if (!dir || !obj)
3079 yaffsfs_SetError(-ENOENT);
3080 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3081 yaffsfs_SetError(-EINVAL);
3083 YCHAR *alias = obj->variant.symlink_variant.alias;
3084 memset(buf, 0, bufsiz);
3085 yaffs_strncpy(buf, alias, bufsiz - 1);
3092 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3094 /* Creates a link called newpath to existing oldpath */
3095 struct yaffs_obj *obj = NULL;
3096 struct yaffs_obj *lnk = NULL;
3097 struct yaffs_obj *obj_dir = NULL;
3098 struct yaffs_obj *lnk_dir = NULL;
3106 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3107 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3108 yaffsfs_SetError(-EFAULT);
3112 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3113 yaffsfs_SetError(-ENAMETOOLONG);
3119 obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
3120 &obj_dir, ¬DirObj, &objLoop);
3121 lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
3122 lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
3123 0, ¬DirLnk, &lnkLoop);
3125 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3126 yaffsfs_SetError(-ENOTDIR);
3127 else if (objLoop || lnkLoop)
3128 yaffsfs_SetError(-ELOOP);
3129 else if (!obj_dir || !lnk_dir || !obj)
3130 yaffsfs_SetError(-ENOENT);
3131 else if (obj->my_dev->read_only)
3132 yaffsfs_SetError(-EROFS);
3133 else if (yaffsfs_TooManyObjects(obj->my_dev))
3134 yaffsfs_SetError(-ENFILE);
3136 yaffsfs_SetError(-EEXIST);
3137 else if (lnk_dir->my_dev != obj->my_dev)
3138 yaffsfs_SetError(-EXDEV);
3140 retVal = yaffsfs_CheckNameLength(newname);
3143 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3147 yaffsfs_SetError(-ENOSPC);
3157 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3163 yaffsfs_SetError(-EINVAL);
3168 * D E B U G F U N C T I O N S
3173 * Returns number of handles attached to the object
3175 int yaffs_n_handles(const YCHAR *path)
3177 struct yaffs_obj *obj;
3179 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3180 yaffsfs_SetError(-EFAULT);
3184 if (yaffsfs_CheckPath(path) < 0) {
3185 yaffsfs_SetError(-ENAMETOOLONG);
3189 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
3192 return yaffsfs_CountHandles(obj);
3197 int yaffs_get_error(void)
3199 return yaffsfs_GetLastError();
3202 int yaffs_set_error(int error)
3204 yaffsfs_SetError(error);
3208 int yaffs_dump_dev(const YCHAR *path)
3215 struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
3218 struct yaffs_dev *dev = obj->my_dev;
3221 "n_page_writes.......... %d\n"
3222 "n_page_reads........... %d\n"
3223 "n_erasures....... %d\n"
3224 "n_gc_copies............ %d\n"
3225 "garbageCollections... %d\n"
3226 "passiveGarbageColl'ns %d\n"
3232 dev->garbageCollections, dev->passiveGarbageCollections);