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 #if IS_ENABLED(CONFIG_NFS_V4_1)
226 init_waitqueue_head(&clp->cl_lock_waitq);
227 #endif
228 INIT_LIST_HEAD(&clp->pending_cb_stateids);
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);
234 if (test_bit(NFS_CS_PNFS, &cl_init->init_flags))
235 __set_bit(NFS_CS_PNFS, &clp->cl_flags);
236 if (test_bit(NFS_CS_NETUNREACH_FATAL, &cl_init->init_flags))
237 __set_bit(NFS_CS_NETUNREACH_FATAL, &clp->cl_flags);
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 }
261 strscpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
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);
270 return clp;
271
272 error:
273 nfs_free_client(clp);
274 return ERR_PTR(err);
275 }
276
277 /*
278 * Destroy the NFS4 callback service
279 */
nfs4_destroy_callback(struct nfs_client * clp)280 static void nfs4_destroy_callback(struct nfs_client *clp)
281 {
282 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
283 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
284 }
285
nfs4_shutdown_client(struct nfs_client * clp)286 static 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);
290 clp->cl_mvops->shutdown_client(clp);
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);
299 kfree(clp->cl_owner_id);
300 }
301
nfs4_free_client(struct nfs_client * clp)302 void nfs4_free_client(struct nfs_client *clp)
303 {
304 nfs4_shutdown_client(clp);
305 nfs_free_client(clp);
306 }
307
308 /*
309 * Initialize the NFS4 callback service
310 */
nfs4_init_callback(struct nfs_client * clp)311 static int nfs4_init_callback(struct nfs_client *clp)
312 {
313 struct rpc_xprt *xprt;
314 int error;
315
316 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
317
318 if (nfs4_has_session(clp)) {
319 error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
320 if (error < 0)
321 return error;
322 }
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
332 return 0;
333 }
334
335 /**
336 * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
337 * @clp: nfs_client to initialize
338 *
339 * Returns zero on success, or a negative errno if some error occurred.
340 */
nfs40_init_client(struct nfs_client * clp)341 int 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) {
353 nfs4_shutdown_slot_table(tbl);
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+
366 * @clp: nfs_client to initialize
367 *
368 * Returns zero on success, or a negative errno if some error occurred.
369 */
nfs41_init_client(struct nfs_client * clp)370 int 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
397 /*
398 * Initialize the minor version specific parts of an NFS4 client record
399 */
nfs4_init_client_minor_version(struct nfs_client * clp)400 static int nfs4_init_client_minor_version(struct nfs_client *clp)
401 {
402 int ret;
403
404 ret = clp->cl_mvops->init_client(clp);
405 if (ret)
406 return ret;
407 return nfs4_init_callback(clp);
408 }
409
nfs4_add_trunk(struct nfs_client * clp,struct nfs_client * old)410 static 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 };
421 int max_connect = test_bit(NFS_CS_PNFS, &clp->cl_flags) ?
422 clp->cl_max_connect : old->cl_max_connect;
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,
436 rpc_clnt_test_and_add_xprt, &max_connect);
437 }
438
439 /**
440 * nfs4_init_client - Initialise an NFS4 client record
441 *
442 * @clp: nfs_client to initialise
443 * @cl_init: pointer to nfs_client_initdata
444 *
445 * Returns pointer to an NFS client, or an ERR_PTR value.
446 */
nfs4_init_client(struct nfs_client * clp,const struct nfs_client_initdata * cl_init)447 struct nfs_client *nfs4_init_client(struct nfs_client *clp,
448 const struct nfs_client_initdata *cl_init)
449 {
450 struct nfs_client *old;
451 int error;
452
453 if (clp->cl_cons_state == NFS_CS_READY)
454 /* the client is initialised already */
455 return clp;
456
457 error = nfs4_init_client_minor_version(clp);
458 if (error < 0)
459 goto error;
460
461 error = nfs4_discover_server_trunking(clp, &old);
462 if (error < 0)
463 goto error;
464
465 if (clp != old) {
466 clp->cl_preserve_clid = true;
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);
473 if (old->cl_mvops->session_trunk)
474 nfs4_add_trunk(clp, old);
475 }
476 clear_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags);
477 nfs_put_client(clp);
478 return old;
479
480 error:
481 nfs_mark_client_ready(clp, error);
482 nfs_put_client(clp);
483 return ERR_PTR(error);
484 }
485
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 */
nfs4_swap_callback_idents(struct nfs_client * keep,struct nfs_client * drop)494 static 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
nfs4_match_client_owner_id(const struct nfs_client * clp1,const struct nfs_client * clp2)517 static 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
nfs4_same_verifier(nfs4_verifier * v1,nfs4_verifier * v2)525 static bool nfs4_same_verifier(nfs4_verifier *v1, nfs4_verifier *v2)
526 {
527 return memcmp(v1->data, v2->data, sizeof(v1->data)) == 0;
528 }
529
nfs4_match_client(struct nfs_client * pos,struct nfs_client * new,struct nfs_client ** prev,struct nfs_net * nn)530 static 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) {
546 refcount_inc(&pos->cl_count);
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
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 */
nfs40_walk_client_list(struct nfs_client * new,struct nfs_client ** result,const struct cred * cred)587 int nfs40_walk_client_list(struct nfs_client *new,
588 struct nfs_client **result,
589 const struct cred *cred)
590 {
591 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
592 struct nfs_client *pos, *prev = NULL;
593 struct nfs4_setclientid_res clid = {
594 .clientid = new->cl_clientid,
595 .confirm = new->cl_confirm,
596 };
597 int status = -NFS4ERR_STALE_CLIENTID;
598
599 spin_lock(&nn->nfs_client_lock);
600 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
601
602 if (pos == new)
603 goto found;
604
605 status = nfs4_match_client(pos, new, &prev, nn);
606 if (status < 0)
607 goto out_unlock;
608 if (status != 0)
609 continue;
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 */
625 found:
626 refcount_inc(&pos->cl_count);
627 spin_unlock(&nn->nfs_client_lock);
628
629 nfs_put_client(prev);
630 prev = pos;
631
632 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
633 switch (status) {
634 case -NFS4ERR_STALE_CLIENTID:
635 break;
636 case 0:
637 nfs4_swap_callback_idents(pos, new);
638 pos->cl_confirm = new->cl_confirm;
639 nfs_mark_client_ready(pos, NFS_CS_READY);
640
641 prev = NULL;
642 *result = pos;
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);
650 goto out;
651 default:
652 goto out;
653 }
654
655 spin_lock(&nn->nfs_client_lock);
656 }
657 out_unlock:
658 spin_unlock(&nn->nfs_client_lock);
659
660 /* No match found. The server lost our clientid */
661 out:
662 nfs_put_client(prev);
663 return status;
664 }
665
666 #ifdef CONFIG_NFS_V4_1
667 /*
668 * Returns true if the server major ids match
669 */
670 bool
nfs4_check_serverowner_major_id(struct nfs41_server_owner * o1,struct nfs41_server_owner * o2)671 nfs4_check_serverowner_major_id(struct nfs41_server_owner *o1,
672 struct nfs41_server_owner *o2)
673 {
674 if (o1->major_id_sz != o2->major_id_sz)
675 return false;
676 return memcmp(o1->major_id, o2->major_id, o1->major_id_sz) == 0;
677 }
678
679 /*
680 * Returns true if the server scopes match
681 */
682 static bool
nfs4_check_server_scope(struct nfs41_server_scope * s1,struct nfs41_server_scope * s2)683 nfs4_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)
687 return false;
688 return memcmp(s1->server_scope, s2->server_scope,
689 s1->server_scope_sz) == 0;
690 }
691
692 /**
693 * nfs4_detect_session_trunking - Checks for session trunking.
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
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.
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 */
nfs4_detect_session_trunking(struct nfs_client * clp,struct nfs41_exchange_id_res * res,struct rpc_xprt * xprt)708 int 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 */
713 if (clp->cl_clientid != res->clientid)
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 */
722 if (clp->cl_serverowner->minor_id != res->server_owner->minor_id)
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;
734 out_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
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 */
nfs41_walk_client_list(struct nfs_client * new,struct nfs_client ** result,const struct cred * cred)754 int nfs41_walk_client_list(struct nfs_client *new,
755 struct nfs_client **result,
756 const struct cred *cred)
757 {
758 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
759 struct nfs_client *pos, *prev = NULL;
760 int status = -NFS4ERR_STALE_CLIENTID;
761
762 spin_lock(&nn->nfs_client_lock);
763 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
764
765 if (pos == new)
766 goto found;
767
768 status = nfs4_match_client(pos, new, &prev, nn);
769 if (status < 0)
770 goto out;
771 if (status != 0)
772 continue;
773
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 */
779 if (!nfs4_check_serverowner_major_id(pos->cl_serverowner,
780 new->cl_serverowner))
781 continue;
782
783 found:
784 refcount_inc(&pos->cl_count);
785 *result = pos;
786 status = 0;
787 break;
788 }
789
790 out:
791 spin_unlock(&nn->nfs_client_lock);
792 nfs_put_client(prev);
793 return status;
794 }
795 #endif /* CONFIG_NFS_V4_1 */
796
nfs4_destroy_server(struct nfs_server * server)797 static void nfs4_destroy_server(struct nfs_server *server)
798 {
799 LIST_HEAD(freeme);
800
801 nfs_server_return_all_delegations(server);
802 unset_pnfs_layoutdriver(server);
803 nfs4_purge_state_owners(server, &freeme);
804 nfs4_free_state_owners(&freeme);
805 }
806
807 /*
808 * NFSv4.0 callback thread helper
809 *
810 * Find a client by callback identifier
811 */
812 struct nfs_client *
nfs4_find_client_ident(struct net * net,int cb_ident)813 nfs4_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)
821 refcount_inc(&clp->cl_count);
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 */
nfs4_cb_match_client(const struct sockaddr * addr,struct nfs_client * clp,u32 minorversion)828 static 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 */
846 return rpc_cmp_addr(addr, clap);
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 */
856 struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid,u32 minorversion)857 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
858 struct nfs4_sessionid *sid, u32 minorversion)
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) {
865 if (!nfs4_cb_match_client(addr, clp, minorversion))
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
876 refcount_inc(&clp->cl_count);
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
886 struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid,u32 minorversion)887 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
888 struct nfs4_sessionid *sid, u32 minorversion)
889 {
890 return NULL;
891 }
892 #endif /* CONFIG_NFS_V4_1 */
893
894 /*
895 * Set up an NFS4 client
896 */
nfs4_set_client(struct nfs_server * server,const char * hostname,const struct sockaddr_storage * addr,const size_t addrlen,const char * ip_addr,int proto,const struct rpc_timeout * timeparms,u32 minorversion,unsigned int nconnect,unsigned int max_connect,struct net * net,struct xprtsec_parms * xprtsec)897 static int nfs4_set_client(struct nfs_server *server,
898 const char *hostname,
899 const struct sockaddr_storage *addr,
900 const size_t addrlen,
901 const char *ip_addr,
902 int proto, const struct rpc_timeout *timeparms,
903 u32 minorversion, unsigned int nconnect,
904 unsigned int max_connect,
905 struct net *net,
906 struct xprtsec_parms *xprtsec)
907 {
908 struct nfs_client_initdata cl_init = {
909 .hostname = hostname,
910 .addr = addr,
911 .addrlen = addrlen,
912 .ip_addr = ip_addr,
913 .nfs_mod = &nfs_v4,
914 .proto = proto,
915 .minorversion = minorversion,
916 .net = net,
917 .timeparms = timeparms,
918 .cred = server->cred,
919 .xprtsec = *xprtsec,
920 };
921 struct nfs_client *clp;
922
923 if (minorversion == 0)
924 __set_bit(NFS_CS_REUSEPORT, &cl_init.init_flags);
925 else
926 cl_init.max_connect = max_connect;
927 switch (proto) {
928 case XPRT_TRANSPORT_RDMA:
929 case XPRT_TRANSPORT_TCP:
930 case XPRT_TRANSPORT_TCP_TLS:
931 cl_init.nconnect = nconnect;
932 }
933
934 if (server->flags & NFS_MOUNT_NORESVPORT)
935 __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
936 if (server->options & NFS_OPTION_MIGRATION)
937 __set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
938 if (test_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status))
939 __set_bit(NFS_CS_TSM_POSSIBLE, &cl_init.init_flags);
940 server->port = rpc_get_port((struct sockaddr *)addr);
941
942 if (server->flags & NFS_MOUNT_NETUNREACH_FATAL)
943 __set_bit(NFS_CS_NETUNREACH_FATAL, &cl_init.init_flags);
944
945 /* Allocate or find a client reference we can use */
946 clp = nfs_get_client(&cl_init);
947 if (IS_ERR(clp))
948 return PTR_ERR(clp);
949
950 if (server->nfs_client == clp) {
951 nfs_put_client(clp);
952 return -ELOOP;
953 }
954
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;
965 nfs_sysfs_add_server(server);
966 nfs_sysfs_link_rpc_client(server, clp->cl_rpcclient, "_state");
967
968 return 0;
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 */
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)981 struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
982 const struct sockaddr_storage *ds_addr, int ds_addrlen,
983 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans,
984 u32 minor_version)
985 {
986 struct rpc_timeout ds_timeout;
987 struct nfs_client *mds_clp = mds_srv->nfs_client;
988 struct nfs_client_initdata cl_init = {
989 .addr = ds_addr,
990 .addrlen = ds_addrlen,
991 .nodename = mds_clp->cl_rpcclient->cl_nodename,
992 .ip_addr = mds_clp->cl_ipaddr,
993 .nfs_mod = &nfs_v4,
994 .proto = ds_proto,
995 .minorversion = minor_version,
996 .net = mds_clp->cl_net,
997 .timeparms = &ds_timeout,
998 .cred = mds_srv->cred,
999 .xprtsec = mds_srv->nfs_client->cl_xprtsec,
1000 };
1001 char buf[INET6_ADDRSTRLEN + 1];
1002
1003 if (rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf)) <= 0)
1004 return ERR_PTR(-EINVAL);
1005 cl_init.hostname = buf;
1006
1007 switch (ds_proto) {
1008 case XPRT_TRANSPORT_RDMA:
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 }
1015 }
1016
1017 if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
1018 __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
1019 if (test_bit(NFS_CS_NETUNREACH_FATAL, &mds_clp->cl_flags))
1020 __set_bit(NFS_CS_NETUNREACH_FATAL, &cl_init.init_flags);
1021
1022 __set_bit(NFS_CS_PNFS, &cl_init.init_flags);
1023 cl_init.max_connect = NFS_MAX_TRANSPORTS;
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);
1030 return nfs_get_client(&cl_init);
1031 }
1032 EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
1033
1034 /*
1035 * Session has been established, and the client marked ready.
1036 * Limit the mount rsize, wsize and dtsize using negotiated fore
1037 * channel attributes.
1038 */
nfs4_session_limit_rwsize(struct nfs_server * server)1039 static void nfs4_session_limit_rwsize(struct nfs_server *server)
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
1052 if (server->dtsize > server_resp_sz)
1053 server->dtsize = server_resp_sz;
1054 if (server->rsize > server_resp_sz)
1055 server->rsize = server_resp_sz;
1056 if (server->wsize > server_rqst_sz)
1057 server->wsize = server_rqst_sz;
1058 #endif /* CONFIG_NFS_V4_1 */
1059 }
1060
1061 /*
1062 * Limit xattr sizes using the channel attributes.
1063 */
nfs4_session_limit_xasize(struct nfs_server * server)1064 static 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
nfs4_server_set_init_caps(struct nfs_server * server)1091 void 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
nfs4_server_common_setup(struct nfs_server * server,struct nfs_fh * mntfh,bool auth_probe)1109 static int nfs4_server_common_setup(struct nfs_server *server,
1110 struct nfs_fh *mntfh, bool auth_probe)
1111 {
1112 int error;
1113
1114 /* data servers support only a subset of NFSv4.1 */
1115 if (is_ds_only_client(server->nfs_client))
1116 return -EPROTONOSUPPORT;
1117
1118 /* We must ensure the session is initialised first */
1119 error = nfs4_init_session(server->nfs_client);
1120 if (error < 0)
1121 goto out;
1122
1123 nfs4_server_set_init_caps(server);
1124
1125 /* Probe the root fh to retrieve its FSID and filehandle */
1126 error = nfs4_get_rootfh(server, mntfh, auth_probe);
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);
1133 nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
1134
1135 error = nfs_probe_server(server, mntfh);
1136 if (error < 0)
1137 goto out;
1138
1139 nfs4_session_limit_rwsize(server);
1140 nfs4_session_limit_xasize(server);
1141
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;
1148 out:
1149 return error;
1150 }
1151
1152 /*
1153 * Create a version 4 volume record
1154 */
nfs4_init_server(struct nfs_server * server,struct fs_context * fc)1155 static int nfs4_init_server(struct nfs_server *server, struct fs_context *fc)
1156 {
1157 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1158 struct rpc_timeout timeparms;
1159 int error;
1160
1161 nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
1162 ctx->timeo, ctx->retrans);
1163
1164 /* Initialise the client representation from the mount data */
1165 server->flags = ctx->flags;
1166 server->options = ctx->options;
1167 server->auth_info = ctx->auth_info;
1168
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 */
1172 if (ctx->auth_info.flavor_len >= 1)
1173 ctx->selected_flavor = ctx->auth_info.flavors[0];
1174 else
1175 ctx->selected_flavor = RPC_AUTH_UNIX;
1176
1177 /* Get a client record */
1178 error = nfs4_set_client(server,
1179 ctx->nfs_server.hostname,
1180 &ctx->nfs_server._address,
1181 ctx->nfs_server.addrlen,
1182 ctx->client_address,
1183 ctx->nfs_server.protocol,
1184 &timeparms,
1185 ctx->minorversion,
1186 ctx->nfs_server.nconnect,
1187 ctx->nfs_server.max_connect,
1188 fc->net_ns,
1189 &ctx->xprtsec);
1190 if (error < 0)
1191 return error;
1192
1193 if (ctx->rsize)
1194 server->rsize = nfs_io_size(ctx->rsize, server->nfs_client->cl_proto);
1195 if (ctx->wsize)
1196 server->wsize = nfs_io_size(ctx->wsize, server->nfs_client->cl_proto);
1197
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;
1203
1204 return nfs_init_server_rpcclient(server, &timeparms,
1205 ctx->selected_flavor);
1206 }
1207
1208 /*
1209 * Create a version 4 volume record
1210 * - keyed on server and FSID
1211 */
nfs4_create_server(struct fs_context * fc)1212 struct nfs_server *nfs4_create_server(struct fs_context *fc)
1213 {
1214 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1215 struct nfs_server *server;
1216 bool auth_probe;
1217 int error;
1218
1219 server = nfs_alloc_server();
1220 if (!server)
1221 return ERR_PTR(-ENOMEM);
1222
1223 server->cred = get_cred(fc->cred);
1224
1225 auth_probe = ctx->auth_info.flavor_len < 1;
1226
1227 /* set up the general RPC client */
1228 error = nfs4_init_server(server, fc);
1229 if (error < 0)
1230 goto error;
1231
1232 error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1233 if (error < 0)
1234 goto error;
1235
1236 return server;
1237
1238 error:
1239 nfs_free_server(server);
1240 return ERR_PTR(error);
1241 }
1242
1243 /*
1244 * Create an NFS4 referral server record
1245 */
nfs4_create_referral_server(struct fs_context * fc)1246 struct nfs_server *nfs4_create_referral_server(struct fs_context *fc)
1247 {
1248 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1249 struct nfs_client *parent_client;
1250 struct nfs_server *server, *parent_server;
1251 int proto, error;
1252 bool auth_probe;
1253
1254 server = nfs_alloc_server();
1255 if (!server)
1256 return ERR_PTR(-ENOMEM);
1257
1258 parent_server = NFS_SB(ctx->clone_data.sb);
1259 parent_client = parent_server->nfs_client;
1260
1261 server->cred = get_cred(parent_server->cred);
1262
1263 /* Initialise the client representation from the parent server */
1264 nfs_server_copy_userdata(server, parent_server);
1265
1266 /* Get a client representation */
1267 #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1268 rpc_set_port(&ctx->nfs_server.address, NFS_RDMA_PORT);
1269 error = nfs4_set_client(server,
1270 ctx->nfs_server.hostname,
1271 &ctx->nfs_server._address,
1272 ctx->nfs_server.addrlen,
1273 parent_client->cl_ipaddr,
1274 XPRT_TRANSPORT_RDMA,
1275 parent_server->client->cl_timeout,
1276 parent_client->cl_mvops->minor_version,
1277 parent_client->cl_nconnect,
1278 parent_client->cl_max_connect,
1279 parent_client->cl_net,
1280 &parent_client->cl_xprtsec);
1281 if (!error)
1282 goto init_server;
1283 #endif /* IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) */
1284
1285 proto = XPRT_TRANSPORT_TCP;
1286 if (parent_client->cl_xprtsec.policy != RPC_XPRTSEC_NONE)
1287 proto = XPRT_TRANSPORT_TCP_TLS;
1288 rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
1289 error = nfs4_set_client(server,
1290 ctx->nfs_server.hostname,
1291 &ctx->nfs_server._address,
1292 ctx->nfs_server.addrlen,
1293 parent_client->cl_ipaddr,
1294 proto,
1295 parent_server->client->cl_timeout,
1296 parent_client->cl_mvops->minor_version,
1297 parent_client->cl_nconnect,
1298 parent_client->cl_max_connect,
1299 parent_client->cl_net,
1300 &parent_client->cl_xprtsec);
1301 if (error < 0)
1302 goto error;
1303
1304 #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1305 init_server:
1306 #endif
1307 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout,
1308 ctx->selected_flavor);
1309 if (error < 0)
1310 goto error;
1311
1312 auth_probe = parent_server->auth_info.flavor_len < 1;
1313
1314 error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1315 if (error < 0)
1316 goto error;
1317
1318 return server;
1319
1320 error:
1321 nfs_free_server(server);
1322 return ERR_PTR(error);
1323 }
1324
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"
1332 * @net: net namespace
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 */
nfs4_update_server(struct nfs_server * server,const char * hostname,struct sockaddr_storage * sap,size_t salen,struct net * net)1340 int nfs4_update_server(struct nfs_server *server, const char *hostname,
1341 struct sockaddr_storage *sap, size_t salen, struct net *net)
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,
1347 .net = net,
1348 .dstaddr = (struct sockaddr *)sap,
1349 .addrlen = salen,
1350 .servername = hostname,
1351 /* cel: bleh. We might need to pass TLS parameters here */
1352 };
1353 char buf[INET6_ADDRSTRLEN + 1];
1354 struct sockaddr_storage address;
1355 struct sockaddr *localaddr = (struct sockaddr *)&address;
1356 int error;
1357
1358 error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
1359 if (error != 0)
1360 return error;
1361
1362 error = rpc_localaddr(clnt, localaddr, sizeof(address));
1363 if (error != 0)
1364 return error;
1365
1366 if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0)
1367 return -EAFNOSUPPORT;
1368
1369 nfs_server_remove_lists(server);
1370 set_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1371 error = nfs4_set_client(server, hostname, sap, salen, buf,
1372 clp->cl_proto, clnt->cl_timeout,
1373 clp->cl_minorversion,
1374 clp->cl_nconnect, clp->cl_max_connect,
1375 net, &clp->cl_xprtsec);
1376 clear_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1377 if (error != 0) {
1378 nfs_server_insert_lists(server);
1379 return error;
1380 }
1381 nfs_put_client(clp);
1382
1383 if (server->nfs_client->cl_hostname == NULL) {
1384 server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
1385 if (server->nfs_client->cl_hostname == NULL)
1386 return -ENOMEM;
1387 }
1388 nfs_server_insert_lists(server);
1389
1390 return nfs_probe_server(server, NFS_FH(d_inode(server->super->s_root)));
1391 }
1392