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 int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
1487 struct yaffs_obj *olddir = NULL;
1488 struct yaffs_obj *newdir = NULL;
1489 struct yaffs_obj *obj = NULL;
1490 struct yaffs_obj *newobj = NULL;
1493 int result = YAFFS_FAIL;
1494 int rename_allowed = 1;
1500 YCHAR *alt_newpath = NULL;
1502 if (yaffsfs_CheckMemRegion(oldPath, 0, 0) < 0 ||
1503 yaffsfs_CheckMemRegion(newPath, 0, 0) < 0) {
1504 yaffsfs_SetError(-EFAULT);
1508 if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
1509 yaffsfs_SetError(-ENAMETOOLONG);
1513 if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
1514 yaffsfs_SetError(-ENOMEM);
1518 newPath = alt_newpath;
1522 olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
1523 ¬OldDir, &oldLoop);
1524 newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
1525 ¬NewDir, &newLoop);
1526 obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
1527 newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
1529 /* If the object being renamed is a directory and the
1530 * path ended with a "/" then the olddir == obj.
1531 * We pass through NULL for the old name to tell the lower layers
1532 * to use olddir as the object.
1538 if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
1539 yaffsfs_SetError(-ENOTDIR);
1541 } else if (oldLoop || newLoop) {
1542 yaffsfs_SetError(-ELOOP);
1544 } else if (olddir && oldname &&
1545 yaffs_strncmp(oldname, _Y("."), 2) == 0) {
1546 yaffsfs_SetError(-EINVAL);
1548 } else if (!olddir || !newdir || !obj) {
1549 yaffsfs_SetError(-ENOENT);
1551 } else if (obj->my_dev->read_only) {
1552 yaffsfs_SetError(-EROFS);
1554 } else if (yaffs_is_non_empty_dir(newobj)) {
1555 yaffsfs_SetError(-ENOTEMPTY);
1557 } else if (olddir->my_dev != newdir->my_dev) {
1558 /* Rename must be on same device */
1559 yaffsfs_SetError(-EXDEV);
1561 } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
1563 * It is a directory, check that it is not being renamed to
1564 * being its own decendent.
1565 * Do this by tracing from the new directory back to the root,
1569 struct yaffs_obj *xx = newdir;
1571 while (rename_allowed && xx) {
1576 if (!rename_allowed)
1577 yaffsfs_SetError(-EINVAL);
1581 result = yaffs_rename_obj(olddir, oldname, newdir, newname);
1587 return (result == YAFFS_FAIL) ? -1 : 0;
1590 static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
1594 obj = yaffs_get_equivalent_obj(obj);
1597 buf->st_dev = (int)obj->my_dev->os_context;
1598 buf->st_ino = obj->obj_id;
1599 buf->st_mode = obj->yst_mode & ~S_IFMT;
1601 if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
1602 buf->st_mode |= S_IFDIR;
1603 else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
1604 buf->st_mode |= S_IFLNK;
1605 else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
1606 buf->st_mode |= S_IFREG;
1608 buf->st_nlink = yaffs_get_obj_link_count(obj);
1611 buf->st_rdev = obj->yst_rdev;
1612 buf->st_size = yaffs_get_obj_length(obj);
1613 buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
1614 buf->st_blocks = (buf->st_size + buf->st_blksize - 1) /
1616 #if CONFIG_YAFFS_WINCE
1617 buf->yst_wince_atime[0] = obj->win_atime[0];
1618 buf->yst_wince_atime[1] = obj->win_atime[1];
1619 buf->yst_wince_ctime[0] = obj->win_ctime[0];
1620 buf->yst_wince_ctime[1] = obj->win_ctime[1];
1621 buf->yst_wince_mtime[0] = obj->win_mtime[0];
1622 buf->yst_wince_mtime[1] = obj->win_mtime[1];
1624 buf->yst_atime = obj->yst_atime;
1625 buf->yst_ctime = obj->yst_ctime;
1626 buf->yst_mtime = obj->yst_mtime;
1633 static int yaffsfs_DoStatOrLStat(const YCHAR *path,
1634 struct yaffs_stat *buf, int doLStat)
1636 struct yaffs_obj *obj = NULL;
1637 struct yaffs_obj *dir = NULL;
1642 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1643 yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1644 yaffsfs_SetError(-EFAULT);
1648 if (yaffsfs_CheckPath(path) < 0) {
1649 yaffsfs_SetError(-ENAMETOOLONG);
1655 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1657 if (!doLStat && obj)
1658 obj = yaffsfs_FollowLink(obj, 0, &loop);
1661 yaffsfs_SetError(-ENOTDIR);
1663 yaffsfs_SetError(-ELOOP);
1664 else if (!dir || !obj)
1665 yaffsfs_SetError(-ENOENT);
1667 retVal = yaffsfs_DoStat(obj, buf);
1675 int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
1677 return yaffsfs_DoStatOrLStat(path, buf, 0);
1680 int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
1682 return yaffsfs_DoStatOrLStat(path, buf, 1);
1685 int yaffs_fstat(int fd, struct yaffs_stat *buf)
1687 struct yaffs_obj *obj;
1691 if (yaffsfs_CheckMemRegion(buf, sizeof(*buf), 1) < 0) {
1692 yaffsfs_SetError(-EFAULT);
1697 obj = yaffsfs_HandleToObject(fd);
1700 retVal = yaffsfs_DoStat(obj, buf);
1703 yaffsfs_SetError(-EBADF);
1710 static int yaffsfs_DoUtime(struct yaffs_obj *obj,
1711 const struct yaffs_utimbuf *buf)
1716 struct yaffs_utimbuf local;
1718 obj = yaffs_get_equivalent_obj(obj);
1720 if (obj && obj->my_dev->read_only) {
1721 yaffsfs_SetError(-EROFS);
1726 local.actime = Y_CURRENT_TIME;
1727 local.modtime = local.actime;
1732 obj->yst_atime = buf->actime;
1733 obj->yst_mtime = buf->modtime;
1735 result = yaffs_flush_file(obj, 0, 0);
1736 retVal = result == YAFFS_OK ? 0 : -1;
1742 int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
1744 struct yaffs_obj *obj = NULL;
1745 struct yaffs_obj *dir = NULL;
1751 yaffsfs_SetError(-EFAULT);
1755 if (yaffsfs_CheckPath(path) < 0) {
1756 yaffsfs_SetError(-ENAMETOOLONG);
1762 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1765 yaffsfs_SetError(-ENOTDIR);
1767 yaffsfs_SetError(-ELOOP);
1768 else if (!dir || !obj)
1769 yaffsfs_SetError(-ENOENT);
1771 retVal = yaffsfs_DoUtime(obj, buf);
1779 int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
1781 struct yaffs_obj *obj;
1786 obj = yaffsfs_HandleToObject(fd);
1789 retVal = yaffsfs_DoUtime(obj, buf);
1792 yaffsfs_SetError(-EBADF);
1799 #ifndef CONFIG_YAFFS_WINCE
1800 /* xattrib functions */
1802 static int yaffs_do_setxattr(const YCHAR *path, const char *name,
1803 const void *data, int size, int flags, int follow)
1805 struct yaffs_obj *obj;
1806 struct yaffs_obj *dir;
1812 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1813 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1814 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1815 yaffsfs_SetError(-EFAULT);
1819 if (yaffsfs_CheckPath(path) < 0) {
1820 yaffsfs_SetError(-ENAMETOOLONG);
1826 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1829 obj = yaffsfs_FollowLink(obj, 0, &loop);
1832 yaffsfs_SetError(-ENOTDIR);
1834 yaffsfs_SetError(-ELOOP);
1835 else if (!dir || !obj)
1836 yaffsfs_SetError(-ENOENT);
1838 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1840 yaffsfs_SetError(retVal);
1851 int yaffs_setxattr(const YCHAR *path, const char *name,
1852 const void *data, int size, int flags)
1854 return yaffs_do_setxattr(path, name, data, size, flags, 1);
1857 int yaffs_lsetxattr(const YCHAR *path, const char *name,
1858 const void *data, int size, int flags)
1860 return yaffs_do_setxattr(path, name, data, size, flags, 0);
1863 int yaffs_fsetxattr(int fd, const char *name,
1864 const void *data, int size, int flags)
1866 struct yaffs_obj *obj;
1870 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1871 yaffsfs_CheckMemRegion(data, size, 0) < 0) {
1872 yaffsfs_SetError(-EFAULT);
1877 obj = yaffsfs_HandleToObject(fd);
1880 yaffsfs_SetError(-EBADF);
1882 retVal = yaffs_set_xattrib(obj, name, data, size, flags);
1884 yaffsfs_SetError(retVal);
1894 static int yaffs_do_getxattr(const YCHAR *path, const char *name,
1895 void *data, int size, int follow)
1897 struct yaffs_obj *obj;
1898 struct yaffs_obj *dir;
1903 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1904 yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1905 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1906 yaffsfs_SetError(-EFAULT);
1910 if (yaffsfs_CheckPath(path) < 0) {
1911 yaffsfs_SetError(-ENAMETOOLONG);
1917 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
1920 obj = yaffsfs_FollowLink(obj, 0, &loop);
1923 yaffsfs_SetError(-ENOTDIR);
1925 yaffsfs_SetError(-ELOOP);
1926 else if (!dir || !obj)
1927 yaffsfs_SetError(-ENOENT);
1929 retVal = yaffs_get_xattrib(obj, name, data, size);
1931 yaffsfs_SetError(retVal);
1941 int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
1943 return yaffs_do_getxattr(path, name, data, size, 1);
1946 int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
1948 return yaffs_do_getxattr(path, name, data, size, 0);
1951 int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
1953 struct yaffs_obj *obj;
1957 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0 ||
1958 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1959 yaffsfs_SetError(-EFAULT);
1964 obj = yaffsfs_HandleToObject(fd);
1967 retVal = yaffs_get_xattrib(obj, name, data, size);
1969 yaffsfs_SetError(retVal);
1974 yaffsfs_SetError(-EBADF);
1981 static int yaffs_do_listxattr(const YCHAR *path, char *data,
1982 int size, int follow)
1984 struct yaffs_obj *obj = NULL;
1985 struct yaffs_obj *dir = NULL;
1990 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
1991 yaffsfs_CheckMemRegion(data, size, 1) < 0) {
1992 yaffsfs_SetError(-EFAULT);
1996 if (yaffsfs_CheckPath(path) < 0) {
1997 yaffsfs_SetError(-ENAMETOOLONG);
2003 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2006 obj = yaffsfs_FollowLink(obj, 0, &loop);
2009 yaffsfs_SetError(-ENOTDIR);
2011 yaffsfs_SetError(-ELOOP);
2012 else if (!dir || !obj)
2013 yaffsfs_SetError(-ENOENT);
2015 retVal = yaffs_list_xattrib(obj, data, size);
2017 yaffsfs_SetError(retVal);
2028 int yaffs_listxattr(const YCHAR *path, char *data, int size)
2030 return yaffs_do_listxattr(path, data, size, 1);
2033 int yaffs_llistxattr(const YCHAR *path, char *data, int size)
2035 return yaffs_do_listxattr(path, data, size, 0);
2038 int yaffs_flistxattr(int fd, char *data, int size)
2040 struct yaffs_obj *obj;
2044 if (yaffsfs_CheckMemRegion(data, size, 1) < 0) {
2045 yaffsfs_SetError(-EFAULT);
2050 obj = yaffsfs_HandleToObject(fd);
2053 retVal = yaffs_list_xattrib(obj, data, size);
2055 yaffsfs_SetError(retVal);
2060 yaffsfs_SetError(-EBADF);
2067 static int yaffs_do_removexattr(const YCHAR *path, const char *name,
2070 struct yaffs_obj *obj = NULL;
2071 struct yaffs_obj *dir = NULL;
2076 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
2077 yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2078 yaffsfs_SetError(-EFAULT);
2082 if (yaffsfs_CheckPath(path) < 0) {
2083 yaffsfs_SetError(-ENAMETOOLONG);
2089 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2092 obj = yaffsfs_FollowLink(obj, 0, &loop);
2095 yaffsfs_SetError(-ENOTDIR);
2097 yaffsfs_SetError(-ELOOP);
2098 else if (!dir || !obj)
2099 yaffsfs_SetError(-ENOENT);
2101 retVal = yaffs_remove_xattrib(obj, name);
2103 yaffsfs_SetError(retVal);
2114 int yaffs_removexattr(const YCHAR *path, const char *name)
2116 return yaffs_do_removexattr(path, name, 1);
2119 int yaffs_lremovexattr(const YCHAR *path, const char *name)
2121 return yaffs_do_removexattr(path, name, 0);
2124 int yaffs_fremovexattr(int fd, const char *name)
2126 struct yaffs_obj *obj;
2130 if (yaffsfs_CheckMemRegion(name, 0, 0) < 0) {
2131 yaffsfs_SetError(-EFAULT);
2136 obj = yaffsfs_HandleToObject(fd);
2139 retVal = yaffs_remove_xattrib(obj, name);
2141 yaffsfs_SetError(retVal);
2146 yaffsfs_SetError(-EBADF);
2154 #ifdef CONFIG_YAFFS_WINCE
2155 int yaffs_get_wince_times(int fd, unsigned *wctime,
2156 unsigned *watime, unsigned *wmtime)
2158 struct yaffs_obj *obj;
2163 obj = yaffsfs_HandleToObject(fd);
2168 wctime[0] = obj->win_ctime[0];
2169 wctime[1] = obj->win_ctime[1];
2172 watime[0] = obj->win_atime[0];
2173 watime[1] = obj->win_atime[1];
2176 wmtime[0] = obj->win_mtime[0];
2177 wmtime[1] = obj->win_mtime[1];
2183 yaffsfs_SetError(-EBADF);
2190 int yaffs_set_wince_times(int fd,
2191 const unsigned *wctime,
2192 const unsigned *watime, const unsigned *wmtime)
2194 struct yaffs_obj *obj;
2199 obj = yaffsfs_HandleToObject(fd);
2204 obj->win_ctime[0] = wctime[0];
2205 obj->win_ctime[1] = wctime[1];
2208 obj->win_atime[0] = watime[0];
2209 obj->win_atime[1] = watime[1];
2212 obj->win_mtime[0] = wmtime[0];
2213 obj->win_mtime[1] = wmtime[1];
2217 result = yaffs_flush_file(obj, 0, 0);
2221 yaffsfs_SetError(-EBADF);
2230 static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
2235 obj = yaffs_get_equivalent_obj(obj);
2238 obj->yst_mode = mode;
2240 result = yaffs_flush_file(obj, 0, 0);
2243 return result == YAFFS_OK ? 0 : -1;
2246 int yaffs_access(const YCHAR *path, int amode)
2248 struct yaffs_obj *obj = NULL;
2249 struct yaffs_obj *dir = NULL;
2254 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2255 yaffsfs_SetError(-EFAULT);
2259 if (yaffsfs_CheckPath(path) < 0) {
2260 yaffsfs_SetError(-ENAMETOOLONG);
2264 if (amode & ~(R_OK | W_OK | X_OK)) {
2265 yaffsfs_SetError(-EINVAL);
2271 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2272 obj = yaffsfs_FollowLink(obj, 0, &loop);
2275 yaffsfs_SetError(-ENOTDIR);
2277 yaffsfs_SetError(-ELOOP);
2278 else if (!dir || !obj)
2279 yaffsfs_SetError(-ENOENT);
2280 else if ((amode & W_OK) && obj->my_dev->read_only)
2281 yaffsfs_SetError(-EROFS);
2285 if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
2287 if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
2289 if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
2293 yaffsfs_SetError(-EACCES);
2304 int yaffs_chmod(const YCHAR *path, mode_t mode)
2306 struct yaffs_obj *obj = NULL;
2307 struct yaffs_obj *dir = NULL;
2312 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2313 yaffsfs_SetError(-EFAULT);
2317 if (yaffsfs_CheckPath(path) < 0) {
2318 yaffsfs_SetError(-ENAMETOOLONG);
2322 if (mode & ~(0777)) {
2323 yaffsfs_SetError(-EINVAL);
2329 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
2330 obj = yaffsfs_FollowLink(obj, 0, &loop);
2333 yaffsfs_SetError(-ENOTDIR);
2335 yaffsfs_SetError(-ELOOP);
2336 else if (!dir || !obj)
2337 yaffsfs_SetError(-ENOENT);
2338 else if (obj->my_dev->read_only)
2339 yaffsfs_SetError(-EROFS);
2341 retVal = yaffsfs_DoChMod(obj, mode);
2349 int yaffs_fchmod(int fd, mode_t mode)
2351 struct yaffs_obj *obj;
2354 if (mode & ~(0777)) {
2355 yaffsfs_SetError(-EINVAL);
2360 obj = yaffsfs_HandleToObject(fd);
2363 yaffsfs_SetError(-EBADF);
2364 else if (obj->my_dev->read_only)
2365 yaffsfs_SetError(-EROFS);
2367 retVal = yaffsfs_DoChMod(obj, mode);
2374 int yaffs_mkdir(const YCHAR *path, mode_t mode)
2376 struct yaffs_obj *parent = NULL;
2377 struct yaffs_obj *dir = NULL;
2379 YCHAR *alt_path = NULL;
2384 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2385 yaffsfs_SetError(-EFAULT);
2389 if (yaffsfs_CheckPath(path) < 0) {
2390 yaffsfs_SetError(-ENAMETOOLONG);
2394 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2395 yaffsfs_SetError(-ENOMEM);
2402 parent = yaffsfs_FindDirectory(NULL, path, &name, 0, ¬Dir, &loop);
2403 if (!parent && notDir)
2404 yaffsfs_SetError(-ENOTDIR);
2406 yaffsfs_SetError(-ELOOP);
2408 yaffsfs_SetError(-ENOENT);
2409 else if (yaffsfs_TooManyObjects(parent->my_dev))
2410 yaffsfs_SetError(-ENFILE);
2411 else if (yaffs_strnlen(name, 5) == 0) {
2412 /* Trying to make the root itself */
2413 yaffsfs_SetError(-EEXIST);
2414 } else if (parent->my_dev->read_only)
2415 yaffsfs_SetError(-EROFS);
2417 dir = yaffs_create_dir(parent, name, mode, 0, 0);
2420 else if (yaffs_find_by_name(parent, name))
2421 yaffsfs_SetError(-EEXIST); /* name exists */
2423 yaffsfs_SetError(-ENOSPC); /* assume no space */
2433 int yaffs_rmdir(const YCHAR *path)
2438 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2439 yaffsfs_SetError(-EFAULT);
2443 if (yaffsfs_CheckPath(path) < 0) {
2444 yaffsfs_SetError(-ENAMETOOLONG);
2448 if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
2449 yaffsfs_SetError(-ENOMEM);
2454 result = yaffsfs_DoUnlink(path, 1);
2461 void *yaffs_getdev(const YCHAR *path)
2463 struct yaffs_dev *dev = NULL;
2465 dev = yaffsfs_FindDevice(path, &dummy);
2469 int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
2472 int result = YAFFS_FAIL;
2473 struct yaffs_dev *dev = NULL;
2475 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2476 yaffsfs_SetError(-EFAULT);
2480 yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
2482 if (yaffsfs_CheckPath(path) < 0) {
2483 yaffsfs_SetError(-ENAMETOOLONG);
2489 yaffsfs_InitHandles();
2491 dev = yaffsfs_FindMountPoint(path);
2493 if (!dev->is_mounted) {
2494 dev->read_only = read_only ? 1 : 0;
2496 u8 skip = dev->param.skip_checkpt_rd;
2497 dev->param.skip_checkpt_rd = 1;
2498 result = yaffs_guts_initialise(dev);
2499 dev->param.skip_checkpt_rd = skip;
2501 result = yaffs_guts_initialise(dev);
2504 if (result == YAFFS_FAIL)
2505 yaffsfs_SetError(-ENOMEM);
2506 retVal = result ? 0 : -1;
2509 yaffsfs_SetError(-EBUSY);
2511 yaffsfs_SetError(-ENODEV);
2518 int yaffs_mount2(const YCHAR *path, int readonly)
2520 return yaffs_mount_common(path, readonly, 0);
2523 int yaffs_mount(const YCHAR *path)
2525 return yaffs_mount_common(path, 0, 0);
2528 int yaffs_sync(const YCHAR *path)
2531 struct yaffs_dev *dev = NULL;
2534 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2535 yaffsfs_SetError(-EFAULT);
2539 if (yaffsfs_CheckPath(path) < 0) {
2540 yaffsfs_SetError(-ENAMETOOLONG);
2545 dev = yaffsfs_FindDevice(path, &dummy);
2547 if (!dev->is_mounted)
2548 yaffsfs_SetError(-EINVAL);
2549 else if (dev->read_only)
2550 yaffsfs_SetError(-EROFS);
2553 yaffs_flush_whole_cache(dev);
2554 yaffs_checkpoint_save(dev);
2559 yaffsfs_SetError(-ENODEV);
2565 static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
2568 struct yaffs_obj *obj;
2570 for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
2571 obj = yaffsfs_HandleToObject(i);
2572 if (obj && obj->my_dev == dev)
2578 int yaffs_remount(const YCHAR *path, int force, int read_only)
2581 struct yaffs_dev *dev = NULL;
2583 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2584 yaffsfs_SetError(-EFAULT);
2588 if (yaffsfs_CheckPath(path) < 0) {
2589 yaffsfs_SetError(-ENAMETOOLONG);
2594 dev = yaffsfs_FindMountPoint(path);
2596 if (dev->is_mounted) {
2597 yaffs_flush_whole_cache(dev);
2599 if (force || !yaffsfs_IsDevBusy(dev)) {
2601 yaffs_checkpoint_save(dev);
2602 dev->read_only = read_only ? 1 : 0;
2605 yaffsfs_SetError(-EBUSY);
2608 yaffsfs_SetError(-EINVAL);
2611 yaffsfs_SetError(-ENODEV);
2618 int yaffs_unmount2(const YCHAR *path, int force)
2621 struct yaffs_dev *dev = NULL;
2623 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2624 yaffsfs_SetError(-EFAULT);
2628 if (yaffsfs_CheckPath(path) < 0) {
2629 yaffsfs_SetError(-ENAMETOOLONG);
2634 dev = yaffsfs_FindMountPoint(path);
2636 if (dev->is_mounted) {
2638 yaffs_flush_whole_cache(dev);
2639 yaffs_checkpoint_save(dev);
2640 inUse = yaffsfs_IsDevBusy(dev);
2641 if (!inUse || force) {
2643 yaffsfs_BreakDeviceHandles(dev);
2644 yaffs_deinitialise(dev);
2648 yaffsfs_SetError(-EBUSY);
2651 yaffsfs_SetError(-EINVAL);
2654 yaffsfs_SetError(-ENODEV);
2661 int yaffs_unmount(const YCHAR *path)
2663 return yaffs_unmount2(path, 0);
2666 int yaffs_format(const YCHAR *path,
2668 int force_unmount_flag,
2672 struct yaffs_dev *dev = NULL;
2676 yaffsfs_SetError(-EFAULT);
2680 if (yaffsfs_CheckPath(path) < 0) {
2681 yaffsfs_SetError(-ENAMETOOLONG);
2686 dev = yaffsfs_FindMountPoint(path);
2689 int was_mounted = dev->is_mounted;
2691 if (dev->is_mounted && unmount_flag) {
2693 yaffs_flush_whole_cache(dev);
2694 yaffs_checkpoint_save(dev);
2695 inUse = yaffsfs_IsDevBusy(dev);
2696 if (!inUse || force_unmount_flag) {
2698 yaffsfs_BreakDeviceHandles(dev);
2699 yaffs_deinitialise(dev);
2703 if(dev->is_mounted) {
2704 yaffsfs_SetError(-EBUSY);
2707 yaffs_format_dev(dev);
2708 if(was_mounted && remount_flag) {
2709 result = yaffs_guts_initialise(dev);
2710 if (result == YAFFS_FAIL) {
2711 yaffsfs_SetError(-ENOMEM);
2717 yaffsfs_SetError(-ENODEV);
2727 Y_LOFF_T yaffs_freespace(const YCHAR *path)
2729 Y_LOFF_T retVal = -1;
2730 struct yaffs_dev *dev = NULL;
2733 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2734 yaffsfs_SetError(-EFAULT);
2738 if (yaffsfs_CheckPath(path) < 0) {
2739 yaffsfs_SetError(-ENAMETOOLONG);
2744 dev = yaffsfs_FindDevice(path, &dummy);
2745 if (dev && dev->is_mounted) {
2746 retVal = yaffs_get_n_free_chunks(dev);
2747 retVal *= dev->data_bytes_per_chunk;
2750 yaffsfs_SetError(-EINVAL);
2756 Y_LOFF_T yaffs_totalspace(const YCHAR *path)
2758 Y_LOFF_T retVal = -1;
2759 struct yaffs_dev *dev = NULL;
2762 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2763 yaffsfs_SetError(-EFAULT);
2767 if (yaffsfs_CheckPath(path) < 0) {
2768 yaffsfs_SetError(-ENAMETOOLONG);
2773 dev = yaffsfs_FindDevice(path, &dummy);
2774 if (dev && dev->is_mounted) {
2775 retVal = (dev->param.end_block - dev->param.start_block + 1) -
2776 dev->param.n_reserved_blocks;
2777 retVal *= dev->param.chunks_per_block;
2778 retVal *= dev->data_bytes_per_chunk;
2781 yaffsfs_SetError(-EINVAL);
2787 int yaffs_inodecount(const YCHAR *path)
2789 Y_LOFF_T retVal = -1;
2790 struct yaffs_dev *dev = NULL;
2793 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
2794 yaffsfs_SetError(-EFAULT);
2798 if (yaffsfs_CheckPath(path) < 0) {
2799 yaffsfs_SetError(-ENAMETOOLONG);
2804 dev = yaffsfs_FindDevice(path, &dummy);
2805 if (dev && dev->is_mounted) {
2806 int n_obj = dev->n_obj;
2807 if (n_obj > dev->n_hardlinks)
2808 retVal = n_obj - dev->n_hardlinks;
2812 yaffsfs_SetError(-EINVAL);
2818 void yaffs_add_device(struct yaffs_dev *dev)
2820 struct list_head *cfg;
2821 /* First check that the device is not in the list. */
2823 list_for_each(cfg, &yaffsfs_deviceList) {
2824 if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
2828 dev->is_mounted = 0;
2829 dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
2831 if (!dev->dev_list.next)
2832 INIT_LIST_HEAD(&dev->dev_list);
2834 list_add(&dev->dev_list, &yaffsfs_deviceList);
2837 void yaffs_remove_device(struct yaffs_dev *dev)
2839 list_del_init(&dev->dev_list);
2842 /* Functions to iterate through devices. NB Use with extreme care! */
2844 static struct list_head *dev_iterator;
2845 void yaffs_dev_rewind(void)
2847 dev_iterator = yaffsfs_deviceList.next;
2850 struct yaffs_dev *yaffs_next_dev(void)
2852 struct yaffs_dev *retval;
2856 if (dev_iterator == &yaffsfs_deviceList)
2859 retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
2860 dev_iterator = dev_iterator->next;
2864 /* Directory search stuff. */
2866 static struct list_head search_contexts;
2868 static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContxt *dsc)
2872 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2876 if (list_empty(&dsc->dirObj->variant.dir_variant.children))
2877 dsc->nextReturn = NULL;
2880 list_entry(dsc->dirObj->variant.dir_variant.
2881 children.next, struct yaffs_obj,
2884 /* Hey someone isn't playing nice! */
2888 static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContxt *dsc)
2892 dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
2894 if (dsc->nextReturn == NULL ||
2895 list_empty(&dsc->dirObj->variant.dir_variant.children))
2896 dsc->nextReturn = NULL;
2898 struct list_head *next = dsc->nextReturn->siblings.next;
2900 if (next == &dsc->dirObj->variant.dir_variant.children)
2901 dsc->nextReturn = NULL; /* end of list */
2903 dsc->nextReturn = list_entry(next,
2908 /* Hey someone isn't playing nice! */
2912 static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
2915 struct list_head *i;
2916 struct yaffsfs_DirSearchContxt *dsc;
2918 /* if search contexts not initilised then skip */
2919 if (!search_contexts.next)
2922 /* Iterate through the directory search contexts.
2923 * If any are the one being removed, then advance the dsc to
2924 * the next one to prevent a hanging ptr.
2926 list_for_each(i, &search_contexts) {
2928 dsc = list_entry(i, struct yaffsfs_DirSearchContxt,
2930 if (dsc->nextReturn == obj)
2931 yaffsfs_DirAdvance(dsc);
2937 yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
2939 yaffs_DIR *dir = NULL;
2940 struct yaffs_obj *obj = NULL;
2941 struct yaffsfs_DirSearchContxt *dsc = NULL;
2945 if (yaffsfs_CheckMemRegion(dirname, 0, 0) < 0) {
2946 yaffsfs_SetError(-EFAULT);
2950 if (yaffsfs_CheckPath(dirname) < 0) {
2951 yaffsfs_SetError(-ENAMETOOLONG);
2957 obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, ¬Dir, &loop);
2960 yaffsfs_SetError(-ENOTDIR);
2962 yaffsfs_SetError(-ELOOP);
2964 yaffsfs_SetError(-ENOENT);
2965 else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
2966 yaffsfs_SetError(-ENOTDIR);
2970 for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
2971 if (!yaffsfs_dsc[i].inUse)
2972 dsc = &yaffsfs_dsc[i];
2975 dir = (yaffs_DIR *) dsc;
2978 memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContxt));
2981 yaffs_strncpy(dsc->name, dirname, NAME_MAX);
2982 INIT_LIST_HEAD(&dsc->others);
2984 if (!search_contexts.next)
2985 INIT_LIST_HEAD(&search_contexts);
2987 list_add(&dsc->others, &search_contexts);
2988 yaffsfs_SetDirRewound(dsc);
2998 struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
3000 struct yaffsfs_DirSearchContxt *dsc;
3001 struct yaffs_dirent *retVal = NULL;
3003 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3006 if (dsc && dsc->inUse) {
3007 yaffsfs_SetError(0);
3008 if (dsc->nextReturn) {
3010 yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
3011 dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
3012 dsc->de.d_off = dsc->offset++;
3013 yaffs_get_obj_name(dsc->nextReturn,
3014 dsc->de.d_name, NAME_MAX);
3015 if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
3016 /* this should not happen! */
3017 yaffs_strcpy(dsc->de.d_name, _Y("zz"));
3019 dsc->de.d_reclen = sizeof(struct yaffs_dirent);
3021 yaffsfs_DirAdvance(dsc);
3025 yaffsfs_SetError(-EBADF);
3033 void yaffs_rewinddir(yaffs_DIR *dirp)
3035 struct yaffsfs_DirSearchContxt *dsc;
3037 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3039 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0)
3044 yaffsfs_SetDirRewound(dsc);
3049 int yaffs_closedir(yaffs_DIR *dirp)
3051 struct yaffsfs_DirSearchContxt *dsc;
3053 dsc = (struct yaffsfs_DirSearchContxt *) dirp;
3055 if (yaffsfs_CheckMemRegion(dirp, sizeof(*dsc), 0) < 0) {
3056 yaffsfs_SetError(-EFAULT);
3062 list_del(&dsc->others); /* unhook from list */
3067 /* End of directory stuff */
3069 int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
3071 struct yaffs_obj *parent = NULL;
3072 struct yaffs_obj *obj;
3075 int mode = 0; /* ignore for now */
3079 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3080 yaffsfs_CheckMemRegion(newpath, 0, 0) < 0) {
3081 yaffsfs_SetError(-EFAULT);
3085 if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3086 yaffsfs_SetError(-ENAMETOOLONG);
3091 parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, ¬Dir, &loop);
3092 if (!parent && notDir)
3093 yaffsfs_SetError(-ENOTDIR);
3095 yaffsfs_SetError(-ELOOP);
3096 else if (!parent || yaffs_strnlen(name, 5) < 1)
3097 yaffsfs_SetError(-ENOENT);
3098 else if (yaffsfs_TooManyObjects(parent->my_dev))
3099 yaffsfs_SetError(-ENFILE);
3100 else if (parent->my_dev->read_only)
3101 yaffsfs_SetError(-EROFS);
3103 obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
3106 else if (yaffsfs_FindObject
3107 (NULL, newpath, 0, 0, NULL, NULL, NULL))
3108 yaffsfs_SetError(-EEXIST);
3110 yaffsfs_SetError(-ENOSPC);
3119 int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
3121 struct yaffs_obj *obj = NULL;
3122 struct yaffs_obj *dir = NULL;
3127 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0 ||
3128 yaffsfs_CheckMemRegion(buf, bufsiz, 1) < 0) {
3129 yaffsfs_SetError(-EFAULT);
3135 obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, ¬Dir, &loop);
3138 yaffsfs_SetError(-ENOTDIR);
3140 yaffsfs_SetError(-ELOOP);
3141 else if (!dir || !obj)
3142 yaffsfs_SetError(-ENOENT);
3143 else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
3144 yaffsfs_SetError(-EINVAL);
3146 YCHAR *alias = obj->variant.symlink_variant.alias;
3147 memset(buf, 0, bufsiz);
3148 yaffs_strncpy(buf, alias, bufsiz - 1);
3155 int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
3157 /* Creates a link called newpath to existing oldpath */
3158 struct yaffs_obj *obj = NULL;
3159 struct yaffs_obj *lnk = NULL;
3160 struct yaffs_obj *obj_dir = NULL;
3161 struct yaffs_obj *lnk_dir = NULL;
3169 if (yaffsfs_CheckMemRegion(oldpath, 0, 0) < 0 ||
3170 yaffsfs_CheckMemRegion(linkpath, 0, 0) < 0) {
3171 yaffsfs_SetError(-EFAULT);
3175 if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
3176 yaffsfs_SetError(-ENAMETOOLONG);
3182 obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
3183 &obj_dir, ¬DirObj, &objLoop);
3184 lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
3185 lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
3186 0, ¬DirLnk, &lnkLoop);
3188 if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
3189 yaffsfs_SetError(-ENOTDIR);
3190 else if (objLoop || lnkLoop)
3191 yaffsfs_SetError(-ELOOP);
3192 else if (!obj_dir || !lnk_dir || !obj)
3193 yaffsfs_SetError(-ENOENT);
3194 else if (obj->my_dev->read_only)
3195 yaffsfs_SetError(-EROFS);
3196 else if (yaffsfs_TooManyObjects(obj->my_dev))
3197 yaffsfs_SetError(-ENFILE);
3199 yaffsfs_SetError(-EEXIST);
3200 else if (lnk_dir->my_dev != obj->my_dev)
3201 yaffsfs_SetError(-EXDEV);
3203 retVal = yaffsfs_CheckNameLength(newname);
3206 lnk = yaffs_link_obj(lnk_dir, newname, obj);
3210 yaffsfs_SetError(-ENOSPC);
3220 int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
3226 yaffsfs_SetError(-EINVAL);
3231 * D E B U G F U N C T I O N S
3236 * Returns number of handles attached to the object
3238 int yaffs_n_handles(const YCHAR *path)
3240 struct yaffs_obj *obj;
3242 if (yaffsfs_CheckMemRegion(path, 0, 0) < 0) {
3243 yaffsfs_SetError(-EFAULT);
3247 if (yaffsfs_CheckPath(path) < 0) {
3248 yaffsfs_SetError(-ENAMETOOLONG);
3252 obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
3255 return yaffsfs_CountHandles(obj);
3260 int yaffs_get_error(void)
3262 return yaffsfs_GetLastError();
3265 int yaffs_set_error(int error)
3267 yaffsfs_SetError(error);
3271 int yaffs_dump_dev(const YCHAR *path)
3278 struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
3281 struct yaffs_dev *dev = obj->my_dev;
3284 "n_page_writes.......... %d\n"
3285 "n_page_reads........... %d\n"
3286 "n_erasures....... %d\n"
3287 "n_gc_copies............ %d\n"
3288 "garbageCollections... %d\n"
3289 "passiveGarbageColl'ns %d\n"
3295 dev->garbageCollections, dev->passiveGarbageCollections);