blob: cc143ee7a56ed80695f93a1685d39aa1ed87f0f5 [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
Trond Myklebust98f98cf2013-04-14 11:49:51 -0400201 if (clp->cl_minorversion != 0)
202 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400203 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
Chuck Lever4edaa302013-03-16 15:56:20 -0400204 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
Trond Myklebust23631222013-04-04 16:14:11 -0400205 if (error == -EINVAL)
Chuck Lever83c168b2013-05-15 22:00:10 -0400206 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400207 if (error < 0)
208 goto error;
209
210 /* If no clientaddr= option was specified, find a usable cb address */
211 if (ip_addr == NULL) {
212 struct sockaddr_storage cb_addr;
213 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
214
215 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
216 if (error < 0)
217 goto error;
218 error = rpc_ntop(sap, buf, sizeof(buf));
219 if (error < 0)
220 goto error;
221 ip_addr = (const char *)buf;
222 }
223 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
224
225 error = nfs_idmap_new(clp);
226 if (error < 0) {
227 dprintk("%s: failed to create idmapper. Error = %d\n",
228 __func__, error);
229 goto error;
230 }
231 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
232
233 error = nfs4_init_client_minor_version(clp);
234 if (error < 0)
235 goto error;
236
237 if (!nfs4_has_session(clp))
238 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever05f4c352012-09-14 17:24:32 -0400239
240 error = nfs4_discover_server_trunking(clp, &old);
241 if (error < 0)
242 goto error;
Chuck Lever05f4c352012-09-14 17:24:32 -0400243
Weston Andros Adamsondaab6e72014-01-19 22:45:36 -0500244 if (clp != old)
245 clp->cl_preserve_clid = true;
246 nfs_put_client(clp);
247 return old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400248
249error:
250 nfs_mark_client_ready(clp, error);
251 nfs_put_client(clp);
252 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
253 return ERR_PTR(error);
254}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400255
Chuck Lever05f4c352012-09-14 17:24:32 -0400256/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400257 * SETCLIENTID just did a callback update with the callback ident in
258 * "drop," but server trunking discovery claims "drop" and "keep" are
259 * actually the same server. Swap the callback IDs so that "keep"
260 * will continue to use the callback ident the server now knows about,
261 * and so that "keep"'s original callback ident is destroyed when
262 * "drop" is freed.
263 */
264static void nfs4_swap_callback_idents(struct nfs_client *keep,
265 struct nfs_client *drop)
266{
267 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
268 unsigned int save = keep->cl_cb_ident;
269
270 if (keep->cl_cb_ident == drop->cl_cb_ident)
271 return;
272
273 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
274 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
275
276 spin_lock(&nn->nfs_client_lock);
277
278 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
279 keep->cl_cb_ident = drop->cl_cb_ident;
280
281 idr_replace(&nn->cb_ident_idr, drop, save);
282 drop->cl_cb_ident = save;
283
284 spin_unlock(&nn->nfs_client_lock);
285}
286
287/**
288 * nfs40_walk_client_list - Find server that recognizes a client ID
289 *
290 * @new: nfs_client with client ID to test
291 * @result: OUT: found nfs_client, or new
292 * @cred: credential to use for trunking test
293 *
294 * Returns zero, a negative errno, or a negative NFS4ERR status.
295 * If zero is returned, an nfs_client pointer is planted in "result."
296 *
297 * NB: nfs40_walk_client_list() relies on the new nfs_client being
298 * the last nfs_client on the list.
299 */
300int nfs40_walk_client_list(struct nfs_client *new,
301 struct nfs_client **result,
302 struct rpc_cred *cred)
303{
304 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400305 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400306 struct nfs4_setclientid_res clid = {
307 .clientid = new->cl_clientid,
308 .confirm = new->cl_confirm,
309 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500310 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400311
312 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400313 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Steve Dickson648d3332014-09-18 09:13:17 -0400314
315 if (pos->rpc_ops != new->rpc_ops)
316 continue;
317
318 if (pos->cl_proto != new->cl_proto)
319 continue;
320
321 if (pos->cl_minorversion != new->cl_minorversion)
322 continue;
323
Chuck Lever05f4c352012-09-14 17:24:32 -0400324 /* If "pos" isn't marked ready, we can't trust the
325 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400326 if (pos->cl_cons_state > NFS_CS_READY) {
327 atomic_inc(&pos->cl_count);
328 spin_unlock(&nn->nfs_client_lock);
329
330 if (prev)
331 nfs_put_client(prev);
332 prev = pos;
333
334 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400335 if (status < 0)
Trond Myklebust14ff66c2014-01-17 17:03:41 -0500336 goto out;
337 status = -NFS4ERR_STALE_CLIENTID;
338 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400339 }
340 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400341 continue;
342
Chuck Lever05f4c352012-09-14 17:24:32 -0400343 if (pos->cl_clientid != new->cl_clientid)
344 continue;
345
346 atomic_inc(&pos->cl_count);
347 spin_unlock(&nn->nfs_client_lock);
348
349 if (prev)
350 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500351 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400352
353 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500354 switch (status) {
355 case -NFS4ERR_STALE_CLIENTID:
356 break;
357 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400358 nfs4_swap_callback_idents(pos, new);
359
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500360 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400361 *result = pos;
362 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
363 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500364 default:
365 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400366 }
367
368 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400369 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500370 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400371
Trond Myklebust202c3122013-01-18 22:56:23 -0500372 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500373out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400374 if (prev)
375 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500376 dprintk("NFS: <-- %s status = %d\n", __func__, status);
377 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400378}
379
380#ifdef CONFIG_NFS_V4_1
381/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700382 * Returns true if the client IDs match
383 */
384static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
385{
386 if (a->cl_clientid != b->cl_clientid) {
387 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
388 __func__, a->cl_clientid, b->cl_clientid);
389 return false;
390 }
391 dprintk("NFS: --> %s client ID %llx matches %llx\n",
392 __func__, a->cl_clientid, b->cl_clientid);
393 return true;
394}
395
396/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400397 * Returns true if the server owners match
398 */
399static bool
400nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
401{
402 struct nfs41_server_owner *o1 = a->cl_serverowner;
403 struct nfs41_server_owner *o2 = b->cl_serverowner;
404
405 if (o1->minor_id != o2->minor_id) {
406 dprintk("NFS: --> %s server owner minor IDs do not match\n",
407 __func__);
408 return false;
409 }
410
411 if (o1->major_id_sz != o2->major_id_sz)
412 goto out_major_mismatch;
413 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
414 goto out_major_mismatch;
415
416 dprintk("NFS: --> %s server owners match\n", __func__);
417 return true;
418
419out_major_mismatch:
420 dprintk("NFS: --> %s server owner major IDs do not match\n",
421 __func__);
422 return false;
423}
424
425/**
426 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
427 *
428 * @new: nfs_client with client ID to test
429 * @result: OUT: found nfs_client, or new
430 * @cred: credential to use for trunking test
431 *
432 * Returns zero, a negative errno, or a negative NFS4ERR status.
433 * If zero is returned, an nfs_client pointer is planted in "result."
434 *
435 * NB: nfs41_walk_client_list() relies on the new nfs_client being
436 * the last nfs_client on the list.
437 */
438int nfs41_walk_client_list(struct nfs_client *new,
439 struct nfs_client **result,
440 struct rpc_cred *cred)
441{
442 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400443 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500444 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400445
446 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400447 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Steve Dickson648d3332014-09-18 09:13:17 -0400448
449 if (pos->rpc_ops != new->rpc_ops)
450 continue;
451
452 if (pos->cl_proto != new->cl_proto)
453 continue;
454
455 if (pos->cl_minorversion != new->cl_minorversion)
456 continue;
457
Chuck Lever05f4c352012-09-14 17:24:32 -0400458 /* If "pos" isn't marked ready, we can't trust the
459 * remaining fields in "pos", especially the client
460 * ID and serverowner fields. Wait for CREATE_SESSION
461 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400462 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400463 atomic_inc(&pos->cl_count);
464 spin_unlock(&nn->nfs_client_lock);
465
466 if (prev)
467 nfs_put_client(prev);
468 prev = pos;
469
Trond Myklebust202c3122013-01-18 22:56:23 -0500470 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400471 if (status == 0) {
472 nfs4_schedule_lease_recovery(pos);
473 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400474 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400475 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500476 if (status < 0)
Trond Myklebust14ff66c2014-01-17 17:03:41 -0500477 break;
478 status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400479 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400480 if (pos->cl_cons_state != NFS_CS_READY)
481 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400482
Chuck Lever05f4c352012-09-14 17:24:32 -0400483 if (!nfs4_match_clientids(pos, new))
484 continue;
485
486 if (!nfs4_match_serverowners(pos, new))
487 continue;
488
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500489 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400490 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400491 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400492 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
493 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400494 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400495 }
496
Trond Myklebust202c3122013-01-18 22:56:23 -0500497 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400498 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500499 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400500 if (prev)
501 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500502 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400503}
504#endif /* CONFIG_NFS_V4_1 */
505
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400506static void nfs4_destroy_server(struct nfs_server *server)
507{
508 nfs_server_return_all_delegations(server);
509 unset_pnfs_layoutdriver(server);
510 nfs4_purge_state_owners(server);
511}
512
513/*
514 * NFSv4.0 callback thread helper
515 *
516 * Find a client by callback identifier
517 */
518struct nfs_client *
519nfs4_find_client_ident(struct net *net, int cb_ident)
520{
521 struct nfs_client *clp;
522 struct nfs_net *nn = net_generic(net, nfs_net_id);
523
524 spin_lock(&nn->nfs_client_lock);
525 clp = idr_find(&nn->cb_ident_idr, cb_ident);
526 if (clp)
527 atomic_inc(&clp->cl_count);
528 spin_unlock(&nn->nfs_client_lock);
529 return clp;
530}
531
532#if defined(CONFIG_NFS_V4_1)
533/* Common match routine for v4.0 and v4.1 callback services */
534static bool nfs4_cb_match_client(const struct sockaddr *addr,
535 struct nfs_client *clp, u32 minorversion)
536{
537 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
538
539 /* Don't match clients that failed to initialise */
540 if (!(clp->cl_cons_state == NFS_CS_READY ||
541 clp->cl_cons_state == NFS_CS_SESSION_INITING))
542 return false;
543
544 smp_rmb();
545
546 /* Match the version and minorversion */
547 if (clp->rpc_ops->version != 4 ||
548 clp->cl_minorversion != minorversion)
549 return false;
550
551 /* Match only the IP address, not the port number */
552 if (!nfs_sockaddr_match_ipaddr(addr, clap))
553 return false;
554
555 return true;
556}
557
558/*
559 * NFSv4.1 callback thread helper
560 * For CB_COMPOUND calls, find a client by IP address, protocol version,
561 * minorversion, and sessionID
562 *
563 * Returns NULL if no such client
564 */
565struct nfs_client *
566nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
567 struct nfs4_sessionid *sid)
568{
569 struct nfs_client *clp;
570 struct nfs_net *nn = net_generic(net, nfs_net_id);
571
572 spin_lock(&nn->nfs_client_lock);
573 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
574 if (nfs4_cb_match_client(addr, clp, 1) == false)
575 continue;
576
577 if (!nfs4_has_session(clp))
578 continue;
579
580 /* Match sessionid*/
581 if (memcmp(clp->cl_session->sess_id.data,
582 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
583 continue;
584
585 atomic_inc(&clp->cl_count);
586 spin_unlock(&nn->nfs_client_lock);
587 return clp;
588 }
589 spin_unlock(&nn->nfs_client_lock);
590 return NULL;
591}
592
593#else /* CONFIG_NFS_V4_1 */
594
595struct nfs_client *
596nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
597 struct nfs4_sessionid *sid)
598{
599 return NULL;
600}
601#endif /* CONFIG_NFS_V4_1 */
602
603/*
604 * Set up an NFS4 client
605 */
606static int nfs4_set_client(struct nfs_server *server,
607 const char *hostname,
608 const struct sockaddr *addr,
609 const size_t addrlen,
610 const char *ip_addr,
611 rpc_authflavor_t authflavour,
612 int proto, const struct rpc_timeout *timeparms,
613 u32 minorversion, struct net *net)
614{
615 struct nfs_client_initdata cl_init = {
616 .hostname = hostname,
617 .addr = addr,
618 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400619 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400620 .proto = proto,
621 .minorversion = minorversion,
622 .net = net,
623 };
624 struct nfs_client *clp;
625 int error;
626
627 dprintk("--> nfs4_set_client()\n");
628
629 if (server->flags & NFS_MOUNT_NORESVPORT)
630 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
631
632 /* Allocate or find a client reference we can use */
633 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
634 if (IS_ERR(clp)) {
635 error = PTR_ERR(clp);
636 goto error;
637 }
638
639 /*
640 * Query for the lease time on clientid setup or renewal
641 *
642 * Note that this will be set on nfs_clients that were created
643 * only for the DS role and did not set this bit, but now will
644 * serve a dual role.
645 */
646 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
647
648 server->nfs_client = clp;
649 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
650 return 0;
651error:
652 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
653 return error;
654}
655
656/*
657 * Set up a pNFS Data Server client.
658 *
659 * Return any existing nfs_client that matches server address,port,version
660 * and minorversion.
661 *
662 * For a new nfs_client, use a soft mount (default), a low retrans and a
663 * low timeout interval so that if a connection is lost, we retry through
664 * the MDS.
665 */
666struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
667 const struct sockaddr *ds_addr, int ds_addrlen,
668 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
669{
670 struct nfs_client_initdata cl_init = {
671 .addr = ds_addr,
672 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400673 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400674 .proto = ds_proto,
675 .minorversion = mds_clp->cl_minorversion,
676 .net = mds_clp->cl_net,
677 };
678 struct rpc_timeout ds_timeout;
679 struct nfs_client *clp;
680
681 /*
682 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
683 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
684 * (section 13.1 RFC 5661).
685 */
686 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
687 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
688 mds_clp->cl_rpcclient->cl_auth->au_flavor);
689
690 dprintk("<-- %s %p\n", __func__, clp);
691 return clp;
692}
693EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
694
695/*
696 * Session has been established, and the client marked ready.
697 * Set the mount rsize and wsize with negotiated fore channel
698 * attributes which will be bound checked in nfs_server_set_fsinfo.
699 */
700static void nfs4_session_set_rwsize(struct nfs_server *server)
701{
702#ifdef CONFIG_NFS_V4_1
703 struct nfs4_session *sess;
704 u32 server_resp_sz;
705 u32 server_rqst_sz;
706
707 if (!nfs4_has_session(server->nfs_client))
708 return;
709 sess = server->nfs_client->cl_session;
710 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
711 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
712
713 if (server->rsize > server_resp_sz)
714 server->rsize = server_resp_sz;
715 if (server->wsize > server_rqst_sz)
716 server->wsize = server_rqst_sz;
717#endif /* CONFIG_NFS_V4_1 */
718}
719
720static int nfs4_server_common_setup(struct nfs_server *server,
721 struct nfs_fh *mntfh)
722{
723 struct nfs_fattr *fattr;
724 int error;
725
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400726 /* data servers support only a subset of NFSv4.1 */
727 if (is_ds_only_client(server->nfs_client))
728 return -EPROTONOSUPPORT;
729
730 fattr = nfs_alloc_fattr();
731 if (fattr == NULL)
732 return -ENOMEM;
733
734 /* We must ensure the session is initialised first */
735 error = nfs4_init_session(server);
736 if (error < 0)
737 goto out;
738
Trond Myklebust39c6daa2013-03-15 16:11:57 -0400739 /* Set the basic capabilities */
740 server->caps |= server->nfs_client->cl_mvops->init_caps;
741 if (server->flags & NFS_MOUNT_NORDIRPLUS)
742 server->caps &= ~NFS_CAP_READDIRPLUS;
743 /*
744 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
745 * authentication.
746 */
747 if (nfs4_disable_idmapping &&
748 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
749 server->caps |= NFS_CAP_UIDGID_NOMAP;
750
751
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400752 /* Probe the root fh to retrieve its FSID and filehandle */
753 error = nfs4_get_rootfh(server, mntfh);
754 if (error < 0)
755 goto out;
756
757 dprintk("Server FSID: %llx:%llx\n",
758 (unsigned long long) server->fsid.major,
759 (unsigned long long) server->fsid.minor);
760 dprintk("Mount FH: %d\n", mntfh->size);
761
762 nfs4_session_set_rwsize(server);
763
764 error = nfs_probe_fsinfo(server, mntfh, fattr);
765 if (error < 0)
766 goto out;
767
768 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
769 server->namelen = NFS4_MAXNAMLEN;
770
771 nfs_server_insert_lists(server);
772 server->mount_time = jiffies;
773 server->destroy = nfs4_destroy_server;
774out:
775 nfs_free_fattr(fattr);
776 return error;
777}
778
779/*
780 * Create a version 4 volume record
781 */
782static int nfs4_init_server(struct nfs_server *server,
783 const struct nfs_parsed_mount_data *data)
784{
785 struct rpc_timeout timeparms;
786 int error;
787
788 dprintk("--> nfs4_init_server()\n");
789
790 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
791 data->timeo, data->retrans);
792
793 /* Initialise the client representation from the mount data */
794 server->flags = data->flags;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400795 server->options = data->options;
796
797 /* Get a client record */
798 error = nfs4_set_client(server,
799 data->nfs_server.hostname,
800 (const struct sockaddr *)&data->nfs_server.address,
801 data->nfs_server.addrlen,
802 data->client_address,
803 data->auth_flavors[0],
804 data->nfs_server.protocol,
805 &timeparms,
806 data->minorversion,
807 data->net);
808 if (error < 0)
809 goto error;
810
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400811 if (data->rsize)
812 server->rsize = nfs_block_size(data->rsize, NULL);
813 if (data->wsize)
814 server->wsize = nfs_block_size(data->wsize, NULL);
815
816 server->acregmin = data->acregmin * HZ;
817 server->acregmax = data->acregmax * HZ;
818 server->acdirmin = data->acdirmin * HZ;
819 server->acdirmax = data->acdirmax * HZ;
820
821 server->port = data->nfs_server.port;
822
823 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
824
825error:
826 /* Done */
827 dprintk("<-- nfs4_init_server() = %d\n", error);
828 return error;
829}
830
831/*
832 * Create a version 4 volume record
833 * - keyed on server and FSID
834 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400835/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
836 struct nfs_fh *mntfh)*/
837struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
838 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400839{
840 struct nfs_server *server;
841 int error;
842
843 dprintk("--> nfs4_create_server()\n");
844
845 server = nfs_alloc_server();
846 if (!server)
847 return ERR_PTR(-ENOMEM);
848
849 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400850 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400851 if (error < 0)
852 goto error;
853
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400854 error = nfs4_server_common_setup(server, mount_info->mntfh);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400855 if (error < 0)
856 goto error;
857
858 dprintk("<-- nfs4_create_server() = %p\n", server);
859 return server;
860
861error:
862 nfs_free_server(server);
863 dprintk("<-- nfs4_create_server() = error %d\n", error);
864 return ERR_PTR(error);
865}
866
867/*
868 * Create an NFS4 referral server record
869 */
870struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
871 struct nfs_fh *mntfh)
872{
873 struct nfs_client *parent_client;
874 struct nfs_server *server, *parent_server;
875 int error;
876
877 dprintk("--> nfs4_create_referral_server()\n");
878
879 server = nfs_alloc_server();
880 if (!server)
881 return ERR_PTR(-ENOMEM);
882
883 parent_server = NFS_SB(data->sb);
884 parent_client = parent_server->nfs_client;
885
886 /* Initialise the client representation from the parent server */
887 nfs_server_copy_userdata(server, parent_server);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400888
889 /* Get a client representation.
890 * Note: NFSv4 always uses TCP, */
891 error = nfs4_set_client(server, data->hostname,
892 data->addr,
893 data->addrlen,
894 parent_client->cl_ipaddr,
895 data->authflavor,
896 rpc_protocol(parent_server->client),
897 parent_server->client->cl_timeout,
898 parent_client->cl_mvops->minor_version,
899 parent_client->cl_net);
900 if (error < 0)
901 goto error;
902
903 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
904 if (error < 0)
905 goto error;
906
907 error = nfs4_server_common_setup(server, mntfh);
908 if (error < 0)
909 goto error;
910
911 dprintk("<-- nfs_create_referral_server() = %p\n", server);
912 return server;
913
914error:
915 nfs_free_server(server);
916 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
917 return ERR_PTR(error);
918}