xref: /openbmc/linux/fs/overlayfs/util.c (revision 2878dffc)
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>
13096a218aSAmir Goldstein #include <linux/fileattr.h>
1402bcd157SAmir Goldstein #include <linux/uuid.h>
15caf70cb2SAmir Goldstein #include <linux/namei.h>
16caf70cb2SAmir Goldstein #include <linux/ratelimit.h>
17bbb1e54dSMiklos Szeredi #include "overlayfs.h"
18bbb1e54dSMiklos Szeredi 
19bbb1e54dSMiklos Szeredi int ovl_want_write(struct dentry *dentry)
20bbb1e54dSMiklos Szeredi {
21bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2208f4c7c8SMiklos Szeredi 	return mnt_want_write(ovl_upper_mnt(ofs));
23bbb1e54dSMiklos Szeredi }
24bbb1e54dSMiklos Szeredi 
25bbb1e54dSMiklos Szeredi void ovl_drop_write(struct dentry *dentry)
26bbb1e54dSMiklos Szeredi {
27bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2808f4c7c8SMiklos Szeredi 	mnt_drop_write(ovl_upper_mnt(ofs));
29bbb1e54dSMiklos Szeredi }
30bbb1e54dSMiklos Szeredi 
31bbb1e54dSMiklos Szeredi struct dentry *ovl_workdir(struct dentry *dentry)
32bbb1e54dSMiklos Szeredi {
33bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
34bbb1e54dSMiklos Szeredi 	return ofs->workdir;
35bbb1e54dSMiklos Szeredi }
36bbb1e54dSMiklos Szeredi 
37bbb1e54dSMiklos Szeredi const struct cred *ovl_override_creds(struct super_block *sb)
38bbb1e54dSMiklos Szeredi {
39bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
40bbb1e54dSMiklos Szeredi 
41bbb1e54dSMiklos Szeredi 	return override_creds(ofs->creator_cred);
42bbb1e54dSMiklos Szeredi }
43bbb1e54dSMiklos Szeredi 
44e487d889SAmir Goldstein /*
45e487d889SAmir Goldstein  * Check if underlying fs supports file handles and try to determine encoding
46e487d889SAmir Goldstein  * type, in order to deduce maximum inode number used by fs.
47e487d889SAmir Goldstein  *
48e487d889SAmir Goldstein  * Return 0 if file handles are not supported.
49e487d889SAmir Goldstein  * Return 1 (FILEID_INO32_GEN) if fs uses the default 32bit inode encoding.
50e487d889SAmir Goldstein  * Return -1 if fs uses a non default encoding with unknown inode size.
51e487d889SAmir Goldstein  */
52e487d889SAmir Goldstein int ovl_can_decode_fh(struct super_block *sb)
5302bcd157SAmir Goldstein {
54c846af05SMiklos Szeredi 	if (!capable(CAP_DAC_READ_SEARCH))
55c846af05SMiklos Szeredi 		return 0;
56c846af05SMiklos Szeredi 
579df085f3SAmir Goldstein 	if (!sb->s_export_op || !sb->s_export_op->fh_to_dentry)
58e487d889SAmir Goldstein 		return 0;
59e487d889SAmir Goldstein 
60e487d889SAmir Goldstein 	return sb->s_export_op->encode_fh ? -1 : FILEID_INO32_GEN;
6102bcd157SAmir Goldstein }
6202bcd157SAmir Goldstein 
6302bcd157SAmir Goldstein struct dentry *ovl_indexdir(struct super_block *sb)
6402bcd157SAmir Goldstein {
6502bcd157SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
6602bcd157SAmir Goldstein 
6702bcd157SAmir Goldstein 	return ofs->indexdir;
6802bcd157SAmir Goldstein }
6902bcd157SAmir Goldstein 
70f168f109SAmir Goldstein /* Index all files on copy up. For now only enabled for NFS export */
71f168f109SAmir Goldstein bool ovl_index_all(struct super_block *sb)
72f168f109SAmir Goldstein {
73f168f109SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
74f168f109SAmir Goldstein 
75f168f109SAmir Goldstein 	return ofs->config.nfs_export && ofs->config.index;
76f168f109SAmir Goldstein }
77f168f109SAmir Goldstein 
78f168f109SAmir Goldstein /* Verify lower origin on lookup. For now only enabled for NFS export */
79f168f109SAmir Goldstein bool ovl_verify_lower(struct super_block *sb)
80f168f109SAmir Goldstein {
81f168f109SAmir Goldstein 	struct ovl_fs *ofs = sb->s_fs_info;
82f168f109SAmir Goldstein 
83f168f109SAmir Goldstein 	return ofs->config.nfs_export && ofs->config.index;
84f168f109SAmir Goldstein }
85f168f109SAmir Goldstein 
86bbb1e54dSMiklos Szeredi struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
87bbb1e54dSMiklos Szeredi {
88bbb1e54dSMiklos Szeredi 	size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
89bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
90bbb1e54dSMiklos Szeredi 
91bbb1e54dSMiklos Szeredi 	if (oe)
92bbb1e54dSMiklos Szeredi 		oe->numlower = numlower;
93bbb1e54dSMiklos Szeredi 
94bbb1e54dSMiklos Szeredi 	return oe;
95bbb1e54dSMiklos Szeredi }
96bbb1e54dSMiklos Szeredi 
97bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry)
98bbb1e54dSMiklos Szeredi {
99bbb1e54dSMiklos Szeredi 	return dentry->d_flags &
1007925dad8SMiklos Szeredi 		(DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
101bbb1e54dSMiklos Szeredi }
102bbb1e54dSMiklos Szeredi 
103f4288844SMiklos Szeredi void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
104f4288844SMiklos Szeredi 			     unsigned int mask)
105f4288844SMiklos Szeredi {
106f4288844SMiklos Szeredi 	struct ovl_entry *oe = OVL_E(dentry);
107f4288844SMiklos Szeredi 	unsigned int i, flags = 0;
108f4288844SMiklos Szeredi 
109bccece1eSMiklos Szeredi 	if (upperdentry)
110bccece1eSMiklos Szeredi 		flags |= upperdentry->d_flags;
111f4288844SMiklos Szeredi 	for (i = 0; i < oe->numlower; i++)
112f4288844SMiklos Szeredi 		flags |= oe->lowerstack[i].dentry->d_flags;
113f4288844SMiklos Szeredi 
114f4288844SMiklos Szeredi 	spin_lock(&dentry->d_lock);
115f4288844SMiklos Szeredi 	dentry->d_flags &= ~mask;
116f4288844SMiklos Szeredi 	dentry->d_flags |= flags & mask;
117f4288844SMiklos Szeredi 	spin_unlock(&dentry->d_lock);
118f4288844SMiklos Szeredi }
119f4288844SMiklos Szeredi 
120bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry)
121bbb1e54dSMiklos Szeredi {
122bbb1e54dSMiklos Szeredi 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
123bbb1e54dSMiklos Szeredi 				  DCACHE_MANAGE_TRANSIT |
124bbb1e54dSMiklos Szeredi 				  DCACHE_OP_HASH |
125bbb1e54dSMiklos Szeredi 				  DCACHE_OP_COMPARE);
126bbb1e54dSMiklos Szeredi }
127bbb1e54dSMiklos Szeredi 
128bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry)
129bbb1e54dSMiklos Szeredi {
130bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
131bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = 0;
132bbb1e54dSMiklos Szeredi 
13309d8b586SMiklos Szeredi 	if (ovl_dentry_upper(dentry)) {
134bbb1e54dSMiklos Szeredi 		type = __OVL_PATH_UPPER;
135bbb1e54dSMiklos Szeredi 
136bbb1e54dSMiklos Szeredi 		/*
13759548503SAmir Goldstein 		 * Non-dir dentry can hold lower dentry of its copy up origin.
138bbb1e54dSMiklos Szeredi 		 */
13959548503SAmir Goldstein 		if (oe->numlower) {
14060124877SVivek Goyal 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
14159548503SAmir Goldstein 				type |= __OVL_PATH_ORIGIN;
1420b17c28aSVivek Goyal 			if (d_is_dir(dentry) ||
1430b17c28aSVivek Goyal 			    !ovl_has_upperdata(d_inode(dentry)))
144bbb1e54dSMiklos Szeredi 				type |= __OVL_PATH_MERGE;
14559548503SAmir Goldstein 		}
146bbb1e54dSMiklos Szeredi 	} else {
147bbb1e54dSMiklos Szeredi 		if (oe->numlower > 1)
148bbb1e54dSMiklos Szeredi 			type |= __OVL_PATH_MERGE;
149bbb1e54dSMiklos Szeredi 	}
150bbb1e54dSMiklos Szeredi 	return type;
151bbb1e54dSMiklos Szeredi }
152bbb1e54dSMiklos Szeredi 
153bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path)
154bbb1e54dSMiklos Szeredi {
155bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
156bbb1e54dSMiklos Szeredi 
15708f4c7c8SMiklos Szeredi 	path->mnt = ovl_upper_mnt(ofs);
15809d8b586SMiklos Szeredi 	path->dentry = ovl_dentry_upper(dentry);
159bbb1e54dSMiklos Szeredi }
160bbb1e54dSMiklos Szeredi 
161bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path)
162bbb1e54dSMiklos Szeredi {
163bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
164bbb1e54dSMiklos Szeredi 
165b9343632SChandan Rajendra 	if (oe->numlower) {
166b9343632SChandan Rajendra 		path->mnt = oe->lowerstack[0].layer->mnt;
167b9343632SChandan Rajendra 		path->dentry = oe->lowerstack[0].dentry;
168b9343632SChandan Rajendra 	} else {
169b9343632SChandan Rajendra 		*path = (struct path) { };
170b9343632SChandan Rajendra 	}
171bbb1e54dSMiklos Szeredi }
172bbb1e54dSMiklos Szeredi 
1734f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
1744f93b426SVivek Goyal {
1754f93b426SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
1764f93b426SVivek Goyal 
1774f93b426SVivek Goyal 	if (oe->numlower) {
1784f93b426SVivek Goyal 		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
1794f93b426SVivek Goyal 		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
1804f93b426SVivek Goyal 	} else {
1814f93b426SVivek Goyal 		*path = (struct path) { };
1824f93b426SVivek Goyal 	}
1834f93b426SVivek Goyal }
1844f93b426SVivek Goyal 
185bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
186bbb1e54dSMiklos Szeredi {
187bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = ovl_path_type(dentry);
188bbb1e54dSMiklos Szeredi 
189bbb1e54dSMiklos Szeredi 	if (!OVL_TYPE_UPPER(type))
190bbb1e54dSMiklos Szeredi 		ovl_path_lower(dentry, path);
191bbb1e54dSMiklos Szeredi 	else
192bbb1e54dSMiklos Szeredi 		ovl_path_upper(dentry, path);
193bbb1e54dSMiklos Szeredi 
194bbb1e54dSMiklos Szeredi 	return type;
195bbb1e54dSMiklos Szeredi }
196bbb1e54dSMiklos Szeredi 
1971248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
1981248ea4bSAmir Goldstein {
1991248ea4bSAmir Goldstein 	enum ovl_path_type type = ovl_path_type(dentry);
2001248ea4bSAmir Goldstein 
2011248ea4bSAmir Goldstein 	WARN_ON_ONCE(d_is_dir(dentry));
2021248ea4bSAmir Goldstein 
2031248ea4bSAmir Goldstein 	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
2041248ea4bSAmir Goldstein 		ovl_path_lowerdata(dentry, path);
2051248ea4bSAmir Goldstein 	else
2061248ea4bSAmir Goldstein 		ovl_path_upper(dentry, path);
2071248ea4bSAmir Goldstein 
2081248ea4bSAmir Goldstein 	return type;
2091248ea4bSAmir Goldstein }
2101248ea4bSAmir Goldstein 
211bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry)
212bbb1e54dSMiklos Szeredi {
21309d8b586SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
214bbb1e54dSMiklos Szeredi }
215bbb1e54dSMiklos Szeredi 
216bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry)
217bbb1e54dSMiklos Szeredi {
218bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
219bbb1e54dSMiklos Szeredi 
22009d8b586SMiklos Szeredi 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
221bbb1e54dSMiklos Szeredi }
222bbb1e54dSMiklos Szeredi 
22313464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
224da309e8cSAmir Goldstein {
225da309e8cSAmir Goldstein 	struct ovl_entry *oe = dentry->d_fsdata;
226da309e8cSAmir Goldstein 
227da309e8cSAmir Goldstein 	return oe->numlower ? oe->lowerstack[0].layer : NULL;
228da309e8cSAmir Goldstein }
229da309e8cSAmir Goldstein 
230647d253fSVivek Goyal /*
231647d253fSVivek Goyal  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
232597534e7SXiong Zhenwu  * depending on what is stored in lowerstack[0]. At times we need to find
233647d253fSVivek Goyal  * lower dentry which has data (and not metacopy dentry). This helper
234647d253fSVivek Goyal  * returns the lower data dentry.
235647d253fSVivek Goyal  */
236647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
237647d253fSVivek Goyal {
238647d253fSVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
239647d253fSVivek Goyal 
240647d253fSVivek Goyal 	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
241647d253fSVivek Goyal }
242647d253fSVivek Goyal 
243bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry)
244bbb1e54dSMiklos Szeredi {
24509d8b586SMiklos Szeredi 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
246bbb1e54dSMiklos Szeredi }
247bbb1e54dSMiklos Szeredi 
2481d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode)
2491d88f183SMiklos Szeredi {
2501d88f183SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(inode));
2511d88f183SMiklos Szeredi }
2521d88f183SMiklos Szeredi 
253ffa5723cSAmir Goldstein void ovl_i_path_real(struct inode *inode, struct path *path)
254ffa5723cSAmir Goldstein {
255ffa5723cSAmir Goldstein 	path->dentry = ovl_i_dentry_upper(inode);
256ffa5723cSAmir Goldstein 	if (!path->dentry) {
257ffa5723cSAmir Goldstein 		path->dentry = OVL_I(inode)->lowerpath.dentry;
258ffa5723cSAmir Goldstein 		path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
259ffa5723cSAmir Goldstein 	} else {
260ffa5723cSAmir Goldstein 		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
261ffa5723cSAmir Goldstein 	}
262ffa5723cSAmir Goldstein }
263ffa5723cSAmir Goldstein 
26409d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode)
26525b7713aSMiklos Szeredi {
2661d88f183SMiklos Szeredi 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
26725b7713aSMiklos Szeredi 
26809d8b586SMiklos Szeredi 	return upperdentry ? d_inode(upperdentry) : NULL;
26925b7713aSMiklos Szeredi }
27025b7713aSMiklos Szeredi 
27109d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode)
27209d8b586SMiklos Szeredi {
273ffa5723cSAmir Goldstein 	struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
274ffa5723cSAmir Goldstein 
275ffa5723cSAmir Goldstein 	return lowerdentry ? d_inode(lowerdentry) : NULL;
27609d8b586SMiklos Szeredi }
27709d8b586SMiklos Szeredi 
27809d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode)
27909d8b586SMiklos Szeredi {
28009d8b586SMiklos Szeredi 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
28109d8b586SMiklos Szeredi }
28209d8b586SMiklos Szeredi 
2832664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */
2842664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode)
2852664bd08SVivek Goyal {
2862664bd08SVivek Goyal 	if (WARN_ON(!S_ISREG(inode->i_mode)))
2872664bd08SVivek Goyal 		return NULL;
2882664bd08SVivek Goyal 
2892664bd08SVivek Goyal 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
2902664bd08SVivek Goyal }
29109d8b586SMiklos Szeredi 
2924823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */
2934823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode)
2944823d49cSVivek Goyal {
2954823d49cSVivek Goyal 	struct inode *upperinode;
2964823d49cSVivek Goyal 
2974823d49cSVivek Goyal 	upperinode = ovl_inode_upper(inode);
2984823d49cSVivek Goyal 	if (upperinode && ovl_has_upperdata(inode))
2994823d49cSVivek Goyal 		return upperinode;
3004823d49cSVivek Goyal 
3014823d49cSVivek Goyal 	return ovl_inode_lowerdata(inode);
3024823d49cSVivek Goyal }
3034823d49cSVivek Goyal 
3044edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
305bbb1e54dSMiklos Szeredi {
3064edb83bbSMiklos Szeredi 	return OVL_I(inode)->cache;
307bbb1e54dSMiklos Szeredi }
308bbb1e54dSMiklos Szeredi 
3094edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
310bbb1e54dSMiklos Szeredi {
3114edb83bbSMiklos Szeredi 	OVL_I(inode)->cache = cache;
312bbb1e54dSMiklos Szeredi }
313bbb1e54dSMiklos Szeredi 
314c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
315c62520a8SAmir Goldstein {
316c62520a8SAmir Goldstein 	set_bit(flag, &OVL_E(dentry)->flags);
317c62520a8SAmir Goldstein }
318c62520a8SAmir Goldstein 
319c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
320c62520a8SAmir Goldstein {
321c62520a8SAmir Goldstein 	clear_bit(flag, &OVL_E(dentry)->flags);
322c62520a8SAmir Goldstein }
323c62520a8SAmir Goldstein 
324c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
325c62520a8SAmir Goldstein {
326c62520a8SAmir Goldstein 	return test_bit(flag, &OVL_E(dentry)->flags);
327c62520a8SAmir Goldstein }
328c62520a8SAmir Goldstein 
329bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry)
330bbb1e54dSMiklos Szeredi {
331c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
332bbb1e54dSMiklos Szeredi }
333bbb1e54dSMiklos Szeredi 
334bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry)
335bbb1e54dSMiklos Szeredi {
336bbb1e54dSMiklos Szeredi 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
337bbb1e54dSMiklos Szeredi }
338bbb1e54dSMiklos Szeredi 
3395cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry)
340bbb1e54dSMiklos Szeredi {
341c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
342bbb1e54dSMiklos Szeredi }
343bbb1e54dSMiklos Szeredi 
34455acc661SMiklos Szeredi /*
345aa3ff3c1SAmir Goldstein  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
346aa3ff3c1SAmir Goldstein  * to return positive, while there's no actual upper alias for the inode.
347aa3ff3c1SAmir Goldstein  * Copy up code needs to know about the existence of the upper alias, so it
348aa3ff3c1SAmir Goldstein  * can't use ovl_dentry_upper().
34955acc661SMiklos Szeredi  */
35055acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry)
35155acc661SMiklos Szeredi {
352c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
35355acc661SMiklos Szeredi }
35455acc661SMiklos Szeredi 
35555acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry)
35655acc661SMiklos Szeredi {
357c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
35855acc661SMiklos Szeredi }
35955acc661SMiklos Szeredi 
3600c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode)
3610c288874SVivek Goyal {
3620c288874SVivek Goyal 	if (!S_ISREG(inode->i_mode))
3630c288874SVivek Goyal 		return false;
3640c288874SVivek Goyal 
3650c288874SVivek Goyal 	if (!ovl_inode_lower(inode))
3660c288874SVivek Goyal 		return false;
3670c288874SVivek Goyal 
3680c288874SVivek Goyal 	return true;
3690c288874SVivek Goyal }
3700c288874SVivek Goyal 
3710c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode)
3720c288874SVivek Goyal {
3730c288874SVivek Goyal 	if (!ovl_should_check_upperdata(inode))
3740c288874SVivek Goyal 		return true;
3750c288874SVivek Goyal 
3760c288874SVivek Goyal 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
3770c288874SVivek Goyal 		return false;
3780c288874SVivek Goyal 	/*
3790c288874SVivek Goyal 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
3800c288874SVivek Goyal 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
3810c288874SVivek Goyal 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
3820c288874SVivek Goyal 	 * before that are visible too.
3830c288874SVivek Goyal 	 */
3840c288874SVivek Goyal 	smp_rmb();
3850c288874SVivek Goyal 	return true;
3860c288874SVivek Goyal }
3870c288874SVivek Goyal 
3880c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode)
3890c288874SVivek Goyal {
3900c288874SVivek Goyal 	/*
3910c288874SVivek Goyal 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
3920c288874SVivek Goyal 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
3930c288874SVivek Goyal 	 * before it are visible as well.
3940c288874SVivek Goyal 	 */
3950c288874SVivek Goyal 	smp_wmb();
3960c288874SVivek Goyal 	ovl_set_flag(OVL_UPPERDATA, inode);
3970c288874SVivek Goyal }
3980c288874SVivek Goyal 
3990c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
4000c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
4010c288874SVivek Goyal {
4020c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4030c288874SVivek Goyal 		return false;
4040c288874SVivek Goyal 
4050c288874SVivek Goyal 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
4060c288874SVivek Goyal }
4070c288874SVivek Goyal 
4080c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
4090c288874SVivek Goyal {
4100c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4110c288874SVivek Goyal 		return false;
4120c288874SVivek Goyal 
4130c288874SVivek Goyal 	return !ovl_has_upperdata(d_inode(dentry));
4140c288874SVivek Goyal }
4150c288874SVivek Goyal 
416a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb)
417a6c60655SMiklos Szeredi {
418a6c60655SMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
419a6c60655SMiklos Szeredi 
42021a22878SAmir Goldstein 	return ofs->config.redirect_dir && !ofs->noxattr;
421a6c60655SMiklos Szeredi }
422a6c60655SMiklos Szeredi 
423a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry)
424a6c60655SMiklos Szeredi {
425cf31c463SMiklos Szeredi 	return OVL_I(d_inode(dentry))->redirect;
426a6c60655SMiklos Szeredi }
427a6c60655SMiklos Szeredi 
428a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
429a6c60655SMiklos Szeredi {
430cf31c463SMiklos Szeredi 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
431a6c60655SMiklos Szeredi 
432cf31c463SMiklos Szeredi 	kfree(oi->redirect);
433cf31c463SMiklos Szeredi 	oi->redirect = redirect;
434a6c60655SMiklos Szeredi }
435a6c60655SMiklos Szeredi 
43609d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
437bbb1e54dSMiklos Szeredi {
43809d8b586SMiklos Szeredi 	struct inode *upperinode = d_inode(upperdentry);
439e6d2ebddSMiklos Szeredi 
44009d8b586SMiklos Szeredi 	WARN_ON(OVL_I(inode)->__upperdentry);
44109d8b586SMiklos Szeredi 
44225b7713aSMiklos Szeredi 	/*
44309d8b586SMiklos Szeredi 	 * Make sure upperdentry is consistent before making it visible
44425b7713aSMiklos Szeredi 	 */
44525b7713aSMiklos Szeredi 	smp_wmb();
44609d8b586SMiklos Szeredi 	OVL_I(inode)->__upperdentry = upperdentry;
44731747edaSAmir Goldstein 	if (inode_unhashed(inode)) {
44825b7713aSMiklos Szeredi 		inode->i_private = upperinode;
449bbb1e54dSMiklos Szeredi 		__insert_inode_hash(inode, (unsigned long) upperinode);
450bbb1e54dSMiklos Szeredi 	}
45125b7713aSMiklos Szeredi }
452bbb1e54dSMiklos Szeredi 
45365cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
454bbb1e54dSMiklos Szeredi {
45504a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
456bbb1e54dSMiklos Szeredi 
45704a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
45865cd913eSAmir Goldstein 	WARN_ON(!d_is_dir(dentry));
4594edb83bbSMiklos Szeredi 	/*
46065cd913eSAmir Goldstein 	 * Version is used by readdir code to keep cache consistent.
46165cd913eSAmir Goldstein 	 * For merge dirs (or dirs with origin) all changes need to be noted.
46265cd913eSAmir Goldstein 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
46365cd913eSAmir Goldstein 	 * which have been copied up and have origins), so only need to note
46465cd913eSAmir Goldstein 	 * changes to impure entries.
4654edb83bbSMiklos Szeredi 	 */
46665cd913eSAmir Goldstein 	if (!ovl_dir_is_real(dentry) || impurity)
46704a01ac7SMiklos Szeredi 		OVL_I(inode)->version++;
468bbb1e54dSMiklos Szeredi }
469bbb1e54dSMiklos Szeredi 
470d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity)
471d9854c87SMiklos Szeredi {
472d9854c87SMiklos Szeredi 	/* Copy mtime/ctime */
473*2878dffcSChristian Brauner 	ovl_copyattr(d_inode(dentry));
474d9854c87SMiklos Szeredi 
47565cd913eSAmir Goldstein 	ovl_dir_version_inc(dentry, impurity);
476d9854c87SMiklos Szeredi }
477d9854c87SMiklos Szeredi 
478bbb1e54dSMiklos Szeredi u64 ovl_dentry_version_get(struct dentry *dentry)
479bbb1e54dSMiklos Szeredi {
48004a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
481bbb1e54dSMiklos Szeredi 
48204a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
48304a01ac7SMiklos Szeredi 	return OVL_I(inode)->version;
484bbb1e54dSMiklos Szeredi }
485bbb1e54dSMiklos Szeredi 
486bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry)
487bbb1e54dSMiklos Szeredi {
488bbb1e54dSMiklos Szeredi 	struct inode *inode = dentry->d_inode;
489bbb1e54dSMiklos Szeredi 
490bbb1e54dSMiklos Szeredi 	return inode && IS_WHITEOUT(inode);
491bbb1e54dSMiklos Szeredi }
492bbb1e54dSMiklos Szeredi 
493bbb1e54dSMiklos Szeredi struct file *ovl_path_open(struct path *path, int flags)
494bbb1e54dSMiklos Szeredi {
49556230d95SMiklos Szeredi 	struct inode *inode = d_inode(path->dentry);
49656230d95SMiklos Szeredi 	int err, acc_mode;
49756230d95SMiklos Szeredi 
49856230d95SMiklos Szeredi 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
49956230d95SMiklos Szeredi 		BUG();
50056230d95SMiklos Szeredi 
50156230d95SMiklos Szeredi 	switch (flags & O_ACCMODE) {
50256230d95SMiklos Szeredi 	case O_RDONLY:
50356230d95SMiklos Szeredi 		acc_mode = MAY_READ;
50456230d95SMiklos Szeredi 		break;
50556230d95SMiklos Szeredi 	case O_WRONLY:
50656230d95SMiklos Szeredi 		acc_mode = MAY_WRITE;
50756230d95SMiklos Szeredi 		break;
50856230d95SMiklos Szeredi 	default:
50956230d95SMiklos Szeredi 		BUG();
51056230d95SMiklos Szeredi 	}
51156230d95SMiklos Szeredi 
51247291baaSChristian Brauner 	err = inode_permission(&init_user_ns, inode, acc_mode | MAY_OPEN);
51356230d95SMiklos Szeredi 	if (err)
51456230d95SMiklos Szeredi 		return ERR_PTR(err);
51556230d95SMiklos Szeredi 
51656230d95SMiklos Szeredi 	/* O_NOATIME is an optimization, don't fail if not permitted */
51721cb47beSChristian Brauner 	if (inode_owner_or_capable(&init_user_ns, inode))
51856230d95SMiklos Szeredi 		flags |= O_NOATIME;
51956230d95SMiklos Szeredi 
52056230d95SMiklos Szeredi 	return dentry_open(path, flags, current_cred());
521bbb1e54dSMiklos Szeredi }
52239d3d60aSAmir Goldstein 
5230c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
5240c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
5250c288874SVivek Goyal {
5260c288874SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5270c288874SVivek Goyal 
5280c288874SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5290c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5300c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
5310c288874SVivek Goyal 		return true;
5320c288874SVivek Goyal 
5330c288874SVivek Goyal 	return false;
5340c288874SVivek Goyal }
5350c288874SVivek Goyal 
5360c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags)
5372002df85SVivek Goyal {
5382002df85SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5392002df85SVivek Goyal 
5402002df85SVivek Goyal 	/*
5412002df85SVivek Goyal 	 * Check if copy-up has happened as well as for upper alias (in
5422002df85SVivek Goyal 	 * case of hard links) is there.
5432002df85SVivek Goyal 	 *
5442002df85SVivek Goyal 	 * Both checks are lockless:
5452002df85SVivek Goyal 	 *  - false negatives: will recheck under oi->lock
5462002df85SVivek Goyal 	 *  - false positives:
5472002df85SVivek Goyal 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
5482002df85SVivek Goyal 	 *      upper dentry is up-to-date
5492002df85SVivek Goyal 	 *    + ovl_dentry_has_upper_alias() relies on locking of
5502002df85SVivek Goyal 	 *      upper parent i_rwsem to prevent reordering copy-up
5512002df85SVivek Goyal 	 *      with rename.
5522002df85SVivek Goyal 	 */
5532002df85SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5540c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5550c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
5562002df85SVivek Goyal 		return true;
5572002df85SVivek Goyal 
5582002df85SVivek Goyal 	return false;
5592002df85SVivek Goyal }
5602002df85SVivek Goyal 
5610c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags)
56239d3d60aSAmir Goldstein {
5631e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
56439d3d60aSAmir Goldstein 	int err;
56539d3d60aSAmir Goldstein 
566531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
5670c288874SVivek Goyal 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
56839d3d60aSAmir Goldstein 		err = 1; /* Already copied up */
5691e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
57039d3d60aSAmir Goldstein 	}
57139d3d60aSAmir Goldstein 
57239d3d60aSAmir Goldstein 	return err;
57339d3d60aSAmir Goldstein }
57439d3d60aSAmir Goldstein 
57539d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry)
57639d3d60aSAmir Goldstein {
5771e92e307SAmir Goldstein 	ovl_inode_unlock(d_inode(dentry));
57839d3d60aSAmir Goldstein }
57982b749b2SAmir Goldstein 
580dad7017aSChristian Brauner bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, struct path *path)
581b79e05aaSAmir Goldstein {
582b79e05aaSAmir Goldstein 	int res;
583b79e05aaSAmir Goldstein 
584dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
585b79e05aaSAmir Goldstein 
586b79e05aaSAmir Goldstein 	/* Zero size value means "copied up but origin unknown" */
587b79e05aaSAmir Goldstein 	if (res >= 0)
588b79e05aaSAmir Goldstein 		return true;
589b79e05aaSAmir Goldstein 
590b79e05aaSAmir Goldstein 	return false;
591b79e05aaSAmir Goldstein }
592b79e05aaSAmir Goldstein 
593dad7017aSChristian Brauner bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, struct path *path,
59443d193f8SMiklos Szeredi 			       enum ovl_xattr ox)
595f3a15685SAmir Goldstein {
596f3a15685SAmir Goldstein 	int res;
597f3a15685SAmir Goldstein 	char val;
598f3a15685SAmir Goldstein 
599dad7017aSChristian Brauner 	if (!d_is_dir(path->dentry))
600f3a15685SAmir Goldstein 		return false;
601f3a15685SAmir Goldstein 
602dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
603f3a15685SAmir Goldstein 	if (res == 1 && val == 'y')
604f3a15685SAmir Goldstein 		return true;
605f3a15685SAmir Goldstein 
606f3a15685SAmir Goldstein 	return false;
607f3a15685SAmir Goldstein }
608f3a15685SAmir Goldstein 
60943d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
61043d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
61143d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
61243d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX	"impure"
61343d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX		"nlink"
61443d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX		"upper"
61543d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
616096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
61743d193f8SMiklos Szeredi 
61843d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \
6192d2f2d73SMiklos Szeredi 	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
6202d2f2d73SMiklos Szeredi 		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
62143d193f8SMiklos Szeredi 
6222d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = {
62343d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
62443d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
62543d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
62643d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
62743d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
62843d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
62943d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
630096a218aSAmir Goldstein 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
63143d193f8SMiklos Szeredi };
63243d193f8SMiklos Szeredi 
633a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
63443d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
63582b749b2SAmir Goldstein 		       int xerr)
63682b749b2SAmir Goldstein {
63782b749b2SAmir Goldstein 	int err;
63882b749b2SAmir Goldstein 
63982b749b2SAmir Goldstein 	if (ofs->noxattr)
64082b749b2SAmir Goldstein 		return xerr;
64182b749b2SAmir Goldstein 
642c914c0e2SAmir Goldstein 	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
64382b749b2SAmir Goldstein 
64482b749b2SAmir Goldstein 	if (err == -EOPNOTSUPP) {
64543d193f8SMiklos Szeredi 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
64682b749b2SAmir Goldstein 		ofs->noxattr = true;
64782b749b2SAmir Goldstein 		return xerr;
64882b749b2SAmir Goldstein 	}
64982b749b2SAmir Goldstein 
65082b749b2SAmir Goldstein 	return err;
65182b749b2SAmir Goldstein }
652f3a15685SAmir Goldstein 
653f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
654f3a15685SAmir Goldstein {
655a0c236b1SAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
656f3a15685SAmir Goldstein 	int err;
657f3a15685SAmir Goldstein 
65813c72075SMiklos Szeredi 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
659f3a15685SAmir Goldstein 		return 0;
660f3a15685SAmir Goldstein 
661f3a15685SAmir Goldstein 	/*
662f3a15685SAmir Goldstein 	 * Do not fail when upper doesn't support xattrs.
663f3a15685SAmir Goldstein 	 * Upper inodes won't have origin nor redirect xattr anyway.
664f3a15685SAmir Goldstein 	 */
665a0c236b1SAmir Goldstein 	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
666f3a15685SAmir Goldstein 	if (!err)
66713c72075SMiklos Szeredi 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
668f3a15685SAmir Goldstein 
669f3a15685SAmir Goldstein 	return err;
670f3a15685SAmir Goldstein }
67113c72075SMiklos Szeredi 
672096a218aSAmir Goldstein 
673096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
674096a218aSAmir Goldstein 
675096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper)
676096a218aSAmir Goldstein {
677096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
678096a218aSAmir Goldstein 	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
679096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX+1];
680096a218aSAmir Goldstein 	int res, n;
681096a218aSAmir Goldstein 
682dad7017aSChristian Brauner 	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
683096a218aSAmir Goldstein 				 OVL_PROTATTR_MAX);
684096a218aSAmir Goldstein 	if (res < 0)
685096a218aSAmir Goldstein 		return;
686096a218aSAmir Goldstein 
687096a218aSAmir Goldstein 	/*
688096a218aSAmir Goldstein 	 * Initialize inode flags from overlay.protattr xattr and upper inode
689096a218aSAmir Goldstein 	 * flags.  If upper inode has those fileattr flags set (i.e. from old
690096a218aSAmir Goldstein 	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
691096a218aSAmir Goldstein 	 * them on next fileattr_set().
692096a218aSAmir Goldstein 	 */
693096a218aSAmir Goldstein 	for (n = 0; n < res; n++) {
694096a218aSAmir Goldstein 		if (buf[n] == 'a')
695096a218aSAmir Goldstein 			iflags |= S_APPEND;
696096a218aSAmir Goldstein 		else if (buf[n] == 'i')
697096a218aSAmir Goldstein 			iflags |= S_IMMUTABLE;
698096a218aSAmir Goldstein 		else
699096a218aSAmir Goldstein 			break;
700096a218aSAmir Goldstein 	}
701096a218aSAmir Goldstein 
702096a218aSAmir Goldstein 	if (!res || n < res) {
703096a218aSAmir Goldstein 		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
704096a218aSAmir Goldstein 				    upper, res);
705096a218aSAmir Goldstein 	} else {
706096a218aSAmir Goldstein 		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
707096a218aSAmir Goldstein 	}
708096a218aSAmir Goldstein }
709096a218aSAmir Goldstein 
710096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper,
711096a218aSAmir Goldstein 		      struct fileattr *fa)
712096a218aSAmir Goldstein {
713096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
714096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX];
715096a218aSAmir Goldstein 	int len = 0, err = 0;
716096a218aSAmir Goldstein 	u32 iflags = 0;
717096a218aSAmir Goldstein 
718096a218aSAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
719096a218aSAmir Goldstein 
720096a218aSAmir Goldstein 	if (fa->flags & FS_APPEND_FL) {
721096a218aSAmir Goldstein 		buf[len++] = 'a';
722096a218aSAmir Goldstein 		iflags |= S_APPEND;
723096a218aSAmir Goldstein 	}
724096a218aSAmir Goldstein 	if (fa->flags & FS_IMMUTABLE_FL) {
725096a218aSAmir Goldstein 		buf[len++] = 'i';
726096a218aSAmir Goldstein 		iflags |= S_IMMUTABLE;
727096a218aSAmir Goldstein 	}
728096a218aSAmir Goldstein 
729096a218aSAmir Goldstein 	/*
730096a218aSAmir Goldstein 	 * Do not allow to set protection flags when upper doesn't support
731096a218aSAmir Goldstein 	 * xattrs, because we do not set those fileattr flags on upper inode.
732096a218aSAmir Goldstein 	 * Remove xattr if it exist and all protection flags are cleared.
733096a218aSAmir Goldstein 	 */
734096a218aSAmir Goldstein 	if (len) {
735096a218aSAmir Goldstein 		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
736096a218aSAmir Goldstein 					 buf, len, -EPERM);
737096a218aSAmir Goldstein 	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
738c914c0e2SAmir Goldstein 		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
739096a218aSAmir Goldstein 		if (err == -EOPNOTSUPP || err == -ENODATA)
740096a218aSAmir Goldstein 			err = 0;
741096a218aSAmir Goldstein 	}
742096a218aSAmir Goldstein 	if (err)
743096a218aSAmir Goldstein 		return err;
744096a218aSAmir Goldstein 
745096a218aSAmir Goldstein 	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
746096a218aSAmir Goldstein 
747096a218aSAmir Goldstein 	/* Mask out the fileattr flags that should not be set in upper inode */
748096a218aSAmir Goldstein 	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
749096a218aSAmir Goldstein 	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
750096a218aSAmir Goldstein 
751096a218aSAmir Goldstein 	return 0;
752096a218aSAmir Goldstein }
753096a218aSAmir Goldstein 
754ad0af710SAmir Goldstein /**
755ad0af710SAmir Goldstein  * Caller must hold a reference to inode to prevent it from being freed while
756ad0af710SAmir Goldstein  * it is marked inuse.
757ad0af710SAmir Goldstein  */
758ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry)
759ad0af710SAmir Goldstein {
760ad0af710SAmir Goldstein 	struct inode *inode = d_inode(dentry);
761ad0af710SAmir Goldstein 	bool locked = false;
762ad0af710SAmir Goldstein 
763ad0af710SAmir Goldstein 	spin_lock(&inode->i_lock);
764ad0af710SAmir Goldstein 	if (!(inode->i_state & I_OVL_INUSE)) {
765ad0af710SAmir Goldstein 		inode->i_state |= I_OVL_INUSE;
766ad0af710SAmir Goldstein 		locked = true;
767ad0af710SAmir Goldstein 	}
768ad0af710SAmir Goldstein 	spin_unlock(&inode->i_lock);
769ad0af710SAmir Goldstein 
770ad0af710SAmir Goldstein 	return locked;
771ad0af710SAmir Goldstein }
772ad0af710SAmir Goldstein 
773ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry)
774ad0af710SAmir Goldstein {
775ad0af710SAmir Goldstein 	if (dentry) {
776ad0af710SAmir Goldstein 		struct inode *inode = d_inode(dentry);
777ad0af710SAmir Goldstein 
778ad0af710SAmir Goldstein 		spin_lock(&inode->i_lock);
779ad0af710SAmir Goldstein 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
780ad0af710SAmir Goldstein 		inode->i_state &= ~I_OVL_INUSE;
781ad0af710SAmir Goldstein 		spin_unlock(&inode->i_lock);
782ad0af710SAmir Goldstein 	}
783ad0af710SAmir Goldstein }
7845f8415d6SAmir Goldstein 
785146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry)
786146d62e5SAmir Goldstein {
787146d62e5SAmir Goldstein 	struct inode *inode = d_inode(dentry);
788146d62e5SAmir Goldstein 	bool inuse;
789146d62e5SAmir Goldstein 
790146d62e5SAmir Goldstein 	spin_lock(&inode->i_lock);
791146d62e5SAmir Goldstein 	inuse = (inode->i_state & I_OVL_INUSE);
792146d62e5SAmir Goldstein 	spin_unlock(&inode->i_lock);
793146d62e5SAmir Goldstein 
794146d62e5SAmir Goldstein 	return inuse;
795146d62e5SAmir Goldstein }
796146d62e5SAmir Goldstein 
79724b33ee1SAmir Goldstein /*
79824b33ee1SAmir Goldstein  * Does this overlay dentry need to be indexed on copy up?
79924b33ee1SAmir Goldstein  */
80024b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry)
80124b33ee1SAmir Goldstein {
80224b33ee1SAmir Goldstein 	struct dentry *lower = ovl_dentry_lower(dentry);
80324b33ee1SAmir Goldstein 
80424b33ee1SAmir Goldstein 	if (!lower || !ovl_indexdir(dentry->d_sb))
80524b33ee1SAmir Goldstein 		return false;
80624b33ee1SAmir Goldstein 
807fbd2d207SAmir Goldstein 	/* Index all files for NFS export and consistency verification */
808016b720fSAmir Goldstein 	if (ovl_index_all(dentry->d_sb))
809fbd2d207SAmir Goldstein 		return true;
810fbd2d207SAmir Goldstein 
81124b33ee1SAmir Goldstein 	/* Index only lower hardlinks on copy up */
81224b33ee1SAmir Goldstein 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
81324b33ee1SAmir Goldstein 		return true;
81424b33ee1SAmir Goldstein 
81524b33ee1SAmir Goldstein 	return false;
81624b33ee1SAmir Goldstein }
81724b33ee1SAmir Goldstein 
8189f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */
819caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry)
820caf70cb2SAmir Goldstein {
8211cdb0cb6SPavel Tikhomirov 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
822e7dd0e71SAmir Goldstein 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
823e7dd0e71SAmir Goldstein 	struct inode *dir = indexdir->d_inode;
824caf70cb2SAmir Goldstein 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
825caf70cb2SAmir Goldstein 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
826caf70cb2SAmir Goldstein 	struct dentry *index = NULL;
827caf70cb2SAmir Goldstein 	struct inode *inode;
82863e13252SAmir Goldstein 	struct qstr name = { };
829caf70cb2SAmir Goldstein 	int err;
830caf70cb2SAmir Goldstein 
8311cdb0cb6SPavel Tikhomirov 	err = ovl_get_index_name(ofs, lowerdentry, &name);
832caf70cb2SAmir Goldstein 	if (err)
833caf70cb2SAmir Goldstein 		goto fail;
834caf70cb2SAmir Goldstein 
835caf70cb2SAmir Goldstein 	inode = d_inode(upperdentry);
83689a17556SAmir Goldstein 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
8371bd0a3aeSlijiazi 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
838caf70cb2SAmir Goldstein 				    upperdentry, inode->i_ino, inode->i_nlink);
839caf70cb2SAmir Goldstein 		/*
840caf70cb2SAmir Goldstein 		 * We either have a bug with persistent union nlink or a lower
841caf70cb2SAmir Goldstein 		 * hardlink was added while overlay is mounted. Adding a lower
842caf70cb2SAmir Goldstein 		 * hardlink and then unlinking all overlay hardlinks would drop
843caf70cb2SAmir Goldstein 		 * overlay nlink to zero before all upper inodes are unlinked.
844caf70cb2SAmir Goldstein 		 * As a safety measure, when that situation is detected, set
845caf70cb2SAmir Goldstein 		 * the overlay nlink to the index inode nlink minus one for the
846caf70cb2SAmir Goldstein 		 * index entry itself.
847caf70cb2SAmir Goldstein 		 */
848caf70cb2SAmir Goldstein 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
849caf70cb2SAmir Goldstein 		ovl_set_nlink_upper(dentry);
850caf70cb2SAmir Goldstein 		goto out;
851caf70cb2SAmir Goldstein 	}
852caf70cb2SAmir Goldstein 
853caf70cb2SAmir Goldstein 	inode_lock_nested(dir, I_MUTEX_PARENT);
85422f289ceSChristian Brauner 	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
855caf70cb2SAmir Goldstein 	err = PTR_ERR(index);
856e7dd0e71SAmir Goldstein 	if (IS_ERR(index)) {
8579f4ec904SAmir Goldstein 		index = NULL;
858e7dd0e71SAmir Goldstein 	} else if (ovl_index_all(dentry->d_sb)) {
859e7dd0e71SAmir Goldstein 		/* Whiteout orphan index to block future open by handle */
860c21c839bSChengguang Xu 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
861c21c839bSChengguang Xu 					       dir, index);
862e7dd0e71SAmir Goldstein 	} else {
863e7dd0e71SAmir Goldstein 		/* Cleanup orphan index entries */
864576bb263SChristian Brauner 		err = ovl_cleanup(ofs, dir, index);
865e7dd0e71SAmir Goldstein 	}
8669f4ec904SAmir Goldstein 
867caf70cb2SAmir Goldstein 	inode_unlock(dir);
868caf70cb2SAmir Goldstein 	if (err)
869caf70cb2SAmir Goldstein 		goto fail;
870caf70cb2SAmir Goldstein 
871caf70cb2SAmir Goldstein out:
87263e13252SAmir Goldstein 	kfree(name.name);
873caf70cb2SAmir Goldstein 	dput(index);
874caf70cb2SAmir Goldstein 	return;
875caf70cb2SAmir Goldstein 
876caf70cb2SAmir Goldstein fail:
8771bd0a3aeSlijiazi 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
878caf70cb2SAmir Goldstein 	goto out;
879caf70cb2SAmir Goldstein }
880caf70cb2SAmir Goldstein 
8815f8415d6SAmir Goldstein /*
8825f8415d6SAmir Goldstein  * Operations that change overlay inode and upper inode nlink need to be
8835f8415d6SAmir Goldstein  * synchronized with copy up for persistent nlink accounting.
8845f8415d6SAmir Goldstein  */
8850e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry)
8865f8415d6SAmir Goldstein {
8871e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
8885f8415d6SAmir Goldstein 	const struct cred *old_cred;
8895f8415d6SAmir Goldstein 	int err;
8905f8415d6SAmir Goldstein 
8911e92e307SAmir Goldstein 	if (WARN_ON(!inode))
8920e32992fSAmir Goldstein 		return -ENOENT;
8935f8415d6SAmir Goldstein 
8945f8415d6SAmir Goldstein 	/*
8955f8415d6SAmir Goldstein 	 * With inodes index is enabled, we store the union overlay nlink
89624b33ee1SAmir Goldstein 	 * in an xattr on the index inode. When whiting out an indexed lower,
8975f8415d6SAmir Goldstein 	 * we need to decrement the overlay persistent nlink, but before the
8985f8415d6SAmir Goldstein 	 * first copy up, we have no upper index inode to store the xattr.
8995f8415d6SAmir Goldstein 	 *
90024b33ee1SAmir Goldstein 	 * As a workaround, before whiteout/rename over an indexed lower,
9015f8415d6SAmir Goldstein 	 * copy up to create the upper index. Creating the upper index will
9025f8415d6SAmir Goldstein 	 * initialize the overlay nlink, so it could be dropped if unlink
9035f8415d6SAmir Goldstein 	 * or rename succeeds.
9045f8415d6SAmir Goldstein 	 *
9055f8415d6SAmir Goldstein 	 * TODO: implement metadata only index copy up when called with
9065f8415d6SAmir Goldstein 	 *       ovl_copy_up_flags(dentry, O_PATH).
9075f8415d6SAmir Goldstein 	 */
90824b33ee1SAmir Goldstein 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
9095f8415d6SAmir Goldstein 		err = ovl_copy_up(dentry);
9105f8415d6SAmir Goldstein 		if (err)
9115f8415d6SAmir Goldstein 			return err;
9125f8415d6SAmir Goldstein 	}
9135f8415d6SAmir Goldstein 
914531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
9155f8415d6SAmir Goldstein 	if (err)
9165f8415d6SAmir Goldstein 		return err;
9175f8415d6SAmir Goldstein 
9181e92e307SAmir Goldstein 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
9195f8415d6SAmir Goldstein 		goto out;
9205f8415d6SAmir Goldstein 
9215f8415d6SAmir Goldstein 	old_cred = ovl_override_creds(dentry->d_sb);
9225f8415d6SAmir Goldstein 	/*
9235f8415d6SAmir Goldstein 	 * The overlay inode nlink should be incremented/decremented IFF the
9245f8415d6SAmir Goldstein 	 * upper operation succeeds, along with nlink change of upper inode.
9255f8415d6SAmir Goldstein 	 * Therefore, before link/unlink/rename, we store the union nlink
9265f8415d6SAmir Goldstein 	 * value relative to the upper inode nlink in an upper inode xattr.
9275f8415d6SAmir Goldstein 	 */
9285f8415d6SAmir Goldstein 	err = ovl_set_nlink_upper(dentry);
9295f8415d6SAmir Goldstein 	revert_creds(old_cred);
9305f8415d6SAmir Goldstein 
9315f8415d6SAmir Goldstein out:
9325f8415d6SAmir Goldstein 	if (err)
9331e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
9345f8415d6SAmir Goldstein 
9355f8415d6SAmir Goldstein 	return err;
9365f8415d6SAmir Goldstein }
9375f8415d6SAmir Goldstein 
9380e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry)
9395f8415d6SAmir Goldstein {
9401e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
9411e92e307SAmir Goldstein 
9421e92e307SAmir Goldstein 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
943caf70cb2SAmir Goldstein 		const struct cred *old_cred;
944caf70cb2SAmir Goldstein 
945caf70cb2SAmir Goldstein 		old_cred = ovl_override_creds(dentry->d_sb);
946caf70cb2SAmir Goldstein 		ovl_cleanup_index(dentry);
947caf70cb2SAmir Goldstein 		revert_creds(old_cred);
948caf70cb2SAmir Goldstein 	}
949caf70cb2SAmir Goldstein 
9501e92e307SAmir Goldstein 	ovl_inode_unlock(inode);
9515f8415d6SAmir Goldstein }
9525820dc08SAmir Goldstein 
9535820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
9545820dc08SAmir Goldstein {
9555820dc08SAmir Goldstein 	/* Workdir should not be the same as upperdir */
9565820dc08SAmir Goldstein 	if (workdir == upperdir)
9575820dc08SAmir Goldstein 		goto err;
9585820dc08SAmir Goldstein 
9595820dc08SAmir Goldstein 	/* Workdir should not be subdir of upperdir and vice versa */
9605820dc08SAmir Goldstein 	if (lock_rename(workdir, upperdir) != NULL)
9615820dc08SAmir Goldstein 		goto err_unlock;
9625820dc08SAmir Goldstein 
9635820dc08SAmir Goldstein 	return 0;
9645820dc08SAmir Goldstein 
9655820dc08SAmir Goldstein err_unlock:
9665820dc08SAmir Goldstein 	unlock_rename(workdir, upperdir);
9675820dc08SAmir Goldstein err:
9681bd0a3aeSlijiazi 	pr_err("failed to lock workdir+upperdir\n");
9695820dc08SAmir Goldstein 	return -EIO;
9705820dc08SAmir Goldstein }
9719d3dfea3SVivek Goyal 
9729d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
973dad7017aSChristian Brauner int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct path *path)
9749d3dfea3SVivek Goyal {
9759d3dfea3SVivek Goyal 	int res;
9769d3dfea3SVivek Goyal 
9779d3dfea3SVivek Goyal 	/* Only regular files can have metacopy xattr */
978dad7017aSChristian Brauner 	if (!S_ISREG(d_inode(path->dentry)->i_mode))
9799d3dfea3SVivek Goyal 		return 0;
9809d3dfea3SVivek Goyal 
981dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
9829d3dfea3SVivek Goyal 	if (res < 0) {
9839d3dfea3SVivek Goyal 		if (res == -ENODATA || res == -EOPNOTSUPP)
9849d3dfea3SVivek Goyal 			return 0;
98587b2c60cSMiklos Szeredi 		/*
98687b2c60cSMiklos Szeredi 		 * getxattr on user.* may fail with EACCES in case there's no
98787b2c60cSMiklos Szeredi 		 * read permission on the inode.  Not much we can do, other than
98887b2c60cSMiklos Szeredi 		 * tell the caller that this is not a metacopy inode.
98987b2c60cSMiklos Szeredi 		 */
99087b2c60cSMiklos Szeredi 		if (ofs->config.userxattr && res == -EACCES)
99187b2c60cSMiklos Szeredi 			return 0;
9929d3dfea3SVivek Goyal 		goto out;
9939d3dfea3SVivek Goyal 	}
9949d3dfea3SVivek Goyal 
9959d3dfea3SVivek Goyal 	return 1;
9969d3dfea3SVivek Goyal out:
9971bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
9989d3dfea3SVivek Goyal 	return res;
9999d3dfea3SVivek Goyal }
100067d756c2SVivek Goyal 
100167d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry)
100267d756c2SVivek Goyal {
100367d756c2SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
100467d756c2SVivek Goyal 
100567d756c2SVivek Goyal 	if (!d_is_reg(dentry))
100667d756c2SVivek Goyal 		return false;
100767d756c2SVivek Goyal 
100867d756c2SVivek Goyal 	if (ovl_dentry_upper(dentry)) {
100967d756c2SVivek Goyal 		if (!ovl_has_upperdata(d_inode(dentry)))
101067d756c2SVivek Goyal 			return true;
101167d756c2SVivek Goyal 		return false;
101267d756c2SVivek Goyal 	}
101367d756c2SVivek Goyal 
101467d756c2SVivek Goyal 	return (oe->numlower > 1);
101567d756c2SVivek Goyal }
10160a2d0d3fSVivek Goyal 
1017dad7017aSChristian Brauner char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct path *path, int padding)
10180a2d0d3fSVivek Goyal {
10190a2d0d3fSVivek Goyal 	int res;
10200a2d0d3fSVivek Goyal 	char *s, *next, *buf = NULL;
10210a2d0d3fSVivek Goyal 
1022dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
102392f0d6c9SMiklos Szeredi 	if (res == -ENODATA || res == -EOPNOTSUPP)
10240a2d0d3fSVivek Goyal 		return NULL;
10250a2d0d3fSVivek Goyal 	if (res < 0)
102692f0d6c9SMiklos Szeredi 		goto fail;
102792f0d6c9SMiklos Szeredi 	if (res == 0)
102892f0d6c9SMiklos Szeredi 		goto invalid;
102992f0d6c9SMiklos Szeredi 
103092f0d6c9SMiklos Szeredi 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
103192f0d6c9SMiklos Szeredi 	if (!buf)
103292f0d6c9SMiklos Szeredi 		return ERR_PTR(-ENOMEM);
103392f0d6c9SMiklos Szeredi 
1034dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
103592f0d6c9SMiklos Szeredi 	if (res < 0)
103692f0d6c9SMiklos Szeredi 		goto fail;
10370a2d0d3fSVivek Goyal 	if (res == 0)
10380a2d0d3fSVivek Goyal 		goto invalid;
10390a2d0d3fSVivek Goyal 
10400a2d0d3fSVivek Goyal 	if (buf[0] == '/') {
10410a2d0d3fSVivek Goyal 		for (s = buf; *s++ == '/'; s = next) {
10420a2d0d3fSVivek Goyal 			next = strchrnul(s, '/');
10430a2d0d3fSVivek Goyal 			if (s == next)
10440a2d0d3fSVivek Goyal 				goto invalid;
10450a2d0d3fSVivek Goyal 		}
10460a2d0d3fSVivek Goyal 	} else {
10470a2d0d3fSVivek Goyal 		if (strchr(buf, '/') != NULL)
10480a2d0d3fSVivek Goyal 			goto invalid;
10490a2d0d3fSVivek Goyal 	}
10500a2d0d3fSVivek Goyal 
10510a2d0d3fSVivek Goyal 	return buf;
10520a2d0d3fSVivek Goyal invalid:
10531bd0a3aeSlijiazi 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
10540a2d0d3fSVivek Goyal 	res = -EINVAL;
105592f0d6c9SMiklos Szeredi 	goto err_free;
105692f0d6c9SMiklos Szeredi fail:
105792f0d6c9SMiklos Szeredi 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
105892f0d6c9SMiklos Szeredi err_free:
1059993a0b2aSVivek Goyal 	kfree(buf);
1060993a0b2aSVivek Goyal 	return ERR_PTR(res);
10610a2d0d3fSVivek Goyal }
1062335d3fc5SSargun Dhillon 
1063335d3fc5SSargun Dhillon /*
1064335d3fc5SSargun Dhillon  * ovl_sync_status() - Check fs sync status for volatile mounts
1065335d3fc5SSargun Dhillon  *
1066335d3fc5SSargun Dhillon  * Returns 1 if this is not a volatile mount and a real sync is required.
1067335d3fc5SSargun Dhillon  *
1068335d3fc5SSargun Dhillon  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1069335d3fc5SSargun Dhillon  * have occurred on the upperdir since the mount.
1070335d3fc5SSargun Dhillon  *
1071335d3fc5SSargun Dhillon  * Returns -errno if it is a volatile mount, and the error that occurred since
1072335d3fc5SSargun Dhillon  * the last mount. If the error code changes, it'll return the latest error
1073335d3fc5SSargun Dhillon  * code.
1074335d3fc5SSargun Dhillon  */
1075335d3fc5SSargun Dhillon 
1076335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs)
1077335d3fc5SSargun Dhillon {
1078335d3fc5SSargun Dhillon 	struct vfsmount *mnt;
1079335d3fc5SSargun Dhillon 
1080335d3fc5SSargun Dhillon 	if (ovl_should_sync(ofs))
1081335d3fc5SSargun Dhillon 		return 1;
1082335d3fc5SSargun Dhillon 
1083335d3fc5SSargun Dhillon 	mnt = ovl_upper_mnt(ofs);
1084335d3fc5SSargun Dhillon 	if (!mnt)
1085335d3fc5SSargun Dhillon 		return 0;
1086335d3fc5SSargun Dhillon 
1087335d3fc5SSargun Dhillon 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1088335d3fc5SSargun Dhillon }
1089*2878dffcSChristian Brauner 
1090*2878dffcSChristian Brauner /*
1091*2878dffcSChristian Brauner  * ovl_copyattr() - copy inode attributes from layer to ovl inode
1092*2878dffcSChristian Brauner  *
1093*2878dffcSChristian Brauner  * When overlay copies inode information from an upper or lower layer to the
1094*2878dffcSChristian Brauner  * relevant overlay inode it will apply the idmapping of the upper or lower
1095*2878dffcSChristian Brauner  * layer when doing so ensuring that the ovl inode ownership will correctly
1096*2878dffcSChristian Brauner  * reflect the ownership of the idmapped upper or lower layer. For example, an
1097*2878dffcSChristian Brauner  * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
1098*2878dffcSChristian Brauner  * map any lower or upper inode owned by id 1001 to id 1000. These mapping
1099*2878dffcSChristian Brauner  * helpers are nops when the relevant layer isn't idmapped.
1100*2878dffcSChristian Brauner  */
1101*2878dffcSChristian Brauner void ovl_copyattr(struct inode *inode)
1102*2878dffcSChristian Brauner {
1103*2878dffcSChristian Brauner 	struct path realpath;
1104*2878dffcSChristian Brauner 	struct inode *realinode;
1105*2878dffcSChristian Brauner 	struct user_namespace *real_mnt_userns;
1106*2878dffcSChristian Brauner 
1107*2878dffcSChristian Brauner 	ovl_i_path_real(inode, &realpath);
1108*2878dffcSChristian Brauner 	realinode = d_inode(realpath.dentry);
1109*2878dffcSChristian Brauner 	real_mnt_userns = mnt_user_ns(realpath.mnt);
1110*2878dffcSChristian Brauner 
1111*2878dffcSChristian Brauner 	inode->i_uid = i_uid_into_mnt(real_mnt_userns, realinode);
1112*2878dffcSChristian Brauner 	inode->i_gid = i_gid_into_mnt(real_mnt_userns, realinode);
1113*2878dffcSChristian Brauner 	inode->i_mode = realinode->i_mode;
1114*2878dffcSChristian Brauner 	inode->i_atime = realinode->i_atime;
1115*2878dffcSChristian Brauner 	inode->i_mtime = realinode->i_mtime;
1116*2878dffcSChristian Brauner 	inode->i_ctime = realinode->i_ctime;
1117*2878dffcSChristian Brauner 	i_size_write(inode, i_size_read(realinode));
1118*2878dffcSChristian Brauner }
1119