1 /* client.c: NFS client sharing and management code
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>
17 #include <linux/kernel.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/stats.h>
25 #include <linux/sunrpc/metrics.h>
26 #include <linux/sunrpc/xprtsock.h>
27 #include <linux/sunrpc/xprtrdma.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/nfs4_mount.h>
31 #include <linux/lockd/bind.h>
32 #include <linux/seq_file.h>
33 #include <linux/mount.h>
34 #include <linux/nfs_idmap.h>
35 #include <linux/vfs.h>
36 #include <linux/inet.h>
37 #include <linux/in6.h>
39 #include <linux/nfs_xdr.h>
41 #include <asm/system.h>
45 #include "delegation.h"
49 #define NFSDBG_FACILITY NFSDBG_CLIENT
51 static DEFINE_SPINLOCK(nfs_client_lock);
52 static LIST_HEAD(nfs_client_list);
53 static LIST_HEAD(nfs_volume_list);
54 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59 static struct rpc_version *nfs_version[5] = {
69 struct rpc_program nfs_program = {
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
78 struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
83 #ifdef CONFIG_NFS_V3_ACL
84 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85 static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
89 struct rpc_program nfsacl_program = {
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
96 #endif /* CONFIG_NFS_V3_ACL */
98 struct nfs_client_initdata {
100 const struct sockaddr *addr;
102 const struct nfs_rpc_ops *rpc_ops;
107 * Allocate a shared client record
109 * Since these are allocated/deallocated very rarely, we don't
110 * bother putting them in a slab cache...
112 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
114 struct nfs_client *clp;
116 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
119 clp->rpc_ops = cl_init->rpc_ops;
121 if (cl_init->rpc_ops->version == 4) {
122 if (nfs_callback_up() < 0)
124 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
127 atomic_set(&clp->cl_count, 1);
128 clp->cl_cons_state = NFS_CS_INITING;
130 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
131 clp->cl_addrlen = cl_init->addrlen;
133 if (cl_init->hostname) {
134 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
135 if (!clp->cl_hostname)
139 INIT_LIST_HEAD(&clp->cl_superblocks);
140 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142 clp->cl_proto = cl_init->proto;
145 init_rwsem(&clp->cl_sem);
146 INIT_LIST_HEAD(&clp->cl_delegations);
147 spin_lock_init(&clp->cl_lock);
148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
157 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
165 static void nfs4_shutdown_client(struct nfs_client *clp)
168 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
169 nfs4_kill_renewd(clp);
170 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
171 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
172 nfs_idmap_delete(clp);
177 * Destroy a shared client record
179 static void nfs_free_client(struct nfs_client *clp)
181 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
183 nfs4_shutdown_client(clp);
185 /* -EIO all pending I/O */
186 if (!IS_ERR(clp->cl_rpcclient))
187 rpc_shutdown_client(clp->cl_rpcclient);
189 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
192 kfree(clp->cl_hostname);
195 dprintk("<-- nfs_free_client()\n");
199 * Release a reference to a shared client record
201 void nfs_put_client(struct nfs_client *clp)
206 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
208 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
209 list_del(&clp->cl_share_link);
210 spin_unlock(&nfs_client_lock);
212 BUG_ON(!list_empty(&clp->cl_superblocks));
214 nfs_free_client(clp);
218 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
219 const struct sockaddr_in *sa2)
221 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
224 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
225 const struct sockaddr_in6 *sa2)
227 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
230 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
231 const struct sockaddr *sa2)
233 switch (sa1->sa_family) {
235 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
236 (const struct sockaddr_in *)sa2);
238 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
239 (const struct sockaddr_in6 *)sa2);
245 * Find a client by IP address and protocol version
246 * - returns NULL if no such client
248 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
250 struct nfs_client *clp;
252 spin_lock(&nfs_client_lock);
253 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
254 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
256 /* Don't match clients that failed to initialise properly */
257 if (clp->cl_cons_state != NFS_CS_READY)
260 /* Different NFS versions cannot share the same nfs_client */
261 if (clp->rpc_ops->version != nfsversion)
264 if (addr->sa_family != clap->sa_family)
266 /* Match only the IP address, not the port number */
267 if (!nfs_sockaddr_match_ipaddr(addr, clap))
270 atomic_inc(&clp->cl_count);
271 spin_unlock(&nfs_client_lock);
274 spin_unlock(&nfs_client_lock);
279 * Find an nfs_client on the list that matches the initialisation data
282 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
284 struct nfs_client *clp;
286 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
287 /* Don't match clients that failed to initialise properly */
288 if (clp->cl_cons_state < 0)
291 /* Different NFS versions cannot share the same nfs_client */
292 if (clp->rpc_ops != data->rpc_ops)
295 if (clp->cl_proto != data->proto)
298 /* Match the full socket address */
299 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
302 atomic_inc(&clp->cl_count);
309 * Look up a client by IP address and protocol version
310 * - creates a new record if one doesn't yet exist
312 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
314 struct nfs_client *clp, *new = NULL;
317 dprintk("--> nfs_get_client(%s,v%u)\n",
318 cl_init->hostname ?: "", cl_init->rpc_ops->version);
320 /* see if the client already exists */
322 spin_lock(&nfs_client_lock);
324 clp = nfs_match_client(cl_init);
330 spin_unlock(&nfs_client_lock);
332 new = nfs_alloc_client(cl_init);
335 return ERR_PTR(-ENOMEM);
337 /* install a new client and return with it unready */
340 list_add(&clp->cl_share_link, &nfs_client_list);
341 spin_unlock(&nfs_client_lock);
342 dprintk("--> nfs_get_client() = %p [new]\n", clp);
345 /* found an existing client
346 * - make sure it's ready before returning
349 spin_unlock(&nfs_client_lock);
352 nfs_free_client(new);
354 error = wait_event_interruptible(nfs_client_active_wq,
355 clp->cl_cons_state != NFS_CS_INITING);
358 return ERR_PTR(-ERESTARTSYS);
361 if (clp->cl_cons_state < NFS_CS_READY) {
362 error = clp->cl_cons_state;
364 return ERR_PTR(error);
367 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
369 dprintk("--> nfs_get_client() = %p [share]\n", clp);
374 * Mark a server as ready or failed
376 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
378 clp->cl_cons_state = state;
379 wake_up_all(&nfs_client_active_wq);
383 * Initialise the timeout values for a connection
385 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
386 unsigned int timeo, unsigned int retrans)
388 to->to_initval = timeo * HZ / 10;
389 to->to_retries = retrans;
394 case XPRT_TRANSPORT_TCP:
395 case XPRT_TRANSPORT_RDMA:
396 if (to->to_initval == 0)
397 to->to_initval = 60 * HZ;
398 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
399 to->to_initval = NFS_MAX_TCP_TIMEOUT;
400 to->to_increment = to->to_initval;
401 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
402 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
403 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
404 if (to->to_maxval < to->to_initval)
405 to->to_maxval = to->to_initval;
406 to->to_exponential = 0;
408 case XPRT_TRANSPORT_UDP:
411 to->to_initval = 11 * HZ / 10;
412 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
413 to->to_initval = NFS_MAX_UDP_TIMEOUT;
414 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
415 to->to_exponential = 1;
421 * Create an RPC client handle
423 static int nfs_create_rpc_client(struct nfs_client *clp,
424 const struct rpc_timeout *timeparms,
425 rpc_authflavor_t flavor,
428 struct rpc_clnt *clnt = NULL;
429 struct rpc_create_args args = {
430 .protocol = clp->cl_proto,
431 .address = (struct sockaddr *)&clp->cl_addr,
432 .addrsize = clp->cl_addrlen,
433 .timeout = timeparms,
434 .servername = clp->cl_hostname,
435 .program = &nfs_program,
436 .version = clp->rpc_ops->version,
437 .authflavor = flavor,
441 if (!IS_ERR(clp->cl_rpcclient))
444 clnt = rpc_create(&args);
446 dprintk("%s: cannot create RPC client. Error = %ld\n",
447 __FUNCTION__, PTR_ERR(clnt));
448 return PTR_ERR(clnt);
451 clp->cl_rpcclient = clnt;
456 * Version 2 or 3 client destruction
458 static void nfs_destroy_server(struct nfs_server *server)
460 if (!(server->flags & NFS_MOUNT_NONLM))
461 lockd_down(); /* release rpc.lockd */
465 * Version 2 or 3 lockd setup
467 static int nfs_start_lockd(struct nfs_server *server)
471 if (server->nfs_client->rpc_ops->version > 3)
473 if (server->flags & NFS_MOUNT_NONLM)
475 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
476 IPPROTO_TCP : IPPROTO_UDP);
478 server->flags |= NFS_MOUNT_NONLM;
480 server->destroy = nfs_destroy_server;
486 * Initialise an NFSv3 ACL client connection
488 #ifdef CONFIG_NFS_V3_ACL
489 static void nfs_init_server_aclclient(struct nfs_server *server)
491 if (server->nfs_client->rpc_ops->version != 3)
493 if (server->flags & NFS_MOUNT_NOACL)
496 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
497 if (IS_ERR(server->client_acl))
500 /* No errors! Assume that Sun nfsacls are supported */
501 server->caps |= NFS_CAP_ACLS;
505 server->caps &= ~NFS_CAP_ACLS;
508 static inline void nfs_init_server_aclclient(struct nfs_server *server)
510 server->flags &= ~NFS_MOUNT_NOACL;
511 server->caps &= ~NFS_CAP_ACLS;
516 * Create a general RPC client
518 static int nfs_init_server_rpcclient(struct nfs_server *server,
519 const struct rpc_timeout *timeo,
520 rpc_authflavor_t pseudoflavour)
522 struct nfs_client *clp = server->nfs_client;
524 server->client = rpc_clone_client(clp->cl_rpcclient);
525 if (IS_ERR(server->client)) {
526 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
527 return PTR_ERR(server->client);
530 memcpy(&server->client->cl_timeout_default,
532 sizeof(server->client->cl_timeout_default));
533 server->client->cl_timeout = &server->client->cl_timeout_default;
535 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
536 struct rpc_auth *auth;
538 auth = rpcauth_create(pseudoflavour, server->client);
540 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
541 return PTR_ERR(auth);
544 server->client->cl_softrtry = 0;
545 if (server->flags & NFS_MOUNT_SOFT)
546 server->client->cl_softrtry = 1;
548 server->client->cl_intr = 0;
549 if (server->flags & NFS4_MOUNT_INTR)
550 server->client->cl_intr = 1;
556 * Initialise an NFS2 or NFS3 client
558 static int nfs_init_client(struct nfs_client *clp,
559 const struct rpc_timeout *timeparms,
560 const struct nfs_parsed_mount_data *data)
564 if (clp->cl_cons_state == NFS_CS_READY) {
565 /* the client is already initialised */
566 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
571 * Create a client RPC handle for doing FSSTAT with UNIX auth only
572 * - RFC 2623, sec 2.3.2
574 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
577 nfs_mark_client_ready(clp, NFS_CS_READY);
581 nfs_mark_client_ready(clp, error);
582 dprintk("<-- nfs_init_client() = xerror %d\n", error);
587 * Create a version 2 or 3 client
589 static int nfs_init_server(struct nfs_server *server,
590 const struct nfs_parsed_mount_data *data)
592 struct nfs_client_initdata cl_init = {
593 .hostname = data->nfs_server.hostname,
594 .addr = (const struct sockaddr *)&data->nfs_server.address,
595 .addrlen = data->nfs_server.addrlen,
596 .rpc_ops = &nfs_v2_clientops,
597 .proto = data->nfs_server.protocol,
599 struct rpc_timeout timeparms;
600 struct nfs_client *clp;
603 dprintk("--> nfs_init_server()\n");
606 if (data->flags & NFS_MOUNT_VER3)
607 cl_init.rpc_ops = &nfs_v3_clientops;
610 /* Allocate or find a client reference we can use */
611 clp = nfs_get_client(&cl_init);
613 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
617 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
618 data->timeo, data->retrans);
619 error = nfs_init_client(clp, &timeparms, data);
623 server->nfs_client = clp;
625 /* Initialise the client representation from the mount data */
626 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
629 server->rsize = nfs_block_size(data->rsize, NULL);
631 server->wsize = nfs_block_size(data->wsize, NULL);
633 server->acregmin = data->acregmin * HZ;
634 server->acregmax = data->acregmax * HZ;
635 server->acdirmin = data->acdirmin * HZ;
636 server->acdirmax = data->acdirmax * HZ;
638 /* Start lockd here, before we might error out */
639 error = nfs_start_lockd(server);
643 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
647 server->namelen = data->namlen;
648 /* Create a client RPC handle for the NFSv3 ACL management interface */
649 nfs_init_server_aclclient(server);
650 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
654 server->nfs_client = NULL;
656 dprintk("<-- nfs_init_server() = xerror %d\n", error);
661 * Load up the server record from information gained in an fsinfo record
663 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
665 unsigned long max_rpc_payload;
667 /* Work out a lot of parameters */
668 if (server->rsize == 0)
669 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
670 if (server->wsize == 0)
671 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
673 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
674 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
675 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
676 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
678 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
679 if (server->rsize > max_rpc_payload)
680 server->rsize = max_rpc_payload;
681 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
682 server->rsize = NFS_MAX_FILE_IO_SIZE;
683 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
685 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
687 if (server->wsize > max_rpc_payload)
688 server->wsize = max_rpc_payload;
689 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
690 server->wsize = NFS_MAX_FILE_IO_SIZE;
691 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
692 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
694 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
695 if (server->dtsize > PAGE_CACHE_SIZE)
696 server->dtsize = PAGE_CACHE_SIZE;
697 if (server->dtsize > server->rsize)
698 server->dtsize = server->rsize;
700 if (server->flags & NFS_MOUNT_NOAC) {
701 server->acregmin = server->acregmax = 0;
702 server->acdirmin = server->acdirmax = 0;
705 server->maxfilesize = fsinfo->maxfilesize;
707 /* We're airborne Set socket buffersize */
708 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
712 * Probe filesystem information, including the FSID on v2/v3
714 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
716 struct nfs_fsinfo fsinfo;
717 struct nfs_client *clp = server->nfs_client;
720 dprintk("--> nfs_probe_fsinfo()\n");
722 if (clp->rpc_ops->set_capabilities != NULL) {
723 error = clp->rpc_ops->set_capabilities(server, mntfh);
728 fsinfo.fattr = fattr;
729 nfs_fattr_init(fattr);
730 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
734 nfs_server_set_fsinfo(server, &fsinfo);
735 error = bdi_init(&server->backing_dev_info);
740 /* Get some general file system info */
741 if (server->namelen == 0) {
742 struct nfs_pathconf pathinfo;
744 pathinfo.fattr = fattr;
745 nfs_fattr_init(fattr);
747 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
748 server->namelen = pathinfo.max_namelen;
751 dprintk("<-- nfs_probe_fsinfo() = 0\n");
755 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
760 * Copy useful information when duplicating a server record
762 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
764 target->flags = source->flags;
765 target->acregmin = source->acregmin;
766 target->acregmax = source->acregmax;
767 target->acdirmin = source->acdirmin;
768 target->acdirmax = source->acdirmax;
769 target->caps = source->caps;
773 * Allocate and initialise a server record
775 static struct nfs_server *nfs_alloc_server(void)
777 struct nfs_server *server;
779 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
783 server->client = server->client_acl = ERR_PTR(-EINVAL);
785 /* Zero out the NFS state stuff */
786 INIT_LIST_HEAD(&server->client_link);
787 INIT_LIST_HEAD(&server->master_link);
789 init_waitqueue_head(&server->active_wq);
790 atomic_set(&server->active, 0);
792 server->io_stats = nfs_alloc_iostats();
793 if (!server->io_stats) {
802 * Free up a server record
804 void nfs_free_server(struct nfs_server *server)
806 dprintk("--> nfs_free_server()\n");
808 spin_lock(&nfs_client_lock);
809 list_del(&server->client_link);
810 list_del(&server->master_link);
811 spin_unlock(&nfs_client_lock);
813 if (server->destroy != NULL)
814 server->destroy(server);
816 if (!IS_ERR(server->client_acl))
817 rpc_shutdown_client(server->client_acl);
818 if (!IS_ERR(server->client))
819 rpc_shutdown_client(server->client);
821 nfs_put_client(server->nfs_client);
823 nfs_free_iostats(server->io_stats);
824 bdi_destroy(&server->backing_dev_info);
826 nfs_release_automount_timer();
827 dprintk("<-- nfs_free_server()\n");
831 * Create a version 2 or 3 volume record
832 * - keyed on server and FSID
834 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
835 struct nfs_fh *mntfh)
837 struct nfs_server *server;
838 struct nfs_fattr fattr;
841 server = nfs_alloc_server();
843 return ERR_PTR(-ENOMEM);
845 /* Get a client representation */
846 error = nfs_init_server(server, data);
850 BUG_ON(!server->nfs_client);
851 BUG_ON(!server->nfs_client->rpc_ops);
852 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
854 /* Probe the root fh to retrieve its FSID */
855 error = nfs_probe_fsinfo(server, mntfh, &fattr);
858 if (server->nfs_client->rpc_ops->version == 3) {
859 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
860 server->namelen = NFS3_MAXNAMLEN;
861 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
862 server->caps |= NFS_CAP_READDIRPLUS;
864 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
865 server->namelen = NFS2_MAXNAMLEN;
868 if (!(fattr.valid & NFS_ATTR_FATTR)) {
869 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
871 dprintk("nfs_create_server: getattr error = %d\n", -error);
875 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
877 dprintk("Server FSID: %llx:%llx\n",
878 (unsigned long long) server->fsid.major,
879 (unsigned long long) server->fsid.minor);
881 BUG_ON(!server->nfs_client);
882 BUG_ON(!server->nfs_client->rpc_ops);
883 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
885 spin_lock(&nfs_client_lock);
886 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
887 list_add_tail(&server->master_link, &nfs_volume_list);
888 spin_unlock(&nfs_client_lock);
890 server->mount_time = jiffies;
894 nfs_free_server(server);
895 return ERR_PTR(error);
900 * Initialise an NFS4 client record
902 static int nfs4_init_client(struct nfs_client *clp,
903 const struct rpc_timeout *timeparms,
905 rpc_authflavor_t authflavour)
909 if (clp->cl_cons_state == NFS_CS_READY) {
910 /* the client is initialised already */
911 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
915 /* Check NFS protocol revision and initialize RPC op vector */
916 clp->rpc_ops = &nfs_v4_clientops;
918 error = nfs_create_rpc_client(clp, timeparms, authflavour,
919 RPC_CLNT_CREATE_DISCRTRY);
922 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
924 error = nfs_idmap_new(clp);
926 dprintk("%s: failed to create idmapper. Error = %d\n",
927 __FUNCTION__, error);
930 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
932 nfs_mark_client_ready(clp, NFS_CS_READY);
936 nfs_mark_client_ready(clp, error);
937 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
942 * Set up an NFS4 client
944 static int nfs4_set_client(struct nfs_server *server,
945 const char *hostname,
946 const struct sockaddr *addr,
947 const size_t addrlen,
949 rpc_authflavor_t authflavour,
950 int proto, const struct rpc_timeout *timeparms)
952 struct nfs_client_initdata cl_init = {
953 .hostname = hostname,
956 .rpc_ops = &nfs_v4_clientops,
959 struct nfs_client *clp;
962 dprintk("--> nfs4_set_client()\n");
964 /* Allocate or find a client reference we can use */
965 clp = nfs_get_client(&cl_init);
967 error = PTR_ERR(clp);
970 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
974 server->nfs_client = clp;
975 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
981 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
986 * Create a version 4 volume record
988 static int nfs4_init_server(struct nfs_server *server,
989 const struct nfs_parsed_mount_data *data)
991 struct rpc_timeout timeparms;
994 dprintk("--> nfs4_init_server()\n");
996 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
997 data->timeo, data->retrans);
999 /* Get a client record */
1000 error = nfs4_set_client(server,
1001 data->nfs_server.hostname,
1002 (const struct sockaddr *)&data->nfs_server.address,
1003 data->nfs_server.addrlen,
1004 data->client_address,
1005 data->auth_flavors[0],
1006 data->nfs_server.protocol,
1011 /* Initialise the client representation from the mount data */
1012 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1013 server->caps |= NFS_CAP_ATOMIC_OPEN;
1016 server->rsize = nfs_block_size(data->rsize, NULL);
1018 server->wsize = nfs_block_size(data->wsize, NULL);
1020 server->acregmin = data->acregmin * HZ;
1021 server->acregmax = data->acregmax * HZ;
1022 server->acdirmin = data->acdirmin * HZ;
1023 server->acdirmax = data->acdirmax * HZ;
1025 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1029 dprintk("<-- nfs4_init_server() = %d\n", error);
1034 * Create a version 4 volume record
1035 * - keyed on server and FSID
1037 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1038 struct nfs_fh *mntfh)
1040 struct nfs_fattr fattr;
1041 struct nfs_server *server;
1044 dprintk("--> nfs4_create_server()\n");
1046 server = nfs_alloc_server();
1048 return ERR_PTR(-ENOMEM);
1050 /* set up the general RPC client */
1051 error = nfs4_init_server(server, data);
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 /* Probe the root fh to retrieve its FSID */
1060 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1064 dprintk("Server FSID: %llx:%llx\n",
1065 (unsigned long long) server->fsid.major,
1066 (unsigned long long) server->fsid.minor);
1067 dprintk("Mount FH: %d\n", mntfh->size);
1069 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1073 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1074 server->namelen = NFS4_MAXNAMLEN;
1076 BUG_ON(!server->nfs_client);
1077 BUG_ON(!server->nfs_client->rpc_ops);
1078 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1080 spin_lock(&nfs_client_lock);
1081 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1082 list_add_tail(&server->master_link, &nfs_volume_list);
1083 spin_unlock(&nfs_client_lock);
1085 server->mount_time = jiffies;
1086 dprintk("<-- nfs4_create_server() = %p\n", server);
1090 nfs_free_server(server);
1091 dprintk("<-- nfs4_create_server() = error %d\n", error);
1092 return ERR_PTR(error);
1096 * Create an NFS4 referral server record
1098 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1099 struct nfs_fh *mntfh)
1101 struct nfs_client *parent_client;
1102 struct nfs_server *server, *parent_server;
1103 struct nfs_fattr fattr;
1106 dprintk("--> nfs4_create_referral_server()\n");
1108 server = nfs_alloc_server();
1110 return ERR_PTR(-ENOMEM);
1112 parent_server = NFS_SB(data->sb);
1113 parent_client = parent_server->nfs_client;
1115 /* Get a client representation.
1116 * Note: NFSv4 always uses TCP, */
1117 error = nfs4_set_client(server, data->hostname,
1120 parent_client->cl_ipaddr,
1122 parent_server->client->cl_xprt->prot,
1123 parent_server->client->cl_timeout);
1127 /* Initialise the client representation from the parent server */
1128 nfs_server_copy_userdata(server, parent_server);
1129 server->caps |= NFS_CAP_ATOMIC_OPEN;
1131 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1135 BUG_ON(!server->nfs_client);
1136 BUG_ON(!server->nfs_client->rpc_ops);
1137 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1139 /* Probe the root fh to retrieve its FSID and filehandle */
1140 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1144 /* probe the filesystem info for this server filesystem */
1145 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1149 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1150 server->namelen = NFS4_MAXNAMLEN;
1152 dprintk("Referral FSID: %llx:%llx\n",
1153 (unsigned long long) server->fsid.major,
1154 (unsigned long long) server->fsid.minor);
1156 spin_lock(&nfs_client_lock);
1157 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1158 list_add_tail(&server->master_link, &nfs_volume_list);
1159 spin_unlock(&nfs_client_lock);
1161 server->mount_time = jiffies;
1163 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1167 nfs_free_server(server);
1168 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1169 return ERR_PTR(error);
1172 #endif /* CONFIG_NFS_V4 */
1175 * Clone an NFS2, NFS3 or NFS4 server record
1177 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1179 struct nfs_fattr *fattr)
1181 struct nfs_server *server;
1182 struct nfs_fattr fattr_fsinfo;
1185 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1186 (unsigned long long) fattr->fsid.major,
1187 (unsigned long long) fattr->fsid.minor);
1189 server = nfs_alloc_server();
1191 return ERR_PTR(-ENOMEM);
1193 /* Copy data from the source */
1194 server->nfs_client = source->nfs_client;
1195 atomic_inc(&server->nfs_client->cl_count);
1196 nfs_server_copy_userdata(server, source);
1198 server->fsid = fattr->fsid;
1200 error = nfs_init_server_rpcclient(server,
1201 source->client->cl_timeout,
1202 source->client->cl_auth->au_flavor);
1204 goto out_free_server;
1205 if (!IS_ERR(source->client_acl))
1206 nfs_init_server_aclclient(server);
1208 /* probe the filesystem info for this server filesystem */
1209 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1211 goto out_free_server;
1213 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1214 server->namelen = NFS4_MAXNAMLEN;
1216 dprintk("Cloned FSID: %llx:%llx\n",
1217 (unsigned long long) server->fsid.major,
1218 (unsigned long long) server->fsid.minor);
1220 error = nfs_start_lockd(server);
1222 goto out_free_server;
1224 spin_lock(&nfs_client_lock);
1225 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1226 list_add_tail(&server->master_link, &nfs_volume_list);
1227 spin_unlock(&nfs_client_lock);
1229 server->mount_time = jiffies;
1231 dprintk("<-- nfs_clone_server() = %p\n", server);
1235 nfs_free_server(server);
1236 dprintk("<-- nfs_clone_server() = error %d\n", error);
1237 return ERR_PTR(error);
1240 #ifdef CONFIG_PROC_FS
1241 static struct proc_dir_entry *proc_fs_nfs;
1243 static int nfs_server_list_open(struct inode *inode, struct file *file);
1244 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1245 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1246 static void nfs_server_list_stop(struct seq_file *p, void *v);
1247 static int nfs_server_list_show(struct seq_file *m, void *v);
1249 static struct seq_operations nfs_server_list_ops = {
1250 .start = nfs_server_list_start,
1251 .next = nfs_server_list_next,
1252 .stop = nfs_server_list_stop,
1253 .show = nfs_server_list_show,
1256 static const struct file_operations nfs_server_list_fops = {
1257 .open = nfs_server_list_open,
1259 .llseek = seq_lseek,
1260 .release = seq_release,
1263 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1264 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1265 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1266 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1267 static int nfs_volume_list_show(struct seq_file *m, void *v);
1269 static struct seq_operations nfs_volume_list_ops = {
1270 .start = nfs_volume_list_start,
1271 .next = nfs_volume_list_next,
1272 .stop = nfs_volume_list_stop,
1273 .show = nfs_volume_list_show,
1276 static const struct file_operations nfs_volume_list_fops = {
1277 .open = nfs_volume_list_open,
1279 .llseek = seq_lseek,
1280 .release = seq_release,
1284 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1287 static int nfs_server_list_open(struct inode *inode, struct file *file)
1292 ret = seq_open(file, &nfs_server_list_ops);
1296 m = file->private_data;
1297 m->private = PDE(inode)->data;
1303 * set up the iterator to start reading from the server list and return the first item
1305 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1307 /* lock the list against modification */
1308 spin_lock(&nfs_client_lock);
1309 return seq_list_start_head(&nfs_client_list, *_pos);
1313 * move to next server
1315 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1317 return seq_list_next(v, &nfs_client_list, pos);
1321 * clean up after reading from the transports list
1323 static void nfs_server_list_stop(struct seq_file *p, void *v)
1325 spin_unlock(&nfs_client_lock);
1329 * display a header line followed by a load of call lines
1331 static int nfs_server_list_show(struct seq_file *m, void *v)
1333 struct nfs_client *clp;
1335 /* display header on line 1 */
1336 if (v == &nfs_client_list) {
1337 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1341 /* display one transport per line on subsequent lines */
1342 clp = list_entry(v, struct nfs_client, cl_share_link);
1344 seq_printf(m, "v%u %s %s %3d %s\n",
1345 clp->rpc_ops->version,
1346 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1347 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1348 atomic_read(&clp->cl_count),
1355 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1357 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1362 ret = seq_open(file, &nfs_volume_list_ops);
1366 m = file->private_data;
1367 m->private = PDE(inode)->data;
1373 * set up the iterator to start reading from the volume list and return the first item
1375 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1377 /* lock the list against modification */
1378 spin_lock(&nfs_client_lock);
1379 return seq_list_start_head(&nfs_volume_list, *_pos);
1383 * move to next volume
1385 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1387 return seq_list_next(v, &nfs_volume_list, pos);
1391 * clean up after reading from the transports list
1393 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1395 spin_unlock(&nfs_client_lock);
1399 * display a header line followed by a load of call lines
1401 static int nfs_volume_list_show(struct seq_file *m, void *v)
1403 struct nfs_server *server;
1404 struct nfs_client *clp;
1405 char dev[8], fsid[17];
1407 /* display header on line 1 */
1408 if (v == &nfs_volume_list) {
1409 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1412 /* display one transport per line on subsequent lines */
1413 server = list_entry(v, struct nfs_server, master_link);
1414 clp = server->nfs_client;
1416 snprintf(dev, 8, "%u:%u",
1417 MAJOR(server->s_dev), MINOR(server->s_dev));
1419 snprintf(fsid, 17, "%llx:%llx",
1420 (unsigned long long) server->fsid.major,
1421 (unsigned long long) server->fsid.minor);
1423 seq_printf(m, "v%u %s %s %-7s %-17s\n",
1424 clp->rpc_ops->version,
1425 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1426 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1434 * initialise the /proc/fs/nfsfs/ directory
1436 int __init nfs_fs_proc_init(void)
1438 struct proc_dir_entry *p;
1440 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1444 proc_fs_nfs->owner = THIS_MODULE;
1446 /* a file of servers with which we're dealing */
1447 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1451 p->proc_fops = &nfs_server_list_fops;
1452 p->owner = THIS_MODULE;
1454 /* a file of volumes that we have mounted */
1455 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1459 p->proc_fops = &nfs_volume_list_fops;
1460 p->owner = THIS_MODULE;
1464 remove_proc_entry("servers", proc_fs_nfs);
1466 remove_proc_entry("nfsfs", proc_root_fs);
1472 * clean up the /proc/fs/nfsfs/ directory
1474 void nfs_fs_proc_exit(void)
1476 remove_proc_entry("volumes", proc_fs_nfs);
1477 remove_proc_entry("servers", proc_fs_nfs);
1478 remove_proc_entry("nfsfs", proc_root_fs);
1481 #endif /* CONFIG_PROC_FS */