export.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  1. /*
  2. * Overlayfs NFS export support.
  3. *
  4. * Amir Goldstein <amir73il@gmail.com>
  5. *
  6. * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License version 2 as published by
  10. * the Free Software Foundation.
  11. */
  12. #include <linux/fs.h>
  13. #include <linux/cred.h>
  14. #include <linux/mount.h>
  15. #include <linux/namei.h>
  16. #include <linux/xattr.h>
  17. #include <linux/exportfs.h>
  18. #include <linux/ratelimit.h>
  19. #include "overlayfs.h"
  20. static int ovl_encode_maybe_copy_up(struct dentry *dentry)
  21. {
  22. int err;
  23. if (ovl_dentry_upper(dentry))
  24. return 0;
  25. err = ovl_want_write(dentry);
  26. if (!err) {
  27. err = ovl_copy_up(dentry);
  28. ovl_drop_write(dentry);
  29. }
  30. if (err) {
  31. pr_warn_ratelimited("overlayfs: failed to copy up on encode (%pd2, err=%i)\n",
  32. dentry, err);
  33. }
  34. return err;
  35. }
  36. /*
  37. * Before encoding a non-upper directory file handle from real layer N, we need
  38. * to check if it will be possible to reconnect an overlay dentry from the real
  39. * lower decoded dentry. This is done by following the overlay ancestry up to a
  40. * "layer N connected" ancestor and verifying that all parents along the way are
  41. * "layer N connectable". If an ancestor that is NOT "layer N connectable" is
  42. * found, we need to copy up an ancestor, which is "layer N connectable", thus
  43. * making that ancestor "layer N connected". For example:
  44. *
  45. * layer 1: /a
  46. * layer 2: /a/b/c
  47. *
  48. * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is
  49. * copied up and renamed, upper dir /a will be indexed by lower dir /a from
  50. * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*)
  51. * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay
  52. * dentry from the connected lower dentry /a/b/c.
  53. *
  54. * To avoid this problem on decode time, we need to copy up an ancestor of
  55. * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is
  56. * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected"
  57. * and when the time comes to decode the file handle from lower dentry /a/b/c,
  58. * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding
  59. * a connected overlay dentry will be accomplished.
  60. *
  61. * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an
  62. * entry /a in the lower layers above layer N and find the indexed dir /a from
  63. * layer 1. If that improvement is made, then the check for "layer N connected"
  64. * will need to verify there are no redirects in lower layers above N. In the
  65. * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a
  66. * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable":
  67. *
  68. * layer 1: /A (redirect = /a)
  69. * layer 2: /a/b/c
  70. */
  71. /* Return the lowest layer for encoding a connectable file handle */
  72. static int ovl_connectable_layer(struct dentry *dentry)
  73. {
  74. struct ovl_entry *oe = OVL_E(dentry);
  75. /* We can get overlay root from root of any layer */
  76. if (dentry == dentry->d_sb->s_root)
  77. return oe->numlower;
  78. /*
  79. * If it's an unindexed merge dir, then it's not connectable with any
  80. * lower layer
  81. */
  82. if (ovl_dentry_upper(dentry) &&
  83. !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  84. return 0;
  85. /* We can get upper/overlay path from indexed/lower dentry */
  86. return oe->lowerstack[0].layer->idx;
  87. }
  88. /*
  89. * @dentry is "connected" if all ancestors up to root or a "connected" ancestor
  90. * have the same uppermost lower layer as the origin's layer. We may need to
  91. * copy up a "connectable" ancestor to make it "connected". A "connected" dentry
  92. * cannot become non "connected", so cache positive result in dentry flags.
  93. *
  94. * Return the connected origin layer or < 0 on error.
  95. */
  96. static int ovl_connect_layer(struct dentry *dentry)
  97. {
  98. struct dentry *next, *parent = NULL;
  99. int origin_layer;
  100. int err = 0;
  101. if (WARN_ON(dentry == dentry->d_sb->s_root) ||
  102. WARN_ON(!ovl_dentry_lower(dentry)))
  103. return -EIO;
  104. origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx;
  105. if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
  106. return origin_layer;
  107. /* Find the topmost origin layer connectable ancestor of @dentry */
  108. next = dget(dentry);
  109. for (;;) {
  110. parent = dget_parent(next);
  111. if (WARN_ON(parent == next)) {
  112. err = -EIO;
  113. break;
  114. }
  115. /*
  116. * If @parent is not origin layer connectable, then copy up
  117. * @next which is origin layer connectable and we are done.
  118. */
  119. if (ovl_connectable_layer(parent) < origin_layer) {
  120. err = ovl_encode_maybe_copy_up(next);
  121. break;
  122. }
  123. /* If @parent is connected or indexed we are done */
  124. if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
  125. ovl_test_flag(OVL_INDEX, d_inode(parent)))
  126. break;
  127. dput(next);
  128. next = parent;
  129. }
  130. dput(parent);
  131. dput(next);
  132. if (!err)
  133. ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
  134. return err ?: origin_layer;
  135. }
  136. /*
  137. * We only need to encode origin if there is a chance that the same object was
  138. * encoded pre copy up and then we need to stay consistent with the same
  139. * encoding also after copy up. If non-pure upper is not indexed, then it was
  140. * copied up before NFS export was enabled. In that case we don't need to worry
  141. * about staying consistent with pre copy up encoding and we encode an upper
  142. * file handle. Overlay root dentry is a private case of non-indexed upper.
  143. *
  144. * The following table summarizes the different file handle encodings used for
  145. * different overlay object types:
  146. *
  147. * Object type | Encoding
  148. * --------------------------------
  149. * Pure upper | U
  150. * Non-indexed upper | U
  151. * Indexed upper | L (*)
  152. * Non-upper | L (*)
  153. *
  154. * U = upper file handle
  155. * L = lower file handle
  156. *
  157. * (*) Connecting an overlay dir from real lower dentry is not always
  158. * possible when there are redirects in lower layers and non-indexed merge dirs.
  159. * To mitigate those case, we may copy up the lower dir ancestor before encode
  160. * a lower dir file handle.
  161. *
  162. * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
  163. */
  164. static int ovl_check_encode_origin(struct dentry *dentry)
  165. {
  166. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  167. /* Upper file handle for pure upper */
  168. if (!ovl_dentry_lower(dentry))
  169. return 0;
  170. /*
  171. * Upper file handle for non-indexed upper.
  172. *
  173. * Root is never indexed, so if there's an upper layer, encode upper for
  174. * root.
  175. */
  176. if (ovl_dentry_upper(dentry) &&
  177. !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  178. return 0;
  179. /*
  180. * Decoding a merge dir, whose origin's ancestor is under a redirected
  181. * lower dir or under a non-indexed upper is not always possible.
  182. * ovl_connect_layer() will try to make origin's layer "connected" by
  183. * copying up a "connectable" ancestor.
  184. */
  185. if (d_is_dir(dentry) && ofs->upper_mnt)
  186. return ovl_connect_layer(dentry);
  187. /* Lower file handle for indexed and non-upper dir/non-dir */
  188. return 1;
  189. }
  190. static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen)
  191. {
  192. struct ovl_fh *fh = NULL;
  193. int err, enc_lower;
  194. /*
  195. * Check if we should encode a lower or upper file handle and maybe
  196. * copy up an ancestor to make lower file handle connectable.
  197. */
  198. err = enc_lower = ovl_check_encode_origin(dentry);
  199. if (enc_lower < 0)
  200. goto fail;
  201. /* Encode an upper or lower file handle */
  202. fh = ovl_encode_fh(enc_lower ? ovl_dentry_lower(dentry) :
  203. ovl_dentry_upper(dentry), !enc_lower);
  204. err = PTR_ERR(fh);
  205. if (IS_ERR(fh))
  206. goto fail;
  207. err = -EOVERFLOW;
  208. if (fh->len > buflen)
  209. goto fail;
  210. memcpy(buf, (char *)fh, fh->len);
  211. err = fh->len;
  212. out:
  213. kfree(fh);
  214. return err;
  215. fail:
  216. pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n",
  217. dentry, err, buflen, fh ? (int)fh->len : 0,
  218. fh ? fh->type : 0);
  219. goto out;
  220. }
  221. static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len)
  222. {
  223. int res, len = *max_len << 2;
  224. res = ovl_d_to_fh(dentry, (char *)fid, len);
  225. if (res <= 0)
  226. return FILEID_INVALID;
  227. len = res;
  228. /* Round up to dwords */
  229. *max_len = (len + 3) >> 2;
  230. return OVL_FILEID;
  231. }
  232. static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len,
  233. struct inode *parent)
  234. {
  235. struct dentry *dentry;
  236. int type;
  237. /* TODO: encode connectable file handles */
  238. if (parent)
  239. return FILEID_INVALID;
  240. dentry = d_find_any_alias(inode);
  241. if (WARN_ON(!dentry))
  242. return FILEID_INVALID;
  243. type = ovl_dentry_to_fh(dentry, fid, max_len);
  244. dput(dentry);
  245. return type;
  246. }
  247. /*
  248. * Find or instantiate an overlay dentry from real dentries and index.
  249. */
  250. static struct dentry *ovl_obtain_alias(struct super_block *sb,
  251. struct dentry *upper_alias,
  252. struct ovl_path *lowerpath,
  253. struct dentry *index)
  254. {
  255. struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
  256. struct dentry *upper = upper_alias ?: index;
  257. struct dentry *dentry;
  258. struct inode *inode;
  259. struct ovl_entry *oe;
  260. /* We get overlay directory dentries with ovl_lookup_real() */
  261. if (d_is_dir(upper ?: lower))
  262. return ERR_PTR(-EIO);
  263. inode = ovl_get_inode(sb, dget(upper), lower, index, !!lower);
  264. if (IS_ERR(inode)) {
  265. dput(upper);
  266. return ERR_CAST(inode);
  267. }
  268. if (index)
  269. ovl_set_flag(OVL_INDEX, inode);
  270. dentry = d_find_any_alias(inode);
  271. if (!dentry) {
  272. dentry = d_alloc_anon(inode->i_sb);
  273. if (!dentry)
  274. goto nomem;
  275. oe = ovl_alloc_entry(lower ? 1 : 0);
  276. if (!oe)
  277. goto nomem;
  278. if (lower) {
  279. oe->lowerstack->dentry = dget(lower);
  280. oe->lowerstack->layer = lowerpath->layer;
  281. }
  282. dentry->d_fsdata = oe;
  283. if (upper_alias)
  284. ovl_dentry_set_upper_alias(dentry);
  285. }
  286. return d_instantiate_anon(dentry, inode);
  287. nomem:
  288. iput(inode);
  289. dput(dentry);
  290. return ERR_PTR(-ENOMEM);
  291. }
  292. /* Get the upper or lower dentry in stach whose on layer @idx */
  293. static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
  294. {
  295. struct ovl_entry *oe = dentry->d_fsdata;
  296. int i;
  297. if (!idx)
  298. return ovl_dentry_upper(dentry);
  299. for (i = 0; i < oe->numlower; i++) {
  300. if (oe->lowerstack[i].layer->idx == idx)
  301. return oe->lowerstack[i].dentry;
  302. }
  303. return NULL;
  304. }
  305. /*
  306. * Lookup a child overlay dentry to get a connected overlay dentry whose real
  307. * dentry is @real. If @real is on upper layer, we lookup a child overlay
  308. * dentry with the same name as the real dentry. Otherwise, we need to consult
  309. * index for lookup.
  310. */
  311. static struct dentry *ovl_lookup_real_one(struct dentry *connected,
  312. struct dentry *real,
  313. struct ovl_layer *layer)
  314. {
  315. struct inode *dir = d_inode(connected);
  316. struct dentry *this, *parent = NULL;
  317. struct name_snapshot name;
  318. int err;
  319. /*
  320. * Lookup child overlay dentry by real name. The dir mutex protects us
  321. * from racing with overlay rename. If the overlay dentry that is above
  322. * real has already been moved to a parent that is not under the
  323. * connected overlay dir, we return -ECHILD and restart the lookup of
  324. * connected real path from the top.
  325. */
  326. inode_lock_nested(dir, I_MUTEX_PARENT);
  327. err = -ECHILD;
  328. parent = dget_parent(real);
  329. if (ovl_dentry_real_at(connected, layer->idx) != parent)
  330. goto fail;
  331. /*
  332. * We also need to take a snapshot of real dentry name to protect us
  333. * from racing with underlying layer rename. In this case, we don't
  334. * care about returning ESTALE, only from dereferencing a free name
  335. * pointer because we hold no lock on the real dentry.
  336. */
  337. take_dentry_name_snapshot(&name, real);
  338. this = lookup_one_len(name.name, connected, strlen(name.name));
  339. err = PTR_ERR(this);
  340. if (IS_ERR(this)) {
  341. goto fail;
  342. } else if (!this || !this->d_inode) {
  343. dput(this);
  344. err = -ENOENT;
  345. goto fail;
  346. } else if (ovl_dentry_real_at(this, layer->idx) != real) {
  347. dput(this);
  348. err = -ESTALE;
  349. goto fail;
  350. }
  351. out:
  352. release_dentry_name_snapshot(&name);
  353. dput(parent);
  354. inode_unlock(dir);
  355. return this;
  356. fail:
  357. pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  358. real, layer->idx, connected, err);
  359. this = ERR_PTR(err);
  360. goto out;
  361. }
  362. static struct dentry *ovl_lookup_real(struct super_block *sb,
  363. struct dentry *real,
  364. struct ovl_layer *layer);
  365. /*
  366. * Lookup an indexed or hashed overlay dentry by real inode.
  367. */
  368. static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
  369. struct dentry *real,
  370. struct ovl_layer *layer)
  371. {
  372. struct ovl_fs *ofs = sb->s_fs_info;
  373. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  374. struct dentry *index = NULL;
  375. struct dentry *this = NULL;
  376. struct inode *inode;
  377. /*
  378. * Decoding upper dir from index is expensive, so first try to lookup
  379. * overlay dentry in inode/dcache.
  380. */
  381. inode = ovl_lookup_inode(sb, real, !layer->idx);
  382. if (IS_ERR(inode))
  383. return ERR_CAST(inode);
  384. if (inode) {
  385. this = d_find_any_alias(inode);
  386. iput(inode);
  387. }
  388. /*
  389. * For decoded lower dir file handle, lookup index by origin to check
  390. * if lower dir was copied up and and/or removed.
  391. */
  392. if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
  393. index = ovl_lookup_index(ofs, NULL, real, false);
  394. if (IS_ERR(index))
  395. return index;
  396. }
  397. /* Get connected upper overlay dir from index */
  398. if (index) {
  399. struct dentry *upper = ovl_index_upper(ofs, index);
  400. dput(index);
  401. if (IS_ERR_OR_NULL(upper))
  402. return upper;
  403. /*
  404. * ovl_lookup_real() in lower layer may call recursively once to
  405. * ovl_lookup_real() in upper layer. The first level call walks
  406. * back lower parents to the topmost indexed parent. The second
  407. * recursive call walks back from indexed upper to the topmost
  408. * connected/hashed upper parent (or up to root).
  409. */
  410. this = ovl_lookup_real(sb, upper, &upper_layer);
  411. dput(upper);
  412. }
  413. if (IS_ERR_OR_NULL(this))
  414. return this;
  415. if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) {
  416. dput(this);
  417. this = ERR_PTR(-EIO);
  418. }
  419. return this;
  420. }
  421. /*
  422. * Lookup an indexed or hashed overlay dentry, whose real dentry is an
  423. * ancestor of @real.
  424. */
  425. static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
  426. struct dentry *real,
  427. struct ovl_layer *layer)
  428. {
  429. struct dentry *next, *parent = NULL;
  430. struct dentry *ancestor = ERR_PTR(-EIO);
  431. if (real == layer->mnt->mnt_root)
  432. return dget(sb->s_root);
  433. /* Find the topmost indexed or hashed ancestor */
  434. next = dget(real);
  435. for (;;) {
  436. parent = dget_parent(next);
  437. /*
  438. * Lookup a matching overlay dentry in inode/dentry
  439. * cache or in index by real inode.
  440. */
  441. ancestor = ovl_lookup_real_inode(sb, next, layer);
  442. if (ancestor)
  443. break;
  444. if (parent == layer->mnt->mnt_root) {
  445. ancestor = dget(sb->s_root);
  446. break;
  447. }
  448. /*
  449. * If @real has been moved out of the layer root directory,
  450. * we will eventully hit the real fs root. This cannot happen
  451. * by legit overlay rename, so we return error in that case.
  452. */
  453. if (parent == next) {
  454. ancestor = ERR_PTR(-EXDEV);
  455. break;
  456. }
  457. dput(next);
  458. next = parent;
  459. }
  460. dput(parent);
  461. dput(next);
  462. return ancestor;
  463. }
  464. /*
  465. * Lookup a connected overlay dentry whose real dentry is @real.
  466. * If @real is on upper layer, we lookup a child overlay dentry with the same
  467. * path the real dentry. Otherwise, we need to consult index for lookup.
  468. */
  469. static struct dentry *ovl_lookup_real(struct super_block *sb,
  470. struct dentry *real,
  471. struct ovl_layer *layer)
  472. {
  473. struct dentry *connected;
  474. int err = 0;
  475. connected = ovl_lookup_real_ancestor(sb, real, layer);
  476. if (IS_ERR(connected))
  477. return connected;
  478. while (!err) {
  479. struct dentry *next, *this;
  480. struct dentry *parent = NULL;
  481. struct dentry *real_connected = ovl_dentry_real_at(connected,
  482. layer->idx);
  483. if (real_connected == real)
  484. break;
  485. /* Find the topmost dentry not yet connected */
  486. next = dget(real);
  487. for (;;) {
  488. parent = dget_parent(next);
  489. if (parent == real_connected)
  490. break;
  491. /*
  492. * If real has been moved out of 'real_connected',
  493. * we will not find 'real_connected' and hit the layer
  494. * root. In that case, we need to restart connecting.
  495. * This game can go on forever in the worst case. We
  496. * may want to consider taking s_vfs_rename_mutex if
  497. * this happens more than once.
  498. */
  499. if (parent == layer->mnt->mnt_root) {
  500. dput(connected);
  501. connected = dget(sb->s_root);
  502. break;
  503. }
  504. /*
  505. * If real file has been moved out of the layer root
  506. * directory, we will eventully hit the real fs root.
  507. * This cannot happen by legit overlay rename, so we
  508. * return error in that case.
  509. */
  510. if (parent == next) {
  511. err = -EXDEV;
  512. break;
  513. }
  514. dput(next);
  515. next = parent;
  516. }
  517. if (!err) {
  518. this = ovl_lookup_real_one(connected, next, layer);
  519. if (IS_ERR(this))
  520. err = PTR_ERR(this);
  521. /*
  522. * Lookup of child in overlay can fail when racing with
  523. * overlay rename of child away from 'connected' parent.
  524. * In this case, we need to restart the lookup from the
  525. * top, because we cannot trust that 'real_connected' is
  526. * still an ancestor of 'real'. There is a good chance
  527. * that the renamed overlay ancestor is now in cache, so
  528. * ovl_lookup_real_ancestor() will find it and we can
  529. * continue to connect exactly from where lookup failed.
  530. */
  531. if (err == -ECHILD) {
  532. this = ovl_lookup_real_ancestor(sb, real,
  533. layer);
  534. err = PTR_ERR_OR_ZERO(this);
  535. }
  536. if (!err) {
  537. dput(connected);
  538. connected = this;
  539. }
  540. }
  541. dput(parent);
  542. dput(next);
  543. }
  544. if (err)
  545. goto fail;
  546. return connected;
  547. fail:
  548. pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  549. real, layer->idx, connected, err);
  550. dput(connected);
  551. return ERR_PTR(err);
  552. }
  553. /*
  554. * Get an overlay dentry from upper/lower real dentries and index.
  555. */
  556. static struct dentry *ovl_get_dentry(struct super_block *sb,
  557. struct dentry *upper,
  558. struct ovl_path *lowerpath,
  559. struct dentry *index)
  560. {
  561. struct ovl_fs *ofs = sb->s_fs_info;
  562. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  563. struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
  564. struct dentry *real = upper ?: (index ?: lowerpath->dentry);
  565. /*
  566. * Obtain a disconnected overlay dentry from a non-dir real dentry
  567. * and index.
  568. */
  569. if (!d_is_dir(real))
  570. return ovl_obtain_alias(sb, upper, lowerpath, index);
  571. /* Removed empty directory? */
  572. if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
  573. return ERR_PTR(-ENOENT);
  574. /*
  575. * If real dentry is connected and hashed, get a connected overlay
  576. * dentry whose real dentry is @real.
  577. */
  578. return ovl_lookup_real(sb, real, layer);
  579. }
  580. static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
  581. struct ovl_fh *fh)
  582. {
  583. struct ovl_fs *ofs = sb->s_fs_info;
  584. struct dentry *dentry;
  585. struct dentry *upper;
  586. if (!ofs->upper_mnt)
  587. return ERR_PTR(-EACCES);
  588. upper = ovl_decode_fh(fh, ofs->upper_mnt);
  589. if (IS_ERR_OR_NULL(upper))
  590. return upper;
  591. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  592. dput(upper);
  593. return dentry;
  594. }
  595. static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
  596. struct ovl_fh *fh)
  597. {
  598. struct ovl_fs *ofs = sb->s_fs_info;
  599. struct ovl_path origin = { };
  600. struct ovl_path *stack = &origin;
  601. struct dentry *dentry = NULL;
  602. struct dentry *index = NULL;
  603. struct inode *inode = NULL;
  604. bool is_deleted = false;
  605. int err;
  606. /* First lookup indexed upper by fh */
  607. if (ofs->indexdir) {
  608. index = ovl_get_index_fh(ofs, fh);
  609. err = PTR_ERR(index);
  610. if (IS_ERR(index)) {
  611. if (err != -ESTALE)
  612. return ERR_PTR(err);
  613. /* Found a whiteout index - treat as deleted inode */
  614. is_deleted = true;
  615. index = NULL;
  616. }
  617. }
  618. /* Then try to get upper dir by index */
  619. if (index && d_is_dir(index)) {
  620. struct dentry *upper = ovl_index_upper(ofs, index);
  621. err = PTR_ERR(upper);
  622. if (IS_ERR_OR_NULL(upper))
  623. goto out_err;
  624. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  625. dput(upper);
  626. goto out;
  627. }
  628. /* Then lookup origin by fh */
  629. err = ovl_check_origin_fh(ofs, fh, NULL, &stack);
  630. if (err) {
  631. goto out_err;
  632. } else if (index) {
  633. err = ovl_verify_origin(index, origin.dentry, false);
  634. if (err)
  635. goto out_err;
  636. } else if (is_deleted) {
  637. /* Lookup deleted non-dir by origin inode */
  638. if (!d_is_dir(origin.dentry))
  639. inode = ovl_lookup_inode(sb, origin.dentry, false);
  640. err = -ESTALE;
  641. if (!inode || atomic_read(&inode->i_count) == 1)
  642. goto out_err;
  643. /* Deleted but still open? */
  644. index = dget(ovl_i_dentry_upper(inode));
  645. }
  646. dentry = ovl_get_dentry(sb, NULL, &origin, index);
  647. out:
  648. dput(origin.dentry);
  649. dput(index);
  650. iput(inode);
  651. return dentry;
  652. out_err:
  653. dentry = ERR_PTR(err);
  654. goto out;
  655. }
  656. static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
  657. int fh_len, int fh_type)
  658. {
  659. struct dentry *dentry = NULL;
  660. struct ovl_fh *fh = (struct ovl_fh *) fid;
  661. int len = fh_len << 2;
  662. unsigned int flags = 0;
  663. int err;
  664. err = -EINVAL;
  665. if (fh_type != OVL_FILEID)
  666. goto out_err;
  667. err = ovl_check_fh_len(fh, len);
  668. if (err)
  669. goto out_err;
  670. flags = fh->flags;
  671. dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
  672. ovl_upper_fh_to_d(sb, fh) :
  673. ovl_lower_fh_to_d(sb, fh);
  674. err = PTR_ERR(dentry);
  675. if (IS_ERR(dentry) && err != -ESTALE)
  676. goto out_err;
  677. return dentry;
  678. out_err:
  679. pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
  680. len, fh_type, flags, err);
  681. return ERR_PTR(err);
  682. }
  683. static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
  684. int fh_len, int fh_type)
  685. {
  686. pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
  687. return ERR_PTR(-EACCES);
  688. }
  689. static int ovl_get_name(struct dentry *parent, char *name,
  690. struct dentry *child)
  691. {
  692. /*
  693. * ovl_fh_to_dentry() returns connected dir overlay dentries and
  694. * ovl_fh_to_parent() is not implemented, so we should not get here.
  695. */
  696. WARN_ON_ONCE(1);
  697. return -EIO;
  698. }
  699. static struct dentry *ovl_get_parent(struct dentry *dentry)
  700. {
  701. /*
  702. * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
  703. * should not get here.
  704. */
  705. WARN_ON_ONCE(1);
  706. return ERR_PTR(-EIO);
  707. }
  708. const struct export_operations ovl_export_operations = {
  709. .encode_fh = ovl_encode_inode_fh,
  710. .fh_to_dentry = ovl_fh_to_dentry,
  711. .fh_to_parent = ovl_fh_to_parent,
  712. .get_name = ovl_get_name,
  713. .get_parent = ovl_get_parent,
  714. };