blob: 511cdce6ecf2f067b4effb1026cf1d236cbb0ae9 [file] [log] [blame]
Bryan Schumaker428360d2012-07-16 16:39:17 -04001/*
2 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
3 * Written by David Howells (dhowells@redhat.com)
4 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -04005#include <linux/module.h>
Bryan Schumaker428360d2012-07-16 16:39:17 -04006#include <linux/nfs_fs.h>
7#include <linux/nfs_idmap.h>
Bryan Schumakerfcf10392012-07-16 16:39:18 -04008#include <linux/nfs_mount.h>
Jeff Layton59766872013-02-04 12:50:00 -05009#include <linux/sunrpc/addr.h>
Bryan Schumaker428360d2012-07-16 16:39:17 -040010#include <linux/sunrpc/auth.h>
11#include <linux/sunrpc/xprt.h>
12#include <linux/sunrpc/bc_xprt.h>
13#include "internal.h"
14#include "callback.h"
Bryan Schumakerfcf10392012-07-16 16:39:18 -040015#include "delegation.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050016#include "nfs4session.h"
Bryan Schumakerfcf10392012-07-16 16:39:18 -040017#include "pnfs.h"
18#include "netns.h"
Bryan Schumaker428360d2012-07-16 16:39:17 -040019
20#define NFSDBG_FACILITY NFSDBG_CLIENT
21
22/*
Bryan Schumakerec409892012-07-16 16:39:21 -040023 * Get a unique NFSv4.0 callback identifier which will be used
24 * by the V4.0 callback service to lookup the nfs_client struct
25 */
26static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
27{
28 int ret = 0;
29 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
30
31 if (clp->rpc_ops->version != 4 || minorversion != 0)
32 return ret;
Tejun Heod6870312013-02-27 17:05:01 -080033 idr_preload(GFP_KERNEL);
Bryan Schumakerec409892012-07-16 16:39:21 -040034 spin_lock(&nn->nfs_client_lock);
Tejun Heod6870312013-02-27 17:05:01 -080035 ret = idr_alloc(&nn->cb_ident_idr, clp, 0, 0, GFP_NOWAIT);
36 if (ret >= 0)
37 clp->cl_cb_ident = ret;
Bryan Schumakerec409892012-07-16 16:39:21 -040038 spin_unlock(&nn->nfs_client_lock);
Tejun Heod6870312013-02-27 17:05:01 -080039 idr_preload_end();
40 return ret < 0 ? ret : 0;
Bryan Schumakerec409892012-07-16 16:39:21 -040041}
42
43#ifdef CONFIG_NFS_V4_1
Andy Adamson0e201622013-09-06 14:14:00 -040044/**
45 * Per auth flavor data server rpc clients
46 */
47struct nfs4_ds_server {
48 struct list_head list; /* ds_clp->cl_ds_clients */
49 struct rpc_clnt *rpc_clnt;
50};
51
52/**
53 * Common lookup case for DS I/O
54 */
55static struct nfs4_ds_server *
56nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
57{
58 struct nfs4_ds_server *dss;
59
60 rcu_read_lock();
61 list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) {
62 if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
63 continue;
64 goto out;
65 }
66 dss = NULL;
67out:
68 rcu_read_unlock();
69 return dss;
70}
71
72static struct nfs4_ds_server *
73nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor,
74 struct nfs4_ds_server *new)
75{
76 struct nfs4_ds_server *dss;
77
78 spin_lock(&ds_clp->cl_lock);
79 list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) {
80 if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
81 continue;
82 goto out;
83 }
84 if (new)
85 list_add_rcu(&new->list, &ds_clp->cl_ds_clients);
86 dss = new;
87out:
88 spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */
89 return dss;
90}
91
92static struct nfs4_ds_server *
93nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
94{
95 struct nfs4_ds_server *dss;
96
97 dss = kmalloc(sizeof(*dss), GFP_NOFS);
98 if (dss == NULL)
99 return ERR_PTR(-ENOMEM);
100
101 dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor);
102 if (IS_ERR(dss->rpc_clnt)) {
103 int err = PTR_ERR(dss->rpc_clnt);
104 kfree (dss);
105 return ERR_PTR(err);
106 }
107 INIT_LIST_HEAD(&dss->list);
108
109 return dss;
110}
111
112static void
113nfs4_free_ds_server(struct nfs4_ds_server *dss)
114{
115 rpc_release_client(dss->rpc_clnt);
116 kfree(dss);
117}
118
119/**
120* Find or create a DS rpc client with th MDS server rpc client auth flavor
121* in the nfs_client cl_ds_clients list.
122*/
123struct rpc_clnt *
124nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode)
125{
126 struct nfs4_ds_server *dss, *new;
127 rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor;
128
129 dss = nfs4_find_ds_client(ds_clp, flavor);
130 if (dss != NULL)
131 goto out;
132 new = nfs4_alloc_ds_server(ds_clp, flavor);
133 if (IS_ERR(new))
134 return ERR_CAST(new);
135 dss = nfs4_add_ds_client(ds_clp, flavor, new);
136 if (dss != new)
137 nfs4_free_ds_server(new);
138out:
139 return dss->rpc_clnt;
140}
141EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client);
142
143static void
144nfs4_shutdown_ds_clients(struct nfs_client *clp)
145{
146 struct nfs4_ds_server *dss;
147 LIST_HEAD(shutdown_list);
148
149 while (!list_empty(&clp->cl_ds_clients)) {
150 dss = list_entry(clp->cl_ds_clients.next,
151 struct nfs4_ds_server, list);
152 list_del(&dss->list);
153 rpc_shutdown_client(dss->rpc_clnt);
154 kfree (dss);
155 }
156}
157
Chuck Leverabf79bb2013-08-09 12:49:11 -0400158void nfs41_shutdown_client(struct nfs_client *clp)
Bryan Schumakerec409892012-07-16 16:39:21 -0400159{
160 if (nfs4_has_session(clp)) {
Andy Adamson0e201622013-09-06 14:14:00 -0400161 nfs4_shutdown_ds_clients(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -0400162 nfs4_destroy_session(clp->cl_session);
163 nfs4_destroy_clientid(clp);
164 }
165
166}
Chuck Leverabf79bb2013-08-09 12:49:11 -0400167#endif /* CONFIG_NFS_V4_1 */
168
169void nfs40_shutdown_client(struct nfs_client *clp)
Bryan Schumakerec409892012-07-16 16:39:21 -0400170{
Chuck Leverabf79bb2013-08-09 12:49:11 -0400171 if (clp->cl_slot_tbl) {
172 nfs4_release_slot_table(clp->cl_slot_tbl);
173 kfree(clp->cl_slot_tbl);
174 }
Bryan Schumakerec409892012-07-16 16:39:21 -0400175}
Bryan Schumakerec409892012-07-16 16:39:21 -0400176
177struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
178{
179 int err;
180 struct nfs_client *clp = nfs_alloc_client(cl_init);
181 if (IS_ERR(clp))
182 return clp;
183
184 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
185 if (err)
186 goto error;
187
Steve Dickson42c2c422013-05-22 12:50:38 -0400188 if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
189 err = -EINVAL;
190 goto error;
191 }
192
Bryan Schumakerec409892012-07-16 16:39:21 -0400193 spin_lock_init(&clp->cl_lock);
194 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
Andy Adamson0e201622013-09-06 14:14:00 -0400195 INIT_LIST_HEAD(&clp->cl_ds_clients);
Bryan Schumakerec409892012-07-16 16:39:21 -0400196 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
197 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
198 clp->cl_minorversion = cl_init->minorversion;
199 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
200 return clp;
201
202error:
Trond Myklebust7653f6f2012-08-20 12:12:29 -0400203 nfs_free_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -0400204 return ERR_PTR(err);
205}
206
207/*
208 * Destroy the NFS4 callback service
209 */
210static void nfs4_destroy_callback(struct nfs_client *clp)
211{
212 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +0400213 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
Bryan Schumakerec409892012-07-16 16:39:21 -0400214}
215
216static void nfs4_shutdown_client(struct nfs_client *clp)
217{
218 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
219 nfs4_kill_renewd(clp);
Chuck Leverabf79bb2013-08-09 12:49:11 -0400220 clp->cl_mvops->shutdown_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -0400221 nfs4_destroy_callback(clp);
222 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
223 nfs_idmap_delete(clp);
224
225 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
226 kfree(clp->cl_serverowner);
227 kfree(clp->cl_serverscope);
228 kfree(clp->cl_implid);
229}
230
231void nfs4_free_client(struct nfs_client *clp)
232{
233 nfs4_shutdown_client(clp);
234 nfs_free_client(clp);
235}
236
237/*
Bryan Schumaker428360d2012-07-16 16:39:17 -0400238 * Initialize the NFS4 callback service
239 */
240static int nfs4_init_callback(struct nfs_client *clp)
241{
242 int error;
243
244 if (clp->rpc_ops->version == 4) {
245 struct rpc_xprt *xprt;
246
247 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
248
249 if (nfs4_has_session(clp)) {
250 error = xprt_setup_backchannel(xprt,
251 NFS41_BC_MIN_CALLBACKS);
252 if (error < 0)
253 return error;
254 }
255
256 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
257 if (error < 0) {
258 dprintk("%s: failed to start callback. Error = %d\n",
259 __func__, error);
260 return error;
261 }
262 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
263 }
264 return 0;
265}
266
Chuck Leverabf79bb2013-08-09 12:49:11 -0400267/**
268 * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
269 * @clp - nfs_client to initialize
270 *
271 * Returns zero on success, or a negative errno if some error occurred.
272 */
273int nfs40_init_client(struct nfs_client *clp)
274{
275 struct nfs4_slot_table *tbl;
276 int ret;
277
278 tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
279 if (tbl == NULL)
280 return -ENOMEM;
281
282 ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
283 "NFSv4.0 transport Slot table");
284 if (ret) {
285 kfree(tbl);
286 return ret;
287 }
288
289 clp->cl_slot_tbl = tbl;
290 return 0;
291}
292
293#if defined(CONFIG_NFS_V4_1)
294
295/**
296 * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
297 * @clp - nfs_client to initialize
298 *
299 * Returns zero on success, or a negative errno if some error occurred.
300 */
301int nfs41_init_client(struct nfs_client *clp)
302{
303 struct nfs4_session *session = NULL;
304
305 /*
306 * Create the session and mark it expired.
307 * When a SEQUENCE operation encounters the expired session
308 * it will do session recovery to initialize it.
309 */
310 session = nfs4_alloc_session(clp);
311 if (!session)
312 return -ENOMEM;
313
314 clp->cl_session = session;
315
316 /*
317 * The create session reply races with the server back
318 * channel probe. Mark the client NFS_CS_SESSION_INITING
319 * so that the client back channel can find the
320 * nfs_client struct
321 */
322 nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
323 return 0;
324}
325
326#endif /* CONFIG_NFS_V4_1 */
327
Bryan Schumaker428360d2012-07-16 16:39:17 -0400328/*
329 * Initialize the minor version specific parts of an NFS4 client record
330 */
331static int nfs4_init_client_minor_version(struct nfs_client *clp)
332{
Chuck Leverabf79bb2013-08-09 12:49:11 -0400333 int ret;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400334
Chuck Leverabf79bb2013-08-09 12:49:11 -0400335 ret = clp->cl_mvops->init_client(clp);
336 if (ret)
337 return ret;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400338 return nfs4_init_callback(clp);
339}
340
341/**
342 * nfs4_init_client - Initialise an NFS4 client record
343 *
344 * @clp: nfs_client to initialise
345 * @timeparms: timeout parameters for underlying RPC transport
346 * @ip_addr: callback IP address in presentation format
347 * @authflavor: authentication flavor for underlying RPC transport
348 *
349 * Returns pointer to an NFS client, or an ERR_PTR value.
350 */
351struct nfs_client *nfs4_init_client(struct nfs_client *clp,
352 const struct rpc_timeout *timeparms,
Andy Adamsonf8407292013-07-24 11:59:49 -0400353 const char *ip_addr)
Bryan Schumaker428360d2012-07-16 16:39:17 -0400354{
355 char buf[INET6_ADDRSTRLEN + 1];
Chuck Lever05f4c352012-09-14 17:24:32 -0400356 struct nfs_client *old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400357 int error;
358
359 if (clp->cl_cons_state == NFS_CS_READY) {
360 /* the client is initialised already */
361 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
362 return clp;
363 }
364
365 /* Check NFS protocol revision and initialize RPC op vector */
366 clp->rpc_ops = &nfs_v4_clientops;
367
Trond Myklebust98f98cf2013-04-14 11:49:51 -0400368 if (clp->cl_minorversion != 0)
369 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400370 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
Trond Myklebust99875242013-09-24 12:06:07 -0400371 __set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags);
Chuck Lever4edaa302013-03-16 15:56:20 -0400372 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
Trond Myklebust23631222013-04-04 16:14:11 -0400373 if (error == -EINVAL)
Chuck Lever83c168b2013-05-15 22:00:10 -0400374 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400375 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);
Chuck Lever05f4c352012-09-14 17:24:32 -0400407
408 error = nfs4_discover_server_trunking(clp, &old);
409 if (error < 0)
410 goto error;
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500411 nfs_put_client(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400412 if (clp != old) {
413 clp->cl_preserve_clid = true;
Chuck Lever05f4c352012-09-14 17:24:32 -0400414 clp = old;
Chuck Lever05f4c352012-09-14 17:24:32 -0400415 }
416
Bryan Schumaker428360d2012-07-16 16:39:17 -0400417 return clp;
418
419error:
420 nfs_mark_client_ready(clp, error);
421 nfs_put_client(clp);
422 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
423 return ERR_PTR(error);
424}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400425
Chuck Lever05f4c352012-09-14 17:24:32 -0400426/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400427 * SETCLIENTID just did a callback update with the callback ident in
428 * "drop," but server trunking discovery claims "drop" and "keep" are
429 * actually the same server. Swap the callback IDs so that "keep"
430 * will continue to use the callback ident the server now knows about,
431 * and so that "keep"'s original callback ident is destroyed when
432 * "drop" is freed.
433 */
434static void nfs4_swap_callback_idents(struct nfs_client *keep,
435 struct nfs_client *drop)
436{
437 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
438 unsigned int save = keep->cl_cb_ident;
439
440 if (keep->cl_cb_ident == drop->cl_cb_ident)
441 return;
442
443 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
444 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
445
446 spin_lock(&nn->nfs_client_lock);
447
448 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
449 keep->cl_cb_ident = drop->cl_cb_ident;
450
451 idr_replace(&nn->cb_ident_idr, drop, save);
452 drop->cl_cb_ident = save;
453
454 spin_unlock(&nn->nfs_client_lock);
455}
456
457/**
458 * nfs40_walk_client_list - Find server that recognizes a client ID
459 *
460 * @new: nfs_client with client ID to test
461 * @result: OUT: found nfs_client, or new
462 * @cred: credential to use for trunking test
463 *
464 * Returns zero, a negative errno, or a negative NFS4ERR status.
465 * If zero is returned, an nfs_client pointer is planted in "result."
466 *
467 * NB: nfs40_walk_client_list() relies on the new nfs_client being
468 * the last nfs_client on the list.
469 */
470int nfs40_walk_client_list(struct nfs_client *new,
471 struct nfs_client **result,
472 struct rpc_cred *cred)
473{
474 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400475 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400476 struct nfs4_setclientid_res clid = {
477 .clientid = new->cl_clientid,
478 .confirm = new->cl_confirm,
479 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500480 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400481
482 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400483 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400484 /* If "pos" isn't marked ready, we can't trust the
485 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400486 if (pos->cl_cons_state > NFS_CS_READY) {
487 atomic_inc(&pos->cl_count);
488 spin_unlock(&nn->nfs_client_lock);
489
490 if (prev)
491 nfs_put_client(prev);
492 prev = pos;
493
494 status = nfs_wait_client_init_complete(pos);
495 spin_lock(&nn->nfs_client_lock);
496 if (status < 0)
497 continue;
498 }
499 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400500 continue;
501
502 if (pos->rpc_ops != new->rpc_ops)
503 continue;
504
505 if (pos->cl_proto != new->cl_proto)
506 continue;
507
508 if (pos->cl_minorversion != new->cl_minorversion)
509 continue;
510
511 if (pos->cl_clientid != new->cl_clientid)
512 continue;
513
514 atomic_inc(&pos->cl_count);
515 spin_unlock(&nn->nfs_client_lock);
516
517 if (prev)
518 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500519 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400520
521 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500522 switch (status) {
523 case -NFS4ERR_STALE_CLIENTID:
524 break;
525 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400526 nfs4_swap_callback_idents(pos, new);
527
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500528 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400529 *result = pos;
530 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
531 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500532 default:
533 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400534 }
535
536 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400537 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500538 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400539
Trond Myklebust202c3122013-01-18 22:56:23 -0500540 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500541out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400542 if (prev)
543 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500544 dprintk("NFS: <-- %s status = %d\n", __func__, status);
545 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400546}
547
548#ifdef CONFIG_NFS_V4_1
549/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700550 * Returns true if the client IDs match
551 */
552static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
553{
554 if (a->cl_clientid != b->cl_clientid) {
555 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
556 __func__, a->cl_clientid, b->cl_clientid);
557 return false;
558 }
559 dprintk("NFS: --> %s client ID %llx matches %llx\n",
560 __func__, a->cl_clientid, b->cl_clientid);
561 return true;
562}
563
564/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400565 * Returns true if the server owners match
566 */
567static bool
568nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
569{
570 struct nfs41_server_owner *o1 = a->cl_serverowner;
571 struct nfs41_server_owner *o2 = b->cl_serverowner;
572
573 if (o1->minor_id != o2->minor_id) {
574 dprintk("NFS: --> %s server owner minor IDs do not match\n",
575 __func__);
576 return false;
577 }
578
579 if (o1->major_id_sz != o2->major_id_sz)
580 goto out_major_mismatch;
581 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
582 goto out_major_mismatch;
583
584 dprintk("NFS: --> %s server owners match\n", __func__);
585 return true;
586
587out_major_mismatch:
588 dprintk("NFS: --> %s server owner major IDs do not match\n",
589 __func__);
590 return false;
591}
592
593/**
594 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
595 *
596 * @new: nfs_client with client ID to test
597 * @result: OUT: found nfs_client, or new
598 * @cred: credential to use for trunking test
599 *
600 * Returns zero, a negative errno, or a negative NFS4ERR status.
601 * If zero is returned, an nfs_client pointer is planted in "result."
602 *
603 * NB: nfs41_walk_client_list() relies on the new nfs_client being
604 * the last nfs_client on the list.
605 */
606int nfs41_walk_client_list(struct nfs_client *new,
607 struct nfs_client **result,
608 struct rpc_cred *cred)
609{
610 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400611 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500612 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400613
614 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400615 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400616 /* If "pos" isn't marked ready, we can't trust the
617 * remaining fields in "pos", especially the client
618 * ID and serverowner fields. Wait for CREATE_SESSION
619 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400620 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400621 atomic_inc(&pos->cl_count);
622 spin_unlock(&nn->nfs_client_lock);
623
624 if (prev)
625 nfs_put_client(prev);
626 prev = pos;
627
Trond Myklebust202c3122013-01-18 22:56:23 -0500628 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400629 if (status == 0) {
630 nfs4_schedule_lease_recovery(pos);
631 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400632 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400633 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500634 if (status < 0)
635 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400636 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400637 if (pos->cl_cons_state != NFS_CS_READY)
638 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400639
640 if (pos->rpc_ops != new->rpc_ops)
641 continue;
642
643 if (pos->cl_proto != new->cl_proto)
644 continue;
645
646 if (pos->cl_minorversion != new->cl_minorversion)
647 continue;
648
649 if (!nfs4_match_clientids(pos, new))
650 continue;
651
652 if (!nfs4_match_serverowners(pos, new))
653 continue;
654
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500655 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400656 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400657 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400658 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
659 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400660 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400661 }
662
Trond Myklebust202c3122013-01-18 22:56:23 -0500663 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400664 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500665 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400666 if (prev)
667 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500668 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400669}
670#endif /* CONFIG_NFS_V4_1 */
671
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400672static void nfs4_destroy_server(struct nfs_server *server)
673{
674 nfs_server_return_all_delegations(server);
675 unset_pnfs_layoutdriver(server);
676 nfs4_purge_state_owners(server);
677}
678
679/*
680 * NFSv4.0 callback thread helper
681 *
682 * Find a client by callback identifier
683 */
684struct nfs_client *
685nfs4_find_client_ident(struct net *net, int cb_ident)
686{
687 struct nfs_client *clp;
688 struct nfs_net *nn = net_generic(net, nfs_net_id);
689
690 spin_lock(&nn->nfs_client_lock);
691 clp = idr_find(&nn->cb_ident_idr, cb_ident);
692 if (clp)
693 atomic_inc(&clp->cl_count);
694 spin_unlock(&nn->nfs_client_lock);
695 return clp;
696}
697
698#if defined(CONFIG_NFS_V4_1)
699/* Common match routine for v4.0 and v4.1 callback services */
700static bool nfs4_cb_match_client(const struct sockaddr *addr,
701 struct nfs_client *clp, u32 minorversion)
702{
703 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
704
705 /* Don't match clients that failed to initialise */
706 if (!(clp->cl_cons_state == NFS_CS_READY ||
707 clp->cl_cons_state == NFS_CS_SESSION_INITING))
708 return false;
709
710 smp_rmb();
711
712 /* Match the version and minorversion */
713 if (clp->rpc_ops->version != 4 ||
714 clp->cl_minorversion != minorversion)
715 return false;
716
717 /* Match only the IP address, not the port number */
718 if (!nfs_sockaddr_match_ipaddr(addr, clap))
719 return false;
720
721 return true;
722}
723
724/*
725 * NFSv4.1 callback thread helper
726 * For CB_COMPOUND calls, find a client by IP address, protocol version,
727 * minorversion, and sessionID
728 *
729 * Returns NULL if no such client
730 */
731struct nfs_client *
732nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400733 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400734{
735 struct nfs_client *clp;
736 struct nfs_net *nn = net_generic(net, nfs_net_id);
737
738 spin_lock(&nn->nfs_client_lock);
739 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400740 if (nfs4_cb_match_client(addr, clp, minorversion) == false)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400741 continue;
742
743 if (!nfs4_has_session(clp))
744 continue;
745
746 /* Match sessionid*/
747 if (memcmp(clp->cl_session->sess_id.data,
748 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
749 continue;
750
751 atomic_inc(&clp->cl_count);
752 spin_unlock(&nn->nfs_client_lock);
753 return clp;
754 }
755 spin_unlock(&nn->nfs_client_lock);
756 return NULL;
757}
758
759#else /* CONFIG_NFS_V4_1 */
760
761struct nfs_client *
762nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400763 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400764{
765 return NULL;
766}
767#endif /* CONFIG_NFS_V4_1 */
768
769/*
770 * Set up an NFS4 client
771 */
772static int nfs4_set_client(struct nfs_server *server,
773 const char *hostname,
774 const struct sockaddr *addr,
775 const size_t addrlen,
776 const char *ip_addr,
777 rpc_authflavor_t authflavour,
778 int proto, const struct rpc_timeout *timeparms,
779 u32 minorversion, struct net *net)
780{
781 struct nfs_client_initdata cl_init = {
782 .hostname = hostname,
783 .addr = addr,
784 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400785 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400786 .proto = proto,
787 .minorversion = minorversion,
788 .net = net,
789 };
790 struct nfs_client *clp;
791 int error;
792
793 dprintk("--> nfs4_set_client()\n");
794
795 if (server->flags & NFS_MOUNT_NORESVPORT)
796 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Chuck Leverf112bb42013-06-25 12:23:27 -0400797 if (server->options & NFS_OPTION_MIGRATION)
798 set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400799
800 /* Allocate or find a client reference we can use */
801 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
802 if (IS_ERR(clp)) {
803 error = PTR_ERR(clp);
804 goto error;
805 }
806
807 /*
808 * Query for the lease time on clientid setup or renewal
809 *
810 * Note that this will be set on nfs_clients that were created
811 * only for the DS role and did not set this bit, but now will
812 * serve a dual role.
813 */
814 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
815
816 server->nfs_client = clp;
817 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
818 return 0;
819error:
820 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
821 return error;
822}
823
824/*
825 * Set up a pNFS Data Server client.
826 *
827 * Return any existing nfs_client that matches server address,port,version
828 * and minorversion.
829 *
830 * For a new nfs_client, use a soft mount (default), a low retrans and a
831 * low timeout interval so that if a connection is lost, we retry through
832 * the MDS.
833 */
834struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
835 const struct sockaddr *ds_addr, int ds_addrlen,
836 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
837{
838 struct nfs_client_initdata cl_init = {
839 .addr = ds_addr,
840 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400841 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400842 .proto = ds_proto,
843 .minorversion = mds_clp->cl_minorversion,
844 .net = mds_clp->cl_net,
845 };
846 struct rpc_timeout ds_timeout;
847 struct nfs_client *clp;
848
849 /*
850 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
851 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
852 * (section 13.1 RFC 5661).
853 */
854 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
855 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
856 mds_clp->cl_rpcclient->cl_auth->au_flavor);
857
858 dprintk("<-- %s %p\n", __func__, clp);
859 return clp;
860}
861EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
862
863/*
864 * Session has been established, and the client marked ready.
865 * Set the mount rsize and wsize with negotiated fore channel
866 * attributes which will be bound checked in nfs_server_set_fsinfo.
867 */
868static void nfs4_session_set_rwsize(struct nfs_server *server)
869{
870#ifdef CONFIG_NFS_V4_1
871 struct nfs4_session *sess;
872 u32 server_resp_sz;
873 u32 server_rqst_sz;
874
875 if (!nfs4_has_session(server->nfs_client))
876 return;
877 sess = server->nfs_client->cl_session;
878 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
879 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
880
881 if (server->rsize > server_resp_sz)
882 server->rsize = server_resp_sz;
883 if (server->wsize > server_rqst_sz)
884 server->wsize = server_rqst_sz;
885#endif /* CONFIG_NFS_V4_1 */
886}
887
888static int nfs4_server_common_setup(struct nfs_server *server,
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400889 struct nfs_fh *mntfh, bool auth_probe)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400890{
891 struct nfs_fattr *fattr;
892 int error;
893
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400894 /* data servers support only a subset of NFSv4.1 */
895 if (is_ds_only_client(server->nfs_client))
896 return -EPROTONOSUPPORT;
897
898 fattr = nfs_alloc_fattr();
899 if (fattr == NULL)
900 return -ENOMEM;
901
902 /* We must ensure the session is initialised first */
Andy Adamson18aad3d2013-06-26 12:21:49 -0400903 error = nfs4_init_session(server->nfs_client);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400904 if (error < 0)
905 goto out;
906
Trond Myklebust39c6daa2013-03-15 16:11:57 -0400907 /* Set the basic capabilities */
908 server->caps |= server->nfs_client->cl_mvops->init_caps;
909 if (server->flags & NFS_MOUNT_NORDIRPLUS)
910 server->caps &= ~NFS_CAP_READDIRPLUS;
911 /*
912 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
913 * authentication.
914 */
915 if (nfs4_disable_idmapping &&
916 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
917 server->caps |= NFS_CAP_UIDGID_NOMAP;
918
919
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400920 /* Probe the root fh to retrieve its FSID and filehandle */
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400921 error = nfs4_get_rootfh(server, mntfh, auth_probe);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400922 if (error < 0)
923 goto out;
924
925 dprintk("Server FSID: %llx:%llx\n",
926 (unsigned long long) server->fsid.major,
927 (unsigned long long) server->fsid.minor);
928 dprintk("Mount FH: %d\n", mntfh->size);
929
930 nfs4_session_set_rwsize(server);
931
932 error = nfs_probe_fsinfo(server, mntfh, fattr);
933 if (error < 0)
934 goto out;
935
936 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
937 server->namelen = NFS4_MAXNAMLEN;
938
939 nfs_server_insert_lists(server);
940 server->mount_time = jiffies;
941 server->destroy = nfs4_destroy_server;
942out:
943 nfs_free_fattr(fattr);
944 return error;
945}
946
947/*
948 * Create a version 4 volume record
949 */
950static int nfs4_init_server(struct nfs_server *server,
951 const struct nfs_parsed_mount_data *data)
952{
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400953 rpc_authflavor_t pseudoflavor = RPC_AUTH_UNIX;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400954 struct rpc_timeout timeparms;
955 int error;
956
957 dprintk("--> nfs4_init_server()\n");
958
959 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
960 data->timeo, data->retrans);
961
962 /* Initialise the client representation from the mount data */
963 server->flags = data->flags;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400964 server->options = data->options;
965
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400966 if (data->auth_flavor_len >= 1)
967 pseudoflavor = data->auth_flavors[0];
968
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400969 /* Get a client record */
970 error = nfs4_set_client(server,
971 data->nfs_server.hostname,
972 (const struct sockaddr *)&data->nfs_server.address,
973 data->nfs_server.addrlen,
974 data->client_address,
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400975 pseudoflavor,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400976 data->nfs_server.protocol,
977 &timeparms,
978 data->minorversion,
979 data->net);
980 if (error < 0)
981 goto error;
982
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400983 if (data->rsize)
984 server->rsize = nfs_block_size(data->rsize, NULL);
985 if (data->wsize)
986 server->wsize = nfs_block_size(data->wsize, NULL);
987
988 server->acregmin = data->acregmin * HZ;
989 server->acregmax = data->acregmax * HZ;
990 server->acdirmin = data->acdirmin * HZ;
991 server->acdirmax = data->acdirmax * HZ;
992
993 server->port = data->nfs_server.port;
994
Trond Myklebust5e6b1992013-09-07 12:58:57 -0400995 error = nfs_init_server_rpcclient(server, &timeparms, pseudoflavor);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400996
997error:
998 /* Done */
999 dprintk("<-- nfs4_init_server() = %d\n", error);
1000 return error;
1001}
1002
1003/*
1004 * Create a version 4 volume record
1005 * - keyed on server and FSID
1006 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001007/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1008 struct nfs_fh *mntfh)*/
1009struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
1010 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001011{
1012 struct nfs_server *server;
Trond Myklebust5e6b1992013-09-07 12:58:57 -04001013 bool auth_probe;
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001014 int error;
1015
1016 dprintk("--> nfs4_create_server()\n");
1017
1018 server = nfs_alloc_server();
1019 if (!server)
1020 return ERR_PTR(-ENOMEM);
1021
Trond Myklebust5e6b1992013-09-07 12:58:57 -04001022 auth_probe = mount_info->parsed->auth_flavor_len < 1;
1023
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001024 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001025 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001026 if (error < 0)
1027 goto error;
1028
Trond Myklebust5e6b1992013-09-07 12:58:57 -04001029 error = nfs4_server_common_setup(server, mount_info->mntfh, auth_probe);
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001030 if (error < 0)
1031 goto error;
1032
1033 dprintk("<-- nfs4_create_server() = %p\n", server);
1034 return server;
1035
1036error:
1037 nfs_free_server(server);
1038 dprintk("<-- nfs4_create_server() = error %d\n", error);
1039 return ERR_PTR(error);
1040}
1041
1042/*
1043 * Create an NFS4 referral server record
1044 */
1045struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1046 struct nfs_fh *mntfh)
1047{
1048 struct nfs_client *parent_client;
1049 struct nfs_server *server, *parent_server;
1050 int error;
1051
1052 dprintk("--> nfs4_create_referral_server()\n");
1053
1054 server = nfs_alloc_server();
1055 if (!server)
1056 return ERR_PTR(-ENOMEM);
1057
1058 parent_server = NFS_SB(data->sb);
1059 parent_client = parent_server->nfs_client;
1060
1061 /* Initialise the client representation from the parent server */
1062 nfs_server_copy_userdata(server, parent_server);
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001063
1064 /* Get a client representation.
1065 * Note: NFSv4 always uses TCP, */
1066 error = nfs4_set_client(server, data->hostname,
1067 data->addr,
1068 data->addrlen,
1069 parent_client->cl_ipaddr,
1070 data->authflavor,
1071 rpc_protocol(parent_server->client),
1072 parent_server->client->cl_timeout,
1073 parent_client->cl_mvops->minor_version,
1074 parent_client->cl_net);
1075 if (error < 0)
1076 goto error;
1077
1078 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1079 if (error < 0)
1080 goto error;
1081
Trond Myklebust47040da2013-09-07 16:01:07 -04001082 error = nfs4_server_common_setup(server, mntfh,
1083 !(parent_server->flags & NFS_MOUNT_SECFLAVOUR));
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001084 if (error < 0)
1085 goto error;
1086
1087 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1088 return server;
1089
1090error:
1091 nfs_free_server(server);
1092 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1093 return ERR_PTR(error);
1094}