overlayfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. *
  3. * Copyright (C) 2011 Novell 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/kernel.h>
  10. #include <linux/uuid.h>
  11. enum ovl_path_type {
  12. __OVL_PATH_UPPER = (1 << 0),
  13. __OVL_PATH_MERGE = (1 << 1),
  14. __OVL_PATH_ORIGIN = (1 << 2),
  15. };
  16. #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
  17. #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
  18. #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
  19. #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
  20. #define OVL_XATTR_OPAQUE OVL_XATTR_PREFIX "opaque"
  21. #define OVL_XATTR_REDIRECT OVL_XATTR_PREFIX "redirect"
  22. #define OVL_XATTR_ORIGIN OVL_XATTR_PREFIX "origin"
  23. #define OVL_XATTR_IMPURE OVL_XATTR_PREFIX "impure"
  24. #define OVL_XATTR_NLINK OVL_XATTR_PREFIX "nlink"
  25. enum ovl_flag {
  26. OVL_IMPURE,
  27. OVL_INDEX,
  28. };
  29. /*
  30. * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
  31. * where:
  32. * origin.fh - exported file handle of the lower file
  33. * origin.uuid - uuid of the lower filesystem
  34. */
  35. #define OVL_FH_VERSION 0
  36. #define OVL_FH_MAGIC 0xfb
  37. /* CPU byte order required for fid decoding: */
  38. #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
  39. #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
  40. /* Is the real inode encoded in fid an upper inode? */
  41. #define OVL_FH_FLAG_PATH_UPPER (1 << 2)
  42. #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
  43. OVL_FH_FLAG_PATH_UPPER)
  44. #if defined(__LITTLE_ENDIAN)
  45. #define OVL_FH_FLAG_CPU_ENDIAN 0
  46. #elif defined(__BIG_ENDIAN)
  47. #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
  48. #else
  49. #error Endianness not defined
  50. #endif
  51. /* On-disk and in-memeory format for redirect by file handle */
  52. struct ovl_fh {
  53. u8 version; /* 0 */
  54. u8 magic; /* 0xfb */
  55. u8 len; /* size of this header + size of fid */
  56. u8 flags; /* OVL_FH_FLAG_* */
  57. u8 type; /* fid_type of fid */
  58. uuid_t uuid; /* uuid of filesystem */
  59. u8 fid[0]; /* file identifier */
  60. } __packed;
  61. static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
  62. {
  63. int err = vfs_rmdir(dir, dentry);
  64. pr_debug("rmdir(%pd2) = %i\n", dentry, err);
  65. return err;
  66. }
  67. static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry)
  68. {
  69. int err = vfs_unlink(dir, dentry, NULL);
  70. pr_debug("unlink(%pd2) = %i\n", dentry, err);
  71. return err;
  72. }
  73. static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir,
  74. struct dentry *new_dentry, bool debug)
  75. {
  76. int err = vfs_link(old_dentry, dir, new_dentry, NULL);
  77. if (debug) {
  78. pr_debug("link(%pd2, %pd2) = %i\n",
  79. old_dentry, new_dentry, err);
  80. }
  81. return err;
  82. }
  83. static inline int ovl_do_create(struct inode *dir, struct dentry *dentry,
  84. umode_t mode, bool debug)
  85. {
  86. int err = vfs_create(dir, dentry, mode, true);
  87. if (debug)
  88. pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
  89. return err;
  90. }
  91. static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry,
  92. umode_t mode, bool debug)
  93. {
  94. int err = vfs_mkdir(dir, dentry, mode);
  95. if (debug)
  96. pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
  97. return err;
  98. }
  99. static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry,
  100. umode_t mode, dev_t dev, bool debug)
  101. {
  102. int err = vfs_mknod(dir, dentry, mode, dev);
  103. if (debug) {
  104. pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n",
  105. dentry, mode, dev, err);
  106. }
  107. return err;
  108. }
  109. static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry,
  110. const char *oldname, bool debug)
  111. {
  112. int err = vfs_symlink(dir, dentry, oldname);
  113. if (debug)
  114. pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
  115. return err;
  116. }
  117. static inline int ovl_do_setxattr(struct dentry *dentry, const char *name,
  118. const void *value, size_t size, int flags)
  119. {
  120. int err = vfs_setxattr(dentry, name, value, size, flags);
  121. pr_debug("setxattr(%pd2, \"%s\", \"%*s\", 0x%x) = %i\n",
  122. dentry, name, (int) size, (char *) value, flags, err);
  123. return err;
  124. }
  125. static inline int ovl_do_removexattr(struct dentry *dentry, const char *name)
  126. {
  127. int err = vfs_removexattr(dentry, name);
  128. pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
  129. return err;
  130. }
  131. static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry,
  132. struct inode *newdir, struct dentry *newdentry,
  133. unsigned int flags)
  134. {
  135. int err;
  136. pr_debug("rename(%pd2, %pd2, 0x%x)\n",
  137. olddentry, newdentry, flags);
  138. err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags);
  139. if (err) {
  140. pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
  141. olddentry, newdentry, err);
  142. }
  143. return err;
  144. }
  145. static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
  146. {
  147. int err = vfs_whiteout(dir, dentry);
  148. pr_debug("whiteout(%pd2) = %i\n", dentry, err);
  149. return err;
  150. }
  151. static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
  152. {
  153. struct dentry *ret = vfs_tmpfile(dentry, mode, 0);
  154. int err = IS_ERR(ret) ? PTR_ERR(ret) : 0;
  155. pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
  156. return ret;
  157. }
  158. /* util.c */
  159. int ovl_want_write(struct dentry *dentry);
  160. void ovl_drop_write(struct dentry *dentry);
  161. struct dentry *ovl_workdir(struct dentry *dentry);
  162. const struct cred *ovl_override_creds(struct super_block *sb);
  163. struct super_block *ovl_same_sb(struct super_block *sb);
  164. bool ovl_can_decode_fh(struct super_block *sb);
  165. struct dentry *ovl_indexdir(struct super_block *sb);
  166. struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
  167. bool ovl_dentry_remote(struct dentry *dentry);
  168. bool ovl_dentry_weird(struct dentry *dentry);
  169. enum ovl_path_type ovl_path_type(struct dentry *dentry);
  170. void ovl_path_upper(struct dentry *dentry, struct path *path);
  171. void ovl_path_lower(struct dentry *dentry, struct path *path);
  172. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
  173. struct dentry *ovl_dentry_upper(struct dentry *dentry);
  174. struct dentry *ovl_dentry_lower(struct dentry *dentry);
  175. struct dentry *ovl_dentry_real(struct dentry *dentry);
  176. struct dentry *ovl_i_dentry_upper(struct inode *inode);
  177. struct inode *ovl_inode_upper(struct inode *inode);
  178. struct inode *ovl_inode_lower(struct inode *inode);
  179. struct inode *ovl_inode_real(struct inode *inode);
  180. struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
  181. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
  182. bool ovl_dentry_is_opaque(struct dentry *dentry);
  183. bool ovl_dentry_is_whiteout(struct dentry *dentry);
  184. void ovl_dentry_set_opaque(struct dentry *dentry);
  185. bool ovl_dentry_has_upper_alias(struct dentry *dentry);
  186. void ovl_dentry_set_upper_alias(struct dentry *dentry);
  187. bool ovl_redirect_dir(struct super_block *sb);
  188. const char *ovl_dentry_get_redirect(struct dentry *dentry);
  189. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
  190. void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
  191. struct dentry *lowerdentry);
  192. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
  193. void ovl_dentry_version_inc(struct dentry *dentry, bool impurity);
  194. u64 ovl_dentry_version_get(struct dentry *dentry);
  195. bool ovl_is_whiteout(struct dentry *dentry);
  196. struct file *ovl_path_open(struct path *path, int flags);
  197. int ovl_copy_up_start(struct dentry *dentry);
  198. void ovl_copy_up_end(struct dentry *dentry);
  199. bool ovl_check_dir_xattr(struct dentry *dentry, const char *name);
  200. int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
  201. const char *name, const void *value, size_t size,
  202. int xerr);
  203. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
  204. void ovl_set_flag(unsigned long flag, struct inode *inode);
  205. void ovl_clear_flag(unsigned long flag, struct inode *inode);
  206. bool ovl_test_flag(unsigned long flag, struct inode *inode);
  207. bool ovl_inuse_trylock(struct dentry *dentry);
  208. void ovl_inuse_unlock(struct dentry *dentry);
  209. int ovl_nlink_start(struct dentry *dentry, bool *locked);
  210. void ovl_nlink_end(struct dentry *dentry, bool locked);
  211. static inline bool ovl_is_impuredir(struct dentry *dentry)
  212. {
  213. return ovl_check_dir_xattr(dentry, OVL_XATTR_IMPURE);
  214. }
  215. /* namei.c */
  216. int ovl_verify_origin(struct dentry *dentry, struct vfsmount *mnt,
  217. struct dentry *origin, bool is_upper, bool set);
  218. int ovl_verify_index(struct dentry *index, struct path *lowerstack,
  219. unsigned int numlower);
  220. int ovl_get_index_name(struct dentry *origin, struct qstr *name);
  221. int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
  222. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags);
  223. bool ovl_lower_positive(struct dentry *dentry);
  224. /* readdir.c */
  225. extern const struct file_operations ovl_dir_operations;
  226. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
  227. void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
  228. void ovl_cache_free(struct list_head *list);
  229. void ovl_dir_cache_free(struct inode *inode);
  230. int ovl_check_d_type_supported(struct path *realpath);
  231. void ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
  232. struct dentry *dentry, int level);
  233. int ovl_indexdir_cleanup(struct dentry *dentry, struct vfsmount *mnt,
  234. struct path *lowerstack, unsigned int numlower);
  235. /* inode.c */
  236. int ovl_set_nlink_upper(struct dentry *dentry);
  237. int ovl_set_nlink_lower(struct dentry *dentry);
  238. unsigned int ovl_get_nlink(struct dentry *lowerdentry,
  239. struct dentry *upperdentry,
  240. unsigned int fallback);
  241. int ovl_setattr(struct dentry *dentry, struct iattr *attr);
  242. int ovl_getattr(const struct path *path, struct kstat *stat,
  243. u32 request_mask, unsigned int flags);
  244. int ovl_permission(struct inode *inode, int mask);
  245. int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
  246. const void *value, size_t size, int flags);
  247. int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
  248. void *value, size_t size);
  249. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
  250. struct posix_acl *ovl_get_acl(struct inode *inode, int type);
  251. int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags);
  252. int ovl_update_time(struct inode *inode, struct timespec *ts, int flags);
  253. bool ovl_is_private_xattr(const char *name);
  254. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
  255. struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry);
  256. static inline void ovl_copyattr(struct inode *from, struct inode *to)
  257. {
  258. to->i_uid = from->i_uid;
  259. to->i_gid = from->i_gid;
  260. to->i_mode = from->i_mode;
  261. to->i_atime = from->i_atime;
  262. to->i_mtime = from->i_mtime;
  263. to->i_ctime = from->i_ctime;
  264. }
  265. /* dir.c */
  266. extern const struct inode_operations ovl_dir_inode_operations;
  267. struct dentry *ovl_lookup_temp(struct dentry *workdir);
  268. struct cattr {
  269. dev_t rdev;
  270. umode_t mode;
  271. const char *link;
  272. };
  273. int ovl_create_real(struct inode *dir, struct dentry *newdentry,
  274. struct cattr *attr,
  275. struct dentry *hardlink, bool debug);
  276. int ovl_cleanup(struct inode *dir, struct dentry *dentry);
  277. /* copy_up.c */
  278. int ovl_copy_up(struct dentry *dentry);
  279. int ovl_copy_up_flags(struct dentry *dentry, int flags);
  280. int ovl_copy_xattr(struct dentry *old, struct dentry *new);
  281. int ovl_set_attr(struct dentry *upper, struct kstat *stat);
  282. struct ovl_fh *ovl_encode_fh(struct dentry *lower, bool is_upper);