Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Overlayfs NFS export support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Amir Goldstein <amir73il@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Copyright (C) 2017-2018 CTERA Networks. All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int ovl_encode_maybe_copy_up(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 	if (ovl_dentry_upper(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	err = ovl_want_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 		err = ovl_copy_up(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		pr_warn_ratelimited("failed to copy up on encode (%pd2, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 				    dentry, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * Before encoding a non-upper directory file handle from real layer N, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * to check if it will be possible to reconnect an overlay dentry from the real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  * lower decoded dentry. This is done by following the overlay ancestry up to a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * "layer N connected" ancestor and verifying that all parents along the way are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * "layer N connectable". If an ancestor that is NOT "layer N connectable" is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * found, we need to copy up an ancestor, which is "layer N connectable", thus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * making that ancestor "layer N connected". For example:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * layer 1: /a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * layer 2: /a/b/c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  * The overlay dentry /a is NOT "layer 2 connectable", because if dir /a is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * copied up and renamed, upper dir /a will be indexed by lower dir /a from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * layer 1. The dir /a from layer 2 will never be indexed, so the algorithm (*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * in ovl_lookup_real_ancestor() will not be able to lookup a connected overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * dentry from the connected lower dentry /a/b/c.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58)  * To avoid this problem on decode time, we need to copy up an ancestor of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59)  * /a/b/c, which is "layer 2 connectable", on encode time. That ancestor is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60)  * /a/b. After copy up (and index) of /a/b, it will become "layer 2 connected"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61)  * and when the time comes to decode the file handle from lower dentry /a/b/c,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62)  * ovl_lookup_real_ancestor() will find the indexed ancestor /a/b and decoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63)  * a connected overlay dentry will be accomplished.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65)  * (*) the algorithm in ovl_lookup_real_ancestor() can be improved to lookup an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66)  * entry /a in the lower layers above layer N and find the indexed dir /a from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67)  * layer 1. If that improvement is made, then the check for "layer N connected"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68)  * will need to verify there are no redirects in lower layers above N. In the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69)  * example above, /a will be "layer 2 connectable". However, if layer 2 dir /a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70)  * is a target of a layer 1 redirect, then /a will NOT be "layer 2 connectable":
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72)  * layer 1: /A (redirect = /a)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73)  * layer 2: /a/b/c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) /* Return the lowest layer for encoding a connectable file handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) static int ovl_connectable_layer(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	struct ovl_entry *oe = OVL_E(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	/* We can get overlay root from root of any layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	if (dentry == dentry->d_sb->s_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		return oe->numlower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	 * If it's an unindexed merge dir, then it's not connectable with any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	 * lower layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	if (ovl_dentry_upper(dentry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	    !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/* We can get upper/overlay path from indexed/lower dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	return oe->lowerstack[0].layer->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * @dentry is "connected" if all ancestors up to root or a "connected" ancestor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * have the same uppermost lower layer as the origin's layer. We may need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * copy up a "connectable" ancestor to make it "connected". A "connected" dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * cannot become non "connected", so cache positive result in dentry flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * Return the connected origin layer or < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int ovl_connect_layer(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	struct dentry *next, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	int origin_layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (WARN_ON(dentry == dentry->d_sb->s_root) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	    WARN_ON(!ovl_dentry_lower(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	origin_layer = OVL_E(dentry)->lowerstack[0].layer->idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (ovl_dentry_test_flag(OVL_E_CONNECTED, dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return origin_layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	/* Find the topmost origin layer connectable ancestor of @dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	next = dget(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (WARN_ON(parent == next)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		 * If @parent is not origin layer connectable, then copy up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		 * @next which is origin layer connectable and we are done.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		if (ovl_connectable_layer(parent) < origin_layer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			err = ovl_encode_maybe_copy_up(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		/* If @parent is connected or indexed we are done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		if (ovl_dentry_test_flag(OVL_E_CONNECTED, parent) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		    ovl_test_flag(OVL_INDEX, d_inode(parent)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 		dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		next = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		ovl_dentry_set_flag(OVL_E_CONNECTED, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return err ?: origin_layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * We only need to encode origin if there is a chance that the same object was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * encoded pre copy up and then we need to stay consistent with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  * encoding also after copy up. If non-pure upper is not indexed, then it was
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * copied up before NFS export was enabled. In that case we don't need to worry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  * about staying consistent with pre copy up encoding and we encode an upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)  * file handle. Overlay root dentry is a private case of non-indexed upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * The following table summarizes the different file handle encodings used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * different overlay object types:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  *  Object type		| Encoding
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  * --------------------------------
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  *  Pure upper		| U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  *  Non-indexed upper	| U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  *  Indexed upper	| L (*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  *  Non-upper		| L (*)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  * U = upper file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)  * L = lower file handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)  * (*) Connecting an overlay dir from real lower dentry is not always
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)  * possible when there are redirects in lower layers and non-indexed merge dirs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)  * To mitigate those case, we may copy up the lower dir ancestor before encode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)  * a lower dir file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * Return 0 for upper file handle, > 0 for lower file handle or < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static int ovl_check_encode_origin(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	/* Upper file handle for pure upper */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (!ovl_dentry_lower(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	 * Upper file handle for non-indexed upper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	 * Root is never indexed, so if there's an upper layer, encode upper for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	 * root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (ovl_dentry_upper(dentry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	    !ovl_test_flag(OVL_INDEX, d_inode(dentry)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * Decoding a merge dir, whose origin's ancestor is under a redirected
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 * lower dir or under a non-indexed upper is not always possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	 * ovl_connect_layer() will try to make origin's layer "connected" by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	 * copying up a "connectable" ancestor.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	if (d_is_dir(dentry) && ovl_upper_mnt(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 		return ovl_connect_layer(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	/* Lower file handle for indexed and non-upper dir/non-dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static int ovl_dentry_to_fid(struct dentry *dentry, u32 *fid, int buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	struct ovl_fh *fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	int err, enc_lower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	 * Check if we should encode a lower or upper file handle and maybe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	 * copy up an ancestor to make lower file handle connectable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 	err = enc_lower = ovl_check_encode_origin(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (enc_lower < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	/* Encode an upper or lower file handle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	fh = ovl_encode_real_fh(enc_lower ? ovl_dentry_lower(dentry) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 				ovl_dentry_upper(dentry), !enc_lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (IS_ERR(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		return PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	len = OVL_FH_LEN(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	if (len <= buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 		memcpy(fid, fh, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	err = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	pr_warn_ratelimited("failed to encode file handle (%pd2, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			    dentry, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) static int ovl_encode_fh(struct inode *inode, u32 *fid, int *max_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			 struct inode *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	int bytes, buflen = *max_len << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	/* TODO: encode connectable file handles */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	if (parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	dentry = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	if (WARN_ON(!dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 	bytes = ovl_dentry_to_fid(dentry, fid, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	if (bytes <= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	*max_len = bytes >> 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	if (bytes > buflen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 		return FILEID_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	return OVL_FILEID_V1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)  * Find or instantiate an overlay dentry from real dentries and index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static struct dentry *ovl_obtain_alias(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 				       struct dentry *upper_alias,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 				       struct ovl_path *lowerpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 				       struct dentry *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	struct dentry *lower = lowerpath ? lowerpath->dentry : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 	struct dentry *upper = upper_alias ?: index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	struct ovl_entry *oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	struct ovl_inode_params oip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 		.lowerpath = lowerpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 		.index = index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 		.numlower = !!lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	/* We get overlay directory dentries with ovl_lookup_real() */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	if (d_is_dir(upper ?: lower))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	oip.upperdentry = dget(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 	inode = ovl_get_inode(sb, &oip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	if (IS_ERR(inode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		return ERR_CAST(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	if (upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		ovl_set_flag(OVL_UPPERDATA, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	dentry = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	if (dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		goto out_iput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	dentry = d_alloc_anon(inode->i_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	if (unlikely(!dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	oe = ovl_alloc_entry(lower ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (!oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		goto nomem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	if (lower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 		oe->lowerstack->dentry = dget(lower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 		oe->lowerstack->layer = lowerpath->layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	dentry->d_fsdata = oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (upper_alias)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		ovl_dentry_set_upper_alias(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ovl_dentry_update_reval(dentry, upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 	return d_instantiate_anon(dentry, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) nomem:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	dentry = ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) out_iput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) /* Get the upper or lower dentry in stach whose on layer @idx */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static struct dentry *ovl_dentry_real_at(struct dentry *dentry, int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	if (!idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 		return ovl_dentry_upper(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 	for (i = 0; i < oe->numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 		if (oe->lowerstack[i].layer->idx == idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 			return oe->lowerstack[i].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * Lookup a child overlay dentry to get a connected overlay dentry whose real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * dentry is @real. If @real is on upper layer, we lookup a child overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * dentry with the same name as the real dentry. Otherwise, we need to consult
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * index for lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct dentry *ovl_lookup_real_one(struct dentry *connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 					  struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 					  const struct ovl_layer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	struct inode *dir = d_inode(connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	struct dentry *this, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 	struct name_snapshot name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	 * Lookup child overlay dentry by real name. The dir mutex protects us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 	 * from racing with overlay rename. If the overlay dentry that is above
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 	 * real has already been moved to a parent that is not under the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	 * connected overlay dir, we return -ECHILD and restart the lookup of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	 * connected real path from the top.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	inode_lock_nested(dir, I_MUTEX_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	err = -ECHILD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	parent = dget_parent(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	if (ovl_dentry_real_at(connected, layer->idx) != parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	 * We also need to take a snapshot of real dentry name to protect us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	 * from racing with underlying layer rename. In this case, we don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 	 * care about returning ESTALE, only from dereferencing a free name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 	 * pointer because we hold no lock on the real dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	take_dentry_name_snapshot(&name, real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	this = lookup_one_len(name.name.name, connected, name.name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	release_dentry_name_snapshot(&name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	err = PTR_ERR(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (IS_ERR(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 	} else if (!this || !this->d_inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 		dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 		err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	} else if (ovl_dentry_real_at(this, layer->idx) != real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 	dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 	inode_unlock(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 	return this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	pr_warn_ratelimited("failed to lookup one by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			    real, layer->idx, connected, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	this = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) static struct dentry *ovl_lookup_real(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				      struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 				      const struct ovl_layer *layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * Lookup an indexed or hashed overlay dentry by real inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static struct dentry *ovl_lookup_real_inode(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 					    struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 					    const struct ovl_layer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	struct dentry *this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	 * Decoding upper dir from index is expensive, so first try to lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	 * overlay dentry in inode/dcache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	inode = ovl_lookup_inode(sb, real, !layer->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 		return ERR_CAST(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 		this = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 		iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 * For decoded lower dir file handle, lookup index by origin to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * if lower dir was copied up and and/or removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	if (!this && layer->idx && ofs->indexdir && !WARN_ON(!d_is_dir(real))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 		index = ovl_lookup_index(ofs, NULL, real, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 		if (IS_ERR(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 			return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	/* Get connected upper overlay dir from index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 	if (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 		struct dentry *upper = ovl_index_upper(ofs, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 		if (IS_ERR_OR_NULL(upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			return upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 		 * ovl_lookup_real() in lower layer may call recursively once to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 		 * ovl_lookup_real() in upper layer. The first level call walks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		 * back lower parents to the topmost indexed parent. The second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 		 * recursive call walks back from indexed upper to the topmost
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		 * connected/hashed upper parent (or up to root).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 		this = ovl_lookup_real(sb, upper, &ofs->layers[0]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	if (IS_ERR_OR_NULL(this))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		return this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	if (ovl_dentry_real_at(this, layer->idx) != real) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 		this = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	return this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)  * Lookup an indexed or hashed overlay dentry, whose real dentry is an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * ancestor of @real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static struct dentry *ovl_lookup_real_ancestor(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 					       struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 					       const struct ovl_layer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	struct dentry *next, *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	struct dentry *ancestor = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	if (real == layer->mnt->mnt_root)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 		return dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 	/* Find the topmost indexed or hashed ancestor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	next = dget(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		 * Lookup a matching overlay dentry in inode/dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 		 * cache or in index by real inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 		ancestor = ovl_lookup_real_inode(sb, next, layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		if (ancestor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 		if (parent == layer->mnt->mnt_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			ancestor = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 		 * If @real has been moved out of the layer root directory,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 		 * we will eventully hit the real fs root. This cannot happen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 		 * by legit overlay rename, so we return error in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 		if (parent == next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 			ancestor = ERR_PTR(-EXDEV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 		dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 		next = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	return ancestor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540)  * Lookup a connected overlay dentry whose real dentry is @real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)  * If @real is on upper layer, we lookup a child overlay dentry with the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)  * path the real dentry. Otherwise, we need to consult index for lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) static struct dentry *ovl_lookup_real(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 				      struct dentry *real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 				      const struct ovl_layer *layer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	struct dentry *connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	connected = ovl_lookup_real_ancestor(sb, real, layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (IS_ERR(connected))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		return connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 	while (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 		struct dentry *next, *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		struct dentry *parent = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		struct dentry *real_connected = ovl_dentry_real_at(connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 								   layer->idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 		if (real_connected == real)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		/* Find the topmost dentry not yet connected */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 		next = dget(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 		for (;;) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 			parent = dget_parent(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			if (parent == real_connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 			 * If real has been moved out of 'real_connected',
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 			 * we will not find 'real_connected' and hit the layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 			 * root. In that case, we need to restart connecting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 			 * This game can go on forever in the worst case. We
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 			 * may want to consider taking s_vfs_rename_mutex if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 			 * this happens more than once.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 			if (parent == layer->mnt->mnt_root) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 				dput(connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 				connected = dget(sb->s_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 			 * If real file has been moved out of the layer root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 			 * directory, we will eventully hit the real fs root.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 			 * This cannot happen by legit overlay rename, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 			 * return error in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 			if (parent == next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 				err = -EXDEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 			dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) 			next = parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 		if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 			this = ovl_lookup_real_one(connected, next, layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 			if (IS_ERR(this))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 				err = PTR_ERR(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 			 * Lookup of child in overlay can fail when racing with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 			 * overlay rename of child away from 'connected' parent.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 			 * In this case, we need to restart the lookup from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 			 * top, because we cannot trust that 'real_connected' is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 			 * still an ancestor of 'real'. There is a good chance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 			 * that the renamed overlay ancestor is now in cache, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 			 * ovl_lookup_real_ancestor() will find it and we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 			 * continue to connect exactly from where lookup failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 			if (err == -ECHILD) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 				this = ovl_lookup_real_ancestor(sb, real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 								layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 				err = PTR_ERR_OR_ZERO(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 			if (!err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 				dput(connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 				connected = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 		dput(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 		dput(next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	return connected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 	pr_warn_ratelimited("failed to lookup by real (%pd2, layer=%d, connected=%pd2, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 			    real, layer->idx, connected, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	dput(connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  * Get an overlay dentry from upper/lower real dentries and index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) static struct dentry *ovl_get_dentry(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) 				     struct dentry *upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) 				     struct ovl_path *lowerpath,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 				     struct dentry *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	const struct ovl_layer *layer = upper ? &ofs->layers[0] : lowerpath->layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	struct dentry *real = upper ?: (index ?: lowerpath->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	 * Obtain a disconnected overlay dentry from a non-dir real dentry
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	 * and index.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 	if (!d_is_dir(real))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) 		return ovl_obtain_alias(sb, upper, lowerpath, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) 	/* Removed empty directory? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	if ((real->d_flags & DCACHE_DISCONNECTED) || d_unhashed(real))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 		return ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	 * If real dentry is connected and hashed, get a connected overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 	 * dentry whose real dentry is @real.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	return ovl_lookup_real(sb, real, layer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) static struct dentry *ovl_upper_fh_to_d(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 					struct ovl_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	struct dentry *dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	struct dentry *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 	if (!ovl_upper_mnt(ofs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 		return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	upper = ovl_decode_real_fh(fh, ovl_upper_mnt(ofs), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	if (IS_ERR_OR_NULL(upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 		return upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 	dentry = ovl_get_dentry(sb, upper, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) static struct dentry *ovl_lower_fh_to_d(struct super_block *sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 					struct ovl_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	struct ovl_fs *ofs = sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	struct ovl_path origin = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 	struct ovl_path *stack = &origin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	struct dentry *dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	/* First lookup overlay inode in inode cache by origin fh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	err = ovl_check_origin_fh(ofs, fh, false, NULL, &stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	if (!d_is_dir(origin.dentry) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	    !(origin.dentry->d_flags & DCACHE_DISCONNECTED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		inode = ovl_lookup_inode(sb, origin.dentry, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 		if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 		if (inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 			dentry = d_find_any_alias(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 			iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 			if (dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 				goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	/* Then lookup indexed upper/whiteout by origin fh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 	if (ofs->indexdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 		index = ovl_get_index_fh(ofs, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 			index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	/* Then try to get a connected upper dir by index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	if (index && d_is_dir(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		struct dentry *upper = ovl_index_upper(ofs, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 		err = PTR_ERR(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		if (IS_ERR_OR_NULL(upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		dentry = ovl_get_dentry(sb, upper, NULL, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	/* Find origin.dentry again with ovl_acceptable() layer check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	if (d_is_dir(origin.dentry)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 		dput(origin.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		origin.dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 		err = ovl_check_origin_fh(ofs, fh, true, NULL, &stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 	if (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 		err = ovl_verify_origin(ofs, index, origin.dentry, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 	/* Get a connected non-upper dir or disconnected non-dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 	dentry = ovl_get_dentry(sb, NULL, &origin, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 	dput(origin.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	dentry = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) static struct ovl_fh *ovl_fid_to_fh(struct fid *fid, int buflen, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	/* If on-wire inner fid is aligned - nothing to do */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	if (fh_type == OVL_FILEID_V1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 		return (struct ovl_fh *)fid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	if (fh_type != OVL_FILEID_V0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	if (buflen <= OVL_FH_WIRE_OFFSET)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	fh = kzalloc(buflen, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	if (!fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	/* Copy unaligned inner fh into aligned buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	memcpy(&fh->fb, fid, buflen - OVL_FH_WIRE_OFFSET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	return fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) static struct dentry *ovl_fh_to_dentry(struct super_block *sb, struct fid *fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 				       int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	struct dentry *dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	struct ovl_fh *fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	int len = fh_len << 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	unsigned int flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 	fh = ovl_fid_to_fh(fid, len, fh_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	err = PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	if (IS_ERR(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	err = ovl_check_fh_len(fh, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	flags = fh->fb.flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 	dentry = (flags & OVL_FH_FLAG_PATH_UPPER) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 		 ovl_upper_fh_to_d(sb, fh) :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 		 ovl_lower_fh_to_d(sb, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	err = PTR_ERR(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	if (IS_ERR(dentry) && err != -ESTALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 	/* We may have needed to re-align OVL_FILEID_V0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 	if (!IS_ERR_OR_NULL(fh) && fh != (void *)fid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 		kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	return dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	pr_warn_ratelimited("failed to decode file handle (len=%d, type=%d, flags=%x, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 			    fh_len, fh_type, flags, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	dentry = ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) static struct dentry *ovl_fh_to_parent(struct super_block *sb, struct fid *fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 				       int fh_len, int fh_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 	pr_warn_ratelimited("connectable file handles not supported; use 'no_subtree_check' exportfs option.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	return ERR_PTR(-EACCES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) static int ovl_get_name(struct dentry *parent, char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 			struct dentry *child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	 * ovl_fh_to_dentry() returns connected dir overlay dentries and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	 * ovl_fh_to_parent() is not implemented, so we should not get here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) static struct dentry *ovl_get_parent(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	 * ovl_fh_to_dentry() returns connected dir overlay dentries, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	 * should not get here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	WARN_ON_ONCE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) const struct export_operations ovl_export_operations = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	.encode_fh	= ovl_encode_fh,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	.fh_to_dentry	= ovl_fh_to_dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	.fh_to_parent	= ovl_fh_to_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	.get_name	= ovl_get_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	.get_parent	= ovl_get_parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) };