overlayfs.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. *
  4. * Copyright (C) 2011 Novell Inc.
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/uuid.h>
  8. #include <linux/fs.h>
  9. #include <linux/fsverity.h>
  10. #include <linux/namei.h>
  11. #include <linux/posix_acl.h>
  12. #include <linux/posix_acl_xattr.h>
  13. #include "ovl_entry.h"
  14. #undef pr_fmt
  15. #define pr_fmt(fmt) "overlayfs: " fmt
  16. enum ovl_path_type {
  17. __OVL_PATH_UPPER = (1 << 0),
  18. __OVL_PATH_MERGE = (1 << 1),
  19. __OVL_PATH_ORIGIN = (1 << 2),
  20. };
  21. #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
  22. #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
  23. #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
  24. #define OVL_XATTR_NAMESPACE "overlay."
  25. #define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE
  26. #define OVL_XATTR_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1)
  27. #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE
  28. #define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1)
  29. #define OVL_XATTR_ESCAPE_PREFIX OVL_XATTR_NAMESPACE
  30. #define OVL_XATTR_ESCAPE_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_PREFIX) - 1)
  31. #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ESCAPE_PREFIX
  32. #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_TRUSTED_PREFIX) - 1)
  33. #define OVL_XATTR_ESCAPE_USER_PREFIX OVL_XATTR_USER_PREFIX OVL_XATTR_ESCAPE_PREFIX
  34. #define OVL_XATTR_ESCAPE_USER_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_USER_PREFIX) - 1)
  35. enum ovl_xattr {
  36. OVL_XATTR_OPAQUE,
  37. OVL_XATTR_REDIRECT,
  38. OVL_XATTR_ORIGIN,
  39. OVL_XATTR_IMPURE,
  40. OVL_XATTR_NLINK,
  41. OVL_XATTR_UPPER,
  42. OVL_XATTR_UUID,
  43. OVL_XATTR_METACOPY,
  44. OVL_XATTR_PROTATTR,
  45. OVL_XATTR_XWHITEOUT,
  46. };
  47. enum ovl_inode_flag {
  48. /* Pure upper dir that may contain non pure upper entries */
  49. OVL_IMPURE,
  50. /* Non-merge dir that may contain whiteout entries */
  51. OVL_WHITEOUTS,
  52. OVL_INDEX,
  53. OVL_UPPERDATA,
  54. /* Inode number will remain constant over copy up. */
  55. OVL_CONST_INO,
  56. OVL_HAS_DIGEST,
  57. OVL_VERIFIED_DIGEST,
  58. };
  59. enum ovl_entry_flag {
  60. OVL_E_UPPER_ALIAS,
  61. OVL_E_OPAQUE,
  62. OVL_E_CONNECTED,
  63. /* Lower stack may contain xwhiteout entries */
  64. OVL_E_XWHITEOUTS,
  65. };
  66. enum {
  67. OVL_REDIRECT_OFF, /* "off" mode is never used. In effect */
  68. OVL_REDIRECT_FOLLOW, /* ...it translates to either "follow" */
  69. OVL_REDIRECT_NOFOLLOW, /* ...or "nofollow". */
  70. OVL_REDIRECT_ON,
  71. };
  72. enum {
  73. OVL_UUID_OFF,
  74. OVL_UUID_NULL,
  75. OVL_UUID_AUTO,
  76. OVL_UUID_ON,
  77. };
  78. enum {
  79. OVL_XINO_OFF,
  80. OVL_XINO_AUTO,
  81. OVL_XINO_ON,
  82. };
  83. enum {
  84. OVL_VERITY_OFF,
  85. OVL_VERITY_ON,
  86. OVL_VERITY_REQUIRE,
  87. };
  88. /*
  89. * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
  90. * where:
  91. * origin.fh - exported file handle of the lower file
  92. * origin.uuid - uuid of the lower filesystem
  93. */
  94. #define OVL_FH_VERSION 0
  95. #define OVL_FH_MAGIC 0xfb
  96. /* CPU byte order required for fid decoding: */
  97. #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
  98. #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
  99. /* Is the real inode encoded in fid an upper inode? */
  100. #define OVL_FH_FLAG_PATH_UPPER (1 << 2)
  101. #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
  102. OVL_FH_FLAG_PATH_UPPER)
  103. #if defined(__LITTLE_ENDIAN)
  104. #define OVL_FH_FLAG_CPU_ENDIAN 0
  105. #elif defined(__BIG_ENDIAN)
  106. #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
  107. #else
  108. #error Endianness not defined
  109. #endif
  110. /* The type used to be returned by overlay exportfs for misaligned fid */
  111. #define OVL_FILEID_V0 0xfb
  112. /* The type returned by overlay exportfs for 32bit aligned fid */
  113. #define OVL_FILEID_V1 0xf8
  114. /* On-disk format for "origin" file handle */
  115. struct ovl_fb {
  116. u8 version; /* 0 */
  117. u8 magic; /* 0xfb */
  118. u8 len; /* size of this header + size of fid */
  119. u8 flags; /* OVL_FH_FLAG_* */
  120. u8 type; /* fid_type of fid */
  121. uuid_t uuid; /* uuid of filesystem */
  122. u32 fid[]; /* file identifier should be 32bit aligned in-memory */
  123. } __packed;
  124. /* In-memory and on-wire format for overlay file handle */
  125. struct ovl_fh {
  126. u8 padding[3]; /* make sure fb.fid is 32bit aligned */
  127. union {
  128. struct ovl_fb fb;
  129. DECLARE_FLEX_ARRAY(u8, buf);
  130. };
  131. } __packed;
  132. #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb)
  133. #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len)
  134. #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \
  135. offsetof(struct ovl_fb, fid))
  136. /* On-disk format for "metacopy" xattr (if non-zero size) */
  137. struct ovl_metacopy {
  138. u8 version; /* 0 */
  139. u8 len; /* size of this header + used digest bytes */
  140. u8 flags;
  141. u8 digest_algo; /* FS_VERITY_HASH_ALG_* constant, 0 for no digest */
  142. u8 digest[FS_VERITY_MAX_DIGEST_SIZE]; /* Only the used part on disk */
  143. } __packed;
  144. #define OVL_METACOPY_MAX_SIZE (sizeof(struct ovl_metacopy))
  145. #define OVL_METACOPY_MIN_SIZE (OVL_METACOPY_MAX_SIZE - FS_VERITY_MAX_DIGEST_SIZE)
  146. #define OVL_METACOPY_INIT { 0, OVL_METACOPY_MIN_SIZE }
  147. static inline int ovl_metadata_digest_size(const struct ovl_metacopy *metacopy)
  148. {
  149. if (metacopy->len < OVL_METACOPY_MIN_SIZE)
  150. return 0;
  151. return (int)metacopy->len - OVL_METACOPY_MIN_SIZE;
  152. }
  153. /* No atime modification on underlying */
  154. #define OVL_OPEN_FLAGS (O_NOATIME)
  155. extern const char *const ovl_xattr_table[][2];
  156. static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
  157. {
  158. return ovl_xattr_table[ox][ofs->config.userxattr];
  159. }
  160. /*
  161. * When changing ownership of an upper object map the intended ownership
  162. * according to the upper layer's idmapping. When an upper mount idmaps files
  163. * that are stored on-disk as owned by id 1001 to id 1000 this means stat on
  164. * this object will report it as being owned by id 1000 when calling stat via
  165. * the upper mount.
  166. * In order to change ownership of an object so stat reports id 1000 when
  167. * called on an idmapped upper mount the value written to disk - i.e., the
  168. * value stored in ia_*id - must 1001. The mount mapping helper will thus take
  169. * care to map 1000 to 1001.
  170. * The mnt idmapping helpers are nops if the upper layer isn't idmapped.
  171. */
  172. static inline int ovl_do_notify_change(struct ovl_fs *ofs,
  173. struct dentry *upperdentry,
  174. struct iattr *attr)
  175. {
  176. return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL);
  177. }
  178. static inline int ovl_do_rmdir(struct ovl_fs *ofs,
  179. struct inode *dir, struct dentry *dentry)
  180. {
  181. int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry);
  182. pr_debug("rmdir(%pd2) = %i\n", dentry, err);
  183. return err;
  184. }
  185. static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir,
  186. struct dentry *dentry)
  187. {
  188. int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL);
  189. pr_debug("unlink(%pd2) = %i\n", dentry, err);
  190. return err;
  191. }
  192. static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry,
  193. struct inode *dir, struct dentry *new_dentry)
  194. {
  195. int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir,
  196. new_dentry, NULL);
  197. pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
  198. return err;
  199. }
  200. static inline int ovl_do_create(struct ovl_fs *ofs,
  201. struct inode *dir, struct dentry *dentry,
  202. umode_t mode)
  203. {
  204. int err = vfs_create(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, true);
  205. pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
  206. return err;
  207. }
  208. static inline int ovl_do_mkdir(struct ovl_fs *ofs,
  209. struct inode *dir, struct dentry *dentry,
  210. umode_t mode)
  211. {
  212. int err = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode);
  213. pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
  214. return err;
  215. }
  216. static inline int ovl_do_mknod(struct ovl_fs *ofs,
  217. struct inode *dir, struct dentry *dentry,
  218. umode_t mode, dev_t dev)
  219. {
  220. int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev);
  221. pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
  222. return err;
  223. }
  224. static inline int ovl_do_symlink(struct ovl_fs *ofs,
  225. struct inode *dir, struct dentry *dentry,
  226. const char *oldname)
  227. {
  228. int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname);
  229. pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
  230. return err;
  231. }
  232. static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name,
  233. void *value, size_t size)
  234. {
  235. int err, len;
  236. WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb);
  237. err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry,
  238. name, value, size);
  239. len = (value && err > 0) ? err : 0;
  240. pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
  241. path->dentry, name, min(len, 48), value, size, err);
  242. return err;
  243. }
  244. static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs,
  245. struct dentry *upperdentry,
  246. enum ovl_xattr ox, void *value,
  247. size_t size)
  248. {
  249. struct path upperpath = {
  250. .dentry = upperdentry,
  251. .mnt = ovl_upper_mnt(ofs),
  252. };
  253. return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size);
  254. }
  255. static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs,
  256. const struct path *path,
  257. enum ovl_xattr ox, void *value,
  258. size_t size)
  259. {
  260. return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size);
  261. }
  262. static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
  263. const char *name, const void *value,
  264. size_t size, int flags)
  265. {
  266. int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name,
  267. value, size, flags);
  268. pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n",
  269. dentry, name, min((int)size, 48), value, size, flags, err);
  270. return err;
  271. }
  272. static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
  273. enum ovl_xattr ox, const void *value,
  274. size_t size)
  275. {
  276. return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0);
  277. }
  278. static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
  279. const char *name)
  280. {
  281. int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name);
  282. pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
  283. return err;
  284. }
  285. static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
  286. enum ovl_xattr ox)
  287. {
  288. return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox));
  289. }
  290. static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry,
  291. const char *acl_name, struct posix_acl *acl)
  292. {
  293. return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl);
  294. }
  295. static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry,
  296. const char *acl_name)
  297. {
  298. return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name);
  299. }
  300. static inline int ovl_do_rename(struct ovl_fs *ofs, struct inode *olddir,
  301. struct dentry *olddentry, struct inode *newdir,
  302. struct dentry *newdentry, unsigned int flags)
  303. {
  304. int err;
  305. struct renamedata rd = {
  306. .old_mnt_idmap = ovl_upper_mnt_idmap(ofs),
  307. .old_dir = olddir,
  308. .old_dentry = olddentry,
  309. .new_mnt_idmap = ovl_upper_mnt_idmap(ofs),
  310. .new_dir = newdir,
  311. .new_dentry = newdentry,
  312. .flags = flags,
  313. };
  314. pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
  315. err = vfs_rename(&rd);
  316. if (err) {
  317. pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
  318. olddentry, newdentry, err);
  319. }
  320. return err;
  321. }
  322. static inline int ovl_do_whiteout(struct ovl_fs *ofs,
  323. struct inode *dir, struct dentry *dentry)
  324. {
  325. int err = vfs_whiteout(ovl_upper_mnt_idmap(ofs), dir, dentry);
  326. pr_debug("whiteout(%pd2) = %i\n", dentry, err);
  327. return err;
  328. }
  329. static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs,
  330. struct dentry *dentry, umode_t mode)
  331. {
  332. struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry };
  333. struct file *file = kernel_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path,
  334. mode, O_LARGEFILE | O_WRONLY,
  335. current_cred());
  336. int err = PTR_ERR_OR_ZERO(file);
  337. pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
  338. return file;
  339. }
  340. static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs,
  341. const char *name,
  342. struct dentry *base, int len)
  343. {
  344. return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len);
  345. }
  346. static inline bool ovl_open_flags_need_copy_up(int flags)
  347. {
  348. if (!flags)
  349. return false;
  350. return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
  351. }
  352. static inline int ovl_do_getattr(const struct path *path, struct kstat *stat,
  353. u32 request_mask, unsigned int flags)
  354. {
  355. if (flags & AT_GETATTR_NOSEC)
  356. return vfs_getattr_nosec(path, stat, request_mask, flags);
  357. return vfs_getattr(path, stat, request_mask, flags);
  358. }
  359. /* util.c */
  360. int ovl_get_write_access(struct dentry *dentry);
  361. void ovl_put_write_access(struct dentry *dentry);
  362. void ovl_start_write(struct dentry *dentry);
  363. void ovl_end_write(struct dentry *dentry);
  364. int ovl_want_write(struct dentry *dentry);
  365. void ovl_drop_write(struct dentry *dentry);
  366. struct dentry *ovl_workdir(struct dentry *dentry);
  367. const struct cred *ovl_override_creds(struct super_block *sb);
  368. static inline const struct cred *ovl_creds(struct super_block *sb)
  369. {
  370. return OVL_FS(sb)->creator_cred;
  371. }
  372. int ovl_can_decode_fh(struct super_block *sb);
  373. struct dentry *ovl_indexdir(struct super_block *sb);
  374. bool ovl_index_all(struct super_block *sb);
  375. bool ovl_verify_lower(struct super_block *sb);
  376. struct ovl_path *ovl_stack_alloc(unsigned int n);
  377. void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n);
  378. void ovl_stack_put(struct ovl_path *stack, unsigned int n);
  379. void ovl_stack_free(struct ovl_path *stack, unsigned int n);
  380. struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
  381. void ovl_free_entry(struct ovl_entry *oe);
  382. bool ovl_dentry_remote(struct dentry *dentry);
  383. void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
  384. void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
  385. struct ovl_entry *oe);
  386. void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
  387. struct ovl_entry *oe, unsigned int mask);
  388. bool ovl_dentry_weird(struct dentry *dentry);
  389. enum ovl_path_type ovl_path_type(struct dentry *dentry);
  390. void ovl_path_upper(struct dentry *dentry, struct path *path);
  391. void ovl_path_lower(struct dentry *dentry, struct path *path);
  392. void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
  393. struct inode *ovl_i_path_real(struct inode *inode, struct path *path);
  394. enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
  395. enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path);
  396. struct dentry *ovl_dentry_upper(struct dentry *dentry);
  397. struct dentry *ovl_dentry_lower(struct dentry *dentry);
  398. struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
  399. int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath);
  400. const struct ovl_layer *ovl_i_layer_lower(struct inode *inode);
  401. const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
  402. struct dentry *ovl_dentry_real(struct dentry *dentry);
  403. struct dentry *ovl_i_dentry_upper(struct inode *inode);
  404. struct inode *ovl_inode_upper(struct inode *inode);
  405. struct inode *ovl_inode_lower(struct inode *inode);
  406. struct inode *ovl_inode_lowerdata(struct inode *inode);
  407. struct inode *ovl_inode_real(struct inode *inode);
  408. struct inode *ovl_inode_realdata(struct inode *inode);
  409. const char *ovl_lowerdata_redirect(struct inode *inode);
  410. struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
  411. void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
  412. void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
  413. void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
  414. bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
  415. bool ovl_dentry_is_opaque(struct dentry *dentry);
  416. bool ovl_dentry_is_whiteout(struct dentry *dentry);
  417. void ovl_dentry_set_opaque(struct dentry *dentry);
  418. bool ovl_dentry_has_xwhiteouts(struct dentry *dentry);
  419. void ovl_dentry_set_xwhiteouts(struct dentry *dentry);
  420. void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs,
  421. const struct ovl_layer *layer);
  422. bool ovl_dentry_has_upper_alias(struct dentry *dentry);
  423. void ovl_dentry_set_upper_alias(struct dentry *dentry);
  424. bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
  425. bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
  426. bool ovl_has_upperdata(struct inode *inode);
  427. void ovl_set_upperdata(struct inode *inode);
  428. const char *ovl_dentry_get_redirect(struct dentry *dentry);
  429. void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
  430. void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
  431. void ovl_dir_modified(struct dentry *dentry, bool impurity);
  432. u64 ovl_inode_version_get(struct inode *inode);
  433. bool ovl_is_whiteout(struct dentry *dentry);
  434. bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path);
  435. struct file *ovl_path_open(const struct path *path, int flags);
  436. int ovl_copy_up_start(struct dentry *dentry, int flags);
  437. void ovl_copy_up_end(struct dentry *dentry);
  438. bool ovl_already_copied_up(struct dentry *dentry, int flags);
  439. char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path,
  440. enum ovl_xattr ox);
  441. bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path);
  442. bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path);
  443. bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
  444. const struct path *upperpath);
  445. static inline bool ovl_upper_is_whiteout(struct ovl_fs *ofs,
  446. struct dentry *upperdentry)
  447. {
  448. struct path upperpath = {
  449. .dentry = upperdentry,
  450. .mnt = ovl_upper_mnt(ofs),
  451. };
  452. return ovl_path_is_whiteout(ofs, &upperpath);
  453. }
  454. static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
  455. struct dentry *upperdentry)
  456. {
  457. struct path upperpath = {
  458. .dentry = upperdentry,
  459. .mnt = ovl_upper_mnt(ofs),
  460. };
  461. return ovl_path_check_origin_xattr(ofs, &upperpath);
  462. }
  463. int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
  464. enum ovl_xattr ox, const void *value, size_t size,
  465. int xerr);
  466. int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
  467. bool ovl_inuse_trylock(struct dentry *dentry);
  468. void ovl_inuse_unlock(struct dentry *dentry);
  469. bool ovl_is_inuse(struct dentry *dentry);
  470. bool ovl_need_index(struct dentry *dentry);
  471. int ovl_nlink_start(struct dentry *dentry);
  472. void ovl_nlink_end(struct dentry *dentry);
  473. int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
  474. int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
  475. struct ovl_metacopy *data);
  476. int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d,
  477. struct ovl_metacopy *metacopy);
  478. bool ovl_is_metacopy_dentry(struct dentry *dentry);
  479. char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding);
  480. int ovl_ensure_verity_loaded(struct path *path);
  481. int ovl_validate_verity(struct ovl_fs *ofs,
  482. struct path *metapath,
  483. struct path *datapath);
  484. int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
  485. struct ovl_metacopy *metacopy);
  486. int ovl_sync_status(struct ovl_fs *ofs);
  487. static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
  488. {
  489. set_bit(flag, &OVL_I(inode)->flags);
  490. }
  491. static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
  492. {
  493. clear_bit(flag, &OVL_I(inode)->flags);
  494. }
  495. static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
  496. {
  497. return test_bit(flag, &OVL_I(inode)->flags);
  498. }
  499. static inline bool ovl_is_impuredir(struct super_block *sb,
  500. struct dentry *upperdentry)
  501. {
  502. struct ovl_fs *ofs = OVL_FS(sb);
  503. struct path upperpath = {
  504. .dentry = upperdentry,
  505. .mnt = ovl_upper_mnt(ofs),
  506. };
  507. return ovl_get_dir_xattr_val(ofs, &upperpath, OVL_XATTR_IMPURE) == 'y';
  508. }
  509. static inline char ovl_get_opaquedir_val(struct ovl_fs *ofs,
  510. const struct path *path)
  511. {
  512. return ovl_get_dir_xattr_val(ofs, path, OVL_XATTR_OPAQUE);
  513. }
  514. static inline bool ovl_redirect_follow(struct ovl_fs *ofs)
  515. {
  516. return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW;
  517. }
  518. static inline bool ovl_redirect_dir(struct ovl_fs *ofs)
  519. {
  520. return ofs->config.redirect_mode == OVL_REDIRECT_ON;
  521. }
  522. static inline bool ovl_origin_uuid(struct ovl_fs *ofs)
  523. {
  524. return ofs->config.uuid != OVL_UUID_OFF;
  525. }
  526. static inline bool ovl_has_fsid(struct ovl_fs *ofs)
  527. {
  528. return ofs->config.uuid == OVL_UUID_ON ||
  529. ofs->config.uuid == OVL_UUID_AUTO;
  530. }
  531. /*
  532. * With xino=auto, we do best effort to keep all inodes on same st_dev and
  533. * d_ino consistent with st_ino.
  534. * With xino=on, we do the same effort but we warn if we failed.
  535. */
  536. static inline bool ovl_xino_warn(struct ovl_fs *ofs)
  537. {
  538. return ofs->config.xino == OVL_XINO_ON;
  539. }
  540. /*
  541. * To avoid regressions in existing setups with overlay lower offline changes,
  542. * we allow lower changes only if none of the new features are used.
  543. */
  544. static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs)
  545. {
  546. return (!ofs->config.index && !ofs->config.metacopy &&
  547. !ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs));
  548. }
  549. /* All layers on same fs? */
  550. static inline bool ovl_same_fs(struct ovl_fs *ofs)
  551. {
  552. return ofs->xino_mode == 0;
  553. }
  554. /* All overlay inodes have same st_dev? */
  555. static inline bool ovl_same_dev(struct ovl_fs *ofs)
  556. {
  557. return ofs->xino_mode >= 0;
  558. }
  559. static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs)
  560. {
  561. return ovl_same_dev(ofs) ? ofs->xino_mode : 0;
  562. }
  563. static inline void ovl_inode_lock(struct inode *inode)
  564. {
  565. mutex_lock(&OVL_I(inode)->lock);
  566. }
  567. static inline int ovl_inode_lock_interruptible(struct inode *inode)
  568. {
  569. return mutex_lock_interruptible(&OVL_I(inode)->lock);
  570. }
  571. static inline void ovl_inode_unlock(struct inode *inode)
  572. {
  573. mutex_unlock(&OVL_I(inode)->lock);
  574. }
  575. /* namei.c */
  576. int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
  577. static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
  578. {
  579. if (fh_len < sizeof(struct ovl_fh))
  580. return -EINVAL;
  581. return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
  582. }
  583. struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
  584. struct vfsmount *mnt, bool connected);
  585. int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
  586. struct dentry *upperdentry, struct ovl_path **stackp);
  587. int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
  588. enum ovl_xattr ox, const struct ovl_fh *fh,
  589. bool is_upper, bool set);
  590. int ovl_verify_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry,
  591. enum ovl_xattr ox, struct dentry *real,
  592. bool is_upper, bool set);
  593. struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index,
  594. bool connected);
  595. int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
  596. int ovl_get_index_name_fh(const struct ovl_fh *fh, struct qstr *name);
  597. int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
  598. struct qstr *name);
  599. struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
  600. struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
  601. struct dentry *origin, bool verify);
  602. int ovl_path_next(int idx, struct dentry *dentry, struct path *path,
  603. const struct ovl_layer **layer);
  604. int ovl_verify_lowerdata(struct dentry *dentry);
  605. struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
  606. unsigned int flags);
  607. bool ovl_lower_positive(struct dentry *dentry);
  608. static inline int ovl_verify_origin_fh(struct ovl_fs *ofs, struct dentry *upper,
  609. const struct ovl_fh *fh, bool set)
  610. {
  611. return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, fh, false, set);
  612. }
  613. static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
  614. struct dentry *origin, bool set)
  615. {
  616. return ovl_verify_origin_xattr(ofs, upper, OVL_XATTR_ORIGIN, origin,
  617. false, set);
  618. }
  619. static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
  620. struct dentry *upper, bool set)
  621. {
  622. return ovl_verify_origin_xattr(ofs, index, OVL_XATTR_UPPER, upper,
  623. true, set);
  624. }
  625. /* readdir.c */
  626. extern const struct file_operations ovl_dir_operations;
  627. struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
  628. int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
  629. void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
  630. struct list_head *list);
  631. void ovl_cache_free(struct list_head *list);
  632. void ovl_dir_cache_free(struct inode *inode);
  633. int ovl_check_d_type_supported(const struct path *realpath);
  634. int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
  635. struct vfsmount *mnt, struct dentry *dentry, int level);
  636. int ovl_indexdir_cleanup(struct ovl_fs *ofs);
  637. /*
  638. * Can we iterate real dir directly?
  639. *
  640. * Non-merge dir may contain whiteouts from a time it was a merge upper, before
  641. * lower dir was removed under it and possibly before it was rotated from upper
  642. * to lower layer.
  643. */
  644. static inline bool ovl_dir_is_real(struct inode *dir)
  645. {
  646. return !ovl_test_flag(OVL_WHITEOUTS, dir);
  647. }
  648. /* inode.c */
  649. int ovl_set_nlink_upper(struct dentry *dentry);
  650. int ovl_set_nlink_lower(struct dentry *dentry);
  651. unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
  652. struct dentry *upperdentry,
  653. unsigned int fallback);
  654. int ovl_permission(struct mnt_idmap *idmap, struct inode *inode,
  655. int mask);
  656. #ifdef CONFIG_FS_POSIX_ACL
  657. struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
  658. struct inode *inode, int type,
  659. bool rcu, bool noperm);
  660. static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type,
  661. bool rcu)
  662. {
  663. return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true);
  664. }
  665. static inline struct posix_acl *ovl_get_acl(struct mnt_idmap *idmap,
  666. struct dentry *dentry, int type)
  667. {
  668. return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false);
  669. }
  670. int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
  671. struct posix_acl *acl, int type);
  672. struct posix_acl *ovl_get_acl_path(const struct path *path,
  673. const char *acl_name, bool noperm);
  674. #else
  675. #define ovl_get_inode_acl NULL
  676. #define ovl_get_acl NULL
  677. #define ovl_set_acl NULL
  678. static inline struct posix_acl *ovl_get_acl_path(const struct path *path,
  679. const char *acl_name,
  680. bool noperm)
  681. {
  682. return NULL;
  683. }
  684. #endif
  685. int ovl_update_time(struct inode *inode, int flags);
  686. bool ovl_is_private_xattr(struct super_block *sb, const char *name);
  687. struct ovl_inode_params {
  688. struct inode *newinode;
  689. struct dentry *upperdentry;
  690. struct ovl_entry *oe;
  691. bool index;
  692. char *redirect;
  693. char *lowerdata_redirect;
  694. };
  695. void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
  696. unsigned long ino, int fsid);
  697. struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
  698. struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
  699. bool is_upper);
  700. bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
  701. struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
  702. struct inode *ovl_get_inode(struct super_block *sb,
  703. struct ovl_inode_params *oip);
  704. void ovl_copyattr(struct inode *to);
  705. /* vfs inode flags copied from real to ovl inode */
  706. #define OVL_COPY_I_FLAGS_MASK (S_SYNC | S_NOATIME | S_APPEND | S_IMMUTABLE)
  707. /* vfs inode flags read from overlay.protattr xattr to ovl inode */
  708. #define OVL_PROT_I_FLAGS_MASK (S_APPEND | S_IMMUTABLE)
  709. /*
  710. * fileattr flags copied from lower to upper inode on copy up.
  711. * We cannot copy up immutable/append-only flags, because that would prevent
  712. * linking temp inode to upper dir, so we store them in xattr instead.
  713. */
  714. #define OVL_COPY_FS_FLAGS_MASK (FS_SYNC_FL | FS_NOATIME_FL)
  715. #define OVL_COPY_FSX_FLAGS_MASK (FS_XFLAG_SYNC | FS_XFLAG_NOATIME)
  716. #define OVL_PROT_FS_FLAGS_MASK (FS_APPEND_FL | FS_IMMUTABLE_FL)
  717. #define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE)
  718. void ovl_check_protattr(struct inode *inode, struct dentry *upper);
  719. int ovl_set_protattr(struct inode *inode, struct dentry *upper,
  720. struct fileattr *fa);
  721. static inline void ovl_copyflags(struct inode *from, struct inode *to)
  722. {
  723. unsigned int mask = OVL_COPY_I_FLAGS_MASK;
  724. inode_set_flags(to, from->i_flags & mask, mask);
  725. }
  726. /* dir.c */
  727. extern const struct inode_operations ovl_dir_inode_operations;
  728. int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
  729. struct dentry *dentry);
  730. struct ovl_cattr {
  731. dev_t rdev;
  732. umode_t mode;
  733. const char *link;
  734. struct dentry *hardlink;
  735. };
  736. #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
  737. int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
  738. struct dentry **newdentry, umode_t mode);
  739. struct dentry *ovl_create_real(struct ovl_fs *ofs,
  740. struct inode *dir, struct dentry *newdentry,
  741. struct ovl_cattr *attr);
  742. int ovl_cleanup(struct ovl_fs *ofs, struct inode *dir, struct dentry *dentry);
  743. struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir);
  744. struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
  745. struct ovl_cattr *attr);
  746. /* file.c */
  747. extern const struct file_operations ovl_file_operations;
  748. int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa);
  749. int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa);
  750. int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa);
  751. int ovl_fileattr_set(struct mnt_idmap *idmap,
  752. struct dentry *dentry, struct fileattr *fa);
  753. /* copy_up.c */
  754. int ovl_copy_up(struct dentry *dentry);
  755. int ovl_copy_up_with_data(struct dentry *dentry);
  756. int ovl_maybe_copy_up(struct dentry *dentry, int flags);
  757. int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new);
  758. int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat);
  759. struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct inode *realinode,
  760. bool is_upper);
  761. struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin);
  762. int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh,
  763. struct dentry *upper);
  764. /* export.c */
  765. extern const struct export_operations ovl_export_operations;
  766. extern const struct export_operations ovl_export_fid_operations;
  767. /* super.c */
  768. int ovl_fill_super(struct super_block *sb, struct fs_context *fc);
  769. /* Will this overlay be forced to mount/remount ro? */
  770. static inline bool ovl_force_readonly(struct ovl_fs *ofs)
  771. {
  772. return (!ovl_upper_mnt(ofs) || !ofs->workdir);
  773. }
  774. /* xattr.c */
  775. const struct xattr_handler * const *ovl_xattr_handlers(struct ovl_fs *ofs);
  776. int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  777. struct iattr *attr);
  778. int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
  779. struct kstat *stat, u32 request_mask, unsigned int flags);
  780. ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);