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