xref: /openbmc/linux/fs/overlayfs/export.c (revision aad29a73199b7fbccfbabea3f1ee627ad1924f52)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
28ed5eec9SAmir Goldstein /*
38ed5eec9SAmir Goldstein  * Overlayfs NFS export support.
48ed5eec9SAmir Goldstein  *
58ed5eec9SAmir Goldstein  * Amir Goldstein <amir73il@gmail.com>
68ed5eec9SAmir Goldstein  *
78ed5eec9SAmir Goldstein  * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
88ed5eec9SAmir Goldstein  */
98ed5eec9SAmir Goldstein 
108ed5eec9SAmir Goldstein #include <linux/fs.h>
118ed5eec9SAmir Goldstein #include <linux/cred.h>
128ed5eec9SAmir Goldstein #include <linux/mount.h>
138ed5eec9SAmir Goldstein #include <linux/namei.h>
148ed5eec9SAmir Goldstein #include <linux/xattr.h>
158ed5eec9SAmir Goldstein #include <linux/exportfs.h>
168ed5eec9SAmir Goldstein #include <linux/ratelimit.h>
178ed5eec9SAmir Goldstein #include "overlayfs.h"
188ed5eec9SAmir Goldstein 
ovl_encode_maybe_copy_up(struct dentry * dentry)192ca3c148SAmir Goldstein static int ovl_encode_maybe_copy_up(struct dentry *dentry)
202ca3c148SAmir Goldstein {
212ca3c148SAmir Goldstein 	int err;
222ca3c148SAmir Goldstein 
232ca3c148SAmir Goldstein 	if (ovl_dentry_upper(dentry))
242ca3c148SAmir Goldstein 		return 0;
252ca3c148SAmir Goldstein 
262ca3c148SAmir Goldstein 	err = ovl_want_write(dentry);
272ca3c148SAmir Goldstein 	if (!err) {
282ca3c148SAmir Goldstein 		err = ovl_copy_up(dentry);
292ca3c148SAmir Goldstein 		ovl_drop_write(dentry);
302ca3c148SAmir Goldstein 	}
312ca3c148SAmir Goldstein 
322ca3c148SAmir Goldstein 	if (err) {
331bd0a3aeSlijiazi 		pr_warn_ratelimited("failed to copy up on encode (%pd2, err=%i)\n",
342ca3c148SAmir Goldstein 				    dentry, err);
352ca3c148SAmir Goldstein 	}
362ca3c148SAmir Goldstein 
372ca3c148SAmir Goldstein 	return err;
382ca3c148SAmir Goldstein }
392ca3c148SAmir Goldstein 
402ca3c148SAmir Goldstein /*
412ca3c148SAmir Goldstein  * Before encoding a non-upper directory file handle from real layer N, we need
422ca3c148SAmir Goldstein  * to check if it will be possible to reconnect an overlay dentry from the real
432ca3c148SAmir Goldstein  * lower decoded dentry. This is done by following the overlay ancestry up to a
442ca3c148SAmir Goldstein  * "layer N connected" ancestor and verifying that all parents along the way are
452ca3c148SAmir Goldstein  * "layer N connectable". If an ancestor that is NOT "layer N connectable" is
462ca3c148SAmir Goldstein  * found, we need to copy up an ancestor, which is "layer N connectable", thus
472ca3c148SAmir Goldstein  * making that ancestor "layer N connected". For example:
482ca3c148SAmir Goldstein  *
492ca3c148SAmir Goldstein  * layer 1: /a
502ca3c148SAmir Goldstein  * layer 2: /a/b/c
512ca3c148SAmir Goldstein  *
522ca3c148SAmir Goldstein  * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is
532ca3c148SAmir Goldstein  * copied up and renamed, upper dir /a will be indexed by lower dir /a from
542ca3c148SAmir Goldstein  * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*)
552ca3c148SAmir Goldstein  * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay
562ca3c148SAmir Goldstein  * dentry from the connected lower dentry /a/b/c.
572ca3c148SAmir Goldstein  *
582ca3c148SAmir Goldstein  * To avoid this problem on decode time, we need to copy up an ancestor of
592ca3c148SAmir Goldstein  * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is
602ca3c148SAmir Goldstein  * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected"
612ca3c148SAmir Goldstein  * and when the time comes to decode the file handle from lower dentry /a/b/c,
622ca3c148SAmir Goldstein  * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding
632ca3c148SAmir Goldstein  * a connected overlay dentry will be accomplished.
642ca3c148SAmir Goldstein  *
652ca3c148SAmir Goldstein  * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an
662ca3c148SAmir Goldstein  * entry /a in the lower layers above layer N and find the indexed dir /a from
672ca3c148SAmir Goldstein  * layer 1. If that improvement is made, then the check for "layer N connected"
682ca3c148SAmir Goldstein  * will need to verify there are no redirects in lower layers above N. In the
692ca3c148SAmir Goldstein  * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a
702ca3c148SAmir Goldstein  * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable":
712ca3c148SAmir Goldstein  *
722ca3c148SAmir Goldstein  * layer 1: /A (redirect = /a)
732ca3c148SAmir Goldstein  * layer 2: /a/b/c
742ca3c148SAmir Goldstein  */
752ca3c148SAmir Goldstein 
762ca3c148SAmir Goldstein /* Return the lowest layer for encoding a connectable file handle */
ovl_connectable_layer(struct dentry * dentry)772ca3c148SAmir Goldstein static int ovl_connectable_layer(struct dentry *dentry)
782ca3c148SAmir Goldstein {
792ca3c148SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
802ca3c148SAmir Goldstein 
812ca3c148SAmir Goldstein 	/* We can get overlay root from root of any layer */
822ca3c148SAmir Goldstein 	if (dentry == dentry->d_sb->s_root)
835522c9c7SAmir Goldstein 		return ovl_numlower(oe);
842ca3c148SAmir Goldstein 
852ca3c148SAmir Goldstein 	/*
862ca3c148SAmir Goldstein 	 * If it's an unindexed merge dir, then it's not connectable with any
872ca3c148SAmir Goldstein 	 * lower layer
882ca3c148SAmir Goldstein 	 */
892ca3c148SAmir Goldstein 	if (ovl_dentry_upper(dentry) &&
902ca3c148SAmir Goldstein 	    !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
912ca3c148SAmir Goldstein 		return 0;
922ca3c148SAmir Goldstein 
932ca3c148SAmir Goldstein 	/* We can get upper/overlay path from indexed/lower dentry */
945522c9c7SAmir Goldstein 	return ovl_lowerstack(oe)->layer->idx;
952ca3c148SAmir Goldstein }
962ca3c148SAmir Goldstein 
972ca3c148SAmir Goldstein /*
982ca3c148SAmir Goldstein  * @dentry is "connected" if all ancestors up to root or a "connected" ancestor
992ca3c148SAmir Goldstein  * have the same uppermost lower layer as the origin's layer. We may need to
1002ca3c148SAmir Goldstein  * copy up a "connectable" ancestor to make it "connected". A "connected" dentry
1012ca3c148SAmir Goldstein  * cannot become non "connected", so cache positive result in dentry flags.
1022ca3c148SAmir Goldstein  *
1032ca3c148SAmir Goldstein  * Return the connected origin layer or < 0 on error.
1042ca3c148SAmir Goldstein  */
ovl_connect_layer(struct dentry * dentry)1052ca3c148SAmir Goldstein static int ovl_connect_layer(struct dentry *dentry)
1062ca3c148SAmir Goldstein {
1072ca3c148SAmir Goldstein 	struct dentry *next, *parent = NULL;
1085522c9c7SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
1092ca3c148SAmir Goldstein 	int origin_layer;
1102ca3c148SAmir Goldstein 	int err = 0;
1112ca3c148SAmir Goldstein 
1122ca3c148SAmir Goldstein 	if (WARN_ON(dentry == dentry->d_sb->s_root) ||
1132ca3c148SAmir Goldstein 	    WARN_ON(!ovl_dentry_lower(dentry)))
1142ca3c148SAmir Goldstein 		return -EIO;
1152ca3c148SAmir Goldstein 
1165522c9c7SAmir Goldstein 	origin_layer = ovl_lowerstack(oe)->layer->idx;
1172ca3c148SAmir Goldstein 	if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
1182ca3c148SAmir Goldstein 		return origin_layer;
1192ca3c148SAmir Goldstein 
1202ca3c148SAmir Goldstein 	/* Find the topmost origin layer connectable ancestor of @dentry */
1212ca3c148SAmir Goldstein 	next = dget(dentry);
1222ca3c148SAmir Goldstein 	for (;;) {
1232ca3c148SAmir Goldstein 		parent = dget_parent(next);
1242ca3c148SAmir Goldstein 		if (WARN_ON(parent == next)) {
1252ca3c148SAmir Goldstein 			err = -EIO;
1262ca3c148SAmir Goldstein 			break;
1272ca3c148SAmir Goldstein 		}
1282ca3c148SAmir Goldstein 
1292ca3c148SAmir Goldstein 		/*
1302ca3c148SAmir Goldstein 		 * If @parent is not origin layer connectable, then copy up
1312ca3c148SAmir Goldstein 		 * @next which is origin layer connectable and we are done.
1322ca3c148SAmir Goldstein 		 */
1332ca3c148SAmir Goldstein 		if (ovl_connectable_layer(parent) < origin_layer) {
1342ca3c148SAmir Goldstein 			err = ovl_encode_maybe_copy_up(next);
1352ca3c148SAmir Goldstein 			break;
1362ca3c148SAmir Goldstein 		}
1372ca3c148SAmir Goldstein 
1382ca3c148SAmir Goldstein 		/* If @parent is connected or indexed we are done */
1392ca3c148SAmir Goldstein 		if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
1402ca3c148SAmir Goldstein 		    ovl_test_flag(OVL_INDEX, d_inode(parent)))
1412ca3c148SAmir Goldstein 			break;
1422ca3c148SAmir Goldstein 
1432ca3c148SAmir Goldstein 		dput(next);
1442ca3c148SAmir Goldstein 		next = parent;
1452ca3c148SAmir Goldstein 	}
1462ca3c148SAmir Goldstein 
1472ca3c148SAmir Goldstein 	dput(parent);
1482ca3c148SAmir Goldstein 	dput(next);
1492ca3c148SAmir Goldstein 
1502ca3c148SAmir Goldstein 	if (!err)
1512ca3c148SAmir Goldstein 		ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
1522ca3c148SAmir Goldstein 
1532ca3c148SAmir Goldstein 	return err ?: origin_layer;
1542ca3c148SAmir Goldstein }
1552ca3c148SAmir Goldstein 
156b305e844SAmir Goldstein /*
157b305e844SAmir Goldstein  * We only need to encode origin if there is a chance that the same object was
158b305e844SAmir Goldstein  * encoded pre copy up and then we need to stay consistent with the same
159b305e844SAmir Goldstein  * encoding also after copy up. If non-pure upper is not indexed, then it was
160b305e844SAmir Goldstein  * copied up before NFS export was enabled. In that case we don't need to worry
161b305e844SAmir Goldstein  * about staying consistent with pre copy up encoding and we encode an upper
162b305e844SAmir Goldstein  * file handle. Overlay root dentry is a private case of non-indexed upper.
163b305e844SAmir Goldstein  *
164b305e844SAmir Goldstein  * The following table summarizes the different file handle encodings used for
165b305e844SAmir Goldstein  * different overlay object types:
166b305e844SAmir Goldstein  *
167b305e844SAmir Goldstein  *  Object type		| Encoding
168b305e844SAmir Goldstein  * --------------------------------
169b305e844SAmir Goldstein  *  Pure upper		| U
170b305e844SAmir Goldstein  *  Non-indexed upper	| U
17105e1f118SAmir Goldstein  *  Indexed upper	| L (*)
17205e1f118SAmir Goldstein  *  Non-upper		| L (*)
173b305e844SAmir Goldstein  *
174b305e844SAmir Goldstein  * U = upper file handle
175b305e844SAmir Goldstein  * L = lower file handle
17605e1f118SAmir Goldstein  *
17716aac5adSAmir Goldstein  * (*) Decoding a connected overlay dir from real lower dentry is not always
1782ca3c148SAmir Goldstein  * possible when there are redirects in lower layers and non-indexed merge dirs.
1792ca3c148SAmir Goldstein  * To mitigate those case, we may copy up the lower dir ancestor before encode
18016aac5adSAmir Goldstein  * of a decodable file handle for non-upper dir.
1812ca3c148SAmir Goldstein  *
1822ca3c148SAmir Goldstein  * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
183b305e844SAmir Goldstein  */
ovl_check_encode_origin(struct inode * inode)184*f0c0ac84SAmir Goldstein static int ovl_check_encode_origin(struct inode *inode)
185b305e844SAmir Goldstein {
186*f0c0ac84SAmir Goldstein 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
18716aac5adSAmir Goldstein 	bool decodable = ofs->config.nfs_export;
188*f0c0ac84SAmir Goldstein 	struct dentry *dentry;
189*f0c0ac84SAmir Goldstein 	int err;
19016aac5adSAmir Goldstein 
19110252816SMiklos Szeredi 	/* No upper layer? */
19210252816SMiklos Szeredi 	if (!ovl_upper_mnt(ofs))
19310252816SMiklos Szeredi 		return 1;
19410252816SMiklos Szeredi 
19516aac5adSAmir Goldstein 	/* Lower file handle for non-upper non-decodable */
196*f0c0ac84SAmir Goldstein 	if (!ovl_inode_upper(inode) && !decodable)
197c7242a45SAmir Goldstein 		return 1;
19805e1f118SAmir Goldstein 
1992ca3c148SAmir Goldstein 	/* Upper file handle for pure upper */
200*f0c0ac84SAmir Goldstein 	if (!ovl_inode_lower(inode))
20105e1f118SAmir Goldstein 		return 0;
20205e1f118SAmir Goldstein 
2032ca3c148SAmir Goldstein 	/*
2042ca3c148SAmir Goldstein 	 * Root is never indexed, so if there's an upper layer, encode upper for
2052ca3c148SAmir Goldstein 	 * root.
2062ca3c148SAmir Goldstein 	 */
207*f0c0ac84SAmir Goldstein 	if (inode == d_inode(inode->i_sb->s_root))
20816aac5adSAmir Goldstein 		return 0;
20916aac5adSAmir Goldstein 
21016aac5adSAmir Goldstein 	/*
21116aac5adSAmir Goldstein 	 * Upper decodable file handle for non-indexed upper.
21216aac5adSAmir Goldstein 	 */
213*f0c0ac84SAmir Goldstein 	if (ovl_inode_upper(inode) && decodable &&
214*f0c0ac84SAmir Goldstein 	    !ovl_test_flag(OVL_INDEX, inode))
2152ca3c148SAmir Goldstein 		return 0;
21605e1f118SAmir Goldstein 
2172ca3c148SAmir Goldstein 	/*
2182ca3c148SAmir Goldstein 	 * Decoding a merge dir, whose origin's ancestor is under a redirected
2192ca3c148SAmir Goldstein 	 * lower dir or under a non-indexed upper is not always possible.
2202ca3c148SAmir Goldstein 	 * ovl_connect_layer() will try to make origin's layer "connected" by
2212ca3c148SAmir Goldstein 	 * copying up a "connectable" ancestor.
2222ca3c148SAmir Goldstein 	 */
223*f0c0ac84SAmir Goldstein 	if (!decodable || !S_ISDIR(inode->i_mode))
224*f0c0ac84SAmir Goldstein 		return 1;
225*f0c0ac84SAmir Goldstein 
226*f0c0ac84SAmir Goldstein 	dentry = d_find_any_alias(inode);
227*f0c0ac84SAmir Goldstein 	if (!dentry)
228*f0c0ac84SAmir Goldstein 		return -ENOENT;
229*f0c0ac84SAmir Goldstein 
230*f0c0ac84SAmir Goldstein 	err = ovl_connect_layer(dentry);
231*f0c0ac84SAmir Goldstein 	dput(dentry);
232*f0c0ac84SAmir Goldstein 	if (err < 0)
233*f0c0ac84SAmir Goldstein 		return err;
23405e1f118SAmir Goldstein 
2352ca3c148SAmir Goldstein 	/* Lower file handle for indexed and non-upper dir/non-dir */
2362ca3c148SAmir Goldstein 	return 1;
23705e1f118SAmir Goldstein }
23805e1f118SAmir Goldstein 
ovl_dentry_to_fid(struct ovl_fs * ofs,struct inode * inode,u32 * fid,int buflen)239*f0c0ac84SAmir Goldstein static int ovl_dentry_to_fid(struct ovl_fs *ofs, struct inode *inode,
2401cdb0cb6SPavel Tikhomirov 			     u32 *fid, int buflen)
2418ed5eec9SAmir Goldstein {
2428ed5eec9SAmir Goldstein 	struct ovl_fh *fh = NULL;
2432ca3c148SAmir Goldstein 	int err, enc_lower;
244cbe7fba8SAmir Goldstein 	int len;
2458ed5eec9SAmir Goldstein 
24605e1f118SAmir Goldstein 	/*
2472ca3c148SAmir Goldstein 	 * Check if we should encode a lower or upper file handle and maybe
2482ca3c148SAmir Goldstein 	 * copy up an ancestor to make lower file handle connectable.
24905e1f118SAmir Goldstein 	 */
250*f0c0ac84SAmir Goldstein 	err = enc_lower = ovl_check_encode_origin(inode);
2512ca3c148SAmir Goldstein 	if (enc_lower < 0)
25205e1f118SAmir Goldstein 		goto fail;
2538ed5eec9SAmir Goldstein 
2542ca3c148SAmir Goldstein 	/* Encode an upper or lower file handle */
255955a355eSAmir Goldstein 	fh = ovl_encode_real_fh(ofs, enc_lower ? ovl_inode_lower(inode) :
256955a355eSAmir Goldstein 				ovl_inode_upper(inode), !enc_lower);
2579b6faee0SAmir Goldstein 	if (IS_ERR(fh))
25897f024b9SDing Xiang 		return PTR_ERR(fh);
2598ed5eec9SAmir Goldstein 
260cbe7fba8SAmir Goldstein 	len = OVL_FH_LEN(fh);
261144da23bSLubos Dolezel 	if (len <= buflen)
262cbe7fba8SAmir Goldstein 		memcpy(fid, fh, len);
263cbe7fba8SAmir Goldstein 	err = len;
2648ed5eec9SAmir Goldstein 
2658ed5eec9SAmir Goldstein out:
2668ed5eec9SAmir Goldstein 	kfree(fh);
2678ed5eec9SAmir Goldstein 	return err;
2688ed5eec9SAmir Goldstein 
2698ed5eec9SAmir Goldstein fail:
270*f0c0ac84SAmir Goldstein 	pr_warn_ratelimited("failed to encode file handle (ino=%lu, err=%i)\n",
271*f0c0ac84SAmir Goldstein 			    inode->i_ino, err);
2728ed5eec9SAmir Goldstein 	goto out;
2738ed5eec9SAmir Goldstein }
2748ed5eec9SAmir Goldstein 
ovl_encode_fh(struct inode * inode,u32 * fid,int * max_len,struct inode * parent)2755b2cccd3SAmir Goldstein static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
2768ed5eec9SAmir Goldstein 			 struct inode *parent)
2778ed5eec9SAmir Goldstein {
2781cdb0cb6SPavel Tikhomirov 	struct ovl_fs *ofs = OVL_FS(inode->i_sb);
279144da23bSLubos Dolezel 	int bytes, buflen = *max_len << 2;
2808ed5eec9SAmir Goldstein 
2818ed5eec9SAmir Goldstein 	/* TODO: encode connectable file handles */
2828ed5eec9SAmir Goldstein 	if (parent)
2838ed5eec9SAmir Goldstein 		return FILEID_INVALID;
2848ed5eec9SAmir Goldstein 
285*f0c0ac84SAmir Goldstein 	bytes = ovl_dentry_to_fid(ofs, inode, fid, buflen);
286cbe7fba8SAmir Goldstein 	if (bytes <= 0)
287cbe7fba8SAmir Goldstein 		return FILEID_INVALID;
288cbe7fba8SAmir Goldstein 
289cbe7fba8SAmir Goldstein 	*max_len = bytes >> 2;
290144da23bSLubos Dolezel 	if (bytes > buflen)
291144da23bSLubos Dolezel 		return FILEID_INVALID;
292cbe7fba8SAmir Goldstein 
293cbe7fba8SAmir Goldstein 	return OVL_FILEID_V1;
2948ed5eec9SAmir Goldstein }
2958ed5eec9SAmir Goldstein 
2968556a420SAmir Goldstein /*
297f71bd9cfSAmir Goldstein  * Find or instantiate an overlay dentry from real dentries and index.
2988556a420SAmir Goldstein  */
ovl_obtain_alias(struct super_block * sb,struct dentry * upper_alias,struct ovl_path * lowerpath,struct dentry * index)2998556a420SAmir Goldstein static struct dentry *ovl_obtain_alias(struct super_block *sb,
300f71bd9cfSAmir Goldstein 				       struct dentry *upper_alias,
301f71bd9cfSAmir Goldstein 				       struct ovl_path *lowerpath,
302f71bd9cfSAmir Goldstein 				       struct dentry *index)
3038556a420SAmir Goldstein {
304f941866fSAmir Goldstein 	struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
305f71bd9cfSAmir Goldstein 	struct dentry *upper = upper_alias ?: index;
3068556a420SAmir Goldstein 	struct dentry *dentry;
3070af950f5SAmir Goldstein 	struct inode *inode = NULL;
3088556a420SAmir Goldstein 	struct ovl_entry *oe;
309ac6a52ebSVivek Goyal 	struct ovl_inode_params oip = {
310ac6a52ebSVivek Goyal 		.index = index,
311ac6a52ebSVivek Goyal 	};
3128556a420SAmir Goldstein 
313f71bd9cfSAmir Goldstein 	/* We get overlay directory dentries with ovl_lookup_real() */
314f71bd9cfSAmir Goldstein 	if (d_is_dir(upper ?: lower))
3158556a420SAmir Goldstein 		return ERR_PTR(-EIO);
3168556a420SAmir Goldstein 
3170af950f5SAmir Goldstein 	oe = ovl_alloc_entry(!!lower);
3180af950f5SAmir Goldstein 	if (!oe)
3190af950f5SAmir Goldstein 		return ERR_PTR(-ENOMEM);
3200af950f5SAmir Goldstein 
321ac6a52ebSVivek Goyal 	oip.upperdentry = dget(upper);
3220af950f5SAmir Goldstein 	if (lower) {
3230af950f5SAmir Goldstein 		ovl_lowerstack(oe)->dentry = dget(lower);
3240af950f5SAmir Goldstein 		ovl_lowerstack(oe)->layer = lowerpath->layer;
3250af950f5SAmir Goldstein 	}
3260af950f5SAmir Goldstein 	oip.oe = oe;
327ac6a52ebSVivek Goyal 	inode = ovl_get_inode(sb, &oip);
3288556a420SAmir Goldstein 	if (IS_ERR(inode)) {
3290af950f5SAmir Goldstein 		ovl_free_entry(oe);
3308556a420SAmir Goldstein 		dput(upper);
3318556a420SAmir Goldstein 		return ERR_CAST(inode);
3328556a420SAmir Goldstein 	}
3338556a420SAmir Goldstein 
3349d3dfea3SVivek Goyal 	if (upper)
3359d3dfea3SVivek Goyal 		ovl_set_flag(OVL_UPPERDATA, inode);
3369d3dfea3SVivek Goyal 
3378556a420SAmir Goldstein 	dentry = d_find_any_alias(inode);
338504f3841SAl Viro 	if (dentry)
339504f3841SAl Viro 		goto out_iput;
340504f3841SAl Viro 
3418556a420SAmir Goldstein 	dentry = d_alloc_anon(inode->i_sb);
342504f3841SAl Viro 	if (unlikely(!dentry))
3438556a420SAmir Goldstein 		goto nomem;
3448556a420SAmir Goldstein 
345f71bd9cfSAmir Goldstein 	if (upper_alias)
3468556a420SAmir Goldstein 		ovl_dentry_set_upper_alias(dentry);
347504f3841SAl Viro 
3480af950f5SAmir Goldstein 	ovl_dentry_init_reval(dentry, upper, OVL_I_E(inode));
3498556a420SAmir Goldstein 
3508556a420SAmir Goldstein 	return d_instantiate_anon(dentry, inode);
3518556a420SAmir Goldstein 
3528556a420SAmir Goldstein nomem:
3538556a420SAmir Goldstein 	dput(dentry);
354504f3841SAl Viro 	dentry = ERR_PTR(-ENOMEM);
355504f3841SAl Viro out_iput:
356504f3841SAl Viro 	iput(inode);
357504f3841SAl Viro 	return dentry;
3588556a420SAmir Goldstein }
3598556a420SAmir Goldstein 
360cdf5c9d1SJiangshan Yi /* Get the upper or lower dentry in stack whose on layer @idx */
ovl_dentry_real_at(struct dentry * dentry,int idx)36198892516SAmir Goldstein static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
36298892516SAmir Goldstein {
363a6ff2bc0SAmir Goldstein 	struct ovl_entry *oe = OVL_E(dentry);
3645522c9c7SAmir Goldstein 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
36598892516SAmir Goldstein 	int i;
36698892516SAmir Goldstein 
36798892516SAmir Goldstein 	if (!idx)
36898892516SAmir Goldstein 		return ovl_dentry_upper(dentry);
36998892516SAmir Goldstein 
3705522c9c7SAmir Goldstein 	for (i = 0; i < ovl_numlower(oe); i++) {
3715522c9c7SAmir Goldstein 		if (lowerstack[i].layer->idx == idx)
3725522c9c7SAmir Goldstein 			return lowerstack[i].dentry;
37398892516SAmir Goldstein 	}
37498892516SAmir Goldstein 
37598892516SAmir Goldstein 	return NULL;
37698892516SAmir Goldstein }
37798892516SAmir Goldstein 
3783985b70aSAmir Goldstein /*
3793985b70aSAmir Goldstein  * Lookup a child overlay dentry to get a connected overlay dentry whose real
3803985b70aSAmir Goldstein  * dentry is @real. If @real is on upper layer, we lookup a child overlay
3813985b70aSAmir Goldstein  * dentry with the same name as the real dentry. Otherwise, we need to consult
3823985b70aSAmir Goldstein  * index for lookup.
3833985b70aSAmir Goldstein  */
ovl_lookup_real_one(struct dentry * connected,struct dentry * real,const struct ovl_layer * layer)3843985b70aSAmir Goldstein static struct dentry *ovl_lookup_real_one(struct dentry *connected,
3853985b70aSAmir Goldstein 					  struct dentry *real,
38613464165SMiklos Szeredi 					  const struct ovl_layer *layer)
3873985b70aSAmir Goldstein {
3883985b70aSAmir Goldstein 	struct inode *dir = d_inode(connected);
3893985b70aSAmir Goldstein 	struct dentry *this, *parent = NULL;
3903985b70aSAmir Goldstein 	struct name_snapshot name;
3913985b70aSAmir Goldstein 	int err;
3923985b70aSAmir Goldstein 
3933985b70aSAmir Goldstein 	/*
3943985b70aSAmir Goldstein 	 * Lookup child overlay dentry by real name. The dir mutex protects us
3953985b70aSAmir Goldstein 	 * from racing with overlay rename. If the overlay dentry that is above
3963985b70aSAmir Goldstein 	 * real has already been moved to a parent that is not under the
3973985b70aSAmir Goldstein 	 * connected overlay dir, we return -ECHILD and restart the lookup of
3983985b70aSAmir Goldstein 	 * connected real path from the top.
3993985b70aSAmir Goldstein 	 */
4003985b70aSAmir Goldstein 	inode_lock_nested(dir, I_MUTEX_PARENT);
4013985b70aSAmir Goldstein 	err = -ECHILD;
4023985b70aSAmir Goldstein 	parent = dget_parent(real);
40398892516SAmir Goldstein 	if (ovl_dentry_real_at(connected, layer->idx) != parent)
4043985b70aSAmir Goldstein 		goto fail;
4053985b70aSAmir Goldstein 
4063985b70aSAmir Goldstein 	/*
4073985b70aSAmir Goldstein 	 * We also need to take a snapshot of real dentry name to protect us
4083985b70aSAmir Goldstein 	 * from racing with underlying layer rename. In this case, we don't
4093985b70aSAmir Goldstein 	 * care about returning ESTALE, only from dereferencing a free name
4103985b70aSAmir Goldstein 	 * pointer because we hold no lock on the real dentry.
4113985b70aSAmir Goldstein 	 */
4123985b70aSAmir Goldstein 	take_dentry_name_snapshot(&name, real);
413ba9ea771SChristian Brauner 	/*
4144609e1f1SChristian Brauner 	 * No idmap handling here: it's an internal lookup.  Could skip
4154609e1f1SChristian Brauner 	 * permission checking altogether, but for now just use non-idmap
416ba9ea771SChristian Brauner 	 * transformed ids.
417ba9ea771SChristian Brauner 	 */
418230c6402SAl Viro 	this = lookup_one_len(name.name.name, connected, name.name.len);
419580c6104SMiklos Szeredi 	release_dentry_name_snapshot(&name);
4203985b70aSAmir Goldstein 	err = PTR_ERR(this);
4213985b70aSAmir Goldstein 	if (IS_ERR(this)) {
4223985b70aSAmir Goldstein 		goto fail;
4233985b70aSAmir Goldstein 	} else if (!this || !this->d_inode) {
4243985b70aSAmir Goldstein 		dput(this);
4253985b70aSAmir Goldstein 		err = -ENOENT;
4263985b70aSAmir Goldstein 		goto fail;
42798892516SAmir Goldstein 	} else if (ovl_dentry_real_at(this, layer->idx) != real) {
4283985b70aSAmir Goldstein 		dput(this);
4293985b70aSAmir Goldstein 		err = -ESTALE;
4303985b70aSAmir Goldstein 		goto fail;
4313985b70aSAmir Goldstein 	}
4323985b70aSAmir Goldstein 
4333985b70aSAmir Goldstein out:
4343985b70aSAmir Goldstein 	dput(parent);
4353985b70aSAmir Goldstein 	inode_unlock(dir);
4363985b70aSAmir Goldstein 	return this;
4373985b70aSAmir Goldstein 
4383985b70aSAmir Goldstein fail:
4391bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
4403985b70aSAmir Goldstein 			    real, layer->idx, connected, err);
4413985b70aSAmir Goldstein 	this = ERR_PTR(err);
4423985b70aSAmir Goldstein 	goto out;
4433985b70aSAmir Goldstein }
4443985b70aSAmir Goldstein 
44506170154SAmir Goldstein static struct dentry *ovl_lookup_real(struct super_block *sb,
44606170154SAmir Goldstein 				      struct dentry *real,
44713464165SMiklos Szeredi 				      const struct ovl_layer *layer);
44806170154SAmir Goldstein 
4493985b70aSAmir Goldstein /*
4504b91c30aSAmir Goldstein  * Lookup an indexed or hashed overlay dentry by real inode.
4514b91c30aSAmir Goldstein  */
ovl_lookup_real_inode(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)4524b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
4534b91c30aSAmir Goldstein 					    struct dentry *real,
45413464165SMiklos Szeredi 					    const struct ovl_layer *layer)
4554b91c30aSAmir Goldstein {
456f01d0889SAndrea Righi 	struct ovl_fs *ofs = OVL_FS(sb);
45706170154SAmir Goldstein 	struct dentry *index = NULL;
4584b91c30aSAmir Goldstein 	struct dentry *this = NULL;
4594b91c30aSAmir Goldstein 	struct inode *inode;
4604b91c30aSAmir Goldstein 
46106170154SAmir Goldstein 	/*
46206170154SAmir Goldstein 	 * Decoding upper dir from index is expensive, so first try to lookup
46306170154SAmir Goldstein 	 * overlay dentry in inode/dcache.
46406170154SAmir Goldstein 	 */
4654b91c30aSAmir Goldstein 	inode = ovl_lookup_inode(sb, real, !layer->idx);
4664b91c30aSAmir Goldstein 	if (IS_ERR(inode))
4674b91c30aSAmir Goldstein 		return ERR_CAST(inode);
4684b91c30aSAmir Goldstein 	if (inode) {
4694b91c30aSAmir Goldstein 		this = d_find_any_alias(inode);
4704b91c30aSAmir Goldstein 		iput(inode);
4714b91c30aSAmir Goldstein 	}
4724b91c30aSAmir Goldstein 
47306170154SAmir Goldstein 	/*
47406170154SAmir Goldstein 	 * For decoded lower dir file handle, lookup index by origin to check
47506170154SAmir Goldstein 	 * if lower dir was copied up and and/or removed.
47606170154SAmir Goldstein 	 */
47706170154SAmir Goldstein 	if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
47806170154SAmir Goldstein 		index = ovl_lookup_index(ofs, NULL, real, false);
47906170154SAmir Goldstein 		if (IS_ERR(index))
48006170154SAmir Goldstein 			return index;
48106170154SAmir Goldstein 	}
48206170154SAmir Goldstein 
48306170154SAmir Goldstein 	/* Get connected upper overlay dir from index */
48406170154SAmir Goldstein 	if (index) {
4858ea28765SAmir Goldstein 		struct dentry *upper = ovl_index_upper(ofs, index, true);
48606170154SAmir Goldstein 
48706170154SAmir Goldstein 		dput(index);
48806170154SAmir Goldstein 		if (IS_ERR_OR_NULL(upper))
48906170154SAmir Goldstein 			return upper;
49006170154SAmir Goldstein 
49106170154SAmir Goldstein 		/*
49206170154SAmir Goldstein 		 * ovl_lookup_real() in lower layer may call recursively once to
49306170154SAmir Goldstein 		 * ovl_lookup_real() in upper layer. The first level call walks
49406170154SAmir Goldstein 		 * back lower parents to the topmost indexed parent. The second
49506170154SAmir Goldstein 		 * recursive call walks back from indexed upper to the topmost
49606170154SAmir Goldstein 		 * connected/hashed upper parent (or up to root).
49706170154SAmir Goldstein 		 */
49894375f9dSAmir Goldstein 		this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
49906170154SAmir Goldstein 		dput(upper);
50006170154SAmir Goldstein 	}
50106170154SAmir Goldstein 
5027168179fSAmir Goldstein 	if (IS_ERR_OR_NULL(this))
5037168179fSAmir Goldstein 		return this;
5044b91c30aSAmir Goldstein 
505124c2de2SAmir Goldstein 	if (ovl_dentry_real_at(this, layer->idx) != real) {
5064b91c30aSAmir Goldstein 		dput(this);
5074b91c30aSAmir Goldstein 		this = ERR_PTR(-EIO);
5084b91c30aSAmir Goldstein 	}
5094b91c30aSAmir Goldstein 
5104b91c30aSAmir Goldstein 	return this;
5114b91c30aSAmir Goldstein }
5124b91c30aSAmir Goldstein 
5134b91c30aSAmir Goldstein /*
5144b91c30aSAmir Goldstein  * Lookup an indexed or hashed overlay dentry, whose real dentry is an
5154b91c30aSAmir Goldstein  * ancestor of @real.
5164b91c30aSAmir Goldstein  */
ovl_lookup_real_ancestor(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)5174b91c30aSAmir Goldstein static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
5184b91c30aSAmir Goldstein 					       struct dentry *real,
51913464165SMiklos Szeredi 					       const struct ovl_layer *layer)
5204b91c30aSAmir Goldstein {
5214b91c30aSAmir Goldstein 	struct dentry *next, *parent = NULL;
5224b91c30aSAmir Goldstein 	struct dentry *ancestor = ERR_PTR(-EIO);
5234b91c30aSAmir Goldstein 
5244b91c30aSAmir Goldstein 	if (real == layer->mnt->mnt_root)
5254b91c30aSAmir Goldstein 		return dget(sb->s_root);
5264b91c30aSAmir Goldstein 
5274b91c30aSAmir Goldstein 	/* Find the topmost indexed or hashed ancestor */
5284b91c30aSAmir Goldstein 	next = dget(real);
5294b91c30aSAmir Goldstein 	for (;;) {
5304b91c30aSAmir Goldstein 		parent = dget_parent(next);
5314b91c30aSAmir Goldstein 
5324b91c30aSAmir Goldstein 		/*
5334b91c30aSAmir Goldstein 		 * Lookup a matching overlay dentry in inode/dentry
5344b91c30aSAmir Goldstein 		 * cache or in index by real inode.
5354b91c30aSAmir Goldstein 		 */
5364b91c30aSAmir Goldstein 		ancestor = ovl_lookup_real_inode(sb, next, layer);
5374b91c30aSAmir Goldstein 		if (ancestor)
5384b91c30aSAmir Goldstein 			break;
5394b91c30aSAmir Goldstein 
5404b91c30aSAmir Goldstein 		if (parent == layer->mnt->mnt_root) {
5414b91c30aSAmir Goldstein 			ancestor = dget(sb->s_root);
5424b91c30aSAmir Goldstein 			break;
5434b91c30aSAmir Goldstein 		}
5444b91c30aSAmir Goldstein 
5454b91c30aSAmir Goldstein 		/*
5464b91c30aSAmir Goldstein 		 * If @real has been moved out of the layer root directory,
5474b91c30aSAmir Goldstein 		 * we will eventully hit the real fs root. This cannot happen
5484b91c30aSAmir Goldstein 		 * by legit overlay rename, so we return error in that case.
5494b91c30aSAmir Goldstein 		 */
5504b91c30aSAmir Goldstein 		if (parent == next) {
5514b91c30aSAmir Goldstein 			ancestor = ERR_PTR(-EXDEV);
5524b91c30aSAmir Goldstein 			break;
5534b91c30aSAmir Goldstein 		}
5544b91c30aSAmir Goldstein 
5554b91c30aSAmir Goldstein 		dput(next);
5564b91c30aSAmir Goldstein 		next = parent;
5574b91c30aSAmir Goldstein 	}
5584b91c30aSAmir Goldstein 
5594b91c30aSAmir Goldstein 	dput(parent);
5604b91c30aSAmir Goldstein 	dput(next);
5614b91c30aSAmir Goldstein 
5624b91c30aSAmir Goldstein 	return ancestor;
5634b91c30aSAmir Goldstein }
5644b91c30aSAmir Goldstein 
5654b91c30aSAmir Goldstein /*
5663985b70aSAmir Goldstein  * Lookup a connected overlay dentry whose real dentry is @real.
5673985b70aSAmir Goldstein  * If @real is on upper layer, we lookup a child overlay dentry with the same
5683985b70aSAmir Goldstein  * path the real dentry. Otherwise, we need to consult index for lookup.
5693985b70aSAmir Goldstein  */
ovl_lookup_real(struct super_block * sb,struct dentry * real,const struct ovl_layer * layer)5703985b70aSAmir Goldstein static struct dentry *ovl_lookup_real(struct super_block *sb,
5713985b70aSAmir Goldstein 				      struct dentry *real,
57213464165SMiklos Szeredi 				      const struct ovl_layer *layer)
5733985b70aSAmir Goldstein {
5743985b70aSAmir Goldstein 	struct dentry *connected;
5753985b70aSAmir Goldstein 	int err = 0;
5763985b70aSAmir Goldstein 
5774b91c30aSAmir Goldstein 	connected = ovl_lookup_real_ancestor(sb, real, layer);
5784b91c30aSAmir Goldstein 	if (IS_ERR(connected))
5794b91c30aSAmir Goldstein 		return connected;
5803985b70aSAmir Goldstein 
5813985b70aSAmir Goldstein 	while (!err) {
5823985b70aSAmir Goldstein 		struct dentry *next, *this;
5833985b70aSAmir Goldstein 		struct dentry *parent = NULL;
58498892516SAmir Goldstein 		struct dentry *real_connected = ovl_dentry_real_at(connected,
58598892516SAmir Goldstein 								   layer->idx);
5863985b70aSAmir Goldstein 
5873985b70aSAmir Goldstein 		if (real_connected == real)
5883985b70aSAmir Goldstein 			break;
5893985b70aSAmir Goldstein 
5903985b70aSAmir Goldstein 		/* Find the topmost dentry not yet connected */
5913985b70aSAmir Goldstein 		next = dget(real);
5923985b70aSAmir Goldstein 		for (;;) {
5933985b70aSAmir Goldstein 			parent = dget_parent(next);
5943985b70aSAmir Goldstein 
5953985b70aSAmir Goldstein 			if (parent == real_connected)
5963985b70aSAmir Goldstein 				break;
5973985b70aSAmir Goldstein 
5983985b70aSAmir Goldstein 			/*
5993985b70aSAmir Goldstein 			 * If real has been moved out of 'real_connected',
6003985b70aSAmir Goldstein 			 * we will not find 'real_connected' and hit the layer
6013985b70aSAmir Goldstein 			 * root. In that case, we need to restart connecting.
6023985b70aSAmir Goldstein 			 * This game can go on forever in the worst case. We
6033985b70aSAmir Goldstein 			 * may want to consider taking s_vfs_rename_mutex if
6043985b70aSAmir Goldstein 			 * this happens more than once.
6053985b70aSAmir Goldstein 			 */
6063985b70aSAmir Goldstein 			if (parent == layer->mnt->mnt_root) {
6073985b70aSAmir Goldstein 				dput(connected);
6083985b70aSAmir Goldstein 				connected = dget(sb->s_root);
6093985b70aSAmir Goldstein 				break;
6103985b70aSAmir Goldstein 			}
6113985b70aSAmir Goldstein 
6123985b70aSAmir Goldstein 			/*
6133985b70aSAmir Goldstein 			 * If real file has been moved out of the layer root
6143985b70aSAmir Goldstein 			 * directory, we will eventully hit the real fs root.
6153985b70aSAmir Goldstein 			 * This cannot happen by legit overlay rename, so we
6163985b70aSAmir Goldstein 			 * return error in that case.
6173985b70aSAmir Goldstein 			 */
6183985b70aSAmir Goldstein 			if (parent == next) {
6193985b70aSAmir Goldstein 				err = -EXDEV;
6203985b70aSAmir Goldstein 				break;
6213985b70aSAmir Goldstein 			}
6223985b70aSAmir Goldstein 
6233985b70aSAmir Goldstein 			dput(next);
6243985b70aSAmir Goldstein 			next = parent;
6253985b70aSAmir Goldstein 		}
6263985b70aSAmir Goldstein 
6273985b70aSAmir Goldstein 		if (!err) {
6283985b70aSAmir Goldstein 			this = ovl_lookup_real_one(connected, next, layer);
6293985b70aSAmir Goldstein 			if (IS_ERR(this))
6303985b70aSAmir Goldstein 				err = PTR_ERR(this);
6313985b70aSAmir Goldstein 
6323985b70aSAmir Goldstein 			/*
6333985b70aSAmir Goldstein 			 * Lookup of child in overlay can fail when racing with
6343985b70aSAmir Goldstein 			 * overlay rename of child away from 'connected' parent.
6353985b70aSAmir Goldstein 			 * In this case, we need to restart the lookup from the
6363985b70aSAmir Goldstein 			 * top, because we cannot trust that 'real_connected' is
6374b91c30aSAmir Goldstein 			 * still an ancestor of 'real'. There is a good chance
6384b91c30aSAmir Goldstein 			 * that the renamed overlay ancestor is now in cache, so
6394b91c30aSAmir Goldstein 			 * ovl_lookup_real_ancestor() will find it and we can
6404b91c30aSAmir Goldstein 			 * continue to connect exactly from where lookup failed.
6413985b70aSAmir Goldstein 			 */
6423985b70aSAmir Goldstein 			if (err == -ECHILD) {
6434b91c30aSAmir Goldstein 				this = ovl_lookup_real_ancestor(sb, real,
6444b91c30aSAmir Goldstein 								layer);
645b5095f24SFengguang Wu 				err = PTR_ERR_OR_ZERO(this);
6463985b70aSAmir Goldstein 			}
6473985b70aSAmir Goldstein 			if (!err) {
6483985b70aSAmir Goldstein 				dput(connected);
6493985b70aSAmir Goldstein 				connected = this;
6503985b70aSAmir Goldstein 			}
6513985b70aSAmir Goldstein 		}
6523985b70aSAmir Goldstein 
6533985b70aSAmir Goldstein 		dput(parent);
6543985b70aSAmir Goldstein 		dput(next);
6553985b70aSAmir Goldstein 	}
6563985b70aSAmir Goldstein 
6573985b70aSAmir Goldstein 	if (err)
6583985b70aSAmir Goldstein 		goto fail;
6593985b70aSAmir Goldstein 
6603985b70aSAmir Goldstein 	return connected;
6613985b70aSAmir Goldstein 
6623985b70aSAmir Goldstein fail:
6631bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
6643985b70aSAmir Goldstein 			    real, layer->idx, connected, err);
6653985b70aSAmir Goldstein 	dput(connected);
6663985b70aSAmir Goldstein 	return ERR_PTR(err);
6673985b70aSAmir Goldstein }
6683985b70aSAmir Goldstein 
6693985b70aSAmir Goldstein /*
670f71bd9cfSAmir Goldstein  * Get an overlay dentry from upper/lower real dentries and index.
6713985b70aSAmir Goldstein  */
ovl_get_dentry(struct super_block * sb,struct dentry * upper,struct ovl_path * lowerpath,struct dentry * index)6723985b70aSAmir Goldstein static struct dentry *ovl_get_dentry(struct super_block *sb,
6733985b70aSAmir Goldstein 				     struct dentry *upper,
674f71bd9cfSAmir Goldstein 				     struct ovl_path *lowerpath,
675f71bd9cfSAmir Goldstein 				     struct dentry *index)
6763985b70aSAmir Goldstein {
677f01d0889SAndrea Righi 	struct ovl_fs *ofs = OVL_FS(sb);
67813464165SMiklos Szeredi 	const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
679f71bd9cfSAmir Goldstein 	struct dentry *real = upper ?: (index ?: lowerpath->dentry);
6803985b70aSAmir Goldstein 
681f941866fSAmir Goldstein 	/*
682f71bd9cfSAmir Goldstein 	 * Obtain a disconnected overlay dentry from a non-dir real dentry
683f71bd9cfSAmir Goldstein 	 * and index.
684f941866fSAmir Goldstein 	 */
685f71bd9cfSAmir Goldstein 	if (!d_is_dir(real))
686f71bd9cfSAmir Goldstein 		return ovl_obtain_alias(sb, upper, lowerpath, index);
687f941866fSAmir Goldstein 
6883985b70aSAmir Goldstein 	/* Removed empty directory? */
68998892516SAmir Goldstein 	if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
6903985b70aSAmir Goldstein 		return ERR_PTR(-ENOENT);
6913985b70aSAmir Goldstein 
6923985b70aSAmir Goldstein 	/*
69398892516SAmir Goldstein 	 * If real dentry is connected and hashed, get a connected overlay
69498892516SAmir Goldstein 	 * dentry whose real dentry is @real.
6953985b70aSAmir Goldstein 	 */
69698892516SAmir Goldstein 	return ovl_lookup_real(sb, real, layer);
6973985b70aSAmir Goldstein }
6983985b70aSAmir Goldstein 
ovl_upper_fh_to_d(struct super_block * sb,struct ovl_fh * fh)6998556a420SAmir Goldstein static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
7008556a420SAmir Goldstein 					struct ovl_fh *fh)
7018556a420SAmir Goldstein {
702f01d0889SAndrea Righi 	struct ovl_fs *ofs = OVL_FS(sb);
7038556a420SAmir Goldstein 	struct dentry *dentry;
7048556a420SAmir Goldstein 	struct dentry *upper;
7058556a420SAmir Goldstein 
70608f4c7c8SMiklos Szeredi 	if (!ovl_upper_mnt(ofs))
7078556a420SAmir Goldstein 		return ERR_PTR(-EACCES);
7088556a420SAmir Goldstein 
7091cdb0cb6SPavel Tikhomirov 	upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
7108556a420SAmir Goldstein 	if (IS_ERR_OR_NULL(upper))
7118556a420SAmir Goldstein 		return upper;
7128556a420SAmir Goldstein 
713f71bd9cfSAmir Goldstein 	dentry = ovl_get_dentry(sb, upper, NULL, NULL);
7148556a420SAmir Goldstein 	dput(upper);
7158556a420SAmir Goldstein 
7168556a420SAmir Goldstein 	return dentry;
7178556a420SAmir Goldstein }
7188556a420SAmir Goldstein 
ovl_lower_fh_to_d(struct super_block * sb,struct ovl_fh * fh)719f941866fSAmir Goldstein static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
720f941866fSAmir Goldstein 					struct ovl_fh *fh)
721f941866fSAmir Goldstein {
722f01d0889SAndrea Righi 	struct ovl_fs *ofs = OVL_FS(sb);
723f941866fSAmir Goldstein 	struct ovl_path origin = { };
724f941866fSAmir Goldstein 	struct ovl_path *stack = &origin;
725f941866fSAmir Goldstein 	struct dentry *dentry = NULL;
726f71bd9cfSAmir Goldstein 	struct dentry *index = NULL;
7278b58924aSAmir Goldstein 	struct inode *inode;
728f941866fSAmir Goldstein 	int err;
729f941866fSAmir Goldstein 
7308b58924aSAmir Goldstein 	/* First lookup overlay inode in inode cache by origin fh */
7318b58924aSAmir Goldstein 	err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
7328b58924aSAmir Goldstein 	if (err)
7338b58924aSAmir Goldstein 		return ERR_PTR(err);
7348b58924aSAmir Goldstein 
7358b58924aSAmir Goldstein 	if (!d_is_dir(origin.dentry) ||
7368b58924aSAmir Goldstein 	    !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
7378b58924aSAmir Goldstein 		inode = ovl_lookup_inode(sb, origin.dentry, false);
7388b58924aSAmir Goldstein 		err = PTR_ERR(inode);
7398b58924aSAmir Goldstein 		if (IS_ERR(inode))
7408b58924aSAmir Goldstein 			goto out_err;
7418b58924aSAmir Goldstein 		if (inode) {
7428b58924aSAmir Goldstein 			dentry = d_find_any_alias(inode);
7438b58924aSAmir Goldstein 			iput(inode);
7448b58924aSAmir Goldstein 			if (dentry)
7458b58924aSAmir Goldstein 				goto out;
7468b58924aSAmir Goldstein 		}
7478b58924aSAmir Goldstein 	}
7488b58924aSAmir Goldstein 
7498b58924aSAmir Goldstein 	/* Then lookup indexed upper/whiteout by origin fh */
750f71bd9cfSAmir Goldstein 	if (ofs->indexdir) {
751f71bd9cfSAmir Goldstein 		index = ovl_get_index_fh(ofs, fh);
752f71bd9cfSAmir Goldstein 		err = PTR_ERR(index);
7539436a1a3SAmir Goldstein 		if (IS_ERR(index)) {
7549436a1a3SAmir Goldstein 			index = NULL;
7558b58924aSAmir Goldstein 			goto out_err;
7569436a1a3SAmir Goldstein 		}
757f71bd9cfSAmir Goldstein 	}
758f941866fSAmir Goldstein 
7598b58924aSAmir Goldstein 	/* Then try to get a connected upper dir by index */
7603b0bfc6eSAmir Goldstein 	if (index && d_is_dir(index)) {
7618ea28765SAmir Goldstein 		struct dentry *upper = ovl_index_upper(ofs, index, true);
7623b0bfc6eSAmir Goldstein 
7633b0bfc6eSAmir Goldstein 		err = PTR_ERR(upper);
7643b0bfc6eSAmir Goldstein 		if (IS_ERR_OR_NULL(upper))
7653b0bfc6eSAmir Goldstein 			goto out_err;
7663b0bfc6eSAmir Goldstein 
7673b0bfc6eSAmir Goldstein 		dentry = ovl_get_dentry(sb, upper, NULL, NULL);
7683b0bfc6eSAmir Goldstein 		dput(upper);
7693b0bfc6eSAmir Goldstein 		goto out;
7703b0bfc6eSAmir Goldstein 	}
7713b0bfc6eSAmir Goldstein 
772155b8a04SAmir Goldstein 	/* Find origin.dentry again with ovl_acceptable() layer check */
773155b8a04SAmir Goldstein 	if (d_is_dir(origin.dentry)) {
7748b58924aSAmir Goldstein 		dput(origin.dentry);
7758b58924aSAmir Goldstein 		origin.dentry = NULL;
7768a22efa1SAmir Goldstein 		err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
7778b58924aSAmir Goldstein 		if (err)
778f71bd9cfSAmir Goldstein 			goto out_err;
7798b58924aSAmir Goldstein 	}
7808b58924aSAmir Goldstein 	if (index) {
781610afc0bSMiklos Szeredi 		err = ovl_verify_origin(ofs, index, origin.dentry, false);
782f71bd9cfSAmir Goldstein 		if (err)
783f71bd9cfSAmir Goldstein 			goto out_err;
784f71bd9cfSAmir Goldstein 	}
785f71bd9cfSAmir Goldstein 
786155b8a04SAmir Goldstein 	/* Get a connected non-upper dir or disconnected non-dir */
787f71bd9cfSAmir Goldstein 	dentry = ovl_get_dentry(sb, NULL, &origin, index);
788f71bd9cfSAmir Goldstein 
789f71bd9cfSAmir Goldstein out:
790f941866fSAmir Goldstein 	dput(origin.dentry);
791f71bd9cfSAmir Goldstein 	dput(index);
792f941866fSAmir Goldstein 	return dentry;
793f71bd9cfSAmir Goldstein 
794f71bd9cfSAmir Goldstein out_err:
795f71bd9cfSAmir Goldstein 	dentry = ERR_PTR(err);
796f71bd9cfSAmir Goldstein 	goto out;
797f941866fSAmir Goldstein }
798f941866fSAmir Goldstein 
ovl_fid_to_fh(struct fid * fid,int buflen,int fh_type)799cbe7fba8SAmir Goldstein static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
800cbe7fba8SAmir Goldstein {
801cbe7fba8SAmir Goldstein 	struct ovl_fh *fh;
802cbe7fba8SAmir Goldstein 
803cbe7fba8SAmir Goldstein 	/* If on-wire inner fid is aligned - nothing to do */
804cbe7fba8SAmir Goldstein 	if (fh_type == OVL_FILEID_V1)
805cbe7fba8SAmir Goldstein 		return (struct ovl_fh *)fid;
806cbe7fba8SAmir Goldstein 
807cbe7fba8SAmir Goldstein 	if (fh_type != OVL_FILEID_V0)
808cbe7fba8SAmir Goldstein 		return ERR_PTR(-EINVAL);
809cbe7fba8SAmir Goldstein 
8109aafc1b0SDan Carpenter 	if (buflen <= OVL_FH_WIRE_OFFSET)
8119aafc1b0SDan Carpenter 		return ERR_PTR(-EINVAL);
8129aafc1b0SDan Carpenter 
813cbe7fba8SAmir Goldstein 	fh = kzalloc(buflen, GFP_KERNEL);
814cbe7fba8SAmir Goldstein 	if (!fh)
815cbe7fba8SAmir Goldstein 		return ERR_PTR(-ENOMEM);
816cbe7fba8SAmir Goldstein 
817cbe7fba8SAmir Goldstein 	/* Copy unaligned inner fh into aligned buffer */
818cf8aa9bfSKees Cook 	memcpy(fh->buf, fid, buflen - OVL_FH_WIRE_OFFSET);
819cbe7fba8SAmir Goldstein 	return fh;
820cbe7fba8SAmir Goldstein }
821cbe7fba8SAmir Goldstein 
ovl_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)8228556a420SAmir Goldstein static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
8238556a420SAmir Goldstein 				       int fh_len, int fh_type)
8248556a420SAmir Goldstein {
8258556a420SAmir Goldstein 	struct dentry *dentry = NULL;
826cbe7fba8SAmir Goldstein 	struct ovl_fh *fh = NULL;
8278556a420SAmir Goldstein 	int len = fh_len << 2;
8288556a420SAmir Goldstein 	unsigned int flags = 0;
8298556a420SAmir Goldstein 	int err;
8308556a420SAmir Goldstein 
831cbe7fba8SAmir Goldstein 	fh = ovl_fid_to_fh(fid, len, fh_type);
832cbe7fba8SAmir Goldstein 	err = PTR_ERR(fh);
833cbe7fba8SAmir Goldstein 	if (IS_ERR(fh))
8348556a420SAmir Goldstein 		goto out_err;
8358556a420SAmir Goldstein 
8368556a420SAmir Goldstein 	err = ovl_check_fh_len(fh, len);
8378556a420SAmir Goldstein 	if (err)
8388556a420SAmir Goldstein 		goto out_err;
8398556a420SAmir Goldstein 
840cbe7fba8SAmir Goldstein 	flags = fh->fb.flags;
841f941866fSAmir Goldstein 	dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
842f941866fSAmir Goldstein 		 ovl_upper_fh_to_d(sb, fh) :
843f941866fSAmir Goldstein 		 ovl_lower_fh_to_d(sb, fh);
8448556a420SAmir Goldstein 	err = PTR_ERR(dentry);
8458556a420SAmir Goldstein 	if (IS_ERR(dentry) && err != -ESTALE)
8468556a420SAmir Goldstein 		goto out_err;
8478556a420SAmir Goldstein 
848cbe7fba8SAmir Goldstein out:
849cbe7fba8SAmir Goldstein 	/* We may have needed to re-align OVL_FILEID_V0 */
850cbe7fba8SAmir Goldstein 	if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
851cbe7fba8SAmir Goldstein 		kfree(fh);
852cbe7fba8SAmir Goldstein 
8538556a420SAmir Goldstein 	return dentry;
8548556a420SAmir Goldstein 
8558556a420SAmir Goldstein out_err:
8561bd0a3aeSlijiazi 	pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
857cbe7fba8SAmir Goldstein 			    fh_len, fh_type, flags, err);
858cbe7fba8SAmir Goldstein 	dentry = ERR_PTR(err);
859cbe7fba8SAmir Goldstein 	goto out;
8608556a420SAmir Goldstein }
8618556a420SAmir Goldstein 
ovl_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)8623985b70aSAmir Goldstein static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
8633985b70aSAmir Goldstein 				       int fh_len, int fh_type)
8643985b70aSAmir Goldstein {
8651bd0a3aeSlijiazi 	pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
8663985b70aSAmir Goldstein 	return ERR_PTR(-EACCES);
8673985b70aSAmir Goldstein }
8683985b70aSAmir Goldstein 
ovl_get_name(struct dentry * parent,char * name,struct dentry * child)8693985b70aSAmir Goldstein static int ovl_get_name(struct dentry *parent, char *name,
8703985b70aSAmir Goldstein 			struct dentry *child)
8713985b70aSAmir Goldstein {
8723985b70aSAmir Goldstein 	/*
8733985b70aSAmir Goldstein 	 * ovl_fh_to_dentry() returns connected dir overlay dentries and
8743985b70aSAmir Goldstein 	 * ovl_fh_to_parent() is not implemented, so we should not get here.
8753985b70aSAmir Goldstein 	 */
8763985b70aSAmir Goldstein 	WARN_ON_ONCE(1);
8773985b70aSAmir Goldstein 	return -EIO;
8783985b70aSAmir Goldstein }
8793985b70aSAmir Goldstein 
ovl_get_parent(struct dentry * dentry)8803985b70aSAmir Goldstein static struct dentry *ovl_get_parent(struct dentry *dentry)
8813985b70aSAmir Goldstein {
8823985b70aSAmir Goldstein 	/*
8833985b70aSAmir Goldstein 	 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
8843985b70aSAmir Goldstein 	 * should not get here.
8853985b70aSAmir Goldstein 	 */
8863985b70aSAmir Goldstein 	WARN_ON_ONCE(1);
8873985b70aSAmir Goldstein 	return ERR_PTR(-EIO);
8883985b70aSAmir Goldstein }
8893985b70aSAmir Goldstein 
8908ed5eec9SAmir Goldstein const struct export_operations ovl_export_operations = {
8915b2cccd3SAmir Goldstein 	.encode_fh	= ovl_encode_fh,
8928556a420SAmir Goldstein 	.fh_to_dentry	= ovl_fh_to_dentry,
8933985b70aSAmir Goldstein 	.fh_to_parent	= ovl_fh_to_parent,
8943985b70aSAmir Goldstein 	.get_name	= ovl_get_name,
8953985b70aSAmir Goldstein 	.get_parent	= ovl_get_parent,
8968ed5eec9SAmir Goldstein };
89716aac5adSAmir Goldstein 
89816aac5adSAmir Goldstein /* encode_fh() encodes non-decodable file handles with nfs_export=off */
89916aac5adSAmir Goldstein const struct export_operations ovl_export_fid_operations = {
90016aac5adSAmir Goldstein 	.encode_fh	= ovl_encode_fh,
90116aac5adSAmir Goldstein };
902