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