util.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Copyright (C) 2011 Novell Inc.
  3. * Copyright (C) 2016 Red Hat, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. */
  9. #include <linux/fs.h>
  10. #include <linux/mount.h>
  11. #include <linux/slab.h>
  12. #include <linux/cred.h>
  13. #include <linux/xattr.h>
  14. #include <linux/exportfs.h>
  15. #include <linux/uuid.h>
  16. #include <linux/namei.h>
  17. #include <linux/ratelimit.h>
  18. #include "overlayfs.h"
  19. int ovl_want_write(struct dentry *dentry)
  20. {
  21. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  22. return mnt_want_write(ofs->upper_mnt);
  23. }
  24. void ovl_drop_write(struct dentry *dentry)
  25. {
  26. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  27. mnt_drop_write(ofs->upper_mnt);
  28. }
  29. struct dentry *ovl_workdir(struct dentry *dentry)
  30. {
  31. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  32. return ofs->workdir;
  33. }
  34. const struct cred *ovl_override_creds(struct super_block *sb)
  35. {
  36. struct ovl_fs *ofs = sb->s_fs_info;
  37. return override_creds(ofs->creator_cred);
  38. }
  39. struct super_block *ovl_same_sb(struct super_block *sb)
  40. {
  41. struct ovl_fs *ofs = sb->s_fs_info;
  42. if (!ofs->numlowerfs)
  43. return ofs->upper_mnt->mnt_sb;
  44. else if (ofs->numlowerfs == 1 && !ofs->upper_mnt)
  45. return ofs->lower_fs[0].sb;
  46. else
  47. return NULL;
  48. }
  49. /*
  50. * Check if underlying fs supports file handles and try to determine encoding
  51. * type, in order to deduce maximum inode number used by fs.
  52. *
  53. * Return 0 if file handles are not supported.
  54. * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
  55. * Return -1 if fs uses a non default encoding with unknown inode size.
  56. */
  57. int ovl_can_decode_fh(struct super_block *sb)
  58. {
  59. if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry ||
  60. uuid_is_null(&sb->s_uuid))
  61. return 0;
  62. return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
  63. }
  64. struct dentry *ovl_indexdir(struct super_block *sb)
  65. {
  66. struct ovl_fs *ofs = sb->s_fs_info;
  67. return ofs->indexdir;
  68. }
  69. /* Index all files on copy up. For now only enabled for NFS export */
  70. bool ovl_index_all(struct super_block *sb)
  71. {
  72. struct ovl_fs *ofs = sb->s_fs_info;
  73. return ofs->config.nfs_export && ofs->config.index;
  74. }
  75. /* Verify lower origin on lookup. For now only enabled for NFS export */
  76. bool ovl_verify_lower(struct super_block *sb)
  77. {
  78. struct ovl_fs *ofs = sb->s_fs_info;
  79. return ofs->config.nfs_export && ofs->config.index;
  80. }
  81. struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  82. {
  83. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  84. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  85. if (oe)
  86. oe->numlower = numlower;
  87. return oe;
  88. }
  89. bool ovl_dentry_remote(struct dentry *dentry)
  90. {
  91. return dentry->d_flags &
  92. (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  93. DCACHE_OP_REAL);
  94. }
  95. bool ovl_dentry_weird(struct dentry *dentry)
  96. {
  97. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  98. DCACHE_MANAGE_TRANSIT |
  99. DCACHE_OP_HASH |
  100. DCACHE_OP_COMPARE);
  101. }
  102. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  103. {
  104. struct ovl_entry *oe = dentry->d_fsdata;
  105. enum ovl_path_type type = 0;
  106. if (ovl_dentry_upper(dentry)) {
  107. type = __OVL_PATH_UPPER;
  108. /*
  109. * Non-dir dentry can hold lower dentry of its copy up origin.
  110. */
  111. if (oe->numlower) {
  112. type |= __OVL_PATH_ORIGIN;
  113. if (d_is_dir(dentry))
  114. type |= __OVL_PATH_MERGE;
  115. }
  116. } else {
  117. if (oe->numlower > 1)
  118. type |= __OVL_PATH_MERGE;
  119. }
  120. return type;
  121. }
  122. void ovl_path_upper(struct dentry *dentry, struct path *path)
  123. {
  124. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  125. path->mnt = ofs->upper_mnt;
  126. path->dentry = ovl_dentry_upper(dentry);
  127. }
  128. void ovl_path_lower(struct dentry *dentry, struct path *path)
  129. {
  130. struct ovl_entry *oe = dentry->d_fsdata;
  131. if (oe->numlower) {
  132. path->mnt = oe->lowerstack[0].layer->mnt;
  133. path->dentry = oe->lowerstack[0].dentry;
  134. } else {
  135. *path = (struct path) { };
  136. }
  137. }
  138. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  139. {
  140. enum ovl_path_type type = ovl_path_type(dentry);
  141. if (!OVL_TYPE_UPPER(type))
  142. ovl_path_lower(dentry, path);
  143. else
  144. ovl_path_upper(dentry, path);
  145. return type;
  146. }
  147. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  148. {
  149. return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
  150. }
  151. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  152. {
  153. struct ovl_entry *oe = dentry->d_fsdata;
  154. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  155. }
  156. struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
  157. {
  158. struct ovl_entry *oe = dentry->d_fsdata;
  159. return oe->numlower ? oe->lowerstack[0].layer : NULL;
  160. }
  161. struct dentry *ovl_dentry_real(struct dentry *dentry)
  162. {
  163. return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
  164. }
  165. struct dentry *ovl_i_dentry_upper(struct inode *inode)
  166. {
  167. return ovl_upperdentry_dereference(OVL_I(inode));
  168. }
  169. struct inode *ovl_inode_upper(struct inode *inode)
  170. {
  171. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  172. return upperdentry ? d_inode(upperdentry) : NULL;
  173. }
  174. struct inode *ovl_inode_lower(struct inode *inode)
  175. {
  176. return OVL_I(inode)->lower;
  177. }
  178. struct inode *ovl_inode_real(struct inode *inode)
  179. {
  180. return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
  181. }
  182. struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
  183. {
  184. return OVL_I(inode)->cache;
  185. }
  186. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
  187. {
  188. OVL_I(inode)->cache = cache;
  189. }
  190. void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
  191. {
  192. set_bit(flag, &OVL_E(dentry)->flags);
  193. }
  194. void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
  195. {
  196. clear_bit(flag, &OVL_E(dentry)->flags);
  197. }
  198. bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
  199. {
  200. return test_bit(flag, &OVL_E(dentry)->flags);
  201. }
  202. bool ovl_dentry_is_opaque(struct dentry *dentry)
  203. {
  204. return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
  205. }
  206. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  207. {
  208. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  209. }
  210. void ovl_dentry_set_opaque(struct dentry *dentry)
  211. {
  212. ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
  213. }
  214. /*
  215. * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
  216. * to return positive, while there's no actual upper alias for the inode.
  217. * Copy up code needs to know about the existence of the upper alias, so it
  218. * can't use ovl_dentry_upper().
  219. */
  220. bool ovl_dentry_has_upper_alias(struct dentry *dentry)
  221. {
  222. return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
  223. }
  224. void ovl_dentry_set_upper_alias(struct dentry *dentry)
  225. {
  226. ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
  227. }
  228. bool ovl_redirect_dir(struct super_block *sb)
  229. {
  230. struct ovl_fs *ofs = sb->s_fs_info;
  231. return ofs->config.redirect_dir && !ofs->noxattr;
  232. }
  233. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  234. {
  235. return OVL_I(d_inode(dentry))->redirect;
  236. }
  237. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  238. {
  239. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  240. kfree(oi->redirect);
  241. oi->redirect = redirect;
  242. }
  243. void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
  244. struct dentry *lowerdentry)
  245. {
  246. struct inode *realinode = d_inode(upperdentry ?: lowerdentry);
  247. if (upperdentry)
  248. OVL_I(inode)->__upperdentry = upperdentry;
  249. if (lowerdentry)
  250. OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
  251. ovl_copyattr(realinode, inode);
  252. if (!inode->i_ino)
  253. inode->i_ino = realinode->i_ino;
  254. }
  255. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
  256. {
  257. struct inode *upperinode = d_inode(upperdentry);
  258. WARN_ON(OVL_I(inode)->__upperdentry);
  259. /*
  260. * Make sure upperdentry is consistent before making it visible
  261. */
  262. smp_wmb();
  263. OVL_I(inode)->__upperdentry = upperdentry;
  264. if (inode_unhashed(inode)) {
  265. if (!inode->i_ino)
  266. inode->i_ino = upperinode->i_ino;
  267. inode->i_private = upperinode;
  268. __insert_inode_hash(inode, (unsigned long) upperinode);
  269. }
  270. }
  271. void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
  272. {
  273. struct inode *inode = d_inode(dentry);
  274. WARN_ON(!inode_is_locked(inode));
  275. /*
  276. * Version is used by readdir code to keep cache consistent. For merge
  277. * dirs all changes need to be noted. For non-merge dirs, cache only
  278. * contains impure (ones which have been copied up and have origins)
  279. * entries, so only need to note changes to impure entries.
  280. */
  281. if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
  282. OVL_I(inode)->version++;
  283. }
  284. u64 ovl_dentry_version_get(struct dentry *dentry)
  285. {
  286. struct inode *inode = d_inode(dentry);
  287. WARN_ON(!inode_is_locked(inode));
  288. return OVL_I(inode)->version;
  289. }
  290. bool ovl_is_whiteout(struct dentry *dentry)
  291. {
  292. struct inode *inode = dentry->d_inode;
  293. return inode && IS_WHITEOUT(inode);
  294. }
  295. struct file *ovl_path_open(struct path *path, int flags)
  296. {
  297. return dentry_open(path, flags | O_NOATIME, current_cred());
  298. }
  299. int ovl_copy_up_start(struct dentry *dentry)
  300. {
  301. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  302. int err;
  303. err = mutex_lock_interruptible(&oi->lock);
  304. if (!err && ovl_dentry_has_upper_alias(dentry)) {
  305. err = 1; /* Already copied up */
  306. mutex_unlock(&oi->lock);
  307. }
  308. return err;
  309. }
  310. void ovl_copy_up_end(struct dentry *dentry)
  311. {
  312. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  313. }
  314. bool ovl_check_origin_xattr(struct dentry *dentry)
  315. {
  316. int res;
  317. res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
  318. /* Zero size value means "copied up but origin unknown" */
  319. if (res >= 0)
  320. return true;
  321. return false;
  322. }
  323. bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
  324. {
  325. int res;
  326. char val;
  327. if (!d_is_dir(dentry))
  328. return false;
  329. res = vfs_getxattr(dentry, name, &val, 1);
  330. if (res == 1 && val == 'y')
  331. return true;
  332. return false;
  333. }
  334. int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
  335. const char *name, const void *value, size_t size,
  336. int xerr)
  337. {
  338. int err;
  339. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  340. if (ofs->noxattr)
  341. return xerr;
  342. err = ovl_do_setxattr(upperdentry, name, value, size, 0);
  343. if (err == -EOPNOTSUPP) {
  344. pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
  345. ofs->noxattr = true;
  346. return xerr;
  347. }
  348. return err;
  349. }
  350. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  351. {
  352. int err;
  353. if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
  354. return 0;
  355. /*
  356. * Do not fail when upper doesn't support xattrs.
  357. * Upper inodes won't have origin nor redirect xattr anyway.
  358. */
  359. err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
  360. "y", 1, 0);
  361. if (!err)
  362. ovl_set_flag(OVL_IMPURE, d_inode(dentry));
  363. return err;
  364. }
  365. void ovl_set_flag(unsigned long flag, struct inode *inode)
  366. {
  367. set_bit(flag, &OVL_I(inode)->flags);
  368. }
  369. void ovl_clear_flag(unsigned long flag, struct inode *inode)
  370. {
  371. clear_bit(flag, &OVL_I(inode)->flags);
  372. }
  373. bool ovl_test_flag(unsigned long flag, struct inode *inode)
  374. {
  375. return test_bit(flag, &OVL_I(inode)->flags);
  376. }
  377. /**
  378. * Caller must hold a reference to inode to prevent it from being freed while
  379. * it is marked inuse.
  380. */
  381. bool ovl_inuse_trylock(struct dentry *dentry)
  382. {
  383. struct inode *inode = d_inode(dentry);
  384. bool locked = false;
  385. spin_lock(&inode->i_lock);
  386. if (!(inode->i_state & I_OVL_INUSE)) {
  387. inode->i_state |= I_OVL_INUSE;
  388. locked = true;
  389. }
  390. spin_unlock(&inode->i_lock);
  391. return locked;
  392. }
  393. void ovl_inuse_unlock(struct dentry *dentry)
  394. {
  395. if (dentry) {
  396. struct inode *inode = d_inode(dentry);
  397. spin_lock(&inode->i_lock);
  398. WARN_ON(!(inode->i_state & I_OVL_INUSE));
  399. inode->i_state &= ~I_OVL_INUSE;
  400. spin_unlock(&inode->i_lock);
  401. }
  402. }
  403. /*
  404. * Does this overlay dentry need to be indexed on copy up?
  405. */
  406. bool ovl_need_index(struct dentry *dentry)
  407. {
  408. struct dentry *lower = ovl_dentry_lower(dentry);
  409. if (!lower || !ovl_indexdir(dentry->d_sb))
  410. return false;
  411. /* Index all files for NFS export and consistency verification */
  412. if (ovl_index_all(dentry->d_sb))
  413. return true;
  414. /* Index only lower hardlinks on copy up */
  415. if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
  416. return true;
  417. return false;
  418. }
  419. /* Caller must hold OVL_I(inode)->lock */
  420. static void ovl_cleanup_index(struct dentry *dentry)
  421. {
  422. struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
  423. struct inode *dir = indexdir->d_inode;
  424. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  425. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  426. struct dentry *index = NULL;
  427. struct inode *inode;
  428. struct qstr name;
  429. int err;
  430. err = ovl_get_index_name(lowerdentry, &name);
  431. if (err)
  432. goto fail;
  433. inode = d_inode(upperdentry);
  434. if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
  435. pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
  436. upperdentry, inode->i_ino, inode->i_nlink);
  437. /*
  438. * We either have a bug with persistent union nlink or a lower
  439. * hardlink was added while overlay is mounted. Adding a lower
  440. * hardlink and then unlinking all overlay hardlinks would drop
  441. * overlay nlink to zero before all upper inodes are unlinked.
  442. * As a safety measure, when that situation is detected, set
  443. * the overlay nlink to the index inode nlink minus one for the
  444. * index entry itself.
  445. */
  446. set_nlink(d_inode(dentry), inode->i_nlink - 1);
  447. ovl_set_nlink_upper(dentry);
  448. goto out;
  449. }
  450. inode_lock_nested(dir, I_MUTEX_PARENT);
  451. index = lookup_one_len(name.name, indexdir, name.len);
  452. err = PTR_ERR(index);
  453. if (IS_ERR(index)) {
  454. index = NULL;
  455. } else if (ovl_index_all(dentry->d_sb)) {
  456. /* Whiteout orphan index to block future open by handle */
  457. err = ovl_cleanup_and_whiteout(indexdir, dir, index);
  458. } else {
  459. /* Cleanup orphan index entries */
  460. err = ovl_cleanup(dir, index);
  461. }
  462. inode_unlock(dir);
  463. if (err)
  464. goto fail;
  465. out:
  466. dput(index);
  467. return;
  468. fail:
  469. pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
  470. goto out;
  471. }
  472. /*
  473. * Operations that change overlay inode and upper inode nlink need to be
  474. * synchronized with copy up for persistent nlink accounting.
  475. */
  476. int ovl_nlink_start(struct dentry *dentry, bool *locked)
  477. {
  478. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  479. const struct cred *old_cred;
  480. int err;
  481. if (!d_inode(dentry))
  482. return 0;
  483. /*
  484. * With inodes index is enabled, we store the union overlay nlink
  485. * in an xattr on the index inode. When whiting out an indexed lower,
  486. * we need to decrement the overlay persistent nlink, but before the
  487. * first copy up, we have no upper index inode to store the xattr.
  488. *
  489. * As a workaround, before whiteout/rename over an indexed lower,
  490. * copy up to create the upper index. Creating the upper index will
  491. * initialize the overlay nlink, so it could be dropped if unlink
  492. * or rename succeeds.
  493. *
  494. * TODO: implement metadata only index copy up when called with
  495. * ovl_copy_up_flags(dentry, O_PATH).
  496. */
  497. if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
  498. err = ovl_copy_up(dentry);
  499. if (err)
  500. return err;
  501. }
  502. err = mutex_lock_interruptible(&oi->lock);
  503. if (err)
  504. return err;
  505. if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  506. goto out;
  507. old_cred = ovl_override_creds(dentry->d_sb);
  508. /*
  509. * The overlay inode nlink should be incremented/decremented IFF the
  510. * upper operation succeeds, along with nlink change of upper inode.
  511. * Therefore, before link/unlink/rename, we store the union nlink
  512. * value relative to the upper inode nlink in an upper inode xattr.
  513. */
  514. err = ovl_set_nlink_upper(dentry);
  515. revert_creds(old_cred);
  516. out:
  517. if (err)
  518. mutex_unlock(&oi->lock);
  519. else
  520. *locked = true;
  521. return err;
  522. }
  523. void ovl_nlink_end(struct dentry *dentry, bool locked)
  524. {
  525. if (locked) {
  526. if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
  527. d_inode(dentry)->i_nlink == 0) {
  528. const struct cred *old_cred;
  529. old_cred = ovl_override_creds(dentry->d_sb);
  530. ovl_cleanup_index(dentry);
  531. revert_creds(old_cred);
  532. }
  533. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  534. }
  535. }
  536. int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
  537. {
  538. /* Workdir should not be the same as upperdir */
  539. if (workdir == upperdir)
  540. goto err;
  541. /* Workdir should not be subdir of upperdir and vice versa */
  542. if (lock_rename(workdir, upperdir) != NULL)
  543. goto err_unlock;
  544. return 0;
  545. err_unlock:
  546. unlock_rename(workdir, upperdir);
  547. err:
  548. pr_err("overlayfs: failed to lock workdir+upperdir\n");
  549. return -EIO;
  550. }