xref: /openbmc/linux/fs/overlayfs/util.c (revision b07d5cc9)
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 
97*b07d5cc9SAmir Goldstein #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
98*b07d5cc9SAmir Goldstein 
99bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry)
100bbb1e54dSMiklos Szeredi {
101*b07d5cc9SAmir Goldstein 	return dentry->d_flags & OVL_D_REVALIDATE;
102bbb1e54dSMiklos Szeredi }
103bbb1e54dSMiklos Szeredi 
104*b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
105*b07d5cc9SAmir Goldstein {
106*b07d5cc9SAmir Goldstein 	if (!ovl_dentry_remote(realdentry))
107*b07d5cc9SAmir Goldstein 		return;
108*b07d5cc9SAmir Goldstein 
109*b07d5cc9SAmir Goldstein 	spin_lock(&dentry->d_lock);
110*b07d5cc9SAmir Goldstein 	dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
111*b07d5cc9SAmir Goldstein 	spin_unlock(&dentry->d_lock);
112*b07d5cc9SAmir Goldstein }
113*b07d5cc9SAmir Goldstein 
114*b07d5cc9SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
115*b07d5cc9SAmir Goldstein {
116*b07d5cc9SAmir Goldstein 	return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
117*b07d5cc9SAmir Goldstein }
118*b07d5cc9SAmir Goldstein 
119*b07d5cc9SAmir Goldstein void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
120f4288844SMiklos Szeredi 			   unsigned int mask)
121f4288844SMiklos Szeredi {
122f4288844SMiklos Szeredi 	struct ovl_entry *oe = OVL_E(dentry);
123f4288844SMiklos Szeredi 	unsigned int i, flags = 0;
124f4288844SMiklos Szeredi 
125bccece1eSMiklos Szeredi 	if (upperdentry)
126bccece1eSMiklos Szeredi 		flags |= upperdentry->d_flags;
127f4288844SMiklos Szeredi 	for (i = 0; i < oe->numlower; i++)
128f4288844SMiklos Szeredi 		flags |= oe->lowerstack[i].dentry->d_flags;
129f4288844SMiklos Szeredi 
130f4288844SMiklos Szeredi 	spin_lock(&dentry->d_lock);
131f4288844SMiklos Szeredi 	dentry->d_flags &= ~mask;
132f4288844SMiklos Szeredi 	dentry->d_flags |= flags & mask;
133f4288844SMiklos Szeredi 	spin_unlock(&dentry->d_lock);
134f4288844SMiklos Szeredi }
135f4288844SMiklos Szeredi 
136bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry)
137bbb1e54dSMiklos Szeredi {
138bbb1e54dSMiklos Szeredi 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
139bbb1e54dSMiklos Szeredi 				  DCACHE_MANAGE_TRANSIT |
140bbb1e54dSMiklos Szeredi 				  DCACHE_OP_HASH |
141bbb1e54dSMiklos Szeredi 				  DCACHE_OP_COMPARE);
142bbb1e54dSMiklos Szeredi }
143bbb1e54dSMiklos Szeredi 
144bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry)
145bbb1e54dSMiklos Szeredi {
146bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
147bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = 0;
148bbb1e54dSMiklos Szeredi 
14909d8b586SMiklos Szeredi 	if (ovl_dentry_upper(dentry)) {
150bbb1e54dSMiklos Szeredi 		type = __OVL_PATH_UPPER;
151bbb1e54dSMiklos Szeredi 
152bbb1e54dSMiklos Szeredi 		/*
15359548503SAmir Goldstein 		 * Non-dir dentry can hold lower dentry of its copy up origin.
154bbb1e54dSMiklos Szeredi 		 */
15559548503SAmir Goldstein 		if (oe->numlower) {
15660124877SVivek Goyal 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
15759548503SAmir Goldstein 				type |= __OVL_PATH_ORIGIN;
1580b17c28aSVivek Goyal 			if (d_is_dir(dentry) ||
1590b17c28aSVivek Goyal 			    !ovl_has_upperdata(d_inode(dentry)))
160bbb1e54dSMiklos Szeredi 				type |= __OVL_PATH_MERGE;
16159548503SAmir Goldstein 		}
162bbb1e54dSMiklos Szeredi 	} else {
163bbb1e54dSMiklos Szeredi 		if (oe->numlower > 1)
164bbb1e54dSMiklos Szeredi 			type |= __OVL_PATH_MERGE;
165bbb1e54dSMiklos Szeredi 	}
166bbb1e54dSMiklos Szeredi 	return type;
167bbb1e54dSMiklos Szeredi }
168bbb1e54dSMiklos Szeredi 
169bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path)
170bbb1e54dSMiklos Szeredi {
171bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
172bbb1e54dSMiklos Szeredi 
17308f4c7c8SMiklos Szeredi 	path->mnt = ovl_upper_mnt(ofs);
17409d8b586SMiklos Szeredi 	path->dentry = ovl_dentry_upper(dentry);
175bbb1e54dSMiklos Szeredi }
176bbb1e54dSMiklos Szeredi 
177bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path)
178bbb1e54dSMiklos Szeredi {
179bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
180bbb1e54dSMiklos Szeredi 
181b9343632SChandan Rajendra 	if (oe->numlower) {
182b9343632SChandan Rajendra 		path->mnt = oe->lowerstack[0].layer->mnt;
183b9343632SChandan Rajendra 		path->dentry = oe->lowerstack[0].dentry;
184b9343632SChandan Rajendra 	} else {
185b9343632SChandan Rajendra 		*path = (struct path) { };
186b9343632SChandan Rajendra 	}
187bbb1e54dSMiklos Szeredi }
188bbb1e54dSMiklos Szeredi 
1894f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
1904f93b426SVivek Goyal {
1914f93b426SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
1924f93b426SVivek Goyal 
1934f93b426SVivek Goyal 	if (oe->numlower) {
1944f93b426SVivek Goyal 		path->mnt = oe->lowerstack[oe->numlower - 1].layer->mnt;
1954f93b426SVivek Goyal 		path->dentry = oe->lowerstack[oe->numlower - 1].dentry;
1964f93b426SVivek Goyal 	} else {
1974f93b426SVivek Goyal 		*path = (struct path) { };
1984f93b426SVivek Goyal 	}
1994f93b426SVivek Goyal }
2004f93b426SVivek Goyal 
201bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
202bbb1e54dSMiklos Szeredi {
203bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = ovl_path_type(dentry);
204bbb1e54dSMiklos Szeredi 
205bbb1e54dSMiklos Szeredi 	if (!OVL_TYPE_UPPER(type))
206bbb1e54dSMiklos Szeredi 		ovl_path_lower(dentry, path);
207bbb1e54dSMiklos Szeredi 	else
208bbb1e54dSMiklos Szeredi 		ovl_path_upper(dentry, path);
209bbb1e54dSMiklos Szeredi 
210bbb1e54dSMiklos Szeredi 	return type;
211bbb1e54dSMiklos Szeredi }
212bbb1e54dSMiklos Szeredi 
2131248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
2141248ea4bSAmir Goldstein {
2151248ea4bSAmir Goldstein 	enum ovl_path_type type = ovl_path_type(dentry);
2161248ea4bSAmir Goldstein 
2171248ea4bSAmir Goldstein 	WARN_ON_ONCE(d_is_dir(dentry));
2181248ea4bSAmir Goldstein 
2191248ea4bSAmir Goldstein 	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
2201248ea4bSAmir Goldstein 		ovl_path_lowerdata(dentry, path);
2211248ea4bSAmir Goldstein 	else
2221248ea4bSAmir Goldstein 		ovl_path_upper(dentry, path);
2231248ea4bSAmir Goldstein 
2241248ea4bSAmir Goldstein 	return type;
2251248ea4bSAmir Goldstein }
2261248ea4bSAmir Goldstein 
227bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry)
228bbb1e54dSMiklos Szeredi {
22909d8b586SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
230bbb1e54dSMiklos Szeredi }
231bbb1e54dSMiklos Szeredi 
232bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry)
233bbb1e54dSMiklos Szeredi {
234bbb1e54dSMiklos Szeredi 	struct ovl_entry *oe = dentry->d_fsdata;
235bbb1e54dSMiklos Szeredi 
23609d8b586SMiklos Szeredi 	return oe->numlower ? oe->lowerstack[0].dentry : NULL;
237bbb1e54dSMiklos Szeredi }
238bbb1e54dSMiklos Szeredi 
23913464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
240da309e8cSAmir Goldstein {
241da309e8cSAmir Goldstein 	struct ovl_entry *oe = dentry->d_fsdata;
242da309e8cSAmir Goldstein 
243da309e8cSAmir Goldstein 	return oe->numlower ? oe->lowerstack[0].layer : NULL;
244da309e8cSAmir Goldstein }
245da309e8cSAmir Goldstein 
246647d253fSVivek Goyal /*
247647d253fSVivek Goyal  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
248597534e7SXiong Zhenwu  * depending on what is stored in lowerstack[0]. At times we need to find
249647d253fSVivek Goyal  * lower dentry which has data (and not metacopy dentry). This helper
250647d253fSVivek Goyal  * returns the lower data dentry.
251647d253fSVivek Goyal  */
252647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
253647d253fSVivek Goyal {
254647d253fSVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
255647d253fSVivek Goyal 
256647d253fSVivek Goyal 	return oe->numlower ? oe->lowerstack[oe->numlower - 1].dentry : NULL;
257647d253fSVivek Goyal }
258647d253fSVivek Goyal 
259bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry)
260bbb1e54dSMiklos Szeredi {
26109d8b586SMiklos Szeredi 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
262bbb1e54dSMiklos Szeredi }
263bbb1e54dSMiklos Szeredi 
2641d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode)
2651d88f183SMiklos Szeredi {
2661d88f183SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(inode));
2671d88f183SMiklos Szeredi }
2681d88f183SMiklos Szeredi 
269b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
270ffa5723cSAmir Goldstein {
271ffa5723cSAmir Goldstein 	path->dentry = ovl_i_dentry_upper(inode);
272ffa5723cSAmir Goldstein 	if (!path->dentry) {
273ffa5723cSAmir Goldstein 		path->dentry = OVL_I(inode)->lowerpath.dentry;
274ffa5723cSAmir Goldstein 		path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
275ffa5723cSAmir Goldstein 	} else {
276ffa5723cSAmir Goldstein 		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
277ffa5723cSAmir Goldstein 	}
278b2dd05f1SZhihao Cheng 
279b2dd05f1SZhihao Cheng 	return path->dentry ? d_inode_rcu(path->dentry) : NULL;
280ffa5723cSAmir Goldstein }
281ffa5723cSAmir Goldstein 
28209d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode)
28325b7713aSMiklos Szeredi {
2841d88f183SMiklos Szeredi 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
28525b7713aSMiklos Szeredi 
28609d8b586SMiklos Szeredi 	return upperdentry ? d_inode(upperdentry) : NULL;
28725b7713aSMiklos Szeredi }
28825b7713aSMiklos Szeredi 
28909d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode)
29009d8b586SMiklos Szeredi {
291ffa5723cSAmir Goldstein 	struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
292ffa5723cSAmir Goldstein 
293ffa5723cSAmir Goldstein 	return lowerdentry ? d_inode(lowerdentry) : NULL;
29409d8b586SMiklos Szeredi }
29509d8b586SMiklos Szeredi 
29609d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode)
29709d8b586SMiklos Szeredi {
29809d8b586SMiklos Szeredi 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
29909d8b586SMiklos Szeredi }
30009d8b586SMiklos Szeredi 
3012664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */
3022664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode)
3032664bd08SVivek Goyal {
3042664bd08SVivek Goyal 	if (WARN_ON(!S_ISREG(inode->i_mode)))
3052664bd08SVivek Goyal 		return NULL;
3062664bd08SVivek Goyal 
3072664bd08SVivek Goyal 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
3082664bd08SVivek Goyal }
30909d8b586SMiklos Szeredi 
3104823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */
3114823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode)
3124823d49cSVivek Goyal {
3134823d49cSVivek Goyal 	struct inode *upperinode;
3144823d49cSVivek Goyal 
3154823d49cSVivek Goyal 	upperinode = ovl_inode_upper(inode);
3164823d49cSVivek Goyal 	if (upperinode && ovl_has_upperdata(inode))
3174823d49cSVivek Goyal 		return upperinode;
3184823d49cSVivek Goyal 
3194823d49cSVivek Goyal 	return ovl_inode_lowerdata(inode);
3204823d49cSVivek Goyal }
3214823d49cSVivek Goyal 
3224edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
323bbb1e54dSMiklos Szeredi {
3244edb83bbSMiklos Szeredi 	return OVL_I(inode)->cache;
325bbb1e54dSMiklos Szeredi }
326bbb1e54dSMiklos Szeredi 
3274edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
328bbb1e54dSMiklos Szeredi {
3294edb83bbSMiklos Szeredi 	OVL_I(inode)->cache = cache;
330bbb1e54dSMiklos Szeredi }
331bbb1e54dSMiklos Szeredi 
332c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
333c62520a8SAmir Goldstein {
334c62520a8SAmir Goldstein 	set_bit(flag, &OVL_E(dentry)->flags);
335c62520a8SAmir Goldstein }
336c62520a8SAmir Goldstein 
337c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
338c62520a8SAmir Goldstein {
339c62520a8SAmir Goldstein 	clear_bit(flag, &OVL_E(dentry)->flags);
340c62520a8SAmir Goldstein }
341c62520a8SAmir Goldstein 
342c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
343c62520a8SAmir Goldstein {
344c62520a8SAmir Goldstein 	return test_bit(flag, &OVL_E(dentry)->flags);
345c62520a8SAmir Goldstein }
346c62520a8SAmir Goldstein 
347bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry)
348bbb1e54dSMiklos Szeredi {
349c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
350bbb1e54dSMiklos Szeredi }
351bbb1e54dSMiklos Szeredi 
352bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry)
353bbb1e54dSMiklos Szeredi {
354bbb1e54dSMiklos Szeredi 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
355bbb1e54dSMiklos Szeredi }
356bbb1e54dSMiklos Szeredi 
3575cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry)
358bbb1e54dSMiklos Szeredi {
359c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
360bbb1e54dSMiklos Szeredi }
361bbb1e54dSMiklos Szeredi 
36255acc661SMiklos Szeredi /*
363aa3ff3c1SAmir Goldstein  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
364aa3ff3c1SAmir Goldstein  * to return positive, while there's no actual upper alias for the inode.
365aa3ff3c1SAmir Goldstein  * Copy up code needs to know about the existence of the upper alias, so it
366aa3ff3c1SAmir Goldstein  * can't use ovl_dentry_upper().
36755acc661SMiklos Szeredi  */
36855acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry)
36955acc661SMiklos Szeredi {
370c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
37155acc661SMiklos Szeredi }
37255acc661SMiklos Szeredi 
37355acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry)
37455acc661SMiklos Szeredi {
375c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
37655acc661SMiklos Szeredi }
37755acc661SMiklos Szeredi 
3780c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode)
3790c288874SVivek Goyal {
3800c288874SVivek Goyal 	if (!S_ISREG(inode->i_mode))
3810c288874SVivek Goyal 		return false;
3820c288874SVivek Goyal 
3830c288874SVivek Goyal 	if (!ovl_inode_lower(inode))
3840c288874SVivek Goyal 		return false;
3850c288874SVivek Goyal 
3860c288874SVivek Goyal 	return true;
3870c288874SVivek Goyal }
3880c288874SVivek Goyal 
3890c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode)
3900c288874SVivek Goyal {
3910c288874SVivek Goyal 	if (!ovl_should_check_upperdata(inode))
3920c288874SVivek Goyal 		return true;
3930c288874SVivek Goyal 
3940c288874SVivek Goyal 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
3950c288874SVivek Goyal 		return false;
3960c288874SVivek Goyal 	/*
3970c288874SVivek Goyal 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
3980c288874SVivek Goyal 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
3990c288874SVivek Goyal 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
4000c288874SVivek Goyal 	 * before that are visible too.
4010c288874SVivek Goyal 	 */
4020c288874SVivek Goyal 	smp_rmb();
4030c288874SVivek Goyal 	return true;
4040c288874SVivek Goyal }
4050c288874SVivek Goyal 
4060c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode)
4070c288874SVivek Goyal {
4080c288874SVivek Goyal 	/*
4090c288874SVivek Goyal 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
4100c288874SVivek Goyal 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
4110c288874SVivek Goyal 	 * before it are visible as well.
4120c288874SVivek Goyal 	 */
4130c288874SVivek Goyal 	smp_wmb();
4140c288874SVivek Goyal 	ovl_set_flag(OVL_UPPERDATA, inode);
4150c288874SVivek Goyal }
4160c288874SVivek Goyal 
4170c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
4180c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
4190c288874SVivek Goyal {
4200c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4210c288874SVivek Goyal 		return false;
4220c288874SVivek Goyal 
4230c288874SVivek Goyal 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
4240c288874SVivek Goyal }
4250c288874SVivek Goyal 
4260c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
4270c288874SVivek Goyal {
4280c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4290c288874SVivek Goyal 		return false;
4300c288874SVivek Goyal 
4310c288874SVivek Goyal 	return !ovl_has_upperdata(d_inode(dentry));
4320c288874SVivek Goyal }
4330c288874SVivek Goyal 
434a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb)
435a6c60655SMiklos Szeredi {
436a6c60655SMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
437a6c60655SMiklos Szeredi 
43821a22878SAmir Goldstein 	return ofs->config.redirect_dir && !ofs->noxattr;
439a6c60655SMiklos Szeredi }
440a6c60655SMiklos Szeredi 
441a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry)
442a6c60655SMiklos Szeredi {
443cf31c463SMiklos Szeredi 	return OVL_I(d_inode(dentry))->redirect;
444a6c60655SMiklos Szeredi }
445a6c60655SMiklos Szeredi 
446a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
447a6c60655SMiklos Szeredi {
448cf31c463SMiklos Szeredi 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
449a6c60655SMiklos Szeredi 
450cf31c463SMiklos Szeredi 	kfree(oi->redirect);
451cf31c463SMiklos Szeredi 	oi->redirect = redirect;
452a6c60655SMiklos Szeredi }
453a6c60655SMiklos Szeredi 
45409d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
455bbb1e54dSMiklos Szeredi {
45609d8b586SMiklos Szeredi 	struct inode *upperinode = d_inode(upperdentry);
457e6d2ebddSMiklos Szeredi 
45809d8b586SMiklos Szeredi 	WARN_ON(OVL_I(inode)->__upperdentry);
45909d8b586SMiklos Szeredi 
46025b7713aSMiklos Szeredi 	/*
46109d8b586SMiklos Szeredi 	 * Make sure upperdentry is consistent before making it visible
46225b7713aSMiklos Szeredi 	 */
46325b7713aSMiklos Szeredi 	smp_wmb();
46409d8b586SMiklos Szeredi 	OVL_I(inode)->__upperdentry = upperdentry;
46531747edaSAmir Goldstein 	if (inode_unhashed(inode)) {
46625b7713aSMiklos Szeredi 		inode->i_private = upperinode;
467bbb1e54dSMiklos Szeredi 		__insert_inode_hash(inode, (unsigned long) upperinode);
468bbb1e54dSMiklos Szeredi 	}
46925b7713aSMiklos Szeredi }
470bbb1e54dSMiklos Szeredi 
47165cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
472bbb1e54dSMiklos Szeredi {
47304a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
474bbb1e54dSMiklos Szeredi 
47504a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
47665cd913eSAmir Goldstein 	WARN_ON(!d_is_dir(dentry));
4774edb83bbSMiklos Szeredi 	/*
47865cd913eSAmir Goldstein 	 * Version is used by readdir code to keep cache consistent.
47965cd913eSAmir Goldstein 	 * For merge dirs (or dirs with origin) all changes need to be noted.
48065cd913eSAmir Goldstein 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
48165cd913eSAmir Goldstein 	 * which have been copied up and have origins), so only need to note
48265cd913eSAmir Goldstein 	 * changes to impure entries.
4834edb83bbSMiklos Szeredi 	 */
4841fa9c5c5SMiklos Szeredi 	if (!ovl_dir_is_real(inode) || impurity)
48504a01ac7SMiklos Szeredi 		OVL_I(inode)->version++;
486bbb1e54dSMiklos Szeredi }
487bbb1e54dSMiklos Szeredi 
488d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity)
489d9854c87SMiklos Szeredi {
490d9854c87SMiklos Szeredi 	/* Copy mtime/ctime */
4912878dffcSChristian Brauner 	ovl_copyattr(d_inode(dentry));
492d9854c87SMiklos Szeredi 
49365cd913eSAmir Goldstein 	ovl_dir_version_inc(dentry, impurity);
494d9854c87SMiklos Szeredi }
495d9854c87SMiklos Szeredi 
4961fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode)
497bbb1e54dSMiklos Szeredi {
49804a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
49904a01ac7SMiklos Szeredi 	return OVL_I(inode)->version;
500bbb1e54dSMiklos Szeredi }
501bbb1e54dSMiklos Szeredi 
502bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry)
503bbb1e54dSMiklos Szeredi {
504bbb1e54dSMiklos Szeredi 	struct inode *inode = dentry->d_inode;
505bbb1e54dSMiklos Szeredi 
506bbb1e54dSMiklos Szeredi 	return inode && IS_WHITEOUT(inode);
507bbb1e54dSMiklos Szeredi }
508bbb1e54dSMiklos Szeredi 
5092d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags)
510bbb1e54dSMiklos Szeredi {
51156230d95SMiklos Szeredi 	struct inode *inode = d_inode(path->dentry);
5124609e1f1SChristian Brauner 	struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
51356230d95SMiklos Szeredi 	int err, acc_mode;
51456230d95SMiklos Szeredi 
51556230d95SMiklos Szeredi 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
51656230d95SMiklos Szeredi 		BUG();
51756230d95SMiklos Szeredi 
51856230d95SMiklos Szeredi 	switch (flags & O_ACCMODE) {
51956230d95SMiklos Szeredi 	case O_RDONLY:
52056230d95SMiklos Szeredi 		acc_mode = MAY_READ;
52156230d95SMiklos Szeredi 		break;
52256230d95SMiklos Szeredi 	case O_WRONLY:
52356230d95SMiklos Szeredi 		acc_mode = MAY_WRITE;
52456230d95SMiklos Szeredi 		break;
52556230d95SMiklos Szeredi 	default:
52656230d95SMiklos Szeredi 		BUG();
52756230d95SMiklos Szeredi 	}
52856230d95SMiklos Szeredi 
5294609e1f1SChristian Brauner 	err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
53056230d95SMiklos Szeredi 	if (err)
53156230d95SMiklos Szeredi 		return ERR_PTR(err);
53256230d95SMiklos Szeredi 
53356230d95SMiklos Szeredi 	/* O_NOATIME is an optimization, don't fail if not permitted */
53401beba79SChristian Brauner 	if (inode_owner_or_capable(real_idmap, inode))
53556230d95SMiklos Szeredi 		flags |= O_NOATIME;
53656230d95SMiklos Szeredi 
53756230d95SMiklos Szeredi 	return dentry_open(path, flags, current_cred());
538bbb1e54dSMiklos Szeredi }
53939d3d60aSAmir Goldstein 
5400c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
5410c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
5420c288874SVivek Goyal {
5430c288874SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5440c288874SVivek Goyal 
5450c288874SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5460c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5470c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
5480c288874SVivek Goyal 		return true;
5490c288874SVivek Goyal 
5500c288874SVivek Goyal 	return false;
5510c288874SVivek Goyal }
5520c288874SVivek Goyal 
5530c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags)
5542002df85SVivek Goyal {
5552002df85SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5562002df85SVivek Goyal 
5572002df85SVivek Goyal 	/*
5582002df85SVivek Goyal 	 * Check if copy-up has happened as well as for upper alias (in
5592002df85SVivek Goyal 	 * case of hard links) is there.
5602002df85SVivek Goyal 	 *
5612002df85SVivek Goyal 	 * Both checks are lockless:
5622002df85SVivek Goyal 	 *  - false negatives: will recheck under oi->lock
5632002df85SVivek Goyal 	 *  - false positives:
5642002df85SVivek Goyal 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
5652002df85SVivek Goyal 	 *      upper dentry is up-to-date
5662002df85SVivek Goyal 	 *    + ovl_dentry_has_upper_alias() relies on locking of
5672002df85SVivek Goyal 	 *      upper parent i_rwsem to prevent reordering copy-up
5682002df85SVivek Goyal 	 *      with rename.
5692002df85SVivek Goyal 	 */
5702002df85SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5710c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5720c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
5732002df85SVivek Goyal 		return true;
5742002df85SVivek Goyal 
5752002df85SVivek Goyal 	return false;
5762002df85SVivek Goyal }
5772002df85SVivek Goyal 
5780c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags)
57939d3d60aSAmir Goldstein {
5801e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
58139d3d60aSAmir Goldstein 	int err;
58239d3d60aSAmir Goldstein 
583531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
5840c288874SVivek Goyal 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
58539d3d60aSAmir Goldstein 		err = 1; /* Already copied up */
5861e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
58739d3d60aSAmir Goldstein 	}
58839d3d60aSAmir Goldstein 
58939d3d60aSAmir Goldstein 	return err;
59039d3d60aSAmir Goldstein }
59139d3d60aSAmir Goldstein 
59239d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry)
59339d3d60aSAmir Goldstein {
5941e92e307SAmir Goldstein 	ovl_inode_unlock(d_inode(dentry));
59539d3d60aSAmir Goldstein }
59682b749b2SAmir Goldstein 
5972d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
598b79e05aaSAmir Goldstein {
599b79e05aaSAmir Goldstein 	int res;
600b79e05aaSAmir Goldstein 
601dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
602b79e05aaSAmir Goldstein 
603b79e05aaSAmir Goldstein 	/* Zero size value means "copied up but origin unknown" */
604b79e05aaSAmir Goldstein 	if (res >= 0)
605b79e05aaSAmir Goldstein 		return true;
606b79e05aaSAmir Goldstein 
607b79e05aaSAmir Goldstein 	return false;
608b79e05aaSAmir Goldstein }
609b79e05aaSAmir Goldstein 
6102d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
61143d193f8SMiklos Szeredi 			       enum ovl_xattr ox)
612f3a15685SAmir Goldstein {
613f3a15685SAmir Goldstein 	int res;
614f3a15685SAmir Goldstein 	char val;
615f3a15685SAmir Goldstein 
616dad7017aSChristian Brauner 	if (!d_is_dir(path->dentry))
617f3a15685SAmir Goldstein 		return false;
618f3a15685SAmir Goldstein 
619dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
620f3a15685SAmir Goldstein 	if (res == 1 && val == 'y')
621f3a15685SAmir Goldstein 		return true;
622f3a15685SAmir Goldstein 
623f3a15685SAmir Goldstein 	return false;
624f3a15685SAmir Goldstein }
625f3a15685SAmir Goldstein 
62643d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
62743d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
62843d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
62943d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX	"impure"
63043d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX		"nlink"
63143d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX		"upper"
63243d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
633096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
63443d193f8SMiklos Szeredi 
63543d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \
6362d2f2d73SMiklos Szeredi 	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
6372d2f2d73SMiklos Szeredi 		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
63843d193f8SMiklos Szeredi 
6392d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = {
64043d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
64143d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
64243d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
64343d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
64443d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
64543d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
64643d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
647096a218aSAmir Goldstein 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
64843d193f8SMiklos Szeredi };
64943d193f8SMiklos Szeredi 
650a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
65143d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
65282b749b2SAmir Goldstein 		       int xerr)
65382b749b2SAmir Goldstein {
65482b749b2SAmir Goldstein 	int err;
65582b749b2SAmir Goldstein 
65682b749b2SAmir Goldstein 	if (ofs->noxattr)
65782b749b2SAmir Goldstein 		return xerr;
65882b749b2SAmir Goldstein 
659c914c0e2SAmir Goldstein 	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
66082b749b2SAmir Goldstein 
66182b749b2SAmir Goldstein 	if (err == -EOPNOTSUPP) {
66243d193f8SMiklos Szeredi 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
66382b749b2SAmir Goldstein 		ofs->noxattr = true;
66482b749b2SAmir Goldstein 		return xerr;
66582b749b2SAmir Goldstein 	}
66682b749b2SAmir Goldstein 
66782b749b2SAmir Goldstein 	return err;
66882b749b2SAmir Goldstein }
669f3a15685SAmir Goldstein 
670f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
671f3a15685SAmir Goldstein {
672a0c236b1SAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
673f3a15685SAmir Goldstein 	int err;
674f3a15685SAmir Goldstein 
67513c72075SMiklos Szeredi 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
676f3a15685SAmir Goldstein 		return 0;
677f3a15685SAmir Goldstein 
678f3a15685SAmir Goldstein 	/*
679f3a15685SAmir Goldstein 	 * Do not fail when upper doesn't support xattrs.
680f3a15685SAmir Goldstein 	 * Upper inodes won't have origin nor redirect xattr anyway.
681f3a15685SAmir Goldstein 	 */
682a0c236b1SAmir Goldstein 	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
683f3a15685SAmir Goldstein 	if (!err)
68413c72075SMiklos Szeredi 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
685f3a15685SAmir Goldstein 
686f3a15685SAmir Goldstein 	return err;
687f3a15685SAmir Goldstein }
68813c72075SMiklos Szeredi 
689096a218aSAmir Goldstein 
690096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
691096a218aSAmir Goldstein 
692096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper)
693096a218aSAmir Goldstein {
694096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
695096a218aSAmir Goldstein 	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
696096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX+1];
697096a218aSAmir Goldstein 	int res, n;
698096a218aSAmir Goldstein 
699dad7017aSChristian Brauner 	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
700096a218aSAmir Goldstein 				 OVL_PROTATTR_MAX);
701096a218aSAmir Goldstein 	if (res < 0)
702096a218aSAmir Goldstein 		return;
703096a218aSAmir Goldstein 
704096a218aSAmir Goldstein 	/*
705096a218aSAmir Goldstein 	 * Initialize inode flags from overlay.protattr xattr and upper inode
706096a218aSAmir Goldstein 	 * flags.  If upper inode has those fileattr flags set (i.e. from old
707096a218aSAmir Goldstein 	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
708096a218aSAmir Goldstein 	 * them on next fileattr_set().
709096a218aSAmir Goldstein 	 */
710096a218aSAmir Goldstein 	for (n = 0; n < res; n++) {
711096a218aSAmir Goldstein 		if (buf[n] == 'a')
712096a218aSAmir Goldstein 			iflags |= S_APPEND;
713096a218aSAmir Goldstein 		else if (buf[n] == 'i')
714096a218aSAmir Goldstein 			iflags |= S_IMMUTABLE;
715096a218aSAmir Goldstein 		else
716096a218aSAmir Goldstein 			break;
717096a218aSAmir Goldstein 	}
718096a218aSAmir Goldstein 
719096a218aSAmir Goldstein 	if (!res || n < res) {
720096a218aSAmir Goldstein 		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
721096a218aSAmir Goldstein 				    upper, res);
722096a218aSAmir Goldstein 	} else {
723096a218aSAmir Goldstein 		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
724096a218aSAmir Goldstein 	}
725096a218aSAmir Goldstein }
726096a218aSAmir Goldstein 
727096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper,
728096a218aSAmir Goldstein 		      struct fileattr *fa)
729096a218aSAmir Goldstein {
730096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
731096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX];
732096a218aSAmir Goldstein 	int len = 0, err = 0;
733096a218aSAmir Goldstein 	u32 iflags = 0;
734096a218aSAmir Goldstein 
735096a218aSAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
736096a218aSAmir Goldstein 
737096a218aSAmir Goldstein 	if (fa->flags & FS_APPEND_FL) {
738096a218aSAmir Goldstein 		buf[len++] = 'a';
739096a218aSAmir Goldstein 		iflags |= S_APPEND;
740096a218aSAmir Goldstein 	}
741096a218aSAmir Goldstein 	if (fa->flags & FS_IMMUTABLE_FL) {
742096a218aSAmir Goldstein 		buf[len++] = 'i';
743096a218aSAmir Goldstein 		iflags |= S_IMMUTABLE;
744096a218aSAmir Goldstein 	}
745096a218aSAmir Goldstein 
746096a218aSAmir Goldstein 	/*
747096a218aSAmir Goldstein 	 * Do not allow to set protection flags when upper doesn't support
748096a218aSAmir Goldstein 	 * xattrs, because we do not set those fileattr flags on upper inode.
749096a218aSAmir Goldstein 	 * Remove xattr if it exist and all protection flags are cleared.
750096a218aSAmir Goldstein 	 */
751096a218aSAmir Goldstein 	if (len) {
752096a218aSAmir Goldstein 		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
753096a218aSAmir Goldstein 					 buf, len, -EPERM);
754096a218aSAmir Goldstein 	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
755c914c0e2SAmir Goldstein 		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
756096a218aSAmir Goldstein 		if (err == -EOPNOTSUPP || err == -ENODATA)
757096a218aSAmir Goldstein 			err = 0;
758096a218aSAmir Goldstein 	}
759096a218aSAmir Goldstein 	if (err)
760096a218aSAmir Goldstein 		return err;
761096a218aSAmir Goldstein 
762096a218aSAmir Goldstein 	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
763096a218aSAmir Goldstein 
764096a218aSAmir Goldstein 	/* Mask out the fileattr flags that should not be set in upper inode */
765096a218aSAmir Goldstein 	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
766096a218aSAmir Goldstein 	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
767096a218aSAmir Goldstein 
768096a218aSAmir Goldstein 	return 0;
769096a218aSAmir Goldstein }
770096a218aSAmir Goldstein 
771ad0af710SAmir Goldstein /**
772ad0af710SAmir Goldstein  * Caller must hold a reference to inode to prevent it from being freed while
773ad0af710SAmir Goldstein  * it is marked inuse.
774ad0af710SAmir Goldstein  */
775ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry)
776ad0af710SAmir Goldstein {
777ad0af710SAmir Goldstein 	struct inode *inode = d_inode(dentry);
778ad0af710SAmir Goldstein 	bool locked = false;
779ad0af710SAmir Goldstein 
780ad0af710SAmir Goldstein 	spin_lock(&inode->i_lock);
781ad0af710SAmir Goldstein 	if (!(inode->i_state & I_OVL_INUSE)) {
782ad0af710SAmir Goldstein 		inode->i_state |= I_OVL_INUSE;
783ad0af710SAmir Goldstein 		locked = true;
784ad0af710SAmir Goldstein 	}
785ad0af710SAmir Goldstein 	spin_unlock(&inode->i_lock);
786ad0af710SAmir Goldstein 
787ad0af710SAmir Goldstein 	return locked;
788ad0af710SAmir Goldstein }
789ad0af710SAmir Goldstein 
790ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry)
791ad0af710SAmir Goldstein {
792ad0af710SAmir Goldstein 	if (dentry) {
793ad0af710SAmir Goldstein 		struct inode *inode = d_inode(dentry);
794ad0af710SAmir Goldstein 
795ad0af710SAmir Goldstein 		spin_lock(&inode->i_lock);
796ad0af710SAmir Goldstein 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
797ad0af710SAmir Goldstein 		inode->i_state &= ~I_OVL_INUSE;
798ad0af710SAmir Goldstein 		spin_unlock(&inode->i_lock);
799ad0af710SAmir Goldstein 	}
800ad0af710SAmir Goldstein }
8015f8415d6SAmir Goldstein 
802146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry)
803146d62e5SAmir Goldstein {
804146d62e5SAmir Goldstein 	struct inode *inode = d_inode(dentry);
805146d62e5SAmir Goldstein 	bool inuse;
806146d62e5SAmir Goldstein 
807146d62e5SAmir Goldstein 	spin_lock(&inode->i_lock);
808146d62e5SAmir Goldstein 	inuse = (inode->i_state & I_OVL_INUSE);
809146d62e5SAmir Goldstein 	spin_unlock(&inode->i_lock);
810146d62e5SAmir Goldstein 
811146d62e5SAmir Goldstein 	return inuse;
812146d62e5SAmir Goldstein }
813146d62e5SAmir Goldstein 
81424b33ee1SAmir Goldstein /*
81524b33ee1SAmir Goldstein  * Does this overlay dentry need to be indexed on copy up?
81624b33ee1SAmir Goldstein  */
81724b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry)
81824b33ee1SAmir Goldstein {
81924b33ee1SAmir Goldstein 	struct dentry *lower = ovl_dentry_lower(dentry);
82024b33ee1SAmir Goldstein 
82124b33ee1SAmir Goldstein 	if (!lower || !ovl_indexdir(dentry->d_sb))
82224b33ee1SAmir Goldstein 		return false;
82324b33ee1SAmir Goldstein 
824fbd2d207SAmir Goldstein 	/* Index all files for NFS export and consistency verification */
825016b720fSAmir Goldstein 	if (ovl_index_all(dentry->d_sb))
826fbd2d207SAmir Goldstein 		return true;
827fbd2d207SAmir Goldstein 
82824b33ee1SAmir Goldstein 	/* Index only lower hardlinks on copy up */
82924b33ee1SAmir Goldstein 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
83024b33ee1SAmir Goldstein 		return true;
83124b33ee1SAmir Goldstein 
83224b33ee1SAmir Goldstein 	return false;
83324b33ee1SAmir Goldstein }
83424b33ee1SAmir Goldstein 
8359f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */
836caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry)
837caf70cb2SAmir Goldstein {
8381cdb0cb6SPavel Tikhomirov 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
839e7dd0e71SAmir Goldstein 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
840e7dd0e71SAmir Goldstein 	struct inode *dir = indexdir->d_inode;
841caf70cb2SAmir Goldstein 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
842caf70cb2SAmir Goldstein 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
843caf70cb2SAmir Goldstein 	struct dentry *index = NULL;
844caf70cb2SAmir Goldstein 	struct inode *inode;
84563e13252SAmir Goldstein 	struct qstr name = { };
846caf70cb2SAmir Goldstein 	int err;
847caf70cb2SAmir Goldstein 
8481cdb0cb6SPavel Tikhomirov 	err = ovl_get_index_name(ofs, lowerdentry, &name);
849caf70cb2SAmir Goldstein 	if (err)
850caf70cb2SAmir Goldstein 		goto fail;
851caf70cb2SAmir Goldstein 
852caf70cb2SAmir Goldstein 	inode = d_inode(upperdentry);
85389a17556SAmir Goldstein 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
8541bd0a3aeSlijiazi 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
855caf70cb2SAmir Goldstein 				    upperdentry, inode->i_ino, inode->i_nlink);
856caf70cb2SAmir Goldstein 		/*
857caf70cb2SAmir Goldstein 		 * We either have a bug with persistent union nlink or a lower
858caf70cb2SAmir Goldstein 		 * hardlink was added while overlay is mounted. Adding a lower
859caf70cb2SAmir Goldstein 		 * hardlink and then unlinking all overlay hardlinks would drop
860caf70cb2SAmir Goldstein 		 * overlay nlink to zero before all upper inodes are unlinked.
861caf70cb2SAmir Goldstein 		 * As a safety measure, when that situation is detected, set
862caf70cb2SAmir Goldstein 		 * the overlay nlink to the index inode nlink minus one for the
863caf70cb2SAmir Goldstein 		 * index entry itself.
864caf70cb2SAmir Goldstein 		 */
865caf70cb2SAmir Goldstein 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
866caf70cb2SAmir Goldstein 		ovl_set_nlink_upper(dentry);
867caf70cb2SAmir Goldstein 		goto out;
868caf70cb2SAmir Goldstein 	}
869caf70cb2SAmir Goldstein 
870caf70cb2SAmir Goldstein 	inode_lock_nested(dir, I_MUTEX_PARENT);
87122f289ceSChristian Brauner 	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
872caf70cb2SAmir Goldstein 	err = PTR_ERR(index);
873e7dd0e71SAmir Goldstein 	if (IS_ERR(index)) {
8749f4ec904SAmir Goldstein 		index = NULL;
875e7dd0e71SAmir Goldstein 	} else if (ovl_index_all(dentry->d_sb)) {
876e7dd0e71SAmir Goldstein 		/* Whiteout orphan index to block future open by handle */
877c21c839bSChengguang Xu 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
878c21c839bSChengguang Xu 					       dir, index);
879e7dd0e71SAmir Goldstein 	} else {
880e7dd0e71SAmir Goldstein 		/* Cleanup orphan index entries */
881576bb263SChristian Brauner 		err = ovl_cleanup(ofs, dir, index);
882e7dd0e71SAmir Goldstein 	}
8839f4ec904SAmir Goldstein 
884caf70cb2SAmir Goldstein 	inode_unlock(dir);
885caf70cb2SAmir Goldstein 	if (err)
886caf70cb2SAmir Goldstein 		goto fail;
887caf70cb2SAmir Goldstein 
888caf70cb2SAmir Goldstein out:
88963e13252SAmir Goldstein 	kfree(name.name);
890caf70cb2SAmir Goldstein 	dput(index);
891caf70cb2SAmir Goldstein 	return;
892caf70cb2SAmir Goldstein 
893caf70cb2SAmir Goldstein fail:
8941bd0a3aeSlijiazi 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
895caf70cb2SAmir Goldstein 	goto out;
896caf70cb2SAmir Goldstein }
897caf70cb2SAmir Goldstein 
8985f8415d6SAmir Goldstein /*
8995f8415d6SAmir Goldstein  * Operations that change overlay inode and upper inode nlink need to be
9005f8415d6SAmir Goldstein  * synchronized with copy up for persistent nlink accounting.
9015f8415d6SAmir Goldstein  */
9020e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry)
9035f8415d6SAmir Goldstein {
9041e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
9055f8415d6SAmir Goldstein 	const struct cred *old_cred;
9065f8415d6SAmir Goldstein 	int err;
9075f8415d6SAmir Goldstein 
9081e92e307SAmir Goldstein 	if (WARN_ON(!inode))
9090e32992fSAmir Goldstein 		return -ENOENT;
9105f8415d6SAmir Goldstein 
9115f8415d6SAmir Goldstein 	/*
9125f8415d6SAmir Goldstein 	 * With inodes index is enabled, we store the union overlay nlink
91324b33ee1SAmir Goldstein 	 * in an xattr on the index inode. When whiting out an indexed lower,
9145f8415d6SAmir Goldstein 	 * we need to decrement the overlay persistent nlink, but before the
9155f8415d6SAmir Goldstein 	 * first copy up, we have no upper index inode to store the xattr.
9165f8415d6SAmir Goldstein 	 *
91724b33ee1SAmir Goldstein 	 * As a workaround, before whiteout/rename over an indexed lower,
9185f8415d6SAmir Goldstein 	 * copy up to create the upper index. Creating the upper index will
9195f8415d6SAmir Goldstein 	 * initialize the overlay nlink, so it could be dropped if unlink
9205f8415d6SAmir Goldstein 	 * or rename succeeds.
9215f8415d6SAmir Goldstein 	 *
9225f8415d6SAmir Goldstein 	 * TODO: implement metadata only index copy up when called with
9235f8415d6SAmir Goldstein 	 *       ovl_copy_up_flags(dentry, O_PATH).
9245f8415d6SAmir Goldstein 	 */
92524b33ee1SAmir Goldstein 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
9265f8415d6SAmir Goldstein 		err = ovl_copy_up(dentry);
9275f8415d6SAmir Goldstein 		if (err)
9285f8415d6SAmir Goldstein 			return err;
9295f8415d6SAmir Goldstein 	}
9305f8415d6SAmir Goldstein 
931531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
9325f8415d6SAmir Goldstein 	if (err)
9335f8415d6SAmir Goldstein 		return err;
9345f8415d6SAmir Goldstein 
9351e92e307SAmir Goldstein 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
9365f8415d6SAmir Goldstein 		goto out;
9375f8415d6SAmir Goldstein 
9385f8415d6SAmir Goldstein 	old_cred = ovl_override_creds(dentry->d_sb);
9395f8415d6SAmir Goldstein 	/*
9405f8415d6SAmir Goldstein 	 * The overlay inode nlink should be incremented/decremented IFF the
9415f8415d6SAmir Goldstein 	 * upper operation succeeds, along with nlink change of upper inode.
9425f8415d6SAmir Goldstein 	 * Therefore, before link/unlink/rename, we store the union nlink
9435f8415d6SAmir Goldstein 	 * value relative to the upper inode nlink in an upper inode xattr.
9445f8415d6SAmir Goldstein 	 */
9455f8415d6SAmir Goldstein 	err = ovl_set_nlink_upper(dentry);
9465f8415d6SAmir Goldstein 	revert_creds(old_cred);
9475f8415d6SAmir Goldstein 
9485f8415d6SAmir Goldstein out:
9495f8415d6SAmir Goldstein 	if (err)
9501e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
9515f8415d6SAmir Goldstein 
9525f8415d6SAmir Goldstein 	return err;
9535f8415d6SAmir Goldstein }
9545f8415d6SAmir Goldstein 
9550e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry)
9565f8415d6SAmir Goldstein {
9571e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
9581e92e307SAmir Goldstein 
9591e92e307SAmir Goldstein 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
960caf70cb2SAmir Goldstein 		const struct cred *old_cred;
961caf70cb2SAmir Goldstein 
962caf70cb2SAmir Goldstein 		old_cred = ovl_override_creds(dentry->d_sb);
963caf70cb2SAmir Goldstein 		ovl_cleanup_index(dentry);
964caf70cb2SAmir Goldstein 		revert_creds(old_cred);
965caf70cb2SAmir Goldstein 	}
966caf70cb2SAmir Goldstein 
9671e92e307SAmir Goldstein 	ovl_inode_unlock(inode);
9685f8415d6SAmir Goldstein }
9695820dc08SAmir Goldstein 
9705820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
9715820dc08SAmir Goldstein {
9725820dc08SAmir Goldstein 	/* Workdir should not be the same as upperdir */
9735820dc08SAmir Goldstein 	if (workdir == upperdir)
9745820dc08SAmir Goldstein 		goto err;
9755820dc08SAmir Goldstein 
9765820dc08SAmir Goldstein 	/* Workdir should not be subdir of upperdir and vice versa */
9775820dc08SAmir Goldstein 	if (lock_rename(workdir, upperdir) != NULL)
9785820dc08SAmir Goldstein 		goto err_unlock;
9795820dc08SAmir Goldstein 
9805820dc08SAmir Goldstein 	return 0;
9815820dc08SAmir Goldstein 
9825820dc08SAmir Goldstein err_unlock:
9835820dc08SAmir Goldstein 	unlock_rename(workdir, upperdir);
9845820dc08SAmir Goldstein err:
9851bd0a3aeSlijiazi 	pr_err("failed to lock workdir+upperdir\n");
9865820dc08SAmir Goldstein 	return -EIO;
9875820dc08SAmir Goldstein }
9889d3dfea3SVivek Goyal 
9899d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
9902d343087SAl Viro int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
9919d3dfea3SVivek Goyal {
9929d3dfea3SVivek Goyal 	int res;
9939d3dfea3SVivek Goyal 
9949d3dfea3SVivek Goyal 	/* Only regular files can have metacopy xattr */
995dad7017aSChristian Brauner 	if (!S_ISREG(d_inode(path->dentry)->i_mode))
9969d3dfea3SVivek Goyal 		return 0;
9979d3dfea3SVivek Goyal 
998dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
9999d3dfea3SVivek Goyal 	if (res < 0) {
10009d3dfea3SVivek Goyal 		if (res == -ENODATA || res == -EOPNOTSUPP)
10019d3dfea3SVivek Goyal 			return 0;
100287b2c60cSMiklos Szeredi 		/*
100387b2c60cSMiklos Szeredi 		 * getxattr on user.* may fail with EACCES in case there's no
100487b2c60cSMiklos Szeredi 		 * read permission on the inode.  Not much we can do, other than
100587b2c60cSMiklos Szeredi 		 * tell the caller that this is not a metacopy inode.
100687b2c60cSMiklos Szeredi 		 */
100787b2c60cSMiklos Szeredi 		if (ofs->config.userxattr && res == -EACCES)
100887b2c60cSMiklos Szeredi 			return 0;
10099d3dfea3SVivek Goyal 		goto out;
10109d3dfea3SVivek Goyal 	}
10119d3dfea3SVivek Goyal 
10129d3dfea3SVivek Goyal 	return 1;
10139d3dfea3SVivek Goyal out:
10141bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
10159d3dfea3SVivek Goyal 	return res;
10169d3dfea3SVivek Goyal }
101767d756c2SVivek Goyal 
101867d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry)
101967d756c2SVivek Goyal {
102067d756c2SVivek Goyal 	struct ovl_entry *oe = dentry->d_fsdata;
102167d756c2SVivek Goyal 
102267d756c2SVivek Goyal 	if (!d_is_reg(dentry))
102367d756c2SVivek Goyal 		return false;
102467d756c2SVivek Goyal 
102567d756c2SVivek Goyal 	if (ovl_dentry_upper(dentry)) {
102667d756c2SVivek Goyal 		if (!ovl_has_upperdata(d_inode(dentry)))
102767d756c2SVivek Goyal 			return true;
102867d756c2SVivek Goyal 		return false;
102967d756c2SVivek Goyal 	}
103067d756c2SVivek Goyal 
103167d756c2SVivek Goyal 	return (oe->numlower > 1);
103267d756c2SVivek Goyal }
10330a2d0d3fSVivek Goyal 
10342d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
10350a2d0d3fSVivek Goyal {
10360a2d0d3fSVivek Goyal 	int res;
10370a2d0d3fSVivek Goyal 	char *s, *next, *buf = NULL;
10380a2d0d3fSVivek Goyal 
1039dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
104092f0d6c9SMiklos Szeredi 	if (res == -ENODATA || res == -EOPNOTSUPP)
10410a2d0d3fSVivek Goyal 		return NULL;
10420a2d0d3fSVivek Goyal 	if (res < 0)
104392f0d6c9SMiklos Szeredi 		goto fail;
104492f0d6c9SMiklos Szeredi 	if (res == 0)
104592f0d6c9SMiklos Szeredi 		goto invalid;
104692f0d6c9SMiklos Szeredi 
104792f0d6c9SMiklos Szeredi 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
104892f0d6c9SMiklos Szeredi 	if (!buf)
104992f0d6c9SMiklos Szeredi 		return ERR_PTR(-ENOMEM);
105092f0d6c9SMiklos Szeredi 
1051dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
105292f0d6c9SMiklos Szeredi 	if (res < 0)
105392f0d6c9SMiklos Szeredi 		goto fail;
10540a2d0d3fSVivek Goyal 	if (res == 0)
10550a2d0d3fSVivek Goyal 		goto invalid;
10560a2d0d3fSVivek Goyal 
10570a2d0d3fSVivek Goyal 	if (buf[0] == '/') {
10580a2d0d3fSVivek Goyal 		for (s = buf; *s++ == '/'; s = next) {
10590a2d0d3fSVivek Goyal 			next = strchrnul(s, '/');
10600a2d0d3fSVivek Goyal 			if (s == next)
10610a2d0d3fSVivek Goyal 				goto invalid;
10620a2d0d3fSVivek Goyal 		}
10630a2d0d3fSVivek Goyal 	} else {
10640a2d0d3fSVivek Goyal 		if (strchr(buf, '/') != NULL)
10650a2d0d3fSVivek Goyal 			goto invalid;
10660a2d0d3fSVivek Goyal 	}
10670a2d0d3fSVivek Goyal 
10680a2d0d3fSVivek Goyal 	return buf;
10690a2d0d3fSVivek Goyal invalid:
10701bd0a3aeSlijiazi 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
10710a2d0d3fSVivek Goyal 	res = -EINVAL;
107292f0d6c9SMiklos Szeredi 	goto err_free;
107392f0d6c9SMiklos Szeredi fail:
107492f0d6c9SMiklos Szeredi 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
107592f0d6c9SMiklos Szeredi err_free:
1076993a0b2aSVivek Goyal 	kfree(buf);
1077993a0b2aSVivek Goyal 	return ERR_PTR(res);
10780a2d0d3fSVivek Goyal }
1079335d3fc5SSargun Dhillon 
1080335d3fc5SSargun Dhillon /*
1081335d3fc5SSargun Dhillon  * ovl_sync_status() - Check fs sync status for volatile mounts
1082335d3fc5SSargun Dhillon  *
1083335d3fc5SSargun Dhillon  * Returns 1 if this is not a volatile mount and a real sync is required.
1084335d3fc5SSargun Dhillon  *
1085335d3fc5SSargun Dhillon  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1086335d3fc5SSargun Dhillon  * have occurred on the upperdir since the mount.
1087335d3fc5SSargun Dhillon  *
1088335d3fc5SSargun Dhillon  * Returns -errno if it is a volatile mount, and the error that occurred since
1089335d3fc5SSargun Dhillon  * the last mount. If the error code changes, it'll return the latest error
1090335d3fc5SSargun Dhillon  * code.
1091335d3fc5SSargun Dhillon  */
1092335d3fc5SSargun Dhillon 
1093335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs)
1094335d3fc5SSargun Dhillon {
1095335d3fc5SSargun Dhillon 	struct vfsmount *mnt;
1096335d3fc5SSargun Dhillon 
1097335d3fc5SSargun Dhillon 	if (ovl_should_sync(ofs))
1098335d3fc5SSargun Dhillon 		return 1;
1099335d3fc5SSargun Dhillon 
1100335d3fc5SSargun Dhillon 	mnt = ovl_upper_mnt(ofs);
1101335d3fc5SSargun Dhillon 	if (!mnt)
1102335d3fc5SSargun Dhillon 		return 0;
1103335d3fc5SSargun Dhillon 
1104335d3fc5SSargun Dhillon 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1105335d3fc5SSargun Dhillon }
11062878dffcSChristian Brauner 
11072878dffcSChristian Brauner /*
11082878dffcSChristian Brauner  * ovl_copyattr() - copy inode attributes from layer to ovl inode
11092878dffcSChristian Brauner  *
11102878dffcSChristian Brauner  * When overlay copies inode information from an upper or lower layer to the
11112878dffcSChristian Brauner  * relevant overlay inode it will apply the idmapping of the upper or lower
11122878dffcSChristian Brauner  * layer when doing so ensuring that the ovl inode ownership will correctly
11132878dffcSChristian Brauner  * reflect the ownership of the idmapped upper or lower layer. For example, an
11142878dffcSChristian Brauner  * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
11152878dffcSChristian Brauner  * map any lower or upper inode owned by id 1001 to id 1000. These mapping
11162878dffcSChristian Brauner  * helpers are nops when the relevant layer isn't idmapped.
11172878dffcSChristian Brauner  */
11182878dffcSChristian Brauner void ovl_copyattr(struct inode *inode)
11192878dffcSChristian Brauner {
11202878dffcSChristian Brauner 	struct path realpath;
11212878dffcSChristian Brauner 	struct inode *realinode;
1122e67fe633SChristian Brauner 	struct mnt_idmap *real_idmap;
112373db6a06SChristian Brauner 	vfsuid_t vfsuid;
112473db6a06SChristian Brauner 	vfsgid_t vfsgid;
11252878dffcSChristian Brauner 
1126b2dd05f1SZhihao Cheng 	realinode = ovl_i_path_real(inode, &realpath);
1127e67fe633SChristian Brauner 	real_idmap = mnt_idmap(realpath.mnt);
11282878dffcSChristian Brauner 
1129e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1130e67fe633SChristian Brauner 	vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
113173db6a06SChristian Brauner 
113273db6a06SChristian Brauner 	inode->i_uid = vfsuid_into_kuid(vfsuid);
113373db6a06SChristian Brauner 	inode->i_gid = vfsgid_into_kgid(vfsgid);
11342878dffcSChristian Brauner 	inode->i_mode = realinode->i_mode;
11352878dffcSChristian Brauner 	inode->i_atime = realinode->i_atime;
11362878dffcSChristian Brauner 	inode->i_mtime = realinode->i_mtime;
11372878dffcSChristian Brauner 	inode->i_ctime = realinode->i_ctime;
11382878dffcSChristian Brauner 	i_size_write(inode, i_size_read(realinode));
11392878dffcSChristian Brauner }
1140