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;
106 * Allocate a shared client record
108 * Since these are allocated/deallocated very rarely, we don't
109 * bother putting them in a slab cache...
111 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
113 struct nfs_client *clp;
115 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
118 clp->rpc_ops = cl_init->rpc_ops;
120 if (cl_init->rpc_ops->version == 4) {
121 if (nfs_callback_up() < 0)
123 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
126 atomic_set(&clp->cl_count, 1);
127 clp->cl_cons_state = NFS_CS_INITING;
129 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
130 clp->cl_addrlen = cl_init->addrlen;
132 if (cl_init->hostname) {
133 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
134 if (!clp->cl_hostname)
138 INIT_LIST_HEAD(&clp->cl_superblocks);
139 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142 init_rwsem(&clp->cl_sem);
143 INIT_LIST_HEAD(&clp->cl_delegations);
144 spin_lock_init(&clp->cl_lock);
145 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
146 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
147 clp->cl_boot_time = CURRENT_TIME;
148 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
154 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
162 static void nfs4_shutdown_client(struct nfs_client *clp)
165 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
166 nfs4_kill_renewd(clp);
167 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
168 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
169 nfs_idmap_delete(clp);
174 * Destroy a shared client record
176 static void nfs_free_client(struct nfs_client *clp)
178 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
180 nfs4_shutdown_client(clp);
182 /* -EIO all pending I/O */
183 if (!IS_ERR(clp->cl_rpcclient))
184 rpc_shutdown_client(clp->cl_rpcclient);
186 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
189 kfree(clp->cl_hostname);
192 dprintk("<-- nfs_free_client()\n");
196 * Release a reference to a shared client record
198 void nfs_put_client(struct nfs_client *clp)
203 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
205 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
206 list_del(&clp->cl_share_link);
207 spin_unlock(&nfs_client_lock);
209 BUG_ON(!list_empty(&clp->cl_superblocks));
211 nfs_free_client(clp);
215 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
216 const struct sockaddr_in *sa2)
218 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
221 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
222 const struct sockaddr_in6 *sa2)
224 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
227 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
228 const struct sockaddr *sa2)
230 switch (sa1->sa_family) {
232 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
233 (const struct sockaddr_in *)sa2);
235 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
236 (const struct sockaddr_in6 *)sa2);
242 * Find a client by IP address and protocol version
243 * - returns NULL if no such client
245 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
247 struct nfs_client *clp;
249 spin_lock(&nfs_client_lock);
250 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
251 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
253 /* Don't match clients that failed to initialise properly */
254 if (clp->cl_cons_state != NFS_CS_READY)
257 /* Different NFS versions cannot share the same nfs_client */
258 if (clp->rpc_ops->version != nfsversion)
261 if (addr->sa_family != clap->sa_family)
263 /* Match only the IP address, not the port number */
264 if (!nfs_sockaddr_match_ipaddr(addr, clap))
267 atomic_inc(&clp->cl_count);
268 spin_unlock(&nfs_client_lock);
271 spin_unlock(&nfs_client_lock);
276 * Find an nfs_client on the list that matches the initialisation data
279 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
281 struct nfs_client *clp;
283 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
284 /* Don't match clients that failed to initialise properly */
285 if (clp->cl_cons_state < 0)
288 /* Different NFS versions cannot share the same nfs_client */
289 if (clp->rpc_ops != data->rpc_ops)
292 /* Match the full socket address */
293 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
296 atomic_inc(&clp->cl_count);
303 * Look up a client by IP address and protocol version
304 * - creates a new record if one doesn't yet exist
306 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
308 struct nfs_client *clp, *new = NULL;
311 dprintk("--> nfs_get_client(%s,v%u)\n",
312 cl_init->hostname ?: "", cl_init->rpc_ops->version);
314 /* see if the client already exists */
316 spin_lock(&nfs_client_lock);
318 clp = nfs_match_client(cl_init);
324 spin_unlock(&nfs_client_lock);
326 new = nfs_alloc_client(cl_init);
329 return ERR_PTR(-ENOMEM);
331 /* install a new client and return with it unready */
334 list_add(&clp->cl_share_link, &nfs_client_list);
335 spin_unlock(&nfs_client_lock);
336 dprintk("--> nfs_get_client() = %p [new]\n", clp);
339 /* found an existing client
340 * - make sure it's ready before returning
343 spin_unlock(&nfs_client_lock);
346 nfs_free_client(new);
348 error = wait_event_interruptible(nfs_client_active_wq,
349 clp->cl_cons_state != NFS_CS_INITING);
352 return ERR_PTR(-ERESTARTSYS);
355 if (clp->cl_cons_state < NFS_CS_READY) {
356 error = clp->cl_cons_state;
358 return ERR_PTR(error);
361 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
363 dprintk("--> nfs_get_client() = %p [share]\n", clp);
368 * Mark a server as ready or failed
370 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
372 clp->cl_cons_state = state;
373 wake_up_all(&nfs_client_active_wq);
377 * Initialise the timeout values for a connection
379 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
380 unsigned int timeo, unsigned int retrans)
382 to->to_initval = timeo * HZ / 10;
383 to->to_retries = retrans;
388 case XPRT_TRANSPORT_TCP:
389 case XPRT_TRANSPORT_RDMA:
391 to->to_initval = 60 * HZ;
392 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
393 to->to_initval = NFS_MAX_TCP_TIMEOUT;
394 to->to_increment = to->to_initval;
395 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
396 to->to_exponential = 0;
398 case XPRT_TRANSPORT_UDP:
401 to->to_initval = 11 * HZ / 10;
402 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
403 to->to_initval = NFS_MAX_UDP_TIMEOUT;
404 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
405 to->to_exponential = 1;
411 * Create an RPC client handle
413 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
415 unsigned int retrans,
416 rpc_authflavor_t flavor,
419 struct rpc_timeout timeparms;
420 struct rpc_clnt *clnt = NULL;
421 struct rpc_create_args args = {
423 .address = (struct sockaddr *)&clp->cl_addr,
424 .addrsize = clp->cl_addrlen,
425 .timeout = &timeparms,
426 .servername = clp->cl_hostname,
427 .program = &nfs_program,
428 .version = clp->rpc_ops->version,
429 .authflavor = flavor,
433 if (!IS_ERR(clp->cl_rpcclient))
436 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
437 clp->retrans_timeo = timeparms.to_initval;
438 clp->retrans_count = timeparms.to_retries;
440 clnt = rpc_create(&args);
442 dprintk("%s: cannot create RPC client. Error = %ld\n",
443 __FUNCTION__, PTR_ERR(clnt));
444 return PTR_ERR(clnt);
447 clp->cl_rpcclient = clnt;
452 * Version 2 or 3 client destruction
454 static void nfs_destroy_server(struct nfs_server *server)
456 if (!(server->flags & NFS_MOUNT_NONLM))
457 lockd_down(); /* release rpc.lockd */
461 * Version 2 or 3 lockd setup
463 static int nfs_start_lockd(struct nfs_server *server)
467 if (server->nfs_client->rpc_ops->version > 3)
469 if (server->flags & NFS_MOUNT_NONLM)
471 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
472 IPPROTO_TCP : IPPROTO_UDP);
474 server->flags |= NFS_MOUNT_NONLM;
476 server->destroy = nfs_destroy_server;
482 * Initialise an NFSv3 ACL client connection
484 #ifdef CONFIG_NFS_V3_ACL
485 static void nfs_init_server_aclclient(struct nfs_server *server)
487 if (server->nfs_client->rpc_ops->version != 3)
489 if (server->flags & NFS_MOUNT_NOACL)
492 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
493 if (IS_ERR(server->client_acl))
496 /* No errors! Assume that Sun nfsacls are supported */
497 server->caps |= NFS_CAP_ACLS;
501 server->caps &= ~NFS_CAP_ACLS;
504 static inline void nfs_init_server_aclclient(struct nfs_server *server)
506 server->flags &= ~NFS_MOUNT_NOACL;
507 server->caps &= ~NFS_CAP_ACLS;
512 * Create a general RPC client
514 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
516 struct nfs_client *clp = server->nfs_client;
518 server->client = rpc_clone_client(clp->cl_rpcclient);
519 if (IS_ERR(server->client)) {
520 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
521 return PTR_ERR(server->client);
524 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
525 struct rpc_auth *auth;
527 auth = rpcauth_create(pseudoflavour, server->client);
529 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
530 return PTR_ERR(auth);
533 server->client->cl_softrtry = 0;
534 if (server->flags & NFS_MOUNT_SOFT)
535 server->client->cl_softrtry = 1;
537 server->client->cl_intr = 0;
538 if (server->flags & NFS4_MOUNT_INTR)
539 server->client->cl_intr = 1;
545 * Initialise an NFS2 or NFS3 client
547 static int nfs_init_client(struct nfs_client *clp,
548 const struct nfs_parsed_mount_data *data)
552 if (clp->cl_cons_state == NFS_CS_READY) {
553 /* the client is already initialised */
554 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
559 * Create a client RPC handle for doing FSSTAT with UNIX auth only
560 * - RFC 2623, sec 2.3.2
562 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
563 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
566 nfs_mark_client_ready(clp, NFS_CS_READY);
570 nfs_mark_client_ready(clp, error);
571 dprintk("<-- nfs_init_client() = xerror %d\n", error);
576 * Create a version 2 or 3 client
578 static int nfs_init_server(struct nfs_server *server,
579 const struct nfs_parsed_mount_data *data)
581 struct nfs_client_initdata cl_init = {
582 .hostname = data->nfs_server.hostname,
583 .addr = (const struct sockaddr *)&data->nfs_server.address,
584 .addrlen = data->nfs_server.addrlen,
585 .rpc_ops = &nfs_v2_clientops,
587 struct nfs_client *clp;
590 dprintk("--> nfs_init_server()\n");
593 if (data->flags & NFS_MOUNT_VER3)
594 cl_init.rpc_ops = &nfs_v3_clientops;
597 /* Allocate or find a client reference we can use */
598 clp = nfs_get_client(&cl_init);
600 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
604 error = nfs_init_client(clp, data);
608 server->nfs_client = clp;
610 /* Initialise the client representation from the mount data */
611 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
614 server->rsize = nfs_block_size(data->rsize, NULL);
616 server->wsize = nfs_block_size(data->wsize, NULL);
618 server->acregmin = data->acregmin * HZ;
619 server->acregmax = data->acregmax * HZ;
620 server->acdirmin = data->acdirmin * HZ;
621 server->acdirmax = data->acdirmax * HZ;
623 /* Start lockd here, before we might error out */
624 error = nfs_start_lockd(server);
628 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
632 server->namelen = data->namlen;
633 /* Create a client RPC handle for the NFSv3 ACL management interface */
634 nfs_init_server_aclclient(server);
635 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
639 server->nfs_client = NULL;
641 dprintk("<-- nfs_init_server() = xerror %d\n", error);
646 * Load up the server record from information gained in an fsinfo record
648 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
650 unsigned long max_rpc_payload;
652 /* Work out a lot of parameters */
653 if (server->rsize == 0)
654 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
655 if (server->wsize == 0)
656 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
658 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
659 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
660 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
661 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
663 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
664 if (server->rsize > max_rpc_payload)
665 server->rsize = max_rpc_payload;
666 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
667 server->rsize = NFS_MAX_FILE_IO_SIZE;
668 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
670 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
672 if (server->wsize > max_rpc_payload)
673 server->wsize = max_rpc_payload;
674 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
675 server->wsize = NFS_MAX_FILE_IO_SIZE;
676 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
677 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
679 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
680 if (server->dtsize > PAGE_CACHE_SIZE)
681 server->dtsize = PAGE_CACHE_SIZE;
682 if (server->dtsize > server->rsize)
683 server->dtsize = server->rsize;
685 if (server->flags & NFS_MOUNT_NOAC) {
686 server->acregmin = server->acregmax = 0;
687 server->acdirmin = server->acdirmax = 0;
690 server->maxfilesize = fsinfo->maxfilesize;
692 /* We're airborne Set socket buffersize */
693 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
697 * Probe filesystem information, including the FSID on v2/v3
699 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
701 struct nfs_fsinfo fsinfo;
702 struct nfs_client *clp = server->nfs_client;
705 dprintk("--> nfs_probe_fsinfo()\n");
707 if (clp->rpc_ops->set_capabilities != NULL) {
708 error = clp->rpc_ops->set_capabilities(server, mntfh);
713 fsinfo.fattr = fattr;
714 nfs_fattr_init(fattr);
715 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
719 nfs_server_set_fsinfo(server, &fsinfo);
720 error = bdi_init(&server->backing_dev_info);
725 /* Get some general file system info */
726 if (server->namelen == 0) {
727 struct nfs_pathconf pathinfo;
729 pathinfo.fattr = fattr;
730 nfs_fattr_init(fattr);
732 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
733 server->namelen = pathinfo.max_namelen;
736 dprintk("<-- nfs_probe_fsinfo() = 0\n");
740 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
745 * Copy useful information when duplicating a server record
747 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
749 target->flags = source->flags;
750 target->acregmin = source->acregmin;
751 target->acregmax = source->acregmax;
752 target->acdirmin = source->acdirmin;
753 target->acdirmax = source->acdirmax;
754 target->caps = source->caps;
758 * Allocate and initialise a server record
760 static struct nfs_server *nfs_alloc_server(void)
762 struct nfs_server *server;
764 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
768 server->client = server->client_acl = ERR_PTR(-EINVAL);
770 /* Zero out the NFS state stuff */
771 INIT_LIST_HEAD(&server->client_link);
772 INIT_LIST_HEAD(&server->master_link);
774 init_waitqueue_head(&server->active_wq);
775 atomic_set(&server->active, 0);
777 server->io_stats = nfs_alloc_iostats();
778 if (!server->io_stats) {
787 * Free up a server record
789 void nfs_free_server(struct nfs_server *server)
791 dprintk("--> nfs_free_server()\n");
793 spin_lock(&nfs_client_lock);
794 list_del(&server->client_link);
795 list_del(&server->master_link);
796 spin_unlock(&nfs_client_lock);
798 if (server->destroy != NULL)
799 server->destroy(server);
801 if (!IS_ERR(server->client_acl))
802 rpc_shutdown_client(server->client_acl);
803 if (!IS_ERR(server->client))
804 rpc_shutdown_client(server->client);
806 nfs_put_client(server->nfs_client);
808 nfs_free_iostats(server->io_stats);
809 bdi_destroy(&server->backing_dev_info);
811 nfs_release_automount_timer();
812 dprintk("<-- nfs_free_server()\n");
816 * Create a version 2 or 3 volume record
817 * - keyed on server and FSID
819 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
820 struct nfs_fh *mntfh)
822 struct nfs_server *server;
823 struct nfs_fattr fattr;
826 server = nfs_alloc_server();
828 return ERR_PTR(-ENOMEM);
830 /* Get a client representation */
831 error = nfs_init_server(server, data);
835 BUG_ON(!server->nfs_client);
836 BUG_ON(!server->nfs_client->rpc_ops);
837 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
839 /* Probe the root fh to retrieve its FSID */
840 error = nfs_probe_fsinfo(server, mntfh, &fattr);
843 if (server->nfs_client->rpc_ops->version == 3) {
844 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
845 server->namelen = NFS3_MAXNAMLEN;
846 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
847 server->caps |= NFS_CAP_READDIRPLUS;
849 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
850 server->namelen = NFS2_MAXNAMLEN;
853 if (!(fattr.valid & NFS_ATTR_FATTR)) {
854 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
856 dprintk("nfs_create_server: getattr error = %d\n", -error);
860 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
862 dprintk("Server FSID: %llx:%llx\n",
863 (unsigned long long) server->fsid.major,
864 (unsigned long long) server->fsid.minor);
866 BUG_ON(!server->nfs_client);
867 BUG_ON(!server->nfs_client->rpc_ops);
868 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
870 spin_lock(&nfs_client_lock);
871 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
872 list_add_tail(&server->master_link, &nfs_volume_list);
873 spin_unlock(&nfs_client_lock);
875 server->mount_time = jiffies;
879 nfs_free_server(server);
880 return ERR_PTR(error);
885 * Initialise an NFS4 client record
887 static int nfs4_init_client(struct nfs_client *clp,
888 int proto, int timeo, int retrans,
890 rpc_authflavor_t authflavour)
894 if (clp->cl_cons_state == NFS_CS_READY) {
895 /* the client is initialised already */
896 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
900 /* Check NFS protocol revision and initialize RPC op vector */
901 clp->rpc_ops = &nfs_v4_clientops;
903 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
904 RPC_CLNT_CREATE_DISCRTRY);
907 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
909 error = nfs_idmap_new(clp);
911 dprintk("%s: failed to create idmapper. Error = %d\n",
912 __FUNCTION__, error);
915 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
917 nfs_mark_client_ready(clp, NFS_CS_READY);
921 nfs_mark_client_ready(clp, error);
922 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
927 * Set up an NFS4 client
929 static int nfs4_set_client(struct nfs_server *server,
930 const char *hostname,
931 const struct sockaddr *addr,
932 const size_t addrlen,
934 rpc_authflavor_t authflavour,
935 int proto, int timeo, int retrans)
937 struct nfs_client_initdata cl_init = {
938 .hostname = hostname,
941 .rpc_ops = &nfs_v4_clientops,
943 struct nfs_client *clp;
946 dprintk("--> nfs4_set_client()\n");
948 /* Allocate or find a client reference we can use */
949 clp = nfs_get_client(&cl_init);
951 error = PTR_ERR(clp);
954 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
958 server->nfs_client = clp;
959 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
965 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
970 * Create a version 4 volume record
972 static int nfs4_init_server(struct nfs_server *server,
973 const struct nfs_parsed_mount_data *data)
977 dprintk("--> nfs4_init_server()\n");
979 /* Initialise the client representation from the mount data */
980 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
981 server->caps |= NFS_CAP_ATOMIC_OPEN;
984 server->rsize = nfs_block_size(data->rsize, NULL);
986 server->wsize = nfs_block_size(data->wsize, NULL);
988 server->acregmin = data->acregmin * HZ;
989 server->acregmax = data->acregmax * HZ;
990 server->acdirmin = data->acdirmin * HZ;
991 server->acdirmax = data->acdirmax * HZ;
993 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
996 dprintk("<-- nfs4_init_server() = %d\n", error);
1001 * Create a version 4 volume record
1002 * - keyed on server and FSID
1004 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1005 struct nfs_fh *mntfh)
1007 struct nfs_fattr fattr;
1008 struct nfs_server *server;
1011 dprintk("--> nfs4_create_server()\n");
1013 server = nfs_alloc_server();
1015 return ERR_PTR(-ENOMEM);
1017 /* Get a client record */
1018 error = nfs4_set_client(server,
1019 data->nfs_server.hostname,
1020 (struct sockaddr *)&data->nfs_server.address,
1021 data->nfs_server.addrlen,
1022 data->client_address,
1023 data->auth_flavors[0],
1024 data->nfs_server.protocol,
1025 data->timeo, data->retrans);
1029 /* set up the general RPC client */
1030 error = nfs4_init_server(server, data);
1034 BUG_ON(!server->nfs_client);
1035 BUG_ON(!server->nfs_client->rpc_ops);
1036 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1038 /* Probe the root fh to retrieve its FSID */
1039 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1043 dprintk("Server FSID: %llx:%llx\n",
1044 (unsigned long long) server->fsid.major,
1045 (unsigned long long) server->fsid.minor);
1046 dprintk("Mount FH: %d\n", mntfh->size);
1048 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1052 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1053 server->namelen = NFS4_MAXNAMLEN;
1055 BUG_ON(!server->nfs_client);
1056 BUG_ON(!server->nfs_client->rpc_ops);
1057 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1059 spin_lock(&nfs_client_lock);
1060 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1061 list_add_tail(&server->master_link, &nfs_volume_list);
1062 spin_unlock(&nfs_client_lock);
1064 server->mount_time = jiffies;
1065 dprintk("<-- nfs4_create_server() = %p\n", server);
1069 nfs_free_server(server);
1070 dprintk("<-- nfs4_create_server() = error %d\n", error);
1071 return ERR_PTR(error);
1075 * Create an NFS4 referral server record
1077 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1078 struct nfs_fh *mntfh)
1080 struct nfs_client *parent_client;
1081 struct nfs_server *server, *parent_server;
1082 struct nfs_fattr fattr;
1085 dprintk("--> nfs4_create_referral_server()\n");
1087 server = nfs_alloc_server();
1089 return ERR_PTR(-ENOMEM);
1091 parent_server = NFS_SB(data->sb);
1092 parent_client = parent_server->nfs_client;
1094 /* Get a client representation.
1095 * Note: NFSv4 always uses TCP, */
1096 error = nfs4_set_client(server, data->hostname,
1099 parent_client->cl_ipaddr,
1101 parent_server->client->cl_xprt->prot,
1102 parent_client->retrans_timeo,
1103 parent_client->retrans_count);
1107 /* Initialise the client representation from the parent server */
1108 nfs_server_copy_userdata(server, parent_server);
1109 server->caps |= NFS_CAP_ATOMIC_OPEN;
1111 error = nfs_init_server_rpcclient(server, data->authflavor);
1115 BUG_ON(!server->nfs_client);
1116 BUG_ON(!server->nfs_client->rpc_ops);
1117 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1119 /* Probe the root fh to retrieve its FSID and filehandle */
1120 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1124 /* probe the filesystem info for this server filesystem */
1125 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1129 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1130 server->namelen = NFS4_MAXNAMLEN;
1132 dprintk("Referral FSID: %llx:%llx\n",
1133 (unsigned long long) server->fsid.major,
1134 (unsigned long long) server->fsid.minor);
1136 spin_lock(&nfs_client_lock);
1137 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1138 list_add_tail(&server->master_link, &nfs_volume_list);
1139 spin_unlock(&nfs_client_lock);
1141 server->mount_time = jiffies;
1143 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1147 nfs_free_server(server);
1148 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1149 return ERR_PTR(error);
1152 #endif /* CONFIG_NFS_V4 */
1155 * Clone an NFS2, NFS3 or NFS4 server record
1157 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1159 struct nfs_fattr *fattr)
1161 struct nfs_server *server;
1162 struct nfs_fattr fattr_fsinfo;
1165 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1166 (unsigned long long) fattr->fsid.major,
1167 (unsigned long long) fattr->fsid.minor);
1169 server = nfs_alloc_server();
1171 return ERR_PTR(-ENOMEM);
1173 /* Copy data from the source */
1174 server->nfs_client = source->nfs_client;
1175 atomic_inc(&server->nfs_client->cl_count);
1176 nfs_server_copy_userdata(server, source);
1178 server->fsid = fattr->fsid;
1180 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1182 goto out_free_server;
1183 if (!IS_ERR(source->client_acl))
1184 nfs_init_server_aclclient(server);
1186 /* probe the filesystem info for this server filesystem */
1187 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1189 goto out_free_server;
1191 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1192 server->namelen = NFS4_MAXNAMLEN;
1194 dprintk("Cloned FSID: %llx:%llx\n",
1195 (unsigned long long) server->fsid.major,
1196 (unsigned long long) server->fsid.minor);
1198 error = nfs_start_lockd(server);
1200 goto out_free_server;
1202 spin_lock(&nfs_client_lock);
1203 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1204 list_add_tail(&server->master_link, &nfs_volume_list);
1205 spin_unlock(&nfs_client_lock);
1207 server->mount_time = jiffies;
1209 dprintk("<-- nfs_clone_server() = %p\n", server);
1213 nfs_free_server(server);
1214 dprintk("<-- nfs_clone_server() = error %d\n", error);
1215 return ERR_PTR(error);
1218 #ifdef CONFIG_PROC_FS
1219 static struct proc_dir_entry *proc_fs_nfs;
1221 static int nfs_server_list_open(struct inode *inode, struct file *file);
1222 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1223 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1224 static void nfs_server_list_stop(struct seq_file *p, void *v);
1225 static int nfs_server_list_show(struct seq_file *m, void *v);
1227 static struct seq_operations nfs_server_list_ops = {
1228 .start = nfs_server_list_start,
1229 .next = nfs_server_list_next,
1230 .stop = nfs_server_list_stop,
1231 .show = nfs_server_list_show,
1234 static const struct file_operations nfs_server_list_fops = {
1235 .open = nfs_server_list_open,
1237 .llseek = seq_lseek,
1238 .release = seq_release,
1241 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1242 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1243 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1244 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1245 static int nfs_volume_list_show(struct seq_file *m, void *v);
1247 static struct seq_operations nfs_volume_list_ops = {
1248 .start = nfs_volume_list_start,
1249 .next = nfs_volume_list_next,
1250 .stop = nfs_volume_list_stop,
1251 .show = nfs_volume_list_show,
1254 static const struct file_operations nfs_volume_list_fops = {
1255 .open = nfs_volume_list_open,
1257 .llseek = seq_lseek,
1258 .release = seq_release,
1262 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1265 static int nfs_server_list_open(struct inode *inode, struct file *file)
1270 ret = seq_open(file, &nfs_server_list_ops);
1274 m = file->private_data;
1275 m->private = PDE(inode)->data;
1281 * set up the iterator to start reading from the server list and return the first item
1283 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1285 /* lock the list against modification */
1286 spin_lock(&nfs_client_lock);
1287 return seq_list_start_head(&nfs_client_list, *_pos);
1291 * move to next server
1293 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1295 return seq_list_next(v, &nfs_client_list, pos);
1299 * clean up after reading from the transports list
1301 static void nfs_server_list_stop(struct seq_file *p, void *v)
1303 spin_unlock(&nfs_client_lock);
1307 * display a header line followed by a load of call lines
1309 static int nfs_server_list_show(struct seq_file *m, void *v)
1311 struct nfs_client *clp;
1313 /* display header on line 1 */
1314 if (v == &nfs_client_list) {
1315 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1319 /* display one transport per line on subsequent lines */
1320 clp = list_entry(v, struct nfs_client, cl_share_link);
1322 seq_printf(m, "v%u %s %s %3d %s\n",
1323 clp->rpc_ops->version,
1324 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1325 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1326 atomic_read(&clp->cl_count),
1333 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1335 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1340 ret = seq_open(file, &nfs_volume_list_ops);
1344 m = file->private_data;
1345 m->private = PDE(inode)->data;
1351 * set up the iterator to start reading from the volume list and return the first item
1353 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1355 /* lock the list against modification */
1356 spin_lock(&nfs_client_lock);
1357 return seq_list_start_head(&nfs_volume_list, *_pos);
1361 * move to next volume
1363 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1365 return seq_list_next(v, &nfs_volume_list, pos);
1369 * clean up after reading from the transports list
1371 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1373 spin_unlock(&nfs_client_lock);
1377 * display a header line followed by a load of call lines
1379 static int nfs_volume_list_show(struct seq_file *m, void *v)
1381 struct nfs_server *server;
1382 struct nfs_client *clp;
1383 char dev[8], fsid[17];
1385 /* display header on line 1 */
1386 if (v == &nfs_volume_list) {
1387 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1390 /* display one transport per line on subsequent lines */
1391 server = list_entry(v, struct nfs_server, master_link);
1392 clp = server->nfs_client;
1394 snprintf(dev, 8, "%u:%u",
1395 MAJOR(server->s_dev), MINOR(server->s_dev));
1397 snprintf(fsid, 17, "%llx:%llx",
1398 (unsigned long long) server->fsid.major,
1399 (unsigned long long) server->fsid.minor);
1401 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1402 clp->rpc_ops->version,
1403 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1404 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1412 * initialise the /proc/fs/nfsfs/ directory
1414 int __init nfs_fs_proc_init(void)
1416 struct proc_dir_entry *p;
1418 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1422 proc_fs_nfs->owner = THIS_MODULE;
1424 /* a file of servers with which we're dealing */
1425 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1429 p->proc_fops = &nfs_server_list_fops;
1430 p->owner = THIS_MODULE;
1432 /* a file of volumes that we have mounted */
1433 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1437 p->proc_fops = &nfs_volume_list_fops;
1438 p->owner = THIS_MODULE;
1442 remove_proc_entry("servers", proc_fs_nfs);
1444 remove_proc_entry("nfsfs", proc_root_fs);
1450 * clean up the /proc/fs/nfsfs/ directory
1452 void nfs_fs_proc_exit(void)
1454 remove_proc_entry("volumes", proc_fs_nfs);
1455 remove_proc_entry("servers", proc_fs_nfs);
1456 remove_proc_entry("nfsfs", proc_root_fs);
1459 #endif /* CONFIG_PROC_FS */