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