blob: 90dce91dd5b5c7aa61a7a17c34242619bbfc5b8d [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,
190 const char *ip_addr,
191 rpc_authflavor_t authflavour)
192{
193 char buf[INET6_ADDRSTRLEN + 1];
Chuck Lever05f4c352012-09-14 17:24:32 -0400194 struct nfs_client *old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400195 int error;
196
197 if (clp->cl_cons_state == NFS_CS_READY) {
198 /* the client is initialised already */
199 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
200 return clp;
201 }
202
203 /* Check NFS protocol revision and initialize RPC op vector */
204 clp->rpc_ops = &nfs_v4_clientops;
205
Trond Myklebust98f98cf2013-04-14 11:49:51 -0400206 if (clp->cl_minorversion != 0)
207 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400208 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
Chuck Lever4edaa302013-03-16 15:56:20 -0400209 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
Trond Myklebust23631222013-04-04 16:14:11 -0400210 if (error == -EINVAL)
Chuck Lever83c168b2013-05-15 22:00:10 -0400211 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400212 if (error < 0)
213 goto error;
214
215 /* If no clientaddr= option was specified, find a usable cb address */
216 if (ip_addr == NULL) {
217 struct sockaddr_storage cb_addr;
218 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
219
220 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
221 if (error < 0)
222 goto error;
223 error = rpc_ntop(sap, buf, sizeof(buf));
224 if (error < 0)
225 goto error;
226 ip_addr = (const char *)buf;
227 }
228 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
229
230 error = nfs_idmap_new(clp);
231 if (error < 0) {
232 dprintk("%s: failed to create idmapper. Error = %d\n",
233 __func__, error);
234 goto error;
235 }
236 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
237
238 error = nfs4_init_client_minor_version(clp);
239 if (error < 0)
240 goto error;
241
242 if (!nfs4_has_session(clp))
243 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever05f4c352012-09-14 17:24:32 -0400244
245 error = nfs4_discover_server_trunking(clp, &old);
246 if (error < 0)
247 goto error;
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500248 nfs_put_client(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400249 if (clp != old) {
250 clp->cl_preserve_clid = true;
Chuck Lever05f4c352012-09-14 17:24:32 -0400251 clp = old;
Chuck Lever05f4c352012-09-14 17:24:32 -0400252 }
253
Bryan Schumaker428360d2012-07-16 16:39:17 -0400254 return clp;
255
256error:
257 nfs_mark_client_ready(clp, error);
258 nfs_put_client(clp);
259 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
260 return ERR_PTR(error);
261}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400262
Chuck Lever05f4c352012-09-14 17:24:32 -0400263/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400264 * SETCLIENTID just did a callback update with the callback ident in
265 * "drop," but server trunking discovery claims "drop" and "keep" are
266 * actually the same server. Swap the callback IDs so that "keep"
267 * will continue to use the callback ident the server now knows about,
268 * and so that "keep"'s original callback ident is destroyed when
269 * "drop" is freed.
270 */
271static void nfs4_swap_callback_idents(struct nfs_client *keep,
272 struct nfs_client *drop)
273{
274 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
275 unsigned int save = keep->cl_cb_ident;
276
277 if (keep->cl_cb_ident == drop->cl_cb_ident)
278 return;
279
280 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
281 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
282
283 spin_lock(&nn->nfs_client_lock);
284
285 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
286 keep->cl_cb_ident = drop->cl_cb_ident;
287
288 idr_replace(&nn->cb_ident_idr, drop, save);
289 drop->cl_cb_ident = save;
290
291 spin_unlock(&nn->nfs_client_lock);
292}
293
294/**
295 * nfs40_walk_client_list - Find server that recognizes a client ID
296 *
297 * @new: nfs_client with client ID to test
298 * @result: OUT: found nfs_client, or new
299 * @cred: credential to use for trunking test
300 *
301 * Returns zero, a negative errno, or a negative NFS4ERR status.
302 * If zero is returned, an nfs_client pointer is planted in "result."
303 *
304 * NB: nfs40_walk_client_list() relies on the new nfs_client being
305 * the last nfs_client on the list.
306 */
307int nfs40_walk_client_list(struct nfs_client *new,
308 struct nfs_client **result,
309 struct rpc_cred *cred)
310{
311 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400312 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400313 struct nfs4_setclientid_res clid = {
314 .clientid = new->cl_clientid,
315 .confirm = new->cl_confirm,
316 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500317 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400318
319 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400320 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400321 /* If "pos" isn't marked ready, we can't trust the
322 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400323 if (pos->cl_cons_state > NFS_CS_READY) {
324 atomic_inc(&pos->cl_count);
325 spin_unlock(&nn->nfs_client_lock);
326
327 if (prev)
328 nfs_put_client(prev);
329 prev = pos;
330
331 status = nfs_wait_client_init_complete(pos);
332 spin_lock(&nn->nfs_client_lock);
333 if (status < 0)
334 continue;
335 }
336 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400337 continue;
338
339 if (pos->rpc_ops != new->rpc_ops)
340 continue;
341
342 if (pos->cl_proto != new->cl_proto)
343 continue;
344
345 if (pos->cl_minorversion != new->cl_minorversion)
346 continue;
347
348 if (pos->cl_clientid != new->cl_clientid)
349 continue;
350
351 atomic_inc(&pos->cl_count);
352 spin_unlock(&nn->nfs_client_lock);
353
354 if (prev)
355 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500356 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400357
358 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500359 switch (status) {
360 case -NFS4ERR_STALE_CLIENTID:
361 break;
362 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400363 nfs4_swap_callback_idents(pos, new);
364
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500365 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400366 *result = pos;
367 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
368 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500369 default:
370 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400371 }
372
373 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400374 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500375 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400376
Trond Myklebust202c3122013-01-18 22:56:23 -0500377 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500378out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400379 if (prev)
380 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500381 dprintk("NFS: <-- %s status = %d\n", __func__, status);
382 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400383}
384
385#ifdef CONFIG_NFS_V4_1
386/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700387 * Returns true if the client IDs match
388 */
389static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
390{
391 if (a->cl_clientid != b->cl_clientid) {
392 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
393 __func__, a->cl_clientid, b->cl_clientid);
394 return false;
395 }
396 dprintk("NFS: --> %s client ID %llx matches %llx\n",
397 __func__, a->cl_clientid, b->cl_clientid);
398 return true;
399}
400
401/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400402 * Returns true if the server owners match
403 */
404static bool
405nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
406{
407 struct nfs41_server_owner *o1 = a->cl_serverowner;
408 struct nfs41_server_owner *o2 = b->cl_serverowner;
409
410 if (o1->minor_id != o2->minor_id) {
411 dprintk("NFS: --> %s server owner minor IDs do not match\n",
412 __func__);
413 return false;
414 }
415
416 if (o1->major_id_sz != o2->major_id_sz)
417 goto out_major_mismatch;
418 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
419 goto out_major_mismatch;
420
421 dprintk("NFS: --> %s server owners match\n", __func__);
422 return true;
423
424out_major_mismatch:
425 dprintk("NFS: --> %s server owner major IDs do not match\n",
426 __func__);
427 return false;
428}
429
430/**
431 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
432 *
433 * @new: nfs_client with client ID to test
434 * @result: OUT: found nfs_client, or new
435 * @cred: credential to use for trunking test
436 *
437 * Returns zero, a negative errno, or a negative NFS4ERR status.
438 * If zero is returned, an nfs_client pointer is planted in "result."
439 *
440 * NB: nfs41_walk_client_list() relies on the new nfs_client being
441 * the last nfs_client on the list.
442 */
443int nfs41_walk_client_list(struct nfs_client *new,
444 struct nfs_client **result,
445 struct rpc_cred *cred)
446{
447 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400448 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500449 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400450
451 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400452 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400453 /* If "pos" isn't marked ready, we can't trust the
454 * remaining fields in "pos", especially the client
455 * ID and serverowner fields. Wait for CREATE_SESSION
456 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400457 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400458 atomic_inc(&pos->cl_count);
459 spin_unlock(&nn->nfs_client_lock);
460
461 if (prev)
462 nfs_put_client(prev);
463 prev = pos;
464
Trond Myklebust202c3122013-01-18 22:56:23 -0500465 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400466 if (status == 0) {
467 nfs4_schedule_lease_recovery(pos);
468 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400469 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400470 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500471 if (status < 0)
472 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400473 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400474 if (pos->cl_cons_state != NFS_CS_READY)
475 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400476
477 if (pos->rpc_ops != new->rpc_ops)
478 continue;
479
480 if (pos->cl_proto != new->cl_proto)
481 continue;
482
483 if (pos->cl_minorversion != new->cl_minorversion)
484 continue;
485
486 if (!nfs4_match_clientids(pos, new))
487 continue;
488
489 if (!nfs4_match_serverowners(pos, new))
490 continue;
491
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500492 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400493 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400494 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400495 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
496 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400497 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400498 }
499
Trond Myklebust202c3122013-01-18 22:56:23 -0500500 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400501 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500502 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400503 if (prev)
504 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500505 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400506}
507#endif /* CONFIG_NFS_V4_1 */
508
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400509static void nfs4_destroy_server(struct nfs_server *server)
510{
511 nfs_server_return_all_delegations(server);
512 unset_pnfs_layoutdriver(server);
513 nfs4_purge_state_owners(server);
514}
515
516/*
517 * NFSv4.0 callback thread helper
518 *
519 * Find a client by callback identifier
520 */
521struct nfs_client *
522nfs4_find_client_ident(struct net *net, int cb_ident)
523{
524 struct nfs_client *clp;
525 struct nfs_net *nn = net_generic(net, nfs_net_id);
526
527 spin_lock(&nn->nfs_client_lock);
528 clp = idr_find(&nn->cb_ident_idr, cb_ident);
529 if (clp)
530 atomic_inc(&clp->cl_count);
531 spin_unlock(&nn->nfs_client_lock);
532 return clp;
533}
534
535#if defined(CONFIG_NFS_V4_1)
536/* Common match routine for v4.0 and v4.1 callback services */
537static bool nfs4_cb_match_client(const struct sockaddr *addr,
538 struct nfs_client *clp, u32 minorversion)
539{
540 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
541
542 /* Don't match clients that failed to initialise */
543 if (!(clp->cl_cons_state == NFS_CS_READY ||
544 clp->cl_cons_state == NFS_CS_SESSION_INITING))
545 return false;
546
547 smp_rmb();
548
549 /* Match the version and minorversion */
550 if (clp->rpc_ops->version != 4 ||
551 clp->cl_minorversion != minorversion)
552 return false;
553
554 /* Match only the IP address, not the port number */
555 if (!nfs_sockaddr_match_ipaddr(addr, clap))
556 return false;
557
558 return true;
559}
560
561/*
562 * NFSv4.1 callback thread helper
563 * For CB_COMPOUND calls, find a client by IP address, protocol version,
564 * minorversion, and sessionID
565 *
566 * Returns NULL if no such client
567 */
568struct nfs_client *
569nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400570 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400571{
572 struct nfs_client *clp;
573 struct nfs_net *nn = net_generic(net, nfs_net_id);
574
575 spin_lock(&nn->nfs_client_lock);
576 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400577 if (nfs4_cb_match_client(addr, clp, minorversion) == false)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400578 continue;
579
580 if (!nfs4_has_session(clp))
581 continue;
582
583 /* Match sessionid*/
584 if (memcmp(clp->cl_session->sess_id.data,
585 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
586 continue;
587
588 atomic_inc(&clp->cl_count);
589 spin_unlock(&nn->nfs_client_lock);
590 return clp;
591 }
592 spin_unlock(&nn->nfs_client_lock);
593 return NULL;
594}
595
596#else /* CONFIG_NFS_V4_1 */
597
598struct nfs_client *
599nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400600 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400601{
602 return NULL;
603}
604#endif /* CONFIG_NFS_V4_1 */
605
606/*
607 * Set up an NFS4 client
608 */
609static int nfs4_set_client(struct nfs_server *server,
610 const char *hostname,
611 const struct sockaddr *addr,
612 const size_t addrlen,
613 const char *ip_addr,
614 rpc_authflavor_t authflavour,
615 int proto, const struct rpc_timeout *timeparms,
616 u32 minorversion, struct net *net)
617{
618 struct nfs_client_initdata cl_init = {
619 .hostname = hostname,
620 .addr = addr,
621 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400622 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400623 .proto = proto,
624 .minorversion = minorversion,
625 .net = net,
626 };
627 struct nfs_client *clp;
628 int error;
629
630 dprintk("--> nfs4_set_client()\n");
631
632 if (server->flags & NFS_MOUNT_NORESVPORT)
633 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Chuck Leverf112bb42013-06-25 12:23:27 -0400634 if (server->options & NFS_OPTION_MIGRATION)
635 set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400636
637 /* Allocate or find a client reference we can use */
638 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
639 if (IS_ERR(clp)) {
640 error = PTR_ERR(clp);
641 goto error;
642 }
643
644 /*
645 * Query for the lease time on clientid setup or renewal
646 *
647 * Note that this will be set on nfs_clients that were created
648 * only for the DS role and did not set this bit, but now will
649 * serve a dual role.
650 */
651 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
652
653 server->nfs_client = clp;
654 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
655 return 0;
656error:
657 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
658 return error;
659}
660
661/*
662 * Set up a pNFS Data Server client.
663 *
664 * Return any existing nfs_client that matches server address,port,version
665 * and minorversion.
666 *
667 * For a new nfs_client, use a soft mount (default), a low retrans and a
668 * low timeout interval so that if a connection is lost, we retry through
669 * the MDS.
670 */
671struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
672 const struct sockaddr *ds_addr, int ds_addrlen,
673 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
674{
675 struct nfs_client_initdata cl_init = {
676 .addr = ds_addr,
677 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400678 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400679 .proto = ds_proto,
680 .minorversion = mds_clp->cl_minorversion,
681 .net = mds_clp->cl_net,
682 };
683 struct rpc_timeout ds_timeout;
684 struct nfs_client *clp;
685
686 /*
687 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
688 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
689 * (section 13.1 RFC 5661).
690 */
691 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
692 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
693 mds_clp->cl_rpcclient->cl_auth->au_flavor);
694
695 dprintk("<-- %s %p\n", __func__, clp);
696 return clp;
697}
698EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
699
700/*
701 * Session has been established, and the client marked ready.
702 * Set the mount rsize and wsize with negotiated fore channel
703 * attributes which will be bound checked in nfs_server_set_fsinfo.
704 */
705static void nfs4_session_set_rwsize(struct nfs_server *server)
706{
707#ifdef CONFIG_NFS_V4_1
708 struct nfs4_session *sess;
709 u32 server_resp_sz;
710 u32 server_rqst_sz;
711
712 if (!nfs4_has_session(server->nfs_client))
713 return;
714 sess = server->nfs_client->cl_session;
715 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
716 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
717
718 if (server->rsize > server_resp_sz)
719 server->rsize = server_resp_sz;
720 if (server->wsize > server_rqst_sz)
721 server->wsize = server_rqst_sz;
722#endif /* CONFIG_NFS_V4_1 */
723}
724
725static int nfs4_server_common_setup(struct nfs_server *server,
726 struct nfs_fh *mntfh)
727{
728 struct nfs_fattr *fattr;
729 int error;
730
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400731 /* data servers support only a subset of NFSv4.1 */
732 if (is_ds_only_client(server->nfs_client))
733 return -EPROTONOSUPPORT;
734
735 fattr = nfs_alloc_fattr();
736 if (fattr == NULL)
737 return -ENOMEM;
738
739 /* We must ensure the session is initialised first */
Andy Adamson18aad3d2013-06-26 12:21:49 -0400740 error = nfs4_init_session(server->nfs_client);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400741 if (error < 0)
742 goto out;
743
Trond Myklebust39c6daa2013-03-15 16:11:57 -0400744 /* Set the basic capabilities */
745 server->caps |= server->nfs_client->cl_mvops->init_caps;
746 if (server->flags & NFS_MOUNT_NORDIRPLUS)
747 server->caps &= ~NFS_CAP_READDIRPLUS;
748 /*
749 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
750 * authentication.
751 */
752 if (nfs4_disable_idmapping &&
753 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
754 server->caps |= NFS_CAP_UIDGID_NOMAP;
755
756
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400757 /* Probe the root fh to retrieve its FSID and filehandle */
758 error = nfs4_get_rootfh(server, mntfh);
759 if (error < 0)
760 goto out;
761
762 dprintk("Server FSID: %llx:%llx\n",
763 (unsigned long long) server->fsid.major,
764 (unsigned long long) server->fsid.minor);
765 dprintk("Mount FH: %d\n", mntfh->size);
766
767 nfs4_session_set_rwsize(server);
768
769 error = nfs_probe_fsinfo(server, mntfh, fattr);
770 if (error < 0)
771 goto out;
772
773 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
774 server->namelen = NFS4_MAXNAMLEN;
775
776 nfs_server_insert_lists(server);
777 server->mount_time = jiffies;
778 server->destroy = nfs4_destroy_server;
779out:
780 nfs_free_fattr(fattr);
781 return error;
782}
783
784/*
785 * Create a version 4 volume record
786 */
787static int nfs4_init_server(struct nfs_server *server,
788 const struct nfs_parsed_mount_data *data)
789{
790 struct rpc_timeout timeparms;
791 int error;
792
793 dprintk("--> nfs4_init_server()\n");
794
795 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
796 data->timeo, data->retrans);
797
798 /* Initialise the client representation from the mount data */
799 server->flags = data->flags;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400800 server->options = data->options;
801
802 /* Get a client record */
803 error = nfs4_set_client(server,
804 data->nfs_server.hostname,
805 (const struct sockaddr *)&data->nfs_server.address,
806 data->nfs_server.addrlen,
807 data->client_address,
808 data->auth_flavors[0],
809 data->nfs_server.protocol,
810 &timeparms,
811 data->minorversion,
812 data->net);
813 if (error < 0)
814 goto error;
815
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400816 if (data->rsize)
817 server->rsize = nfs_block_size(data->rsize, NULL);
818 if (data->wsize)
819 server->wsize = nfs_block_size(data->wsize, NULL);
820
821 server->acregmin = data->acregmin * HZ;
822 server->acregmax = data->acregmax * HZ;
823 server->acdirmin = data->acdirmin * HZ;
824 server->acdirmax = data->acdirmax * HZ;
825
826 server->port = data->nfs_server.port;
827
828 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
829
830error:
831 /* Done */
832 dprintk("<-- nfs4_init_server() = %d\n", error);
833 return error;
834}
835
836/*
837 * Create a version 4 volume record
838 * - keyed on server and FSID
839 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400840/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
841 struct nfs_fh *mntfh)*/
842struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
843 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400844{
845 struct nfs_server *server;
846 int error;
847
848 dprintk("--> nfs4_create_server()\n");
849
850 server = nfs_alloc_server();
851 if (!server)
852 return ERR_PTR(-ENOMEM);
853
854 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400855 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400856 if (error < 0)
857 goto error;
858
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400859 error = nfs4_server_common_setup(server, mount_info->mntfh);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400860 if (error < 0)
861 goto error;
862
863 dprintk("<-- nfs4_create_server() = %p\n", server);
864 return server;
865
866error:
867 nfs_free_server(server);
868 dprintk("<-- nfs4_create_server() = error %d\n", error);
869 return ERR_PTR(error);
870}
871
872/*
873 * Create an NFS4 referral server record
874 */
875struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
876 struct nfs_fh *mntfh)
877{
878 struct nfs_client *parent_client;
879 struct nfs_server *server, *parent_server;
880 int error;
881
882 dprintk("--> nfs4_create_referral_server()\n");
883
884 server = nfs_alloc_server();
885 if (!server)
886 return ERR_PTR(-ENOMEM);
887
888 parent_server = NFS_SB(data->sb);
889 parent_client = parent_server->nfs_client;
890
891 /* Initialise the client representation from the parent server */
892 nfs_server_copy_userdata(server, parent_server);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400893
894 /* Get a client representation.
895 * Note: NFSv4 always uses TCP, */
896 error = nfs4_set_client(server, data->hostname,
897 data->addr,
898 data->addrlen,
899 parent_client->cl_ipaddr,
900 data->authflavor,
901 rpc_protocol(parent_server->client),
902 parent_server->client->cl_timeout,
903 parent_client->cl_mvops->minor_version,
904 parent_client->cl_net);
905 if (error < 0)
906 goto error;
907
908 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
909 if (error < 0)
910 goto error;
911
912 error = nfs4_server_common_setup(server, mntfh);
913 if (error < 0)
914 goto error;
915
916 dprintk("<-- nfs_create_referral_server() = %p\n", server);
917 return server;
918
919error:
920 nfs_free_server(server);
921 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
922 return ERR_PTR(error);
923}