1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
5 * Copyright (C) 2016 Red Hat, Inc.
6 */
7
8struct ovl_config {
9	char *upperdir;
10	char *workdir;
11	char **lowerdirs;
12	bool default_permissions;
13	int redirect_mode;
14	int verity_mode;
15	bool index;
16	int uuid;
17	bool nfs_export;
18	int xino;
19	bool metacopy;
20	bool userxattr;
21	bool ovl_volatile;
22};
23
24struct ovl_sb {
25	struct super_block *sb;
26	dev_t pseudo_dev;
27	/* Unusable (conflicting) uuid */
28	bool bad_uuid;
29	/* Used as a lower layer (but maybe also as upper) */
30	bool is_lower;
31};
32
33struct ovl_layer {
34	/* ovl_free_fs() relies on @mnt being the first member! */
35	struct vfsmount *mnt;
36	/* Trap in ovl inode cache */
37	struct inode *trap;
38	struct ovl_sb *fs;
39	/* Index of this layer in fs root (upper idx == 0) */
40	int idx;
41	/* One fsid per unique underlying sb (upper fsid == 0) */
42	int fsid;
43};
44
45struct ovl_path {
46	const struct ovl_layer *layer;
47	struct dentry *dentry;
48};
49
50struct ovl_entry {
51	unsigned int __numlower;
52	struct ovl_path __lowerstack[];
53};
54
55/* private information held for overlayfs's superblock */
56struct ovl_fs {
57	unsigned int numlayer;
58	/* Number of unique fs among layers including upper fs */
59	unsigned int numfs;
60	/* Number of data-only lower layers */
61	unsigned int numdatalayer;
62	const struct ovl_layer *layers;
63	struct ovl_sb *fs;
64	/* workbasedir is the path at workdir= mount option */
65	struct dentry *workbasedir;
66	/* workdir is the 'work' directory under workbasedir */
67	struct dentry *workdir;
68	/* index directory listing overlay inodes by origin file handle */
69	struct dentry *indexdir;
70	long namelen;
71	/* pathnames of lower and upper dirs, for show_options */
72	struct ovl_config config;
73	/* creds of process who forced instantiation of super block */
74	const struct cred *creator_cred;
75	bool tmpfile;
76	bool noxattr;
77	bool nofh;
78	/* Did we take the inuse lock? */
79	bool upperdir_locked;
80	bool workdir_locked;
81	/* Traps in ovl inode cache */
82	struct inode *workbasedir_trap;
83	struct inode *workdir_trap;
84	struct inode *indexdir_trap;
85	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
86	int xino_mode;
87	/* For allocation of non-persistent inode numbers */
88	atomic_long_t last_ino;
89	/* Shared whiteout cache */
90	struct dentry *whiteout;
91	bool no_shared_whiteout;
92	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
93	errseq_t errseq;
94};
95
96/* Number of lower layers, not including data-only layers */
97static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs)
98{
99	return ofs->numlayer - ofs->numdatalayer - 1;
100}
101
102static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
103{
104	return ofs->layers[0].mnt;
105}
106
107static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs)
108{
109	return mnt_idmap(ovl_upper_mnt(ofs));
110}
111
112extern struct file_system_type ovl_fs_type;
113
114static inline struct ovl_fs *OVL_FS(struct super_block *sb)
115{
116	if (IS_ENABLED(CONFIG_OVERLAY_FS_DEBUG))
117		WARN_ON_ONCE(sb->s_type != &ovl_fs_type);
118
119	return (struct ovl_fs *)sb->s_fs_info;
120}
121
122static inline bool ovl_should_sync(struct ovl_fs *ofs)
123{
124	return !ofs->config.ovl_volatile;
125}
126
127static inline unsigned int ovl_numlower(struct ovl_entry *oe)
128{
129	return oe ? oe->__numlower : 0;
130}
131
132static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe)
133{
134	return ovl_numlower(oe) ? oe->__lowerstack : NULL;
135}
136
137static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe)
138{
139	return ovl_lowerstack(oe);
140}
141
142static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe)
143{
144	struct ovl_path *lowerstack = ovl_lowerstack(oe);
145
146	return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL;
147}
148
149/* May return NULL if lazy lookup of lowerdata is needed */
150static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe)
151{
152	struct ovl_path *lowerdata = ovl_lowerdata(oe);
153
154	return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL;
155}
156
157/* private information held for every overlayfs dentry */
158static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry)
159{
160	return (unsigned long *) &dentry->d_fsdata;
161}
162
163struct ovl_inode {
164	union {
165		struct ovl_dir_cache *cache;	/* directory */
166		const char *lowerdata_redirect;	/* regular file */
167	};
168	const char *redirect;
169	u64 version;
170	unsigned long flags;
171	struct inode vfs_inode;
172	struct dentry *__upperdentry;
173	struct ovl_entry *oe;
174
175	/* synchronize copy up and more */
176	struct mutex lock;
177};
178
179static inline struct ovl_inode *OVL_I(struct inode *inode)
180{
181	return container_of(inode, struct ovl_inode, vfs_inode);
182}
183
184static inline struct ovl_entry *OVL_I_E(struct inode *inode)
185{
186	return inode ? OVL_I(inode)->oe : NULL;
187}
188
189static inline struct ovl_entry *OVL_E(struct dentry *dentry)
190{
191	return OVL_I_E(d_inode(dentry));
192}
193
194static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
195{
196	return READ_ONCE(oi->__upperdentry);
197}
198