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 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
227 const struct sockaddr_in *sa2)
229 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
232 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
233 const struct sockaddr_in6 *sa2)
235 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
238 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
239 const struct sockaddr *sa2)
241 switch (sa1->sa_family) {
243 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
244 (const struct sockaddr_in *)sa2);
246 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
247 (const struct sockaddr_in6 *)sa2);
253 * Find a client by IP address and protocol version
254 * - returns NULL if no such client
256 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
258 struct nfs_client *clp;
260 spin_lock(&nfs_client_lock);
261 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
262 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
264 /* Don't match clients that failed to initialise properly */
265 if (clp->cl_cons_state != NFS_CS_READY)
268 /* Different NFS versions cannot share the same nfs_client */
269 if (clp->rpc_ops->version != nfsversion)
272 if (addr->sa_family != clap->sa_family)
274 /* Match only the IP address, not the port number */
275 if (!nfs_sockaddr_match_ipaddr(addr, clap))
278 atomic_inc(&clp->cl_count);
279 spin_unlock(&nfs_client_lock);
282 spin_unlock(&nfs_client_lock);
287 * Find a client by IP address and protocol version
288 * - returns NULL if no such client
290 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
292 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
293 u32 nfsvers = clp->rpc_ops->version;
295 spin_lock(&nfs_client_lock);
296 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
297 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
299 /* Don't match clients that failed to initialise properly */
300 if (clp->cl_cons_state != NFS_CS_READY)
303 /* Different NFS versions cannot share the same nfs_client */
304 if (clp->rpc_ops->version != nfsvers)
307 if (sap->sa_family != clap->sa_family)
309 /* Match only the IP address, not the port number */
310 if (!nfs_sockaddr_match_ipaddr(sap, clap))
313 atomic_inc(&clp->cl_count);
314 spin_unlock(&nfs_client_lock);
317 spin_unlock(&nfs_client_lock);
322 * Find an nfs_client on the list that matches the initialisation data
325 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
327 struct nfs_client *clp;
329 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
330 /* Don't match clients that failed to initialise properly */
331 if (clp->cl_cons_state < 0)
334 /* Different NFS versions cannot share the same nfs_client */
335 if (clp->rpc_ops != data->rpc_ops)
338 if (clp->cl_proto != data->proto)
341 /* Match the full socket address */
342 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
345 atomic_inc(&clp->cl_count);
352 * Look up a client by IP address and protocol version
353 * - creates a new record if one doesn't yet exist
355 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
357 struct nfs_client *clp, *new = NULL;
360 dprintk("--> nfs_get_client(%s,v%u)\n",
361 cl_init->hostname ?: "", cl_init->rpc_ops->version);
363 /* see if the client already exists */
365 spin_lock(&nfs_client_lock);
367 clp = nfs_match_client(cl_init);
373 spin_unlock(&nfs_client_lock);
375 new = nfs_alloc_client(cl_init);
378 return ERR_PTR(-ENOMEM);
380 /* install a new client and return with it unready */
383 list_add(&clp->cl_share_link, &nfs_client_list);
384 spin_unlock(&nfs_client_lock);
385 dprintk("--> nfs_get_client() = %p [new]\n", clp);
388 /* found an existing client
389 * - make sure it's ready before returning
392 spin_unlock(&nfs_client_lock);
395 nfs_free_client(new);
397 error = wait_event_killable(nfs_client_active_wq,
398 clp->cl_cons_state != NFS_CS_INITING);
401 return ERR_PTR(-ERESTARTSYS);
404 if (clp->cl_cons_state < NFS_CS_READY) {
405 error = clp->cl_cons_state;
407 return ERR_PTR(error);
410 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
412 dprintk("--> nfs_get_client() = %p [share]\n", clp);
417 * Mark a server as ready or failed
419 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
421 clp->cl_cons_state = state;
422 wake_up_all(&nfs_client_active_wq);
426 * Initialise the timeout values for a connection
428 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
429 unsigned int timeo, unsigned int retrans)
431 to->to_initval = timeo * HZ / 10;
432 to->to_retries = retrans;
435 case XPRT_TRANSPORT_TCP:
436 case XPRT_TRANSPORT_RDMA:
437 if (to->to_retries == 0)
438 to->to_retries = NFS_DEF_TCP_RETRANS;
439 if (to->to_initval == 0)
440 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
441 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
442 to->to_initval = NFS_MAX_TCP_TIMEOUT;
443 to->to_increment = to->to_initval;
444 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
445 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
446 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
447 if (to->to_maxval < to->to_initval)
448 to->to_maxval = to->to_initval;
449 to->to_exponential = 0;
451 case XPRT_TRANSPORT_UDP:
452 if (to->to_retries == 0)
453 to->to_retries = NFS_DEF_UDP_RETRANS;
455 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
456 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
457 to->to_initval = NFS_MAX_UDP_TIMEOUT;
458 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
459 to->to_exponential = 1;
467 * Create an RPC client handle
469 static int nfs_create_rpc_client(struct nfs_client *clp,
470 const struct rpc_timeout *timeparms,
471 rpc_authflavor_t flavor,
472 int discrtry, int noresvport)
474 struct rpc_clnt *clnt = NULL;
475 struct rpc_create_args args = {
476 .protocol = clp->cl_proto,
477 .address = (struct sockaddr *)&clp->cl_addr,
478 .addrsize = clp->cl_addrlen,
479 .timeout = timeparms,
480 .servername = clp->cl_hostname,
481 .program = &nfs_program,
482 .version = clp->rpc_ops->version,
483 .authflavor = flavor,
487 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
489 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
491 if (!IS_ERR(clp->cl_rpcclient))
494 clnt = rpc_create(&args);
496 dprintk("%s: cannot create RPC client. Error = %ld\n",
497 __func__, PTR_ERR(clnt));
498 return PTR_ERR(clnt);
501 clp->cl_rpcclient = clnt;
506 * Version 2 or 3 client destruction
508 static void nfs_destroy_server(struct nfs_server *server)
510 if (!(server->flags & NFS_MOUNT_NONLM))
511 nlmclnt_done(server->nlm_host);
515 * Version 2 or 3 lockd setup
517 static int nfs_start_lockd(struct nfs_server *server)
519 struct nlm_host *host;
520 struct nfs_client *clp = server->nfs_client;
521 struct nlmclnt_initdata nlm_init = {
522 .hostname = clp->cl_hostname,
523 .address = (struct sockaddr *)&clp->cl_addr,
524 .addrlen = clp->cl_addrlen,
525 .protocol = server->flags & NFS_MOUNT_TCP ?
526 IPPROTO_TCP : IPPROTO_UDP,
527 .nfs_version = clp->rpc_ops->version,
528 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
532 if (nlm_init.nfs_version > 3)
534 if (server->flags & NFS_MOUNT_NONLM)
537 host = nlmclnt_init(&nlm_init);
539 return PTR_ERR(host);
541 server->nlm_host = host;
542 server->destroy = nfs_destroy_server;
547 * Initialise an NFSv3 ACL client connection
549 #ifdef CONFIG_NFS_V3_ACL
550 static void nfs_init_server_aclclient(struct nfs_server *server)
552 if (server->nfs_client->rpc_ops->version != 3)
554 if (server->flags & NFS_MOUNT_NOACL)
557 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
558 if (IS_ERR(server->client_acl))
561 /* No errors! Assume that Sun nfsacls are supported */
562 server->caps |= NFS_CAP_ACLS;
566 server->caps &= ~NFS_CAP_ACLS;
569 static inline void nfs_init_server_aclclient(struct nfs_server *server)
571 server->flags &= ~NFS_MOUNT_NOACL;
572 server->caps &= ~NFS_CAP_ACLS;
577 * Create a general RPC client
579 static int nfs_init_server_rpcclient(struct nfs_server *server,
580 const struct rpc_timeout *timeo,
581 rpc_authflavor_t pseudoflavour)
583 struct nfs_client *clp = server->nfs_client;
585 server->client = rpc_clone_client(clp->cl_rpcclient);
586 if (IS_ERR(server->client)) {
587 dprintk("%s: couldn't create rpc_client!\n", __func__);
588 return PTR_ERR(server->client);
591 memcpy(&server->client->cl_timeout_default,
593 sizeof(server->client->cl_timeout_default));
594 server->client->cl_timeout = &server->client->cl_timeout_default;
596 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
597 struct rpc_auth *auth;
599 auth = rpcauth_create(pseudoflavour, server->client);
601 dprintk("%s: couldn't create credcache!\n", __func__);
602 return PTR_ERR(auth);
605 server->client->cl_softrtry = 0;
606 if (server->flags & NFS_MOUNT_SOFT)
607 server->client->cl_softrtry = 1;
613 * Initialise an NFS2 or NFS3 client
615 static int nfs_init_client(struct nfs_client *clp,
616 const struct rpc_timeout *timeparms,
617 const struct nfs_parsed_mount_data *data)
621 if (clp->cl_cons_state == NFS_CS_READY) {
622 /* the client is already initialised */
623 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
628 * Create a client RPC handle for doing FSSTAT with UNIX auth only
629 * - RFC 2623, sec 2.3.2
631 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
632 0, data->flags & NFS_MOUNT_NORESVPORT);
635 nfs_mark_client_ready(clp, NFS_CS_READY);
639 nfs_mark_client_ready(clp, error);
640 dprintk("<-- nfs_init_client() = xerror %d\n", error);
645 * Create a version 2 or 3 client
647 static int nfs_init_server(struct nfs_server *server,
648 const struct nfs_parsed_mount_data *data)
650 struct nfs_client_initdata cl_init = {
651 .hostname = data->nfs_server.hostname,
652 .addr = (const struct sockaddr *)&data->nfs_server.address,
653 .addrlen = data->nfs_server.addrlen,
654 .rpc_ops = &nfs_v2_clientops,
655 .proto = data->nfs_server.protocol,
657 struct rpc_timeout timeparms;
658 struct nfs_client *clp;
661 dprintk("--> nfs_init_server()\n");
664 if (data->flags & NFS_MOUNT_VER3)
665 cl_init.rpc_ops = &nfs_v3_clientops;
668 /* Allocate or find a client reference we can use */
669 clp = nfs_get_client(&cl_init);
671 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
675 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
676 data->timeo, data->retrans);
677 error = nfs_init_client(clp, &timeparms, data);
681 server->nfs_client = clp;
683 /* Initialise the client representation from the mount data */
684 server->flags = data->flags;
687 server->rsize = nfs_block_size(data->rsize, NULL);
689 server->wsize = nfs_block_size(data->wsize, NULL);
691 server->acregmin = data->acregmin * HZ;
692 server->acregmax = data->acregmax * HZ;
693 server->acdirmin = data->acdirmin * HZ;
694 server->acdirmax = data->acdirmax * HZ;
696 /* Start lockd here, before we might error out */
697 error = nfs_start_lockd(server);
701 server->port = data->nfs_server.port;
703 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
707 /* Preserve the values of mount_server-related mount options */
708 if (data->mount_server.addrlen) {
709 memcpy(&server->mountd_address, &data->mount_server.address,
710 data->mount_server.addrlen);
711 server->mountd_addrlen = data->mount_server.addrlen;
713 server->mountd_version = data->mount_server.version;
714 server->mountd_port = data->mount_server.port;
715 server->mountd_protocol = data->mount_server.protocol;
717 server->namelen = data->namlen;
718 /* Create a client RPC handle for the NFSv3 ACL management interface */
719 nfs_init_server_aclclient(server);
720 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
724 server->nfs_client = NULL;
726 dprintk("<-- nfs_init_server() = xerror %d\n", error);
731 * Load up the server record from information gained in an fsinfo record
733 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
735 unsigned long max_rpc_payload;
737 /* Work out a lot of parameters */
738 if (server->rsize == 0)
739 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
740 if (server->wsize == 0)
741 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
743 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
744 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
745 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
746 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
748 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
749 if (server->rsize > max_rpc_payload)
750 server->rsize = max_rpc_payload;
751 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
752 server->rsize = NFS_MAX_FILE_IO_SIZE;
753 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
755 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
757 if (server->wsize > max_rpc_payload)
758 server->wsize = max_rpc_payload;
759 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
760 server->wsize = NFS_MAX_FILE_IO_SIZE;
761 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
762 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
764 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
765 if (server->dtsize > PAGE_CACHE_SIZE)
766 server->dtsize = PAGE_CACHE_SIZE;
767 if (server->dtsize > server->rsize)
768 server->dtsize = server->rsize;
770 if (server->flags & NFS_MOUNT_NOAC) {
771 server->acregmin = server->acregmax = 0;
772 server->acdirmin = server->acdirmax = 0;
775 server->maxfilesize = fsinfo->maxfilesize;
777 /* We're airborne Set socket buffersize */
778 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
782 * Probe filesystem information, including the FSID on v2/v3
784 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
786 struct nfs_fsinfo fsinfo;
787 struct nfs_client *clp = server->nfs_client;
790 dprintk("--> nfs_probe_fsinfo()\n");
792 if (clp->rpc_ops->set_capabilities != NULL) {
793 error = clp->rpc_ops->set_capabilities(server, mntfh);
798 fsinfo.fattr = fattr;
799 nfs_fattr_init(fattr);
800 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
804 nfs_server_set_fsinfo(server, &fsinfo);
805 error = bdi_init(&server->backing_dev_info);
810 /* Get some general file system info */
811 if (server->namelen == 0) {
812 struct nfs_pathconf pathinfo;
814 pathinfo.fattr = fattr;
815 nfs_fattr_init(fattr);
817 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
818 server->namelen = pathinfo.max_namelen;
821 dprintk("<-- nfs_probe_fsinfo() = 0\n");
825 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
830 * Copy useful information when duplicating a server record
832 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
834 target->flags = source->flags;
835 target->acregmin = source->acregmin;
836 target->acregmax = source->acregmax;
837 target->acdirmin = source->acdirmin;
838 target->acdirmax = source->acdirmax;
839 target->caps = source->caps;
843 * Allocate and initialise a server record
845 static struct nfs_server *nfs_alloc_server(void)
847 struct nfs_server *server;
849 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
853 server->client = server->client_acl = ERR_PTR(-EINVAL);
855 /* Zero out the NFS state stuff */
856 INIT_LIST_HEAD(&server->client_link);
857 INIT_LIST_HEAD(&server->master_link);
859 atomic_set(&server->active, 0);
861 server->io_stats = nfs_alloc_iostats();
862 if (!server->io_stats) {
871 * Free up a server record
873 void nfs_free_server(struct nfs_server *server)
875 dprintk("--> nfs_free_server()\n");
877 spin_lock(&nfs_client_lock);
878 list_del(&server->client_link);
879 list_del(&server->master_link);
880 spin_unlock(&nfs_client_lock);
882 if (server->destroy != NULL)
883 server->destroy(server);
885 if (!IS_ERR(server->client_acl))
886 rpc_shutdown_client(server->client_acl);
887 if (!IS_ERR(server->client))
888 rpc_shutdown_client(server->client);
890 nfs_put_client(server->nfs_client);
892 nfs_free_iostats(server->io_stats);
893 bdi_destroy(&server->backing_dev_info);
895 nfs_release_automount_timer();
896 dprintk("<-- nfs_free_server()\n");
900 * Create a version 2 or 3 volume record
901 * - keyed on server and FSID
903 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
904 struct nfs_fh *mntfh)
906 struct nfs_server *server;
907 struct nfs_fattr fattr;
910 server = nfs_alloc_server();
912 return ERR_PTR(-ENOMEM);
914 /* Get a client representation */
915 error = nfs_init_server(server, data);
919 BUG_ON(!server->nfs_client);
920 BUG_ON(!server->nfs_client->rpc_ops);
921 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
923 /* Probe the root fh to retrieve its FSID */
924 error = nfs_probe_fsinfo(server, mntfh, &fattr);
927 if (server->nfs_client->rpc_ops->version == 3) {
928 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
929 server->namelen = NFS3_MAXNAMLEN;
930 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
931 server->caps |= NFS_CAP_READDIRPLUS;
933 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
934 server->namelen = NFS2_MAXNAMLEN;
937 if (!(fattr.valid & NFS_ATTR_FATTR)) {
938 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
940 dprintk("nfs_create_server: getattr error = %d\n", -error);
944 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
946 dprintk("Server FSID: %llx:%llx\n",
947 (unsigned long long) server->fsid.major,
948 (unsigned long long) server->fsid.minor);
950 BUG_ON(!server->nfs_client);
951 BUG_ON(!server->nfs_client->rpc_ops);
952 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
954 spin_lock(&nfs_client_lock);
955 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
956 list_add_tail(&server->master_link, &nfs_volume_list);
957 spin_unlock(&nfs_client_lock);
959 server->mount_time = jiffies;
963 nfs_free_server(server);
964 return ERR_PTR(error);
969 * Initialise an NFS4 client record
971 static int nfs4_init_client(struct nfs_client *clp,
972 const struct rpc_timeout *timeparms,
974 rpc_authflavor_t authflavour,
979 if (clp->cl_cons_state == NFS_CS_READY) {
980 /* the client is initialised already */
981 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
985 /* Check NFS protocol revision and initialize RPC op vector */
986 clp->rpc_ops = &nfs_v4_clientops;
988 error = nfs_create_rpc_client(clp, timeparms, authflavour,
989 1, flags & NFS_MOUNT_NORESVPORT);
992 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
994 error = nfs_idmap_new(clp);
996 dprintk("%s: failed to create idmapper. Error = %d\n",
1000 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1002 nfs_mark_client_ready(clp, NFS_CS_READY);
1006 nfs_mark_client_ready(clp, error);
1007 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1012 * Set up an NFS4 client
1014 static int nfs4_set_client(struct nfs_server *server,
1015 const char *hostname,
1016 const struct sockaddr *addr,
1017 const size_t addrlen,
1018 const char *ip_addr,
1019 rpc_authflavor_t authflavour,
1020 int proto, const struct rpc_timeout *timeparms)
1022 struct nfs_client_initdata cl_init = {
1023 .hostname = hostname,
1026 .rpc_ops = &nfs_v4_clientops,
1029 struct nfs_client *clp;
1032 dprintk("--> nfs4_set_client()\n");
1034 /* Allocate or find a client reference we can use */
1035 clp = nfs_get_client(&cl_init);
1037 error = PTR_ERR(clp);
1040 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1045 server->nfs_client = clp;
1046 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1050 nfs_put_client(clp);
1052 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1057 * Create a version 4 volume record
1059 static int nfs4_init_server(struct nfs_server *server,
1060 const struct nfs_parsed_mount_data *data)
1062 struct rpc_timeout timeparms;
1065 dprintk("--> nfs4_init_server()\n");
1067 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1068 data->timeo, data->retrans);
1070 /* Initialise the client representation from the mount data */
1071 server->flags = data->flags;
1072 server->caps |= NFS_CAP_ATOMIC_OPEN;
1074 /* Get a client record */
1075 error = nfs4_set_client(server,
1076 data->nfs_server.hostname,
1077 (const struct sockaddr *)&data->nfs_server.address,
1078 data->nfs_server.addrlen,
1079 data->client_address,
1080 data->auth_flavors[0],
1081 data->nfs_server.protocol,
1087 server->rsize = nfs_block_size(data->rsize, NULL);
1089 server->wsize = nfs_block_size(data->wsize, NULL);
1091 server->acregmin = data->acregmin * HZ;
1092 server->acregmax = data->acregmax * HZ;
1093 server->acdirmin = data->acdirmin * HZ;
1094 server->acdirmax = data->acdirmax * HZ;
1096 server->port = data->nfs_server.port;
1098 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1102 dprintk("<-- nfs4_init_server() = %d\n", error);
1107 * Create a version 4 volume record
1108 * - keyed on server and FSID
1110 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1111 struct nfs_fh *mntfh)
1113 struct nfs_fattr fattr;
1114 struct nfs_server *server;
1117 dprintk("--> nfs4_create_server()\n");
1119 server = nfs_alloc_server();
1121 return ERR_PTR(-ENOMEM);
1123 /* set up the general RPC client */
1124 error = nfs4_init_server(server, data);
1128 BUG_ON(!server->nfs_client);
1129 BUG_ON(!server->nfs_client->rpc_ops);
1130 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1132 /* Probe the root fh to retrieve its FSID */
1133 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1137 dprintk("Server FSID: %llx:%llx\n",
1138 (unsigned long long) server->fsid.major,
1139 (unsigned long long) server->fsid.minor);
1140 dprintk("Mount FH: %d\n", mntfh->size);
1142 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1146 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1147 server->namelen = NFS4_MAXNAMLEN;
1149 BUG_ON(!server->nfs_client);
1150 BUG_ON(!server->nfs_client->rpc_ops);
1151 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1153 spin_lock(&nfs_client_lock);
1154 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1155 list_add_tail(&server->master_link, &nfs_volume_list);
1156 spin_unlock(&nfs_client_lock);
1158 server->mount_time = jiffies;
1159 dprintk("<-- nfs4_create_server() = %p\n", server);
1163 nfs_free_server(server);
1164 dprintk("<-- nfs4_create_server() = error %d\n", error);
1165 return ERR_PTR(error);
1169 * Create an NFS4 referral server record
1171 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1172 struct nfs_fh *mntfh)
1174 struct nfs_client *parent_client;
1175 struct nfs_server *server, *parent_server;
1176 struct nfs_fattr fattr;
1179 dprintk("--> nfs4_create_referral_server()\n");
1181 server = nfs_alloc_server();
1183 return ERR_PTR(-ENOMEM);
1185 parent_server = NFS_SB(data->sb);
1186 parent_client = parent_server->nfs_client;
1188 /* Initialise the client representation from the parent server */
1189 nfs_server_copy_userdata(server, parent_server);
1190 server->caps |= NFS_CAP_ATOMIC_OPEN;
1192 /* Get a client representation.
1193 * Note: NFSv4 always uses TCP, */
1194 error = nfs4_set_client(server, data->hostname,
1197 parent_client->cl_ipaddr,
1199 parent_server->client->cl_xprt->prot,
1200 parent_server->client->cl_timeout);
1204 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1208 BUG_ON(!server->nfs_client);
1209 BUG_ON(!server->nfs_client->rpc_ops);
1210 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1212 /* Probe the root fh to retrieve its FSID and filehandle */
1213 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1217 /* probe the filesystem info for this server filesystem */
1218 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1222 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1223 server->namelen = NFS4_MAXNAMLEN;
1225 dprintk("Referral FSID: %llx:%llx\n",
1226 (unsigned long long) server->fsid.major,
1227 (unsigned long long) server->fsid.minor);
1229 spin_lock(&nfs_client_lock);
1230 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1231 list_add_tail(&server->master_link, &nfs_volume_list);
1232 spin_unlock(&nfs_client_lock);
1234 server->mount_time = jiffies;
1236 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1240 nfs_free_server(server);
1241 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1242 return ERR_PTR(error);
1245 #endif /* CONFIG_NFS_V4 */
1248 * Clone an NFS2, NFS3 or NFS4 server record
1250 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1252 struct nfs_fattr *fattr)
1254 struct nfs_server *server;
1255 struct nfs_fattr fattr_fsinfo;
1258 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1259 (unsigned long long) fattr->fsid.major,
1260 (unsigned long long) fattr->fsid.minor);
1262 server = nfs_alloc_server();
1264 return ERR_PTR(-ENOMEM);
1266 /* Copy data from the source */
1267 server->nfs_client = source->nfs_client;
1268 atomic_inc(&server->nfs_client->cl_count);
1269 nfs_server_copy_userdata(server, source);
1271 server->fsid = fattr->fsid;
1273 error = nfs_init_server_rpcclient(server,
1274 source->client->cl_timeout,
1275 source->client->cl_auth->au_flavor);
1277 goto out_free_server;
1278 if (!IS_ERR(source->client_acl))
1279 nfs_init_server_aclclient(server);
1281 /* probe the filesystem info for this server filesystem */
1282 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1284 goto out_free_server;
1286 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1287 server->namelen = NFS4_MAXNAMLEN;
1289 dprintk("Cloned FSID: %llx:%llx\n",
1290 (unsigned long long) server->fsid.major,
1291 (unsigned long long) server->fsid.minor);
1293 error = nfs_start_lockd(server);
1295 goto out_free_server;
1297 spin_lock(&nfs_client_lock);
1298 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1299 list_add_tail(&server->master_link, &nfs_volume_list);
1300 spin_unlock(&nfs_client_lock);
1302 server->mount_time = jiffies;
1304 dprintk("<-- nfs_clone_server() = %p\n", server);
1308 nfs_free_server(server);
1309 dprintk("<-- nfs_clone_server() = error %d\n", error);
1310 return ERR_PTR(error);
1313 #ifdef CONFIG_PROC_FS
1314 static struct proc_dir_entry *proc_fs_nfs;
1316 static int nfs_server_list_open(struct inode *inode, struct file *file);
1317 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1318 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1319 static void nfs_server_list_stop(struct seq_file *p, void *v);
1320 static int nfs_server_list_show(struct seq_file *m, void *v);
1322 static struct seq_operations nfs_server_list_ops = {
1323 .start = nfs_server_list_start,
1324 .next = nfs_server_list_next,
1325 .stop = nfs_server_list_stop,
1326 .show = nfs_server_list_show,
1329 static const struct file_operations nfs_server_list_fops = {
1330 .open = nfs_server_list_open,
1332 .llseek = seq_lseek,
1333 .release = seq_release,
1334 .owner = THIS_MODULE,
1337 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1338 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1339 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1340 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1341 static int nfs_volume_list_show(struct seq_file *m, void *v);
1343 static struct seq_operations nfs_volume_list_ops = {
1344 .start = nfs_volume_list_start,
1345 .next = nfs_volume_list_next,
1346 .stop = nfs_volume_list_stop,
1347 .show = nfs_volume_list_show,
1350 static const struct file_operations nfs_volume_list_fops = {
1351 .open = nfs_volume_list_open,
1353 .llseek = seq_lseek,
1354 .release = seq_release,
1355 .owner = THIS_MODULE,
1359 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1362 static int nfs_server_list_open(struct inode *inode, struct file *file)
1367 ret = seq_open(file, &nfs_server_list_ops);
1371 m = file->private_data;
1372 m->private = PDE(inode)->data;
1378 * set up the iterator to start reading from the server list and return the first item
1380 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1382 /* lock the list against modification */
1383 spin_lock(&nfs_client_lock);
1384 return seq_list_start_head(&nfs_client_list, *_pos);
1388 * move to next server
1390 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1392 return seq_list_next(v, &nfs_client_list, pos);
1396 * clean up after reading from the transports list
1398 static void nfs_server_list_stop(struct seq_file *p, void *v)
1400 spin_unlock(&nfs_client_lock);
1404 * display a header line followed by a load of call lines
1406 static int nfs_server_list_show(struct seq_file *m, void *v)
1408 struct nfs_client *clp;
1410 /* display header on line 1 */
1411 if (v == &nfs_client_list) {
1412 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1416 /* display one transport per line on subsequent lines */
1417 clp = list_entry(v, struct nfs_client, cl_share_link);
1419 seq_printf(m, "v%u %s %s %3d %s\n",
1420 clp->rpc_ops->version,
1421 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1422 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1423 atomic_read(&clp->cl_count),
1430 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1432 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1437 ret = seq_open(file, &nfs_volume_list_ops);
1441 m = file->private_data;
1442 m->private = PDE(inode)->data;
1448 * set up the iterator to start reading from the volume list and return the first item
1450 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1452 /* lock the list against modification */
1453 spin_lock(&nfs_client_lock);
1454 return seq_list_start_head(&nfs_volume_list, *_pos);
1458 * move to next volume
1460 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1462 return seq_list_next(v, &nfs_volume_list, pos);
1466 * clean up after reading from the transports list
1468 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1470 spin_unlock(&nfs_client_lock);
1474 * display a header line followed by a load of call lines
1476 static int nfs_volume_list_show(struct seq_file *m, void *v)
1478 struct nfs_server *server;
1479 struct nfs_client *clp;
1480 char dev[8], fsid[17];
1482 /* display header on line 1 */
1483 if (v == &nfs_volume_list) {
1484 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1487 /* display one transport per line on subsequent lines */
1488 server = list_entry(v, struct nfs_server, master_link);
1489 clp = server->nfs_client;
1491 snprintf(dev, 8, "%u:%u",
1492 MAJOR(server->s_dev), MINOR(server->s_dev));
1494 snprintf(fsid, 17, "%llx:%llx",
1495 (unsigned long long) server->fsid.major,
1496 (unsigned long long) server->fsid.minor);
1498 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1499 clp->rpc_ops->version,
1500 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1501 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1509 * initialise the /proc/fs/nfsfs/ directory
1511 int __init nfs_fs_proc_init(void)
1513 struct proc_dir_entry *p;
1515 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1519 proc_fs_nfs->owner = THIS_MODULE;
1521 /* a file of servers with which we're dealing */
1522 p = proc_create("servers", S_IFREG|S_IRUGO,
1523 proc_fs_nfs, &nfs_server_list_fops);
1527 /* a file of volumes that we have mounted */
1528 p = proc_create("volumes", S_IFREG|S_IRUGO,
1529 proc_fs_nfs, &nfs_volume_list_fops);
1535 remove_proc_entry("servers", proc_fs_nfs);
1537 remove_proc_entry("fs/nfsfs", NULL);
1543 * clean up the /proc/fs/nfsfs/ directory
1545 void nfs_fs_proc_exit(void)
1547 remove_proc_entry("volumes", proc_fs_nfs);
1548 remove_proc_entry("servers", proc_fs_nfs);
1549 remove_proc_entry("fs/nfsfs", NULL);
1552 #endif /* CONFIG_PROC_FS */