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)  * Copyright (C) 2011 Novell Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2016 Red Hat, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/cred.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/xattr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/ratelimit.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/mount.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/exportfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include "overlayfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) struct ovl_lookup_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 	struct super_block *sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 	struct qstr name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 	bool is_dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 	bool opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) 	bool stop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 	bool last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) 	char *redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 	bool metacopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) 			      size_t prelen, const char *post)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	struct ovl_fs *ofs = OVL_FS(d->sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 	buf = ovl_get_redirect_xattr(ofs, dentry, prelen + strlen(post));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	if (IS_ERR_OR_NULL(buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 		return PTR_ERR(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	if (buf[0] == '/') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 		 * One of the ancestor path elements in an absolute path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 		 * lookup in ovl_lookup_layer() could have been opaque and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 		 * that will stop further lookup in lower layers (d->stop=true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 		 * But we have found an absolute redirect in decendant path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 		 * element and that should force continue lookup in lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 		 * layers (reset d->stop).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 		d->stop = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 		res = strlen(buf) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		memmove(buf + prelen, buf, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 		memcpy(buf, d->name.name, prelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	strcat(buf, post);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	kfree(d->redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	d->redirect = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	d->name.name = d->redirect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	d->name.len = strlen(d->redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static int ovl_acceptable(void *ctx, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	 * A non-dir origin may be disconnected, which is fine, because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	 * we only need it for its unique inode number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	if (!d_is_dir(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	/* Don't decode a deleted empty directory */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	if (d_unhashed(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	/* Check if directory belongs to the layer we are decoding from */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82)  * Check validity of an overlay file handle buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84)  * Return 0 for a valid file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85)  * Return -ENODATA for "origin unknown".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86)  * Return <0 for an invalid file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 	if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	if (fb->magic != OVL_FH_MAGIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	/* Treat larger version and unknown flags as "origin unknown" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 	if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 	/* Treat endianness mismatch as "origin unknown" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	    (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 				 enum ovl_xattr ox)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	ssize_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 	struct ovl_fh *fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 	res = ovl_do_getxattr(ofs, dentry, ox, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	if (res < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		if (res == -ENODATA || res == -EOPNOTSUPP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 			return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 	/* Zero size value means "copied up but origin unknown" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	if (res == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	if (!fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	res = ovl_do_getxattr(ofs, dentry, ox, fh->buf, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	if (res < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	err = ovl_check_fb_len(&fh->fb, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 		if (err == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	return fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 	pr_warn_ratelimited("failed to get origin (%zi)\n", res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	pr_warn_ratelimited("invalid origin (%*phN)\n", (int)res, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 				  bool connected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	struct dentry *real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	int bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	 * Make sure that the stored uuid matches the uuid of the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	 * layer where file handle will be decoded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	if (!uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 				  bytes >> 2, (int)fh->fb.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 				  connected ? ovl_acceptable : NULL, mnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	if (IS_ERR(real)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		 * Treat stale file handle to lower file as "origin unknown".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		 * upper file handle could become stale when upper file is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 		 * unlinked and this information is needed to handle stale
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		 * index entries correctly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		if (real == ERR_PTR(-ESTALE) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		    !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			real = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		return real;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	if (ovl_dentry_weird(real)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		dput(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	return real;
^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) static bool ovl_is_opaquedir(struct super_block *sb, struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_OPAQUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static struct dentry *ovl_lookup_positive_unlocked(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 						   struct dentry *base, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 						   bool drop_negative)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	struct dentry *ret = lookup_one_len_unlocked(name, base, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		if (drop_negative && ret->d_lockref.count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 			spin_lock(&ret->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 			/* Recheck condition under lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 			if (d_is_negative(ret) && ret->d_lockref.count == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 				__d_drop(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 			spin_unlock(&ret->d_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		dput(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		ret = ERR_PTR(-ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			     const char *name, unsigned int namelen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			     size_t prelen, const char *post,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			     struct dentry **ret, bool drop_negative)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct dentry *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	bool last_element = !post[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	this = ovl_lookup_positive_unlocked(name, base, namelen, drop_negative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 	if (IS_ERR(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 		err = PTR_ERR(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 		this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		if (err == -ENOENT || err == -ENAMETOOLONG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 	if (ovl_dentry_weird(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		/* Don't support traversing automounts and other weirdness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 		err = -EREMOTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	if (ovl_is_whiteout(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 		d->stop = d->opaque = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 		goto put_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	 * This dentry should be a regular file if previous layer lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	 * found a metacopy dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (last_element && d->metacopy && !d_is_reg(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		d->stop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 		goto put_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	if (!d_can_lookup(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		if (d->is_dir || !last_element) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			d->stop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			goto put_and_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 		err = ovl_check_metacopy_xattr(OVL_FS(d->sb), this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		d->metacopy = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 		d->stop = !d->metacopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 		if (!d->metacopy || d->last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 		if (ovl_lookup_trap_inode(d->sb, this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 			/* Caught in a trap of overlapping layers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 			err = -ELOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 			goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 		if (last_element)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 			d->is_dir = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 		if (d->last)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		if (ovl_is_opaquedir(d->sb, this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 			d->stop = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 			if (last_element)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 				d->opaque = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	err = ovl_check_redirect(this, d, prelen, post);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	*ret = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) put_and_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 			    struct dentry **ret, bool drop_negative)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	/* Counting down from the end, since the prefix can change */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	size_t rem = d->name.len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	struct dentry *dentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	if (d->name.name[0] != '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		return ovl_lookup_single(base, d, d->name.name, d->name.len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 					 0, "", ret, drop_negative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 		const char *s = d->name.name + d->name.len - rem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 		const char *next = strchrnul(s, '/');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		size_t thislen = next - s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		bool end = !next[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		/* Verify we did not go off the rails */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		if (WARN_ON(s[-1] != '/'))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		err = ovl_lookup_single(base, d, s, thislen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 					d->name.len - rem, next, &base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 					drop_negative);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		dput(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 			return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		dentry = base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		rem -= thislen + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 		if (WARN_ON(rem >= d->name.len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 			return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	*ret = dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 			struct dentry *upperdentry, struct ovl_path **stackp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	struct dentry *origin = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	for (i = 1; i < ofs->numlayer; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		 * If lower fs uuid is not unique among lower fs we cannot match
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		 * fh->uuid to layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		if (ofs->layers[i].fsid &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		    ofs->layers[i].fs->bad_uuid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		origin = ovl_decode_real_fh(fh, ofs->layers[i].mnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 					    connected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		if (origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	if (!origin)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		return -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	else if (IS_ERR(origin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		return PTR_ERR(origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	if (upperdentry && !ovl_is_whiteout(upperdentry) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 	    inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 		goto invalid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	if (!*stackp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		*stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	if (!*stackp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 		dput(origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	**stackp = (struct ovl_path){
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		.dentry = origin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		.layer = &ofs->layers[i]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) invalid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			    upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 			    d_inode(origin)->i_mode & S_IFMT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	dput(origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			    struct ovl_path **stackp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	if (IS_ERR_OR_NULL(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		return PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		if (err == -ESTALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416)  * Verify that @fh matches the file handle stored in xattr @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417)  * Return 0 on match, -ESTALE on mismatch, < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 			 enum ovl_xattr ox, const struct ovl_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	if (!ofh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 		return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	if (IS_ERR(ofh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		return PTR_ERR(ofh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	kfree(ofh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439)  * Verify that @real dentry matches the file handle stored in xattr @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441)  * If @set is true and there is no stored file handle, encode @real and store
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442)  * file handle in xattr @name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444)  * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 		      enum ovl_xattr ox, struct dentry *real, bool is_upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		      bool set)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	fh = ovl_encode_real_fh(real, is_upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	err = PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 	if (IS_ERR(fh)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	err = ovl_verify_fh(ofs, dentry, ox, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	if (set && err == -ENODATA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		err = ovl_do_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	inode = d_inode(real);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 			    is_upper ? "upper" : "origin", real,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 			    inode ? inode->i_ino : 0, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) /* Get upper dentry from index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	struct dentry *upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	if (!d_is_dir(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		return dget(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 	if (IS_ERR_OR_NULL(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		return ERR_CAST(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	upper = ovl_decode_real_fh(fh, ovl_upper_mnt(ofs), true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	if (IS_ERR_OR_NULL(upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 		return upper ?: ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	if (!d_is_dir(upper)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 				    index, upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		return ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 	return upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  * Verify that an index entry name matches the origin file handle stored in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	struct ovl_fh *fh = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	struct ovl_path origin = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 	struct ovl_path *stack = &origin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	struct dentry *upper = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	if (!d_inode(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	if (index->d_name.len < sizeof(struct ovl_fb)*2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	len = index->d_name.len / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (!fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (hex2bin(fh->buf, index->d_name.name, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	err = ovl_check_fb_len(&fh->fb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 * Whiteout index entries are used as an indication that an exported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	 * overlay file handle should be treated as stale (i.e. after unlink
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	 * of the overlay inode). These entries contain no origin xattr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	if (ovl_is_whiteout(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	 * Verifying directory index entries are not stale is expensive, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	 * only verify stale dir index if NFS export is enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	if (d_is_dir(index) && !ofs->config.nfs_export)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	 * Directory index entries should have 'upper' xattr pointing to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	 * real upper dir. Non-dir index entries are hardlinks to the upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	 * real inode. For non-dir index, we can read the copy up origin xattr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	 * directly from the index dentry, but for dir index we first need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	 * decode the upper directory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	upper = ovl_index_upper(ofs, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	if (IS_ERR_OR_NULL(upper)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 		err = PTR_ERR(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		 * Directory index entries with no 'upper' xattr need to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		 * removed. When dir index entry has a stale 'upper' xattr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		 * we assume that upper dir was removed and we treat the dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		 * index as orphan entry that needs to be whited out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		if (err == -ESTALE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 			goto orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 		else if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 			err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	dput(upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	/* Check if non-dir index is orphan and don't warn before cleaning it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 		err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			goto orphan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	dput(origin.dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 			    index, d_inode(index)->i_mode & S_IFMT, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) orphan:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 			    index, d_inode(index)->i_mode & S_IFMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			    d_inode(index)->i_nlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	char *n, *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	s  = bin2hex(n, fh->buf, fh->fb.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	*name = (struct qstr) QSTR_INIT(n, s - n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630)  * Lookup in indexdir for the index entry of a lower real inode or a copy up
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631)  * origin inode. The index entry name is the hex representation of the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632)  * inode file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634)  * If the index dentry in negative, then either no lower aliases have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635)  * copied up yet, or aliases have been copied up in older kernels and are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636)  * not indexed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638)  * If the index dentry for a copy up origin inode is positive, but points
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639)  * to an inode different than the upper inode, then either the upper inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640)  * has been copied up and not indexed or it was indexed, but since then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641)  * index dir was cleared. Either way, that index cannot be used to indentify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642)  * the overlay inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) int ovl_get_index_name(struct dentry *origin, struct qstr *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	struct ovl_fh *fh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	fh = ovl_encode_real_fh(origin, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	if (IS_ERR(fh))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		return PTR_ERR(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	err = ovl_get_index_name_fh(fh, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	kfree(fh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) /* Lookup index by file handle for NFS export */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	struct dentry *index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 	struct qstr name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	err = ovl_get_index_name_fh(fh, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	kfree(name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (PTR_ERR(index) == -ENOENT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (ovl_is_whiteout(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		err = -ESTALE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	else if (ovl_dentry_weird(index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 		return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 				struct dentry *origin, bool verify)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	struct dentry *index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 	struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct qstr name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	bool is_dir = d_is_dir(origin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	err = ovl_get_index_name(origin, &name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 		if (err == -ENOENT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 			index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 				    "overlayfs: mount with '-o index=off' to disable inodes index.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 				    d_inode(origin)->i_ino, name.len, name.name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 				    err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	inode = d_inode(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	if (ovl_is_whiteout(index) && !verify) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 		 * When index lookup is called with !verify for decoding an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		 * overlay file handle, a whiteout index implies that decode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		 * should treat file handle as stale and no need to print a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		 * warning about it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		index = ERR_PTR(-ESTALE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	} else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		   inode_wrong_type(inode, d_inode(origin)->i_mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		 * Index should always be of the same file type as origin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 		 * except for the case of a whiteout index. A whiteout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 		 * index should only exist if all lower aliases have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 		 * unlinked, which means that finding a lower origin on lookup
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		 * whose index is a whiteout should be treated as an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 		pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 				    index, d_inode(index)->i_mode & S_IFMT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 				    d_inode(origin)->i_mode & S_IFMT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	} else if (is_dir && verify) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 		if (!upper) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 			pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 					    origin, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 		/* Verify that dir index 'upper' xattr points to upper dir */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		err = ovl_verify_upper(ofs, index, upper, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			if (err == -ESTALE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 				pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 						    upper, origin, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	} else if (upper && d_inode(upper) != inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 		goto out_dput;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	kfree(name.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	return index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) out_dput:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	index = ERR_PTR(-EIO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775)  * Returns next layer in stack starting from top.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776)  * Returns -1 if this is the last layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	struct ovl_entry *oe = dentry->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	BUG_ON(idx < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	if (idx == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 		ovl_path_upper(dentry, path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 		if (path->dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			return oe->numlower ? 1 : -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 		idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	BUG_ON(idx > oe->numlower);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	path->dentry = oe->lowerstack[idx - 1].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	path->mnt = oe->lowerstack[idx - 1].layer->mnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	return (idx < oe->numlower) ? idx + 1 : -1;
^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) /* Fix missing 'origin' xattr */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 			  struct dentry *lower, struct dentry *upper)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	if (ovl_check_origin_xattr(ofs, upper))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	err = ovl_want_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	err = ovl_set_origin(dentry, lower, upper);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 		err = ovl_set_impure(dentry->d_parent, upper->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	ovl_drop_write(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			  unsigned int flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	struct ovl_entry *oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	struct ovl_entry *poe = dentry->d_parent->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	struct ovl_path *stack = NULL, *origin_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	struct dentry *upperdir, *upperdentry = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	struct dentry *origin = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	struct dentry *index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 	unsigned int ctr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	struct inode *inode = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	bool upperopaque = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	char *upperredirect = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	struct dentry *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	bool uppermetacopy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 	struct ovl_lookup_data d = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		.sb = dentry->d_sb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 		.name = dentry->d_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		.is_dir = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		.opaque = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		.stop = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		.last = ofs->config.redirect_follow ? false : !poe->numlower,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		.redirect = NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 		.metacopy = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	if (dentry->d_name.len > ofs->namelen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 		return ERR_PTR(-ENAMETOOLONG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 	upperdir = ovl_dentry_upper(dentry->d_parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	if (upperdir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 			dput(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			err = -EREMOTE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (upperdentry && !d.is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 			 * Lookup copy up origin by decoding origin file handle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 			 * We may get a disconnected dentry, which is fine,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 			 * because we only need to hold the origin inode in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 			 * cache and use its inode number.  We may even get a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 			 * connected dentry, that is not under any of the lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 			 * layers root.  That is also fine for using it's inode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 			 * number - it's the same as if we held a reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 			 * to a dentry in lower layer that was moved under us.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			err = ovl_check_origin(ofs, upperdentry, &origin_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 			if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 				goto out_put_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 			if (d.metacopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 				uppermetacopy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		if (d.redirect) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 			err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 			upperredirect = kstrdup(d.redirect, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 			if (!upperredirect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 				goto out_put_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 			if (d.redirect[0] == '/')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 				poe = roe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 		upperopaque = d.opaque;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	if (!d.stop && poe->numlower) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 				GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		if (!stack)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 			goto out_put_upper;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	for (i = 0; !d.stop && i < poe->numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 		struct ovl_path lower = poe->lowerstack[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		if (!ofs->config.redirect_follow)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			d.last = i == poe->numlower - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			d.last = lower.layer->idx == roe->numlower;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		err = ovl_lookup_layer(lower.dentry, &d, &this, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 		if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		if (!this)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 			dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		 * If no origin fh is stored in upper of a merge dir, store fh
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		 * of lower dir and set upper parent "impure".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 		if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			err = ovl_fix_origin(ofs, dentry, this, upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 				dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 		 * When "verify_lower" feature is enabled, do not merge with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		 * lower dir that does not match a stored origin xattr. In any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		 * case, only verified origin is used for index lookup.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		 * For non-dir dentry, if index=on, then ensure origin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		 * matches the dentry found using path based lookup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		 * otherwise error out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		if (upperdentry && !ctr &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 		    ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 		     (!d.is_dir && ofs->config.index && origin_path))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 			err = ovl_verify_origin(ofs, upperdentry, this, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 			if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 				dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 				if (d.is_dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 				goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 			origin = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		if (d.metacopy && ctr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			 * Do not store intermediate metacopy dentries in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 			 * lower chain, except top most lower metacopy dentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			 * Continue the loop so that if there is an absolute
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			 * redirect on this dentry, poe can be reset to roe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			this = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			stack[ctr].dentry = this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			stack[ctr].layer = lower.layer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			ctr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 		 * Following redirects can have security consequences: it's like
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		 * a symlink into the lower layer without the permission checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		 * This is only a problem if the upper layer is untrusted (e.g
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 		 * comes from an USB drive).  This can allow a non-readable file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		 * or directory to become readable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		 * Only following redirects when redirects are enabled disables
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 		 * this attack vector when not necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		err = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		if (d.redirect && !ofs->config.redirect_follow) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 			pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 					    dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 		if (d.stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 		if (d.redirect && d.redirect[0] == '/' && poe != roe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 			poe = roe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			/* Find the current layer on the root dentry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 			i = lower.layer->idx - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	 * For regular non-metacopy upper dentries, there is no lower
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	 * path based lookup, hence ctr will be zero. If a dentry is found
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	 * using ORIGIN xattr on upper, install it in stack.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	 * For metacopy dentry, path based lookup will find lower dentries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	 * Just make sure a corresponding data dentry has been found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (d.metacopy || (uppermetacopy && !ctr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	} else if (!d.is_dir && upperdentry && !ctr && origin_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		if (WARN_ON(stack != NULL)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 			err = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		stack = origin_path;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 		ctr = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 		origin = origin_path->dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		origin_path = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	 * Always lookup index if there is no-upperdentry.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 	 * For the case of upperdentry, we have set origin by now if it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	 * needed to be set. There are basically three cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	 * For directories, lookup index by lower inode and verify it matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	 * upper inode. We only trust dir index if we verified that lower dir
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 	 * matches origin, otherwise dir index entries may be inconsistent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	 * and we ignore them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	 * For regular upper, we already set origin if upper had ORIGIN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 * xattr. There is no verification though as there is no path
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	 * based dentry lookup in lower in this case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	 * For metacopy upper, we set a verified origin already if index
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	 * is enabled and if upper had an ORIGIN xattr.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	if (!upperdentry && ctr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 		origin = stack[0].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	if (origin && ovl_indexdir(dentry->d_sb) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 	    (!d.is_dir || ovl_index_all(dentry->d_sb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 		index = ovl_lookup_index(ofs, upperdentry, origin, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		if (IS_ERR(index)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 			err = PTR_ERR(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			index = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	oe = ovl_alloc_entry(ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	if (!oe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		goto out_put;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	dentry->d_fsdata = oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	if (upperopaque)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 		ovl_dentry_set_opaque(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 	if (upperdentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 		ovl_dentry_set_upper_alias(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	else if (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		upperdentry = dget(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		upperredirect = ovl_get_redirect_xattr(ofs, upperdentry, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 		if (IS_ERR(upperredirect)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 			err = PTR_ERR(upperredirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 			upperredirect = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 			goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 		err = ovl_check_metacopy_xattr(ofs, upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 			goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		uppermetacopy = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if (upperdentry || ctr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		struct ovl_inode_params oip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 			.upperdentry = upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 			.lowerpath = stack,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 			.index = index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			.numlower = ctr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 			.redirect = upperredirect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 			.lowerdata = (ctr > 1 && !d.is_dir) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 				      stack[ctr - 1].dentry : NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		inode = ovl_get_inode(dentry->d_sb, &oip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 		err = PTR_ERR(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		if (IS_ERR(inode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 			goto out_free_oe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		if (upperdentry && !uppermetacopy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 			ovl_set_flag(OVL_UPPERDATA, inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	ovl_dentry_update_reval(dentry, upperdentry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	if (origin_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 		dput(origin_path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 		kfree(origin_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	kfree(stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 	kfree(d.redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	return d_splice_alias(inode, dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) out_free_oe:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	dentry->d_fsdata = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	kfree(oe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) out_put:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	dput(index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	for (i = 0; i < ctr; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		dput(stack[i].dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	kfree(stack);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) out_put_upper:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 	if (origin_path) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 		dput(origin_path->dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 		kfree(origin_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	dput(upperdentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	kfree(upperredirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	kfree(d.redirect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) bool ovl_lower_positive(struct dentry *dentry)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	struct ovl_entry *poe = dentry->d_parent->d_fsdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	const struct qstr *name = &dentry->d_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	const struct cred *old_cred;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	bool positive = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 	bool done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	 * If dentry is negative, then lower is positive iff this is a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	 * whiteout.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	if (!dentry->d_inode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		return ovl_dentry_is_opaque(dentry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	/* Negative upper -> positive lower */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	if (!ovl_dentry_upper(dentry))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 	old_cred = ovl_override_creds(dentry->d_sb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 	/* Positive upper -> have to look up lower to see whether it exists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	for (i = 0; !done && !positive && i < poe->numlower; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 		struct dentry *this;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 		struct dentry *lowerdir = poe->lowerstack[i].dentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		this = lookup_positive_unlocked(name->name, lowerdir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 					       name->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 		if (IS_ERR(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			switch (PTR_ERR(this)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 			case -ENAMETOOLONG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 			default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 				 * Assume something is there, we just couldn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 				 * access it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 				positive = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			positive = !ovl_is_whiteout(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 			done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 			dput(this);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	ovl_revert_creds(dentry->d_sb, old_cred);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	return positive;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) }