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