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_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;
153 cred = rpc_lookup_machine_cred();
155 clp->cl_machine_cred = cred;
160 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
168 static void nfs4_shutdown_client(struct nfs_client *clp)
171 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
172 nfs4_kill_renewd(clp);
173 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
174 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
175 nfs_idmap_delete(clp);
177 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
182 * Destroy a shared client record
184 static void nfs_free_client(struct nfs_client *clp)
186 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
188 nfs4_shutdown_client(clp);
190 /* -EIO all pending I/O */
191 if (!IS_ERR(clp->cl_rpcclient))
192 rpc_shutdown_client(clp->cl_rpcclient);
194 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
197 if (clp->cl_machine_cred != NULL)
198 put_rpccred(clp->cl_machine_cred);
200 kfree(clp->cl_hostname);
203 dprintk("<-- nfs_free_client()\n");
207 * Release a reference to a shared client record
209 void nfs_put_client(struct nfs_client *clp)
214 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
216 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
217 list_del(&clp->cl_share_link);
218 spin_unlock(&nfs_client_lock);
220 BUG_ON(!list_empty(&clp->cl_superblocks));
222 nfs_free_client(clp);
226 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
228 * Test if two ip6 socket addresses refer to the same socket by
229 * comparing relevant fields. The padding bytes specifically, are not
230 * compared. sin6_flowinfo is not compared because it only affects QoS
231 * and sin6_scope_id is only compared if the address is "link local"
232 * because "link local" addresses need only be unique to a specific
233 * link. Conversely, ordinary unicast addresses might have different
236 * The caller should ensure both socket addresses are AF_INET6.
238 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
239 const struct sockaddr *sa2)
241 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
242 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
244 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
245 sin1->sin6_scope_id != sin2->sin6_scope_id)
248 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
250 #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
251 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
252 const struct sockaddr *sa2)
259 * Test if two ip4 socket addresses refer to the same socket, by
260 * comparing relevant fields. The padding bytes specifically, are
263 * The caller should ensure both socket addresses are AF_INET.
265 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
266 const struct sockaddr *sa2)
268 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
269 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
271 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
274 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
275 const struct sockaddr *sa2)
277 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
278 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
280 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
281 (sin1->sin6_port == sin2->sin6_port);
284 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
285 const struct sockaddr *sa2)
287 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
288 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
290 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
291 (sin1->sin_port == sin2->sin_port);
295 * Test if two socket addresses represent the same actual socket,
296 * by comparing (only) relevant fields, excluding the port number.
298 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
299 const struct sockaddr *sa2)
301 if (sa1->sa_family != sa2->sa_family)
304 switch (sa1->sa_family) {
306 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
308 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
314 * Test if two socket addresses represent the same actual socket,
315 * by comparing (only) relevant fields, including the port number.
317 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
320 if (sa1->sa_family != sa2->sa_family)
323 switch (sa1->sa_family) {
325 return nfs_sockaddr_cmp_ip4(sa1, sa2);
327 return nfs_sockaddr_cmp_ip6(sa1, sa2);
333 * Find a client by IP address and protocol version
334 * - returns NULL if no such client
336 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
338 struct nfs_client *clp;
340 spin_lock(&nfs_client_lock);
341 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
342 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
344 /* Don't match clients that failed to initialise properly */
345 if (clp->cl_cons_state != NFS_CS_READY)
348 /* Different NFS versions cannot share the same nfs_client */
349 if (clp->rpc_ops->version != nfsversion)
352 /* Match only the IP address, not the port number */
353 if (!nfs_sockaddr_match_ipaddr(addr, clap))
356 atomic_inc(&clp->cl_count);
357 spin_unlock(&nfs_client_lock);
360 spin_unlock(&nfs_client_lock);
365 * Find a client by IP address and protocol version
366 * - returns NULL if no such client
368 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
370 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
371 u32 nfsvers = clp->rpc_ops->version;
373 spin_lock(&nfs_client_lock);
374 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
375 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
377 /* Don't match clients that failed to initialise properly */
378 if (clp->cl_cons_state != NFS_CS_READY)
381 /* Different NFS versions cannot share the same nfs_client */
382 if (clp->rpc_ops->version != nfsvers)
385 /* Match only the IP address, not the port number */
386 if (!nfs_sockaddr_match_ipaddr(sap, clap))
389 atomic_inc(&clp->cl_count);
390 spin_unlock(&nfs_client_lock);
393 spin_unlock(&nfs_client_lock);
398 * Find an nfs_client on the list that matches the initialisation data
401 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
403 struct nfs_client *clp;
404 const struct sockaddr *sap = data->addr;
406 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
407 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
408 /* Don't match clients that failed to initialise properly */
409 if (clp->cl_cons_state < 0)
412 /* Different NFS versions cannot share the same nfs_client */
413 if (clp->rpc_ops != data->rpc_ops)
416 if (clp->cl_proto != data->proto)
419 /* Match the full socket address */
420 if (!nfs_sockaddr_cmp(sap, clap))
423 atomic_inc(&clp->cl_count);
430 * Look up a client by IP address and protocol version
431 * - creates a new record if one doesn't yet exist
433 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
435 struct nfs_client *clp, *new = NULL;
438 dprintk("--> nfs_get_client(%s,v%u)\n",
439 cl_init->hostname ?: "", cl_init->rpc_ops->version);
441 /* see if the client already exists */
443 spin_lock(&nfs_client_lock);
445 clp = nfs_match_client(cl_init);
451 spin_unlock(&nfs_client_lock);
453 new = nfs_alloc_client(cl_init);
456 return ERR_PTR(-ENOMEM);
458 /* install a new client and return with it unready */
461 list_add(&clp->cl_share_link, &nfs_client_list);
462 spin_unlock(&nfs_client_lock);
463 dprintk("--> nfs_get_client() = %p [new]\n", clp);
466 /* found an existing client
467 * - make sure it's ready before returning
470 spin_unlock(&nfs_client_lock);
473 nfs_free_client(new);
475 error = wait_event_killable(nfs_client_active_wq,
476 clp->cl_cons_state != NFS_CS_INITING);
479 return ERR_PTR(-ERESTARTSYS);
482 if (clp->cl_cons_state < NFS_CS_READY) {
483 error = clp->cl_cons_state;
485 return ERR_PTR(error);
488 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
490 dprintk("--> nfs_get_client() = %p [share]\n", clp);
495 * Mark a server as ready or failed
497 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
499 clp->cl_cons_state = state;
500 wake_up_all(&nfs_client_active_wq);
504 * Initialise the timeout values for a connection
506 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
507 unsigned int timeo, unsigned int retrans)
509 to->to_initval = timeo * HZ / 10;
510 to->to_retries = retrans;
513 case XPRT_TRANSPORT_TCP:
514 case XPRT_TRANSPORT_RDMA:
515 if (to->to_retries == 0)
516 to->to_retries = NFS_DEF_TCP_RETRANS;
517 if (to->to_initval == 0)
518 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
519 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
520 to->to_initval = NFS_MAX_TCP_TIMEOUT;
521 to->to_increment = to->to_initval;
522 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
523 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
524 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
525 if (to->to_maxval < to->to_initval)
526 to->to_maxval = to->to_initval;
527 to->to_exponential = 0;
529 case XPRT_TRANSPORT_UDP:
530 if (to->to_retries == 0)
531 to->to_retries = NFS_DEF_UDP_RETRANS;
533 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
534 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
535 to->to_initval = NFS_MAX_UDP_TIMEOUT;
536 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
537 to->to_exponential = 1;
545 * Create an RPC client handle
547 static int nfs_create_rpc_client(struct nfs_client *clp,
548 const struct rpc_timeout *timeparms,
549 rpc_authflavor_t flavor,
550 int discrtry, int noresvport)
552 struct rpc_clnt *clnt = NULL;
553 struct rpc_create_args args = {
554 .protocol = clp->cl_proto,
555 .address = (struct sockaddr *)&clp->cl_addr,
556 .addrsize = clp->cl_addrlen,
557 .timeout = timeparms,
558 .servername = clp->cl_hostname,
559 .program = &nfs_program,
560 .version = clp->rpc_ops->version,
561 .authflavor = flavor,
565 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
567 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
569 if (!IS_ERR(clp->cl_rpcclient))
572 clnt = rpc_create(&args);
574 dprintk("%s: cannot create RPC client. Error = %ld\n",
575 __func__, PTR_ERR(clnt));
576 return PTR_ERR(clnt);
579 clp->cl_rpcclient = clnt;
584 * Version 2 or 3 client destruction
586 static void nfs_destroy_server(struct nfs_server *server)
588 if (!(server->flags & NFS_MOUNT_NONLM))
589 nlmclnt_done(server->nlm_host);
593 * Version 2 or 3 lockd setup
595 static int nfs_start_lockd(struct nfs_server *server)
597 struct nlm_host *host;
598 struct nfs_client *clp = server->nfs_client;
599 struct nlmclnt_initdata nlm_init = {
600 .hostname = clp->cl_hostname,
601 .address = (struct sockaddr *)&clp->cl_addr,
602 .addrlen = clp->cl_addrlen,
603 .protocol = server->flags & NFS_MOUNT_TCP ?
604 IPPROTO_TCP : IPPROTO_UDP,
605 .nfs_version = clp->rpc_ops->version,
606 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
610 if (nlm_init.nfs_version > 3)
612 if (server->flags & NFS_MOUNT_NONLM)
615 host = nlmclnt_init(&nlm_init);
617 return PTR_ERR(host);
619 server->nlm_host = host;
620 server->destroy = nfs_destroy_server;
625 * Initialise an NFSv3 ACL client connection
627 #ifdef CONFIG_NFS_V3_ACL
628 static void nfs_init_server_aclclient(struct nfs_server *server)
630 if (server->nfs_client->rpc_ops->version != 3)
632 if (server->flags & NFS_MOUNT_NOACL)
635 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
636 if (IS_ERR(server->client_acl))
639 /* No errors! Assume that Sun nfsacls are supported */
640 server->caps |= NFS_CAP_ACLS;
644 server->caps &= ~NFS_CAP_ACLS;
647 static inline void nfs_init_server_aclclient(struct nfs_server *server)
649 server->flags &= ~NFS_MOUNT_NOACL;
650 server->caps &= ~NFS_CAP_ACLS;
655 * Create a general RPC client
657 static int nfs_init_server_rpcclient(struct nfs_server *server,
658 const struct rpc_timeout *timeo,
659 rpc_authflavor_t pseudoflavour)
661 struct nfs_client *clp = server->nfs_client;
663 server->client = rpc_clone_client(clp->cl_rpcclient);
664 if (IS_ERR(server->client)) {
665 dprintk("%s: couldn't create rpc_client!\n", __func__);
666 return PTR_ERR(server->client);
669 memcpy(&server->client->cl_timeout_default,
671 sizeof(server->client->cl_timeout_default));
672 server->client->cl_timeout = &server->client->cl_timeout_default;
674 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
675 struct rpc_auth *auth;
677 auth = rpcauth_create(pseudoflavour, server->client);
679 dprintk("%s: couldn't create credcache!\n", __func__);
680 return PTR_ERR(auth);
683 server->client->cl_softrtry = 0;
684 if (server->flags & NFS_MOUNT_SOFT)
685 server->client->cl_softrtry = 1;
691 * Initialise an NFS2 or NFS3 client
693 static int nfs_init_client(struct nfs_client *clp,
694 const struct rpc_timeout *timeparms,
695 const struct nfs_parsed_mount_data *data)
699 if (clp->cl_cons_state == NFS_CS_READY) {
700 /* the client is already initialised */
701 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
706 * Create a client RPC handle for doing FSSTAT with UNIX auth only
707 * - RFC 2623, sec 2.3.2
709 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
710 0, data->flags & NFS_MOUNT_NORESVPORT);
713 nfs_mark_client_ready(clp, NFS_CS_READY);
717 nfs_mark_client_ready(clp, error);
718 dprintk("<-- nfs_init_client() = xerror %d\n", error);
723 * Create a version 2 or 3 client
725 static int nfs_init_server(struct nfs_server *server,
726 const struct nfs_parsed_mount_data *data)
728 struct nfs_client_initdata cl_init = {
729 .hostname = data->nfs_server.hostname,
730 .addr = (const struct sockaddr *)&data->nfs_server.address,
731 .addrlen = data->nfs_server.addrlen,
732 .rpc_ops = &nfs_v2_clientops,
733 .proto = data->nfs_server.protocol,
735 struct rpc_timeout timeparms;
736 struct nfs_client *clp;
739 dprintk("--> nfs_init_server()\n");
742 if (data->flags & NFS_MOUNT_VER3)
743 cl_init.rpc_ops = &nfs_v3_clientops;
746 /* Allocate or find a client reference we can use */
747 clp = nfs_get_client(&cl_init);
749 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
753 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
754 data->timeo, data->retrans);
755 error = nfs_init_client(clp, &timeparms, data);
759 server->nfs_client = clp;
761 /* Initialise the client representation from the mount data */
762 server->flags = data->flags;
765 server->rsize = nfs_block_size(data->rsize, NULL);
767 server->wsize = nfs_block_size(data->wsize, NULL);
769 server->acregmin = data->acregmin * HZ;
770 server->acregmax = data->acregmax * HZ;
771 server->acdirmin = data->acdirmin * HZ;
772 server->acdirmax = data->acdirmax * HZ;
774 /* Start lockd here, before we might error out */
775 error = nfs_start_lockd(server);
779 server->port = data->nfs_server.port;
781 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
785 /* Preserve the values of mount_server-related mount options */
786 if (data->mount_server.addrlen) {
787 memcpy(&server->mountd_address, &data->mount_server.address,
788 data->mount_server.addrlen);
789 server->mountd_addrlen = data->mount_server.addrlen;
791 server->mountd_version = data->mount_server.version;
792 server->mountd_port = data->mount_server.port;
793 server->mountd_protocol = data->mount_server.protocol;
795 server->namelen = data->namlen;
796 /* Create a client RPC handle for the NFSv3 ACL management interface */
797 nfs_init_server_aclclient(server);
798 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
802 server->nfs_client = NULL;
804 dprintk("<-- nfs_init_server() = xerror %d\n", error);
809 * Load up the server record from information gained in an fsinfo record
811 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
813 unsigned long max_rpc_payload;
815 /* Work out a lot of parameters */
816 if (server->rsize == 0)
817 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
818 if (server->wsize == 0)
819 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
821 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
822 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
823 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
824 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
826 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
827 if (server->rsize > max_rpc_payload)
828 server->rsize = max_rpc_payload;
829 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
830 server->rsize = NFS_MAX_FILE_IO_SIZE;
831 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
833 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
835 if (server->wsize > max_rpc_payload)
836 server->wsize = max_rpc_payload;
837 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
838 server->wsize = NFS_MAX_FILE_IO_SIZE;
839 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
840 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
842 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
843 if (server->dtsize > PAGE_CACHE_SIZE)
844 server->dtsize = PAGE_CACHE_SIZE;
845 if (server->dtsize > server->rsize)
846 server->dtsize = server->rsize;
848 if (server->flags & NFS_MOUNT_NOAC) {
849 server->acregmin = server->acregmax = 0;
850 server->acdirmin = server->acdirmax = 0;
853 server->maxfilesize = fsinfo->maxfilesize;
855 /* We're airborne Set socket buffersize */
856 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
860 * Probe filesystem information, including the FSID on v2/v3
862 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
864 struct nfs_fsinfo fsinfo;
865 struct nfs_client *clp = server->nfs_client;
868 dprintk("--> nfs_probe_fsinfo()\n");
870 if (clp->rpc_ops->set_capabilities != NULL) {
871 error = clp->rpc_ops->set_capabilities(server, mntfh);
876 fsinfo.fattr = fattr;
877 nfs_fattr_init(fattr);
878 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
882 nfs_server_set_fsinfo(server, &fsinfo);
883 error = bdi_init(&server->backing_dev_info);
888 /* Get some general file system info */
889 if (server->namelen == 0) {
890 struct nfs_pathconf pathinfo;
892 pathinfo.fattr = fattr;
893 nfs_fattr_init(fattr);
895 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
896 server->namelen = pathinfo.max_namelen;
899 dprintk("<-- nfs_probe_fsinfo() = 0\n");
903 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
908 * Copy useful information when duplicating a server record
910 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
912 target->flags = source->flags;
913 target->acregmin = source->acregmin;
914 target->acregmax = source->acregmax;
915 target->acdirmin = source->acdirmin;
916 target->acdirmax = source->acdirmax;
917 target->caps = source->caps;
921 * Allocate and initialise a server record
923 static struct nfs_server *nfs_alloc_server(void)
925 struct nfs_server *server;
927 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
931 server->client = server->client_acl = ERR_PTR(-EINVAL);
933 /* Zero out the NFS state stuff */
934 INIT_LIST_HEAD(&server->client_link);
935 INIT_LIST_HEAD(&server->master_link);
937 atomic_set(&server->active, 0);
939 server->io_stats = nfs_alloc_iostats();
940 if (!server->io_stats) {
949 * Free up a server record
951 void nfs_free_server(struct nfs_server *server)
953 dprintk("--> nfs_free_server()\n");
955 spin_lock(&nfs_client_lock);
956 list_del(&server->client_link);
957 list_del(&server->master_link);
958 spin_unlock(&nfs_client_lock);
960 if (server->destroy != NULL)
961 server->destroy(server);
963 if (!IS_ERR(server->client_acl))
964 rpc_shutdown_client(server->client_acl);
965 if (!IS_ERR(server->client))
966 rpc_shutdown_client(server->client);
968 nfs_put_client(server->nfs_client);
970 nfs_free_iostats(server->io_stats);
971 bdi_destroy(&server->backing_dev_info);
973 nfs_release_automount_timer();
974 dprintk("<-- nfs_free_server()\n");
978 * Create a version 2 or 3 volume record
979 * - keyed on server and FSID
981 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
982 struct nfs_fh *mntfh)
984 struct nfs_server *server;
985 struct nfs_fattr fattr;
988 server = nfs_alloc_server();
990 return ERR_PTR(-ENOMEM);
992 /* Get a client representation */
993 error = nfs_init_server(server, data);
997 BUG_ON(!server->nfs_client);
998 BUG_ON(!server->nfs_client->rpc_ops);
999 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1001 /* Probe the root fh to retrieve its FSID */
1002 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1005 if (server->nfs_client->rpc_ops->version == 3) {
1006 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1007 server->namelen = NFS3_MAXNAMLEN;
1008 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1009 server->caps |= NFS_CAP_READDIRPLUS;
1011 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1012 server->namelen = NFS2_MAXNAMLEN;
1015 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1016 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1018 dprintk("nfs_create_server: getattr error = %d\n", -error);
1022 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1024 dprintk("Server FSID: %llx:%llx\n",
1025 (unsigned long long) server->fsid.major,
1026 (unsigned long long) server->fsid.minor);
1028 BUG_ON(!server->nfs_client);
1029 BUG_ON(!server->nfs_client->rpc_ops);
1030 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1032 spin_lock(&nfs_client_lock);
1033 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1034 list_add_tail(&server->master_link, &nfs_volume_list);
1035 spin_unlock(&nfs_client_lock);
1037 server->mount_time = jiffies;
1041 nfs_free_server(server);
1042 return ERR_PTR(error);
1045 #ifdef CONFIG_NFS_V4
1047 * Initialise an NFS4 client record
1049 static int nfs4_init_client(struct nfs_client *clp,
1050 const struct rpc_timeout *timeparms,
1051 const char *ip_addr,
1052 rpc_authflavor_t authflavour,
1057 if (clp->cl_cons_state == NFS_CS_READY) {
1058 /* the client is initialised already */
1059 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1063 /* Check NFS protocol revision and initialize RPC op vector */
1064 clp->rpc_ops = &nfs_v4_clientops;
1066 error = nfs_create_rpc_client(clp, timeparms, authflavour,
1067 1, flags & NFS_MOUNT_NORESVPORT);
1070 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1072 error = nfs_idmap_new(clp);
1074 dprintk("%s: failed to create idmapper. Error = %d\n",
1078 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1080 nfs_mark_client_ready(clp, NFS_CS_READY);
1084 nfs_mark_client_ready(clp, error);
1085 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1090 * Set up an NFS4 client
1092 static int nfs4_set_client(struct nfs_server *server,
1093 const char *hostname,
1094 const struct sockaddr *addr,
1095 const size_t addrlen,
1096 const char *ip_addr,
1097 rpc_authflavor_t authflavour,
1098 int proto, const struct rpc_timeout *timeparms)
1100 struct nfs_client_initdata cl_init = {
1101 .hostname = hostname,
1104 .rpc_ops = &nfs_v4_clientops,
1107 struct nfs_client *clp;
1110 dprintk("--> nfs4_set_client()\n");
1112 /* Allocate or find a client reference we can use */
1113 clp = nfs_get_client(&cl_init);
1115 error = PTR_ERR(clp);
1118 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1123 server->nfs_client = clp;
1124 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1128 nfs_put_client(clp);
1130 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1135 * Create a version 4 volume record
1137 static int nfs4_init_server(struct nfs_server *server,
1138 const struct nfs_parsed_mount_data *data)
1140 struct rpc_timeout timeparms;
1143 dprintk("--> nfs4_init_server()\n");
1145 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1146 data->timeo, data->retrans);
1148 /* Initialise the client representation from the mount data */
1149 server->flags = data->flags;
1150 server->caps |= NFS_CAP_ATOMIC_OPEN;
1152 /* Get a client record */
1153 error = nfs4_set_client(server,
1154 data->nfs_server.hostname,
1155 (const struct sockaddr *)&data->nfs_server.address,
1156 data->nfs_server.addrlen,
1157 data->client_address,
1158 data->auth_flavors[0],
1159 data->nfs_server.protocol,
1165 server->rsize = nfs_block_size(data->rsize, NULL);
1167 server->wsize = nfs_block_size(data->wsize, NULL);
1169 server->acregmin = data->acregmin * HZ;
1170 server->acregmax = data->acregmax * HZ;
1171 server->acdirmin = data->acdirmin * HZ;
1172 server->acdirmax = data->acdirmax * HZ;
1174 server->port = data->nfs_server.port;
1176 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1180 dprintk("<-- nfs4_init_server() = %d\n", error);
1185 * Create a version 4 volume record
1186 * - keyed on server and FSID
1188 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1189 struct nfs_fh *mntfh)
1191 struct nfs_fattr fattr;
1192 struct nfs_server *server;
1195 dprintk("--> nfs4_create_server()\n");
1197 server = nfs_alloc_server();
1199 return ERR_PTR(-ENOMEM);
1201 /* set up the general RPC client */
1202 error = nfs4_init_server(server, data);
1206 BUG_ON(!server->nfs_client);
1207 BUG_ON(!server->nfs_client->rpc_ops);
1208 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1210 /* Probe the root fh to retrieve its FSID */
1211 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1215 dprintk("Server FSID: %llx:%llx\n",
1216 (unsigned long long) server->fsid.major,
1217 (unsigned long long) server->fsid.minor);
1218 dprintk("Mount FH: %d\n", mntfh->size);
1220 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1224 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1225 server->namelen = NFS4_MAXNAMLEN;
1227 BUG_ON(!server->nfs_client);
1228 BUG_ON(!server->nfs_client->rpc_ops);
1229 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1231 spin_lock(&nfs_client_lock);
1232 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1233 list_add_tail(&server->master_link, &nfs_volume_list);
1234 spin_unlock(&nfs_client_lock);
1236 server->mount_time = jiffies;
1237 dprintk("<-- nfs4_create_server() = %p\n", server);
1241 nfs_free_server(server);
1242 dprintk("<-- nfs4_create_server() = error %d\n", error);
1243 return ERR_PTR(error);
1247 * Create an NFS4 referral server record
1249 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1250 struct nfs_fh *mntfh)
1252 struct nfs_client *parent_client;
1253 struct nfs_server *server, *parent_server;
1254 struct nfs_fattr fattr;
1257 dprintk("--> nfs4_create_referral_server()\n");
1259 server = nfs_alloc_server();
1261 return ERR_PTR(-ENOMEM);
1263 parent_server = NFS_SB(data->sb);
1264 parent_client = parent_server->nfs_client;
1266 /* Initialise the client representation from the parent server */
1267 nfs_server_copy_userdata(server, parent_server);
1268 server->caps |= NFS_CAP_ATOMIC_OPEN;
1270 /* Get a client representation.
1271 * Note: NFSv4 always uses TCP, */
1272 error = nfs4_set_client(server, data->hostname,
1275 parent_client->cl_ipaddr,
1277 parent_server->client->cl_xprt->prot,
1278 parent_server->client->cl_timeout);
1282 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1286 BUG_ON(!server->nfs_client);
1287 BUG_ON(!server->nfs_client->rpc_ops);
1288 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1290 /* Probe the root fh to retrieve its FSID and filehandle */
1291 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1295 /* probe the filesystem info for this server filesystem */
1296 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1300 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1301 server->namelen = NFS4_MAXNAMLEN;
1303 dprintk("Referral FSID: %llx:%llx\n",
1304 (unsigned long long) server->fsid.major,
1305 (unsigned long long) server->fsid.minor);
1307 spin_lock(&nfs_client_lock);
1308 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1309 list_add_tail(&server->master_link, &nfs_volume_list);
1310 spin_unlock(&nfs_client_lock);
1312 server->mount_time = jiffies;
1314 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1318 nfs_free_server(server);
1319 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1320 return ERR_PTR(error);
1323 #endif /* CONFIG_NFS_V4 */
1326 * Clone an NFS2, NFS3 or NFS4 server record
1328 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1330 struct nfs_fattr *fattr)
1332 struct nfs_server *server;
1333 struct nfs_fattr fattr_fsinfo;
1336 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1337 (unsigned long long) fattr->fsid.major,
1338 (unsigned long long) fattr->fsid.minor);
1340 server = nfs_alloc_server();
1342 return ERR_PTR(-ENOMEM);
1344 /* Copy data from the source */
1345 server->nfs_client = source->nfs_client;
1346 atomic_inc(&server->nfs_client->cl_count);
1347 nfs_server_copy_userdata(server, source);
1349 server->fsid = fattr->fsid;
1351 error = nfs_init_server_rpcclient(server,
1352 source->client->cl_timeout,
1353 source->client->cl_auth->au_flavor);
1355 goto out_free_server;
1356 if (!IS_ERR(source->client_acl))
1357 nfs_init_server_aclclient(server);
1359 /* probe the filesystem info for this server filesystem */
1360 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1362 goto out_free_server;
1364 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1365 server->namelen = NFS4_MAXNAMLEN;
1367 dprintk("Cloned FSID: %llx:%llx\n",
1368 (unsigned long long) server->fsid.major,
1369 (unsigned long long) server->fsid.minor);
1371 error = nfs_start_lockd(server);
1373 goto out_free_server;
1375 spin_lock(&nfs_client_lock);
1376 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1377 list_add_tail(&server->master_link, &nfs_volume_list);
1378 spin_unlock(&nfs_client_lock);
1380 server->mount_time = jiffies;
1382 dprintk("<-- nfs_clone_server() = %p\n", server);
1386 nfs_free_server(server);
1387 dprintk("<-- nfs_clone_server() = error %d\n", error);
1388 return ERR_PTR(error);
1391 #ifdef CONFIG_PROC_FS
1392 static struct proc_dir_entry *proc_fs_nfs;
1394 static int nfs_server_list_open(struct inode *inode, struct file *file);
1395 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1396 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1397 static void nfs_server_list_stop(struct seq_file *p, void *v);
1398 static int nfs_server_list_show(struct seq_file *m, void *v);
1400 static struct seq_operations nfs_server_list_ops = {
1401 .start = nfs_server_list_start,
1402 .next = nfs_server_list_next,
1403 .stop = nfs_server_list_stop,
1404 .show = nfs_server_list_show,
1407 static const struct file_operations nfs_server_list_fops = {
1408 .open = nfs_server_list_open,
1410 .llseek = seq_lseek,
1411 .release = seq_release,
1412 .owner = THIS_MODULE,
1415 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1416 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1417 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1418 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1419 static int nfs_volume_list_show(struct seq_file *m, void *v);
1421 static struct seq_operations nfs_volume_list_ops = {
1422 .start = nfs_volume_list_start,
1423 .next = nfs_volume_list_next,
1424 .stop = nfs_volume_list_stop,
1425 .show = nfs_volume_list_show,
1428 static const struct file_operations nfs_volume_list_fops = {
1429 .open = nfs_volume_list_open,
1431 .llseek = seq_lseek,
1432 .release = seq_release,
1433 .owner = THIS_MODULE,
1437 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1440 static int nfs_server_list_open(struct inode *inode, struct file *file)
1445 ret = seq_open(file, &nfs_server_list_ops);
1449 m = file->private_data;
1450 m->private = PDE(inode)->data;
1456 * set up the iterator to start reading from the server list and return the first item
1458 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1460 /* lock the list against modification */
1461 spin_lock(&nfs_client_lock);
1462 return seq_list_start_head(&nfs_client_list, *_pos);
1466 * move to next server
1468 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1470 return seq_list_next(v, &nfs_client_list, pos);
1474 * clean up after reading from the transports list
1476 static void nfs_server_list_stop(struct seq_file *p, void *v)
1478 spin_unlock(&nfs_client_lock);
1482 * display a header line followed by a load of call lines
1484 static int nfs_server_list_show(struct seq_file *m, void *v)
1486 struct nfs_client *clp;
1488 /* display header on line 1 */
1489 if (v == &nfs_client_list) {
1490 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1494 /* display one transport per line on subsequent lines */
1495 clp = list_entry(v, struct nfs_client, cl_share_link);
1497 seq_printf(m, "v%u %s %s %3d %s\n",
1498 clp->rpc_ops->version,
1499 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1500 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1501 atomic_read(&clp->cl_count),
1508 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1510 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1515 ret = seq_open(file, &nfs_volume_list_ops);
1519 m = file->private_data;
1520 m->private = PDE(inode)->data;
1526 * set up the iterator to start reading from the volume list and return the first item
1528 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1530 /* lock the list against modification */
1531 spin_lock(&nfs_client_lock);
1532 return seq_list_start_head(&nfs_volume_list, *_pos);
1536 * move to next volume
1538 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1540 return seq_list_next(v, &nfs_volume_list, pos);
1544 * clean up after reading from the transports list
1546 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1548 spin_unlock(&nfs_client_lock);
1552 * display a header line followed by a load of call lines
1554 static int nfs_volume_list_show(struct seq_file *m, void *v)
1556 struct nfs_server *server;
1557 struct nfs_client *clp;
1558 char dev[8], fsid[17];
1560 /* display header on line 1 */
1561 if (v == &nfs_volume_list) {
1562 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1565 /* display one transport per line on subsequent lines */
1566 server = list_entry(v, struct nfs_server, master_link);
1567 clp = server->nfs_client;
1569 snprintf(dev, 8, "%u:%u",
1570 MAJOR(server->s_dev), MINOR(server->s_dev));
1572 snprintf(fsid, 17, "%llx:%llx",
1573 (unsigned long long) server->fsid.major,
1574 (unsigned long long) server->fsid.minor);
1576 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1577 clp->rpc_ops->version,
1578 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1579 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1587 * initialise the /proc/fs/nfsfs/ directory
1589 int __init nfs_fs_proc_init(void)
1591 struct proc_dir_entry *p;
1593 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1597 /* a file of servers with which we're dealing */
1598 p = proc_create("servers", S_IFREG|S_IRUGO,
1599 proc_fs_nfs, &nfs_server_list_fops);
1603 /* a file of volumes that we have mounted */
1604 p = proc_create("volumes", S_IFREG|S_IRUGO,
1605 proc_fs_nfs, &nfs_volume_list_fops);
1611 remove_proc_entry("servers", proc_fs_nfs);
1613 remove_proc_entry("fs/nfsfs", NULL);
1619 * clean up the /proc/fs/nfsfs/ directory
1621 void nfs_fs_proc_exit(void)
1623 remove_proc_entry("volumes", proc_fs_nfs);
1624 remove_proc_entry("servers", proc_fs_nfs);
1625 remove_proc_entry("fs/nfsfs", NULL);
1628 #endif /* CONFIG_PROC_FS */