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)
227 static const struct in6_addr *nfs_map_ipv4_addr(const struct sockaddr *sa, struct in6_addr *addr_mapped)
229 switch (sa->sa_family) {
233 return &((const struct sockaddr_in6 *)sa)->sin6_addr;
236 ipv6_addr_set_v4mapped(((const struct sockaddr_in *)sa)->sin_addr.s_addr,
242 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
243 const struct sockaddr *sa2)
245 const struct in6_addr *addr1;
246 const struct in6_addr *addr2;
247 struct in6_addr addr1_mapped;
248 struct in6_addr addr2_mapped;
250 addr1 = nfs_map_ipv4_addr(sa1, &addr1_mapped);
251 if (likely(addr1 != NULL)) {
252 addr2 = nfs_map_ipv4_addr(sa2, &addr2_mapped);
253 if (likely(addr2 != NULL))
254 return ipv6_addr_equal(addr1, addr2);
260 * Test if two ip6 socket addresses refer to the same socket by
261 * comparing relevant fields. The padding bytes specifically, are not
262 * compared. sin6_flowinfo is not compared because it only affects QoS
263 * and sin6_scope_id is only compared if the address is "link local"
264 * because "link local" addresses need only be unique to a specific
265 * link. Conversely, ordinary unicast addresses might have different
268 * The caller should ensure both socket addresses are AF_INET6.
270 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
271 const struct sockaddr *sa2)
273 const struct sockaddr_in6 *saddr1 = (const struct sockaddr_in6 *)sa1;
274 const struct sockaddr_in6 *saddr2 = (const struct sockaddr_in6 *)sa2;
276 if (!ipv6_addr_equal(&saddr1->sin6_addr,
279 if (ipv6_addr_scope(&saddr1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
280 saddr1->sin6_scope_id != saddr2->sin6_scope_id)
282 return saddr1->sin6_port == saddr2->sin6_port;
285 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
286 const struct sockaddr_in *sa2)
288 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
291 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
292 const struct sockaddr *sa2)
294 if (unlikely(sa1->sa_family != AF_INET || sa2->sa_family != AF_INET))
296 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
297 (const struct sockaddr_in *)sa2);
300 static int nfs_sockaddr_cmp_ip6(const struct sockaddr * sa1,
301 const struct sockaddr * sa2)
308 * Test if two ip4 socket addresses refer to the same socket, by
309 * comparing relevant fields. The padding bytes specifically, are
312 * The caller should ensure both socket addresses are AF_INET.
314 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
315 const struct sockaddr *sa2)
317 const struct sockaddr_in *saddr1 = (const struct sockaddr_in *)sa1;
318 const struct sockaddr_in *saddr2 = (const struct sockaddr_in *)sa2;
320 if (saddr1->sin_addr.s_addr != saddr2->sin_addr.s_addr)
322 return saddr1->sin_port == saddr2->sin_port;
326 * Test if two socket addresses represent the same actual socket,
327 * by comparing (only) relevant fields.
329 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
330 const struct sockaddr *sa2)
332 if (sa1->sa_family != sa2->sa_family)
335 switch (sa1->sa_family) {
337 return nfs_sockaddr_cmp_ip4(sa1, sa2);
339 return nfs_sockaddr_cmp_ip6(sa1, sa2);
345 * Find a client by IP address and protocol version
346 * - returns NULL if no such client
348 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
350 struct nfs_client *clp;
352 spin_lock(&nfs_client_lock);
353 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
354 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
356 /* Don't match clients that failed to initialise properly */
357 if (clp->cl_cons_state != NFS_CS_READY)
360 /* Different NFS versions cannot share the same nfs_client */
361 if (clp->rpc_ops->version != nfsversion)
364 /* Match only the IP address, not the port number */
365 if (!nfs_sockaddr_match_ipaddr(addr, clap))
368 atomic_inc(&clp->cl_count);
369 spin_unlock(&nfs_client_lock);
372 spin_unlock(&nfs_client_lock);
377 * Find a client by IP address and protocol version
378 * - returns NULL if no such client
380 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
382 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
383 u32 nfsvers = clp->rpc_ops->version;
385 spin_lock(&nfs_client_lock);
386 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
387 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
389 /* Don't match clients that failed to initialise properly */
390 if (clp->cl_cons_state != NFS_CS_READY)
393 /* Different NFS versions cannot share the same nfs_client */
394 if (clp->rpc_ops->version != nfsvers)
397 /* Match only the IP address, not the port number */
398 if (!nfs_sockaddr_match_ipaddr(sap, clap))
401 atomic_inc(&clp->cl_count);
402 spin_unlock(&nfs_client_lock);
405 spin_unlock(&nfs_client_lock);
410 * Find an nfs_client on the list that matches the initialisation data
413 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
415 struct nfs_client *clp;
416 const struct sockaddr *sap = data->addr;
418 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
419 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
420 /* Don't match clients that failed to initialise properly */
421 if (clp->cl_cons_state < 0)
424 /* Different NFS versions cannot share the same nfs_client */
425 if (clp->rpc_ops != data->rpc_ops)
428 if (clp->cl_proto != data->proto)
431 /* Match the full socket address */
432 if (!nfs_sockaddr_cmp(sap, clap))
435 atomic_inc(&clp->cl_count);
442 * Look up a client by IP address and protocol version
443 * - creates a new record if one doesn't yet exist
445 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
447 struct nfs_client *clp, *new = NULL;
450 dprintk("--> nfs_get_client(%s,v%u)\n",
451 cl_init->hostname ?: "", cl_init->rpc_ops->version);
453 /* see if the client already exists */
455 spin_lock(&nfs_client_lock);
457 clp = nfs_match_client(cl_init);
463 spin_unlock(&nfs_client_lock);
465 new = nfs_alloc_client(cl_init);
468 return ERR_PTR(-ENOMEM);
470 /* install a new client and return with it unready */
473 list_add(&clp->cl_share_link, &nfs_client_list);
474 spin_unlock(&nfs_client_lock);
475 dprintk("--> nfs_get_client() = %p [new]\n", clp);
478 /* found an existing client
479 * - make sure it's ready before returning
482 spin_unlock(&nfs_client_lock);
485 nfs_free_client(new);
487 error = wait_event_killable(nfs_client_active_wq,
488 clp->cl_cons_state != NFS_CS_INITING);
491 return ERR_PTR(-ERESTARTSYS);
494 if (clp->cl_cons_state < NFS_CS_READY) {
495 error = clp->cl_cons_state;
497 return ERR_PTR(error);
500 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
502 dprintk("--> nfs_get_client() = %p [share]\n", clp);
507 * Mark a server as ready or failed
509 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
511 clp->cl_cons_state = state;
512 wake_up_all(&nfs_client_active_wq);
516 * Initialise the timeout values for a connection
518 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
519 unsigned int timeo, unsigned int retrans)
521 to->to_initval = timeo * HZ / 10;
522 to->to_retries = retrans;
525 case XPRT_TRANSPORT_TCP:
526 case XPRT_TRANSPORT_RDMA:
527 if (to->to_retries == 0)
528 to->to_retries = NFS_DEF_TCP_RETRANS;
529 if (to->to_initval == 0)
530 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
531 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
532 to->to_initval = NFS_MAX_TCP_TIMEOUT;
533 to->to_increment = to->to_initval;
534 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
535 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
536 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
537 if (to->to_maxval < to->to_initval)
538 to->to_maxval = to->to_initval;
539 to->to_exponential = 0;
541 case XPRT_TRANSPORT_UDP:
542 if (to->to_retries == 0)
543 to->to_retries = NFS_DEF_UDP_RETRANS;
545 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
546 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
547 to->to_initval = NFS_MAX_UDP_TIMEOUT;
548 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
549 to->to_exponential = 1;
557 * Create an RPC client handle
559 static int nfs_create_rpc_client(struct nfs_client *clp,
560 const struct rpc_timeout *timeparms,
561 rpc_authflavor_t flavor,
562 int discrtry, int noresvport)
564 struct rpc_clnt *clnt = NULL;
565 struct rpc_create_args args = {
566 .protocol = clp->cl_proto,
567 .address = (struct sockaddr *)&clp->cl_addr,
568 .addrsize = clp->cl_addrlen,
569 .timeout = timeparms,
570 .servername = clp->cl_hostname,
571 .program = &nfs_program,
572 .version = clp->rpc_ops->version,
573 .authflavor = flavor,
577 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
579 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
581 if (!IS_ERR(clp->cl_rpcclient))
584 clnt = rpc_create(&args);
586 dprintk("%s: cannot create RPC client. Error = %ld\n",
587 __func__, PTR_ERR(clnt));
588 return PTR_ERR(clnt);
591 clp->cl_rpcclient = clnt;
596 * Version 2 or 3 client destruction
598 static void nfs_destroy_server(struct nfs_server *server)
600 if (!(server->flags & NFS_MOUNT_NONLM))
601 nlmclnt_done(server->nlm_host);
605 * Version 2 or 3 lockd setup
607 static int nfs_start_lockd(struct nfs_server *server)
609 struct nlm_host *host;
610 struct nfs_client *clp = server->nfs_client;
611 struct nlmclnt_initdata nlm_init = {
612 .hostname = clp->cl_hostname,
613 .address = (struct sockaddr *)&clp->cl_addr,
614 .addrlen = clp->cl_addrlen,
615 .protocol = server->flags & NFS_MOUNT_TCP ?
616 IPPROTO_TCP : IPPROTO_UDP,
617 .nfs_version = clp->rpc_ops->version,
618 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
622 if (nlm_init.nfs_version > 3)
624 if (server->flags & NFS_MOUNT_NONLM)
627 host = nlmclnt_init(&nlm_init);
629 return PTR_ERR(host);
631 server->nlm_host = host;
632 server->destroy = nfs_destroy_server;
637 * Initialise an NFSv3 ACL client connection
639 #ifdef CONFIG_NFS_V3_ACL
640 static void nfs_init_server_aclclient(struct nfs_server *server)
642 if (server->nfs_client->rpc_ops->version != 3)
644 if (server->flags & NFS_MOUNT_NOACL)
647 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
648 if (IS_ERR(server->client_acl))
651 /* No errors! Assume that Sun nfsacls are supported */
652 server->caps |= NFS_CAP_ACLS;
656 server->caps &= ~NFS_CAP_ACLS;
659 static inline void nfs_init_server_aclclient(struct nfs_server *server)
661 server->flags &= ~NFS_MOUNT_NOACL;
662 server->caps &= ~NFS_CAP_ACLS;
667 * Create a general RPC client
669 static int nfs_init_server_rpcclient(struct nfs_server *server,
670 const struct rpc_timeout *timeo,
671 rpc_authflavor_t pseudoflavour)
673 struct nfs_client *clp = server->nfs_client;
675 server->client = rpc_clone_client(clp->cl_rpcclient);
676 if (IS_ERR(server->client)) {
677 dprintk("%s: couldn't create rpc_client!\n", __func__);
678 return PTR_ERR(server->client);
681 memcpy(&server->client->cl_timeout_default,
683 sizeof(server->client->cl_timeout_default));
684 server->client->cl_timeout = &server->client->cl_timeout_default;
686 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
687 struct rpc_auth *auth;
689 auth = rpcauth_create(pseudoflavour, server->client);
691 dprintk("%s: couldn't create credcache!\n", __func__);
692 return PTR_ERR(auth);
695 server->client->cl_softrtry = 0;
696 if (server->flags & NFS_MOUNT_SOFT)
697 server->client->cl_softrtry = 1;
703 * Initialise an NFS2 or NFS3 client
705 static int nfs_init_client(struct nfs_client *clp,
706 const struct rpc_timeout *timeparms,
707 const struct nfs_parsed_mount_data *data)
711 if (clp->cl_cons_state == NFS_CS_READY) {
712 /* the client is already initialised */
713 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
718 * Create a client RPC handle for doing FSSTAT with UNIX auth only
719 * - RFC 2623, sec 2.3.2
721 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
722 0, data->flags & NFS_MOUNT_NORESVPORT);
725 nfs_mark_client_ready(clp, NFS_CS_READY);
729 nfs_mark_client_ready(clp, error);
730 dprintk("<-- nfs_init_client() = xerror %d\n", error);
735 * Create a version 2 or 3 client
737 static int nfs_init_server(struct nfs_server *server,
738 const struct nfs_parsed_mount_data *data)
740 struct nfs_client_initdata cl_init = {
741 .hostname = data->nfs_server.hostname,
742 .addr = (const struct sockaddr *)&data->nfs_server.address,
743 .addrlen = data->nfs_server.addrlen,
744 .rpc_ops = &nfs_v2_clientops,
745 .proto = data->nfs_server.protocol,
747 struct rpc_timeout timeparms;
748 struct nfs_client *clp;
751 dprintk("--> nfs_init_server()\n");
754 if (data->flags & NFS_MOUNT_VER3)
755 cl_init.rpc_ops = &nfs_v3_clientops;
758 /* Allocate or find a client reference we can use */
759 clp = nfs_get_client(&cl_init);
761 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
765 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
766 data->timeo, data->retrans);
767 error = nfs_init_client(clp, &timeparms, data);
771 server->nfs_client = clp;
773 /* Initialise the client representation from the mount data */
774 server->flags = data->flags;
777 server->rsize = nfs_block_size(data->rsize, NULL);
779 server->wsize = nfs_block_size(data->wsize, NULL);
781 server->acregmin = data->acregmin * HZ;
782 server->acregmax = data->acregmax * HZ;
783 server->acdirmin = data->acdirmin * HZ;
784 server->acdirmax = data->acdirmax * HZ;
786 /* Start lockd here, before we might error out */
787 error = nfs_start_lockd(server);
791 server->port = data->nfs_server.port;
793 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
797 /* Preserve the values of mount_server-related mount options */
798 if (data->mount_server.addrlen) {
799 memcpy(&server->mountd_address, &data->mount_server.address,
800 data->mount_server.addrlen);
801 server->mountd_addrlen = data->mount_server.addrlen;
803 server->mountd_version = data->mount_server.version;
804 server->mountd_port = data->mount_server.port;
805 server->mountd_protocol = data->mount_server.protocol;
807 server->namelen = data->namlen;
808 /* Create a client RPC handle for the NFSv3 ACL management interface */
809 nfs_init_server_aclclient(server);
810 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
814 server->nfs_client = NULL;
816 dprintk("<-- nfs_init_server() = xerror %d\n", error);
821 * Load up the server record from information gained in an fsinfo record
823 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
825 unsigned long max_rpc_payload;
827 /* Work out a lot of parameters */
828 if (server->rsize == 0)
829 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
830 if (server->wsize == 0)
831 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
833 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
834 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
835 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
836 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
838 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
839 if (server->rsize > max_rpc_payload)
840 server->rsize = max_rpc_payload;
841 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
842 server->rsize = NFS_MAX_FILE_IO_SIZE;
843 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
845 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
847 if (server->wsize > max_rpc_payload)
848 server->wsize = max_rpc_payload;
849 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
850 server->wsize = NFS_MAX_FILE_IO_SIZE;
851 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
852 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
854 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
855 if (server->dtsize > PAGE_CACHE_SIZE)
856 server->dtsize = PAGE_CACHE_SIZE;
857 if (server->dtsize > server->rsize)
858 server->dtsize = server->rsize;
860 if (server->flags & NFS_MOUNT_NOAC) {
861 server->acregmin = server->acregmax = 0;
862 server->acdirmin = server->acdirmax = 0;
865 server->maxfilesize = fsinfo->maxfilesize;
867 /* We're airborne Set socket buffersize */
868 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
872 * Probe filesystem information, including the FSID on v2/v3
874 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
876 struct nfs_fsinfo fsinfo;
877 struct nfs_client *clp = server->nfs_client;
880 dprintk("--> nfs_probe_fsinfo()\n");
882 if (clp->rpc_ops->set_capabilities != NULL) {
883 error = clp->rpc_ops->set_capabilities(server, mntfh);
888 fsinfo.fattr = fattr;
889 nfs_fattr_init(fattr);
890 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
894 nfs_server_set_fsinfo(server, &fsinfo);
895 error = bdi_init(&server->backing_dev_info);
900 /* Get some general file system info */
901 if (server->namelen == 0) {
902 struct nfs_pathconf pathinfo;
904 pathinfo.fattr = fattr;
905 nfs_fattr_init(fattr);
907 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
908 server->namelen = pathinfo.max_namelen;
911 dprintk("<-- nfs_probe_fsinfo() = 0\n");
915 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
920 * Copy useful information when duplicating a server record
922 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
924 target->flags = source->flags;
925 target->acregmin = source->acregmin;
926 target->acregmax = source->acregmax;
927 target->acdirmin = source->acdirmin;
928 target->acdirmax = source->acdirmax;
929 target->caps = source->caps;
933 * Allocate and initialise a server record
935 static struct nfs_server *nfs_alloc_server(void)
937 struct nfs_server *server;
939 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
943 server->client = server->client_acl = ERR_PTR(-EINVAL);
945 /* Zero out the NFS state stuff */
946 INIT_LIST_HEAD(&server->client_link);
947 INIT_LIST_HEAD(&server->master_link);
949 atomic_set(&server->active, 0);
951 server->io_stats = nfs_alloc_iostats();
952 if (!server->io_stats) {
961 * Free up a server record
963 void nfs_free_server(struct nfs_server *server)
965 dprintk("--> nfs_free_server()\n");
967 spin_lock(&nfs_client_lock);
968 list_del(&server->client_link);
969 list_del(&server->master_link);
970 spin_unlock(&nfs_client_lock);
972 if (server->destroy != NULL)
973 server->destroy(server);
975 if (!IS_ERR(server->client_acl))
976 rpc_shutdown_client(server->client_acl);
977 if (!IS_ERR(server->client))
978 rpc_shutdown_client(server->client);
980 nfs_put_client(server->nfs_client);
982 nfs_free_iostats(server->io_stats);
983 bdi_destroy(&server->backing_dev_info);
985 nfs_release_automount_timer();
986 dprintk("<-- nfs_free_server()\n");
990 * Create a version 2 or 3 volume record
991 * - keyed on server and FSID
993 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
994 struct nfs_fh *mntfh)
996 struct nfs_server *server;
997 struct nfs_fattr fattr;
1000 server = nfs_alloc_server();
1002 return ERR_PTR(-ENOMEM);
1004 /* Get a client representation */
1005 error = nfs_init_server(server, data);
1009 BUG_ON(!server->nfs_client);
1010 BUG_ON(!server->nfs_client->rpc_ops);
1011 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1013 /* Probe the root fh to retrieve its FSID */
1014 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1017 if (server->nfs_client->rpc_ops->version == 3) {
1018 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1019 server->namelen = NFS3_MAXNAMLEN;
1020 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1021 server->caps |= NFS_CAP_READDIRPLUS;
1023 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1024 server->namelen = NFS2_MAXNAMLEN;
1027 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1028 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1030 dprintk("nfs_create_server: getattr error = %d\n", -error);
1034 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1036 dprintk("Server FSID: %llx:%llx\n",
1037 (unsigned long long) server->fsid.major,
1038 (unsigned long long) server->fsid.minor);
1040 BUG_ON(!server->nfs_client);
1041 BUG_ON(!server->nfs_client->rpc_ops);
1042 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1044 spin_lock(&nfs_client_lock);
1045 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1046 list_add_tail(&server->master_link, &nfs_volume_list);
1047 spin_unlock(&nfs_client_lock);
1049 server->mount_time = jiffies;
1053 nfs_free_server(server);
1054 return ERR_PTR(error);
1057 #ifdef CONFIG_NFS_V4
1059 * Initialise an NFS4 client record
1061 static int nfs4_init_client(struct nfs_client *clp,
1062 const struct rpc_timeout *timeparms,
1063 const char *ip_addr,
1064 rpc_authflavor_t authflavour,
1069 if (clp->cl_cons_state == NFS_CS_READY) {
1070 /* the client is initialised already */
1071 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1075 /* Check NFS protocol revision and initialize RPC op vector */
1076 clp->rpc_ops = &nfs_v4_clientops;
1078 error = nfs_create_rpc_client(clp, timeparms, authflavour,
1079 1, flags & NFS_MOUNT_NORESVPORT);
1082 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1084 error = nfs_idmap_new(clp);
1086 dprintk("%s: failed to create idmapper. Error = %d\n",
1090 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1092 nfs_mark_client_ready(clp, NFS_CS_READY);
1096 nfs_mark_client_ready(clp, error);
1097 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1102 * Set up an NFS4 client
1104 static int nfs4_set_client(struct nfs_server *server,
1105 const char *hostname,
1106 const struct sockaddr *addr,
1107 const size_t addrlen,
1108 const char *ip_addr,
1109 rpc_authflavor_t authflavour,
1110 int proto, const struct rpc_timeout *timeparms)
1112 struct nfs_client_initdata cl_init = {
1113 .hostname = hostname,
1116 .rpc_ops = &nfs_v4_clientops,
1119 struct nfs_client *clp;
1122 dprintk("--> nfs4_set_client()\n");
1124 /* Allocate or find a client reference we can use */
1125 clp = nfs_get_client(&cl_init);
1127 error = PTR_ERR(clp);
1130 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1135 server->nfs_client = clp;
1136 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1140 nfs_put_client(clp);
1142 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1147 * Create a version 4 volume record
1149 static int nfs4_init_server(struct nfs_server *server,
1150 const struct nfs_parsed_mount_data *data)
1152 struct rpc_timeout timeparms;
1155 dprintk("--> nfs4_init_server()\n");
1157 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1158 data->timeo, data->retrans);
1160 /* Initialise the client representation from the mount data */
1161 server->flags = data->flags;
1162 server->caps |= NFS_CAP_ATOMIC_OPEN;
1164 /* Get a client record */
1165 error = nfs4_set_client(server,
1166 data->nfs_server.hostname,
1167 (const struct sockaddr *)&data->nfs_server.address,
1168 data->nfs_server.addrlen,
1169 data->client_address,
1170 data->auth_flavors[0],
1171 data->nfs_server.protocol,
1177 server->rsize = nfs_block_size(data->rsize, NULL);
1179 server->wsize = nfs_block_size(data->wsize, NULL);
1181 server->acregmin = data->acregmin * HZ;
1182 server->acregmax = data->acregmax * HZ;
1183 server->acdirmin = data->acdirmin * HZ;
1184 server->acdirmax = data->acdirmax * HZ;
1186 server->port = data->nfs_server.port;
1188 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1192 dprintk("<-- nfs4_init_server() = %d\n", error);
1197 * Create a version 4 volume record
1198 * - keyed on server and FSID
1200 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1201 struct nfs_fh *mntfh)
1203 struct nfs_fattr fattr;
1204 struct nfs_server *server;
1207 dprintk("--> nfs4_create_server()\n");
1209 server = nfs_alloc_server();
1211 return ERR_PTR(-ENOMEM);
1213 /* set up the general RPC client */
1214 error = nfs4_init_server(server, data);
1218 BUG_ON(!server->nfs_client);
1219 BUG_ON(!server->nfs_client->rpc_ops);
1220 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1222 /* Probe the root fh to retrieve its FSID */
1223 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1227 dprintk("Server FSID: %llx:%llx\n",
1228 (unsigned long long) server->fsid.major,
1229 (unsigned long long) server->fsid.minor);
1230 dprintk("Mount FH: %d\n", mntfh->size);
1232 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1236 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1237 server->namelen = NFS4_MAXNAMLEN;
1239 BUG_ON(!server->nfs_client);
1240 BUG_ON(!server->nfs_client->rpc_ops);
1241 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1243 spin_lock(&nfs_client_lock);
1244 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1245 list_add_tail(&server->master_link, &nfs_volume_list);
1246 spin_unlock(&nfs_client_lock);
1248 server->mount_time = jiffies;
1249 dprintk("<-- nfs4_create_server() = %p\n", server);
1253 nfs_free_server(server);
1254 dprintk("<-- nfs4_create_server() = error %d\n", error);
1255 return ERR_PTR(error);
1259 * Create an NFS4 referral server record
1261 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1262 struct nfs_fh *mntfh)
1264 struct nfs_client *parent_client;
1265 struct nfs_server *server, *parent_server;
1266 struct nfs_fattr fattr;
1269 dprintk("--> nfs4_create_referral_server()\n");
1271 server = nfs_alloc_server();
1273 return ERR_PTR(-ENOMEM);
1275 parent_server = NFS_SB(data->sb);
1276 parent_client = parent_server->nfs_client;
1278 /* Initialise the client representation from the parent server */
1279 nfs_server_copy_userdata(server, parent_server);
1280 server->caps |= NFS_CAP_ATOMIC_OPEN;
1282 /* Get a client representation.
1283 * Note: NFSv4 always uses TCP, */
1284 error = nfs4_set_client(server, data->hostname,
1287 parent_client->cl_ipaddr,
1289 parent_server->client->cl_xprt->prot,
1290 parent_server->client->cl_timeout);
1294 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1298 BUG_ON(!server->nfs_client);
1299 BUG_ON(!server->nfs_client->rpc_ops);
1300 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1302 /* Probe the root fh to retrieve its FSID and filehandle */
1303 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1307 /* probe the filesystem info for this server filesystem */
1308 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1312 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1313 server->namelen = NFS4_MAXNAMLEN;
1315 dprintk("Referral FSID: %llx:%llx\n",
1316 (unsigned long long) server->fsid.major,
1317 (unsigned long long) server->fsid.minor);
1319 spin_lock(&nfs_client_lock);
1320 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1321 list_add_tail(&server->master_link, &nfs_volume_list);
1322 spin_unlock(&nfs_client_lock);
1324 server->mount_time = jiffies;
1326 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1330 nfs_free_server(server);
1331 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1332 return ERR_PTR(error);
1335 #endif /* CONFIG_NFS_V4 */
1338 * Clone an NFS2, NFS3 or NFS4 server record
1340 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1342 struct nfs_fattr *fattr)
1344 struct nfs_server *server;
1345 struct nfs_fattr fattr_fsinfo;
1348 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1349 (unsigned long long) fattr->fsid.major,
1350 (unsigned long long) fattr->fsid.minor);
1352 server = nfs_alloc_server();
1354 return ERR_PTR(-ENOMEM);
1356 /* Copy data from the source */
1357 server->nfs_client = source->nfs_client;
1358 atomic_inc(&server->nfs_client->cl_count);
1359 nfs_server_copy_userdata(server, source);
1361 server->fsid = fattr->fsid;
1363 error = nfs_init_server_rpcclient(server,
1364 source->client->cl_timeout,
1365 source->client->cl_auth->au_flavor);
1367 goto out_free_server;
1368 if (!IS_ERR(source->client_acl))
1369 nfs_init_server_aclclient(server);
1371 /* probe the filesystem info for this server filesystem */
1372 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1374 goto out_free_server;
1376 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1377 server->namelen = NFS4_MAXNAMLEN;
1379 dprintk("Cloned FSID: %llx:%llx\n",
1380 (unsigned long long) server->fsid.major,
1381 (unsigned long long) server->fsid.minor);
1383 error = nfs_start_lockd(server);
1385 goto out_free_server;
1387 spin_lock(&nfs_client_lock);
1388 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1389 list_add_tail(&server->master_link, &nfs_volume_list);
1390 spin_unlock(&nfs_client_lock);
1392 server->mount_time = jiffies;
1394 dprintk("<-- nfs_clone_server() = %p\n", server);
1398 nfs_free_server(server);
1399 dprintk("<-- nfs_clone_server() = error %d\n", error);
1400 return ERR_PTR(error);
1403 #ifdef CONFIG_PROC_FS
1404 static struct proc_dir_entry *proc_fs_nfs;
1406 static int nfs_server_list_open(struct inode *inode, struct file *file);
1407 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1408 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1409 static void nfs_server_list_stop(struct seq_file *p, void *v);
1410 static int nfs_server_list_show(struct seq_file *m, void *v);
1412 static struct seq_operations nfs_server_list_ops = {
1413 .start = nfs_server_list_start,
1414 .next = nfs_server_list_next,
1415 .stop = nfs_server_list_stop,
1416 .show = nfs_server_list_show,
1419 static const struct file_operations nfs_server_list_fops = {
1420 .open = nfs_server_list_open,
1422 .llseek = seq_lseek,
1423 .release = seq_release,
1424 .owner = THIS_MODULE,
1427 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1428 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1429 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1430 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1431 static int nfs_volume_list_show(struct seq_file *m, void *v);
1433 static struct seq_operations nfs_volume_list_ops = {
1434 .start = nfs_volume_list_start,
1435 .next = nfs_volume_list_next,
1436 .stop = nfs_volume_list_stop,
1437 .show = nfs_volume_list_show,
1440 static const struct file_operations nfs_volume_list_fops = {
1441 .open = nfs_volume_list_open,
1443 .llseek = seq_lseek,
1444 .release = seq_release,
1445 .owner = THIS_MODULE,
1449 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1452 static int nfs_server_list_open(struct inode *inode, struct file *file)
1457 ret = seq_open(file, &nfs_server_list_ops);
1461 m = file->private_data;
1462 m->private = PDE(inode)->data;
1468 * set up the iterator to start reading from the server list and return the first item
1470 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1472 /* lock the list against modification */
1473 spin_lock(&nfs_client_lock);
1474 return seq_list_start_head(&nfs_client_list, *_pos);
1478 * move to next server
1480 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1482 return seq_list_next(v, &nfs_client_list, pos);
1486 * clean up after reading from the transports list
1488 static void nfs_server_list_stop(struct seq_file *p, void *v)
1490 spin_unlock(&nfs_client_lock);
1494 * display a header line followed by a load of call lines
1496 static int nfs_server_list_show(struct seq_file *m, void *v)
1498 struct nfs_client *clp;
1500 /* display header on line 1 */
1501 if (v == &nfs_client_list) {
1502 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1506 /* display one transport per line on subsequent lines */
1507 clp = list_entry(v, struct nfs_client, cl_share_link);
1509 seq_printf(m, "v%u %s %s %3d %s\n",
1510 clp->rpc_ops->version,
1511 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1512 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1513 atomic_read(&clp->cl_count),
1520 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1522 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1527 ret = seq_open(file, &nfs_volume_list_ops);
1531 m = file->private_data;
1532 m->private = PDE(inode)->data;
1538 * set up the iterator to start reading from the volume list and return the first item
1540 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1542 /* lock the list against modification */
1543 spin_lock(&nfs_client_lock);
1544 return seq_list_start_head(&nfs_volume_list, *_pos);
1548 * move to next volume
1550 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1552 return seq_list_next(v, &nfs_volume_list, pos);
1556 * clean up after reading from the transports list
1558 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1560 spin_unlock(&nfs_client_lock);
1564 * display a header line followed by a load of call lines
1566 static int nfs_volume_list_show(struct seq_file *m, void *v)
1568 struct nfs_server *server;
1569 struct nfs_client *clp;
1570 char dev[8], fsid[17];
1572 /* display header on line 1 */
1573 if (v == &nfs_volume_list) {
1574 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1577 /* display one transport per line on subsequent lines */
1578 server = list_entry(v, struct nfs_server, master_link);
1579 clp = server->nfs_client;
1581 snprintf(dev, 8, "%u:%u",
1582 MAJOR(server->s_dev), MINOR(server->s_dev));
1584 snprintf(fsid, 17, "%llx:%llx",
1585 (unsigned long long) server->fsid.major,
1586 (unsigned long long) server->fsid.minor);
1588 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1589 clp->rpc_ops->version,
1590 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1591 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1599 * initialise the /proc/fs/nfsfs/ directory
1601 int __init nfs_fs_proc_init(void)
1603 struct proc_dir_entry *p;
1605 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1609 proc_fs_nfs->owner = THIS_MODULE;
1611 /* a file of servers with which we're dealing */
1612 p = proc_create("servers", S_IFREG|S_IRUGO,
1613 proc_fs_nfs, &nfs_server_list_fops);
1617 /* a file of volumes that we have mounted */
1618 p = proc_create("volumes", S_IFREG|S_IRUGO,
1619 proc_fs_nfs, &nfs_volume_list_fops);
1625 remove_proc_entry("servers", proc_fs_nfs);
1627 remove_proc_entry("fs/nfsfs", NULL);
1633 * clean up the /proc/fs/nfsfs/ directory
1635 void nfs_fs_proc_exit(void)
1637 remove_proc_entry("volumes", proc_fs_nfs);
1638 remove_proc_entry("servers", proc_fs_nfs);
1639 remove_proc_entry("fs/nfsfs", NULL);
1642 #endif /* CONFIG_PROC_FS */