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