util.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 inode *inode)
  147. {
  148. return OVL_I(inode)->cache;
  149. }
  150. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
  151. {
  152. OVL_I(inode)->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, bool impurity)
  222. {
  223. struct inode *inode = d_inode(dentry);
  224. WARN_ON(!inode_is_locked(inode));
  225. /*
  226. * Version is used by readdir code to keep cache consistent. For merge
  227. * dirs all changes need to be noted. For non-merge dirs, cache only
  228. * contains impure (ones which have been copied up and have origins)
  229. * entries, so only need to note changes to impure entries.
  230. */
  231. if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
  232. OVL_I(inode)->version++;
  233. }
  234. u64 ovl_dentry_version_get(struct dentry *dentry)
  235. {
  236. struct inode *inode = d_inode(dentry);
  237. WARN_ON(!inode_is_locked(inode));
  238. return OVL_I(inode)->version;
  239. }
  240. bool ovl_is_whiteout(struct dentry *dentry)
  241. {
  242. struct inode *inode = dentry->d_inode;
  243. return inode && IS_WHITEOUT(inode);
  244. }
  245. struct file *ovl_path_open(struct path *path, int flags)
  246. {
  247. return dentry_open(path, flags | O_NOATIME, current_cred());
  248. }
  249. int ovl_copy_up_start(struct dentry *dentry)
  250. {
  251. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  252. int err;
  253. err = mutex_lock_interruptible(&oi->lock);
  254. if (!err && ovl_dentry_has_upper_alias(dentry)) {
  255. err = 1; /* Already copied up */
  256. mutex_unlock(&oi->lock);
  257. }
  258. return err;
  259. }
  260. void ovl_copy_up_end(struct dentry *dentry)
  261. {
  262. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  263. }
  264. bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
  265. {
  266. int res;
  267. char val;
  268. if (!d_is_dir(dentry))
  269. return false;
  270. res = vfs_getxattr(dentry, name, &val, 1);
  271. if (res == 1 && val == 'y')
  272. return true;
  273. return false;
  274. }
  275. int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
  276. const char *name, const void *value, size_t size,
  277. int xerr)
  278. {
  279. int err;
  280. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  281. if (ofs->noxattr)
  282. return xerr;
  283. err = ovl_do_setxattr(upperdentry, name, value, size, 0);
  284. if (err == -EOPNOTSUPP) {
  285. pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
  286. ofs->noxattr = true;
  287. return xerr;
  288. }
  289. return err;
  290. }
  291. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  292. {
  293. int err;
  294. if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
  295. return 0;
  296. /*
  297. * Do not fail when upper doesn't support xattrs.
  298. * Upper inodes won't have origin nor redirect xattr anyway.
  299. */
  300. err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
  301. "y", 1, 0);
  302. if (!err)
  303. ovl_set_flag(OVL_IMPURE, d_inode(dentry));
  304. return err;
  305. }
  306. void ovl_set_flag(unsigned long flag, struct inode *inode)
  307. {
  308. set_bit(flag, &OVL_I(inode)->flags);
  309. }
  310. void ovl_clear_flag(unsigned long flag, struct inode *inode)
  311. {
  312. clear_bit(flag, &OVL_I(inode)->flags);
  313. }
  314. bool ovl_test_flag(unsigned long flag, struct inode *inode)
  315. {
  316. return test_bit(flag, &OVL_I(inode)->flags);
  317. }
  318. /**
  319. * Caller must hold a reference to inode to prevent it from being freed while
  320. * it is marked inuse.
  321. */
  322. bool ovl_inuse_trylock(struct dentry *dentry)
  323. {
  324. struct inode *inode = d_inode(dentry);
  325. bool locked = false;
  326. spin_lock(&inode->i_lock);
  327. if (!(inode->i_state & I_OVL_INUSE)) {
  328. inode->i_state |= I_OVL_INUSE;
  329. locked = true;
  330. }
  331. spin_unlock(&inode->i_lock);
  332. return locked;
  333. }
  334. void ovl_inuse_unlock(struct dentry *dentry)
  335. {
  336. if (dentry) {
  337. struct inode *inode = d_inode(dentry);
  338. spin_lock(&inode->i_lock);
  339. WARN_ON(!(inode->i_state & I_OVL_INUSE));
  340. inode->i_state &= ~I_OVL_INUSE;
  341. spin_unlock(&inode->i_lock);
  342. }
  343. }
  344. /* Called must hold OVL_I(inode)->oi_lock */
  345. static void ovl_cleanup_index(struct dentry *dentry)
  346. {
  347. struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
  348. struct dentry *lowerdentry = ovl_dentry_lower(dentry);
  349. struct dentry *upperdentry = ovl_dentry_upper(dentry);
  350. struct dentry *index = NULL;
  351. struct inode *inode;
  352. struct qstr name;
  353. int err;
  354. err = ovl_get_index_name(lowerdentry, &name);
  355. if (err)
  356. goto fail;
  357. inode = d_inode(upperdentry);
  358. if (inode->i_nlink != 1) {
  359. pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
  360. upperdentry, inode->i_ino, inode->i_nlink);
  361. /*
  362. * We either have a bug with persistent union nlink or a lower
  363. * hardlink was added while overlay is mounted. Adding a lower
  364. * hardlink and then unlinking all overlay hardlinks would drop
  365. * overlay nlink to zero before all upper inodes are unlinked.
  366. * As a safety measure, when that situation is detected, set
  367. * the overlay nlink to the index inode nlink minus one for the
  368. * index entry itself.
  369. */
  370. set_nlink(d_inode(dentry), inode->i_nlink - 1);
  371. ovl_set_nlink_upper(dentry);
  372. goto out;
  373. }
  374. inode_lock_nested(dir, I_MUTEX_PARENT);
  375. /* TODO: whiteout instead of cleanup to block future open by handle */
  376. index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
  377. err = PTR_ERR(index);
  378. if (!IS_ERR(index))
  379. err = ovl_cleanup(dir, index);
  380. inode_unlock(dir);
  381. if (err)
  382. goto fail;
  383. out:
  384. dput(index);
  385. return;
  386. fail:
  387. pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
  388. goto out;
  389. }
  390. /*
  391. * Operations that change overlay inode and upper inode nlink need to be
  392. * synchronized with copy up for persistent nlink accounting.
  393. */
  394. int ovl_nlink_start(struct dentry *dentry, bool *locked)
  395. {
  396. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  397. const struct cred *old_cred;
  398. int err;
  399. if (!d_inode(dentry) || d_is_dir(dentry))
  400. return 0;
  401. /*
  402. * With inodes index is enabled, we store the union overlay nlink
  403. * in an xattr on the index inode. When whiting out lower hardlinks
  404. * we need to decrement the overlay persistent nlink, but before the
  405. * first copy up, we have no upper index inode to store the xattr.
  406. *
  407. * As a workaround, before whiteout/rename over of a lower hardlink,
  408. * copy up to create the upper index. Creating the upper index will
  409. * initialize the overlay nlink, so it could be dropped if unlink
  410. * or rename succeeds.
  411. *
  412. * TODO: implement metadata only index copy up when called with
  413. * ovl_copy_up_flags(dentry, O_PATH).
  414. */
  415. if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
  416. d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
  417. err = ovl_copy_up(dentry);
  418. if (err)
  419. return err;
  420. }
  421. err = mutex_lock_interruptible(&oi->lock);
  422. if (err)
  423. return err;
  424. if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
  425. goto out;
  426. old_cred = ovl_override_creds(dentry->d_sb);
  427. /*
  428. * The overlay inode nlink should be incremented/decremented IFF the
  429. * upper operation succeeds, along with nlink change of upper inode.
  430. * Therefore, before link/unlink/rename, we store the union nlink
  431. * value relative to the upper inode nlink in an upper inode xattr.
  432. */
  433. err = ovl_set_nlink_upper(dentry);
  434. revert_creds(old_cred);
  435. out:
  436. if (err)
  437. mutex_unlock(&oi->lock);
  438. else
  439. *locked = true;
  440. return err;
  441. }
  442. void ovl_nlink_end(struct dentry *dentry, bool locked)
  443. {
  444. if (locked) {
  445. if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
  446. d_inode(dentry)->i_nlink == 0) {
  447. const struct cred *old_cred;
  448. old_cred = ovl_override_creds(dentry->d_sb);
  449. ovl_cleanup_index(dentry);
  450. revert_creds(old_cred);
  451. }
  452. mutex_unlock(&OVL_I(d_inode(dentry))->lock);
  453. }
  454. }