export.c 23 KB

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