xref: /openbmc/linux/fs/overlayfs/ovl_entry.h (revision a535116d)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2bbb1e54dSMiklos Szeredi /*
3bbb1e54dSMiklos Szeredi  *
4bbb1e54dSMiklos Szeredi  * Copyright (C) 2011 Novell Inc.
5bbb1e54dSMiklos Szeredi  * Copyright (C) 2016 Red Hat, Inc.
6bbb1e54dSMiklos Szeredi  */
7bbb1e54dSMiklos Szeredi 
8bbb1e54dSMiklos Szeredi struct ovl_config {
9bbb1e54dSMiklos Szeredi 	char *upperdir;
10bbb1e54dSMiklos Szeredi 	char *workdir;
11*a535116dSAmir Goldstein 	char **lowerdirs;
12bbb1e54dSMiklos Szeredi 	bool default_permissions;
13af5f2396SAmir Goldstein 	int redirect_mode;
14ae8cba40SAlexander Larsson 	int verity_mode;
1502bcd157SAmir Goldstein 	bool index;
16b0504bfeSAmir Goldstein 	int uuid;
17f168f109SAmir Goldstein 	bool nfs_export;
18795939a9SAmir Goldstein 	int xino;
19d5791044SVivek Goyal 	bool metacopy;
202d2f2d73SMiklos Szeredi 	bool userxattr;
21c86243b0SVivek Goyal 	bool ovl_volatile;
22bbb1e54dSMiklos Szeredi };
23bbb1e54dSMiklos Szeredi 
245148626bSAmir Goldstein struct ovl_sb {
255148626bSAmir Goldstein 	struct super_block *sb;
265148626bSAmir Goldstein 	dev_t pseudo_dev;
277e63c87fSAmir Goldstein 	/* Unusable (conflicting) uuid */
287e63c87fSAmir Goldstein 	bool bad_uuid;
291b81ddddSAmir Goldstein 	/* Used as a lower layer (but maybe also as upper) */
301b81ddddSAmir Goldstein 	bool is_lower;
315148626bSAmir Goldstein };
325148626bSAmir Goldstein 
33b9343632SChandan Rajendra struct ovl_layer {
34f723edb8SChristian Brauner 	/* ovl_free_fs() relies on @mnt being the first member! */
35b9343632SChandan Rajendra 	struct vfsmount *mnt;
36146d62e5SAmir Goldstein 	/* Trap in ovl inode cache */
37146d62e5SAmir Goldstein 	struct inode *trap;
385148626bSAmir Goldstein 	struct ovl_sb *fs;
395148626bSAmir Goldstein 	/* Index of this layer in fs root (upper idx == 0) */
40d583ed7dSAmir Goldstein 	int idx;
415148626bSAmir Goldstein 	/* One fsid per unique underlying sb (upper fsid == 0) */
425148626bSAmir Goldstein 	int fsid;
43b9343632SChandan Rajendra };
44b9343632SChandan Rajendra 
45b9343632SChandan Rajendra struct ovl_path {
4613464165SMiklos Szeredi 	const struct ovl_layer *layer;
47b9343632SChandan Rajendra 	struct dentry *dentry;
48b9343632SChandan Rajendra };
49b9343632SChandan Rajendra 
500af950f5SAmir Goldstein struct ovl_entry {
510af950f5SAmir Goldstein 	unsigned int __numlower;
520af950f5SAmir Goldstein 	struct ovl_path __lowerstack[];
530af950f5SAmir Goldstein };
540af950f5SAmir Goldstein 
55bbb1e54dSMiklos Szeredi /* private information held for overlayfs's superblock */
56bbb1e54dSMiklos Szeredi struct ovl_fs {
5794375f9dSAmir Goldstein 	unsigned int numlayer;
5807f1e596SAmir Goldstein 	/* Number of unique fs among layers including upper fs */
5907f1e596SAmir Goldstein 	unsigned int numfs;
6037ebf056SAmir Goldstein 	/* Number of data-only lower layers */
6137ebf056SAmir Goldstein 	unsigned int numdatalayer;
6213464165SMiklos Szeredi 	const struct ovl_layer *layers;
6307f1e596SAmir Goldstein 	struct ovl_sb *fs;
642cac0c00SAmir Goldstein 	/* workbasedir is the path at workdir= mount option */
652cac0c00SAmir Goldstein 	struct dentry *workbasedir;
662cac0c00SAmir Goldstein 	/* workdir is the 'work' directory under workbasedir */
67bbb1e54dSMiklos Szeredi 	struct dentry *workdir;
6802bcd157SAmir Goldstein 	/* index directory listing overlay inodes by origin file handle */
6902bcd157SAmir Goldstein 	struct dentry *indexdir;
706b2d5fe4SMiklos Szeredi 	long namelen;
71bbb1e54dSMiklos Szeredi 	/* pathnames of lower and upper dirs, for show_options */
72bbb1e54dSMiklos Szeredi 	struct ovl_config config;
73bbb1e54dSMiklos Szeredi 	/* creds of process who forced instantiation of super block */
74bbb1e54dSMiklos Szeredi 	const struct cred *creator_cred;
75e7f52429SAmir Goldstein 	bool tmpfile;
7682b749b2SAmir Goldstein 	bool noxattr;
7716aac5adSAmir Goldstein 	bool nofh;
7885fdee1eSAmir Goldstein 	/* Did we take the inuse lock? */
7985fdee1eSAmir Goldstein 	bool upperdir_locked;
8085fdee1eSAmir Goldstein 	bool workdir_locked;
81146d62e5SAmir Goldstein 	/* Traps in ovl inode cache */
820be0bfd2SAmir Goldstein 	struct inode *workbasedir_trap;
83146d62e5SAmir Goldstein 	struct inode *workdir_trap;
84146d62e5SAmir Goldstein 	struct inode *indexdir_trap;
850f831ec8SAmir Goldstein 	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
860f831ec8SAmir Goldstein 	int xino_mode;
874d314f78SAmir Goldstein 	/* For allocation of non-persistent inode numbers */
884d314f78SAmir Goldstein 	atomic_long_t last_ino;
89e4599d4bSAmir Goldstein 	/* Shared whiteout cache */
90c21c839bSChengguang Xu 	struct dentry *whiteout;
91e4599d4bSAmir Goldstein 	bool no_shared_whiteout;
92335d3fc5SSargun Dhillon 	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
93335d3fc5SSargun Dhillon 	errseq_t errseq;
94bbb1e54dSMiklos Szeredi };
95bbb1e54dSMiklos Szeredi 
9637ebf056SAmir Goldstein /* Number of lower layers, not including data-only layers */
ovl_numlowerlayer(struct ovl_fs * ofs)9737ebf056SAmir Goldstein static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs)
9837ebf056SAmir Goldstein {
9937ebf056SAmir Goldstein 	return ofs->numlayer - ofs->numdatalayer - 1;
10037ebf056SAmir Goldstein }
10137ebf056SAmir Goldstein 
ovl_upper_mnt(struct ovl_fs * ofs)10208f4c7c8SMiklos Szeredi static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
10308f4c7c8SMiklos Szeredi {
104b8e42a65SMiklos Szeredi 	return ofs->layers[0].mnt;
10508f4c7c8SMiklos Szeredi }
10608f4c7c8SMiklos Szeredi 
ovl_upper_mnt_idmap(struct ovl_fs * ofs)107abf08576SChristian Brauner static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs)
108abf08576SChristian Brauner {
109abf08576SChristian Brauner 	return mnt_idmap(ovl_upper_mnt(ofs));
110abf08576SChristian Brauner }
111abf08576SChristian Brauner 
112f01d0889SAndrea Righi extern struct file_system_type ovl_fs_type;
113f01d0889SAndrea Righi 
OVL_FS(struct super_block * sb)1140f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb)
1150f831ec8SAmir Goldstein {
116adcd459fSAndrea Righi 	if (IS_ENABLED(CONFIG_OVERLAY_FS_DEBUG))
117adcd459fSAndrea Righi 		WARN_ON_ONCE(sb->s_type != &ovl_fs_type);
118adcd459fSAndrea Righi 
1190f831ec8SAmir Goldstein 	return (struct ovl_fs *)sb->s_fs_info;
1200f831ec8SAmir Goldstein }
1210f831ec8SAmir Goldstein 
ovl_should_sync(struct ovl_fs * ofs)122c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs)
123c86243b0SVivek Goyal {
124c86243b0SVivek Goyal 	return !ofs->config.ovl_volatile;
125c86243b0SVivek Goyal }
126c86243b0SVivek Goyal 
ovl_numlower(struct ovl_entry * oe)1275522c9c7SAmir Goldstein static inline unsigned int ovl_numlower(struct ovl_entry *oe)
1285522c9c7SAmir Goldstein {
1295522c9c7SAmir Goldstein 	return oe ? oe->__numlower : 0;
1305522c9c7SAmir Goldstein }
1315522c9c7SAmir Goldstein 
ovl_lowerstack(struct ovl_entry * oe)1325522c9c7SAmir Goldstein static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe)
1335522c9c7SAmir Goldstein {
1345522c9c7SAmir Goldstein 	return ovl_numlower(oe) ? oe->__lowerstack : NULL;
1355522c9c7SAmir Goldstein }
1365522c9c7SAmir Goldstein 
ovl_lowerpath(struct ovl_entry * oe)137ac900ed4SAmir Goldstein static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe)
138ac900ed4SAmir Goldstein {
139ac900ed4SAmir Goldstein 	return ovl_lowerstack(oe);
140ac900ed4SAmir Goldstein }
141ac900ed4SAmir Goldstein 
ovl_lowerdata(struct ovl_entry * oe)142ab1eb5ffSAmir Goldstein static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe)
143ab1eb5ffSAmir Goldstein {
144ab1eb5ffSAmir Goldstein 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
145ab1eb5ffSAmir Goldstein 
146ab1eb5ffSAmir Goldstein 	return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL;
147ab1eb5ffSAmir Goldstein }
148ab1eb5ffSAmir Goldstein 
1492b21da92SAmir Goldstein /* May return NULL if lazy lookup of lowerdata is needed */
ovl_lowerdata_dentry(struct ovl_entry * oe)150ab1eb5ffSAmir Goldstein static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe)
151ab1eb5ffSAmir Goldstein {
152ab1eb5ffSAmir Goldstein 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
153ab1eb5ffSAmir Goldstein 
15442dd69aeSAmir Goldstein 	return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL;
155ab1eb5ffSAmir Goldstein }
156ab1eb5ffSAmir Goldstein 
1570af950f5SAmir Goldstein /* private information held for every overlayfs dentry */
OVL_E_FLAGS(struct dentry * dentry)158a6ff2bc0SAmir Goldstein static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry)
159a6ff2bc0SAmir Goldstein {
1600af950f5SAmir Goldstein 	return (unsigned long *) &dentry->d_fsdata;
161a6ff2bc0SAmir Goldstein }
162a6ff2bc0SAmir Goldstein 
16313cf199dSAmir Goldstein struct ovl_inode {
1642664bd08SVivek Goyal 	union {
1652664bd08SVivek Goyal 		struct ovl_dir_cache *cache;	/* directory */
1662b21da92SAmir Goldstein 		const char *lowerdata_redirect;	/* regular file */
1672664bd08SVivek Goyal 	};
168cf31c463SMiklos Szeredi 	const char *redirect;
16904a01ac7SMiklos Szeredi 	u64 version;
17013c72075SMiklos Szeredi 	unsigned long flags;
17113cf199dSAmir Goldstein 	struct inode vfs_inode;
17209d8b586SMiklos Szeredi 	struct dentry *__upperdentry;
1730af950f5SAmir Goldstein 	struct ovl_entry *oe;
174a015dafcSAmir Goldstein 
175a015dafcSAmir Goldstein 	/* synchronize copy up and more */
176a015dafcSAmir Goldstein 	struct mutex lock;
17713cf199dSAmir Goldstein };
17813cf199dSAmir Goldstein 
OVL_I(struct inode * inode)17913cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode)
18013cf199dSAmir Goldstein {
18113cf199dSAmir Goldstein 	return container_of(inode, struct ovl_inode, vfs_inode);
18213cf199dSAmir Goldstein }
18309d8b586SMiklos Szeredi 
OVL_I_E(struct inode * inode)1840af950f5SAmir Goldstein static inline struct ovl_entry *OVL_I_E(struct inode *inode)
1850af950f5SAmir Goldstein {
1860af950f5SAmir Goldstein 	return inode ? OVL_I(inode)->oe : NULL;
1870af950f5SAmir Goldstein }
1880af950f5SAmir Goldstein 
OVL_E(struct dentry * dentry)1890af950f5SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry)
1900af950f5SAmir Goldstein {
1910af950f5SAmir Goldstein 	return OVL_I_E(d_inode(dentry));
1920af950f5SAmir Goldstein }
1930af950f5SAmir Goldstein 
ovl_upperdentry_dereference(struct ovl_inode * oi)19409d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
19509d8b586SMiklos Szeredi {
196506458efSWill Deacon 	return READ_ONCE(oi->__upperdentry);
19709d8b586SMiklos Szeredi }
198