xref: /linux/fs/overlayfs/ovl_entry.h (revision c4101e55974cc7d835fbd2d8e01553a3f61e9e75)
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 
8 struct 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 
24 struct 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 
33 struct 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 
45 struct ovl_path {
46 	const struct ovl_layer *layer;
47 	struct dentry *dentry;
48 };
49 
50 struct ovl_entry {
51 	unsigned int __numlower;
52 	struct ovl_path __lowerstack[];
53 };
54 
55 /* private information held for overlayfs's superblock */
56 struct 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' or 'index' directory under workbasedir */
67 	struct dentry *workdir;
68 	long namelen;
69 	/* pathnames of lower and upper dirs, for show_options */
70 	struct ovl_config config;
71 	/* creds of process who forced instantiation of super block */
72 	const struct cred *creator_cred;
73 	bool tmpfile;
74 	bool noxattr;
75 	bool nofh;
76 	/* Did we take the inuse lock? */
77 	bool upperdir_locked;
78 	bool workdir_locked;
79 	/* Traps in ovl inode cache */
80 	struct inode *workbasedir_trap;
81 	struct inode *workdir_trap;
82 	/* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
83 	int xino_mode;
84 	/* For allocation of non-persistent inode numbers */
85 	atomic_long_t last_ino;
86 	/* Shared whiteout cache */
87 	struct dentry *whiteout;
88 	bool no_shared_whiteout;
89 	/* r/o snapshot of upperdir sb's only taken on volatile mounts */
90 	errseq_t errseq;
91 };
92 
93 /* Number of lower layers, not including data-only layers */
94 static inline unsigned int ovl_numlowerlayer(struct ovl_fs *ofs)
95 {
96 	return ofs->numlayer - ofs->numdatalayer - 1;
97 }
98 
99 static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
100 {
101 	return ofs->layers[0].mnt;
102 }
103 
104 static inline struct mnt_idmap *ovl_upper_mnt_idmap(struct ovl_fs *ofs)
105 {
106 	return mnt_idmap(ovl_upper_mnt(ofs));
107 }
108 
109 extern struct file_system_type ovl_fs_type;
110 
111 static inline struct ovl_fs *OVL_FS(struct super_block *sb)
112 {
113 	if (IS_ENABLED(CONFIG_OVERLAY_FS_DEBUG))
114 		WARN_ON_ONCE(sb->s_type != &ovl_fs_type);
115 
116 	return (struct ovl_fs *)sb->s_fs_info;
117 }
118 
119 static inline bool ovl_should_sync(struct ovl_fs *ofs)
120 {
121 	return !ofs->config.ovl_volatile;
122 }
123 
124 static inline unsigned int ovl_numlower(struct ovl_entry *oe)
125 {
126 	return oe ? oe->__numlower : 0;
127 }
128 
129 static inline struct ovl_path *ovl_lowerstack(struct ovl_entry *oe)
130 {
131 	return ovl_numlower(oe) ? oe->__lowerstack : NULL;
132 }
133 
134 static inline struct ovl_path *ovl_lowerpath(struct ovl_entry *oe)
135 {
136 	return ovl_lowerstack(oe);
137 }
138 
139 static inline struct ovl_path *ovl_lowerdata(struct ovl_entry *oe)
140 {
141 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
142 
143 	return lowerstack ? &lowerstack[oe->__numlower - 1] : NULL;
144 }
145 
146 /* May return NULL if lazy lookup of lowerdata is needed */
147 static inline struct dentry *ovl_lowerdata_dentry(struct ovl_entry *oe)
148 {
149 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
150 
151 	return lowerdata ? READ_ONCE(lowerdata->dentry) : NULL;
152 }
153 
154 /* private information held for every overlayfs dentry */
155 static inline unsigned long *OVL_E_FLAGS(struct dentry *dentry)
156 {
157 	return (unsigned long *) &dentry->d_fsdata;
158 }
159 
160 struct ovl_inode {
161 	union {
162 		struct ovl_dir_cache *cache;	/* directory */
163 		const char *lowerdata_redirect;	/* regular file */
164 	};
165 	const char *redirect;
166 	u64 version;
167 	unsigned long flags;
168 	struct inode vfs_inode;
169 	struct dentry *__upperdentry;
170 	struct ovl_entry *oe;
171 
172 	/* synchronize copy up and more */
173 	struct mutex lock;
174 };
175 
176 static inline struct ovl_inode *OVL_I(struct inode *inode)
177 {
178 	return container_of(inode, struct ovl_inode, vfs_inode);
179 }
180 
181 static inline struct ovl_entry *OVL_I_E(struct inode *inode)
182 {
183 	return inode ? OVL_I(inode)->oe : NULL;
184 }
185 
186 static inline struct ovl_entry *OVL_E(struct dentry *dentry)
187 {
188 	return OVL_I_E(d_inode(dentry));
189 }
190 
191 static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
192 {
193 	return READ_ONCE(oi->__upperdentry);
194 }
195