xref: /openbmc/linux/fs/overlayfs/ovl_entry.h (revision c86243b0)
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;
17f168f109SAmir Goldstein 	bool nfs_export;
18795939a9SAmir Goldstein 	int xino;
19d5791044SVivek Goyal 	bool metacopy;
20c86243b0SVivek Goyal 	bool ovl_volatile;
21bbb1e54dSMiklos Szeredi };
22bbb1e54dSMiklos Szeredi 
235148626bSAmir Goldstein struct ovl_sb {
245148626bSAmir Goldstein 	struct super_block *sb;
255148626bSAmir Goldstein 	dev_t pseudo_dev;
267e63c87fSAmir Goldstein 	/* Unusable (conflicting) uuid */
277e63c87fSAmir Goldstein 	bool bad_uuid;
281b81ddddSAmir Goldstein 	/* Used as a lower layer (but maybe also as upper) */
291b81ddddSAmir Goldstein 	bool is_lower;
305148626bSAmir Goldstein };
315148626bSAmir Goldstein 
32b9343632SChandan Rajendra struct ovl_layer {
33b9343632SChandan Rajendra 	struct vfsmount *mnt;
34146d62e5SAmir Goldstein 	/* Trap in ovl inode cache */
35146d62e5SAmir Goldstein 	struct inode *trap;
365148626bSAmir Goldstein 	struct ovl_sb *fs;
375148626bSAmir Goldstein 	/* Index of this layer in fs root (upper idx == 0) */
38d583ed7dSAmir Goldstein 	int idx;
395148626bSAmir Goldstein 	/* One fsid per unique underlying sb (upper fsid == 0) */
405148626bSAmir Goldstein 	int fsid;
41b9343632SChandan Rajendra };
42b9343632SChandan Rajendra 
43b9343632SChandan Rajendra struct ovl_path {
4413464165SMiklos Szeredi 	const struct ovl_layer *layer;
45b9343632SChandan Rajendra 	struct dentry *dentry;
46b9343632SChandan Rajendra };
47b9343632SChandan Rajendra 
48bbb1e54dSMiklos Szeredi /* private information held for overlayfs's superblock */
49bbb1e54dSMiklos Szeredi struct ovl_fs {
5094375f9dSAmir Goldstein 	unsigned int numlayer;
5107f1e596SAmir Goldstein 	/* Number of unique fs among layers including upper fs */
5207f1e596SAmir Goldstein 	unsigned int numfs;
5313464165SMiklos Szeredi 	const struct ovl_layer *layers;
5407f1e596SAmir Goldstein 	struct ovl_sb *fs;
552cac0c00SAmir Goldstein 	/* workbasedir is the path at workdir= mount option */
562cac0c00SAmir Goldstein 	struct dentry *workbasedir;
572cac0c00SAmir Goldstein 	/* workdir is the 'work' directory under workbasedir */
58bbb1e54dSMiklos Szeredi 	struct dentry *workdir;
5902bcd157SAmir Goldstein 	/* index directory listing overlay inodes by origin file handle */
6002bcd157SAmir Goldstein 	struct dentry *indexdir;
616b2d5fe4SMiklos Szeredi 	long namelen;
62bbb1e54dSMiklos Szeredi 	/* pathnames of lower and upper dirs, for show_options */
63bbb1e54dSMiklos Szeredi 	struct ovl_config config;
64bbb1e54dSMiklos Szeredi 	/* creds of process who forced instantiation of super block */
65bbb1e54dSMiklos Szeredi 	const struct cred *creator_cred;
66e7f52429SAmir Goldstein 	bool tmpfile;
6782b749b2SAmir Goldstein 	bool noxattr;
6885fdee1eSAmir Goldstein 	/* Did we take the inuse lock? */
6985fdee1eSAmir Goldstein 	bool upperdir_locked;
7085fdee1eSAmir Goldstein 	bool workdir_locked;
71c21c839bSChengguang Xu 	bool share_whiteout;
72146d62e5SAmir Goldstein 	/* Traps in ovl inode cache */
730be0bfd2SAmir Goldstein 	struct inode *workbasedir_trap;
74146d62e5SAmir Goldstein 	struct inode *workdir_trap;
75146d62e5SAmir Goldstein 	struct inode *indexdir_trap;
760f831ec8SAmir Goldstein 	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
770f831ec8SAmir Goldstein 	int xino_mode;
784d314f78SAmir Goldstein 	/* For allocation of non-persistent inode numbers */
794d314f78SAmir Goldstein 	atomic_long_t last_ino;
80c21c839bSChengguang Xu 	/* Whiteout dentry cache */
81c21c839bSChengguang Xu 	struct dentry *whiteout;
82bbb1e54dSMiklos Szeredi };
83bbb1e54dSMiklos Szeredi 
8408f4c7c8SMiklos Szeredi static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
8508f4c7c8SMiklos Szeredi {
86b8e42a65SMiklos Szeredi 	return ofs->layers[0].mnt;
8708f4c7c8SMiklos Szeredi }
8808f4c7c8SMiklos Szeredi 
890f831ec8SAmir Goldstein static inline struct ovl_fs *OVL_FS(struct super_block *sb)
900f831ec8SAmir Goldstein {
910f831ec8SAmir Goldstein 	return (struct ovl_fs *)sb->s_fs_info;
920f831ec8SAmir Goldstein }
930f831ec8SAmir Goldstein 
94c86243b0SVivek Goyal static inline bool ovl_should_sync(struct ovl_fs *ofs)
95c86243b0SVivek Goyal {
96c86243b0SVivek Goyal 	return !ofs->config.ovl_volatile;
97c86243b0SVivek Goyal }
98c86243b0SVivek Goyal 
99bbb1e54dSMiklos Szeredi /* private information held for every overlayfs dentry */
100bbb1e54dSMiklos Szeredi struct ovl_entry {
101bbb1e54dSMiklos Szeredi 	union {
10255acc661SMiklos Szeredi 		struct {
103c62520a8SAmir Goldstein 			unsigned long flags;
10455acc661SMiklos Szeredi 		};
105bbb1e54dSMiklos Szeredi 		struct rcu_head rcu;
106bbb1e54dSMiklos Szeredi 	};
107bbb1e54dSMiklos Szeredi 	unsigned numlower;
108b9343632SChandan Rajendra 	struct ovl_path lowerstack[];
109bbb1e54dSMiklos Szeredi };
110bbb1e54dSMiklos Szeredi 
111bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
112bbb1e54dSMiklos Szeredi 
113c62520a8SAmir Goldstein static inline struct ovl_entry *OVL_E(struct dentry *dentry)
114c62520a8SAmir Goldstein {
115c62520a8SAmir Goldstein 	return (struct ovl_entry *) dentry->d_fsdata;
116c62520a8SAmir Goldstein }
117c62520a8SAmir Goldstein 
11813cf199dSAmir Goldstein struct ovl_inode {
1192664bd08SVivek Goyal 	union {
1202664bd08SVivek Goyal 		struct ovl_dir_cache *cache;	/* directory */
1212664bd08SVivek Goyal 		struct inode *lowerdata;	/* regular file */
1222664bd08SVivek Goyal 	};
123cf31c463SMiklos Szeredi 	const char *redirect;
12404a01ac7SMiklos Szeredi 	u64 version;
12513c72075SMiklos Szeredi 	unsigned long flags;
12613cf199dSAmir Goldstein 	struct inode vfs_inode;
12709d8b586SMiklos Szeredi 	struct dentry *__upperdentry;
12825b7713aSMiklos Szeredi 	struct inode *lower;
129a015dafcSAmir Goldstein 
130a015dafcSAmir Goldstein 	/* synchronize copy up and more */
131a015dafcSAmir Goldstein 	struct mutex lock;
13213cf199dSAmir Goldstein };
13313cf199dSAmir Goldstein 
13413cf199dSAmir Goldstein static inline struct ovl_inode *OVL_I(struct inode *inode)
13513cf199dSAmir Goldstein {
13613cf199dSAmir Goldstein 	return container_of(inode, struct ovl_inode, vfs_inode);
13713cf199dSAmir Goldstein }
13809d8b586SMiklos Szeredi 
13909d8b586SMiklos Szeredi static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
14009d8b586SMiklos Szeredi {
141506458efSWill Deacon 	return READ_ONCE(oi->__upperdentry);
14209d8b586SMiklos Szeredi }
143