util.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 ovl_entry *ovl_alloc_entry(unsigned int numlower)
  37. {
  38. size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
  39. struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
  40. if (oe)
  41. oe->numlower = numlower;
  42. return oe;
  43. }
  44. bool ovl_dentry_remote(struct dentry *dentry)
  45. {
  46. return dentry->d_flags &
  47. (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
  48. DCACHE_OP_REAL);
  49. }
  50. bool ovl_dentry_weird(struct dentry *dentry)
  51. {
  52. return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
  53. DCACHE_MANAGE_TRANSIT |
  54. DCACHE_OP_HASH |
  55. DCACHE_OP_COMPARE);
  56. }
  57. enum ovl_path_type ovl_path_type(struct dentry *dentry)
  58. {
  59. struct ovl_entry *oe = dentry->d_fsdata;
  60. enum ovl_path_type type = 0;
  61. if (oe->__upperdentry) {
  62. type = __OVL_PATH_UPPER;
  63. /*
  64. * Non-dir dentry can hold lower dentry from previous
  65. * location.
  66. */
  67. if (oe->numlower && d_is_dir(dentry))
  68. type |= __OVL_PATH_MERGE;
  69. } else {
  70. if (oe->numlower > 1)
  71. type |= __OVL_PATH_MERGE;
  72. }
  73. return type;
  74. }
  75. void ovl_path_upper(struct dentry *dentry, struct path *path)
  76. {
  77. struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
  78. struct ovl_entry *oe = dentry->d_fsdata;
  79. path->mnt = ofs->upper_mnt;
  80. path->dentry = ovl_upperdentry_dereference(oe);
  81. }
  82. void ovl_path_lower(struct dentry *dentry, struct path *path)
  83. {
  84. struct ovl_entry *oe = dentry->d_fsdata;
  85. *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
  86. }
  87. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
  88. {
  89. enum ovl_path_type type = ovl_path_type(dentry);
  90. if (!OVL_TYPE_UPPER(type))
  91. ovl_path_lower(dentry, path);
  92. else
  93. ovl_path_upper(dentry, path);
  94. return type;
  95. }
  96. struct dentry *ovl_dentry_upper(struct dentry *dentry)
  97. {
  98. struct ovl_entry *oe = dentry->d_fsdata;
  99. return ovl_upperdentry_dereference(oe);
  100. }
  101. static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
  102. {
  103. return oe->numlower ? oe->lowerstack[0].dentry : NULL;
  104. }
  105. struct dentry *ovl_dentry_lower(struct dentry *dentry)
  106. {
  107. struct ovl_entry *oe = dentry->d_fsdata;
  108. return __ovl_dentry_lower(oe);
  109. }
  110. struct dentry *ovl_dentry_real(struct dentry *dentry)
  111. {
  112. struct ovl_entry *oe = dentry->d_fsdata;
  113. struct dentry *realdentry;
  114. realdentry = ovl_upperdentry_dereference(oe);
  115. if (!realdentry)
  116. realdentry = __ovl_dentry_lower(oe);
  117. return realdentry;
  118. }
  119. struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
  120. {
  121. struct ovl_entry *oe = dentry->d_fsdata;
  122. return oe->cache;
  123. }
  124. void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
  125. {
  126. struct ovl_entry *oe = dentry->d_fsdata;
  127. oe->cache = cache;
  128. }
  129. bool ovl_dentry_is_opaque(struct dentry *dentry)
  130. {
  131. struct ovl_entry *oe = dentry->d_fsdata;
  132. return oe->opaque;
  133. }
  134. bool ovl_dentry_is_whiteout(struct dentry *dentry)
  135. {
  136. return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
  137. }
  138. void ovl_dentry_set_opaque(struct dentry *dentry)
  139. {
  140. struct ovl_entry *oe = dentry->d_fsdata;
  141. oe->opaque = true;
  142. }
  143. bool ovl_redirect_dir(struct super_block *sb)
  144. {
  145. struct ovl_fs *ofs = sb->s_fs_info;
  146. return ofs->config.redirect_dir;
  147. }
  148. void ovl_clear_redirect_dir(struct super_block *sb)
  149. {
  150. struct ovl_fs *ofs = sb->s_fs_info;
  151. ofs->config.redirect_dir = false;
  152. }
  153. const char *ovl_dentry_get_redirect(struct dentry *dentry)
  154. {
  155. struct ovl_entry *oe = dentry->d_fsdata;
  156. return oe->redirect;
  157. }
  158. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
  159. {
  160. struct ovl_entry *oe = dentry->d_fsdata;
  161. kfree(oe->redirect);
  162. oe->redirect = redirect;
  163. }
  164. void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
  165. {
  166. struct ovl_entry *oe = dentry->d_fsdata;
  167. WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
  168. WARN_ON(oe->__upperdentry);
  169. /*
  170. * Make sure upperdentry is consistent before making it visible to
  171. * ovl_upperdentry_dereference().
  172. */
  173. smp_wmb();
  174. oe->__upperdentry = upperdentry;
  175. }
  176. void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
  177. {
  178. WRITE_ONCE(inode->i_private, (unsigned long) realinode |
  179. (is_upper ? OVL_ISUPPER_MASK : 0));
  180. }
  181. void ovl_inode_update(struct inode *inode, struct inode *upperinode)
  182. {
  183. WARN_ON(!upperinode);
  184. WARN_ON(!inode_unhashed(inode));
  185. WRITE_ONCE(inode->i_private,
  186. (unsigned long) upperinode | OVL_ISUPPER_MASK);
  187. if (!S_ISDIR(upperinode->i_mode))
  188. __insert_inode_hash(inode, (unsigned long) upperinode);
  189. }
  190. void ovl_dentry_version_inc(struct dentry *dentry)
  191. {
  192. struct ovl_entry *oe = dentry->d_fsdata;
  193. WARN_ON(!inode_is_locked(dentry->d_inode));
  194. oe->version++;
  195. }
  196. u64 ovl_dentry_version_get(struct dentry *dentry)
  197. {
  198. struct ovl_entry *oe = dentry->d_fsdata;
  199. WARN_ON(!inode_is_locked(dentry->d_inode));
  200. return oe->version;
  201. }
  202. bool ovl_is_whiteout(struct dentry *dentry)
  203. {
  204. struct inode *inode = dentry->d_inode;
  205. return inode && IS_WHITEOUT(inode);
  206. }
  207. struct file *ovl_path_open(struct path *path, int flags)
  208. {
  209. return dentry_open(path, flags | O_NOATIME, current_cred());
  210. }