shim_namei.c 25 KB

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