overlayfs.h 31 KB

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