shim_namei.c 29 KB

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