xref: /openbmc/linux/fs/overlayfs/ovl_entry.h (revision ac900ed4)
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;
6013464165SMiklos Szeredi 	const struct ovl_layer *layers;
6107f1e596SAmir Goldstein 	struct ovl_sb *fs;
622cac0c00SAmir Goldstein 	/* workbasedir is the path at workdir= mount option */
632cac0c00SAmir Goldstein 	struct dentry *workbasedir;
642cac0c00SAmir Goldstein 	/* workdir is the 'work' directory under workbasedir */
65bbb1e54dSMiklos Szeredi 	struct dentry *workdir;
6602bcd157SAmir Goldstein 	/* index directory listing overlay inodes by origin file handle */
6702bcd157SAmir Goldstein 	struct dentry *indexdir;
686b2d5fe4SMiklos Szeredi 	long namelen;
69bbb1e54dSMiklos Szeredi 	/* pathnames of lower and upper dirs, for show_options */
70bbb1e54dSMiklos Szeredi 	struct ovl_config config;
71bbb1e54dSMiklos Szeredi 	/* creds of process who forced instantiation of super block */
72bbb1e54dSMiklos Szeredi 	const struct cred *creator_cred;
73e7f52429SAmir Goldstein 	bool tmpfile;
7482b749b2SAmir Goldstein 	bool noxattr;
7585fdee1eSAmir Goldstein 	/* Did we take the inuse lock? */
7685fdee1eSAmir Goldstein 	bool upperdir_locked;
7785fdee1eSAmir Goldstein 	bool workdir_locked;
78c21c839bSChengguang Xu 	bool share_whiteout;
79146d62e5SAmir Goldstein 	/* Traps in ovl inode cache */
800be0bfd2SAmir Goldstein 	struct inode *workbasedir_trap;
81146d62e5SAmir Goldstein 	struct inode *workdir_trap;
82146d62e5SAmir Goldstein 	struct inode *indexdir_trap;
830f831ec8SAmir Goldstein 	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
840f831ec8SAmir Goldstein 	int xino_mode;
854d314f78SAmir Goldstein 	/* For allocation of non-persistent inode numbers */
864d314f78SAmir Goldstein 	atomic_long_t last_ino;
87c21c839bSChengguang Xu 	/* Whiteout dentry cache */
88c21c839bSChengguang Xu 	struct dentry *whiteout;
89335d3fc5SSargun Dhillon 	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
90335d3fc5SSargun Dhillon 	errseq_t errseq;
91bbb1e54dSMiklos Szeredi };
92bbb1e54dSMiklos Szeredi 
9308f4c7c8SMiklos Szeredi static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
9408f4c7c8SMiklos Szeredi {
95b8e42a65SMiklos Szeredi 	return ofs->layers[0].mnt;
9608f4c7c8SMiklos Szeredi }
9708f4c7c8SMiklos Szeredi 
98abf08576SChristian Brauner static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs)
99abf08576SChristian Brauner {
100abf08576SChristian Brauner 	return mnt_idmap(ovl_upper_mnt(ofs));
101abf08576SChristian Brauner }
102abf08576SChristian Brauner 
1030f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb)
1040f831ec8SAmir Goldstein {
1050f831ec8SAmir Goldstein 	return (struct ovl_fs *)sb->s_fs_info;
1060f831ec8SAmir Goldstein }
1070f831ec8SAmir Goldstein 
108c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs)
109c86243b0SVivek Goyal {
110c86243b0SVivek Goyal 	return !ofs->config.ovl_volatile;
111c86243b0SVivek Goyal }
112c86243b0SVivek Goyal 
1135522c9c7SAmir Goldstein static inline unsigned int ovl_numlower(struct ovl_entry *oe)
1145522c9c7SAmir Goldstein {
1155522c9c7SAmir Goldstein 	return oe ? oe->__numlower : 0;
1165522c9c7SAmir Goldstein }
1175522c9c7SAmir Goldstein 
1185522c9c7SAmir Goldstein static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe)
1195522c9c7SAmir Goldstein {
1205522c9c7SAmir Goldstein 	return ovl_numlower(oe) ? oe->__lowerstack : NULL;
1215522c9c7SAmir Goldstein }
1225522c9c7SAmir Goldstein 
123*ac900ed4SAmir Goldstein static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe)
124*ac900ed4SAmir Goldstein {
125*ac900ed4SAmir Goldstein 	return ovl_lowerstack(oe);
126*ac900ed4SAmir Goldstein }
127*ac900ed4SAmir Goldstein 
1280af950f5SAmir Goldstein /* private information held for every overlayfs dentry */
129a6ff2bc0SAmir Goldstein static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry)
130a6ff2bc0SAmir Goldstein {
1310af950f5SAmir Goldstein 	return (unsigned long *) &dentry->d_fsdata;
132a6ff2bc0SAmir Goldstein }
133a6ff2bc0SAmir Goldstein 
13413cf199dSAmir Goldstein struct ovl_inode {
1352664bd08SVivek Goyal 	union {
1362664bd08SVivek Goyal 		struct ovl_dir_cache *cache;	/* directory */
1372664bd08SVivek Goyal 		struct inode *lowerdata;	/* regular file */
1382664bd08SVivek Goyal 	};
139cf31c463SMiklos Szeredi 	const char *redirect;
14004a01ac7SMiklos Szeredi 	u64 version;
14113c72075SMiklos Szeredi 	unsigned long flags;
14213cf199dSAmir Goldstein 	struct inode vfs_inode;
14309d8b586SMiklos Szeredi 	struct dentry *__upperdentry;
1440af950f5SAmir Goldstein 	struct ovl_entry *oe;
145a015dafcSAmir Goldstein 
146a015dafcSAmir Goldstein 	/* synchronize copy up and more */
147a015dafcSAmir Goldstein 	struct mutex lock;
14813cf199dSAmir Goldstein };
14913cf199dSAmir Goldstein 
15013cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode)
15113cf199dSAmir Goldstein {
15213cf199dSAmir Goldstein 	return container_of(inode, struct ovl_inode, vfs_inode);
15313cf199dSAmir Goldstein }
15409d8b586SMiklos Szeredi 
1550af950f5SAmir Goldstein static inline struct ovl_entry *OVL_I_E(struct inode *inode)
1560af950f5SAmir Goldstein {
1570af950f5SAmir Goldstein 	return inode ? OVL_I(inode)->oe : NULL;
1580af950f5SAmir Goldstein }
1590af950f5SAmir Goldstein 
1600af950f5SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry)
1610af950f5SAmir Goldstein {
1620af950f5SAmir Goldstein 	return OVL_I_E(d_inode(dentry));
1630af950f5SAmir Goldstein }
1640af950f5SAmir Goldstein 
16509d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
16609d8b586SMiklos Szeredi {
167506458efSWill Deacon 	return READ_ONCE(oi->__upperdentry);
16809d8b586SMiklos Szeredi }
169