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