shim_namei.c 26 KB

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