xref: /linux/fs/overlayfs/overlayfs.h (revision bc8df7a3dc035903426a17ea3027f55817de13a8)
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
31d431e652SAlexander Larsson #define OVL_XATTR_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_TRUSTED_PREFIX) - 1)
322d2f2d73SMiklos Szeredi #define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE
33d431e652SAlexander Larsson #define OVL_XATTR_USER_PREFIX_LEN (sizeof(OVL_XATTR_USER_PREFIX) - 1)
3443d193f8SMiklos Szeredi 
35dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_PREFIX OVL_XATTR_NAMESPACE
36dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_PREFIX) - 1)
37dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX OVL_XATTR_TRUSTED_PREFIX OVL_XATTR_ESCAPE_PREFIX
38dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_TRUSTED_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_TRUSTED_PREFIX) - 1)
39dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_USER_PREFIX OVL_XATTR_USER_PREFIX OVL_XATTR_ESCAPE_PREFIX
40dad02fadSAlexander Larsson #define OVL_XATTR_ESCAPE_USER_PREFIX_LEN (sizeof(OVL_XATTR_ESCAPE_USER_PREFIX) - 1)
41dad02fadSAlexander Larsson 
4243d193f8SMiklos Szeredi enum ovl_xattr {
4343d193f8SMiklos Szeredi 	OVL_XATTR_OPAQUE,
4443d193f8SMiklos Szeredi 	OVL_XATTR_REDIRECT,
4543d193f8SMiklos Szeredi 	OVL_XATTR_ORIGIN,
4643d193f8SMiklos Szeredi 	OVL_XATTR_IMPURE,
4743d193f8SMiklos Szeredi 	OVL_XATTR_NLINK,
4843d193f8SMiklos Szeredi 	OVL_XATTR_UPPER,
49d9544c1bSAmir Goldstein 	OVL_XATTR_UUID,
5043d193f8SMiklos Szeredi 	OVL_XATTR_METACOPY,
51096a218aSAmir Goldstein 	OVL_XATTR_PROTATTR,
52*bc8df7a3SAlexander Larsson 	OVL_XATTR_XWHITEOUT,
53*bc8df7a3SAlexander Larsson 	OVL_XATTR_XWHITEOUTS,
5443d193f8SMiklos Szeredi };
553a1e819bSAmir Goldstein 
56c62520a8SAmir Goldstein enum ovl_inode_flag {
57b79e05aaSAmir Goldstein 	/* Pure upper dir that may contain non pure upper entries */
5813c72075SMiklos Szeredi 	OVL_IMPURE,
59b79e05aaSAmir Goldstein 	/* Non-merge dir that may contain whiteout entries */
60b79e05aaSAmir Goldstein 	OVL_WHITEOUTS,
61359f392cSAmir Goldstein 	OVL_INDEX,
620c288874SVivek Goyal 	OVL_UPPERDATA,
63a00c2d59SVivek Goyal 	/* Inode number will remain constant over copy up. */
64a00c2d59SVivek Goyal 	OVL_CONST_INO,
65184996e9SAlexander Larsson 	OVL_HAS_DIGEST,
66184996e9SAlexander Larsson 	OVL_VERIFIED_DIGEST,
6713c72075SMiklos Szeredi };
6813c72075SMiklos Szeredi 
69c62520a8SAmir Goldstein enum ovl_entry_flag {
70c62520a8SAmir Goldstein 	OVL_E_UPPER_ALIAS,
71c62520a8SAmir Goldstein 	OVL_E_OPAQUE,
722ca3c148SAmir Goldstein 	OVL_E_CONNECTED,
73c62520a8SAmir Goldstein };
74c62520a8SAmir Goldstein 
75926e94d7SAmir Goldstein enum {
76af5f2396SAmir Goldstein 	OVL_REDIRECT_OFF,	/* "off" mode is never used. In effect	*/
77af5f2396SAmir Goldstein 	OVL_REDIRECT_FOLLOW,	/* ...it translates to either "follow"	*/
78af5f2396SAmir Goldstein 	OVL_REDIRECT_NOFOLLOW,	/* ...or "nofollow".			*/
79af5f2396SAmir Goldstein 	OVL_REDIRECT_ON,
80af5f2396SAmir Goldstein };
81af5f2396SAmir Goldstein 
82af5f2396SAmir Goldstein enum {
83b0504bfeSAmir Goldstein 	OVL_UUID_OFF,
84b0504bfeSAmir Goldstein 	OVL_UUID_NULL,
85cbb44f09SAmir Goldstein 	OVL_UUID_AUTO,
86b0504bfeSAmir Goldstein 	OVL_UUID_ON,
87b0504bfeSAmir Goldstein };
88b0504bfeSAmir Goldstein 
89b0504bfeSAmir Goldstein enum {
90926e94d7SAmir Goldstein 	OVL_XINO_OFF,
91926e94d7SAmir Goldstein 	OVL_XINO_AUTO,
92926e94d7SAmir Goldstein 	OVL_XINO_ON,
93926e94d7SAmir Goldstein };
94926e94d7SAmir Goldstein 
95ae8cba40SAlexander Larsson enum {
96ae8cba40SAlexander Larsson 	OVL_VERITY_OFF,
97ae8cba40SAlexander Larsson 	OVL_VERITY_ON,
98ae8cba40SAlexander Larsson 	OVL_VERITY_REQUIRE,
99ae8cba40SAlexander Larsson };
100ae8cba40SAlexander Larsson 
1013a1e819bSAmir Goldstein /*
1023a1e819bSAmir Goldstein  * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
1033a1e819bSAmir Goldstein  * where:
1043a1e819bSAmir Goldstein  * origin.fh	- exported file handle of the lower file
1053a1e819bSAmir Goldstein  * origin.uuid	- uuid of the lower filesystem
1063a1e819bSAmir Goldstein  */
1073a1e819bSAmir Goldstein #define OVL_FH_VERSION	0
1083a1e819bSAmir Goldstein #define OVL_FH_MAGIC	0xfb
1093a1e819bSAmir Goldstein 
1103a1e819bSAmir Goldstein /* CPU byte order required for fid decoding:  */
1113a1e819bSAmir Goldstein #define OVL_FH_FLAG_BIG_ENDIAN	(1 << 0)
1123a1e819bSAmir Goldstein #define OVL_FH_FLAG_ANY_ENDIAN	(1 << 1)
11354fb347eSAmir Goldstein /* Is the real inode encoded in fid an upper inode? */
11454fb347eSAmir Goldstein #define OVL_FH_FLAG_PATH_UPPER	(1 << 2)
1153a1e819bSAmir Goldstein 
116961af647SAmir Goldstein #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
117961af647SAmir Goldstein 			 OVL_FH_FLAG_PATH_UPPER)
1183a1e819bSAmir Goldstein 
1193a1e819bSAmir Goldstein #if defined(__LITTLE_ENDIAN)
1203a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN 0
1213a1e819bSAmir Goldstein #elif defined(__BIG_ENDIAN)
1223a1e819bSAmir Goldstein #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
1233a1e819bSAmir Goldstein #else
1243a1e819bSAmir Goldstein #error Endianness not defined
1253a1e819bSAmir Goldstein #endif
1263a1e819bSAmir Goldstein 
127cbe7fba8SAmir Goldstein /* The type used to be returned by overlay exportfs for misaligned fid */
128cbe7fba8SAmir Goldstein #define OVL_FILEID_V0	0xfb
129cbe7fba8SAmir Goldstein /* The type returned by overlay exportfs for 32bit aligned fid */
130cbe7fba8SAmir Goldstein #define OVL_FILEID_V1	0xf8
1318ed5eec9SAmir Goldstein 
132cbe7fba8SAmir Goldstein /* On-disk format for "origin" file handle */
133cbe7fba8SAmir Goldstein struct ovl_fb {
1343a1e819bSAmir Goldstein 	u8 version;	/* 0 */
1353a1e819bSAmir Goldstein 	u8 magic;	/* 0xfb */
1363a1e819bSAmir Goldstein 	u8 len;		/* size of this header + size of fid */
1373a1e819bSAmir Goldstein 	u8 flags;	/* OVL_FH_FLAG_* */
1383a1e819bSAmir Goldstein 	u8 type;	/* fid_type of fid */
13901633fd2SChristoph Hellwig 	uuid_t uuid;	/* uuid of filesystem */
1400efbe7c4SGustavo A. R. Silva 	u32 fid[];	/* file identifier should be 32bit aligned in-memory */
1413a1e819bSAmir Goldstein } __packed;
142e9be9d5eSMiklos Szeredi 
143cbe7fba8SAmir Goldstein /* In-memory and on-wire format for overlay file handle */
144cbe7fba8SAmir Goldstein struct ovl_fh {
145cbe7fba8SAmir Goldstein 	u8 padding[3];	/* make sure fb.fid is 32bit aligned */
146cbe7fba8SAmir Goldstein 	union {
147cbe7fba8SAmir Goldstein 		struct ovl_fb fb;
148cf8aa9bfSKees Cook 		DECLARE_FLEX_ARRAY(u8, buf);
149cbe7fba8SAmir Goldstein 	};
150cbe7fba8SAmir Goldstein } __packed;
151cbe7fba8SAmir Goldstein 
152cbe7fba8SAmir Goldstein #define OVL_FH_WIRE_OFFSET	offsetof(struct ovl_fh, fb)
153cbe7fba8SAmir Goldstein #define OVL_FH_LEN(fh)		(OVL_FH_WIRE_OFFSET + (fh)->fb.len)
154cbe7fba8SAmir Goldstein #define OVL_FH_FID_OFFSET	(OVL_FH_WIRE_OFFSET + \
155cbe7fba8SAmir Goldstein 				 offsetof(struct ovl_fb, fid))
156cbe7fba8SAmir Goldstein 
157bf070890SAlexander Larsson /* On-disk format for "metacopy" xattr (if non-zero size) */
158bf070890SAlexander Larsson struct ovl_metacopy {
159bf070890SAlexander Larsson 	u8 version;	/* 0 */
160bf070890SAlexander Larsson 	u8 len;         /* size of this header + used digest bytes */
161bf070890SAlexander Larsson 	u8 flags;
162bf070890SAlexander Larsson 	u8 digest_algo;	/* FS_VERITY_HASH_ALG_* constant, 0 for no digest */
163bf070890SAlexander Larsson 	u8 digest[FS_VERITY_MAX_DIGEST_SIZE];  /* Only the used part on disk */
164bf070890SAlexander Larsson } __packed;
165bf070890SAlexander Larsson 
166bf070890SAlexander Larsson #define OVL_METACOPY_MAX_SIZE (sizeof(struct ovl_metacopy))
167bf070890SAlexander Larsson #define OVL_METACOPY_MIN_SIZE (OVL_METACOPY_MAX_SIZE - FS_VERITY_MAX_DIGEST_SIZE)
168bf070890SAlexander Larsson #define OVL_METACOPY_INIT { 0, OVL_METACOPY_MIN_SIZE }
169bf070890SAlexander Larsson 
170bf070890SAlexander Larsson static inline int ovl_metadata_digest_size(const struct ovl_metacopy *metacopy)
171bf070890SAlexander Larsson {
172bf070890SAlexander Larsson 	if (metacopy->len < OVL_METACOPY_MIN_SIZE)
173bf070890SAlexander Larsson 		return 0;
174bf070890SAlexander Larsson 	return (int)metacopy->len - OVL_METACOPY_MIN_SIZE;
175bf070890SAlexander Larsson }
176bf070890SAlexander Larsson 
1772d2f2d73SMiklos Szeredi extern const char *const ovl_xattr_table[][2];
17843d193f8SMiklos Szeredi static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
17943d193f8SMiklos Szeredi {
1802d2f2d73SMiklos Szeredi 	return ovl_xattr_table[ox][ofs->config.userxattr];
18143d193f8SMiklos Szeredi }
18243d193f8SMiklos Szeredi 
183a15506eaSChristian Brauner /*
184a15506eaSChristian Brauner  * When changing ownership of an upper object map the intended ownership
185a15506eaSChristian Brauner  * according to the upper layer's idmapping. When an upper mount idmaps files
186a15506eaSChristian Brauner  * that are stored on-disk as owned by id 1001 to id 1000 this means stat on
187a15506eaSChristian Brauner  * this object will report it as being owned by id 1000 when calling stat via
188a15506eaSChristian Brauner  * the upper mount.
189a15506eaSChristian Brauner  * In order to change ownership of an object so stat reports id 1000 when
190a15506eaSChristian Brauner  * called on an idmapped upper mount the value written to disk - i.e., the
191a15506eaSChristian Brauner  * value stored in ia_*id - must 1001. The mount mapping helper will thus take
192a15506eaSChristian Brauner  * care to map 1000 to 1001.
193a15506eaSChristian Brauner  * The mnt idmapping helpers are nops if the upper layer isn't idmapped.
194a15506eaSChristian Brauner  */
195a15506eaSChristian Brauner static inline int ovl_do_notify_change(struct ovl_fs *ofs,
196a15506eaSChristian Brauner 				       struct dentry *upperdentry,
197a15506eaSChristian Brauner 				       struct iattr *attr)
198a15506eaSChristian Brauner {
199abf08576SChristian Brauner 	return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL);
200a15506eaSChristian Brauner }
201a15506eaSChristian Brauner 
202576bb263SChristian Brauner static inline int ovl_do_rmdir(struct ovl_fs *ofs,
203576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry)
204e9be9d5eSMiklos Szeredi {
205abf08576SChristian Brauner 	int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry);
2066cf00764SAmir Goldstein 
207e9be9d5eSMiklos Szeredi 	pr_debug("rmdir(%pd2) = %i\n", dentry, err);
208e9be9d5eSMiklos Szeredi 	return err;
209e9be9d5eSMiklos Szeredi }
210e9be9d5eSMiklos Szeredi 
211576bb263SChristian Brauner static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir,
212576bb263SChristian Brauner 				struct dentry *dentry)
213e9be9d5eSMiklos Szeredi {
214abf08576SChristian Brauner 	int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL);
2156cf00764SAmir Goldstein 
216e9be9d5eSMiklos Szeredi 	pr_debug("unlink(%pd2) = %i\n", dentry, err);
217e9be9d5eSMiklos Szeredi 	return err;
218e9be9d5eSMiklos Szeredi }
219e9be9d5eSMiklos Szeredi 
220576bb263SChristian Brauner static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry,
221576bb263SChristian Brauner 			      struct inode *dir, struct dentry *new_dentry)
222e9be9d5eSMiklos Szeredi {
223abf08576SChristian Brauner 	int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir,
224abf08576SChristian Brauner 			   new_dentry, NULL);
2256cf00764SAmir Goldstein 
2266cf00764SAmir Goldstein 	pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
227e9be9d5eSMiklos Szeredi 	return err;
228e9be9d5eSMiklos Szeredi }
229e9be9d5eSMiklos Szeredi 
230576bb263SChristian Brauner static inline int ovl_do_create(struct ovl_fs *ofs,
231576bb263SChristian Brauner 				struct inode *dir, struct dentry *dentry,
2326cf00764SAmir Goldstein 				umode_t mode)
233e9be9d5eSMiklos Szeredi {
234abf08576SChristian Brauner 	int err = vfs_create(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, true);
2356cf00764SAmir Goldstein 
236e9be9d5eSMiklos Szeredi 	pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
237e9be9d5eSMiklos Szeredi 	return err;
238e9be9d5eSMiklos Szeredi }
239e9be9d5eSMiklos Szeredi 
240576bb263SChristian Brauner static inline int ovl_do_mkdir(struct ovl_fs *ofs,
241576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry,
2426cf00764SAmir Goldstein 			       umode_t mode)
243e9be9d5eSMiklos Szeredi {
244abf08576SChristian Brauner 	int err = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode);
245e9be9d5eSMiklos Szeredi 	pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
246e9be9d5eSMiklos Szeredi 	return err;
247e9be9d5eSMiklos Szeredi }
248e9be9d5eSMiklos Szeredi 
249576bb263SChristian Brauner static inline int ovl_do_mknod(struct ovl_fs *ofs,
250576bb263SChristian Brauner 			       struct inode *dir, struct dentry *dentry,
2516cf00764SAmir Goldstein 			       umode_t mode, dev_t dev)
252e9be9d5eSMiklos Szeredi {
253abf08576SChristian Brauner 	int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev);
2546cf00764SAmir Goldstein 
2556cf00764SAmir Goldstein 	pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
256e9be9d5eSMiklos Szeredi 	return err;
257e9be9d5eSMiklos Szeredi }
258e9be9d5eSMiklos Szeredi 
259576bb263SChristian Brauner static inline int ovl_do_symlink(struct ovl_fs *ofs,
260576bb263SChristian Brauner 				 struct inode *dir, struct dentry *dentry,
2616cf00764SAmir Goldstein 				 const char *oldname)
262e9be9d5eSMiklos Szeredi {
263abf08576SChristian Brauner 	int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname);
2646cf00764SAmir Goldstein 
265e9be9d5eSMiklos Szeredi 	pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
266e9be9d5eSMiklos Szeredi 	return err;
267e9be9d5eSMiklos Szeredi }
268e9be9d5eSMiklos Szeredi 
2692d343087SAl Viro static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name,
270dad7017aSChristian Brauner 				      void *value, size_t size)
271d5dc7486SMiklos Szeredi {
272dad7017aSChristian Brauner 	int err, len;
273dad7017aSChristian Brauner 
274dad7017aSChristian Brauner 	WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb);
275dad7017aSChristian Brauner 
2764609e1f1SChristian Brauner 	err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry,
277dad7017aSChristian Brauner 			       name, value, size);
278dad7017aSChristian Brauner 	len = (value && err > 0) ? err : 0;
2795e717c6fSAmir Goldstein 
2805e717c6fSAmir Goldstein 	pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
281dad7017aSChristian Brauner 		 path->dentry, name, min(len, 48), value, size, err);
2825e717c6fSAmir Goldstein 	return err;
283d5dc7486SMiklos Szeredi }
284d5dc7486SMiklos Szeredi 
285dad7017aSChristian Brauner static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs,
286dad7017aSChristian Brauner 					 struct dentry *upperdentry,
287c914c0e2SAmir Goldstein 					 enum ovl_xattr ox, void *value,
288610afc0bSMiklos Szeredi 					 size_t size)
289e9be9d5eSMiklos Szeredi {
290dad7017aSChristian Brauner 	struct path upperpath = {
291dad7017aSChristian Brauner 		.dentry = upperdentry,
292dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
293dad7017aSChristian Brauner 	};
294dad7017aSChristian Brauner 
295dad7017aSChristian Brauner 	return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size);
296dad7017aSChristian Brauner }
297dad7017aSChristian Brauner 
298dad7017aSChristian Brauner static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs,
2992d343087SAl Viro 					 const struct path *path,
300dad7017aSChristian Brauner 					 enum ovl_xattr ox, void *value,
301dad7017aSChristian Brauner 					 size_t size)
302dad7017aSChristian Brauner {
303dad7017aSChristian Brauner 	return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size);
304c914c0e2SAmir Goldstein }
305c914c0e2SAmir Goldstein 
306c914c0e2SAmir Goldstein static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
307c914c0e2SAmir Goldstein 				  const char *name, const void *value,
308c914c0e2SAmir Goldstein 				  size_t size, int flags)
309c914c0e2SAmir Goldstein {
3104609e1f1SChristian Brauner 	int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name,
3116344e669SChristian Brauner 			       value, size, flags);
312c914c0e2SAmir Goldstein 
313c914c0e2SAmir Goldstein 	pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n",
314c914c0e2SAmir Goldstein 		 dentry, name, min((int)size, 48), value, size, flags, err);
315e9be9d5eSMiklos Szeredi 	return err;
316e9be9d5eSMiklos Szeredi }
317e9be9d5eSMiklos Szeredi 
318c914c0e2SAmir Goldstein static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
319c914c0e2SAmir Goldstein 			       enum ovl_xattr ox, const void *value,
320c914c0e2SAmir Goldstein 			       size_t size)
321e9be9d5eSMiklos Szeredi {
322c914c0e2SAmir Goldstein 	return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0);
323c914c0e2SAmir Goldstein }
324c914c0e2SAmir Goldstein 
325c914c0e2SAmir Goldstein static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
326c914c0e2SAmir Goldstein 				     const char *name)
327c914c0e2SAmir Goldstein {
3284609e1f1SChristian Brauner 	int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name);
329e9be9d5eSMiklos Szeredi 	pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
330e9be9d5eSMiklos Szeredi 	return err;
331e9be9d5eSMiklos Szeredi }
332e9be9d5eSMiklos Szeredi 
333c914c0e2SAmir Goldstein static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
334c914c0e2SAmir Goldstein 				  enum ovl_xattr ox)
335c914c0e2SAmir Goldstein {
336c914c0e2SAmir Goldstein 	return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox));
337c914c0e2SAmir Goldstein }
338c914c0e2SAmir Goldstein 
3390e641857SChristian Brauner static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry,
3400e641857SChristian Brauner 				 const char *acl_name, struct posix_acl *acl)
3410e641857SChristian Brauner {
34213e83a49SChristian Brauner 	return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl);
3430e641857SChristian Brauner }
3440e641857SChristian Brauner 
3450e641857SChristian Brauner static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry,
3460e641857SChristian Brauner 				    const char *acl_name)
3470e641857SChristian Brauner {
34813e83a49SChristian Brauner 	return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name);
3490e641857SChristian Brauner }
3500e641857SChristian Brauner 
351576bb263SChristian Brauner static inline int ovl_do_rename(struct ovl_fs *ofs, struct inode *olddir,
352576bb263SChristian Brauner 				struct dentry *olddentry, struct inode *newdir,
353576bb263SChristian Brauner 				struct dentry *newdentry, unsigned int flags)
354e9be9d5eSMiklos Szeredi {
355e9be9d5eSMiklos Szeredi 	int err;
3569fe61450SChristian Brauner 	struct renamedata rd = {
357abf08576SChristian Brauner 		.old_mnt_idmap	= ovl_upper_mnt_idmap(ofs),
3589fe61450SChristian Brauner 		.old_dir 	= olddir,
3599fe61450SChristian Brauner 		.old_dentry 	= olddentry,
360abf08576SChristian Brauner 		.new_mnt_idmap	= ovl_upper_mnt_idmap(ofs),
3619fe61450SChristian Brauner 		.new_dir 	= newdir,
3629fe61450SChristian Brauner 		.new_dentry 	= newdentry,
3639fe61450SChristian Brauner 		.flags 		= flags,
3649fe61450SChristian Brauner 	};
365e9be9d5eSMiklos Szeredi 
3666cf00764SAmir Goldstein 	pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
3679fe61450SChristian Brauner 	err = vfs_rename(&rd);
368e9be9d5eSMiklos Szeredi 	if (err) {
3692773bf00SMiklos Szeredi 		pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
370e9be9d5eSMiklos Szeredi 			 olddentry, newdentry, err);
371e9be9d5eSMiklos Szeredi 	}
372e9be9d5eSMiklos Szeredi 	return err;
373e9be9d5eSMiklos Szeredi }
374e9be9d5eSMiklos Szeredi 
375576bb263SChristian Brauner static inline int ovl_do_whiteout(struct ovl_fs *ofs,
376576bb263SChristian Brauner 				  struct inode *dir, struct dentry *dentry)
377e9be9d5eSMiklos Szeredi {
378abf08576SChristian Brauner 	int err = vfs_whiteout(ovl_upper_mnt_idmap(ofs), dir, dentry);
379e9be9d5eSMiklos Szeredi 	pr_debug("whiteout(%pd2) = %i\n", dentry, err);
380e9be9d5eSMiklos Szeredi 	return err;
381e9be9d5eSMiklos Szeredi }
382e9be9d5eSMiklos Szeredi 
3832b1a7746SMiklos Szeredi static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs,
384576bb263SChristian Brauner 					  struct dentry *dentry, umode_t mode)
385e7f52429SAmir Goldstein {
3862b1a7746SMiklos Szeredi 	struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry };
387d56e0ddbSAmir Goldstein 	struct file *file = kernel_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path,
388d56e0ddbSAmir Goldstein 						mode, O_LARGEFILE | O_WRONLY,
389d56e0ddbSAmir Goldstein 						current_cred());
3902b1a7746SMiklos Szeredi 	int err = PTR_ERR_OR_ZERO(file);
391e7f52429SAmir Goldstein 
392e7f52429SAmir Goldstein 	pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
3932b1a7746SMiklos Szeredi 	return file;
394e7f52429SAmir Goldstein }
395e7f52429SAmir Goldstein 
39622f289ceSChristian Brauner static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs,
39722f289ceSChristian Brauner 					      const char *name,
39822f289ceSChristian Brauner 					      struct dentry *base, int len)
39922f289ceSChristian Brauner {
4004609e1f1SChristian Brauner 	return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len);
40122f289ceSChristian Brauner }
40222f289ceSChristian Brauner 
4030c288874SVivek Goyal static inline bool ovl_open_flags_need_copy_up(int flags)
4040c288874SVivek Goyal {
4050c288874SVivek Goyal 	if (!flags)
4060c288874SVivek Goyal 		return false;
4070c288874SVivek Goyal 
4080c288874SVivek Goyal 	return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
4090c288874SVivek Goyal }
4100c288874SVivek Goyal 
411bbb1e54dSMiklos Szeredi /* util.c */
412d08d3b3cSAmir Goldstein int ovl_get_write_access(struct dentry *dentry);
413d08d3b3cSAmir Goldstein void ovl_put_write_access(struct dentry *dentry);
414d08d3b3cSAmir Goldstein void ovl_start_write(struct dentry *dentry);
415d08d3b3cSAmir Goldstein void ovl_end_write(struct dentry *dentry);
416bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry);
417bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry);
418bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry);
419bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb);
420e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb);
42102bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb);
422f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb);
423f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb);
424163db0daSAmir Goldstein struct ovl_path *ovl_stack_alloc(unsigned int n);
425163db0daSAmir Goldstein void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n);
426163db0daSAmir Goldstein void ovl_stack_put(struct ovl_path *stack, unsigned int n);
427163db0daSAmir Goldstein void ovl_stack_free(struct ovl_path *stack, unsigned int n);
428bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
429163db0daSAmir Goldstein void ovl_free_entry(struct ovl_entry *oe);
430bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry);
431b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
4320af950f5SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
4330af950f5SAmir Goldstein 			   struct ovl_entry *oe);
434b07d5cc9SAmir Goldstein void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
4350af950f5SAmir Goldstein 			   struct ovl_entry *oe, unsigned int mask);
436bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry);
437e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry);
438e9be9d5eSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path);
439e9be9d5eSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path);
4404f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
441b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path);
442e9be9d5eSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
4431248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path);
444e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry);
445e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry);
446647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
44742dd69aeSAmir Goldstein int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath);
448ffa5723cSAmir Goldstein const struct ovl_layer *ovl_i_layer_lower(struct inode *inode);
44913464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
450e9be9d5eSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry);
4511d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode);
45209d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode);
45309d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode);
4542664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode);
45509d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode);
4564823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode);
4572b21da92SAmir Goldstein const char *ovl_lowerdata_redirect(struct inode *inode);
4584edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
4594edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
460c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
461c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
462c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
463e9be9d5eSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry);
464c412ce49SMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry);
4655cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry);
46655acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry);
46755acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry);
4680c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
4690c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
4700c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode);
4710c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode);
472a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry);
473a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
47409d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
475d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity);
4761fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode);
477bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry);
478*bc8df7a3SAlexander Larsson bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path);
4792d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags);
4800c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags);
48139d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry);
4820c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags);
4832d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
48443d193f8SMiklos Szeredi 			      enum ovl_xattr ox);
4852d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path);
486*bc8df7a3SAlexander Larsson bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path);
487*bc8df7a3SAlexander Larsson bool ovl_path_check_xwhiteouts_xattr(struct ovl_fs *ofs, const struct path *path);
488d9544c1bSAmir Goldstein bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
489d9544c1bSAmir Goldstein 			 const struct path *upperpath);
490dad7017aSChristian Brauner 
491*bc8df7a3SAlexander Larsson static inline bool ovl_upper_is_whiteout(struct ovl_fs *ofs,
492*bc8df7a3SAlexander Larsson 					 struct dentry *upperdentry)
493*bc8df7a3SAlexander Larsson {
494*bc8df7a3SAlexander Larsson 	struct path upperpath = {
495*bc8df7a3SAlexander Larsson 		.dentry = upperdentry,
496*bc8df7a3SAlexander Larsson 		.mnt = ovl_upper_mnt(ofs),
497*bc8df7a3SAlexander Larsson 	};
498*bc8df7a3SAlexander Larsson 	return ovl_path_is_whiteout(ofs, &upperpath);
499*bc8df7a3SAlexander Larsson }
500*bc8df7a3SAlexander Larsson 
501dad7017aSChristian Brauner static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
502dad7017aSChristian Brauner 					  struct dentry *upperdentry)
503dad7017aSChristian Brauner {
504dad7017aSChristian Brauner 	struct path upperpath = {
505dad7017aSChristian Brauner 		.dentry = upperdentry,
506dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
507dad7017aSChristian Brauner 	};
508dad7017aSChristian Brauner 	return ovl_path_check_origin_xattr(ofs, &upperpath);
509dad7017aSChristian Brauner }
510dad7017aSChristian Brauner 
511a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
51243d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
513f3a15685SAmir Goldstein 		       int xerr);
514f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
515ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry);
516ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry);
517146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry);
51824b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry);
5190e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry);
5200e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry);
5215820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
522bf070890SAlexander Larsson int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
523bf070890SAlexander Larsson 			     struct ovl_metacopy *data);
524184996e9SAlexander Larsson int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d,
525184996e9SAlexander Larsson 			   struct ovl_metacopy *metacopy);
52667d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry);
5272d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding);
5280c71faf5SAlexander Larsson int ovl_ensure_verity_loaded(struct path *path);
529184996e9SAlexander Larsson int ovl_get_verity_xattr(struct ovl_fs *ofs, const struct path *path,
530184996e9SAlexander Larsson 			 u8 *digest_buf, int *buf_length);
531184996e9SAlexander Larsson int ovl_validate_verity(struct ovl_fs *ofs,
532184996e9SAlexander Larsson 			struct path *metapath,
533184996e9SAlexander Larsson 			struct path *datapath);
5340c71faf5SAlexander Larsson int ovl_get_verity_digest(struct ovl_fs *ofs, struct path *src,
5350c71faf5SAlexander Larsson 			  struct ovl_metacopy *metacopy);
536335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs);
537f3a15685SAmir Goldstein 
53865cd913eSAmir Goldstein static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
53965cd913eSAmir Goldstein {
54065cd913eSAmir Goldstein 	set_bit(flag, &OVL_I(inode)->flags);
54165cd913eSAmir Goldstein }
54265cd913eSAmir Goldstein 
54365cd913eSAmir Goldstein static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
54465cd913eSAmir Goldstein {
54565cd913eSAmir Goldstein 	clear_bit(flag, &OVL_I(inode)->flags);
54665cd913eSAmir Goldstein }
54765cd913eSAmir Goldstein 
54865cd913eSAmir Goldstein static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
54965cd913eSAmir Goldstein {
55065cd913eSAmir Goldstein 	return test_bit(flag, &OVL_I(inode)->flags);
55165cd913eSAmir Goldstein }
55265cd913eSAmir Goldstein 
553610afc0bSMiklos Szeredi static inline bool ovl_is_impuredir(struct super_block *sb,
554dad7017aSChristian Brauner 				    struct dentry *upperdentry)
555f3a15685SAmir Goldstein {
556dad7017aSChristian Brauner 	struct ovl_fs *ofs = OVL_FS(sb);
557dad7017aSChristian Brauner 	struct path upperpath = {
558dad7017aSChristian Brauner 		.dentry = upperdentry,
559dad7017aSChristian Brauner 		.mnt = ovl_upper_mnt(ofs),
560dad7017aSChristian Brauner 	};
561dad7017aSChristian Brauner 
562dad7017aSChristian Brauner 	return ovl_path_check_dir_xattr(ofs, &upperpath, OVL_XATTR_IMPURE);
563f3a15685SAmir Goldstein }
564f3a15685SAmir Goldstein 
565af5f2396SAmir Goldstein static inline bool ovl_redirect_follow(struct ovl_fs *ofs)
566af5f2396SAmir Goldstein {
567af5f2396SAmir Goldstein 	return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW;
568af5f2396SAmir Goldstein }
569af5f2396SAmir Goldstein 
570af5f2396SAmir Goldstein static inline bool ovl_redirect_dir(struct ovl_fs *ofs)
571af5f2396SAmir Goldstein {
572af5f2396SAmir Goldstein 	return ofs->config.redirect_mode == OVL_REDIRECT_ON;
573af5f2396SAmir Goldstein }
574af5f2396SAmir Goldstein 
575b0504bfeSAmir Goldstein static inline bool ovl_origin_uuid(struct ovl_fs *ofs)
576b0504bfeSAmir Goldstein {
577b0504bfeSAmir Goldstein 	return ofs->config.uuid != OVL_UUID_OFF;
578b0504bfeSAmir Goldstein }
579b0504bfeSAmir Goldstein 
580b0504bfeSAmir Goldstein static inline bool ovl_has_fsid(struct ovl_fs *ofs)
581b0504bfeSAmir Goldstein {
582cbb44f09SAmir Goldstein 	return ofs->config.uuid == OVL_UUID_ON ||
583cbb44f09SAmir Goldstein 	       ofs->config.uuid == OVL_UUID_AUTO;
584b0504bfeSAmir Goldstein }
585b0504bfeSAmir Goldstein 
586926e94d7SAmir Goldstein /*
587926e94d7SAmir Goldstein  * With xino=auto, we do best effort to keep all inodes on same st_dev and
588926e94d7SAmir Goldstein  * d_ino consistent with st_ino.
589926e94d7SAmir Goldstein  * With xino=on, we do the same effort but we warn if we failed.
590926e94d7SAmir Goldstein  */
591dcb399deSAmir Goldstein static inline bool ovl_xino_warn(struct ovl_fs *ofs)
592926e94d7SAmir Goldstein {
593dcb399deSAmir Goldstein 	return ofs->config.xino == OVL_XINO_ON;
594926e94d7SAmir Goldstein }
595926e94d7SAmir Goldstein 
596af5f2396SAmir Goldstein /*
597af5f2396SAmir Goldstein  * To avoid regressions in existing setups with overlay lower offline changes,
598af5f2396SAmir Goldstein  * we allow lower changes only if none of the new features are used.
599af5f2396SAmir Goldstein  */
600af5f2396SAmir Goldstein static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs)
601af5f2396SAmir Goldstein {
602af5f2396SAmir Goldstein 	return (!ofs->config.index && !ofs->config.metacopy &&
603af5f2396SAmir Goldstein 		!ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs));
6040f831ec8SAmir Goldstein }
605e487d889SAmir Goldstein 
606e487d889SAmir Goldstein /* All layers on same fs? */
607dcb399deSAmir Goldstein static inline bool ovl_same_fs(struct ovl_fs *ofs)
608bbb1e54dSMiklos Szeredi {
609dcb399deSAmir Goldstein 	return ofs->xino_mode == 0;
6108a22efa1SAmir Goldstein }
6118a22efa1SAmir Goldstein 
6128a22efa1SAmir Goldstein /* All overlay inodes have same st_dev? */
613dcb399deSAmir Goldstein static inline bool ovl_same_dev(struct ovl_fs *ofs)
6141e92e307SAmir Goldstein {
615dcb399deSAmir Goldstein 	return ofs->xino_mode >= 0;
6161e92e307SAmir Goldstein }
6171e92e307SAmir Goldstein 
618dcb399deSAmir Goldstein static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs)
6191e92e307SAmir Goldstein {
620dcb399deSAmir Goldstein 	return ovl_same_dev(ofs) ? ofs->xino_mode : 0;
621f941866fSAmir Goldstein }
62205122443SAmir Goldstein 
623531d3040SAmir Goldstein static inline void ovl_inode_lock(struct inode *inode)
624531d3040SAmir Goldstein {
625531d3040SAmir Goldstein 	mutex_lock(&OVL_I(inode)->lock);
626531d3040SAmir Goldstein }
627531d3040SAmir Goldstein 
628531d3040SAmir Goldstein static inline int ovl_inode_lock_interruptible(struct inode *inode)
6291e92e307SAmir Goldstein {
6301e92e307SAmir Goldstein 	return mutex_lock_interruptible(&OVL_I(inode)->lock);
6311e92e307SAmir Goldstein }
6321e92e307SAmir Goldstein 
6331e92e307SAmir Goldstein static inline void ovl_inode_unlock(struct inode *inode)
6341e92e307SAmir Goldstein {
6351e92e307SAmir Goldstein 	mutex_unlock(&OVL_I(inode)->lock);
6361e92e307SAmir Goldstein }
6371e92e307SAmir Goldstein 
638e9be9d5eSMiklos Szeredi 
639bbb1e54dSMiklos Szeredi /* namei.c */
640cbe7fba8SAmir Goldstein int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
641cbe7fba8SAmir Goldstein 
642cbe7fba8SAmir Goldstein static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
643cbe7fba8SAmir Goldstein {
644522f6e6cSAmir Goldstein 	if (fh_len < sizeof(struct ovl_fh))
645522f6e6cSAmir Goldstein 		return -EINVAL;
646522f6e6cSAmir Goldstein 
647cbe7fba8SAmir Goldstein 	return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
648cbe7fba8SAmir Goldstein }
649cbe7fba8SAmir Goldstein 
6501cdb0cb6SPavel Tikhomirov struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
6511cdb0cb6SPavel Tikhomirov 				  struct vfsmount *mnt, bool connected);
65205122443SAmir Goldstein int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
6531eff1a1dSAmir Goldstein 			struct dentry *upperdentry, struct ovl_path **stackp);
654610afc0bSMiklos Szeredi int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
6555b02bfc1SAmir Goldstein 		      enum ovl_xattr ox, const struct ovl_fh *fh,
6565b02bfc1SAmir Goldstein 		      bool is_upper, bool set);
6575b02bfc1SAmir Goldstein int ovl_verify_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry,
6585b02bfc1SAmir Goldstein 			    enum ovl_xattr ox, struct dentry *real,
6595b02bfc1SAmir Goldstein 			    bool is_upper, bool set);
6608ea28765SAmir Goldstein struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index,
6618ea28765SAmir Goldstein 			       bool connected);
662415543d5SAmir Goldstein int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
6635b02bfc1SAmir Goldstein int ovl_get_index_name_fh(const struct ovl_fh *fh, struct qstr *name);
6641cdb0cb6SPavel Tikhomirov int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
6651cdb0cb6SPavel Tikhomirov 		       struct qstr *name);
66691ffe7beSAmir Goldstein struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
66706170154SAmir Goldstein struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
66806170154SAmir Goldstein 				struct dentry *origin, bool verify);
669bbb1e54dSMiklos Szeredi int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
670184996e9SAlexander Larsson int ovl_verify_lowerdata(struct dentry *dentry);
6711eff1a1dSAmir Goldstein struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
6721eff1a1dSAmir Goldstein 			  unsigned int flags);
673bbb1e54dSMiklos Szeredi bool ovl_lower_positive(struct dentry *dentry);
674e9be9d5eSMiklos Szeredi 
6755b02bfc1SAmir Goldstein static inline int ovl_verify_origin_fh(struct ovl_fs *ofs, struct dentry *upper,
6765b02bfc1SAmir Goldstein 				       const struct ovl_fh *fh, bool set)
6775b02bfc1SAmir Goldstein {
6785b02bfc1SAmir Goldstein 	return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, fh, false, set);
6795b02bfc1SAmir Goldstein }
6805b02bfc1SAmir Goldstein 
681610afc0bSMiklos Szeredi static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
68205122443SAmir Goldstein 				    struct dentry *origin, bool set)
68305122443SAmir Goldstein {
6845b02bfc1SAmir Goldstein 	return ovl_verify_origin_xattr(ofs, upper, OVL_XATTR_ORIGIN, origin,
685610afc0bSMiklos Szeredi 				       false, set);
68605122443SAmir Goldstein }
68705122443SAmir Goldstein 
688610afc0bSMiklos Szeredi static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
689ad1d615cSAmir Goldstein 				   struct dentry *upper, bool set)
690ad1d615cSAmir Goldstein {
6915b02bfc1SAmir Goldstein 	return ovl_verify_origin_xattr(ofs, index, OVL_XATTR_UPPER, upper,
6925b02bfc1SAmir Goldstein 				       true, set);
693ad1d615cSAmir Goldstein }
694ad1d615cSAmir Goldstein 
695e9be9d5eSMiklos Szeredi /* readdir.c */
696e9be9d5eSMiklos Szeredi extern const struct file_operations ovl_dir_operations;
69761536bedSAmir Goldstein struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
698e9be9d5eSMiklos Szeredi int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
699576bb263SChristian Brauner void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
700576bb263SChristian Brauner 			   struct list_head *list);
701e9be9d5eSMiklos Szeredi void ovl_cache_free(struct list_head *list);
7024edb83bbSMiklos Szeredi void ovl_dir_cache_free(struct inode *inode);
7032d343087SAl Viro int ovl_check_d_type_supported(const struct path *realpath);
704576bb263SChristian Brauner int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
705576bb263SChristian Brauner 			struct vfsmount *mnt, struct dentry *dentry, int level);
7061eff1a1dSAmir Goldstein int ovl_indexdir_cleanup(struct ovl_fs *ofs);
707e9be9d5eSMiklos Szeredi 
70865cd913eSAmir Goldstein /*
70965cd913eSAmir Goldstein  * Can we iterate real dir directly?
71065cd913eSAmir Goldstein  *
71165cd913eSAmir Goldstein  * Non-merge dir may contain whiteouts from a time it was a merge upper, before
71265cd913eSAmir Goldstein  * lower dir was removed under it and possibly before it was rotated from upper
71365cd913eSAmir Goldstein  * to lower layer.
71465cd913eSAmir Goldstein  */
7151fa9c5c5SMiklos Szeredi static inline bool ovl_dir_is_real(struct inode *dir)
71665cd913eSAmir Goldstein {
7171fa9c5c5SMiklos Szeredi 	return !ovl_test_flag(OVL_WHITEOUTS, dir);
71865cd913eSAmir Goldstein }
71965cd913eSAmir Goldstein 
720e9be9d5eSMiklos Szeredi /* inode.c */
7215f8415d6SAmir Goldstein int ovl_set_nlink_upper(struct dentry *dentry);
7225f8415d6SAmir Goldstein int ovl_set_nlink_lower(struct dentry *dentry);
723610afc0bSMiklos Szeredi unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
724caf70cb2SAmir Goldstein 			   struct dentry *upperdentry,
725caf70cb2SAmir Goldstein 			   unsigned int fallback);
7264609e1f1SChristian Brauner int ovl_permission(struct mnt_idmap *idmap, struct inode *inode,
727549c7297SChristian Brauner 		   int mask);
728ded53656SYang Xu 
729ded53656SYang Xu #ifdef CONFIG_FS_POSIX_ACL
73077435322SChristian Brauner struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
7316c0a8bfbSChristian Brauner 				 struct inode *inode, int type,
7326c0a8bfbSChristian Brauner 				 bool rcu, bool noperm);
7336c0a8bfbSChristian Brauner static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type,
7346c0a8bfbSChristian Brauner 						  bool rcu)
7356c0a8bfbSChristian Brauner {
73677435322SChristian Brauner 	return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true);
7376c0a8bfbSChristian Brauner }
73877435322SChristian Brauner static inline struct posix_acl *ovl_get_acl(struct mnt_idmap *idmap,
7396c0a8bfbSChristian Brauner 					    struct dentry *dentry, int type)
7406c0a8bfbSChristian Brauner {
74177435322SChristian Brauner 	return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false);
7426c0a8bfbSChristian Brauner }
74313e83a49SChristian Brauner int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
7440e641857SChristian Brauner 		struct posix_acl *acl, int type);
74531acceb9SChristian Brauner struct posix_acl *ovl_get_acl_path(const struct path *path,
74631acceb9SChristian Brauner 				   const char *acl_name, bool noperm);
747ded53656SYang Xu #else
7486c0a8bfbSChristian Brauner #define ovl_get_inode_acl	NULL
749ded53656SYang Xu #define ovl_get_acl		NULL
7500e641857SChristian Brauner #define ovl_set_acl		NULL
75131acceb9SChristian Brauner static inline struct posix_acl *ovl_get_acl_path(const struct path *path,
75231acceb9SChristian Brauner 						 const char *acl_name,
75331acceb9SChristian Brauner 						 bool noperm)
75431acceb9SChristian Brauner {
75531acceb9SChristian Brauner 	return NULL;
75631acceb9SChristian Brauner }
757ded53656SYang Xu #endif
758ded53656SYang Xu 
759913e9928SJeff Layton int ovl_update_time(struct inode *inode, int flags);
760610afc0bSMiklos Szeredi bool ovl_is_private_xattr(struct super_block *sb, const char *name);
761e9be9d5eSMiklos Szeredi 
762ac6a52ebSVivek Goyal struct ovl_inode_params {
76301b39dccSAmir Goldstein 	struct inode *newinode;
764ac6a52ebSVivek Goyal 	struct dentry *upperdentry;
7650af950f5SAmir Goldstein 	struct ovl_entry *oe;
76674c6e384SMiklos Szeredi 	bool index;
7679cec54c8SVivek Goyal 	char *redirect;
7682b21da92SAmir Goldstein 	char *lowerdata_redirect;
769ac6a52ebSVivek Goyal };
77062c832edSAmir Goldstein void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
77162c832edSAmir Goldstein 		    unsigned long ino, int fsid);
772ca4c8a3aSMiklos Szeredi struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
7734b91c30aSAmir Goldstein struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
7744b91c30aSAmir Goldstein 			       bool is_upper);
775146d62e5SAmir Goldstein bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
776146d62e5SAmir Goldstein struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
777ac6a52ebSVivek Goyal struct inode *ovl_get_inode(struct super_block *sb,
778ac6a52ebSVivek Goyal 			    struct ovl_inode_params *oip);
7792878dffcSChristian Brauner void ovl_copyattr(struct inode *to);
780e9be9d5eSMiklos Szeredi 
78172db8211SAmir Goldstein /* vfs inode flags copied from real to ovl inode */
78272db8211SAmir Goldstein #define OVL_COPY_I_FLAGS_MASK	(S_SYNC | S_NOATIME | S_APPEND | S_IMMUTABLE)
783096a218aSAmir Goldstein /* vfs inode flags read from overlay.protattr xattr to ovl inode */
784096a218aSAmir Goldstein #define OVL_PROT_I_FLAGS_MASK	(S_APPEND | S_IMMUTABLE)
78572db8211SAmir Goldstein 
78672db8211SAmir Goldstein /*
78772db8211SAmir Goldstein  * fileattr flags copied from lower to upper inode on copy up.
788096a218aSAmir Goldstein  * We cannot copy up immutable/append-only flags, because that would prevent
789096a218aSAmir Goldstein  * linking temp inode to upper dir, so we store them in xattr instead.
79072db8211SAmir Goldstein  */
79172db8211SAmir Goldstein #define OVL_COPY_FS_FLAGS_MASK	(FS_SYNC_FL | FS_NOATIME_FL)
79272db8211SAmir Goldstein #define OVL_COPY_FSX_FLAGS_MASK	(FS_XFLAG_SYNC | FS_XFLAG_NOATIME)
793096a218aSAmir Goldstein #define OVL_PROT_FS_FLAGS_MASK  (FS_APPEND_FL | FS_IMMUTABLE_FL)
794096a218aSAmir Goldstein #define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE)
795096a218aSAmir Goldstein 
796096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper);
797096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper,
798096a218aSAmir Goldstein 		      struct fileattr *fa);
79972db8211SAmir Goldstein 
8004f357295SMiklos Szeredi static inline void ovl_copyflags(struct inode *from, struct inode *to)
8014f357295SMiklos Szeredi {
80272db8211SAmir Goldstein 	unsigned int mask = OVL_COPY_I_FLAGS_MASK;
8034f357295SMiklos Szeredi 
8044f357295SMiklos Szeredi 	inode_set_flags(to, from->i_flags & mask, mask);
8054f357295SMiklos Szeredi }
8064f357295SMiklos Szeredi 
807e9be9d5eSMiklos Szeredi /* dir.c */
808e9be9d5eSMiklos Szeredi extern const struct inode_operations ovl_dir_inode_operations;
809c21c839bSChengguang Xu int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
810e7dd0e71SAmir Goldstein 			     struct dentry *dentry);
811471ec5dcSAmir Goldstein struct ovl_cattr {
81232a3d848SAl Viro 	dev_t rdev;
81332a3d848SAl Viro 	umode_t mode;
81432a3d848SAl Viro 	const char *link;
815471ec5dcSAmir Goldstein 	struct dentry *hardlink;
81632a3d848SAl Viro };
817471ec5dcSAmir Goldstein 
818471ec5dcSAmir Goldstein #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
819471ec5dcSAmir Goldstein 
820576bb263SChristian Brauner int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
821576bb263SChristian Brauner 		   struct dentry **newdentry, umode_t mode);
822576bb263SChristian Brauner struct dentry *ovl_create_real(struct ovl_fs *ofs,
823576bb263SChristian Brauner 			       struct inode *dir, struct dentry *newdentry,
824471ec5dcSAmir Goldstein 			       struct ovl_cattr *attr);
825576bb263SChristian Brauner int ovl_cleanup(struct ovl_fs *ofs, struct inode *dir, struct dentry *dentry);
826576bb263SChristian Brauner struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir);
827576bb263SChristian Brauner struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
828576bb263SChristian Brauner 			       struct ovl_cattr *attr);
829e9be9d5eSMiklos Szeredi 
830d1d04ef8SMiklos Szeredi /* file.c */
831d1d04ef8SMiklos Szeredi extern const struct file_operations ovl_file_operations;
8322406a307SJiufei Xue int __init ovl_aio_request_cache_init(void);
8332406a307SJiufei Xue void ovl_aio_request_cache_destroy(void);
8342d343087SAl Viro int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa);
8352d343087SAl Viro int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa);
83666dbfabfSMiklos Szeredi int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa);
8378782a9aeSChristian Brauner int ovl_fileattr_set(struct mnt_idmap *idmap,
83866dbfabfSMiklos Szeredi 		     struct dentry *dentry, struct fileattr *fa);
839d1d04ef8SMiklos Szeredi 
840e9be9d5eSMiklos Szeredi /* copy_up.c */
841e9be9d5eSMiklos Szeredi int ovl_copy_up(struct dentry *dentry);
842d1e6f6a9SVivek Goyal int ovl_copy_up_with_data(struct dentry *dentry);
8433428030dSAmir Goldstein int ovl_maybe_copy_up(struct dentry *dentry, int flags);
8442d343087SAl Viro int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new);
8455272eaf3SChristian Brauner int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat);
8461cdb0cb6SPavel Tikhomirov struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
8471cdb0cb6SPavel Tikhomirov 				  bool is_upper);
8485b02bfc1SAmir Goldstein struct ovl_fh *ovl_get_origin_fh(struct ovl_fs *ofs, struct dentry *origin);
8495b02bfc1SAmir Goldstein int ovl_set_origin_fh(struct ovl_fs *ofs, const struct ovl_fh *fh,
850a0c236b1SAmir Goldstein 		      struct dentry *upper);
8518ed5eec9SAmir Goldstein 
8528ed5eec9SAmir Goldstein /* export.c */
8538ed5eec9SAmir Goldstein extern const struct export_operations ovl_export_operations;
85416aac5adSAmir Goldstein extern const struct export_operations ovl_export_fid_operations;
8557fb7998bSChristian Brauner 
8567fb7998bSChristian Brauner /* super.c */
8577fb7998bSChristian Brauner int ovl_fill_super(struct super_block *sb, struct fs_context *fc);
8587fb7998bSChristian Brauner 
8597fb7998bSChristian Brauner /* Will this overlay be forced to mount/remount ro? */
8607fb7998bSChristian Brauner static inline bool ovl_force_readonly(struct ovl_fs *ofs)
8617fb7998bSChristian Brauner {
8627fb7998bSChristian Brauner 	return (!ovl_upper_mnt(ofs) || !ofs->workdir);
8637fb7998bSChristian Brauner }
864420a62ddSAmir Goldstein 
865420a62ddSAmir Goldstein /* xattr.c */
866420a62ddSAmir Goldstein 
867420a62ddSAmir Goldstein const struct xattr_handler * const *ovl_xattr_handlers(struct ovl_fs *ofs);
868420a62ddSAmir Goldstein int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
869420a62ddSAmir Goldstein 		struct iattr *attr);
870420a62ddSAmir Goldstein int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
871420a62ddSAmir Goldstein 		struct kstat *stat, u32 request_mask, unsigned int flags);
872420a62ddSAmir Goldstein ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
873