util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. #include "ovl_entry.h"
  20. int ovl_want_write(struct dentry *dentry)
  21. {
  22. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  23. return mnt_want_write(ofs->upper_mnt);
  24. }
  25. void ovl_drop_write(struct dentry *dentry)
  26. {
  27. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  28. mnt_drop_write(ofs->upper_mnt);
  29. }
  30. struct dentry *ovl_workdir(struct dentry *dentry)
  31. {
  32. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  33. return ofs->workdir;
  34. }
  35. const struct cred *ovl_override_creds(struct super_block *sb)
  36. {
  37. struct ovl_fs *ofs = sb->s_fs_info;
  38. return override_creds(ofs->creator_cred);
  39. }
  40. struct super_block *ovl_same_sb(struct super_block *sb)
  41. {
  42. struct ovl_fs *ofs = sb->s_fs_info;
  43. return ofs->same_sb;
  44. }
  45. bool ovl_can_decode_fh(struct super_block *sb)
  46. {
  47. return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
  48. !uuid_is_null(&sb->s_uuid));
  49. }
  50. struct dentry *ovl_indexdir(struct super_block *sb)
  51. {
  52. struct ovl_fs *ofs = sb->s_fs_info;
  53. return ofs->indexdir;
  54. }
  55. struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  56. {
  57. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  58. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  59. if (oe)
  60. oe->numlower = numlower;
  61. return oe;
  62. }
  63. bool ovl_dentry_remote(struct dentry *dentry)
  64. {
  65. return dentry->d_flags &
  66. (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  67. DCACHE_OP_REAL);
  68. }
  69. bool ovl_dentry_weird(struct dentry *dentry)
  70. {
  71. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  72. DCACHE_MANAGE_TRANSIT |
  73. DCACHE_OP_HASH |
  74. DCACHE_OP_COMPARE);
  75. }
  76. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  77. {
  78. struct ovl_entry *oe = dentry->d_fsdata;
  79. enum ovl_path_type type = 0;
  80. if (ovl_dentry_upper(dentry)) {
  81. type = __OVL_PATH_UPPER;
  82. /*
  83. * Non-dir dentry can hold lower dentry of its copy up origin.
  84. */
  85. if (oe->numlower) {
  86. type |= __OVL_PATH_ORIGIN;
  87. if (d_is_dir(dentry))
  88. type |= __OVL_PATH_MERGE;
  89. }
  90. } else {
  91. if (oe->numlower > 1)
  92. type |= __OVL_PATH_MERGE;
  93. }
  94. return type;
  95. }
  96. void ovl_path_upper(struct dentry *dentry, struct path *path)
  97. {
  98. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  99. path->mnt = ofs->upper_mnt;
  100. path->dentry = ovl_dentry_upper(dentry);
  101. }
  102. void ovl_path_lower(struct dentry *dentry, struct path *path)
  103. {
  104. struct ovl_entry *oe = dentry->d_fsdata;
  105. *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
  106. }
  107. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  108. {
  109. enum ovl_path_type type = ovl_path_type(dentry);
  110. if (!OVL_TYPE_UPPER(type))
  111. ovl_path_lower(dentry, path);
  112. else
  113. ovl_path_upper(dentry, path);
  114. return type;
  115. }
  116. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  117. {
  118. return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
  119. }
  120. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  121. {
  122. struct ovl_entry *oe = dentry->d_fsdata;
  123. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  124. }
  125. struct dentry *ovl_dentry_real(struct dentry *dentry)
  126. {
  127. return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
  128. }
  129. struct dentry *ovl_i_dentry_upper(struct inode *inode)
  130. {
  131. return ovl_upperdentry_dereference(OVL_I(inode));
  132. }
  133. struct inode *ovl_inode_upper(struct inode *inode)
  134. {
  135. struct dentry *upperdentry = ovl_i_dentry_upper(inode);
  136. return upperdentry ? d_inode(upperdentry) : NULL;
  137. }
  138. struct inode *ovl_inode_lower(struct inode *inode)
  139. {
  140. return OVL_I(inode)->lower;
  141. }
  142. struct inode *ovl_inode_real(struct inode *inode)
  143. {
  144. return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
  145. }
  146. struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
  147. {
  148. return OVL_I(d_inode(dentry))->cache;
  149. }
  150. void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
  151. {
  152. OVL_I(d_inode(dentry))->cache = cache;
  153. }
  154. bool ovl_dentry_is_opaque(struct dentry *dentry)
  155. {
  156. struct ovl_entry *oe = dentry->d_fsdata;
  157. return oe->opaque;
  158. }
  159. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  160. {
  161. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  162. }
  163. void ovl_dentry_set_opaque(struct dentry *dentry)
  164. {
  165. struct ovl_entry *oe = dentry->d_fsdata;
  166. oe->opaque = true;
  167. }
  168. /*
  169. * For hard links it's possible for ovl_dentry_upper() to return positive, while
  170. * there's no actual upper alias for the inode. Copy up code needs to know
  171. * about the existence of the upper alias, so it can't use ovl_dentry_upper().
  172. */
  173. bool ovl_dentry_has_upper_alias(struct dentry *dentry)
  174. {
  175. struct ovl_entry *oe = dentry->d_fsdata;
  176. return oe->has_upper;
  177. }
  178. void ovl_dentry_set_upper_alias(struct dentry *dentry)
  179. {
  180. struct ovl_entry *oe = dentry->d_fsdata;
  181. oe->has_upper = true;
  182. }
  183. bool ovl_redirect_dir(struct super_block *sb)
  184. {
  185. struct ovl_fs *ofs = sb->s_fs_info;
  186. return ofs->config.redirect_dir && !ofs->noxattr;
  187. }
  188. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  189. {
  190. return OVL_I(d_inode(dentry))->redirect;
  191. }
  192. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  193. {
  194. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  195. kfree(oi->redirect);
  196. oi->redirect = redirect;
  197. }
  198. void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
  199. struct dentry *lowerdentry)
  200. {
  201. if (upperdentry)
  202. OVL_I(inode)->__upperdentry = upperdentry;
  203. if (lowerdentry)
  204. OVL_I(inode)->lower = d_inode(lowerdentry);
  205. ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
  206. }
  207. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
  208. {
  209. struct inode *upperinode = d_inode(upperdentry);
  210. WARN_ON(OVL_I(inode)->__upperdentry);
  211. /*
  212. * Make sure upperdentry is consistent before making it visible
  213. */
  214. smp_wmb();
  215. OVL_I(inode)->__upperdentry = upperdentry;
  216. if (!S_ISDIR(upperinode->i_mode) && inode_unhashed(inode)) {
  217. inode->i_private = upperinode;
  218. __insert_inode_hash(inode, (unsigned long) upperinode);
  219. }
  220. }
  221. void ovl_dentry_version_inc(struct dentry *dentry)
  222. {
  223. struct inode *inode = d_inode(dentry);
  224. WARN_ON(!inode_is_locked(inode));
  225. OVL_I(inode)->version++;
  226. }
  227. u64 ovl_dentry_version_get(struct dentry *dentry)
  228. {
  229. struct inode *inode = d_inode(dentry);
  230. WARN_ON(!inode_is_locked(inode));
  231. return OVL_I(inode)->version;
  232. }
  233. bool ovl_is_whiteout(struct dentry *dentry)
  234. {
  235. struct inode *inode = dentry->d_inode;
  236. return inode && IS_WHITEOUT(inode);
  237. }
  238. struct file *ovl_path_open(struct path *path, int flags)
  239. {
  240. return dentry_open(path, flags | O_NOATIME, current_cred());
  241. }
  242. int ovl_copy_up_start(struct dentry *dentry)
  243. {
  244. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  245. int err;
  246. err = mutex_lock_interruptible(&oi->lock);
  247. if (!err && ovl_dentry_has_upper_alias(dentry)) {
  248. err = 1; /* Already copied up */
  249. mutex_unlock(&oi->lock);
  250. }
  251. return err;
  252. }
  253. void ovl_copy_up_end(struct dentry *dentry)
  254. {
  255. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  256. }
  257. bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
  258. {
  259. int res;
  260. char val;
  261. if (!d_is_dir(dentry))
  262. return false;
  263. res = vfs_getxattr(dentry, name, &val, 1);
  264. if (res == 1 && val == 'y')
  265. return true;
  266. return false;
  267. }
  268. int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
  269. const char *name, const void *value, size_t size,
  270. int xerr)
  271. {
  272. int err;
  273. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  274. if (ofs->noxattr)
  275. return xerr;
  276. err = ovl_do_setxattr(upperdentry, name, value, size, 0);
  277. if (err == -EOPNOTSUPP) {
  278. pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
  279. ofs->noxattr = true;
  280. return xerr;
  281. }
  282. return err;
  283. }
  284. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  285. {
  286. int err;
  287. if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
  288. return 0;
  289. /*
  290. * Do not fail when upper doesn't support xattrs.
  291. * Upper inodes won't have origin nor redirect xattr anyway.
  292. */
  293. err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
  294. "y", 1, 0);
  295. if (!err)
  296. ovl_set_flag(OVL_IMPURE, d_inode(dentry));
  297. return err;
  298. }
  299. void ovl_set_flag(unsigned long flag, struct inode *inode)
  300. {
  301. set_bit(flag, &OVL_I(inode)->flags);
  302. }
  303. bool ovl_test_flag(unsigned long flag, struct inode *inode)
  304. {
  305. return test_bit(flag, &OVL_I(inode)->flags);
  306. }
  307. /**
  308. * Caller must hold a reference to inode to prevent it from being freed while
  309. * it is marked inuse.
  310. */
  311. bool ovl_inuse_trylock(struct dentry *dentry)
  312. {
  313. struct inode *inode = d_inode(dentry);
  314. bool locked = false;
  315. spin_lock(&inode->i_lock);
  316. if (!(inode->i_state & I_OVL_INUSE)) {
  317. inode->i_state |= I_OVL_INUSE;
  318. locked = true;
  319. }
  320. spin_unlock(&inode->i_lock);
  321. return locked;
  322. }
  323. void ovl_inuse_unlock(struct dentry *dentry)
  324. {
  325. if (dentry) {
  326. struct inode *inode = d_inode(dentry);
  327. spin_lock(&inode->i_lock);
  328. WARN_ON(!(inode->i_state & I_OVL_INUSE));
  329. inode->i_state &= ~I_OVL_INUSE;
  330. spin_unlock(&inode->i_lock);
  331. }
  332. }
  333. /* Called must hold OVL_I(inode)->oi_lock */
  334. static void ovl_cleanup_index(struct dentry *dentry)
  335. {
  336. struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
  337. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  338. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  339. struct dentry *index = NULL;
  340. struct inode *inode;
  341. struct qstr name;
  342. int err;
  343. err = ovl_get_index_name(lowerdentry, &name);
  344. if (err)
  345. goto fail;
  346. inode = d_inode(upperdentry);
  347. if (inode->i_nlink != 1) {
  348. pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
  349. upperdentry, inode->i_ino, inode->i_nlink);
  350. /*
  351. * We either have a bug with persistent union nlink or a lower
  352. * hardlink was added while overlay is mounted. Adding a lower
  353. * hardlink and then unlinking all overlay hardlinks would drop
  354. * overlay nlink to zero before all upper inodes are unlinked.
  355. * As a safety measure, when that situation is detected, set
  356. * the overlay nlink to the index inode nlink minus one for the
  357. * index entry itself.
  358. */
  359. set_nlink(d_inode(dentry), inode->i_nlink - 1);
  360. ovl_set_nlink_upper(dentry);
  361. goto out;
  362. }
  363. inode_lock_nested(dir, I_MUTEX_PARENT);
  364. /* TODO: whiteout instead of cleanup to block future open by handle */
  365. index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
  366. err = PTR_ERR(index);
  367. if (!IS_ERR(index))
  368. err = ovl_cleanup(dir, index);
  369. inode_unlock(dir);
  370. if (err)
  371. goto fail;
  372. out:
  373. dput(index);
  374. return;
  375. fail:
  376. pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
  377. goto out;
  378. }
  379. /*
  380. * Operations that change overlay inode and upper inode nlink need to be
  381. * synchronized with copy up for persistent nlink accounting.
  382. */
  383. int ovl_nlink_start(struct dentry *dentry, bool *locked)
  384. {
  385. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  386. const struct cred *old_cred;
  387. int err;
  388. if (!d_inode(dentry) || d_is_dir(dentry))
  389. return 0;
  390. /*
  391. * With inodes index is enabled, we store the union overlay nlink
  392. * in an xattr on the index inode. When whiting out lower hardlinks
  393. * we need to decrement the overlay persistent nlink, but before the
  394. * first copy up, we have no upper index inode to store the xattr.
  395. *
  396. * As a workaround, before whiteout/rename over of a lower hardlink,
  397. * copy up to create the upper index. Creating the upper index will
  398. * initialize the overlay nlink, so it could be dropped if unlink
  399. * or rename succeeds.
  400. *
  401. * TODO: implement metadata only index copy up when called with
  402. * ovl_copy_up_flags(dentry, O_PATH).
  403. */
  404. if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
  405. d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
  406. err = ovl_copy_up(dentry);
  407. if (err)
  408. return err;
  409. }
  410. err = mutex_lock_interruptible(&oi->lock);
  411. if (err)
  412. return err;
  413. if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  414. goto out;
  415. old_cred = ovl_override_creds(dentry->d_sb);
  416. /*
  417. * The overlay inode nlink should be incremented/decremented IFF the
  418. * upper operation succeeds, along with nlink change of upper inode.
  419. * Therefore, before link/unlink/rename, we store the union nlink
  420. * value relative to the upper inode nlink in an upper inode xattr.
  421. */
  422. err = ovl_set_nlink_upper(dentry);
  423. revert_creds(old_cred);
  424. out:
  425. if (err)
  426. mutex_unlock(&oi->lock);
  427. else
  428. *locked = true;
  429. return err;
  430. }
  431. void ovl_nlink_end(struct dentry *dentry, bool locked)
  432. {
  433. if (locked) {
  434. if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
  435. d_inode(dentry)->i_nlink == 0) {
  436. const struct cred *old_cred;
  437. old_cred = ovl_override_creds(dentry->d_sb);
  438. ovl_cleanup_index(dentry);
  439. revert_creds(old_cred);
  440. }
  441. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  442. }
  443. }