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