xref: /linux/fs/overlayfs/util.c (revision a3d97d1d3fa68253927a6e6c2fdfe7c039073af7)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2011 Novell Inc.
4  * Copyright (C) 2016 Red Hat, Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/mount.h>
9 #include <linux/slab.h>
10 #include <linux/cred.h>
11 #include <linux/xattr.h>
12 #include <linux/exportfs.h>
13 #include <linux/file.h>
14 #include <linux/fileattr.h>
15 #include <linux/uuid.h>
16 #include <linux/namei.h>
17 #include <linux/ratelimit.h>
18 #include <linux/overflow.h>
19 #include "overlayfs.h"
20 
21 /* Get write access to upper mnt - may fail if upper sb was remounted ro */
ovl_get_write_access(struct dentry * dentry)22 int ovl_get_write_access(struct dentry *dentry)
23 {
24 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
25 	return mnt_get_write_access(ovl_upper_mnt(ofs));
26 }
27 
28 /* Get write access to upper sb - may block if upper sb is frozen */
ovl_start_write(struct dentry * dentry)29 void ovl_start_write(struct dentry *dentry)
30 {
31 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
32 	sb_start_write(ovl_upper_mnt(ofs)->mnt_sb);
33 }
34 
ovl_want_write(struct dentry * dentry)35 int ovl_want_write(struct dentry *dentry)
36 {
37 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
38 	return mnt_want_write(ovl_upper_mnt(ofs));
39 }
40 
ovl_put_write_access(struct dentry * dentry)41 void ovl_put_write_access(struct dentry *dentry)
42 {
43 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
44 	mnt_put_write_access(ovl_upper_mnt(ofs));
45 }
46 
ovl_end_write(struct dentry * dentry)47 void ovl_end_write(struct dentry *dentry)
48 {
49 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
50 	sb_end_write(ovl_upper_mnt(ofs)->mnt_sb);
51 }
52 
ovl_drop_write(struct dentry * dentry)53 void ovl_drop_write(struct dentry *dentry)
54 {
55 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
56 	mnt_drop_write(ovl_upper_mnt(ofs));
57 }
58 
ovl_workdir(struct dentry * dentry)59 struct dentry *ovl_workdir(struct dentry *dentry)
60 {
61 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
62 	return ofs->workdir;
63 }
64 
ovl_override_creds(struct super_block * sb)65 const struct cred *ovl_override_creds(struct super_block *sb)
66 {
67 	struct ovl_fs *ofs = OVL_FS(sb);
68 
69 	return override_creds(ofs->creator_cred);
70 }
71 
72 /*
73  * Check if underlying fs supports file handles and try to determine encoding
74  * type, in order to deduce maximum inode number used by fs.
75  *
76  * Return 0 if file handles are not supported.
77  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
78  * Return -1 if fs uses a non default encoding with unknown inode size.
79  */
ovl_can_decode_fh(struct super_block * sb)80 int ovl_can_decode_fh(struct super_block *sb)
81 {
82 	if (!capable(CAP_DAC_READ_SEARCH))
83 		return 0;
84 
85 	if (!exportfs_can_decode_fh(sb->s_export_op))
86 		return 0;
87 
88 	if (sb->s_export_op->encode_fh == generic_encode_ino32_fh)
89 		return FILEID_INO32_GEN;
90 
91 	return -1;
92 }
93 
ovl_indexdir(struct super_block * sb)94 struct dentry *ovl_indexdir(struct super_block *sb)
95 {
96 	struct ovl_fs *ofs = OVL_FS(sb);
97 
98 	return ofs->config.index ? ofs->workdir : NULL;
99 }
100 
101 /* Index all files on copy up. For now only enabled for NFS export */
ovl_index_all(struct super_block * sb)102 bool ovl_index_all(struct super_block *sb)
103 {
104 	struct ovl_fs *ofs = OVL_FS(sb);
105 
106 	return ofs->config.nfs_export && ofs->config.index;
107 }
108 
109 /* Verify lower origin on lookup. For now only enabled for NFS export */
ovl_verify_lower(struct super_block * sb)110 bool ovl_verify_lower(struct super_block *sb)
111 {
112 	struct ovl_fs *ofs = OVL_FS(sb);
113 
114 	return ofs->config.nfs_export && ofs->config.index;
115 }
116 
ovl_stack_alloc(unsigned int n)117 struct ovl_path *ovl_stack_alloc(unsigned int n)
118 {
119 	return kzalloc_objs(struct ovl_path, n);
120 }
121 
ovl_stack_cpy(struct ovl_path * dst,struct ovl_path * src,unsigned int n)122 void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n)
123 {
124 	unsigned int i;
125 
126 	memcpy(dst, src, sizeof(struct ovl_path) * n);
127 	for (i = 0; i < n; i++)
128 		dget(src[i].dentry);
129 }
130 
ovl_stack_put(struct ovl_path * stack,unsigned int n)131 void ovl_stack_put(struct ovl_path *stack, unsigned int n)
132 {
133 	unsigned int i;
134 
135 	for (i = 0; stack && i < n; i++)
136 		dput(stack[i].dentry);
137 }
138 
ovl_stack_free(struct ovl_path * stack,unsigned int n)139 void ovl_stack_free(struct ovl_path *stack, unsigned int n)
140 {
141 	ovl_stack_put(stack, n);
142 	kfree(stack);
143 }
144 
ovl_alloc_entry(unsigned int numlower)145 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
146 {
147 	struct ovl_entry *oe;
148 
149 	oe = kzalloc_flex(*oe, __lowerstack, numlower);
150 	if (oe)
151 		oe->__numlower = numlower;
152 
153 	return oe;
154 }
155 
ovl_free_entry(struct ovl_entry * oe)156 void ovl_free_entry(struct ovl_entry *oe)
157 {
158 	ovl_stack_put(ovl_lowerstack(oe), ovl_numlower(oe));
159 	kfree(oe);
160 }
161 
162 #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
163 
ovl_dentry_remote(struct dentry * dentry)164 bool ovl_dentry_remote(struct dentry *dentry)
165 {
166 	return dentry->d_flags & OVL_D_REVALIDATE;
167 }
168 
ovl_dentry_update_reval(struct dentry * dentry,struct dentry * realdentry)169 void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
170 {
171 	if (!ovl_dentry_remote(realdentry))
172 		return;
173 
174 	spin_lock(&dentry->d_lock);
175 	dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
176 	spin_unlock(&dentry->d_lock);
177 }
178 
ovl_dentry_init_reval(struct dentry * dentry,struct dentry * upperdentry,struct ovl_entry * oe)179 void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
180 			   struct ovl_entry *oe)
181 {
182 	return ovl_dentry_init_flags(dentry, upperdentry, oe, OVL_D_REVALIDATE);
183 }
184 
ovl_dentry_init_flags(struct dentry * dentry,struct dentry * upperdentry,struct ovl_entry * oe,unsigned int mask)185 void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
186 			   struct ovl_entry *oe, unsigned int mask)
187 {
188 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
189 	unsigned int i, flags = 0;
190 
191 	if (upperdentry)
192 		flags |= upperdentry->d_flags;
193 	for (i = 0; i < ovl_numlower(oe) && lowerstack[i].dentry; i++)
194 		flags |= lowerstack[i].dentry->d_flags;
195 
196 	spin_lock(&dentry->d_lock);
197 	dentry->d_flags &= ~mask;
198 	dentry->d_flags |= flags & mask;
199 	spin_unlock(&dentry->d_lock);
200 }
201 
ovl_dentry_weird(struct dentry * dentry)202 bool ovl_dentry_weird(struct dentry *dentry)
203 {
204 	if (!d_can_lookup(dentry) && !d_is_file(dentry) && !d_is_symlink(dentry))
205 		return true;
206 
207 	if (dentry->d_flags & (DCACHE_NEED_AUTOMOUNT | DCACHE_MANAGE_TRANSIT))
208 		return true;
209 
210 	/*
211 	 * Exceptionally for layers with casefold, we accept that they have
212 	 * their own hash and compare operations
213 	 */
214 	if (sb_has_encoding(dentry->d_sb))
215 		return false;
216 
217 	return dentry->d_flags & (DCACHE_OP_HASH | DCACHE_OP_COMPARE);
218 }
219 
ovl_path_type(struct dentry * dentry)220 enum ovl_path_type ovl_path_type(struct dentry *dentry)
221 {
222 	struct ovl_entry *oe = OVL_E(dentry);
223 	enum ovl_path_type type = 0;
224 
225 	if (ovl_dentry_upper(dentry)) {
226 		type = __OVL_PATH_UPPER;
227 
228 		/*
229 		 * Non-dir dentry can hold lower dentry of its copy up origin.
230 		 */
231 		if (ovl_numlower(oe)) {
232 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
233 				type |= __OVL_PATH_ORIGIN;
234 			if (d_is_dir(dentry) ||
235 			    !ovl_has_upperdata(d_inode(dentry)))
236 				type |= __OVL_PATH_MERGE;
237 		}
238 	} else {
239 		if (ovl_numlower(oe) > 1)
240 			type |= __OVL_PATH_MERGE;
241 	}
242 	return type;
243 }
244 
ovl_path_upper(struct dentry * dentry,struct path * path)245 void ovl_path_upper(struct dentry *dentry, struct path *path)
246 {
247 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
248 
249 	path->mnt = ovl_upper_mnt(ofs);
250 	path->dentry = ovl_dentry_upper(dentry);
251 }
252 
ovl_path_lower(struct dentry * dentry,struct path * path)253 void ovl_path_lower(struct dentry *dentry, struct path *path)
254 {
255 	struct ovl_entry *oe = OVL_E(dentry);
256 	struct ovl_path *lowerpath = ovl_lowerstack(oe);
257 
258 	if (ovl_numlower(oe)) {
259 		path->mnt = lowerpath->layer->mnt;
260 		path->dentry = lowerpath->dentry;
261 	} else {
262 		*path = (struct path) { };
263 	}
264 }
265 
ovl_path_lowerdata(struct dentry * dentry,struct path * path)266 void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
267 {
268 	struct ovl_entry *oe = OVL_E(dentry);
269 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
270 	struct dentry *lowerdata_dentry = ovl_lowerdata_dentry(oe);
271 
272 	if (lowerdata_dentry) {
273 		path->dentry = lowerdata_dentry;
274 		/*
275 		 * Pairs with smp_wmb() in ovl_dentry_set_lowerdata().
276 		 * Make sure that if lowerdata->dentry is visible, then
277 		 * datapath->layer is visible as well.
278 		 */
279 		smp_rmb();
280 		path->mnt = READ_ONCE(lowerdata->layer)->mnt;
281 	} else {
282 		*path = (struct path) { };
283 	}
284 }
285 
ovl_path_real(struct dentry * dentry,struct path * path)286 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
287 {
288 	enum ovl_path_type type = ovl_path_type(dentry);
289 
290 	if (!OVL_TYPE_UPPER(type))
291 		ovl_path_lower(dentry, path);
292 	else
293 		ovl_path_upper(dentry, path);
294 
295 	return type;
296 }
297 
ovl_path_realdata(struct dentry * dentry,struct path * path)298 enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
299 {
300 	enum ovl_path_type type = ovl_path_type(dentry);
301 
302 	WARN_ON_ONCE(d_is_dir(dentry));
303 
304 	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
305 		ovl_path_lowerdata(dentry, path);
306 	else
307 		ovl_path_upper(dentry, path);
308 
309 	return type;
310 }
311 
ovl_dentry_upper(struct dentry * dentry)312 struct dentry *ovl_dentry_upper(struct dentry *dentry)
313 {
314 	struct inode *inode = d_inode(dentry);
315 
316 	return inode ? ovl_upperdentry_dereference(OVL_I(inode)) : NULL;
317 }
318 
ovl_dentry_lower(struct dentry * dentry)319 struct dentry *ovl_dentry_lower(struct dentry *dentry)
320 {
321 	struct ovl_entry *oe = OVL_E(dentry);
322 
323 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
324 }
325 
ovl_layer_lower(struct dentry * dentry)326 const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
327 {
328 	struct ovl_entry *oe = OVL_E(dentry);
329 
330 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
331 }
332 
333 /*
334  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
335  * depending on what is stored in lowerstack[0]. At times we need to find
336  * lower dentry which has data (and not metacopy dentry). This helper
337  * returns the lower data dentry.
338  */
ovl_dentry_lowerdata(struct dentry * dentry)339 struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
340 {
341 	return ovl_lowerdata_dentry(OVL_E(dentry));
342 }
343 
ovl_dentry_set_lowerdata(struct dentry * dentry,struct ovl_path * datapath)344 int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath)
345 {
346 	struct ovl_entry *oe = OVL_E(dentry);
347 	struct ovl_path *lowerdata = ovl_lowerdata(oe);
348 	struct dentry *datadentry = datapath->dentry;
349 
350 	if (WARN_ON_ONCE(ovl_numlower(oe) <= 1))
351 		return -EIO;
352 
353 	WRITE_ONCE(lowerdata->layer, datapath->layer);
354 	/*
355 	 * Pairs with smp_rmb() in ovl_path_lowerdata().
356 	 * Make sure that if lowerdata->dentry is visible, then
357 	 * lowerdata->layer is visible as well.
358 	 */
359 	smp_wmb();
360 	WRITE_ONCE(lowerdata->dentry, dget(datadentry));
361 
362 	ovl_dentry_update_reval(dentry, datadentry);
363 
364 	return 0;
365 }
366 
ovl_dentry_real(struct dentry * dentry)367 struct dentry *ovl_dentry_real(struct dentry *dentry)
368 {
369 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
370 }
371 
ovl_i_dentry_upper(struct inode * inode)372 struct dentry *ovl_i_dentry_upper(struct inode *inode)
373 {
374 	return ovl_upperdentry_dereference(OVL_I(inode));
375 }
376 
ovl_i_path_real(struct inode * inode,struct path * path)377 struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
378 {
379 	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
380 
381 	path->dentry = ovl_i_dentry_upper(inode);
382 	if (!path->dentry) {
383 		path->dentry = lowerpath->dentry;
384 		path->mnt = lowerpath->layer->mnt;
385 	} else {
386 		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
387 	}
388 
389 	return path->dentry ? d_inode_rcu(path->dentry) : NULL;
390 }
391 
ovl_inode_upper(struct inode * inode)392 struct inode *ovl_inode_upper(struct inode *inode)
393 {
394 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
395 
396 	return upperdentry ? d_inode(upperdentry) : NULL;
397 }
398 
ovl_inode_lower(struct inode * inode)399 struct inode *ovl_inode_lower(struct inode *inode)
400 {
401 	struct ovl_path *lowerpath = ovl_lowerpath(OVL_I_E(inode));
402 
403 	return lowerpath ? d_inode(lowerpath->dentry) : NULL;
404 }
405 
ovl_inode_real(struct inode * inode)406 struct inode *ovl_inode_real(struct inode *inode)
407 {
408 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
409 }
410 
411 /* Return inode which contains lower data. Do not return metacopy */
ovl_inode_lowerdata(struct inode * inode)412 struct inode *ovl_inode_lowerdata(struct inode *inode)
413 {
414 	struct dentry *lowerdata = ovl_lowerdata_dentry(OVL_I_E(inode));
415 
416 	if (WARN_ON(!S_ISREG(inode->i_mode)))
417 		return NULL;
418 
419 	return lowerdata ? d_inode(lowerdata) : NULL;
420 }
421 
422 /* Return real inode which contains data. Does not return metacopy inode */
ovl_inode_realdata(struct inode * inode)423 struct inode *ovl_inode_realdata(struct inode *inode)
424 {
425 	struct inode *upperinode;
426 
427 	upperinode = ovl_inode_upper(inode);
428 	if (upperinode && ovl_has_upperdata(inode))
429 		return upperinode;
430 
431 	return ovl_inode_lowerdata(inode);
432 }
433 
ovl_lowerdata_redirect(struct inode * inode)434 const char *ovl_lowerdata_redirect(struct inode *inode)
435 {
436 	return inode && S_ISREG(inode->i_mode) ?
437 		OVL_I(inode)->lowerdata_redirect : NULL;
438 }
439 
ovl_dir_cache(struct inode * inode)440 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
441 {
442 	return inode && S_ISDIR(inode->i_mode) ? OVL_I(inode)->cache : NULL;
443 }
444 
ovl_set_dir_cache(struct inode * inode,struct ovl_dir_cache * cache)445 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
446 {
447 	OVL_I(inode)->cache = cache;
448 }
449 
ovl_dentry_set_flag(unsigned long flag,struct dentry * dentry)450 void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
451 {
452 	set_bit(flag, OVL_E_FLAGS(dentry));
453 }
454 
ovl_dentry_clear_flag(unsigned long flag,struct dentry * dentry)455 void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
456 {
457 	clear_bit(flag, OVL_E_FLAGS(dentry));
458 }
459 
ovl_dentry_test_flag(unsigned long flag,struct dentry * dentry)460 bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
461 {
462 	return test_bit(flag, OVL_E_FLAGS(dentry));
463 }
464 
ovl_dentry_is_opaque(struct dentry * dentry)465 bool ovl_dentry_is_opaque(struct dentry *dentry)
466 {
467 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
468 }
469 
ovl_dentry_is_whiteout(struct dentry * dentry)470 bool ovl_dentry_is_whiteout(struct dentry *dentry)
471 {
472 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
473 }
474 
ovl_dentry_set_opaque(struct dentry * dentry)475 void ovl_dentry_set_opaque(struct dentry *dentry)
476 {
477 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
478 }
479 
ovl_dentry_has_xwhiteouts(struct dentry * dentry)480 bool ovl_dentry_has_xwhiteouts(struct dentry *dentry)
481 {
482 	return ovl_dentry_test_flag(OVL_E_XWHITEOUTS, dentry);
483 }
484 
ovl_dentry_set_xwhiteouts(struct dentry * dentry)485 void ovl_dentry_set_xwhiteouts(struct dentry *dentry)
486 {
487 	ovl_dentry_set_flag(OVL_E_XWHITEOUTS, dentry);
488 }
489 
490 /*
491  * ovl_layer_set_xwhiteouts() is called before adding the overlay dir
492  * dentry to dcache, while readdir of that same directory happens after
493  * the overlay dir dentry is in dcache, so if some cpu observes that
494  * ovl_dentry_is_xwhiteouts(), it will also observe layer->has_xwhiteouts
495  * for the layers where xwhiteouts marker was found in that merge dir.
496  */
ovl_layer_set_xwhiteouts(struct ovl_fs * ofs,const struct ovl_layer * layer)497 void ovl_layer_set_xwhiteouts(struct ovl_fs *ofs,
498 			      const struct ovl_layer *layer)
499 {
500 	if (layer->has_xwhiteouts)
501 		return;
502 
503 	/* Write once to read-mostly layer properties */
504 	ofs->layers[layer->idx].has_xwhiteouts = true;
505 }
506 
507 /*
508  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
509  * to return positive, while there's no actual upper alias for the inode.
510  * Copy up code needs to know about the existence of the upper alias, so it
511  * can't use ovl_dentry_upper().
512  */
ovl_dentry_has_upper_alias(struct dentry * dentry)513 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
514 {
515 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
516 }
517 
ovl_dentry_set_upper_alias(struct dentry * dentry)518 void ovl_dentry_set_upper_alias(struct dentry *dentry)
519 {
520 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
521 }
522 
ovl_should_check_upperdata(struct inode * inode)523 static bool ovl_should_check_upperdata(struct inode *inode)
524 {
525 	if (!S_ISREG(inode->i_mode))
526 		return false;
527 
528 	if (!ovl_inode_lower(inode))
529 		return false;
530 
531 	return true;
532 }
533 
ovl_has_upperdata(struct inode * inode)534 bool ovl_has_upperdata(struct inode *inode)
535 {
536 	if (!ovl_should_check_upperdata(inode))
537 		return true;
538 
539 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
540 		return false;
541 	/*
542 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
543 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
544 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
545 	 * before that are visible too.
546 	 */
547 	smp_rmb();
548 	return true;
549 }
550 
ovl_set_upperdata(struct inode * inode)551 void ovl_set_upperdata(struct inode *inode)
552 {
553 	/*
554 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
555 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
556 	 * before it are visible as well.
557 	 */
558 	smp_wmb();
559 	ovl_set_flag(OVL_UPPERDATA, inode);
560 }
561 
562 /* Caller should hold ovl_inode->lock */
ovl_dentry_needs_data_copy_up_locked(struct dentry * dentry,int flags)563 bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
564 {
565 	if (!ovl_open_flags_need_copy_up(flags))
566 		return false;
567 
568 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
569 }
570 
ovl_dentry_needs_data_copy_up(struct dentry * dentry,int flags)571 bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
572 {
573 	if (!ovl_open_flags_need_copy_up(flags))
574 		return false;
575 
576 	return !ovl_has_upperdata(d_inode(dentry));
577 }
578 
ovl_dentry_get_redirect(struct dentry * dentry)579 const char *ovl_dentry_get_redirect(struct dentry *dentry)
580 {
581 	return OVL_I(d_inode(dentry))->redirect;
582 }
583 
ovl_dentry_set_redirect(struct dentry * dentry,const char * redirect)584 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
585 {
586 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
587 
588 	kfree(oi->redirect);
589 	oi->redirect = redirect;
590 }
591 
ovl_inode_update(struct inode * inode,struct dentry * upperdentry)592 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
593 {
594 	struct inode *upperinode = d_inode(upperdentry);
595 
596 	WARN_ON(OVL_I(inode)->__upperdentry);
597 
598 	/*
599 	 * Make sure upperdentry is consistent before making it visible
600 	 */
601 	smp_wmb();
602 	OVL_I(inode)->__upperdentry = upperdentry;
603 	if (inode_unhashed(inode)) {
604 		inode->i_private = upperinode;
605 		__insert_inode_hash(inode, (unsigned long) upperinode);
606 	}
607 }
608 
ovl_dir_version_inc(struct dentry * dentry,bool impurity)609 static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
610 {
611 	struct inode *inode = d_inode(dentry);
612 
613 	WARN_ON(!inode_is_locked(inode));
614 	WARN_ON(!d_is_dir(dentry));
615 	/*
616 	 * Version is used by readdir code to keep cache consistent.
617 	 * For merge dirs (or dirs with origin) all changes need to be noted.
618 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
619 	 * which have been copied up and have origins), so only need to note
620 	 * changes to impure entries.
621 	 */
622 	if (!ovl_dir_is_real(inode) || impurity)
623 		OVL_I(inode)->version++;
624 }
625 
ovl_dir_modified(struct dentry * dentry,bool impurity)626 void ovl_dir_modified(struct dentry *dentry, bool impurity)
627 {
628 	/* Copy mtime/ctime */
629 	ovl_copyattr(d_inode(dentry));
630 
631 	ovl_dir_version_inc(dentry, impurity);
632 }
633 
ovl_inode_version_get(struct inode * inode)634 u64 ovl_inode_version_get(struct inode *inode)
635 {
636 	WARN_ON(!inode_is_locked(inode));
637 	return OVL_I(inode)->version;
638 }
639 
ovl_is_whiteout(struct dentry * dentry)640 bool ovl_is_whiteout(struct dentry *dentry)
641 {
642 	struct inode *inode = dentry->d_inode;
643 
644 	return inode && IS_WHITEOUT(inode);
645 }
646 
647 /*
648  * Use this over ovl_is_whiteout for upper and lower files, as it also
649  * handles overlay.whiteout xattr whiteout files.
650  */
ovl_path_is_whiteout(struct ovl_fs * ofs,const struct path * path)651 bool ovl_path_is_whiteout(struct ovl_fs *ofs, const struct path *path)
652 {
653 	return ovl_is_whiteout(path->dentry) ||
654 		ovl_path_check_xwhiteout_xattr(ofs, path);
655 }
656 
ovl_path_open(const struct path * path,int flags)657 struct file *ovl_path_open(const struct path *path, int flags)
658 {
659 	struct inode *inode = d_inode(path->dentry);
660 	struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
661 	int err, acc_mode;
662 
663 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
664 		BUG();
665 
666 	switch (flags & O_ACCMODE) {
667 	case O_RDONLY:
668 		acc_mode = MAY_READ;
669 		break;
670 	case O_WRONLY:
671 		acc_mode = MAY_WRITE;
672 		break;
673 	default:
674 		BUG();
675 	}
676 
677 	err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
678 	if (err)
679 		return ERR_PTR(err);
680 
681 	/* O_NOATIME is an optimization, don't fail if not permitted */
682 	if (inode_owner_or_capable(real_idmap, inode))
683 		flags |= O_NOATIME;
684 
685 	return dentry_open(path, flags, current_cred());
686 }
687 
688 /* Caller should hold ovl_inode->lock */
ovl_already_copied_up_locked(struct dentry * dentry,int flags)689 static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
690 {
691 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
692 
693 	if (ovl_dentry_upper(dentry) &&
694 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
695 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
696 		return true;
697 
698 	return false;
699 }
700 
ovl_already_copied_up(struct dentry * dentry,int flags)701 bool ovl_already_copied_up(struct dentry *dentry, int flags)
702 {
703 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
704 
705 	/*
706 	 * Check if copy-up has happened as well as for upper alias (in
707 	 * case of hard links) is there.
708 	 *
709 	 * Both checks are lockless:
710 	 *  - false negatives: will recheck under oi->lock
711 	 *  - false positives:
712 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
713 	 *      upper dentry is up-to-date
714 	 *    + ovl_dentry_has_upper_alias() relies on locking of
715 	 *      upper parent i_rwsem to prevent reordering copy-up
716 	 *      with rename.
717 	 */
718 	if (ovl_dentry_upper(dentry) &&
719 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
720 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
721 		return true;
722 
723 	return false;
724 }
725 
726 /*
727  * The copy up "transaction" keeps an elevated mnt write count on upper mnt,
728  * but leaves taking freeze protection on upper sb to lower level helpers.
729  */
ovl_copy_up_start(struct dentry * dentry,int flags)730 int ovl_copy_up_start(struct dentry *dentry, int flags)
731 {
732 	struct inode *inode = d_inode(dentry);
733 	int err;
734 
735 	err = ovl_inode_lock_interruptible(inode);
736 	if (err)
737 		return err;
738 
739 	if (ovl_already_copied_up_locked(dentry, flags))
740 		err = 1; /* Already copied up */
741 	else
742 		err = ovl_get_write_access(dentry);
743 	if (err)
744 		goto out_unlock;
745 
746 	return 0;
747 
748 out_unlock:
749 	ovl_inode_unlock(inode);
750 	return err;
751 }
752 
ovl_copy_up_end(struct dentry * dentry)753 void ovl_copy_up_end(struct dentry *dentry)
754 {
755 	ovl_put_write_access(dentry);
756 	ovl_inode_unlock(d_inode(dentry));
757 }
758 
ovl_path_check_origin_xattr(struct ovl_fs * ofs,const struct path * path)759 bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
760 {
761 	int res;
762 
763 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
764 
765 	/* Zero size value means "copied up but origin unknown" */
766 	if (res >= 0)
767 		return true;
768 
769 	return false;
770 }
771 
ovl_path_check_xwhiteout_xattr(struct ovl_fs * ofs,const struct path * path)772 bool ovl_path_check_xwhiteout_xattr(struct ovl_fs *ofs, const struct path *path)
773 {
774 	struct dentry *dentry = path->dentry;
775 	int res;
776 
777 	/* xattr.whiteout must be a zero size regular file */
778 	if (!d_is_reg(dentry) || i_size_read(d_inode(dentry)) != 0)
779 		return false;
780 
781 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_XWHITEOUT, NULL, 0);
782 	return res >= 0;
783 }
784 
785 /*
786  * Load persistent uuid from xattr into s_uuid if found, or store a new
787  * random generated value in s_uuid and in xattr.
788  */
ovl_init_uuid_xattr(struct super_block * sb,struct ovl_fs * ofs,const struct path * upperpath)789 bool ovl_init_uuid_xattr(struct super_block *sb, struct ovl_fs *ofs,
790 			 const struct path *upperpath)
791 {
792 	bool set = false;
793 	uuid_t uuid;
794 	int res;
795 
796 	/* Try to load existing persistent uuid */
797 	res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_UUID, uuid.b,
798 				UUID_SIZE);
799 	if (res == UUID_SIZE)
800 		goto set_uuid;
801 
802 	if (res != -ENODATA)
803 		goto fail;
804 
805 	/*
806 	 * With uuid=auto, if uuid xattr is found, it will be used.
807 	 * If uuid xattrs is not found, generate a persistent uuid only on mount
808 	 * of new overlays where upper root dir is not yet marked as impure.
809 	 * An upper dir is marked as impure on copy up or lookup of its subdirs.
810 	 */
811 	if (ofs->config.uuid == OVL_UUID_AUTO) {
812 		res = ovl_path_getxattr(ofs, upperpath, OVL_XATTR_IMPURE, NULL,
813 					0);
814 		if (res > 0) {
815 			/* Any mount of old overlay - downgrade to uuid=null */
816 			ofs->config.uuid = OVL_UUID_NULL;
817 			return true;
818 		} else if (res == -ENODATA) {
819 			/* First mount of new overlay - upgrade to uuid=on */
820 			ofs->config.uuid = OVL_UUID_ON;
821 		} else if (res < 0) {
822 			goto fail;
823 		}
824 
825 	}
826 
827 	/* Generate overlay instance uuid */
828 	uuid_gen(&uuid);
829 
830 	/* Try to store persistent uuid */
831 	set = true;
832 	res = ovl_setxattr(ofs, upperpath->dentry, OVL_XATTR_UUID, uuid.b,
833 			   UUID_SIZE);
834 	if (res)
835 		goto fail;
836 
837 set_uuid:
838 	super_set_uuid(sb, uuid.b, sizeof(uuid));
839 	return true;
840 
841 fail:
842 	ofs->config.uuid = OVL_UUID_NULL;
843 	pr_warn("failed to %s uuid (%pd2, err=%i); falling back to uuid=null.\n",
844 		set ? "set" : "get", upperpath->dentry, res);
845 	return false;
846 }
847 
ovl_get_dir_xattr_val(struct ovl_fs * ofs,const struct path * path,enum ovl_xattr ox)848 char ovl_get_dir_xattr_val(struct ovl_fs *ofs, const struct path *path,
849 			   enum ovl_xattr ox)
850 {
851 	int res;
852 	char val;
853 
854 	if (!d_is_dir(path->dentry))
855 		return 0;
856 
857 	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
858 	return res == 1 ? val : 0;
859 }
860 
861 #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
862 #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
863 #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
864 #define OVL_XATTR_IMPURE_POSTFIX	"impure"
865 #define OVL_XATTR_NLINK_POSTFIX		"nlink"
866 #define OVL_XATTR_UPPER_POSTFIX		"upper"
867 #define OVL_XATTR_UUID_POSTFIX		"uuid"
868 #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
869 #define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
870 #define OVL_XATTR_XWHITEOUT_POSTFIX	"whiteout"
871 
872 #define OVL_XATTR_TAB_ENTRY(x) \
873 	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
874 		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
875 
876 const char *const ovl_xattr_table[][2] = {
877 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
878 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
879 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
880 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
881 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
882 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
883 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UUID),
884 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
885 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
886 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_XWHITEOUT),
887 };
888 
ovl_check_setxattr(struct ovl_fs * ofs,struct dentry * upperdentry,enum ovl_xattr ox,const void * value,size_t size,int xerr)889 int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
890 		       enum ovl_xattr ox, const void *value, size_t size,
891 		       int xerr)
892 {
893 	int err;
894 
895 	if (ofs->noxattr)
896 		return xerr;
897 
898 	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
899 
900 	if (err == -EOPNOTSUPP) {
901 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
902 		ofs->noxattr = true;
903 		return xerr;
904 	}
905 
906 	return err;
907 }
908 
ovl_set_impure(struct dentry * dentry,struct dentry * upperdentry)909 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
910 {
911 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
912 	int err;
913 
914 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
915 		return 0;
916 
917 	/*
918 	 * Do not fail when upper doesn't support xattrs.
919 	 * Upper inodes won't have origin nor redirect xattr anyway.
920 	 */
921 	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
922 	if (!err)
923 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
924 
925 	return err;
926 }
927 
928 
929 #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
930 
ovl_check_protattr(struct inode * inode,struct dentry * upper)931 void ovl_check_protattr(struct inode *inode, struct dentry *upper)
932 {
933 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
934 	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
935 	char buf[OVL_PROTATTR_MAX+1];
936 	int res, n;
937 
938 	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
939 				 OVL_PROTATTR_MAX);
940 	if (res < 0)
941 		return;
942 
943 	/*
944 	 * Initialize inode flags from overlay.protattr xattr and upper inode
945 	 * flags.  If upper inode has those fileattr flags set (i.e. from old
946 	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
947 	 * them on next fileattr_set().
948 	 */
949 	for (n = 0; n < res; n++) {
950 		if (buf[n] == 'a')
951 			iflags |= S_APPEND;
952 		else if (buf[n] == 'i')
953 			iflags |= S_IMMUTABLE;
954 		else
955 			break;
956 	}
957 
958 	if (!res || n < res) {
959 		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
960 				    upper, res);
961 	} else {
962 		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
963 	}
964 }
965 
ovl_set_protattr(struct inode * inode,struct dentry * upper,struct file_kattr * fa)966 int ovl_set_protattr(struct inode *inode, struct dentry *upper,
967 		      struct file_kattr *fa)
968 {
969 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
970 	char buf[OVL_PROTATTR_MAX];
971 	int len = 0, err = 0;
972 	u32 iflags = 0;
973 
974 	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
975 
976 	if (fa->flags & FS_APPEND_FL) {
977 		buf[len++] = 'a';
978 		iflags |= S_APPEND;
979 	}
980 	if (fa->flags & FS_IMMUTABLE_FL) {
981 		buf[len++] = 'i';
982 		iflags |= S_IMMUTABLE;
983 	}
984 
985 	/*
986 	 * Do not allow to set protection flags when upper doesn't support
987 	 * xattrs, because we do not set those fileattr flags on upper inode.
988 	 * Remove xattr if it exist and all protection flags are cleared.
989 	 */
990 	if (len) {
991 		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
992 					 buf, len, -EPERM);
993 	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
994 		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
995 		if (err == -EOPNOTSUPP || err == -ENODATA)
996 			err = 0;
997 	}
998 	if (err)
999 		return err;
1000 
1001 	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
1002 
1003 	/* Mask out the fileattr flags that should not be set in upper inode */
1004 	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
1005 	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
1006 
1007 	return 0;
1008 }
1009 
1010 /*
1011  * Caller must hold a reference to inode to prevent it from being freed while
1012  * it is marked inuse.
1013  */
ovl_inuse_trylock(struct dentry * dentry)1014 bool ovl_inuse_trylock(struct dentry *dentry)
1015 {
1016 	struct inode *inode = d_inode(dentry);
1017 	bool locked = false;
1018 
1019 	spin_lock(&inode->i_lock);
1020 	if (!(inode_state_read(inode) & I_OVL_INUSE)) {
1021 		inode_state_set(inode, I_OVL_INUSE);
1022 		locked = true;
1023 	}
1024 	spin_unlock(&inode->i_lock);
1025 
1026 	return locked;
1027 }
1028 
ovl_inuse_unlock(struct dentry * dentry)1029 void ovl_inuse_unlock(struct dentry *dentry)
1030 {
1031 	if (dentry) {
1032 		struct inode *inode = d_inode(dentry);
1033 
1034 		spin_lock(&inode->i_lock);
1035 		WARN_ON(!(inode_state_read(inode) & I_OVL_INUSE));
1036 		inode_state_clear(inode, I_OVL_INUSE);
1037 		spin_unlock(&inode->i_lock);
1038 	}
1039 }
1040 
ovl_is_inuse(struct dentry * dentry)1041 bool ovl_is_inuse(struct dentry *dentry)
1042 {
1043 	struct inode *inode = d_inode(dentry);
1044 	bool inuse;
1045 
1046 	spin_lock(&inode->i_lock);
1047 	inuse = (inode_state_read(inode) & I_OVL_INUSE);
1048 	spin_unlock(&inode->i_lock);
1049 
1050 	return inuse;
1051 }
1052 
1053 /*
1054  * Does this overlay dentry need to be indexed on copy up?
1055  */
ovl_need_index(struct dentry * dentry)1056 bool ovl_need_index(struct dentry *dentry)
1057 {
1058 	struct dentry *lower = ovl_dentry_lower(dentry);
1059 
1060 	if (!lower || !ovl_indexdir(dentry->d_sb))
1061 		return false;
1062 
1063 	/* Index all files for NFS export and consistency verification */
1064 	if (ovl_index_all(dentry->d_sb))
1065 		return true;
1066 
1067 	/* Index only lower hardlinks on copy up */
1068 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
1069 		return true;
1070 
1071 	return false;
1072 }
1073 
1074 /* Caller must hold OVL_I(inode)->lock */
ovl_cleanup_index(struct dentry * dentry)1075 static void ovl_cleanup_index(struct dentry *dentry)
1076 {
1077 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1078 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
1079 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
1080 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
1081 	struct dentry *index = NULL;
1082 	struct inode *inode;
1083 	struct qstr name = { };
1084 	bool got_write = false;
1085 	int err;
1086 
1087 	err = ovl_get_index_name(ofs, lowerdentry, &name);
1088 	if (err)
1089 		goto fail;
1090 
1091 	err = ovl_want_write(dentry);
1092 	if (err)
1093 		goto fail;
1094 
1095 	got_write = true;
1096 	inode = d_inode(upperdentry);
1097 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
1098 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
1099 				    upperdentry, inode->i_ino, inode->i_nlink);
1100 		/*
1101 		 * We either have a bug with persistent union nlink or a lower
1102 		 * hardlink was added while overlay is mounted. Adding a lower
1103 		 * hardlink and then unlinking all overlay hardlinks would drop
1104 		 * overlay nlink to zero before all upper inodes are unlinked.
1105 		 * As a safety measure, when that situation is detected, set
1106 		 * the overlay nlink to the index inode nlink minus one for the
1107 		 * index entry itself.
1108 		 */
1109 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
1110 		ovl_set_nlink_upper(dentry);
1111 		goto out;
1112 	}
1113 
1114 	index = ovl_lookup_upper_unlocked(ofs, name.name, indexdir, name.len);
1115 	err = PTR_ERR(index);
1116 	if (IS_ERR(index)) {
1117 		index = NULL;
1118 	} else if (ovl_index_all(dentry->d_sb)) {
1119 		/* Whiteout orphan index to block future open by handle */
1120 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
1121 					       indexdir, index);
1122 	} else {
1123 		/* Cleanup orphan index entries */
1124 		err = ovl_cleanup(ofs, indexdir, index);
1125 	}
1126 	if (err)
1127 		goto fail;
1128 
1129 out:
1130 	if (got_write)
1131 		ovl_drop_write(dentry);
1132 	kfree(name.name);
1133 	dput(index);
1134 	return;
1135 
1136 fail:
1137 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
1138 	goto out;
1139 }
1140 
1141 /*
1142  * Operations that change overlay inode and upper inode nlink need to be
1143  * synchronized with copy up for persistent nlink accounting.
1144  */
ovl_nlink_start(struct dentry * dentry)1145 int ovl_nlink_start(struct dentry *dentry)
1146 {
1147 	struct inode *inode = d_inode(dentry);
1148 	int err;
1149 
1150 	if (WARN_ON(!inode))
1151 		return -ENOENT;
1152 
1153 	/*
1154 	 * With inodes index is enabled, we store the union overlay nlink
1155 	 * in an xattr on the index inode. When whiting out an indexed lower,
1156 	 * we need to decrement the overlay persistent nlink, but before the
1157 	 * first copy up, we have no upper index inode to store the xattr.
1158 	 *
1159 	 * As a workaround, before whiteout/rename over an indexed lower,
1160 	 * copy up to create the upper index. Creating the upper index will
1161 	 * initialize the overlay nlink, so it could be dropped if unlink
1162 	 * or rename succeeds.
1163 	 *
1164 	 * TODO: implement metadata only index copy up when called with
1165 	 *       ovl_copy_up_flags(dentry, O_PATH).
1166 	 */
1167 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
1168 		err = ovl_copy_up(dentry);
1169 		if (err)
1170 			return err;
1171 	}
1172 
1173 	err = ovl_inode_lock_interruptible(inode);
1174 	if (err)
1175 		return err;
1176 
1177 	err = ovl_want_write(dentry);
1178 	if (err)
1179 		goto out_unlock;
1180 
1181 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
1182 		return 0;
1183 
1184 	/*
1185 	 * The overlay inode nlink should be incremented/decremented IFF the
1186 	 * upper operation succeeds, along with nlink change of upper inode.
1187 	 * Therefore, before link/unlink/rename, we store the union nlink
1188 	 * value relative to the upper inode nlink in an upper inode xattr.
1189 	 */
1190 	with_ovl_creds(dentry->d_sb)
1191 		err = ovl_set_nlink_upper(dentry);
1192 	if (err)
1193 		goto out_drop_write;
1194 
1195 	return 0;
1196 
1197 out_drop_write:
1198 	ovl_drop_write(dentry);
1199 out_unlock:
1200 	ovl_inode_unlock(inode);
1201 
1202 	return err;
1203 }
1204 
ovl_nlink_end(struct dentry * dentry)1205 void ovl_nlink_end(struct dentry *dentry)
1206 {
1207 	struct inode *inode = d_inode(dentry);
1208 
1209 	ovl_drop_write(dentry);
1210 
1211 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
1212 		with_ovl_creds(dentry->d_sb)
1213 			ovl_cleanup_index(dentry);
1214 	}
1215 
1216 	ovl_inode_unlock(inode);
1217 }
1218 
ovl_lock_rename_workdir(struct dentry * workdir,struct dentry * work,struct dentry * upperdir,struct dentry * upper)1219 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *work,
1220 			    struct dentry *upperdir, struct dentry *upper)
1221 {
1222 	struct dentry *trap;
1223 
1224 	/* Workdir should not be subdir of upperdir and vice versa */
1225 	trap = lock_rename(workdir, upperdir);
1226 	if (IS_ERR(trap))
1227 		goto err;
1228 	if (trap)
1229 		goto err_unlock;
1230 	if (work && (work->d_parent != workdir || d_unhashed(work)))
1231 		goto err_unlock;
1232 	if (upper && (upper->d_parent != upperdir || d_unhashed(upper)))
1233 		goto err_unlock;
1234 
1235 	return 0;
1236 
1237 err_unlock:
1238 	unlock_rename(workdir, upperdir);
1239 err:
1240 	pr_err("failed to lock workdir+upperdir\n");
1241 	return -EIO;
1242 }
1243 
1244 /*
1245  * err < 0, 0 if no metacopy xattr, metacopy data size if xattr found.
1246  * an empty xattr returns OVL_METACOPY_MIN_SIZE to distinguish from no xattr value.
1247  */
ovl_check_metacopy_xattr(struct ovl_fs * ofs,const struct path * path,struct ovl_metacopy * data)1248 int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path,
1249 			     struct ovl_metacopy *data)
1250 {
1251 	int res;
1252 
1253 	/* Only regular files can have metacopy xattr */
1254 	if (!S_ISREG(d_inode(path->dentry)->i_mode))
1255 		return 0;
1256 
1257 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY,
1258 				data, data ? OVL_METACOPY_MAX_SIZE : 0);
1259 	if (res < 0) {
1260 		if (res == -ENODATA || res == -EOPNOTSUPP)
1261 			return 0;
1262 		/*
1263 		 * getxattr on user.* may fail with EACCES in case there's no
1264 		 * read permission on the inode.  Not much we can do, other than
1265 		 * tell the caller that this is not a metacopy inode.
1266 		 */
1267 		if (ofs->config.userxattr && res == -EACCES)
1268 			return 0;
1269 		goto out;
1270 	}
1271 
1272 	if (res == 0) {
1273 		/* Emulate empty data for zero size metacopy xattr */
1274 		res = OVL_METACOPY_MIN_SIZE;
1275 		if (data) {
1276 			memset(data, 0, res);
1277 			data->len = res;
1278 		}
1279 	} else if (res < OVL_METACOPY_MIN_SIZE) {
1280 		pr_warn_ratelimited("metacopy file '%pd' has too small xattr\n",
1281 				    path->dentry);
1282 		return -EIO;
1283 	} else if (data) {
1284 		if (data->version != 0) {
1285 			pr_warn_ratelimited("metacopy file '%pd' has unsupported version\n",
1286 					    path->dentry);
1287 			return -EIO;
1288 		}
1289 		if (res != data->len) {
1290 			pr_warn_ratelimited("metacopy file '%pd' has invalid xattr size\n",
1291 					    path->dentry);
1292 			return -EIO;
1293 		}
1294 	}
1295 
1296 	return res;
1297 out:
1298 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
1299 	return res;
1300 }
1301 
ovl_set_metacopy_xattr(struct ovl_fs * ofs,struct dentry * d,struct ovl_metacopy * metacopy)1302 int ovl_set_metacopy_xattr(struct ovl_fs *ofs, struct dentry *d, struct ovl_metacopy *metacopy)
1303 {
1304 	size_t len = metacopy->len;
1305 
1306 	/* If no flags or digest fall back to empty metacopy file */
1307 	if (metacopy->version == 0 && metacopy->flags == 0 && metacopy->digest_algo == 0)
1308 		len = 0;
1309 
1310 	return ovl_check_setxattr(ofs, d, OVL_XATTR_METACOPY,
1311 				  metacopy, len, -EOPNOTSUPP);
1312 }
1313 
ovl_is_metacopy_dentry(struct dentry * dentry)1314 bool ovl_is_metacopy_dentry(struct dentry *dentry)
1315 {
1316 	struct ovl_entry *oe = OVL_E(dentry);
1317 
1318 	if (!d_is_reg(dentry))
1319 		return false;
1320 
1321 	if (ovl_dentry_upper(dentry)) {
1322 		if (!ovl_has_upperdata(d_inode(dentry)))
1323 			return true;
1324 		return false;
1325 	}
1326 
1327 	return (ovl_numlower(oe) > 1);
1328 }
1329 
ovl_get_redirect_xattr(struct ovl_fs * ofs,const struct path * path,int padding)1330 char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
1331 {
1332 	int res;
1333 	char *s, *next, *buf = NULL;
1334 
1335 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
1336 	if (res == -ENODATA || res == -EOPNOTSUPP)
1337 		return NULL;
1338 	if (res < 0)
1339 		goto fail;
1340 	if (res == 0)
1341 		goto invalid;
1342 
1343 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
1344 	if (!buf)
1345 		return ERR_PTR(-ENOMEM);
1346 
1347 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
1348 	if (res < 0)
1349 		goto fail;
1350 	if (res == 0)
1351 		goto invalid;
1352 
1353 	if (buf[0] == '/') {
1354 		for (s = buf; *s++ == '/'; s = next) {
1355 			next = strchrnul(s, '/');
1356 			if (s == next)
1357 				goto invalid;
1358 		}
1359 	} else {
1360 		if (strchr(buf, '/') != NULL)
1361 			goto invalid;
1362 	}
1363 
1364 	return buf;
1365 invalid:
1366 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
1367 	res = -EINVAL;
1368 	goto err_free;
1369 fail:
1370 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
1371 err_free:
1372 	kfree(buf);
1373 	return ERR_PTR(res);
1374 }
1375 
1376 /* Call with mounter creds as it may open the file */
ovl_ensure_verity_loaded(const struct path * datapath)1377 int ovl_ensure_verity_loaded(const struct path *datapath)
1378 {
1379 	struct inode *inode = d_inode(datapath->dentry);
1380 	struct file *filp;
1381 
1382 	if (!fsverity_active(inode) && IS_VERITY(inode)) {
1383 		/*
1384 		 * If this inode was not yet opened, the verity info hasn't been
1385 		 * loaded yet, so we need to do that here to force it into memory.
1386 		 */
1387 		filp = kernel_file_open(datapath, O_RDONLY, current_cred());
1388 		if (IS_ERR(filp))
1389 			return PTR_ERR(filp);
1390 		fput(filp);
1391 	}
1392 
1393 	return 0;
1394 }
1395 
ovl_validate_verity(struct ovl_fs * ofs,const struct path * metapath,const struct path * datapath)1396 int ovl_validate_verity(struct ovl_fs *ofs,
1397 			const struct path *metapath,
1398 			const struct path *datapath)
1399 {
1400 	struct ovl_metacopy metacopy_data;
1401 	u8 actual_digest[FS_VERITY_MAX_DIGEST_SIZE];
1402 	int xattr_digest_size, digest_size;
1403 	int xattr_size, err;
1404 	u8 verity_algo;
1405 
1406 	if (!ofs->config.verity_mode ||
1407 	    /* Verity only works on regular files */
1408 	    !S_ISREG(d_inode(metapath->dentry)->i_mode))
1409 		return 0;
1410 
1411 	xattr_size = ovl_check_metacopy_xattr(ofs, metapath, &metacopy_data);
1412 	if (xattr_size < 0)
1413 		return xattr_size;
1414 
1415 	if (!xattr_size || !metacopy_data.digest_algo) {
1416 		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1417 			pr_warn_ratelimited("metacopy file '%pd' has no digest specified\n",
1418 					    metapath->dentry);
1419 			return -EIO;
1420 		}
1421 		return 0;
1422 	}
1423 
1424 	xattr_digest_size = ovl_metadata_digest_size(&metacopy_data);
1425 
1426 	err = ovl_ensure_verity_loaded(datapath);
1427 	if (err < 0) {
1428 		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1429 				    datapath->dentry);
1430 		return -EIO;
1431 	}
1432 
1433 	digest_size = fsverity_get_digest(d_inode(datapath->dentry), actual_digest,
1434 					  &verity_algo, NULL);
1435 	if (digest_size == 0) {
1436 		pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n", datapath->dentry);
1437 		return -EIO;
1438 	}
1439 
1440 	if (xattr_digest_size != digest_size ||
1441 	    metacopy_data.digest_algo != verity_algo ||
1442 	    memcmp(metacopy_data.digest, actual_digest, xattr_digest_size) != 0) {
1443 		pr_warn_ratelimited("lower file '%pd' has the wrong fs-verity digest\n",
1444 				    datapath->dentry);
1445 		return -EIO;
1446 	}
1447 
1448 	return 0;
1449 }
1450 
ovl_get_verity_digest(struct ovl_fs * ofs,const struct path * src,struct ovl_metacopy * metacopy)1451 int ovl_get_verity_digest(struct ovl_fs *ofs, const struct path *src,
1452 			  struct ovl_metacopy *metacopy)
1453 {
1454 	int err, digest_size;
1455 
1456 	if (!ofs->config.verity_mode || !S_ISREG(d_inode(src->dentry)->i_mode))
1457 		return 0;
1458 
1459 	err = ovl_ensure_verity_loaded(src);
1460 	if (err < 0) {
1461 		pr_warn_ratelimited("lower file '%pd' failed to load fs-verity info\n",
1462 				    src->dentry);
1463 		return -EIO;
1464 	}
1465 
1466 	digest_size = fsverity_get_digest(d_inode(src->dentry),
1467 					  metacopy->digest, &metacopy->digest_algo, NULL);
1468 	if (digest_size == 0 ||
1469 	    WARN_ON_ONCE(digest_size > FS_VERITY_MAX_DIGEST_SIZE)) {
1470 		if (ofs->config.verity_mode == OVL_VERITY_REQUIRE) {
1471 			pr_warn_ratelimited("lower file '%pd' has no fs-verity digest\n",
1472 					    src->dentry);
1473 			return -EIO;
1474 		}
1475 		return 0;
1476 	}
1477 
1478 	metacopy->len += digest_size;
1479 	return 0;
1480 }
1481 
1482 /*
1483  * ovl_sync_status() - Check fs sync status for volatile mounts
1484  *
1485  * Returns 1 if this is not a volatile mount and a real sync is required.
1486  *
1487  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1488  * have occurred on the upperdir since the mount.
1489  *
1490  * Returns -errno if it is a volatile mount, and the error that occurred since
1491  * the last mount. If the error code changes, it'll return the latest error
1492  * code.
1493  */
1494 
ovl_sync_status(struct ovl_fs * ofs)1495 int ovl_sync_status(struct ovl_fs *ofs)
1496 {
1497 	struct vfsmount *mnt;
1498 
1499 	if (ovl_should_sync(ofs))
1500 		return 1;
1501 
1502 	mnt = ovl_upper_mnt(ofs);
1503 	if (!mnt)
1504 		return 0;
1505 
1506 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1507 }
1508 
1509 /*
1510  * ovl_copyattr() - copy inode attributes from layer to ovl inode
1511  *
1512  * When overlay copies inode information from an upper or lower layer to the
1513  * relevant overlay inode it will apply the idmapping of the upper or lower
1514  * layer when doing so ensuring that the ovl inode ownership will correctly
1515  * reflect the ownership of the idmapped upper or lower layer. For example, an
1516  * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1517  * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1518  * helpers are nops when the relevant layer isn't idmapped.
1519  */
ovl_copyattr(struct inode * inode)1520 void ovl_copyattr(struct inode *inode)
1521 {
1522 	struct path realpath;
1523 	struct inode *realinode;
1524 	struct mnt_idmap *real_idmap;
1525 	vfsuid_t vfsuid;
1526 	vfsgid_t vfsgid;
1527 
1528 	realinode = ovl_i_path_real(inode, &realpath);
1529 	real_idmap = mnt_idmap(realpath.mnt);
1530 
1531 	spin_lock(&inode->i_lock);
1532 	vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1533 	vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
1534 
1535 	inode->i_uid = vfsuid_into_kuid(vfsuid);
1536 	inode->i_gid = vfsgid_into_kgid(vfsgid);
1537 	inode->i_mode = realinode->i_mode;
1538 	inode_set_atime_to_ts(inode, inode_get_atime(realinode));
1539 	inode_set_mtime_to_ts(inode, inode_get_mtime(realinode));
1540 	inode_set_ctime_to_ts(inode, inode_get_ctime(realinode));
1541 	i_size_write(inode, i_size_read(realinode));
1542 	spin_unlock(&inode->i_lock);
1543 }
1544