export.c 23 KB

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