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>
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>
39 #include <linux/nfs_xdr.h>
41 #include <asm/system.h>
45 #include "delegation.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);
59 static struct rpc_version *nfs_version[5] = {
69 struct rpc_program nfs_program = {
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 = {
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 {
100 const struct sockaddr *addr;
102 const struct nfs_rpc_ops *rpc_ops;
107 * Allocate a shared client record
109 * Since these are allocated/deallocated very rarely, we don't
110 * bother putting them in a slab cache...
112 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
114 struct nfs_client *clp;
115 struct rpc_cred *cred;
117 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
120 clp->rpc_ops = cl_init->rpc_ops;
122 if (cl_init->rpc_ops->version == 4) {
123 if (nfs_callback_up() < 0)
125 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
128 atomic_set(&clp->cl_count, 1);
129 clp->cl_cons_state = NFS_CS_INITING;
131 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
132 clp->cl_addrlen = cl_init->addrlen;
134 if (cl_init->hostname) {
135 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
136 if (!clp->cl_hostname)
140 INIT_LIST_HEAD(&clp->cl_superblocks);
141 clp->cl_rpcclient = ERR_PTR(-EINVAL);
143 clp->cl_proto = cl_init->proto;
146 init_rwsem(&clp->cl_sem);
147 INIT_LIST_HEAD(&clp->cl_delegations);
148 spin_lock_init(&clp->cl_lock);
149 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
150 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
151 clp->cl_boot_time = CURRENT_TIME;
152 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
154 cred = rpc_lookup_machine_cred();
156 clp->cl_machine_cred = cred;
161 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
169 static void nfs4_shutdown_client(struct nfs_client *clp)
172 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
173 nfs4_kill_renewd(clp);
174 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
175 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
176 nfs_idmap_delete(clp);
178 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
183 * Destroy a shared client record
185 static void nfs_free_client(struct nfs_client *clp)
187 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
189 nfs4_shutdown_client(clp);
191 /* -EIO all pending I/O */
192 if (!IS_ERR(clp->cl_rpcclient))
193 rpc_shutdown_client(clp->cl_rpcclient);
195 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
198 if (clp->cl_machine_cred != NULL)
199 put_rpccred(clp->cl_machine_cred);
201 kfree(clp->cl_hostname);
204 dprintk("<-- nfs_free_client()\n");
208 * Release a reference to a shared client record
210 void nfs_put_client(struct nfs_client *clp)
215 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
217 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
218 list_del(&clp->cl_share_link);
219 spin_unlock(&nfs_client_lock);
221 BUG_ON(!list_empty(&clp->cl_superblocks));
223 nfs_free_client(clp);
227 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
228 const struct sockaddr_in *sa2)
230 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
233 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
234 const struct sockaddr_in6 *sa2)
236 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
239 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
240 const struct sockaddr *sa2)
242 switch (sa1->sa_family) {
244 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
245 (const struct sockaddr_in *)sa2);
247 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
248 (const struct sockaddr_in6 *)sa2);
254 * Find a client by IP address and protocol version
255 * - returns NULL if no such client
257 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
259 struct nfs_client *clp;
261 spin_lock(&nfs_client_lock);
262 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
263 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
265 /* Don't match clients that failed to initialise properly */
266 if (clp->cl_cons_state != NFS_CS_READY)
269 /* Different NFS versions cannot share the same nfs_client */
270 if (clp->rpc_ops->version != nfsversion)
273 if (addr->sa_family != clap->sa_family)
275 /* Match only the IP address, not the port number */
276 if (!nfs_sockaddr_match_ipaddr(addr, clap))
279 atomic_inc(&clp->cl_count);
280 spin_unlock(&nfs_client_lock);
283 spin_unlock(&nfs_client_lock);
288 * Find a client by IP address and protocol version
289 * - returns NULL if no such client
291 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
293 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
294 u32 nfsvers = clp->rpc_ops->version;
296 spin_lock(&nfs_client_lock);
297 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
298 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
300 /* Don't match clients that failed to initialise properly */
301 if (clp->cl_cons_state != NFS_CS_READY)
304 /* Different NFS versions cannot share the same nfs_client */
305 if (clp->rpc_ops->version != nfsvers)
308 if (sap->sa_family != clap->sa_family)
310 /* Match only the IP address, not the port number */
311 if (!nfs_sockaddr_match_ipaddr(sap, clap))
314 atomic_inc(&clp->cl_count);
315 spin_unlock(&nfs_client_lock);
318 spin_unlock(&nfs_client_lock);
323 * Find an nfs_client on the list that matches the initialisation data
326 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
328 struct nfs_client *clp;
330 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
331 /* Don't match clients that failed to initialise properly */
332 if (clp->cl_cons_state < 0)
335 /* Different NFS versions cannot share the same nfs_client */
336 if (clp->rpc_ops != data->rpc_ops)
339 if (clp->cl_proto != data->proto)
342 /* Match the full socket address */
343 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
346 atomic_inc(&clp->cl_count);
353 * Look up a client by IP address and protocol version
354 * - creates a new record if one doesn't yet exist
356 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
358 struct nfs_client *clp, *new = NULL;
361 dprintk("--> nfs_get_client(%s,v%u)\n",
362 cl_init->hostname ?: "", cl_init->rpc_ops->version);
364 /* see if the client already exists */
366 spin_lock(&nfs_client_lock);
368 clp = nfs_match_client(cl_init);
374 spin_unlock(&nfs_client_lock);
376 new = nfs_alloc_client(cl_init);
379 return ERR_PTR(-ENOMEM);
381 /* install a new client and return with it unready */
384 list_add(&clp->cl_share_link, &nfs_client_list);
385 spin_unlock(&nfs_client_lock);
386 dprintk("--> nfs_get_client() = %p [new]\n", clp);
389 /* found an existing client
390 * - make sure it's ready before returning
393 spin_unlock(&nfs_client_lock);
396 nfs_free_client(new);
398 error = wait_event_killable(nfs_client_active_wq,
399 clp->cl_cons_state != NFS_CS_INITING);
402 return ERR_PTR(-ERESTARTSYS);
405 if (clp->cl_cons_state < NFS_CS_READY) {
406 error = clp->cl_cons_state;
408 return ERR_PTR(error);
411 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
413 dprintk("--> nfs_get_client() = %p [share]\n", clp);
418 * Mark a server as ready or failed
420 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
422 clp->cl_cons_state = state;
423 wake_up_all(&nfs_client_active_wq);
427 * Initialise the timeout values for a connection
429 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
430 unsigned int timeo, unsigned int retrans)
432 to->to_initval = timeo * HZ / 10;
433 to->to_retries = retrans;
438 case XPRT_TRANSPORT_TCP:
439 case XPRT_TRANSPORT_RDMA:
440 if (to->to_initval == 0)
441 to->to_initval = 60 * HZ;
442 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
443 to->to_initval = NFS_MAX_TCP_TIMEOUT;
444 to->to_increment = to->to_initval;
445 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
446 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
447 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
448 if (to->to_maxval < to->to_initval)
449 to->to_maxval = to->to_initval;
450 to->to_exponential = 0;
452 case XPRT_TRANSPORT_UDP:
455 to->to_initval = 11 * HZ / 10;
456 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
457 to->to_initval = NFS_MAX_UDP_TIMEOUT;
458 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
459 to->to_exponential = 1;
465 * Create an RPC client handle
467 static int nfs_create_rpc_client(struct nfs_client *clp,
468 const struct rpc_timeout *timeparms,
469 rpc_authflavor_t flavor,
472 struct rpc_clnt *clnt = NULL;
473 struct rpc_create_args args = {
474 .protocol = clp->cl_proto,
475 .address = (struct sockaddr *)&clp->cl_addr,
476 .addrsize = clp->cl_addrlen,
477 .timeout = timeparms,
478 .servername = clp->cl_hostname,
479 .program = &nfs_program,
480 .version = clp->rpc_ops->version,
481 .authflavor = flavor,
485 if (!IS_ERR(clp->cl_rpcclient))
488 clnt = rpc_create(&args);
490 dprintk("%s: cannot create RPC client. Error = %ld\n",
491 __func__, PTR_ERR(clnt));
492 return PTR_ERR(clnt);
495 clp->cl_rpcclient = clnt;
500 * Version 2 or 3 client destruction
502 static void nfs_destroy_server(struct nfs_server *server)
504 if (!(server->flags & NFS_MOUNT_NONLM))
505 nlmclnt_done(server->nlm_host);
509 * Version 2 or 3 lockd setup
511 static int nfs_start_lockd(struct nfs_server *server)
513 struct nlm_host *host;
514 struct nfs_client *clp = server->nfs_client;
515 struct nlmclnt_initdata nlm_init = {
516 .hostname = clp->cl_hostname,
517 .address = (struct sockaddr *)&clp->cl_addr,
518 .addrlen = clp->cl_addrlen,
519 .protocol = server->flags & NFS_MOUNT_TCP ?
520 IPPROTO_TCP : IPPROTO_UDP,
521 .nfs_version = clp->rpc_ops->version,
524 if (nlm_init.nfs_version > 3)
526 if (server->flags & NFS_MOUNT_NONLM)
529 host = nlmclnt_init(&nlm_init);
531 return PTR_ERR(host);
533 server->nlm_host = host;
534 server->destroy = nfs_destroy_server;
539 * Initialise an NFSv3 ACL client connection
541 #ifdef CONFIG_NFS_V3_ACL
542 static void nfs_init_server_aclclient(struct nfs_server *server)
544 if (server->nfs_client->rpc_ops->version != 3)
546 if (server->flags & NFS_MOUNT_NOACL)
549 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
550 if (IS_ERR(server->client_acl))
553 /* No errors! Assume that Sun nfsacls are supported */
554 server->caps |= NFS_CAP_ACLS;
558 server->caps &= ~NFS_CAP_ACLS;
561 static inline void nfs_init_server_aclclient(struct nfs_server *server)
563 server->flags &= ~NFS_MOUNT_NOACL;
564 server->caps &= ~NFS_CAP_ACLS;
569 * Create a general RPC client
571 static int nfs_init_server_rpcclient(struct nfs_server *server,
572 const struct rpc_timeout *timeo,
573 rpc_authflavor_t pseudoflavour)
575 struct nfs_client *clp = server->nfs_client;
577 server->client = rpc_clone_client(clp->cl_rpcclient);
578 if (IS_ERR(server->client)) {
579 dprintk("%s: couldn't create rpc_client!\n", __func__);
580 return PTR_ERR(server->client);
583 memcpy(&server->client->cl_timeout_default,
585 sizeof(server->client->cl_timeout_default));
586 server->client->cl_timeout = &server->client->cl_timeout_default;
588 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
589 struct rpc_auth *auth;
591 auth = rpcauth_create(pseudoflavour, server->client);
593 dprintk("%s: couldn't create credcache!\n", __func__);
594 return PTR_ERR(auth);
597 server->client->cl_softrtry = 0;
598 if (server->flags & NFS_MOUNT_SOFT)
599 server->client->cl_softrtry = 1;
605 * Initialise an NFS2 or NFS3 client
607 static int nfs_init_client(struct nfs_client *clp,
608 const struct rpc_timeout *timeparms,
609 const struct nfs_parsed_mount_data *data)
613 if (clp->cl_cons_state == NFS_CS_READY) {
614 /* the client is already initialised */
615 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
620 * Create a client RPC handle for doing FSSTAT with UNIX auth only
621 * - RFC 2623, sec 2.3.2
623 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
626 nfs_mark_client_ready(clp, NFS_CS_READY);
630 nfs_mark_client_ready(clp, error);
631 dprintk("<-- nfs_init_client() = xerror %d\n", error);
636 * Create a version 2 or 3 client
638 static int nfs_init_server(struct nfs_server *server,
639 const struct nfs_parsed_mount_data *data)
641 struct nfs_client_initdata cl_init = {
642 .hostname = data->nfs_server.hostname,
643 .addr = (const struct sockaddr *)&data->nfs_server.address,
644 .addrlen = data->nfs_server.addrlen,
645 .rpc_ops = &nfs_v2_clientops,
646 .proto = data->nfs_server.protocol,
648 struct rpc_timeout timeparms;
649 struct nfs_client *clp;
652 dprintk("--> nfs_init_server()\n");
655 if (data->flags & NFS_MOUNT_VER3)
656 cl_init.rpc_ops = &nfs_v3_clientops;
659 /* Allocate or find a client reference we can use */
660 clp = nfs_get_client(&cl_init);
662 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
666 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
667 data->timeo, data->retrans);
668 error = nfs_init_client(clp, &timeparms, data);
672 server->nfs_client = clp;
674 /* Initialise the client representation from the mount data */
675 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
678 server->rsize = nfs_block_size(data->rsize, NULL);
680 server->wsize = nfs_block_size(data->wsize, NULL);
682 server->acregmin = data->acregmin * HZ;
683 server->acregmax = data->acregmax * HZ;
684 server->acdirmin = data->acdirmin * HZ;
685 server->acdirmax = data->acdirmax * HZ;
687 /* Start lockd here, before we might error out */
688 error = nfs_start_lockd(server);
692 server->port = data->nfs_server.port;
694 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
698 /* Preserve the values of mount_server-related mount options */
699 if (data->mount_server.addrlen) {
700 memcpy(&server->mountd_address, &data->mount_server.address,
701 data->mount_server.addrlen);
702 server->mountd_addrlen = data->mount_server.addrlen;
704 server->mountd_version = data->mount_server.version;
705 server->mountd_port = data->mount_server.port;
706 server->mountd_protocol = data->mount_server.protocol;
708 server->namelen = data->namlen;
709 /* Create a client RPC handle for the NFSv3 ACL management interface */
710 nfs_init_server_aclclient(server);
711 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
715 server->nfs_client = NULL;
717 dprintk("<-- nfs_init_server() = xerror %d\n", error);
722 * Load up the server record from information gained in an fsinfo record
724 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
726 unsigned long max_rpc_payload;
728 /* Work out a lot of parameters */
729 if (server->rsize == 0)
730 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
731 if (server->wsize == 0)
732 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
734 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
735 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
736 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
737 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
739 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
740 if (server->rsize > max_rpc_payload)
741 server->rsize = max_rpc_payload;
742 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
743 server->rsize = NFS_MAX_FILE_IO_SIZE;
744 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
746 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
748 if (server->wsize > max_rpc_payload)
749 server->wsize = max_rpc_payload;
750 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
751 server->wsize = NFS_MAX_FILE_IO_SIZE;
752 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
753 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
755 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
756 if (server->dtsize > PAGE_CACHE_SIZE)
757 server->dtsize = PAGE_CACHE_SIZE;
758 if (server->dtsize > server->rsize)
759 server->dtsize = server->rsize;
761 if (server->flags & NFS_MOUNT_NOAC) {
762 server->acregmin = server->acregmax = 0;
763 server->acdirmin = server->acdirmax = 0;
766 server->maxfilesize = fsinfo->maxfilesize;
768 /* We're airborne Set socket buffersize */
769 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
773 * Probe filesystem information, including the FSID on v2/v3
775 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
777 struct nfs_fsinfo fsinfo;
778 struct nfs_client *clp = server->nfs_client;
781 dprintk("--> nfs_probe_fsinfo()\n");
783 if (clp->rpc_ops->set_capabilities != NULL) {
784 error = clp->rpc_ops->set_capabilities(server, mntfh);
789 fsinfo.fattr = fattr;
790 nfs_fattr_init(fattr);
791 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
795 nfs_server_set_fsinfo(server, &fsinfo);
796 error = bdi_init(&server->backing_dev_info);
801 /* Get some general file system info */
802 if (server->namelen == 0) {
803 struct nfs_pathconf pathinfo;
805 pathinfo.fattr = fattr;
806 nfs_fattr_init(fattr);
808 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
809 server->namelen = pathinfo.max_namelen;
812 dprintk("<-- nfs_probe_fsinfo() = 0\n");
816 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
821 * Copy useful information when duplicating a server record
823 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
825 target->flags = source->flags;
826 target->acregmin = source->acregmin;
827 target->acregmax = source->acregmax;
828 target->acdirmin = source->acdirmin;
829 target->acdirmax = source->acdirmax;
830 target->caps = source->caps;
834 * Allocate and initialise a server record
836 static struct nfs_server *nfs_alloc_server(void)
838 struct nfs_server *server;
840 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
844 server->client = server->client_acl = ERR_PTR(-EINVAL);
846 /* Zero out the NFS state stuff */
847 INIT_LIST_HEAD(&server->client_link);
848 INIT_LIST_HEAD(&server->master_link);
850 init_waitqueue_head(&server->active_wq);
851 atomic_set(&server->active, 0);
853 server->io_stats = nfs_alloc_iostats();
854 if (!server->io_stats) {
863 * Free up a server record
865 void nfs_free_server(struct nfs_server *server)
867 dprintk("--> nfs_free_server()\n");
869 spin_lock(&nfs_client_lock);
870 list_del(&server->client_link);
871 list_del(&server->master_link);
872 spin_unlock(&nfs_client_lock);
874 if (server->destroy != NULL)
875 server->destroy(server);
877 if (!IS_ERR(server->client_acl))
878 rpc_shutdown_client(server->client_acl);
879 if (!IS_ERR(server->client))
880 rpc_shutdown_client(server->client);
882 nfs_put_client(server->nfs_client);
884 nfs_free_iostats(server->io_stats);
885 bdi_destroy(&server->backing_dev_info);
887 nfs_release_automount_timer();
888 dprintk("<-- nfs_free_server()\n");
892 * Create a version 2 or 3 volume record
893 * - keyed on server and FSID
895 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
896 struct nfs_fh *mntfh)
898 struct nfs_server *server;
899 struct nfs_fattr fattr;
902 server = nfs_alloc_server();
904 return ERR_PTR(-ENOMEM);
906 /* Get a client representation */
907 error = nfs_init_server(server, data);
911 BUG_ON(!server->nfs_client);
912 BUG_ON(!server->nfs_client->rpc_ops);
913 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
915 /* Probe the root fh to retrieve its FSID */
916 error = nfs_probe_fsinfo(server, mntfh, &fattr);
919 if (server->nfs_client->rpc_ops->version == 3) {
920 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
921 server->namelen = NFS3_MAXNAMLEN;
922 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
923 server->caps |= NFS_CAP_READDIRPLUS;
925 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
926 server->namelen = NFS2_MAXNAMLEN;
929 if (!(fattr.valid & NFS_ATTR_FATTR)) {
930 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
932 dprintk("nfs_create_server: getattr error = %d\n", -error);
936 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
938 dprintk("Server FSID: %llx:%llx\n",
939 (unsigned long long) server->fsid.major,
940 (unsigned long long) server->fsid.minor);
942 BUG_ON(!server->nfs_client);
943 BUG_ON(!server->nfs_client->rpc_ops);
944 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
946 spin_lock(&nfs_client_lock);
947 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
948 list_add_tail(&server->master_link, &nfs_volume_list);
949 spin_unlock(&nfs_client_lock);
951 server->mount_time = jiffies;
955 nfs_free_server(server);
956 return ERR_PTR(error);
961 * Initialise an NFS4 client record
963 static int nfs4_init_client(struct nfs_client *clp,
964 const struct rpc_timeout *timeparms,
966 rpc_authflavor_t authflavour)
970 if (clp->cl_cons_state == NFS_CS_READY) {
971 /* the client is initialised already */
972 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
976 /* Check NFS protocol revision and initialize RPC op vector */
977 clp->rpc_ops = &nfs_v4_clientops;
979 error = nfs_create_rpc_client(clp, timeparms, authflavour,
980 RPC_CLNT_CREATE_DISCRTRY);
983 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
985 error = nfs_idmap_new(clp);
987 dprintk("%s: failed to create idmapper. Error = %d\n",
991 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
993 nfs_mark_client_ready(clp, NFS_CS_READY);
997 nfs_mark_client_ready(clp, error);
998 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1003 * Set up an NFS4 client
1005 static int nfs4_set_client(struct nfs_server *server,
1006 const char *hostname,
1007 const struct sockaddr *addr,
1008 const size_t addrlen,
1009 const char *ip_addr,
1010 rpc_authflavor_t authflavour,
1011 int proto, const struct rpc_timeout *timeparms)
1013 struct nfs_client_initdata cl_init = {
1014 .hostname = hostname,
1017 .rpc_ops = &nfs_v4_clientops,
1020 struct nfs_client *clp;
1023 dprintk("--> nfs4_set_client()\n");
1025 /* Allocate or find a client reference we can use */
1026 clp = nfs_get_client(&cl_init);
1028 error = PTR_ERR(clp);
1031 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
1035 server->nfs_client = clp;
1036 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1040 nfs_put_client(clp);
1042 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1047 * Create a version 4 volume record
1049 static int nfs4_init_server(struct nfs_server *server,
1050 const struct nfs_parsed_mount_data *data)
1052 struct rpc_timeout timeparms;
1055 dprintk("--> nfs4_init_server()\n");
1057 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1058 data->timeo, data->retrans);
1060 /* Get a client record */
1061 error = nfs4_set_client(server,
1062 data->nfs_server.hostname,
1063 (const struct sockaddr *)&data->nfs_server.address,
1064 data->nfs_server.addrlen,
1065 data->client_address,
1066 data->auth_flavors[0],
1067 data->nfs_server.protocol,
1072 /* Initialise the client representation from the mount data */
1073 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1074 server->caps |= NFS_CAP_ATOMIC_OPEN;
1077 server->rsize = nfs_block_size(data->rsize, NULL);
1079 server->wsize = nfs_block_size(data->wsize, NULL);
1081 server->acregmin = data->acregmin * HZ;
1082 server->acregmax = data->acregmax * HZ;
1083 server->acdirmin = data->acdirmin * HZ;
1084 server->acdirmax = data->acdirmax * HZ;
1086 server->port = data->nfs_server.port;
1088 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1092 dprintk("<-- nfs4_init_server() = %d\n", error);
1097 * Create a version 4 volume record
1098 * - keyed on server and FSID
1100 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1101 struct nfs_fh *mntfh)
1103 struct nfs_fattr fattr;
1104 struct nfs_server *server;
1107 dprintk("--> nfs4_create_server()\n");
1109 server = nfs_alloc_server();
1111 return ERR_PTR(-ENOMEM);
1113 /* set up the general RPC client */
1114 error = nfs4_init_server(server, data);
1118 BUG_ON(!server->nfs_client);
1119 BUG_ON(!server->nfs_client->rpc_ops);
1120 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1122 /* Probe the root fh to retrieve its FSID */
1123 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1127 dprintk("Server FSID: %llx:%llx\n",
1128 (unsigned long long) server->fsid.major,
1129 (unsigned long long) server->fsid.minor);
1130 dprintk("Mount FH: %d\n", mntfh->size);
1132 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1136 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1137 server->namelen = NFS4_MAXNAMLEN;
1139 BUG_ON(!server->nfs_client);
1140 BUG_ON(!server->nfs_client->rpc_ops);
1141 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1143 spin_lock(&nfs_client_lock);
1144 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1145 list_add_tail(&server->master_link, &nfs_volume_list);
1146 spin_unlock(&nfs_client_lock);
1148 server->mount_time = jiffies;
1149 dprintk("<-- nfs4_create_server() = %p\n", server);
1153 nfs_free_server(server);
1154 dprintk("<-- nfs4_create_server() = error %d\n", error);
1155 return ERR_PTR(error);
1159 * Create an NFS4 referral server record
1161 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1162 struct nfs_fh *mntfh)
1164 struct nfs_client *parent_client;
1165 struct nfs_server *server, *parent_server;
1166 struct nfs_fattr fattr;
1169 dprintk("--> nfs4_create_referral_server()\n");
1171 server = nfs_alloc_server();
1173 return ERR_PTR(-ENOMEM);
1175 parent_server = NFS_SB(data->sb);
1176 parent_client = parent_server->nfs_client;
1178 /* Get a client representation.
1179 * Note: NFSv4 always uses TCP, */
1180 error = nfs4_set_client(server, data->hostname,
1183 parent_client->cl_ipaddr,
1185 parent_server->client->cl_xprt->prot,
1186 parent_server->client->cl_timeout);
1190 /* Initialise the client representation from the parent server */
1191 nfs_server_copy_userdata(server, parent_server);
1192 server->caps |= NFS_CAP_ATOMIC_OPEN;
1194 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1198 BUG_ON(!server->nfs_client);
1199 BUG_ON(!server->nfs_client->rpc_ops);
1200 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1202 /* Probe the root fh to retrieve its FSID and filehandle */
1203 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1207 /* probe the filesystem info for this server filesystem */
1208 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1212 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1213 server->namelen = NFS4_MAXNAMLEN;
1215 dprintk("Referral FSID: %llx:%llx\n",
1216 (unsigned long long) server->fsid.major,
1217 (unsigned long long) server->fsid.minor);
1219 spin_lock(&nfs_client_lock);
1220 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1221 list_add_tail(&server->master_link, &nfs_volume_list);
1222 spin_unlock(&nfs_client_lock);
1224 server->mount_time = jiffies;
1226 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1230 nfs_free_server(server);
1231 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1232 return ERR_PTR(error);
1235 #endif /* CONFIG_NFS_V4 */
1238 * Clone an NFS2, NFS3 or NFS4 server record
1240 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1242 struct nfs_fattr *fattr)
1244 struct nfs_server *server;
1245 struct nfs_fattr fattr_fsinfo;
1248 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1249 (unsigned long long) fattr->fsid.major,
1250 (unsigned long long) fattr->fsid.minor);
1252 server = nfs_alloc_server();
1254 return ERR_PTR(-ENOMEM);
1256 /* Copy data from the source */
1257 server->nfs_client = source->nfs_client;
1258 atomic_inc(&server->nfs_client->cl_count);
1259 nfs_server_copy_userdata(server, source);
1261 server->fsid = fattr->fsid;
1263 error = nfs_init_server_rpcclient(server,
1264 source->client->cl_timeout,
1265 source->client->cl_auth->au_flavor);
1267 goto out_free_server;
1268 if (!IS_ERR(source->client_acl))
1269 nfs_init_server_aclclient(server);
1271 /* probe the filesystem info for this server filesystem */
1272 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1274 goto out_free_server;
1276 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1277 server->namelen = NFS4_MAXNAMLEN;
1279 dprintk("Cloned FSID: %llx:%llx\n",
1280 (unsigned long long) server->fsid.major,
1281 (unsigned long long) server->fsid.minor);
1283 error = nfs_start_lockd(server);
1285 goto out_free_server;
1287 spin_lock(&nfs_client_lock);
1288 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1289 list_add_tail(&server->master_link, &nfs_volume_list);
1290 spin_unlock(&nfs_client_lock);
1292 server->mount_time = jiffies;
1294 dprintk("<-- nfs_clone_server() = %p\n", server);
1298 nfs_free_server(server);
1299 dprintk("<-- nfs_clone_server() = error %d\n", error);
1300 return ERR_PTR(error);
1303 #ifdef CONFIG_PROC_FS
1304 static struct proc_dir_entry *proc_fs_nfs;
1306 static int nfs_server_list_open(struct inode *inode, struct file *file);
1307 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1308 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1309 static void nfs_server_list_stop(struct seq_file *p, void *v);
1310 static int nfs_server_list_show(struct seq_file *m, void *v);
1312 static struct seq_operations nfs_server_list_ops = {
1313 .start = nfs_server_list_start,
1314 .next = nfs_server_list_next,
1315 .stop = nfs_server_list_stop,
1316 .show = nfs_server_list_show,
1319 static const struct file_operations nfs_server_list_fops = {
1320 .open = nfs_server_list_open,
1322 .llseek = seq_lseek,
1323 .release = seq_release,
1324 .owner = THIS_MODULE,
1327 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1328 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1329 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1330 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1331 static int nfs_volume_list_show(struct seq_file *m, void *v);
1333 static struct seq_operations nfs_volume_list_ops = {
1334 .start = nfs_volume_list_start,
1335 .next = nfs_volume_list_next,
1336 .stop = nfs_volume_list_stop,
1337 .show = nfs_volume_list_show,
1340 static const struct file_operations nfs_volume_list_fops = {
1341 .open = nfs_volume_list_open,
1343 .llseek = seq_lseek,
1344 .release = seq_release,
1345 .owner = THIS_MODULE,
1349 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1352 static int nfs_server_list_open(struct inode *inode, struct file *file)
1357 ret = seq_open(file, &nfs_server_list_ops);
1361 m = file->private_data;
1362 m->private = PDE(inode)->data;
1368 * set up the iterator to start reading from the server list and return the first item
1370 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1372 /* lock the list against modification */
1373 spin_lock(&nfs_client_lock);
1374 return seq_list_start_head(&nfs_client_list, *_pos);
1378 * move to next server
1380 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1382 return seq_list_next(v, &nfs_client_list, pos);
1386 * clean up after reading from the transports list
1388 static void nfs_server_list_stop(struct seq_file *p, void *v)
1390 spin_unlock(&nfs_client_lock);
1394 * display a header line followed by a load of call lines
1396 static int nfs_server_list_show(struct seq_file *m, void *v)
1398 struct nfs_client *clp;
1400 /* display header on line 1 */
1401 if (v == &nfs_client_list) {
1402 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1406 /* display one transport per line on subsequent lines */
1407 clp = list_entry(v, struct nfs_client, cl_share_link);
1409 seq_printf(m, "v%u %s %s %3d %s\n",
1410 clp->rpc_ops->version,
1411 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1412 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1413 atomic_read(&clp->cl_count),
1420 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1422 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1427 ret = seq_open(file, &nfs_volume_list_ops);
1431 m = file->private_data;
1432 m->private = PDE(inode)->data;
1438 * set up the iterator to start reading from the volume list and return the first item
1440 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1442 /* lock the list against modification */
1443 spin_lock(&nfs_client_lock);
1444 return seq_list_start_head(&nfs_volume_list, *_pos);
1448 * move to next volume
1450 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1452 return seq_list_next(v, &nfs_volume_list, pos);
1456 * clean up after reading from the transports list
1458 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1460 spin_unlock(&nfs_client_lock);
1464 * display a header line followed by a load of call lines
1466 static int nfs_volume_list_show(struct seq_file *m, void *v)
1468 struct nfs_server *server;
1469 struct nfs_client *clp;
1470 char dev[8], fsid[17];
1472 /* display header on line 1 */
1473 if (v == &nfs_volume_list) {
1474 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1477 /* display one transport per line on subsequent lines */
1478 server = list_entry(v, struct nfs_server, master_link);
1479 clp = server->nfs_client;
1481 snprintf(dev, 8, "%u:%u",
1482 MAJOR(server->s_dev), MINOR(server->s_dev));
1484 snprintf(fsid, 17, "%llx:%llx",
1485 (unsigned long long) server->fsid.major,
1486 (unsigned long long) server->fsid.minor);
1488 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1489 clp->rpc_ops->version,
1490 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1491 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1499 * initialise the /proc/fs/nfsfs/ directory
1501 int __init nfs_fs_proc_init(void)
1503 struct proc_dir_entry *p;
1505 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1509 proc_fs_nfs->owner = THIS_MODULE;
1511 /* a file of servers with which we're dealing */
1512 p = proc_create("servers", S_IFREG|S_IRUGO,
1513 proc_fs_nfs, &nfs_server_list_fops);
1517 /* a file of volumes that we have mounted */
1518 p = proc_create("volumes", S_IFREG|S_IRUGO,
1519 proc_fs_nfs, &nfs_volume_list_fops);
1525 remove_proc_entry("servers", proc_fs_nfs);
1527 remove_proc_entry("fs/nfsfs", NULL);
1533 * clean up the /proc/fs/nfsfs/ directory
1535 void nfs_fs_proc_exit(void)
1537 remove_proc_entry("volumes", proc_fs_nfs);
1538 remove_proc_entry("servers", proc_fs_nfs);
1539 remove_proc_entry("fs/nfsfs", NULL);
1542 #endif /* CONFIG_PROC_FS */