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;
116 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
119 clp->rpc_ops = cl_init->rpc_ops;
121 if (cl_init->rpc_ops->version == 4) {
122 if (nfs_callback_up() < 0)
124 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
127 atomic_set(&clp->cl_count, 1);
128 clp->cl_cons_state = NFS_CS_INITING;
130 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
131 clp->cl_addrlen = cl_init->addrlen;
133 if (cl_init->hostname) {
134 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
135 if (!clp->cl_hostname)
139 INIT_LIST_HEAD(&clp->cl_superblocks);
140 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142 clp->cl_proto = cl_init->proto;
145 init_rwsem(&clp->cl_sem);
146 INIT_LIST_HEAD(&clp->cl_delegations);
147 spin_lock_init(&clp->cl_lock);
148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
157 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
165 static void nfs4_shutdown_client(struct nfs_client *clp)
168 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
169 nfs4_kill_renewd(clp);
170 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
171 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
172 nfs_idmap_delete(clp);
174 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
179 * Destroy a shared client record
181 static void nfs_free_client(struct nfs_client *clp)
183 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
185 nfs4_shutdown_client(clp);
187 /* -EIO all pending I/O */
188 if (!IS_ERR(clp->cl_rpcclient))
189 rpc_shutdown_client(clp->cl_rpcclient);
191 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
194 kfree(clp->cl_hostname);
197 dprintk("<-- nfs_free_client()\n");
201 * Release a reference to a shared client record
203 void nfs_put_client(struct nfs_client *clp)
208 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
210 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
211 list_del(&clp->cl_share_link);
212 spin_unlock(&nfs_client_lock);
214 BUG_ON(!list_empty(&clp->cl_superblocks));
216 nfs_free_client(clp);
220 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
221 const struct sockaddr_in *sa2)
223 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
226 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
227 const struct sockaddr_in6 *sa2)
229 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
232 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
233 const struct sockaddr *sa2)
235 switch (sa1->sa_family) {
237 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
238 (const struct sockaddr_in *)sa2);
240 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
241 (const struct sockaddr_in6 *)sa2);
247 * Find a client by IP address and protocol version
248 * - returns NULL if no such client
250 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
252 struct nfs_client *clp;
254 spin_lock(&nfs_client_lock);
255 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
256 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
258 /* Don't match clients that failed to initialise properly */
259 if (clp->cl_cons_state != NFS_CS_READY)
262 /* Different NFS versions cannot share the same nfs_client */
263 if (clp->rpc_ops->version != nfsversion)
266 if (addr->sa_family != clap->sa_family)
268 /* Match only the IP address, not the port number */
269 if (!nfs_sockaddr_match_ipaddr(addr, clap))
272 atomic_inc(&clp->cl_count);
273 spin_unlock(&nfs_client_lock);
276 spin_unlock(&nfs_client_lock);
281 * Find a client by IP address and protocol version
282 * - returns NULL if no such client
284 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
286 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
287 u32 nfsvers = clp->rpc_ops->version;
289 spin_lock(&nfs_client_lock);
290 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
291 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
293 /* Don't match clients that failed to initialise properly */
294 if (clp->cl_cons_state != NFS_CS_READY)
297 /* Different NFS versions cannot share the same nfs_client */
298 if (clp->rpc_ops->version != nfsvers)
301 if (sap->sa_family != clap->sa_family)
303 /* Match only the IP address, not the port number */
304 if (!nfs_sockaddr_match_ipaddr(sap, clap))
307 atomic_inc(&clp->cl_count);
308 spin_unlock(&nfs_client_lock);
311 spin_unlock(&nfs_client_lock);
316 * Find an nfs_client on the list that matches the initialisation data
319 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
321 struct nfs_client *clp;
323 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
324 /* Don't match clients that failed to initialise properly */
325 if (clp->cl_cons_state < 0)
328 /* Different NFS versions cannot share the same nfs_client */
329 if (clp->rpc_ops != data->rpc_ops)
332 if (clp->cl_proto != data->proto)
335 /* Match the full socket address */
336 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
339 atomic_inc(&clp->cl_count);
346 * Look up a client by IP address and protocol version
347 * - creates a new record if one doesn't yet exist
349 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
351 struct nfs_client *clp, *new = NULL;
354 dprintk("--> nfs_get_client(%s,v%u)\n",
355 cl_init->hostname ?: "", cl_init->rpc_ops->version);
357 /* see if the client already exists */
359 spin_lock(&nfs_client_lock);
361 clp = nfs_match_client(cl_init);
367 spin_unlock(&nfs_client_lock);
369 new = nfs_alloc_client(cl_init);
372 return ERR_PTR(-ENOMEM);
374 /* install a new client and return with it unready */
377 list_add(&clp->cl_share_link, &nfs_client_list);
378 spin_unlock(&nfs_client_lock);
379 dprintk("--> nfs_get_client() = %p [new]\n", clp);
382 /* found an existing client
383 * - make sure it's ready before returning
386 spin_unlock(&nfs_client_lock);
389 nfs_free_client(new);
391 error = wait_event_killable(nfs_client_active_wq,
392 clp->cl_cons_state != NFS_CS_INITING);
395 return ERR_PTR(-ERESTARTSYS);
398 if (clp->cl_cons_state < NFS_CS_READY) {
399 error = clp->cl_cons_state;
401 return ERR_PTR(error);
404 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
406 dprintk("--> nfs_get_client() = %p [share]\n", clp);
411 * Mark a server as ready or failed
413 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
415 clp->cl_cons_state = state;
416 wake_up_all(&nfs_client_active_wq);
420 * Initialise the timeout values for a connection
422 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
423 unsigned int timeo, unsigned int retrans)
425 to->to_initval = timeo * HZ / 10;
426 to->to_retries = retrans;
431 case XPRT_TRANSPORT_TCP:
432 case XPRT_TRANSPORT_RDMA:
433 if (to->to_initval == 0)
434 to->to_initval = 60 * HZ;
435 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
436 to->to_initval = NFS_MAX_TCP_TIMEOUT;
437 to->to_increment = to->to_initval;
438 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
439 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
440 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
441 if (to->to_maxval < to->to_initval)
442 to->to_maxval = to->to_initval;
443 to->to_exponential = 0;
445 case XPRT_TRANSPORT_UDP:
448 to->to_initval = 11 * HZ / 10;
449 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
450 to->to_initval = NFS_MAX_UDP_TIMEOUT;
451 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
452 to->to_exponential = 1;
458 * Create an RPC client handle
460 static int nfs_create_rpc_client(struct nfs_client *clp,
461 const struct rpc_timeout *timeparms,
462 rpc_authflavor_t flavor,
465 struct rpc_clnt *clnt = NULL;
466 struct rpc_create_args args = {
467 .protocol = clp->cl_proto,
468 .address = (struct sockaddr *)&clp->cl_addr,
469 .addrsize = clp->cl_addrlen,
470 .timeout = timeparms,
471 .servername = clp->cl_hostname,
472 .program = &nfs_program,
473 .version = clp->rpc_ops->version,
474 .authflavor = flavor,
478 if (!IS_ERR(clp->cl_rpcclient))
481 clnt = rpc_create(&args);
483 dprintk("%s: cannot create RPC client. Error = %ld\n",
484 __FUNCTION__, PTR_ERR(clnt));
485 return PTR_ERR(clnt);
488 clp->cl_rpcclient = clnt;
493 * Version 2 or 3 client destruction
495 static void nfs_destroy_server(struct nfs_server *server)
497 if (!(server->flags & NFS_MOUNT_NONLM))
498 nlmclnt_done(server->nlm_host);
502 * Version 2 or 3 lockd setup
504 static int nfs_start_lockd(struct nfs_server *server)
506 struct nlm_host *host;
507 struct nfs_client *clp = server->nfs_client;
508 struct nlmclnt_initdata nlm_init = {
509 .hostname = clp->cl_hostname,
510 .address = (struct sockaddr *)&clp->cl_addr,
511 .addrlen = clp->cl_addrlen,
512 .protocol = server->flags & NFS_MOUNT_TCP ?
513 IPPROTO_TCP : IPPROTO_UDP,
514 .nfs_version = clp->rpc_ops->version,
517 if (nlm_init.nfs_version > 3)
519 if (server->flags & NFS_MOUNT_NONLM)
522 host = nlmclnt_init(&nlm_init);
524 return PTR_ERR(host);
526 server->nlm_host = host;
527 server->destroy = nfs_destroy_server;
532 * Initialise an NFSv3 ACL client connection
534 #ifdef CONFIG_NFS_V3_ACL
535 static void nfs_init_server_aclclient(struct nfs_server *server)
537 if (server->nfs_client->rpc_ops->version != 3)
539 if (server->flags & NFS_MOUNT_NOACL)
542 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
543 if (IS_ERR(server->client_acl))
546 /* No errors! Assume that Sun nfsacls are supported */
547 server->caps |= NFS_CAP_ACLS;
551 server->caps &= ~NFS_CAP_ACLS;
554 static inline void nfs_init_server_aclclient(struct nfs_server *server)
556 server->flags &= ~NFS_MOUNT_NOACL;
557 server->caps &= ~NFS_CAP_ACLS;
562 * Create a general RPC client
564 static int nfs_init_server_rpcclient(struct nfs_server *server,
565 const struct rpc_timeout *timeo,
566 rpc_authflavor_t pseudoflavour)
568 struct nfs_client *clp = server->nfs_client;
570 server->client = rpc_clone_client(clp->cl_rpcclient);
571 if (IS_ERR(server->client)) {
572 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
573 return PTR_ERR(server->client);
576 memcpy(&server->client->cl_timeout_default,
578 sizeof(server->client->cl_timeout_default));
579 server->client->cl_timeout = &server->client->cl_timeout_default;
581 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
582 struct rpc_auth *auth;
584 auth = rpcauth_create(pseudoflavour, server->client);
586 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
587 return PTR_ERR(auth);
590 server->client->cl_softrtry = 0;
591 if (server->flags & NFS_MOUNT_SOFT)
592 server->client->cl_softrtry = 1;
598 * Initialise an NFS2 or NFS3 client
600 static int nfs_init_client(struct nfs_client *clp,
601 const struct rpc_timeout *timeparms,
602 const struct nfs_parsed_mount_data *data)
606 if (clp->cl_cons_state == NFS_CS_READY) {
607 /* the client is already initialised */
608 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
613 * Create a client RPC handle for doing FSSTAT with UNIX auth only
614 * - RFC 2623, sec 2.3.2
616 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
619 nfs_mark_client_ready(clp, NFS_CS_READY);
623 nfs_mark_client_ready(clp, error);
624 dprintk("<-- nfs_init_client() = xerror %d\n", error);
629 * Create a version 2 or 3 client
631 static int nfs_init_server(struct nfs_server *server,
632 const struct nfs_parsed_mount_data *data)
634 struct nfs_client_initdata cl_init = {
635 .hostname = data->nfs_server.hostname,
636 .addr = (const struct sockaddr *)&data->nfs_server.address,
637 .addrlen = data->nfs_server.addrlen,
638 .rpc_ops = &nfs_v2_clientops,
639 .proto = data->nfs_server.protocol,
641 struct rpc_timeout timeparms;
642 struct nfs_client *clp;
645 dprintk("--> nfs_init_server()\n");
648 if (data->flags & NFS_MOUNT_VER3)
649 cl_init.rpc_ops = &nfs_v3_clientops;
652 /* Allocate or find a client reference we can use */
653 clp = nfs_get_client(&cl_init);
655 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
659 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
660 data->timeo, data->retrans);
661 error = nfs_init_client(clp, &timeparms, data);
665 server->nfs_client = clp;
667 /* Initialise the client representation from the mount data */
668 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
671 server->rsize = nfs_block_size(data->rsize, NULL);
673 server->wsize = nfs_block_size(data->wsize, NULL);
675 server->acregmin = data->acregmin * HZ;
676 server->acregmax = data->acregmax * HZ;
677 server->acdirmin = data->acdirmin * HZ;
678 server->acdirmax = data->acdirmax * HZ;
680 /* Start lockd here, before we might error out */
681 error = nfs_start_lockd(server);
685 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
689 server->namelen = data->namlen;
690 /* Create a client RPC handle for the NFSv3 ACL management interface */
691 nfs_init_server_aclclient(server);
692 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
696 server->nfs_client = NULL;
698 dprintk("<-- nfs_init_server() = xerror %d\n", error);
703 * Load up the server record from information gained in an fsinfo record
705 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
707 unsigned long max_rpc_payload;
709 /* Work out a lot of parameters */
710 if (server->rsize == 0)
711 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
712 if (server->wsize == 0)
713 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
715 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
716 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
717 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
718 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
720 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
721 if (server->rsize > max_rpc_payload)
722 server->rsize = max_rpc_payload;
723 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
724 server->rsize = NFS_MAX_FILE_IO_SIZE;
725 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
727 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
729 if (server->wsize > max_rpc_payload)
730 server->wsize = max_rpc_payload;
731 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
732 server->wsize = NFS_MAX_FILE_IO_SIZE;
733 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
734 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
736 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
737 if (server->dtsize > PAGE_CACHE_SIZE)
738 server->dtsize = PAGE_CACHE_SIZE;
739 if (server->dtsize > server->rsize)
740 server->dtsize = server->rsize;
742 if (server->flags & NFS_MOUNT_NOAC) {
743 server->acregmin = server->acregmax = 0;
744 server->acdirmin = server->acdirmax = 0;
747 server->maxfilesize = fsinfo->maxfilesize;
749 /* We're airborne Set socket buffersize */
750 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
754 * Probe filesystem information, including the FSID on v2/v3
756 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
758 struct nfs_fsinfo fsinfo;
759 struct nfs_client *clp = server->nfs_client;
762 dprintk("--> nfs_probe_fsinfo()\n");
764 if (clp->rpc_ops->set_capabilities != NULL) {
765 error = clp->rpc_ops->set_capabilities(server, mntfh);
770 fsinfo.fattr = fattr;
771 nfs_fattr_init(fattr);
772 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
776 nfs_server_set_fsinfo(server, &fsinfo);
777 error = bdi_init(&server->backing_dev_info);
782 /* Get some general file system info */
783 if (server->namelen == 0) {
784 struct nfs_pathconf pathinfo;
786 pathinfo.fattr = fattr;
787 nfs_fattr_init(fattr);
789 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
790 server->namelen = pathinfo.max_namelen;
793 dprintk("<-- nfs_probe_fsinfo() = 0\n");
797 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
802 * Copy useful information when duplicating a server record
804 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
806 target->flags = source->flags;
807 target->acregmin = source->acregmin;
808 target->acregmax = source->acregmax;
809 target->acdirmin = source->acdirmin;
810 target->acdirmax = source->acdirmax;
811 target->caps = source->caps;
815 * Allocate and initialise a server record
817 static struct nfs_server *nfs_alloc_server(void)
819 struct nfs_server *server;
821 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
825 server->client = server->client_acl = ERR_PTR(-EINVAL);
827 /* Zero out the NFS state stuff */
828 INIT_LIST_HEAD(&server->client_link);
829 INIT_LIST_HEAD(&server->master_link);
831 init_waitqueue_head(&server->active_wq);
832 atomic_set(&server->active, 0);
834 server->io_stats = nfs_alloc_iostats();
835 if (!server->io_stats) {
844 * Free up a server record
846 void nfs_free_server(struct nfs_server *server)
848 dprintk("--> nfs_free_server()\n");
850 spin_lock(&nfs_client_lock);
851 list_del(&server->client_link);
852 list_del(&server->master_link);
853 spin_unlock(&nfs_client_lock);
855 if (server->destroy != NULL)
856 server->destroy(server);
858 if (!IS_ERR(server->client_acl))
859 rpc_shutdown_client(server->client_acl);
860 if (!IS_ERR(server->client))
861 rpc_shutdown_client(server->client);
863 nfs_put_client(server->nfs_client);
865 nfs_free_iostats(server->io_stats);
866 bdi_destroy(&server->backing_dev_info);
868 nfs_release_automount_timer();
869 dprintk("<-- nfs_free_server()\n");
873 * Create a version 2 or 3 volume record
874 * - keyed on server and FSID
876 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
877 struct nfs_fh *mntfh)
879 struct nfs_server *server;
880 struct nfs_fattr fattr;
883 server = nfs_alloc_server();
885 return ERR_PTR(-ENOMEM);
887 /* Get a client representation */
888 error = nfs_init_server(server, data);
892 BUG_ON(!server->nfs_client);
893 BUG_ON(!server->nfs_client->rpc_ops);
894 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
896 /* Probe the root fh to retrieve its FSID */
897 error = nfs_probe_fsinfo(server, mntfh, &fattr);
900 if (server->nfs_client->rpc_ops->version == 3) {
901 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
902 server->namelen = NFS3_MAXNAMLEN;
903 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
904 server->caps |= NFS_CAP_READDIRPLUS;
906 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
907 server->namelen = NFS2_MAXNAMLEN;
910 if (!(fattr.valid & NFS_ATTR_FATTR)) {
911 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
913 dprintk("nfs_create_server: getattr error = %d\n", -error);
917 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
919 dprintk("Server FSID: %llx:%llx\n",
920 (unsigned long long) server->fsid.major,
921 (unsigned long long) server->fsid.minor);
923 BUG_ON(!server->nfs_client);
924 BUG_ON(!server->nfs_client->rpc_ops);
925 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
927 spin_lock(&nfs_client_lock);
928 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
929 list_add_tail(&server->master_link, &nfs_volume_list);
930 spin_unlock(&nfs_client_lock);
932 server->mount_time = jiffies;
936 nfs_free_server(server);
937 return ERR_PTR(error);
942 * Initialise an NFS4 client record
944 static int nfs4_init_client(struct nfs_client *clp,
945 const struct rpc_timeout *timeparms,
947 rpc_authflavor_t authflavour)
951 if (clp->cl_cons_state == NFS_CS_READY) {
952 /* the client is initialised already */
953 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
957 /* Check NFS protocol revision and initialize RPC op vector */
958 clp->rpc_ops = &nfs_v4_clientops;
960 error = nfs_create_rpc_client(clp, timeparms, authflavour,
961 RPC_CLNT_CREATE_DISCRTRY);
964 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
966 error = nfs_idmap_new(clp);
968 dprintk("%s: failed to create idmapper. Error = %d\n",
969 __FUNCTION__, error);
972 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
974 nfs_mark_client_ready(clp, NFS_CS_READY);
978 nfs_mark_client_ready(clp, error);
979 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
984 * Set up an NFS4 client
986 static int nfs4_set_client(struct nfs_server *server,
987 const char *hostname,
988 const struct sockaddr *addr,
989 const size_t addrlen,
991 rpc_authflavor_t authflavour,
992 int proto, const struct rpc_timeout *timeparms)
994 struct nfs_client_initdata cl_init = {
995 .hostname = hostname,
998 .rpc_ops = &nfs_v4_clientops,
1001 struct nfs_client *clp;
1004 dprintk("--> nfs4_set_client()\n");
1006 /* Allocate or find a client reference we can use */
1007 clp = nfs_get_client(&cl_init);
1009 error = PTR_ERR(clp);
1012 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
1016 server->nfs_client = clp;
1017 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1021 nfs_put_client(clp);
1023 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1028 * Create a version 4 volume record
1030 static int nfs4_init_server(struct nfs_server *server,
1031 const struct nfs_parsed_mount_data *data)
1033 struct rpc_timeout timeparms;
1036 dprintk("--> nfs4_init_server()\n");
1038 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1039 data->timeo, data->retrans);
1041 /* Get a client record */
1042 error = nfs4_set_client(server,
1043 data->nfs_server.hostname,
1044 (const struct sockaddr *)&data->nfs_server.address,
1045 data->nfs_server.addrlen,
1046 data->client_address,
1047 data->auth_flavors[0],
1048 data->nfs_server.protocol,
1053 /* Initialise the client representation from the mount data */
1054 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1055 server->caps |= NFS_CAP_ATOMIC_OPEN;
1058 server->rsize = nfs_block_size(data->rsize, NULL);
1060 server->wsize = nfs_block_size(data->wsize, NULL);
1062 server->acregmin = data->acregmin * HZ;
1063 server->acregmax = data->acregmax * HZ;
1064 server->acdirmin = data->acdirmin * HZ;
1065 server->acdirmax = data->acdirmax * HZ;
1067 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1071 dprintk("<-- nfs4_init_server() = %d\n", error);
1076 * Create a version 4 volume record
1077 * - keyed on server and FSID
1079 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1080 struct nfs_fh *mntfh)
1082 struct nfs_fattr fattr;
1083 struct nfs_server *server;
1086 dprintk("--> nfs4_create_server()\n");
1088 server = nfs_alloc_server();
1090 return ERR_PTR(-ENOMEM);
1092 /* set up the general RPC client */
1093 error = nfs4_init_server(server, data);
1097 BUG_ON(!server->nfs_client);
1098 BUG_ON(!server->nfs_client->rpc_ops);
1099 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1101 /* Probe the root fh to retrieve its FSID */
1102 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1106 dprintk("Server FSID: %llx:%llx\n",
1107 (unsigned long long) server->fsid.major,
1108 (unsigned long long) server->fsid.minor);
1109 dprintk("Mount FH: %d\n", mntfh->size);
1111 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1115 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1116 server->namelen = NFS4_MAXNAMLEN;
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 spin_lock(&nfs_client_lock);
1123 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1124 list_add_tail(&server->master_link, &nfs_volume_list);
1125 spin_unlock(&nfs_client_lock);
1127 server->mount_time = jiffies;
1128 dprintk("<-- nfs4_create_server() = %p\n", server);
1132 nfs_free_server(server);
1133 dprintk("<-- nfs4_create_server() = error %d\n", error);
1134 return ERR_PTR(error);
1138 * Create an NFS4 referral server record
1140 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1141 struct nfs_fh *mntfh)
1143 struct nfs_client *parent_client;
1144 struct nfs_server *server, *parent_server;
1145 struct nfs_fattr fattr;
1148 dprintk("--> nfs4_create_referral_server()\n");
1150 server = nfs_alloc_server();
1152 return ERR_PTR(-ENOMEM);
1154 parent_server = NFS_SB(data->sb);
1155 parent_client = parent_server->nfs_client;
1157 /* Get a client representation.
1158 * Note: NFSv4 always uses TCP, */
1159 error = nfs4_set_client(server, data->hostname,
1162 parent_client->cl_ipaddr,
1164 parent_server->client->cl_xprt->prot,
1165 parent_server->client->cl_timeout);
1169 /* Initialise the client representation from the parent server */
1170 nfs_server_copy_userdata(server, parent_server);
1171 server->caps |= NFS_CAP_ATOMIC_OPEN;
1173 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1177 BUG_ON(!server->nfs_client);
1178 BUG_ON(!server->nfs_client->rpc_ops);
1179 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1181 /* Probe the root fh to retrieve its FSID and filehandle */
1182 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1186 /* probe the filesystem info for this server filesystem */
1187 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1191 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1192 server->namelen = NFS4_MAXNAMLEN;
1194 dprintk("Referral FSID: %llx:%llx\n",
1195 (unsigned long long) server->fsid.major,
1196 (unsigned long long) server->fsid.minor);
1198 spin_lock(&nfs_client_lock);
1199 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1200 list_add_tail(&server->master_link, &nfs_volume_list);
1201 spin_unlock(&nfs_client_lock);
1203 server->mount_time = jiffies;
1205 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1209 nfs_free_server(server);
1210 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1211 return ERR_PTR(error);
1214 #endif /* CONFIG_NFS_V4 */
1217 * Clone an NFS2, NFS3 or NFS4 server record
1219 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1221 struct nfs_fattr *fattr)
1223 struct nfs_server *server;
1224 struct nfs_fattr fattr_fsinfo;
1227 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1228 (unsigned long long) fattr->fsid.major,
1229 (unsigned long long) fattr->fsid.minor);
1231 server = nfs_alloc_server();
1233 return ERR_PTR(-ENOMEM);
1235 /* Copy data from the source */
1236 server->nfs_client = source->nfs_client;
1237 atomic_inc(&server->nfs_client->cl_count);
1238 nfs_server_copy_userdata(server, source);
1240 server->fsid = fattr->fsid;
1242 error = nfs_init_server_rpcclient(server,
1243 source->client->cl_timeout,
1244 source->client->cl_auth->au_flavor);
1246 goto out_free_server;
1247 if (!IS_ERR(source->client_acl))
1248 nfs_init_server_aclclient(server);
1250 /* probe the filesystem info for this server filesystem */
1251 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1253 goto out_free_server;
1255 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1256 server->namelen = NFS4_MAXNAMLEN;
1258 dprintk("Cloned FSID: %llx:%llx\n",
1259 (unsigned long long) server->fsid.major,
1260 (unsigned long long) server->fsid.minor);
1262 error = nfs_start_lockd(server);
1264 goto out_free_server;
1266 spin_lock(&nfs_client_lock);
1267 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1268 list_add_tail(&server->master_link, &nfs_volume_list);
1269 spin_unlock(&nfs_client_lock);
1271 server->mount_time = jiffies;
1273 dprintk("<-- nfs_clone_server() = %p\n", server);
1277 nfs_free_server(server);
1278 dprintk("<-- nfs_clone_server() = error %d\n", error);
1279 return ERR_PTR(error);
1282 #ifdef CONFIG_PROC_FS
1283 static struct proc_dir_entry *proc_fs_nfs;
1285 static int nfs_server_list_open(struct inode *inode, struct file *file);
1286 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1287 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1288 static void nfs_server_list_stop(struct seq_file *p, void *v);
1289 static int nfs_server_list_show(struct seq_file *m, void *v);
1291 static struct seq_operations nfs_server_list_ops = {
1292 .start = nfs_server_list_start,
1293 .next = nfs_server_list_next,
1294 .stop = nfs_server_list_stop,
1295 .show = nfs_server_list_show,
1298 static const struct file_operations nfs_server_list_fops = {
1299 .open = nfs_server_list_open,
1301 .llseek = seq_lseek,
1302 .release = seq_release,
1305 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1306 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1307 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1308 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1309 static int nfs_volume_list_show(struct seq_file *m, void *v);
1311 static struct seq_operations nfs_volume_list_ops = {
1312 .start = nfs_volume_list_start,
1313 .next = nfs_volume_list_next,
1314 .stop = nfs_volume_list_stop,
1315 .show = nfs_volume_list_show,
1318 static const struct file_operations nfs_volume_list_fops = {
1319 .open = nfs_volume_list_open,
1321 .llseek = seq_lseek,
1322 .release = seq_release,
1326 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1329 static int nfs_server_list_open(struct inode *inode, struct file *file)
1334 ret = seq_open(file, &nfs_server_list_ops);
1338 m = file->private_data;
1339 m->private = PDE(inode)->data;
1345 * set up the iterator to start reading from the server list and return the first item
1347 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1349 /* lock the list against modification */
1350 spin_lock(&nfs_client_lock);
1351 return seq_list_start_head(&nfs_client_list, *_pos);
1355 * move to next server
1357 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1359 return seq_list_next(v, &nfs_client_list, pos);
1363 * clean up after reading from the transports list
1365 static void nfs_server_list_stop(struct seq_file *p, void *v)
1367 spin_unlock(&nfs_client_lock);
1371 * display a header line followed by a load of call lines
1373 static int nfs_server_list_show(struct seq_file *m, void *v)
1375 struct nfs_client *clp;
1377 /* display header on line 1 */
1378 if (v == &nfs_client_list) {
1379 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1383 /* display one transport per line on subsequent lines */
1384 clp = list_entry(v, struct nfs_client, cl_share_link);
1386 seq_printf(m, "v%u %s %s %3d %s\n",
1387 clp->rpc_ops->version,
1388 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1389 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1390 atomic_read(&clp->cl_count),
1397 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1399 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1404 ret = seq_open(file, &nfs_volume_list_ops);
1408 m = file->private_data;
1409 m->private = PDE(inode)->data;
1415 * set up the iterator to start reading from the volume list and return the first item
1417 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1419 /* lock the list against modification */
1420 spin_lock(&nfs_client_lock);
1421 return seq_list_start_head(&nfs_volume_list, *_pos);
1425 * move to next volume
1427 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1429 return seq_list_next(v, &nfs_volume_list, pos);
1433 * clean up after reading from the transports list
1435 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1437 spin_unlock(&nfs_client_lock);
1441 * display a header line followed by a load of call lines
1443 static int nfs_volume_list_show(struct seq_file *m, void *v)
1445 struct nfs_server *server;
1446 struct nfs_client *clp;
1447 char dev[8], fsid[17];
1449 /* display header on line 1 */
1450 if (v == &nfs_volume_list) {
1451 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1454 /* display one transport per line on subsequent lines */
1455 server = list_entry(v, struct nfs_server, master_link);
1456 clp = server->nfs_client;
1458 snprintf(dev, 8, "%u:%u",
1459 MAJOR(server->s_dev), MINOR(server->s_dev));
1461 snprintf(fsid, 17, "%llx:%llx",
1462 (unsigned long long) server->fsid.major,
1463 (unsigned long long) server->fsid.minor);
1465 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1466 clp->rpc_ops->version,
1467 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1468 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1476 * initialise the /proc/fs/nfsfs/ directory
1478 int __init nfs_fs_proc_init(void)
1480 struct proc_dir_entry *p;
1482 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1486 proc_fs_nfs->owner = THIS_MODULE;
1488 /* a file of servers with which we're dealing */
1489 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1493 p->proc_fops = &nfs_server_list_fops;
1494 p->owner = THIS_MODULE;
1496 /* a file of volumes that we have mounted */
1497 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1501 p->proc_fops = &nfs_volume_list_fops;
1502 p->owner = THIS_MODULE;
1506 remove_proc_entry("servers", proc_fs_nfs);
1508 remove_proc_entry("nfsfs", proc_root_fs);
1514 * clean up the /proc/fs/nfsfs/ directory
1516 void nfs_fs_proc_exit(void)
1518 remove_proc_entry("volumes", proc_fs_nfs);
1519 remove_proc_entry("servers", proc_fs_nfs);
1520 remove_proc_entry("nfsfs", proc_root_fs);
1523 #endif /* CONFIG_PROC_FS */