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