shim_namei.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_namei.c
  17. *
  18. * This file contains codes for parsing a FS path and looking up in the
  19. * directory cache.
  20. * The source codes are imported from Linux kernel, but simplified according
  21. * to the characteristic of library OS.
  22. */
  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 <errno.h>
  31. #include <fcntl.h>
  32. /* check permission of a dentry. If force is not set, permission
  33. is consider granted on invalid dentries */
  34. /* have dcache_lock acquired */
  35. int permission (struct shim_dentry * dent, int mask, bool force)
  36. {
  37. mode_t mode = 0;
  38. if (dent->state & DENTRY_ANCESTER)
  39. return 0;
  40. if (dent->state & DENTRY_NEGATIVE)
  41. return -ENOENT;
  42. if (!(dent->state & DENTRY_VALID) || dent->mode == NO_MODE) {
  43. if (!dent->fs || !dent->fs->d_ops || !dent->fs->d_ops->mode)
  44. return 0;
  45. /* the filesystem will decide the results when permission
  46. check isn't forced. If -ESKIPPED is returned, we assume
  47. the file/directory is accessible for now. */
  48. int err = dent->fs->d_ops->mode(dent, &mode, force);
  49. if (err == -ESKIPPED)
  50. return 0;
  51. if (err < 0)
  52. return err;
  53. if (dent->parent)
  54. dent->parent->nchildren++;
  55. dent->state |= DENTRY_VALID|DENTRY_RECENTLY;
  56. dent->mode = mode;
  57. } else {
  58. mode = dent->mode;
  59. }
  60. if (((mode >> 6) & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)
  61. return 0;
  62. return -EACCES;
  63. }
  64. static inline int __do_lookup_dentry (struct shim_dentry * dent, bool force)
  65. {
  66. int err = 0;
  67. if (!(dent->state & DENTRY_VALID) &&
  68. dent->fs && dent->fs->d_ops && dent->fs->d_ops->lookup) {
  69. if ((err = dent->fs->d_ops->lookup(dent, force)) < 0) {
  70. if (err == -ENOENT) {
  71. dent->state |= DENTRY_NEGATIVE;
  72. } else {
  73. if (err == -ESKIPPED)
  74. err = 0;
  75. return err;
  76. }
  77. }
  78. if (dent->parent)
  79. dent->parent->nchildren++;
  80. dent->state |= DENTRY_VALID|DENTRY_RECENTLY;
  81. }
  82. return 0;
  83. }
  84. /* looking up single dentry based on its parent and name */
  85. /* have dcache_lock acquired */
  86. int lookup_dentry (struct shim_dentry * parent, const char * name, int namelen,
  87. bool force, struct shim_dentry ** new)
  88. {
  89. struct shim_dentry * dent = NULL;
  90. int err = 0;
  91. HASHTYPE hash;
  92. dent = __lookup_dcache(parent, name, namelen, NULL, 0, &hash);
  93. if ((err = permission(parent, MAY_EXEC, false)) < 0) {
  94. if (dent)
  95. dent->state |= DENTRY_UNREACHABLE;
  96. goto out;
  97. }
  98. if (!dent) {
  99. dent = get_new_dentry(parent, name, namelen);
  100. if (!dent) {
  101. err = -ENOMEM;
  102. goto out;
  103. }
  104. if (parent->fs) {
  105. get_mount(parent->fs);
  106. dent->fs = parent->fs;
  107. }
  108. __set_parent_dentry(dent, parent);
  109. __add_dcache(dent, &hash);
  110. }
  111. err = __do_lookup_dentry(dent, force);
  112. dent->state |= DENTRY_REACHABLE;
  113. *new = dent;
  114. out:
  115. return err;
  116. }
  117. static void path_reacquire (struct lookup * look, struct shim_dentry * dent);
  118. /* looking up single dentry, but use struct lookup */
  119. /* have dcache_lock acquired */
  120. static int do_lookup (struct lookup * look, const char * name, int namelen,
  121. bool force)
  122. {
  123. int err = 0;
  124. struct shim_dentry * dent = NULL;
  125. if ((err = lookup_dentry(look->dentry, name, namelen,force, &dent)) < 0)
  126. goto fail;
  127. path_reacquire(look, dent);
  128. look->last = dentry_get_name(dent);
  129. look->last_type = LAST_NORM;
  130. fail:
  131. return err;
  132. }
  133. static int link_path_walk (const char * name, struct lookup * look);
  134. /* have dcache_lock acquired */
  135. void path_acquire (struct lookup * look)
  136. {
  137. if (look->dentry)
  138. get_dentry(look->dentry);
  139. if (look->mount)
  140. get_mount(look->mount);
  141. }
  142. /* have dcache_lock acquired */
  143. void path_release (struct lookup * look)
  144. {
  145. if (look->dentry)
  146. put_dentry(look->dentry);
  147. if (look->mount)
  148. put_mount(look->mount);
  149. }
  150. /* have dcache_lock acquired */
  151. static void path_reacquire (struct lookup * look, struct shim_dentry * dent)
  152. {
  153. struct shim_dentry * old_dent = look->dentry;
  154. struct shim_mount * old_mount = look->mount;
  155. if (dent && dent != old_dent) {
  156. get_dentry(dent);
  157. if (old_dent)
  158. put_dentry(old_dent);
  159. look->dentry = dent;
  160. }
  161. if (dent && dent->fs && dent->fs != old_mount) {
  162. get_mount(dent->fs);
  163. if (old_mount)
  164. put_mount(old_mount);
  165. look->mount = dent->fs;
  166. }
  167. }
  168. /* try follow a link where the dentry points to */
  169. /* have dcache_lock acquired */
  170. static inline int __do_follow_link (struct lookup * look)
  171. {
  172. int err = 0;
  173. struct shim_dentry * dent = look->dentry;
  174. assert(dent->state & DENTRY_ISLINK);
  175. assert(dent->fs->d_ops && dent->fs->d_ops->follow_link);
  176. struct shim_qstr this = QSTR_INIT;
  177. if ((err = dent->fs->d_ops->follow_link(dent, &this)) < 0)
  178. goto out;
  179. const char * link = qstrgetstr(&this);
  180. if (link) {
  181. /* symlink name starts with a slash, restart lookup at root */
  182. if (*link == '/') {
  183. struct shim_dentry * root = get_cur_thread()->root;
  184. path_reacquire(look, root);
  185. }
  186. look->flags |= LOOKUP_CONTINUE;
  187. /* now walk the whole link again */
  188. err = link_path_walk(link, look);
  189. }
  190. out:
  191. qstrfree(&this);
  192. return err;
  193. }
  194. /* follow links on a dentry until the last target */
  195. /* have dcache_lock acquired */
  196. static int follow_link (struct lookup * look)
  197. {
  198. int err = 0;
  199. int old_depth = look->depth;
  200. while (err >= 0 && look->dentry->state & DENTRY_ISLINK) {
  201. /* checks to contain link explosion */
  202. if (look->depth > 80) {
  203. err = -ELOOP;
  204. break;
  205. }
  206. look->depth++;
  207. err = __do_follow_link(look);
  208. }
  209. if (err < 0)
  210. look->depth = old_depth;
  211. return err;
  212. }
  213. /* follow a single dot-dot to the parent */
  214. /* have dcache_lock acquired */
  215. static int follow_dotdot (struct lookup * look)
  216. {
  217. struct shim_dentry * dent = look->dentry;
  218. struct shim_mount * mount = look->mount;
  219. struct shim_thread * cur_thread = get_cur_thread();
  220. while (1) {
  221. /* if it reaches the root of current filesystem,
  222. return immediately. */
  223. if (dent == cur_thread->root)
  224. break;
  225. if (dent != mount->root) {
  226. struct shim_dentry * parent = dent->parent;
  227. path_reacquire(look, parent);
  228. break;
  229. }
  230. struct shim_dentry * parent = mount->mount_point;
  231. path_reacquire(look, parent);
  232. dent = parent;
  233. mount = parent->fs;
  234. }
  235. return 0;
  236. }
  237. /* walk through a absolute path based on current lookup structure,
  238. across mount point, dot dot and symlinks */
  239. /* have dcache_lock acquired */
  240. static int link_path_walk (const char * name, struct lookup * look)
  241. {
  242. struct shim_dentry * dent = NULL;
  243. int err = 0;
  244. int lookup_flags = look->flags;
  245. /* remove all the slashes at the beginning */
  246. while (*name == '/')
  247. name++;
  248. if (!*name) {
  249. if (!(lookup_flags & LOOKUP_CONTINUE) &&
  250. (lookup_flags & LOOKUP_PARENT))
  251. path_reacquire(look, look->dentry->parent);
  252. goto out;
  253. }
  254. dent = look->dentry;
  255. if (look->depth)
  256. lookup_flags |= LOOKUP_FOLLOW;
  257. lookup_flags |= LOOKUP_CONTINUE;
  258. while (*name) {
  259. const char * this_name = look->last = name;
  260. int namelen = -1;
  261. char c;
  262. do {
  263. namelen++;
  264. c = name[namelen];
  265. } while (c && (c != '/'));
  266. name += namelen;
  267. if (!c) {
  268. lookup_flags &= ~LOOKUP_CONTINUE;
  269. } else {
  270. while (*(++name) == '/');
  271. if (!*name) {
  272. lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  273. lookup_flags &= ~LOOKUP_CONTINUE;
  274. }
  275. }
  276. look->last_type = LAST_NORM;
  277. if (this_name[0] == '.')
  278. switch (namelen) {
  279. case 1:
  280. look->last_type = LAST_DOT;
  281. break;
  282. case 2:
  283. if (this_name[1] == '.')
  284. look->last_type = LAST_DOTDOT;
  285. /* fallthrough */
  286. default:
  287. break;
  288. }
  289. if (!(lookup_flags & LOOKUP_CONTINUE) &&
  290. (lookup_flags & LOOKUP_PARENT))
  291. goto out;
  292. switch (look->last_type) {
  293. case LAST_DOT:
  294. continue;
  295. case LAST_DOTDOT:
  296. err = follow_dotdot(look);
  297. if (err < 0)
  298. goto out;
  299. /* fallthrough */
  300. default:
  301. break;
  302. }
  303. if (look->last_type == LAST_NORM) {
  304. /* actual lookup */
  305. err = do_lookup(look, this_name, namelen, false);
  306. if (err < 0)
  307. goto out;
  308. }
  309. if (look->dentry->state & DENTRY_ISLINK) {
  310. err = follow_link(look);
  311. if (err < 0)
  312. goto out;
  313. }
  314. assert(!(look->dentry->state & DENTRY_MOUNTPOINT));
  315. dent = look->dentry;
  316. if (!(dent->state & DENTRY_VALID) &&
  317. (look->flags & LOOKUP_SYNC && !(lookup_flags & LOOKUP_CONTINUE)) &&
  318. look->mount && look->mount->d_ops &&
  319. look->mount->d_ops->lookup) {
  320. err = look->mount->d_ops->lookup(dent, 1);
  321. if (err < 0) {
  322. if (err == -ENOENT) {
  323. if (dent->state & DENTRY_VALID && dent->parent)
  324. dent->parent->nchildren--;
  325. dent->state |= DENTRY_NEGATIVE;
  326. err = 0;
  327. } else {
  328. debug("lookup failure\n");
  329. goto out;
  330. }
  331. }
  332. if (!(dent->state & DENTRY_NEGATIVE) && dent->parent)
  333. dent->parent->nchildren++;
  334. dent->state |= DENTRY_VALID|DENTRY_RECENTLY;
  335. }
  336. if (dent->state & DENTRY_NEGATIVE) {
  337. if (lookup_flags & LOOKUP_CONTINUE) {
  338. if (!(dent->state & DENTRY_ANCESTER)) {
  339. err = -ENOENT;
  340. goto out;
  341. }
  342. } else {
  343. goto out;
  344. }
  345. }
  346. if (!(lookup_flags & LOOKUP_CONTINUE) &&
  347. (look->flags & LOOKUP_DIRECTORY) &&
  348. (dent->state & DENTRY_VALID) &&
  349. !(dent->state & DENTRY_ISDIRECTORY)) {
  350. err = -ENOTDIR;
  351. goto out;
  352. }
  353. }
  354. out:
  355. return err;
  356. }
  357. DEFINE_PROFILE_OCCURENCE(dcache_hit, dcache);
  358. DEFINE_PROFILE_OCCURENCE(dcache_miss, dcache);
  359. static int path_lookup_dcache (struct shim_dentry * start, const char * path,
  360. int flags,
  361. struct shim_dentry ** dent,
  362. struct shim_thread * cur_thread)
  363. {
  364. if (!start && cur_thread)
  365. start = *path == '/' ? cur_thread->root : cur_thread->cwd;
  366. const char * startpath = NULL;
  367. int startpathlen = 0;
  368. char * fullpath = __alloca(STR_SIZE);
  369. if (start) {
  370. startpath = dentry_get_path(start, true, &startpathlen);
  371. memcpy(fullpath, startpath, startpathlen);
  372. }
  373. char * name = fullpath + startpathlen;
  374. int namelen;
  375. if ((namelen = get_norm_path(path, name, STR_SIZE - startpathlen)) < 0)
  376. return namelen;
  377. struct shim_dentry * found =
  378. __lookup_dcache(start, name, namelen,
  379. fullpath, startpathlen + namelen, NULL);
  380. if (found) {
  381. INC_PROFILE_OCCURENCE(dcache_hit);
  382. if (flags & LOOKUP_SYNC) {
  383. int ret = __do_lookup_dentry(found, true);
  384. if (ret < 0) {
  385. put_dentry(found);
  386. return ret;
  387. }
  388. }
  389. if (!(found->state & DENTRY_NEGATIVE) &&
  390. !(found->state & DENTRY_ISDIRECTORY) &&
  391. flags & LOOKUP_DIRECTORY) {
  392. put_dentry(found);
  393. return -ENOTDIR;
  394. }
  395. if (!(found->state & (DENTRY_REACHABLE|DENTRY_UNREACHABLE))) {
  396. put_dentry(found);
  397. found = NULL;
  398. }
  399. } else {
  400. INC_PROFILE_OCCURENCE(dcache_miss);
  401. }
  402. *dent = found;
  403. return 0;
  404. }
  405. /* have dcache_lock acquired */
  406. static int path_lookup_walk (struct shim_dentry * start,
  407. const char * name, int flags,
  408. struct lookup * look,
  409. struct shim_thread * cur_thread)
  410. {
  411. struct shim_dentry * dent = start;
  412. if (!dent) {
  413. if (cur_thread)
  414. lock(cur_thread->lock);
  415. dent = (*name == '/' ?
  416. (cur_thread ? cur_thread->root : NULL) :
  417. (cur_thread ? cur_thread->cwd : NULL)) ? : dentry_root;
  418. if (cur_thread)
  419. unlock(cur_thread->lock);
  420. }
  421. while (dent->state & DENTRY_MOUNTPOINT)
  422. dent = dent->mounted->root;
  423. look->dentry = dent;
  424. look->mount = dent->fs;
  425. look->last = dentry_get_name(dent);
  426. look->last_type = LAST_ROOT;
  427. look->flags = flags;
  428. look->depth = 0;
  429. path_acquire(look);
  430. return link_path_walk(name, look);
  431. }
  432. int __path_lookupat (struct shim_dentry * start, const char * path, int flags,
  433. struct shim_dentry ** dent)
  434. {
  435. struct shim_thread * cur_thread = get_cur_thread();
  436. struct shim_dentry * found = NULL;
  437. int ret = 0;
  438. struct lookup look;
  439. ret = path_lookup_dcache(start, path, flags, &found, cur_thread);
  440. if (ret < 0)
  441. return ret;
  442. if (!found) {
  443. if ((ret = path_lookup_walk(start, path, flags, &look, cur_thread)) < 0)
  444. return ret;
  445. get_dentry(look.dentry);
  446. found = look.dentry;
  447. if (flags & LOOKUP_SYNC) {
  448. if ((ret = __do_lookup_dentry(found, true)) < 0)
  449. goto out_if;
  450. }
  451. if (!(found->state & DENTRY_ISDIRECTORY) &&
  452. flags & LOOKUP_DIRECTORY) {
  453. ret = -ENOTDIR;
  454. goto out_if;
  455. }
  456. out_if:
  457. path_release(&look);
  458. }
  459. if (found) {
  460. if (!ret && dent)
  461. *dent = found;
  462. else
  463. put_dentry(found);
  464. }
  465. return 0;
  466. }
  467. /* if path_lookup succeed, the returned dentry is pop'ed */
  468. int path_lookupat (struct shim_dentry * start, const char * path, int flags,
  469. struct shim_dentry ** dent)
  470. {
  471. struct shim_thread * cur_thread = get_cur_thread();
  472. struct shim_dentry * found = NULL;
  473. int ret = 0;
  474. struct lookup look;
  475. lock(dcache_lock);
  476. ret = path_lookup_dcache(start, path, flags, &found, cur_thread);
  477. if (ret < 0) {
  478. unlock(dcache_lock);
  479. return ret;
  480. }
  481. if (!found) {
  482. if ((ret = path_lookup_walk(start, path, flags, &look,
  483. cur_thread)) < 0)
  484. goto out;
  485. get_dentry(look.dentry);
  486. found = look.dentry;
  487. if (flags & LOOKUP_SYNC) {
  488. if ((ret = __do_lookup_dentry(found, true)) < 0)
  489. goto out_release;
  490. }
  491. if (found->state & DENTRY_NEGATIVE &&
  492. !(flags & LOOKUP_CREATE)) {
  493. ret = -ENOENT;
  494. goto out_release;
  495. }
  496. if (!(found->state & DENTRY_NEGATIVE) &&
  497. !(found->state & DENTRY_ISDIRECTORY) &&
  498. flags & LOOKUP_DIRECTORY) {
  499. ret = -ENOTDIR;
  500. goto out_release;
  501. }
  502. out_release:
  503. path_release(&look);
  504. }
  505. if (found) {
  506. if (!ret && dent)
  507. *dent = found;
  508. else
  509. put_dentry(found);
  510. }
  511. out:
  512. unlock(dcache_lock);
  513. return ret;
  514. }
  515. static inline int __lookup_flags (int flags)
  516. {
  517. int retval = LOOKUP_FOLLOW;
  518. if (flags & O_NOFOLLOW)
  519. retval &= ~LOOKUP_FOLLOW;
  520. if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  521. retval &= ~LOOKUP_FOLLOW;
  522. if (flags & O_DIRECTORY)
  523. retval |= LOOKUP_DIRECTORY;
  524. return retval;
  525. }
  526. int create_dentry (struct shim_handle * hdl, struct shim_dentry * dir,
  527. struct shim_dentry * dent, int flags, int mode)
  528. {
  529. int err = permission(dir, MAY_WRITE | MAY_EXEC, true);
  530. if (err)
  531. return err;
  532. if (!dir->fs->d_ops || !dir->fs->d_ops->creat)
  533. return -EACCES;
  534. err = dir->fs->d_ops->creat(hdl, dir, dent, flags, mode);
  535. if (err)
  536. return err;
  537. if (!hdl)
  538. return 0;
  539. set_handle_fs(hdl, dent->fs);
  540. get_dentry(dent);
  541. hdl->dentry = dent;
  542. hdl->flags = flags;
  543. int size;
  544. char *path = dentry_get_path(dent, true, &size);
  545. qstrsetstr(&hdl->path, path, size);
  546. return 0;
  547. }
  548. int create_directory (struct shim_dentry * dir, struct shim_dentry * dent,
  549. int mode)
  550. {
  551. int err = permission(dir, MAY_WRITE | MAY_EXEC, true);
  552. if (err)
  553. return err;
  554. if (!dir->fs->d_ops || !dir->fs->d_ops->mkdir)
  555. return -EACCES;
  556. return dir->fs->d_ops->mkdir(dir, dent, mode);
  557. }
  558. DEFINE_PROFILE_CATAGORY(open_namei, dcache);
  559. DEFINE_PROFILE_INTERVAL(path_lookup_dcache_for_open_namei, open_namei);
  560. DEFINE_PROFILE_INTERVAL(path_lookup_walk_for_open_namei, open_namei);
  561. DEFINE_PROFILE_INTERVAL(path_lookup_walk_2_for_open_namei, open_namei);
  562. DEFINE_PROFILE_INTERVAL(end_open_namei, open_namei);
  563. DEFINE_PROFILE_INTERVAL(open_namei_permission, open_namei);
  564. DEFINE_PROFILE_INTERVAL(open_namei_dir_open, open_namei);
  565. DEFINE_PROFILE_INTERVAL(open_namei_dentry_open, open_namei);
  566. DEFINE_PROFILE_INTERVAL(open_namei_lookup_2, open_namei);
  567. DEFINE_PROFILE_INTERVAL(open_namei_path_reacquire, open_namei);
  568. DEFINE_PROFILE_INTERVAL(open_namei_create_dir, open_namei);
  569. DEFINE_PROFILE_INTERVAL(open_namei_create_dentry, open_namei);
  570. int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
  571. const char * path, int flags, int mode,
  572. struct shim_dentry ** dent)
  573. {
  574. struct shim_thread * cur_thread = get_cur_thread();
  575. struct lookup look = { .dentry = NULL, .mount = NULL };
  576. struct shim_dentry * dir = NULL;
  577. int err = 0;
  578. int acc_mode = ACC_MODE(flags & O_ACCMODE);
  579. int lookup_flags = __lookup_flags(flags);
  580. #ifdef MAY_APPEND
  581. if (flags & O_APPEND)
  582. acc_mode |= MAY_APPEND;
  583. #endif
  584. BEGIN_PROFILE_INTERVAL();
  585. lock(dcache_lock);
  586. err = path_lookup_dcache(start, path, lookup_flags|LOOKUP_OPEN,
  587. &look.dentry, cur_thread);
  588. if (err >= 0 && look.dentry) {
  589. look.mount = look.dentry->fs;
  590. if (look.mount)
  591. get_mount(look.mount);
  592. }
  593. SAVE_PROFILE_INTERVAL(path_lookup_dcache_for_open_namei);
  594. if (err < 0) {
  595. unlock(dcache_lock);
  596. SAVE_PROFILE_INTERVAL(end_open_namei);
  597. return err;
  598. }
  599. if (look.dentry) {
  600. if (look.dentry->state & DENTRY_NEGATIVE) {
  601. if (!(flags & O_CREAT)) {
  602. err = -ENOENT;
  603. goto exit;
  604. }
  605. dir = look.dentry->parent;
  606. get_dentry(dir);
  607. goto do_creat;
  608. }
  609. if (flags & O_EXCL) {
  610. err = -EEXIST;
  611. goto exit;
  612. }
  613. goto do_open_locked;
  614. }
  615. /* no create, just look it up. */
  616. if (!(flags & O_CREAT)) {
  617. err = path_lookup_walk(start, path, lookup_flags|LOOKUP_OPEN,
  618. &look, cur_thread);
  619. SAVE_PROFILE_INTERVAL(path_lookup_walk_for_open_namei);
  620. if (err) {
  621. debug("path_lookup error in open_namei\n");
  622. goto exit;
  623. }
  624. do_open_locked:
  625. unlock(dcache_lock);
  626. do_open:
  627. if ((err = permission(look.dentry, acc_mode, true)) < 0)
  628. goto exit;
  629. SAVE_PROFILE_INTERVAL(open_namei_permission);
  630. if (hdl) {
  631. if (look.dentry->state & DENTRY_ISDIRECTORY) {
  632. assert(flags & O_DIRECTORY);
  633. if ((err = directory_open(hdl, look.dentry, flags)) < 0)
  634. goto exit;
  635. SAVE_PROFILE_INTERVAL(open_namei_dir_open);
  636. } else {
  637. assert(!(flags & O_DIRECTORY));
  638. if ((err = dentry_open(hdl, look.dentry, flags)) < 0)
  639. goto exit;
  640. SAVE_PROFILE_INTERVAL(open_namei_dentry_open);
  641. }
  642. }
  643. goto done;
  644. }
  645. /* create, so we need the parent */
  646. err = path_lookup_walk(start, path, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE,
  647. &look, cur_thread);
  648. SAVE_PROFILE_INTERVAL(path_lookup_walk_2_for_open_namei);
  649. if (err < 0 || look.last_type != LAST_NORM)
  650. goto exit;
  651. struct shim_dentry * new = NULL;
  652. dir = look.dentry;
  653. err = lookup_dentry(dir, look.last, strlen(look.last), true, &new);
  654. SAVE_PROFILE_INTERVAL(open_namei_lookup_2);
  655. if (err < 0 && (err != -ENOENT || !new))
  656. goto exit;
  657. path_reacquire(&look, new);
  658. SAVE_PROFILE_INTERVAL(open_namei_path_reacquire);
  659. do_creat:
  660. assert(dir);
  661. unlock(dcache_lock);
  662. /* negative dentry */
  663. if (look.dentry->state & DENTRY_NEGATIVE) {
  664. if (flags & O_DIRECTORY) {
  665. if ((err = create_directory(dir, look.dentry, mode)) < 0) {
  666. debug("error: create directory in open_namei\n");
  667. goto exit;
  668. }
  669. SAVE_PROFILE_INTERVAL(open_namei_create_dir);
  670. look.dentry->state |= DENTRY_ISDIRECTORY;
  671. } else {
  672. if ((err = create_dentry(hdl, dir, look.dentry, flags,
  673. mode)) < 0) {
  674. debug("error: create file in open_namei\n");
  675. goto exit;
  676. }
  677. SAVE_PROFILE_INTERVAL(open_namei_create_dentry);
  678. }
  679. look.dentry->state &= ~DENTRY_NEGATIVE;
  680. if (hdl && (flags & O_DIRECTORY))
  681. goto do_open;
  682. else
  683. goto done;
  684. }
  685. /* existing dentry */
  686. if (flags & O_EXCL) {
  687. err = -EEXIST;
  688. debug("error: existing dentry with O_EXCL\n");
  689. goto exit;
  690. }
  691. if (look.dentry->state & DENTRY_ISLINK) {
  692. if (flags & O_NOFOLLOW) {
  693. err = -ELOOP;
  694. debug("error: linked dentry with O_NOFOLLOW\n");
  695. goto exit;
  696. }
  697. if ((err = follow_link(&look)) < 0)
  698. goto exit;
  699. }
  700. assert(!(look.dentry->state & DENTRY_MOUNTPOINT));
  701. goto do_open;
  702. done:
  703. if (dent) {
  704. get_dentry(look.dentry);
  705. *dent = look.dentry;
  706. }
  707. path_release(&look);
  708. if (locked(dcache_lock))
  709. unlock(dcache_lock);
  710. SAVE_PROFILE_INTERVAL(end_open_namei);
  711. return 0;
  712. exit:
  713. path_release(&look);
  714. if (dir)
  715. put_dentry(dir);
  716. if (locked(dcache_lock))
  717. unlock(dcache_lock);
  718. SAVE_PROFILE_INTERVAL(end_open_namei);
  719. return err;
  720. }
  721. DEFINE_PROFILE_CATAGORY(dentry_open, dcache);
  722. DEFINE_PROFILE_INTERVAL(dentry_open_open, dentry_open);
  723. DEFINE_PROFILE_INTERVAL(dentry_open_truncate, dentry_open);
  724. DEFINE_PROFILE_INTERVAL(dentry_open_set_path, dentry_open);
  725. int dentry_open (struct shim_handle * hdl, struct shim_dentry * dent,
  726. int flags)
  727. {
  728. int ret = 0;
  729. struct shim_mount * fs = dent->fs;
  730. BEGIN_PROFILE_INTERVAL();
  731. if (!fs->d_ops || !fs->d_ops->open) {
  732. ret = -EACCES;
  733. goto out;
  734. }
  735. if ((ret = fs->d_ops->open(hdl, dent, flags)) < 0)
  736. goto out;
  737. SAVE_PROFILE_INTERVAL(dentry_open_open);
  738. set_handle_fs(hdl, fs);
  739. get_dentry(dent);
  740. hdl->dentry = dent;
  741. hdl->flags = flags;
  742. /* truncate the file if O_TRUNC is given */
  743. if (ret >= 0 && (flags & O_TRUNC) && fs->fs_ops->truncate) {
  744. ret = fs->fs_ops->truncate(hdl, 0);
  745. SAVE_PROFILE_INTERVAL(dentry_open_truncate);
  746. }
  747. if (ret < 0)
  748. goto out;
  749. int size;
  750. char *path = dentry_get_path(dent, true, &size);
  751. qstrsetstr(&hdl->path, path, size);
  752. SAVE_PROFILE_INTERVAL(dentry_open_set_path);
  753. out:
  754. return ret;
  755. }
  756. static inline void set_dirent_type (mode_t * type, int d_type)
  757. {
  758. switch (d_type) {
  759. case LINUX_DT_FIFO:
  760. *type = S_IFIFO;
  761. return;
  762. case LINUX_DT_CHR:
  763. *type = S_IFCHR;
  764. return;
  765. case LINUX_DT_BLK:
  766. *type = S_IFBLK;
  767. return;
  768. case LINUX_DT_REG:
  769. *type = S_IFREG;
  770. return;
  771. case LINUX_DT_LNK:
  772. *type = S_IFLNK;
  773. return;
  774. case LINUX_DT_SOCK:
  775. *type = S_IFSOCK;
  776. return;
  777. default:
  778. *type = 0;
  779. return;
  780. }
  781. }
  782. int directory_open (struct shim_handle * hdl, struct shim_dentry * dent,
  783. int flags)
  784. {
  785. struct shim_mount * fs = dent->fs;
  786. int ret = 0;
  787. if (!fs->d_ops || !fs->d_ops->readdir) {
  788. ret = -EACCES;
  789. goto out;
  790. }
  791. int size;
  792. const char * path = dentry_get_path(dent, true, &size);
  793. lock(dcache_lock);
  794. if (!(dent->state & DENTRY_LISTED)) {
  795. struct shim_dirent * dirent = NULL;
  796. if ((ret = fs->d_ops->readdir(dent, &dirent)) < 0 || !dirent)
  797. goto done_read;
  798. struct shim_dirent * d = dirent;
  799. for ( ; d ; d = d->next) {
  800. debug("read %s from %s\n", d->name, path);
  801. struct shim_dentry * child;
  802. if ((ret = lookup_dentry(dent, d->name, strlen(d->name), false,
  803. &child)) < 0)
  804. goto done_read;
  805. if (child->state & DENTRY_NEGATIVE)
  806. continue;
  807. if (!(child->state & DENTRY_VALID)) {
  808. set_dirent_type(&child->type, d->type);
  809. child->state |= DENTRY_VALID|DENTRY_RECENTLY;
  810. }
  811. child->ino = d->ino;
  812. }
  813. free(dirent);
  814. dent->state |= DENTRY_LISTED;
  815. }
  816. done_read:
  817. unlock(dcache_lock);
  818. if (!(dent->state & DENTRY_LISTED))
  819. return ret;
  820. int nchildren = dent->nchildren, count = 0;
  821. struct shim_dentry ** children = malloc(sizeof(struct shim_dentry *) *
  822. (nchildren + 1));
  823. struct shim_dentry * child;
  824. list_for_each_entry(child, &dent->children, siblings) {
  825. if (count >= nchildren)
  826. break;
  827. struct shim_dentry * c = child;
  828. while (c->state & DENTRY_MOUNTPOINT)
  829. c = c->mounted->root;
  830. if (c->state & DENTRY_VALID) {
  831. get_dentry(c);
  832. children[count++] = c;
  833. }
  834. }
  835. children[count] = NULL;
  836. qstrsetstr(&hdl->path, path, size);
  837. hdl->type = TYPE_DIR;
  838. hdl->fs = fs;
  839. memcpy(hdl->fs_type, fs->type, sizeof(fs->type));
  840. hdl->dentry = dent;
  841. hdl->flags = flags;
  842. hdl->info.dir.dot = dent;
  843. hdl->info.dir.dotdot = dent->parent;
  844. hdl->info.dir.buf = children;
  845. hdl->info.dir.ptr = children;
  846. out:
  847. return ret;
  848. }
  849. int path_startat (int dfd, struct shim_dentry ** dir)
  850. {
  851. if (dfd == AT_FDCWD) {
  852. struct shim_thread * cur = get_cur_thread();
  853. get_dentry(cur->cwd);
  854. *dir = cur->cwd;
  855. return 0;
  856. } else if (dfd < 0) {
  857. return -EBADF;
  858. } else {
  859. struct shim_handle * hdl = get_fd_handle(dfd, NULL, NULL);
  860. if (!hdl)
  861. return -EBADF;
  862. if (hdl->type != TYPE_DIR) {
  863. put_handle(hdl);
  864. return -ENOTDIR;
  865. }
  866. get_dentry(hdl->dentry);
  867. put_handle(hdl);
  868. *dir = hdl->dentry;
  869. return 0;
  870. }
  871. }