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