shim_namei.c 28 KB

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