blob: 767a5e37fe9731673a81ae284123979b4b72134d [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
Steve Dickson42c2c422013-05-22 12:50:38 -040069 if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
70 err = -EINVAL;
71 goto error;
72 }
73
Bryan Schumakerec409892012-07-16 16:39:21 -040074 spin_lock_init(&clp->cl_lock);
75 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
76 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
77 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
78 clp->cl_minorversion = cl_init->minorversion;
79 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
80 return clp;
81
82error:
Trond Myklebust7653f6f2012-08-20 12:12:29 -040083 nfs_free_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -040084 return ERR_PTR(err);
85}
86
87/*
88 * Destroy the NFS4 callback service
89 */
90static void nfs4_destroy_callback(struct nfs_client *clp)
91{
92 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040093 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
Bryan Schumakerec409892012-07-16 16:39:21 -040094}
95
96static void nfs4_shutdown_client(struct nfs_client *clp)
97{
98 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
99 nfs4_kill_renewd(clp);
100 nfs4_shutdown_session(clp);
101 nfs4_destroy_callback(clp);
102 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
103 nfs_idmap_delete(clp);
104
105 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
106 kfree(clp->cl_serverowner);
107 kfree(clp->cl_serverscope);
108 kfree(clp->cl_implid);
109}
110
111void nfs4_free_client(struct nfs_client *clp)
112{
113 nfs4_shutdown_client(clp);
114 nfs_free_client(clp);
115}
116
117/*
Bryan Schumaker428360d2012-07-16 16:39:17 -0400118 * Initialize the NFS4 callback service
119 */
120static int nfs4_init_callback(struct nfs_client *clp)
121{
122 int error;
123
124 if (clp->rpc_ops->version == 4) {
125 struct rpc_xprt *xprt;
126
127 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
128
129 if (nfs4_has_session(clp)) {
130 error = xprt_setup_backchannel(xprt,
131 NFS41_BC_MIN_CALLBACKS);
132 if (error < 0)
133 return error;
134 }
135
136 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
137 if (error < 0) {
138 dprintk("%s: failed to start callback. Error = %d\n",
139 __func__, error);
140 return error;
141 }
142 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
143 }
144 return 0;
145}
146
147/*
148 * Initialize the minor version specific parts of an NFS4 client record
149 */
150static int nfs4_init_client_minor_version(struct nfs_client *clp)
151{
152#if defined(CONFIG_NFS_V4_1)
153 if (clp->cl_mvops->minor_version) {
154 struct nfs4_session *session = NULL;
155 /*
156 * Create the session and mark it expired.
157 * When a SEQUENCE operation encounters the expired session
158 * it will do session recovery to initialize it.
159 */
160 session = nfs4_alloc_session(clp);
161 if (!session)
162 return -ENOMEM;
163
164 clp->cl_session = session;
165 /*
166 * The create session reply races with the server back
167 * channel probe. Mark the client NFS_CS_SESSION_INITING
168 * so that the client back channel can find the
169 * nfs_client struct
170 */
171 nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
172 }
173#endif /* CONFIG_NFS_V4_1 */
174
175 return nfs4_init_callback(clp);
176}
177
178/**
179 * nfs4_init_client - Initialise an NFS4 client record
180 *
181 * @clp: nfs_client to initialise
182 * @timeparms: timeout parameters for underlying RPC transport
183 * @ip_addr: callback IP address in presentation format
184 * @authflavor: authentication flavor for underlying RPC transport
185 *
186 * Returns pointer to an NFS client, or an ERR_PTR value.
187 */
188struct nfs_client *nfs4_init_client(struct nfs_client *clp,
189 const struct rpc_timeout *timeparms,
Andy Adamsonf8407292013-07-24 11:59:49 -0400190 const char *ip_addr)
Bryan Schumaker428360d2012-07-16 16:39:17 -0400191{
192 char buf[INET6_ADDRSTRLEN + 1];
Chuck Lever05f4c352012-09-14 17:24:32 -0400193 struct nfs_client *old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400194 int error;
195
196 if (clp->cl_cons_state == NFS_CS_READY) {
197 /* the client is initialised already */
198 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
199 return clp;
200 }
201
202 /* Check NFS protocol revision and initialize RPC op vector */
203 clp->rpc_ops = &nfs_v4_clientops;
204
Trond Myklebust98f98cf2013-04-14 11:49:51 -0400205 if (clp->cl_minorversion != 0)
206 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400207 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
Chuck Lever4edaa302013-03-16 15:56:20 -0400208 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
Trond Myklebust23631222013-04-04 16:14:11 -0400209 if (error == -EINVAL)
Chuck Lever83c168b2013-05-15 22:00:10 -0400210 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400211 if (error < 0)
212 goto error;
213
214 /* If no clientaddr= option was specified, find a usable cb address */
215 if (ip_addr == NULL) {
216 struct sockaddr_storage cb_addr;
217 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
218
219 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
220 if (error < 0)
221 goto error;
222 error = rpc_ntop(sap, buf, sizeof(buf));
223 if (error < 0)
224 goto error;
225 ip_addr = (const char *)buf;
226 }
227 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
228
229 error = nfs_idmap_new(clp);
230 if (error < 0) {
231 dprintk("%s: failed to create idmapper. Error = %d\n",
232 __func__, error);
233 goto error;
234 }
235 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
236
237 error = nfs4_init_client_minor_version(clp);
238 if (error < 0)
239 goto error;
240
241 if (!nfs4_has_session(clp))
242 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever05f4c352012-09-14 17:24:32 -0400243
244 error = nfs4_discover_server_trunking(clp, &old);
245 if (error < 0)
246 goto error;
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500247 nfs_put_client(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400248 if (clp != old) {
249 clp->cl_preserve_clid = true;
Chuck Lever05f4c352012-09-14 17:24:32 -0400250 clp = old;
Chuck Lever05f4c352012-09-14 17:24:32 -0400251 }
252
Bryan Schumaker428360d2012-07-16 16:39:17 -0400253 return clp;
254
255error:
256 nfs_mark_client_ready(clp, error);
257 nfs_put_client(clp);
258 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
259 return ERR_PTR(error);
260}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400261
Chuck Lever05f4c352012-09-14 17:24:32 -0400262/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400263 * SETCLIENTID just did a callback update with the callback ident in
264 * "drop," but server trunking discovery claims "drop" and "keep" are
265 * actually the same server. Swap the callback IDs so that "keep"
266 * will continue to use the callback ident the server now knows about,
267 * and so that "keep"'s original callback ident is destroyed when
268 * "drop" is freed.
269 */
270static void nfs4_swap_callback_idents(struct nfs_client *keep,
271 struct nfs_client *drop)
272{
273 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
274 unsigned int save = keep->cl_cb_ident;
275
276 if (keep->cl_cb_ident == drop->cl_cb_ident)
277 return;
278
279 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
280 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
281
282 spin_lock(&nn->nfs_client_lock);
283
284 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
285 keep->cl_cb_ident = drop->cl_cb_ident;
286
287 idr_replace(&nn->cb_ident_idr, drop, save);
288 drop->cl_cb_ident = save;
289
290 spin_unlock(&nn->nfs_client_lock);
291}
292
293/**
294 * nfs40_walk_client_list - Find server that recognizes a client ID
295 *
296 * @new: nfs_client with client ID to test
297 * @result: OUT: found nfs_client, or new
298 * @cred: credential to use for trunking test
299 *
300 * Returns zero, a negative errno, or a negative NFS4ERR status.
301 * If zero is returned, an nfs_client pointer is planted in "result."
302 *
303 * NB: nfs40_walk_client_list() relies on the new nfs_client being
304 * the last nfs_client on the list.
305 */
306int nfs40_walk_client_list(struct nfs_client *new,
307 struct nfs_client **result,
308 struct rpc_cred *cred)
309{
310 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400311 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400312 struct nfs4_setclientid_res clid = {
313 .clientid = new->cl_clientid,
314 .confirm = new->cl_confirm,
315 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500316 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400317
318 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400319 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400320 /* If "pos" isn't marked ready, we can't trust the
321 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400322 if (pos->cl_cons_state > NFS_CS_READY) {
323 atomic_inc(&pos->cl_count);
324 spin_unlock(&nn->nfs_client_lock);
325
326 if (prev)
327 nfs_put_client(prev);
328 prev = pos;
329
330 status = nfs_wait_client_init_complete(pos);
331 spin_lock(&nn->nfs_client_lock);
332 if (status < 0)
333 continue;
334 }
335 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400336 continue;
337
338 if (pos->rpc_ops != new->rpc_ops)
339 continue;
340
341 if (pos->cl_proto != new->cl_proto)
342 continue;
343
344 if (pos->cl_minorversion != new->cl_minorversion)
345 continue;
346
347 if (pos->cl_clientid != new->cl_clientid)
348 continue;
349
350 atomic_inc(&pos->cl_count);
351 spin_unlock(&nn->nfs_client_lock);
352
353 if (prev)
354 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500355 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400356
357 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500358 switch (status) {
359 case -NFS4ERR_STALE_CLIENTID:
360 break;
361 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400362 nfs4_swap_callback_idents(pos, new);
363
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500364 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400365 *result = pos;
366 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
367 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500368 default:
369 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400370 }
371
372 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400373 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500374 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400375
Trond Myklebust202c3122013-01-18 22:56:23 -0500376 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500377out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400378 if (prev)
379 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500380 dprintk("NFS: <-- %s status = %d\n", __func__, status);
381 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400382}
383
384#ifdef CONFIG_NFS_V4_1
385/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700386 * Returns true if the client IDs match
387 */
388static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
389{
390 if (a->cl_clientid != b->cl_clientid) {
391 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
392 __func__, a->cl_clientid, b->cl_clientid);
393 return false;
394 }
395 dprintk("NFS: --> %s client ID %llx matches %llx\n",
396 __func__, a->cl_clientid, b->cl_clientid);
397 return true;
398}
399
400/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400401 * Returns true if the server owners match
402 */
403static bool
404nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
405{
406 struct nfs41_server_owner *o1 = a->cl_serverowner;
407 struct nfs41_server_owner *o2 = b->cl_serverowner;
408
409 if (o1->minor_id != o2->minor_id) {
410 dprintk("NFS: --> %s server owner minor IDs do not match\n",
411 __func__);
412 return false;
413 }
414
415 if (o1->major_id_sz != o2->major_id_sz)
416 goto out_major_mismatch;
417 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
418 goto out_major_mismatch;
419
420 dprintk("NFS: --> %s server owners match\n", __func__);
421 return true;
422
423out_major_mismatch:
424 dprintk("NFS: --> %s server owner major IDs do not match\n",
425 __func__);
426 return false;
427}
428
429/**
430 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
431 *
432 * @new: nfs_client with client ID to test
433 * @result: OUT: found nfs_client, or new
434 * @cred: credential to use for trunking test
435 *
436 * Returns zero, a negative errno, or a negative NFS4ERR status.
437 * If zero is returned, an nfs_client pointer is planted in "result."
438 *
439 * NB: nfs41_walk_client_list() relies on the new nfs_client being
440 * the last nfs_client on the list.
441 */
442int nfs41_walk_client_list(struct nfs_client *new,
443 struct nfs_client **result,
444 struct rpc_cred *cred)
445{
446 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400447 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500448 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400449
450 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400451 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400452 /* If "pos" isn't marked ready, we can't trust the
453 * remaining fields in "pos", especially the client
454 * ID and serverowner fields. Wait for CREATE_SESSION
455 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400456 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400457 atomic_inc(&pos->cl_count);
458 spin_unlock(&nn->nfs_client_lock);
459
460 if (prev)
461 nfs_put_client(prev);
462 prev = pos;
463
Trond Myklebust202c3122013-01-18 22:56:23 -0500464 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400465 if (status == 0) {
466 nfs4_schedule_lease_recovery(pos);
467 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400468 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400469 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500470 if (status < 0)
471 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400472 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400473 if (pos->cl_cons_state != NFS_CS_READY)
474 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400475
476 if (pos->rpc_ops != new->rpc_ops)
477 continue;
478
479 if (pos->cl_proto != new->cl_proto)
480 continue;
481
482 if (pos->cl_minorversion != new->cl_minorversion)
483 continue;
484
485 if (!nfs4_match_clientids(pos, new))
486 continue;
487
488 if (!nfs4_match_serverowners(pos, new))
489 continue;
490
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500491 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400492 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400493 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400494 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
495 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400496 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400497 }
498
Trond Myklebust202c3122013-01-18 22:56:23 -0500499 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400500 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500501 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400502 if (prev)
503 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500504 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400505}
506#endif /* CONFIG_NFS_V4_1 */
507
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400508static void nfs4_destroy_server(struct nfs_server *server)
509{
510 nfs_server_return_all_delegations(server);
511 unset_pnfs_layoutdriver(server);
512 nfs4_purge_state_owners(server);
513}
514
515/*
516 * NFSv4.0 callback thread helper
517 *
518 * Find a client by callback identifier
519 */
520struct nfs_client *
521nfs4_find_client_ident(struct net *net, int cb_ident)
522{
523 struct nfs_client *clp;
524 struct nfs_net *nn = net_generic(net, nfs_net_id);
525
526 spin_lock(&nn->nfs_client_lock);
527 clp = idr_find(&nn->cb_ident_idr, cb_ident);
528 if (clp)
529 atomic_inc(&clp->cl_count);
530 spin_unlock(&nn->nfs_client_lock);
531 return clp;
532}
533
534#if defined(CONFIG_NFS_V4_1)
535/* Common match routine for v4.0 and v4.1 callback services */
536static bool nfs4_cb_match_client(const struct sockaddr *addr,
537 struct nfs_client *clp, u32 minorversion)
538{
539 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
540
541 /* Don't match clients that failed to initialise */
542 if (!(clp->cl_cons_state == NFS_CS_READY ||
543 clp->cl_cons_state == NFS_CS_SESSION_INITING))
544 return false;
545
546 smp_rmb();
547
548 /* Match the version and minorversion */
549 if (clp->rpc_ops->version != 4 ||
550 clp->cl_minorversion != minorversion)
551 return false;
552
553 /* Match only the IP address, not the port number */
554 if (!nfs_sockaddr_match_ipaddr(addr, clap))
555 return false;
556
557 return true;
558}
559
560/*
561 * NFSv4.1 callback thread helper
562 * For CB_COMPOUND calls, find a client by IP address, protocol version,
563 * minorversion, and sessionID
564 *
565 * Returns NULL if no such client
566 */
567struct nfs_client *
568nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400569 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400570{
571 struct nfs_client *clp;
572 struct nfs_net *nn = net_generic(net, nfs_net_id);
573
574 spin_lock(&nn->nfs_client_lock);
575 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400576 if (nfs4_cb_match_client(addr, clp, minorversion) == false)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400577 continue;
578
579 if (!nfs4_has_session(clp))
580 continue;
581
582 /* Match sessionid*/
583 if (memcmp(clp->cl_session->sess_id.data,
584 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
585 continue;
586
587 atomic_inc(&clp->cl_count);
588 spin_unlock(&nn->nfs_client_lock);
589 return clp;
590 }
591 spin_unlock(&nn->nfs_client_lock);
592 return NULL;
593}
594
595#else /* CONFIG_NFS_V4_1 */
596
597struct nfs_client *
598nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400599 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400600{
601 return NULL;
602}
603#endif /* CONFIG_NFS_V4_1 */
604
605/*
606 * Set up an NFS4 client
607 */
608static int nfs4_set_client(struct nfs_server *server,
609 const char *hostname,
610 const struct sockaddr *addr,
611 const size_t addrlen,
612 const char *ip_addr,
613 rpc_authflavor_t authflavour,
614 int proto, const struct rpc_timeout *timeparms,
615 u32 minorversion, struct net *net)
616{
617 struct nfs_client_initdata cl_init = {
618 .hostname = hostname,
619 .addr = addr,
620 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400621 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400622 .proto = proto,
623 .minorversion = minorversion,
624 .net = net,
625 };
626 struct nfs_client *clp;
627 int error;
628
629 dprintk("--> nfs4_set_client()\n");
630
631 if (server->flags & NFS_MOUNT_NORESVPORT)
632 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Chuck Leverf112bb42013-06-25 12:23:27 -0400633 if (server->options & NFS_OPTION_MIGRATION)
634 set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400635
636 /* Allocate or find a client reference we can use */
637 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
638 if (IS_ERR(clp)) {
639 error = PTR_ERR(clp);
640 goto error;
641 }
642
643 /*
644 * Query for the lease time on clientid setup or renewal
645 *
646 * Note that this will be set on nfs_clients that were created
647 * only for the DS role and did not set this bit, but now will
648 * serve a dual role.
649 */
650 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
651
652 server->nfs_client = clp;
653 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
654 return 0;
655error:
656 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
657 return error;
658}
659
660/*
661 * Set up a pNFS Data Server client.
662 *
663 * Return any existing nfs_client that matches server address,port,version
664 * and minorversion.
665 *
666 * For a new nfs_client, use a soft mount (default), a low retrans and a
667 * low timeout interval so that if a connection is lost, we retry through
668 * the MDS.
669 */
670struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
671 const struct sockaddr *ds_addr, int ds_addrlen,
672 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
673{
674 struct nfs_client_initdata cl_init = {
675 .addr = ds_addr,
676 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400677 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400678 .proto = ds_proto,
679 .minorversion = mds_clp->cl_minorversion,
680 .net = mds_clp->cl_net,
681 };
682 struct rpc_timeout ds_timeout;
683 struct nfs_client *clp;
684
685 /*
686 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
687 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
688 * (section 13.1 RFC 5661).
689 */
690 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
691 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
692 mds_clp->cl_rpcclient->cl_auth->au_flavor);
693
694 dprintk("<-- %s %p\n", __func__, clp);
695 return clp;
696}
697EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
698
699/*
700 * Session has been established, and the client marked ready.
701 * Set the mount rsize and wsize with negotiated fore channel
702 * attributes which will be bound checked in nfs_server_set_fsinfo.
703 */
704static void nfs4_session_set_rwsize(struct nfs_server *server)
705{
706#ifdef CONFIG_NFS_V4_1
707 struct nfs4_session *sess;
708 u32 server_resp_sz;
709 u32 server_rqst_sz;
710
711 if (!nfs4_has_session(server->nfs_client))
712 return;
713 sess = server->nfs_client->cl_session;
714 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
715 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
716
717 if (server->rsize > server_resp_sz)
718 server->rsize = server_resp_sz;
719 if (server->wsize > server_rqst_sz)
720 server->wsize = server_rqst_sz;
721#endif /* CONFIG_NFS_V4_1 */
722}
723
724static int nfs4_server_common_setup(struct nfs_server *server,
725 struct nfs_fh *mntfh)
726{
727 struct nfs_fattr *fattr;
728 int error;
729
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400730 /* data servers support only a subset of NFSv4.1 */
731 if (is_ds_only_client(server->nfs_client))
732 return -EPROTONOSUPPORT;
733
734 fattr = nfs_alloc_fattr();
735 if (fattr == NULL)
736 return -ENOMEM;
737
738 /* We must ensure the session is initialised first */
Andy Adamson18aad3d2013-06-26 12:21:49 -0400739 error = nfs4_init_session(server->nfs_client);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400740 if (error < 0)
741 goto out;
742
Trond Myklebust39c6daa2013-03-15 16:11:57 -0400743 /* Set the basic capabilities */
744 server->caps |= server->nfs_client->cl_mvops->init_caps;
745 if (server->flags & NFS_MOUNT_NORDIRPLUS)
746 server->caps &= ~NFS_CAP_READDIRPLUS;
747 /*
748 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
749 * authentication.
750 */
751 if (nfs4_disable_idmapping &&
752 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
753 server->caps |= NFS_CAP_UIDGID_NOMAP;
754
755
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400756 /* Probe the root fh to retrieve its FSID and filehandle */
757 error = nfs4_get_rootfh(server, mntfh);
758 if (error < 0)
759 goto out;
760
761 dprintk("Server FSID: %llx:%llx\n",
762 (unsigned long long) server->fsid.major,
763 (unsigned long long) server->fsid.minor);
764 dprintk("Mount FH: %d\n", mntfh->size);
765
766 nfs4_session_set_rwsize(server);
767
768 error = nfs_probe_fsinfo(server, mntfh, fattr);
769 if (error < 0)
770 goto out;
771
772 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
773 server->namelen = NFS4_MAXNAMLEN;
774
775 nfs_server_insert_lists(server);
776 server->mount_time = jiffies;
777 server->destroy = nfs4_destroy_server;
778out:
779 nfs_free_fattr(fattr);
780 return error;
781}
782
783/*
784 * Create a version 4 volume record
785 */
786static int nfs4_init_server(struct nfs_server *server,
787 const struct nfs_parsed_mount_data *data)
788{
789 struct rpc_timeout timeparms;
790 int error;
791
792 dprintk("--> nfs4_init_server()\n");
793
794 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
795 data->timeo, data->retrans);
796
797 /* Initialise the client representation from the mount data */
798 server->flags = data->flags;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400799 server->options = data->options;
800
801 /* Get a client record */
802 error = nfs4_set_client(server,
803 data->nfs_server.hostname,
804 (const struct sockaddr *)&data->nfs_server.address,
805 data->nfs_server.addrlen,
806 data->client_address,
807 data->auth_flavors[0],
808 data->nfs_server.protocol,
809 &timeparms,
810 data->minorversion,
811 data->net);
812 if (error < 0)
813 goto error;
814
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400815 if (data->rsize)
816 server->rsize = nfs_block_size(data->rsize, NULL);
817 if (data->wsize)
818 server->wsize = nfs_block_size(data->wsize, NULL);
819
820 server->acregmin = data->acregmin * HZ;
821 server->acregmax = data->acregmax * HZ;
822 server->acdirmin = data->acdirmin * HZ;
823 server->acdirmax = data->acdirmax * HZ;
824
825 server->port = data->nfs_server.port;
826
827 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
828
829error:
830 /* Done */
831 dprintk("<-- nfs4_init_server() = %d\n", error);
832 return error;
833}
834
835/*
836 * Create a version 4 volume record
837 * - keyed on server and FSID
838 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400839/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
840 struct nfs_fh *mntfh)*/
841struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
842 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400843{
844 struct nfs_server *server;
845 int error;
846
847 dprintk("--> nfs4_create_server()\n");
848
849 server = nfs_alloc_server();
850 if (!server)
851 return ERR_PTR(-ENOMEM);
852
853 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400854 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400855 if (error < 0)
856 goto error;
857
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400858 error = nfs4_server_common_setup(server, mount_info->mntfh);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400859 if (error < 0)
860 goto error;
861
862 dprintk("<-- nfs4_create_server() = %p\n", server);
863 return server;
864
865error:
866 nfs_free_server(server);
867 dprintk("<-- nfs4_create_server() = error %d\n", error);
868 return ERR_PTR(error);
869}
870
871/*
872 * Create an NFS4 referral server record
873 */
874struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
875 struct nfs_fh *mntfh)
876{
877 struct nfs_client *parent_client;
878 struct nfs_server *server, *parent_server;
879 int error;
880
881 dprintk("--> nfs4_create_referral_server()\n");
882
883 server = nfs_alloc_server();
884 if (!server)
885 return ERR_PTR(-ENOMEM);
886
887 parent_server = NFS_SB(data->sb);
888 parent_client = parent_server->nfs_client;
889
890 /* Initialise the client representation from the parent server */
891 nfs_server_copy_userdata(server, parent_server);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400892
893 /* Get a client representation.
894 * Note: NFSv4 always uses TCP, */
895 error = nfs4_set_client(server, data->hostname,
896 data->addr,
897 data->addrlen,
898 parent_client->cl_ipaddr,
899 data->authflavor,
900 rpc_protocol(parent_server->client),
901 parent_server->client->cl_timeout,
902 parent_client->cl_mvops->minor_version,
903 parent_client->cl_net);
904 if (error < 0)
905 goto error;
906
907 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
908 if (error < 0)
909 goto error;
910
911 error = nfs4_server_common_setup(server, mntfh);
912 if (error < 0)
913 goto error;
914
915 dprintk("<-- nfs_create_referral_server() = %p\n", server);
916 return server;
917
918error:
919 nfs_free_server(server);
920 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
921 return ERR_PTR(error);
922}