util.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 "overlayfs.h"
  15. #include "ovl_entry.h"
  16. int ovl_want_write(struct dentry *dentry)
  17. {
  18. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  19. return mnt_want_write(ofs->upper_mnt);
  20. }
  21. void ovl_drop_write(struct dentry *dentry)
  22. {
  23. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  24. mnt_drop_write(ofs->upper_mnt);
  25. }
  26. struct dentry *ovl_workdir(struct dentry *dentry)
  27. {
  28. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  29. return ofs->workdir;
  30. }
  31. const struct cred *ovl_override_creds(struct super_block *sb)
  32. {
  33. struct ovl_fs *ofs = sb->s_fs_info;
  34. return override_creds(ofs->creator_cred);
  35. }
  36. struct super_block *ovl_same_sb(struct super_block *sb)
  37. {
  38. struct ovl_fs *ofs = sb->s_fs_info;
  39. return ofs->same_sb;
  40. }
  41. struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
  42. {
  43. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  44. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  45. if (oe)
  46. oe->numlower = numlower;
  47. return oe;
  48. }
  49. bool ovl_dentry_remote(struct dentry *dentry)
  50. {
  51. return dentry->d_flags &
  52. (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  53. DCACHE_OP_REAL);
  54. }
  55. bool ovl_dentry_weird(struct dentry *dentry)
  56. {
  57. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  58. DCACHE_MANAGE_TRANSIT |
  59. DCACHE_OP_HASH |
  60. DCACHE_OP_COMPARE);
  61. }
  62. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  63. {
  64. struct ovl_entry *oe = dentry->d_fsdata;
  65. enum ovl_path_type type = 0;
  66. if (ovl_dentry_upper(dentry)) {
  67. type = __OVL_PATH_UPPER;
  68. /*
  69. * Non-dir dentry can hold lower dentry of its copy up origin.
  70. */
  71. if (oe->numlower) {
  72. type |= __OVL_PATH_ORIGIN;
  73. if (d_is_dir(dentry))
  74. type |= __OVL_PATH_MERGE;
  75. }
  76. } else {
  77. if (oe->numlower > 1)
  78. type |= __OVL_PATH_MERGE;
  79. }
  80. return type;
  81. }
  82. void ovl_path_upper(struct dentry *dentry, struct path *path)
  83. {
  84. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  85. path->mnt = ofs->upper_mnt;
  86. path->dentry = ovl_dentry_upper(dentry);
  87. }
  88. void ovl_path_lower(struct dentry *dentry, struct path *path)
  89. {
  90. struct ovl_entry *oe = dentry->d_fsdata;
  91. *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
  92. }
  93. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  94. {
  95. enum ovl_path_type type = ovl_path_type(dentry);
  96. if (!OVL_TYPE_UPPER(type))
  97. ovl_path_lower(dentry, path);
  98. else
  99. ovl_path_upper(dentry, path);
  100. return type;
  101. }
  102. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  103. {
  104. return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
  105. }
  106. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  107. {
  108. struct ovl_entry *oe = dentry->d_fsdata;
  109. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  110. }
  111. struct dentry *ovl_dentry_real(struct dentry *dentry)
  112. {
  113. return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
  114. }
  115. struct inode *ovl_inode_upper(struct inode *inode)
  116. {
  117. struct dentry *upperdentry = ovl_upperdentry_dereference(OVL_I(inode));
  118. return upperdentry ? d_inode(upperdentry) : NULL;
  119. }
  120. struct inode *ovl_inode_lower(struct inode *inode)
  121. {
  122. return OVL_I(inode)->lower;
  123. }
  124. struct inode *ovl_inode_real(struct inode *inode)
  125. {
  126. return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
  127. }
  128. struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
  129. {
  130. struct ovl_entry *oe = dentry->d_fsdata;
  131. return oe->cache;
  132. }
  133. void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
  134. {
  135. struct ovl_entry *oe = dentry->d_fsdata;
  136. oe->cache = cache;
  137. }
  138. bool ovl_dentry_is_opaque(struct dentry *dentry)
  139. {
  140. struct ovl_entry *oe = dentry->d_fsdata;
  141. return oe->opaque;
  142. }
  143. bool ovl_dentry_is_impure(struct dentry *dentry)
  144. {
  145. struct ovl_entry *oe = dentry->d_fsdata;
  146. return oe->impure;
  147. }
  148. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  149. {
  150. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  151. }
  152. void ovl_dentry_set_opaque(struct dentry *dentry)
  153. {
  154. struct ovl_entry *oe = dentry->d_fsdata;
  155. oe->opaque = true;
  156. }
  157. bool ovl_redirect_dir(struct super_block *sb)
  158. {
  159. struct ovl_fs *ofs = sb->s_fs_info;
  160. return ofs->config.redirect_dir && !ofs->noxattr;
  161. }
  162. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  163. {
  164. return OVL_I(d_inode(dentry))->redirect;
  165. }
  166. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  167. {
  168. struct ovl_inode *oi = OVL_I(d_inode(dentry));
  169. kfree(oi->redirect);
  170. oi->redirect = redirect;
  171. }
  172. void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
  173. struct dentry *lowerdentry)
  174. {
  175. if (upperdentry)
  176. OVL_I(inode)->__upperdentry = upperdentry;
  177. if (lowerdentry)
  178. OVL_I(inode)->lower = d_inode(lowerdentry);
  179. ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
  180. }
  181. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
  182. {
  183. struct inode *upperinode = d_inode(upperdentry);
  184. WARN_ON(!inode_unhashed(inode));
  185. WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
  186. WARN_ON(OVL_I(inode)->__upperdentry);
  187. /*
  188. * Make sure upperdentry is consistent before making it visible
  189. */
  190. smp_wmb();
  191. OVL_I(inode)->__upperdentry = upperdentry;
  192. if (!S_ISDIR(upperinode->i_mode)) {
  193. inode->i_private = upperinode;
  194. __insert_inode_hash(inode, (unsigned long) upperinode);
  195. }
  196. }
  197. void ovl_dentry_version_inc(struct dentry *dentry)
  198. {
  199. struct ovl_entry *oe = dentry->d_fsdata;
  200. WARN_ON(!inode_is_locked(dentry->d_inode));
  201. oe->version++;
  202. }
  203. u64 ovl_dentry_version_get(struct dentry *dentry)
  204. {
  205. struct ovl_entry *oe = dentry->d_fsdata;
  206. WARN_ON(!inode_is_locked(dentry->d_inode));
  207. return oe->version;
  208. }
  209. bool ovl_is_whiteout(struct dentry *dentry)
  210. {
  211. struct inode *inode = dentry->d_inode;
  212. return inode && IS_WHITEOUT(inode);
  213. }
  214. struct file *ovl_path_open(struct path *path, int flags)
  215. {
  216. return dentry_open(path, flags | O_NOATIME, current_cred());
  217. }
  218. int ovl_copy_up_start(struct dentry *dentry)
  219. {
  220. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  221. struct ovl_entry *oe = dentry->d_fsdata;
  222. int err;
  223. spin_lock(&ofs->copyup_wq.lock);
  224. err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
  225. if (!err) {
  226. if (ovl_dentry_upper(dentry))
  227. err = 1; /* Already copied up */
  228. else
  229. oe->copying = true;
  230. }
  231. spin_unlock(&ofs->copyup_wq.lock);
  232. return err;
  233. }
  234. void ovl_copy_up_end(struct dentry *dentry)
  235. {
  236. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  237. struct ovl_entry *oe = dentry->d_fsdata;
  238. spin_lock(&ofs->copyup_wq.lock);
  239. oe->copying = false;
  240. wake_up_locked(&ofs->copyup_wq);
  241. spin_unlock(&ofs->copyup_wq.lock);
  242. }
  243. bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
  244. {
  245. int res;
  246. char val;
  247. if (!d_is_dir(dentry))
  248. return false;
  249. res = vfs_getxattr(dentry, name, &val, 1);
  250. if (res == 1 && val == 'y')
  251. return true;
  252. return false;
  253. }
  254. int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
  255. const char *name, const void *value, size_t size,
  256. int xerr)
  257. {
  258. int err;
  259. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  260. if (ofs->noxattr)
  261. return xerr;
  262. err = ovl_do_setxattr(upperdentry, name, value, size, 0);
  263. if (err == -EOPNOTSUPP) {
  264. pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
  265. ofs->noxattr = true;
  266. return xerr;
  267. }
  268. return err;
  269. }
  270. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
  271. {
  272. int err;
  273. struct ovl_entry *oe = dentry->d_fsdata;
  274. if (oe->impure)
  275. return 0;
  276. /*
  277. * Do not fail when upper doesn't support xattrs.
  278. * Upper inodes won't have origin nor redirect xattr anyway.
  279. */
  280. err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
  281. "y", 1, 0);
  282. if (!err)
  283. oe->impure = true;
  284. return err;
  285. }