xref: /openbmc/linux/fs/overlayfs/util.c (revision 1cdb0cb6)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bbb1e54dSMiklos Szeredi /*
3bbb1e54dSMiklos Szeredi  * Copyright (C) 2011 Novell Inc.
4bbb1e54dSMiklos Szeredi  * Copyright (C) 2016 Red Hat, Inc.
5bbb1e54dSMiklos Szeredi  */
6bbb1e54dSMiklos Szeredi 
7bbb1e54dSMiklos Szeredi #include <linux/fs.h>
8bbb1e54dSMiklos Szeredi #include <linux/mount.h>
9bbb1e54dSMiklos Szeredi #include <linux/slab.h>
105b825c3aSIngo Molnar #include <linux/cred.h>
11bbb1e54dSMiklos Szeredi #include <linux/xattr.h>
1202bcd157SAmir Goldstein #include <linux/exportfs.h>
1302bcd157SAmir Goldstein #include <linux/uuid.h>
14caf70cb2SAmir Goldstein #include <linux/namei.h>
15caf70cb2SAmir Goldstein #include <linux/ratelimit.h>
16bbb1e54dSMiklos Szeredi #include "overlayfs.h"
17bbb1e54dSMiklos Szeredi 
18bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry)
19bbb1e54dSMiklos Szeredi {
20bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2108f4c7c8SMiklos Szeredi 	return mnt_want_write(ovl_upper_mnt(ofs));
22bbb1e54dSMiklos Szeredi }
23bbb1e54dSMiklos Szeredi 
24bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry)
25bbb1e54dSMiklos Szeredi {
26bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2708f4c7c8SMiklos Szeredi 	mnt_drop_write(ovl_upper_mnt(ofs));
28bbb1e54dSMiklos Szeredi }
29bbb1e54dSMiklos Szeredi 
30bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry)
31bbb1e54dSMiklos Szeredi {
32bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
33bbb1e54dSMiklos Szeredi 	return ofs->workdir;
34bbb1e54dSMiklos Szeredi }
35bbb1e54dSMiklos Szeredi 
36bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb)
37bbb1e54dSMiklos Szeredi {
38bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
39bbb1e54dSMiklos Szeredi 
40bbb1e54dSMiklos Szeredi 	return override_creds(ofs->creator_cred);
41bbb1e54dSMiklos Szeredi }
42bbb1e54dSMiklos Szeredi 
43e487d889SAmir Goldstein /*
44e487d889SAmir Goldstein  * Check if underlying fs supports file handles and try to determine encoding
45e487d889SAmir Goldstein  * type, in order to deduce maximum inode number used by fs.
46e487d889SAmir Goldstein  *
47e487d889SAmir Goldstein  * Return 0 if file handles are not supported.
48e487d889SAmir Goldstein  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
49e487d889SAmir Goldstein  * Return -1 if fs uses a non default encoding with unknown inode size.
50e487d889SAmir Goldstein  */
51e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb)
5202bcd157SAmir Goldstein {
539df085f3SAmir Goldstein 	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
54e487d889SAmir Goldstein 		return 0;
55e487d889SAmir Goldstein 
56e487d889SAmir Goldstein 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
5702bcd157SAmir Goldstein }
5802bcd157SAmir Goldstein 
5902bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb)
6002bcd157SAmir Goldstein {
6102bcd157SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
6202bcd157SAmir Goldstein 
6302bcd157SAmir Goldstein 	return ofs->indexdir;
6402bcd157SAmir Goldstein }
6502bcd157SAmir Goldstein 
66f168f109SAmir Goldstein /* Index all files on copy up. For now only enabled for NFS export */
67f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb)
68f168f109SAmir Goldstein {
69f168f109SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
70f168f109SAmir Goldstein 
71f168f109SAmir Goldstein 	return ofs->config.nfs_export && ofs->config.index;
72f168f109SAmir Goldstein }
73f168f109SAmir Goldstein 
74f168f109SAmir Goldstein /* Verify lower origin on lookup. For now only enabled for NFS export */
75f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb)
76f168f109SAmir Goldstein {
77f168f109SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
78f168f109SAmir Goldstein 
79f168f109SAmir Goldstein 	return ofs->config.nfs_export && ofs->config.index;
80f168f109SAmir Goldstein }
81f168f109SAmir Goldstein 
82bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
83bbb1e54dSMiklos Szeredi {
84bbb1e54dSMiklos Szeredi 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
85bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
86bbb1e54dSMiklos Szeredi 
87bbb1e54dSMiklos Szeredi 	if (oe)
88bbb1e54dSMiklos Szeredi 		oe->numlower = numlower;
89bbb1e54dSMiklos Szeredi 
90bbb1e54dSMiklos Szeredi 	return oe;
91bbb1e54dSMiklos Szeredi }
92bbb1e54dSMiklos Szeredi 
93bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry)
94bbb1e54dSMiklos Szeredi {
95bbb1e54dSMiklos Szeredi 	return dentry->d_flags &
967925dad8SMiklos Szeredi 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
97bbb1e54dSMiklos Szeredi }
98bbb1e54dSMiklos Szeredi 
99f4288844SMiklos Szeredi void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
100f4288844SMiklos Szeredi 			     unsigned int mask)
101f4288844SMiklos Szeredi {
102f4288844SMiklos Szeredi 	struct ovl_entry *oe = OVL_E(dentry);
103f4288844SMiklos Szeredi 	unsigned int i, flags = 0;
104f4288844SMiklos Szeredi 
105bccece1eSMiklos Szeredi 	if (upperdentry)
106bccece1eSMiklos Szeredi 		flags |= upperdentry->d_flags;
107f4288844SMiklos Szeredi 	for (i = 0; i < oe->numlower; i++)
108f4288844SMiklos Szeredi 		flags |= oe->lowerstack[i].dentry->d_flags;
109f4288844SMiklos Szeredi 
110f4288844SMiklos Szeredi 	spin_lock(&dentry->d_lock);
111f4288844SMiklos Szeredi 	dentry->d_flags &= ~mask;
112f4288844SMiklos Szeredi 	dentry->d_flags |= flags & mask;
113f4288844SMiklos Szeredi 	spin_unlock(&dentry->d_lock);
114f4288844SMiklos Szeredi }
115f4288844SMiklos Szeredi 
116bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry)
117bbb1e54dSMiklos Szeredi {
118bbb1e54dSMiklos Szeredi 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
119bbb1e54dSMiklos Szeredi 				  DCACHE_MANAGE_TRANSIT |
120bbb1e54dSMiklos Szeredi 				  DCACHE_OP_HASH |
121bbb1e54dSMiklos Szeredi 				  DCACHE_OP_COMPARE);
122bbb1e54dSMiklos Szeredi }
123bbb1e54dSMiklos Szeredi 
124bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry)
125bbb1e54dSMiklos Szeredi {
126bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
127bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = 0;
128bbb1e54dSMiklos Szeredi 
12909d8b586SMiklos Szeredi 	if (ovl_dentry_upper(dentry)) {
130bbb1e54dSMiklos Szeredi 		type = __OVL_PATH_UPPER;
131bbb1e54dSMiklos Szeredi 
132bbb1e54dSMiklos Szeredi 		/*
13359548503SAmir Goldstein 		 * Non-dir dentry can hold lower dentry of its copy up origin.
134bbb1e54dSMiklos Szeredi 		 */
13559548503SAmir Goldstein 		if (oe->numlower) {
13660124877SVivek Goyal 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
13759548503SAmir Goldstein 				type |= __OVL_PATH_ORIGIN;
1380b17c28aSVivek Goyal 			if (d_is_dir(dentry) ||
1390b17c28aSVivek Goyal 			    !ovl_has_upperdata(d_inode(dentry)))
140bbb1e54dSMiklos Szeredi 				type |= __OVL_PATH_MERGE;
14159548503SAmir Goldstein 		}
142bbb1e54dSMiklos Szeredi 	} else {
143bbb1e54dSMiklos Szeredi 		if (oe->numlower > 1)
144bbb1e54dSMiklos Szeredi 			type |= __OVL_PATH_MERGE;
145bbb1e54dSMiklos Szeredi 	}
146bbb1e54dSMiklos Szeredi 	return type;
147bbb1e54dSMiklos Szeredi }
148bbb1e54dSMiklos Szeredi 
149bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path)
150bbb1e54dSMiklos Szeredi {
151bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
152bbb1e54dSMiklos Szeredi 
15308f4c7c8SMiklos Szeredi 	path->mnt = ovl_upper_mnt(ofs);
15409d8b586SMiklos Szeredi 	path->dentry = ovl_dentry_upper(dentry);
155bbb1e54dSMiklos Szeredi }
156bbb1e54dSMiklos Szeredi 
157bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path)
158bbb1e54dSMiklos Szeredi {
159bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
160bbb1e54dSMiklos Szeredi 
161b9343632SChandan Rajendra 	if (oe->numlower) {
162b9343632SChandan Rajendra 		path->mnt = oe->lowerstack[0].layer->mnt;
163b9343632SChandan Rajendra 		path->dentry = oe->lowerstack[0].dentry;
164b9343632SChandan Rajendra 	} else {
165b9343632SChandan Rajendra 		*path = (struct path) { };
166b9343632SChandan Rajendra 	}
167bbb1e54dSMiklos Szeredi }
168bbb1e54dSMiklos Szeredi 
1694f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
1704f93b426SVivek Goyal {
1714f93b426SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
1724f93b426SVivek Goyal 
1734f93b426SVivek Goyal 	if (oe->numlower) {
1744f93b426SVivek Goyal 		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
1754f93b426SVivek Goyal 		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
1764f93b426SVivek Goyal 	} else {
1774f93b426SVivek Goyal 		*path = (struct path) { };
1784f93b426SVivek Goyal 	}
1794f93b426SVivek Goyal }
1804f93b426SVivek Goyal 
181bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
182bbb1e54dSMiklos Szeredi {
183bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = ovl_path_type(dentry);
184bbb1e54dSMiklos Szeredi 
185bbb1e54dSMiklos Szeredi 	if (!OVL_TYPE_UPPER(type))
186bbb1e54dSMiklos Szeredi 		ovl_path_lower(dentry, path);
187bbb1e54dSMiklos Szeredi 	else
188bbb1e54dSMiklos Szeredi 		ovl_path_upper(dentry, path);
189bbb1e54dSMiklos Szeredi 
190bbb1e54dSMiklos Szeredi 	return type;
191bbb1e54dSMiklos Szeredi }
192bbb1e54dSMiklos Szeredi 
193bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry)
194bbb1e54dSMiklos Szeredi {
19509d8b586SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
196bbb1e54dSMiklos Szeredi }
197bbb1e54dSMiklos Szeredi 
198bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry)
199bbb1e54dSMiklos Szeredi {
200bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
201bbb1e54dSMiklos Szeredi 
20209d8b586SMiklos Szeredi 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
203bbb1e54dSMiklos Szeredi }
204bbb1e54dSMiklos Szeredi 
20513464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
206da309e8cSAmir Goldstein {
207da309e8cSAmir Goldstein 	struct ovl_entry *oe = dentry->d_fsdata;
208da309e8cSAmir Goldstein 
209da309e8cSAmir Goldstein 	return oe->numlower ? oe->lowerstack[0].layer : NULL;
210da309e8cSAmir Goldstein }
211da309e8cSAmir Goldstein 
212647d253fSVivek Goyal /*
213647d253fSVivek Goyal  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
214647d253fSVivek Goyal  * dependig on what is stored in lowerstack[0]. At times we need to find
215647d253fSVivek Goyal  * lower dentry which has data (and not metacopy dentry). This helper
216647d253fSVivek Goyal  * returns the lower data dentry.
217647d253fSVivek Goyal  */
218647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
219647d253fSVivek Goyal {
220647d253fSVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
221647d253fSVivek Goyal 
222647d253fSVivek Goyal 	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
223647d253fSVivek Goyal }
224647d253fSVivek Goyal 
225bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry)
226bbb1e54dSMiklos Szeredi {
22709d8b586SMiklos Szeredi 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
228bbb1e54dSMiklos Szeredi }
229bbb1e54dSMiklos Szeredi 
2301d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode)
2311d88f183SMiklos Szeredi {
2321d88f183SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(inode));
2331d88f183SMiklos Szeredi }
2341d88f183SMiklos Szeredi 
23509d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode)
23625b7713aSMiklos Szeredi {
2371d88f183SMiklos Szeredi 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
23825b7713aSMiklos Szeredi 
23909d8b586SMiklos Szeredi 	return upperdentry ? d_inode(upperdentry) : NULL;
24025b7713aSMiklos Szeredi }
24125b7713aSMiklos Szeredi 
24209d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode)
24309d8b586SMiklos Szeredi {
24409d8b586SMiklos Szeredi 	return OVL_I(inode)->lower;
24509d8b586SMiklos Szeredi }
24609d8b586SMiklos Szeredi 
24709d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode)
24809d8b586SMiklos Szeredi {
24909d8b586SMiklos Szeredi 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
25009d8b586SMiklos Szeredi }
25109d8b586SMiklos Szeredi 
2522664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */
2532664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode)
2542664bd08SVivek Goyal {
2552664bd08SVivek Goyal 	if (WARN_ON(!S_ISREG(inode->i_mode)))
2562664bd08SVivek Goyal 		return NULL;
2572664bd08SVivek Goyal 
2582664bd08SVivek Goyal 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
2592664bd08SVivek Goyal }
26009d8b586SMiklos Szeredi 
2614823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */
2624823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode)
2634823d49cSVivek Goyal {
2644823d49cSVivek Goyal 	struct inode *upperinode;
2654823d49cSVivek Goyal 
2664823d49cSVivek Goyal 	upperinode = ovl_inode_upper(inode);
2674823d49cSVivek Goyal 	if (upperinode && ovl_has_upperdata(inode))
2684823d49cSVivek Goyal 		return upperinode;
2694823d49cSVivek Goyal 
2704823d49cSVivek Goyal 	return ovl_inode_lowerdata(inode);
2714823d49cSVivek Goyal }
2724823d49cSVivek Goyal 
2734edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
274bbb1e54dSMiklos Szeredi {
2754edb83bbSMiklos Szeredi 	return OVL_I(inode)->cache;
276bbb1e54dSMiklos Szeredi }
277bbb1e54dSMiklos Szeredi 
2784edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
279bbb1e54dSMiklos Szeredi {
2804edb83bbSMiklos Szeredi 	OVL_I(inode)->cache = cache;
281bbb1e54dSMiklos Szeredi }
282bbb1e54dSMiklos Szeredi 
283c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
284c62520a8SAmir Goldstein {
285c62520a8SAmir Goldstein 	set_bit(flag, &OVL_E(dentry)->flags);
286c62520a8SAmir Goldstein }
287c62520a8SAmir Goldstein 
288c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
289c62520a8SAmir Goldstein {
290c62520a8SAmir Goldstein 	clear_bit(flag, &OVL_E(dentry)->flags);
291c62520a8SAmir Goldstein }
292c62520a8SAmir Goldstein 
293c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
294c62520a8SAmir Goldstein {
295c62520a8SAmir Goldstein 	return test_bit(flag, &OVL_E(dentry)->flags);
296c62520a8SAmir Goldstein }
297c62520a8SAmir Goldstein 
298bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry)
299bbb1e54dSMiklos Szeredi {
300c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
301bbb1e54dSMiklos Szeredi }
302bbb1e54dSMiklos Szeredi 
303bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry)
304bbb1e54dSMiklos Szeredi {
305bbb1e54dSMiklos Szeredi 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
306bbb1e54dSMiklos Szeredi }
307bbb1e54dSMiklos Szeredi 
3085cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry)
309bbb1e54dSMiklos Szeredi {
310c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
311bbb1e54dSMiklos Szeredi }
312bbb1e54dSMiklos Szeredi 
31355acc661SMiklos Szeredi /*
314aa3ff3c1SAmir Goldstein  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
315aa3ff3c1SAmir Goldstein  * to return positive, while there's no actual upper alias for the inode.
316aa3ff3c1SAmir Goldstein  * Copy up code needs to know about the existence of the upper alias, so it
317aa3ff3c1SAmir Goldstein  * can't use ovl_dentry_upper().
31855acc661SMiklos Szeredi  */
31955acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry)
32055acc661SMiklos Szeredi {
321c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
32255acc661SMiklos Szeredi }
32355acc661SMiklos Szeredi 
32455acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry)
32555acc661SMiklos Szeredi {
326c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
32755acc661SMiklos Szeredi }
32855acc661SMiklos Szeredi 
3290c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode)
3300c288874SVivek Goyal {
3310c288874SVivek Goyal 	if (!S_ISREG(inode->i_mode))
3320c288874SVivek Goyal 		return false;
3330c288874SVivek Goyal 
3340c288874SVivek Goyal 	if (!ovl_inode_lower(inode))
3350c288874SVivek Goyal 		return false;
3360c288874SVivek Goyal 
3370c288874SVivek Goyal 	return true;
3380c288874SVivek Goyal }
3390c288874SVivek Goyal 
3400c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode)
3410c288874SVivek Goyal {
3420c288874SVivek Goyal 	if (!ovl_should_check_upperdata(inode))
3430c288874SVivek Goyal 		return true;
3440c288874SVivek Goyal 
3450c288874SVivek Goyal 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
3460c288874SVivek Goyal 		return false;
3470c288874SVivek Goyal 	/*
3480c288874SVivek Goyal 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
3490c288874SVivek Goyal 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
3500c288874SVivek Goyal 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
3510c288874SVivek Goyal 	 * before that are visible too.
3520c288874SVivek Goyal 	 */
3530c288874SVivek Goyal 	smp_rmb();
3540c288874SVivek Goyal 	return true;
3550c288874SVivek Goyal }
3560c288874SVivek Goyal 
3570c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode)
3580c288874SVivek Goyal {
3590c288874SVivek Goyal 	/*
3600c288874SVivek Goyal 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
3610c288874SVivek Goyal 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
3620c288874SVivek Goyal 	 * before it are visible as well.
3630c288874SVivek Goyal 	 */
3640c288874SVivek Goyal 	smp_wmb();
3650c288874SVivek Goyal 	ovl_set_flag(OVL_UPPERDATA, inode);
3660c288874SVivek Goyal }
3670c288874SVivek Goyal 
3680c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
3690c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
3700c288874SVivek Goyal {
3710c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
3720c288874SVivek Goyal 		return false;
3730c288874SVivek Goyal 
3740c288874SVivek Goyal 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
3750c288874SVivek Goyal }
3760c288874SVivek Goyal 
3770c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
3780c288874SVivek Goyal {
3790c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
3800c288874SVivek Goyal 		return false;
3810c288874SVivek Goyal 
3820c288874SVivek Goyal 	return !ovl_has_upperdata(d_inode(dentry));
3830c288874SVivek Goyal }
3840c288874SVivek Goyal 
385a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb)
386a6c60655SMiklos Szeredi {
387a6c60655SMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
388a6c60655SMiklos Szeredi 
38921a22878SAmir Goldstein 	return ofs->config.redirect_dir && !ofs->noxattr;
390a6c60655SMiklos Szeredi }
391a6c60655SMiklos Szeredi 
392a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry)
393a6c60655SMiklos Szeredi {
394cf31c463SMiklos Szeredi 	return OVL_I(d_inode(dentry))->redirect;
395a6c60655SMiklos Szeredi }
396a6c60655SMiklos Szeredi 
397a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
398a6c60655SMiklos Szeredi {
399cf31c463SMiklos Szeredi 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
400a6c60655SMiklos Szeredi 
401cf31c463SMiklos Szeredi 	kfree(oi->redirect);
402cf31c463SMiklos Szeredi 	oi->redirect = redirect;
403a6c60655SMiklos Szeredi }
404a6c60655SMiklos Szeredi 
40509d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
406bbb1e54dSMiklos Szeredi {
40709d8b586SMiklos Szeredi 	struct inode *upperinode = d_inode(upperdentry);
408e6d2ebddSMiklos Szeredi 
40909d8b586SMiklos Szeredi 	WARN_ON(OVL_I(inode)->__upperdentry);
41009d8b586SMiklos Szeredi 
41125b7713aSMiklos Szeredi 	/*
41209d8b586SMiklos Szeredi 	 * Make sure upperdentry is consistent before making it visible
41325b7713aSMiklos Szeredi 	 */
41425b7713aSMiklos Szeredi 	smp_wmb();
41509d8b586SMiklos Szeredi 	OVL_I(inode)->__upperdentry = upperdentry;
41631747edaSAmir Goldstein 	if (inode_unhashed(inode)) {
41725b7713aSMiklos Szeredi 		inode->i_private = upperinode;
418bbb1e54dSMiklos Szeredi 		__insert_inode_hash(inode, (unsigned long) upperinode);
419bbb1e54dSMiklos Szeredi 	}
42025b7713aSMiklos Szeredi }
421bbb1e54dSMiklos Szeredi 
422d9854c87SMiklos Szeredi static void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
423bbb1e54dSMiklos Szeredi {
42404a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
425bbb1e54dSMiklos Szeredi 
42604a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
4274edb83bbSMiklos Szeredi 	/*
4284edb83bbSMiklos Szeredi 	 * Version is used by readdir code to keep cache consistent.  For merge
4294edb83bbSMiklos Szeredi 	 * dirs all changes need to be noted.  For non-merge dirs, cache only
4304edb83bbSMiklos Szeredi 	 * contains impure (ones which have been copied up and have origins)
4314edb83bbSMiklos Szeredi 	 * entries, so only need to note changes to impure entries.
4324edb83bbSMiklos Szeredi 	 */
4334edb83bbSMiklos Szeredi 	if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
43404a01ac7SMiklos Szeredi 		OVL_I(inode)->version++;
435bbb1e54dSMiklos Szeredi }
436bbb1e54dSMiklos Szeredi 
437d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity)
438d9854c87SMiklos Szeredi {
439d9854c87SMiklos Szeredi 	/* Copy mtime/ctime */
440d9854c87SMiklos Szeredi 	ovl_copyattr(d_inode(ovl_dentry_upper(dentry)), d_inode(dentry));
441d9854c87SMiklos Szeredi 
442d9854c87SMiklos Szeredi 	ovl_dentry_version_inc(dentry, impurity);
443d9854c87SMiklos Szeredi }
444d9854c87SMiklos Szeredi 
445bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry)
446bbb1e54dSMiklos Szeredi {
44704a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
448bbb1e54dSMiklos Szeredi 
44904a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
45004a01ac7SMiklos Szeredi 	return OVL_I(inode)->version;
451bbb1e54dSMiklos Szeredi }
452bbb1e54dSMiklos Szeredi 
453bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry)
454bbb1e54dSMiklos Szeredi {
455bbb1e54dSMiklos Szeredi 	struct inode *inode = dentry->d_inode;
456bbb1e54dSMiklos Szeredi 
457bbb1e54dSMiklos Szeredi 	return inode && IS_WHITEOUT(inode);
458bbb1e54dSMiklos Szeredi }
459bbb1e54dSMiklos Szeredi 
460bbb1e54dSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags)
461bbb1e54dSMiklos Szeredi {
46256230d95SMiklos Szeredi 	struct inode *inode = d_inode(path->dentry);
46356230d95SMiklos Szeredi 	int err, acc_mode;
46456230d95SMiklos Szeredi 
46556230d95SMiklos Szeredi 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
46656230d95SMiklos Szeredi 		BUG();
46756230d95SMiklos Szeredi 
46856230d95SMiklos Szeredi 	switch (flags & O_ACCMODE) {
46956230d95SMiklos Szeredi 	case O_RDONLY:
47056230d95SMiklos Szeredi 		acc_mode = MAY_READ;
47156230d95SMiklos Szeredi 		break;
47256230d95SMiklos Szeredi 	case O_WRONLY:
47356230d95SMiklos Szeredi 		acc_mode = MAY_WRITE;
47456230d95SMiklos Szeredi 		break;
47556230d95SMiklos Szeredi 	default:
47656230d95SMiklos Szeredi 		BUG();
47756230d95SMiklos Szeredi 	}
47856230d95SMiklos Szeredi 
47956230d95SMiklos Szeredi 	err = inode_permission(inode, acc_mode | MAY_OPEN);
48056230d95SMiklos Szeredi 	if (err)
48156230d95SMiklos Szeredi 		return ERR_PTR(err);
48256230d95SMiklos Szeredi 
48356230d95SMiklos Szeredi 	/* O_NOATIME is an optimization, don't fail if not permitted */
48456230d95SMiklos Szeredi 	if (inode_owner_or_capable(inode))
48556230d95SMiklos Szeredi 		flags |= O_NOATIME;
48656230d95SMiklos Szeredi 
48756230d95SMiklos Szeredi 	return dentry_open(path, flags, current_cred());
488bbb1e54dSMiklos Szeredi }
48939d3d60aSAmir Goldstein 
4900c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
4910c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
4920c288874SVivek Goyal {
4930c288874SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
4940c288874SVivek Goyal 
4950c288874SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
4960c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
4970c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
4980c288874SVivek Goyal 		return true;
4990c288874SVivek Goyal 
5000c288874SVivek Goyal 	return false;
5010c288874SVivek Goyal }
5020c288874SVivek Goyal 
5030c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags)
5042002df85SVivek Goyal {
5052002df85SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5062002df85SVivek Goyal 
5072002df85SVivek Goyal 	/*
5082002df85SVivek Goyal 	 * Check if copy-up has happened as well as for upper alias (in
5092002df85SVivek Goyal 	 * case of hard links) is there.
5102002df85SVivek Goyal 	 *
5112002df85SVivek Goyal 	 * Both checks are lockless:
5122002df85SVivek Goyal 	 *  - false negatives: will recheck under oi->lock
5132002df85SVivek Goyal 	 *  - false positives:
5142002df85SVivek Goyal 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
5152002df85SVivek Goyal 	 *      upper dentry is up-to-date
5162002df85SVivek Goyal 	 *    + ovl_dentry_has_upper_alias() relies on locking of
5172002df85SVivek Goyal 	 *      upper parent i_rwsem to prevent reordering copy-up
5182002df85SVivek Goyal 	 *      with rename.
5192002df85SVivek Goyal 	 */
5202002df85SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5210c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5220c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
5232002df85SVivek Goyal 		return true;
5242002df85SVivek Goyal 
5252002df85SVivek Goyal 	return false;
5262002df85SVivek Goyal }
5272002df85SVivek Goyal 
5280c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags)
52939d3d60aSAmir Goldstein {
5301e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
53139d3d60aSAmir Goldstein 	int err;
53239d3d60aSAmir Goldstein 
533531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
5340c288874SVivek Goyal 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
53539d3d60aSAmir Goldstein 		err = 1; /* Already copied up */
5361e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
53739d3d60aSAmir Goldstein 	}
53839d3d60aSAmir Goldstein 
53939d3d60aSAmir Goldstein 	return err;
54039d3d60aSAmir Goldstein }
54139d3d60aSAmir Goldstein 
54239d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry)
54339d3d60aSAmir Goldstein {
5441e92e307SAmir Goldstein 	ovl_inode_unlock(d_inode(dentry));
54539d3d60aSAmir Goldstein }
54682b749b2SAmir Goldstein 
547610afc0bSMiklos Szeredi bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry)
548b79e05aaSAmir Goldstein {
549b79e05aaSAmir Goldstein 	int res;
550b79e05aaSAmir Goldstein 
551610afc0bSMiklos Szeredi 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_ORIGIN, NULL, 0);
552b79e05aaSAmir Goldstein 
553b79e05aaSAmir Goldstein 	/* Zero size value means "copied up but origin unknown" */
554b79e05aaSAmir Goldstein 	if (res >= 0)
555b79e05aaSAmir Goldstein 		return true;
556b79e05aaSAmir Goldstein 
557b79e05aaSAmir Goldstein 	return false;
558b79e05aaSAmir Goldstein }
559b79e05aaSAmir Goldstein 
560610afc0bSMiklos Szeredi bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
56143d193f8SMiklos Szeredi 			 enum ovl_xattr ox)
562f3a15685SAmir Goldstein {
563f3a15685SAmir Goldstein 	int res;
564f3a15685SAmir Goldstein 	char val;
565f3a15685SAmir Goldstein 
566f3a15685SAmir Goldstein 	if (!d_is_dir(dentry))
567f3a15685SAmir Goldstein 		return false;
568f3a15685SAmir Goldstein 
56943d193f8SMiklos Szeredi 	res = ovl_do_getxattr(OVL_FS(sb), dentry, ox, &val, 1);
570f3a15685SAmir Goldstein 	if (res == 1 && val == 'y')
571f3a15685SAmir Goldstein 		return true;
572f3a15685SAmir Goldstein 
573f3a15685SAmir Goldstein 	return false;
574f3a15685SAmir Goldstein }
575f3a15685SAmir Goldstein 
57643d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
57743d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
57843d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
57943d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX	"impure"
58043d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX		"nlink"
58143d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX		"upper"
58243d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
58343d193f8SMiklos Szeredi 
58443d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \
58543d193f8SMiklos Szeredi 	[x] = OVL_XATTR_PREFIX x ## _POSTFIX
58643d193f8SMiklos Szeredi 
58743d193f8SMiklos Szeredi const char *ovl_xattr_table[] = {
58843d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
58943d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
59043d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
59143d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
59243d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
59343d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
59443d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
59543d193f8SMiklos Szeredi };
59643d193f8SMiklos Szeredi 
59782b749b2SAmir Goldstein int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
59843d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
59982b749b2SAmir Goldstein 		       int xerr)
60082b749b2SAmir Goldstein {
60182b749b2SAmir Goldstein 	int err;
60282b749b2SAmir Goldstein 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
60382b749b2SAmir Goldstein 
60482b749b2SAmir Goldstein 	if (ofs->noxattr)
60582b749b2SAmir Goldstein 		return xerr;
60682b749b2SAmir Goldstein 
60743d193f8SMiklos Szeredi 	err = ovl_do_setxattr(ofs, upperdentry, ox, value, size);
60882b749b2SAmir Goldstein 
60982b749b2SAmir Goldstein 	if (err == -EOPNOTSUPP) {
61043d193f8SMiklos Szeredi 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
61182b749b2SAmir Goldstein 		ofs->noxattr = true;
61282b749b2SAmir Goldstein 		return xerr;
61382b749b2SAmir Goldstein 	}
61482b749b2SAmir Goldstein 
61582b749b2SAmir Goldstein 	return err;
61682b749b2SAmir Goldstein }
617f3a15685SAmir Goldstein 
618f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
619f3a15685SAmir Goldstein {
620f3a15685SAmir Goldstein 	int err;
621f3a15685SAmir Goldstein 
62213c72075SMiklos Szeredi 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
623f3a15685SAmir Goldstein 		return 0;
624f3a15685SAmir Goldstein 
625f3a15685SAmir Goldstein 	/*
626f3a15685SAmir Goldstein 	 * Do not fail when upper doesn't support xattrs.
627f3a15685SAmir Goldstein 	 * Upper inodes won't have origin nor redirect xattr anyway.
628f3a15685SAmir Goldstein 	 */
629f3a15685SAmir Goldstein 	err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
630f3a15685SAmir Goldstein 				 "y", 1, 0);
631f3a15685SAmir Goldstein 	if (!err)
63213c72075SMiklos Szeredi 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
633f3a15685SAmir Goldstein 
634f3a15685SAmir Goldstein 	return err;
635f3a15685SAmir Goldstein }
63613c72075SMiklos Szeredi 
63713c72075SMiklos Szeredi void ovl_set_flag(unsigned long flag, struct inode *inode)
63813c72075SMiklos Szeredi {
63913c72075SMiklos Szeredi 	set_bit(flag, &OVL_I(inode)->flags);
64013c72075SMiklos Szeredi }
64113c72075SMiklos Szeredi 
6424edb83bbSMiklos Szeredi void ovl_clear_flag(unsigned long flag, struct inode *inode)
6434edb83bbSMiklos Szeredi {
6444edb83bbSMiklos Szeredi 	clear_bit(flag, &OVL_I(inode)->flags);
6454edb83bbSMiklos Szeredi }
6464edb83bbSMiklos Szeredi 
64713c72075SMiklos Szeredi bool ovl_test_flag(unsigned long flag, struct inode *inode)
64813c72075SMiklos Szeredi {
64913c72075SMiklos Szeredi 	return test_bit(flag, &OVL_I(inode)->flags);
65013c72075SMiklos Szeredi }
651ad0af710SAmir Goldstein 
652ad0af710SAmir Goldstein /**
653ad0af710SAmir Goldstein  * Caller must hold a reference to inode to prevent it from being freed while
654ad0af710SAmir Goldstein  * it is marked inuse.
655ad0af710SAmir Goldstein  */
656ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry)
657ad0af710SAmir Goldstein {
658ad0af710SAmir Goldstein 	struct inode *inode = d_inode(dentry);
659ad0af710SAmir Goldstein 	bool locked = false;
660ad0af710SAmir Goldstein 
661ad0af710SAmir Goldstein 	spin_lock(&inode->i_lock);
662ad0af710SAmir Goldstein 	if (!(inode->i_state & I_OVL_INUSE)) {
663ad0af710SAmir Goldstein 		inode->i_state |= I_OVL_INUSE;
664ad0af710SAmir Goldstein 		locked = true;
665ad0af710SAmir Goldstein 	}
666ad0af710SAmir Goldstein 	spin_unlock(&inode->i_lock);
667ad0af710SAmir Goldstein 
668ad0af710SAmir Goldstein 	return locked;
669ad0af710SAmir Goldstein }
670ad0af710SAmir Goldstein 
671ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry)
672ad0af710SAmir Goldstein {
673ad0af710SAmir Goldstein 	if (dentry) {
674ad0af710SAmir Goldstein 		struct inode *inode = d_inode(dentry);
675ad0af710SAmir Goldstein 
676ad0af710SAmir Goldstein 		spin_lock(&inode->i_lock);
677ad0af710SAmir Goldstein 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
678ad0af710SAmir Goldstein 		inode->i_state &= ~I_OVL_INUSE;
679ad0af710SAmir Goldstein 		spin_unlock(&inode->i_lock);
680ad0af710SAmir Goldstein 	}
681ad0af710SAmir Goldstein }
6825f8415d6SAmir Goldstein 
683146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry)
684146d62e5SAmir Goldstein {
685146d62e5SAmir Goldstein 	struct inode *inode = d_inode(dentry);
686146d62e5SAmir Goldstein 	bool inuse;
687146d62e5SAmir Goldstein 
688146d62e5SAmir Goldstein 	spin_lock(&inode->i_lock);
689146d62e5SAmir Goldstein 	inuse = (inode->i_state & I_OVL_INUSE);
690146d62e5SAmir Goldstein 	spin_unlock(&inode->i_lock);
691146d62e5SAmir Goldstein 
692146d62e5SAmir Goldstein 	return inuse;
693146d62e5SAmir Goldstein }
694146d62e5SAmir Goldstein 
69524b33ee1SAmir Goldstein /*
69624b33ee1SAmir Goldstein  * Does this overlay dentry need to be indexed on copy up?
69724b33ee1SAmir Goldstein  */
69824b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry)
69924b33ee1SAmir Goldstein {
70024b33ee1SAmir Goldstein 	struct dentry *lower = ovl_dentry_lower(dentry);
70124b33ee1SAmir Goldstein 
70224b33ee1SAmir Goldstein 	if (!lower || !ovl_indexdir(dentry->d_sb))
70324b33ee1SAmir Goldstein 		return false;
70424b33ee1SAmir Goldstein 
705fbd2d207SAmir Goldstein 	/* Index all files for NFS export and consistency verification */
706016b720fSAmir Goldstein 	if (ovl_index_all(dentry->d_sb))
707fbd2d207SAmir Goldstein 		return true;
708fbd2d207SAmir Goldstein 
70924b33ee1SAmir Goldstein 	/* Index only lower hardlinks on copy up */
71024b33ee1SAmir Goldstein 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
71124b33ee1SAmir Goldstein 		return true;
71224b33ee1SAmir Goldstein 
71324b33ee1SAmir Goldstein 	return false;
71424b33ee1SAmir Goldstein }
71524b33ee1SAmir Goldstein 
7169f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */
717caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry)
718caf70cb2SAmir Goldstein {
719*1cdb0cb6SPavel Tikhomirov 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
720e7dd0e71SAmir Goldstein 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
721e7dd0e71SAmir Goldstein 	struct inode *dir = indexdir->d_inode;
722caf70cb2SAmir Goldstein 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
723caf70cb2SAmir Goldstein 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
724caf70cb2SAmir Goldstein 	struct dentry *index = NULL;
725caf70cb2SAmir Goldstein 	struct inode *inode;
72663e13252SAmir Goldstein 	struct qstr name = { };
727caf70cb2SAmir Goldstein 	int err;
728caf70cb2SAmir Goldstein 
729*1cdb0cb6SPavel Tikhomirov 	err = ovl_get_index_name(ofs, lowerdentry, &name);
730caf70cb2SAmir Goldstein 	if (err)
731caf70cb2SAmir Goldstein 		goto fail;
732caf70cb2SAmir Goldstein 
733caf70cb2SAmir Goldstein 	inode = d_inode(upperdentry);
73489a17556SAmir Goldstein 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
7351bd0a3aeSlijiazi 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
736caf70cb2SAmir Goldstein 				    upperdentry, inode->i_ino, inode->i_nlink);
737caf70cb2SAmir Goldstein 		/*
738caf70cb2SAmir Goldstein 		 * We either have a bug with persistent union nlink or a lower
739caf70cb2SAmir Goldstein 		 * hardlink was added while overlay is mounted. Adding a lower
740caf70cb2SAmir Goldstein 		 * hardlink and then unlinking all overlay hardlinks would drop
741caf70cb2SAmir Goldstein 		 * overlay nlink to zero before all upper inodes are unlinked.
742caf70cb2SAmir Goldstein 		 * As a safety measure, when that situation is detected, set
743caf70cb2SAmir Goldstein 		 * the overlay nlink to the index inode nlink minus one for the
744caf70cb2SAmir Goldstein 		 * index entry itself.
745caf70cb2SAmir Goldstein 		 */
746caf70cb2SAmir Goldstein 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
747caf70cb2SAmir Goldstein 		ovl_set_nlink_upper(dentry);
748caf70cb2SAmir Goldstein 		goto out;
749caf70cb2SAmir Goldstein 	}
750caf70cb2SAmir Goldstein 
751caf70cb2SAmir Goldstein 	inode_lock_nested(dir, I_MUTEX_PARENT);
752e7dd0e71SAmir Goldstein 	index = lookup_one_len(name.name, indexdir, name.len);
753caf70cb2SAmir Goldstein 	err = PTR_ERR(index);
754e7dd0e71SAmir Goldstein 	if (IS_ERR(index)) {
7559f4ec904SAmir Goldstein 		index = NULL;
756e7dd0e71SAmir Goldstein 	} else if (ovl_index_all(dentry->d_sb)) {
757e7dd0e71SAmir Goldstein 		/* Whiteout orphan index to block future open by handle */
758c21c839bSChengguang Xu 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
759c21c839bSChengguang Xu 					       dir, index);
760e7dd0e71SAmir Goldstein 	} else {
761e7dd0e71SAmir Goldstein 		/* Cleanup orphan index entries */
762e7dd0e71SAmir Goldstein 		err = ovl_cleanup(dir, index);
763e7dd0e71SAmir Goldstein 	}
7649f4ec904SAmir Goldstein 
765caf70cb2SAmir Goldstein 	inode_unlock(dir);
766caf70cb2SAmir Goldstein 	if (err)
767caf70cb2SAmir Goldstein 		goto fail;
768caf70cb2SAmir Goldstein 
769caf70cb2SAmir Goldstein out:
77063e13252SAmir Goldstein 	kfree(name.name);
771caf70cb2SAmir Goldstein 	dput(index);
772caf70cb2SAmir Goldstein 	return;
773caf70cb2SAmir Goldstein 
774caf70cb2SAmir Goldstein fail:
7751bd0a3aeSlijiazi 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
776caf70cb2SAmir Goldstein 	goto out;
777caf70cb2SAmir Goldstein }
778caf70cb2SAmir Goldstein 
7795f8415d6SAmir Goldstein /*
7805f8415d6SAmir Goldstein  * Operations that change overlay inode and upper inode nlink need to be
7815f8415d6SAmir Goldstein  * synchronized with copy up for persistent nlink accounting.
7825f8415d6SAmir Goldstein  */
7830e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry)
7845f8415d6SAmir Goldstein {
7851e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
7865f8415d6SAmir Goldstein 	const struct cred *old_cred;
7875f8415d6SAmir Goldstein 	int err;
7885f8415d6SAmir Goldstein 
7891e92e307SAmir Goldstein 	if (WARN_ON(!inode))
7900e32992fSAmir Goldstein 		return -ENOENT;
7915f8415d6SAmir Goldstein 
7925f8415d6SAmir Goldstein 	/*
7935f8415d6SAmir Goldstein 	 * With inodes index is enabled, we store the union overlay nlink
79424b33ee1SAmir Goldstein 	 * in an xattr on the index inode. When whiting out an indexed lower,
7955f8415d6SAmir Goldstein 	 * we need to decrement the overlay persistent nlink, but before the
7965f8415d6SAmir Goldstein 	 * first copy up, we have no upper index inode to store the xattr.
7975f8415d6SAmir Goldstein 	 *
79824b33ee1SAmir Goldstein 	 * As a workaround, before whiteout/rename over an indexed lower,
7995f8415d6SAmir Goldstein 	 * copy up to create the upper index. Creating the upper index will
8005f8415d6SAmir Goldstein 	 * initialize the overlay nlink, so it could be dropped if unlink
8015f8415d6SAmir Goldstein 	 * or rename succeeds.
8025f8415d6SAmir Goldstein 	 *
8035f8415d6SAmir Goldstein 	 * TODO: implement metadata only index copy up when called with
8045f8415d6SAmir Goldstein 	 *       ovl_copy_up_flags(dentry, O_PATH).
8055f8415d6SAmir Goldstein 	 */
80624b33ee1SAmir Goldstein 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
8075f8415d6SAmir Goldstein 		err = ovl_copy_up(dentry);
8085f8415d6SAmir Goldstein 		if (err)
8095f8415d6SAmir Goldstein 			return err;
8105f8415d6SAmir Goldstein 	}
8115f8415d6SAmir Goldstein 
812531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
8135f8415d6SAmir Goldstein 	if (err)
8145f8415d6SAmir Goldstein 		return err;
8155f8415d6SAmir Goldstein 
8161e92e307SAmir Goldstein 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
8175f8415d6SAmir Goldstein 		goto out;
8185f8415d6SAmir Goldstein 
8195f8415d6SAmir Goldstein 	old_cred = ovl_override_creds(dentry->d_sb);
8205f8415d6SAmir Goldstein 	/*
8215f8415d6SAmir Goldstein 	 * The overlay inode nlink should be incremented/decremented IFF the
8225f8415d6SAmir Goldstein 	 * upper operation succeeds, along with nlink change of upper inode.
8235f8415d6SAmir Goldstein 	 * Therefore, before link/unlink/rename, we store the union nlink
8245f8415d6SAmir Goldstein 	 * value relative to the upper inode nlink in an upper inode xattr.
8255f8415d6SAmir Goldstein 	 */
8265f8415d6SAmir Goldstein 	err = ovl_set_nlink_upper(dentry);
8275f8415d6SAmir Goldstein 	revert_creds(old_cred);
8285f8415d6SAmir Goldstein 
8295f8415d6SAmir Goldstein out:
8305f8415d6SAmir Goldstein 	if (err)
8311e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
8325f8415d6SAmir Goldstein 
8335f8415d6SAmir Goldstein 	return err;
8345f8415d6SAmir Goldstein }
8355f8415d6SAmir Goldstein 
8360e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry)
8375f8415d6SAmir Goldstein {
8381e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
8391e92e307SAmir Goldstein 
8401e92e307SAmir Goldstein 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
841caf70cb2SAmir Goldstein 		const struct cred *old_cred;
842caf70cb2SAmir Goldstein 
843caf70cb2SAmir Goldstein 		old_cred = ovl_override_creds(dentry->d_sb);
844caf70cb2SAmir Goldstein 		ovl_cleanup_index(dentry);
845caf70cb2SAmir Goldstein 		revert_creds(old_cred);
846caf70cb2SAmir Goldstein 	}
847caf70cb2SAmir Goldstein 
8481e92e307SAmir Goldstein 	ovl_inode_unlock(inode);
8495f8415d6SAmir Goldstein }
8505820dc08SAmir Goldstein 
8515820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
8525820dc08SAmir Goldstein {
8535820dc08SAmir Goldstein 	/* Workdir should not be the same as upperdir */
8545820dc08SAmir Goldstein 	if (workdir == upperdir)
8555820dc08SAmir Goldstein 		goto err;
8565820dc08SAmir Goldstein 
8575820dc08SAmir Goldstein 	/* Workdir should not be subdir of upperdir and vice versa */
8585820dc08SAmir Goldstein 	if (lock_rename(workdir, upperdir) != NULL)
8595820dc08SAmir Goldstein 		goto err_unlock;
8605820dc08SAmir Goldstein 
8615820dc08SAmir Goldstein 	return 0;
8625820dc08SAmir Goldstein 
8635820dc08SAmir Goldstein err_unlock:
8645820dc08SAmir Goldstein 	unlock_rename(workdir, upperdir);
8655820dc08SAmir Goldstein err:
8661bd0a3aeSlijiazi 	pr_err("failed to lock workdir+upperdir\n");
8675820dc08SAmir Goldstein 	return -EIO;
8685820dc08SAmir Goldstein }
8699d3dfea3SVivek Goyal 
8709d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
871610afc0bSMiklos Szeredi int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry)
8729d3dfea3SVivek Goyal {
8739d3dfea3SVivek Goyal 	int res;
8749d3dfea3SVivek Goyal 
8759d3dfea3SVivek Goyal 	/* Only regular files can have metacopy xattr */
8769d3dfea3SVivek Goyal 	if (!S_ISREG(d_inode(dentry)->i_mode))
8779d3dfea3SVivek Goyal 		return 0;
8789d3dfea3SVivek Goyal 
879610afc0bSMiklos Szeredi 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_METACOPY, NULL, 0);
8809d3dfea3SVivek Goyal 	if (res < 0) {
8819d3dfea3SVivek Goyal 		if (res == -ENODATA || res == -EOPNOTSUPP)
8829d3dfea3SVivek Goyal 			return 0;
8839d3dfea3SVivek Goyal 		goto out;
8849d3dfea3SVivek Goyal 	}
8859d3dfea3SVivek Goyal 
8869d3dfea3SVivek Goyal 	return 1;
8879d3dfea3SVivek Goyal out:
8881bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
8899d3dfea3SVivek Goyal 	return res;
8909d3dfea3SVivek Goyal }
89167d756c2SVivek Goyal 
89267d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry)
89367d756c2SVivek Goyal {
89467d756c2SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
89567d756c2SVivek Goyal 
89667d756c2SVivek Goyal 	if (!d_is_reg(dentry))
89767d756c2SVivek Goyal 		return false;
89867d756c2SVivek Goyal 
89967d756c2SVivek Goyal 	if (ovl_dentry_upper(dentry)) {
90067d756c2SVivek Goyal 		if (!ovl_has_upperdata(d_inode(dentry)))
90167d756c2SVivek Goyal 			return true;
90267d756c2SVivek Goyal 		return false;
90367d756c2SVivek Goyal 	}
90467d756c2SVivek Goyal 
90567d756c2SVivek Goyal 	return (oe->numlower > 1);
90667d756c2SVivek Goyal }
9070a2d0d3fSVivek Goyal 
908610afc0bSMiklos Szeredi char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
909610afc0bSMiklos Szeredi 			     int padding)
9100a2d0d3fSVivek Goyal {
9110a2d0d3fSVivek Goyal 	int res;
9120a2d0d3fSVivek Goyal 	char *s, *next, *buf = NULL;
9130a2d0d3fSVivek Goyal 
914610afc0bSMiklos Szeredi 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, NULL, 0);
91592f0d6c9SMiklos Szeredi 	if (res == -ENODATA || res == -EOPNOTSUPP)
9160a2d0d3fSVivek Goyal 		return NULL;
9170a2d0d3fSVivek Goyal 	if (res < 0)
91892f0d6c9SMiklos Szeredi 		goto fail;
91992f0d6c9SMiklos Szeredi 	if (res == 0)
92092f0d6c9SMiklos Szeredi 		goto invalid;
92192f0d6c9SMiklos Szeredi 
92292f0d6c9SMiklos Szeredi 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
92392f0d6c9SMiklos Szeredi 	if (!buf)
92492f0d6c9SMiklos Szeredi 		return ERR_PTR(-ENOMEM);
92592f0d6c9SMiklos Szeredi 
926610afc0bSMiklos Szeredi 	res = ovl_do_getxattr(ofs, dentry, OVL_XATTR_REDIRECT, buf, res);
92792f0d6c9SMiklos Szeredi 	if (res < 0)
92892f0d6c9SMiklos Szeredi 		goto fail;
9290a2d0d3fSVivek Goyal 	if (res == 0)
9300a2d0d3fSVivek Goyal 		goto invalid;
9310a2d0d3fSVivek Goyal 
9320a2d0d3fSVivek Goyal 	if (buf[0] == '/') {
9330a2d0d3fSVivek Goyal 		for (s = buf; *s++ == '/'; s = next) {
9340a2d0d3fSVivek Goyal 			next = strchrnul(s, '/');
9350a2d0d3fSVivek Goyal 			if (s == next)
9360a2d0d3fSVivek Goyal 				goto invalid;
9370a2d0d3fSVivek Goyal 		}
9380a2d0d3fSVivek Goyal 	} else {
9390a2d0d3fSVivek Goyal 		if (strchr(buf, '/') != NULL)
9400a2d0d3fSVivek Goyal 			goto invalid;
9410a2d0d3fSVivek Goyal 	}
9420a2d0d3fSVivek Goyal 
9430a2d0d3fSVivek Goyal 	return buf;
9440a2d0d3fSVivek Goyal invalid:
9451bd0a3aeSlijiazi 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
9460a2d0d3fSVivek Goyal 	res = -EINVAL;
94792f0d6c9SMiklos Szeredi 	goto err_free;
94892f0d6c9SMiklos Szeredi fail:
94992f0d6c9SMiklos Szeredi 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
95092f0d6c9SMiklos Szeredi err_free:
951993a0b2aSVivek Goyal 	kfree(buf);
952993a0b2aSVivek Goyal 	return ERR_PTR(res);
9530a2d0d3fSVivek Goyal }
954