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