export.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. Could skip
  335. * permission checking altogether, but for now just use non-idmap
  336. * transformed ids.
  337. */
  338. this = lookup_one_len(name.name.name, connected, name.name.len);
  339. release_dentry_name_snapshot(&name);
  340. err = PTR_ERR(this);
  341. if (IS_ERR(this)) {
  342. goto fail;
  343. } else if (!this || !this->d_inode) {
  344. dput(this);
  345. err = -ENOENT;
  346. goto fail;
  347. } else if (ovl_dentry_real_at(this, layer->idx) != real) {
  348. dput(this);
  349. err = -ESTALE;
  350. goto fail;
  351. }
  352. out:
  353. dput(parent);
  354. inode_unlock(dir);
  355. return this;
  356. fail:
  357. pr_warn_ratelimited("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. const 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. const struct ovl_layer *layer)
  371. {
  372. struct ovl_fs *ofs = OVL_FS(sb);
  373. struct dentry *index = NULL;
  374. struct dentry *this = NULL;
  375. struct inode *inode;
  376. /*
  377. * Decoding upper dir from index is expensive, so first try to lookup
  378. * overlay dentry in inode/dcache.
  379. */
  380. inode = ovl_lookup_inode(sb, real, !layer->idx);
  381. if (IS_ERR(inode))
  382. return ERR_CAST(inode);
  383. if (inode) {
  384. this = d_find_any_alias(inode);
  385. iput(inode);
  386. }
  387. /*
  388. * For decoded lower dir file handle, lookup index by origin to check
  389. * if lower dir was copied up and and/or removed.
  390. */
  391. if (!this && layer->idx && ovl_indexdir(sb) && !WARN_ON(!d_is_dir(real))) {
  392. index = ovl_lookup_index(ofs, NULL, real, false);
  393. if (IS_ERR(index))
  394. return index;
  395. }
  396. /* Get connected upper overlay dir from index */
  397. if (index) {
  398. struct dentry *upper = ovl_index_upper(ofs, index, true);
  399. dput(index);
  400. if (IS_ERR_OR_NULL(upper))
  401. return upper;
  402. /*
  403. * ovl_lookup_real() in lower layer may call recursively once to
  404. * ovl_lookup_real() in upper layer. The first level call walks
  405. * back lower parents to the topmost indexed parent. The second
  406. * recursive call walks back from indexed upper to the topmost
  407. * connected/hashed upper parent (or up to root).
  408. */
  409. this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
  410. dput(upper);
  411. }
  412. if (IS_ERR_OR_NULL(this))
  413. return this;
  414. if (ovl_dentry_real_at(this, layer->idx) != real) {
  415. dput(this);
  416. this = ERR_PTR(-EIO);
  417. }
  418. return this;
  419. }
  420. /*
  421. * Lookup an indexed or hashed overlay dentry, whose real dentry is an
  422. * ancestor of @real.
  423. */
  424. static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
  425. struct dentry *real,
  426. const struct ovl_layer *layer)
  427. {
  428. struct dentry *next, *parent = NULL;
  429. struct dentry *ancestor = ERR_PTR(-EIO);
  430. if (real == layer->mnt->mnt_root)
  431. return dget(sb->s_root);
  432. /* Find the topmost indexed or hashed ancestor */
  433. next = dget(real);
  434. for (;;) {
  435. parent = dget_parent(next);
  436. /*
  437. * Lookup a matching overlay dentry in inode/dentry
  438. * cache or in index by real inode.
  439. */
  440. ancestor = ovl_lookup_real_inode(sb, next, layer);
  441. if (ancestor)
  442. break;
  443. if (parent == layer->mnt->mnt_root) {
  444. ancestor = dget(sb->s_root);
  445. break;
  446. }
  447. /*
  448. * If @real has been moved out of the layer root directory,
  449. * we will eventully hit the real fs root. This cannot happen
  450. * by legit overlay rename, so we return error in that case.
  451. */
  452. if (parent == next) {
  453. ancestor = ERR_PTR(-EXDEV);
  454. break;
  455. }
  456. dput(next);
  457. next = parent;
  458. }
  459. dput(parent);
  460. dput(next);
  461. return ancestor;
  462. }
  463. /*
  464. * Lookup a connected overlay dentry whose real dentry is @real.
  465. * If @real is on upper layer, we lookup a child overlay dentry with the same
  466. * path the real dentry. Otherwise, we need to consult index for lookup.
  467. */
  468. static struct dentry *ovl_lookup_real(struct super_block *sb,
  469. struct dentry *real,
  470. const struct ovl_layer *layer)
  471. {
  472. struct dentry *connected;
  473. int err = 0;
  474. connected = ovl_lookup_real_ancestor(sb, real, layer);
  475. if (IS_ERR(connected))
  476. return connected;
  477. while (!err) {
  478. struct dentry *next, *this;
  479. struct dentry *parent = NULL;
  480. struct dentry *real_connected = ovl_dentry_real_at(connected,
  481. layer->idx);
  482. if (real_connected == real)
  483. break;
  484. /* Find the topmost dentry not yet connected */
  485. next = dget(real);
  486. for (;;) {
  487. parent = dget_parent(next);
  488. if (parent == real_connected)
  489. break;
  490. /*
  491. * If real has been moved out of 'real_connected',
  492. * we will not find 'real_connected' and hit the layer
  493. * root. In that case, we need to restart connecting.
  494. * This game can go on forever in the worst case. We
  495. * may want to consider taking s_vfs_rename_mutex if
  496. * this happens more than once.
  497. */
  498. if (parent == layer->mnt->mnt_root) {
  499. dput(connected);
  500. connected = dget(sb->s_root);
  501. break;
  502. }
  503. /*
  504. * If real file has been moved out of the layer root
  505. * directory, we will eventully hit the real fs root.
  506. * This cannot happen by legit overlay rename, so we
  507. * return error in that case.
  508. */
  509. if (parent == next) {
  510. err = -EXDEV;
  511. break;
  512. }
  513. dput(next);
  514. next = parent;
  515. }
  516. if (!err) {
  517. this = ovl_lookup_real_one(connected, next, layer);
  518. if (IS_ERR(this))
  519. err = PTR_ERR(this);
  520. /*
  521. * Lookup of child in overlay can fail when racing with
  522. * overlay rename of child away from 'connected' parent.
  523. * In this case, we need to restart the lookup from the
  524. * top, because we cannot trust that 'real_connected' is
  525. * still an ancestor of 'real'. There is a good chance
  526. * that the renamed overlay ancestor is now in cache, so
  527. * ovl_lookup_real_ancestor() will find it and we can
  528. * continue to connect exactly from where lookup failed.
  529. */
  530. if (err == -ECHILD) {
  531. this = ovl_lookup_real_ancestor(sb, real,
  532. layer);
  533. err = PTR_ERR_OR_ZERO(this);
  534. }
  535. if (!err) {
  536. dput(connected);
  537. connected = this;
  538. }
  539. }
  540. dput(parent);
  541. dput(next);
  542. }
  543. if (err)
  544. goto fail;
  545. return connected;
  546. fail:
  547. pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  548. real, layer->idx, connected, err);
  549. dput(connected);
  550. return ERR_PTR(err);
  551. }
  552. /*
  553. * Get an overlay dentry from upper/lower real dentries and index.
  554. */
  555. static struct dentry *ovl_get_dentry(struct super_block *sb,
  556. struct dentry *upper,
  557. struct ovl_path *lowerpath,
  558. struct dentry *index)
  559. {
  560. struct ovl_fs *ofs = OVL_FS(sb);
  561. const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
  562. struct dentry *real = upper ?: (index ?: lowerpath->dentry);
  563. /*
  564. * Obtain a disconnected overlay dentry from a non-dir real dentry
  565. * and index.
  566. */
  567. if (!d_is_dir(real))
  568. return ovl_obtain_alias(sb, upper, lowerpath, index);
  569. /* Removed empty directory? */
  570. if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
  571. return ERR_PTR(-ENOENT);
  572. /*
  573. * If real dentry is connected and hashed, get a connected overlay
  574. * dentry whose real dentry is @real.
  575. */
  576. return ovl_lookup_real(sb, real, layer);
  577. }
  578. static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
  579. struct ovl_fh *fh)
  580. {
  581. struct ovl_fs *ofs = OVL_FS(sb);
  582. struct dentry *dentry;
  583. struct dentry *upper;
  584. if (!ovl_upper_mnt(ofs))
  585. return ERR_PTR(-EACCES);
  586. upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
  587. if (IS_ERR_OR_NULL(upper))
  588. return upper;
  589. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  590. dput(upper);
  591. return dentry;
  592. }
  593. static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
  594. struct ovl_fh *fh)
  595. {
  596. struct ovl_fs *ofs = OVL_FS(sb);
  597. struct ovl_path origin = { };
  598. struct ovl_path *stack = &origin;
  599. struct dentry *dentry = NULL;
  600. struct dentry *index = NULL;
  601. struct inode *inode;
  602. int err;
  603. /* First lookup overlay inode in inode cache by origin fh */
  604. err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
  605. if (err)
  606. return ERR_PTR(err);
  607. if (!d_is_dir(origin.dentry) ||
  608. !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
  609. inode = ovl_lookup_inode(sb, origin.dentry, false);
  610. err = PTR_ERR(inode);
  611. if (IS_ERR(inode))
  612. goto out_err;
  613. if (inode) {
  614. dentry = d_find_any_alias(inode);
  615. iput(inode);
  616. if (dentry)
  617. goto out;
  618. }
  619. }
  620. /* Then lookup indexed upper/whiteout by origin fh */
  621. if (ovl_indexdir(sb)) {
  622. index = ovl_get_index_fh(ofs, fh);
  623. err = PTR_ERR(index);
  624. if (IS_ERR(index)) {
  625. index = NULL;
  626. goto out_err;
  627. }
  628. }
  629. /* Then try to get a connected upper dir by index */
  630. if (index && d_is_dir(index)) {
  631. struct dentry *upper = ovl_index_upper(ofs, index, true);
  632. err = PTR_ERR(upper);
  633. if (IS_ERR_OR_NULL(upper))
  634. goto out_err;
  635. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  636. dput(upper);
  637. goto out;
  638. }
  639. /* Find origin.dentry again with ovl_acceptable() layer check */
  640. if (d_is_dir(origin.dentry)) {
  641. dput(origin.dentry);
  642. origin.dentry = NULL;
  643. err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
  644. if (err)
  645. goto out_err;
  646. }
  647. if (index) {
  648. err = ovl_verify_origin(ofs, index, origin.dentry, false);
  649. if (err)
  650. goto out_err;
  651. }
  652. /* Get a connected non-upper dir or disconnected non-dir */
  653. dentry = ovl_get_dentry(sb, NULL, &origin, index);
  654. out:
  655. dput(origin.dentry);
  656. dput(index);
  657. return dentry;
  658. out_err:
  659. dentry = ERR_PTR(err);
  660. goto out;
  661. }
  662. static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
  663. {
  664. struct ovl_fh *fh;
  665. /* If on-wire inner fid is aligned - nothing to do */
  666. if (fh_type == OVL_FILEID_V1)
  667. return (struct ovl_fh *)fid;
  668. if (fh_type != OVL_FILEID_V0)
  669. return ERR_PTR(-EINVAL);
  670. if (buflen <= OVL_FH_WIRE_OFFSET)
  671. return ERR_PTR(-EINVAL);
  672. fh = kzalloc(buflen, GFP_KERNEL);
  673. if (!fh)
  674. return ERR_PTR(-ENOMEM);
  675. /* Copy unaligned inner fh into aligned buffer */
  676. memcpy(fh->buf, fid, buflen - OVL_FH_WIRE_OFFSET);
  677. return fh;
  678. }
  679. static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
  680. int fh_len, int fh_type)
  681. {
  682. struct dentry *dentry = NULL;
  683. struct ovl_fh *fh = NULL;
  684. int len = fh_len << 2;
  685. unsigned int flags = 0;
  686. int err;
  687. fh = ovl_fid_to_fh(fid, len, fh_type);
  688. err = PTR_ERR(fh);
  689. if (IS_ERR(fh))
  690. goto out_err;
  691. err = ovl_check_fh_len(fh, len);
  692. if (err)
  693. goto out_err;
  694. flags = fh->fb.flags;
  695. dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
  696. ovl_upper_fh_to_d(sb, fh) :
  697. ovl_lower_fh_to_d(sb, fh);
  698. err = PTR_ERR(dentry);
  699. if (IS_ERR(dentry) && err != -ESTALE)
  700. goto out_err;
  701. out:
  702. /* We may have needed to re-align OVL_FILEID_V0 */
  703. if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
  704. kfree(fh);
  705. return dentry;
  706. out_err:
  707. pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
  708. fh_len, fh_type, flags, err);
  709. dentry = ERR_PTR(err);
  710. goto out;
  711. }
  712. static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
  713. int fh_len, int fh_type)
  714. {
  715. pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
  716. return ERR_PTR(-EACCES);
  717. }
  718. static int ovl_get_name(struct dentry *parent, char *name,
  719. struct dentry *child)
  720. {
  721. /*
  722. * ovl_fh_to_dentry() returns connected dir overlay dentries and
  723. * ovl_fh_to_parent() is not implemented, so we should not get here.
  724. */
  725. WARN_ON_ONCE(1);
  726. return -EIO;
  727. }
  728. static struct dentry *ovl_get_parent(struct dentry *dentry)
  729. {
  730. /*
  731. * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
  732. * should not get here.
  733. */
  734. WARN_ON_ONCE(1);
  735. return ERR_PTR(-EIO);
  736. }
  737. const struct export_operations ovl_export_operations = {
  738. .encode_fh = ovl_encode_fh,
  739. .fh_to_dentry = ovl_fh_to_dentry,
  740. .fh_to_parent = ovl_fh_to_parent,
  741. .get_name = ovl_get_name,
  742. .get_parent = ovl_get_parent,
  743. };
  744. /* encode_fh() encodes non-decodable file handles with nfs_export=off */
  745. const struct export_operations ovl_export_fid_operations = {
  746. .encode_fh = ovl_encode_fh,
  747. };