xref: /openbmc/linux/fs/overlayfs/ovl_entry.h (revision 42dd69ae)
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 *lowerdir;
10bbb1e54dSMiklos Szeredi 	char *upperdir;
11bbb1e54dSMiklos Szeredi 	char *workdir;
12bbb1e54dSMiklos Szeredi 	bool default_permissions;
13a6c60655SMiklos Szeredi 	bool redirect_dir;
14438c84c2SMiklos Szeredi 	bool redirect_follow;
15438c84c2SMiklos Szeredi 	const char *redirect_mode;
1602bcd157SAmir Goldstein 	bool index;
175830fb6bSPavel Tikhomirov 	bool uuid;
18f168f109SAmir Goldstein 	bool nfs_export;
19795939a9SAmir Goldstein 	int xino;
20d5791044SVivek Goyal 	bool metacopy;
212d2f2d73SMiklos Szeredi 	bool userxattr;
22c86243b0SVivek Goyal 	bool ovl_volatile;
23bbb1e54dSMiklos Szeredi };
24bbb1e54dSMiklos Szeredi 
255148626bSAmir Goldstein struct ovl_sb {
265148626bSAmir Goldstein 	struct super_block *sb;
275148626bSAmir Goldstein 	dev_t pseudo_dev;
287e63c87fSAmir Goldstein 	/* Unusable (conflicting) uuid */
297e63c87fSAmir Goldstein 	bool bad_uuid;
301b81ddddSAmir Goldstein 	/* Used as a lower layer (but maybe also as upper) */
311b81ddddSAmir Goldstein 	bool is_lower;
325148626bSAmir Goldstein };
335148626bSAmir Goldstein 
34b9343632SChandan Rajendra struct ovl_layer {
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;
7785fdee1eSAmir Goldstein 	/* Did we take the inuse lock? */
7885fdee1eSAmir Goldstein 	bool upperdir_locked;
7985fdee1eSAmir Goldstein 	bool workdir_locked;
80c21c839bSChengguang Xu 	bool share_whiteout;
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;
89c21c839bSChengguang Xu 	/* Whiteout dentry cache */
90c21c839bSChengguang Xu 	struct dentry *whiteout;
91335d3fc5SSargun Dhillon 	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
92335d3fc5SSargun Dhillon 	errseq_t errseq;
93bbb1e54dSMiklos Szeredi };
94bbb1e54dSMiklos Szeredi 
9537ebf056SAmir Goldstein 
9637ebf056SAmir Goldstein /* Number of lower layers, not including data-only layers */
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 
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 
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 
1120f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb)
1130f831ec8SAmir Goldstein {
1140f831ec8SAmir Goldstein 	return (struct ovl_fs *)sb->s_fs_info;
1150f831ec8SAmir Goldstein }
1160f831ec8SAmir Goldstein 
117c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs)
118c86243b0SVivek Goyal {
119c86243b0SVivek Goyal 	return !ofs->config.ovl_volatile;
120c86243b0SVivek Goyal }
121c86243b0SVivek Goyal 
1225522c9c7SAmir Goldstein static inline unsigned int ovl_numlower(struct ovl_entry *oe)
1235522c9c7SAmir Goldstein {
1245522c9c7SAmir Goldstein 	return oe ? oe->__numlower : 0;
1255522c9c7SAmir Goldstein }
1265522c9c7SAmir Goldstein 
1275522c9c7SAmir Goldstein static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe)
1285522c9c7SAmir Goldstein {
1295522c9c7SAmir Goldstein 	return ovl_numlower(oe) ? oe->__lowerstack : NULL;
1305522c9c7SAmir Goldstein }
1315522c9c7SAmir Goldstein 
132ac900ed4SAmir Goldstein static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe)
133ac900ed4SAmir Goldstein {
134ac900ed4SAmir Goldstein 	return ovl_lowerstack(oe);
135ac900ed4SAmir Goldstein }
136ac900ed4SAmir Goldstein 
137ab1eb5ffSAmir Goldstein static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe)
138ab1eb5ffSAmir Goldstein {
139ab1eb5ffSAmir Goldstein 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
140ab1eb5ffSAmir Goldstein 
141ab1eb5ffSAmir Goldstein 	return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL;
142ab1eb5ffSAmir Goldstein }
143ab1eb5ffSAmir Goldstein 
1442b21da92SAmir Goldstein /* May return NULL if lazy lookup of lowerdata is needed */
145ab1eb5ffSAmir Goldstein static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe)
146ab1eb5ffSAmir Goldstein {
147ab1eb5ffSAmir Goldstein 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
148ab1eb5ffSAmir Goldstein 
149*42dd69aeSAmir Goldstein 	return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL;
150ab1eb5ffSAmir Goldstein }
151ab1eb5ffSAmir Goldstein 
1520af950f5SAmir Goldstein /* private information held for every overlayfs dentry */
153a6ff2bc0SAmir Goldstein static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry)
154a6ff2bc0SAmir Goldstein {
1550af950f5SAmir Goldstein 	return (unsigned long *) &dentry->d_fsdata;
156a6ff2bc0SAmir Goldstein }
157a6ff2bc0SAmir Goldstein 
15813cf199dSAmir Goldstein struct ovl_inode {
1592664bd08SVivek Goyal 	union {
1602664bd08SVivek Goyal 		struct ovl_dir_cache *cache;	/* directory */
1612b21da92SAmir Goldstein 		const char *lowerdata_redirect;	/* regular file */
1622664bd08SVivek Goyal 	};
163cf31c463SMiklos Szeredi 	const char *redirect;
16404a01ac7SMiklos Szeredi 	u64 version;
16513c72075SMiklos Szeredi 	unsigned long flags;
16613cf199dSAmir Goldstein 	struct inode vfs_inode;
16709d8b586SMiklos Szeredi 	struct dentry *__upperdentry;
1680af950f5SAmir Goldstein 	struct ovl_entry *oe;
169a015dafcSAmir Goldstein 
170a015dafcSAmir Goldstein 	/* synchronize copy up and more */
171a015dafcSAmir Goldstein 	struct mutex lock;
17213cf199dSAmir Goldstein };
17313cf199dSAmir Goldstein 
17413cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode)
17513cf199dSAmir Goldstein {
17613cf199dSAmir Goldstein 	return container_of(inode, struct ovl_inode, vfs_inode);
17713cf199dSAmir Goldstein }
17809d8b586SMiklos Szeredi 
1790af950f5SAmir Goldstein static inline struct ovl_entry *OVL_I_E(struct inode *inode)
1800af950f5SAmir Goldstein {
1810af950f5SAmir Goldstein 	return inode ? OVL_I(inode)->oe : NULL;
1820af950f5SAmir Goldstein }
1830af950f5SAmir Goldstein 
1840af950f5SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry)
1850af950f5SAmir Goldstein {
1860af950f5SAmir Goldstein 	return OVL_I_E(d_inode(dentry));
1870af950f5SAmir Goldstein }
1880af950f5SAmir Goldstein 
18909d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
19009d8b586SMiklos Szeredi {
191506458efSWill Deacon 	return READ_ONCE(oi->__upperdentry);
19209d8b586SMiklos Szeredi }
193