shim_namei.c 29 KB

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