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 *lowerdir;
10	char *upperdir;
11	char *workdir;
12	bool default_permissions;
13	bool redirect_dir;
14	bool redirect_follow;
15	const char *redirect_mode;
16	bool index;
17	bool nfs_export;
18	int xino;
19	bool metacopy;
20	bool ovl_volatile;
21};
22
23struct ovl_sb {
24	struct super_block *sb;
25	dev_t pseudo_dev;
26	/* Unusable (conflicting) uuid */
27	bool bad_uuid;
28	/* Used as a lower layer (but maybe also as upper) */
29	bool is_lower;
30};
31
32struct ovl_layer {
33	/* ovl_free_fs() relies on @mnt being the first member! */
34	struct vfsmount *mnt;
35	/* Trap in ovl inode cache */
36	struct inode *trap;
37	struct ovl_sb *fs;
38	/* Index of this layer in fs root (upper idx == 0) */
39	int idx;
40	/* One fsid per unique underlying sb (upper fsid == 0) */
41	int fsid;
42};
43
44/*
45 * ovl_free_fs() relies on @mnt being the first member when unmounting
46 * the private mounts created for each layer. Let's check both the
47 * offset and type.
48 */
49static_assert(offsetof(struct ovl_layer, mnt) == 0);
50static_assert(__same_type(typeof_member(struct ovl_layer, mnt), struct vfsmount *));
51
52struct ovl_path {
53	const struct ovl_layer *layer;
54	struct dentry *dentry;
55};
56
57/* private information held for overlayfs's superblock */
58struct ovl_fs {
59	unsigned int numlayer;
60	/* Number of unique fs among layers including upper fs */
61	unsigned int numfs;
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	/* Did we take the inuse lock? */
78	bool upperdir_locked;
79	bool workdir_locked;
80	bool share_whiteout;
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	/* Whiteout dentry cache */
90	struct dentry *whiteout;
91	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
92	errseq_t errseq;
93};
94
95static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
96{
97	return ofs->layers[0].mnt;
98}
99
100static inline struct ovl_fs *OVL_FS(struct super_block *sb)
101{
102	return (struct ovl_fs *)sb->s_fs_info;
103}
104
105static inline bool ovl_should_sync(struct ovl_fs *ofs)
106{
107	return !ofs->config.ovl_volatile;
108}
109
110/* private information held for every overlayfs dentry */
111struct ovl_entry {
112	union {
113		struct {
114			unsigned long flags;
115		};
116		struct rcu_head rcu;
117	};
118	unsigned numlower;
119	struct ovl_path lowerstack[];
120};
121
122struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
123
124static inline struct ovl_entry *OVL_E(struct dentry *dentry)
125{
126	return (struct ovl_entry *) dentry->d_fsdata;
127}
128
129struct ovl_inode {
130	union {
131		struct ovl_dir_cache *cache;	/* directory */
132		struct inode *lowerdata;	/* regular file */
133	};
134	const char *redirect;
135	u64 version;
136	unsigned long flags;
137	struct inode vfs_inode;
138	struct dentry *__upperdentry;
139	struct inode *lower;
140
141	/* synchronize copy up and more */
142	struct mutex lock;
143};
144
145static inline struct ovl_inode *OVL_I(struct inode *inode)
146{
147	return container_of(inode, struct ovl_inode, vfs_inode);
148}
149
150static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
151{
152	return READ_ONCE(oi->__upperdentry);
153}
154