namei.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /*
  2. * Copyright (C) 2011 Novell Inc.
  3. * Copyright (C) 2016 Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/cred.h>
  11. #include <linux/namei.h>
  12. #include <linux/xattr.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/mount.h>
  15. #include <linux/exportfs.h>
  16. #include "overlayfs.h"
  17. #include "ovl_entry.h"
  18. struct ovl_lookup_data {
  19. struct qstr name;
  20. bool is_dir;
  21. bool opaque;
  22. bool stop;
  23. bool last;
  24. char *redirect;
  25. };
  26. static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
  27. size_t prelen, const char *post)
  28. {
  29. int res;
  30. char *s, *next, *buf = NULL;
  31. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
  32. if (res < 0) {
  33. if (res == -ENODATA || res == -EOPNOTSUPP)
  34. return 0;
  35. goto fail;
  36. }
  37. buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
  38. if (!buf)
  39. return -ENOMEM;
  40. if (res == 0)
  41. goto invalid;
  42. res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
  43. if (res < 0)
  44. goto fail;
  45. if (res == 0)
  46. goto invalid;
  47. if (buf[0] == '/') {
  48. for (s = buf; *s++ == '/'; s = next) {
  49. next = strchrnul(s, '/');
  50. if (s == next)
  51. goto invalid;
  52. }
  53. } else {
  54. if (strchr(buf, '/') != NULL)
  55. goto invalid;
  56. memmove(buf + prelen, buf, res);
  57. memcpy(buf, d->name.name, prelen);
  58. }
  59. strcat(buf, post);
  60. kfree(d->redirect);
  61. d->redirect = buf;
  62. d->name.name = d->redirect;
  63. d->name.len = strlen(d->redirect);
  64. return 0;
  65. err_free:
  66. kfree(buf);
  67. return 0;
  68. fail:
  69. pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
  70. goto err_free;
  71. invalid:
  72. pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
  73. goto err_free;
  74. }
  75. static int ovl_acceptable(void *ctx, struct dentry *dentry)
  76. {
  77. return 1;
  78. }
  79. static struct ovl_fh *ovl_get_origin_fh(struct dentry *dentry)
  80. {
  81. int res;
  82. struct ovl_fh *fh = NULL;
  83. res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
  84. if (res < 0) {
  85. if (res == -ENODATA || res == -EOPNOTSUPP)
  86. return NULL;
  87. goto fail;
  88. }
  89. /* Zero size value means "copied up but origin unknown" */
  90. if (res == 0)
  91. return NULL;
  92. fh = kzalloc(res, GFP_KERNEL);
  93. if (!fh)
  94. return ERR_PTR(-ENOMEM);
  95. res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res);
  96. if (res < 0)
  97. goto fail;
  98. if (res < sizeof(struct ovl_fh) || res < fh->len)
  99. goto invalid;
  100. if (fh->magic != OVL_FH_MAGIC)
  101. goto invalid;
  102. /* Treat larger version and unknown flags as "origin unknown" */
  103. if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
  104. goto out;
  105. /* Treat endianness mismatch as "origin unknown" */
  106. if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
  107. (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
  108. goto out;
  109. return fh;
  110. out:
  111. kfree(fh);
  112. return NULL;
  113. fail:
  114. pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
  115. goto out;
  116. invalid:
  117. pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
  118. goto out;
  119. }
  120. static struct dentry *ovl_get_origin(struct dentry *dentry,
  121. struct vfsmount *mnt)
  122. {
  123. struct dentry *origin = NULL;
  124. struct ovl_fh *fh = ovl_get_origin_fh(dentry);
  125. int bytes;
  126. if (IS_ERR_OR_NULL(fh))
  127. return (struct dentry *)fh;
  128. /*
  129. * Make sure that the stored uuid matches the uuid of the lower
  130. * layer where file handle will be decoded.
  131. */
  132. if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
  133. goto out;
  134. bytes = (fh->len - offsetof(struct ovl_fh, fid));
  135. origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
  136. bytes >> 2, (int)fh->type,
  137. ovl_acceptable, NULL);
  138. if (IS_ERR(origin)) {
  139. /* Treat stale file handle as "origin unknown" */
  140. if (origin == ERR_PTR(-ESTALE))
  141. origin = NULL;
  142. goto out;
  143. }
  144. if (ovl_dentry_weird(origin) ||
  145. ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT))
  146. goto invalid;
  147. out:
  148. kfree(fh);
  149. return origin;
  150. invalid:
  151. pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin);
  152. dput(origin);
  153. origin = NULL;
  154. goto out;
  155. }
  156. static bool ovl_is_opaquedir(struct dentry *dentry)
  157. {
  158. return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
  159. }
  160. static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
  161. const char *name, unsigned int namelen,
  162. size_t prelen, const char *post,
  163. struct dentry **ret)
  164. {
  165. struct dentry *this;
  166. int err;
  167. this = lookup_one_len_unlocked(name, base, namelen);
  168. if (IS_ERR(this)) {
  169. err = PTR_ERR(this);
  170. this = NULL;
  171. if (err == -ENOENT || err == -ENAMETOOLONG)
  172. goto out;
  173. goto out_err;
  174. }
  175. if (!this->d_inode)
  176. goto put_and_out;
  177. if (ovl_dentry_weird(this)) {
  178. /* Don't support traversing automounts and other weirdness */
  179. err = -EREMOTE;
  180. goto out_err;
  181. }
  182. if (ovl_is_whiteout(this)) {
  183. d->stop = d->opaque = true;
  184. goto put_and_out;
  185. }
  186. if (!d_can_lookup(this)) {
  187. d->stop = true;
  188. if (d->is_dir)
  189. goto put_and_out;
  190. goto out;
  191. }
  192. d->is_dir = true;
  193. if (!d->last && ovl_is_opaquedir(this)) {
  194. d->stop = d->opaque = true;
  195. goto out;
  196. }
  197. err = ovl_check_redirect(this, d, prelen, post);
  198. if (err)
  199. goto out_err;
  200. out:
  201. *ret = this;
  202. return 0;
  203. put_and_out:
  204. dput(this);
  205. this = NULL;
  206. goto out;
  207. out_err:
  208. dput(this);
  209. return err;
  210. }
  211. static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
  212. struct dentry **ret)
  213. {
  214. /* Counting down from the end, since the prefix can change */
  215. size_t rem = d->name.len - 1;
  216. struct dentry *dentry = NULL;
  217. int err;
  218. if (d->name.name[0] != '/')
  219. return ovl_lookup_single(base, d, d->name.name, d->name.len,
  220. 0, "", ret);
  221. while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
  222. const char *s = d->name.name + d->name.len - rem;
  223. const char *next = strchrnul(s, '/');
  224. size_t thislen = next - s;
  225. bool end = !next[0];
  226. /* Verify we did not go off the rails */
  227. if (WARN_ON(s[-1] != '/'))
  228. return -EIO;
  229. err = ovl_lookup_single(base, d, s, thislen,
  230. d->name.len - rem, next, &base);
  231. dput(dentry);
  232. if (err)
  233. return err;
  234. dentry = base;
  235. if (end)
  236. break;
  237. rem -= thislen + 1;
  238. if (WARN_ON(rem >= d->name.len))
  239. return -EIO;
  240. }
  241. *ret = dentry;
  242. return 0;
  243. }
  244. static int ovl_check_origin(struct dentry *upperdentry,
  245. struct path *lowerstack, unsigned int numlower,
  246. struct path **stackp, unsigned int *ctrp)
  247. {
  248. struct vfsmount *mnt;
  249. struct dentry *origin = NULL;
  250. int i;
  251. for (i = 0; i < numlower; i++) {
  252. mnt = lowerstack[i].mnt;
  253. origin = ovl_get_origin(upperdentry, mnt);
  254. if (IS_ERR(origin))
  255. return PTR_ERR(origin);
  256. if (origin)
  257. break;
  258. }
  259. if (!origin)
  260. return 0;
  261. BUG_ON(*ctrp);
  262. if (!*stackp)
  263. *stackp = kmalloc(sizeof(struct path), GFP_KERNEL);
  264. if (!*stackp) {
  265. dput(origin);
  266. return -ENOMEM;
  267. }
  268. **stackp = (struct path) { .dentry = origin, .mnt = mnt };
  269. *ctrp = 1;
  270. return 0;
  271. }
  272. /*
  273. * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN.
  274. * Return 0 on match, -ESTALE on mismatch, < 0 on error.
  275. */
  276. static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)
  277. {
  278. struct ovl_fh *ofh = ovl_get_origin_fh(dentry);
  279. int err = 0;
  280. if (!ofh)
  281. return -ENODATA;
  282. if (IS_ERR(ofh))
  283. return PTR_ERR(ofh);
  284. if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
  285. err = -ESTALE;
  286. kfree(ofh);
  287. return err;
  288. }
  289. /*
  290. * Verify that an inode matches the origin file handle stored in upper inode.
  291. *
  292. * If @set is true and there is no stored file handle, encode and store origin
  293. * file handle in OVL_XATTR_ORIGIN.
  294. *
  295. * Return 0 on match, -ESTALE on mismatch, < 0 on error.
  296. */
  297. int ovl_verify_origin(struct dentry *dentry, struct vfsmount *mnt,
  298. struct dentry *origin, bool is_upper, bool set)
  299. {
  300. struct inode *inode;
  301. struct ovl_fh *fh;
  302. int err;
  303. fh = ovl_encode_fh(origin, is_upper);
  304. err = PTR_ERR(fh);
  305. if (IS_ERR(fh))
  306. goto fail;
  307. err = ovl_verify_origin_fh(dentry, fh);
  308. if (set && err == -ENODATA)
  309. err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0);
  310. if (err)
  311. goto fail;
  312. out:
  313. kfree(fh);
  314. return err;
  315. fail:
  316. inode = d_inode(origin);
  317. pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n",
  318. origin, inode ? inode->i_ino : 0, err);
  319. goto out;
  320. }
  321. /*
  322. * Verify that an index entry name matches the origin file handle stored in
  323. * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
  324. * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
  325. */
  326. int ovl_verify_index(struct dentry *index, struct path *lowerstack,
  327. unsigned int numlower)
  328. {
  329. struct ovl_fh *fh = NULL;
  330. size_t len;
  331. struct path origin = { };
  332. struct path *stack = &origin;
  333. unsigned int ctr = 0;
  334. int err;
  335. if (!d_inode(index))
  336. return 0;
  337. /*
  338. * Directory index entries are going to be used for looking up
  339. * redirected upper dirs by lower dir fh when decoding an overlay
  340. * file handle of a merge dir. Whiteout index entries are going to be
  341. * used as an indication that an exported overlay file handle should
  342. * be treated as stale (i.e. after unlink of the overlay inode).
  343. * We don't know the verification rules for directory and whiteout
  344. * index entries, because they have not been implemented yet, so return
  345. * EROFS if those entries are found to avoid corrupting an index that
  346. * was created by a newer kernel.
  347. */
  348. err = -EROFS;
  349. if (d_is_dir(index) || ovl_is_whiteout(index))
  350. goto fail;
  351. err = -EINVAL;
  352. if (index->d_name.len < sizeof(struct ovl_fh)*2)
  353. goto fail;
  354. err = -ENOMEM;
  355. len = index->d_name.len / 2;
  356. fh = kzalloc(len, GFP_KERNEL);
  357. if (!fh)
  358. goto fail;
  359. err = -EINVAL;
  360. if (hex2bin((u8 *)fh, index->d_name.name, len) || len != fh->len)
  361. goto fail;
  362. err = ovl_verify_origin_fh(index, fh);
  363. if (err)
  364. goto fail;
  365. err = ovl_check_origin(index, lowerstack, numlower, &stack, &ctr);
  366. if (!err && !ctr)
  367. err = -ESTALE;
  368. if (err)
  369. goto fail;
  370. /* Check if index is orphan and don't warn before cleaning it */
  371. if (d_inode(index)->i_nlink == 1 &&
  372. ovl_get_nlink(index, origin.dentry, 0) == 0)
  373. err = -ENOENT;
  374. dput(origin.dentry);
  375. out:
  376. kfree(fh);
  377. return err;
  378. fail:
  379. pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
  380. index, d_inode(index)->i_mode & S_IFMT, err);
  381. goto out;
  382. }
  383. /*
  384. * Lookup in indexdir for the index entry of a lower real inode or a copy up
  385. * origin inode. The index entry name is the hex representation of the lower
  386. * inode file handle.
  387. *
  388. * If the index dentry in negative, then either no lower aliases have been
  389. * copied up yet, or aliases have been copied up in older kernels and are
  390. * not indexed.
  391. *
  392. * If the index dentry for a copy up origin inode is positive, but points
  393. * to an inode different than the upper inode, then either the upper inode
  394. * has been copied up and not indexed or it was indexed, but since then
  395. * index dir was cleared. Either way, that index cannot be used to indentify
  396. * the overlay inode.
  397. */
  398. int ovl_get_index_name(struct dentry *origin, struct qstr *name)
  399. {
  400. int err;
  401. struct ovl_fh *fh;
  402. char *n, *s;
  403. fh = ovl_encode_fh(origin, false);
  404. if (IS_ERR(fh))
  405. return PTR_ERR(fh);
  406. err = -ENOMEM;
  407. n = kzalloc(fh->len * 2, GFP_KERNEL);
  408. if (n) {
  409. s = bin2hex(n, fh, fh->len);
  410. *name = (struct qstr) QSTR_INIT(n, s - n);
  411. err = 0;
  412. }
  413. kfree(fh);
  414. return err;
  415. }
  416. static struct dentry *ovl_lookup_index(struct dentry *dentry,
  417. struct dentry *upper,
  418. struct dentry *origin)
  419. {
  420. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  421. struct dentry *index;
  422. struct inode *inode;
  423. struct qstr name;
  424. int err;
  425. err = ovl_get_index_name(origin, &name);
  426. if (err)
  427. return ERR_PTR(err);
  428. index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
  429. if (IS_ERR(index)) {
  430. pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
  431. "overlayfs: mount with '-o index=off' to disable inodes index.\n",
  432. d_inode(origin)->i_ino, name.len, name.name,
  433. err);
  434. goto out;
  435. }
  436. inode = d_inode(index);
  437. if (d_is_negative(index)) {
  438. if (upper && d_inode(origin)->i_nlink > 1) {
  439. pr_warn_ratelimited("overlayfs: hard link with origin but no index (ino=%lu).\n",
  440. d_inode(origin)->i_ino);
  441. goto fail;
  442. }
  443. dput(index);
  444. index = NULL;
  445. } else if (upper && d_inode(upper) != inode) {
  446. pr_warn_ratelimited("overlayfs: wrong index found (index=%pd2, ino=%lu, upper ino=%lu).\n",
  447. index, inode->i_ino, d_inode(upper)->i_ino);
  448. goto fail;
  449. } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
  450. ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
  451. /*
  452. * Index should always be of the same file type as origin
  453. * except for the case of a whiteout index. A whiteout
  454. * index should only exist if all lower aliases have been
  455. * unlinked, which means that finding a lower origin on lookup
  456. * whose index is a whiteout should be treated as an error.
  457. */
  458. pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
  459. index, d_inode(index)->i_mode & S_IFMT,
  460. d_inode(origin)->i_mode & S_IFMT);
  461. goto fail;
  462. }
  463. out:
  464. kfree(name.name);
  465. return index;
  466. fail:
  467. dput(index);
  468. index = ERR_PTR(-EIO);
  469. goto out;
  470. }
  471. /*
  472. * Returns next layer in stack starting from top.
  473. * Returns -1 if this is the last layer.
  474. */
  475. int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
  476. {
  477. struct ovl_entry *oe = dentry->d_fsdata;
  478. BUG_ON(idx < 0);
  479. if (idx == 0) {
  480. ovl_path_upper(dentry, path);
  481. if (path->dentry)
  482. return oe->numlower ? 1 : -1;
  483. idx++;
  484. }
  485. BUG_ON(idx > oe->numlower);
  486. *path = oe->lowerstack[idx - 1];
  487. return (idx < oe->numlower) ? idx + 1 : -1;
  488. }
  489. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  490. unsigned int flags)
  491. {
  492. struct ovl_entry *oe;
  493. const struct cred *old_cred;
  494. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  495. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  496. struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
  497. struct path *stack = NULL;
  498. struct dentry *upperdir, *upperdentry = NULL;
  499. struct dentry *index = NULL;
  500. unsigned int ctr = 0;
  501. struct inode *inode = NULL;
  502. bool upperopaque = false;
  503. char *upperredirect = NULL;
  504. struct dentry *this;
  505. unsigned int i;
  506. int err;
  507. struct ovl_lookup_data d = {
  508. .name = dentry->d_name,
  509. .is_dir = false,
  510. .opaque = false,
  511. .stop = false,
  512. .last = !poe->numlower,
  513. .redirect = NULL,
  514. };
  515. if (dentry->d_name.len > ofs->namelen)
  516. return ERR_PTR(-ENAMETOOLONG);
  517. old_cred = ovl_override_creds(dentry->d_sb);
  518. upperdir = ovl_dentry_upper(dentry->d_parent);
  519. if (upperdir) {
  520. err = ovl_lookup_layer(upperdir, &d, &upperdentry);
  521. if (err)
  522. goto out;
  523. if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
  524. dput(upperdentry);
  525. err = -EREMOTE;
  526. goto out;
  527. }
  528. if (upperdentry && !d.is_dir) {
  529. BUG_ON(!d.stop || d.redirect);
  530. /*
  531. * Lookup copy up origin by decoding origin file handle.
  532. * We may get a disconnected dentry, which is fine,
  533. * because we only need to hold the origin inode in
  534. * cache and use its inode number. We may even get a
  535. * connected dentry, that is not under any of the lower
  536. * layers root. That is also fine for using it's inode
  537. * number - it's the same as if we held a reference
  538. * to a dentry in lower layer that was moved under us.
  539. */
  540. err = ovl_check_origin(upperdentry, roe->lowerstack,
  541. roe->numlower, &stack, &ctr);
  542. if (err)
  543. goto out;
  544. }
  545. if (d.redirect) {
  546. upperredirect = kstrdup(d.redirect, GFP_KERNEL);
  547. if (!upperredirect)
  548. goto out_put_upper;
  549. if (d.redirect[0] == '/')
  550. poe = roe;
  551. }
  552. upperopaque = d.opaque;
  553. }
  554. if (!d.stop && poe->numlower) {
  555. err = -ENOMEM;
  556. stack = kcalloc(ofs->numlower, sizeof(struct path),
  557. GFP_KERNEL);
  558. if (!stack)
  559. goto out_put_upper;
  560. }
  561. for (i = 0; !d.stop && i < poe->numlower; i++) {
  562. struct path lowerpath = poe->lowerstack[i];
  563. d.last = i == poe->numlower - 1;
  564. err = ovl_lookup_layer(lowerpath.dentry, &d, &this);
  565. if (err)
  566. goto out_put;
  567. if (!this)
  568. continue;
  569. stack[ctr].dentry = this;
  570. stack[ctr].mnt = lowerpath.mnt;
  571. ctr++;
  572. if (d.stop)
  573. break;
  574. if (d.redirect && d.redirect[0] == '/' && poe != roe) {
  575. poe = roe;
  576. /* Find the current layer on the root dentry */
  577. for (i = 0; i < poe->numlower; i++)
  578. if (poe->lowerstack[i].mnt == lowerpath.mnt)
  579. break;
  580. if (WARN_ON(i == poe->numlower))
  581. break;
  582. }
  583. }
  584. /* Lookup index by lower inode and verify it matches upper inode */
  585. if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) {
  586. struct dentry *origin = stack[0].dentry;
  587. index = ovl_lookup_index(dentry, upperdentry, origin);
  588. if (IS_ERR(index)) {
  589. err = PTR_ERR(index);
  590. index = NULL;
  591. goto out_put;
  592. }
  593. }
  594. oe = ovl_alloc_entry(ctr);
  595. err = -ENOMEM;
  596. if (!oe)
  597. goto out_put;
  598. oe->opaque = upperopaque;
  599. memcpy(oe->lowerstack, stack, sizeof(struct path) * ctr);
  600. dentry->d_fsdata = oe;
  601. if (upperdentry)
  602. ovl_dentry_set_upper_alias(dentry);
  603. else if (index)
  604. upperdentry = dget(index);
  605. if (upperdentry || ctr) {
  606. inode = ovl_get_inode(dentry, upperdentry);
  607. err = PTR_ERR(inode);
  608. if (IS_ERR(inode))
  609. goto out_free_oe;
  610. OVL_I(inode)->redirect = upperredirect;
  611. if (index)
  612. ovl_set_flag(OVL_INDEX, inode);
  613. }
  614. revert_creds(old_cred);
  615. dput(index);
  616. kfree(stack);
  617. kfree(d.redirect);
  618. d_add(dentry, inode);
  619. return NULL;
  620. out_free_oe:
  621. dentry->d_fsdata = NULL;
  622. kfree(oe);
  623. out_put:
  624. dput(index);
  625. for (i = 0; i < ctr; i++)
  626. dput(stack[i].dentry);
  627. kfree(stack);
  628. out_put_upper:
  629. dput(upperdentry);
  630. kfree(upperredirect);
  631. out:
  632. kfree(d.redirect);
  633. revert_creds(old_cred);
  634. return ERR_PTR(err);
  635. }
  636. bool ovl_lower_positive(struct dentry *dentry)
  637. {
  638. struct ovl_entry *oe = dentry->d_fsdata;
  639. struct ovl_entry *poe = dentry->d_parent->d_fsdata;
  640. const struct qstr *name = &dentry->d_name;
  641. unsigned int i;
  642. bool positive = false;
  643. bool done = false;
  644. /*
  645. * If dentry is negative, then lower is positive iff this is a
  646. * whiteout.
  647. */
  648. if (!dentry->d_inode)
  649. return oe->opaque;
  650. /* Negative upper -> positive lower */
  651. if (!ovl_dentry_upper(dentry))
  652. return true;
  653. /* Positive upper -> have to look up lower to see whether it exists */
  654. for (i = 0; !done && !positive && i < poe->numlower; i++) {
  655. struct dentry *this;
  656. struct dentry *lowerdir = poe->lowerstack[i].dentry;
  657. this = lookup_one_len_unlocked(name->name, lowerdir,
  658. name->len);
  659. if (IS_ERR(this)) {
  660. switch (PTR_ERR(this)) {
  661. case -ENOENT:
  662. case -ENAMETOOLONG:
  663. break;
  664. default:
  665. /*
  666. * Assume something is there, we just couldn't
  667. * access it.
  668. */
  669. positive = true;
  670. break;
  671. }
  672. } else {
  673. if (this->d_inode) {
  674. positive = !ovl_is_whiteout(this);
  675. done = true;
  676. }
  677. dput(this);
  678. }
  679. }
  680. return positive;
  681. }