PageRenderTime 67ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/yaffs2/yaffsfs.c

https://bitbucket.org/hldspb/uboot-sbc8600
C | 3211 lines | 2416 code | 601 blank | 194 comment | 709 complexity | 33fce00db010ea17565c0415b31f9b07 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  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.
  12. */
  13. #include <div64.h>
  14. #include "yaffsfs.h"
  15. #include "yaffs_guts.h"
  16. #include "yaffscfg.h"
  17. #include "yportenv.h"
  18. #include "yaffs_trace.h"
  19. #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
  20. #ifndef NULL
  21. #define NULL ((void *)0)
  22. #endif
  23. /* YAFFSFS_RW_SIZE must be a power of 2 */
  24. #define YAFFSFS_RW_SHIFT (13)
  25. #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
  26. /* Some forward references */
  27. static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
  28. const YCHAR *path,
  29. int symDepth, int getEquiv,
  30. struct yaffs_obj **dirOut,
  31. int *notDir, int *loop);
  32. static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
  33. unsigned int yaffs_wr_attempts;
  34. /*
  35. * Handle management.
  36. * There are open inodes in struct yaffsfs_Inode.
  37. * There are open file descriptors in yaffsfs_FileDes.
  38. * There are open handles in yaffsfs_FileDes.
  39. *
  40. * Things are structured this way to be like the Linux VFS model
  41. * so that interactions with the yaffs guts calls are similar.
  42. * That means more common code paths and less special code.
  43. * That means better testing etc.
  44. *
  45. * We have 3 layers because:
  46. * A handle is different than an fd because you can use dup()
  47. * to create a new handle that accesses the *same* fd. The two
  48. * handles will use the same offset (part of the fd). We only close
  49. * down the fd when there are no more handles accessing it.
  50. *
  51. * More than one fd can currently access one file, but each fd
  52. * has its own permsiions and offset.
  53. */
  54. struct yaffsfs_Inode {
  55. int count; /* Number of handles accessing this inode */
  56. struct yaffs_obj *iObj;
  57. };
  58. struct yaffsfs_FileDes {
  59. u8 reading:1;
  60. u8 writing:1;
  61. u8 append:1;
  62. u8 shareRead:1;
  63. u8 shareWrite:1;
  64. int inodeId:12; /* Index to corresponding yaffsfs_Inode */
  65. int handleCount:10; /* Number of handles for this fd */
  66. loff_t position; /* current position in file */
  67. };
  68. struct yaffsfs_Handle {
  69. short int fdId;
  70. short int useCount;
  71. };
  72. struct yaffsfs_DirSearchContxt {
  73. struct yaffs_dirent de; /* directory entry */
  74. YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
  75. struct yaffs_obj *dirObj; /* ptr to directory being searched */
  76. struct yaffs_obj *nextReturn; /* obj returned by next readddir */
  77. struct list_head others;
  78. int offset:20;
  79. unsigned inUse:1;
  80. };
  81. static struct yaffsfs_DirSearchContxt yaffsfs_dsc[YAFFSFS_N_DSC];
  82. static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
  83. static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
  84. static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
  85. static int yaffsfs_handlesInitialised;
  86. unsigned yaffs_set_trace(unsigned tm)
  87. {
  88. yaffs_trace_mask = tm;
  89. return yaffs_trace_mask;
  90. }
  91. unsigned yaffs_get_trace(void)
  92. {
  93. return yaffs_trace_mask;
  94. }
  95. /*
  96. * yaffsfs_InitHandle
  97. * Inilitalise handle management on start-up.
  98. */
  99. static void yaffsfs_InitHandles(void)
  100. {
  101. int i;
  102. if (yaffsfs_handlesInitialised)
  103. return;
  104. memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
  105. memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
  106. memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
  107. memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
  108. for (i = 0; i < YAFFSFS_N_HANDLES; i++)
  109. yaffsfs_fd[i].inodeId = -1;
  110. for (i = 0; i < YAFFSFS_N_HANDLES; i++)
  111. yaffsfs_handle[i].fdId = -1;
  112. }
  113. static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
  114. {
  115. if (h >= 0 && h < YAFFSFS_N_HANDLES)
  116. return &yaffsfs_handle[h];
  117. return NULL;
  118. }
  119. static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
  120. {
  121. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  122. if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
  123. return &yaffsfs_fd[h->fdId];
  124. return NULL;
  125. }
  126. static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
  127. {
  128. struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
  129. if (fd && fd->handleCount > 0 &&
  130. fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
  131. return &yaffsfs_inode[fd->inodeId];
  132. return NULL;
  133. }
  134. static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
  135. {
  136. struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
  137. if (in)
  138. return in->iObj;
  139. return NULL;
  140. }
  141. /*
  142. * yaffsfs_FindInodeIdForObject
  143. * Find the inode entry for an object, if it exists.
  144. */
  145. static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
  146. {
  147. int i;
  148. int ret = -1;
  149. if (obj)
  150. obj = yaffs_get_equivalent_obj(obj);
  151. /* Look for it in open inode table */
  152. for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
  153. if (yaffsfs_inode[i].iObj == obj)
  154. ret = i;
  155. }
  156. return ret;
  157. }
  158. /*
  159. * yaffsfs_GetInodeIdForObject
  160. * Grab an inode entry when opening a new inode.
  161. */
  162. static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
  163. {
  164. int i;
  165. int ret;
  166. struct yaffsfs_Inode *in = NULL;
  167. if (obj)
  168. obj = yaffs_get_equivalent_obj(obj);
  169. ret = yaffsfs_FindInodeIdForObject(obj);
  170. for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
  171. if (!yaffsfs_inode[i].iObj)
  172. ret = i;
  173. }
  174. if (ret >= 0) {
  175. in = &yaffsfs_inode[ret];
  176. if (!in->iObj)
  177. in->count = 0;
  178. in->iObj = obj;
  179. in->count++;
  180. }
  181. return ret;
  182. }
  183. static int yaffsfs_CountHandles(struct yaffs_obj *obj)
  184. {
  185. int i = yaffsfs_FindInodeIdForObject(obj);
  186. if (i >= 0)
  187. return yaffsfs_inode[i].count;
  188. else
  189. return 0;
  190. }
  191. static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
  192. {
  193. struct yaffs_obj *obj;
  194. obj = in->iObj;
  195. if (obj->unlinked)
  196. yaffs_del_obj(obj);
  197. obj->my_inode = NULL;
  198. in->iObj = NULL;
  199. }
  200. static void yaffsfs_PutInode(int inodeId)
  201. {
  202. if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
  203. struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
  204. in->count--;
  205. if (in->count <= 0) {
  206. yaffsfs_ReleaseInode(in);
  207. in->count = 0;
  208. }
  209. }
  210. }
  211. static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
  212. {
  213. int i;
  214. struct yaffsfs_Handle *h;
  215. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  216. h = &yaffsfs_handle[i];
  217. if (h->useCount < 1) {
  218. memset(h, 0, sizeof(struct yaffsfs_Handle));
  219. h->fdId = -1;
  220. h->useCount = 1;
  221. if (hptr)
  222. *hptr = h;
  223. return i;
  224. }
  225. }
  226. return -1;
  227. }
  228. static int yaffsfs_NewHandleAndFileDes(void)
  229. {
  230. int i;
  231. struct yaffsfs_FileDes *fd;
  232. struct yaffsfs_Handle *h = NULL;
  233. int handle = yaffsfs_NewHandle(&h);
  234. if (handle < 0)
  235. return -1;
  236. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  237. fd = &yaffsfs_fd[i];
  238. if (fd->handleCount < 1) {
  239. memset(fd, 0, sizeof(struct yaffsfs_FileDes));
  240. fd->inodeId = -1;
  241. fd->handleCount = 1;
  242. h->fdId = i;
  243. return handle;
  244. }
  245. }
  246. /* Dump the handle because we could not get a fd */
  247. h->useCount = 0;
  248. return -1;
  249. }
  250. /*
  251. * yaffs_get_handle
  252. * Increase use of handle when reading/writing a file
  253. * Also gets the file descriptor.
  254. */
  255. static int yaffsfs_GetHandle(int handle)
  256. {
  257. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  258. if (h && h->useCount > 0) {
  259. h->useCount++;
  260. return 0;
  261. }
  262. return -1;
  263. }
  264. /*
  265. * yaffs_put_handle
  266. * Let go of a handle when closing a file or aborting an open or
  267. * ending a read or write.
  268. */
  269. static int yaffsfs_PutFileDes(int fdId)
  270. {
  271. struct yaffsfs_FileDes *fd;
  272. if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
  273. fd = &yaffsfs_fd[fdId];
  274. fd->handleCount--;
  275. if (fd->handleCount < 1) {
  276. if (fd->inodeId >= 0) {
  277. yaffsfs_PutInode(fd->inodeId);
  278. fd->inodeId = -1;
  279. }
  280. }
  281. }
  282. return 0;
  283. }
  284. static int yaffsfs_PutHandle(int handle)
  285. {
  286. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  287. if (h && h->useCount > 0) {
  288. h->useCount--;
  289. if (h->useCount < 1) {
  290. yaffsfs_PutFileDes(h->fdId);
  291. h->fdId = -1;
  292. }
  293. }
  294. return 0;
  295. }
  296. static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
  297. {
  298. struct yaffsfs_FileDes *fd;
  299. struct yaffsfs_Handle *h;
  300. struct yaffs_obj *obj;
  301. int i;
  302. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  303. h = yaffsfs_HandleToPointer(i);
  304. fd = yaffsfs_HandleToFileDes(i);
  305. obj = yaffsfs_HandleToObject(i);
  306. if (h && h->useCount > 0) {
  307. h->useCount = 0;
  308. h->fdId = 0;
  309. }
  310. if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
  311. fd->handleCount = 0;
  312. yaffsfs_PutInode(fd->inodeId);
  313. fd->inodeId = -1;
  314. }
  315. }
  316. }
  317. /*
  318. * Stuff to handle names.
  319. */
  320. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  321. static int yaffs_toupper(YCHAR a)
  322. {
  323. if (a >= 'a' && a <= 'z')
  324. return (a - 'a') + 'A';
  325. else
  326. return a;
  327. }
  328. int yaffsfs_Match(YCHAR a, YCHAR b)
  329. {
  330. return (yaffs_toupper(a) == yaffs_toupper(b));
  331. }
  332. #else
  333. int yaffsfs_Match(YCHAR a, YCHAR b)
  334. {
  335. /* case sensitive */
  336. return (a == b);
  337. }
  338. #endif
  339. int yaffsfs_IsPathDivider(YCHAR ch)
  340. {
  341. const YCHAR *str = YAFFS_PATH_DIVIDERS;
  342. while (*str) {
  343. if (*str == ch)
  344. return 1;
  345. str++;
  346. }
  347. return 0;
  348. }
  349. int yaffsfs_CheckNameLength(const char *name)
  350. {
  351. int retVal = 0;
  352. int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
  353. if (nameLength == 0) {
  354. yaffsfs_SetError(-ENOENT);
  355. retVal = -1;
  356. } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
  357. yaffsfs_SetError(-ENAMETOOLONG);
  358. retVal = -1;
  359. }
  360. return retVal;
  361. }
  362. static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
  363. {
  364. YCHAR *alt_path = NULL;
  365. int path_length;
  366. int i;
  367. /*
  368. * We don't have a definition for max path length.
  369. * We will use 3 * max name length instead.
  370. */
  371. *ret_path = NULL;
  372. path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
  373. /* If the last character is a path divider, then we need to
  374. * trim it back so that the name look-up works properly.
  375. * eg. /foo/new_dir/ -> /foo/newdir
  376. * Curveball: Need to handle multiple path dividers:
  377. * eg. /foof/sdfse///// -> /foo/sdfse
  378. */
  379. if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
  380. alt_path = kmalloc(path_length + 1, 0);
  381. if (!alt_path)
  382. return -1;
  383. yaffs_strcpy(alt_path, path);
  384. for (i = path_length - 1;
  385. i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
  386. alt_path[i] = (YCHAR) 0;
  387. }
  388. *ret_path = alt_path;
  389. return 0;
  390. }
  391. LIST_HEAD(yaffsfs_deviceList);
  392. /*
  393. * yaffsfs_FindDevice
  394. * yaffsfs_FindRoot
  395. * Scan the configuration list to find the device
  396. * Curveballs: Should match paths that end in '/' too
  397. * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
  398. */
  399. static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
  400. YCHAR **restOfPath)
  401. {
  402. struct list_head *cfg;
  403. const YCHAR *leftOver;
  404. const YCHAR *p;
  405. struct yaffs_dev *retval = NULL;
  406. struct yaffs_dev *dev = NULL;
  407. int thisMatchLength;
  408. int longestMatch = -1;
  409. int matching;
  410. /*
  411. * Check all configs, choose the one that:
  412. * 1) Actually matches a prefix (ie /a amd /abc will not match
  413. * 2) Matches the longest.
  414. */
  415. list_for_each(cfg, &yaffsfs_deviceList) {
  416. dev = list_entry(cfg, struct yaffs_dev, dev_list);
  417. leftOver = path;
  418. p = dev->param.name;
  419. thisMatchLength = 0;
  420. matching = 1;
  421. while (matching && *p && *leftOver) {
  422. /* Skip over any /s */
  423. while (yaffsfs_IsPathDivider(*p))
  424. p++;
  425. /* Skip over any /s */
  426. while (yaffsfs_IsPathDivider(*leftOver))
  427. leftOver++;
  428. /* Now match the text part */
  429. while (matching &&
  430. *p && !yaffsfs_IsPathDivider(*p) &&
  431. *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
  432. if (yaffsfs_Match(*p, *leftOver)) {
  433. p++;
  434. leftOver++;
  435. thisMatchLength++;
  436. } else {
  437. matching = 0;
  438. }
  439. }
  440. }
  441. /* Skip over any /s in leftOver */
  442. while (yaffsfs_IsPathDivider(*leftOver))
  443. leftOver++;
  444. /*Skip over any /s in p */
  445. while (yaffsfs_IsPathDivider(*p))
  446. p++;
  447. /* p should now be at the end of the string if fully matched */
  448. if (*p)
  449. matching = 0;
  450. if (matching && (thisMatchLength > longestMatch)) {
  451. /* Matched prefix */
  452. *restOfPath = (YCHAR *) leftOver;
  453. retval = dev;
  454. longestMatch = thisMatchLength;
  455. }
  456. }
  457. return retval;
  458. }
  459. static int yaffsfs_CheckPath(const YCHAR *path)
  460. {
  461. int n = 0;
  462. int divs = 0;
  463. while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
  464. if (yaffsfs_IsPathDivider(*path)) {
  465. n = 0;
  466. divs++;
  467. } else
  468. n++;
  469. path++;
  470. }
  471. return (*path) ? -1 : 0;
  472. }
  473. /* FindMountPoint only returns a dev entry if the path is a mount point */
  474. static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
  475. {
  476. struct yaffs_dev *dev;
  477. YCHAR *restOfPath = NULL;
  478. dev = yaffsfs_FindDevice(path, &restOfPath);
  479. if (dev && restOfPath && *restOfPath)
  480. dev = NULL;
  481. return dev;
  482. }
  483. static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
  484. YCHAR **restOfPath)
  485. {
  486. struct yaffs_dev *dev;
  487. dev = yaffsfs_FindDevice(path, restOfPath);
  488. if (dev && dev->is_mounted)
  489. return dev->root_dir;
  490. return NULL;
  491. }
  492. static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
  493. int symDepth, int *loop)
  494. {
  495. if (obj)
  496. obj = yaffs_get_equivalent_obj(obj);
  497. while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  498. YCHAR *alias = obj->variant.symlink_variant.alias;
  499. if (yaffsfs_IsPathDivider(*alias))
  500. /* Starts with a /, need to scan from root up */
  501. obj = yaffsfs_FindObject(NULL, alias, symDepth++,
  502. 1, NULL, NULL, loop);
  503. else
  504. /*
  505. * Relative to here so use the parent of the
  506. * symlink as a start
  507. */
  508. obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
  509. 1, NULL, NULL, loop);
  510. }
  511. return obj;
  512. }
  513. /*
  514. * yaffsfs_FindDirectory
  515. * Parse a path to determine the directory and the name within the directory.
  516. *
  517. * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
  518. */
  519. static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
  520. const YCHAR *path,
  521. YCHAR **name, int symDepth,
  522. int *notDir, int *loop)
  523. {
  524. struct yaffs_obj *dir;
  525. YCHAR *restOfPath;
  526. YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
  527. int i;
  528. if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
  529. if (loop)
  530. *loop = 1;
  531. return NULL;
  532. }
  533. if (startDir) {
  534. dir = startDir;
  535. restOfPath = (YCHAR *) path;
  536. } else
  537. dir = yaffsfs_FindRoot(path, &restOfPath);
  538. while (dir) {
  539. /*
  540. * parse off /.
  541. * curve ball: also throw away surplus '/'
  542. * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
  543. */
  544. while (yaffsfs_IsPathDivider(*restOfPath))
  545. restOfPath++; /* get rid of '/' */
  546. *name = restOfPath;
  547. i = 0;
  548. while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
  549. if (i < YAFFS_MAX_NAME_LENGTH) {
  550. str[i] = *restOfPath;
  551. str[i + 1] = '\0';
  552. i++;
  553. }
  554. restOfPath++;
  555. }
  556. if (!*restOfPath)
  557. /* got to the end of the string */
  558. return dir;
  559. else {
  560. if (yaffs_strcmp(str, _Y(".")) == 0) {
  561. /* Do nothing */
  562. } else if (yaffs_strcmp(str, _Y("..")) == 0) {
  563. dir = dir->parent;
  564. } else {
  565. dir = yaffs_find_by_name(dir, str);
  566. dir = yaffsfs_FollowLink(dir, symDepth, loop);
  567. if (dir && dir->variant_type !=
  568. YAFFS_OBJECT_TYPE_DIRECTORY) {
  569. if (notDir)
  570. *notDir = 1;
  571. dir = NULL;
  572. }
  573. }
  574. }
  575. }
  576. /* directory did not exist. */
  577. return NULL;
  578. }
  579. static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
  580. const YCHAR *path,
  581. YCHAR **name,
  582. int symDepth,
  583. int *notDir, int *loop)
  584. {
  585. return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
  586. loop);
  587. }
  588. /*
  589. * yaffsfs_FindObject turns a path for an existing object into the object
  590. */
  591. static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
  592. const YCHAR *path, int symDepth,
  593. int getEquiv,
  594. struct yaffs_obj **dirOut,
  595. int *notDir, int *loop)
  596. {
  597. struct yaffs_obj *dir;
  598. struct yaffs_obj *obj;
  599. YCHAR *name;
  600. dir =
  601. yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
  602. if (dirOut)
  603. *dirOut = dir;
  604. if (dir && *name)
  605. obj = yaffs_find_by_name(dir, name);
  606. else
  607. obj = dir;
  608. if (getEquiv)
  609. obj = yaffs_get_equivalent_obj(obj);
  610. return obj;
  611. }
  612. /*************************************************************************
  613. * Start of yaffsfs visible functions.
  614. *************************************************************************/
  615. int yaffs_dup(int handle)
  616. {
  617. int newHandleNumber = -1;
  618. struct yaffsfs_FileDes *existingFD = NULL;
  619. struct yaffsfs_Handle *existingHandle = NULL;
  620. struct yaffsfs_Handle *newHandle = NULL;
  621. yaffsfs_Lock();
  622. existingHandle = yaffsfs_HandleToPointer(handle);
  623. existingFD = yaffsfs_HandleToFileDes(handle);
  624. if (existingFD)
  625. newHandleNumber = yaffsfs_NewHandle(&newHandle);
  626. if (newHandle) {
  627. newHandle->fdId = existingHandle->fdId;
  628. existingFD->handleCount++;
  629. }
  630. yaffsfs_Unlock();
  631. if (!existingFD)
  632. yaffsfs_SetError(-EBADF);
  633. else if (!newHandle)
  634. yaffsfs_SetError(-ENOMEM);
  635. return newHandleNumber;
  636. }
  637. static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
  638. {
  639. int current_objects = dev->n_obj - dev->n_deleted_files;
  640. if (dev->param.max_objects && current_objects > dev->param.max_objects)
  641. return 1;
  642. else
  643. return 0;
  644. }
  645. int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
  646. {
  647. struct yaffs_obj *obj = NULL;
  648. struct yaffs_obj *dir = NULL;
  649. YCHAR *name;
  650. int handle = -1;
  651. struct yaffsfs_FileDes *fd = NULL;
  652. int openDenied = 0;
  653. int symDepth = 0;
  654. int errorReported = 0;
  655. int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
  656. u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
  657. u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
  658. u8 sharedReadAllowed;
  659. u8 sharedWriteAllowed;
  660. u8 alreadyReading;
  661. u8 alreadyWriting;
  662. u8 readRequested;
  663. u8 writeRequested;
  664. int notDir = 0;
  665. int loop = 0;
  666. if (!path) {
  667. yaffsfs_SetError(-EFAULT);
  668. return -1;
  669. }
  670. if (yaffsfs_CheckPath(path) < 0) {
  671. yaffsfs_SetError(-ENAMETOOLONG);
  672. return -1;
  673. }
  674. /* O_EXCL only has meaning if O_CREAT is specified */
  675. if (!(oflag & O_CREAT))
  676. oflag &= ~(O_EXCL);
  677. /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
  678. if ((oflag & O_CREAT) & (oflag & O_EXCL))
  679. oflag &= ~(O_TRUNC);
  680. /* Todo: Are there any more flag combos to sanitise ? */
  681. /* Figure out if reading or writing is requested */
  682. readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
  683. writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
  684. yaffsfs_Lock();
  685. handle = yaffsfs_NewHandleAndFileDes();
  686. if (handle < 0) {
  687. yaffsfs_SetError(-ENFILE);
  688. errorReported = 1;
  689. } else {
  690. fd = yaffsfs_HandleToFileDes(handle);
  691. /* try to find the exisiting object */
  692. obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
  693. obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
  694. if (obj &&
  695. obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
  696. obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  697. obj = NULL;
  698. if (obj) {
  699. /* The file already exists or it might be a directory */
  700. /* A directory can't be opened as a file */
  701. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  702. openDenied = 1;
  703. yaffsfs_SetError(-EISDIR);
  704. errorReported = 1;
  705. }
  706. /* Open should fail if O_CREAT and O_EXCL are specified
  707. * for a file that exists.
  708. */
  709. if (!errorReported &&
  710. (oflag & O_EXCL) && (oflag & O_CREAT)) {
  711. openDenied = 1;
  712. yaffsfs_SetError(-EEXIST);
  713. errorReported = 1;
  714. }
  715. /* Check file permissions */
  716. if (readRequested && !(obj->yst_mode & S_IREAD))
  717. openDenied = 1;
  718. if (writeRequested && !(obj->yst_mode & S_IWRITE))
  719. openDenied = 1;
  720. if (!errorReported && writeRequested &&
  721. obj->my_dev->read_only) {
  722. openDenied = 1;
  723. yaffsfs_SetError(-EROFS);
  724. errorReported = 1;
  725. }
  726. if (openDenied && !errorReported) {
  727. yaffsfs_SetError(-EACCES);
  728. errorReported = 1;
  729. }
  730. /* Check sharing of an existing object. */
  731. if (!openDenied) {
  732. struct yaffsfs_FileDes *fdx;
  733. int i;
  734. sharedReadAllowed = 1;
  735. sharedWriteAllowed = 1;
  736. alreadyReading = 0;
  737. alreadyWriting = 0;
  738. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  739. fdx = &yaffsfs_fd[i];
  740. if (fdx->handleCount > 0 &&
  741. fdx->inodeId >= 0 &&
  742. yaffsfs_inode[fdx->inodeId].iObj
  743. == obj) {
  744. if (!fdx->shareRead)
  745. sharedReadAllowed = 0;
  746. if (!fdx->shareWrite)
  747. sharedWriteAllowed = 0;
  748. if (fdx->reading)
  749. alreadyReading = 1;
  750. if (fdx->writing)
  751. alreadyWriting = 1;
  752. }
  753. }
  754. if ((!sharedReadAllowed && readRequested) ||
  755. (!shareRead && alreadyReading) ||
  756. (!sharedWriteAllowed && writeRequested) ||
  757. (!shareWrite && alreadyWriting)) {
  758. openDenied = 1;
  759. yaffsfs_SetError(-EBUSY);
  760. errorReported = 1;
  761. }
  762. }
  763. }
  764. /* If we could not open an existing object, then let's see if
  765. * the directory exists. If not, error.
  766. */
  767. if (!obj && !errorReported) {
  768. dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
  769. &notDir, &loop);
  770. if (!dir && notDir) {
  771. yaffsfs_SetError(-ENOTDIR);
  772. errorReported = 1;
  773. } else if (loop) {
  774. yaffsfs_SetError(-ELOOP);
  775. errorReported = 1;
  776. } else if (!dir) {
  777. yaffsfs_SetError(-ENOENT);
  778. errorReported = 1;
  779. }
  780. }
  781. if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
  782. /* Let's see if we can create this file */
  783. if (dir->my_dev->read_only) {
  784. yaffsfs_SetError(-EROFS);
  785. errorReported = 1;
  786. } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
  787. yaffsfs_SetError(-ENFILE);
  788. errorReported = 1;
  789. } else
  790. obj = yaffs_create_file(dir, name, mode, 0, 0);
  791. if (!obj && !errorReported) {
  792. yaffsfs_SetError(-ENOSPC);
  793. errorReported = 1;
  794. }
  795. }
  796. if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
  797. yaffsfs_SetError(-ENOENT);
  798. errorReported = 1;
  799. }
  800. if (obj && !openDenied) {
  801. int inodeId = yaffsfs_GetInodeIdForObject(obj);
  802. if (inodeId < 0) {
  803. /*
  804. * Todo: Fix any problem if inodes run out,
  805. * That can't happen if the number of inode
  806. * items >= number of handles.
  807. */
  808. }
  809. fd->inodeId = inodeId;
  810. fd->reading = readRequested;
  811. fd->writing = writeRequested;
  812. fd->append = (oflag & O_APPEND) ? 1 : 0;
  813. fd->position = 0;
  814. fd->shareRead = shareRead;
  815. fd->shareWrite = shareWrite;
  816. /* Hook inode to object */
  817. obj->my_inode = (void *)&yaffsfs_inode[inodeId];
  818. if ((oflag & O_TRUNC) && fd->writing)
  819. yaffs_resize_file(obj, 0);
  820. } else {
  821. yaffsfs_PutHandle(handle);
  822. if (!errorReported)
  823. yaffsfs_SetError(0); /* Problem */
  824. handle = -1;
  825. }
  826. }
  827. yaffsfs_Unlock();
  828. return handle;
  829. }
  830. int yaffs_open(const YCHAR *path, int oflag, int mode)
  831. {
  832. return yaffs_open_sharing(path, oflag, mode,
  833. YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
  834. }
  835. int yaffs_Dofsync(int handle, int datasync)
  836. {
  837. int retVal = -1;
  838. struct yaffs_obj *obj;
  839. yaffsfs_Lock();
  840. obj = yaffsfs_HandleToObject(handle);
  841. if (!obj)
  842. yaffsfs_SetError(-EBADF);
  843. else if (obj->my_dev->read_only)
  844. yaffsfs_SetError(-EROFS);
  845. else {
  846. yaffs_flush_file(obj, 1, datasync);
  847. retVal = 0;
  848. }
  849. yaffsfs_Unlock();
  850. return retVal;
  851. }
  852. int yaffs_fsync(int handle)
  853. {
  854. return yaffs_Dofsync(handle, 0);
  855. }
  856. int yaffs_flush(int handle)
  857. {
  858. return yaffs_fsync(handle);
  859. }
  860. int yaffs_fdatasync(int handle)
  861. {
  862. return yaffs_Dofsync(handle, 1);
  863. }
  864. int yaffs_close(int handle)
  865. {
  866. struct yaffsfs_Handle *h = NULL;
  867. struct yaffs_obj *obj = NULL;
  868. int retVal = -1;
  869. yaffsfs_Lock();
  870. h = yaffsfs_HandleToPointer(handle);
  871. obj = yaffsfs_HandleToObject(handle);
  872. if (!h || !obj)
  873. yaffsfs_SetError(-EBADF);
  874. else {
  875. /* clean up */
  876. yaffs_flush_file(obj, 1, 0);
  877. yaffsfs_PutHandle(handle);
  878. retVal = 0;
  879. }
  880. yaffsfs_Unlock();
  881. return retVal;
  882. }
  883. int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
  884. int isPread, loff_t offset)
  885. {
  886. struct yaffsfs_FileDes *fd = NULL;
  887. struct yaffs_obj *obj = NULL;
  888. loff_t pos = 0;
  889. loff_t startPos = 0;
  890. loff_t endPos = 0;
  891. int nRead = 0;
  892. int nToRead = 0;
  893. int totalRead = 0;
  894. loff_t maxRead;
  895. u8 *buf = (u8 *) vbuf;
  896. if (!vbuf) {
  897. yaffsfs_SetError(-EFAULT);
  898. return -1;
  899. }
  900. yaffsfs_Lock();
  901. fd = yaffsfs_HandleToFileDes(handle);
  902. obj = yaffsfs_HandleToObject(handle);
  903. if (!fd || !obj) {
  904. /* bad handle */
  905. yaffsfs_SetError(-EBADF);
  906. totalRead = -1;
  907. } else if (!fd->reading) {
  908. /* Not a reading handle */
  909. yaffsfs_SetError(-EINVAL);
  910. totalRead = -1;
  911. } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
  912. yaffsfs_SetError(-EINVAL);
  913. totalRead = -1;
  914. } else {
  915. if (isPread)
  916. startPos = offset;
  917. else
  918. startPos = fd->position;
  919. pos = startPos;
  920. if (yaffs_get_obj_length(obj) > pos)
  921. maxRead = yaffs_get_obj_length(obj) - pos;
  922. else
  923. maxRead = 0;
  924. if (nbyte > maxRead)
  925. nbyte = maxRead;
  926. yaffsfs_GetHandle(handle);
  927. endPos = pos + nbyte;
  928. if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
  929. nbyte > YAFFS_MAX_FILE_SIZE ||
  930. endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
  931. totalRead = -1;
  932. nbyte = 0;
  933. }
  934. while (nbyte > 0) {
  935. nToRead = YAFFSFS_RW_SIZE -
  936. (pos & (YAFFSFS_RW_SIZE - 1));
  937. if (nToRead > nbyte)
  938. nToRead = nbyte;
  939. /* Tricky bit...
  940. * Need to reverify object in case the device was
  941. * unmounted in another thread.
  942. */
  943. obj = yaffsfs_HandleToObject(handle);
  944. if (!obj)
  945. nRead = 0;
  946. else
  947. nRead = yaffs_file_rd(obj, buf, pos, nToRead);
  948. if (nRead > 0) {
  949. totalRead += nRead;
  950. pos += nRead;
  951. buf += nRead;
  952. }
  953. if (nRead == nToRead)
  954. nbyte -= nRead;
  955. else
  956. nbyte = 0; /* no more to read */
  957. if (nbyte > 0) {
  958. yaffsfs_Unlock();
  959. yaffsfs_Lock();
  960. }
  961. }
  962. yaffsfs_PutHandle(handle);
  963. if (!isPread) {
  964. if (totalRead >= 0)
  965. fd->position = startPos + totalRead;
  966. else
  967. yaffsfs_SetError(-EINVAL);
  968. }
  969. }
  970. yaffsfs_Unlock();
  971. return (totalRead >= 0) ? totalRead : -1;
  972. }
  973. int yaffs_read(int handle, void *buf, unsigned int nbyte)
  974. {
  975. return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
  976. }
  977. int yaffs_pread(int handle, void *buf, unsigned int nbyte, loff_t offset)
  978. {
  979. return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
  980. }
  981. int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
  982. int isPwrite, loff_t offset)
  983. {
  984. struct yaffsfs_FileDes *fd = NULL;
  985. struct yaffs_obj *obj = NULL;
  986. loff_t pos = 0;
  987. loff_t startPos = 0;
  988. loff_t endPos;
  989. int nWritten = 0;
  990. int totalWritten = 0;
  991. int write_trhrough = 0;
  992. int nToWrite = 0;
  993. const u8 *buf = (const u8 *)vbuf;
  994. if (!vbuf) {
  995. yaffsfs_SetError(-EFAULT);
  996. return -1;
  997. }
  998. yaffsfs_Lock();
  999. fd = yaffsfs_HandleToFileDes(handle);
  1000. obj = yaffsfs_HandleToObject(handle);
  1001. if (!fd || !obj) {
  1002. /* bad handle */
  1003. yaffsfs_SetError(-EBADF);
  1004. totalWritten = -1;
  1005. } else if (!fd->writing) {
  1006. yaffsfs_SetError(-EINVAL);
  1007. totalWritten = -1;
  1008. } else if (obj->my_dev->read_only) {
  1009. yaffsfs_SetError(-EROFS);
  1010. totalWritten = -1;
  1011. } else {
  1012. if (fd->append)
  1013. startPos = yaffs_get_obj_length(obj);
  1014. else if (isPwrite)
  1015. startPos = offset;
  1016. else
  1017. startPos = fd->position;
  1018. yaffsfs_GetHandle(handle);
  1019. pos = startPos;
  1020. endPos = pos + nbyte;
  1021. if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
  1022. nbyte > YAFFS_MAX_FILE_SIZE ||
  1023. endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
  1024. totalWritten = -1;
  1025. nbyte = 0;
  1026. }
  1027. while (nbyte > 0) {
  1028. nToWrite = YAFFSFS_RW_SIZE -
  1029. (pos & (YAFFSFS_RW_SIZE - 1));
  1030. if (nToWrite > nbyte)
  1031. nToWrite = nbyte;
  1032. /* Tricky bit...
  1033. * Need to reverify object in case the device was
  1034. * remounted or unmounted in another thread.
  1035. */
  1036. obj = yaffsfs_HandleToObject(handle);
  1037. if (!obj || obj->my_dev->read_only)
  1038. nWritten = 0;
  1039. else
  1040. nWritten =
  1041. yaffs_wr_file(obj, buf, pos, nToWrite,
  1042. write_trhrough);
  1043. if (nWritten > 0) {
  1044. totalWritten += nWritten;
  1045. pos += nWritten;
  1046. buf += nWritten;
  1047. }
  1048. if (nWritten == nToWrite)
  1049. nbyte -= nToWrite;
  1050. else
  1051. nbyte = 0;
  1052. if (nWritten < 1 && totalWritten < 1) {
  1053. yaffsfs_SetError(-ENOSPC);
  1054. totalWritten = -1;
  1055. }
  1056. if (nbyte > 0) {
  1057. yaffsfs_Unlock();
  1058. yaffsfs_Lock();
  1059. }
  1060. }
  1061. yaffsfs_PutHandle(handle);
  1062. if (!isPwrite) {
  1063. if (totalWritten > 0)
  1064. fd->position = startPos + totalWritten;
  1065. else
  1066. yaffsfs_SetError(-EINVAL);
  1067. }
  1068. }
  1069. yaffsfs_Unlock();
  1070. return (totalWritten >= 0) ? totalWritten : -1;
  1071. }
  1072. int yaffs_write(int fd, const void *buf, unsigned int nbyte)
  1073. {
  1074. return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
  1075. }
  1076. int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, loff_t offset)
  1077. {
  1078. return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
  1079. }
  1080. int yaffs_truncate(const YCHAR *path, loff_t new_size)
  1081. {
  1082. struct yaffs_obj *obj = NULL;
  1083. struct yaffs_obj *dir = NULL;
  1084. int result = YAFFS_FAIL;
  1085. int notDir = 0;
  1086. int loop = 0;
  1087. if (!path) {
  1088. yaffsfs_SetError(-EFAULT);
  1089. return -1;
  1090. }
  1091. if (yaffsfs_CheckPath(path) < 0) {
  1092. yaffsfs_SetError(-ENAMETOOLONG);
  1093. return -1;
  1094. }
  1095. yaffsfs_Lock();
  1096. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1097. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1098. if (!dir && notDir)
  1099. yaffsfs_SetError(-ENOTDIR);
  1100. else if (loop)
  1101. yaffsfs_SetError(-ELOOP);
  1102. else if (!dir || !obj)
  1103. yaffsfs_SetError(-ENOENT);
  1104. else if (obj->my_dev->read_only)
  1105. yaffsfs_SetError(-EROFS);
  1106. else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  1107. yaffsfs_SetError(-EISDIR);
  1108. else if (obj->my_dev->read_only)
  1109. yaffsfs_SetError(-EROFS);
  1110. else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
  1111. yaffsfs_SetError(-EINVAL);
  1112. else
  1113. result = yaffs_resize_file(obj, new_size);
  1114. yaffsfs_Unlock();
  1115. return (result) ? 0 : -1;
  1116. }
  1117. int yaffs_ftruncate(int handle, loff_t new_size)
  1118. {
  1119. struct yaffsfs_FileDes *fd = NULL;
  1120. struct yaffs_obj *obj = NULL;
  1121. int result = 0;
  1122. yaffsfs_Lock();
  1123. fd = yaffsfs_HandleToFileDes(handle);
  1124. obj = yaffsfs_HandleToObject(handle);
  1125. if (!fd || !obj)
  1126. /* bad handle */
  1127. yaffsfs_SetError(-EBADF);
  1128. else if (!fd->writing)
  1129. yaffsfs_SetError(-EINVAL);
  1130. else if (obj->my_dev->read_only)
  1131. yaffsfs_SetError(-EROFS);
  1132. else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
  1133. yaffsfs_SetError(-EINVAL);
  1134. else
  1135. /* resize the file */
  1136. result = yaffs_resize_file(obj, new_size);
  1137. yaffsfs_Unlock();
  1138. return (result) ? 0 : -1;
  1139. }
  1140. loff_t yaffs_lseek(int handle, loff_t offset, int whence)
  1141. {
  1142. struct yaffsfs_FileDes *fd = NULL;
  1143. struct yaffs_obj *obj = NULL;
  1144. loff_t pos = -1;
  1145. loff_t fSize = -1;
  1146. yaffsfs_Lock();
  1147. fd = yaffsfs_HandleToFileDes(handle);
  1148. obj = yaffsfs_HandleToObject(handle);
  1149. if (!fd || !obj)
  1150. yaffsfs_SetError(-EBADF);
  1151. else if (offset > YAFFS_MAX_FILE_SIZE)
  1152. yaffsfs_SetError(-EINVAL);
  1153. else {
  1154. if (whence == SEEK_SET) {
  1155. if (offset >= 0)
  1156. pos = offset;
  1157. } else if (whence == SEEK_CUR) {
  1158. if ((fd->position + offset) >= 0)
  1159. pos = (fd->position + offset);
  1160. } else if (whence == SEEK_END) {
  1161. fSize = yaffs_get_obj_length(obj);
  1162. if (fSize >= 0 && (fSize + offset) >= 0)
  1163. pos = fSize + offset;
  1164. }
  1165. if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
  1166. fd->position = pos;
  1167. else {
  1168. yaffsfs_SetError(-EINVAL);
  1169. pos = -1;
  1170. }
  1171. }
  1172. yaffsfs_Unlock();
  1173. return pos;
  1174. }
  1175. int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
  1176. {
  1177. struct yaffs_obj *dir = NULL;
  1178. struct yaffs_obj *obj = NULL;
  1179. YCHAR *name;
  1180. int result = YAFFS_FAIL;
  1181. int notDir = 0;
  1182. int loop = 0;
  1183. if (!path) {
  1184. yaffsfs_SetError(-EFAULT);
  1185. return -1;
  1186. }
  1187. if (yaffsfs_CheckPath(path) < 0) {
  1188. yaffsfs_SetError(-ENAMETOOLONG);
  1189. return -1;
  1190. }
  1191. yaffsfs_Lock();
  1192. obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
  1193. dir = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
  1194. if (!dir && notDir)
  1195. yaffsfs_SetError(-ENOTDIR);
  1196. else if (loop)
  1197. yaffsfs_SetError(-ELOOP);
  1198. else if (!dir)
  1199. yaffsfs_SetError(-ENOENT);
  1200. else if (yaffs_strncmp(name, _Y("."), 2) == 0)
  1201. yaffsfs_SetError(-EINVAL);
  1202. else if (!obj)
  1203. yaffsfs_SetError(-ENOENT);
  1204. else if (obj->my_dev->read_only)
  1205. yaffsfs_SetError(-EROFS);
  1206. else if (!isDirectory &&
  1207. obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  1208. yaffsfs_SetError(-EISDIR);
  1209. else if (isDirectory &&
  1210. obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  1211. yaffsfs_SetError(-ENOTDIR);
  1212. else if (isDirectory && obj == obj->my_dev->root_dir)
  1213. yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
  1214. else {
  1215. result = yaffs_unlinker(dir, name);
  1216. if (result == YAFFS_FAIL && isDirectory)
  1217. yaffsfs_SetError(-ENOTEMPTY);
  1218. }
  1219. yaffsfs_Unlock();
  1220. return (result == YAFFS_FAIL) ? -1 : 0;
  1221. }
  1222. int yaffs_unlink(const YCHAR *path)
  1223. {
  1224. return yaffsfs_DoUnlink(path, 0);
  1225. }
  1226. int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
  1227. {
  1228. struct yaffs_obj *olddir = NULL;
  1229. struct yaffs_obj *newdir = NULL;
  1230. struct yaffs_obj *obj = NULL;
  1231. struct yaffs_obj *newobj = NULL;
  1232. YCHAR *oldname;
  1233. YCHAR *newname;
  1234. int result = YAFFS_FAIL;
  1235. int rename_allowed = 1;
  1236. int notOldDir = 0;
  1237. int notNewDir = 0;
  1238. int oldLoop = 0;
  1239. int newLoop = 0;
  1240. YCHAR *alt_newpath = NULL;
  1241. if (!oldPath || !newPath) {
  1242. yaffsfs_SetError(-EFAULT);
  1243. return -1;
  1244. }
  1245. if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
  1246. yaffsfs_SetError(-ENAMETOOLONG);
  1247. return -1;
  1248. }
  1249. if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
  1250. yaffsfs_SetError(-ENOMEM);
  1251. return -1;
  1252. }
  1253. if (alt_newpath)
  1254. newPath = alt_newpath;
  1255. yaffsfs_Lock();
  1256. olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
  1257. &notOldDir, &oldLoop);
  1258. newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
  1259. &notNewDir, &newLoop);
  1260. obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
  1261. newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
  1262. /* If the object being renamed is a directory and the
  1263. * path ended with a "/" then the olddir == obj.
  1264. * We pass through NULL for the old name to tell the lower layers
  1265. * to use olddir as the object.
  1266. */
  1267. if (olddir == obj)
  1268. oldname = NULL;
  1269. if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
  1270. yaffsfs_SetError(-ENOTDIR);
  1271. rename_allowed = 0;
  1272. } else if (oldLoop || newLoop) {
  1273. yaffsfs_SetError(-ELOOP);
  1274. rename_allowed = 0;
  1275. } else if (olddir && oldname &&
  1276. yaffs_strncmp(oldname, _Y("."), 2) == 0) {
  1277. yaffsfs_SetError(-EINVAL);
  1278. rename_allowed = 0;
  1279. } else if (!olddir || !newdir || !obj) {
  1280. yaffsfs_SetError(-ENOENT);
  1281. rename_allowed = 0;
  1282. } else if (obj->my_dev->read_only) {
  1283. yaffsfs_SetError(-EROFS);
  1284. rename_allowed = 0;
  1285. } else if (yaffs_is_non_empty_dir(newobj)) {
  1286. yaffsfs_SetError(-ENOTEMPTY);
  1287. rename_allowed = 0;
  1288. } else if (olddir->my_dev != newdir->my_dev) {
  1289. /* Rename must be on same device */
  1290. yaffsfs_SetError(-EXDEV);
  1291. rename_allowed = 0;
  1292. } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  1293. /*
  1294. * It is a directory, check that it is not being renamed to
  1295. * being its own decendent.
  1296. * Do this by tracing from the new directory back to the root,
  1297. * checking for obj
  1298. */
  1299. struct yaffs_obj *xx = newdir;
  1300. while (rename_allowed && xx) {
  1301. if (xx == obj)
  1302. rename_allowed = 0;
  1303. xx = xx->parent;
  1304. }
  1305. if (!rename_allowed)
  1306. yaffsfs_SetError(-EINVAL);
  1307. }
  1308. if (rename_allowed)
  1309. result = yaffs_rename_obj(olddir, oldname, newdir, newname);
  1310. yaffsfs_Unlock();
  1311. kfree(alt_newpath);
  1312. return (result == YAFFS_FAIL) ? -1 : 0;
  1313. }
  1314. static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
  1315. {
  1316. int retVal = -1;
  1317. obj = yaffs_get_equivalent_obj(obj);
  1318. if (obj && buf) {
  1319. buf->st_dev = (int)obj->my_dev->os_context;
  1320. buf->st_ino = obj->obj_id;
  1321. buf->st_mode = obj->yst_mode & ~S_IFMT;
  1322. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  1323. buf->st_mode |= S_IFDIR;
  1324. else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  1325. buf->st_mode |= S_IFLNK;
  1326. else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  1327. buf->st_mode |= S_IFREG;
  1328. buf->st_nlink = yaffs_get_obj_link_count(obj);
  1329. buf->st_uid = 0;
  1330. buf->st_gid = 0;
  1331. buf->st_rdev = obj->yst_rdev;
  1332. buf->st_size = yaffs_get_obj_length(obj);
  1333. buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
  1334. buf->st_blocks = lldiv(buf->st_size + buf->st_blksize - 1,
  1335. buf->st_blksize);
  1336. #if CONFIG_YAFFS_WINCE
  1337. buf->yst_wince_atime[0] = obj->win_atime[0];
  1338. buf->yst_wince_atime[1] = obj->win_atime[1];
  1339. buf->yst_wince_ctime[0] = obj->win_ctime[0];
  1340. buf->yst_wince_ctime[1] = obj->win_ctime[1];
  1341. buf->yst_wince_mtime[0] = obj->win_mtime[0];
  1342. buf->yst_wince_mtime[1] = obj->win_mtime[1];
  1343. #else
  1344. buf->yst_atime = obj->yst_atime;
  1345. buf->yst_ctime = obj->yst_ctime;
  1346. buf->yst_mtime = obj->yst_mtime;
  1347. #endif
  1348. retVal = 0;
  1349. }
  1350. return retVal;
  1351. }
  1352. static int yaffsfs_DoStatOrLStat(const YCHAR *path,
  1353. struct yaffs_stat *buf, int doLStat)
  1354. {
  1355. struct yaffs_obj *obj = NULL;
  1356. struct yaffs_obj *dir = NULL;
  1357. int retVal = -1;
  1358. int notDir = 0;
  1359. int loop = 0;
  1360. if (!path || !buf) {
  1361. yaffsfs_SetError(-EFAULT);
  1362. return -1;
  1363. }
  1364. if (yaffsfs_CheckPath(path) < 0) {
  1365. yaffsfs_SetError(-ENAMETOOLONG);
  1366. return -1;
  1367. }
  1368. yaffsfs_Lock();
  1369. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1370. if (!doLStat && obj)
  1371. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1372. if (!dir && notDir)
  1373. yaffsfs_SetError(-ENOTDIR);
  1374. else if (loop)
  1375. yaffsfs_SetError(-ELOOP);
  1376. else if (!dir || !obj)
  1377. yaffsfs_SetError(-ENOENT);
  1378. else
  1379. retVal = yaffsfs_DoStat(obj, buf);
  1380. yaffsfs_Unlock();
  1381. return retVal;
  1382. }
  1383. int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
  1384. {
  1385. return yaffsfs_DoStatOrLStat(path, buf, 0);
  1386. }
  1387. int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
  1388. {
  1389. return yaffsfs_DoStatOrLStat(path, buf, 1);
  1390. }
  1391. int yaffs_fstat(int fd, struct yaffs_stat *buf)
  1392. {
  1393. struct yaffs_obj *obj;
  1394. int retVal = -1;
  1395. if (!buf) {
  1396. yaffsfs_SetError(-EFAULT);
  1397. return -1;
  1398. }
  1399. yaffsfs_Lock();
  1400. obj = yaffsfs_HandleToObject(fd);
  1401. if (obj)
  1402. retVal = yaffsfs_DoStat(obj, buf);
  1403. else
  1404. /* bad handle */
  1405. yaffsfs_SetError(-EBADF);
  1406. yaffsfs_Unlock();
  1407. return retVal;
  1408. }
  1409. static int yaffsfs_DoUtime(struct yaffs_obj *obj,
  1410. const struct yaffs_utimbuf *buf)
  1411. {
  1412. int retVal = -1;
  1413. int result;
  1414. struct yaffs_utimbuf local;
  1415. obj = yaffs_get_equivalent_obj(obj);
  1416. if (obj && obj->my_dev->read_only) {
  1417. yaffsfs_SetError(-EROFS);
  1418. return -1;
  1419. }
  1420. if (!buf) {
  1421. local.actime = Y_CURRENT_TIME;
  1422. local.modtime = local.actime;
  1423. buf = &local;
  1424. }
  1425. if (obj) {
  1426. obj->yst_atime = buf->actime;
  1427. obj->yst_mtime = buf->modtime;
  1428. obj->dirty = 1;
  1429. result = yaffs_flush_file(obj, 0, 0);
  1430. retVal = result == YAFFS_OK ? 0 : -1;
  1431. }
  1432. return retVal;
  1433. }
  1434. int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
  1435. {
  1436. struct yaffs_obj *obj = NULL;
  1437. struct yaffs_obj *dir = NULL;
  1438. int retVal = -1;
  1439. int notDir = 0;
  1440. int loop = 0;
  1441. if (!path) {
  1442. yaffsfs_SetError(-EFAULT);
  1443. return -1;
  1444. }
  1445. if (yaffsfs_CheckPath(path) < 0) {
  1446. yaffsfs_SetError(-ENAMETOOLONG);
  1447. return -1;
  1448. }
  1449. yaffsfs_Lock();
  1450. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1451. if (!dir && notDir)
  1452. yaffsfs_SetError(-ENOTDIR);
  1453. else if (loop)
  1454. yaffsfs_SetError(-ELOOP);
  1455. else if (!dir || !obj)
  1456. yaffsfs_SetError(-ENOENT);
  1457. else
  1458. retVal = yaffsfs_DoUtime(obj, buf);
  1459. yaffsfs_Unlock();
  1460. return retVal;
  1461. }
  1462. int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
  1463. {
  1464. struct yaffs_obj *obj;
  1465. int retVal = -1;
  1466. yaffsfs_Lock();
  1467. obj = yaffsfs_HandleToObject(fd);
  1468. if (obj)
  1469. retVal = yaffsfs_DoUtime(obj, buf);
  1470. else
  1471. /* bad handle */
  1472. yaffsfs_SetError(-EBADF);
  1473. yaffsfs_Unlock();
  1474. return retVal;
  1475. }
  1476. #ifndef CONFIG_YAFFS_WINCE
  1477. /* xattrib functions */
  1478. static int yaffs_do_setxattr(const YCHAR *path, const char *name,
  1479. const void *data, int size, int flags, int follow)
  1480. {
  1481. struct yaffs_obj *obj;
  1482. struct yaffs_obj *dir;
  1483. int notDir = 0;
  1484. int loop = 0;
  1485. int retVal = -1;
  1486. if (!path || !name || !data) {
  1487. yaffsfs_SetError(-EFAULT);
  1488. return -1;
  1489. }
  1490. if (yaffsfs_CheckPath(path) < 0) {
  1491. yaffsfs_SetError(-ENAMETOOLONG);
  1492. return -1;
  1493. }
  1494. yaffsfs_Lock();
  1495. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1496. if (follow)
  1497. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1498. if (!dir && notDir)
  1499. yaffsfs_SetError(-ENOTDIR);
  1500. else if (loop)
  1501. yaffsfs_SetError(-ELOOP);
  1502. else if (!dir || !obj)
  1503. yaffsfs_SetError(-ENOENT);
  1504. else {
  1505. retVal = yaffs_set_xattrib(obj, name, data, size, flags);
  1506. if (retVal < 0) {
  1507. yaffsfs_SetError(retVal);
  1508. retVal = -1;
  1509. }
  1510. }
  1511. yaffsfs_Unlock();
  1512. return retVal;
  1513. }
  1514. int yaffs_setxattr(const YCHAR *path, const char *name,
  1515. const void *data, int size, int flags)
  1516. {
  1517. return yaffs_do_setxattr(path, name, data, size, flags, 1);
  1518. }
  1519. int yaffs_lsetxattr(const YCHAR *path, const char *name,
  1520. const void *data, int size, int flags)
  1521. {
  1522. return yaffs_do_setxattr(path, name, data, size, flags, 0);
  1523. }
  1524. int yaffs_fsetxattr(int fd, const char *name,
  1525. const void *data, int size, int flags)
  1526. {
  1527. struct yaffs_obj *obj;
  1528. int retVal = -1;
  1529. if (!name || !data) {
  1530. yaffsfs_SetError(-EFAULT);
  1531. return -1;
  1532. }
  1533. yaffsfs_Lock();
  1534. obj = yaffsfs_HandleToObject(fd);
  1535. if (!obj)
  1536. yaffsfs_SetError(-EBADF);
  1537. else {
  1538. retVal = yaffs_set_xattrib(obj, name, data, size, flags);
  1539. if (retVal < 0) {
  1540. yaffsfs_SetError(retVal);
  1541. retVal = -1;
  1542. }
  1543. }
  1544. yaffsfs_Unlock();
  1545. return retVal;
  1546. }
  1547. static int yaffs_do_getxattr(const YCHAR *path, const char *name,
  1548. void *data, int size, int follow)
  1549. {
  1550. struct yaffs_obj *obj;
  1551. struct yaffs_obj *dir;
  1552. int retVal = -1;
  1553. int notDir = 0;
  1554. int loop = 0;
  1555. if (!path || !name || !data) {
  1556. yaffsfs_SetError(-EFAULT);
  1557. return -1;
  1558. }
  1559. if (yaffsfs_CheckPath(path) < 0) {
  1560. yaffsfs_SetError(-ENAMETOOLONG);
  1561. return -1;
  1562. }
  1563. yaffsfs_Lock();
  1564. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1565. if (follow)
  1566. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1567. if (!dir && notDir)
  1568. yaffsfs_SetError(-ENOTDIR);
  1569. else if (loop)
  1570. yaffsfs_SetError(-ELOOP);
  1571. else if (!dir || !obj)
  1572. yaffsfs_SetError(-ENOENT);
  1573. else {
  1574. retVal = yaffs_get_xattrib(obj, name, data, size);
  1575. if (retVal < 0) {
  1576. yaffsfs_SetError(retVal);
  1577. retVal = -1;
  1578. }
  1579. }
  1580. yaffsfs_Unlock();
  1581. return retVal;
  1582. }
  1583. int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
  1584. {
  1585. return yaffs_do_getxattr(path, name, data, size, 1);
  1586. }
  1587. int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
  1588. {
  1589. return yaffs_do_getxattr(path, name, data, size, 0);
  1590. }
  1591. int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
  1592. {
  1593. struct yaffs_obj *obj;
  1594. int retVal = -1;
  1595. if (!name || !data) {
  1596. yaffsfs_SetError(-EFAULT);
  1597. return -1;
  1598. }
  1599. yaffsfs_Lock();
  1600. obj = yaffsfs_HandleToObject(fd);
  1601. if (obj) {
  1602. retVal = yaffs_get_xattrib(obj, name, data, size);
  1603. if (retVal < 0) {
  1604. yaffsfs_SetError(retVal);
  1605. retVal = -1;
  1606. }
  1607. } else
  1608. /* bad handle */
  1609. yaffsfs_SetError(-EBADF);
  1610. yaffsfs_Unlock();
  1611. return retVal;
  1612. }
  1613. static int yaffs_do_listxattr(const YCHAR *path, char *data,
  1614. int size, int follow)
  1615. {
  1616. struct yaffs_obj *obj = NULL;
  1617. struct yaffs_obj *dir = NULL;
  1618. int retVal = -1;
  1619. int notDir = 0;
  1620. int loop = 0;
  1621. if (!path || !data) {
  1622. yaffsfs_SetError(-EFAULT);
  1623. return -1;
  1624. }
  1625. if (yaffsfs_CheckPath(path) < 0) {
  1626. yaffsfs_SetError(-ENAMETOOLONG);
  1627. return -1;
  1628. }
  1629. yaffsfs_Lock();
  1630. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1631. if (follow)
  1632. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1633. if (!dir && notDir)
  1634. yaffsfs_SetError(-ENOTDIR);
  1635. else if (loop)
  1636. yaffsfs_SetError(-ELOOP);
  1637. else if (!dir || !obj)
  1638. yaffsfs_SetError(-ENOENT);
  1639. else {
  1640. retVal = yaffs_list_xattrib(obj, data, size);
  1641. if (retVal < 0) {
  1642. yaffsfs_SetError(retVal);
  1643. retVal = -1;
  1644. }
  1645. }
  1646. yaffsfs_Unlock();
  1647. return retVal;
  1648. }
  1649. int yaffs_listxattr(const YCHAR *path, char *data, int size)
  1650. {
  1651. return yaffs_do_listxattr(path, data, size, 1);
  1652. }
  1653. int yaffs_llistxattr(const YCHAR *path, char *data, int size)
  1654. {
  1655. return yaffs_do_listxattr(path, data, size, 0);
  1656. }
  1657. int yaffs_flistxattr(int fd, char *data, int size)
  1658. {
  1659. struct yaffs_obj *obj;
  1660. int retVal = -1;
  1661. if (!data) {
  1662. yaffsfs_SetError(-EFAULT);
  1663. return -1;
  1664. }
  1665. yaffsfs_Lock();
  1666. obj = yaffsfs_HandleToObject(fd);
  1667. if (obj) {
  1668. retVal = yaffs_list_xattrib(obj, data, size);
  1669. if (retVal < 0) {
  1670. yaffsfs_SetError(retVal);
  1671. retVal = -1;
  1672. }
  1673. } else
  1674. /* bad handle */
  1675. yaffsfs_SetError(-EBADF);
  1676. yaffsfs_Unlock();
  1677. return retVal;
  1678. }
  1679. static int yaffs_do_removexattr(const YCHAR *path, const char *name,
  1680. int follow)
  1681. {
  1682. struct yaffs_obj *obj = NULL;
  1683. struct yaffs_obj *dir = NULL;
  1684. int notDir = 0;
  1685. int loop = 0;
  1686. int retVal = -1;
  1687. if (!path || !name) {
  1688. yaffsfs_SetError(-EFAULT);
  1689. return -1;
  1690. }
  1691. if (yaffsfs_CheckPath(path) < 0) {
  1692. yaffsfs_SetError(-ENAMETOOLONG);
  1693. return -1;
  1694. }
  1695. yaffsfs_Lock();
  1696. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1697. if (follow)
  1698. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1699. if (!dir && notDir)
  1700. yaffsfs_SetError(-ENOTDIR);
  1701. else if (loop)
  1702. yaffsfs_SetError(-ELOOP);
  1703. else if (!dir || !obj)
  1704. yaffsfs_SetError(-ENOENT);
  1705. else {
  1706. retVal = yaffs_remove_xattrib(obj, name);
  1707. if (retVal < 0) {
  1708. yaffsfs_SetError(retVal);
  1709. retVal = -1;
  1710. }
  1711. }
  1712. yaffsfs_Unlock();
  1713. return retVal;
  1714. }
  1715. int yaffs_removexattr(const YCHAR *path, const char *name)
  1716. {
  1717. return yaffs_do_removexattr(path, name, 1);
  1718. }
  1719. int yaffs_lremovexattr(const YCHAR *path, const char *name)
  1720. {
  1721. return yaffs_do_removexattr(path, name, 0);
  1722. }
  1723. int yaffs_fremovexattr(int fd, const char *name)
  1724. {
  1725. struct yaffs_obj *obj;
  1726. int retVal = -1;
  1727. if (!name) {
  1728. yaffsfs_SetError(-EFAULT);
  1729. return -1;
  1730. }
  1731. yaffsfs_Lock();
  1732. obj = yaffsfs_HandleToObject(fd);
  1733. if (obj) {
  1734. retVal = yaffs_remove_xattrib(obj, name);
  1735. if (retVal < 0) {
  1736. yaffsfs_SetError(retVal);
  1737. retVal = -1;
  1738. }
  1739. } else
  1740. /* bad handle */
  1741. yaffsfs_SetError(-EBADF);
  1742. yaffsfs_Unlock();
  1743. return retVal;
  1744. }
  1745. #endif
  1746. #ifdef CONFIG_YAFFS_WINCE
  1747. int yaffs_get_wince_times(int fd, unsigned *wctime,
  1748. unsigned *watime, unsigned *wmtime)
  1749. {
  1750. struct yaffs_obj *obj;
  1751. int retVal = -1;
  1752. yaffsfs_Lock();
  1753. obj = yaffsfs_HandleToObject(fd);
  1754. if (obj) {
  1755. if (wctime) {
  1756. wctime[0] = obj->win_ctime[0];
  1757. wctime[1] = obj->win_ctime[1];
  1758. }
  1759. if (watime) {
  1760. watime[0] = obj->win_atime[0];
  1761. watime[1] = obj->win_atime[1];
  1762. }
  1763. if (wmtime) {
  1764. wmtime[0] = obj->win_mtime[0];
  1765. wmtime[1] = obj->win_mtime[1];
  1766. }
  1767. retVal = 0;
  1768. } else
  1769. /* bad handle */
  1770. yaffsfs_SetError(-EBADF);
  1771. yaffsfs_Unlock();
  1772. return retVal;
  1773. }
  1774. int yaffs_set_wince_times(int fd,
  1775. const unsigned *wctime,
  1776. const unsigned *watime, const unsigned *wmtime)
  1777. {
  1778. struct yaffs_obj *obj;
  1779. int result;
  1780. int retVal = -1;
  1781. yaffsfs_Lock();
  1782. obj = yaffsfs_HandleToObject(fd);
  1783. if (obj) {
  1784. if (wctime) {
  1785. obj->win_ctime[0] = wctime[0];
  1786. obj->win_ctime[1] = wctime[1];
  1787. }
  1788. if (watime) {
  1789. obj->win_atime[0] = watime[0];
  1790. obj->win_atime[1] = watime[1];
  1791. }
  1792. if (wmtime) {
  1793. obj->win_mtime[0] = wmtime[0];
  1794. obj->win_mtime[1] = wmtime[1];
  1795. }
  1796. obj->dirty = 1;
  1797. result = yaffs_flush_file(obj, 0, 0);
  1798. retVal = 0;
  1799. } else
  1800. /* bad handle */
  1801. yaffsfs_SetError(-EBADF);
  1802. yaffsfs_Unlock();
  1803. return retVal;
  1804. }
  1805. #endif
  1806. static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
  1807. {
  1808. int result = -1;
  1809. if (obj)
  1810. obj = yaffs_get_equivalent_obj(obj);
  1811. if (obj) {
  1812. obj->yst_mode = mode;
  1813. obj->dirty = 1;
  1814. result = yaffs_flush_file(obj, 0, 0);
  1815. }
  1816. return result == YAFFS_OK ? 0 : -1;
  1817. }
  1818. int yaffs_access(const YCHAR *path, int amode)
  1819. {
  1820. struct yaffs_obj *obj = NULL;
  1821. struct yaffs_obj *dir = NULL;
  1822. int notDir = 0;
  1823. int loop = 0;
  1824. int retval = -1;
  1825. if (!path) {
  1826. yaffsfs_SetError(-EFAULT);
  1827. return -1;
  1828. }
  1829. if (yaffsfs_CheckPath(path) < 0) {
  1830. yaffsfs_SetError(-ENAMETOOLONG);
  1831. return -1;
  1832. }
  1833. if (amode & ~(R_OK | W_OK | X_OK)) {
  1834. yaffsfs_SetError(-EINVAL);
  1835. return -1;
  1836. }
  1837. yaffsfs_Lock();
  1838. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1839. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1840. if (!dir && notDir)
  1841. yaffsfs_SetError(-ENOTDIR);
  1842. else if (loop)
  1843. yaffsfs_SetError(-ELOOP);
  1844. else if (!dir || !obj)
  1845. yaffsfs_SetError(-ENOENT);
  1846. else if ((amode & W_OK) && obj->my_dev->read_only)
  1847. yaffsfs_SetError(-EROFS);
  1848. else {
  1849. int access_ok = 1;
  1850. if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
  1851. access_ok = 0;
  1852. if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
  1853. access_ok = 0;
  1854. if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
  1855. access_ok = 0;
  1856. if (!access_ok)
  1857. yaffsfs_SetError(-EACCES);
  1858. else
  1859. retval = 0;
  1860. }
  1861. yaffsfs_Unlock();
  1862. return retval;
  1863. }
  1864. int yaffs_chmod(const YCHAR *path, mode_t mode)
  1865. {
  1866. struct yaffs_obj *obj = NULL;
  1867. struct yaffs_obj *dir = NULL;
  1868. int retVal = -1;
  1869. int notDir = 0;
  1870. int loop = 0;
  1871. if (!path) {
  1872. yaffsfs_SetError(-EFAULT);
  1873. return -1;
  1874. }
  1875. if (yaffsfs_CheckPath(path) < 0) {
  1876. yaffsfs_SetError(-ENAMETOOLONG);
  1877. return -1;
  1878. }
  1879. if (mode & ~(0777)) {
  1880. yaffsfs_SetError(-EINVAL);
  1881. return -1;
  1882. }
  1883. yaffsfs_Lock();
  1884. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1885. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1886. if (!dir && notDir)
  1887. yaffsfs_SetError(-ENOTDIR);
  1888. else if (loop)
  1889. yaffsfs_SetError(-ELOOP);
  1890. else if (!dir || !obj)
  1891. yaffsfs_SetError(-ENOENT);
  1892. else if (obj->my_dev->read_only)
  1893. yaffsfs_SetError(-EROFS);
  1894. else
  1895. retVal = yaffsfs_DoChMod(obj, mode);
  1896. yaffsfs_Unlock();
  1897. return retVal;
  1898. }
  1899. int yaffs_fchmod(int fd, mode_t mode)
  1900. {
  1901. struct yaffs_obj *obj;
  1902. int retVal = -1;
  1903. if (mode & ~(0777)) {
  1904. yaffsfs_SetError(-EINVAL);
  1905. return -1;
  1906. }
  1907. yaffsfs_Lock();
  1908. obj = yaffsfs_HandleToObject(fd);
  1909. if (!obj)
  1910. yaffsfs_SetError(-EBADF);
  1911. else if (obj->my_dev->read_only)
  1912. yaffsfs_SetError(-EROFS);
  1913. else
  1914. retVal = yaffsfs_DoChMod(obj, mode);
  1915. yaffsfs_Unlock();
  1916. return retVal;
  1917. }
  1918. int yaffs_mkdir(const YCHAR *path, mode_t mode)
  1919. {
  1920. struct yaffs_obj *parent = NULL;
  1921. struct yaffs_obj *dir = NULL;
  1922. YCHAR *name;
  1923. YCHAR *alt_path = NULL;
  1924. int retVal = -1;
  1925. int notDir = 0;
  1926. int loop = 0;
  1927. if (!path) {
  1928. yaffsfs_SetError(-EFAULT);
  1929. return -1;
  1930. }
  1931. if (yaffsfs_CheckPath(path) < 0) {
  1932. yaffsfs_SetError(-ENAMETOOLONG);
  1933. return -1;
  1934. }
  1935. if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
  1936. yaffsfs_SetError(-ENOMEM);
  1937. return -1;
  1938. }
  1939. if (alt_path)
  1940. path = alt_path;
  1941. yaffsfs_Lock();
  1942. parent = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
  1943. if (!parent && notDir)
  1944. yaffsfs_SetError(-ENOTDIR);
  1945. else if (loop)
  1946. yaffsfs_SetError(-ELOOP);
  1947. else if (!parent)
  1948. yaffsfs_SetError(-ENOENT);
  1949. else if (yaffsfs_TooManyObjects(parent->my_dev))
  1950. yaffsfs_SetError(-ENFILE);
  1951. else if (yaffs_strnlen(name, 5) == 0) {
  1952. /* Trying to make the root itself */
  1953. yaffsfs_SetError(-EE

Large files files are truncated, but you can click here to view the full file