shim_namei.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. /* Copyright (C) 2017, University of North Carolina at Chapel Hill
  2. and Fortanix, Inc.
  3. This file is part of Graphene Library OS.
  4. Graphene Library OS is free software: you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public License
  6. as published by the Free Software Foundation, either version 3 of the
  7. License, or (at your option) any later version.
  8. Graphene Library OS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. /*
  15. * shim_namei.c
  16. *
  17. * This file contains codes for parsing a FS path and looking up in the
  18. * directory cache.
  19. */
  20. #include <asm/fcntl.h>
  21. #include <linux/fcntl.h>
  22. #include <linux/stat.h>
  23. #include <stdbool.h>
  24. #include <pal.h>
  25. #include <shim_fs.h>
  26. #include <shim_handle.h>
  27. #include <shim_internal.h>
  28. #include <shim_profile.h>
  29. #include <shim_thread.h>
  30. #include <shim_utils.h>
  31. /* Advances a char pointer (string) past any repeated slashes and returns the result.
  32. * Must be a null-terminated string. */
  33. static inline const char * eat_slashes (const char * string)
  34. {
  35. while (*string == '/')
  36. string++;
  37. return string;
  38. }
  39. static inline int __lookup_flags (int flags)
  40. {
  41. int retval = LOOKUP_FOLLOW;
  42. if (flags & O_NOFOLLOW)
  43. retval &= ~LOOKUP_FOLLOW;
  44. if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  45. retval &= ~LOOKUP_FOLLOW;
  46. if (flags & O_DIRECTORY)
  47. retval |= LOOKUP_DIRECTORY;
  48. return retval;
  49. }
  50. /* check permission (specified by mask) of a dentry. If force is not set,
  51. * permission is considered granted on invalid dentries
  52. *
  53. * mask is the same as mode in the manual for access(2): F_OK, R_OK, W_OK,
  54. * X_OK
  55. *
  56. * Returns 0 on success, negative on failure.
  57. */
  58. /* Assume caller has acquired dcache_lock */
  59. int __permission(struct shim_dentry* dent, mode_t mask) {
  60. assert(locked(&dcache_lock));
  61. mode_t mode = 0;
  62. /* Pseudo dentries don't really have permssions. I wonder if
  63. * we could tighten up the range of allowed calls.
  64. */
  65. if (dent->state & DENTRY_ANCESTOR)
  66. return 0;
  67. if (dent->state & DENTRY_NEGATIVE)
  68. return -ENOENT;
  69. /* At this point, we can just return zero if we are only
  70. * checking F_OK (the dentry isn't negative). */
  71. if (mask == F_OK)
  72. return 0;
  73. /* A dentry may not have the mode stored. The original code
  74. * used both NO_MODE and !DENTRY_VALID; let's try to consolidate to
  75. * just NO_MODE.
  76. */
  77. if (dent->mode == NO_MODE) {
  78. /* DEP 6/16/17: I don't think we should be defaulting to 0 if
  79. * there isn't a mode function. */
  80. assert(dent->fs);
  81. assert(dent->fs->d_ops);
  82. assert(dent->fs->d_ops->mode);
  83. /* Fall back to the low-level file system */
  84. int err = dent->fs->d_ops->mode(dent, &mode);
  85. /*
  86. * DEP 6/16/17: I think the low-level file system should be
  87. * setting modes, rather than defaulting to open here.
  88. * I'm ok with a file system that doesn't care setting the
  89. * permission to all.
  90. *
  91. * Set an assertion here to catch these cases in debugging.
  92. */
  93. assert(err != -ESKIPPED);
  94. if (err < 0)
  95. return err;
  96. dent->mode = mode;
  97. }
  98. mode = dent->mode;
  99. if (((mode >> 6) & mask) == mask)
  100. return 0;
  101. return -EACCES;
  102. }
  103. /*
  104. * This function looks up a single dentry based on its parent dentry pointer
  105. * and the name. Namelen is the length of char * name.
  106. * The dentry is returned in pointer *new. The refcount of new is incremented
  107. * by one.
  108. *
  109. * Parent can be null when mounting the root file system. In this case, the
  110. * function creates a new, negative dentry, which will then be initialized by
  111. * the mount code and made non-negative.
  112. *
  113. * The fs argument specifies the file system type to use on a miss; typically,
  114. * this will be parent->fs.
  115. *
  116. * This function checks the dcache first, and then, on a miss, falls back
  117. * to the low-level file system.
  118. *
  119. * XXX: The original code returned whether the process can exec the task?
  120. * Not clear this is needed; try not doing this.
  121. *
  122. * Returns zero if the file is found; -ENOENT if it doesn't exist,
  123. * possibly other errors.
  124. *
  125. * The caller should hold the dcache_lock.
  126. */
  127. int lookup_dentry (struct shim_dentry * parent, const char * name, int namelen, struct shim_dentry ** new, struct shim_mount * fs)
  128. {
  129. assert(locked(&dcache_lock));
  130. struct shim_dentry * dent = NULL;
  131. int do_fs_lookup = 0;
  132. int err = 0;
  133. /* Look up the name in the dcache first, one atom at a time. */
  134. dent = __lookup_dcache(parent, name, namelen, NULL);
  135. if (!dent) {
  136. if (parent) {
  137. /* Newly created dentry's relative path will be a concatenation of parent
  138. * + name strings (see get_new_dentry), make sure it fits into qstr */
  139. if (parent->rel_path.len + 1 + namelen >= STR_SIZE) { /* +1 for '/' */
  140. debug("Relative path exceeds the limit %d\n", STR_SIZE);
  141. err = -ENAMETOOLONG;
  142. goto out;
  143. }
  144. }
  145. dent = get_new_dentry(fs, parent, name, namelen, NULL);
  146. if (!dent) {
  147. err = -ENOMEM;
  148. goto out;
  149. }
  150. do_fs_lookup = 1;
  151. } else {
  152. if (!(dent->state & DENTRY_VALID))
  153. do_fs_lookup = 1;
  154. }
  155. if (do_fs_lookup) {
  156. // This doesn't make any sense if there isn't a low-level
  157. // lookup function.
  158. assert(dent->fs);
  159. assert(dent->fs->d_ops);
  160. assert(dent->fs->d_ops->lookup);
  161. err = dent->fs->d_ops->lookup(dent);
  162. /* XXX: On an error, it seems like we should probably destroy
  163. * the dentry, rather than keep some malformed dentry lying around.
  164. * Not done in original code, so leaving for now.
  165. */
  166. if (err) {
  167. if (err == -ENOENT || err == -EACCES) {
  168. /* Non-existing files and inaccessible files are marked as
  169. * negative dentries, so they can still be cached */
  170. dent->state |= DENTRY_NEGATIVE;
  171. } else {
  172. /* Trying to weed out ESKIPPED */
  173. assert(err != -ESKIPPED);
  174. goto out;
  175. }
  176. }
  177. dent->state |= DENTRY_VALID;
  178. }
  179. /* I think we can assume we have a valid dent at this point */
  180. assert(dent);
  181. assert(dent->state & DENTRY_VALID);
  182. // Set the err is ENOENT if negative
  183. if (dent->state & DENTRY_NEGATIVE)
  184. err = -ENOENT;
  185. if (new) {
  186. get_dentry(dent);
  187. *new = dent;
  188. }
  189. out:
  190. if (dent) {
  191. put_dentry(dent);
  192. }
  193. return err;
  194. }
  195. /*
  196. * Looks up path under start dentry. Saves in dent (if specified; dent may be
  197. * NULL). Start is a hint, and may be null; in this case, we use the first
  198. * char of the path to infer whether the path is relative or absolute.
  199. *
  200. * Primarily to support bootstrapping, this function takes a fs parameter,
  201. * that specifies a mount point. Typically, this would be start->fs, but there
  202. * are cases where start may be absent (e.g., in bootstrapping). Fs can be
  203. * null only if the current thread is defined.
  204. *
  205. * Assumes dcache_lock is held; main difference from path_lookupat is that
  206. * dcache_lock is acquired/released.
  207. *
  208. * We assume the caller has incremented the reference count on start and its
  209. * associated file system mount by one
  210. *
  211. * The refcount is raised by one on the returned dentry and associated mount.
  212. *
  213. * The make_ancestor flag creates pseudo-dentries for any parent paths that
  214. * are not in cache and do not exist on the underlying file system. This is
  215. * intended for use only in setting up the file system view specified in the
  216. * manifest.
  217. *
  218. * If the file isn't found, returns -ENOENT.
  219. *
  220. * If the LOOKUP_DIRECTORY flag is set, and the found file isn't a directory,
  221. * returns -ENOTDIR.
  222. */
  223. int __path_lookupat (struct shim_dentry * start, const char * path, int flags,
  224. struct shim_dentry ** dent, int link_depth,
  225. struct shim_mount * fs, bool make_ancestor)
  226. {
  227. assert(locked(&dcache_lock));
  228. // Basic idea: recursively iterate over path, peeling off one atom at a
  229. // time.
  230. /* Chia-Che 12/5/2014:
  231. * XXX I am not a big fan of recursion. I am giving a pass to this code
  232. * for now, but eventually someone (probably me) should rewrite it. */
  233. const char * my_path;
  234. int my_pathlen = 0;
  235. int err = 0;
  236. struct shim_dentry *my_dent = NULL;
  237. struct shim_qstr this = QSTR_INIT;
  238. bool leaf_case = false; // Leaf call in case of recursion
  239. bool no_start = false; // start not passed
  240. bool no_fs = false; // fs not passed
  241. struct shim_thread * cur_thread = get_cur_thread();
  242. if (cur_thread && *path == '/') {
  243. /*
  244. * Allow (start != NULL, absolute path) for *at() system calls.
  245. * which are common case as normal namei path resolution.
  246. */
  247. start = cur_thread->root;
  248. no_start = true;
  249. get_dentry(start);
  250. fs = NULL;
  251. }
  252. if (!start) {
  253. if (cur_thread) {
  254. start = cur_thread->cwd;
  255. } else {
  256. /* Start at the global root if we have no fs and no start dentry.
  257. * This should only happen as part of initialization.
  258. */
  259. start = dentry_root;
  260. assert(start);
  261. }
  262. no_start = true;
  263. // refcount should only be incremented if the caller didn't do it
  264. get_dentry(start);
  265. assert(fs == NULL);
  266. }
  267. assert(start);
  268. if (!fs) {
  269. no_fs = true;
  270. fs = start->fs;
  271. // refcount should only be incremented if the caller didn't do it
  272. get_mount(fs);
  273. }
  274. assert(fs);
  275. assert(start->state & DENTRY_ISDIRECTORY);
  276. // Peel off any preceeding slashes
  277. path = eat_slashes(path);
  278. // Check that we didn't hit the leaf case
  279. if (*path == '\0') {
  280. // We'll return start since this is the last path element
  281. my_dent = start;
  282. // Increment refcount of the found entry
  283. get_dentry(my_dent);
  284. leaf_case = true;
  285. } else {
  286. my_path = path;
  287. // Find the length of the path
  288. while (*my_path != '/' && *my_path != '\0') {
  289. my_path++;
  290. my_pathlen++;
  291. }
  292. if (my_pathlen > MAX_FILENAME) {
  293. err = -ENAMETOOLONG;
  294. goto out;
  295. }
  296. /* Handle . */
  297. if (my_pathlen == 1 && *path == '.') {
  298. /* For the recursion to work, we need to do the following:
  299. * Bump the ref count, set my_dent to start
  300. */
  301. my_dent = start;
  302. get_dentry(my_dent);
  303. } else if (my_pathlen == 2 && path[0] == '.' && path[1] == '.') {
  304. if (start->parent) {
  305. my_dent = start->parent;
  306. } else {
  307. // Root
  308. my_dent = start;
  309. }
  310. get_dentry(my_dent);
  311. } else {
  312. // Once we have an atom, look it up and update start
  313. err = lookup_dentry(start, path, my_pathlen, &my_dent, fs);
  314. // my_dent's refcount is incremented after this, consistent with cases above
  315. // Allow a negative dentry to move forward
  316. if (err < 0 && err != -ENOENT)
  317. goto out;
  318. // Drop any trailing slashes from the path
  319. my_path = eat_slashes(my_path);
  320. // If the LOOKUP_FOLLOW flag is set, check if we hit a symlink
  321. if ((flags & LOOKUP_FOLLOW) && (my_dent->state & DENTRY_ISLINK)) {
  322. // Keep from following too many links
  323. if (link_depth > 80) {
  324. err = -ELOOP;
  325. goto out;
  326. }
  327. link_depth++;
  328. assert(my_dent->fs->d_ops && my_dent->fs->d_ops->follow_link);
  329. if ((err = my_dent->fs->d_ops->follow_link(my_dent, &this)) < 0)
  330. goto out;
  331. path = qstrgetstr(&this);
  332. if (path) {
  333. /* symlink name starts with a slash, restart lookup at root */
  334. if (*path == '/') {
  335. struct shim_dentry * root;
  336. // not sure how to deal with this case if cur_thread isn't defined
  337. assert(cur_thread);
  338. root = cur_thread->root;
  339. /*XXX: Check out path_reacquire here? */
  340. // my_dent's refcount was incremented by lookup_dentry above,
  341. // we need to not leak it here
  342. put_dentry(my_dent);
  343. my_dent = root;
  344. get_dentry(my_dent);
  345. } else {
  346. // Relative path, stay in this dir
  347. put_dentry(my_dent);
  348. my_dent = start;
  349. get_dentry(my_dent);
  350. }
  351. }
  352. }
  353. }
  354. // Drop any trailing slashes from the path
  355. my_path = eat_slashes(my_path);
  356. // If we found something, and there is more, recur
  357. if (*my_path != '\0') {
  358. /* If we have more to look up, but got a negative DENTRY,
  359. * we need to fail or (unlikely) create an ancestor dentry.*/
  360. if (my_dent->state & DENTRY_NEGATIVE) {
  361. if (make_ancestor) {
  362. my_dent->state |= DENTRY_ANCESTOR;
  363. my_dent->state |= DENTRY_ISDIRECTORY;
  364. my_dent->state &= ~DENTRY_NEGATIVE;
  365. } else {
  366. err = -ENOENT;
  367. goto out;
  368. }
  369. }
  370. /* Although this is slight over-kill, let's just always increment the
  371. * mount ref count on a recursion, for easier bookkeeping */
  372. get_mount(my_dent->fs);
  373. err = __path_lookupat (my_dent, my_path, flags, dent, link_depth,
  374. my_dent->fs, make_ancestor);
  375. if (err < 0)
  376. goto out;
  377. /* If we aren't returning a live reference to the target dentry, go
  378. * ahead and release the ref count when we unwind the recursion.
  379. */
  380. put_mount(my_dent->fs);
  381. put_dentry(my_dent);
  382. } else {
  383. /* If make_ancestor is set, we also need to handle the case here */
  384. if (make_ancestor && (my_dent->state & DENTRY_NEGATIVE)) {
  385. my_dent->state |= DENTRY_ANCESTOR;
  386. my_dent->state |= DENTRY_ISDIRECTORY;
  387. my_dent->state &= ~DENTRY_NEGATIVE;
  388. if (err == -ENOENT)
  389. err = 0;
  390. }
  391. leaf_case = true;
  392. }
  393. }
  394. /* Base case. Set dent and return. */
  395. if (leaf_case) {
  396. if (dent)
  397. *dent = my_dent;
  398. // Enforce LOOKUP_CREATE flag at a higher level
  399. if (my_dent->state & DENTRY_NEGATIVE) {
  400. err = -ENOENT;
  401. goto out;
  402. }
  403. // Enforce the LOOKUP_DIRECTORY flag
  404. if ((flags & LOOKUP_DIRECTORY) && !(my_dent->state & DENTRY_ISDIRECTORY))
  405. err = -ENOTDIR;
  406. }
  407. out:
  408. /* If we didn't have a start dentry, decrement the ref count here */
  409. if (no_start)
  410. put_dentry(start);
  411. if (no_fs)
  412. put_mount(fs);
  413. qstrfree(&this);
  414. return err;
  415. }
  416. /* Just wraps __path_lookupat, but also acquires and releases the dcache_lock.
  417. */
  418. int path_lookupat (struct shim_dentry * start, const char * name, int flags,
  419. struct shim_dentry ** dent, struct shim_mount * fs)
  420. {
  421. int ret = 0;
  422. lock(&dcache_lock);
  423. ret = __path_lookupat (start, name, flags, dent, 0, fs, 0);
  424. unlock(&dcache_lock);
  425. return ret;
  426. }
  427. /* Open path with given flags, in mode, similar to Unix open.
  428. *
  429. * The start dentry specifies where to begin the search, and can be null. If
  430. * specified, we assume the caller has incremented the ref count on the start,
  431. * but not the associated mount (probably using get_dirfd_dentry)
  432. *
  433. * hdl is an optional argument; if passed in, it is initialized to
  434. * refer to the opened path.
  435. *
  436. * We assume the caller has not increased
  437. *
  438. * The result is stored in dent.
  439. */
  440. int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
  441. const char * path, int flags, int mode,
  442. struct shim_dentry ** dent)
  443. {
  444. int lookup_flags = __lookup_flags(flags);
  445. mode_t acc_mode = ACC_MODE(flags & O_ACCMODE);
  446. int err = 0, newly_created = 0;
  447. struct shim_dentry *mydent = NULL;
  448. if (*path == '\0') {
  449. /* corner case: trying to open with empty filename */
  450. return -ENOENT;
  451. }
  452. lock(&dcache_lock);
  453. // lookup the path from start, passing flags
  454. err = __path_lookupat(start, path, lookup_flags, &mydent, 0, NULL, 0);
  455. if (mydent && (mydent->state & DENTRY_ISDIRECTORY)) {
  456. if (flags & O_WRONLY || flags & O_RDWR) {
  457. err = -EISDIR;
  458. goto out;
  459. }
  460. }
  461. // Deal with O_CREAT, O_EXCL, but only if we actually got a valid prefix
  462. // of directories.
  463. if (mydent && err == -ENOENT && (flags & O_CREAT)) {
  464. // Create the file
  465. struct shim_dentry * dir = mydent->parent;
  466. if (!dir) {
  467. err = -ENOENT;
  468. goto out;
  469. }
  470. // Check the parent permission first
  471. err = __permission(dir, MAY_WRITE | MAY_EXEC);
  472. if (err) goto out;
  473. // Try EINVAL when creat isn't an option
  474. if (!dir->fs->d_ops || !dir->fs->d_ops->creat) {
  475. err = -EINVAL;
  476. goto out;
  477. }
  478. // Differentiate directory and file creation;
  479. // Seems like overloading functionality that could probably be more
  480. // cleanly pushed into shim_do_mkdir
  481. if (flags & O_DIRECTORY) {
  482. err = dir->fs->d_ops->mkdir(dir, mydent, mode);
  483. } else {
  484. err = dir->fs->d_ops->creat(hdl, dir, mydent, flags, mode);
  485. }
  486. if (err)
  487. goto out;
  488. newly_created = 1;
  489. // If we didn't get an error and made a directory, set the dcache dir flag
  490. if (flags & O_DIRECTORY) {
  491. mydent->state |= DENTRY_ISDIRECTORY;
  492. mydent->type = S_IFDIR;
  493. }
  494. // Once the dentry is creat-ed, drop the negative flag
  495. mydent->state &= ~DENTRY_NEGATIVE;
  496. // Set err back to zero and fall through
  497. err = 0;
  498. } else if (err == 0 && ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))) {
  499. err = -EEXIST;
  500. }
  501. if (err < 0) {
  502. goto out;
  503. }
  504. // Check permission, but only if we didn't create the file.
  505. // creat/O_CREAT have idiosyncratic semantics about opening a
  506. // newly-created, read-only file for writing, but only the first time.
  507. if (!newly_created) {
  508. if ((err = __permission(mydent, acc_mode)) < 0)
  509. goto out;
  510. }
  511. // Set up the file handle, if provided
  512. if (hdl)
  513. err = dentry_open(hdl, mydent, flags);
  514. out:
  515. if (dent && !err)
  516. *dent = mydent;
  517. unlock(&dcache_lock);
  518. return err;
  519. }
  520. /* This function calls the low-level file system to do the work
  521. * of opening file indicated by dent, and initializing it in hdl.
  522. * Flags are standard open flags.
  523. *
  524. * If O_TRUNC is specified, this function is responsible for calling
  525. * the underlying truncate function.
  526. */
  527. int dentry_open (struct shim_handle * hdl, struct shim_dentry * dent,
  528. int flags)
  529. {
  530. int ret = 0;
  531. size_t size;
  532. char *path;
  533. struct shim_mount * fs = dent->fs;
  534. /* I think missing functionality should be treated as EINVAL, or maybe
  535. * ENOSYS?*/
  536. if (!fs->d_ops || !fs->d_ops->open) {
  537. ret = -EINVAL;
  538. goto out;
  539. }
  540. if ((ret = fs->d_ops->open(hdl, dent, flags)) < 0)
  541. goto out;
  542. set_handle_fs(hdl, fs);
  543. get_dentry(dent);
  544. hdl->dentry = dent;
  545. hdl->flags = flags;
  546. // Set the type of the handle if we have a directory. The original code
  547. // had a special case for this.
  548. // XXX: Having a type on the handle seems a little redundant if we have a
  549. // dentry too.
  550. if (dent->state & DENTRY_ISDIRECTORY) {
  551. hdl->type = TYPE_DIR;
  552. memcpy(hdl->fs_type, fs->type, sizeof(fs->type));
  553. // Set dot and dot dot for some reason
  554. get_dentry(dent);
  555. hdl->dir_info.dot = dent;
  556. if (dent->parent) {
  557. get_dentry(dent->parent);
  558. hdl->dir_info.dotdot = dent->parent;
  559. } else {
  560. hdl->dir_info.dotdot = NULL;
  561. }
  562. // Let's defer setting the DENTRY_LISTED flag until we need it
  563. // Use -1 to indicate that the buf/ptr isn't initialized
  564. hdl->dir_info.buf = (void *)-1;
  565. hdl->dir_info.ptr = (void *)-1;
  566. }
  567. path = dentry_get_path(dent, true, &size);
  568. if (!path) {
  569. ret = -ENOMEM;
  570. goto out;
  571. }
  572. qstrsetstr(&hdl->path, path, size);
  573. /* truncate regular writable file if O_TRUNC is given */
  574. if ((flags & O_TRUNC) &&
  575. ((flags & O_RDWR) | (flags & O_WRONLY)) &&
  576. !(dent->state & DENTRY_ISDIRECTORY) &&
  577. !(dent->state & DENTRY_MOUNTPOINT) &&
  578. !(dent->state & DENTRY_ISLINK)) {
  579. if (!fs->fs_ops->truncate) {
  580. ret = -EINVAL;
  581. goto out;
  582. }
  583. ret = fs->fs_ops->truncate(hdl, 0);
  584. }
  585. out:
  586. return ret;
  587. }
  588. static inline void set_dirent_type (mode_t * type, int d_type)
  589. {
  590. switch (d_type) {
  591. case LINUX_DT_DIR:
  592. *type = S_IFDIR;
  593. return;
  594. case LINUX_DT_FIFO:
  595. *type = S_IFIFO;
  596. return;
  597. case LINUX_DT_CHR:
  598. *type = S_IFCHR;
  599. return;
  600. case LINUX_DT_BLK:
  601. *type = S_IFBLK;
  602. return;
  603. case LINUX_DT_REG:
  604. *type = S_IFREG;
  605. return;
  606. case LINUX_DT_LNK:
  607. *type = S_IFLNK;
  608. return;
  609. case LINUX_DT_SOCK:
  610. *type = S_IFSOCK;
  611. return;
  612. default:
  613. *type = 0;
  614. return;
  615. }
  616. }
  617. /* This function enumerates a directory and caches the results in the cache.
  618. *
  619. * Input: A dentry for a directory in the DENTRY_ISDIRECTORY and not in the
  620. * DENTRY_LISTED state. The dentry DENTRY_LISTED flag is set upon success.
  621. *
  622. * Return value: 0 on success, <0 on error
  623. *
  624. * DEP 7/9/17: This work was once done as part of open, but, since getdents*
  625. * have no consistency semantics, we can apply the principle of laziness and
  626. * not do the work until we are sure we really need to.
  627. */
  628. int list_directory_dentry (struct shim_dentry *dent) {
  629. int ret = 0;
  630. struct shim_mount * fs = dent->fs;
  631. lock(&dcache_lock);
  632. /* DEP 8/4/17: Another process could list this directory
  633. * while we are waiting on the dcache lock. This is ok,
  634. * no need to blow an assert.
  635. */
  636. if (dent->state & DENTRY_LISTED){
  637. unlock(&dcache_lock);
  638. return 0;
  639. }
  640. // DEP 7/9/17: In yet another strange turn of events in POSIX-land,
  641. // you can do a readdir on a rmdir-ed directory handle. What you
  642. // expect to learn is beyond me, but be careful with blowing assert
  643. // and tell the program something to keep it moving.
  644. if (dent->state & DENTRY_NEGATIVE) {
  645. unlock(&dcache_lock);
  646. return 0;
  647. }
  648. assert(dent->state & DENTRY_ISDIRECTORY);
  649. struct shim_dirent * dirent = NULL;
  650. if ((ret = fs->d_ops->readdir(dent, &dirent)) < 0 || !dirent) {
  651. dirent = NULL;
  652. goto done_read;
  653. }
  654. struct shim_dirent * d = dirent;
  655. for ( ; d ; d = d->next) {
  656. struct shim_dentry * child;
  657. if ((ret = lookup_dentry(dent, d->name, strlen(d->name),
  658. &child, fs)) < 0) {
  659. if (ret != -ENOENT) {
  660. /* if the file is recently deleted or inaccessible, ignore it */
  661. goto done_read;
  662. }
  663. }
  664. if (child->state & DENTRY_NEGATIVE)
  665. continue;
  666. if (!(child->state & DENTRY_VALID)) {
  667. set_dirent_type(&child->type, d->type);
  668. child->state |= DENTRY_VALID|DENTRY_RECENTLY;
  669. }
  670. child->ino = d->ino;
  671. }
  672. /* Once DENTRY_LISTED is set, the ino of the newly created file will not be updated, so its
  673. * ino needs to be set in create() or open(O_CREAT). */
  674. dent->state |= DENTRY_LISTED;
  675. ret = 0;
  676. done_read:
  677. unlock(&dcache_lock);
  678. free(dirent);
  679. return ret;
  680. }
  681. /* This function caches the contents of a directory (dent), already
  682. * in the listed state, in a buffer associated with a handle (hdl).
  683. *
  684. * This function should only be called once on a handle.
  685. *
  686. * Returns 0 on success, <0 on failure.
  687. */
  688. int list_directory_handle (struct shim_dentry * dent, struct shim_handle * hdl)
  689. {
  690. struct shim_dentry ** children = NULL;
  691. int nchildren = dent->nchildren, count = 0;
  692. struct shim_dentry * child;
  693. assert(hdl->dir_info.buf == (void*)-1);
  694. assert(hdl->dir_info.ptr == (void*)-1);
  695. // Handle the case where the handle is on a rmdir-ed directory
  696. // Handle is already locked by caller, so these values shouldn't change
  697. // after dcache lock is acquired
  698. if (dent->state & DENTRY_NEGATIVE) {
  699. hdl->dir_info.buf = NULL;
  700. hdl->dir_info.ptr = NULL;
  701. return 0;
  702. }
  703. children = malloc(sizeof(struct shim_dentry *) * (nchildren + 1));
  704. if (!children)
  705. return -ENOMEM;
  706. lock(&dcache_lock);
  707. LISTP_FOR_EACH_ENTRY(child, &dent->children, siblings) {
  708. if (count >= nchildren)
  709. break;
  710. struct shim_dentry * c = child;
  711. while (c->state & DENTRY_MOUNTPOINT)
  712. c = c->mounted->root;
  713. if (c->state & DENTRY_VALID) {
  714. get_dentry(c);
  715. children[count++] = c;
  716. }
  717. }
  718. children[count] = NULL;
  719. hdl->dir_info.buf = children;
  720. hdl->dir_info.ptr = children;
  721. unlock(&dcache_lock);
  722. return 0;
  723. }
  724. int get_dirfd_dentry(int dirfd, struct shim_dentry** dir) {
  725. if (dirfd == AT_FDCWD) {
  726. struct shim_thread* cur = get_cur_thread();
  727. get_dentry(cur->cwd);
  728. *dir = cur->cwd;
  729. return 0;
  730. }
  731. if (dirfd < 0) {
  732. return -EBADF;
  733. }
  734. struct shim_handle* hdl = get_fd_handle(dirfd, NULL, NULL);
  735. if (!hdl) {
  736. return -EBADF;
  737. }
  738. if (hdl->type != TYPE_DIR) {
  739. put_handle(hdl);
  740. return -ENOTDIR;
  741. }
  742. get_dentry(hdl->dentry);
  743. *dir = hdl->dentry;
  744. put_handle(hdl);
  745. return 0;
  746. }