xref: /OK3568_Linux_fs/kernel/fs/nfs/nfs4client.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4*4882a593Smuzhiyun  * Written by David Howells (dhowells@redhat.com)
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun #include <linux/module.h>
7*4882a593Smuzhiyun #include <linux/nfs_fs.h>
8*4882a593Smuzhiyun #include <linux/nfs_mount.h>
9*4882a593Smuzhiyun #include <linux/sunrpc/addr.h>
10*4882a593Smuzhiyun #include <linux/sunrpc/auth.h>
11*4882a593Smuzhiyun #include <linux/sunrpc/xprt.h>
12*4882a593Smuzhiyun #include <linux/sunrpc/bc_xprt.h>
13*4882a593Smuzhiyun #include <linux/sunrpc/rpc_pipe_fs.h>
14*4882a593Smuzhiyun #include "internal.h"
15*4882a593Smuzhiyun #include "callback.h"
16*4882a593Smuzhiyun #include "delegation.h"
17*4882a593Smuzhiyun #include "nfs4session.h"
18*4882a593Smuzhiyun #include "nfs4idmap.h"
19*4882a593Smuzhiyun #include "pnfs.h"
20*4882a593Smuzhiyun #include "netns.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun #define NFSDBG_FACILITY		NFSDBG_CLIENT
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun /*
25*4882a593Smuzhiyun  * Get a unique NFSv4.0 callback identifier which will be used
26*4882a593Smuzhiyun  * by the V4.0 callback service to lookup the nfs_client struct
27*4882a593Smuzhiyun  */
nfs_get_cb_ident_idr(struct nfs_client * clp,int minorversion)28*4882a593Smuzhiyun static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	int ret = 0;
31*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	if (clp->rpc_ops->version != 4 || minorversion != 0)
34*4882a593Smuzhiyun 		return ret;
35*4882a593Smuzhiyun 	idr_preload(GFP_KERNEL);
36*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
37*4882a593Smuzhiyun 	ret = idr_alloc(&nn->cb_ident_idr, clp, 1, 0, GFP_NOWAIT);
38*4882a593Smuzhiyun 	if (ret >= 0)
39*4882a593Smuzhiyun 		clp->cl_cb_ident = ret;
40*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
41*4882a593Smuzhiyun 	idr_preload_end();
42*4882a593Smuzhiyun 	return ret < 0 ? ret : 0;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun #ifdef CONFIG_NFS_V4_1
46*4882a593Smuzhiyun /*
47*4882a593Smuzhiyun  * Per auth flavor data server rpc clients
48*4882a593Smuzhiyun  */
49*4882a593Smuzhiyun struct nfs4_ds_server {
50*4882a593Smuzhiyun 	struct list_head	list;   /* ds_clp->cl_ds_clients */
51*4882a593Smuzhiyun 	struct rpc_clnt		*rpc_clnt;
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun /**
55*4882a593Smuzhiyun  * nfs4_find_ds_client - Common lookup case for DS I/O
56*4882a593Smuzhiyun  * @ds_clp: pointer to the DS's nfs_client
57*4882a593Smuzhiyun  * @flavor: rpc auth flavour to match
58*4882a593Smuzhiyun  */
59*4882a593Smuzhiyun static struct nfs4_ds_server *
nfs4_find_ds_client(struct nfs_client * ds_clp,rpc_authflavor_t flavor)60*4882a593Smuzhiyun nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun 	struct nfs4_ds_server *dss;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	rcu_read_lock();
65*4882a593Smuzhiyun 	list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) {
66*4882a593Smuzhiyun 		if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
67*4882a593Smuzhiyun 			continue;
68*4882a593Smuzhiyun 		goto out;
69*4882a593Smuzhiyun 	}
70*4882a593Smuzhiyun 	dss = NULL;
71*4882a593Smuzhiyun out:
72*4882a593Smuzhiyun 	rcu_read_unlock();
73*4882a593Smuzhiyun 	return dss;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun static struct nfs4_ds_server *
nfs4_add_ds_client(struct nfs_client * ds_clp,rpc_authflavor_t flavor,struct nfs4_ds_server * new)77*4882a593Smuzhiyun nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor,
78*4882a593Smuzhiyun 			   struct nfs4_ds_server *new)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun 	struct nfs4_ds_server *dss;
81*4882a593Smuzhiyun 
82*4882a593Smuzhiyun 	spin_lock(&ds_clp->cl_lock);
83*4882a593Smuzhiyun 	list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) {
84*4882a593Smuzhiyun 		if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
85*4882a593Smuzhiyun 			continue;
86*4882a593Smuzhiyun 		goto out;
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 	if (new)
89*4882a593Smuzhiyun 		list_add_rcu(&new->list, &ds_clp->cl_ds_clients);
90*4882a593Smuzhiyun 	dss = new;
91*4882a593Smuzhiyun out:
92*4882a593Smuzhiyun 	spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */
93*4882a593Smuzhiyun 	return dss;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun static struct nfs4_ds_server *
nfs4_alloc_ds_server(struct nfs_client * ds_clp,rpc_authflavor_t flavor)97*4882a593Smuzhiyun nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	struct nfs4_ds_server *dss;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	dss = kmalloc(sizeof(*dss), GFP_NOFS);
102*4882a593Smuzhiyun 	if (dss == NULL)
103*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 	dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor);
106*4882a593Smuzhiyun 	if (IS_ERR(dss->rpc_clnt)) {
107*4882a593Smuzhiyun 		int err = PTR_ERR(dss->rpc_clnt);
108*4882a593Smuzhiyun 		kfree (dss);
109*4882a593Smuzhiyun 		return ERR_PTR(err);
110*4882a593Smuzhiyun 	}
111*4882a593Smuzhiyun 	INIT_LIST_HEAD(&dss->list);
112*4882a593Smuzhiyun 
113*4882a593Smuzhiyun 	return dss;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun static void
nfs4_free_ds_server(struct nfs4_ds_server * dss)117*4882a593Smuzhiyun nfs4_free_ds_server(struct nfs4_ds_server *dss)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	rpc_release_client(dss->rpc_clnt);
120*4882a593Smuzhiyun 	kfree(dss);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun /**
124*4882a593Smuzhiyun  * nfs4_find_or_create_ds_client - Find or create a DS rpc client
125*4882a593Smuzhiyun  * @ds_clp: pointer to the DS's nfs_client
126*4882a593Smuzhiyun  * @inode: pointer to the inode
127*4882a593Smuzhiyun  *
128*4882a593Smuzhiyun  * Find or create a DS rpc client with th MDS server rpc client auth flavor
129*4882a593Smuzhiyun  * in the nfs_client cl_ds_clients list.
130*4882a593Smuzhiyun  */
131*4882a593Smuzhiyun struct rpc_clnt *
nfs4_find_or_create_ds_client(struct nfs_client * ds_clp,struct inode * inode)132*4882a593Smuzhiyun nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct nfs4_ds_server *dss, *new;
135*4882a593Smuzhiyun 	rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	dss = nfs4_find_ds_client(ds_clp, flavor);
138*4882a593Smuzhiyun 	if (dss != NULL)
139*4882a593Smuzhiyun 		goto out;
140*4882a593Smuzhiyun 	new = nfs4_alloc_ds_server(ds_clp, flavor);
141*4882a593Smuzhiyun 	if (IS_ERR(new))
142*4882a593Smuzhiyun 		return ERR_CAST(new);
143*4882a593Smuzhiyun 	dss = nfs4_add_ds_client(ds_clp, flavor, new);
144*4882a593Smuzhiyun 	if (dss != new)
145*4882a593Smuzhiyun 		nfs4_free_ds_server(new);
146*4882a593Smuzhiyun out:
147*4882a593Smuzhiyun 	return dss->rpc_clnt;
148*4882a593Smuzhiyun }
149*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client);
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun static void
nfs4_shutdown_ds_clients(struct nfs_client * clp)152*4882a593Smuzhiyun nfs4_shutdown_ds_clients(struct nfs_client *clp)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun 	struct nfs4_ds_server *dss;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	while (!list_empty(&clp->cl_ds_clients)) {
157*4882a593Smuzhiyun 		dss = list_entry(clp->cl_ds_clients.next,
158*4882a593Smuzhiyun 					struct nfs4_ds_server, list);
159*4882a593Smuzhiyun 		list_del(&dss->list);
160*4882a593Smuzhiyun 		rpc_shutdown_client(dss->rpc_clnt);
161*4882a593Smuzhiyun 		kfree (dss);
162*4882a593Smuzhiyun 	}
163*4882a593Smuzhiyun }
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun static void
nfs4_cleanup_callback(struct nfs_client * clp)166*4882a593Smuzhiyun nfs4_cleanup_callback(struct nfs_client *clp)
167*4882a593Smuzhiyun {
168*4882a593Smuzhiyun 	struct nfs4_copy_state *cp_state;
169*4882a593Smuzhiyun 
170*4882a593Smuzhiyun 	while (!list_empty(&clp->pending_cb_stateids)) {
171*4882a593Smuzhiyun 		cp_state = list_entry(clp->pending_cb_stateids.next,
172*4882a593Smuzhiyun 					struct nfs4_copy_state, copies);
173*4882a593Smuzhiyun 		list_del(&cp_state->copies);
174*4882a593Smuzhiyun 		kfree(cp_state);
175*4882a593Smuzhiyun 	}
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun 
nfs41_shutdown_client(struct nfs_client * clp)178*4882a593Smuzhiyun void nfs41_shutdown_client(struct nfs_client *clp)
179*4882a593Smuzhiyun {
180*4882a593Smuzhiyun 	if (nfs4_has_session(clp)) {
181*4882a593Smuzhiyun 		nfs4_cleanup_callback(clp);
182*4882a593Smuzhiyun 		nfs4_shutdown_ds_clients(clp);
183*4882a593Smuzhiyun 		nfs4_destroy_session(clp->cl_session);
184*4882a593Smuzhiyun 		nfs4_destroy_clientid(clp);
185*4882a593Smuzhiyun 	}
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun #endif	/* CONFIG_NFS_V4_1 */
189*4882a593Smuzhiyun 
nfs40_shutdown_client(struct nfs_client * clp)190*4882a593Smuzhiyun void nfs40_shutdown_client(struct nfs_client *clp)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	if (clp->cl_slot_tbl) {
193*4882a593Smuzhiyun 		nfs4_shutdown_slot_table(clp->cl_slot_tbl);
194*4882a593Smuzhiyun 		kfree(clp->cl_slot_tbl);
195*4882a593Smuzhiyun 	}
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
nfs4_alloc_client(const struct nfs_client_initdata * cl_init)198*4882a593Smuzhiyun struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun 	char buf[INET6_ADDRSTRLEN + 1];
201*4882a593Smuzhiyun 	const char *ip_addr = cl_init->ip_addr;
202*4882a593Smuzhiyun 	struct nfs_client *clp = nfs_alloc_client(cl_init);
203*4882a593Smuzhiyun 	int err;
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 	if (IS_ERR(clp))
206*4882a593Smuzhiyun 		return clp;
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
209*4882a593Smuzhiyun 	if (err)
210*4882a593Smuzhiyun 		goto error;
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 	if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
213*4882a593Smuzhiyun 		err = -EINVAL;
214*4882a593Smuzhiyun 		goto error;
215*4882a593Smuzhiyun 	}
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	spin_lock_init(&clp->cl_lock);
218*4882a593Smuzhiyun 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
219*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->cl_ds_clients);
220*4882a593Smuzhiyun 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
221*4882a593Smuzhiyun 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
222*4882a593Smuzhiyun 	clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
223*4882a593Smuzhiyun 	clp->cl_mig_gen = 1;
224*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_NFS_V4_1)
225*4882a593Smuzhiyun 	init_waitqueue_head(&clp->cl_lock_waitq);
226*4882a593Smuzhiyun #endif
227*4882a593Smuzhiyun 	INIT_LIST_HEAD(&clp->pending_cb_stateids);
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 	if (cl_init->minorversion != 0)
230*4882a593Smuzhiyun 		__set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
231*4882a593Smuzhiyun 	__set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
232*4882a593Smuzhiyun 	__set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags);
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	/*
235*4882a593Smuzhiyun 	 * Set up the connection to the server before we add add to the
236*4882a593Smuzhiyun 	 * global list.
237*4882a593Smuzhiyun 	 */
238*4882a593Smuzhiyun 	err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_GSS_KRB5I);
239*4882a593Smuzhiyun 	if (err == -EINVAL)
240*4882a593Smuzhiyun 		err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
241*4882a593Smuzhiyun 	if (err < 0)
242*4882a593Smuzhiyun 		goto error;
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* If no clientaddr= option was specified, find a usable cb address */
245*4882a593Smuzhiyun 	if (ip_addr == NULL) {
246*4882a593Smuzhiyun 		struct sockaddr_storage cb_addr;
247*4882a593Smuzhiyun 		struct sockaddr *sap = (struct sockaddr *)&cb_addr;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 		err = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
250*4882a593Smuzhiyun 		if (err < 0)
251*4882a593Smuzhiyun 			goto error;
252*4882a593Smuzhiyun 		err = rpc_ntop(sap, buf, sizeof(buf));
253*4882a593Smuzhiyun 		if (err < 0)
254*4882a593Smuzhiyun 			goto error;
255*4882a593Smuzhiyun 		ip_addr = (const char *)buf;
256*4882a593Smuzhiyun 	}
257*4882a593Smuzhiyun 	strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	err = nfs_idmap_new(clp);
260*4882a593Smuzhiyun 	if (err < 0) {
261*4882a593Smuzhiyun 		dprintk("%s: failed to create idmapper. Error = %d\n",
262*4882a593Smuzhiyun 			__func__, err);
263*4882a593Smuzhiyun 		goto error;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
266*4882a593Smuzhiyun 	return clp;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun error:
269*4882a593Smuzhiyun 	nfs_free_client(clp);
270*4882a593Smuzhiyun 	return ERR_PTR(err);
271*4882a593Smuzhiyun }
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun /*
274*4882a593Smuzhiyun  * Destroy the NFS4 callback service
275*4882a593Smuzhiyun  */
nfs4_destroy_callback(struct nfs_client * clp)276*4882a593Smuzhiyun static void nfs4_destroy_callback(struct nfs_client *clp)
277*4882a593Smuzhiyun {
278*4882a593Smuzhiyun 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
279*4882a593Smuzhiyun 		nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun 
nfs4_shutdown_client(struct nfs_client * clp)282*4882a593Smuzhiyun static void nfs4_shutdown_client(struct nfs_client *clp)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
285*4882a593Smuzhiyun 		nfs4_kill_renewd(clp);
286*4882a593Smuzhiyun 	clp->cl_mvops->shutdown_client(clp);
287*4882a593Smuzhiyun 	nfs4_destroy_callback(clp);
288*4882a593Smuzhiyun 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
289*4882a593Smuzhiyun 		nfs_idmap_delete(clp);
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
292*4882a593Smuzhiyun 	kfree(clp->cl_serverowner);
293*4882a593Smuzhiyun 	kfree(clp->cl_serverscope);
294*4882a593Smuzhiyun 	kfree(clp->cl_implid);
295*4882a593Smuzhiyun 	kfree(clp->cl_owner_id);
296*4882a593Smuzhiyun }
297*4882a593Smuzhiyun 
nfs4_free_client(struct nfs_client * clp)298*4882a593Smuzhiyun void nfs4_free_client(struct nfs_client *clp)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	nfs4_shutdown_client(clp);
301*4882a593Smuzhiyun 	nfs_free_client(clp);
302*4882a593Smuzhiyun }
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun /*
305*4882a593Smuzhiyun  * Initialize the NFS4 callback service
306*4882a593Smuzhiyun  */
nfs4_init_callback(struct nfs_client * clp)307*4882a593Smuzhiyun static int nfs4_init_callback(struct nfs_client *clp)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	struct rpc_xprt *xprt;
310*4882a593Smuzhiyun 	int error;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
313*4882a593Smuzhiyun 
314*4882a593Smuzhiyun 	if (nfs4_has_session(clp)) {
315*4882a593Smuzhiyun 		error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
316*4882a593Smuzhiyun 		if (error < 0)
317*4882a593Smuzhiyun 			return error;
318*4882a593Smuzhiyun 	}
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
321*4882a593Smuzhiyun 	if (error < 0) {
322*4882a593Smuzhiyun 		dprintk("%s: failed to start callback. Error = %d\n",
323*4882a593Smuzhiyun 			__func__, error);
324*4882a593Smuzhiyun 		return error;
325*4882a593Smuzhiyun 	}
326*4882a593Smuzhiyun 	__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	return 0;
329*4882a593Smuzhiyun }
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun /**
332*4882a593Smuzhiyun  * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
333*4882a593Smuzhiyun  * @clp: nfs_client to initialize
334*4882a593Smuzhiyun  *
335*4882a593Smuzhiyun  * Returns zero on success, or a negative errno if some error occurred.
336*4882a593Smuzhiyun  */
nfs40_init_client(struct nfs_client * clp)337*4882a593Smuzhiyun int nfs40_init_client(struct nfs_client *clp)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun 	struct nfs4_slot_table *tbl;
340*4882a593Smuzhiyun 	int ret;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
343*4882a593Smuzhiyun 	if (tbl == NULL)
344*4882a593Smuzhiyun 		return -ENOMEM;
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 	ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
347*4882a593Smuzhiyun 					"NFSv4.0 transport Slot table");
348*4882a593Smuzhiyun 	if (ret) {
349*4882a593Smuzhiyun 		nfs4_shutdown_slot_table(tbl);
350*4882a593Smuzhiyun 		kfree(tbl);
351*4882a593Smuzhiyun 		return ret;
352*4882a593Smuzhiyun 	}
353*4882a593Smuzhiyun 
354*4882a593Smuzhiyun 	clp->cl_slot_tbl = tbl;
355*4882a593Smuzhiyun 	return 0;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun #if defined(CONFIG_NFS_V4_1)
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun /**
361*4882a593Smuzhiyun  * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
362*4882a593Smuzhiyun  * @clp: nfs_client to initialize
363*4882a593Smuzhiyun  *
364*4882a593Smuzhiyun  * Returns zero on success, or a negative errno if some error occurred.
365*4882a593Smuzhiyun  */
nfs41_init_client(struct nfs_client * clp)366*4882a593Smuzhiyun int nfs41_init_client(struct nfs_client *clp)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun 	struct nfs4_session *session = NULL;
369*4882a593Smuzhiyun 
370*4882a593Smuzhiyun 	/*
371*4882a593Smuzhiyun 	 * Create the session and mark it expired.
372*4882a593Smuzhiyun 	 * When a SEQUENCE operation encounters the expired session
373*4882a593Smuzhiyun 	 * it will do session recovery to initialize it.
374*4882a593Smuzhiyun 	 */
375*4882a593Smuzhiyun 	session = nfs4_alloc_session(clp);
376*4882a593Smuzhiyun 	if (!session)
377*4882a593Smuzhiyun 		return -ENOMEM;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	clp->cl_session = session;
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/*
382*4882a593Smuzhiyun 	 * The create session reply races with the server back
383*4882a593Smuzhiyun 	 * channel probe. Mark the client NFS_CS_SESSION_INITING
384*4882a593Smuzhiyun 	 * so that the client back channel can find the
385*4882a593Smuzhiyun 	 * nfs_client struct
386*4882a593Smuzhiyun 	 */
387*4882a593Smuzhiyun 	nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
388*4882a593Smuzhiyun 	return 0;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun #endif	/* CONFIG_NFS_V4_1 */
392*4882a593Smuzhiyun 
393*4882a593Smuzhiyun /*
394*4882a593Smuzhiyun  * Initialize the minor version specific parts of an NFS4 client record
395*4882a593Smuzhiyun  */
nfs4_init_client_minor_version(struct nfs_client * clp)396*4882a593Smuzhiyun static int nfs4_init_client_minor_version(struct nfs_client *clp)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	int ret;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	ret = clp->cl_mvops->init_client(clp);
401*4882a593Smuzhiyun 	if (ret)
402*4882a593Smuzhiyun 		return ret;
403*4882a593Smuzhiyun 	return nfs4_init_callback(clp);
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun /**
407*4882a593Smuzhiyun  * nfs4_init_client - Initialise an NFS4 client record
408*4882a593Smuzhiyun  *
409*4882a593Smuzhiyun  * @clp: nfs_client to initialise
410*4882a593Smuzhiyun  * @cl_init: pointer to nfs_client_initdata
411*4882a593Smuzhiyun  *
412*4882a593Smuzhiyun  * Returns pointer to an NFS client, or an ERR_PTR value.
413*4882a593Smuzhiyun  */
nfs4_init_client(struct nfs_client * clp,const struct nfs_client_initdata * cl_init)414*4882a593Smuzhiyun struct nfs_client *nfs4_init_client(struct nfs_client *clp,
415*4882a593Smuzhiyun 				    const struct nfs_client_initdata *cl_init)
416*4882a593Smuzhiyun {
417*4882a593Smuzhiyun 	struct nfs_client *old;
418*4882a593Smuzhiyun 	int error;
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	if (clp->cl_cons_state == NFS_CS_READY)
421*4882a593Smuzhiyun 		/* the client is initialised already */
422*4882a593Smuzhiyun 		return clp;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun 	error = nfs4_init_client_minor_version(clp);
425*4882a593Smuzhiyun 	if (error < 0)
426*4882a593Smuzhiyun 		goto error;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	error = nfs4_discover_server_trunking(clp, &old);
429*4882a593Smuzhiyun 	if (error < 0)
430*4882a593Smuzhiyun 		goto error;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	if (clp != old) {
433*4882a593Smuzhiyun 		clp->cl_preserve_clid = true;
434*4882a593Smuzhiyun 		/*
435*4882a593Smuzhiyun 		 * Mark the client as having failed initialization so other
436*4882a593Smuzhiyun 		 * processes walking the nfs_client_list in nfs_match_client()
437*4882a593Smuzhiyun 		 * won't try to use it.
438*4882a593Smuzhiyun 		 */
439*4882a593Smuzhiyun 		nfs_mark_client_ready(clp, -EPERM);
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun 	clear_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags);
442*4882a593Smuzhiyun 	nfs_put_client(clp);
443*4882a593Smuzhiyun 	return old;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun error:
446*4882a593Smuzhiyun 	nfs_mark_client_ready(clp, error);
447*4882a593Smuzhiyun 	nfs_put_client(clp);
448*4882a593Smuzhiyun 	return ERR_PTR(error);
449*4882a593Smuzhiyun }
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun /*
452*4882a593Smuzhiyun  * SETCLIENTID just did a callback update with the callback ident in
453*4882a593Smuzhiyun  * "drop," but server trunking discovery claims "drop" and "keep" are
454*4882a593Smuzhiyun  * actually the same server.  Swap the callback IDs so that "keep"
455*4882a593Smuzhiyun  * will continue to use the callback ident the server now knows about,
456*4882a593Smuzhiyun  * and so that "keep"'s original callback ident is destroyed when
457*4882a593Smuzhiyun  * "drop" is freed.
458*4882a593Smuzhiyun  */
nfs4_swap_callback_idents(struct nfs_client * keep,struct nfs_client * drop)459*4882a593Smuzhiyun static void nfs4_swap_callback_idents(struct nfs_client *keep,
460*4882a593Smuzhiyun 				      struct nfs_client *drop)
461*4882a593Smuzhiyun {
462*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
463*4882a593Smuzhiyun 	unsigned int save = keep->cl_cb_ident;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 	if (keep->cl_cb_ident == drop->cl_cb_ident)
466*4882a593Smuzhiyun 		return;
467*4882a593Smuzhiyun 
468*4882a593Smuzhiyun 	dprintk("%s: keeping callback ident %u and dropping ident %u\n",
469*4882a593Smuzhiyun 		__func__, keep->cl_cb_ident, drop->cl_cb_ident);
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
472*4882a593Smuzhiyun 
473*4882a593Smuzhiyun 	idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
474*4882a593Smuzhiyun 	keep->cl_cb_ident = drop->cl_cb_ident;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun 	idr_replace(&nn->cb_ident_idr, drop, save);
477*4882a593Smuzhiyun 	drop->cl_cb_ident = save;
478*4882a593Smuzhiyun 
479*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
480*4882a593Smuzhiyun }
481*4882a593Smuzhiyun 
nfs4_match_client_owner_id(const struct nfs_client * clp1,const struct nfs_client * clp2)482*4882a593Smuzhiyun static bool nfs4_match_client_owner_id(const struct nfs_client *clp1,
483*4882a593Smuzhiyun 		const struct nfs_client *clp2)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun 	if (clp1->cl_owner_id == NULL || clp2->cl_owner_id == NULL)
486*4882a593Smuzhiyun 		return true;
487*4882a593Smuzhiyun 	return strcmp(clp1->cl_owner_id, clp2->cl_owner_id) == 0;
488*4882a593Smuzhiyun }
489*4882a593Smuzhiyun 
nfs4_same_verifier(nfs4_verifier * v1,nfs4_verifier * v2)490*4882a593Smuzhiyun static bool nfs4_same_verifier(nfs4_verifier *v1, nfs4_verifier *v2)
491*4882a593Smuzhiyun {
492*4882a593Smuzhiyun 	return memcmp(v1->data, v2->data, sizeof(v1->data)) == 0;
493*4882a593Smuzhiyun }
494*4882a593Smuzhiyun 
nfs4_match_client(struct nfs_client * pos,struct nfs_client * new,struct nfs_client ** prev,struct nfs_net * nn)495*4882a593Smuzhiyun static int nfs4_match_client(struct nfs_client  *pos,  struct nfs_client *new,
496*4882a593Smuzhiyun 			     struct nfs_client **prev, struct nfs_net *nn)
497*4882a593Smuzhiyun {
498*4882a593Smuzhiyun 	int status;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	if (pos->rpc_ops != new->rpc_ops)
501*4882a593Smuzhiyun 		return 1;
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 	if (pos->cl_minorversion != new->cl_minorversion)
504*4882a593Smuzhiyun 		return 1;
505*4882a593Smuzhiyun 
506*4882a593Smuzhiyun 	/* If "pos" isn't marked ready, we can't trust the
507*4882a593Smuzhiyun 	 * remaining fields in "pos", especially the client
508*4882a593Smuzhiyun 	 * ID and serverowner fields.  Wait for CREATE_SESSION
509*4882a593Smuzhiyun 	 * to finish. */
510*4882a593Smuzhiyun 	if (pos->cl_cons_state > NFS_CS_READY) {
511*4882a593Smuzhiyun 		refcount_inc(&pos->cl_count);
512*4882a593Smuzhiyun 		spin_unlock(&nn->nfs_client_lock);
513*4882a593Smuzhiyun 
514*4882a593Smuzhiyun 		nfs_put_client(*prev);
515*4882a593Smuzhiyun 		*prev = pos;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 		status = nfs_wait_client_init_complete(pos);
518*4882a593Smuzhiyun 		spin_lock(&nn->nfs_client_lock);
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 		if (status < 0)
521*4882a593Smuzhiyun 			return status;
522*4882a593Smuzhiyun 	}
523*4882a593Smuzhiyun 
524*4882a593Smuzhiyun 	if (pos->cl_cons_state != NFS_CS_READY)
525*4882a593Smuzhiyun 		return 1;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	if (pos->cl_clientid != new->cl_clientid)
528*4882a593Smuzhiyun 		return 1;
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	/* NFSv4.1 always uses the uniform string, however someone
531*4882a593Smuzhiyun 	 * might switch the uniquifier string on us.
532*4882a593Smuzhiyun 	 */
533*4882a593Smuzhiyun 	if (!nfs4_match_client_owner_id(pos, new))
534*4882a593Smuzhiyun 		return 1;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	return 0;
537*4882a593Smuzhiyun }
538*4882a593Smuzhiyun 
539*4882a593Smuzhiyun /**
540*4882a593Smuzhiyun  * nfs40_walk_client_list - Find server that recognizes a client ID
541*4882a593Smuzhiyun  *
542*4882a593Smuzhiyun  * @new: nfs_client with client ID to test
543*4882a593Smuzhiyun  * @result: OUT: found nfs_client, or new
544*4882a593Smuzhiyun  * @cred: credential to use for trunking test
545*4882a593Smuzhiyun  *
546*4882a593Smuzhiyun  * Returns zero, a negative errno, or a negative NFS4ERR status.
547*4882a593Smuzhiyun  * If zero is returned, an nfs_client pointer is planted in "result."
548*4882a593Smuzhiyun  *
549*4882a593Smuzhiyun  * NB: nfs40_walk_client_list() relies on the new nfs_client being
550*4882a593Smuzhiyun  *     the last nfs_client on the list.
551*4882a593Smuzhiyun  */
nfs40_walk_client_list(struct nfs_client * new,struct nfs_client ** result,const struct cred * cred)552*4882a593Smuzhiyun int nfs40_walk_client_list(struct nfs_client *new,
553*4882a593Smuzhiyun 			   struct nfs_client **result,
554*4882a593Smuzhiyun 			   const struct cred *cred)
555*4882a593Smuzhiyun {
556*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
557*4882a593Smuzhiyun 	struct nfs_client *pos, *prev = NULL;
558*4882a593Smuzhiyun 	struct nfs4_setclientid_res clid = {
559*4882a593Smuzhiyun 		.clientid	= new->cl_clientid,
560*4882a593Smuzhiyun 		.confirm	= new->cl_confirm,
561*4882a593Smuzhiyun 	};
562*4882a593Smuzhiyun 	int status = -NFS4ERR_STALE_CLIENTID;
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
565*4882a593Smuzhiyun 	list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
566*4882a593Smuzhiyun 
567*4882a593Smuzhiyun 		if (pos == new)
568*4882a593Smuzhiyun 			goto found;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		status = nfs4_match_client(pos, new, &prev, nn);
571*4882a593Smuzhiyun 		if (status < 0)
572*4882a593Smuzhiyun 			goto out_unlock;
573*4882a593Smuzhiyun 		if (status != 0)
574*4882a593Smuzhiyun 			continue;
575*4882a593Smuzhiyun 		/*
576*4882a593Smuzhiyun 		 * We just sent a new SETCLIENTID, which should have
577*4882a593Smuzhiyun 		 * caused the server to return a new cl_confirm.  So if
578*4882a593Smuzhiyun 		 * cl_confirm is the same, then this is a different
579*4882a593Smuzhiyun 		 * server that just returned the same cl_confirm by
580*4882a593Smuzhiyun 		 * coincidence:
581*4882a593Smuzhiyun 		 */
582*4882a593Smuzhiyun 		if ((new != pos) && nfs4_same_verifier(&pos->cl_confirm,
583*4882a593Smuzhiyun 						       &new->cl_confirm))
584*4882a593Smuzhiyun 			continue;
585*4882a593Smuzhiyun 		/*
586*4882a593Smuzhiyun 		 * But if the cl_confirm's are different, then the only
587*4882a593Smuzhiyun 		 * way that a SETCLIENTID_CONFIRM to pos can succeed is
588*4882a593Smuzhiyun 		 * if new and pos point to the same server:
589*4882a593Smuzhiyun 		 */
590*4882a593Smuzhiyun found:
591*4882a593Smuzhiyun 		refcount_inc(&pos->cl_count);
592*4882a593Smuzhiyun 		spin_unlock(&nn->nfs_client_lock);
593*4882a593Smuzhiyun 
594*4882a593Smuzhiyun 		nfs_put_client(prev);
595*4882a593Smuzhiyun 		prev = pos;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 		status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
598*4882a593Smuzhiyun 		switch (status) {
599*4882a593Smuzhiyun 		case -NFS4ERR_STALE_CLIENTID:
600*4882a593Smuzhiyun 			break;
601*4882a593Smuzhiyun 		case 0:
602*4882a593Smuzhiyun 			nfs4_swap_callback_idents(pos, new);
603*4882a593Smuzhiyun 			pos->cl_confirm = new->cl_confirm;
604*4882a593Smuzhiyun 			nfs_mark_client_ready(pos, NFS_CS_READY);
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 			prev = NULL;
607*4882a593Smuzhiyun 			*result = pos;
608*4882a593Smuzhiyun 			goto out;
609*4882a593Smuzhiyun 		case -ERESTARTSYS:
610*4882a593Smuzhiyun 		case -ETIMEDOUT:
611*4882a593Smuzhiyun 			/* The callback path may have been inadvertently
612*4882a593Smuzhiyun 			 * changed. Schedule recovery!
613*4882a593Smuzhiyun 			 */
614*4882a593Smuzhiyun 			nfs4_schedule_path_down_recovery(pos);
615*4882a593Smuzhiyun 		default:
616*4882a593Smuzhiyun 			goto out;
617*4882a593Smuzhiyun 		}
618*4882a593Smuzhiyun 
619*4882a593Smuzhiyun 		spin_lock(&nn->nfs_client_lock);
620*4882a593Smuzhiyun 	}
621*4882a593Smuzhiyun out_unlock:
622*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	/* No match found. The server lost our clientid */
625*4882a593Smuzhiyun out:
626*4882a593Smuzhiyun 	nfs_put_client(prev);
627*4882a593Smuzhiyun 	return status;
628*4882a593Smuzhiyun }
629*4882a593Smuzhiyun 
630*4882a593Smuzhiyun #ifdef CONFIG_NFS_V4_1
631*4882a593Smuzhiyun /*
632*4882a593Smuzhiyun  * Returns true if the server major ids match
633*4882a593Smuzhiyun  */
634*4882a593Smuzhiyun bool
nfs4_check_serverowner_major_id(struct nfs41_server_owner * o1,struct nfs41_server_owner * o2)635*4882a593Smuzhiyun nfs4_check_serverowner_major_id(struct nfs41_server_owner *o1,
636*4882a593Smuzhiyun 				struct nfs41_server_owner *o2)
637*4882a593Smuzhiyun {
638*4882a593Smuzhiyun 	if (o1->major_id_sz != o2->major_id_sz)
639*4882a593Smuzhiyun 		return false;
640*4882a593Smuzhiyun 	return memcmp(o1->major_id, o2->major_id, o1->major_id_sz) == 0;
641*4882a593Smuzhiyun }
642*4882a593Smuzhiyun 
643*4882a593Smuzhiyun /*
644*4882a593Smuzhiyun  * Returns true if the server scopes match
645*4882a593Smuzhiyun  */
646*4882a593Smuzhiyun static bool
nfs4_check_server_scope(struct nfs41_server_scope * s1,struct nfs41_server_scope * s2)647*4882a593Smuzhiyun nfs4_check_server_scope(struct nfs41_server_scope *s1,
648*4882a593Smuzhiyun 			struct nfs41_server_scope *s2)
649*4882a593Smuzhiyun {
650*4882a593Smuzhiyun 	if (s1->server_scope_sz != s2->server_scope_sz)
651*4882a593Smuzhiyun 		return false;
652*4882a593Smuzhiyun 	return memcmp(s1->server_scope, s2->server_scope,
653*4882a593Smuzhiyun 					s1->server_scope_sz) == 0;
654*4882a593Smuzhiyun }
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun /**
657*4882a593Smuzhiyun  * nfs4_detect_session_trunking - Checks for session trunking.
658*4882a593Smuzhiyun  * @clp:    original mount nfs_client
659*4882a593Smuzhiyun  * @res:    result structure from an exchange_id using the original mount
660*4882a593Smuzhiyun  *          nfs_client with a new multi_addr transport
661*4882a593Smuzhiyun  * @xprt:   pointer to the transport to add.
662*4882a593Smuzhiyun  *
663*4882a593Smuzhiyun  * Called after a successful EXCHANGE_ID on a multi-addr connection.
664*4882a593Smuzhiyun  * Upon success, add the transport.
665*4882a593Smuzhiyun  *
666*4882a593Smuzhiyun  * Returns zero on success, otherwise -EINVAL
667*4882a593Smuzhiyun  *
668*4882a593Smuzhiyun  * Note: since the exchange_id for the new multi_addr transport uses the
669*4882a593Smuzhiyun  * same nfs_client from the original mount, the cl_owner_id is reused,
670*4882a593Smuzhiyun  * so eir_clientowner is the same.
671*4882a593Smuzhiyun  */
nfs4_detect_session_trunking(struct nfs_client * clp,struct nfs41_exchange_id_res * res,struct rpc_xprt * xprt)672*4882a593Smuzhiyun int nfs4_detect_session_trunking(struct nfs_client *clp,
673*4882a593Smuzhiyun 				 struct nfs41_exchange_id_res *res,
674*4882a593Smuzhiyun 				 struct rpc_xprt *xprt)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun 	/* Check eir_clientid */
677*4882a593Smuzhiyun 	if (clp->cl_clientid != res->clientid)
678*4882a593Smuzhiyun 		goto out_err;
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	/* Check eir_server_owner so_major_id */
681*4882a593Smuzhiyun 	if (!nfs4_check_serverowner_major_id(clp->cl_serverowner,
682*4882a593Smuzhiyun 					     res->server_owner))
683*4882a593Smuzhiyun 		goto out_err;
684*4882a593Smuzhiyun 
685*4882a593Smuzhiyun 	/* Check eir_server_owner so_minor_id */
686*4882a593Smuzhiyun 	if (clp->cl_serverowner->minor_id != res->server_owner->minor_id)
687*4882a593Smuzhiyun 		goto out_err;
688*4882a593Smuzhiyun 
689*4882a593Smuzhiyun 	/* Check eir_server_scope */
690*4882a593Smuzhiyun 	if (!nfs4_check_server_scope(clp->cl_serverscope, res->server_scope))
691*4882a593Smuzhiyun 		goto out_err;
692*4882a593Smuzhiyun 
693*4882a593Smuzhiyun 	pr_info("NFS:  %s: Session trunking succeeded for %s\n",
694*4882a593Smuzhiyun 		clp->cl_hostname,
695*4882a593Smuzhiyun 		xprt->address_strings[RPC_DISPLAY_ADDR]);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	return 0;
698*4882a593Smuzhiyun out_err:
699*4882a593Smuzhiyun 	pr_info("NFS:  %s: Session trunking failed for %s\n", clp->cl_hostname,
700*4882a593Smuzhiyun 		xprt->address_strings[RPC_DISPLAY_ADDR]);
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun 	return -EINVAL;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun /**
706*4882a593Smuzhiyun  * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
707*4882a593Smuzhiyun  *
708*4882a593Smuzhiyun  * @new: nfs_client with client ID to test
709*4882a593Smuzhiyun  * @result: OUT: found nfs_client, or new
710*4882a593Smuzhiyun  * @cred: credential to use for trunking test
711*4882a593Smuzhiyun  *
712*4882a593Smuzhiyun  * Returns zero, a negative errno, or a negative NFS4ERR status.
713*4882a593Smuzhiyun  * If zero is returned, an nfs_client pointer is planted in "result."
714*4882a593Smuzhiyun  *
715*4882a593Smuzhiyun  * NB: nfs41_walk_client_list() relies on the new nfs_client being
716*4882a593Smuzhiyun  *     the last nfs_client on the list.
717*4882a593Smuzhiyun  */
nfs41_walk_client_list(struct nfs_client * new,struct nfs_client ** result,const struct cred * cred)718*4882a593Smuzhiyun int nfs41_walk_client_list(struct nfs_client *new,
719*4882a593Smuzhiyun 			   struct nfs_client **result,
720*4882a593Smuzhiyun 			   const struct cred *cred)
721*4882a593Smuzhiyun {
722*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
723*4882a593Smuzhiyun 	struct nfs_client *pos, *prev = NULL;
724*4882a593Smuzhiyun 	int status = -NFS4ERR_STALE_CLIENTID;
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
727*4882a593Smuzhiyun 	list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
728*4882a593Smuzhiyun 
729*4882a593Smuzhiyun 		if (pos == new)
730*4882a593Smuzhiyun 			goto found;
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 		status = nfs4_match_client(pos, new, &prev, nn);
733*4882a593Smuzhiyun 		if (status < 0)
734*4882a593Smuzhiyun 			goto out;
735*4882a593Smuzhiyun 		if (status != 0)
736*4882a593Smuzhiyun 			continue;
737*4882a593Smuzhiyun 
738*4882a593Smuzhiyun 		/*
739*4882a593Smuzhiyun 		 * Note that session trunking is just a special subcase of
740*4882a593Smuzhiyun 		 * client id trunking. In either case, we want to fall back
741*4882a593Smuzhiyun 		 * to using the existing nfs_client.
742*4882a593Smuzhiyun 		 */
743*4882a593Smuzhiyun 		if (!nfs4_check_serverowner_major_id(pos->cl_serverowner,
744*4882a593Smuzhiyun 						     new->cl_serverowner))
745*4882a593Smuzhiyun 			continue;
746*4882a593Smuzhiyun 
747*4882a593Smuzhiyun found:
748*4882a593Smuzhiyun 		refcount_inc(&pos->cl_count);
749*4882a593Smuzhiyun 		*result = pos;
750*4882a593Smuzhiyun 		status = 0;
751*4882a593Smuzhiyun 		break;
752*4882a593Smuzhiyun 	}
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun out:
755*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
756*4882a593Smuzhiyun 	nfs_put_client(prev);
757*4882a593Smuzhiyun 	return status;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun #endif	/* CONFIG_NFS_V4_1 */
760*4882a593Smuzhiyun 
nfs4_destroy_server(struct nfs_server * server)761*4882a593Smuzhiyun static void nfs4_destroy_server(struct nfs_server *server)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	LIST_HEAD(freeme);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	nfs_server_return_all_delegations(server);
766*4882a593Smuzhiyun 	unset_pnfs_layoutdriver(server);
767*4882a593Smuzhiyun 	nfs4_purge_state_owners(server, &freeme);
768*4882a593Smuzhiyun 	nfs4_free_state_owners(&freeme);
769*4882a593Smuzhiyun }
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun /*
772*4882a593Smuzhiyun  * NFSv4.0 callback thread helper
773*4882a593Smuzhiyun  *
774*4882a593Smuzhiyun  * Find a client by callback identifier
775*4882a593Smuzhiyun  */
776*4882a593Smuzhiyun struct nfs_client *
nfs4_find_client_ident(struct net * net,int cb_ident)777*4882a593Smuzhiyun nfs4_find_client_ident(struct net *net, int cb_ident)
778*4882a593Smuzhiyun {
779*4882a593Smuzhiyun 	struct nfs_client *clp;
780*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(net, nfs_net_id);
781*4882a593Smuzhiyun 
782*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
783*4882a593Smuzhiyun 	clp = idr_find(&nn->cb_ident_idr, cb_ident);
784*4882a593Smuzhiyun 	if (clp)
785*4882a593Smuzhiyun 		refcount_inc(&clp->cl_count);
786*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
787*4882a593Smuzhiyun 	return clp;
788*4882a593Smuzhiyun }
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun #if defined(CONFIG_NFS_V4_1)
791*4882a593Smuzhiyun /* Common match routine for v4.0 and v4.1 callback services */
nfs4_cb_match_client(const struct sockaddr * addr,struct nfs_client * clp,u32 minorversion)792*4882a593Smuzhiyun static bool nfs4_cb_match_client(const struct sockaddr *addr,
793*4882a593Smuzhiyun 		struct nfs_client *clp, u32 minorversion)
794*4882a593Smuzhiyun {
795*4882a593Smuzhiyun 	struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
796*4882a593Smuzhiyun 
797*4882a593Smuzhiyun 	/* Don't match clients that failed to initialise */
798*4882a593Smuzhiyun 	if (!(clp->cl_cons_state == NFS_CS_READY ||
799*4882a593Smuzhiyun 	    clp->cl_cons_state == NFS_CS_SESSION_INITING))
800*4882a593Smuzhiyun 		return false;
801*4882a593Smuzhiyun 
802*4882a593Smuzhiyun 	smp_rmb();
803*4882a593Smuzhiyun 
804*4882a593Smuzhiyun 	/* Match the version and minorversion */
805*4882a593Smuzhiyun 	if (clp->rpc_ops->version != 4 ||
806*4882a593Smuzhiyun 	    clp->cl_minorversion != minorversion)
807*4882a593Smuzhiyun 		return false;
808*4882a593Smuzhiyun 
809*4882a593Smuzhiyun 	/* Match only the IP address, not the port number */
810*4882a593Smuzhiyun 	return rpc_cmp_addr(addr, clap);
811*4882a593Smuzhiyun }
812*4882a593Smuzhiyun 
813*4882a593Smuzhiyun /*
814*4882a593Smuzhiyun  * NFSv4.1 callback thread helper
815*4882a593Smuzhiyun  * For CB_COMPOUND calls, find a client by IP address, protocol version,
816*4882a593Smuzhiyun  * minorversion, and sessionID
817*4882a593Smuzhiyun  *
818*4882a593Smuzhiyun  * Returns NULL if no such client
819*4882a593Smuzhiyun  */
820*4882a593Smuzhiyun struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid,u32 minorversion)821*4882a593Smuzhiyun nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
822*4882a593Smuzhiyun 			   struct nfs4_sessionid *sid, u32 minorversion)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun 	struct nfs_client *clp;
825*4882a593Smuzhiyun 	struct nfs_net *nn = net_generic(net, nfs_net_id);
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	spin_lock(&nn->nfs_client_lock);
828*4882a593Smuzhiyun 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
829*4882a593Smuzhiyun 		if (!nfs4_cb_match_client(addr, clp, minorversion))
830*4882a593Smuzhiyun 			continue;
831*4882a593Smuzhiyun 
832*4882a593Smuzhiyun 		if (!nfs4_has_session(clp))
833*4882a593Smuzhiyun 			continue;
834*4882a593Smuzhiyun 
835*4882a593Smuzhiyun 		/* Match sessionid*/
836*4882a593Smuzhiyun 		if (memcmp(clp->cl_session->sess_id.data,
837*4882a593Smuzhiyun 		    sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
838*4882a593Smuzhiyun 			continue;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 		refcount_inc(&clp->cl_count);
841*4882a593Smuzhiyun 		spin_unlock(&nn->nfs_client_lock);
842*4882a593Smuzhiyun 		return clp;
843*4882a593Smuzhiyun 	}
844*4882a593Smuzhiyun 	spin_unlock(&nn->nfs_client_lock);
845*4882a593Smuzhiyun 	return NULL;
846*4882a593Smuzhiyun }
847*4882a593Smuzhiyun 
848*4882a593Smuzhiyun #else /* CONFIG_NFS_V4_1 */
849*4882a593Smuzhiyun 
850*4882a593Smuzhiyun struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid,u32 minorversion)851*4882a593Smuzhiyun nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
852*4882a593Smuzhiyun 			   struct nfs4_sessionid *sid, u32 minorversion)
853*4882a593Smuzhiyun {
854*4882a593Smuzhiyun 	return NULL;
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun #endif /* CONFIG_NFS_V4_1 */
857*4882a593Smuzhiyun 
858*4882a593Smuzhiyun /*
859*4882a593Smuzhiyun  * Set up an NFS4 client
860*4882a593Smuzhiyun  */
nfs4_set_client(struct nfs_server * server,const char * hostname,const struct sockaddr * addr,const size_t addrlen,const char * ip_addr,int proto,const struct rpc_timeout * timeparms,u32 minorversion,unsigned int nconnect,struct net * net)861*4882a593Smuzhiyun static int nfs4_set_client(struct nfs_server *server,
862*4882a593Smuzhiyun 		const char *hostname,
863*4882a593Smuzhiyun 		const struct sockaddr *addr,
864*4882a593Smuzhiyun 		const size_t addrlen,
865*4882a593Smuzhiyun 		const char *ip_addr,
866*4882a593Smuzhiyun 		int proto, const struct rpc_timeout *timeparms,
867*4882a593Smuzhiyun 		u32 minorversion, unsigned int nconnect,
868*4882a593Smuzhiyun 		struct net *net)
869*4882a593Smuzhiyun {
870*4882a593Smuzhiyun 	struct nfs_client_initdata cl_init = {
871*4882a593Smuzhiyun 		.hostname = hostname,
872*4882a593Smuzhiyun 		.addr = addr,
873*4882a593Smuzhiyun 		.addrlen = addrlen,
874*4882a593Smuzhiyun 		.ip_addr = ip_addr,
875*4882a593Smuzhiyun 		.nfs_mod = &nfs_v4,
876*4882a593Smuzhiyun 		.proto = proto,
877*4882a593Smuzhiyun 		.minorversion = minorversion,
878*4882a593Smuzhiyun 		.net = net,
879*4882a593Smuzhiyun 		.timeparms = timeparms,
880*4882a593Smuzhiyun 		.cred = server->cred,
881*4882a593Smuzhiyun 	};
882*4882a593Smuzhiyun 	struct nfs_client *clp;
883*4882a593Smuzhiyun 
884*4882a593Smuzhiyun 	if (minorversion == 0)
885*4882a593Smuzhiyun 		__set_bit(NFS_CS_REUSEPORT, &cl_init.init_flags);
886*4882a593Smuzhiyun 	if (proto == XPRT_TRANSPORT_TCP)
887*4882a593Smuzhiyun 		cl_init.nconnect = nconnect;
888*4882a593Smuzhiyun 
889*4882a593Smuzhiyun 	if (server->flags & NFS_MOUNT_NORESVPORT)
890*4882a593Smuzhiyun 		__set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
891*4882a593Smuzhiyun 	if (server->options & NFS_OPTION_MIGRATION)
892*4882a593Smuzhiyun 		__set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
893*4882a593Smuzhiyun 	if (test_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status))
894*4882a593Smuzhiyun 		__set_bit(NFS_CS_TSM_POSSIBLE, &cl_init.init_flags);
895*4882a593Smuzhiyun 	server->port = rpc_get_port(addr);
896*4882a593Smuzhiyun 
897*4882a593Smuzhiyun 	/* Allocate or find a client reference we can use */
898*4882a593Smuzhiyun 	clp = nfs_get_client(&cl_init);
899*4882a593Smuzhiyun 	if (IS_ERR(clp))
900*4882a593Smuzhiyun 		return PTR_ERR(clp);
901*4882a593Smuzhiyun 
902*4882a593Smuzhiyun 	if (server->nfs_client == clp) {
903*4882a593Smuzhiyun 		nfs_put_client(clp);
904*4882a593Smuzhiyun 		return -ELOOP;
905*4882a593Smuzhiyun 	}
906*4882a593Smuzhiyun 
907*4882a593Smuzhiyun 	/*
908*4882a593Smuzhiyun 	 * Query for the lease time on clientid setup or renewal
909*4882a593Smuzhiyun 	 *
910*4882a593Smuzhiyun 	 * Note that this will be set on nfs_clients that were created
911*4882a593Smuzhiyun 	 * only for the DS role and did not set this bit, but now will
912*4882a593Smuzhiyun 	 * serve a dual role.
913*4882a593Smuzhiyun 	 */
914*4882a593Smuzhiyun 	set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	server->nfs_client = clp;
917*4882a593Smuzhiyun 	return 0;
918*4882a593Smuzhiyun }
919*4882a593Smuzhiyun 
920*4882a593Smuzhiyun /*
921*4882a593Smuzhiyun  * Set up a pNFS Data Server client.
922*4882a593Smuzhiyun  *
923*4882a593Smuzhiyun  * Return any existing nfs_client that matches server address,port,version
924*4882a593Smuzhiyun  * and minorversion.
925*4882a593Smuzhiyun  *
926*4882a593Smuzhiyun  * For a new nfs_client, use a soft mount (default), a low retrans and a
927*4882a593Smuzhiyun  * low timeout interval so that if a connection is lost, we retry through
928*4882a593Smuzhiyun  * the MDS.
929*4882a593Smuzhiyun  */
nfs4_set_ds_client(struct nfs_server * mds_srv,const struct sockaddr * ds_addr,int ds_addrlen,int ds_proto,unsigned int ds_timeo,unsigned int ds_retrans,u32 minor_version)930*4882a593Smuzhiyun struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
931*4882a593Smuzhiyun 		const struct sockaddr *ds_addr, int ds_addrlen,
932*4882a593Smuzhiyun 		int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans,
933*4882a593Smuzhiyun 		u32 minor_version)
934*4882a593Smuzhiyun {
935*4882a593Smuzhiyun 	struct rpc_timeout ds_timeout;
936*4882a593Smuzhiyun 	struct nfs_client *mds_clp = mds_srv->nfs_client;
937*4882a593Smuzhiyun 	struct nfs_client_initdata cl_init = {
938*4882a593Smuzhiyun 		.addr = ds_addr,
939*4882a593Smuzhiyun 		.addrlen = ds_addrlen,
940*4882a593Smuzhiyun 		.nodename = mds_clp->cl_rpcclient->cl_nodename,
941*4882a593Smuzhiyun 		.ip_addr = mds_clp->cl_ipaddr,
942*4882a593Smuzhiyun 		.nfs_mod = &nfs_v4,
943*4882a593Smuzhiyun 		.proto = ds_proto,
944*4882a593Smuzhiyun 		.minorversion = minor_version,
945*4882a593Smuzhiyun 		.net = mds_clp->cl_net,
946*4882a593Smuzhiyun 		.timeparms = &ds_timeout,
947*4882a593Smuzhiyun 		.cred = mds_srv->cred,
948*4882a593Smuzhiyun 	};
949*4882a593Smuzhiyun 	char buf[INET6_ADDRSTRLEN + 1];
950*4882a593Smuzhiyun 
951*4882a593Smuzhiyun 	if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0)
952*4882a593Smuzhiyun 		return ERR_PTR(-EINVAL);
953*4882a593Smuzhiyun 	cl_init.hostname = buf;
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	if (mds_clp->cl_nconnect > 1 && ds_proto == XPRT_TRANSPORT_TCP)
956*4882a593Smuzhiyun 		cl_init.nconnect = mds_clp->cl_nconnect;
957*4882a593Smuzhiyun 
958*4882a593Smuzhiyun 	if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
959*4882a593Smuzhiyun 		__set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
960*4882a593Smuzhiyun 
961*4882a593Smuzhiyun 	/*
962*4882a593Smuzhiyun 	 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
963*4882a593Smuzhiyun 	 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
964*4882a593Smuzhiyun 	 * (section 13.1 RFC 5661).
965*4882a593Smuzhiyun 	 */
966*4882a593Smuzhiyun 	nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
967*4882a593Smuzhiyun 	return nfs_get_client(&cl_init);
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
970*4882a593Smuzhiyun 
971*4882a593Smuzhiyun /*
972*4882a593Smuzhiyun  * Session has been established, and the client marked ready.
973*4882a593Smuzhiyun  * Limit the mount rsize, wsize and dtsize using negotiated fore
974*4882a593Smuzhiyun  * channel attributes.
975*4882a593Smuzhiyun  */
nfs4_session_limit_rwsize(struct nfs_server * server)976*4882a593Smuzhiyun static void nfs4_session_limit_rwsize(struct nfs_server *server)
977*4882a593Smuzhiyun {
978*4882a593Smuzhiyun #ifdef CONFIG_NFS_V4_1
979*4882a593Smuzhiyun 	struct nfs4_session *sess;
980*4882a593Smuzhiyun 	u32 server_resp_sz;
981*4882a593Smuzhiyun 	u32 server_rqst_sz;
982*4882a593Smuzhiyun 
983*4882a593Smuzhiyun 	if (!nfs4_has_session(server->nfs_client))
984*4882a593Smuzhiyun 		return;
985*4882a593Smuzhiyun 	sess = server->nfs_client->cl_session;
986*4882a593Smuzhiyun 	server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
987*4882a593Smuzhiyun 	server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
988*4882a593Smuzhiyun 
989*4882a593Smuzhiyun 	if (server->dtsize > server_resp_sz)
990*4882a593Smuzhiyun 		server->dtsize = server_resp_sz;
991*4882a593Smuzhiyun 	if (server->rsize > server_resp_sz)
992*4882a593Smuzhiyun 		server->rsize = server_resp_sz;
993*4882a593Smuzhiyun 	if (server->wsize > server_rqst_sz)
994*4882a593Smuzhiyun 		server->wsize = server_rqst_sz;
995*4882a593Smuzhiyun #endif /* CONFIG_NFS_V4_1 */
996*4882a593Smuzhiyun }
997*4882a593Smuzhiyun 
998*4882a593Smuzhiyun /*
999*4882a593Smuzhiyun  * Limit xattr sizes using the channel attributes.
1000*4882a593Smuzhiyun  */
nfs4_session_limit_xasize(struct nfs_server * server)1001*4882a593Smuzhiyun static void nfs4_session_limit_xasize(struct nfs_server *server)
1002*4882a593Smuzhiyun {
1003*4882a593Smuzhiyun #ifdef CONFIG_NFS_V4_2
1004*4882a593Smuzhiyun 	struct nfs4_session *sess;
1005*4882a593Smuzhiyun 	u32 server_gxa_sz;
1006*4882a593Smuzhiyun 	u32 server_sxa_sz;
1007*4882a593Smuzhiyun 	u32 server_lxa_sz;
1008*4882a593Smuzhiyun 
1009*4882a593Smuzhiyun 	if (!nfs4_has_session(server->nfs_client))
1010*4882a593Smuzhiyun 		return;
1011*4882a593Smuzhiyun 
1012*4882a593Smuzhiyun 	sess = server->nfs_client->cl_session;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun 	server_gxa_sz = sess->fc_attrs.max_resp_sz - nfs42_maxgetxattr_overhead;
1015*4882a593Smuzhiyun 	server_sxa_sz = sess->fc_attrs.max_rqst_sz - nfs42_maxsetxattr_overhead;
1016*4882a593Smuzhiyun 	server_lxa_sz = sess->fc_attrs.max_resp_sz -
1017*4882a593Smuzhiyun 	    nfs42_maxlistxattrs_overhead;
1018*4882a593Smuzhiyun 
1019*4882a593Smuzhiyun 	if (server->gxasize > server_gxa_sz)
1020*4882a593Smuzhiyun 		server->gxasize = server_gxa_sz;
1021*4882a593Smuzhiyun 	if (server->sxasize > server_sxa_sz)
1022*4882a593Smuzhiyun 		server->sxasize = server_sxa_sz;
1023*4882a593Smuzhiyun 	if (server->lxasize > server_lxa_sz)
1024*4882a593Smuzhiyun 		server->lxasize = server_lxa_sz;
1025*4882a593Smuzhiyun #endif
1026*4882a593Smuzhiyun }
1027*4882a593Smuzhiyun 
nfs4_server_common_setup(struct nfs_server * server,struct nfs_fh * mntfh,bool auth_probe)1028*4882a593Smuzhiyun static int nfs4_server_common_setup(struct nfs_server *server,
1029*4882a593Smuzhiyun 		struct nfs_fh *mntfh, bool auth_probe)
1030*4882a593Smuzhiyun {
1031*4882a593Smuzhiyun 	struct nfs_fattr *fattr;
1032*4882a593Smuzhiyun 	int error;
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	/* data servers support only a subset of NFSv4.1 */
1035*4882a593Smuzhiyun 	if (is_ds_only_client(server->nfs_client))
1036*4882a593Smuzhiyun 		return -EPROTONOSUPPORT;
1037*4882a593Smuzhiyun 
1038*4882a593Smuzhiyun 	fattr = nfs_alloc_fattr();
1039*4882a593Smuzhiyun 	if (fattr == NULL)
1040*4882a593Smuzhiyun 		return -ENOMEM;
1041*4882a593Smuzhiyun 
1042*4882a593Smuzhiyun 	/* We must ensure the session is initialised first */
1043*4882a593Smuzhiyun 	error = nfs4_init_session(server->nfs_client);
1044*4882a593Smuzhiyun 	if (error < 0)
1045*4882a593Smuzhiyun 		goto out;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	/* Set the basic capabilities */
1048*4882a593Smuzhiyun 	server->caps |= server->nfs_client->cl_mvops->init_caps;
1049*4882a593Smuzhiyun 	if (server->flags & NFS_MOUNT_NORDIRPLUS)
1050*4882a593Smuzhiyun 			server->caps &= ~NFS_CAP_READDIRPLUS;
1051*4882a593Smuzhiyun 	if (server->nfs_client->cl_proto == XPRT_TRANSPORT_RDMA)
1052*4882a593Smuzhiyun 		server->caps &= ~NFS_CAP_READ_PLUS;
1053*4882a593Smuzhiyun 	/*
1054*4882a593Smuzhiyun 	 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1055*4882a593Smuzhiyun 	 * authentication.
1056*4882a593Smuzhiyun 	 */
1057*4882a593Smuzhiyun 	if (nfs4_disable_idmapping &&
1058*4882a593Smuzhiyun 			server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
1059*4882a593Smuzhiyun 		server->caps |= NFS_CAP_UIDGID_NOMAP;
1060*4882a593Smuzhiyun 
1061*4882a593Smuzhiyun 
1062*4882a593Smuzhiyun 	/* Probe the root fh to retrieve its FSID and filehandle */
1063*4882a593Smuzhiyun 	error = nfs4_get_rootfh(server, mntfh, auth_probe);
1064*4882a593Smuzhiyun 	if (error < 0)
1065*4882a593Smuzhiyun 		goto out;
1066*4882a593Smuzhiyun 
1067*4882a593Smuzhiyun 	dprintk("Server FSID: %llx:%llx\n",
1068*4882a593Smuzhiyun 			(unsigned long long) server->fsid.major,
1069*4882a593Smuzhiyun 			(unsigned long long) server->fsid.minor);
1070*4882a593Smuzhiyun 	nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun 	error = nfs_probe_fsinfo(server, mntfh, fattr);
1073*4882a593Smuzhiyun 	if (error < 0)
1074*4882a593Smuzhiyun 		goto out;
1075*4882a593Smuzhiyun 
1076*4882a593Smuzhiyun 	nfs4_session_limit_rwsize(server);
1077*4882a593Smuzhiyun 	nfs4_session_limit_xasize(server);
1078*4882a593Smuzhiyun 
1079*4882a593Smuzhiyun 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1080*4882a593Smuzhiyun 		server->namelen = NFS4_MAXNAMLEN;
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun 	nfs_server_insert_lists(server);
1083*4882a593Smuzhiyun 	server->mount_time = jiffies;
1084*4882a593Smuzhiyun 	server->destroy = nfs4_destroy_server;
1085*4882a593Smuzhiyun out:
1086*4882a593Smuzhiyun 	nfs_free_fattr(fattr);
1087*4882a593Smuzhiyun 	return error;
1088*4882a593Smuzhiyun }
1089*4882a593Smuzhiyun 
1090*4882a593Smuzhiyun /*
1091*4882a593Smuzhiyun  * Create a version 4 volume record
1092*4882a593Smuzhiyun  */
nfs4_init_server(struct nfs_server * server,struct fs_context * fc)1093*4882a593Smuzhiyun static int nfs4_init_server(struct nfs_server *server, struct fs_context *fc)
1094*4882a593Smuzhiyun {
1095*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1096*4882a593Smuzhiyun 	struct rpc_timeout timeparms;
1097*4882a593Smuzhiyun 	int error;
1098*4882a593Smuzhiyun 
1099*4882a593Smuzhiyun 	nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
1100*4882a593Smuzhiyun 				ctx->timeo, ctx->retrans);
1101*4882a593Smuzhiyun 
1102*4882a593Smuzhiyun 	/* Initialise the client representation from the mount data */
1103*4882a593Smuzhiyun 	server->flags = ctx->flags;
1104*4882a593Smuzhiyun 	server->options = ctx->options;
1105*4882a593Smuzhiyun 	server->auth_info = ctx->auth_info;
1106*4882a593Smuzhiyun 
1107*4882a593Smuzhiyun 	/* Use the first specified auth flavor. If this flavor isn't
1108*4882a593Smuzhiyun 	 * allowed by the server, use the SECINFO path to try the
1109*4882a593Smuzhiyun 	 * other specified flavors */
1110*4882a593Smuzhiyun 	if (ctx->auth_info.flavor_len >= 1)
1111*4882a593Smuzhiyun 		ctx->selected_flavor = ctx->auth_info.flavors[0];
1112*4882a593Smuzhiyun 	else
1113*4882a593Smuzhiyun 		ctx->selected_flavor = RPC_AUTH_UNIX;
1114*4882a593Smuzhiyun 
1115*4882a593Smuzhiyun 	/* Get a client record */
1116*4882a593Smuzhiyun 	error = nfs4_set_client(server,
1117*4882a593Smuzhiyun 				ctx->nfs_server.hostname,
1118*4882a593Smuzhiyun 				&ctx->nfs_server.address,
1119*4882a593Smuzhiyun 				ctx->nfs_server.addrlen,
1120*4882a593Smuzhiyun 				ctx->client_address,
1121*4882a593Smuzhiyun 				ctx->nfs_server.protocol,
1122*4882a593Smuzhiyun 				&timeparms,
1123*4882a593Smuzhiyun 				ctx->minorversion,
1124*4882a593Smuzhiyun 				ctx->nfs_server.nconnect,
1125*4882a593Smuzhiyun 				fc->net_ns);
1126*4882a593Smuzhiyun 	if (error < 0)
1127*4882a593Smuzhiyun 		return error;
1128*4882a593Smuzhiyun 
1129*4882a593Smuzhiyun 	if (ctx->rsize)
1130*4882a593Smuzhiyun 		server->rsize = nfs_block_size(ctx->rsize, NULL);
1131*4882a593Smuzhiyun 	if (ctx->wsize)
1132*4882a593Smuzhiyun 		server->wsize = nfs_block_size(ctx->wsize, NULL);
1133*4882a593Smuzhiyun 
1134*4882a593Smuzhiyun 	server->acregmin = ctx->acregmin * HZ;
1135*4882a593Smuzhiyun 	server->acregmax = ctx->acregmax * HZ;
1136*4882a593Smuzhiyun 	server->acdirmin = ctx->acdirmin * HZ;
1137*4882a593Smuzhiyun 	server->acdirmax = ctx->acdirmax * HZ;
1138*4882a593Smuzhiyun 	server->port     = ctx->nfs_server.port;
1139*4882a593Smuzhiyun 
1140*4882a593Smuzhiyun 	return nfs_init_server_rpcclient(server, &timeparms,
1141*4882a593Smuzhiyun 					 ctx->selected_flavor);
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun 
1144*4882a593Smuzhiyun /*
1145*4882a593Smuzhiyun  * Create a version 4 volume record
1146*4882a593Smuzhiyun  * - keyed on server and FSID
1147*4882a593Smuzhiyun  */
nfs4_create_server(struct fs_context * fc)1148*4882a593Smuzhiyun struct nfs_server *nfs4_create_server(struct fs_context *fc)
1149*4882a593Smuzhiyun {
1150*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1151*4882a593Smuzhiyun 	struct nfs_server *server;
1152*4882a593Smuzhiyun 	bool auth_probe;
1153*4882a593Smuzhiyun 	int error;
1154*4882a593Smuzhiyun 
1155*4882a593Smuzhiyun 	server = nfs_alloc_server();
1156*4882a593Smuzhiyun 	if (!server)
1157*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1158*4882a593Smuzhiyun 
1159*4882a593Smuzhiyun 	server->cred = get_cred(current_cred());
1160*4882a593Smuzhiyun 
1161*4882a593Smuzhiyun 	auth_probe = ctx->auth_info.flavor_len < 1;
1162*4882a593Smuzhiyun 
1163*4882a593Smuzhiyun 	/* set up the general RPC client */
1164*4882a593Smuzhiyun 	error = nfs4_init_server(server, fc);
1165*4882a593Smuzhiyun 	if (error < 0)
1166*4882a593Smuzhiyun 		goto error;
1167*4882a593Smuzhiyun 
1168*4882a593Smuzhiyun 	error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1169*4882a593Smuzhiyun 	if (error < 0)
1170*4882a593Smuzhiyun 		goto error;
1171*4882a593Smuzhiyun 
1172*4882a593Smuzhiyun 	return server;
1173*4882a593Smuzhiyun 
1174*4882a593Smuzhiyun error:
1175*4882a593Smuzhiyun 	nfs_free_server(server);
1176*4882a593Smuzhiyun 	return ERR_PTR(error);
1177*4882a593Smuzhiyun }
1178*4882a593Smuzhiyun 
1179*4882a593Smuzhiyun /*
1180*4882a593Smuzhiyun  * Create an NFS4 referral server record
1181*4882a593Smuzhiyun  */
nfs4_create_referral_server(struct fs_context * fc)1182*4882a593Smuzhiyun struct nfs_server *nfs4_create_referral_server(struct fs_context *fc)
1183*4882a593Smuzhiyun {
1184*4882a593Smuzhiyun 	struct nfs_fs_context *ctx = nfs_fc2context(fc);
1185*4882a593Smuzhiyun 	struct nfs_client *parent_client;
1186*4882a593Smuzhiyun 	struct nfs_server *server, *parent_server;
1187*4882a593Smuzhiyun 	bool auth_probe;
1188*4882a593Smuzhiyun 	int error;
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	server = nfs_alloc_server();
1191*4882a593Smuzhiyun 	if (!server)
1192*4882a593Smuzhiyun 		return ERR_PTR(-ENOMEM);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun 	parent_server = NFS_SB(ctx->clone_data.sb);
1195*4882a593Smuzhiyun 	parent_client = parent_server->nfs_client;
1196*4882a593Smuzhiyun 
1197*4882a593Smuzhiyun 	server->cred = get_cred(parent_server->cred);
1198*4882a593Smuzhiyun 
1199*4882a593Smuzhiyun 	/* Initialise the client representation from the parent server */
1200*4882a593Smuzhiyun 	nfs_server_copy_userdata(server, parent_server);
1201*4882a593Smuzhiyun 
1202*4882a593Smuzhiyun 	/* Get a client representation */
1203*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1204*4882a593Smuzhiyun 	rpc_set_port(&ctx->nfs_server.address, NFS_RDMA_PORT);
1205*4882a593Smuzhiyun 	error = nfs4_set_client(server,
1206*4882a593Smuzhiyun 				ctx->nfs_server.hostname,
1207*4882a593Smuzhiyun 				&ctx->nfs_server.address,
1208*4882a593Smuzhiyun 				ctx->nfs_server.addrlen,
1209*4882a593Smuzhiyun 				parent_client->cl_ipaddr,
1210*4882a593Smuzhiyun 				XPRT_TRANSPORT_RDMA,
1211*4882a593Smuzhiyun 				parent_server->client->cl_timeout,
1212*4882a593Smuzhiyun 				parent_client->cl_mvops->minor_version,
1213*4882a593Smuzhiyun 				parent_client->cl_nconnect,
1214*4882a593Smuzhiyun 				parent_client->cl_net);
1215*4882a593Smuzhiyun 	if (!error)
1216*4882a593Smuzhiyun 		goto init_server;
1217*4882a593Smuzhiyun #endif	/* IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) */
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
1220*4882a593Smuzhiyun 	error = nfs4_set_client(server,
1221*4882a593Smuzhiyun 				ctx->nfs_server.hostname,
1222*4882a593Smuzhiyun 				&ctx->nfs_server.address,
1223*4882a593Smuzhiyun 				ctx->nfs_server.addrlen,
1224*4882a593Smuzhiyun 				parent_client->cl_ipaddr,
1225*4882a593Smuzhiyun 				XPRT_TRANSPORT_TCP,
1226*4882a593Smuzhiyun 				parent_server->client->cl_timeout,
1227*4882a593Smuzhiyun 				parent_client->cl_mvops->minor_version,
1228*4882a593Smuzhiyun 				parent_client->cl_nconnect,
1229*4882a593Smuzhiyun 				parent_client->cl_net);
1230*4882a593Smuzhiyun 	if (error < 0)
1231*4882a593Smuzhiyun 		goto error;
1232*4882a593Smuzhiyun 
1233*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1234*4882a593Smuzhiyun init_server:
1235*4882a593Smuzhiyun #endif
1236*4882a593Smuzhiyun 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout,
1237*4882a593Smuzhiyun 					  ctx->selected_flavor);
1238*4882a593Smuzhiyun 	if (error < 0)
1239*4882a593Smuzhiyun 		goto error;
1240*4882a593Smuzhiyun 
1241*4882a593Smuzhiyun 	auth_probe = parent_server->auth_info.flavor_len < 1;
1242*4882a593Smuzhiyun 
1243*4882a593Smuzhiyun 	error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1244*4882a593Smuzhiyun 	if (error < 0)
1245*4882a593Smuzhiyun 		goto error;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	return server;
1248*4882a593Smuzhiyun 
1249*4882a593Smuzhiyun error:
1250*4882a593Smuzhiyun 	nfs_free_server(server);
1251*4882a593Smuzhiyun 	return ERR_PTR(error);
1252*4882a593Smuzhiyun }
1253*4882a593Smuzhiyun 
1254*4882a593Smuzhiyun /*
1255*4882a593Smuzhiyun  * Grab the destination's particulars, including lease expiry time.
1256*4882a593Smuzhiyun  *
1257*4882a593Smuzhiyun  * Returns zero if probe succeeded and retrieved FSID matches the FSID
1258*4882a593Smuzhiyun  * we have cached.
1259*4882a593Smuzhiyun  */
nfs_probe_destination(struct nfs_server * server)1260*4882a593Smuzhiyun static int nfs_probe_destination(struct nfs_server *server)
1261*4882a593Smuzhiyun {
1262*4882a593Smuzhiyun 	struct inode *inode = d_inode(server->super->s_root);
1263*4882a593Smuzhiyun 	struct nfs_fattr *fattr;
1264*4882a593Smuzhiyun 	int error;
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	fattr = nfs_alloc_fattr();
1267*4882a593Smuzhiyun 	if (fattr == NULL)
1268*4882a593Smuzhiyun 		return -ENOMEM;
1269*4882a593Smuzhiyun 
1270*4882a593Smuzhiyun 	/* Sanity: the probe won't work if the destination server
1271*4882a593Smuzhiyun 	 * does not recognize the migrated FH. */
1272*4882a593Smuzhiyun 	error = nfs_probe_fsinfo(server, NFS_FH(inode), fattr);
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 	nfs_free_fattr(fattr);
1275*4882a593Smuzhiyun 	return error;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun /**
1279*4882a593Smuzhiyun  * nfs4_update_server - Move an nfs_server to a different nfs_client
1280*4882a593Smuzhiyun  *
1281*4882a593Smuzhiyun  * @server: represents FSID to be moved
1282*4882a593Smuzhiyun  * @hostname: new end-point's hostname
1283*4882a593Smuzhiyun  * @sap: new end-point's socket address
1284*4882a593Smuzhiyun  * @salen: size of "sap"
1285*4882a593Smuzhiyun  * @net: net namespace
1286*4882a593Smuzhiyun  *
1287*4882a593Smuzhiyun  * The nfs_server must be quiescent before this function is invoked.
1288*4882a593Smuzhiyun  * Either its session is drained (NFSv4.1+), or its transport is
1289*4882a593Smuzhiyun  * plugged and drained (NFSv4.0).
1290*4882a593Smuzhiyun  *
1291*4882a593Smuzhiyun  * Returns zero on success, or a negative errno value.
1292*4882a593Smuzhiyun  */
nfs4_update_server(struct nfs_server * server,const char * hostname,struct sockaddr * sap,size_t salen,struct net * net)1293*4882a593Smuzhiyun int nfs4_update_server(struct nfs_server *server, const char *hostname,
1294*4882a593Smuzhiyun 		       struct sockaddr *sap, size_t salen, struct net *net)
1295*4882a593Smuzhiyun {
1296*4882a593Smuzhiyun 	struct nfs_client *clp = server->nfs_client;
1297*4882a593Smuzhiyun 	struct rpc_clnt *clnt = server->client;
1298*4882a593Smuzhiyun 	struct xprt_create xargs = {
1299*4882a593Smuzhiyun 		.ident		= clp->cl_proto,
1300*4882a593Smuzhiyun 		.net		= net,
1301*4882a593Smuzhiyun 		.dstaddr	= sap,
1302*4882a593Smuzhiyun 		.addrlen	= salen,
1303*4882a593Smuzhiyun 		.servername	= hostname,
1304*4882a593Smuzhiyun 	};
1305*4882a593Smuzhiyun 	char buf[INET6_ADDRSTRLEN + 1];
1306*4882a593Smuzhiyun 	struct sockaddr_storage address;
1307*4882a593Smuzhiyun 	struct sockaddr *localaddr = (struct sockaddr *)&address;
1308*4882a593Smuzhiyun 	int error;
1309*4882a593Smuzhiyun 
1310*4882a593Smuzhiyun 	error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
1311*4882a593Smuzhiyun 	if (error != 0)
1312*4882a593Smuzhiyun 		return error;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	error = rpc_localaddr(clnt, localaddr, sizeof(address));
1315*4882a593Smuzhiyun 	if (error != 0)
1316*4882a593Smuzhiyun 		return error;
1317*4882a593Smuzhiyun 
1318*4882a593Smuzhiyun 	if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0)
1319*4882a593Smuzhiyun 		return -EAFNOSUPPORT;
1320*4882a593Smuzhiyun 
1321*4882a593Smuzhiyun 	nfs_server_remove_lists(server);
1322*4882a593Smuzhiyun 	set_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1323*4882a593Smuzhiyun 	error = nfs4_set_client(server, hostname, sap, salen, buf,
1324*4882a593Smuzhiyun 				clp->cl_proto, clnt->cl_timeout,
1325*4882a593Smuzhiyun 				clp->cl_minorversion,
1326*4882a593Smuzhiyun 				clp->cl_nconnect, net);
1327*4882a593Smuzhiyun 	clear_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1328*4882a593Smuzhiyun 	if (error != 0) {
1329*4882a593Smuzhiyun 		nfs_server_insert_lists(server);
1330*4882a593Smuzhiyun 		return error;
1331*4882a593Smuzhiyun 	}
1332*4882a593Smuzhiyun 	nfs_put_client(clp);
1333*4882a593Smuzhiyun 
1334*4882a593Smuzhiyun 	if (server->nfs_client->cl_hostname == NULL) {
1335*4882a593Smuzhiyun 		server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
1336*4882a593Smuzhiyun 		if (server->nfs_client->cl_hostname == NULL)
1337*4882a593Smuzhiyun 			return -ENOMEM;
1338*4882a593Smuzhiyun 	}
1339*4882a593Smuzhiyun 	nfs_server_insert_lists(server);
1340*4882a593Smuzhiyun 
1341*4882a593Smuzhiyun 	return nfs_probe_destination(server);
1342*4882a593Smuzhiyun }
1343