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