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