shim_namei.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. 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. DEFINE_PROFILE_CATAGORY(path_lookup, dcache);
  433. DEFINE_PROFILE_INTERVAL(lookup_dcache_for_path_lookup, path_lookup);
  434. DEFINE_PROFILE_INTERVAL(lookup_walk_for_path_lookup, path_lookup);
  435. int __path_lookupat (struct shim_dentry * start, const char * path, int flags,
  436. struct shim_dentry ** dent)
  437. {
  438. struct shim_thread * cur_thread = get_cur_thread();
  439. struct shim_dentry * found = NULL;
  440. int ret = 0;
  441. struct lookup look;
  442. BEGIN_PROFILE_INTERVAL();
  443. ret = path_lookup_dcache(start, path, flags, &found, cur_thread);
  444. if (ret < 0)
  445. return ret;
  446. SAVE_PROFILE_INTERVAL(lookup_dcache_for_path_lookup);
  447. if (!found) {
  448. if ((ret = path_lookup_walk(start, path, flags, &look, cur_thread)) < 0)
  449. return ret;
  450. get_dentry(look.dentry);
  451. found = look.dentry;
  452. SAVE_PROFILE_INTERVAL(lookup_walk_for_path_lookup);
  453. if (flags & LOOKUP_SYNC) {
  454. if ((ret = __do_lookup_dentry(found, true)) < 0)
  455. goto out_if;
  456. }
  457. if (!(found->state & DENTRY_ISDIRECTORY) &&
  458. flags & LOOKUP_DIRECTORY) {
  459. ret = -ENOTDIR;
  460. goto out_if;
  461. }
  462. out_if:
  463. path_release(&look);
  464. }
  465. if (found) {
  466. if (!ret && dent)
  467. *dent = found;
  468. else
  469. put_dentry(found);
  470. }
  471. return 0;
  472. }
  473. /* if path_lookup succeed, the returned dentry is pop'ed */
  474. int path_lookupat (struct shim_dentry * start, const char * path, int flags,
  475. struct shim_dentry ** dent)
  476. {
  477. struct shim_thread * cur_thread = get_cur_thread();
  478. struct shim_dentry * found = NULL;
  479. int ret = 0;
  480. struct lookup look;
  481. lock(dcache_lock);
  482. BEGIN_PROFILE_INTERVAL();
  483. ret = path_lookup_dcache(start, path, flags, &found, cur_thread);
  484. if (ret < 0) {
  485. unlock(dcache_lock);
  486. return ret;
  487. }
  488. SAVE_PROFILE_INTERVAL(lookup_dcache_for_path_lookup);
  489. if (!found) {
  490. if ((ret = path_lookup_walk(start, path, flags, &look,
  491. cur_thread)) < 0)
  492. goto out;
  493. get_dentry(look.dentry);
  494. found = look.dentry;
  495. SAVE_PROFILE_INTERVAL(lookup_walk_for_path_lookup);
  496. if (flags & LOOKUP_SYNC) {
  497. if ((ret = __do_lookup_dentry(found, true)) < 0)
  498. goto out_release;
  499. }
  500. if (found->state & DENTRY_NEGATIVE &&
  501. !(flags & LOOKUP_CREATE)) {
  502. ret = -ENOENT;
  503. goto out_release;
  504. }
  505. if (!(found->state & DENTRY_NEGATIVE) &&
  506. !(found->state & DENTRY_ISDIRECTORY) &&
  507. flags & LOOKUP_DIRECTORY) {
  508. ret = -ENOTDIR;
  509. goto out_release;
  510. }
  511. out_release:
  512. path_release(&look);
  513. }
  514. if (found) {
  515. if (!ret && dent)
  516. *dent = found;
  517. else
  518. put_dentry(found);
  519. }
  520. out:
  521. unlock(dcache_lock);
  522. return ret;
  523. }
  524. static inline int __lookup_flags (int flags)
  525. {
  526. int retval = LOOKUP_FOLLOW;
  527. if (flags & O_NOFOLLOW)
  528. retval &= ~LOOKUP_FOLLOW;
  529. if ((flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL))
  530. retval &= ~LOOKUP_FOLLOW;
  531. if (flags & O_DIRECTORY)
  532. retval |= LOOKUP_DIRECTORY;
  533. return retval;
  534. }
  535. int create_dentry (struct shim_handle * hdl, struct shim_dentry * dir,
  536. struct shim_dentry * dent, int flags, int mode)
  537. {
  538. int err = permission(dir, MAY_WRITE | MAY_EXEC, true);
  539. if (err)
  540. return err;
  541. if (!dir->fs->d_ops || !dir->fs->d_ops->creat)
  542. return -EACCES;
  543. err = dir->fs->d_ops->creat(hdl, dir, dent, flags, mode);
  544. if (err)
  545. return err;
  546. if (!hdl)
  547. return 0;
  548. set_handle_fs(hdl, dent->fs);
  549. get_dentry(dent);
  550. hdl->dentry = dent;
  551. hdl->flags = flags;
  552. int size;
  553. char *path = dentry_get_path(dent, true, &size);
  554. qstrsetstr(&hdl->path, path, size);
  555. return 0;
  556. }
  557. int create_directory (struct shim_dentry * dir, struct shim_dentry * dent,
  558. int mode)
  559. {
  560. int err = permission(dir, MAY_WRITE | MAY_EXEC, true);
  561. if (err)
  562. return err;
  563. if (!dir->fs->d_ops || !dir->fs->d_ops->mkdir)
  564. return -EACCES;
  565. return dir->fs->d_ops->mkdir(dir, dent, mode);
  566. }
  567. DEFINE_PROFILE_CATAGORY(open_namei, dcache);
  568. DEFINE_PROFILE_INTERVAL(path_lookup_dcache_for_open_namei, open_namei);
  569. DEFINE_PROFILE_INTERVAL(path_lookup_walk_for_open_namei, open_namei);
  570. DEFINE_PROFILE_INTERVAL(path_lookup_walk_2_for_open_namei, open_namei);
  571. DEFINE_PROFILE_INTERVAL(end_open_namei, open_namei);
  572. DEFINE_PROFILE_INTERVAL(open_namei_permission, open_namei);
  573. DEFINE_PROFILE_INTERVAL(open_namei_dir_open, open_namei);
  574. DEFINE_PROFILE_INTERVAL(open_namei_dentry_open, open_namei);
  575. DEFINE_PROFILE_INTERVAL(open_namei_lookup_2, open_namei);
  576. DEFINE_PROFILE_INTERVAL(open_namei_path_reacquire, open_namei);
  577. DEFINE_PROFILE_INTERVAL(open_namei_create_dir, open_namei);
  578. DEFINE_PROFILE_INTERVAL(open_namei_create_dentry, open_namei);
  579. int open_namei (struct shim_handle * hdl, struct shim_dentry * start,
  580. const char * path, int flags, int mode,
  581. struct shim_dentry ** dent)
  582. {
  583. struct shim_thread * cur_thread = get_cur_thread();
  584. struct lookup look = { .dentry = NULL, .mount = NULL };
  585. struct shim_dentry * dir = NULL;
  586. int err = 0;
  587. int acc_mode = ACC_MODE(flags & O_ACCMODE);
  588. int lookup_flags = __lookup_flags(flags);
  589. #ifdef MAY_APPEND
  590. if (flags & O_APPEND)
  591. acc_mode |= MAY_APPEND;
  592. #endif
  593. BEGIN_PROFILE_INTERVAL();
  594. lock(dcache_lock);
  595. #if 0
  596. err = path_lookup_dcache(start, path, lookup_flags|LOOKUP_OPEN,
  597. &look.dentry, cur_thread);
  598. if (err >= 0 && look.dentry) {
  599. look.mount = look.dentry->fs;
  600. if (look.mount)
  601. get_mount(look.mount);
  602. }
  603. SAVE_PROFILE_INTERVAL(path_lookup_dcache_for_open_namei);
  604. if (err < 0) {
  605. unlock(dcache_lock);
  606. SAVE_PROFILE_INTERVAL(end_open_namei);
  607. return err;
  608. }
  609. #endif
  610. if (look.dentry) {
  611. if (look.dentry->state & DENTRY_NEGATIVE) {
  612. if (!(flags & O_CREAT)) {
  613. err = -ENOENT;
  614. goto exit;
  615. }
  616. dir = look.dentry->parent;
  617. get_dentry(dir);
  618. goto do_creat;
  619. }
  620. if (flags & O_EXCL) {
  621. err = -EEXIST;
  622. goto exit;
  623. }
  624. goto do_open_locked;
  625. }
  626. /* no create, just look it up. */
  627. if (!(flags & O_CREAT)) {
  628. err = path_lookup_walk(start, path, lookup_flags|LOOKUP_OPEN,
  629. &look, cur_thread);
  630. SAVE_PROFILE_INTERVAL(path_lookup_walk_for_open_namei);
  631. if (err) {
  632. debug("path_lookup error in open_namei\n");
  633. goto exit;
  634. }
  635. do_open_locked:
  636. unlock(dcache_lock);
  637. do_open:
  638. if ((err = permission(look.dentry, acc_mode, true)) < 0)
  639. goto exit;
  640. SAVE_PROFILE_INTERVAL(open_namei_permission);
  641. if (hdl) {
  642. if (look.dentry->state & DENTRY_ISDIRECTORY) {
  643. if ((err = directory_open(hdl, look.dentry, flags)) < 0)
  644. goto exit;
  645. SAVE_PROFILE_INTERVAL(open_namei_dir_open);
  646. } else {
  647. err = -ENOTDIR;
  648. if (flags & O_DIRECTORY) {
  649. debug("%s is not a directory\n",
  650. dentry_get_path(look.dentry, true, NULL));
  651. goto exit;
  652. }
  653. if ((err = dentry_open(hdl, look.dentry, flags)) < 0)
  654. goto exit;
  655. SAVE_PROFILE_INTERVAL(open_namei_dentry_open);
  656. }
  657. }
  658. goto done;
  659. }
  660. /* create, so we need the parent */
  661. err = path_lookup_walk(start, path, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE,
  662. &look, cur_thread);
  663. SAVE_PROFILE_INTERVAL(path_lookup_walk_2_for_open_namei);
  664. if (err < 0 || look.last_type != LAST_NORM)
  665. goto exit;
  666. struct shim_dentry * new = NULL;
  667. dir = look.dentry;
  668. get_dentry(dir);
  669. err = lookup_dentry(dir, look.last, strlen(look.last), true, &new);
  670. SAVE_PROFILE_INTERVAL(open_namei_lookup_2);
  671. if (err < 0 && (err != -ENOENT || !new))
  672. goto exit;
  673. path_reacquire(&look, new);
  674. SAVE_PROFILE_INTERVAL(open_namei_path_reacquire);
  675. do_creat:
  676. assert(dir);
  677. unlock(dcache_lock);
  678. /* negative dentry */
  679. if (look.dentry->state & DENTRY_NEGATIVE) {
  680. if (flags & O_DIRECTORY) {
  681. if ((err = create_directory(dir, look.dentry, mode)) < 0) {
  682. debug("error: create directory in open_namei\n");
  683. goto exit;
  684. }
  685. SAVE_PROFILE_INTERVAL(open_namei_create_dir);
  686. look.dentry->state |= DENTRY_ISDIRECTORY;
  687. } else {
  688. if ((err = create_dentry(hdl, dir, look.dentry, flags,
  689. mode)) < 0) {
  690. debug("error: create file in open_namei\n");
  691. goto exit;
  692. }
  693. SAVE_PROFILE_INTERVAL(open_namei_create_dentry);
  694. }
  695. look.dentry->state &= ~DENTRY_NEGATIVE;
  696. if (hdl && (flags & O_DIRECTORY))
  697. goto do_open;
  698. else
  699. goto done;
  700. }
  701. /* existing dentry */
  702. if (flags & O_EXCL) {
  703. err = -EEXIST;
  704. debug("error: existing dentry with O_EXCL\n");
  705. goto exit;
  706. }
  707. if (look.dentry->state & DENTRY_ISLINK) {
  708. if (flags & O_NOFOLLOW) {
  709. err = -ELOOP;
  710. debug("error: linked dentry with O_NOFOLLOW\n");
  711. goto exit;
  712. }
  713. if ((err = follow_link(&look)) < 0)
  714. goto exit;
  715. }
  716. assert(!(look.dentry->state & DENTRY_MOUNTPOINT));
  717. goto do_open;
  718. done:
  719. if (dent) {
  720. get_dentry(look.dentry);
  721. *dent = look.dentry;
  722. }
  723. path_release(&look);
  724. if (locked(dcache_lock))
  725. unlock(dcache_lock);
  726. SAVE_PROFILE_INTERVAL(end_open_namei);
  727. return 0;
  728. exit:
  729. path_release(&look);
  730. if (dir)
  731. put_dentry(dir);
  732. if (locked(dcache_lock))
  733. unlock(dcache_lock);
  734. SAVE_PROFILE_INTERVAL(end_open_namei);
  735. return err;
  736. }
  737. DEFINE_PROFILE_CATAGORY(dentry_open, dcache);
  738. DEFINE_PROFILE_INTERVAL(dentry_open_open, dentry_open);
  739. DEFINE_PROFILE_INTERVAL(dentry_open_truncate, dentry_open);
  740. DEFINE_PROFILE_INTERVAL(dentry_open_set_path, dentry_open);
  741. int dentry_open (struct shim_handle * hdl, struct shim_dentry * dent,
  742. int flags)
  743. {
  744. int ret = 0;
  745. struct shim_mount * fs = dent->fs;
  746. BEGIN_PROFILE_INTERVAL();
  747. if (!fs->d_ops || !fs->d_ops->open) {
  748. ret = -EACCES;
  749. goto out;
  750. }
  751. if ((ret = fs->d_ops->open(hdl, dent, flags)) < 0)
  752. goto out;
  753. SAVE_PROFILE_INTERVAL(dentry_open_open);
  754. set_handle_fs(hdl, fs);
  755. get_dentry(dent);
  756. hdl->dentry = dent;
  757. hdl->flags = flags;
  758. /* truncate the file if O_TRUNC is given */
  759. if (ret >= 0 && (flags & O_TRUNC) && fs->fs_ops->truncate) {
  760. ret = fs->fs_ops->truncate(hdl, 0);
  761. SAVE_PROFILE_INTERVAL(dentry_open_truncate);
  762. }
  763. if (ret < 0)
  764. goto out;
  765. int size;
  766. char *path = dentry_get_path(dent, true, &size);
  767. qstrsetstr(&hdl->path, path, size);
  768. SAVE_PROFILE_INTERVAL(dentry_open_set_path);
  769. out:
  770. return ret;
  771. }
  772. static inline void set_dirent_type (mode_t * type, int d_type)
  773. {
  774. switch (d_type) {
  775. case LINUX_DT_FIFO:
  776. *type = S_IFIFO;
  777. return;
  778. case LINUX_DT_CHR:
  779. *type = S_IFCHR;
  780. return;
  781. case LINUX_DT_BLK:
  782. *type = S_IFBLK;
  783. return;
  784. case LINUX_DT_REG:
  785. *type = S_IFREG;
  786. return;
  787. case LINUX_DT_LNK:
  788. *type = S_IFLNK;
  789. return;
  790. case LINUX_DT_SOCK:
  791. *type = S_IFSOCK;
  792. return;
  793. default:
  794. *type = 0;
  795. return;
  796. }
  797. }
  798. int directory_open (struct shim_handle * hdl, struct shim_dentry * dent,
  799. int flags)
  800. {
  801. struct shim_mount * fs = dent->fs;
  802. int ret = 0;
  803. if (!fs->d_ops || !fs->d_ops->readdir) {
  804. ret = -EACCES;
  805. goto out;
  806. }
  807. int size;
  808. const char * path = dentry_get_path(dent, true, &size);
  809. lock(dcache_lock);
  810. if (!(dent->state & DENTRY_LISTED)) {
  811. struct shim_dirent * dirent = NULL;
  812. if ((ret = fs->d_ops->readdir(dent, &dirent)) < 0 || !dirent)
  813. goto done_read;
  814. struct shim_dirent * d = dirent;
  815. for ( ; d ; d = d->next) {
  816. debug("read %s from %s\n", d->name, path);
  817. struct shim_dentry * child;
  818. if ((ret = lookup_dentry(dent, d->name, strlen(d->name), false,
  819. &child)) < 0)
  820. goto done_read;
  821. if (child->state & DENTRY_NEGATIVE)
  822. continue;
  823. if (!(child->state & DENTRY_VALID)) {
  824. set_dirent_type(&child->type, d->type);
  825. child->state |= DENTRY_VALID|DENTRY_RECENTLY;
  826. }
  827. child->ino = d->ino;
  828. }
  829. free(dirent);
  830. dent->state |= DENTRY_LISTED;
  831. }
  832. done_read:
  833. unlock(dcache_lock);
  834. struct shim_dentry ** children = NULL;
  835. if (dent->state & DENTRY_LISTED) {
  836. int nchildren = dent->nchildren, count = 0;
  837. struct shim_dentry * child;
  838. children = malloc(sizeof(struct shim_dentry *) * (nchildren + 1));
  839. list_for_each_entry(child, &dent->children, siblings) {
  840. if (count >= nchildren)
  841. break;
  842. struct shim_dentry * c = child;
  843. while (c->state & DENTRY_MOUNTPOINT)
  844. c = c->mounted->root;
  845. if (c->state & DENTRY_VALID) {
  846. get_dentry(c);
  847. children[count++] = c;
  848. }
  849. }
  850. children[count] = NULL;
  851. }
  852. qstrsetstr(&hdl->path, path, size);
  853. hdl->type = TYPE_DIR;
  854. hdl->fs = fs;
  855. memcpy(hdl->fs_type, fs->type, sizeof(fs->type));
  856. hdl->dentry = dent;
  857. hdl->flags = flags;
  858. get_dentry(dent);
  859. hdl->info.dir.dot = dent;
  860. if (dent->parent) {
  861. get_dentry(dent->parent);
  862. hdl->info.dir.dotdot = dent->parent;
  863. }
  864. hdl->info.dir.buf = children;
  865. hdl->info.dir.ptr = children;
  866. out:
  867. return ret;
  868. }
  869. int path_startat (int dfd, struct shim_dentry ** dir)
  870. {
  871. if (dfd == AT_FDCWD) {
  872. struct shim_thread * cur = get_cur_thread();
  873. get_dentry(cur->cwd);
  874. *dir = cur->cwd;
  875. return 0;
  876. } else if (dfd < 0) {
  877. return -EBADF;
  878. } else {
  879. struct shim_handle * hdl = get_fd_handle(dfd, NULL, NULL);
  880. if (!hdl)
  881. return -EBADF;
  882. if (hdl->type != TYPE_DIR) {
  883. put_handle(hdl);
  884. return -ENOTDIR;
  885. }
  886. get_dentry(hdl->dentry);
  887. put_handle(hdl);
  888. *dir = hdl->dentry;
  889. return 0;
  890. }
  891. }