diff options
author | Bryan Schumaker <bjschuma@netapp.com> | 2012-07-16 16:39:18 -0400 |
---|---|---|
committer | Trond Myklebust <Trond.Myklebust@netapp.com> | 2012-07-17 13:33:53 -0400 |
commit | fcf10398f641c4450119f8a4cc27e9e584edb010 (patch) | |
tree | 6c102abc69d152affb678ef1ca80f67c409839e4 /fs/nfs/nfs4client.c | |
parent | 428360d77c801932e4b28f15160aebbdb5f5a03e (diff) |
NFS: Split out NFS v4 server creating code
These functions are specific to NFS v4 and can be moved to nfs4client.c
to keep them out of the generic client.
Signed-off-by: Bryan Schumaker <bjschuma@netapp.com>
Signed-off-by: Trond Myklebust <Trond.Myklebust@netapp.com>
Diffstat (limited to 'fs/nfs/nfs4client.c')
-rw-r--r-- | fs/nfs/nfs4client.c | 428 |
1 files changed, 428 insertions, 0 deletions
diff --git a/fs/nfs/nfs4client.c b/fs/nfs/nfs4client.c index c5234b589907..a71d95ecbea9 100644 --- a/fs/nfs/nfs4client.c +++ b/fs/nfs/nfs4client.c | |||
@@ -2,17 +2,27 @@ | |||
2 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. | 2 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. |
3 | * Written by David Howells (dhowells@redhat.com) | 3 | * Written by David Howells (dhowells@redhat.com) |
4 | */ | 4 | */ |
5 | #include <linux/module.h> | ||
5 | #include <linux/nfs_fs.h> | 6 | #include <linux/nfs_fs.h> |
6 | #include <linux/nfs_idmap.h> | 7 | #include <linux/nfs_idmap.h> |
8 | #include <linux/nfs_mount.h> | ||
7 | #include <linux/sunrpc/auth.h> | 9 | #include <linux/sunrpc/auth.h> |
8 | #include <linux/sunrpc/xprt.h> | 10 | #include <linux/sunrpc/xprt.h> |
9 | #include <linux/sunrpc/bc_xprt.h> | 11 | #include <linux/sunrpc/bc_xprt.h> |
10 | #include "internal.h" | 12 | #include "internal.h" |
11 | #include "callback.h" | 13 | #include "callback.h" |
14 | #include "delegation.h" | ||
15 | #include "pnfs.h" | ||
16 | #include "netns.h" | ||
12 | 17 | ||
13 | #define NFSDBG_FACILITY NFSDBG_CLIENT | 18 | #define NFSDBG_FACILITY NFSDBG_CLIENT |
14 | 19 | ||
15 | /* | 20 | /* |
21 | * Turn off NFSv4 uid/gid mapping when using AUTH_SYS | ||
22 | */ | ||
23 | static bool nfs4_disable_idmapping = true; | ||
24 | |||
25 | /* | ||
16 | * Initialize the NFS4 callback service | 26 | * Initialize the NFS4 callback service |
17 | */ | 27 | */ |
18 | static int nfs4_init_callback(struct nfs_client *clp) | 28 | static int nfs4_init_callback(struct nfs_client *clp) |
@@ -142,3 +152,421 @@ error: | |||
142 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); | 152 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); |
143 | return ERR_PTR(error); | 153 | return ERR_PTR(error); |
144 | } | 154 | } |
155 | |||
156 | static void nfs4_destroy_server(struct nfs_server *server) | ||
157 | { | ||
158 | nfs_server_return_all_delegations(server); | ||
159 | unset_pnfs_layoutdriver(server); | ||
160 | nfs4_purge_state_owners(server); | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * NFSv4.0 callback thread helper | ||
165 | * | ||
166 | * Find a client by callback identifier | ||
167 | */ | ||
168 | struct nfs_client * | ||
169 | nfs4_find_client_ident(struct net *net, int cb_ident) | ||
170 | { | ||
171 | struct nfs_client *clp; | ||
172 | struct nfs_net *nn = net_generic(net, nfs_net_id); | ||
173 | |||
174 | spin_lock(&nn->nfs_client_lock); | ||
175 | clp = idr_find(&nn->cb_ident_idr, cb_ident); | ||
176 | if (clp) | ||
177 | atomic_inc(&clp->cl_count); | ||
178 | spin_unlock(&nn->nfs_client_lock); | ||
179 | return clp; | ||
180 | } | ||
181 | |||
182 | #if defined(CONFIG_NFS_V4_1) | ||
183 | /* Common match routine for v4.0 and v4.1 callback services */ | ||
184 | static bool nfs4_cb_match_client(const struct sockaddr *addr, | ||
185 | struct nfs_client *clp, u32 minorversion) | ||
186 | { | ||
187 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; | ||
188 | |||
189 | /* Don't match clients that failed to initialise */ | ||
190 | if (!(clp->cl_cons_state == NFS_CS_READY || | ||
191 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) | ||
192 | return false; | ||
193 | |||
194 | smp_rmb(); | ||
195 | |||
196 | /* Match the version and minorversion */ | ||
197 | if (clp->rpc_ops->version != 4 || | ||
198 | clp->cl_minorversion != minorversion) | ||
199 | return false; | ||
200 | |||
201 | /* Match only the IP address, not the port number */ | ||
202 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) | ||
203 | return false; | ||
204 | |||
205 | return true; | ||
206 | } | ||
207 | |||
208 | /* | ||
209 | * NFSv4.1 callback thread helper | ||
210 | * For CB_COMPOUND calls, find a client by IP address, protocol version, | ||
211 | * minorversion, and sessionID | ||
212 | * | ||
213 | * Returns NULL if no such client | ||
214 | */ | ||
215 | struct nfs_client * | ||
216 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | ||
217 | struct nfs4_sessionid *sid) | ||
218 | { | ||
219 | struct nfs_client *clp; | ||
220 | struct nfs_net *nn = net_generic(net, nfs_net_id); | ||
221 | |||
222 | spin_lock(&nn->nfs_client_lock); | ||
223 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { | ||
224 | if (nfs4_cb_match_client(addr, clp, 1) == false) | ||
225 | continue; | ||
226 | |||
227 | if (!nfs4_has_session(clp)) | ||
228 | continue; | ||
229 | |||
230 | /* Match sessionid*/ | ||
231 | if (memcmp(clp->cl_session->sess_id.data, | ||
232 | sid->data, NFS4_MAX_SESSIONID_LEN) != 0) | ||
233 | continue; | ||
234 | |||
235 | atomic_inc(&clp->cl_count); | ||
236 | spin_unlock(&nn->nfs_client_lock); | ||
237 | return clp; | ||
238 | } | ||
239 | spin_unlock(&nn->nfs_client_lock); | ||
240 | return NULL; | ||
241 | } | ||
242 | |||
243 | #else /* CONFIG_NFS_V4_1 */ | ||
244 | |||
245 | struct nfs_client * | ||
246 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, | ||
247 | struct nfs4_sessionid *sid) | ||
248 | { | ||
249 | return NULL; | ||
250 | } | ||
251 | #endif /* CONFIG_NFS_V4_1 */ | ||
252 | |||
253 | /* | ||
254 | * Set up an NFS4 client | ||
255 | */ | ||
256 | static int nfs4_set_client(struct nfs_server *server, | ||
257 | const char *hostname, | ||
258 | const struct sockaddr *addr, | ||
259 | const size_t addrlen, | ||
260 | const char *ip_addr, | ||
261 | rpc_authflavor_t authflavour, | ||
262 | int proto, const struct rpc_timeout *timeparms, | ||
263 | u32 minorversion, struct net *net) | ||
264 | { | ||
265 | struct nfs_client_initdata cl_init = { | ||
266 | .hostname = hostname, | ||
267 | .addr = addr, | ||
268 | .addrlen = addrlen, | ||
269 | .rpc_ops = &nfs_v4_clientops, | ||
270 | .proto = proto, | ||
271 | .minorversion = minorversion, | ||
272 | .net = net, | ||
273 | }; | ||
274 | struct nfs_client *clp; | ||
275 | int error; | ||
276 | |||
277 | dprintk("--> nfs4_set_client()\n"); | ||
278 | |||
279 | if (server->flags & NFS_MOUNT_NORESVPORT) | ||
280 | set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); | ||
281 | |||
282 | /* Allocate or find a client reference we can use */ | ||
283 | clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour); | ||
284 | if (IS_ERR(clp)) { | ||
285 | error = PTR_ERR(clp); | ||
286 | goto error; | ||
287 | } | ||
288 | |||
289 | /* | ||
290 | * Query for the lease time on clientid setup or renewal | ||
291 | * | ||
292 | * Note that this will be set on nfs_clients that were created | ||
293 | * only for the DS role and did not set this bit, but now will | ||
294 | * serve a dual role. | ||
295 | */ | ||
296 | set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); | ||
297 | |||
298 | server->nfs_client = clp; | ||
299 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); | ||
300 | return 0; | ||
301 | error: | ||
302 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); | ||
303 | return error; | ||
304 | } | ||
305 | |||
306 | /* | ||
307 | * Set up a pNFS Data Server client. | ||
308 | * | ||
309 | * Return any existing nfs_client that matches server address,port,version | ||
310 | * and minorversion. | ||
311 | * | ||
312 | * For a new nfs_client, use a soft mount (default), a low retrans and a | ||
313 | * low timeout interval so that if a connection is lost, we retry through | ||
314 | * the MDS. | ||
315 | */ | ||
316 | struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp, | ||
317 | const struct sockaddr *ds_addr, int ds_addrlen, | ||
318 | int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans) | ||
319 | { | ||
320 | struct nfs_client_initdata cl_init = { | ||
321 | .addr = ds_addr, | ||
322 | .addrlen = ds_addrlen, | ||
323 | .rpc_ops = &nfs_v4_clientops, | ||
324 | .proto = ds_proto, | ||
325 | .minorversion = mds_clp->cl_minorversion, | ||
326 | .net = mds_clp->cl_net, | ||
327 | }; | ||
328 | struct rpc_timeout ds_timeout; | ||
329 | struct nfs_client *clp; | ||
330 | |||
331 | /* | ||
332 | * Set an authflavor equual to the MDS value. Use the MDS nfs_client | ||
333 | * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS | ||
334 | * (section 13.1 RFC 5661). | ||
335 | */ | ||
336 | nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); | ||
337 | clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr, | ||
338 | mds_clp->cl_rpcclient->cl_auth->au_flavor); | ||
339 | |||
340 | dprintk("<-- %s %p\n", __func__, clp); | ||
341 | return clp; | ||
342 | } | ||
343 | EXPORT_SYMBOL_GPL(nfs4_set_ds_client); | ||
344 | |||
345 | /* | ||
346 | * Session has been established, and the client marked ready. | ||
347 | * Set the mount rsize and wsize with negotiated fore channel | ||
348 | * attributes which will be bound checked in nfs_server_set_fsinfo. | ||
349 | */ | ||
350 | static void nfs4_session_set_rwsize(struct nfs_server *server) | ||
351 | { | ||
352 | #ifdef CONFIG_NFS_V4_1 | ||
353 | struct nfs4_session *sess; | ||
354 | u32 server_resp_sz; | ||
355 | u32 server_rqst_sz; | ||
356 | |||
357 | if (!nfs4_has_session(server->nfs_client)) | ||
358 | return; | ||
359 | sess = server->nfs_client->cl_session; | ||
360 | server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; | ||
361 | server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; | ||
362 | |||
363 | if (server->rsize > server_resp_sz) | ||
364 | server->rsize = server_resp_sz; | ||
365 | if (server->wsize > server_rqst_sz) | ||
366 | server->wsize = server_rqst_sz; | ||
367 | #endif /* CONFIG_NFS_V4_1 */ | ||
368 | } | ||
369 | |||
370 | static int nfs4_server_common_setup(struct nfs_server *server, | ||
371 | struct nfs_fh *mntfh) | ||
372 | { | ||
373 | struct nfs_fattr *fattr; | ||
374 | int error; | ||
375 | |||
376 | BUG_ON(!server->nfs_client); | ||
377 | BUG_ON(!server->nfs_client->rpc_ops); | ||
378 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); | ||
379 | |||
380 | /* data servers support only a subset of NFSv4.1 */ | ||
381 | if (is_ds_only_client(server->nfs_client)) | ||
382 | return -EPROTONOSUPPORT; | ||
383 | |||
384 | fattr = nfs_alloc_fattr(); | ||
385 | if (fattr == NULL) | ||
386 | return -ENOMEM; | ||
387 | |||
388 | /* We must ensure the session is initialised first */ | ||
389 | error = nfs4_init_session(server); | ||
390 | if (error < 0) | ||
391 | goto out; | ||
392 | |||
393 | /* Probe the root fh to retrieve its FSID and filehandle */ | ||
394 | error = nfs4_get_rootfh(server, mntfh); | ||
395 | if (error < 0) | ||
396 | goto out; | ||
397 | |||
398 | dprintk("Server FSID: %llx:%llx\n", | ||
399 | (unsigned long long) server->fsid.major, | ||
400 | (unsigned long long) server->fsid.minor); | ||
401 | dprintk("Mount FH: %d\n", mntfh->size); | ||
402 | |||
403 | nfs4_session_set_rwsize(server); | ||
404 | |||
405 | error = nfs_probe_fsinfo(server, mntfh, fattr); | ||
406 | if (error < 0) | ||
407 | goto out; | ||
408 | |||
409 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) | ||
410 | server->namelen = NFS4_MAXNAMLEN; | ||
411 | |||
412 | nfs_server_insert_lists(server); | ||
413 | server->mount_time = jiffies; | ||
414 | server->destroy = nfs4_destroy_server; | ||
415 | out: | ||
416 | nfs_free_fattr(fattr); | ||
417 | return error; | ||
418 | } | ||
419 | |||
420 | /* | ||
421 | * Create a version 4 volume record | ||
422 | */ | ||
423 | static int nfs4_init_server(struct nfs_server *server, | ||
424 | const struct nfs_parsed_mount_data *data) | ||
425 | { | ||
426 | struct rpc_timeout timeparms; | ||
427 | int error; | ||
428 | |||
429 | dprintk("--> nfs4_init_server()\n"); | ||
430 | |||
431 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, | ||
432 | data->timeo, data->retrans); | ||
433 | |||
434 | /* Initialise the client representation from the mount data */ | ||
435 | server->flags = data->flags; | ||
436 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK; | ||
437 | if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) | ||
438 | server->caps |= NFS_CAP_READDIRPLUS; | ||
439 | server->options = data->options; | ||
440 | |||
441 | /* Get a client record */ | ||
442 | error = nfs4_set_client(server, | ||
443 | data->nfs_server.hostname, | ||
444 | (const struct sockaddr *)&data->nfs_server.address, | ||
445 | data->nfs_server.addrlen, | ||
446 | data->client_address, | ||
447 | data->auth_flavors[0], | ||
448 | data->nfs_server.protocol, | ||
449 | &timeparms, | ||
450 | data->minorversion, | ||
451 | data->net); | ||
452 | if (error < 0) | ||
453 | goto error; | ||
454 | |||
455 | /* | ||
456 | * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower | ||
457 | * authentication. | ||
458 | */ | ||
459 | if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX) | ||
460 | server->caps |= NFS_CAP_UIDGID_NOMAP; | ||
461 | |||
462 | if (data->rsize) | ||
463 | server->rsize = nfs_block_size(data->rsize, NULL); | ||
464 | if (data->wsize) | ||
465 | server->wsize = nfs_block_size(data->wsize, NULL); | ||
466 | |||
467 | server->acregmin = data->acregmin * HZ; | ||
468 | server->acregmax = data->acregmax * HZ; | ||
469 | server->acdirmin = data->acdirmin * HZ; | ||
470 | server->acdirmax = data->acdirmax * HZ; | ||
471 | |||
472 | server->port = data->nfs_server.port; | ||
473 | |||
474 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); | ||
475 | |||
476 | error: | ||
477 | /* Done */ | ||
478 | dprintk("<-- nfs4_init_server() = %d\n", error); | ||
479 | return error; | ||
480 | } | ||
481 | |||
482 | /* | ||
483 | * Create a version 4 volume record | ||
484 | * - keyed on server and FSID | ||
485 | */ | ||
486 | struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, | ||
487 | struct nfs_fh *mntfh) | ||
488 | { | ||
489 | struct nfs_server *server; | ||
490 | int error; | ||
491 | |||
492 | dprintk("--> nfs4_create_server()\n"); | ||
493 | |||
494 | server = nfs_alloc_server(); | ||
495 | if (!server) | ||
496 | return ERR_PTR(-ENOMEM); | ||
497 | |||
498 | /* set up the general RPC client */ | ||
499 | error = nfs4_init_server(server, data); | ||
500 | if (error < 0) | ||
501 | goto error; | ||
502 | |||
503 | error = nfs4_server_common_setup(server, mntfh); | ||
504 | if (error < 0) | ||
505 | goto error; | ||
506 | |||
507 | dprintk("<-- nfs4_create_server() = %p\n", server); | ||
508 | return server; | ||
509 | |||
510 | error: | ||
511 | nfs_free_server(server); | ||
512 | dprintk("<-- nfs4_create_server() = error %d\n", error); | ||
513 | return ERR_PTR(error); | ||
514 | } | ||
515 | |||
516 | /* | ||
517 | * Create an NFS4 referral server record | ||
518 | */ | ||
519 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, | ||
520 | struct nfs_fh *mntfh) | ||
521 | { | ||
522 | struct nfs_client *parent_client; | ||
523 | struct nfs_server *server, *parent_server; | ||
524 | int error; | ||
525 | |||
526 | dprintk("--> nfs4_create_referral_server()\n"); | ||
527 | |||
528 | server = nfs_alloc_server(); | ||
529 | if (!server) | ||
530 | return ERR_PTR(-ENOMEM); | ||
531 | |||
532 | parent_server = NFS_SB(data->sb); | ||
533 | parent_client = parent_server->nfs_client; | ||
534 | |||
535 | /* Initialise the client representation from the parent server */ | ||
536 | nfs_server_copy_userdata(server, parent_server); | ||
537 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; | ||
538 | |||
539 | /* Get a client representation. | ||
540 | * Note: NFSv4 always uses TCP, */ | ||
541 | error = nfs4_set_client(server, data->hostname, | ||
542 | data->addr, | ||
543 | data->addrlen, | ||
544 | parent_client->cl_ipaddr, | ||
545 | data->authflavor, | ||
546 | rpc_protocol(parent_server->client), | ||
547 | parent_server->client->cl_timeout, | ||
548 | parent_client->cl_mvops->minor_version, | ||
549 | parent_client->cl_net); | ||
550 | if (error < 0) | ||
551 | goto error; | ||
552 | |||
553 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); | ||
554 | if (error < 0) | ||
555 | goto error; | ||
556 | |||
557 | error = nfs4_server_common_setup(server, mntfh); | ||
558 | if (error < 0) | ||
559 | goto error; | ||
560 | |||
561 | dprintk("<-- nfs_create_referral_server() = %p\n", server); | ||
562 | return server; | ||
563 | |||
564 | error: | ||
565 | nfs_free_server(server); | ||
566 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); | ||
567 | return ERR_PTR(error); | ||
568 | } | ||
569 | |||
570 | module_param(nfs4_disable_idmapping, bool, 0644); | ||
571 | MODULE_PARM_DESC(nfs4_disable_idmapping, | ||
572 | "Turn off NFSv4 idmapping when using 'sec=sys'"); | ||