blob: 66b6664dcd4c4c9cd209d9420eb9de895cd1e344 [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
44static void nfs4_shutdown_session(struct nfs_client *clp)
45{
46 if (nfs4_has_session(clp)) {
47 nfs4_destroy_session(clp->cl_session);
48 nfs4_destroy_clientid(clp);
49 }
50
51}
52#else /* CONFIG_NFS_V4_1 */
53static void nfs4_shutdown_session(struct nfs_client *clp)
54{
55}
56#endif /* CONFIG_NFS_V4_1 */
57
58struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
59{
60 int err;
61 struct nfs_client *clp = nfs_alloc_client(cl_init);
62 if (IS_ERR(clp))
63 return clp;
64
65 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
66 if (err)
67 goto error;
68
69 spin_lock_init(&clp->cl_lock);
70 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
71 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
72 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
73 clp->cl_minorversion = cl_init->minorversion;
74 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
75 return clp;
76
77error:
Trond Myklebust7653f6f2012-08-20 12:12:29 -040078 nfs_free_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -040079 return ERR_PTR(err);
80}
81
82/*
83 * Destroy the NFS4 callback service
84 */
85static void nfs4_destroy_callback(struct nfs_client *clp)
86{
87 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040088 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
Bryan Schumakerec409892012-07-16 16:39:21 -040089}
90
91static void nfs4_shutdown_client(struct nfs_client *clp)
92{
93 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
94 nfs4_kill_renewd(clp);
95 nfs4_shutdown_session(clp);
96 nfs4_destroy_callback(clp);
97 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
98 nfs_idmap_delete(clp);
99
100 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
101 kfree(clp->cl_serverowner);
102 kfree(clp->cl_serverscope);
103 kfree(clp->cl_implid);
104}
105
106void nfs4_free_client(struct nfs_client *clp)
107{
108 nfs4_shutdown_client(clp);
109 nfs_free_client(clp);
110}
111
112/*
Bryan Schumaker428360d2012-07-16 16:39:17 -0400113 * Initialize the NFS4 callback service
114 */
115static int nfs4_init_callback(struct nfs_client *clp)
116{
117 int error;
118
119 if (clp->rpc_ops->version == 4) {
120 struct rpc_xprt *xprt;
121
122 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
123
124 if (nfs4_has_session(clp)) {
125 error = xprt_setup_backchannel(xprt,
126 NFS41_BC_MIN_CALLBACKS);
127 if (error < 0)
128 return error;
129 }
130
131 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
132 if (error < 0) {
133 dprintk("%s: failed to start callback. Error = %d\n",
134 __func__, error);
135 return error;
136 }
137 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
138 }
139 return 0;
140}
141
142/*
143 * Initialize the minor version specific parts of an NFS4 client record
144 */
145static int nfs4_init_client_minor_version(struct nfs_client *clp)
146{
147#if defined(CONFIG_NFS_V4_1)
148 if (clp->cl_mvops->minor_version) {
149 struct nfs4_session *session = NULL;
150 /*
151 * Create the session and mark it expired.
152 * When a SEQUENCE operation encounters the expired session
153 * it will do session recovery to initialize it.
154 */
155 session = nfs4_alloc_session(clp);
156 if (!session)
157 return -ENOMEM;
158
159 clp->cl_session = session;
160 /*
161 * The create session reply races with the server back
162 * channel probe. Mark the client NFS_CS_SESSION_INITING
163 * so that the client back channel can find the
164 * nfs_client struct
165 */
166 nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
167 }
168#endif /* CONFIG_NFS_V4_1 */
169
170 return nfs4_init_callback(clp);
171}
172
173/**
174 * nfs4_init_client - Initialise an NFS4 client record
175 *
176 * @clp: nfs_client to initialise
177 * @timeparms: timeout parameters for underlying RPC transport
178 * @ip_addr: callback IP address in presentation format
179 * @authflavor: authentication flavor for underlying RPC transport
180 *
181 * Returns pointer to an NFS client, or an ERR_PTR value.
182 */
183struct nfs_client *nfs4_init_client(struct nfs_client *clp,
184 const struct rpc_timeout *timeparms,
185 const char *ip_addr,
186 rpc_authflavor_t authflavour)
187{
188 char buf[INET6_ADDRSTRLEN + 1];
Chuck Lever05f4c352012-09-14 17:24:32 -0400189 struct nfs_client *old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400190 int error;
191
192 if (clp->cl_cons_state == NFS_CS_READY) {
193 /* the client is initialised already */
194 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
195 return clp;
196 }
197
198 /* Check NFS protocol revision and initialize RPC op vector */
199 clp->rpc_ops = &nfs_v4_clientops;
200
201 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
202 error = nfs_create_rpc_client(clp, timeparms, authflavour);
203 if (error < 0)
204 goto error;
205
206 /* If no clientaddr= option was specified, find a usable cb address */
207 if (ip_addr == NULL) {
208 struct sockaddr_storage cb_addr;
209 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
210
211 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
212 if (error < 0)
213 goto error;
214 error = rpc_ntop(sap, buf, sizeof(buf));
215 if (error < 0)
216 goto error;
217 ip_addr = (const char *)buf;
218 }
219 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
220
221 error = nfs_idmap_new(clp);
222 if (error < 0) {
223 dprintk("%s: failed to create idmapper. Error = %d\n",
224 __func__, error);
225 goto error;
226 }
227 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
228
229 error = nfs4_init_client_minor_version(clp);
230 if (error < 0)
231 goto error;
232
233 if (!nfs4_has_session(clp))
234 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever05f4c352012-09-14 17:24:32 -0400235
236 error = nfs4_discover_server_trunking(clp, &old);
237 if (error < 0)
238 goto error;
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500239 nfs_put_client(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400240 if (clp != old) {
241 clp->cl_preserve_clid = true;
Chuck Lever05f4c352012-09-14 17:24:32 -0400242 clp = old;
Chuck Lever05f4c352012-09-14 17:24:32 -0400243 }
244
Bryan Schumaker428360d2012-07-16 16:39:17 -0400245 return clp;
246
247error:
248 nfs_mark_client_ready(clp, error);
249 nfs_put_client(clp);
250 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
251 return ERR_PTR(error);
252}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400253
Chuck Lever05f4c352012-09-14 17:24:32 -0400254/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400255 * SETCLIENTID just did a callback update with the callback ident in
256 * "drop," but server trunking discovery claims "drop" and "keep" are
257 * actually the same server. Swap the callback IDs so that "keep"
258 * will continue to use the callback ident the server now knows about,
259 * and so that "keep"'s original callback ident is destroyed when
260 * "drop" is freed.
261 */
262static void nfs4_swap_callback_idents(struct nfs_client *keep,
263 struct nfs_client *drop)
264{
265 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
266 unsigned int save = keep->cl_cb_ident;
267
268 if (keep->cl_cb_ident == drop->cl_cb_ident)
269 return;
270
271 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
272 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
273
274 spin_lock(&nn->nfs_client_lock);
275
276 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
277 keep->cl_cb_ident = drop->cl_cb_ident;
278
279 idr_replace(&nn->cb_ident_idr, drop, save);
280 drop->cl_cb_ident = save;
281
282 spin_unlock(&nn->nfs_client_lock);
283}
284
285/**
286 * nfs40_walk_client_list - Find server that recognizes a client ID
287 *
288 * @new: nfs_client with client ID to test
289 * @result: OUT: found nfs_client, or new
290 * @cred: credential to use for trunking test
291 *
292 * Returns zero, a negative errno, or a negative NFS4ERR status.
293 * If zero is returned, an nfs_client pointer is planted in "result."
294 *
295 * NB: nfs40_walk_client_list() relies on the new nfs_client being
296 * the last nfs_client on the list.
297 */
298int nfs40_walk_client_list(struct nfs_client *new,
299 struct nfs_client **result,
300 struct rpc_cred *cred)
301{
302 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400303 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400304 struct nfs4_setclientid_res clid = {
305 .clientid = new->cl_clientid,
306 .confirm = new->cl_confirm,
307 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500308 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400309
310 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400311 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400312 /* If "pos" isn't marked ready, we can't trust the
313 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400314 if (pos->cl_cons_state > NFS_CS_READY) {
315 atomic_inc(&pos->cl_count);
316 spin_unlock(&nn->nfs_client_lock);
317
318 if (prev)
319 nfs_put_client(prev);
320 prev = pos;
321
322 status = nfs_wait_client_init_complete(pos);
323 spin_lock(&nn->nfs_client_lock);
324 if (status < 0)
325 continue;
326 }
327 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400328 continue;
329
330 if (pos->rpc_ops != new->rpc_ops)
331 continue;
332
333 if (pos->cl_proto != new->cl_proto)
334 continue;
335
336 if (pos->cl_minorversion != new->cl_minorversion)
337 continue;
338
339 if (pos->cl_clientid != new->cl_clientid)
340 continue;
341
342 atomic_inc(&pos->cl_count);
343 spin_unlock(&nn->nfs_client_lock);
344
345 if (prev)
346 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500347 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400348
349 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500350 switch (status) {
351 case -NFS4ERR_STALE_CLIENTID:
352 break;
353 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400354 nfs4_swap_callback_idents(pos, new);
355
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500356 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400357 *result = pos;
358 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
359 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500360 default:
361 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400362 }
363
364 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400365 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500366 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400367
Trond Myklebust202c3122013-01-18 22:56:23 -0500368 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500369out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400370 if (prev)
371 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500372 dprintk("NFS: <-- %s status = %d\n", __func__, status);
373 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400374}
375
376#ifdef CONFIG_NFS_V4_1
377/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700378 * Returns true if the client IDs match
379 */
380static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
381{
382 if (a->cl_clientid != b->cl_clientid) {
383 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
384 __func__, a->cl_clientid, b->cl_clientid);
385 return false;
386 }
387 dprintk("NFS: --> %s client ID %llx matches %llx\n",
388 __func__, a->cl_clientid, b->cl_clientid);
389 return true;
390}
391
392/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400393 * Returns true if the server owners match
394 */
395static bool
396nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
397{
398 struct nfs41_server_owner *o1 = a->cl_serverowner;
399 struct nfs41_server_owner *o2 = b->cl_serverowner;
400
401 if (o1->minor_id != o2->minor_id) {
402 dprintk("NFS: --> %s server owner minor IDs do not match\n",
403 __func__);
404 return false;
405 }
406
407 if (o1->major_id_sz != o2->major_id_sz)
408 goto out_major_mismatch;
409 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
410 goto out_major_mismatch;
411
412 dprintk("NFS: --> %s server owners match\n", __func__);
413 return true;
414
415out_major_mismatch:
416 dprintk("NFS: --> %s server owner major IDs do not match\n",
417 __func__);
418 return false;
419}
420
421/**
422 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
423 *
424 * @new: nfs_client with client ID to test
425 * @result: OUT: found nfs_client, or new
426 * @cred: credential to use for trunking test
427 *
428 * Returns zero, a negative errno, or a negative NFS4ERR status.
429 * If zero is returned, an nfs_client pointer is planted in "result."
430 *
431 * NB: nfs41_walk_client_list() relies on the new nfs_client being
432 * the last nfs_client on the list.
433 */
434int nfs41_walk_client_list(struct nfs_client *new,
435 struct nfs_client **result,
436 struct rpc_cred *cred)
437{
438 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400439 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500440 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400441
442 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400443 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400444 /* If "pos" isn't marked ready, we can't trust the
445 * remaining fields in "pos", especially the client
446 * ID and serverowner fields. Wait for CREATE_SESSION
447 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400448 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400449 atomic_inc(&pos->cl_count);
450 spin_unlock(&nn->nfs_client_lock);
451
452 if (prev)
453 nfs_put_client(prev);
454 prev = pos;
455
Trond Myklebust202c3122013-01-18 22:56:23 -0500456 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400457 if (status == 0) {
458 nfs4_schedule_lease_recovery(pos);
459 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400460 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400461 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500462 if (status < 0)
463 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400464 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400465 if (pos->cl_cons_state != NFS_CS_READY)
466 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400467
468 if (pos->rpc_ops != new->rpc_ops)
469 continue;
470
471 if (pos->cl_proto != new->cl_proto)
472 continue;
473
474 if (pos->cl_minorversion != new->cl_minorversion)
475 continue;
476
477 if (!nfs4_match_clientids(pos, new))
478 continue;
479
480 if (!nfs4_match_serverowners(pos, new))
481 continue;
482
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500483 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400484 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400485 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400486 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
487 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400488 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400489 }
490
Trond Myklebust202c3122013-01-18 22:56:23 -0500491 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400492 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500493 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400494 if (prev)
495 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500496 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400497}
498#endif /* CONFIG_NFS_V4_1 */
499
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400500static void nfs4_destroy_server(struct nfs_server *server)
501{
502 nfs_server_return_all_delegations(server);
503 unset_pnfs_layoutdriver(server);
504 nfs4_purge_state_owners(server);
505}
506
507/*
508 * NFSv4.0 callback thread helper
509 *
510 * Find a client by callback identifier
511 */
512struct nfs_client *
513nfs4_find_client_ident(struct net *net, int cb_ident)
514{
515 struct nfs_client *clp;
516 struct nfs_net *nn = net_generic(net, nfs_net_id);
517
518 spin_lock(&nn->nfs_client_lock);
519 clp = idr_find(&nn->cb_ident_idr, cb_ident);
520 if (clp)
521 atomic_inc(&clp->cl_count);
522 spin_unlock(&nn->nfs_client_lock);
523 return clp;
524}
525
526#if defined(CONFIG_NFS_V4_1)
527/* Common match routine for v4.0 and v4.1 callback services */
528static bool nfs4_cb_match_client(const struct sockaddr *addr,
529 struct nfs_client *clp, u32 minorversion)
530{
531 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
532
533 /* Don't match clients that failed to initialise */
534 if (!(clp->cl_cons_state == NFS_CS_READY ||
535 clp->cl_cons_state == NFS_CS_SESSION_INITING))
536 return false;
537
538 smp_rmb();
539
540 /* Match the version and minorversion */
541 if (clp->rpc_ops->version != 4 ||
542 clp->cl_minorversion != minorversion)
543 return false;
544
545 /* Match only the IP address, not the port number */
546 if (!nfs_sockaddr_match_ipaddr(addr, clap))
547 return false;
548
549 return true;
550}
551
552/*
553 * NFSv4.1 callback thread helper
554 * For CB_COMPOUND calls, find a client by IP address, protocol version,
555 * minorversion, and sessionID
556 *
557 * Returns NULL if no such client
558 */
559struct nfs_client *
560nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
561 struct nfs4_sessionid *sid)
562{
563 struct nfs_client *clp;
564 struct nfs_net *nn = net_generic(net, nfs_net_id);
565
566 spin_lock(&nn->nfs_client_lock);
567 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
568 if (nfs4_cb_match_client(addr, clp, 1) == false)
569 continue;
570
571 if (!nfs4_has_session(clp))
572 continue;
573
574 /* Match sessionid*/
575 if (memcmp(clp->cl_session->sess_id.data,
576 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
577 continue;
578
579 atomic_inc(&clp->cl_count);
580 spin_unlock(&nn->nfs_client_lock);
581 return clp;
582 }
583 spin_unlock(&nn->nfs_client_lock);
584 return NULL;
585}
586
587#else /* CONFIG_NFS_V4_1 */
588
589struct nfs_client *
590nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
591 struct nfs4_sessionid *sid)
592{
593 return NULL;
594}
595#endif /* CONFIG_NFS_V4_1 */
596
597/*
598 * Set up an NFS4 client
599 */
600static int nfs4_set_client(struct nfs_server *server,
601 const char *hostname,
602 const struct sockaddr *addr,
603 const size_t addrlen,
604 const char *ip_addr,
605 rpc_authflavor_t authflavour,
606 int proto, const struct rpc_timeout *timeparms,
607 u32 minorversion, struct net *net)
608{
609 struct nfs_client_initdata cl_init = {
610 .hostname = hostname,
611 .addr = addr,
612 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400613 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400614 .proto = proto,
615 .minorversion = minorversion,
616 .net = net,
617 };
618 struct nfs_client *clp;
619 int error;
620
621 dprintk("--> nfs4_set_client()\n");
622
623 if (server->flags & NFS_MOUNT_NORESVPORT)
624 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
625
626 /* Allocate or find a client reference we can use */
627 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
628 if (IS_ERR(clp)) {
629 error = PTR_ERR(clp);
630 goto error;
631 }
632
633 /*
634 * Query for the lease time on clientid setup or renewal
635 *
636 * Note that this will be set on nfs_clients that were created
637 * only for the DS role and did not set this bit, but now will
638 * serve a dual role.
639 */
640 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
641
642 server->nfs_client = clp;
643 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
644 return 0;
645error:
646 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
647 return error;
648}
649
650/*
651 * Set up a pNFS Data Server client.
652 *
653 * Return any existing nfs_client that matches server address,port,version
654 * and minorversion.
655 *
656 * For a new nfs_client, use a soft mount (default), a low retrans and a
657 * low timeout interval so that if a connection is lost, we retry through
658 * the MDS.
659 */
660struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
661 const struct sockaddr *ds_addr, int ds_addrlen,
662 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
663{
664 struct nfs_client_initdata cl_init = {
665 .addr = ds_addr,
666 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400667 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400668 .proto = ds_proto,
669 .minorversion = mds_clp->cl_minorversion,
670 .net = mds_clp->cl_net,
671 };
672 struct rpc_timeout ds_timeout;
673 struct nfs_client *clp;
674
675 /*
676 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
677 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
678 * (section 13.1 RFC 5661).
679 */
680 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
681 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
682 mds_clp->cl_rpcclient->cl_auth->au_flavor);
683
684 dprintk("<-- %s %p\n", __func__, clp);
685 return clp;
686}
687EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
688
689/*
690 * Session has been established, and the client marked ready.
691 * Set the mount rsize and wsize with negotiated fore channel
692 * attributes which will be bound checked in nfs_server_set_fsinfo.
693 */
694static void nfs4_session_set_rwsize(struct nfs_server *server)
695{
696#ifdef CONFIG_NFS_V4_1
697 struct nfs4_session *sess;
698 u32 server_resp_sz;
699 u32 server_rqst_sz;
700
701 if (!nfs4_has_session(server->nfs_client))
702 return;
703 sess = server->nfs_client->cl_session;
704 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
705 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
706
707 if (server->rsize > server_resp_sz)
708 server->rsize = server_resp_sz;
709 if (server->wsize > server_rqst_sz)
710 server->wsize = server_rqst_sz;
711#endif /* CONFIG_NFS_V4_1 */
712}
713
714static int nfs4_server_common_setup(struct nfs_server *server,
715 struct nfs_fh *mntfh)
716{
717 struct nfs_fattr *fattr;
718 int error;
719
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400720 /* data servers support only a subset of NFSv4.1 */
721 if (is_ds_only_client(server->nfs_client))
722 return -EPROTONOSUPPORT;
723
724 fattr = nfs_alloc_fattr();
725 if (fattr == NULL)
726 return -ENOMEM;
727
728 /* We must ensure the session is initialised first */
729 error = nfs4_init_session(server);
730 if (error < 0)
731 goto out;
732
733 /* Probe the root fh to retrieve its FSID and filehandle */
734 error = nfs4_get_rootfh(server, mntfh);
735 if (error < 0)
736 goto out;
737
738 dprintk("Server FSID: %llx:%llx\n",
739 (unsigned long long) server->fsid.major,
740 (unsigned long long) server->fsid.minor);
741 dprintk("Mount FH: %d\n", mntfh->size);
742
743 nfs4_session_set_rwsize(server);
744
745 error = nfs_probe_fsinfo(server, mntfh, fattr);
746 if (error < 0)
747 goto out;
748
749 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
750 server->namelen = NFS4_MAXNAMLEN;
751
752 nfs_server_insert_lists(server);
753 server->mount_time = jiffies;
754 server->destroy = nfs4_destroy_server;
755out:
756 nfs_free_fattr(fattr);
757 return error;
758}
759
760/*
761 * Create a version 4 volume record
762 */
763static int nfs4_init_server(struct nfs_server *server,
764 const struct nfs_parsed_mount_data *data)
765{
766 struct rpc_timeout timeparms;
767 int error;
768
769 dprintk("--> nfs4_init_server()\n");
770
771 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
772 data->timeo, data->retrans);
773
774 /* Initialise the client representation from the mount data */
775 server->flags = data->flags;
776 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
777 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
778 server->caps |= NFS_CAP_READDIRPLUS;
779 server->options = data->options;
780
781 /* Get a client record */
782 error = nfs4_set_client(server,
783 data->nfs_server.hostname,
784 (const struct sockaddr *)&data->nfs_server.address,
785 data->nfs_server.addrlen,
786 data->client_address,
787 data->auth_flavors[0],
788 data->nfs_server.protocol,
789 &timeparms,
790 data->minorversion,
791 data->net);
792 if (error < 0)
793 goto error;
794
795 /*
796 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
797 * authentication.
798 */
799 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
800 server->caps |= NFS_CAP_UIDGID_NOMAP;
801
802 if (data->rsize)
803 server->rsize = nfs_block_size(data->rsize, NULL);
804 if (data->wsize)
805 server->wsize = nfs_block_size(data->wsize, NULL);
806
807 server->acregmin = data->acregmin * HZ;
808 server->acregmax = data->acregmax * HZ;
809 server->acdirmin = data->acdirmin * HZ;
810 server->acdirmax = data->acdirmax * HZ;
811
812 server->port = data->nfs_server.port;
813
814 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
815
816error:
817 /* Done */
818 dprintk("<-- nfs4_init_server() = %d\n", error);
819 return error;
820}
821
822/*
823 * Create a version 4 volume record
824 * - keyed on server and FSID
825 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400826/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
827 struct nfs_fh *mntfh)*/
828struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
829 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400830{
831 struct nfs_server *server;
832 int error;
833
834 dprintk("--> nfs4_create_server()\n");
835
836 server = nfs_alloc_server();
837 if (!server)
838 return ERR_PTR(-ENOMEM);
839
840 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400841 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400842 if (error < 0)
843 goto error;
844
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400845 error = nfs4_server_common_setup(server, mount_info->mntfh);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400846 if (error < 0)
847 goto error;
848
849 dprintk("<-- nfs4_create_server() = %p\n", server);
850 return server;
851
852error:
853 nfs_free_server(server);
854 dprintk("<-- nfs4_create_server() = error %d\n", error);
855 return ERR_PTR(error);
856}
857
858/*
859 * Create an NFS4 referral server record
860 */
861struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
862 struct nfs_fh *mntfh)
863{
864 struct nfs_client *parent_client;
865 struct nfs_server *server, *parent_server;
866 int error;
867
868 dprintk("--> nfs4_create_referral_server()\n");
869
870 server = nfs_alloc_server();
871 if (!server)
872 return ERR_PTR(-ENOMEM);
873
874 parent_server = NFS_SB(data->sb);
875 parent_client = parent_server->nfs_client;
876
877 /* Initialise the client representation from the parent server */
878 nfs_server_copy_userdata(server, parent_server);
879 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
880
881 /* Get a client representation.
882 * Note: NFSv4 always uses TCP, */
883 error = nfs4_set_client(server, data->hostname,
884 data->addr,
885 data->addrlen,
886 parent_client->cl_ipaddr,
887 data->authflavor,
888 rpc_protocol(parent_server->client),
889 parent_server->client->cl_timeout,
890 parent_client->cl_mvops->minor_version,
891 parent_client->cl_net);
892 if (error < 0)
893 goto error;
894
895 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
896 if (error < 0)
897 goto error;
898
899 error = nfs4_server_common_setup(server, mntfh);
900 if (error < 0)
901 goto error;
902
903 dprintk("<-- nfs_create_referral_server() = %p\n", server);
904 return server;
905
906error:
907 nfs_free_server(server);
908 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
909 return ERR_PTR(error);
910}