xref: /linux/fs/overlayfs/overlayfs.h (revision 184996e92e86c4a4224dc4aaee75b2ccd04b6e78)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2e9be9d5eSMiklos Szeredi /*
3e9be9d5eSMiklos Szeredi  *
4e9be9d5eSMiklos Szeredi  * Copyright (C) 2011 Novell Inc.
5e9be9d5eSMiklos Szeredi  */
6e9be9d5eSMiklos Szeredi 
7e9be9d5eSMiklos Szeredi #include <linux/kernel.h>
83a1e819bSAmir Goldstein #include <linux/uuid.h>
946e5d0a3SMiklos Szeredi #include <linux/fs.h>
10bf070890SAlexander Larsson #include <linux/fsverity.h>
1122f289ceSChristian Brauner #include <linux/namei.h>
120e641857SChristian Brauner #include <linux/posix_acl.h>
130e641857SChristian Brauner #include <linux/posix_acl_xattr.h>
14ee023c30SAmir Goldstein #include "ovl_entry.h"
15e9be9d5eSMiklos Szeredi 
161bd0a3aeSlijiazi #undef pr_fmt
171bd0a3aeSlijiazi #define pr_fmt(fmt) "overlayfs: " fmt
181bd0a3aeSlijiazi 
19e9be9d5eSMiklos Szeredi enum ovl_path_type {
2038e813dbSMiklos Szeredi 	__OVL_PATH_UPPER	= (1 << 0),
2138e813dbSMiklos Szeredi 	__OVL_PATH_MERGE	= (1 << 1),
2259548503SAmir Goldstein 	__OVL_PATH_ORIGIN	= (1 << 2),
23e9be9d5eSMiklos Szeredi };
24e9be9d5eSMiklos Szeredi 
251afaba1eSMiklos Szeredi #define OVL_TYPE_UPPER(type)	((type) & __OVL_PATH_UPPER)
261afaba1eSMiklos Szeredi #define OVL_TYPE_MERGE(type)	((type) & __OVL_PATH_MERGE)
2759548503SAmir Goldstein #define OVL_TYPE_ORIGIN(type)	((type) & __OVL_PATH_ORIGIN)
28d837a49bSMiklos Szeredi 
292d2f2d73SMiklos Szeredi #define OVL_XATTR_NAMESPACE "overlay."
302d2f2d73SMiklos Szeredi #define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE
312d2f2d73SMiklos Szeredi #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE
3243d193f8SMiklos Szeredi 
3343d193f8SMiklos Szeredi enum ovl_xattr {
3443d193f8SMiklos Szeredi 	OVL_XATTR_OPAQUE,
3543d193f8SMiklos Szeredi 	OVL_XATTR_REDIRECT,
3643d193f8SMiklos Szeredi 	OVL_XATTR_ORIGIN,
3743d193f8SMiklos Szeredi 	OVL_XATTR_IMPURE,
3843d193f8SMiklos Szeredi 	OVL_XATTR_NLINK,
3943d193f8SMiklos Szeredi 	OVL_XATTR_UPPER,
4043d193f8SMiklos Szeredi 	OVL_XATTR_METACOPY,
41096a218aSAmir Goldstein 	OVL_XATTR_PROTATTR,
4243d193f8SMiklos Szeredi };
433a1e819bSAmir Goldstein 
44c62520a8SAmir Goldstein enum ovl_inode_flag {
45b79e05aaSAmir Goldstein 	/* Pure upper dir that may contain non pure upper entries */
4613c72075SMiklos Szeredi 	OVL_IMPURE,
47b79e05aaSAmir Goldstein 	/* Non-merge dir that may contain whiteout entries */
48b79e05aaSAmir Goldstein 	OVL_WHITEOUTS,
49359f392cSAmir Goldstein 	OVL_INDEX,
500c288874SVivek Goyal 	OVL_UPPERDATA,
51a00c2d59SVivek Goyal 	/* Inode number will remain constant over copy up. */
52a00c2d59SVivek Goyal 	OVL_CONST_INO,
53*184996e9SAlexander Larsson 	OVL_HAS_DIGEST,
54*184996e9SAlexander Larsson 	OVL_VERIFIED_DIGEST,
5513c72075SMiklos Szeredi };
5613c72075SMiklos Szeredi 
57c62520a8SAmir Goldstein enum ovl_entry_flag {
58c62520a8SAmir Goldstein 	OVL_E_UPPER_ALIAS,
59c62520a8SAmir Goldstein 	OVL_E_OPAQUE,
602ca3c148SAmir Goldstein 	OVL_E_CONNECTED,
61c62520a8SAmir Goldstein };
62c62520a8SAmir Goldstein 
63926e94d7SAmir Goldstein enum {
64af5f2396SAmir Goldstein 	OVL_REDIRECT_OFF,	/* "off" mode is never used. In effect	*/
65af5f2396SAmir Goldstein 	OVL_REDIRECT_FOLLOW,	/* ...it translates to either "follow"	*/
66af5f2396SAmir Goldstein 	OVL_REDIRECT_NOFOLLOW,	/* ...or "nofollow".			*/
67af5f2396SAmir Goldstein 	OVL_REDIRECT_ON,
68af5f2396SAmir Goldstein };
69af5f2396SAmir Goldstein 
70af5f2396SAmir Goldstein enum {
71926e94d7SAmir Goldstein 	OVL_XINO_OFF,
72926e94d7SAmir Goldstein 	OVL_XINO_AUTO,
73926e94d7SAmir Goldstein 	OVL_XINO_ON,
74926e94d7SAmir Goldstein };
75926e94d7SAmir Goldstein 
76ae8cba40SAlexander Larsson enum {
77ae8cba40SAlexander Larsson 	OVL_VERITY_OFF,
78ae8cba40SAlexander Larsson 	OVL_VERITY_ON,
79ae8cba40SAlexander Larsson 	OVL_VERITY_REQUIRE,
80ae8cba40SAlexander Larsson };
81ae8cba40SAlexander Larsson 
823a1e819bSAmir Goldstein /*
833a1e819bSAmir Goldstein  * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
843a1e819bSAmir Goldstein  * where:
853a1e819bSAmir Goldstein  * origin.fh	- exported file handle of the lower file
863a1e819bSAmir Goldstein  * origin.uuid	- uuid of the lower filesystem
873a1e819bSAmir Goldstein  */
883a1e819bSAmir Goldstein #define OVL_FH_VERSION	0
893a1e819bSAmir Goldstein #define OVL_FH_MAGIC	0xfb
903a1e819bSAmir Goldstein 
913a1e819bSAmir Goldstein /* CPU byte order required for fid decoding:  */
923a1e819bSAmir Goldstein #define OVL_FH_FLAG_BIG_ENDIAN	(1 << 0)
933a1e819bSAmir Goldstein #define OVL_FH_FLAG_ANY_ENDIAN	(1 << 1)
9454fb347eSAmir Goldstein /* Is the real inode encoded in fid an upper inode? */
9554fb347eSAmir Goldstein #define OVL_FH_FLAG_PATH_UPPER	(1 << 2)
963a1e819bSAmir Goldstein 
97961af647SAmir Goldstein #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
98961af647SAmir Goldstein 			 OVL_FH_FLAG_PATH_UPPER)
993a1e819bSAmir Goldstein 
1003a1e819bSAmir Goldstein #if defined(__LITTLE_ENDIAN)
1013a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN 0
1023a1e819bSAmir Goldstein #elif defined(__BIG_ENDIAN)
1033a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
1043a1e819bSAmir Goldstein #else
1053a1e819bSAmir Goldstein #error Endianness not defined
1063a1e819bSAmir Goldstein #endif
1073a1e819bSAmir Goldstein 
108cbe7fba8SAmir Goldstein /* The type used to be returned by overlay exportfs for misaligned fid */
109cbe7fba8SAmir Goldstein #define OVL_FILEID_V0	0xfb
110cbe7fba8SAmir Goldstein /* The type returned by overlay exportfs for 32bit aligned fid */
111cbe7fba8SAmir Goldstein #define OVL_FILEID_V1	0xf8
1128ed5eec9SAmir Goldstein 
113cbe7fba8SAmir Goldstein /* On-disk format for "origin" file handle */
114cbe7fba8SAmir Goldstein struct ovl_fb {
1153a1e819bSAmir Goldstein 	u8 version;	/* 0 */
1163a1e819bSAmir Goldstein 	u8 magic;	/* 0xfb */
1173a1e819bSAmir Goldstein 	u8 len;		/* size of this header + size of fid */
1183a1e819bSAmir Goldstein 	u8 flags;	/* OVL_FH_FLAG_* */
1193a1e819bSAmir Goldstein 	u8 type;	/* fid_type of fid */
12001633fd2SChristoph Hellwig 	uuid_t uuid;	/* uuid of filesystem */
1210efbe7c4SGustavo A. R. Silva 	u32 fid[];	/* file identifier should be 32bit aligned in-memory */
1223a1e819bSAmir Goldstein } __packed;
123e9be9d5eSMiklos Szeredi 
124cbe7fba8SAmir Goldstein /* In-memory and on-wire format for overlay file handle */
125cbe7fba8SAmir Goldstein struct ovl_fh {
126cbe7fba8SAmir Goldstein 	u8 padding[3];	/* make sure fb.fid is 32bit aligned */
127cbe7fba8SAmir Goldstein 	union {
128cbe7fba8SAmir Goldstein 		struct ovl_fb fb;
129cf8aa9bfSKees Cook 		DECLARE_FLEX_ARRAY(u8, buf);
130cbe7fba8SAmir Goldstein 	};
131cbe7fba8SAmir Goldstein } __packed;
132cbe7fba8SAmir Goldstein 
133cbe7fba8SAmir Goldstein #define OVL_FH_WIRE_OFFSET	offsetof(struct ovl_fh, fb)
134cbe7fba8SAmir Goldstein #define OVL_FH_LEN(fh)		(OVL_FH_WIRE_OFFSET + (fh)->fb.len)
135cbe7fba8SAmir Goldstein #define OVL_FH_FID_OFFSET	(OVL_FH_WIRE_OFFSET + \
136cbe7fba8SAmir Goldstein 				 offsetof(struct ovl_fb, fid))
137cbe7fba8SAmir Goldstein 
138bf070890SAlexander Larsson /* On-disk format for "metacopy" xattr (if non-zero size) */
139bf070890SAlexander Larsson struct ovl_metacopy {
140bf070890SAlexander Larsson 	u8 version;	/* 0 */
141bf070890SAlexander Larsson 	u8 len;         /* size of this header + used digest bytes */
142bf070890SAlexander Larsson 	u8 flags;
143bf070890SAlexander Larsson 	u8 digest_algo;	/* FS_VERITY_HASH_ALG_* constant, 0 for no digest */
144bf070890SAlexander Larsson 	u8 digest[FS_VERITY_MAX_DIGEST_SIZE];  /* Only the used part on disk */
145bf070890SAlexander Larsson } __packed;
146bf070890SAlexander Larsson 
147bf070890SAlexander Larsson #define OVL_METACOPY_MAX_SIZE (sizeof(struct ovl_metacopy))
148bf070890SAlexander Larsson #define OVL_METACOPY_MIN_SIZE (OVL_METACOPY_MAX_SIZE - FS_VERITY_MAX_DIGEST_SIZE)
149bf070890SAlexander Larsson #define OVL_METACOPY_INIT { 0, OVL_METACOPY_MIN_SIZE }
150bf070890SAlexander Larsson 
151bf070890SAlexander Larsson static inline int ovl_metadata_digest_size(const struct ovl_metacopy *metacopy)
152bf070890SAlexander Larsson {
153bf070890SAlexander Larsson 	if (metacopy->len < OVL_METACOPY_MIN_SIZE)
154bf070890SAlexander Larsson 		return 0;
155bf070890SAlexander Larsson 	return (int)metacopy->len - OVL_METACOPY_MIN_SIZE;
156bf070890SAlexander Larsson }
157bf070890SAlexander Larsson 
1582d2f2d73SMiklos Szeredi extern const char *const ovl_xattr_table[][2];
15943d193f8SMiklos Szeredi static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
16043d193f8SMiklos Szeredi {
1612d2f2d73SMiklos Szeredi 	return ovl_xattr_table[ox][ofs->config.userxattr];
16243d193f8SMiklos Szeredi }
16343d193f8SMiklos Szeredi 
164a15506eaSChristian Brauner /*
165a15506eaSChristian Brauner  * When changing ownership of an upper object map the intended ownership
166a15506eaSChristian Brauner  * according to the upper layer's idmapping. When an upper mount idmaps files
167a15506eaSChristian Brauner  * that are stored on-disk as owned by id 1001 to id 1000 this means stat on
168a15506eaSChristian Brauner  * this object will report it as being owned by id 1000 when calling stat via
169a15506eaSChristian Brauner  * the upper mount.
170a15506eaSChristian Brauner  * In order to change ownership of an object so stat reports id 1000 when
171a15506eaSChristian Brauner  * called on an idmapped upper mount the value written to disk - i.e., the
172a15506eaSChristian Brauner  * value stored in ia_*id - must 1001. The mount mapping helper will thus take
173a15506eaSChristian Brauner  * care to map 1000 to 1001.
174a15506eaSChristian Brauner  * The mnt idmapping helpers are nops if the upper layer isn't idmapped.
175a15506eaSChristian Brauner  */
176a15506eaSChristian Brauner static inline int ovl_do_notify_change(struct ovl_fs *ofs,
177a15506eaSChristian Brauner 				       struct dentry *upperdentry,
178a15506eaSChristian Brauner 				       struct iattr *attr)
179a15506eaSChristian Brauner {
180abf08576SChristian Brauner 	return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL);
181a15506eaSChristian Brauner }
182a15506eaSChristian Brauner 
183576bb263SChristian Brauner static inline int ovl_do_rmdir(struct ovl_fs *ofs,
184576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry)
185e9be9d5eSMiklos Szeredi {
186abf08576SChristian Brauner 	int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry);
1876cf00764SAmir Goldstein 
188e9be9d5eSMiklos Szeredi 	pr_debug("rmdir(%pd2) = %i\n", dentry, err);
189e9be9d5eSMiklos Szeredi 	return err;
190e9be9d5eSMiklos Szeredi }
191e9be9d5eSMiklos Szeredi 
192576bb263SChristian Brauner static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir,
193576bb263SChristian Brauner 				struct dentry *dentry)
194e9be9d5eSMiklos Szeredi {
195abf08576SChristian Brauner 	int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL);
1966cf00764SAmir Goldstein 
197e9be9d5eSMiklos Szeredi 	pr_debug("unlink(%pd2) = %i\n", dentry, err);
198e9be9d5eSMiklos Szeredi 	return err;
199e9be9d5eSMiklos Szeredi }
200e9be9d5eSMiklos Szeredi 
201576bb263SChristian Brauner static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry,
202576bb263SChristian Brauner 			      struct inode *dir, struct dentry *new_dentry)
203e9be9d5eSMiklos Szeredi {
204abf08576SChristian Brauner 	int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir,
205abf08576SChristian Brauner 			   new_dentry, NULL);
2066cf00764SAmir Goldstein 
2076cf00764SAmir Goldstein 	pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
208e9be9d5eSMiklos Szeredi 	return err;
209e9be9d5eSMiklos Szeredi }
210e9be9d5eSMiklos Szeredi 
211576bb263SChristian Brauner static inline int ovl_do_create(struct ovl_fs *ofs,
212576bb263SChristian Brauner 				struct inode *dir, struct dentry *dentry,
2136cf00764SAmir Goldstein 				umode_t mode)
214e9be9d5eSMiklos Szeredi {
215abf08576SChristian Brauner 	int err = vfs_create(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, true);
2166cf00764SAmir Goldstein 
217e9be9d5eSMiklos Szeredi 	pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
218e9be9d5eSMiklos Szeredi 	return err;
219e9be9d5eSMiklos Szeredi }
220e9be9d5eSMiklos Szeredi 
221576bb263SChristian Brauner static inline int ovl_do_mkdir(struct ovl_fs *ofs,
222576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry,
2236cf00764SAmir Goldstein 			       umode_t mode)
224e9be9d5eSMiklos Szeredi {
225abf08576SChristian Brauner 	int err = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode);
226e9be9d5eSMiklos Szeredi 	pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
227e9be9d5eSMiklos Szeredi 	return err;
228e9be9d5eSMiklos Szeredi }
229e9be9d5eSMiklos Szeredi 
230576bb263SChristian Brauner static inline int ovl_do_mknod(struct ovl_fs *ofs,
231576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry,
2326cf00764SAmir Goldstein 			       umode_t mode, dev_t dev)
233e9be9d5eSMiklos Szeredi {
234abf08576SChristian Brauner 	int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev);
2356cf00764SAmir Goldstein 
2366cf00764SAmir Goldstein 	pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
237e9be9d5eSMiklos Szeredi 	return err;
238e9be9d5eSMiklos Szeredi }
239e9be9d5eSMiklos Szeredi 
240576bb263SChristian Brauner static inline int ovl_do_symlink(struct ovl_fs *ofs,
241576bb263SChristian Brauner 				 struct inode *dir, struct dentry *dentry,
2426cf00764SAmir Goldstein 				 const char *oldname)
243e9be9d5eSMiklos Szeredi {
244abf08576SChristian Brauner 	int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname);
2456cf00764SAmir Goldstein 
246e9be9d5eSMiklos Szeredi 	pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
247e9be9d5eSMiklos Szeredi 	return err;
248e9be9d5eSMiklos Szeredi }
249e9be9d5eSMiklos Szeredi 
2502d343087SAl Viro static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name,
251dad7017aSChristian Brauner 				      void *value, size_t size)
252d5dc7486SMiklos Szeredi {
253dad7017aSChristian Brauner 	int err, len;
254dad7017aSChristian Brauner 
255dad7017aSChristian Brauner 	WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb);
256dad7017aSChristian Brauner 
2574609e1f1SChristian Brauner 	err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry,
258dad7017aSChristian Brauner 			       name, value, size);
259dad7017aSChristian Brauner 	len = (value && err > 0) ? err : 0;
2605e717c6fSAmir Goldstein 
2615e717c6fSAmir Goldstein 	pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
262dad7017aSChristian Brauner 		 path->dentry, name, min(len, 48), value, size, err);
2635e717c6fSAmir Goldstein 	return err;
264d5dc7486SMiklos Szeredi }
265d5dc7486SMiklos Szeredi 
266dad7017aSChristian Brauner static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs,
267dad7017aSChristian Brauner 					 struct dentry *upperdentry,
268c914c0e2SAmir Goldstein 					 enum ovl_xattr ox, void *value,
269610afc0bSMiklos Szeredi 					 size_t size)
270e9be9d5eSMiklos Szeredi {
271dad7017aSChristian Brauner 	struct path upperpath = {
272dad7017aSChristian Brauner 		.dentry = upperdentry,
273dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
274dad7017aSChristian Brauner 	};
275dad7017aSChristian Brauner 
276dad7017aSChristian Brauner 	return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size);
277dad7017aSChristian Brauner }
278dad7017aSChristian Brauner 
279dad7017aSChristian Brauner static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs,
2802d343087SAl Viro 					 const struct path *path,
281dad7017aSChristian Brauner 					 enum ovl_xattr ox, void *value,
282dad7017aSChristian Brauner 					 size_t size)
283dad7017aSChristian Brauner {
284dad7017aSChristian Brauner 	return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size);
285c914c0e2SAmir Goldstein }
286c914c0e2SAmir Goldstein 
287c914c0e2SAmir Goldstein static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
288c914c0e2SAmir Goldstein 				  const char *name, const void *value,
289c914c0e2SAmir Goldstein 				  size_t size, int flags)
290c914c0e2SAmir Goldstein {
2914609e1f1SChristian Brauner 	int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name,
2926344e669SChristian Brauner 			       value, size, flags);
293c914c0e2SAmir Goldstein 
294c914c0e2SAmir Goldstein 	pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n",
295c914c0e2SAmir Goldstein 		 dentry, name, min((int)size, 48), value, size, flags, err);
296e9be9d5eSMiklos Szeredi 	return err;
297e9be9d5eSMiklos Szeredi }
298e9be9d5eSMiklos Szeredi 
299c914c0e2SAmir Goldstein static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
300c914c0e2SAmir Goldstein 			       enum ovl_xattr ox, const void *value,
301c914c0e2SAmir Goldstein 			       size_t size)
302e9be9d5eSMiklos Szeredi {
303c914c0e2SAmir Goldstein 	return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0);
304c914c0e2SAmir Goldstein }
305c914c0e2SAmir Goldstein 
306c914c0e2SAmir Goldstein static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
307c914c0e2SAmir Goldstein 				     const char *name)
308c914c0e2SAmir Goldstein {
3094609e1f1SChristian Brauner 	int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name);
310e9be9d5eSMiklos Szeredi 	pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
311e9be9d5eSMiklos Szeredi 	return err;
312e9be9d5eSMiklos Szeredi }
313e9be9d5eSMiklos Szeredi 
314c914c0e2SAmir Goldstein static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
315c914c0e2SAmir Goldstein 				  enum ovl_xattr ox)
316c914c0e2SAmir Goldstein {
317c914c0e2SAmir Goldstein 	return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox));
318c914c0e2SAmir Goldstein }
319c914c0e2SAmir Goldstein 
3200e641857SChristian Brauner static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry,
3210e641857SChristian Brauner 				 const char *acl_name, struct posix_acl *acl)
3220e641857SChristian Brauner {
32313e83a49SChristian Brauner 	return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl);
3240e641857SChristian Brauner }
3250e641857SChristian Brauner 
3260e641857SChristian Brauner static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry,
3270e641857SChristian Brauner 				    const char *acl_name)
3280e641857SChristian Brauner {
32913e83a49SChristian Brauner 	return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name);
3300e641857SChristian Brauner }
3310e641857SChristian Brauner 
332576bb263SChristian Brauner static inline int ovl_do_rename(struct ovl_fs *ofs, struct inode *olddir,
333576bb263SChristian Brauner 				struct dentry *olddentry, struct inode *newdir,
334576bb263SChristian Brauner 				struct dentry *newdentry, unsigned int flags)
335e9be9d5eSMiklos Szeredi {
336e9be9d5eSMiklos Szeredi 	int err;
3379fe61450SChristian Brauner 	struct renamedata rd = {
338abf08576SChristian Brauner 		.old_mnt_idmap	= ovl_upper_mnt_idmap(ofs),
3399fe61450SChristian Brauner 		.old_dir 	= olddir,
3409fe61450SChristian Brauner 		.old_dentry 	= olddentry,
341abf08576SChristian Brauner 		.new_mnt_idmap	= ovl_upper_mnt_idmap(ofs),
3429fe61450SChristian Brauner 		.new_dir 	= newdir,
3439fe61450SChristian Brauner 		.new_dentry 	= newdentry,
3449fe61450SChristian Brauner 		.flags 		= flags,
3459fe61450SChristian Brauner 	};
346e9be9d5eSMiklos Szeredi 
3476cf00764SAmir Goldstein 	pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
3489fe61450SChristian Brauner 	err = vfs_rename(&rd);
349e9be9d5eSMiklos Szeredi 	if (err) {
3502773bf00SMiklos Szeredi 		pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
351e9be9d5eSMiklos Szeredi 			 olddentry, newdentry, err);
352e9be9d5eSMiklos Szeredi 	}
353e9be9d5eSMiklos Szeredi 	return err;
354e9be9d5eSMiklos Szeredi }
355e9be9d5eSMiklos Szeredi 
356576bb263SChristian Brauner static inline int ovl_do_whiteout(struct ovl_fs *ofs,
357576bb263SChristian Brauner 				  struct inode *dir, struct dentry *dentry)
358e9be9d5eSMiklos Szeredi {
359abf08576SChristian Brauner 	int err = vfs_whiteout(ovl_upper_mnt_idmap(ofs), dir, dentry);
360e9be9d5eSMiklos Szeredi 	pr_debug("whiteout(%pd2) = %i\n", dentry, err);
361e9be9d5eSMiklos Szeredi 	return err;
362e9be9d5eSMiklos Szeredi }
363e9be9d5eSMiklos Szeredi 
3642b1a7746SMiklos Szeredi static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs,
365576bb263SChristian Brauner 					  struct dentry *dentry, umode_t mode)
366e7f52429SAmir Goldstein {
3672b1a7746SMiklos Szeredi 	struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry };
368d56e0ddbSAmir Goldstein 	struct file *file = kernel_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path,
369d56e0ddbSAmir Goldstein 						mode, O_LARGEFILE | O_WRONLY,
370d56e0ddbSAmir Goldstein 						current_cred());
3712b1a7746SMiklos Szeredi 	int err = PTR_ERR_OR_ZERO(file);
372e7f52429SAmir Goldstein 
373e7f52429SAmir Goldstein 	pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
3742b1a7746SMiklos Szeredi 	return file;
375e7f52429SAmir Goldstein }
376e7f52429SAmir Goldstein 
37722f289ceSChristian Brauner static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs,
37822f289ceSChristian Brauner 					      const char *name,
37922f289ceSChristian Brauner 					      struct dentry *base, int len)
38022f289ceSChristian Brauner {
3814609e1f1SChristian Brauner 	return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len);
38222f289ceSChristian Brauner }
38322f289ceSChristian Brauner 
3840c288874SVivek Goyal static inline bool ovl_open_flags_need_copy_up(int flags)
3850c288874SVivek Goyal {
3860c288874SVivek Goyal 	if (!flags)
3870c288874SVivek Goyal 		return false;
3880c288874SVivek Goyal 
3890c288874SVivek Goyal 	return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
3900c288874SVivek Goyal }
3910c288874SVivek Goyal 
392bbb1e54dSMiklos Szeredi /* util.c */
393bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry);
394bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry);
395bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry);
396bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb);
397e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb);
39802bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb);
399f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb);
400f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb);
401163db0daSAmir Goldstein struct ovl_path *ovl_stack_alloc(unsigned int n);
402163db0daSAmir Goldstein void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n);
403163db0daSAmir Goldstein void ovl_stack_put(struct ovl_path *stack, unsigned int n);
404163db0daSAmir Goldstein void ovl_stack_free(struct ovl_path *stack, unsigned int n);
405bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
406163db0daSAmir Goldstein void ovl_free_entry(struct ovl_entry *oe);
407bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry);
408b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
4090af950f5SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
4100af950f5SAmir Goldstein 			   struct ovl_entry *oe);
411b07d5cc9SAmir Goldstein void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
4120af950f5SAmir Goldstein 			   struct ovl_entry *oe, unsigned int mask);
413bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry);
414e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry);
415e9be9d5eSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path);
416e9be9d5eSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path);
4174f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
418b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path);
419e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
4201248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path);
421e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry);
422e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry);
423647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
42442dd69aeSAmir Goldstein int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath);
425ffa5723cSAmir Goldstein const struct ovl_layer *ovl_i_layer_lower(struct inode *inode);
42613464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
427e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry);
4281d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode);
42909d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode);
43009d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode);
4312664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode);
43209d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode);
4334823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode);
4342b21da92SAmir Goldstein const char *ovl_lowerdata_redirect(struct inode *inode);
4354edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
4364edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
437c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
438c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
439c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
440e9be9d5eSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry);
441c412ce49SMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry);
4425cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry);
44355acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry);
44455acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry);
4450c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
4460c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
4470c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode);
4480c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode);
449a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry);
450a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
45109d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
452d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity);
4531fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode);
454bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry);
4552d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags);
4560c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags);
45739d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry);
4580c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags);
4592d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
46043d193f8SMiklos Szeredi 			      enum ovl_xattr ox);
4612d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path);
462dad7017aSChristian Brauner 
463dad7017aSChristian Brauner static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
464dad7017aSChristian Brauner 					  struct dentry *upperdentry)
465dad7017aSChristian Brauner {
466dad7017aSChristian Brauner 	struct path upperpath = {
467dad7017aSChristian Brauner 		.dentry = upperdentry,
468dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
469dad7017aSChristian Brauner 	};
470dad7017aSChristian Brauner 	return ovl_path_check_origin_xattr(ofs, &upperpath);
471dad7017aSChristian Brauner }
472dad7017aSChristian Brauner 
473a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
47443d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
475f3a15685SAmir Goldstein 		       int xerr);
476f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
477ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry);
478ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry);
479146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry);
48024b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry);
4810e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry);
4820e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry);
4835820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
484bf070890SAlexander Larsson int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
485bf070890SAlexander Larsson 			     struct ovl_metacopy *data);
486*184996e9SAlexander Larsson int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d,
487*184996e9SAlexander Larsson 			   struct ovl_metacopy *metacopy);
48867d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry);
4892d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding);
490*184996e9SAlexander Larsson int ovl_get_verity_xattr(struct ovl_fs *ofs, const struct path *path,
491*184996e9SAlexander Larsson 			 u8 *digest_buf, int *buf_length);
492*184996e9SAlexander Larsson int ovl_validate_verity(struct ovl_fs *ofs,
493*184996e9SAlexander Larsson 			struct path *metapath,
494*184996e9SAlexander Larsson 			struct path *datapath);
495335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs);
496f3a15685SAmir Goldstein 
49765cd913eSAmir Goldstein static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
49865cd913eSAmir Goldstein {
49965cd913eSAmir Goldstein 	set_bit(flag, &OVL_I(inode)->flags);
50065cd913eSAmir Goldstein }
50165cd913eSAmir Goldstein 
50265cd913eSAmir Goldstein static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
50365cd913eSAmir Goldstein {
50465cd913eSAmir Goldstein 	clear_bit(flag, &OVL_I(inode)->flags);
50565cd913eSAmir Goldstein }
50665cd913eSAmir Goldstein 
50765cd913eSAmir Goldstein static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
50865cd913eSAmir Goldstein {
50965cd913eSAmir Goldstein 	return test_bit(flag, &OVL_I(inode)->flags);
51065cd913eSAmir Goldstein }
51165cd913eSAmir Goldstein 
512610afc0bSMiklos Szeredi static inline bool ovl_is_impuredir(struct super_block *sb,
513dad7017aSChristian Brauner 				    struct dentry *upperdentry)
514f3a15685SAmir Goldstein {
515dad7017aSChristian Brauner 	struct ovl_fs *ofs = OVL_FS(sb);
516dad7017aSChristian Brauner 	struct path upperpath = {
517dad7017aSChristian Brauner 		.dentry = upperdentry,
518dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
519dad7017aSChristian Brauner 	};
520dad7017aSChristian Brauner 
521dad7017aSChristian Brauner 	return ovl_path_check_dir_xattr(ofs, &upperpath, OVL_XATTR_IMPURE);
522f3a15685SAmir Goldstein }
523f3a15685SAmir Goldstein 
524af5f2396SAmir Goldstein static inline bool ovl_redirect_follow(struct ovl_fs *ofs)
525af5f2396SAmir Goldstein {
526af5f2396SAmir Goldstein 	return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW;
527af5f2396SAmir Goldstein }
528af5f2396SAmir Goldstein 
529af5f2396SAmir Goldstein static inline bool ovl_redirect_dir(struct ovl_fs *ofs)
530af5f2396SAmir Goldstein {
531af5f2396SAmir Goldstein 	return ofs->config.redirect_mode == OVL_REDIRECT_ON;
532af5f2396SAmir Goldstein }
533af5f2396SAmir Goldstein 
534926e94d7SAmir Goldstein /*
535926e94d7SAmir Goldstein  * With xino=auto, we do best effort to keep all inodes on same st_dev and
536926e94d7SAmir Goldstein  * d_ino consistent with st_ino.
537926e94d7SAmir Goldstein  * With xino=on, we do the same effort but we warn if we failed.
538926e94d7SAmir Goldstein  */
539dcb399deSAmir Goldstein static inline bool ovl_xino_warn(struct ovl_fs *ofs)
540926e94d7SAmir Goldstein {
541dcb399deSAmir Goldstein 	return ofs->config.xino == OVL_XINO_ON;
542926e94d7SAmir Goldstein }
543926e94d7SAmir Goldstein 
544af5f2396SAmir Goldstein /*
545af5f2396SAmir Goldstein  * To avoid regressions in existing setups with overlay lower offline changes,
546af5f2396SAmir Goldstein  * we allow lower changes only if none of the new features are used.
547af5f2396SAmir Goldstein  */
548af5f2396SAmir Goldstein static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs)
549af5f2396SAmir Goldstein {
550af5f2396SAmir Goldstein 	return (!ofs->config.index && !ofs->config.metacopy &&
551af5f2396SAmir Goldstein 		!ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs));
5520f831ec8SAmir Goldstein }
553e487d889SAmir Goldstein 
554e487d889SAmir Goldstein /* All layers on same fs? */
555dcb399deSAmir Goldstein static inline bool ovl_same_fs(struct ovl_fs *ofs)
556bbb1e54dSMiklos Szeredi {
557dcb399deSAmir Goldstein 	return ofs->xino_mode == 0;
5588a22efa1SAmir Goldstein }
5598a22efa1SAmir Goldstein 
5608a22efa1SAmir Goldstein /* All overlay inodes have same st_dev? */
561dcb399deSAmir Goldstein static inline bool ovl_same_dev(struct ovl_fs *ofs)
5621e92e307SAmir Goldstein {
563dcb399deSAmir Goldstein 	return ofs->xino_mode >= 0;
5641e92e307SAmir Goldstein }
5651e92e307SAmir Goldstein 
566dcb399deSAmir Goldstein static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs)
5671e92e307SAmir Goldstein {
568dcb399deSAmir Goldstein 	return ovl_same_dev(ofs) ? ofs->xino_mode : 0;
569f941866fSAmir Goldstein }
57005122443SAmir Goldstein 
571531d3040SAmir Goldstein static inline void ovl_inode_lock(struct inode *inode)
572531d3040SAmir Goldstein {
573531d3040SAmir Goldstein 	mutex_lock(&OVL_I(inode)->lock);
574531d3040SAmir Goldstein }
575531d3040SAmir Goldstein 
576531d3040SAmir Goldstein static inline int ovl_inode_lock_interruptible(struct inode *inode)
5771e92e307SAmir Goldstein {
5781e92e307SAmir Goldstein 	return mutex_lock_interruptible(&OVL_I(inode)->lock);
5791e92e307SAmir Goldstein }
5801e92e307SAmir Goldstein 
5811e92e307SAmir Goldstein static inline void ovl_inode_unlock(struct inode *inode)
5821e92e307SAmir Goldstein {
5831e92e307SAmir Goldstein 	mutex_unlock(&OVL_I(inode)->lock);
5841e92e307SAmir Goldstein }
5851e92e307SAmir Goldstein 
586e9be9d5eSMiklos Szeredi 
587bbb1e54dSMiklos Szeredi /* namei.c */
588cbe7fba8SAmir Goldstein int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
589cbe7fba8SAmir Goldstein 
590cbe7fba8SAmir Goldstein static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
591cbe7fba8SAmir Goldstein {
592522f6e6cSAmir Goldstein 	if (fh_len < sizeof(struct ovl_fh))
593522f6e6cSAmir Goldstein 		return -EINVAL;
594522f6e6cSAmir Goldstein 
595cbe7fba8SAmir Goldstein 	return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
596cbe7fba8SAmir Goldstein }
597cbe7fba8SAmir Goldstein 
5981cdb0cb6SPavel Tikhomirov struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
5991cdb0cb6SPavel Tikhomirov 				  struct vfsmount *mnt, bool connected);
60005122443SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
6011eff1a1dSAmir Goldstein 			struct dentry *upperdentry, struct ovl_path **stackp);
602610afc0bSMiklos Szeredi int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
60343d193f8SMiklos Szeredi 		      enum ovl_xattr ox, struct dentry *real, bool is_upper,
604610afc0bSMiklos Szeredi 		      bool set);
6058ea28765SAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index,
6068ea28765SAmir Goldstein 			       bool connected);
607415543d5SAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
6081cdb0cb6SPavel Tikhomirov int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
6091cdb0cb6SPavel Tikhomirov 		       struct qstr *name);
61091ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
61106170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
61206170154SAmir Goldstein 				struct dentry *origin, bool verify);
613bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
614*184996e9SAlexander Larsson int ovl_verify_lowerdata(struct dentry *dentry);
6151eff1a1dSAmir Goldstein struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
6161eff1a1dSAmir Goldstein 			  unsigned int flags);
617bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry);
618e9be9d5eSMiklos Szeredi 
619610afc0bSMiklos Szeredi static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
62005122443SAmir Goldstein 				    struct dentry *origin, bool set)
62105122443SAmir Goldstein {
622610afc0bSMiklos Szeredi 	return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
623610afc0bSMiklos Szeredi 				 false, set);
62405122443SAmir Goldstein }
62505122443SAmir Goldstein 
626610afc0bSMiklos Szeredi static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
627ad1d615cSAmir Goldstein 				   struct dentry *upper, bool set)
628ad1d615cSAmir Goldstein {
629610afc0bSMiklos Szeredi 	return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
630ad1d615cSAmir Goldstein }
631ad1d615cSAmir Goldstein 
632e9be9d5eSMiklos Szeredi /* readdir.c */
633e9be9d5eSMiklos Szeredi extern const struct file_operations ovl_dir_operations;
63461536bedSAmir Goldstein struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
635e9be9d5eSMiklos Szeredi int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
636576bb263SChristian Brauner void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
637576bb263SChristian Brauner 			   struct list_head *list);
638e9be9d5eSMiklos Szeredi void ovl_cache_free(struct list_head *list);
6394edb83bbSMiklos Szeredi void ovl_dir_cache_free(struct inode *inode);
6402d343087SAl Viro int ovl_check_d_type_supported(const struct path *realpath);
641576bb263SChristian Brauner int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
642576bb263SChristian Brauner 			struct vfsmount *mnt, struct dentry *dentry, int level);
6431eff1a1dSAmir Goldstein int ovl_indexdir_cleanup(struct ovl_fs *ofs);
644e9be9d5eSMiklos Szeredi 
64565cd913eSAmir Goldstein /*
64665cd913eSAmir Goldstein  * Can we iterate real dir directly?
64765cd913eSAmir Goldstein  *
64865cd913eSAmir Goldstein  * Non-merge dir may contain whiteouts from a time it was a merge upper, before
64965cd913eSAmir Goldstein  * lower dir was removed under it and possibly before it was rotated from upper
65065cd913eSAmir Goldstein  * to lower layer.
65165cd913eSAmir Goldstein  */
6521fa9c5c5SMiklos Szeredi static inline bool ovl_dir_is_real(struct inode *dir)
65365cd913eSAmir Goldstein {
6541fa9c5c5SMiklos Szeredi 	return !ovl_test_flag(OVL_WHITEOUTS, dir);
65565cd913eSAmir Goldstein }
65665cd913eSAmir Goldstein 
657e9be9d5eSMiklos Szeredi /* inode.c */
6585f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry);
6595f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry);
660610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
661caf70cb2SAmir Goldstein 			   struct dentry *upperdentry,
662caf70cb2SAmir Goldstein 			   unsigned int fallback);
663c1632a0fSChristian Brauner int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
664549c7297SChristian Brauner 		struct iattr *attr);
665b74d24f7SChristian Brauner int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
666549c7297SChristian Brauner 		struct kstat *stat, u32 request_mask, unsigned int flags);
6674609e1f1SChristian Brauner int ovl_permission(struct mnt_idmap *idmap, struct inode *inode,
668549c7297SChristian Brauner 		   int mask);
6691d88f183SMiklos Szeredi int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
6701d88f183SMiklos Szeredi 		  const void *value, size_t size, int flags);
6711d88f183SMiklos Szeredi int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
6720eb45fc3SAndreas Gruenbacher 		  void *value, size_t size);
673e9be9d5eSMiklos Szeredi ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
674ded53656SYang Xu 
675ded53656SYang Xu #ifdef CONFIG_FS_POSIX_ACL
67677435322SChristian Brauner struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
6776c0a8bfbSChristian Brauner 				 struct inode *inode, int type,
6786c0a8bfbSChristian Brauner 				 bool rcu, bool noperm);
6796c0a8bfbSChristian Brauner static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type,
6806c0a8bfbSChristian Brauner 						  bool rcu)
6816c0a8bfbSChristian Brauner {
68277435322SChristian Brauner 	return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true);
6836c0a8bfbSChristian Brauner }
68477435322SChristian Brauner static inline struct posix_acl *ovl_get_acl(struct mnt_idmap *idmap,
6856c0a8bfbSChristian Brauner 					    struct dentry *dentry, int type)
6866c0a8bfbSChristian Brauner {
68777435322SChristian Brauner 	return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false);
6886c0a8bfbSChristian Brauner }
68913e83a49SChristian Brauner int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
6900e641857SChristian Brauner 		struct posix_acl *acl, int type);
69131acceb9SChristian Brauner struct posix_acl *ovl_get_acl_path(const struct path *path,
69231acceb9SChristian Brauner 				   const char *acl_name, bool noperm);
693ded53656SYang Xu #else
6946c0a8bfbSChristian Brauner #define ovl_get_inode_acl	NULL
695ded53656SYang Xu #define ovl_get_acl		NULL
6960e641857SChristian Brauner #define ovl_set_acl		NULL
69731acceb9SChristian Brauner static inline struct posix_acl *ovl_get_acl_path(const struct path *path,
69831acceb9SChristian Brauner 						 const char *acl_name,
69931acceb9SChristian Brauner 						 bool noperm)
70031acceb9SChristian Brauner {
70131acceb9SChristian Brauner 	return NULL;
70231acceb9SChristian Brauner }
703ded53656SYang Xu #endif
704ded53656SYang Xu 
70595582b00SDeepa Dinamani int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
706610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name);
707e9be9d5eSMiklos Szeredi 
708ac6a52ebSVivek Goyal struct ovl_inode_params {
70901b39dccSAmir Goldstein 	struct inode *newinode;
710ac6a52ebSVivek Goyal 	struct dentry *upperdentry;
7110af950f5SAmir Goldstein 	struct ovl_entry *oe;
71274c6e384SMiklos Szeredi 	bool index;
7139cec54c8SVivek Goyal 	char *redirect;
7142b21da92SAmir Goldstein 	char *lowerdata_redirect;
715ac6a52ebSVivek Goyal };
71662c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
71762c832edSAmir Goldstein 		    unsigned long ino, int fsid);
718ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
7194b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
7204b91c30aSAmir Goldstein 			       bool is_upper);
721146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
722146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
723ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb,
724ac6a52ebSVivek Goyal 			    struct ovl_inode_params *oip);
7252878dffcSChristian Brauner void ovl_copyattr(struct inode *to);
726e9be9d5eSMiklos Szeredi 
72772db8211SAmir Goldstein /* vfs inode flags copied from real to ovl inode */
72872db8211SAmir Goldstein #define OVL_COPY_I_FLAGS_MASK	(S_SYNC | S_NOATIME | S_APPEND | S_IMMUTABLE)
729096a218aSAmir Goldstein /* vfs inode flags read from overlay.protattr xattr to ovl inode */
730096a218aSAmir Goldstein #define OVL_PROT_I_FLAGS_MASK	(S_APPEND | S_IMMUTABLE)
73172db8211SAmir Goldstein 
73272db8211SAmir Goldstein /*
73372db8211SAmir Goldstein  * fileattr flags copied from lower to upper inode on copy up.
734096a218aSAmir Goldstein  * We cannot copy up immutable/append-only flags, because that would prevent
735096a218aSAmir Goldstein  * linking temp inode to upper dir, so we store them in xattr instead.
73672db8211SAmir Goldstein  */
73772db8211SAmir Goldstein #define OVL_COPY_FS_FLAGS_MASK	(FS_SYNC_FL | FS_NOATIME_FL)
73872db8211SAmir Goldstein #define OVL_COPY_FSX_FLAGS_MASK	(FS_XFLAG_SYNC | FS_XFLAG_NOATIME)
739096a218aSAmir Goldstein #define OVL_PROT_FS_FLAGS_MASK  (FS_APPEND_FL | FS_IMMUTABLE_FL)
740096a218aSAmir Goldstein #define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE)
741096a218aSAmir Goldstein 
742096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper);
743096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper,
744096a218aSAmir Goldstein 		      struct fileattr *fa);
74572db8211SAmir Goldstein 
7464f357295SMiklos Szeredi static inline void ovl_copyflags(struct inode *from, struct inode *to)
7474f357295SMiklos Szeredi {
74872db8211SAmir Goldstein 	unsigned int mask = OVL_COPY_I_FLAGS_MASK;
7494f357295SMiklos Szeredi 
7504f357295SMiklos Szeredi 	inode_set_flags(to, from->i_flags & mask, mask);
7514f357295SMiklos Szeredi }
7524f357295SMiklos Szeredi 
753e9be9d5eSMiklos Szeredi /* dir.c */
754e9be9d5eSMiklos Szeredi extern const struct inode_operations ovl_dir_inode_operations;
755c21c839bSChengguang Xu int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
756e7dd0e71SAmir Goldstein 			     struct dentry *dentry);
757471ec5dcSAmir Goldstein struct ovl_cattr {
75832a3d848SAl Viro 	dev_t rdev;
75932a3d848SAl Viro 	umode_t mode;
76032a3d848SAl Viro 	const char *link;
761471ec5dcSAmir Goldstein 	struct dentry *hardlink;
76232a3d848SAl Viro };
763471ec5dcSAmir Goldstein 
764471ec5dcSAmir Goldstein #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
765471ec5dcSAmir Goldstein 
766576bb263SChristian Brauner int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
767576bb263SChristian Brauner 		   struct dentry **newdentry, umode_t mode);
768576bb263SChristian Brauner struct dentry *ovl_create_real(struct ovl_fs *ofs,
769576bb263SChristian Brauner 			       struct inode *dir, struct dentry *newdentry,
770471ec5dcSAmir Goldstein 			       struct ovl_cattr *attr);
771576bb263SChristian Brauner int ovl_cleanup(struct ovl_fs *ofs, struct inode *dir, struct dentry *dentry);
772576bb263SChristian Brauner struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir);
773576bb263SChristian Brauner struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
774576bb263SChristian Brauner 			       struct ovl_cattr *attr);
775e9be9d5eSMiklos Szeredi 
776d1d04ef8SMiklos Szeredi /* file.c */
777d1d04ef8SMiklos Szeredi extern const struct file_operations ovl_file_operations;
7782406a307SJiufei Xue int __init ovl_aio_request_cache_init(void);
7792406a307SJiufei Xue void ovl_aio_request_cache_destroy(void);
7802d343087SAl Viro int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa);
7812d343087SAl Viro int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa);
78266dbfabfSMiklos Szeredi int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa);
7838782a9aeSChristian Brauner int ovl_fileattr_set(struct mnt_idmap *idmap,
78466dbfabfSMiklos Szeredi 		     struct dentry *dentry, struct fileattr *fa);
785d1d04ef8SMiklos Szeredi 
786e9be9d5eSMiklos Szeredi /* copy_up.c */
787e9be9d5eSMiklos Szeredi int ovl_copy_up(struct dentry *dentry);
788d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry);
7893428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags);
7902d343087SAl Viro int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new);
7915272eaf3SChristian Brauner int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat);
7921cdb0cb6SPavel Tikhomirov struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
7931cdb0cb6SPavel Tikhomirov 				  bool is_upper);
794a0c236b1SAmir Goldstein int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
795a0c236b1SAmir Goldstein 		   struct dentry *upper);
7968ed5eec9SAmir Goldstein 
7978ed5eec9SAmir Goldstein /* export.c */
7988ed5eec9SAmir Goldstein extern const struct export_operations ovl_export_operations;
7997fb7998bSChristian Brauner 
8007fb7998bSChristian Brauner /* super.c */
8017fb7998bSChristian Brauner int ovl_fill_super(struct super_block *sb, struct fs_context *fc);
8027fb7998bSChristian Brauner 
8037fb7998bSChristian Brauner /* Will this overlay be forced to mount/remount ro? */
8047fb7998bSChristian Brauner static inline bool ovl_force_readonly(struct ovl_fs *ofs)
8057fb7998bSChristian Brauner {
8067fb7998bSChristian Brauner 	return (!ovl_upper_mnt(ofs) || !ofs->workdir);
8077fb7998bSChristian Brauner }
808