xref: /openbmc/linux/fs/overlayfs/util.c (revision 5522c9c7)
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 {
88*5522c9c7SAmir Goldstein 	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)
92*5522c9c7SAmir Goldstein 		oe->__numlower = numlower;
93bbb1e54dSMiklos Szeredi 
94bbb1e54dSMiklos Szeredi 	return oe;
95bbb1e54dSMiklos Szeredi }
96bbb1e54dSMiklos Szeredi 
97b07d5cc9SAmir Goldstein #define OVL_D_REVALIDATE (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE)
98b07d5cc9SAmir Goldstein 
99bbb1e54dSMiklos Szeredi bool ovl_dentry_remote(struct dentry *dentry)
100bbb1e54dSMiklos Szeredi {
101b07d5cc9SAmir Goldstein 	return dentry->d_flags & OVL_D_REVALIDATE;
102bbb1e54dSMiklos Szeredi }
103bbb1e54dSMiklos Szeredi 
104b07d5cc9SAmir Goldstein void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry)
105b07d5cc9SAmir Goldstein {
106b07d5cc9SAmir Goldstein 	if (!ovl_dentry_remote(realdentry))
107b07d5cc9SAmir Goldstein 		return;
108b07d5cc9SAmir Goldstein 
109b07d5cc9SAmir Goldstein 	spin_lock(&dentry->d_lock);
110b07d5cc9SAmir Goldstein 	dentry->d_flags |= realdentry->d_flags & OVL_D_REVALIDATE;
111b07d5cc9SAmir Goldstein 	spin_unlock(&dentry->d_lock);
112b07d5cc9SAmir Goldstein }
113b07d5cc9SAmir Goldstein 
114b07d5cc9SAmir Goldstein void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry)
115b07d5cc9SAmir Goldstein {
116b07d5cc9SAmir Goldstein 	return ovl_dentry_init_flags(dentry, upperdentry, OVL_D_REVALIDATE);
117b07d5cc9SAmir Goldstein }
118b07d5cc9SAmir Goldstein 
119b07d5cc9SAmir 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);
123*5522c9c7SAmir Goldstein 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
124f4288844SMiklos Szeredi 	unsigned int i, flags = 0;
125f4288844SMiklos Szeredi 
126bccece1eSMiklos Szeredi 	if (upperdentry)
127bccece1eSMiklos Szeredi 		flags |= upperdentry->d_flags;
128*5522c9c7SAmir Goldstein 	for (i = 0; i < ovl_numlower(oe); i++)
129*5522c9c7SAmir Goldstein 		flags |= lowerstack[i].dentry->d_flags;
130f4288844SMiklos Szeredi 
131f4288844SMiklos Szeredi 	spin_lock(&dentry->d_lock);
132f4288844SMiklos Szeredi 	dentry->d_flags &= ~mask;
133f4288844SMiklos Szeredi 	dentry->d_flags |= flags & mask;
134f4288844SMiklos Szeredi 	spin_unlock(&dentry->d_lock);
135f4288844SMiklos Szeredi }
136f4288844SMiklos Szeredi 
137bbb1e54dSMiklos Szeredi bool ovl_dentry_weird(struct dentry *dentry)
138bbb1e54dSMiklos Szeredi {
139bbb1e54dSMiklos Szeredi 	return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
140bbb1e54dSMiklos Szeredi 				  DCACHE_MANAGE_TRANSIT |
141bbb1e54dSMiklos Szeredi 				  DCACHE_OP_HASH |
142bbb1e54dSMiklos Szeredi 				  DCACHE_OP_COMPARE);
143bbb1e54dSMiklos Szeredi }
144bbb1e54dSMiklos Szeredi 
145bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_type(struct dentry *dentry)
146bbb1e54dSMiklos Szeredi {
147a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
148bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = 0;
149bbb1e54dSMiklos Szeredi 
15009d8b586SMiklos Szeredi 	if (ovl_dentry_upper(dentry)) {
151bbb1e54dSMiklos Szeredi 		type = __OVL_PATH_UPPER;
152bbb1e54dSMiklos Szeredi 
153bbb1e54dSMiklos Szeredi 		/*
15459548503SAmir Goldstein 		 * Non-dir dentry can hold lower dentry of its copy up origin.
155bbb1e54dSMiklos Szeredi 		 */
156*5522c9c7SAmir Goldstein 		if (ovl_numlower(oe)) {
15760124877SVivek Goyal 			if (ovl_test_flag(OVL_CONST_INO, d_inode(dentry)))
15859548503SAmir Goldstein 				type |= __OVL_PATH_ORIGIN;
1590b17c28aSVivek Goyal 			if (d_is_dir(dentry) ||
1600b17c28aSVivek Goyal 			    !ovl_has_upperdata(d_inode(dentry)))
161bbb1e54dSMiklos Szeredi 				type |= __OVL_PATH_MERGE;
16259548503SAmir Goldstein 		}
163bbb1e54dSMiklos Szeredi 	} else {
164*5522c9c7SAmir Goldstein 		if (ovl_numlower(oe) > 1)
165bbb1e54dSMiklos Szeredi 			type |= __OVL_PATH_MERGE;
166bbb1e54dSMiklos Szeredi 	}
167bbb1e54dSMiklos Szeredi 	return type;
168bbb1e54dSMiklos Szeredi }
169bbb1e54dSMiklos Szeredi 
170bbb1e54dSMiklos Szeredi void ovl_path_upper(struct dentry *dentry, struct path *path)
171bbb1e54dSMiklos Szeredi {
172bbb1e54dSMiklos Szeredi 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
173bbb1e54dSMiklos Szeredi 
17408f4c7c8SMiklos Szeredi 	path->mnt = ovl_upper_mnt(ofs);
17509d8b586SMiklos Szeredi 	path->dentry = ovl_dentry_upper(dentry);
176bbb1e54dSMiklos Szeredi }
177bbb1e54dSMiklos Szeredi 
178bbb1e54dSMiklos Szeredi void ovl_path_lower(struct dentry *dentry, struct path *path)
179bbb1e54dSMiklos Szeredi {
180a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
181*5522c9c7SAmir Goldstein 	struct ovl_path *lowerpath = ovl_lowerstack(oe);
182bbb1e54dSMiklos Szeredi 
183*5522c9c7SAmir Goldstein 	if (ovl_numlower(oe)) {
184*5522c9c7SAmir Goldstein 		path->mnt = lowerpath->layer->mnt;
185*5522c9c7SAmir Goldstein 		path->dentry = lowerpath->dentry;
186b9343632SChandan Rajendra 	} else {
187b9343632SChandan Rajendra 		*path = (struct path) { };
188b9343632SChandan Rajendra 	}
189bbb1e54dSMiklos Szeredi }
190bbb1e54dSMiklos Szeredi 
1914f93b426SVivek Goyal void ovl_path_lowerdata(struct dentry *dentry, struct path *path)
1924f93b426SVivek Goyal {
193a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
194*5522c9c7SAmir Goldstein 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
1954f93b426SVivek Goyal 
196*5522c9c7SAmir Goldstein 	if (ovl_numlower(oe)) {
197*5522c9c7SAmir Goldstein 		path->mnt = lowerstack[ovl_numlower(oe) - 1].layer->mnt;
198*5522c9c7SAmir Goldstein 		path->dentry = lowerstack[ovl_numlower(oe) - 1].dentry;
1994f93b426SVivek Goyal 	} else {
2004f93b426SVivek Goyal 		*path = (struct path) { };
2014f93b426SVivek Goyal 	}
2024f93b426SVivek Goyal }
2034f93b426SVivek Goyal 
204bbb1e54dSMiklos Szeredi enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
205bbb1e54dSMiklos Szeredi {
206bbb1e54dSMiklos Szeredi 	enum ovl_path_type type = ovl_path_type(dentry);
207bbb1e54dSMiklos Szeredi 
208bbb1e54dSMiklos Szeredi 	if (!OVL_TYPE_UPPER(type))
209bbb1e54dSMiklos Szeredi 		ovl_path_lower(dentry, path);
210bbb1e54dSMiklos Szeredi 	else
211bbb1e54dSMiklos Szeredi 		ovl_path_upper(dentry, path);
212bbb1e54dSMiklos Szeredi 
213bbb1e54dSMiklos Szeredi 	return type;
214bbb1e54dSMiklos Szeredi }
215bbb1e54dSMiklos Szeredi 
2161248ea4bSAmir Goldstein enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path)
2171248ea4bSAmir Goldstein {
2181248ea4bSAmir Goldstein 	enum ovl_path_type type = ovl_path_type(dentry);
2191248ea4bSAmir Goldstein 
2201248ea4bSAmir Goldstein 	WARN_ON_ONCE(d_is_dir(dentry));
2211248ea4bSAmir Goldstein 
2221248ea4bSAmir Goldstein 	if (!OVL_TYPE_UPPER(type) || OVL_TYPE_MERGE(type))
2231248ea4bSAmir Goldstein 		ovl_path_lowerdata(dentry, path);
2241248ea4bSAmir Goldstein 	else
2251248ea4bSAmir Goldstein 		ovl_path_upper(dentry, path);
2261248ea4bSAmir Goldstein 
2271248ea4bSAmir Goldstein 	return type;
2281248ea4bSAmir Goldstein }
2291248ea4bSAmir Goldstein 
230bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_upper(struct dentry *dentry)
231bbb1e54dSMiklos Szeredi {
23209d8b586SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
233bbb1e54dSMiklos Szeredi }
234bbb1e54dSMiklos Szeredi 
235bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_lower(struct dentry *dentry)
236bbb1e54dSMiklos Szeredi {
237a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
238bbb1e54dSMiklos Szeredi 
239*5522c9c7SAmir Goldstein 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->dentry : NULL;
240bbb1e54dSMiklos Szeredi }
241bbb1e54dSMiklos Szeredi 
24213464165SMiklos Szeredi const struct ovl_layer *ovl_layer_lower(struct dentry *dentry)
243da309e8cSAmir Goldstein {
244a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
245da309e8cSAmir Goldstein 
246*5522c9c7SAmir Goldstein 	return ovl_numlower(oe) ? ovl_lowerstack(oe)->layer : NULL;
247da309e8cSAmir Goldstein }
248da309e8cSAmir Goldstein 
249647d253fSVivek Goyal /*
250647d253fSVivek Goyal  * ovl_dentry_lower() could return either a data dentry or metacopy dentry
251597534e7SXiong Zhenwu  * depending on what is stored in lowerstack[0]. At times we need to find
252647d253fSVivek Goyal  * lower dentry which has data (and not metacopy dentry). This helper
253647d253fSVivek Goyal  * returns the lower data dentry.
254647d253fSVivek Goyal  */
255647d253fSVivek Goyal struct dentry *ovl_dentry_lowerdata(struct dentry *dentry)
256647d253fSVivek Goyal {
257a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
258647d253fSVivek Goyal 
259*5522c9c7SAmir Goldstein 	return ovl_numlower(oe) ?
260*5522c9c7SAmir Goldstein 		ovl_lowerstack(oe)[ovl_numlower(oe) - 1].dentry : NULL;
261647d253fSVivek Goyal }
262647d253fSVivek Goyal 
263bbb1e54dSMiklos Szeredi struct dentry *ovl_dentry_real(struct dentry *dentry)
264bbb1e54dSMiklos Szeredi {
26509d8b586SMiklos Szeredi 	return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
266bbb1e54dSMiklos Szeredi }
267bbb1e54dSMiklos Szeredi 
2681d88f183SMiklos Szeredi struct dentry *ovl_i_dentry_upper(struct inode *inode)
2691d88f183SMiklos Szeredi {
2701d88f183SMiklos Szeredi 	return ovl_upperdentry_dereference(OVL_I(inode));
2711d88f183SMiklos Szeredi }
2721d88f183SMiklos Szeredi 
273b2dd05f1SZhihao Cheng struct inode *ovl_i_path_real(struct inode *inode, struct path *path)
274ffa5723cSAmir Goldstein {
275ffa5723cSAmir Goldstein 	path->dentry = ovl_i_dentry_upper(inode);
276ffa5723cSAmir Goldstein 	if (!path->dentry) {
277ffa5723cSAmir Goldstein 		path->dentry = OVL_I(inode)->lowerpath.dentry;
278ffa5723cSAmir Goldstein 		path->mnt = OVL_I(inode)->lowerpath.layer->mnt;
279ffa5723cSAmir Goldstein 	} else {
280ffa5723cSAmir Goldstein 		path->mnt = ovl_upper_mnt(OVL_FS(inode->i_sb));
281ffa5723cSAmir Goldstein 	}
282b2dd05f1SZhihao Cheng 
283b2dd05f1SZhihao Cheng 	return path->dentry ? d_inode_rcu(path->dentry) : NULL;
284ffa5723cSAmir Goldstein }
285ffa5723cSAmir Goldstein 
28609d8b586SMiklos Szeredi struct inode *ovl_inode_upper(struct inode *inode)
28725b7713aSMiklos Szeredi {
2881d88f183SMiklos Szeredi 	struct dentry *upperdentry = ovl_i_dentry_upper(inode);
28925b7713aSMiklos Szeredi 
29009d8b586SMiklos Szeredi 	return upperdentry ? d_inode(upperdentry) : NULL;
29125b7713aSMiklos Szeredi }
29225b7713aSMiklos Szeredi 
29309d8b586SMiklos Szeredi struct inode *ovl_inode_lower(struct inode *inode)
29409d8b586SMiklos Szeredi {
295ffa5723cSAmir Goldstein 	struct dentry *lowerdentry = OVL_I(inode)->lowerpath.dentry;
296ffa5723cSAmir Goldstein 
297ffa5723cSAmir Goldstein 	return lowerdentry ? d_inode(lowerdentry) : NULL;
29809d8b586SMiklos Szeredi }
29909d8b586SMiklos Szeredi 
30009d8b586SMiklos Szeredi struct inode *ovl_inode_real(struct inode *inode)
30109d8b586SMiklos Szeredi {
30209d8b586SMiklos Szeredi 	return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
30309d8b586SMiklos Szeredi }
30409d8b586SMiklos Szeredi 
3052664bd08SVivek Goyal /* Return inode which contains lower data. Do not return metacopy */
3062664bd08SVivek Goyal struct inode *ovl_inode_lowerdata(struct inode *inode)
3072664bd08SVivek Goyal {
3082664bd08SVivek Goyal 	if (WARN_ON(!S_ISREG(inode->i_mode)))
3092664bd08SVivek Goyal 		return NULL;
3102664bd08SVivek Goyal 
3112664bd08SVivek Goyal 	return OVL_I(inode)->lowerdata ?: ovl_inode_lower(inode);
3122664bd08SVivek Goyal }
31309d8b586SMiklos Szeredi 
3144823d49cSVivek Goyal /* Return real inode which contains data. Does not return metacopy inode */
3154823d49cSVivek Goyal struct inode *ovl_inode_realdata(struct inode *inode)
3164823d49cSVivek Goyal {
3174823d49cSVivek Goyal 	struct inode *upperinode;
3184823d49cSVivek Goyal 
3194823d49cSVivek Goyal 	upperinode = ovl_inode_upper(inode);
3204823d49cSVivek Goyal 	if (upperinode && ovl_has_upperdata(inode))
3214823d49cSVivek Goyal 		return upperinode;
3224823d49cSVivek Goyal 
3234823d49cSVivek Goyal 	return ovl_inode_lowerdata(inode);
3244823d49cSVivek Goyal }
3254823d49cSVivek Goyal 
3264edb83bbSMiklos Szeredi struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
327bbb1e54dSMiklos Szeredi {
3284edb83bbSMiklos Szeredi 	return OVL_I(inode)->cache;
329bbb1e54dSMiklos Szeredi }
330bbb1e54dSMiklos Szeredi 
3314edb83bbSMiklos Szeredi void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
332bbb1e54dSMiklos Szeredi {
3334edb83bbSMiklos Szeredi 	OVL_I(inode)->cache = cache;
334bbb1e54dSMiklos Szeredi }
335bbb1e54dSMiklos Szeredi 
336c62520a8SAmir Goldstein void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry)
337c62520a8SAmir Goldstein {
338a6ff2bc0SAmir Goldstein 	set_bit(flag, OVL_E_FLAGS(dentry));
339c62520a8SAmir Goldstein }
340c62520a8SAmir Goldstein 
341c62520a8SAmir Goldstein void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry)
342c62520a8SAmir Goldstein {
343a6ff2bc0SAmir Goldstein 	clear_bit(flag, OVL_E_FLAGS(dentry));
344c62520a8SAmir Goldstein }
345c62520a8SAmir Goldstein 
346c62520a8SAmir Goldstein bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry)
347c62520a8SAmir Goldstein {
348a6ff2bc0SAmir Goldstein 	return test_bit(flag, OVL_E_FLAGS(dentry));
349c62520a8SAmir Goldstein }
350c62520a8SAmir Goldstein 
351bbb1e54dSMiklos Szeredi bool ovl_dentry_is_opaque(struct dentry *dentry)
352bbb1e54dSMiklos Szeredi {
353c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_OPAQUE, dentry);
354bbb1e54dSMiklos Szeredi }
355bbb1e54dSMiklos Szeredi 
356bbb1e54dSMiklos Szeredi bool ovl_dentry_is_whiteout(struct dentry *dentry)
357bbb1e54dSMiklos Szeredi {
358bbb1e54dSMiklos Szeredi 	return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
359bbb1e54dSMiklos Szeredi }
360bbb1e54dSMiklos Szeredi 
3615cf5b477SMiklos Szeredi void ovl_dentry_set_opaque(struct dentry *dentry)
362bbb1e54dSMiklos Szeredi {
363c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_OPAQUE, dentry);
364bbb1e54dSMiklos Szeredi }
365bbb1e54dSMiklos Szeredi 
36655acc661SMiklos Szeredi /*
367aa3ff3c1SAmir Goldstein  * For hard links and decoded file handles, it's possible for ovl_dentry_upper()
368aa3ff3c1SAmir Goldstein  * to return positive, while there's no actual upper alias for the inode.
369aa3ff3c1SAmir Goldstein  * Copy up code needs to know about the existence of the upper alias, so it
370aa3ff3c1SAmir Goldstein  * can't use ovl_dentry_upper().
37155acc661SMiklos Szeredi  */
37255acc661SMiklos Szeredi bool ovl_dentry_has_upper_alias(struct dentry *dentry)
37355acc661SMiklos Szeredi {
374c62520a8SAmir Goldstein 	return ovl_dentry_test_flag(OVL_E_UPPER_ALIAS, dentry);
37555acc661SMiklos Szeredi }
37655acc661SMiklos Szeredi 
37755acc661SMiklos Szeredi void ovl_dentry_set_upper_alias(struct dentry *dentry)
37855acc661SMiklos Szeredi {
379c62520a8SAmir Goldstein 	ovl_dentry_set_flag(OVL_E_UPPER_ALIAS, dentry);
38055acc661SMiklos Szeredi }
38155acc661SMiklos Szeredi 
3820c288874SVivek Goyal static bool ovl_should_check_upperdata(struct inode *inode)
3830c288874SVivek Goyal {
3840c288874SVivek Goyal 	if (!S_ISREG(inode->i_mode))
3850c288874SVivek Goyal 		return false;
3860c288874SVivek Goyal 
3870c288874SVivek Goyal 	if (!ovl_inode_lower(inode))
3880c288874SVivek Goyal 		return false;
3890c288874SVivek Goyal 
3900c288874SVivek Goyal 	return true;
3910c288874SVivek Goyal }
3920c288874SVivek Goyal 
3930c288874SVivek Goyal bool ovl_has_upperdata(struct inode *inode)
3940c288874SVivek Goyal {
3950c288874SVivek Goyal 	if (!ovl_should_check_upperdata(inode))
3960c288874SVivek Goyal 		return true;
3970c288874SVivek Goyal 
3980c288874SVivek Goyal 	if (!ovl_test_flag(OVL_UPPERDATA, inode))
3990c288874SVivek Goyal 		return false;
4000c288874SVivek Goyal 	/*
4010c288874SVivek Goyal 	 * Pairs with smp_wmb() in ovl_set_upperdata(). Main user of
4020c288874SVivek Goyal 	 * ovl_has_upperdata() is ovl_copy_up_meta_inode_data(). Make sure
4030c288874SVivek Goyal 	 * if setting of OVL_UPPERDATA is visible, then effects of writes
4040c288874SVivek Goyal 	 * before that are visible too.
4050c288874SVivek Goyal 	 */
4060c288874SVivek Goyal 	smp_rmb();
4070c288874SVivek Goyal 	return true;
4080c288874SVivek Goyal }
4090c288874SVivek Goyal 
4100c288874SVivek Goyal void ovl_set_upperdata(struct inode *inode)
4110c288874SVivek Goyal {
4120c288874SVivek Goyal 	/*
4130c288874SVivek Goyal 	 * Pairs with smp_rmb() in ovl_has_upperdata(). Make sure
4140c288874SVivek Goyal 	 * if OVL_UPPERDATA flag is visible, then effects of write operations
4150c288874SVivek Goyal 	 * before it are visible as well.
4160c288874SVivek Goyal 	 */
4170c288874SVivek Goyal 	smp_wmb();
4180c288874SVivek Goyal 	ovl_set_flag(OVL_UPPERDATA, inode);
4190c288874SVivek Goyal }
4200c288874SVivek Goyal 
4210c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
4220c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags)
4230c288874SVivek Goyal {
4240c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4250c288874SVivek Goyal 		return false;
4260c288874SVivek Goyal 
4270c288874SVivek Goyal 	return !ovl_test_flag(OVL_UPPERDATA, d_inode(dentry));
4280c288874SVivek Goyal }
4290c288874SVivek Goyal 
4300c288874SVivek Goyal bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags)
4310c288874SVivek Goyal {
4320c288874SVivek Goyal 	if (!ovl_open_flags_need_copy_up(flags))
4330c288874SVivek Goyal 		return false;
4340c288874SVivek Goyal 
4350c288874SVivek Goyal 	return !ovl_has_upperdata(d_inode(dentry));
4360c288874SVivek Goyal }
4370c288874SVivek Goyal 
438a6c60655SMiklos Szeredi bool ovl_redirect_dir(struct super_block *sb)
439a6c60655SMiklos Szeredi {
440a6c60655SMiklos Szeredi 	struct ovl_fs *ofs = sb->s_fs_info;
441a6c60655SMiklos Szeredi 
44221a22878SAmir Goldstein 	return ofs->config.redirect_dir && !ofs->noxattr;
443a6c60655SMiklos Szeredi }
444a6c60655SMiklos Szeredi 
445a6c60655SMiklos Szeredi const char *ovl_dentry_get_redirect(struct dentry *dentry)
446a6c60655SMiklos Szeredi {
447cf31c463SMiklos Szeredi 	return OVL_I(d_inode(dentry))->redirect;
448a6c60655SMiklos Szeredi }
449a6c60655SMiklos Szeredi 
450a6c60655SMiklos Szeredi void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
451a6c60655SMiklos Szeredi {
452cf31c463SMiklos Szeredi 	struct ovl_inode *oi = OVL_I(d_inode(dentry));
453a6c60655SMiklos Szeredi 
454cf31c463SMiklos Szeredi 	kfree(oi->redirect);
455cf31c463SMiklos Szeredi 	oi->redirect = redirect;
456a6c60655SMiklos Szeredi }
457a6c60655SMiklos Szeredi 
45809d8b586SMiklos Szeredi void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
459bbb1e54dSMiklos Szeredi {
46009d8b586SMiklos Szeredi 	struct inode *upperinode = d_inode(upperdentry);
461e6d2ebddSMiklos Szeredi 
46209d8b586SMiklos Szeredi 	WARN_ON(OVL_I(inode)->__upperdentry);
46309d8b586SMiklos Szeredi 
46425b7713aSMiklos Szeredi 	/*
46509d8b586SMiklos Szeredi 	 * Make sure upperdentry is consistent before making it visible
46625b7713aSMiklos Szeredi 	 */
46725b7713aSMiklos Szeredi 	smp_wmb();
46809d8b586SMiklos Szeredi 	OVL_I(inode)->__upperdentry = upperdentry;
46931747edaSAmir Goldstein 	if (inode_unhashed(inode)) {
47025b7713aSMiklos Szeredi 		inode->i_private = upperinode;
471bbb1e54dSMiklos Szeredi 		__insert_inode_hash(inode, (unsigned long) upperinode);
472bbb1e54dSMiklos Szeredi 	}
47325b7713aSMiklos Szeredi }
474bbb1e54dSMiklos Szeredi 
47565cd913eSAmir Goldstein static void ovl_dir_version_inc(struct dentry *dentry, bool impurity)
476bbb1e54dSMiklos Szeredi {
47704a01ac7SMiklos Szeredi 	struct inode *inode = d_inode(dentry);
478bbb1e54dSMiklos Szeredi 
47904a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
48065cd913eSAmir Goldstein 	WARN_ON(!d_is_dir(dentry));
4814edb83bbSMiklos Szeredi 	/*
48265cd913eSAmir Goldstein 	 * Version is used by readdir code to keep cache consistent.
48365cd913eSAmir Goldstein 	 * For merge dirs (or dirs with origin) all changes need to be noted.
48465cd913eSAmir Goldstein 	 * For non-merge dirs, cache contains only impure entries (i.e. ones
48565cd913eSAmir Goldstein 	 * which have been copied up and have origins), so only need to note
48665cd913eSAmir Goldstein 	 * changes to impure entries.
4874edb83bbSMiklos Szeredi 	 */
4881fa9c5c5SMiklos Szeredi 	if (!ovl_dir_is_real(inode) || impurity)
48904a01ac7SMiklos Szeredi 		OVL_I(inode)->version++;
490bbb1e54dSMiklos Szeredi }
491bbb1e54dSMiklos Szeredi 
492d9854c87SMiklos Szeredi void ovl_dir_modified(struct dentry *dentry, bool impurity)
493d9854c87SMiklos Szeredi {
494d9854c87SMiklos Szeredi 	/* Copy mtime/ctime */
4952878dffcSChristian Brauner 	ovl_copyattr(d_inode(dentry));
496d9854c87SMiklos Szeredi 
49765cd913eSAmir Goldstein 	ovl_dir_version_inc(dentry, impurity);
498d9854c87SMiklos Szeredi }
499d9854c87SMiklos Szeredi 
5001fa9c5c5SMiklos Szeredi u64 ovl_inode_version_get(struct inode *inode)
501bbb1e54dSMiklos Szeredi {
50204a01ac7SMiklos Szeredi 	WARN_ON(!inode_is_locked(inode));
50304a01ac7SMiklos Szeredi 	return OVL_I(inode)->version;
504bbb1e54dSMiklos Szeredi }
505bbb1e54dSMiklos Szeredi 
506bbb1e54dSMiklos Szeredi bool ovl_is_whiteout(struct dentry *dentry)
507bbb1e54dSMiklos Szeredi {
508bbb1e54dSMiklos Szeredi 	struct inode *inode = dentry->d_inode;
509bbb1e54dSMiklos Szeredi 
510bbb1e54dSMiklos Szeredi 	return inode && IS_WHITEOUT(inode);
511bbb1e54dSMiklos Szeredi }
512bbb1e54dSMiklos Szeredi 
5132d343087SAl Viro struct file *ovl_path_open(const struct path *path, int flags)
514bbb1e54dSMiklos Szeredi {
51556230d95SMiklos Szeredi 	struct inode *inode = d_inode(path->dentry);
5164609e1f1SChristian Brauner 	struct mnt_idmap *real_idmap = mnt_idmap(path->mnt);
51756230d95SMiklos Szeredi 	int err, acc_mode;
51856230d95SMiklos Szeredi 
51956230d95SMiklos Szeredi 	if (flags & ~(O_ACCMODE | O_LARGEFILE))
52056230d95SMiklos Szeredi 		BUG();
52156230d95SMiklos Szeredi 
52256230d95SMiklos Szeredi 	switch (flags & O_ACCMODE) {
52356230d95SMiklos Szeredi 	case O_RDONLY:
52456230d95SMiklos Szeredi 		acc_mode = MAY_READ;
52556230d95SMiklos Szeredi 		break;
52656230d95SMiklos Szeredi 	case O_WRONLY:
52756230d95SMiklos Szeredi 		acc_mode = MAY_WRITE;
52856230d95SMiklos Szeredi 		break;
52956230d95SMiklos Szeredi 	default:
53056230d95SMiklos Szeredi 		BUG();
53156230d95SMiklos Szeredi 	}
53256230d95SMiklos Szeredi 
5334609e1f1SChristian Brauner 	err = inode_permission(real_idmap, inode, acc_mode | MAY_OPEN);
53456230d95SMiklos Szeredi 	if (err)
53556230d95SMiklos Szeredi 		return ERR_PTR(err);
53656230d95SMiklos Szeredi 
53756230d95SMiklos Szeredi 	/* O_NOATIME is an optimization, don't fail if not permitted */
53801beba79SChristian Brauner 	if (inode_owner_or_capable(real_idmap, inode))
53956230d95SMiklos Szeredi 		flags |= O_NOATIME;
54056230d95SMiklos Szeredi 
54156230d95SMiklos Szeredi 	return dentry_open(path, flags, current_cred());
542bbb1e54dSMiklos Szeredi }
54339d3d60aSAmir Goldstein 
5440c288874SVivek Goyal /* Caller should hold ovl_inode->lock */
5450c288874SVivek Goyal static bool ovl_already_copied_up_locked(struct dentry *dentry, int flags)
5460c288874SVivek Goyal {
5470c288874SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5480c288874SVivek Goyal 
5490c288874SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5500c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5510c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up_locked(dentry, flags))
5520c288874SVivek Goyal 		return true;
5530c288874SVivek Goyal 
5540c288874SVivek Goyal 	return false;
5550c288874SVivek Goyal }
5560c288874SVivek Goyal 
5570c288874SVivek Goyal bool ovl_already_copied_up(struct dentry *dentry, int flags)
5582002df85SVivek Goyal {
5592002df85SVivek Goyal 	bool disconnected = dentry->d_flags & DCACHE_DISCONNECTED;
5602002df85SVivek Goyal 
5612002df85SVivek Goyal 	/*
5622002df85SVivek Goyal 	 * Check if copy-up has happened as well as for upper alias (in
5632002df85SVivek Goyal 	 * case of hard links) is there.
5642002df85SVivek Goyal 	 *
5652002df85SVivek Goyal 	 * Both checks are lockless:
5662002df85SVivek Goyal 	 *  - false negatives: will recheck under oi->lock
5672002df85SVivek Goyal 	 *  - false positives:
5682002df85SVivek Goyal 	 *    + ovl_dentry_upper() uses memory barriers to ensure the
5692002df85SVivek Goyal 	 *      upper dentry is up-to-date
5702002df85SVivek Goyal 	 *    + ovl_dentry_has_upper_alias() relies on locking of
5712002df85SVivek Goyal 	 *      upper parent i_rwsem to prevent reordering copy-up
5722002df85SVivek Goyal 	 *      with rename.
5732002df85SVivek Goyal 	 */
5742002df85SVivek Goyal 	if (ovl_dentry_upper(dentry) &&
5750c288874SVivek Goyal 	    (ovl_dentry_has_upper_alias(dentry) || disconnected) &&
5760c288874SVivek Goyal 	    !ovl_dentry_needs_data_copy_up(dentry, flags))
5772002df85SVivek Goyal 		return true;
5782002df85SVivek Goyal 
5792002df85SVivek Goyal 	return false;
5802002df85SVivek Goyal }
5812002df85SVivek Goyal 
5820c288874SVivek Goyal int ovl_copy_up_start(struct dentry *dentry, int flags)
58339d3d60aSAmir Goldstein {
5841e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
58539d3d60aSAmir Goldstein 	int err;
58639d3d60aSAmir Goldstein 
587531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
5880c288874SVivek Goyal 	if (!err && ovl_already_copied_up_locked(dentry, flags)) {
58939d3d60aSAmir Goldstein 		err = 1; /* Already copied up */
5901e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
59139d3d60aSAmir Goldstein 	}
59239d3d60aSAmir Goldstein 
59339d3d60aSAmir Goldstein 	return err;
59439d3d60aSAmir Goldstein }
59539d3d60aSAmir Goldstein 
59639d3d60aSAmir Goldstein void ovl_copy_up_end(struct dentry *dentry)
59739d3d60aSAmir Goldstein {
5981e92e307SAmir Goldstein 	ovl_inode_unlock(d_inode(dentry));
59939d3d60aSAmir Goldstein }
60082b749b2SAmir Goldstein 
6012d343087SAl Viro bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path)
602b79e05aaSAmir Goldstein {
603b79e05aaSAmir Goldstein 	int res;
604b79e05aaSAmir Goldstein 
605dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_ORIGIN, NULL, 0);
606b79e05aaSAmir Goldstein 
607b79e05aaSAmir Goldstein 	/* Zero size value means "copied up but origin unknown" */
608b79e05aaSAmir Goldstein 	if (res >= 0)
609b79e05aaSAmir Goldstein 		return true;
610b79e05aaSAmir Goldstein 
611b79e05aaSAmir Goldstein 	return false;
612b79e05aaSAmir Goldstein }
613b79e05aaSAmir Goldstein 
6142d343087SAl Viro bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
61543d193f8SMiklos Szeredi 			       enum ovl_xattr ox)
616f3a15685SAmir Goldstein {
617f3a15685SAmir Goldstein 	int res;
618f3a15685SAmir Goldstein 	char val;
619f3a15685SAmir Goldstein 
620dad7017aSChristian Brauner 	if (!d_is_dir(path->dentry))
621f3a15685SAmir Goldstein 		return false;
622f3a15685SAmir Goldstein 
623dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, ox, &val, 1);
624f3a15685SAmir Goldstein 	if (res == 1 && val == 'y')
625f3a15685SAmir Goldstein 		return true;
626f3a15685SAmir Goldstein 
627f3a15685SAmir Goldstein 	return false;
628f3a15685SAmir Goldstein }
629f3a15685SAmir Goldstein 
63043d193f8SMiklos Szeredi #define OVL_XATTR_OPAQUE_POSTFIX	"opaque"
63143d193f8SMiklos Szeredi #define OVL_XATTR_REDIRECT_POSTFIX	"redirect"
63243d193f8SMiklos Szeredi #define OVL_XATTR_ORIGIN_POSTFIX	"origin"
63343d193f8SMiklos Szeredi #define OVL_XATTR_IMPURE_POSTFIX	"impure"
63443d193f8SMiklos Szeredi #define OVL_XATTR_NLINK_POSTFIX		"nlink"
63543d193f8SMiklos Szeredi #define OVL_XATTR_UPPER_POSTFIX		"upper"
63643d193f8SMiklos Szeredi #define OVL_XATTR_METACOPY_POSTFIX	"metacopy"
637096a218aSAmir Goldstein #define OVL_XATTR_PROTATTR_POSTFIX	"protattr"
63843d193f8SMiklos Szeredi 
63943d193f8SMiklos Szeredi #define OVL_XATTR_TAB_ENTRY(x) \
6402d2f2d73SMiklos Szeredi 	[x] = { [false] = OVL_XATTR_TRUSTED_PREFIX x ## _POSTFIX, \
6412d2f2d73SMiklos Szeredi 		[true] = OVL_XATTR_USER_PREFIX x ## _POSTFIX }
64243d193f8SMiklos Szeredi 
6432d2f2d73SMiklos Szeredi const char *const ovl_xattr_table[][2] = {
64443d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_OPAQUE),
64543d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_REDIRECT),
64643d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_ORIGIN),
64743d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_IMPURE),
64843d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_NLINK),
64943d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_UPPER),
65043d193f8SMiklos Szeredi 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_METACOPY),
651096a218aSAmir Goldstein 	OVL_XATTR_TAB_ENTRY(OVL_XATTR_PROTATTR),
65243d193f8SMiklos Szeredi };
65343d193f8SMiklos Szeredi 
654a0c236b1SAmir Goldstein int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
65543d193f8SMiklos Szeredi 		       enum ovl_xattr ox, const void *value, size_t size,
65682b749b2SAmir Goldstein 		       int xerr)
65782b749b2SAmir Goldstein {
65882b749b2SAmir Goldstein 	int err;
65982b749b2SAmir Goldstein 
66082b749b2SAmir Goldstein 	if (ofs->noxattr)
66182b749b2SAmir Goldstein 		return xerr;
66282b749b2SAmir Goldstein 
663c914c0e2SAmir Goldstein 	err = ovl_setxattr(ofs, upperdentry, ox, value, size);
66482b749b2SAmir Goldstein 
66582b749b2SAmir Goldstein 	if (err == -EOPNOTSUPP) {
66643d193f8SMiklos Szeredi 		pr_warn("cannot set %s xattr on upper\n", ovl_xattr(ofs, ox));
66782b749b2SAmir Goldstein 		ofs->noxattr = true;
66882b749b2SAmir Goldstein 		return xerr;
66982b749b2SAmir Goldstein 	}
67082b749b2SAmir Goldstein 
67182b749b2SAmir Goldstein 	return err;
67282b749b2SAmir Goldstein }
673f3a15685SAmir Goldstein 
674f3a15685SAmir Goldstein int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
675f3a15685SAmir Goldstein {
676a0c236b1SAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
677f3a15685SAmir Goldstein 	int err;
678f3a15685SAmir Goldstein 
67913c72075SMiklos Szeredi 	if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
680f3a15685SAmir Goldstein 		return 0;
681f3a15685SAmir Goldstein 
682f3a15685SAmir Goldstein 	/*
683f3a15685SAmir Goldstein 	 * Do not fail when upper doesn't support xattrs.
684f3a15685SAmir Goldstein 	 * Upper inodes won't have origin nor redirect xattr anyway.
685f3a15685SAmir Goldstein 	 */
686a0c236b1SAmir Goldstein 	err = ovl_check_setxattr(ofs, upperdentry, OVL_XATTR_IMPURE, "y", 1, 0);
687f3a15685SAmir Goldstein 	if (!err)
68813c72075SMiklos Szeredi 		ovl_set_flag(OVL_IMPURE, d_inode(dentry));
689f3a15685SAmir Goldstein 
690f3a15685SAmir Goldstein 	return err;
691f3a15685SAmir Goldstein }
69213c72075SMiklos Szeredi 
693096a218aSAmir Goldstein 
694096a218aSAmir Goldstein #define OVL_PROTATTR_MAX 32 /* Reserved for future flags */
695096a218aSAmir Goldstein 
696096a218aSAmir Goldstein void ovl_check_protattr(struct inode *inode, struct dentry *upper)
697096a218aSAmir Goldstein {
698096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
699096a218aSAmir Goldstein 	u32 iflags = inode->i_flags & OVL_PROT_I_FLAGS_MASK;
700096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX+1];
701096a218aSAmir Goldstein 	int res, n;
702096a218aSAmir Goldstein 
703dad7017aSChristian Brauner 	res = ovl_getxattr_upper(ofs, upper, OVL_XATTR_PROTATTR, buf,
704096a218aSAmir Goldstein 				 OVL_PROTATTR_MAX);
705096a218aSAmir Goldstein 	if (res < 0)
706096a218aSAmir Goldstein 		return;
707096a218aSAmir Goldstein 
708096a218aSAmir Goldstein 	/*
709096a218aSAmir Goldstein 	 * Initialize inode flags from overlay.protattr xattr and upper inode
710096a218aSAmir Goldstein 	 * flags.  If upper inode has those fileattr flags set (i.e. from old
711096a218aSAmir Goldstein 	 * kernel), we do not clear them on ovl_get_inode(), but we will clear
712096a218aSAmir Goldstein 	 * them on next fileattr_set().
713096a218aSAmir Goldstein 	 */
714096a218aSAmir Goldstein 	for (n = 0; n < res; n++) {
715096a218aSAmir Goldstein 		if (buf[n] == 'a')
716096a218aSAmir Goldstein 			iflags |= S_APPEND;
717096a218aSAmir Goldstein 		else if (buf[n] == 'i')
718096a218aSAmir Goldstein 			iflags |= S_IMMUTABLE;
719096a218aSAmir Goldstein 		else
720096a218aSAmir Goldstein 			break;
721096a218aSAmir Goldstein 	}
722096a218aSAmir Goldstein 
723096a218aSAmir Goldstein 	if (!res || n < res) {
724096a218aSAmir Goldstein 		pr_warn_ratelimited("incompatible overlay.protattr format (%pd2, len=%d)\n",
725096a218aSAmir Goldstein 				    upper, res);
726096a218aSAmir Goldstein 	} else {
727096a218aSAmir Goldstein 		inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
728096a218aSAmir Goldstein 	}
729096a218aSAmir Goldstein }
730096a218aSAmir Goldstein 
731096a218aSAmir Goldstein int ovl_set_protattr(struct inode *inode, struct dentry *upper,
732096a218aSAmir Goldstein 		      struct fileattr *fa)
733096a218aSAmir Goldstein {
734096a218aSAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
735096a218aSAmir Goldstein 	char buf[OVL_PROTATTR_MAX];
736096a218aSAmir Goldstein 	int len = 0, err = 0;
737096a218aSAmir Goldstein 	u32 iflags = 0;
738096a218aSAmir Goldstein 
739096a218aSAmir Goldstein 	BUILD_BUG_ON(HWEIGHT32(OVL_PROT_FS_FLAGS_MASK) > OVL_PROTATTR_MAX);
740096a218aSAmir Goldstein 
741096a218aSAmir Goldstein 	if (fa->flags & FS_APPEND_FL) {
742096a218aSAmir Goldstein 		buf[len++] = 'a';
743096a218aSAmir Goldstein 		iflags |= S_APPEND;
744096a218aSAmir Goldstein 	}
745096a218aSAmir Goldstein 	if (fa->flags & FS_IMMUTABLE_FL) {
746096a218aSAmir Goldstein 		buf[len++] = 'i';
747096a218aSAmir Goldstein 		iflags |= S_IMMUTABLE;
748096a218aSAmir Goldstein 	}
749096a218aSAmir Goldstein 
750096a218aSAmir Goldstein 	/*
751096a218aSAmir Goldstein 	 * Do not allow to set protection flags when upper doesn't support
752096a218aSAmir Goldstein 	 * xattrs, because we do not set those fileattr flags on upper inode.
753096a218aSAmir Goldstein 	 * Remove xattr if it exist and all protection flags are cleared.
754096a218aSAmir Goldstein 	 */
755096a218aSAmir Goldstein 	if (len) {
756096a218aSAmir Goldstein 		err = ovl_check_setxattr(ofs, upper, OVL_XATTR_PROTATTR,
757096a218aSAmir Goldstein 					 buf, len, -EPERM);
758096a218aSAmir Goldstein 	} else if (inode->i_flags & OVL_PROT_I_FLAGS_MASK) {
759c914c0e2SAmir Goldstein 		err = ovl_removexattr(ofs, upper, OVL_XATTR_PROTATTR);
760096a218aSAmir Goldstein 		if (err == -EOPNOTSUPP || err == -ENODATA)
761096a218aSAmir Goldstein 			err = 0;
762096a218aSAmir Goldstein 	}
763096a218aSAmir Goldstein 	if (err)
764096a218aSAmir Goldstein 		return err;
765096a218aSAmir Goldstein 
766096a218aSAmir Goldstein 	inode_set_flags(inode, iflags, OVL_PROT_I_FLAGS_MASK);
767096a218aSAmir Goldstein 
768096a218aSAmir Goldstein 	/* Mask out the fileattr flags that should not be set in upper inode */
769096a218aSAmir Goldstein 	fa->flags &= ~OVL_PROT_FS_FLAGS_MASK;
770096a218aSAmir Goldstein 	fa->fsx_xflags &= ~OVL_PROT_FSX_FLAGS_MASK;
771096a218aSAmir Goldstein 
772096a218aSAmir Goldstein 	return 0;
773096a218aSAmir Goldstein }
774096a218aSAmir Goldstein 
775ad0af710SAmir Goldstein /**
776ad0af710SAmir Goldstein  * Caller must hold a reference to inode to prevent it from being freed while
777ad0af710SAmir Goldstein  * it is marked inuse.
778ad0af710SAmir Goldstein  */
779ad0af710SAmir Goldstein bool ovl_inuse_trylock(struct dentry *dentry)
780ad0af710SAmir Goldstein {
781ad0af710SAmir Goldstein 	struct inode *inode = d_inode(dentry);
782ad0af710SAmir Goldstein 	bool locked = false;
783ad0af710SAmir Goldstein 
784ad0af710SAmir Goldstein 	spin_lock(&inode->i_lock);
785ad0af710SAmir Goldstein 	if (!(inode->i_state & I_OVL_INUSE)) {
786ad0af710SAmir Goldstein 		inode->i_state |= I_OVL_INUSE;
787ad0af710SAmir Goldstein 		locked = true;
788ad0af710SAmir Goldstein 	}
789ad0af710SAmir Goldstein 	spin_unlock(&inode->i_lock);
790ad0af710SAmir Goldstein 
791ad0af710SAmir Goldstein 	return locked;
792ad0af710SAmir Goldstein }
793ad0af710SAmir Goldstein 
794ad0af710SAmir Goldstein void ovl_inuse_unlock(struct dentry *dentry)
795ad0af710SAmir Goldstein {
796ad0af710SAmir Goldstein 	if (dentry) {
797ad0af710SAmir Goldstein 		struct inode *inode = d_inode(dentry);
798ad0af710SAmir Goldstein 
799ad0af710SAmir Goldstein 		spin_lock(&inode->i_lock);
800ad0af710SAmir Goldstein 		WARN_ON(!(inode->i_state & I_OVL_INUSE));
801ad0af710SAmir Goldstein 		inode->i_state &= ~I_OVL_INUSE;
802ad0af710SAmir Goldstein 		spin_unlock(&inode->i_lock);
803ad0af710SAmir Goldstein 	}
804ad0af710SAmir Goldstein }
8055f8415d6SAmir Goldstein 
806146d62e5SAmir Goldstein bool ovl_is_inuse(struct dentry *dentry)
807146d62e5SAmir Goldstein {
808146d62e5SAmir Goldstein 	struct inode *inode = d_inode(dentry);
809146d62e5SAmir Goldstein 	bool inuse;
810146d62e5SAmir Goldstein 
811146d62e5SAmir Goldstein 	spin_lock(&inode->i_lock);
812146d62e5SAmir Goldstein 	inuse = (inode->i_state & I_OVL_INUSE);
813146d62e5SAmir Goldstein 	spin_unlock(&inode->i_lock);
814146d62e5SAmir Goldstein 
815146d62e5SAmir Goldstein 	return inuse;
816146d62e5SAmir Goldstein }
817146d62e5SAmir Goldstein 
81824b33ee1SAmir Goldstein /*
81924b33ee1SAmir Goldstein  * Does this overlay dentry need to be indexed on copy up?
82024b33ee1SAmir Goldstein  */
82124b33ee1SAmir Goldstein bool ovl_need_index(struct dentry *dentry)
82224b33ee1SAmir Goldstein {
82324b33ee1SAmir Goldstein 	struct dentry *lower = ovl_dentry_lower(dentry);
82424b33ee1SAmir Goldstein 
82524b33ee1SAmir Goldstein 	if (!lower || !ovl_indexdir(dentry->d_sb))
82624b33ee1SAmir Goldstein 		return false;
82724b33ee1SAmir Goldstein 
828fbd2d207SAmir Goldstein 	/* Index all files for NFS export and consistency verification */
829016b720fSAmir Goldstein 	if (ovl_index_all(dentry->d_sb))
830fbd2d207SAmir Goldstein 		return true;
831fbd2d207SAmir Goldstein 
83224b33ee1SAmir Goldstein 	/* Index only lower hardlinks on copy up */
83324b33ee1SAmir Goldstein 	if (!d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
83424b33ee1SAmir Goldstein 		return true;
83524b33ee1SAmir Goldstein 
83624b33ee1SAmir Goldstein 	return false;
83724b33ee1SAmir Goldstein }
83824b33ee1SAmir Goldstein 
8399f4ec904SAmir Goldstein /* Caller must hold OVL_I(inode)->lock */
840caf70cb2SAmir Goldstein static void ovl_cleanup_index(struct dentry *dentry)
841caf70cb2SAmir Goldstein {
8421cdb0cb6SPavel Tikhomirov 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
843e7dd0e71SAmir Goldstein 	struct dentry *indexdir = ovl_indexdir(dentry->d_sb);
844e7dd0e71SAmir Goldstein 	struct inode *dir = indexdir->d_inode;
845caf70cb2SAmir Goldstein 	struct dentry *lowerdentry = ovl_dentry_lower(dentry);
846caf70cb2SAmir Goldstein 	struct dentry *upperdentry = ovl_dentry_upper(dentry);
847caf70cb2SAmir Goldstein 	struct dentry *index = NULL;
848caf70cb2SAmir Goldstein 	struct inode *inode;
84963e13252SAmir Goldstein 	struct qstr name = { };
850caf70cb2SAmir Goldstein 	int err;
851caf70cb2SAmir Goldstein 
8521cdb0cb6SPavel Tikhomirov 	err = ovl_get_index_name(ofs, lowerdentry, &name);
853caf70cb2SAmir Goldstein 	if (err)
854caf70cb2SAmir Goldstein 		goto fail;
855caf70cb2SAmir Goldstein 
856caf70cb2SAmir Goldstein 	inode = d_inode(upperdentry);
85789a17556SAmir Goldstein 	if (!S_ISDIR(inode->i_mode) && inode->i_nlink != 1) {
8581bd0a3aeSlijiazi 		pr_warn_ratelimited("cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
859caf70cb2SAmir Goldstein 				    upperdentry, inode->i_ino, inode->i_nlink);
860caf70cb2SAmir Goldstein 		/*
861caf70cb2SAmir Goldstein 		 * We either have a bug with persistent union nlink or a lower
862caf70cb2SAmir Goldstein 		 * hardlink was added while overlay is mounted. Adding a lower
863caf70cb2SAmir Goldstein 		 * hardlink and then unlinking all overlay hardlinks would drop
864caf70cb2SAmir Goldstein 		 * overlay nlink to zero before all upper inodes are unlinked.
865caf70cb2SAmir Goldstein 		 * As a safety measure, when that situation is detected, set
866caf70cb2SAmir Goldstein 		 * the overlay nlink to the index inode nlink minus one for the
867caf70cb2SAmir Goldstein 		 * index entry itself.
868caf70cb2SAmir Goldstein 		 */
869caf70cb2SAmir Goldstein 		set_nlink(d_inode(dentry), inode->i_nlink - 1);
870caf70cb2SAmir Goldstein 		ovl_set_nlink_upper(dentry);
871caf70cb2SAmir Goldstein 		goto out;
872caf70cb2SAmir Goldstein 	}
873caf70cb2SAmir Goldstein 
874caf70cb2SAmir Goldstein 	inode_lock_nested(dir, I_MUTEX_PARENT);
87522f289ceSChristian Brauner 	index = ovl_lookup_upper(ofs, name.name, indexdir, name.len);
876caf70cb2SAmir Goldstein 	err = PTR_ERR(index);
877e7dd0e71SAmir Goldstein 	if (IS_ERR(index)) {
8789f4ec904SAmir Goldstein 		index = NULL;
879e7dd0e71SAmir Goldstein 	} else if (ovl_index_all(dentry->d_sb)) {
880e7dd0e71SAmir Goldstein 		/* Whiteout orphan index to block future open by handle */
881c21c839bSChengguang Xu 		err = ovl_cleanup_and_whiteout(OVL_FS(dentry->d_sb),
882c21c839bSChengguang Xu 					       dir, index);
883e7dd0e71SAmir Goldstein 	} else {
884e7dd0e71SAmir Goldstein 		/* Cleanup orphan index entries */
885576bb263SChristian Brauner 		err = ovl_cleanup(ofs, dir, index);
886e7dd0e71SAmir Goldstein 	}
8879f4ec904SAmir Goldstein 
888caf70cb2SAmir Goldstein 	inode_unlock(dir);
889caf70cb2SAmir Goldstein 	if (err)
890caf70cb2SAmir Goldstein 		goto fail;
891caf70cb2SAmir Goldstein 
892caf70cb2SAmir Goldstein out:
89363e13252SAmir Goldstein 	kfree(name.name);
894caf70cb2SAmir Goldstein 	dput(index);
895caf70cb2SAmir Goldstein 	return;
896caf70cb2SAmir Goldstein 
897caf70cb2SAmir Goldstein fail:
8981bd0a3aeSlijiazi 	pr_err("cleanup index of '%pd2' failed (%i)\n", dentry, err);
899caf70cb2SAmir Goldstein 	goto out;
900caf70cb2SAmir Goldstein }
901caf70cb2SAmir Goldstein 
9025f8415d6SAmir Goldstein /*
9035f8415d6SAmir Goldstein  * Operations that change overlay inode and upper inode nlink need to be
9045f8415d6SAmir Goldstein  * synchronized with copy up for persistent nlink accounting.
9055f8415d6SAmir Goldstein  */
9060e32992fSAmir Goldstein int ovl_nlink_start(struct dentry *dentry)
9075f8415d6SAmir Goldstein {
9081e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
9095f8415d6SAmir Goldstein 	const struct cred *old_cred;
9105f8415d6SAmir Goldstein 	int err;
9115f8415d6SAmir Goldstein 
9121e92e307SAmir Goldstein 	if (WARN_ON(!inode))
9130e32992fSAmir Goldstein 		return -ENOENT;
9145f8415d6SAmir Goldstein 
9155f8415d6SAmir Goldstein 	/*
9165f8415d6SAmir Goldstein 	 * With inodes index is enabled, we store the union overlay nlink
91724b33ee1SAmir Goldstein 	 * in an xattr on the index inode. When whiting out an indexed lower,
9185f8415d6SAmir Goldstein 	 * we need to decrement the overlay persistent nlink, but before the
9195f8415d6SAmir Goldstein 	 * first copy up, we have no upper index inode to store the xattr.
9205f8415d6SAmir Goldstein 	 *
92124b33ee1SAmir Goldstein 	 * As a workaround, before whiteout/rename over an indexed lower,
9225f8415d6SAmir Goldstein 	 * copy up to create the upper index. Creating the upper index will
9235f8415d6SAmir Goldstein 	 * initialize the overlay nlink, so it could be dropped if unlink
9245f8415d6SAmir Goldstein 	 * or rename succeeds.
9255f8415d6SAmir Goldstein 	 *
9265f8415d6SAmir Goldstein 	 * TODO: implement metadata only index copy up when called with
9275f8415d6SAmir Goldstein 	 *       ovl_copy_up_flags(dentry, O_PATH).
9285f8415d6SAmir Goldstein 	 */
92924b33ee1SAmir Goldstein 	if (ovl_need_index(dentry) && !ovl_dentry_has_upper_alias(dentry)) {
9305f8415d6SAmir Goldstein 		err = ovl_copy_up(dentry);
9315f8415d6SAmir Goldstein 		if (err)
9325f8415d6SAmir Goldstein 			return err;
9335f8415d6SAmir Goldstein 	}
9345f8415d6SAmir Goldstein 
935531d3040SAmir Goldstein 	err = ovl_inode_lock_interruptible(inode);
9365f8415d6SAmir Goldstein 	if (err)
9375f8415d6SAmir Goldstein 		return err;
9385f8415d6SAmir Goldstein 
9391e92e307SAmir Goldstein 	if (d_is_dir(dentry) || !ovl_test_flag(OVL_INDEX, inode))
9405f8415d6SAmir Goldstein 		goto out;
9415f8415d6SAmir Goldstein 
9425f8415d6SAmir Goldstein 	old_cred = ovl_override_creds(dentry->d_sb);
9435f8415d6SAmir Goldstein 	/*
9445f8415d6SAmir Goldstein 	 * The overlay inode nlink should be incremented/decremented IFF the
9455f8415d6SAmir Goldstein 	 * upper operation succeeds, along with nlink change of upper inode.
9465f8415d6SAmir Goldstein 	 * Therefore, before link/unlink/rename, we store the union nlink
9475f8415d6SAmir Goldstein 	 * value relative to the upper inode nlink in an upper inode xattr.
9485f8415d6SAmir Goldstein 	 */
9495f8415d6SAmir Goldstein 	err = ovl_set_nlink_upper(dentry);
9505f8415d6SAmir Goldstein 	revert_creds(old_cred);
9515f8415d6SAmir Goldstein 
9525f8415d6SAmir Goldstein out:
9535f8415d6SAmir Goldstein 	if (err)
9541e92e307SAmir Goldstein 		ovl_inode_unlock(inode);
9555f8415d6SAmir Goldstein 
9565f8415d6SAmir Goldstein 	return err;
9575f8415d6SAmir Goldstein }
9585f8415d6SAmir Goldstein 
9590e32992fSAmir Goldstein void ovl_nlink_end(struct dentry *dentry)
9605f8415d6SAmir Goldstein {
9611e92e307SAmir Goldstein 	struct inode *inode = d_inode(dentry);
9621e92e307SAmir Goldstein 
9631e92e307SAmir Goldstein 	if (ovl_test_flag(OVL_INDEX, inode) && inode->i_nlink == 0) {
964caf70cb2SAmir Goldstein 		const struct cred *old_cred;
965caf70cb2SAmir Goldstein 
966caf70cb2SAmir Goldstein 		old_cred = ovl_override_creds(dentry->d_sb);
967caf70cb2SAmir Goldstein 		ovl_cleanup_index(dentry);
968caf70cb2SAmir Goldstein 		revert_creds(old_cred);
969caf70cb2SAmir Goldstein 	}
970caf70cb2SAmir Goldstein 
9711e92e307SAmir Goldstein 	ovl_inode_unlock(inode);
9725f8415d6SAmir Goldstein }
9735820dc08SAmir Goldstein 
9745820dc08SAmir Goldstein int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
9755820dc08SAmir Goldstein {
9765820dc08SAmir Goldstein 	/* Workdir should not be the same as upperdir */
9775820dc08SAmir Goldstein 	if (workdir == upperdir)
9785820dc08SAmir Goldstein 		goto err;
9795820dc08SAmir Goldstein 
9805820dc08SAmir Goldstein 	/* Workdir should not be subdir of upperdir and vice versa */
9815820dc08SAmir Goldstein 	if (lock_rename(workdir, upperdir) != NULL)
9825820dc08SAmir Goldstein 		goto err_unlock;
9835820dc08SAmir Goldstein 
9845820dc08SAmir Goldstein 	return 0;
9855820dc08SAmir Goldstein 
9865820dc08SAmir Goldstein err_unlock:
9875820dc08SAmir Goldstein 	unlock_rename(workdir, upperdir);
9885820dc08SAmir Goldstein err:
9891bd0a3aeSlijiazi 	pr_err("failed to lock workdir+upperdir\n");
9905820dc08SAmir Goldstein 	return -EIO;
9915820dc08SAmir Goldstein }
9929d3dfea3SVivek Goyal 
9939d3dfea3SVivek Goyal /* err < 0, 0 if no metacopy xattr, 1 if metacopy xattr found */
9942d343087SAl Viro int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path)
9959d3dfea3SVivek Goyal {
9969d3dfea3SVivek Goyal 	int res;
9979d3dfea3SVivek Goyal 
9989d3dfea3SVivek Goyal 	/* Only regular files can have metacopy xattr */
999dad7017aSChristian Brauner 	if (!S_ISREG(d_inode(path->dentry)->i_mode))
10009d3dfea3SVivek Goyal 		return 0;
10019d3dfea3SVivek Goyal 
1002dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_METACOPY, NULL, 0);
10039d3dfea3SVivek Goyal 	if (res < 0) {
10049d3dfea3SVivek Goyal 		if (res == -ENODATA || res == -EOPNOTSUPP)
10059d3dfea3SVivek Goyal 			return 0;
100687b2c60cSMiklos Szeredi 		/*
100787b2c60cSMiklos Szeredi 		 * getxattr on user.* may fail with EACCES in case there's no
100887b2c60cSMiklos Szeredi 		 * read permission on the inode.  Not much we can do, other than
100987b2c60cSMiklos Szeredi 		 * tell the caller that this is not a metacopy inode.
101087b2c60cSMiklos Szeredi 		 */
101187b2c60cSMiklos Szeredi 		if (ofs->config.userxattr && res == -EACCES)
101287b2c60cSMiklos Szeredi 			return 0;
10139d3dfea3SVivek Goyal 		goto out;
10149d3dfea3SVivek Goyal 	}
10159d3dfea3SVivek Goyal 
10169d3dfea3SVivek Goyal 	return 1;
10179d3dfea3SVivek Goyal out:
10181bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to get metacopy (%i)\n", res);
10199d3dfea3SVivek Goyal 	return res;
10209d3dfea3SVivek Goyal }
102167d756c2SVivek Goyal 
102267d756c2SVivek Goyal bool ovl_is_metacopy_dentry(struct dentry *dentry)
102367d756c2SVivek Goyal {
1024a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
102567d756c2SVivek Goyal 
102667d756c2SVivek Goyal 	if (!d_is_reg(dentry))
102767d756c2SVivek Goyal 		return false;
102867d756c2SVivek Goyal 
102967d756c2SVivek Goyal 	if (ovl_dentry_upper(dentry)) {
103067d756c2SVivek Goyal 		if (!ovl_has_upperdata(d_inode(dentry)))
103167d756c2SVivek Goyal 			return true;
103267d756c2SVivek Goyal 		return false;
103367d756c2SVivek Goyal 	}
103467d756c2SVivek Goyal 
1035*5522c9c7SAmir Goldstein 	return (ovl_numlower(oe) > 1);
103667d756c2SVivek Goyal }
10370a2d0d3fSVivek Goyal 
10382d343087SAl Viro char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding)
10390a2d0d3fSVivek Goyal {
10400a2d0d3fSVivek Goyal 	int res;
10410a2d0d3fSVivek Goyal 	char *s, *next, *buf = NULL;
10420a2d0d3fSVivek Goyal 
1043dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, NULL, 0);
104492f0d6c9SMiklos Szeredi 	if (res == -ENODATA || res == -EOPNOTSUPP)
10450a2d0d3fSVivek Goyal 		return NULL;
10460a2d0d3fSVivek Goyal 	if (res < 0)
104792f0d6c9SMiklos Szeredi 		goto fail;
104892f0d6c9SMiklos Szeredi 	if (res == 0)
104992f0d6c9SMiklos Szeredi 		goto invalid;
105092f0d6c9SMiklos Szeredi 
105192f0d6c9SMiklos Szeredi 	buf = kzalloc(res + padding + 1, GFP_KERNEL);
105292f0d6c9SMiklos Szeredi 	if (!buf)
105392f0d6c9SMiklos Szeredi 		return ERR_PTR(-ENOMEM);
105492f0d6c9SMiklos Szeredi 
1055dad7017aSChristian Brauner 	res = ovl_path_getxattr(ofs, path, OVL_XATTR_REDIRECT, buf, res);
105692f0d6c9SMiklos Szeredi 	if (res < 0)
105792f0d6c9SMiklos Szeredi 		goto fail;
10580a2d0d3fSVivek Goyal 	if (res == 0)
10590a2d0d3fSVivek Goyal 		goto invalid;
10600a2d0d3fSVivek Goyal 
10610a2d0d3fSVivek Goyal 	if (buf[0] == '/') {
10620a2d0d3fSVivek Goyal 		for (s = buf; *s++ == '/'; s = next) {
10630a2d0d3fSVivek Goyal 			next = strchrnul(s, '/');
10640a2d0d3fSVivek Goyal 			if (s == next)
10650a2d0d3fSVivek Goyal 				goto invalid;
10660a2d0d3fSVivek Goyal 		}
10670a2d0d3fSVivek Goyal 	} else {
10680a2d0d3fSVivek Goyal 		if (strchr(buf, '/') != NULL)
10690a2d0d3fSVivek Goyal 			goto invalid;
10700a2d0d3fSVivek Goyal 	}
10710a2d0d3fSVivek Goyal 
10720a2d0d3fSVivek Goyal 	return buf;
10730a2d0d3fSVivek Goyal invalid:
10741bd0a3aeSlijiazi 	pr_warn_ratelimited("invalid redirect (%s)\n", buf);
10750a2d0d3fSVivek Goyal 	res = -EINVAL;
107692f0d6c9SMiklos Szeredi 	goto err_free;
107792f0d6c9SMiklos Szeredi fail:
107892f0d6c9SMiklos Szeredi 	pr_warn_ratelimited("failed to get redirect (%i)\n", res);
107992f0d6c9SMiklos Szeredi err_free:
1080993a0b2aSVivek Goyal 	kfree(buf);
1081993a0b2aSVivek Goyal 	return ERR_PTR(res);
10820a2d0d3fSVivek Goyal }
1083335d3fc5SSargun Dhillon 
1084335d3fc5SSargun Dhillon /*
1085335d3fc5SSargun Dhillon  * ovl_sync_status() - Check fs sync status for volatile mounts
1086335d3fc5SSargun Dhillon  *
1087335d3fc5SSargun Dhillon  * Returns 1 if this is not a volatile mount and a real sync is required.
1088335d3fc5SSargun Dhillon  *
1089335d3fc5SSargun Dhillon  * Returns 0 if syncing can be skipped because mount is volatile, and no errors
1090335d3fc5SSargun Dhillon  * have occurred on the upperdir since the mount.
1091335d3fc5SSargun Dhillon  *
1092335d3fc5SSargun Dhillon  * Returns -errno if it is a volatile mount, and the error that occurred since
1093335d3fc5SSargun Dhillon  * the last mount. If the error code changes, it'll return the latest error
1094335d3fc5SSargun Dhillon  * code.
1095335d3fc5SSargun Dhillon  */
1096335d3fc5SSargun Dhillon 
1097335d3fc5SSargun Dhillon int ovl_sync_status(struct ovl_fs *ofs)
1098335d3fc5SSargun Dhillon {
1099335d3fc5SSargun Dhillon 	struct vfsmount *mnt;
1100335d3fc5SSargun Dhillon 
1101335d3fc5SSargun Dhillon 	if (ovl_should_sync(ofs))
1102335d3fc5SSargun Dhillon 		return 1;
1103335d3fc5SSargun Dhillon 
1104335d3fc5SSargun Dhillon 	mnt = ovl_upper_mnt(ofs);
1105335d3fc5SSargun Dhillon 	if (!mnt)
1106335d3fc5SSargun Dhillon 		return 0;
1107335d3fc5SSargun Dhillon 
1108335d3fc5SSargun Dhillon 	return errseq_check(&mnt->mnt_sb->s_wb_err, ofs->errseq);
1109335d3fc5SSargun Dhillon }
11102878dffcSChristian Brauner 
11112878dffcSChristian Brauner /*
11122878dffcSChristian Brauner  * ovl_copyattr() - copy inode attributes from layer to ovl inode
11132878dffcSChristian Brauner  *
11142878dffcSChristian Brauner  * When overlay copies inode information from an upper or lower layer to the
11152878dffcSChristian Brauner  * relevant overlay inode it will apply the idmapping of the upper or lower
11162878dffcSChristian Brauner  * layer when doing so ensuring that the ovl inode ownership will correctly
11172878dffcSChristian Brauner  * reflect the ownership of the idmapped upper or lower layer. For example, an
11182878dffcSChristian Brauner  * idmapped upper or lower layer mapping id 1001 to id 1000 will take care to
11192878dffcSChristian Brauner  * map any lower or upper inode owned by id 1001 to id 1000. These mapping
11202878dffcSChristian Brauner  * helpers are nops when the relevant layer isn't idmapped.
11212878dffcSChristian Brauner  */
11222878dffcSChristian Brauner void ovl_copyattr(struct inode *inode)
11232878dffcSChristian Brauner {
11242878dffcSChristian Brauner 	struct path realpath;
11252878dffcSChristian Brauner 	struct inode *realinode;
1126e67fe633SChristian Brauner 	struct mnt_idmap *real_idmap;
112773db6a06SChristian Brauner 	vfsuid_t vfsuid;
112873db6a06SChristian Brauner 	vfsgid_t vfsgid;
11292878dffcSChristian Brauner 
1130b2dd05f1SZhihao Cheng 	realinode = ovl_i_path_real(inode, &realpath);
1131e67fe633SChristian Brauner 	real_idmap = mnt_idmap(realpath.mnt);
11322878dffcSChristian Brauner 
1133e67fe633SChristian Brauner 	vfsuid = i_uid_into_vfsuid(real_idmap, realinode);
1134e67fe633SChristian Brauner 	vfsgid = i_gid_into_vfsgid(real_idmap, realinode);
113573db6a06SChristian Brauner 
113673db6a06SChristian Brauner 	inode->i_uid = vfsuid_into_kuid(vfsuid);
113773db6a06SChristian Brauner 	inode->i_gid = vfsgid_into_kgid(vfsgid);
11382878dffcSChristian Brauner 	inode->i_mode = realinode->i_mode;
11392878dffcSChristian Brauner 	inode->i_atime = realinode->i_atime;
11402878dffcSChristian Brauner 	inode->i_mtime = realinode->i_mtime;
11412878dffcSChristian Brauner 	inode->i_ctime = realinode->i_ctime;
11422878dffcSChristian Brauner 	i_size_write(inode, i_size_read(realinode));
11432878dffcSChristian Brauner }
1144