export.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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_real_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_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. struct ovl_inode_params oip = {
  261. .lowerpath = lowerpath,
  262. .index = index,
  263. .numlower = !!lower
  264. };
  265. /* We get overlay directory dentries with ovl_lookup_real() */
  266. if (d_is_dir(upper ?: lower))
  267. return ERR_PTR(-EIO);
  268. oip.upperdentry = dget(upper);
  269. inode = ovl_get_inode(sb, &oip);
  270. if (IS_ERR(inode)) {
  271. dput(upper);
  272. return ERR_CAST(inode);
  273. }
  274. if (upper)
  275. ovl_set_flag(OVL_UPPERDATA, inode);
  276. dentry = d_find_any_alias(inode);
  277. if (!dentry) {
  278. dentry = d_alloc_anon(inode->i_sb);
  279. if (!dentry)
  280. goto nomem;
  281. oe = ovl_alloc_entry(lower ? 1 : 0);
  282. if (!oe)
  283. goto nomem;
  284. if (lower) {
  285. oe->lowerstack->dentry = dget(lower);
  286. oe->lowerstack->layer = lowerpath->layer;
  287. }
  288. dentry->d_fsdata = oe;
  289. if (upper_alias)
  290. ovl_dentry_set_upper_alias(dentry);
  291. }
  292. return d_instantiate_anon(dentry, inode);
  293. nomem:
  294. iput(inode);
  295. dput(dentry);
  296. return ERR_PTR(-ENOMEM);
  297. }
  298. /* Get the upper or lower dentry in stach whose on layer @idx */
  299. static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
  300. {
  301. struct ovl_entry *oe = dentry->d_fsdata;
  302. int i;
  303. if (!idx)
  304. return ovl_dentry_upper(dentry);
  305. for (i = 0; i < oe->numlower; i++) {
  306. if (oe->lowerstack[i].layer->idx == idx)
  307. return oe->lowerstack[i].dentry;
  308. }
  309. return NULL;
  310. }
  311. /*
  312. * Lookup a child overlay dentry to get a connected overlay dentry whose real
  313. * dentry is @real. If @real is on upper layer, we lookup a child overlay
  314. * dentry with the same name as the real dentry. Otherwise, we need to consult
  315. * index for lookup.
  316. */
  317. static struct dentry *ovl_lookup_real_one(struct dentry *connected,
  318. struct dentry *real,
  319. struct ovl_layer *layer)
  320. {
  321. struct inode *dir = d_inode(connected);
  322. struct dentry *this, *parent = NULL;
  323. struct name_snapshot name;
  324. int err;
  325. /*
  326. * Lookup child overlay dentry by real name. The dir mutex protects us
  327. * from racing with overlay rename. If the overlay dentry that is above
  328. * real has already been moved to a parent that is not under the
  329. * connected overlay dir, we return -ECHILD and restart the lookup of
  330. * connected real path from the top.
  331. */
  332. inode_lock_nested(dir, I_MUTEX_PARENT);
  333. err = -ECHILD;
  334. parent = dget_parent(real);
  335. if (ovl_dentry_real_at(connected, layer->idx) != parent)
  336. goto fail;
  337. /*
  338. * We also need to take a snapshot of real dentry name to protect us
  339. * from racing with underlying layer rename. In this case, we don't
  340. * care about returning ESTALE, only from dereferencing a free name
  341. * pointer because we hold no lock on the real dentry.
  342. */
  343. take_dentry_name_snapshot(&name, real);
  344. this = lookup_one_len(name.name, connected, strlen(name.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. release_dentry_name_snapshot(&name);
  359. dput(parent);
  360. inode_unlock(dir);
  361. return this;
  362. fail:
  363. pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  364. real, layer->idx, connected, err);
  365. this = ERR_PTR(err);
  366. goto out;
  367. }
  368. static struct dentry *ovl_lookup_real(struct super_block *sb,
  369. struct dentry *real,
  370. struct ovl_layer *layer);
  371. /*
  372. * Lookup an indexed or hashed overlay dentry by real inode.
  373. */
  374. static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
  375. struct dentry *real,
  376. struct ovl_layer *layer)
  377. {
  378. struct ovl_fs *ofs = sb->s_fs_info;
  379. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  380. struct dentry *index = NULL;
  381. struct dentry *this = NULL;
  382. struct inode *inode;
  383. /*
  384. * Decoding upper dir from index is expensive, so first try to lookup
  385. * overlay dentry in inode/dcache.
  386. */
  387. inode = ovl_lookup_inode(sb, real, !layer->idx);
  388. if (IS_ERR(inode))
  389. return ERR_CAST(inode);
  390. if (inode) {
  391. this = d_find_any_alias(inode);
  392. iput(inode);
  393. }
  394. /*
  395. * For decoded lower dir file handle, lookup index by origin to check
  396. * if lower dir was copied up and and/or removed.
  397. */
  398. if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
  399. index = ovl_lookup_index(ofs, NULL, real, false);
  400. if (IS_ERR(index))
  401. return index;
  402. }
  403. /* Get connected upper overlay dir from index */
  404. if (index) {
  405. struct dentry *upper = ovl_index_upper(ofs, index);
  406. dput(index);
  407. if (IS_ERR_OR_NULL(upper))
  408. return upper;
  409. /*
  410. * ovl_lookup_real() in lower layer may call recursively once to
  411. * ovl_lookup_real() in upper layer. The first level call walks
  412. * back lower parents to the topmost indexed parent. The second
  413. * recursive call walks back from indexed upper to the topmost
  414. * connected/hashed upper parent (or up to root).
  415. */
  416. this = ovl_lookup_real(sb, upper, &upper_layer);
  417. dput(upper);
  418. }
  419. if (IS_ERR_OR_NULL(this))
  420. return this;
  421. if (WARN_ON(ovl_dentry_real_at(this, layer->idx) != real)) {
  422. dput(this);
  423. this = ERR_PTR(-EIO);
  424. }
  425. return this;
  426. }
  427. /*
  428. * Lookup an indexed or hashed overlay dentry, whose real dentry is an
  429. * ancestor of @real.
  430. */
  431. static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
  432. struct dentry *real,
  433. struct ovl_layer *layer)
  434. {
  435. struct dentry *next, *parent = NULL;
  436. struct dentry *ancestor = ERR_PTR(-EIO);
  437. if (real == layer->mnt->mnt_root)
  438. return dget(sb->s_root);
  439. /* Find the topmost indexed or hashed ancestor */
  440. next = dget(real);
  441. for (;;) {
  442. parent = dget_parent(next);
  443. /*
  444. * Lookup a matching overlay dentry in inode/dentry
  445. * cache or in index by real inode.
  446. */
  447. ancestor = ovl_lookup_real_inode(sb, next, layer);
  448. if (ancestor)
  449. break;
  450. if (parent == layer->mnt->mnt_root) {
  451. ancestor = dget(sb->s_root);
  452. break;
  453. }
  454. /*
  455. * If @real has been moved out of the layer root directory,
  456. * we will eventully hit the real fs root. This cannot happen
  457. * by legit overlay rename, so we return error in that case.
  458. */
  459. if (parent == next) {
  460. ancestor = ERR_PTR(-EXDEV);
  461. break;
  462. }
  463. dput(next);
  464. next = parent;
  465. }
  466. dput(parent);
  467. dput(next);
  468. return ancestor;
  469. }
  470. /*
  471. * Lookup a connected overlay dentry whose real dentry is @real.
  472. * If @real is on upper layer, we lookup a child overlay dentry with the same
  473. * path the real dentry. Otherwise, we need to consult index for lookup.
  474. */
  475. static struct dentry *ovl_lookup_real(struct super_block *sb,
  476. struct dentry *real,
  477. struct ovl_layer *layer)
  478. {
  479. struct dentry *connected;
  480. int err = 0;
  481. connected = ovl_lookup_real_ancestor(sb, real, layer);
  482. if (IS_ERR(connected))
  483. return connected;
  484. while (!err) {
  485. struct dentry *next, *this;
  486. struct dentry *parent = NULL;
  487. struct dentry *real_connected = ovl_dentry_real_at(connected,
  488. layer->idx);
  489. if (real_connected == real)
  490. break;
  491. /* Find the topmost dentry not yet connected */
  492. next = dget(real);
  493. for (;;) {
  494. parent = dget_parent(next);
  495. if (parent == real_connected)
  496. break;
  497. /*
  498. * If real has been moved out of 'real_connected',
  499. * we will not find 'real_connected' and hit the layer
  500. * root. In that case, we need to restart connecting.
  501. * This game can go on forever in the worst case. We
  502. * may want to consider taking s_vfs_rename_mutex if
  503. * this happens more than once.
  504. */
  505. if (parent == layer->mnt->mnt_root) {
  506. dput(connected);
  507. connected = dget(sb->s_root);
  508. break;
  509. }
  510. /*
  511. * If real file has been moved out of the layer root
  512. * directory, we will eventully hit the real fs root.
  513. * This cannot happen by legit overlay rename, so we
  514. * return error in that case.
  515. */
  516. if (parent == next) {
  517. err = -EXDEV;
  518. break;
  519. }
  520. dput(next);
  521. next = parent;
  522. }
  523. if (!err) {
  524. this = ovl_lookup_real_one(connected, next, layer);
  525. if (IS_ERR(this))
  526. err = PTR_ERR(this);
  527. /*
  528. * Lookup of child in overlay can fail when racing with
  529. * overlay rename of child away from 'connected' parent.
  530. * In this case, we need to restart the lookup from the
  531. * top, because we cannot trust that 'real_connected' is
  532. * still an ancestor of 'real'. There is a good chance
  533. * that the renamed overlay ancestor is now in cache, so
  534. * ovl_lookup_real_ancestor() will find it and we can
  535. * continue to connect exactly from where lookup failed.
  536. */
  537. if (err == -ECHILD) {
  538. this = ovl_lookup_real_ancestor(sb, real,
  539. layer);
  540. err = PTR_ERR_OR_ZERO(this);
  541. }
  542. if (!err) {
  543. dput(connected);
  544. connected = this;
  545. }
  546. }
  547. dput(parent);
  548. dput(next);
  549. }
  550. if (err)
  551. goto fail;
  552. return connected;
  553. fail:
  554. pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  555. real, layer->idx, connected, err);
  556. dput(connected);
  557. return ERR_PTR(err);
  558. }
  559. /*
  560. * Get an overlay dentry from upper/lower real dentries and index.
  561. */
  562. static struct dentry *ovl_get_dentry(struct super_block *sb,
  563. struct dentry *upper,
  564. struct ovl_path *lowerpath,
  565. struct dentry *index)
  566. {
  567. struct ovl_fs *ofs = sb->s_fs_info;
  568. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  569. struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
  570. struct dentry *real = upper ?: (index ?: lowerpath->dentry);
  571. /*
  572. * Obtain a disconnected overlay dentry from a non-dir real dentry
  573. * and index.
  574. */
  575. if (!d_is_dir(real))
  576. return ovl_obtain_alias(sb, upper, lowerpath, index);
  577. /* Removed empty directory? */
  578. if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
  579. return ERR_PTR(-ENOENT);
  580. /*
  581. * If real dentry is connected and hashed, get a connected overlay
  582. * dentry whose real dentry is @real.
  583. */
  584. return ovl_lookup_real(sb, real, layer);
  585. }
  586. static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
  587. struct ovl_fh *fh)
  588. {
  589. struct ovl_fs *ofs = sb->s_fs_info;
  590. struct dentry *dentry;
  591. struct dentry *upper;
  592. if (!ofs->upper_mnt)
  593. return ERR_PTR(-EACCES);
  594. upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
  595. if (IS_ERR_OR_NULL(upper))
  596. return upper;
  597. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  598. dput(upper);
  599. return dentry;
  600. }
  601. static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
  602. struct ovl_fh *fh)
  603. {
  604. struct ovl_fs *ofs = sb->s_fs_info;
  605. struct ovl_path origin = { };
  606. struct ovl_path *stack = &origin;
  607. struct dentry *dentry = NULL;
  608. struct dentry *index = NULL;
  609. struct inode *inode;
  610. int err;
  611. /* First lookup overlay inode in inode cache by origin fh */
  612. err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
  613. if (err)
  614. return ERR_PTR(err);
  615. if (!d_is_dir(origin.dentry) ||
  616. !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
  617. inode = ovl_lookup_inode(sb, origin.dentry, false);
  618. err = PTR_ERR(inode);
  619. if (IS_ERR(inode))
  620. goto out_err;
  621. if (inode) {
  622. dentry = d_find_any_alias(inode);
  623. iput(inode);
  624. if (dentry)
  625. goto out;
  626. }
  627. }
  628. /* Then lookup indexed upper/whiteout by origin fh */
  629. if (ofs->indexdir) {
  630. index = ovl_get_index_fh(ofs, fh);
  631. err = PTR_ERR(index);
  632. if (IS_ERR(index)) {
  633. index = NULL;
  634. goto out_err;
  635. }
  636. }
  637. /* Then try to get a connected upper dir by index */
  638. if (index && d_is_dir(index)) {
  639. struct dentry *upper = ovl_index_upper(ofs, index);
  640. err = PTR_ERR(upper);
  641. if (IS_ERR_OR_NULL(upper))
  642. goto out_err;
  643. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  644. dput(upper);
  645. goto out;
  646. }
  647. /* Find origin.dentry again with ovl_acceptable() layer check */
  648. if (d_is_dir(origin.dentry)) {
  649. dput(origin.dentry);
  650. origin.dentry = NULL;
  651. err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
  652. if (err)
  653. goto out_err;
  654. }
  655. if (index) {
  656. err = ovl_verify_origin(index, origin.dentry, false);
  657. if (err)
  658. goto out_err;
  659. }
  660. /* Get a connected non-upper dir or disconnected non-dir */
  661. dentry = ovl_get_dentry(sb, NULL, &origin, index);
  662. out:
  663. dput(origin.dentry);
  664. dput(index);
  665. return dentry;
  666. out_err:
  667. dentry = ERR_PTR(err);
  668. goto out;
  669. }
  670. static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
  671. int fh_len, int fh_type)
  672. {
  673. struct dentry *dentry = NULL;
  674. struct ovl_fh *fh = (struct ovl_fh *) fid;
  675. int len = fh_len << 2;
  676. unsigned int flags = 0;
  677. int err;
  678. err = -EINVAL;
  679. if (fh_type != OVL_FILEID)
  680. goto out_err;
  681. err = ovl_check_fh_len(fh, len);
  682. if (err)
  683. goto out_err;
  684. flags = fh->flags;
  685. dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
  686. ovl_upper_fh_to_d(sb, fh) :
  687. ovl_lower_fh_to_d(sb, fh);
  688. err = PTR_ERR(dentry);
  689. if (IS_ERR(dentry) && err != -ESTALE)
  690. goto out_err;
  691. return dentry;
  692. out_err:
  693. pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
  694. len, fh_type, flags, err);
  695. return ERR_PTR(err);
  696. }
  697. static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
  698. int fh_len, int fh_type)
  699. {
  700. pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
  701. return ERR_PTR(-EACCES);
  702. }
  703. static int ovl_get_name(struct dentry *parent, char *name,
  704. struct dentry *child)
  705. {
  706. /*
  707. * ovl_fh_to_dentry() returns connected dir overlay dentries and
  708. * ovl_fh_to_parent() is not implemented, so we should not get here.
  709. */
  710. WARN_ON_ONCE(1);
  711. return -EIO;
  712. }
  713. static struct dentry *ovl_get_parent(struct dentry *dentry)
  714. {
  715. /*
  716. * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
  717. * should not get here.
  718. */
  719. WARN_ON_ONCE(1);
  720. return ERR_PTR(-EIO);
  721. }
  722. const struct export_operations ovl_export_operations = {
  723. .encode_fh = ovl_encode_fh,
  724. .fh_to_dentry = ovl_fh_to_dentry,
  725. .fh_to_parent = ovl_fh_to_parent,
  726. .get_name = ovl_get_name,
  727. .get_parent = ovl_get_parent,
  728. };