shim_namei.c 26 KB

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