util.c 5.7 KB

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