export.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. /*
  21. * We only need to encode origin if there is a chance that the same object was
  22. * encoded pre copy up and then we need to stay consistent with the same
  23. * encoding also after copy up. If non-pure upper is not indexed, then it was
  24. * copied up before NFS export was enabled. In that case we don't need to worry
  25. * about staying consistent with pre copy up encoding and we encode an upper
  26. * file handle. Overlay root dentry is a private case of non-indexed upper.
  27. *
  28. * The following table summarizes the different file handle encodings used for
  29. * different overlay object types:
  30. *
  31. * Object type | Encoding
  32. * --------------------------------
  33. * Pure upper | U
  34. * Non-indexed upper | U
  35. * Indexed upper | L (*)
  36. * Non-upper | L (*)
  37. *
  38. * U = upper file handle
  39. * L = lower file handle
  40. *
  41. * (*) Connecting an overlay dir from real lower dentry is not always
  42. * possible when there are redirects in lower layers. To mitigate this case,
  43. * we copy up the lower dir first and then encode an upper dir file handle.
  44. */
  45. static bool ovl_should_encode_origin(struct dentry *dentry)
  46. {
  47. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  48. if (!ovl_dentry_lower(dentry))
  49. return false;
  50. /*
  51. * Decoding a merge dir, whose origin's parent is under a redirected
  52. * lower dir is not always possible. As a simple aproximation, we do
  53. * not encode lower dir file handles when overlay has multiple lower
  54. * layers and origin is below the topmost lower layer.
  55. *
  56. * TODO: copy up only the parent that is under redirected lower.
  57. */
  58. if (d_is_dir(dentry) && ofs->upper_mnt &&
  59. OVL_E(dentry)->lowerstack[0].layer->idx > 1)
  60. return false;
  61. /* Decoding a non-indexed upper from origin is not implemented */
  62. if (ovl_dentry_upper(dentry) &&
  63. !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  64. return false;
  65. return true;
  66. }
  67. static int ovl_encode_maybe_copy_up(struct dentry *dentry)
  68. {
  69. int err;
  70. if (ovl_dentry_upper(dentry))
  71. return 0;
  72. err = ovl_want_write(dentry);
  73. if (err)
  74. return err;
  75. err = ovl_copy_up(dentry);
  76. ovl_drop_write(dentry);
  77. return err;
  78. }
  79. static int ovl_d_to_fh(struct dentry *dentry, char *buf, int buflen)
  80. {
  81. struct dentry *upper;
  82. struct dentry *origin = ovl_dentry_lower(dentry);
  83. struct ovl_fh *fh = NULL;
  84. int err;
  85. /*
  86. * If we should not encode a lower dir file handle, copy up and encode
  87. * an upper dir file handle.
  88. */
  89. if (!ovl_should_encode_origin(dentry)) {
  90. err = ovl_encode_maybe_copy_up(dentry);
  91. if (err)
  92. goto fail;
  93. origin = NULL;
  94. }
  95. upper = ovl_dentry_upper(dentry);
  96. err = -EACCES;
  97. if (!upper || origin)
  98. goto fail;
  99. /* TODO: encode non pure-upper by origin */
  100. fh = ovl_encode_fh(upper, true);
  101. err = -EOVERFLOW;
  102. if (fh->len > buflen)
  103. goto fail;
  104. memcpy(buf, (char *)fh, fh->len);
  105. err = fh->len;
  106. out:
  107. kfree(fh);
  108. return err;
  109. fail:
  110. pr_warn_ratelimited("overlayfs: failed to encode file handle (%pd2, err=%i, buflen=%d, len=%d, type=%d)\n",
  111. dentry, err, buflen, fh ? (int)fh->len : 0,
  112. fh ? fh->type : 0);
  113. goto out;
  114. }
  115. static int ovl_dentry_to_fh(struct dentry *dentry, u32 *fid, int *max_len)
  116. {
  117. int res, len = *max_len << 2;
  118. res = ovl_d_to_fh(dentry, (char *)fid, len);
  119. if (res <= 0)
  120. return FILEID_INVALID;
  121. len = res;
  122. /* Round up to dwords */
  123. *max_len = (len + 3) >> 2;
  124. return OVL_FILEID;
  125. }
  126. static int ovl_encode_inode_fh(struct inode *inode, u32 *fid, int *max_len,
  127. struct inode *parent)
  128. {
  129. struct dentry *dentry;
  130. int type;
  131. /* TODO: encode connectable file handles */
  132. if (parent)
  133. return FILEID_INVALID;
  134. dentry = d_find_any_alias(inode);
  135. if (WARN_ON(!dentry))
  136. return FILEID_INVALID;
  137. type = ovl_dentry_to_fh(dentry, fid, max_len);
  138. dput(dentry);
  139. return type;
  140. }
  141. /*
  142. * Find or instantiate an overlay dentry from real dentries.
  143. */
  144. static struct dentry *ovl_obtain_alias(struct super_block *sb,
  145. struct dentry *upper,
  146. struct ovl_path *lowerpath)
  147. {
  148. struct inode *inode;
  149. struct dentry *dentry;
  150. struct ovl_entry *oe;
  151. void *fsdata = &oe;
  152. /* TODO: obtain non pure-upper */
  153. if (lowerpath)
  154. return ERR_PTR(-EIO);
  155. inode = ovl_get_inode(sb, dget(upper), NULL, NULL, 0);
  156. if (IS_ERR(inode)) {
  157. dput(upper);
  158. return ERR_CAST(inode);
  159. }
  160. dentry = d_find_any_alias(inode);
  161. if (!dentry) {
  162. dentry = d_alloc_anon(inode->i_sb);
  163. if (!dentry)
  164. goto nomem;
  165. oe = ovl_alloc_entry(0);
  166. if (!oe)
  167. goto nomem;
  168. dentry->d_fsdata = oe;
  169. ovl_dentry_set_upper_alias(dentry);
  170. }
  171. return d_instantiate_anon(dentry, inode);
  172. nomem:
  173. iput(inode);
  174. dput(dentry);
  175. return ERR_PTR(-ENOMEM);
  176. }
  177. /*
  178. * Lookup a child overlay dentry to get a connected overlay dentry whose real
  179. * dentry is @real. If @real is on upper layer, we lookup a child overlay
  180. * dentry with the same name as the real dentry. Otherwise, we need to consult
  181. * index for lookup.
  182. */
  183. static struct dentry *ovl_lookup_real_one(struct dentry *connected,
  184. struct dentry *real,
  185. struct ovl_layer *layer)
  186. {
  187. struct inode *dir = d_inode(connected);
  188. struct dentry *this, *parent = NULL;
  189. struct name_snapshot name;
  190. int err;
  191. /* TODO: lookup by lower real dentry */
  192. if (layer->idx)
  193. return ERR_PTR(-EACCES);
  194. /*
  195. * Lookup child overlay dentry by real name. The dir mutex protects us
  196. * from racing with overlay rename. If the overlay dentry that is above
  197. * real has already been moved to a parent that is not under the
  198. * connected overlay dir, we return -ECHILD and restart the lookup of
  199. * connected real path from the top.
  200. */
  201. inode_lock_nested(dir, I_MUTEX_PARENT);
  202. err = -ECHILD;
  203. parent = dget_parent(real);
  204. if (ovl_dentry_upper(connected) != parent)
  205. goto fail;
  206. /*
  207. * We also need to take a snapshot of real dentry name to protect us
  208. * from racing with underlying layer rename. In this case, we don't
  209. * care about returning ESTALE, only from dereferencing a free name
  210. * pointer because we hold no lock on the real dentry.
  211. */
  212. take_dentry_name_snapshot(&name, real);
  213. this = lookup_one_len(name.name, connected, strlen(name.name));
  214. err = PTR_ERR(this);
  215. if (IS_ERR(this)) {
  216. goto fail;
  217. } else if (!this || !this->d_inode) {
  218. dput(this);
  219. err = -ENOENT;
  220. goto fail;
  221. } else if (ovl_dentry_upper(this) != real) {
  222. dput(this);
  223. err = -ESTALE;
  224. goto fail;
  225. }
  226. out:
  227. release_dentry_name_snapshot(&name);
  228. dput(parent);
  229. inode_unlock(dir);
  230. return this;
  231. fail:
  232. pr_warn_ratelimited("overlayfs: failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  233. real, layer->idx, connected, err);
  234. this = ERR_PTR(err);
  235. goto out;
  236. }
  237. /*
  238. * Lookup a connected overlay dentry whose real dentry is @real.
  239. * If @real is on upper layer, we lookup a child overlay dentry with the same
  240. * path the real dentry. Otherwise, we need to consult index for lookup.
  241. */
  242. static struct dentry *ovl_lookup_real(struct super_block *sb,
  243. struct dentry *real,
  244. struct ovl_layer *layer)
  245. {
  246. struct dentry *connected;
  247. int err = 0;
  248. /* TODO: use index when looking up by lower real dentry */
  249. if (layer->idx)
  250. return ERR_PTR(-EACCES);
  251. connected = dget(sb->s_root);
  252. while (!err) {
  253. struct dentry *next, *this;
  254. struct dentry *parent = NULL;
  255. struct dentry *real_connected = ovl_dentry_upper(connected);
  256. if (real_connected == real)
  257. break;
  258. /* Find the topmost dentry not yet connected */
  259. next = dget(real);
  260. for (;;) {
  261. parent = dget_parent(next);
  262. if (parent == real_connected)
  263. break;
  264. /*
  265. * If real has been moved out of 'real_connected',
  266. * we will not find 'real_connected' and hit the layer
  267. * root. In that case, we need to restart connecting.
  268. * This game can go on forever in the worst case. We
  269. * may want to consider taking s_vfs_rename_mutex if
  270. * this happens more than once.
  271. */
  272. if (parent == layer->mnt->mnt_root) {
  273. dput(connected);
  274. connected = dget(sb->s_root);
  275. break;
  276. }
  277. /*
  278. * If real file has been moved out of the layer root
  279. * directory, we will eventully hit the real fs root.
  280. * This cannot happen by legit overlay rename, so we
  281. * return error in that case.
  282. */
  283. if (parent == next) {
  284. err = -EXDEV;
  285. break;
  286. }
  287. dput(next);
  288. next = parent;
  289. }
  290. if (!err) {
  291. this = ovl_lookup_real_one(connected, next, layer);
  292. if (IS_ERR(this))
  293. err = PTR_ERR(this);
  294. /*
  295. * Lookup of child in overlay can fail when racing with
  296. * overlay rename of child away from 'connected' parent.
  297. * In this case, we need to restart the lookup from the
  298. * top, because we cannot trust that 'real_connected' is
  299. * still an ancestor of 'real'.
  300. */
  301. if (err == -ECHILD) {
  302. this = dget(sb->s_root);
  303. err = 0;
  304. }
  305. if (!err) {
  306. dput(connected);
  307. connected = this;
  308. }
  309. }
  310. dput(parent);
  311. dput(next);
  312. }
  313. if (err)
  314. goto fail;
  315. return connected;
  316. fail:
  317. pr_warn_ratelimited("overlayfs: failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
  318. real, layer->idx, connected, err);
  319. dput(connected);
  320. return ERR_PTR(err);
  321. }
  322. /*
  323. * Get an overlay dentry from upper/lower real dentries.
  324. */
  325. static struct dentry *ovl_get_dentry(struct super_block *sb,
  326. struct dentry *upper,
  327. struct ovl_path *lowerpath)
  328. {
  329. struct ovl_fs *ofs = sb->s_fs_info;
  330. struct ovl_layer upper_layer = { .mnt = ofs->upper_mnt };
  331. /* TODO: get non-upper dentry */
  332. if (!upper)
  333. return ERR_PTR(-EACCES);
  334. /*
  335. * Obtain a disconnected overlay dentry from a non-dir real upper
  336. * dentry.
  337. */
  338. if (!d_is_dir(upper))
  339. return ovl_obtain_alias(sb, upper, NULL);
  340. /* Removed empty directory? */
  341. if ((upper->d_flags & DCACHE_DISCONNECTED) || d_unhashed(upper))
  342. return ERR_PTR(-ENOENT);
  343. /*
  344. * If real upper dentry is connected and hashed, get a connected
  345. * overlay dentry with the same path as the real upper dentry.
  346. */
  347. return ovl_lookup_real(sb, upper, &upper_layer);
  348. }
  349. static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
  350. struct ovl_fh *fh)
  351. {
  352. struct ovl_fs *ofs = sb->s_fs_info;
  353. struct dentry *dentry;
  354. struct dentry *upper;
  355. if (!ofs->upper_mnt)
  356. return ERR_PTR(-EACCES);
  357. upper = ovl_decode_fh(fh, ofs->upper_mnt);
  358. if (IS_ERR_OR_NULL(upper))
  359. return upper;
  360. dentry = ovl_get_dentry(sb, upper, NULL);
  361. dput(upper);
  362. return dentry;
  363. }
  364. static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
  365. int fh_len, int fh_type)
  366. {
  367. struct dentry *dentry = NULL;
  368. struct ovl_fh *fh = (struct ovl_fh *) fid;
  369. int len = fh_len << 2;
  370. unsigned int flags = 0;
  371. int err;
  372. err = -EINVAL;
  373. if (fh_type != OVL_FILEID)
  374. goto out_err;
  375. err = ovl_check_fh_len(fh, len);
  376. if (err)
  377. goto out_err;
  378. /* TODO: decode non-upper */
  379. flags = fh->flags;
  380. if (flags & OVL_FH_FLAG_PATH_UPPER)
  381. dentry = ovl_upper_fh_to_d(sb, fh);
  382. err = PTR_ERR(dentry);
  383. if (IS_ERR(dentry) && err != -ESTALE)
  384. goto out_err;
  385. return dentry;
  386. out_err:
  387. pr_warn_ratelimited("overlayfs: failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
  388. len, fh_type, flags, err);
  389. return ERR_PTR(err);
  390. }
  391. static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
  392. int fh_len, int fh_type)
  393. {
  394. pr_warn_ratelimited("overlayfs: connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
  395. return ERR_PTR(-EACCES);
  396. }
  397. static int ovl_get_name(struct dentry *parent, char *name,
  398. struct dentry *child)
  399. {
  400. /*
  401. * ovl_fh_to_dentry() returns connected dir overlay dentries and
  402. * ovl_fh_to_parent() is not implemented, so we should not get here.
  403. */
  404. WARN_ON_ONCE(1);
  405. return -EIO;
  406. }
  407. static struct dentry *ovl_get_parent(struct dentry *dentry)
  408. {
  409. /*
  410. * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
  411. * should not get here.
  412. */
  413. WARN_ON_ONCE(1);
  414. return ERR_PTR(-EIO);
  415. }
  416. const struct export_operations ovl_export_operations = {
  417. .encode_fh = ovl_encode_inode_fh,
  418. .fh_to_dentry = ovl_fh_to_dentry,
  419. .fh_to_parent = ovl_fh_to_parent,
  420. .get_name = ovl_get_name,
  421. .get_parent = ovl_get_parent,
  422. };