shim_namei.c 26 KB

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