VisionFive2 Linux kernel

StarFive Tech Linux Kernel for VisionFive (JH7110) boards (mirror)

More than 9999 Commits   33 Branches   57 Tags
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    1) /*
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    2) *  Copyright (c) 2004 The Regents of the University of Michigan.
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400    3) *  Copyright (c) 2012 Jeff Layton <jlayton@redhat.com>
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    4) *  All rights reserved.
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    5) *
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    6) *  Andy Adamson <andros@citi.umich.edu>
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    7) *
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    8) *  Redistribution and use in source and binary forms, with or without
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700    9) *  modification, are permitted provided that the following conditions
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   10) *  are met:
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   11) *
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   12) *  1. Redistributions of source code must retain the above copyright
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   13) *     notice, this list of conditions and the following disclaimer.
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   14) *  2. Redistributions in binary form must reproduce the above copyright
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   15) *     notice, this list of conditions and the following disclaimer in the
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   16) *     documentation and/or other materials provided with the distribution.
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   17) *  3. Neither the name of the University nor the names of its
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   18) *     contributors may be used to endorse or promote products derived
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   19) *     from this software without specific prior written permission.
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   20) *
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   21) *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   22) *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   23) *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   24) *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   25) *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   26) *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   27) *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   28) *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   29) *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   30) *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   31) *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   32) *
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   33) */
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   34) 
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800   35) #include <crypto/hash.h>
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   36) #include <linux/file.h>
5a0e3ad6af866 (Tejun Heo            2010-03-24 17:04:11 +0900   37) #include <linux/slab.h>
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   38) #include <linux/namei.h>
e8edc6e03a5c8 (Alexey Dobriyan      2007-05-21 01:22:52 +0400   39) #include <linux/sched.h>
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   40) #include <linux/fs.h>
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400   41) #include <linux/module.h>
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   42) #include <net/net_namespace.h>
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   43) #include <linux/sunrpc/rpc_pipe_fs.h>
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   44) #include <linux/sunrpc/clnt.h>
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   45) #include <linux/nfsd/cld.h>
9a74af21330c8 (Boaz Harrosh         2009-12-03 20:30:56 +0200   46) 
9a74af21330c8 (Boaz Harrosh         2009-12-03 20:30:56 +0200   47) #include "nfsd.h"
9a74af21330c8 (Boaz Harrosh         2009-12-03 20:30:56 +0200   48) #include "state.h"
0a3adadee42f2 (J. Bruce Fields      2009-11-04 18:12:35 -0500   49) #include "vfs.h"
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400   50) #include "netns.h"
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   51) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   52) #define NFSDDBG_FACILITY                NFSDDBG_PROC
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   53) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   54) /* Declarations */
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   55) struct nfsd4_client_tracking_ops {
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   56) 	int (*init)(struct net *);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   57) 	void (*exit)(struct net *);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   58) 	void (*create)(struct nfs4_client *);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   59) 	void (*remove)(struct nfs4_client *);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   60) 	int (*check)(struct nfs4_client *);
919b8049f0e00 (Jeff Layton          2014-09-12 16:40:20 -0400   61) 	void (*grace_done)(struct nfsd_net *);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400   62) 	uint8_t version;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400   63) 	size_t msglen;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   64) };
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400   65) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400   66) static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400   67) static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400   68) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   69) /* Globals */
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400   70) static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery";
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   71) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   72) static int
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   73) nfs4_save_creds(const struct cred **original_creds)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   74) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   75) 	struct cred *new;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   76) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   77) 	new = prepare_creds();
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   78) 	if (!new)
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   79) 		return -ENOMEM;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   80) 
6fab877900030 (Eric W. Biederman    2013-02-02 06:53:11 -0800   81) 	new->fsuid = GLOBAL_ROOT_UID;
6fab877900030 (Eric W. Biederman    2013-02-02 06:53:11 -0800   82) 	new->fsgid = GLOBAL_ROOT_GID;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   83) 	*original_creds = override_creds(new);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   84) 	put_cred(new);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   85) 	return 0;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   86) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   87) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   88) static void
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   89) nfs4_reset_creds(const struct cred *original)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   90) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100   91) 	revert_creds(original);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   92) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700   93) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   94) static void
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   95) md5_to_hex(char *out, char *md5)
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   96) {
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   97) 	int i;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   98) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700   99) 	for (i=0; i<16; i++) {
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  100) 		unsigned char c = md5[i];
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  101) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  102) 		*out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1);
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  103) 		*out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1);
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  104) 	}
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  105) 	*out = '\0';
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  106) }
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  107) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  108) static int
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  109) nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname)
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  110) {
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  111) 	struct xdr_netobj cksum;
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  112) 	struct crypto_shash *tfm;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  113) 	int status;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  114) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  115) 	dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n",
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  116) 			clname->len, clname->data);
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  117) 	tfm = crypto_alloc_shash("md5", 0, 0);
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  118) 	if (IS_ERR(tfm)) {
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  119) 		status = PTR_ERR(tfm);
35058687912aa (Herbert Xu           2006-08-24 19:10:20 +1000  120) 		goto out_no_tfm;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  121) 	}
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  122) 
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  123) 	cksum.len = crypto_shash_digestsize(tfm);
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  124) 	cksum.data = kmalloc(cksum.len, GFP_KERNEL);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  125) 	if (cksum.data == NULL) {
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  126) 		status = -ENOMEM;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  127)  		goto out;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  128) 	}
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  129) 
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700  130) 	status = crypto_shash_tfm_digest(tfm, clname->data, clname->len,
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700  131) 					 cksum.data);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  132) 	if (status)
35058687912aa (Herbert Xu           2006-08-24 19:10:20 +1000  133) 		goto out;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  134) 
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  135) 	md5_to_hex(dname, cksum.data);
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  136) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  137) 	status = 0;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  138) out:
2bd9e7b62e6e1 (Krishna Kumar        2008-10-20 11:47:09 +0530  139) 	kfree(cksum.data);
1edb82d2021c7 (Herbert Xu           2016-01-24 21:20:02 +0800  140) 	crypto_free_shash(tfm);
35058687912aa (Herbert Xu           2006-08-24 19:10:20 +1000  141) out_no_tfm:
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  142) 	return status;
a55370a3c0106 (NeilBrown            2005-06-23 22:03:52 -0700  143) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  144) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  145) /*
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  146)  * If we had an error generating the recdir name for the legacy tracker
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  147)  * then warn the admin. If the error doesn't appear to be transient,
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  148)  * then disable recovery tracking.
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  149)  */
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  150) static void
7255e716b1757 (Jeff Layton          2013-05-09 08:36:23 -0400  151) legacy_recdir_name_error(struct nfs4_client *clp, int error)
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  152) {
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  153) 	printk(KERN_ERR "NFSD: unable to generate recoverydir "
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  154) 			"name (%d).\n", error);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  155) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  156) 	/*
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  157) 	 * if the algorithm just doesn't exist, then disable the recovery
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  158) 	 * tracker altogether. The crypto libs will generally return this if
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  159) 	 * FIPS is enabled as well.
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  160) 	 */
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  161) 	if (error == -ENOENT) {
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  162) 		printk(KERN_ERR "NFSD: disabling legacy clientid tracking. "
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  163) 			"Reboot recovery will not function correctly!\n");
7255e716b1757 (Jeff Layton          2013-05-09 08:36:23 -0400  164) 		nfsd4_client_tracking_exit(clp->net);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  165) 	}
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  166) }
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  167) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  168) static void
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  169) __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp,
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  170) 		const char *dname, int len, struct nfsd_net *nn)
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  171) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  172) 	struct xdr_netobj name;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  173) 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  174) 	struct nfs4_client_reclaim *crp;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  175) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  176) 	name.data = kmemdup(dname, len, GFP_KERNEL);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  177) 	if (!name.data) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  178) 		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  179) 			__func__);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  180) 		return;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  181) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  182) 	name.len = len;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  183) 	crp = nfs4_client_to_reclaim(name, princhash, nn);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  184) 	if (!crp) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  185) 		kfree(name.data);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  186) 		return;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  187) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  188) 	crp->cr_clp = clp;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  189) }
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  190) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  191) static void
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  192) nfsd4_create_clid_dir(struct nfs4_client *clp)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  193) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  194) 	const struct cred *original_cred;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  195) 	char dname[HEXDIR_LEN];
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  196) 	struct dentry *dir, *dentry;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  197) 	int status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  198) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  199) 
a52d726bbd928 (Jeff Layton          2012-03-21 09:52:02 -0400  200) 	if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
7a6ef8c72314f (J. Bruce Fields      2012-01-05 15:38:41 -0500  201) 		return;
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  202) 	if (!nn->rec_file)
7a6ef8c72314f (J. Bruce Fields      2012-01-05 15:38:41 -0500  203) 		return;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  204) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  205) 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  206) 	if (status)
7255e716b1757 (Jeff Layton          2013-05-09 08:36:23 -0400  207) 		return legacy_recdir_name_error(clp, status);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  208) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  209) 	status = nfs4_save_creds(&original_cred);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  210) 	if (status < 0)
7a6ef8c72314f (J. Bruce Fields      2012-01-05 15:38:41 -0500  211) 		return;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  212) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  213) 	status = mnt_want_write_file(nn->rec_file);
4a55c1017b8dc (Jan Kara             2012-06-12 16:20:33 +0200  214) 	if (status)
c2236f141ebb6 (Kinglong Mee         2014-09-02 22:11:27 +0800  215) 		goto out_creds;
4a55c1017b8dc (Jan Kara             2012-06-12 16:20:33 +0200  216) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  217) 	dir = nn->rec_file->f_path.dentry;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  218) 	/* lock the parent */
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  219) 	inode_lock(d_inode(dir));
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  220) 
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  221) 	dentry = lookup_one_len(dname, dir, HEXDIR_LEN-1);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  222) 	if (IS_ERR(dentry)) {
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  223) 		status = PTR_ERR(dentry);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  224) 		goto out_unlock;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  225) 	}
2b0143b5c986b (David Howells        2015-03-17 22:25:59 +0000  226) 	if (d_really_is_positive(dentry))
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  227) 		/*
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  228) 		 * In the 4.1 case, where we're called from
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  229) 		 * reclaim_complete(), records from the previous reboot
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  230) 		 * may still be left, so this is OK.
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  231) 		 *
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  232) 		 * In the 4.0 case, we should never get here; but we may
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  233) 		 * as well be forgiving and just succeed silently.
aec39680b02ed (J. Bruce Fields      2012-01-02 17:30:05 -0500  234) 		 */
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  235) 		goto out_put;
6521f89170829 (Christian Brauner    2021-01-21 14:19:33 +0100  236) 	status = vfs_mkdir(&init_user_ns, d_inode(dir), dentry, S_IRWXU);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  237) out_put:
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  238) 	dput(dentry);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  239) out_unlock:
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  240) 	inode_unlock(d_inode(dir));
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  241) 	if (status == 0) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  242) 		if (nn->in_grace)
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  243) 			__nfsd4_create_reclaim_record_grace(clp, dname,
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  244) 					HEXDIR_LEN, nn);
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  245) 		vfs_fsync(nn->rec_file, 0);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  246) 	} else {
6577aac01f006 (Boaz Harrosh         2011-08-12 17:30:12 -0700  247) 		printk(KERN_ERR "NFSD: failed to write recovery record"
6577aac01f006 (Boaz Harrosh         2011-08-12 17:30:12 -0700  248) 				" (err %d); please check that %s exists"
6577aac01f006 (Boaz Harrosh         2011-08-12 17:30:12 -0700  249) 				" and is writeable", status,
6577aac01f006 (Boaz Harrosh         2011-08-12 17:30:12 -0700  250) 				user_recovery_dirname);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  251) 	}
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  252) 	mnt_drop_write_file(nn->rec_file);
c2236f141ebb6 (Kinglong Mee         2014-09-02 22:11:27 +0800  253) out_creds:
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  254) 	nfs4_reset_creds(original_cred);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  255) }
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  256) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  257) typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  258) 
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  259) struct name_list {
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  260) 	char name[HEXDIR_LEN];
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  261) 	struct list_head list;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  262) };
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  263) 
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  264) struct nfs4_dir_ctx {
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  265) 	struct dir_context ctx;
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  266) 	struct list_head names;
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  267) };
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  268) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  269) static int
ac7576f4b1da8 (Miklos Szeredi       2014-10-30 17:37:34 +0100  270) nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen,
afefdbb28a0a2 (David Howells        2006-10-03 01:13:46 -0700  271) 		loff_t offset, u64 ino, unsigned int d_type)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  272) {
ac7576f4b1da8 (Miklos Szeredi       2014-10-30 17:37:34 +0100  273) 	struct nfs4_dir_ctx *ctx =
ac7576f4b1da8 (Miklos Szeredi       2014-10-30 17:37:34 +0100  274) 		container_of(__ctx, struct nfs4_dir_ctx, ctx);
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  275) 	struct name_list *entry;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  276) 
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  277) 	if (namlen != HEXDIR_LEN - 1)
b37ad28bcaa7c (Al Viro              2006-10-19 23:28:59 -0700  278) 		return 0;
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  279) 	entry = kmalloc(sizeof(struct name_list), GFP_KERNEL);
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  280) 	if (entry == NULL)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  281) 		return -ENOMEM;
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  282) 	memcpy(entry->name, name, HEXDIR_LEN - 1);
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  283) 	entry->name[HEXDIR_LEN - 1] = '\0';
bb6f619b3a49f (Al Viro              2013-05-15 18:49:12 -0400  284) 	list_add(&entry->list, &ctx->names);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  285) 	return 0;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  286) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  287) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  288) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  289) nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  290) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  291) 	const struct cred *original_cred;
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  292) 	struct dentry *dir = nn->rec_file->f_path.dentry;
ac6614b76478e (Al Viro              2013-05-22 22:22:04 -0400  293) 	struct nfs4_dir_ctx ctx = {
ac6614b76478e (Al Viro              2013-05-22 22:22:04 -0400  294) 		.ctx.actor = nfsd4_build_namelist,
ac6614b76478e (Al Viro              2013-05-22 22:22:04 -0400  295) 		.names = LIST_HEAD_INIT(ctx.names)
ac6614b76478e (Al Viro              2013-05-22 22:22:04 -0400  296) 	};
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  297) 	struct name_list *entry, *tmp;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  298) 	int status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  299) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  300) 	status = nfs4_save_creds(&original_cred);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  301) 	if (status < 0)
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  302) 		return status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  303) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  304) 	status = vfs_llseek(nn->rec_file, 0, SEEK_SET);
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  305) 	if (status < 0) {
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  306) 		nfs4_reset_creds(original_cred);
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  307) 		return status;
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  308) 	}
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  309) 
5c0ba4e0762e6 (Al Viro              2013-05-15 13:52:59 -0400  310) 	status = iterate_dir(nn->rec_file, &ctx.ctx);
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  311) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  312) 
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  313) 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  314) 		if (!status) {
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  315) 			struct dentry *dentry;
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  316) 			dentry = lookup_one_len(entry->name, dir, HEXDIR_LEN-1);
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  317) 			if (IS_ERR(dentry)) {
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  318) 				status = PTR_ERR(dentry);
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  319) 				break;
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  320) 			}
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  321) 			status = f(dir, dentry, nn);
5b4b299cc7b6a (Al Viro              2011-07-07 18:43:21 -0400  322) 			dput(dentry);
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  323) 		}
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  324) 		list_del(&entry->list);
05f4f678b0511 (J. Bruce Fields      2009-03-13 16:02:59 -0400  325) 		kfree(entry);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  326) 	}
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  327) 	inode_unlock(d_inode(dir));
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  328) 	nfs4_reset_creds(original_cred);
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  329) 
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  330) 	list_for_each_entry_safe(entry, tmp, &ctx.names, list) {
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  331) 		dprintk("NFSD: %s. Left entry %s\n", __func__, entry->name);
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  332) 		list_del(&entry->list);
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  333) 		kfree(entry);
4691b271ac3a3 (Kinglong Mee         2015-07-07 10:13:02 +0800  334) 	}
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  335) 	return status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  336) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  337) 
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  338) static int
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  339) nfsd4_unlink_clid_dir(char *name, int namlen, struct nfsd_net *nn)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  340) {
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  341) 	struct dentry *dir, *dentry;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  342) 	int status;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  343) 
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  344) 	dprintk("NFSD: nfsd4_unlink_clid_dir. name %.*s\n", namlen, name);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  345) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  346) 	dir = nn->rec_file->f_path.dentry;
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  347) 	inode_lock_nested(d_inode(dir), I_MUTEX_PARENT);
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  348) 	dentry = lookup_one_len(name, dir, namlen);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  349) 	if (IS_ERR(dentry)) {
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  350) 		status = PTR_ERR(dentry);
2f9092e102024 (David Woodhouse      2009-04-20 23:18:37 +0100  351) 		goto out_unlock;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  352) 	}
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  353) 	status = -ENOENT;
2b0143b5c986b (David Howells        2015-03-17 22:25:59 +0000  354) 	if (d_really_is_negative(dentry))
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  355) 		goto out;
6521f89170829 (Christian Brauner    2021-01-21 14:19:33 +0100  356) 	status = vfs_rmdir(&init_user_ns, d_inode(dir), dentry);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  357) out:
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  358) 	dput(dentry);
2f9092e102024 (David Woodhouse      2009-04-20 23:18:37 +0100  359) out_unlock:
5955102c9984f (Al Viro              2016-01-22 15:40:57 -0500  360) 	inode_unlock(d_inode(dir));
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  361) 	return status;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  362) }
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  363) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  364) static void
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  365) __nfsd4_remove_reclaim_record_grace(const char *dname, int len,
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  366) 		struct nfsd_net *nn)
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  367) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  368) 	struct xdr_netobj name;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  369) 	struct nfs4_client_reclaim *crp;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  370) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  371) 	name.data = kmemdup(dname, len, GFP_KERNEL);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  372) 	if (!name.data) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  373) 		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  374) 			__func__);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  375) 		return;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  376) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  377) 	name.len = len;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  378) 	crp = nfsd4_find_reclaim_client(name, nn);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  379) 	kfree(name.data);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  380) 	if (crp)
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  381) 		nfs4_remove_reclaim_record(crp, nn);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  382) }
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  383) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  384) static void
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  385) nfsd4_remove_clid_dir(struct nfs4_client *clp)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  386) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  387) 	const struct cred *original_cred;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  388) 	char dname[HEXDIR_LEN];
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  389) 	int status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  390) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  391) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  392) 	if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  393) 		return;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  394) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  395) 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  396) 	if (status)
7255e716b1757 (Jeff Layton          2013-05-09 08:36:23 -0400  397) 		return legacy_recdir_name_error(clp, status);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  398) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  399) 	status = mnt_want_write_file(nn->rec_file);
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  400) 	if (status)
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  401) 		goto out;
a52d726bbd928 (Jeff Layton          2012-03-21 09:52:02 -0400  402) 	clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  403) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  404) 	status = nfs4_save_creds(&original_cred);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  405) 	if (status < 0)
698d8d875a059 (Jeff Layton          2012-11-09 15:31:53 -0500  406) 		goto out_drop_write;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  407) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  408) 	status = nfsd4_unlink_clid_dir(dname, HEXDIR_LEN-1, nn);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  409) 	nfs4_reset_creds(original_cred);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  410) 	if (status == 0) {
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  411) 		vfs_fsync(nn->rec_file, 0);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  412) 		if (nn->in_grace)
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  413) 			__nfsd4_remove_reclaim_record_grace(dname,
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  414) 					HEXDIR_LEN, nn);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  415) 	}
698d8d875a059 (Jeff Layton          2012-11-09 15:31:53 -0500  416) out_drop_write:
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  417) 	mnt_drop_write_file(nn->rec_file);
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  418) out:
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  419) 	if (status)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  420) 		printk("NFSD: Failed to remove expired client state directory"
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  421) 				" %.*s\n", HEXDIR_LEN, dname);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  422) }
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  423) 
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  424) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  425) purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  426) {
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  427) 	int status;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  428) 	struct xdr_netobj name;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  429) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  430) 	if (child->d_name.len != HEXDIR_LEN - 1) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  431) 		printk("%s: illegal name %pd in recovery directory\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  432) 				__func__, child);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  433) 		/* Keep trying; maybe the others are OK: */
b37ad28bcaa7c (Al Viro              2006-10-19 23:28:59 -0700  434) 		return 0;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  435) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  436) 	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  437) 	if (!name.data) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  438) 		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  439) 			__func__);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  440) 		goto out;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  441) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  442) 	name.len = HEXDIR_LEN;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  443) 	if (nfs4_has_reclaimed_state(name, nn))
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  444) 		goto out_free;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  445) 
6521f89170829 (Christian Brauner    2021-01-21 14:19:33 +0100  446) 	status = vfs_rmdir(&init_user_ns, d_inode(parent), child);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  447) 	if (status)
a6a9f18f0a9f9 (Al Viro              2013-09-16 10:57:01 -0400  448) 		printk("failed to remove client recovery directory %pd\n",
a6a9f18f0a9f9 (Al Viro              2013-09-16 10:57:01 -0400  449) 				child);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  450) out_free:
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  451) 	kfree(name.data);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  452) out:
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  453) 	/* Keep trying, success or failure: */
b37ad28bcaa7c (Al Viro              2006-10-19 23:28:59 -0700  454) 	return 0;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  455) }
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  456) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  457) static void
919b8049f0e00 (Jeff Layton          2014-09-12 16:40:20 -0400  458) nfsd4_recdir_purge_old(struct nfsd_net *nn)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  459) {
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  460) 	int status;
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  461) 
f141f79d709de (Stanislav Kinsbursky 2012-11-26 16:16:30 +0300  462) 	nn->in_grace = false;
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  463) 	if (!nn->rec_file)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  464) 		return;
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  465) 	status = mnt_want_write_file(nn->rec_file);
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  466) 	if (status)
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  467) 		goto out;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  468) 	status = nfsd4_list_rec_dir(purge_old, nn);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  469) 	if (status == 0)
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  470) 		vfs_fsync(nn->rec_file, 0);
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  471) 	mnt_drop_write_file(nn->rec_file);
0622753b800e4 (Dave Hansen          2008-02-15 14:37:34 -0800  472) out:
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  473) 	nfs4_release_reclaim(nn);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  474) 	if (status)
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  475) 		printk("nfsd4: failed to purge old clients from recovery"
a6a9f18f0a9f9 (Al Viro              2013-09-16 10:57:01 -0400  476) 			" directory %pD\n", nn->rec_file);
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  477) }
c7b9a45927e74 (NeilBrown            2005-06-23 22:04:30 -0700  478) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  479) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  480) load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  481) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  482) 	struct xdr_netobj name;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  483) 	struct xdr_netobj princhash = { .len = 0, .data = NULL };
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  484) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  485) 	if (child->d_name.len != HEXDIR_LEN - 1) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  486) 		printk("%s: illegal name %pd in recovery directory\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  487) 				__func__, child);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  488) 		/* Keep trying; maybe the others are OK: */
b37ad28bcaa7c (Al Viro              2006-10-19 23:28:59 -0700  489) 		return 0;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  490) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  491) 	name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  492) 	if (!name.data) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  493) 		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  494) 			__func__);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  495) 		goto out;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  496) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  497) 	name.len = HEXDIR_LEN;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  498) 	if (!nfs4_client_to_reclaim(name, princhash, nn))
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  499) 		kfree(name.data);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  500) out:
b37ad28bcaa7c (Al Viro              2006-10-19 23:28:59 -0700  501) 	return 0;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  502) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  503) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  504) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  505) nfsd4_recdir_load(struct net *net) {
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  506) 	int status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  507) 	struct nfsd_net *nn =  net_generic(net, nfsd_net_id);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  508) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  509) 	if (!nn->rec_file)
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  510) 		return 0;
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  511) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  512) 	status = nfsd4_list_rec_dir(load_recdir, nn);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  513) 	if (status)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  514) 		printk("nfsd4: failed loading clients from recovery"
a6a9f18f0a9f9 (Al Viro              2013-09-16 10:57:01 -0400  515) 			" directory %pD\n", nn->rec_file);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  516) 	return status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  517) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  518) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  519) /*
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  520)  * Hold reference to the recovery directory.
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  521)  */
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  522) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  523) static int
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  524) nfsd4_init_recdir(struct net *net)
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  525) {
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  526) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  527) 	const struct cred *original_cred;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  528) 	int status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  529) 
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  530) 	printk("NFSD: Using %s as the NFSv4 state recovery directory\n",
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  531) 			user_recovery_dirname);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  532) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  533) 	BUG_ON(nn->rec_file);
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  534) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  535) 	status = nfs4_save_creds(&original_cred);
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  536) 	if (status < 0) {
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  537) 		printk("NFSD: Unable to change credentials to find recovery"
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  538) 		       " directory: error %d\n",
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  539) 		       status);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  540) 		return status;
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  541) 	}
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  542) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  543) 	nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0);
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  544) 	if (IS_ERR(nn->rec_file)) {
c2642ab05b855 (J. Bruce Fields      2006-01-18 17:43:29 -0800  545) 		printk("NFSD: unable to find recovery directory %s\n",
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  546) 				user_recovery_dirname);
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  547) 		status = PTR_ERR(nn->rec_file);
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  548) 		nn->rec_file = NULL;
e970a573ce30a (Christoph Hellwig    2010-03-22 17:32:14 +0100  549) 	}
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  550) 
d84f4f992cbd7 (David Howells        2008-11-14 10:39:23 +1100  551) 	nfs4_reset_creds(original_cred);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  552) 	if (!status)
f141f79d709de (Stanislav Kinsbursky 2012-11-26 16:16:30 +0300  553) 		nn->in_grace = true;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  554) 	return status;
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  555) }
190e4fbf96037 (NeilBrown            2005-06-23 22:04:25 -0700  556) 
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  557) static void
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  558) nfsd4_shutdown_recdir(struct net *net)
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  559) {
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  560) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  561) 
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  562) 	if (!nn->rec_file)
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  563) 		return;
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  564) 	fput(nn->rec_file);
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  565) 	nn->rec_file = NULL;
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  566) }
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  567) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  568) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  569) nfs4_legacy_state_init(struct net *net)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  570) {
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  571) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  572) 	int i;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  573) 
6da2ec56059c3 (Kees Cook            2018-06-12 13:55:00 -0700  574) 	nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
6da2ec56059c3 (Kees Cook            2018-06-12 13:55:00 -0700  575) 						sizeof(struct list_head),
6da2ec56059c3 (Kees Cook            2018-06-12 13:55:00 -0700  576) 						GFP_KERNEL);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  577) 	if (!nn->reclaim_str_hashtbl)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  578) 		return -ENOMEM;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  579) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  580) 	for (i = 0; i < CLIENT_HASH_SIZE; i++)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  581) 		INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  582) 	nn->reclaim_str_hashtbl_size = 0;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  583) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  584) 	return 0;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  585) }
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  586) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  587) static void
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  588) nfs4_legacy_state_shutdown(struct net *net)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  589) {
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  590) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  591) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  592) 	kfree(nn->reclaim_str_hashtbl);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  593) }
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  594) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  595) static int
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  596) nfsd4_load_reboot_recovery_data(struct net *net)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  597) {
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  598) 	int status;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  599) 
3a0733692f666 (Stanislav Kinsbursky 2012-11-26 16:16:25 +0300  600) 	status = nfsd4_init_recdir(net);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  601) 	if (status)
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  602) 		return status;
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  603) 
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  604) 	status = nfsd4_recdir_load(net);
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  605) 	if (status)
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  606) 		nfsd4_shutdown_recdir(net);
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  607) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  608) 	return status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  609) }
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  610) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  611) static int
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  612) nfsd4_legacy_tracking_init(struct net *net)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  613) {
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  614) 	int status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  615) 
cc27e0d407021 (Jeff Layton          2012-03-21 09:52:09 -0400  616) 	/* XXX: The legacy code won't work in a container */
cc27e0d407021 (Jeff Layton          2012-03-21 09:52:09 -0400  617) 	if (net != &init_net) {
46cc8ba304450 (Paul Gortmaker       2015-08-25 16:59:16 -0400  618) 		pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n");
cc27e0d407021 (Jeff Layton          2012-03-21 09:52:09 -0400  619) 		return -EINVAL;
cc27e0d407021 (Jeff Layton          2012-03-21 09:52:09 -0400  620) 	}
cc27e0d407021 (Jeff Layton          2012-03-21 09:52:09 -0400  621) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  622) 	status = nfs4_legacy_state_init(net);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  623) 	if (status)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  624) 		return status;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  625) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  626) 	status = nfsd4_load_reboot_recovery_data(net);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  627) 	if (status)
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  628) 		goto err;
f988a7b71d1e6 (Paul Menzel          2021-03-12 22:03:00 +0100  629) 	pr_info("NFSD: Using legacy client tracking operations.\n");
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  630) 	return 0;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  631) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  632) err:
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  633) 	nfs4_legacy_state_shutdown(net);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  634) 	return status;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  635) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  636) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  637) static void
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  638) nfsd4_legacy_tracking_exit(struct net *net)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  639) {
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  640) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  641) 
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  642) 	nfs4_release_reclaim(nn);
15d176c195b16 (Kinglong Mee         2014-09-02 22:12:17 +0800  643) 	nfsd4_shutdown_recdir(net);
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  644) 	nfs4_legacy_state_shutdown(net);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  645) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  646) 
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  647) /*
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  648)  * Change the NFSv4 recovery directory to recdir.
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  649)  */
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  650) int
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  651) nfs4_reset_recoverydir(char *recdir)
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  652) {
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  653) 	int status;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  654) 	struct path path;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  655) 
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  656) 	status = kern_path(recdir, LOOKUP_FOLLOW, &path);
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  657) 	if (status)
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  658) 		return status;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  659) 	status = -ENOTDIR;
e36cb0b89ce20 (David Howells        2015-01-29 12:02:35 +0000  660) 	if (d_is_dir(path.dentry)) {
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  661) 		strcpy(user_recovery_dirname, recdir);
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  662) 		status = 0;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  663) 	}
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  664) 	path_put(&path);
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  665) 	return status;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  666) }
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  667) 
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  668) char *
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  669) nfs4_recoverydir(void)
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  670) {
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  671) 	return user_recovery_dirname;
48483bf23a568 (J. Bruce Fields      2011-08-26 20:40:28 -0400  672) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  673) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  674) static int
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  675) nfsd4_check_legacy_client(struct nfs4_client *clp)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  676) {
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  677) 	int status;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  678) 	char dname[HEXDIR_LEN];
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  679) 	struct nfs4_client_reclaim *crp;
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  680) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  681) 	struct xdr_netobj name;
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  682) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  683) 	/* did we already find that this client is stable? */
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  684) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  685) 		return 0;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  686) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  687) 	status = nfs4_make_rec_clidname(dname, &clp->cl_name);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  688) 	if (status) {
7255e716b1757 (Jeff Layton          2013-05-09 08:36:23 -0400  689) 		legacy_recdir_name_error(clp, status);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  690) 		return status;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  691) 	}
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500  692) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  693) 	/* look for it in the reclaim hashtable otherwise */
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  694) 	name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  695) 	if (!name.data) {
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  696) 		dprintk("%s: failed to allocate memory for name.data!\n",
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  697) 			__func__);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  698) 		goto out_enoent;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  699) 	}
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  700) 	name.len = HEXDIR_LEN;
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  701) 	crp = nfsd4_find_reclaim_client(name, nn);
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  702) 	kfree(name.data);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  703) 	if (crp) {
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  704) 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
0ce0c2b5d2308 (Jeff Layton          2012-11-12 15:00:55 -0500  705) 		crp->cr_clp = clp;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  706) 		return 0;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  707) 	}
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  708) 
6b1891052a3f8 (Scott Mayhew         2019-03-26 18:06:26 -0400  709) out_enoent:
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  710) 	return -ENOENT;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  711) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  712) 
7c582e4faaf25 (Julia Lawall         2015-11-22 08:22:10 +0100  713) static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = {
52e19c09a183d (Stanislav Kinsbursky 2012-11-14 18:21:16 +0300  714) 	.init		= nfsd4_legacy_tracking_init,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  715) 	.exit		= nfsd4_legacy_tracking_exit,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  716) 	.create		= nfsd4_create_clid_dir,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  717) 	.remove		= nfsd4_remove_clid_dir,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  718) 	.check		= nfsd4_check_legacy_client,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  719) 	.grace_done	= nfsd4_recdir_purge_old,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  720) 	.version	= 1,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  721) 	.msglen		= 0,
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  722) };
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400  723) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  724) /* Globals */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  725) #define NFSD_PIPE_DIR		"nfsd"
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  726) #define NFSD_CLD_PIPE		"cld"
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  727) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  728) /* per-net-ns structure for holding cld upcall info */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  729) struct cld_net {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  730) 	struct rpc_pipe		*cn_pipe;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  731) 	spinlock_t		 cn_lock;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  732) 	struct list_head	 cn_list;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  733) 	unsigned int		 cn_xid;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  734) 	bool			 cn_has_legacy;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  735) 	struct crypto_shash	*cn_tfm;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  736) };
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  737) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  738) struct cld_upcall {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  739) 	struct list_head	 cu_list;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  740) 	struct cld_net		*cu_net;
b493fd31c0b89 (Scott Mayhew         2018-11-06 13:35:08 -0500  741) 	struct completion	 cu_done;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  742) 	union {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  743) 		struct cld_msg_hdr	 cu_hdr;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  744) 		struct cld_msg		 cu_msg;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  745) 		struct cld_msg_v2	 cu_msg_v2;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  746) 	} cu_u;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  747) };
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  748) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  749) static int
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400  750) __cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  751) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  752) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  753) 	struct rpc_pipe_msg msg;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  754) 	struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  755) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  756) 	memset(&msg, 0, sizeof(msg));
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  757) 	msg.data = cmsg;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  758) 	msg.len = nn->client_tracking_ops->msglen;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  759) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  760) 	ret = rpc_queue_upcall(pipe, &msg);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  761) 	if (ret < 0) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  762) 		goto out;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  763) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  764) 
b493fd31c0b89 (Scott Mayhew         2018-11-06 13:35:08 -0500  765) 	wait_for_completion(&cup->cu_done);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  766) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  767) 	if (msg.errno < 0)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  768) 		ret = msg.errno;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  769) out:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  770) 	return ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  771) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  772) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  773) static int
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400  774) cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  775) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  776) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  777) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  778) 	/*
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  779) 	 * -EAGAIN occurs when pipe is closed and reopened while there are
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  780) 	 *  upcalls queued.
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  781) 	 */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  782) 	do {
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400  783) 		ret = __cld_pipe_upcall(pipe, cmsg, nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  784) 	} while (ret == -EAGAIN);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  785) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  786) 	return ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  787) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  788) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  789) static ssize_t
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  790) __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  791) 		struct nfsd_net *nn)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  792) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  793) 	uint8_t cmd, princhashlen;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  794) 	struct xdr_netobj name, princhash = { .len = 0, .data = NULL };
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  795) 	uint16_t namelen;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  796) 	struct cld_net *cn = nn->cld_net;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  797) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  798) 	if (get_user(cmd, &cmsg->cm_cmd)) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  799) 		dprintk("%s: error when copying cmd from userspace", __func__);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  800) 		return -EFAULT;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  801) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  802) 	if (cmd == Cld_GraceStart) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  803) 		if (nn->client_tracking_ops->version >= 2) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  804) 			const struct cld_clntinfo __user *ci;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  805) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  806) 			ci = &cmsg->cm_u.cm_clntinfo;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  807) 			if (get_user(namelen, &ci->cc_name.cn_len))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  808) 				return -EFAULT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  809) 			name.data = memdup_user(&ci->cc_name.cn_id, namelen);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  810) 			if (IS_ERR_OR_NULL(name.data))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  811) 				return -EFAULT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  812) 			name.len = namelen;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  813) 			get_user(princhashlen, &ci->cc_princhash.cp_len);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  814) 			if (princhashlen > 0) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  815) 				princhash.data = memdup_user(
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  816) 						&ci->cc_princhash.cp_data,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  817) 						princhashlen);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  818) 				if (IS_ERR_OR_NULL(princhash.data))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  819) 					return -EFAULT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  820) 				princhash.len = princhashlen;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  821) 			} else
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  822) 				princhash.len = 0;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  823) 		} else {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  824) 			const struct cld_name __user *cnm;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  825) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  826) 			cnm = &cmsg->cm_u.cm_name;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  827) 			if (get_user(namelen, &cnm->cn_len))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  828) 				return -EFAULT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  829) 			name.data = memdup_user(&cnm->cn_id, namelen);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  830) 			if (IS_ERR_OR_NULL(name.data))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  831) 				return -EFAULT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  832) 			name.len = namelen;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  833) 		}
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  834) 		if (name.len > 5 && memcmp(name.data, "hash:", 5) == 0) {
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  835) 			name.len = name.len - 5;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  836) 			memmove(name.data, name.data + 5, name.len);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  837) 			cn->cn_has_legacy = true;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400  838) 		}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  839) 		if (!nfs4_client_to_reclaim(name, princhash, nn)) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  840) 			kfree(name.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  841) 			kfree(princhash.data);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  842) 			return -EFAULT;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  843) 		}
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  844) 		return nn->client_tracking_ops->msglen;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  845) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  846) 	return -EFAULT;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  847) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  848) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  849) static ssize_t
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  850) cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  851) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  852) 	struct cld_upcall *tmp, *cup;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  853) 	struct cld_msg_hdr __user *hdr = (struct cld_msg_hdr __user *)src;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  854) 	struct cld_msg_v2 __user *cmsg = (struct cld_msg_v2 __user *)src;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  855) 	uint32_t xid;
ef8a1a10e95be (Al Viro              2014-10-31 00:41:12 -0400  856) 	struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  857) 						nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  858) 	struct cld_net *cn = nn->cld_net;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  859) 	int16_t status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  860) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  861) 	if (mlen != nn->client_tracking_ops->msglen) {
8a7dc4b04b22b (Randy Dunlap         2012-04-30 12:25:31 -0700  862) 		dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  863) 			nn->client_tracking_ops->msglen);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  864) 		return -EINVAL;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  865) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  866) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  867) 	/* copy just the xid so we can try to find that */
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  868) 	if (copy_from_user(&xid, &hdr->cm_xid, sizeof(xid)) != 0) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  869) 		dprintk("%s: error when copying xid from userspace", __func__);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  870) 		return -EFAULT;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  871) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  872) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  873) 	/*
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  874) 	 * copy the status so we know whether to remove the upcall from the
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  875) 	 * list (for -EINPROGRESS, we just want to make sure the xid is
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  876) 	 * valid, not remove the upcall from the list)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  877) 	 */
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  878) 	if (get_user(status, &hdr->cm_status)) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  879) 		dprintk("%s: error when copying status from userspace", __func__);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  880) 		return -EFAULT;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  881) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  882) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  883) 	/* walk the list and find corresponding xid */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  884) 	cup = NULL;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  885) 	spin_lock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  886) 	list_for_each_entry(tmp, &cn->cn_list, cu_list) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  887) 		if (get_unaligned(&tmp->cu_u.cu_hdr.cm_xid) == xid) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  888) 			cup = tmp;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  889) 			if (status != -EINPROGRESS)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  890) 				list_del_init(&cup->cu_list);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  891) 			break;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  892) 		}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  893) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  894) 	spin_unlock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  895) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  896) 	/* couldn't find upcall? */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  897) 	if (!cup) {
21f72c9f0a677 (Jeff Layton          2012-03-28 07:36:01 -0400  898) 		dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  899) 		return -EINVAL;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  900) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  901) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  902) 	if (status == -EINPROGRESS)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  903) 		return __cld_pipe_inprogress_downcall(cmsg, nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400  904) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400  905) 	if (copy_from_user(&cup->cu_u.cu_msg_v2, src, mlen) != 0)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  906) 		return -EFAULT;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  907) 
b493fd31c0b89 (Scott Mayhew         2018-11-06 13:35:08 -0500  908) 	complete(&cup->cu_done);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  909) 	return mlen;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  910) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  911) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  912) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  913) cld_pipe_destroy_msg(struct rpc_pipe_msg *msg)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  914) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  915) 	struct cld_msg *cmsg = msg->data;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  916) 	struct cld_upcall *cup = container_of(cmsg, struct cld_upcall,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400  917) 						 cu_u.cu_msg);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  918) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  919) 	/* errno >= 0 means we got a downcall */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  920) 	if (msg->errno >= 0)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  921) 		return;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  922) 
b493fd31c0b89 (Scott Mayhew         2018-11-06 13:35:08 -0500  923) 	complete(&cup->cu_done);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  924) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  925) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  926) static const struct rpc_pipe_ops cld_upcall_ops = {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  927) 	.upcall		= rpc_pipe_generic_upcall,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  928) 	.downcall	= cld_pipe_downcall,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  929) 	.destroy_msg	= cld_pipe_destroy_msg,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  930) };
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  931) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  932) static struct dentry *
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  933) nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  934) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  935) 	struct dentry *dir, *dentry;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  936) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  937) 	dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  938) 	if (dir == NULL)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  939) 		return ERR_PTR(-ENOENT);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  940) 	dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  941) 	dput(dir);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  942) 	return dentry;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  943) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  944) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  945) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  946) nfsd4_cld_unregister_sb(struct rpc_pipe *pipe)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  947) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  948) 	if (pipe->dentry)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  949) 		rpc_unlink(pipe->dentry);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  950) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  951) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  952) static struct dentry *
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  953) nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  954) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  955) 	struct super_block *sb;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  956) 	struct dentry *dentry;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  957) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  958) 	sb = rpc_get_sb_net(net);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  959) 	if (!sb)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  960) 		return NULL;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  961) 	dentry = nfsd4_cld_register_sb(sb, pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  962) 	rpc_put_sb_net(net);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  963) 	return dentry;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  964) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  965) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  966) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  967) nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  968) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  969) 	struct super_block *sb;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  970) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  971) 	sb = rpc_get_sb_net(net);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  972) 	if (sb) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  973) 		nfsd4_cld_unregister_sb(pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  974) 		rpc_put_sb_net(net);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  975) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  976) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  977) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  978) /* Initialize rpc_pipefs pipe for communication with client tracking daemon */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  979) static int
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400  980) __nfsd4_init_cld_pipe(struct net *net)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  981) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  982) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  983) 	struct dentry *dentry;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  984) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  985) 	struct cld_net *cn;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  986) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  987) 	if (nn->cld_net)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  988) 		return 0;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  989) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  990) 	cn = kzalloc(sizeof(*cn), GFP_KERNEL);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  991) 	if (!cn) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  992) 		ret = -ENOMEM;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  993) 		goto err;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  994) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  995) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  996) 	cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  997) 	if (IS_ERR(cn->cn_pipe)) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  998) 		ret = PTR_ERR(cn->cn_pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400  999) 		goto err;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1000) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1001) 	spin_lock_init(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1002) 	INIT_LIST_HEAD(&cn->cn_list);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1003) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1004) 	dentry = nfsd4_cld_register_net(net, cn->cn_pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1005) 	if (IS_ERR(dentry)) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1006) 		ret = PTR_ERR(dentry);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1007) 		goto err_destroy_data;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1008) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1009) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1010) 	cn->cn_pipe->dentry = dentry;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1011) 	cn->cn_has_legacy = false;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1012) 	nn->cld_net = cn;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1013) 	return 0;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1014) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1015) err_destroy_data:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1016) 	rpc_destroy_pipe_data(cn->cn_pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1017) err:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1018) 	kfree(cn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1019) 	printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n",
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1020) 			ret);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1021) 	return ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1022) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1023) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1024) static int
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1025) nfsd4_init_cld_pipe(struct net *net)
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1026) {
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1027) 	int status;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1028) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1029) 	status = __nfsd4_init_cld_pipe(net);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1030) 	if (!status)
f988a7b71d1e6 (Paul Menzel          2021-03-12 22:03:00 +0100 1031) 		pr_info("NFSD: Using old nfsdcld client tracking operations.\n");
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1032) 	return status;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1033) }
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1034) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1035) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1036) nfsd4_remove_cld_pipe(struct net *net)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1037) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1038) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1039) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1040) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1041) 	nfsd4_cld_unregister_net(net, cn->cn_pipe);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1042) 	rpc_destroy_pipe_data(cn->cn_pipe);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1043) 	if (cn->cn_tfm)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1044) 		crypto_free_shash(cn->cn_tfm);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1045) 	kfree(nn->cld_net);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1046) 	nn->cld_net = NULL;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1047) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1048) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1049) static struct cld_upcall *
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1050) alloc_cld_upcall(struct nfsd_net *nn)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1051) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1052) 	struct cld_upcall *new, *tmp;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1053) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1054) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1055) 	new = kzalloc(sizeof(*new), GFP_KERNEL);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1056) 	if (!new)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1057) 		return new;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1058) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1059) 	/* FIXME: hard cap on number in flight? */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1060) restart_search:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1061) 	spin_lock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1062) 	list_for_each_entry(tmp, &cn->cn_list, cu_list) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1063) 		if (tmp->cu_u.cu_msg.cm_xid == cn->cn_xid) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1064) 			cn->cn_xid++;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1065) 			spin_unlock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1066) 			goto restart_search;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1067) 		}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1068) 	}
b493fd31c0b89 (Scott Mayhew         2018-11-06 13:35:08 -0500 1069) 	init_completion(&new->cu_done);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1070) 	new->cu_u.cu_msg.cm_vers = nn->client_tracking_ops->version;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1071) 	put_unaligned(cn->cn_xid++, &new->cu_u.cu_msg.cm_xid);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1072) 	new->cu_net = cn;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1073) 	list_add(&new->cu_list, &cn->cn_list);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1074) 	spin_unlock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1075) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1076) 	dprintk("%s: allocated xid %u\n", __func__, new->cu_u.cu_msg.cm_xid);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1077) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1078) 	return new;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1079) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1080) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1081) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1082) free_cld_upcall(struct cld_upcall *victim)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1083) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1084) 	struct cld_net *cn = victim->cu_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1085) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1086) 	spin_lock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1087) 	list_del(&victim->cu_list);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1088) 	spin_unlock(&cn->cn_lock);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1089) 	kfree(victim);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1090) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1091) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1092) /* Ask daemon to create a new record */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1093) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1094) nfsd4_cld_create(struct nfs4_client *clp)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1095) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1096) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1097) 	struct cld_upcall *cup;
c212cecfa21b3 (Stanislav Kinsbursky 2012-11-14 18:21:10 +0300 1098) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1099) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1100) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1101) 	/* Don't upcall if it's already stored */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1102) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1103) 		return;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1104) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1105) 	cup = alloc_cld_upcall(nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1106) 	if (!cup) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1107) 		ret = -ENOMEM;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1108) 		goto out_err;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1109) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1110) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1111) 	cup->cu_u.cu_msg.cm_cmd = Cld_Create;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1112) 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1113) 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1114) 			clp->cl_name.len);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1115) 
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1116) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1117) 	if (!ret) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1118) 		ret = cup->cu_u.cu_msg.cm_status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1119) 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1120) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1121) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1122) 	free_cld_upcall(cup);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1123) out_err:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1124) 	if (ret)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1125) 		printk(KERN_ERR "NFSD: Unable to create client "
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1126) 				"record on stable storage: %d\n", ret);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1127) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1128) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1129) /* Ask daemon to create a new record */
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1130) static void
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1131) nfsd4_cld_create_v2(struct nfs4_client *clp)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1132) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1133) 	int ret;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1134) 	struct cld_upcall *cup;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1135) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1136) 	struct cld_net *cn = nn->cld_net;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1137) 	struct cld_msg_v2 *cmsg;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1138) 	struct crypto_shash *tfm = cn->cn_tfm;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1139) 	struct xdr_netobj cksum;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1140) 	char *principal = NULL;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1141) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1142) 	/* Don't upcall if it's already stored */
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1143) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1144) 		return;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1145) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1146) 	cup = alloc_cld_upcall(nn);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1147) 	if (!cup) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1148) 		ret = -ENOMEM;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1149) 		goto out_err;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1150) 	}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1151) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1152) 	cmsg = &cup->cu_u.cu_msg_v2;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1153) 	cmsg->cm_cmd = Cld_Create;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1154) 	cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1155) 	memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1156) 			clp->cl_name.len);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1157) 	if (clp->cl_cred.cr_raw_principal)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1158) 		principal = clp->cl_cred.cr_raw_principal;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1159) 	else if (clp->cl_cred.cr_principal)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1160) 		principal = clp->cl_cred.cr_principal;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1161) 	if (principal) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1162) 		cksum.len = crypto_shash_digestsize(tfm);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1163) 		cksum.data = kmalloc(cksum.len, GFP_KERNEL);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1164) 		if (cksum.data == NULL) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1165) 			ret = -ENOMEM;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1166) 			goto out;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1167) 		}
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700 1168) 		ret = crypto_shash_tfm_digest(tfm, principal, strlen(principal),
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700 1169) 					      cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1170) 		if (ret) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1171) 			kfree(cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1172) 			goto out;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1173) 		}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1174) 		cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = cksum.len;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1175) 		memcpy(cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1176) 		       cksum.data, cksum.len);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1177) 		kfree(cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1178) 	} else
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1179) 		cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1180) 
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1181) 	ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1182) 	if (!ret) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1183) 		ret = cmsg->cm_status;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1184) 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1185) 	}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1186) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1187) out:
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1188) 	free_cld_upcall(cup);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1189) out_err:
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1190) 	if (ret)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1191) 		pr_err("NFSD: Unable to create client record on stable storage: %d\n",
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1192) 				ret);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1193) }
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1194) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1195) /* Ask daemon to create a new record */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1196) static void
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1197) nfsd4_cld_remove(struct nfs4_client *clp)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1198) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1199) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1200) 	struct cld_upcall *cup;
c212cecfa21b3 (Stanislav Kinsbursky 2012-11-14 18:21:10 +0300 1201) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1202) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1203) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1204) 	/* Don't upcall if it's already removed */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1205) 	if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1206) 		return;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1207) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1208) 	cup = alloc_cld_upcall(nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1209) 	if (!cup) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1210) 		ret = -ENOMEM;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1211) 		goto out_err;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1212) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1213) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1214) 	cup->cu_u.cu_msg.cm_cmd = Cld_Remove;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1215) 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1216) 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1217) 			clp->cl_name.len);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1218) 
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1219) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1220) 	if (!ret) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1221) 		ret = cup->cu_u.cu_msg.cm_status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1222) 		clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1223) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1224) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1225) 	free_cld_upcall(cup);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1226) out_err:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1227) 	if (ret)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1228) 		printk(KERN_ERR "NFSD: Unable to remove client "
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1229) 				"record from stable storage: %d\n", ret);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1230) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1231) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1232) /*
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1233)  * For older nfsdcld's that do not allow us to "slurp" the clients
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1234)  * from the tracking database during startup.
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1235)  *
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1236)  * Check for presence of a record, and update its timestamp
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1237)  */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1238) static int
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1239) nfsd4_cld_check_v0(struct nfs4_client *clp)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1240) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1241) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1242) 	struct cld_upcall *cup;
c212cecfa21b3 (Stanislav Kinsbursky 2012-11-14 18:21:10 +0300 1243) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1244) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1245) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1246) 	/* Don't upcall if one was already stored during this grace pd */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1247) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1248) 		return 0;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1249) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1250) 	cup = alloc_cld_upcall(nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1251) 	if (!cup) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1252) 		printk(KERN_ERR "NFSD: Unable to check client record on "
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1253) 				"stable storage: %d\n", -ENOMEM);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1254) 		return -ENOMEM;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1255) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1256) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1257) 	cup->cu_u.cu_msg.cm_cmd = Cld_Check;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1258) 	cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1259) 	memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1260) 			clp->cl_name.len);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1261) 
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1262) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1263) 	if (!ret) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1264) 		ret = cup->cu_u.cu_msg.cm_status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1265) 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1266) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1267) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1268) 	free_cld_upcall(cup);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1269) 	return ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1270) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1271) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1272) /*
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1273)  * For newer nfsdcld's that allow us to "slurp" the clients
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1274)  * from the tracking database during startup.
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1275)  *
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1276)  * Check for presence of a record in the reclaim_str_hashtbl
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1277)  */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1278) static int
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1279) nfsd4_cld_check(struct nfs4_client *clp)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1280) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1281) 	struct nfs4_client_reclaim *crp;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1282) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1283) 	struct cld_net *cn = nn->cld_net;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1284) 	int status;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1285) 	char dname[HEXDIR_LEN];
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1286) 	struct xdr_netobj name;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1287) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1288) 	/* did we already find that this client is stable? */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1289) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1290) 		return 0;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1291) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1292) 	/* look for it in the reclaim hashtable otherwise */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1293) 	crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1294) 	if (crp)
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1295) 		goto found;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1296) 
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1297) 	if (cn->cn_has_legacy) {
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1298) 		status = nfs4_make_rec_clidname(dname, &clp->cl_name);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1299) 		if (status)
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1300) 			return -ENOENT;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1301) 
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1302) 		name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1303) 		if (!name.data) {
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1304) 			dprintk("%s: failed to allocate memory for name.data!\n",
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1305) 				__func__);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1306) 			return -ENOENT;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1307) 		}
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1308) 		name.len = HEXDIR_LEN;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1309) 		crp = nfsd4_find_reclaim_client(name, nn);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1310) 		kfree(name.data);
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1311) 		if (crp)
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1312) 			goto found;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1313) 
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1314) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1315) 	return -ENOENT;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1316) found:
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1317) 	crp->cr_clp = clp;
8a9f4f41248a4 (Scott Mayhew         2019-03-26 18:06:30 -0400 1318) 	return 0;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1319) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1320) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1321) static int
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1322) nfsd4_cld_check_v2(struct nfs4_client *clp)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1323) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1324) 	struct nfs4_client_reclaim *crp;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1325) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1326) 	struct cld_net *cn = nn->cld_net;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1327) 	int status;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1328) 	char dname[HEXDIR_LEN];
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1329) 	struct xdr_netobj name;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1330) 	struct crypto_shash *tfm = cn->cn_tfm;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1331) 	struct xdr_netobj cksum;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1332) 	char *principal = NULL;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1333) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1334) 	/* did we already find that this client is stable? */
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1335) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1336) 		return 0;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1337) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1338) 	/* look for it in the reclaim hashtable otherwise */
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1339) 	crp = nfsd4_find_reclaim_client(clp->cl_name, nn);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1340) 	if (crp)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1341) 		goto found;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1342) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1343) 	if (cn->cn_has_legacy) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1344) 		status = nfs4_make_rec_clidname(dname, &clp->cl_name);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1345) 		if (status)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1346) 			return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1347) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1348) 		name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1349) 		if (!name.data) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1350) 			dprintk("%s: failed to allocate memory for name.data\n",
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1351) 					__func__);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1352) 			return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1353) 		}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1354) 		name.len = HEXDIR_LEN;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1355) 		crp = nfsd4_find_reclaim_client(name, nn);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1356) 		kfree(name.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1357) 		if (crp)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1358) 			goto found;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1359) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1360) 	}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1361) 	return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1362) found:
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1363) 	if (crp->cr_princhash.len) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1364) 		if (clp->cl_cred.cr_raw_principal)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1365) 			principal = clp->cl_cred.cr_raw_principal;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1366) 		else if (clp->cl_cred.cr_principal)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1367) 			principal = clp->cl_cred.cr_principal;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1368) 		if (principal == NULL)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1369) 			return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1370) 		cksum.len = crypto_shash_digestsize(tfm);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1371) 		cksum.data = kmalloc(cksum.len, GFP_KERNEL);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1372) 		if (cksum.data == NULL)
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1373) 			return -ENOENT;
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700 1374) 		status = crypto_shash_tfm_digest(tfm, principal,
ea794db2646a3 (Eric Biggers         2020-05-01 22:31:17 -0700 1375) 						 strlen(principal), cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1376) 		if (status) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1377) 			kfree(cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1378) 			return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1379) 		}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1380) 		if (memcmp(crp->cr_princhash.data, cksum.data,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1381) 				crp->cr_princhash.len)) {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1382) 			kfree(cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1383) 			return -ENOENT;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1384) 		}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1385) 		kfree(cksum.data);
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1386) 	}
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1387) 	crp->cr_clp = clp;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1388) 	return 0;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1389) }
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1390) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1391) static int
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1392) nfsd4_cld_grace_start(struct nfsd_net *nn)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1393) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1394) 	int ret;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1395) 	struct cld_upcall *cup;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1396) 	struct cld_net *cn = nn->cld_net;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1397) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1398) 	cup = alloc_cld_upcall(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1399) 	if (!cup) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1400) 		ret = -ENOMEM;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1401) 		goto out_err;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1402) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1403) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1404) 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart;
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1405) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1406) 	if (!ret)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1407) 		ret = cup->cu_u.cu_msg.cm_status;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1408) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1409) 	free_cld_upcall(cup);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1410) out_err:
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1411) 	if (ret)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1412) 		dprintk("%s: Unable to get clients from userspace: %d\n",
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1413) 			__func__, ret);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1414) 	return ret;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1415) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1416) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1417) /* For older nfsdcld's that need cm_gracetime */
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1418) static void
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1419) nfsd4_cld_grace_done_v0(struct nfsd_net *nn)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1420) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1421) 	int ret;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1422) 	struct cld_upcall *cup;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1423) 	struct cld_net *cn = nn->cld_net;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1424) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1425) 	cup = alloc_cld_upcall(nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1426) 	if (!cup) {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1427) 		ret = -ENOMEM;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1428) 		goto out_err;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1429) 	}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1430) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1431) 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
9cc7680149b23 (Arnd Bergmann        2019-10-31 15:53:13 +0100 1432) 	cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time;
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1433) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1434) 	if (!ret)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1435) 		ret = cup->cu_u.cu_msg.cm_status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1436) 
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1437) 	free_cld_upcall(cup);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1438) out_err:
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1439) 	if (ret)
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1440) 		printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1441) }
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1442) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1443) /*
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1444)  * For newer nfsdcld's that do not need cm_gracetime.  We also need to call
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1445)  * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl.
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1446)  */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1447) static void
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1448) nfsd4_cld_grace_done(struct nfsd_net *nn)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1449) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1450) 	int ret;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1451) 	struct cld_upcall *cup;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1452) 	struct cld_net *cn = nn->cld_net;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1453) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1454) 	cup = alloc_cld_upcall(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1455) 	if (!cup) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1456) 		ret = -ENOMEM;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1457) 		goto out_err;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1458) 	}
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1459) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1460) 	cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone;
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1461) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1462) 	if (!ret)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1463) 		ret = cup->cu_u.cu_msg.cm_status;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1464) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1465) 	free_cld_upcall(cup);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1466) out_err:
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1467) 	nfs4_release_reclaim(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1468) 	if (ret)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1469) 		printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1470) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1471) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1472) static int
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1473) nfs4_cld_state_init(struct net *net)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1474) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1475) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1476) 	int i;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1477) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1478) 	nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1479) 						sizeof(struct list_head),
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1480) 						GFP_KERNEL);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1481) 	if (!nn->reclaim_str_hashtbl)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1482) 		return -ENOMEM;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1483) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1484) 	for (i = 0; i < CLIENT_HASH_SIZE; i++)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1485) 		INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1486) 	nn->reclaim_str_hashtbl_size = 0;
362063a595be9 (Scott Mayhew         2019-03-26 18:06:28 -0400 1487) 	nn->track_reclaim_completes = true;
362063a595be9 (Scott Mayhew         2019-03-26 18:06:28 -0400 1488) 	atomic_set(&nn->nr_reclaim_complete, 0);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1489) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1490) 	return 0;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1491) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1492) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1493) static void
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1494) nfs4_cld_state_shutdown(struct net *net)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1495) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1496) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1497) 
362063a595be9 (Scott Mayhew         2019-03-26 18:06:28 -0400 1498) 	nn->track_reclaim_completes = false;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1499) 	kfree(nn->reclaim_str_hashtbl);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1500) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1501) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1502) static bool
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1503) cld_running(struct nfsd_net *nn)
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1504) {
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1505) 	struct cld_net *cn = nn->cld_net;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1506) 	struct rpc_pipe *pipe = cn->cn_pipe;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1507) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1508) 	return pipe->nreaders || pipe->nwriters;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1509) }
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1510) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1511) static int
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1512) nfsd4_cld_get_version(struct nfsd_net *nn)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1513) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1514) 	int ret = 0;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1515) 	struct cld_upcall *cup;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1516) 	struct cld_net *cn = nn->cld_net;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1517) 	uint8_t version;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1518) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1519) 	cup = alloc_cld_upcall(nn);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1520) 	if (!cup) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1521) 		ret = -ENOMEM;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1522) 		goto out_err;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1523) 	}
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1524) 	cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion;
df60446cd1fb4 (Scott Mayhew         2020-07-10 16:33:07 -0400 1525) 	ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1526) 	if (!ret) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1527) 		ret = cup->cu_u.cu_msg.cm_status;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1528) 		if (ret)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1529) 			goto out_free;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1530) 		version = cup->cu_u.cu_msg.cm_u.cm_version;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1531) 		dprintk("%s: userspace returned version %u\n",
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1532) 				__func__, version);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1533) 		if (version < 1)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1534) 			version = 1;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1535) 		else if (version > CLD_UPCALL_VERSION)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1536) 			version = CLD_UPCALL_VERSION;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1537) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1538) 		switch (version) {
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1539) 		case 1:
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1540) 			nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1541) 			break;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1542) 		case 2:
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1543) 			nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2;
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1544) 			break;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1545) 		default:
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1546) 			break;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1547) 		}
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1548) 	}
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1549) out_free:
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1550) 	free_cld_upcall(cup);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1551) out_err:
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1552) 	if (ret)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1553) 		dprintk("%s: Unable to get version from userspace: %d\n",
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1554) 			__func__, ret);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1555) 	return ret;
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1556) }
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1557) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1558) static int
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1559) nfsd4_cld_tracking_init(struct net *net)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1560) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1561) 	int status;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1562) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1563) 	bool running;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1564) 	int retries = 10;
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1565) 	struct crypto_shash *tfm;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1566) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1567) 	status = nfs4_cld_state_init(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1568) 	if (status)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1569) 		return status;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1570) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1571) 	status = __nfsd4_init_cld_pipe(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1572) 	if (status)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1573) 		goto err_shutdown;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1574) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1575) 	/*
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1576) 	 * rpc pipe upcalls take 30 seconds to time out, so we don't want to
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1577) 	 * queue an upcall unless we know that nfsdcld is running (because we
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1578) 	 * want this to fail fast so that nfsd4_client_tracking_init() can try
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1579) 	 * the next client tracking method).  nfsdcld should already be running
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1580) 	 * before nfsd is started, so the wait here is for nfsdcld to open the
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1581) 	 * pipefs file we just created.
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1582) 	 */
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1583) 	while (!(running = cld_running(nn)) && retries--)
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1584) 		msleep(100);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1585) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1586) 	if (!running) {
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1587) 		status = -ETIMEDOUT;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1588) 		goto err_remove;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1589) 	}
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1590) 	tfm = crypto_alloc_shash("sha256", 0, 0);
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1591) 	if (IS_ERR(tfm)) {
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1592) 		status = PTR_ERR(tfm);
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1593) 		goto err_remove;
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1594) 	}
18b9a895e6529 (Scott Mayhew         2019-11-12 14:01:43 -0500 1595) 	nn->cld_net->cn_tfm = tfm;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1596) 
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1597) 	status = nfsd4_cld_get_version(nn);
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1598) 	if (status == -EOPNOTSUPP)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1599) 		pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n");
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1600) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1601) 	status = nfsd4_cld_grace_start(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1602) 	if (status) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1603) 		if (status == -EOPNOTSUPP)
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1604) 			pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n");
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1605) 		nfs4_release_reclaim(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1606) 		goto err_remove;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1607) 	} else
f988a7b71d1e6 (Paul Menzel          2021-03-12 22:03:00 +0100 1608) 		pr_info("NFSD: Using nfsdcld client tracking operations.\n");
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1609) 	return 0;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1610) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1611) err_remove:
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1612) 	nfsd4_remove_cld_pipe(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1613) err_shutdown:
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1614) 	nfs4_cld_state_shutdown(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1615) 	return status;
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1616) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1617) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1618) static void
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1619) nfsd4_cld_tracking_exit(struct net *net)
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1620) {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1621) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1622) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1623) 	nfs4_release_reclaim(nn);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1624) 	nfsd4_remove_cld_pipe(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1625) 	nfs4_cld_state_shutdown(net);
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1626) }
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1627) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1628) /* For older nfsdcld's */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1629) static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = {
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1630) 	.init		= nfsd4_init_cld_pipe,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1631) 	.exit		= nfsd4_remove_cld_pipe,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1632) 	.create		= nfsd4_cld_create,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1633) 	.remove		= nfsd4_cld_remove,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1634) 	.check		= nfsd4_cld_check_v0,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1635) 	.grace_done	= nfsd4_cld_grace_done_v0,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1636) 	.version	= 1,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1637) 	.msglen		= sizeof(struct cld_msg),
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1638) };
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1639) 
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1640) /* For newer nfsdcld's */
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1641) static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = {
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1642) 	.init		= nfsd4_cld_tracking_init,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1643) 	.exit		= nfsd4_cld_tracking_exit,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1644) 	.create		= nfsd4_cld_create,
74725959c33c1 (Scott Mayhew         2019-03-26 18:06:27 -0400 1645) 	.remove		= nfsd4_cld_remove,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1646) 	.check		= nfsd4_cld_check,
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1647) 	.grace_done	= nfsd4_cld_grace_done,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1648) 	.version	= 1,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 1649) 	.msglen		= sizeof(struct cld_msg),
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1650) };
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 1651) 
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1652) /* v2 create/check ops include the principal, if available */
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1653) static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = {
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1654) 	.init		= nfsd4_cld_tracking_init,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1655) 	.exit		= nfsd4_cld_tracking_exit,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1656) 	.create		= nfsd4_cld_create_v2,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1657) 	.remove		= nfsd4_cld_remove,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1658) 	.check		= nfsd4_cld_check_v2,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1659) 	.grace_done	= nfsd4_cld_grace_done,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1660) 	.version	= 2,
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1661) 	.msglen		= sizeof(struct cld_msg_v2),
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1662) };
6ee95d1c89918 (Scott Mayhew         2019-09-09 16:10:31 -0400 1663) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1664) /* upcall via usermodehelper */
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1665) static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack";
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1666) module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog),
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1667) 			S_IRUGO|S_IWUSR);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1668) MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program");
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1669) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1670) static bool cltrack_legacy_disable;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1671) module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1672) MODULE_PARM_DESC(cltrack_legacy_disable,
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1673) 		"Disable legacy recoverydir conversion. Default: false");
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1674) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1675) #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR="
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1676) #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR="
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1677) #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION="
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1678) #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START="
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1679) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1680) static char *
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1681) nfsd4_cltrack_legacy_topdir(void)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1682) {
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1683) 	int copied;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1684) 	size_t len;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1685) 	char *result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1686) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1687) 	if (cltrack_legacy_disable)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1688) 		return NULL;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1689) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1690) 	len = strlen(LEGACY_TOPDIR_ENV_PREFIX) +
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1691) 		strlen(nfs4_recoverydir()) + 1;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1692) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1693) 	result = kmalloc(len, GFP_KERNEL);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1694) 	if (!result)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1695) 		return result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1696) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1697) 	copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s",
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1698) 				nfs4_recoverydir());
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1699) 	if (copied >= len) {
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1700) 		/* just return nothing if output was truncated */
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1701) 		kfree(result);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1702) 		return NULL;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1703) 	}
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1704) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1705) 	return result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1706) }
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1707) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1708) static char *
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1709) nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1710) {
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1711) 	int copied;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1712) 	size_t len;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1713) 	char *result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1714) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1715) 	if (cltrack_legacy_disable)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1716) 		return NULL;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1717) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1718) 	/* +1 is for '/' between "topdir" and "recdir" */
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1719) 	len = strlen(LEGACY_RECDIR_ENV_PREFIX) +
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1720) 		strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1721) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1722) 	result = kmalloc(len, GFP_KERNEL);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1723) 	if (!result)
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1724) 		return result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1725) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1726) 	copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/",
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1727) 				nfs4_recoverydir());
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1728) 	if (copied > (len - HEXDIR_LEN)) {
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1729) 		/* just return nothing if output will be truncated */
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1730) 		kfree(result);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1731) 		return NULL;
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1732) 	}
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1733) 
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1734) 	copied = nfs4_make_rec_clidname(result + copied, name);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1735) 	if (copied) {
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1736) 		kfree(result);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1737) 		return NULL;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1738) 	}
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1739) 
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1740) 	return result;
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1741) }
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1742) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1743) static char *
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1744) nfsd4_cltrack_client_has_session(struct nfs4_client *clp)
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1745) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1746) 	int copied;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1747) 	size_t len;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1748) 	char *result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1749) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1750) 	/* prefix + Y/N character + terminating NULL */
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1751) 	len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1752) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1753) 	result = kmalloc(len, GFP_KERNEL);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1754) 	if (!result)
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1755) 		return result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1756) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1757) 	copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c",
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1758) 				clp->cl_minorversion ? 'Y' : 'N');
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1759) 	if (copied >= len) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1760) 		/* just return nothing if output was truncated */
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1761) 		kfree(result);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1762) 		return NULL;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1763) 	}
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1764) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1765) 	return result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1766) }
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1767) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1768) static char *
9cc7680149b23 (Arnd Bergmann        2019-10-31 15:53:13 +0100 1769) nfsd4_cltrack_grace_start(time64_t grace_start)
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1770) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1771) 	int copied;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1772) 	size_t len;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1773) 	char *result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1774) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1775) 	/* prefix + max width of int64_t string + terminating NULL */
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1776) 	len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1777) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1778) 	result = kmalloc(len, GFP_KERNEL);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1779) 	if (!result)
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1780) 		return result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1781) 
9cc7680149b23 (Arnd Bergmann        2019-10-31 15:53:13 +0100 1782) 	copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld",
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1783) 				grace_start);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1784) 	if (copied >= len) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1785) 		/* just return nothing if output was truncated */
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1786) 		kfree(result);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1787) 		return NULL;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1788) 	}
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1789) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1790) 	return result;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1791) }
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1792) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1793) static int
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1794) nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1795) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1796) 	char *envp[3];
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1797) 	char *argv[4];
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1798) 	int ret;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1799) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1800) 	if (unlikely(!cltrack_prog[0])) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1801) 		dprintk("%s: cltrack_prog is disabled\n", __func__);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1802) 		return -EACCES;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1803) 	}
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1804) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1805) 	dprintk("%s: cmd: %s\n", __func__, cmd);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1806) 	dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)");
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1807) 	dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)");
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1808) 	dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)");
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1809) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1810) 	envp[0] = env0;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1811) 	envp[1] = env1;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1812) 	envp[2] = NULL;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1813) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1814) 	argv[0] = (char *)cltrack_prog;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1815) 	argv[1] = cmd;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1816) 	argv[2] = arg;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1817) 	argv[3] = NULL;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1818) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1819) 	ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1820) 	/*
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1821) 	 * Disable the upcall mechanism if we're getting an ENOENT or EACCES
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1822) 	 * error. The admin can re-enable it on the fly by using sysfs
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1823) 	 * once the problem has been fixed.
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1824) 	 */
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1825) 	if (ret == -ENOENT || ret == -EACCES) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1826) 		dprintk("NFSD: %s was not found or isn't executable (%d). "
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1827) 			"Setting cltrack_prog to blank string!",
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1828) 			cltrack_prog, ret);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1829) 		cltrack_prog[0] = '\0';
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1830) 	}
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1831) 	dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1832) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1833) 	return ret;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1834) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1835) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1836) static char *
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1837) bin_to_hex_dup(const unsigned char *src, int srclen)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1838) {
12b4157b7d3b6 (Andy Shevchenko      2019-10-11 19:02:58 +0300 1839) 	char *buf;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1840) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1841) 	/* +1 for terminating NULL */
12b4157b7d3b6 (Andy Shevchenko      2019-10-11 19:02:58 +0300 1842) 	buf = kzalloc((srclen * 2) + 1, GFP_KERNEL);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1843) 	if (!buf)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1844) 		return buf;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1845) 
12b4157b7d3b6 (Andy Shevchenko      2019-10-11 19:02:58 +0300 1846) 	bin2hex(buf, src, srclen);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1847) 	return buf;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1848) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1849) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1850) static int
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1851) nfsd4_umh_cltrack_init(struct net *net)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1852) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1853) 	int ret;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1854) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1855) 	char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1856) 
71a50306934f4 (Stanislav Kinsbursky 2013-02-01 15:56:22 +0300 1857) 	/* XXX: The usermode helper s not working in container yet. */
71a50306934f4 (Stanislav Kinsbursky 2013-02-01 15:56:22 +0300 1858) 	if (net != &init_net) {
46cc8ba304450 (Paul Gortmaker       2015-08-25 16:59:16 -0400 1859) 		pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n");
956ccef3c9766 (Sudip Mukherjee      2016-03-07 15:40:03 +0530 1860) 		kfree(grace_start);
71a50306934f4 (Stanislav Kinsbursky 2013-02-01 15:56:22 +0300 1861) 		return -EINVAL;
71a50306934f4 (Stanislav Kinsbursky 2013-02-01 15:56:22 +0300 1862) 	}
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1863) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1864) 	ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1865) 	kfree(grace_start);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 1866) 	if (!ret)
f988a7b71d1e6 (Paul Menzel          2021-03-12 22:03:00 +0100 1867) 		pr_info("NFSD: Using UMH upcall client tracking operations.\n");
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1868) 	return ret;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1869) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1870) 
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1871) static void
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1872) nfsd4_cltrack_upcall_lock(struct nfs4_client *clp)
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1873) {
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1874) 	wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK,
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1875) 			 TASK_UNINTERRUPTIBLE);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1876) }
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1877) 
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1878) static void
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1879) nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp)
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1880) {
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1881) 	smp_mb__before_atomic();
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1882) 	clear_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1883) 	smp_mb__after_atomic();
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1884) 	wake_up_bit(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1885) }
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1886) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1887) static void
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1888) nfsd4_umh_cltrack_create(struct nfs4_client *clp)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1889) {
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1890) 	char *hexid, *has_session, *grace_start;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1891) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1892) 
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1893) 	/*
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1894) 	 * With v4.0 clients, there's little difference in outcome between a
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1895) 	 * create and check operation, and we can end up calling into this
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1896) 	 * function multiple times per client (once for each openowner). So,
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1897) 	 * for v4.0 clients skip upcalling once the client has been recorded
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1898) 	 * on stable storage.
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1899) 	 *
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1900) 	 * For v4.1+ clients, the outcome of the two operations is different,
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1901) 	 * so we must ensure that we upcall for the create operation. v4.1+
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1902) 	 * clients call this on RECLAIM_COMPLETE though, so we should only end
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1903) 	 * up doing a single create upcall per client.
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1904) 	 */
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1905) 	if (clp->cl_minorversion == 0 &&
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1906) 	    test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1907) 		return;
65decb650a95b (Jeff Layton          2014-09-12 16:40:22 -0400 1908) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1909) 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1910) 	if (!hexid) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1911) 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1912) 		return;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1913) 	}
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1914) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1915) 	has_session = nfsd4_cltrack_client_has_session(clp);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1916) 	grace_start = nfsd4_cltrack_grace_start(nn->boot_time);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1917) 
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1918) 	nfsd4_cltrack_upcall_lock(clp);
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1919) 	if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start))
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1920) 		set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1921) 	nfsd4_cltrack_upcall_unlock(clp);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1922) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1923) 	kfree(has_session);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1924) 	kfree(grace_start);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1925) 	kfree(hexid);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1926) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1927) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1928) static void
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1929) nfsd4_umh_cltrack_remove(struct nfs4_client *clp)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1930) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1931) 	char *hexid;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1932) 
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1933) 	if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1934) 		return;
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1935) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1936) 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1937) 	if (!hexid) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1938) 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1939) 		return;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1940) 	}
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1941) 
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1942) 	nfsd4_cltrack_upcall_lock(clp);
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1943) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) &&
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1944) 	    nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0)
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1945) 		clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1946) 	nfsd4_cltrack_upcall_unlock(clp);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1947) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1948) 	kfree(hexid);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1949) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1950) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1951) static int
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1952) nfsd4_umh_cltrack_check(struct nfs4_client *clp)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1953) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1954) 	int ret;
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1955) 	char *hexid, *has_session, *legacy;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1956) 
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1957) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags))
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1958) 		return 0;
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1959) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1960) 	hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1961) 	if (!hexid) {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1962) 		dprintk("%s: can't allocate memory for upcall!\n", __func__);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1963) 		return -ENOMEM;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1964) 	}
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1965) 
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1966) 	has_session = nfsd4_cltrack_client_has_session(clp);
2216d449a9792 (Jeff Layton          2012-11-12 15:00:57 -0500 1967) 	legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name);
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1968) 
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1969) 	nfsd4_cltrack_upcall_lock(clp);
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1970) 	if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) {
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1971) 		ret = 0;
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1972) 	} else {
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1973) 		ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy);
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1974) 		if (ret == 0)
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1975) 			set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags);
788a7914ad433 (Jeff Layton          2014-09-12 16:40:21 -0400 1976) 	}
d682e750ce14c (Jeff Layton          2014-09-12 16:40:21 -0400 1977) 	nfsd4_cltrack_upcall_unlock(clp);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1978) 	kfree(has_session);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1979) 	kfree(legacy);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1980) 	kfree(hexid);
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1981) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1982) 	return ret;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1983) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1984) 
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1985) static void
919b8049f0e00 (Jeff Layton          2014-09-12 16:40:20 -0400 1986) nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn)
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1987) {
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1988) 	char *legacy;
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1989) 	char timestr[22]; /* FIXME: better way to determine max size? */
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1990) 
9cc7680149b23 (Arnd Bergmann        2019-10-31 15:53:13 +0100 1991) 	sprintf(timestr, "%lld", nn->boot_time);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1992) 	legacy = nfsd4_cltrack_legacy_topdir();
d4318acd5d2d3 (Jeff Layton          2014-09-12 16:40:21 -0400 1993) 	nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL);
f3aa7e24c91ee (Jeff Layton          2012-11-12 15:00:50 -0500 1994) 	kfree(legacy);
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1995) }
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1996) 
7c582e4faaf25 (Julia Lawall         2015-11-22 08:22:10 +0100 1997) static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = {
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1998) 	.init		= nfsd4_umh_cltrack_init,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 1999) 	.exit		= NULL,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2000) 	.create		= nfsd4_umh_cltrack_create,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2001) 	.remove		= nfsd4_umh_cltrack_remove,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2002) 	.check		= nfsd4_umh_cltrack_check,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2003) 	.grace_done	= nfsd4_umh_cltrack_grace_done,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 2004) 	.version	= 1,
11a60d159259d (Scott Mayhew         2019-09-09 16:10:30 -0400 2005) 	.msglen		= 0,
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2006) };
2873d2147e1e1 (Jeff Layton          2012-11-12 15:00:48 -0500 2007) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2008) int
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2009) nfsd4_client_tracking_init(struct net *net)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2010) {
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2011) 	int status;
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 2012) 	struct path path;
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2013) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2014) 
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2015) 	/* just run the init if it the method is already decided */
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2016) 	if (nn->client_tracking_ops)
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2017) 		goto do_init;
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2018) 
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2019) 	/* First, try to use nfsdcld */
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2020) 	nn->client_tracking_ops = &nfsd4_cld_tracking_ops;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2021) 	status = nn->client_tracking_ops->init(net);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2022) 	if (!status)
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2023) 		return status;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2024) 	if (status != -ETIMEDOUT) {
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2025) 		nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2026) 		status = nn->client_tracking_ops->init(net);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2027) 		if (!status)
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2028) 			return status;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2029) 	}
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2030) 
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2031) 	/*
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2032) 	 * Next, try the UMH upcall.
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2033) 	 */
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2034) 	nn->client_tracking_ops = &nfsd4_umh_tracking_ops;
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2035) 	status = nn->client_tracking_ops->init(net);
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2036) 	if (!status)
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2037) 		return status;
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2038) 
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2039) 	/*
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2040) 	 * Finally, See if the recoverydir exists and is a directory.
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2041) 	 * If it is, then use the legacy ops.
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2042) 	 */
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2043) 	nn->client_tracking_ops = &nfsd4_legacy_tracking_ops;
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2044) 	status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path);
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2045) 	if (!status) {
e36cb0b89ce20 (David Howells        2015-01-29 12:02:35 +0000 2046) 		status = d_is_dir(path.dentry);
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2047) 		path_put(&path);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2048) 		if (!status) {
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2049) 			status = -EINVAL;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2050) 			goto out;
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2051) 		}
f3f8014862d81 (Jeff Layton          2012-03-21 09:52:07 -0400 2052) 	}
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2053) 
2d77bf0a55d64 (Jeff Layton          2012-11-12 15:00:49 -0500 2054) do_init:
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2055) 	status = nn->client_tracking_ops->init(net);
869216075b63c (Scott Mayhew         2019-03-26 18:06:29 -0400 2056) out:
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2057) 	if (status) {
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2058) 		printk(KERN_WARNING "NFSD: Unable to initialize client "
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2059) 				    "recovery tracking! (%d)\n", status);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2060) 		nn->client_tracking_ops = NULL;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2061) 	}
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2062) 	return status;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2063) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2064) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2065) void
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2066) nfsd4_client_tracking_exit(struct net *net)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2067) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2068) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2069) 
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2070) 	if (nn->client_tracking_ops) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2071) 		if (nn->client_tracking_ops->exit)
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2072) 			nn->client_tracking_ops->exit(net);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2073) 		nn->client_tracking_ops = NULL;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2074) 	}
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2075) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2076) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2077) void
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2078) nfsd4_client_record_create(struct nfs4_client *clp)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2079) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2080) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2081) 
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2082) 	if (nn->client_tracking_ops)
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2083) 		nn->client_tracking_ops->create(clp);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2084) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2085) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2086) void
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2087) nfsd4_client_record_remove(struct nfs4_client *clp)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2088) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2089) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2090) 
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2091) 	if (nn->client_tracking_ops)
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2092) 		nn->client_tracking_ops->remove(clp);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2093) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2094) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2095) int
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2096) nfsd4_client_record_check(struct nfs4_client *clp)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2097) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2098) 	struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2099) 
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2100) 	if (nn->client_tracking_ops)
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2101) 		return nn->client_tracking_ops->check(clp);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2102) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2103) 	return -EOPNOTSUPP;
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2104) }
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2105) 
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2106) void
919b8049f0e00 (Jeff Layton          2014-09-12 16:40:20 -0400 2107) nfsd4_record_grace_done(struct nfsd_net *nn)
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2108) {
9a9c6478a8b6c (Stanislav Kinsbursky 2012-12-04 14:29:27 +0300 2109) 	if (nn->client_tracking_ops)
919b8049f0e00 (Jeff Layton          2014-09-12 16:40:20 -0400 2110) 		nn->client_tracking_ops->grace_done(nn);
2a4317c55438d (Jeff Layton          2012-03-21 16:42:43 -0400 2111) }
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2112) 
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2113) static int
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2114) rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr)
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2115) {
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2116) 	struct super_block *sb = ptr;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2117) 	struct net *net = sb->s_fs_info;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2118) 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2119) 	struct cld_net *cn = nn->cld_net;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2120) 	struct dentry *dentry;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2121) 	int ret = 0;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2122) 
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2123) 	if (!try_module_get(THIS_MODULE))
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2124) 		return 0;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2125) 
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2126) 	if (!cn) {
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2127) 		module_put(THIS_MODULE);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2128) 		return 0;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2129) 	}
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2130) 
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2131) 	switch (event) {
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2132) 	case RPC_PIPEFS_MOUNT:
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2133) 		dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2134) 		if (IS_ERR(dentry)) {
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2135) 			ret = PTR_ERR(dentry);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2136) 			break;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2137) 		}
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2138) 		cn->cn_pipe->dentry = dentry;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2139) 		break;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2140) 	case RPC_PIPEFS_UMOUNT:
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2141) 		if (cn->cn_pipe->dentry)
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2142) 			nfsd4_cld_unregister_sb(cn->cn_pipe);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2143) 		break;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2144) 	default:
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2145) 		ret = -ENOTSUPP;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2146) 		break;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2147) 	}
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2148) 	module_put(THIS_MODULE);
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2149) 	return ret;
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2150) }
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2151) 
2355c59644def (J. Bruce Fields      2012-04-25 16:56:22 -0400 2152) static struct notifier_block nfsd4_cld_block = {
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2153) 	.notifier_call = rpc_pipefs_event,
813fd320c1669 (Jeff Layton          2012-03-21 09:52:08 -0400 2154) };
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2155) 
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2156) int
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2157) register_cld_notifier(void)
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2158) {
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2159) 	return rpc_pipefs_notifier_register(&nfsd4_cld_block);
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2160) }
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2161) 
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2162) void
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2163) unregister_cld_notifier(void)
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2164) {
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2165) 	rpc_pipefs_notifier_unregister(&nfsd4_cld_block);
797a9d797f848 (Jeff Layton          2012-03-29 07:52:49 -0400 2166) }