]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nfs/nfs4client.c
NFSv4.1: Fix client id trunking on Linux
[mirror_ubuntu-zesty-kernel.git] / fs / nfs / nfs4client.c
CommitLineData
428360d7
BS
1/*
2 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
3 * Written by David Howells (dhowells@redhat.com)
4 */
fcf10398 5#include <linux/module.h>
428360d7
BS
6#include <linux/nfs_fs.h>
7#include <linux/nfs_idmap.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"
fcf10398
BS
18#include "pnfs.h"
19#include "netns.h"
428360d7
BS
20
21#define NFSDBG_FACILITY NFSDBG_CLIENT
22
ec409897
BS
23/*
24 * Get a unique NFSv4.0 callback identifier which will be used
25 * by the V4.0 callback service to lookup the nfs_client struct
26 */
27static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
28{
29 int ret = 0;
30 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
31
32 if (clp->rpc_ops->version != 4 || minorversion != 0)
33 return ret;
d6870312 34 idr_preload(GFP_KERNEL);
ec409897 35 spin_lock(&nn->nfs_client_lock);
d6870312
TH
36 ret = idr_alloc(&nn->cb_ident_idr, clp, 0, 0, GFP_NOWAIT);
37 if (ret >= 0)
38 clp->cl_cb_ident = ret;
ec409897 39 spin_unlock(&nn->nfs_client_lock);
d6870312
TH
40 idr_preload_end();
41 return ret < 0 ? ret : 0;
ec409897
BS
42}
43
44#ifdef CONFIG_NFS_V4_1
0e20162e
AA
45/**
46 * Per auth flavor data server rpc clients
47 */
48struct nfs4_ds_server {
49 struct list_head list; /* ds_clp->cl_ds_clients */
50 struct rpc_clnt *rpc_clnt;
51};
52
53/**
54 * Common lookup case for DS I/O
55 */
56static struct nfs4_ds_server *
57nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
58{
59 struct nfs4_ds_server *dss;
60
61 rcu_read_lock();
62 list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) {
63 if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
64 continue;
65 goto out;
66 }
67 dss = NULL;
68out:
69 rcu_read_unlock();
70 return dss;
71}
72
73static struct nfs4_ds_server *
74nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor,
75 struct nfs4_ds_server *new)
76{
77 struct nfs4_ds_server *dss;
78
79 spin_lock(&ds_clp->cl_lock);
80 list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) {
81 if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
82 continue;
83 goto out;
84 }
85 if (new)
86 list_add_rcu(&new->list, &ds_clp->cl_ds_clients);
87 dss = new;
88out:
89 spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */
90 return dss;
91}
92
93static struct nfs4_ds_server *
94nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
95{
96 struct nfs4_ds_server *dss;
97
98 dss = kmalloc(sizeof(*dss), GFP_NOFS);
99 if (dss == NULL)
100 return ERR_PTR(-ENOMEM);
101
102 dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor);
103 if (IS_ERR(dss->rpc_clnt)) {
104 int err = PTR_ERR(dss->rpc_clnt);
105 kfree (dss);
106 return ERR_PTR(err);
107 }
108 INIT_LIST_HEAD(&dss->list);
109
110 return dss;
111}
112
113static void
114nfs4_free_ds_server(struct nfs4_ds_server *dss)
115{
116 rpc_release_client(dss->rpc_clnt);
117 kfree(dss);
118}
119
120/**
121* Find or create a DS rpc client with th MDS server rpc client auth flavor
122* in the nfs_client cl_ds_clients list.
123*/
124struct rpc_clnt *
125nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode)
126{
127 struct nfs4_ds_server *dss, *new;
128 rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor;
129
130 dss = nfs4_find_ds_client(ds_clp, flavor);
131 if (dss != NULL)
132 goto out;
133 new = nfs4_alloc_ds_server(ds_clp, flavor);
134 if (IS_ERR(new))
135 return ERR_CAST(new);
136 dss = nfs4_add_ds_client(ds_clp, flavor, new);
137 if (dss != new)
138 nfs4_free_ds_server(new);
139out:
140 return dss->rpc_clnt;
141}
142EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client);
143
144static void
145nfs4_shutdown_ds_clients(struct nfs_client *clp)
146{
147 struct nfs4_ds_server *dss;
148 LIST_HEAD(shutdown_list);
149
150 while (!list_empty(&clp->cl_ds_clients)) {
151 dss = list_entry(clp->cl_ds_clients.next,
152 struct nfs4_ds_server, list);
153 list_del(&dss->list);
154 rpc_shutdown_client(dss->rpc_clnt);
155 kfree (dss);
156 }
157}
158
abf79bb3 159void nfs41_shutdown_client(struct nfs_client *clp)
ec409897
BS
160{
161 if (nfs4_has_session(clp)) {
0e20162e 162 nfs4_shutdown_ds_clients(clp);
ec409897
BS
163 nfs4_destroy_session(clp->cl_session);
164 nfs4_destroy_clientid(clp);
165 }
166
167}
abf79bb3
CL
168#endif /* CONFIG_NFS_V4_1 */
169
170void nfs40_shutdown_client(struct nfs_client *clp)
ec409897 171{
abf79bb3 172 if (clp->cl_slot_tbl) {
20b9a902 173 nfs4_shutdown_slot_table(clp->cl_slot_tbl);
abf79bb3
CL
174 kfree(clp->cl_slot_tbl);
175 }
ec409897 176}
ec409897
BS
177
178struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
179{
180 int err;
181 struct nfs_client *clp = nfs_alloc_client(cl_init);
182 if (IS_ERR(clp))
183 return clp;
184
185 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
186 if (err)
187 goto error;
188
42c2c424
SD
189 if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
190 err = -EINVAL;
191 goto error;
192 }
193
ec409897
BS
194 spin_lock_init(&clp->cl_lock);
195 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
0e20162e 196 INIT_LIST_HEAD(&clp->cl_ds_clients);
ec409897
BS
197 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
198 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
199 clp->cl_minorversion = cl_init->minorversion;
200 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
c9fdeb28 201 clp->cl_mig_gen = 1;
ec409897
BS
202 return clp;
203
204error:
7653f6ff 205 nfs_free_client(clp);
ec409897
BS
206 return ERR_PTR(err);
207}
208
209/*
210 * Destroy the NFS4 callback service
211 */
212static void nfs4_destroy_callback(struct nfs_client *clp)
213{
214 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
c946556b 215 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
ec409897
BS
216}
217
218static void nfs4_shutdown_client(struct nfs_client *clp)
219{
220 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
221 nfs4_kill_renewd(clp);
abf79bb3 222 clp->cl_mvops->shutdown_client(clp);
ec409897
BS
223 nfs4_destroy_callback(clp);
224 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
225 nfs_idmap_delete(clp);
226
227 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
228 kfree(clp->cl_serverowner);
229 kfree(clp->cl_serverscope);
230 kfree(clp->cl_implid);
231}
232
233void nfs4_free_client(struct nfs_client *clp)
234{
235 nfs4_shutdown_client(clp);
236 nfs_free_client(clp);
237}
238
428360d7
BS
239/*
240 * Initialize the NFS4 callback service
241 */
242static int nfs4_init_callback(struct nfs_client *clp)
243{
c2ef47b7 244 struct rpc_xprt *xprt;
428360d7
BS
245 int error;
246
c2ef47b7 247 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
428360d7 248
c2ef47b7
CL
249 if (nfs4_has_session(clp)) {
250 error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
251 if (error < 0)
428360d7 252 return error;
428360d7 253 }
c2ef47b7
CL
254
255 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
256 if (error < 0) {
257 dprintk("%s: failed to start callback. Error = %d\n",
258 __func__, error);
259 return error;
260 }
261 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
262
428360d7
BS
263 return 0;
264}
265
abf79bb3
CL
266/**
267 * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
268 * @clp - nfs_client to initialize
269 *
270 * Returns zero on success, or a negative errno if some error occurred.
271 */
272int nfs40_init_client(struct nfs_client *clp)
273{
274 struct nfs4_slot_table *tbl;
275 int ret;
276
277 tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
278 if (tbl == NULL)
279 return -ENOMEM;
280
281 ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
282 "NFSv4.0 transport Slot table");
283 if (ret) {
284 kfree(tbl);
285 return ret;
286 }
287
288 clp->cl_slot_tbl = tbl;
289 return 0;
290}
291
292#if defined(CONFIG_NFS_V4_1)
293
294/**
295 * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
296 * @clp - nfs_client to initialize
297 *
298 * Returns zero on success, or a negative errno if some error occurred.
299 */
300int nfs41_init_client(struct nfs_client *clp)
301{
302 struct nfs4_session *session = NULL;
303
304 /*
305 * Create the session and mark it expired.
306 * When a SEQUENCE operation encounters the expired session
307 * it will do session recovery to initialize it.
308 */
309 session = nfs4_alloc_session(clp);
310 if (!session)
311 return -ENOMEM;
312
313 clp->cl_session = session;
314
315 /*
316 * The create session reply races with the server back
317 * channel probe. Mark the client NFS_CS_SESSION_INITING
318 * so that the client back channel can find the
319 * nfs_client struct
320 */
321 nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
322 return 0;
323}
324
325#endif /* CONFIG_NFS_V4_1 */
326
428360d7
BS
327/*
328 * Initialize the minor version specific parts of an NFS4 client record
329 */
330static int nfs4_init_client_minor_version(struct nfs_client *clp)
331{
abf79bb3 332 int ret;
428360d7 333
abf79bb3
CL
334 ret = clp->cl_mvops->init_client(clp);
335 if (ret)
336 return ret;
428360d7
BS
337 return nfs4_init_callback(clp);
338}
339
340/**
341 * nfs4_init_client - Initialise an NFS4 client record
342 *
343 * @clp: nfs_client to initialise
344 * @timeparms: timeout parameters for underlying RPC transport
345 * @ip_addr: callback IP address in presentation format
346 * @authflavor: authentication flavor for underlying RPC transport
347 *
348 * Returns pointer to an NFS client, or an ERR_PTR value.
349 */
350struct nfs_client *nfs4_init_client(struct nfs_client *clp,
351 const struct rpc_timeout *timeparms,
f8407299 352 const char *ip_addr)
428360d7
BS
353{
354 char buf[INET6_ADDRSTRLEN + 1];
05f4c350 355 struct nfs_client *old;
428360d7
BS
356 int error;
357
358 if (clp->cl_cons_state == NFS_CS_READY) {
359 /* the client is initialised already */
360 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
361 return clp;
362 }
363
364 /* Check NFS protocol revision and initialize RPC op vector */
365 clp->rpc_ops = &nfs_v4_clientops;
366
98f98cf5
TM
367 if (clp->cl_minorversion != 0)
368 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
428360d7 369 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
99875249 370 __set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags);
6aa23d76 371
0ea9de0e 372 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
23631227 373 if (error == -EINVAL)
83c168bf 374 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
428360d7
BS
375 if (error < 0)
376 goto error;
377
378 /* If no clientaddr= option was specified, find a usable cb address */
379 if (ip_addr == NULL) {
380 struct sockaddr_storage cb_addr;
381 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
382
383 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
384 if (error < 0)
385 goto error;
386 error = rpc_ntop(sap, buf, sizeof(buf));
387 if (error < 0)
388 goto error;
389 ip_addr = (const char *)buf;
390 }
391 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
392
393 error = nfs_idmap_new(clp);
394 if (error < 0) {
395 dprintk("%s: failed to create idmapper. Error = %d\n",
396 __func__, error);
397 goto error;
398 }
399 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
400
401 error = nfs4_init_client_minor_version(clp);
402 if (error < 0)
403 goto error;
404
405 if (!nfs4_has_session(clp))
406 nfs_mark_client_ready(clp, NFS_CS_READY);
05f4c350
CL
407
408 error = nfs4_discover_server_trunking(clp, &old);
409 if (error < 0)
410 goto error;
05f4c350 411
abad2fa5
WAA
412 if (clp != old)
413 clp->cl_preserve_clid = true;
414 nfs_put_client(clp);
415 return old;
428360d7
BS
416
417error:
418 nfs_mark_client_ready(clp, error);
419 nfs_put_client(clp);
420 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
421 return ERR_PTR(error);
422}
fcf10398 423
05f4c350
CL
424/*
425 * SETCLIENTID just did a callback update with the callback ident in
426 * "drop," but server trunking discovery claims "drop" and "keep" are
427 * actually the same server. Swap the callback IDs so that "keep"
428 * will continue to use the callback ident the server now knows about,
429 * and so that "keep"'s original callback ident is destroyed when
430 * "drop" is freed.
431 */
432static void nfs4_swap_callback_idents(struct nfs_client *keep,
433 struct nfs_client *drop)
434{
435 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
436 unsigned int save = keep->cl_cb_ident;
437
438 if (keep->cl_cb_ident == drop->cl_cb_ident)
439 return;
440
441 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
442 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
443
444 spin_lock(&nn->nfs_client_lock);
445
446 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
447 keep->cl_cb_ident = drop->cl_cb_ident;
448
449 idr_replace(&nn->cb_ident_idr, drop, save);
450 drop->cl_cb_ident = save;
451
452 spin_unlock(&nn->nfs_client_lock);
453}
454
455/**
456 * nfs40_walk_client_list - Find server that recognizes a client ID
457 *
458 * @new: nfs_client with client ID to test
459 * @result: OUT: found nfs_client, or new
460 * @cred: credential to use for trunking test
461 *
462 * Returns zero, a negative errno, or a negative NFS4ERR status.
463 * If zero is returned, an nfs_client pointer is planted in "result."
464 *
465 * NB: nfs40_walk_client_list() relies on the new nfs_client being
466 * the last nfs_client on the list.
467 */
468int nfs40_walk_client_list(struct nfs_client *new,
469 struct nfs_client **result,
470 struct rpc_cred *cred)
471{
472 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
7b1f1fd1 473 struct nfs_client *pos, *prev = NULL;
05f4c350
CL
474 struct nfs4_setclientid_res clid = {
475 .clientid = new->cl_clientid,
476 .confirm = new->cl_confirm,
477 };
4ae19c2d 478 int status = -NFS4ERR_STALE_CLIENTID;
05f4c350
CL
479
480 spin_lock(&nn->nfs_client_lock);
7b1f1fd1 481 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
080af20c
SD
482
483 if (pos->rpc_ops != new->rpc_ops)
484 continue;
485
486 if (pos->cl_proto != new->cl_proto)
487 continue;
488
489 if (pos->cl_minorversion != new->cl_minorversion)
490 continue;
491
05f4c350
CL
492 /* If "pos" isn't marked ready, we can't trust the
493 * remaining fields in "pos" */
7b1f1fd1
TM
494 if (pos->cl_cons_state > NFS_CS_READY) {
495 atomic_inc(&pos->cl_count);
496 spin_unlock(&nn->nfs_client_lock);
497
fe0bf118 498 nfs_put_client(prev);
7b1f1fd1
TM
499 prev = pos;
500
501 status = nfs_wait_client_init_complete(pos);
7b1f1fd1 502 if (status < 0)
64590daa
TM
503 goto out;
504 status = -NFS4ERR_STALE_CLIENTID;
505 spin_lock(&nn->nfs_client_lock);
7b1f1fd1
TM
506 }
507 if (pos->cl_cons_state != NFS_CS_READY)
05f4c350
CL
508 continue;
509
05f4c350
CL
510 if (pos->cl_clientid != new->cl_clientid)
511 continue;
512
513 atomic_inc(&pos->cl_count);
514 spin_unlock(&nn->nfs_client_lock);
515
fe0bf118 516 nfs_put_client(prev);
4ae19c2d 517 prev = pos;
05f4c350
CL
518
519 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
4ae19c2d
TM
520 switch (status) {
521 case -NFS4ERR_STALE_CLIENTID:
522 break;
523 case 0:
05f4c350
CL
524 nfs4_swap_callback_idents(pos, new);
525
4ae19c2d 526 prev = NULL;
05f4c350
CL
527 *result = pos;
528 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
529 __func__, pos, atomic_read(&pos->cl_count));
f9b7ebdf
TM
530 goto out;
531 case -ERESTARTSYS:
532 case -ETIMEDOUT:
533 /* The callback path may have been inadvertently
534 * changed. Schedule recovery!
535 */
536 nfs4_schedule_path_down_recovery(pos);
4ae19c2d
TM
537 default:
538 goto out;
05f4c350
CL
539 }
540
541 spin_lock(&nn->nfs_client_lock);
05f4c350 542 }
4ae19c2d 543 spin_unlock(&nn->nfs_client_lock);
05f4c350 544
202c312d 545 /* No match found. The server lost our clientid */
4ae19c2d 546out:
fe0bf118 547 nfs_put_client(prev);
4ae19c2d
TM
548 dprintk("NFS: <-- %s status = %d\n", __func__, status);
549 return status;
05f4c350
CL
550}
551
552#ifdef CONFIG_NFS_V4_1
f9d640f3
TM
553/*
554 * Returns true if the client IDs match
555 */
556static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
557{
558 if (a->cl_clientid != b->cl_clientid) {
559 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
560 __func__, a->cl_clientid, b->cl_clientid);
561 return false;
562 }
563 dprintk("NFS: --> %s client ID %llx matches %llx\n",
564 __func__, a->cl_clientid, b->cl_clientid);
565 return true;
566}
567
05f4c350 568/*
1fc0703a 569 * Returns true if the server major ids match
05f4c350
CL
570 */
571static bool
1fc0703a 572nfs4_check_clientid_trunking(struct nfs_client *a, struct nfs_client *b)
05f4c350
CL
573{
574 struct nfs41_server_owner *o1 = a->cl_serverowner;
575 struct nfs41_server_owner *o2 = b->cl_serverowner;
576
05f4c350
CL
577 if (o1->major_id_sz != o2->major_id_sz)
578 goto out_major_mismatch;
579 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
580 goto out_major_mismatch;
581
582 dprintk("NFS: --> %s server owners match\n", __func__);
583 return true;
584
585out_major_mismatch:
586 dprintk("NFS: --> %s server owner major IDs do not match\n",
587 __func__);
588 return false;
589}
590
591/**
592 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
593 *
594 * @new: nfs_client with client ID to test
595 * @result: OUT: found nfs_client, or new
596 * @cred: credential to use for trunking test
597 *
598 * Returns zero, a negative errno, or a negative NFS4ERR status.
599 * If zero is returned, an nfs_client pointer is planted in "result."
600 *
601 * NB: nfs41_walk_client_list() relies on the new nfs_client being
602 * the last nfs_client on the list.
603 */
604int nfs41_walk_client_list(struct nfs_client *new,
605 struct nfs_client **result,
606 struct rpc_cred *cred)
607{
608 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
7b1f1fd1 609 struct nfs_client *pos, *prev = NULL;
202c312d 610 int status = -NFS4ERR_STALE_CLIENTID;
05f4c350
CL
611
612 spin_lock(&nn->nfs_client_lock);
7b1f1fd1 613 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
080af20c
SD
614
615 if (pos->rpc_ops != new->rpc_ops)
616 continue;
617
618 if (pos->cl_proto != new->cl_proto)
619 continue;
620
621 if (pos->cl_minorversion != new->cl_minorversion)
622 continue;
623
05f4c350
CL
624 /* If "pos" isn't marked ready, we can't trust the
625 * remaining fields in "pos", especially the client
626 * ID and serverowner fields. Wait for CREATE_SESSION
627 * to finish. */
7b1f1fd1 628 if (pos->cl_cons_state > NFS_CS_READY) {
05f4c350
CL
629 atomic_inc(&pos->cl_count);
630 spin_unlock(&nn->nfs_client_lock);
631
fe0bf118 632 nfs_put_client(prev);
05f4c350
CL
633 prev = pos;
634
202c312d 635 status = nfs_wait_client_init_complete(pos);
7b1f1fd1
TM
636 if (status == 0) {
637 nfs4_schedule_lease_recovery(pos);
638 status = nfs4_wait_clnt_recover(pos);
05f4c350 639 }
05f4c350 640 spin_lock(&nn->nfs_client_lock);
65436ec0 641 if (status < 0)
64590daa
TM
642 break;
643 status = -NFS4ERR_STALE_CLIENTID;
05f4c350 644 }
7b1f1fd1
TM
645 if (pos->cl_cons_state != NFS_CS_READY)
646 continue;
05f4c350 647
05f4c350
CL
648 if (!nfs4_match_clientids(pos, new))
649 continue;
650
1fc0703a
TM
651 /*
652 * Note that session trunking is just a special subcase of
653 * client id trunking. In either case, we want to fall back
654 * to using the existing nfs_client.
655 */
656 if (!nfs4_check_clientid_trunking(pos, new))
05f4c350
CL
657 continue;
658
4ae19c2d 659 atomic_inc(&pos->cl_count);
7b1f1fd1 660 *result = pos;
eb04e0ac 661 status = 0;
05f4c350
CL
662 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
663 __func__, pos, atomic_read(&pos->cl_count));
7b1f1fd1 664 break;
05f4c350
CL
665 }
666
202c312d 667 /* No matching nfs_client found. */
05f4c350 668 spin_unlock(&nn->nfs_client_lock);
202c312d 669 dprintk("NFS: <-- %s status = %d\n", __func__, status);
fe0bf118 670 nfs_put_client(prev);
202c312d 671 return status;
05f4c350
CL
672}
673#endif /* CONFIG_NFS_V4_1 */
674
fcf10398
BS
675static void nfs4_destroy_server(struct nfs_server *server)
676{
677 nfs_server_return_all_delegations(server);
678 unset_pnfs_layoutdriver(server);
679 nfs4_purge_state_owners(server);
680}
681
682/*
683 * NFSv4.0 callback thread helper
684 *
685 * Find a client by callback identifier
686 */
687struct nfs_client *
688nfs4_find_client_ident(struct net *net, int cb_ident)
689{
690 struct nfs_client *clp;
691 struct nfs_net *nn = net_generic(net, nfs_net_id);
692
693 spin_lock(&nn->nfs_client_lock);
694 clp = idr_find(&nn->cb_ident_idr, cb_ident);
695 if (clp)
696 atomic_inc(&clp->cl_count);
697 spin_unlock(&nn->nfs_client_lock);
698 return clp;
699}
700
701#if defined(CONFIG_NFS_V4_1)
702/* Common match routine for v4.0 and v4.1 callback services */
703static bool nfs4_cb_match_client(const struct sockaddr *addr,
704 struct nfs_client *clp, u32 minorversion)
705{
706 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
707
708 /* Don't match clients that failed to initialise */
709 if (!(clp->cl_cons_state == NFS_CS_READY ||
710 clp->cl_cons_state == NFS_CS_SESSION_INITING))
711 return false;
712
713 smp_rmb();
714
715 /* Match the version and minorversion */
716 if (clp->rpc_ops->version != 4 ||
717 clp->cl_minorversion != minorversion)
718 return false;
719
720 /* Match only the IP address, not the port number */
721 if (!nfs_sockaddr_match_ipaddr(addr, clap))
722 return false;
723
724 return true;
725}
726
727/*
728 * NFSv4.1 callback thread helper
729 * For CB_COMPOUND calls, find a client by IP address, protocol version,
730 * minorversion, and sessionID
731 *
732 * Returns NULL if no such client
733 */
734struct nfs_client *
735nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
459de2ed 736 struct nfs4_sessionid *sid, u32 minorversion)
fcf10398
BS
737{
738 struct nfs_client *clp;
739 struct nfs_net *nn = net_generic(net, nfs_net_id);
740
741 spin_lock(&nn->nfs_client_lock);
742 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
459de2ed 743 if (nfs4_cb_match_client(addr, clp, minorversion) == false)
fcf10398
BS
744 continue;
745
746 if (!nfs4_has_session(clp))
747 continue;
748
749 /* Match sessionid*/
750 if (memcmp(clp->cl_session->sess_id.data,
751 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
752 continue;
753
754 atomic_inc(&clp->cl_count);
755 spin_unlock(&nn->nfs_client_lock);
756 return clp;
757 }
758 spin_unlock(&nn->nfs_client_lock);
759 return NULL;
760}
761
762#else /* CONFIG_NFS_V4_1 */
763
764struct nfs_client *
765nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
459de2ed 766 struct nfs4_sessionid *sid, u32 minorversion)
fcf10398
BS
767{
768 return NULL;
769}
770#endif /* CONFIG_NFS_V4_1 */
771
772/*
773 * Set up an NFS4 client
774 */
775static int nfs4_set_client(struct nfs_server *server,
776 const char *hostname,
777 const struct sockaddr *addr,
778 const size_t addrlen,
779 const char *ip_addr,
780 rpc_authflavor_t authflavour,
781 int proto, const struct rpc_timeout *timeparms,
782 u32 minorversion, struct net *net)
783{
784 struct nfs_client_initdata cl_init = {
785 .hostname = hostname,
786 .addr = addr,
787 .addrlen = addrlen,
ab7017a3 788 .nfs_mod = &nfs_v4,
fcf10398
BS
789 .proto = proto,
790 .minorversion = minorversion,
791 .net = net,
792 };
793 struct nfs_client *clp;
794 int error;
795
796 dprintk("--> nfs4_set_client()\n");
797
798 if (server->flags & NFS_MOUNT_NORESVPORT)
799 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
f112bb48
CL
800 if (server->options & NFS_OPTION_MIGRATION)
801 set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
fcf10398
BS
802
803 /* Allocate or find a client reference we can use */
804 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
805 if (IS_ERR(clp)) {
806 error = PTR_ERR(clp);
807 goto error;
808 }
809
810 /*
811 * Query for the lease time on clientid setup or renewal
812 *
813 * Note that this will be set on nfs_clients that were created
814 * only for the DS role and did not set this bit, but now will
815 * serve a dual role.
816 */
817 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
818
819 server->nfs_client = clp;
820 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
821 return 0;
822error:
823 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
824 return error;
825}
826
827/*
828 * Set up a pNFS Data Server client.
829 *
830 * Return any existing nfs_client that matches server address,port,version
831 * and minorversion.
832 *
833 * For a new nfs_client, use a soft mount (default), a low retrans and a
834 * low timeout interval so that if a connection is lost, we retry through
835 * the MDS.
836 */
837struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
838 const struct sockaddr *ds_addr, int ds_addrlen,
839 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
840{
841 struct nfs_client_initdata cl_init = {
842 .addr = ds_addr,
843 .addrlen = ds_addrlen,
ab7017a3 844 .nfs_mod = &nfs_v4,
fcf10398
BS
845 .proto = ds_proto,
846 .minorversion = mds_clp->cl_minorversion,
847 .net = mds_clp->cl_net,
848 };
849 struct rpc_timeout ds_timeout;
850 struct nfs_client *clp;
a363e32e
PT
851 char buf[INET6_ADDRSTRLEN + 1];
852
853 if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0)
854 return ERR_PTR(-EINVAL);
855 cl_init.hostname = buf;
fcf10398
BS
856
857 /*
858 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
859 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
860 * (section 13.1 RFC 5661).
861 */
862 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
863 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
864 mds_clp->cl_rpcclient->cl_auth->au_flavor);
865
866 dprintk("<-- %s %p\n", __func__, clp);
867 return clp;
868}
869EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
870
871/*
872 * Session has been established, and the client marked ready.
873 * Set the mount rsize and wsize with negotiated fore channel
874 * attributes which will be bound checked in nfs_server_set_fsinfo.
875 */
876static void nfs4_session_set_rwsize(struct nfs_server *server)
877{
878#ifdef CONFIG_NFS_V4_1
879 struct nfs4_session *sess;
880 u32 server_resp_sz;
881 u32 server_rqst_sz;
882
883 if (!nfs4_has_session(server->nfs_client))
884 return;
885 sess = server->nfs_client->cl_session;
886 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
887 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
888
889 if (server->rsize > server_resp_sz)
890 server->rsize = server_resp_sz;
891 if (server->wsize > server_rqst_sz)
892 server->wsize = server_rqst_sz;
893#endif /* CONFIG_NFS_V4_1 */
894}
895
896static int nfs4_server_common_setup(struct nfs_server *server,
5e6b1990 897 struct nfs_fh *mntfh, bool auth_probe)
fcf10398
BS
898{
899 struct nfs_fattr *fattr;
900 int error;
901
fcf10398
BS
902 /* data servers support only a subset of NFSv4.1 */
903 if (is_ds_only_client(server->nfs_client))
904 return -EPROTONOSUPPORT;
905
906 fattr = nfs_alloc_fattr();
907 if (fattr == NULL)
908 return -ENOMEM;
909
910 /* We must ensure the session is initialised first */
18aad3d5 911 error = nfs4_init_session(server->nfs_client);
fcf10398
BS
912 if (error < 0)
913 goto out;
914
39c6daae
TM
915 /* Set the basic capabilities */
916 server->caps |= server->nfs_client->cl_mvops->init_caps;
917 if (server->flags & NFS_MOUNT_NORDIRPLUS)
918 server->caps &= ~NFS_CAP_READDIRPLUS;
919 /*
920 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
921 * authentication.
922 */
923 if (nfs4_disable_idmapping &&
924 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
925 server->caps |= NFS_CAP_UIDGID_NOMAP;
926
927
fcf10398 928 /* Probe the root fh to retrieve its FSID and filehandle */
5e6b1990 929 error = nfs4_get_rootfh(server, mntfh, auth_probe);
fcf10398
BS
930 if (error < 0)
931 goto out;
932
933 dprintk("Server FSID: %llx:%llx\n",
934 (unsigned long long) server->fsid.major,
935 (unsigned long long) server->fsid.minor);
9e6ee76d 936 nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
fcf10398
BS
937
938 nfs4_session_set_rwsize(server);
939
940 error = nfs_probe_fsinfo(server, mntfh, fattr);
941 if (error < 0)
942 goto out;
943
944 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
945 server->namelen = NFS4_MAXNAMLEN;
946
947 nfs_server_insert_lists(server);
948 server->mount_time = jiffies;
949 server->destroy = nfs4_destroy_server;
950out:
951 nfs_free_fattr(fattr);
952 return error;
953}
954
955/*
956 * Create a version 4 volume record
957 */
958static int nfs4_init_server(struct nfs_server *server,
a3f73c27 959 struct nfs_parsed_mount_data *data)
fcf10398
BS
960{
961 struct rpc_timeout timeparms;
962 int error;
963
964 dprintk("--> nfs4_init_server()\n");
965
966 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
967 data->timeo, data->retrans);
968
969 /* Initialise the client representation from the mount data */
970 server->flags = data->flags;
fcf10398 971 server->options = data->options;
0f5f49b8 972 server->auth_info = data->auth_info;
fcf10398 973
4d4b69dd
WAA
974 /* Use the first specified auth flavor. If this flavor isn't
975 * allowed by the server, use the SECINFO path to try the
976 * other specified flavors */
a3f73c27
WAA
977 if (data->auth_info.flavor_len >= 1)
978 data->selected_flavor = data->auth_info.flavors[0];
979 else
980 data->selected_flavor = RPC_AUTH_UNIX;
5e6b1990 981
fcf10398
BS
982 /* Get a client record */
983 error = nfs4_set_client(server,
984 data->nfs_server.hostname,
985 (const struct sockaddr *)&data->nfs_server.address,
986 data->nfs_server.addrlen,
987 data->client_address,
a3f73c27 988 data->selected_flavor,
fcf10398
BS
989 data->nfs_server.protocol,
990 &timeparms,
991 data->minorversion,
992 data->net);
993 if (error < 0)
994 goto error;
995
fcf10398
BS
996 if (data->rsize)
997 server->rsize = nfs_block_size(data->rsize, NULL);
998 if (data->wsize)
999 server->wsize = nfs_block_size(data->wsize, NULL);
1000
1001 server->acregmin = data->acregmin * HZ;
1002 server->acregmax = data->acregmax * HZ;
1003 server->acdirmin = data->acdirmin * HZ;
1004 server->acdirmax = data->acdirmax * HZ;
1005
1006 server->port = data->nfs_server.port;
1007
a3f73c27
WAA
1008 error = nfs_init_server_rpcclient(server, &timeparms,
1009 data->selected_flavor);
fcf10398
BS
1010
1011error:
1012 /* Done */
1013 dprintk("<-- nfs4_init_server() = %d\n", error);
1014 return error;
1015}
1016
1017/*
1018 * Create a version 4 volume record
1019 * - keyed on server and FSID
1020 */
1179acc6
BS
1021/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1022 struct nfs_fh *mntfh)*/
1023struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
1024 struct nfs_subversion *nfs_mod)
fcf10398
BS
1025{
1026 struct nfs_server *server;
5e6b1990 1027 bool auth_probe;
fcf10398
BS
1028 int error;
1029
1030 dprintk("--> nfs4_create_server()\n");
1031
1032 server = nfs_alloc_server();
1033 if (!server)
1034 return ERR_PTR(-ENOMEM);
1035
a3f73c27 1036 auth_probe = mount_info->parsed->auth_info.flavor_len < 1;
5e6b1990 1037
fcf10398 1038 /* set up the general RPC client */
1179acc6 1039 error = nfs4_init_server(server, mount_info->parsed);
fcf10398
BS
1040 if (error < 0)
1041 goto error;
1042
5e6b1990 1043 error = nfs4_server_common_setup(server, mount_info->mntfh, auth_probe);
fcf10398
BS
1044 if (error < 0)
1045 goto error;
1046
1047 dprintk("<-- nfs4_create_server() = %p\n", server);
1048 return server;
1049
1050error:
1051 nfs_free_server(server);
1052 dprintk("<-- nfs4_create_server() = error %d\n", error);
1053 return ERR_PTR(error);
1054}
1055
1056/*
1057 * Create an NFS4 referral server record
1058 */
1059struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1060 struct nfs_fh *mntfh)
1061{
1062 struct nfs_client *parent_client;
1063 struct nfs_server *server, *parent_server;
5837f6df 1064 bool auth_probe;
fcf10398
BS
1065 int error;
1066
1067 dprintk("--> nfs4_create_referral_server()\n");
1068
1069 server = nfs_alloc_server();
1070 if (!server)
1071 return ERR_PTR(-ENOMEM);
1072
1073 parent_server = NFS_SB(data->sb);
1074 parent_client = parent_server->nfs_client;
1075
1076 /* Initialise the client representation from the parent server */
1077 nfs_server_copy_userdata(server, parent_server);
fcf10398
BS
1078
1079 /* Get a client representation.
1080 * Note: NFSv4 always uses TCP, */
1081 error = nfs4_set_client(server, data->hostname,
1082 data->addr,
1083 data->addrlen,
1084 parent_client->cl_ipaddr,
1085 data->authflavor,
1086 rpc_protocol(parent_server->client),
1087 parent_server->client->cl_timeout,
1088 parent_client->cl_mvops->minor_version,
1089 parent_client->cl_net);
1090 if (error < 0)
1091 goto error;
1092
1093 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1094 if (error < 0)
1095 goto error;
1096
5837f6df
WAA
1097 auth_probe = parent_server->auth_info.flavor_len < 1;
1098
1099 error = nfs4_server_common_setup(server, mntfh, auth_probe);
fcf10398
BS
1100 if (error < 0)
1101 goto error;
1102
1103 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1104 return server;
1105
1106error:
1107 nfs_free_server(server);
1108 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1109 return ERR_PTR(error);
1110}
32e62b7c
CL
1111
1112/*
1113 * Grab the destination's particulars, including lease expiry time.
1114 *
1115 * Returns zero if probe succeeded and retrieved FSID matches the FSID
1116 * we have cached.
1117 */
1118static int nfs_probe_destination(struct nfs_server *server)
1119{
1120 struct inode *inode = server->super->s_root->d_inode;
1121 struct nfs_fattr *fattr;
1122 int error;
1123
1124 fattr = nfs_alloc_fattr();
1125 if (fattr == NULL)
1126 return -ENOMEM;
1127
1128 /* Sanity: the probe won't work if the destination server
1129 * does not recognize the migrated FH. */
1130 error = nfs_probe_fsinfo(server, NFS_FH(inode), fattr);
1131
1132 nfs_free_fattr(fattr);
1133 return error;
1134}
1135
1136/**
1137 * nfs4_update_server - Move an nfs_server to a different nfs_client
1138 *
1139 * @server: represents FSID to be moved
1140 * @hostname: new end-point's hostname
1141 * @sap: new end-point's socket address
1142 * @salen: size of "sap"
292f503c 1143 * @net: net namespace
32e62b7c
CL
1144 *
1145 * The nfs_server must be quiescent before this function is invoked.
1146 * Either its session is drained (NFSv4.1+), or its transport is
1147 * plugged and drained (NFSv4.0).
1148 *
1149 * Returns zero on success, or a negative errno value.
1150 */
1151int nfs4_update_server(struct nfs_server *server, const char *hostname,
292f503c 1152 struct sockaddr *sap, size_t salen, struct net *net)
32e62b7c
CL
1153{
1154 struct nfs_client *clp = server->nfs_client;
1155 struct rpc_clnt *clnt = server->client;
1156 struct xprt_create xargs = {
1157 .ident = clp->cl_proto,
292f503c 1158 .net = net,
32e62b7c
CL
1159 .dstaddr = sap,
1160 .addrlen = salen,
1161 .servername = hostname,
1162 };
1163 char buf[INET6_ADDRSTRLEN + 1];
1164 struct sockaddr_storage address;
1165 struct sockaddr *localaddr = (struct sockaddr *)&address;
1166 int error;
1167
1168 dprintk("--> %s: move FSID %llx:%llx to \"%s\")\n", __func__,
1169 (unsigned long long)server->fsid.major,
1170 (unsigned long long)server->fsid.minor,
1171 hostname);
1172
1173 error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
1174 if (error != 0) {
1175 dprintk("<-- %s(): rpc_switch_client_transport returned %d\n",
1176 __func__, error);
1177 goto out;
1178 }
1179
1180 error = rpc_localaddr(clnt, localaddr, sizeof(address));
1181 if (error != 0) {
1182 dprintk("<-- %s(): rpc_localaddr returned %d\n",
1183 __func__, error);
1184 goto out;
1185 }
1186
1187 error = -EAFNOSUPPORT;
1188 if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0) {
1189 dprintk("<-- %s(): rpc_ntop returned %d\n",
1190 __func__, error);
1191 goto out;
1192 }
1193
1194 nfs_server_remove_lists(server);
1195 error = nfs4_set_client(server, hostname, sap, salen, buf,
1196 clp->cl_rpcclient->cl_auth->au_flavor,
1197 clp->cl_proto, clnt->cl_timeout,
292f503c 1198 clp->cl_minorversion, net);
32e62b7c
CL
1199 nfs_put_client(clp);
1200 if (error != 0) {
1201 nfs_server_insert_lists(server);
1202 dprintk("<-- %s(): nfs4_set_client returned %d\n",
1203 __func__, error);
1204 goto out;
1205 }
1206
1207 if (server->nfs_client->cl_hostname == NULL)
1208 server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
1209 nfs_server_insert_lists(server);
1210
1211 error = nfs_probe_destination(server);
1212 if (error < 0)
1213 goto out;
1214
1215 dprintk("<-- %s() succeeded\n", __func__);
1216
1217out:
1218 return error;
1219}