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