blob: 98c0104bb0c4d93f3bcf0501b5d3705c2d406cef [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
Chuck Leverabf79bb2013-08-09 12:49:11 -040044void nfs41_shutdown_client(struct nfs_client *clp)
Bryan Schumakerec409892012-07-16 16:39:21 -040045{
46 if (nfs4_has_session(clp)) {
47 nfs4_destroy_session(clp->cl_session);
48 nfs4_destroy_clientid(clp);
49 }
50
51}
Chuck Leverabf79bb2013-08-09 12:49:11 -040052#endif /* CONFIG_NFS_V4_1 */
53
54void nfs40_shutdown_client(struct nfs_client *clp)
Bryan Schumakerec409892012-07-16 16:39:21 -040055{
Chuck Leverabf79bb2013-08-09 12:49:11 -040056 if (clp->cl_slot_tbl) {
57 nfs4_release_slot_table(clp->cl_slot_tbl);
58 kfree(clp->cl_slot_tbl);
59 }
Bryan Schumakerec409892012-07-16 16:39:21 -040060}
Bryan Schumakerec409892012-07-16 16:39:21 -040061
62struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
63{
64 int err;
65 struct nfs_client *clp = nfs_alloc_client(cl_init);
66 if (IS_ERR(clp))
67 return clp;
68
69 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
70 if (err)
71 goto error;
72
Steve Dickson42c2c422013-05-22 12:50:38 -040073 if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
74 err = -EINVAL;
75 goto error;
76 }
77
Bryan Schumakerec409892012-07-16 16:39:21 -040078 spin_lock_init(&clp->cl_lock);
79 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
80 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
81 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
82 clp->cl_minorversion = cl_init->minorversion;
83 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
84 return clp;
85
86error:
Trond Myklebust7653f6f2012-08-20 12:12:29 -040087 nfs_free_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -040088 return ERR_PTR(err);
89}
90
91/*
92 * Destroy the NFS4 callback service
93 */
94static void nfs4_destroy_callback(struct nfs_client *clp)
95{
96 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Stanislav Kinsburskyc9465562012-08-20 18:00:16 +040097 nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
Bryan Schumakerec409892012-07-16 16:39:21 -040098}
99
100static void nfs4_shutdown_client(struct nfs_client *clp)
101{
102 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
103 nfs4_kill_renewd(clp);
Chuck Leverabf79bb2013-08-09 12:49:11 -0400104 clp->cl_mvops->shutdown_client(clp);
Bryan Schumakerec409892012-07-16 16:39:21 -0400105 nfs4_destroy_callback(clp);
106 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
107 nfs_idmap_delete(clp);
108
109 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
110 kfree(clp->cl_serverowner);
111 kfree(clp->cl_serverscope);
112 kfree(clp->cl_implid);
113}
114
115void nfs4_free_client(struct nfs_client *clp)
116{
117 nfs4_shutdown_client(clp);
118 nfs_free_client(clp);
119}
120
121/*
Bryan Schumaker428360d2012-07-16 16:39:17 -0400122 * Initialize the NFS4 callback service
123 */
124static int nfs4_init_callback(struct nfs_client *clp)
125{
126 int error;
127
128 if (clp->rpc_ops->version == 4) {
129 struct rpc_xprt *xprt;
130
131 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
132
133 if (nfs4_has_session(clp)) {
134 error = xprt_setup_backchannel(xprt,
135 NFS41_BC_MIN_CALLBACKS);
136 if (error < 0)
137 return error;
138 }
139
140 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
141 if (error < 0) {
142 dprintk("%s: failed to start callback. Error = %d\n",
143 __func__, error);
144 return error;
145 }
146 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
147 }
148 return 0;
149}
150
Chuck Leverabf79bb2013-08-09 12:49:11 -0400151/**
152 * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
153 * @clp - nfs_client to initialize
154 *
155 * Returns zero on success, or a negative errno if some error occurred.
156 */
157int nfs40_init_client(struct nfs_client *clp)
158{
159 struct nfs4_slot_table *tbl;
160 int ret;
161
162 tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
163 if (tbl == NULL)
164 return -ENOMEM;
165
166 ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
167 "NFSv4.0 transport Slot table");
168 if (ret) {
169 kfree(tbl);
170 return ret;
171 }
172
173 clp->cl_slot_tbl = tbl;
174 return 0;
175}
176
177#if defined(CONFIG_NFS_V4_1)
178
179/**
180 * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
181 * @clp - nfs_client to initialize
182 *
183 * Returns zero on success, or a negative errno if some error occurred.
184 */
185int nfs41_init_client(struct nfs_client *clp)
186{
187 struct nfs4_session *session = NULL;
188
189 /*
190 * Create the session and mark it expired.
191 * When a SEQUENCE operation encounters the expired session
192 * it will do session recovery to initialize it.
193 */
194 session = nfs4_alloc_session(clp);
195 if (!session)
196 return -ENOMEM;
197
198 clp->cl_session = session;
199
200 /*
201 * The create session reply races with the server back
202 * channel probe. Mark the client NFS_CS_SESSION_INITING
203 * so that the client back channel can find the
204 * nfs_client struct
205 */
206 nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
207 return 0;
208}
209
210#endif /* CONFIG_NFS_V4_1 */
211
Bryan Schumaker428360d2012-07-16 16:39:17 -0400212/*
213 * Initialize the minor version specific parts of an NFS4 client record
214 */
215static int nfs4_init_client_minor_version(struct nfs_client *clp)
216{
Chuck Leverabf79bb2013-08-09 12:49:11 -0400217 int ret;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400218
Chuck Leverabf79bb2013-08-09 12:49:11 -0400219 ret = clp->cl_mvops->init_client(clp);
220 if (ret)
221 return ret;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400222 return nfs4_init_callback(clp);
223}
224
225/**
226 * nfs4_init_client - Initialise an NFS4 client record
227 *
228 * @clp: nfs_client to initialise
229 * @timeparms: timeout parameters for underlying RPC transport
230 * @ip_addr: callback IP address in presentation format
231 * @authflavor: authentication flavor for underlying RPC transport
232 *
233 * Returns pointer to an NFS client, or an ERR_PTR value.
234 */
235struct nfs_client *nfs4_init_client(struct nfs_client *clp,
236 const struct rpc_timeout *timeparms,
Andy Adamsonf8407292013-07-24 11:59:49 -0400237 const char *ip_addr)
Bryan Schumaker428360d2012-07-16 16:39:17 -0400238{
239 char buf[INET6_ADDRSTRLEN + 1];
Chuck Lever05f4c352012-09-14 17:24:32 -0400240 struct nfs_client *old;
Bryan Schumaker428360d2012-07-16 16:39:17 -0400241 int error;
242
243 if (clp->cl_cons_state == NFS_CS_READY) {
244 /* the client is initialised already */
245 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
246 return clp;
247 }
248
249 /* Check NFS protocol revision and initialize RPC op vector */
250 clp->rpc_ops = &nfs_v4_clientops;
251
Trond Myklebust98f98cf2013-04-14 11:49:51 -0400252 if (clp->cl_minorversion != 0)
253 __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400254 __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
Chuck Lever4edaa302013-03-16 15:56:20 -0400255 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
Trond Myklebust23631222013-04-04 16:14:11 -0400256 if (error == -EINVAL)
Chuck Lever83c168b2013-05-15 22:00:10 -0400257 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
Bryan Schumaker428360d2012-07-16 16:39:17 -0400258 if (error < 0)
259 goto error;
260
261 /* If no clientaddr= option was specified, find a usable cb address */
262 if (ip_addr == NULL) {
263 struct sockaddr_storage cb_addr;
264 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
265
266 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
267 if (error < 0)
268 goto error;
269 error = rpc_ntop(sap, buf, sizeof(buf));
270 if (error < 0)
271 goto error;
272 ip_addr = (const char *)buf;
273 }
274 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
275
276 error = nfs_idmap_new(clp);
277 if (error < 0) {
278 dprintk("%s: failed to create idmapper. Error = %d\n",
279 __func__, error);
280 goto error;
281 }
282 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
283
284 error = nfs4_init_client_minor_version(clp);
285 if (error < 0)
286 goto error;
287
288 if (!nfs4_has_session(clp))
289 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever05f4c352012-09-14 17:24:32 -0400290
291 error = nfs4_discover_server_trunking(clp, &old);
292 if (error < 0)
293 goto error;
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500294 nfs_put_client(clp);
Chuck Lever05f4c352012-09-14 17:24:32 -0400295 if (clp != old) {
296 clp->cl_preserve_clid = true;
Chuck Lever05f4c352012-09-14 17:24:32 -0400297 clp = old;
Chuck Lever05f4c352012-09-14 17:24:32 -0400298 }
299
Bryan Schumaker428360d2012-07-16 16:39:17 -0400300 return clp;
301
302error:
303 nfs_mark_client_ready(clp, error);
304 nfs_put_client(clp);
305 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
306 return ERR_PTR(error);
307}
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400308
Chuck Lever05f4c352012-09-14 17:24:32 -0400309/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400310 * SETCLIENTID just did a callback update with the callback ident in
311 * "drop," but server trunking discovery claims "drop" and "keep" are
312 * actually the same server. Swap the callback IDs so that "keep"
313 * will continue to use the callback ident the server now knows about,
314 * and so that "keep"'s original callback ident is destroyed when
315 * "drop" is freed.
316 */
317static void nfs4_swap_callback_idents(struct nfs_client *keep,
318 struct nfs_client *drop)
319{
320 struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
321 unsigned int save = keep->cl_cb_ident;
322
323 if (keep->cl_cb_ident == drop->cl_cb_ident)
324 return;
325
326 dprintk("%s: keeping callback ident %u and dropping ident %u\n",
327 __func__, keep->cl_cb_ident, drop->cl_cb_ident);
328
329 spin_lock(&nn->nfs_client_lock);
330
331 idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
332 keep->cl_cb_ident = drop->cl_cb_ident;
333
334 idr_replace(&nn->cb_ident_idr, drop, save);
335 drop->cl_cb_ident = save;
336
337 spin_unlock(&nn->nfs_client_lock);
338}
339
340/**
341 * nfs40_walk_client_list - Find server that recognizes a client ID
342 *
343 * @new: nfs_client with client ID to test
344 * @result: OUT: found nfs_client, or new
345 * @cred: credential to use for trunking test
346 *
347 * Returns zero, a negative errno, or a negative NFS4ERR status.
348 * If zero is returned, an nfs_client pointer is planted in "result."
349 *
350 * NB: nfs40_walk_client_list() relies on the new nfs_client being
351 * the last nfs_client on the list.
352 */
353int nfs40_walk_client_list(struct nfs_client *new,
354 struct nfs_client **result,
355 struct rpc_cred *cred)
356{
357 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400358 struct nfs_client *pos, *prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400359 struct nfs4_setclientid_res clid = {
360 .clientid = new->cl_clientid,
361 .confirm = new->cl_confirm,
362 };
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500363 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400364
365 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400366 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400367 /* If "pos" isn't marked ready, we can't trust the
368 * remaining fields in "pos" */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400369 if (pos->cl_cons_state > NFS_CS_READY) {
370 atomic_inc(&pos->cl_count);
371 spin_unlock(&nn->nfs_client_lock);
372
373 if (prev)
374 nfs_put_client(prev);
375 prev = pos;
376
377 status = nfs_wait_client_init_complete(pos);
378 spin_lock(&nn->nfs_client_lock);
379 if (status < 0)
380 continue;
381 }
382 if (pos->cl_cons_state != NFS_CS_READY)
Chuck Lever05f4c352012-09-14 17:24:32 -0400383 continue;
384
385 if (pos->rpc_ops != new->rpc_ops)
386 continue;
387
388 if (pos->cl_proto != new->cl_proto)
389 continue;
390
391 if (pos->cl_minorversion != new->cl_minorversion)
392 continue;
393
394 if (pos->cl_clientid != new->cl_clientid)
395 continue;
396
397 atomic_inc(&pos->cl_count);
398 spin_unlock(&nn->nfs_client_lock);
399
400 if (prev)
401 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500402 prev = pos;
Chuck Lever05f4c352012-09-14 17:24:32 -0400403
404 status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500405 switch (status) {
406 case -NFS4ERR_STALE_CLIENTID:
407 break;
408 case 0:
Chuck Lever05f4c352012-09-14 17:24:32 -0400409 nfs4_swap_callback_idents(pos, new);
410
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500411 prev = NULL;
Chuck Lever05f4c352012-09-14 17:24:32 -0400412 *result = pos;
413 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
414 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500415 default:
416 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -0400417 }
418
419 spin_lock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400420 }
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500421 spin_unlock(&nn->nfs_client_lock);
Chuck Lever05f4c352012-09-14 17:24:32 -0400422
Trond Myklebust202c3122013-01-18 22:56:23 -0500423 /* No match found. The server lost our clientid */
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500424out:
Chuck Lever05f4c352012-09-14 17:24:32 -0400425 if (prev)
426 nfs_put_client(prev);
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500427 dprintk("NFS: <-- %s status = %d\n", __func__, status);
428 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400429}
430
431#ifdef CONFIG_NFS_V4_1
432/*
Trond Myklebustf9d640f2012-10-01 16:37:51 -0700433 * Returns true if the client IDs match
434 */
435static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
436{
437 if (a->cl_clientid != b->cl_clientid) {
438 dprintk("NFS: --> %s client ID %llx does not match %llx\n",
439 __func__, a->cl_clientid, b->cl_clientid);
440 return false;
441 }
442 dprintk("NFS: --> %s client ID %llx matches %llx\n",
443 __func__, a->cl_clientid, b->cl_clientid);
444 return true;
445}
446
447/*
Chuck Lever05f4c352012-09-14 17:24:32 -0400448 * Returns true if the server owners match
449 */
450static bool
451nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
452{
453 struct nfs41_server_owner *o1 = a->cl_serverowner;
454 struct nfs41_server_owner *o2 = b->cl_serverowner;
455
456 if (o1->minor_id != o2->minor_id) {
457 dprintk("NFS: --> %s server owner minor IDs do not match\n",
458 __func__);
459 return false;
460 }
461
462 if (o1->major_id_sz != o2->major_id_sz)
463 goto out_major_mismatch;
464 if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
465 goto out_major_mismatch;
466
467 dprintk("NFS: --> %s server owners match\n", __func__);
468 return true;
469
470out_major_mismatch:
471 dprintk("NFS: --> %s server owner major IDs do not match\n",
472 __func__);
473 return false;
474}
475
476/**
477 * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
478 *
479 * @new: nfs_client with client ID to test
480 * @result: OUT: found nfs_client, or new
481 * @cred: credential to use for trunking test
482 *
483 * Returns zero, a negative errno, or a negative NFS4ERR status.
484 * If zero is returned, an nfs_client pointer is planted in "result."
485 *
486 * NB: nfs41_walk_client_list() relies on the new nfs_client being
487 * the last nfs_client on the list.
488 */
489int nfs41_walk_client_list(struct nfs_client *new,
490 struct nfs_client **result,
491 struct rpc_cred *cred)
492{
493 struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400494 struct nfs_client *pos, *prev = NULL;
Trond Myklebust202c3122013-01-18 22:56:23 -0500495 int status = -NFS4ERR_STALE_CLIENTID;
Chuck Lever05f4c352012-09-14 17:24:32 -0400496
497 spin_lock(&nn->nfs_client_lock);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400498 list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400499 /* If "pos" isn't marked ready, we can't trust the
500 * remaining fields in "pos", especially the client
501 * ID and serverowner fields. Wait for CREATE_SESSION
502 * to finish. */
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400503 if (pos->cl_cons_state > NFS_CS_READY) {
Chuck Lever05f4c352012-09-14 17:24:32 -0400504 atomic_inc(&pos->cl_count);
505 spin_unlock(&nn->nfs_client_lock);
506
507 if (prev)
508 nfs_put_client(prev);
509 prev = pos;
510
Trond Myklebust202c3122013-01-18 22:56:23 -0500511 status = nfs_wait_client_init_complete(pos);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400512 if (status == 0) {
513 nfs4_schedule_lease_recovery(pos);
514 status = nfs4_wait_clnt_recover(pos);
Chuck Lever05f4c352012-09-14 17:24:32 -0400515 }
Chuck Lever05f4c352012-09-14 17:24:32 -0400516 spin_lock(&nn->nfs_client_lock);
Trond Myklebust65436ec2013-01-18 23:01:43 -0500517 if (status < 0)
518 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400519 }
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400520 if (pos->cl_cons_state != NFS_CS_READY)
521 continue;
Chuck Lever05f4c352012-09-14 17:24:32 -0400522
523 if (pos->rpc_ops != new->rpc_ops)
524 continue;
525
526 if (pos->cl_proto != new->cl_proto)
527 continue;
528
529 if (pos->cl_minorversion != new->cl_minorversion)
530 continue;
531
532 if (!nfs4_match_clientids(pos, new))
533 continue;
534
535 if (!nfs4_match_serverowners(pos, new))
536 continue;
537
Trond Myklebust4ae19c22013-01-18 22:41:53 -0500538 atomic_inc(&pos->cl_count);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400539 *result = pos;
Trond Myklebusteb04e0a2013-04-10 12:44:18 -0400540 status = 0;
Chuck Lever05f4c352012-09-14 17:24:32 -0400541 dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
542 __func__, pos, atomic_read(&pos->cl_count));
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400543 break;
Chuck Lever05f4c352012-09-14 17:24:32 -0400544 }
545
Trond Myklebust202c3122013-01-18 22:56:23 -0500546 /* No matching nfs_client found. */
Chuck Lever05f4c352012-09-14 17:24:32 -0400547 spin_unlock(&nn->nfs_client_lock);
Trond Myklebust202c3122013-01-18 22:56:23 -0500548 dprintk("NFS: <-- %s status = %d\n", __func__, status);
Trond Myklebust7b1f1fd2013-04-05 16:11:11 -0400549 if (prev)
550 nfs_put_client(prev);
Trond Myklebust202c3122013-01-18 22:56:23 -0500551 return status;
Chuck Lever05f4c352012-09-14 17:24:32 -0400552}
553#endif /* CONFIG_NFS_V4_1 */
554
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400555static void nfs4_destroy_server(struct nfs_server *server)
556{
557 nfs_server_return_all_delegations(server);
558 unset_pnfs_layoutdriver(server);
559 nfs4_purge_state_owners(server);
560}
561
562/*
563 * NFSv4.0 callback thread helper
564 *
565 * Find a client by callback identifier
566 */
567struct nfs_client *
568nfs4_find_client_ident(struct net *net, int cb_ident)
569{
570 struct nfs_client *clp;
571 struct nfs_net *nn = net_generic(net, nfs_net_id);
572
573 spin_lock(&nn->nfs_client_lock);
574 clp = idr_find(&nn->cb_ident_idr, cb_ident);
575 if (clp)
576 atomic_inc(&clp->cl_count);
577 spin_unlock(&nn->nfs_client_lock);
578 return clp;
579}
580
581#if defined(CONFIG_NFS_V4_1)
582/* Common match routine for v4.0 and v4.1 callback services */
583static bool nfs4_cb_match_client(const struct sockaddr *addr,
584 struct nfs_client *clp, u32 minorversion)
585{
586 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
587
588 /* Don't match clients that failed to initialise */
589 if (!(clp->cl_cons_state == NFS_CS_READY ||
590 clp->cl_cons_state == NFS_CS_SESSION_INITING))
591 return false;
592
593 smp_rmb();
594
595 /* Match the version and minorversion */
596 if (clp->rpc_ops->version != 4 ||
597 clp->cl_minorversion != minorversion)
598 return false;
599
600 /* Match only the IP address, not the port number */
601 if (!nfs_sockaddr_match_ipaddr(addr, clap))
602 return false;
603
604 return true;
605}
606
607/*
608 * NFSv4.1 callback thread helper
609 * For CB_COMPOUND calls, find a client by IP address, protocol version,
610 * minorversion, and sessionID
611 *
612 * Returns NULL if no such client
613 */
614struct nfs_client *
615nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400616 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400617{
618 struct nfs_client *clp;
619 struct nfs_net *nn = net_generic(net, nfs_net_id);
620
621 spin_lock(&nn->nfs_client_lock);
622 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400623 if (nfs4_cb_match_client(addr, clp, minorversion) == false)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400624 continue;
625
626 if (!nfs4_has_session(clp))
627 continue;
628
629 /* Match sessionid*/
630 if (memcmp(clp->cl_session->sess_id.data,
631 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
632 continue;
633
634 atomic_inc(&clp->cl_count);
635 spin_unlock(&nn->nfs_client_lock);
636 return clp;
637 }
638 spin_unlock(&nn->nfs_client_lock);
639 return NULL;
640}
641
642#else /* CONFIG_NFS_V4_1 */
643
644struct nfs_client *
645nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Bryan Schumaker459de2e2013-06-05 11:15:01 -0400646 struct nfs4_sessionid *sid, u32 minorversion)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400647{
648 return NULL;
649}
650#endif /* CONFIG_NFS_V4_1 */
651
652/*
653 * Set up an NFS4 client
654 */
655static int nfs4_set_client(struct nfs_server *server,
656 const char *hostname,
657 const struct sockaddr *addr,
658 const size_t addrlen,
659 const char *ip_addr,
660 rpc_authflavor_t authflavour,
661 int proto, const struct rpc_timeout *timeparms,
662 u32 minorversion, struct net *net)
663{
664 struct nfs_client_initdata cl_init = {
665 .hostname = hostname,
666 .addr = addr,
667 .addrlen = addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400668 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400669 .proto = proto,
670 .minorversion = minorversion,
671 .net = net,
672 };
673 struct nfs_client *clp;
674 int error;
675
676 dprintk("--> nfs4_set_client()\n");
677
678 if (server->flags & NFS_MOUNT_NORESVPORT)
679 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Chuck Leverf112bb42013-06-25 12:23:27 -0400680 if (server->options & NFS_OPTION_MIGRATION)
681 set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400682
683 /* Allocate or find a client reference we can use */
684 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
685 if (IS_ERR(clp)) {
686 error = PTR_ERR(clp);
687 goto error;
688 }
689
690 /*
691 * Query for the lease time on clientid setup or renewal
692 *
693 * Note that this will be set on nfs_clients that were created
694 * only for the DS role and did not set this bit, but now will
695 * serve a dual role.
696 */
697 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
698
699 server->nfs_client = clp;
700 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
701 return 0;
702error:
703 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
704 return error;
705}
706
707/*
708 * Set up a pNFS Data Server client.
709 *
710 * Return any existing nfs_client that matches server address,port,version
711 * and minorversion.
712 *
713 * For a new nfs_client, use a soft mount (default), a low retrans and a
714 * low timeout interval so that if a connection is lost, we retry through
715 * the MDS.
716 */
717struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
718 const struct sockaddr *ds_addr, int ds_addrlen,
719 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
720{
721 struct nfs_client_initdata cl_init = {
722 .addr = ds_addr,
723 .addrlen = ds_addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400724 .nfs_mod = &nfs_v4,
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400725 .proto = ds_proto,
726 .minorversion = mds_clp->cl_minorversion,
727 .net = mds_clp->cl_net,
728 };
729 struct rpc_timeout ds_timeout;
730 struct nfs_client *clp;
731
732 /*
733 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
734 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
735 * (section 13.1 RFC 5661).
736 */
737 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
738 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
739 mds_clp->cl_rpcclient->cl_auth->au_flavor);
740
741 dprintk("<-- %s %p\n", __func__, clp);
742 return clp;
743}
744EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
745
746/*
747 * Session has been established, and the client marked ready.
748 * Set the mount rsize and wsize with negotiated fore channel
749 * attributes which will be bound checked in nfs_server_set_fsinfo.
750 */
751static void nfs4_session_set_rwsize(struct nfs_server *server)
752{
753#ifdef CONFIG_NFS_V4_1
754 struct nfs4_session *sess;
755 u32 server_resp_sz;
756 u32 server_rqst_sz;
757
758 if (!nfs4_has_session(server->nfs_client))
759 return;
760 sess = server->nfs_client->cl_session;
761 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
762 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
763
764 if (server->rsize > server_resp_sz)
765 server->rsize = server_resp_sz;
766 if (server->wsize > server_rqst_sz)
767 server->wsize = server_rqst_sz;
768#endif /* CONFIG_NFS_V4_1 */
769}
770
771static int nfs4_server_common_setup(struct nfs_server *server,
772 struct nfs_fh *mntfh)
773{
774 struct nfs_fattr *fattr;
775 int error;
776
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400777 /* data servers support only a subset of NFSv4.1 */
778 if (is_ds_only_client(server->nfs_client))
779 return -EPROTONOSUPPORT;
780
781 fattr = nfs_alloc_fattr();
782 if (fattr == NULL)
783 return -ENOMEM;
784
785 /* We must ensure the session is initialised first */
Andy Adamson18aad3d2013-06-26 12:21:49 -0400786 error = nfs4_init_session(server->nfs_client);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400787 if (error < 0)
788 goto out;
789
Trond Myklebust39c6daa2013-03-15 16:11:57 -0400790 /* Set the basic capabilities */
791 server->caps |= server->nfs_client->cl_mvops->init_caps;
792 if (server->flags & NFS_MOUNT_NORDIRPLUS)
793 server->caps &= ~NFS_CAP_READDIRPLUS;
794 /*
795 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
796 * authentication.
797 */
798 if (nfs4_disable_idmapping &&
799 server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
800 server->caps |= NFS_CAP_UIDGID_NOMAP;
801
802
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400803 /* Probe the root fh to retrieve its FSID and filehandle */
804 error = nfs4_get_rootfh(server, mntfh);
805 if (error < 0)
806 goto out;
807
808 dprintk("Server FSID: %llx:%llx\n",
809 (unsigned long long) server->fsid.major,
810 (unsigned long long) server->fsid.minor);
811 dprintk("Mount FH: %d\n", mntfh->size);
812
813 nfs4_session_set_rwsize(server);
814
815 error = nfs_probe_fsinfo(server, mntfh, fattr);
816 if (error < 0)
817 goto out;
818
819 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
820 server->namelen = NFS4_MAXNAMLEN;
821
822 nfs_server_insert_lists(server);
823 server->mount_time = jiffies;
824 server->destroy = nfs4_destroy_server;
825out:
826 nfs_free_fattr(fattr);
827 return error;
828}
829
830/*
831 * Create a version 4 volume record
832 */
833static int nfs4_init_server(struct nfs_server *server,
834 const struct nfs_parsed_mount_data *data)
835{
836 struct rpc_timeout timeparms;
837 int error;
838
839 dprintk("--> nfs4_init_server()\n");
840
841 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
842 data->timeo, data->retrans);
843
844 /* Initialise the client representation from the mount data */
845 server->flags = data->flags;
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400846 server->options = data->options;
847
848 /* Get a client record */
849 error = nfs4_set_client(server,
850 data->nfs_server.hostname,
851 (const struct sockaddr *)&data->nfs_server.address,
852 data->nfs_server.addrlen,
853 data->client_address,
854 data->auth_flavors[0],
855 data->nfs_server.protocol,
856 &timeparms,
857 data->minorversion,
858 data->net);
859 if (error < 0)
860 goto error;
861
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400862 if (data->rsize)
863 server->rsize = nfs_block_size(data->rsize, NULL);
864 if (data->wsize)
865 server->wsize = nfs_block_size(data->wsize, NULL);
866
867 server->acregmin = data->acregmin * HZ;
868 server->acregmax = data->acregmax * HZ;
869 server->acdirmin = data->acdirmin * HZ;
870 server->acdirmax = data->acdirmax * HZ;
871
872 server->port = data->nfs_server.port;
873
874 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
875
876error:
877 /* Done */
878 dprintk("<-- nfs4_init_server() = %d\n", error);
879 return error;
880}
881
882/*
883 * Create a version 4 volume record
884 * - keyed on server and FSID
885 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400886/*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
887 struct nfs_fh *mntfh)*/
888struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
889 struct nfs_subversion *nfs_mod)
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400890{
891 struct nfs_server *server;
892 int error;
893
894 dprintk("--> nfs4_create_server()\n");
895
896 server = nfs_alloc_server();
897 if (!server)
898 return ERR_PTR(-ENOMEM);
899
900 /* set up the general RPC client */
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400901 error = nfs4_init_server(server, mount_info->parsed);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400902 if (error < 0)
903 goto error;
904
Bryan Schumaker1179acc2012-07-30 16:05:19 -0400905 error = nfs4_server_common_setup(server, mount_info->mntfh);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400906 if (error < 0)
907 goto error;
908
909 dprintk("<-- nfs4_create_server() = %p\n", server);
910 return server;
911
912error:
913 nfs_free_server(server);
914 dprintk("<-- nfs4_create_server() = error %d\n", error);
915 return ERR_PTR(error);
916}
917
918/*
919 * Create an NFS4 referral server record
920 */
921struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
922 struct nfs_fh *mntfh)
923{
924 struct nfs_client *parent_client;
925 struct nfs_server *server, *parent_server;
926 int error;
927
928 dprintk("--> nfs4_create_referral_server()\n");
929
930 server = nfs_alloc_server();
931 if (!server)
932 return ERR_PTR(-ENOMEM);
933
934 parent_server = NFS_SB(data->sb);
935 parent_client = parent_server->nfs_client;
936
937 /* Initialise the client representation from the parent server */
938 nfs_server_copy_userdata(server, parent_server);
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400939
940 /* Get a client representation.
941 * Note: NFSv4 always uses TCP, */
942 error = nfs4_set_client(server, data->hostname,
943 data->addr,
944 data->addrlen,
945 parent_client->cl_ipaddr,
946 data->authflavor,
947 rpc_protocol(parent_server->client),
948 parent_server->client->cl_timeout,
949 parent_client->cl_mvops->minor_version,
950 parent_client->cl_net);
951 if (error < 0)
952 goto error;
953
954 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
955 if (error < 0)
956 goto error;
957
958 error = nfs4_server_common_setup(server, mntfh);
959 if (error < 0)
960 goto error;
961
962 dprintk("<-- nfs_create_referral_server() = %p\n", server);
963 return server;
964
965error:
966 nfs_free_server(server);
967 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
968 return ERR_PTR(error);
969}