shim_namei.c 25 KB

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