export.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. dentry = d_find_any_alias(inode);
  275. if (!dentry) {
  276. dentry = d_alloc_anon(inode->i_sb);
  277. if (!dentry)
  278. goto nomem;
  279. oe = ovl_alloc_entry(lower ? 1 : 0);
  280. if (!oe)
  281. goto nomem;
  282. if (lower) {
  283. oe->lowerstack->dentry = dget(lower);
  284. oe->lowerstack->layer = lowerpath->layer;
  285. }
  286. dentry->d_fsdata = oe;
  287. if (upper_alias)
  288. ovl_dentry_set_upper_alias(dentry);
  289. }
  290. return d_instantiate_anon(dentry, inode);
  291. nomem:
  292. iput(inode);
  293. dput(dentry);
  294. return ERR_PTR(-ENOMEM);
  295. }
  296. /* Get the upper or lower dentry in stach whose on layer @idx */
  297. static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
  298. {
  299. struct ovl_entry *oe = dentry->d_fsdata;
  300. int i;
  301. if (!idx)
  302. return ovl_dentry_upper(dentry);
  303. for (i = 0; i < oe->numlower; i++) {
  304. if (oe->lowerstack[i].layer->idx == idx)
  305. return oe->lowerstack[i].dentry;
  306. }
  307. return NULL;
  308. }
  309. /*
  310. * Lookup a child overlay dentry to get a connected overlay dentry whose real
  311. * dentry is @real. If @real is on upper layer, we lookup a child overlay
  312. * dentry with the same name as the real dentry. Otherwise, we need to consult
  313. * index for lookup.
  314. */
  315. static struct dentry *ovl_lookup_real_one(struct dentry *connected,
  316. struct dentry *real,
  317. struct ovl_layer *layer)
  318. {
  319. struct inode *dir = d_inode(connected);
  320. struct dentry *this, *parent = NULL;
  321. struct name_snapshot name;
  322. int err;
  323. /*
  324. * Lookup child overlay dentry by real name. The dir mutex protects us
  325. * from racing with overlay rename. If the overlay dentry that is above
  326. * real has already been moved to a parent that is not under the
  327. * connected overlay dir, we return -ECHILD and restart the lookup of
  328. * connected real path from the top.
  329. */
  330. inode_lock_nested(dir, I_MUTEX_PARENT);
  331. err = -ECHILD;
  332. parent = dget_parent(real);
  333. if (ovl_dentry_real_at(connected, layer->idx) != parent)
  334. goto fail;
  335. /*
  336. * We also need to take a snapshot of real dentry name to protect us
  337. * from racing with underlying layer rename. In this case, we don't
  338. * care about returning ESTALE, only from dereferencing a free name
  339. * pointer because we hold no lock on the real dentry.
  340. */
  341. take_dentry_name_snapshot(&name, real);
  342. this = lookup_one_len(name.name, connected, strlen(name.name));
  343. err = PTR_ERR(this);
  344. if (IS_ERR(this)) {
  345. goto fail;
  346. } else if (!this || !this->d_inode) {
  347. dput(this);
  348. err = -ENOENT;
  349. goto fail;
  350. } else if (ovl_dentry_real_at(this, layer->idx) != real) {
  351. dput(this);
  352. err = -ESTALE;
  353. goto fail;
  354. }
  355. out:
  356. release_dentry_name_snapshot(&name);
  357. dput(parent);
  358. inode_unlock(dir);
  359. return this;
  360. fail:
  361. pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  362. real, layer->idx, connected, err);
  363. this = ERR_PTR(err);
  364. goto out;
  365. }
  366. static struct dentry *ovl_lookup_real(struct super_block *sb,
  367. struct dentry *real,
  368. struct ovl_layer *layer);
  369. /*
  370. * Lookup an indexed or hashed overlay dentry by real inode.
  371. */
  372. static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
  373. struct dentry *real,
  374. struct ovl_layer *layer)
  375. {
  376. struct ovl_fs *ofs = sb->s_fs_info;
  377. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  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, &upper_layer);
  415. dput(upper);
  416. }
  417. if (IS_ERR_OR_NULL(this))
  418. return this;
  419. if (WARN_ON(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. 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. 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("overlayfs: 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. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  567. struct ovl_layer *layer = upper ? &upper_layer : lowerpath->layer;
  568. struct dentry *real = upper ?: (index ?: lowerpath->dentry);
  569. /*
  570. * Obtain a disconnected overlay dentry from a non-dir real dentry
  571. * and index.
  572. */
  573. if (!d_is_dir(real))
  574. return ovl_obtain_alias(sb, upper, lowerpath, index);
  575. /* Removed empty directory? */
  576. if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
  577. return ERR_PTR(-ENOENT);
  578. /*
  579. * If real dentry is connected and hashed, get a connected overlay
  580. * dentry whose real dentry is @real.
  581. */
  582. return ovl_lookup_real(sb, real, layer);
  583. }
  584. static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
  585. struct ovl_fh *fh)
  586. {
  587. struct ovl_fs *ofs = sb->s_fs_info;
  588. struct dentry *dentry;
  589. struct dentry *upper;
  590. if (!ofs->upper_mnt)
  591. return ERR_PTR(-EACCES);
  592. upper = ovl_decode_real_fh(fh, ofs->upper_mnt, true);
  593. if (IS_ERR_OR_NULL(upper))
  594. return upper;
  595. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  596. dput(upper);
  597. return dentry;
  598. }
  599. static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
  600. struct ovl_fh *fh)
  601. {
  602. struct ovl_fs *ofs = sb->s_fs_info;
  603. struct ovl_path origin = { };
  604. struct ovl_path *stack = &origin;
  605. struct dentry *dentry = NULL;
  606. struct dentry *index = NULL;
  607. struct inode *inode;
  608. int err;
  609. /* First lookup overlay inode in inode cache by origin fh */
  610. err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
  611. if (err)
  612. return ERR_PTR(err);
  613. if (!d_is_dir(origin.dentry) ||
  614. !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
  615. inode = ovl_lookup_inode(sb, origin.dentry, false);
  616. err = PTR_ERR(inode);
  617. if (IS_ERR(inode))
  618. goto out_err;
  619. if (inode) {
  620. dentry = d_find_any_alias(inode);
  621. iput(inode);
  622. if (dentry)
  623. goto out;
  624. }
  625. }
  626. /* Then lookup indexed upper/whiteout by origin fh */
  627. if (ofs->indexdir) {
  628. index = ovl_get_index_fh(ofs, fh);
  629. err = PTR_ERR(index);
  630. if (IS_ERR(index)) {
  631. index = NULL;
  632. goto out_err;
  633. }
  634. }
  635. /* Then try to get a connected upper dir by index */
  636. if (index && d_is_dir(index)) {
  637. struct dentry *upper = ovl_index_upper(ofs, index);
  638. err = PTR_ERR(upper);
  639. if (IS_ERR_OR_NULL(upper))
  640. goto out_err;
  641. dentry = ovl_get_dentry(sb, upper, NULL, NULL);
  642. dput(upper);
  643. goto out;
  644. }
  645. /* Otherwise, get a connected non-upper dir or disconnected non-dir */
  646. if (d_is_dir(origin.dentry) &&
  647. (origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
  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. dentry = ovl_get_dentry(sb, NULL, &origin, index);
  660. out:
  661. dput(origin.dentry);
  662. dput(index);
  663. return dentry;
  664. out_err:
  665. dentry = ERR_PTR(err);
  666. goto out;
  667. }
  668. static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
  669. int fh_len, int fh_type)
  670. {
  671. struct dentry *dentry = NULL;
  672. struct ovl_fh *fh = (struct ovl_fh *) fid;
  673. int len = fh_len << 2;
  674. unsigned int flags = 0;
  675. int err;
  676. err = -EINVAL;
  677. if (fh_type != OVL_FILEID)
  678. goto out_err;
  679. err = ovl_check_fh_len(fh, len);
  680. if (err)
  681. goto out_err;
  682. flags = fh->flags;
  683. dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
  684. ovl_upper_fh_to_d(sb, fh) :
  685. ovl_lower_fh_to_d(sb, fh);
  686. err = PTR_ERR(dentry);
  687. if (IS_ERR(dentry) && err != -ESTALE)
  688. goto out_err;
  689. return dentry;
  690. out_err:
  691. pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
  692. len, fh_type, flags, err);
  693. return ERR_PTR(err);
  694. }
  695. static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
  696. int fh_len, int fh_type)
  697. {
  698. pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
  699. return ERR_PTR(-EACCES);
  700. }
  701. static int ovl_get_name(struct dentry *parent, char *name,
  702. struct dentry *child)
  703. {
  704. /*
  705. * ovl_fh_to_dentry() returns connected dir overlay dentries and
  706. * ovl_fh_to_parent() is not implemented, so we should not get here.
  707. */
  708. WARN_ON_ONCE(1);
  709. return -EIO;
  710. }
  711. static struct dentry *ovl_get_parent(struct dentry *dentry)
  712. {
  713. /*
  714. * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
  715. * should not get here.
  716. */
  717. WARN_ON_ONCE(1);
  718. return ERR_PTR(-EIO);
  719. }
  720. const struct export_operations ovl_export_operations = {
  721. .encode_fh = ovl_encode_fh,
  722. .fh_to_dentry = ovl_fh_to_dentry,
  723. .fh_to_parent = ovl_fh_to_parent,
  724. .get_name = ovl_get_name,
  725. .get_parent = ovl_get_parent,
  726. };