NFS: Support non-IPv4 addresses in nfs_parsed_mount_data
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / client.c
blob906613362a568ffdf1448cb3590c428d944ff412
1 /* client.c: NFS client sharing and management code
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/stats.h>
25 #include <linux/sunrpc/metrics.h>
26 #include <linux/sunrpc/xprtsock.h>
27 #include <linux/sunrpc/xprtrdma.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/nfs4_mount.h>
31 #include <linux/lockd/bind.h>
32 #include <linux/seq_file.h>
33 #include <linux/mount.h>
34 #include <linux/nfs_idmap.h>
35 #include <linux/vfs.h>
36 #include <linux/inet.h>
37 #include <linux/in6.h>
38 #include <net/ipv6.h>
39 #include <linux/nfs_xdr.h>
41 #include <asm/system.h>
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47 #include "internal.h"
49 #define NFSDBG_FACILITY NFSDBG_CLIENT
51 static DEFINE_SPINLOCK(nfs_client_lock);
52 static LIST_HEAD(nfs_client_list);
53 static LIST_HEAD(nfs_volume_list);
54 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
57 * RPC cruft for NFS
59 static struct rpc_version *nfs_version[5] = {
60 [2] = &nfs_version2,
61 #ifdef CONFIG_NFS_V3
62 [3] = &nfs_version3,
63 #endif
64 #ifdef CONFIG_NFS_V4
65 [4] = &nfs_version4,
66 #endif
69 struct rpc_program nfs_program = {
70 .name = "nfs",
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
78 struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
83 #ifdef CONFIG_NFS_V3_ACL
84 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85 static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
89 struct rpc_program nfsacl_program = {
90 .name = "nfsacl",
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
96 #endif /* CONFIG_NFS_V3_ACL */
98 struct nfs_client_initdata {
99 const char *hostname;
100 const struct sockaddr *addr;
101 size_t addrlen;
102 const struct nfs_rpc_ops *rpc_ops;
106 * Allocate a shared client record
108 * Since these are allocated/deallocated very rarely, we don't
109 * bother putting them in a slab cache...
111 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
113 struct nfs_client *clp;
115 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
116 goto error_0;
118 clp->rpc_ops = cl_init->rpc_ops;
120 if (cl_init->rpc_ops->version == 4) {
121 if (nfs_callback_up() < 0)
122 goto error_2;
123 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
126 atomic_set(&clp->cl_count, 1);
127 clp->cl_cons_state = NFS_CS_INITING;
129 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
130 clp->cl_addrlen = cl_init->addrlen;
132 if (cl_init->hostname) {
133 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
134 if (!clp->cl_hostname)
135 goto error_3;
138 INIT_LIST_HEAD(&clp->cl_superblocks);
139 clp->cl_rpcclient = ERR_PTR(-EINVAL);
141 #ifdef CONFIG_NFS_V4
142 init_rwsem(&clp->cl_sem);
143 INIT_LIST_HEAD(&clp->cl_delegations);
144 spin_lock_init(&clp->cl_lock);
145 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
146 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
147 clp->cl_boot_time = CURRENT_TIME;
148 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
149 #endif
151 return clp;
153 error_3:
154 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
155 nfs_callback_down();
156 error_2:
157 kfree(clp);
158 error_0:
159 return NULL;
162 static void nfs4_shutdown_client(struct nfs_client *clp)
164 #ifdef CONFIG_NFS_V4
165 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
166 nfs4_kill_renewd(clp);
167 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
168 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
169 nfs_idmap_delete(clp);
170 #endif
174 * Destroy a shared client record
176 static void nfs_free_client(struct nfs_client *clp)
178 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
180 nfs4_shutdown_client(clp);
182 /* -EIO all pending I/O */
183 if (!IS_ERR(clp->cl_rpcclient))
184 rpc_shutdown_client(clp->cl_rpcclient);
186 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
187 nfs_callback_down();
189 kfree(clp->cl_hostname);
190 kfree(clp);
192 dprintk("<-- nfs_free_client()\n");
196 * Release a reference to a shared client record
198 void nfs_put_client(struct nfs_client *clp)
200 if (!clp)
201 return;
203 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
205 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
206 list_del(&clp->cl_share_link);
207 spin_unlock(&nfs_client_lock);
209 BUG_ON(!list_empty(&clp->cl_superblocks));
211 nfs_free_client(clp);
215 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
216 const struct sockaddr_in *sa2)
218 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
221 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
222 const struct sockaddr_in6 *sa2)
224 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
227 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
228 const struct sockaddr *sa2)
230 switch (sa1->sa_family) {
231 case AF_INET:
232 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
233 (const struct sockaddr_in *)sa2);
234 case AF_INET6:
235 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
236 (const struct sockaddr_in6 *)sa2);
238 BUG();
242 * Find a client by IP address and protocol version
243 * - returns NULL if no such client
245 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
247 struct nfs_client *clp;
249 spin_lock(&nfs_client_lock);
250 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
251 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
253 /* Don't match clients that failed to initialise properly */
254 if (clp->cl_cons_state != NFS_CS_READY)
255 continue;
257 /* Different NFS versions cannot share the same nfs_client */
258 if (clp->rpc_ops->version != nfsversion)
259 continue;
261 if (addr->sa_family != clap->sa_family)
262 continue;
263 /* Match only the IP address, not the port number */
264 if (!nfs_sockaddr_match_ipaddr(addr, clap))
265 continue;
267 atomic_inc(&clp->cl_count);
268 spin_unlock(&nfs_client_lock);
269 return clp;
271 spin_unlock(&nfs_client_lock);
272 return NULL;
276 * Find an nfs_client on the list that matches the initialisation data
277 * that is supplied.
279 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
281 struct nfs_client *clp;
283 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
284 /* Don't match clients that failed to initialise properly */
285 if (clp->cl_cons_state < 0)
286 continue;
288 /* Different NFS versions cannot share the same nfs_client */
289 if (clp->rpc_ops != data->rpc_ops)
290 continue;
292 /* Match the full socket address */
293 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
294 continue;
296 atomic_inc(&clp->cl_count);
297 return clp;
299 return NULL;
303 * Look up a client by IP address and protocol version
304 * - creates a new record if one doesn't yet exist
306 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
308 struct nfs_client *clp, *new = NULL;
309 int error;
311 dprintk("--> nfs_get_client(%s,v%u)\n",
312 cl_init->hostname ?: "", cl_init->rpc_ops->version);
314 /* see if the client already exists */
315 do {
316 spin_lock(&nfs_client_lock);
318 clp = nfs_match_client(cl_init);
319 if (clp)
320 goto found_client;
321 if (new)
322 goto install_client;
324 spin_unlock(&nfs_client_lock);
326 new = nfs_alloc_client(cl_init);
327 } while (new);
329 return ERR_PTR(-ENOMEM);
331 /* install a new client and return with it unready */
332 install_client:
333 clp = new;
334 list_add(&clp->cl_share_link, &nfs_client_list);
335 spin_unlock(&nfs_client_lock);
336 dprintk("--> nfs_get_client() = %p [new]\n", clp);
337 return clp;
339 /* found an existing client
340 * - make sure it's ready before returning
342 found_client:
343 spin_unlock(&nfs_client_lock);
345 if (new)
346 nfs_free_client(new);
348 error = wait_event_interruptible(nfs_client_active_wq,
349 clp->cl_cons_state != NFS_CS_INITING);
350 if (error < 0) {
351 nfs_put_client(clp);
352 return ERR_PTR(-ERESTARTSYS);
355 if (clp->cl_cons_state < NFS_CS_READY) {
356 error = clp->cl_cons_state;
357 nfs_put_client(clp);
358 return ERR_PTR(error);
361 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
363 dprintk("--> nfs_get_client() = %p [share]\n", clp);
364 return clp;
368 * Mark a server as ready or failed
370 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
372 clp->cl_cons_state = state;
373 wake_up_all(&nfs_client_active_wq);
377 * Initialise the timeout values for a connection
379 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
380 unsigned int timeo, unsigned int retrans)
382 to->to_initval = timeo * HZ / 10;
383 to->to_retries = retrans;
384 if (!to->to_retries)
385 to->to_retries = 2;
387 switch (proto) {
388 case XPRT_TRANSPORT_TCP:
389 case XPRT_TRANSPORT_RDMA:
390 if (!to->to_initval)
391 to->to_initval = 60 * HZ;
392 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
393 to->to_initval = NFS_MAX_TCP_TIMEOUT;
394 to->to_increment = to->to_initval;
395 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
396 to->to_exponential = 0;
397 break;
398 case XPRT_TRANSPORT_UDP:
399 default:
400 if (!to->to_initval)
401 to->to_initval = 11 * HZ / 10;
402 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
403 to->to_initval = NFS_MAX_UDP_TIMEOUT;
404 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
405 to->to_exponential = 1;
406 break;
411 * Create an RPC client handle
413 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
414 unsigned int timeo,
415 unsigned int retrans,
416 rpc_authflavor_t flavor,
417 int flags)
419 struct rpc_timeout timeparms;
420 struct rpc_clnt *clnt = NULL;
421 struct rpc_create_args args = {
422 .protocol = proto,
423 .address = (struct sockaddr *)&clp->cl_addr,
424 .addrsize = clp->cl_addrlen,
425 .timeout = &timeparms,
426 .servername = clp->cl_hostname,
427 .program = &nfs_program,
428 .version = clp->rpc_ops->version,
429 .authflavor = flavor,
430 .flags = flags,
433 if (!IS_ERR(clp->cl_rpcclient))
434 return 0;
436 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
437 clp->retrans_timeo = timeparms.to_initval;
438 clp->retrans_count = timeparms.to_retries;
440 clnt = rpc_create(&args);
441 if (IS_ERR(clnt)) {
442 dprintk("%s: cannot create RPC client. Error = %ld\n",
443 __FUNCTION__, PTR_ERR(clnt));
444 return PTR_ERR(clnt);
447 clp->cl_rpcclient = clnt;
448 return 0;
452 * Version 2 or 3 client destruction
454 static void nfs_destroy_server(struct nfs_server *server)
456 if (!(server->flags & NFS_MOUNT_NONLM))
457 lockd_down(); /* release rpc.lockd */
461 * Version 2 or 3 lockd setup
463 static int nfs_start_lockd(struct nfs_server *server)
465 int error = 0;
467 if (server->nfs_client->rpc_ops->version > 3)
468 goto out;
469 if (server->flags & NFS_MOUNT_NONLM)
470 goto out;
471 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
472 IPPROTO_TCP : IPPROTO_UDP);
473 if (error < 0)
474 server->flags |= NFS_MOUNT_NONLM;
475 else
476 server->destroy = nfs_destroy_server;
477 out:
478 return error;
482 * Initialise an NFSv3 ACL client connection
484 #ifdef CONFIG_NFS_V3_ACL
485 static void nfs_init_server_aclclient(struct nfs_server *server)
487 if (server->nfs_client->rpc_ops->version != 3)
488 goto out_noacl;
489 if (server->flags & NFS_MOUNT_NOACL)
490 goto out_noacl;
492 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
493 if (IS_ERR(server->client_acl))
494 goto out_noacl;
496 /* No errors! Assume that Sun nfsacls are supported */
497 server->caps |= NFS_CAP_ACLS;
498 return;
500 out_noacl:
501 server->caps &= ~NFS_CAP_ACLS;
503 #else
504 static inline void nfs_init_server_aclclient(struct nfs_server *server)
506 server->flags &= ~NFS_MOUNT_NOACL;
507 server->caps &= ~NFS_CAP_ACLS;
509 #endif
512 * Create a general RPC client
514 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
516 struct nfs_client *clp = server->nfs_client;
518 server->client = rpc_clone_client(clp->cl_rpcclient);
519 if (IS_ERR(server->client)) {
520 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
521 return PTR_ERR(server->client);
524 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
525 struct rpc_auth *auth;
527 auth = rpcauth_create(pseudoflavour, server->client);
528 if (IS_ERR(auth)) {
529 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
530 return PTR_ERR(auth);
533 server->client->cl_softrtry = 0;
534 if (server->flags & NFS_MOUNT_SOFT)
535 server->client->cl_softrtry = 1;
537 server->client->cl_intr = 0;
538 if (server->flags & NFS4_MOUNT_INTR)
539 server->client->cl_intr = 1;
541 return 0;
545 * Initialise an NFS2 or NFS3 client
547 static int nfs_init_client(struct nfs_client *clp,
548 const struct nfs_parsed_mount_data *data)
550 int error;
552 if (clp->cl_cons_state == NFS_CS_READY) {
553 /* the client is already initialised */
554 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
555 return 0;
559 * Create a client RPC handle for doing FSSTAT with UNIX auth only
560 * - RFC 2623, sec 2.3.2
562 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
563 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
564 if (error < 0)
565 goto error;
566 nfs_mark_client_ready(clp, NFS_CS_READY);
567 return 0;
569 error:
570 nfs_mark_client_ready(clp, error);
571 dprintk("<-- nfs_init_client() = xerror %d\n", error);
572 return error;
576 * Create a version 2 or 3 client
578 static int nfs_init_server(struct nfs_server *server,
579 const struct nfs_parsed_mount_data *data)
581 struct nfs_client_initdata cl_init = {
582 .hostname = data->nfs_server.hostname,
583 .addr = (const struct sockaddr *)&data->nfs_server.address,
584 .addrlen = data->nfs_server.addrlen,
585 .rpc_ops = &nfs_v2_clientops,
587 struct nfs_client *clp;
588 int error;
590 dprintk("--> nfs_init_server()\n");
592 #ifdef CONFIG_NFS_V3
593 if (data->flags & NFS_MOUNT_VER3)
594 cl_init.rpc_ops = &nfs_v3_clientops;
595 #endif
597 /* Allocate or find a client reference we can use */
598 clp = nfs_get_client(&cl_init);
599 if (IS_ERR(clp)) {
600 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
601 return PTR_ERR(clp);
604 error = nfs_init_client(clp, data);
605 if (error < 0)
606 goto error;
608 server->nfs_client = clp;
610 /* Initialise the client representation from the mount data */
611 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
613 if (data->rsize)
614 server->rsize = nfs_block_size(data->rsize, NULL);
615 if (data->wsize)
616 server->wsize = nfs_block_size(data->wsize, NULL);
618 server->acregmin = data->acregmin * HZ;
619 server->acregmax = data->acregmax * HZ;
620 server->acdirmin = data->acdirmin * HZ;
621 server->acdirmax = data->acdirmax * HZ;
623 /* Start lockd here, before we might error out */
624 error = nfs_start_lockd(server);
625 if (error < 0)
626 goto error;
628 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
629 if (error < 0)
630 goto error;
632 server->namelen = data->namlen;
633 /* Create a client RPC handle for the NFSv3 ACL management interface */
634 nfs_init_server_aclclient(server);
635 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
636 return 0;
638 error:
639 server->nfs_client = NULL;
640 nfs_put_client(clp);
641 dprintk("<-- nfs_init_server() = xerror %d\n", error);
642 return error;
646 * Load up the server record from information gained in an fsinfo record
648 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
650 unsigned long max_rpc_payload;
652 /* Work out a lot of parameters */
653 if (server->rsize == 0)
654 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
655 if (server->wsize == 0)
656 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
658 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
659 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
660 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
661 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
663 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
664 if (server->rsize > max_rpc_payload)
665 server->rsize = max_rpc_payload;
666 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
667 server->rsize = NFS_MAX_FILE_IO_SIZE;
668 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
670 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
672 if (server->wsize > max_rpc_payload)
673 server->wsize = max_rpc_payload;
674 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
675 server->wsize = NFS_MAX_FILE_IO_SIZE;
676 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
677 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
679 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
680 if (server->dtsize > PAGE_CACHE_SIZE)
681 server->dtsize = PAGE_CACHE_SIZE;
682 if (server->dtsize > server->rsize)
683 server->dtsize = server->rsize;
685 if (server->flags & NFS_MOUNT_NOAC) {
686 server->acregmin = server->acregmax = 0;
687 server->acdirmin = server->acdirmax = 0;
690 server->maxfilesize = fsinfo->maxfilesize;
692 /* We're airborne Set socket buffersize */
693 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
697 * Probe filesystem information, including the FSID on v2/v3
699 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
701 struct nfs_fsinfo fsinfo;
702 struct nfs_client *clp = server->nfs_client;
703 int error;
705 dprintk("--> nfs_probe_fsinfo()\n");
707 if (clp->rpc_ops->set_capabilities != NULL) {
708 error = clp->rpc_ops->set_capabilities(server, mntfh);
709 if (error < 0)
710 goto out_error;
713 fsinfo.fattr = fattr;
714 nfs_fattr_init(fattr);
715 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
716 if (error < 0)
717 goto out_error;
719 nfs_server_set_fsinfo(server, &fsinfo);
720 error = bdi_init(&server->backing_dev_info);
721 if (error)
722 goto out_error;
725 /* Get some general file system info */
726 if (server->namelen == 0) {
727 struct nfs_pathconf pathinfo;
729 pathinfo.fattr = fattr;
730 nfs_fattr_init(fattr);
732 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
733 server->namelen = pathinfo.max_namelen;
736 dprintk("<-- nfs_probe_fsinfo() = 0\n");
737 return 0;
739 out_error:
740 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
741 return error;
745 * Copy useful information when duplicating a server record
747 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
749 target->flags = source->flags;
750 target->acregmin = source->acregmin;
751 target->acregmax = source->acregmax;
752 target->acdirmin = source->acdirmin;
753 target->acdirmax = source->acdirmax;
754 target->caps = source->caps;
758 * Allocate and initialise a server record
760 static struct nfs_server *nfs_alloc_server(void)
762 struct nfs_server *server;
764 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
765 if (!server)
766 return NULL;
768 server->client = server->client_acl = ERR_PTR(-EINVAL);
770 /* Zero out the NFS state stuff */
771 INIT_LIST_HEAD(&server->client_link);
772 INIT_LIST_HEAD(&server->master_link);
774 init_waitqueue_head(&server->active_wq);
775 atomic_set(&server->active, 0);
777 server->io_stats = nfs_alloc_iostats();
778 if (!server->io_stats) {
779 kfree(server);
780 return NULL;
783 return server;
787 * Free up a server record
789 void nfs_free_server(struct nfs_server *server)
791 dprintk("--> nfs_free_server()\n");
793 spin_lock(&nfs_client_lock);
794 list_del(&server->client_link);
795 list_del(&server->master_link);
796 spin_unlock(&nfs_client_lock);
798 if (server->destroy != NULL)
799 server->destroy(server);
801 if (!IS_ERR(server->client_acl))
802 rpc_shutdown_client(server->client_acl);
803 if (!IS_ERR(server->client))
804 rpc_shutdown_client(server->client);
806 nfs_put_client(server->nfs_client);
808 nfs_free_iostats(server->io_stats);
809 bdi_destroy(&server->backing_dev_info);
810 kfree(server);
811 nfs_release_automount_timer();
812 dprintk("<-- nfs_free_server()\n");
816 * Create a version 2 or 3 volume record
817 * - keyed on server and FSID
819 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
820 struct nfs_fh *mntfh)
822 struct nfs_server *server;
823 struct nfs_fattr fattr;
824 int error;
826 server = nfs_alloc_server();
827 if (!server)
828 return ERR_PTR(-ENOMEM);
830 /* Get a client representation */
831 error = nfs_init_server(server, data);
832 if (error < 0)
833 goto error;
835 BUG_ON(!server->nfs_client);
836 BUG_ON(!server->nfs_client->rpc_ops);
837 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
839 /* Probe the root fh to retrieve its FSID */
840 error = nfs_probe_fsinfo(server, mntfh, &fattr);
841 if (error < 0)
842 goto error;
843 if (server->nfs_client->rpc_ops->version == 3) {
844 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
845 server->namelen = NFS3_MAXNAMLEN;
846 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
847 server->caps |= NFS_CAP_READDIRPLUS;
848 } else {
849 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
850 server->namelen = NFS2_MAXNAMLEN;
853 if (!(fattr.valid & NFS_ATTR_FATTR)) {
854 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
855 if (error < 0) {
856 dprintk("nfs_create_server: getattr error = %d\n", -error);
857 goto error;
860 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
862 dprintk("Server FSID: %llx:%llx\n",
863 (unsigned long long) server->fsid.major,
864 (unsigned long long) server->fsid.minor);
866 BUG_ON(!server->nfs_client);
867 BUG_ON(!server->nfs_client->rpc_ops);
868 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
870 spin_lock(&nfs_client_lock);
871 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
872 list_add_tail(&server->master_link, &nfs_volume_list);
873 spin_unlock(&nfs_client_lock);
875 server->mount_time = jiffies;
876 return server;
878 error:
879 nfs_free_server(server);
880 return ERR_PTR(error);
883 #ifdef CONFIG_NFS_V4
885 * Initialise an NFS4 client record
887 static int nfs4_init_client(struct nfs_client *clp,
888 int proto, int timeo, int retrans,
889 const char *ip_addr,
890 rpc_authflavor_t authflavour)
892 int error;
894 if (clp->cl_cons_state == NFS_CS_READY) {
895 /* the client is initialised already */
896 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
897 return 0;
900 /* Check NFS protocol revision and initialize RPC op vector */
901 clp->rpc_ops = &nfs_v4_clientops;
903 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
904 RPC_CLNT_CREATE_DISCRTRY);
905 if (error < 0)
906 goto error;
907 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
909 error = nfs_idmap_new(clp);
910 if (error < 0) {
911 dprintk("%s: failed to create idmapper. Error = %d\n",
912 __FUNCTION__, error);
913 goto error;
915 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
917 nfs_mark_client_ready(clp, NFS_CS_READY);
918 return 0;
920 error:
921 nfs_mark_client_ready(clp, error);
922 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
923 return error;
927 * Set up an NFS4 client
929 static int nfs4_set_client(struct nfs_server *server,
930 const char *hostname,
931 const struct sockaddr *addr,
932 const size_t addrlen,
933 const char *ip_addr,
934 rpc_authflavor_t authflavour,
935 int proto, int timeo, int retrans)
937 struct nfs_client_initdata cl_init = {
938 .hostname = hostname,
939 .addr = addr,
940 .addrlen = addrlen,
941 .rpc_ops = &nfs_v4_clientops,
943 struct nfs_client *clp;
944 int error;
946 dprintk("--> nfs4_set_client()\n");
948 /* Allocate or find a client reference we can use */
949 clp = nfs_get_client(&cl_init);
950 if (IS_ERR(clp)) {
951 error = PTR_ERR(clp);
952 goto error;
954 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
955 if (error < 0)
956 goto error_put;
958 server->nfs_client = clp;
959 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
960 return 0;
962 error_put:
963 nfs_put_client(clp);
964 error:
965 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
966 return error;
970 * Create a version 4 volume record
972 static int nfs4_init_server(struct nfs_server *server,
973 const struct nfs_parsed_mount_data *data)
975 int error;
977 dprintk("--> nfs4_init_server()\n");
979 /* Initialise the client representation from the mount data */
980 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
981 server->caps |= NFS_CAP_ATOMIC_OPEN;
983 if (data->rsize)
984 server->rsize = nfs_block_size(data->rsize, NULL);
985 if (data->wsize)
986 server->wsize = nfs_block_size(data->wsize, NULL);
988 server->acregmin = data->acregmin * HZ;
989 server->acregmax = data->acregmax * HZ;
990 server->acdirmin = data->acdirmin * HZ;
991 server->acdirmax = data->acdirmax * HZ;
993 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
995 /* Done */
996 dprintk("<-- nfs4_init_server() = %d\n", error);
997 return error;
1001 * Create a version 4 volume record
1002 * - keyed on server and FSID
1004 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1005 struct nfs_fh *mntfh)
1007 struct nfs_fattr fattr;
1008 struct nfs_server *server;
1009 int error;
1011 dprintk("--> nfs4_create_server()\n");
1013 server = nfs_alloc_server();
1014 if (!server)
1015 return ERR_PTR(-ENOMEM);
1017 /* Get a client record */
1018 error = nfs4_set_client(server,
1019 data->nfs_server.hostname,
1020 (struct sockaddr *)&data->nfs_server.address,
1021 data->nfs_server.addrlen,
1022 data->client_address,
1023 data->auth_flavors[0],
1024 data->nfs_server.protocol,
1025 data->timeo, data->retrans);
1026 if (error < 0)
1027 goto error;
1029 /* set up the general RPC client */
1030 error = nfs4_init_server(server, data);
1031 if (error < 0)
1032 goto error;
1034 BUG_ON(!server->nfs_client);
1035 BUG_ON(!server->nfs_client->rpc_ops);
1036 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1038 /* Probe the root fh to retrieve its FSID */
1039 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1040 if (error < 0)
1041 goto error;
1043 dprintk("Server FSID: %llx:%llx\n",
1044 (unsigned long long) server->fsid.major,
1045 (unsigned long long) server->fsid.minor);
1046 dprintk("Mount FH: %d\n", mntfh->size);
1048 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1049 if (error < 0)
1050 goto error;
1052 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1053 server->namelen = NFS4_MAXNAMLEN;
1055 BUG_ON(!server->nfs_client);
1056 BUG_ON(!server->nfs_client->rpc_ops);
1057 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1059 spin_lock(&nfs_client_lock);
1060 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1061 list_add_tail(&server->master_link, &nfs_volume_list);
1062 spin_unlock(&nfs_client_lock);
1064 server->mount_time = jiffies;
1065 dprintk("<-- nfs4_create_server() = %p\n", server);
1066 return server;
1068 error:
1069 nfs_free_server(server);
1070 dprintk("<-- nfs4_create_server() = error %d\n", error);
1071 return ERR_PTR(error);
1075 * Create an NFS4 referral server record
1077 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1078 struct nfs_fh *mntfh)
1080 struct nfs_client *parent_client;
1081 struct nfs_server *server, *parent_server;
1082 struct nfs_fattr fattr;
1083 int error;
1085 dprintk("--> nfs4_create_referral_server()\n");
1087 server = nfs_alloc_server();
1088 if (!server)
1089 return ERR_PTR(-ENOMEM);
1091 parent_server = NFS_SB(data->sb);
1092 parent_client = parent_server->nfs_client;
1094 /* Get a client representation.
1095 * Note: NFSv4 always uses TCP, */
1096 error = nfs4_set_client(server, data->hostname,
1097 data->addr,
1098 data->addrlen,
1099 parent_client->cl_ipaddr,
1100 data->authflavor,
1101 parent_server->client->cl_xprt->prot,
1102 parent_client->retrans_timeo,
1103 parent_client->retrans_count);
1104 if (error < 0)
1105 goto error;
1107 /* Initialise the client representation from the parent server */
1108 nfs_server_copy_userdata(server, parent_server);
1109 server->caps |= NFS_CAP_ATOMIC_OPEN;
1111 error = nfs_init_server_rpcclient(server, data->authflavor);
1112 if (error < 0)
1113 goto error;
1115 BUG_ON(!server->nfs_client);
1116 BUG_ON(!server->nfs_client->rpc_ops);
1117 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1119 /* Probe the root fh to retrieve its FSID and filehandle */
1120 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1121 if (error < 0)
1122 goto error;
1124 /* probe the filesystem info for this server filesystem */
1125 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1126 if (error < 0)
1127 goto error;
1129 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1130 server->namelen = NFS4_MAXNAMLEN;
1132 dprintk("Referral FSID: %llx:%llx\n",
1133 (unsigned long long) server->fsid.major,
1134 (unsigned long long) server->fsid.minor);
1136 spin_lock(&nfs_client_lock);
1137 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1138 list_add_tail(&server->master_link, &nfs_volume_list);
1139 spin_unlock(&nfs_client_lock);
1141 server->mount_time = jiffies;
1143 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1144 return server;
1146 error:
1147 nfs_free_server(server);
1148 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1149 return ERR_PTR(error);
1152 #endif /* CONFIG_NFS_V4 */
1155 * Clone an NFS2, NFS3 or NFS4 server record
1157 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1158 struct nfs_fh *fh,
1159 struct nfs_fattr *fattr)
1161 struct nfs_server *server;
1162 struct nfs_fattr fattr_fsinfo;
1163 int error;
1165 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1166 (unsigned long long) fattr->fsid.major,
1167 (unsigned long long) fattr->fsid.minor);
1169 server = nfs_alloc_server();
1170 if (!server)
1171 return ERR_PTR(-ENOMEM);
1173 /* Copy data from the source */
1174 server->nfs_client = source->nfs_client;
1175 atomic_inc(&server->nfs_client->cl_count);
1176 nfs_server_copy_userdata(server, source);
1178 server->fsid = fattr->fsid;
1180 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1181 if (error < 0)
1182 goto out_free_server;
1183 if (!IS_ERR(source->client_acl))
1184 nfs_init_server_aclclient(server);
1186 /* probe the filesystem info for this server filesystem */
1187 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1188 if (error < 0)
1189 goto out_free_server;
1191 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1192 server->namelen = NFS4_MAXNAMLEN;
1194 dprintk("Cloned FSID: %llx:%llx\n",
1195 (unsigned long long) server->fsid.major,
1196 (unsigned long long) server->fsid.minor);
1198 error = nfs_start_lockd(server);
1199 if (error < 0)
1200 goto out_free_server;
1202 spin_lock(&nfs_client_lock);
1203 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1204 list_add_tail(&server->master_link, &nfs_volume_list);
1205 spin_unlock(&nfs_client_lock);
1207 server->mount_time = jiffies;
1209 dprintk("<-- nfs_clone_server() = %p\n", server);
1210 return server;
1212 out_free_server:
1213 nfs_free_server(server);
1214 dprintk("<-- nfs_clone_server() = error %d\n", error);
1215 return ERR_PTR(error);
1218 #ifdef CONFIG_PROC_FS
1219 static struct proc_dir_entry *proc_fs_nfs;
1221 static int nfs_server_list_open(struct inode *inode, struct file *file);
1222 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1223 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1224 static void nfs_server_list_stop(struct seq_file *p, void *v);
1225 static int nfs_server_list_show(struct seq_file *m, void *v);
1227 static struct seq_operations nfs_server_list_ops = {
1228 .start = nfs_server_list_start,
1229 .next = nfs_server_list_next,
1230 .stop = nfs_server_list_stop,
1231 .show = nfs_server_list_show,
1234 static const struct file_operations nfs_server_list_fops = {
1235 .open = nfs_server_list_open,
1236 .read = seq_read,
1237 .llseek = seq_lseek,
1238 .release = seq_release,
1241 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1242 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1243 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1244 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1245 static int nfs_volume_list_show(struct seq_file *m, void *v);
1247 static struct seq_operations nfs_volume_list_ops = {
1248 .start = nfs_volume_list_start,
1249 .next = nfs_volume_list_next,
1250 .stop = nfs_volume_list_stop,
1251 .show = nfs_volume_list_show,
1254 static const struct file_operations nfs_volume_list_fops = {
1255 .open = nfs_volume_list_open,
1256 .read = seq_read,
1257 .llseek = seq_lseek,
1258 .release = seq_release,
1262 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1263 * we're dealing
1265 static int nfs_server_list_open(struct inode *inode, struct file *file)
1267 struct seq_file *m;
1268 int ret;
1270 ret = seq_open(file, &nfs_server_list_ops);
1271 if (ret < 0)
1272 return ret;
1274 m = file->private_data;
1275 m->private = PDE(inode)->data;
1277 return 0;
1281 * set up the iterator to start reading from the server list and return the first item
1283 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1285 /* lock the list against modification */
1286 spin_lock(&nfs_client_lock);
1287 return seq_list_start_head(&nfs_client_list, *_pos);
1291 * move to next server
1293 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1295 return seq_list_next(v, &nfs_client_list, pos);
1299 * clean up after reading from the transports list
1301 static void nfs_server_list_stop(struct seq_file *p, void *v)
1303 spin_unlock(&nfs_client_lock);
1307 * display a header line followed by a load of call lines
1309 static int nfs_server_list_show(struct seq_file *m, void *v)
1311 struct nfs_client *clp;
1313 /* display header on line 1 */
1314 if (v == &nfs_client_list) {
1315 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1316 return 0;
1319 /* display one transport per line on subsequent lines */
1320 clp = list_entry(v, struct nfs_client, cl_share_link);
1322 seq_printf(m, "v%u %s %s %3d %s\n",
1323 clp->rpc_ops->version,
1324 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1325 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1326 atomic_read(&clp->cl_count),
1327 clp->cl_hostname);
1329 return 0;
1333 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1335 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1337 struct seq_file *m;
1338 int ret;
1340 ret = seq_open(file, &nfs_volume_list_ops);
1341 if (ret < 0)
1342 return ret;
1344 m = file->private_data;
1345 m->private = PDE(inode)->data;
1347 return 0;
1351 * set up the iterator to start reading from the volume list and return the first item
1353 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1355 /* lock the list against modification */
1356 spin_lock(&nfs_client_lock);
1357 return seq_list_start_head(&nfs_volume_list, *_pos);
1361 * move to next volume
1363 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1365 return seq_list_next(v, &nfs_volume_list, pos);
1369 * clean up after reading from the transports list
1371 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1373 spin_unlock(&nfs_client_lock);
1377 * display a header line followed by a load of call lines
1379 static int nfs_volume_list_show(struct seq_file *m, void *v)
1381 struct nfs_server *server;
1382 struct nfs_client *clp;
1383 char dev[8], fsid[17];
1385 /* display header on line 1 */
1386 if (v == &nfs_volume_list) {
1387 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1388 return 0;
1390 /* display one transport per line on subsequent lines */
1391 server = list_entry(v, struct nfs_server, master_link);
1392 clp = server->nfs_client;
1394 snprintf(dev, 8, "%u:%u",
1395 MAJOR(server->s_dev), MINOR(server->s_dev));
1397 snprintf(fsid, 17, "%llx:%llx",
1398 (unsigned long long) server->fsid.major,
1399 (unsigned long long) server->fsid.minor);
1401 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1402 clp->rpc_ops->version,
1403 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1404 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1405 dev,
1406 fsid);
1408 return 0;
1412 * initialise the /proc/fs/nfsfs/ directory
1414 int __init nfs_fs_proc_init(void)
1416 struct proc_dir_entry *p;
1418 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1419 if (!proc_fs_nfs)
1420 goto error_0;
1422 proc_fs_nfs->owner = THIS_MODULE;
1424 /* a file of servers with which we're dealing */
1425 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1426 if (!p)
1427 goto error_1;
1429 p->proc_fops = &nfs_server_list_fops;
1430 p->owner = THIS_MODULE;
1432 /* a file of volumes that we have mounted */
1433 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1434 if (!p)
1435 goto error_2;
1437 p->proc_fops = &nfs_volume_list_fops;
1438 p->owner = THIS_MODULE;
1439 return 0;
1441 error_2:
1442 remove_proc_entry("servers", proc_fs_nfs);
1443 error_1:
1444 remove_proc_entry("nfsfs", proc_root_fs);
1445 error_0:
1446 return -ENOMEM;
1450 * clean up the /proc/fs/nfsfs/ directory
1452 void nfs_fs_proc_exit(void)
1454 remove_proc_entry("volumes", proc_fs_nfs);
1455 remove_proc_entry("servers", proc_fs_nfs);
1456 remove_proc_entry("nfsfs", proc_root_fs);
1459 #endif /* CONFIG_PROC_FS */