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>
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/nfs_fs.h>
27 #include <linux/nfs_mount.h>
28 #include <linux/nfs4_mount.h>
29 #include <linux/lockd/bind.h>
30 #include <linux/smp_lock.h>
31 #include <linux/seq_file.h>
32 #include <linux/mount.h>
33 #include <linux/nfs_idmap.h>
34 #include <linux/vfs.h>
35 #include <linux/inet.h>
36 #include <linux/nfs_xdr.h>
38 #include <asm/system.h>
42 #include "delegation.h"
46 #define NFSDBG_FACILITY NFSDBG_CLIENT
48 static DEFINE_SPINLOCK(nfs_client_lock);
49 static LIST_HEAD(nfs_client_list);
50 static LIST_HEAD(nfs_volume_list);
51 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56 static struct rpc_version *nfs_version[5] = {
66 struct rpc_program nfs_program = {
68 .number = NFS_PROGRAM,
69 .nrvers = ARRAY_SIZE(nfs_version),
70 .version = nfs_version,
71 .stats = &nfs_rpcstat,
72 .pipe_dir_name = "/nfs",
75 struct rpc_stat nfs_rpcstat = {
76 .program = &nfs_program
80 #ifdef CONFIG_NFS_V3_ACL
81 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
82 static struct rpc_version * nfsacl_version[] = {
83 [3] = &nfsacl_version3,
86 struct rpc_program nfsacl_program = {
88 .number = NFS_ACL_PROGRAM,
89 .nrvers = ARRAY_SIZE(nfsacl_version),
90 .version = nfsacl_version,
91 .stats = &nfsacl_rpcstat,
93 #endif /* CONFIG_NFS_V3_ACL */
96 * Allocate a shared client record
98 * Since these are allocated/deallocated very rarely, we don't
99 * bother putting them in a slab cache...
101 static struct nfs_client *nfs_alloc_client(const char *hostname,
102 const struct sockaddr_in *addr,
105 struct nfs_client *clp;
108 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
113 dprintk("%s: couldn't start rpciod! Error = %d\n",
114 __FUNCTION__, error);
117 __set_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
119 if (nfsversion == 4) {
120 if (nfs_callback_up() < 0)
122 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
125 atomic_set(&clp->cl_count, 1);
126 clp->cl_cons_state = NFS_CS_INITING;
128 clp->cl_nfsversion = nfsversion;
129 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
132 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
133 if (!clp->cl_hostname)
137 INIT_LIST_HEAD(&clp->cl_superblocks);
138 clp->cl_rpcclient = ERR_PTR(-EINVAL);
141 init_rwsem(&clp->cl_sem);
142 INIT_LIST_HEAD(&clp->cl_delegations);
143 INIT_LIST_HEAD(&clp->cl_state_owners);
144 INIT_LIST_HEAD(&clp->cl_unused);
145 spin_lock_init(&clp->cl_lock);
146 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
147 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
148 clp->cl_boot_time = CURRENT_TIME;
149 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
155 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
159 __clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
166 static void nfs4_shutdown_client(struct nfs_client *clp)
169 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
170 nfs4_kill_renewd(clp);
171 while (!list_empty(&clp->cl_unused)) {
172 struct nfs4_state_owner *sp;
174 sp = list_entry(clp->cl_unused.next,
175 struct nfs4_state_owner,
177 list_del(&sp->so_list);
180 BUG_ON(!list_empty(&clp->cl_state_owners));
181 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
182 nfs_idmap_delete(clp);
187 * Destroy a shared client record
189 static void nfs_free_client(struct nfs_client *clp)
191 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
193 nfs4_shutdown_client(clp);
195 /* -EIO all pending I/O */
196 if (!IS_ERR(clp->cl_rpcclient))
197 rpc_shutdown_client(clp->cl_rpcclient);
199 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
202 if (__test_and_clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state))
205 kfree(clp->cl_hostname);
208 dprintk("<-- nfs_free_client()\n");
212 * Release a reference to a shared client record
214 void nfs_put_client(struct nfs_client *clp)
219 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
221 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
222 list_del(&clp->cl_share_link);
223 spin_unlock(&nfs_client_lock);
225 BUG_ON(!list_empty(&clp->cl_superblocks));
227 nfs_free_client(clp);
232 * Find a client by address
233 * - caller must hold nfs_client_lock
235 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
237 struct nfs_client *clp;
239 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
240 /* Don't match clients that failed to initialise properly */
241 if (clp->cl_cons_state < 0)
244 /* Different NFS versions cannot share the same nfs_client */
245 if (clp->cl_nfsversion != nfsversion)
248 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
249 sizeof(clp->cl_addr.sin_addr)) != 0)
252 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
259 atomic_inc(&clp->cl_count);
264 * Find a client by IP address and protocol version
265 * - returns NULL if no such client
267 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
269 struct nfs_client *clp;
271 spin_lock(&nfs_client_lock);
272 clp = __nfs_find_client(addr, nfsversion, 0);
273 spin_unlock(&nfs_client_lock);
274 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
282 * Look up a client by IP address and protocol version
283 * - creates a new record if one doesn't yet exist
285 static struct nfs_client *nfs_get_client(const char *hostname,
286 const struct sockaddr_in *addr,
289 struct nfs_client *clp, *new = NULL;
292 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
293 hostname ?: "", NIPQUAD(addr->sin_addr),
294 addr->sin_port, nfsversion);
296 /* see if the client already exists */
298 spin_lock(&nfs_client_lock);
300 clp = __nfs_find_client(addr, nfsversion, 1);
306 spin_unlock(&nfs_client_lock);
308 new = nfs_alloc_client(hostname, addr, nfsversion);
311 return ERR_PTR(-ENOMEM);
313 /* install a new client and return with it unready */
316 list_add(&clp->cl_share_link, &nfs_client_list);
317 spin_unlock(&nfs_client_lock);
318 dprintk("--> nfs_get_client() = %p [new]\n", clp);
321 /* found an existing client
322 * - make sure it's ready before returning
325 spin_unlock(&nfs_client_lock);
328 nfs_free_client(new);
330 error = wait_event_interruptible(nfs_client_active_wq,
331 clp->cl_cons_state != NFS_CS_INITING);
334 return ERR_PTR(-ERESTARTSYS);
337 if (clp->cl_cons_state < NFS_CS_READY) {
338 error = clp->cl_cons_state;
340 return ERR_PTR(error);
343 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
345 dprintk("--> nfs_get_client() = %p [share]\n", clp);
350 * Mark a server as ready or failed
352 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
354 clp->cl_cons_state = state;
355 wake_up_all(&nfs_client_active_wq);
359 * Initialise the timeout values for a connection
361 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
362 unsigned int timeo, unsigned int retrans)
364 to->to_initval = timeo * HZ / 10;
365 to->to_retries = retrans;
372 to->to_initval = 60 * HZ;
373 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
374 to->to_initval = NFS_MAX_TCP_TIMEOUT;
375 to->to_increment = to->to_initval;
376 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
377 to->to_exponential = 0;
382 to->to_initval = 11 * HZ / 10;
383 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
384 to->to_initval = NFS_MAX_UDP_TIMEOUT;
385 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
386 to->to_exponential = 1;
392 * Create an RPC client handle
394 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
396 unsigned int retrans,
397 rpc_authflavor_t flavor,
400 struct rpc_timeout timeparms;
401 struct rpc_clnt *clnt = NULL;
402 struct rpc_create_args args = {
404 .address = (struct sockaddr *)&clp->cl_addr,
405 .addrsize = sizeof(clp->cl_addr),
406 .timeout = &timeparms,
407 .servername = clp->cl_hostname,
408 .program = &nfs_program,
409 .version = clp->rpc_ops->version,
410 .authflavor = flavor,
414 if (!IS_ERR(clp->cl_rpcclient))
417 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
418 clp->retrans_timeo = timeparms.to_initval;
419 clp->retrans_count = timeparms.to_retries;
421 clnt = rpc_create(&args);
423 dprintk("%s: cannot create RPC client. Error = %ld\n",
424 __FUNCTION__, PTR_ERR(clnt));
425 return PTR_ERR(clnt);
428 clp->cl_rpcclient = clnt;
433 * Version 2 or 3 client destruction
435 static void nfs_destroy_server(struct nfs_server *server)
437 if (!IS_ERR(server->client_acl))
438 rpc_shutdown_client(server->client_acl);
440 if (!(server->flags & NFS_MOUNT_NONLM))
441 lockd_down(); /* release rpc.lockd */
445 * Version 2 or 3 lockd setup
447 static int nfs_start_lockd(struct nfs_server *server)
451 if (server->nfs_client->cl_nfsversion > 3)
453 if (server->flags & NFS_MOUNT_NONLM)
455 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
456 IPPROTO_TCP : IPPROTO_UDP);
458 server->flags |= NFS_MOUNT_NONLM;
460 server->destroy = nfs_destroy_server;
466 * Initialise an NFSv3 ACL client connection
468 #ifdef CONFIG_NFS_V3_ACL
469 static void nfs_init_server_aclclient(struct nfs_server *server)
471 if (server->nfs_client->cl_nfsversion != 3)
473 if (server->flags & NFS_MOUNT_NOACL)
476 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
477 if (IS_ERR(server->client_acl))
480 /* No errors! Assume that Sun nfsacls are supported */
481 server->caps |= NFS_CAP_ACLS;
485 server->caps &= ~NFS_CAP_ACLS;
488 static inline void nfs_init_server_aclclient(struct nfs_server *server)
490 server->flags &= ~NFS_MOUNT_NOACL;
491 server->caps &= ~NFS_CAP_ACLS;
496 * Create a general RPC client
498 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
500 struct nfs_client *clp = server->nfs_client;
502 server->client = rpc_clone_client(clp->cl_rpcclient);
503 if (IS_ERR(server->client)) {
504 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
505 return PTR_ERR(server->client);
508 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
509 struct rpc_auth *auth;
511 auth = rpcauth_create(pseudoflavour, server->client);
513 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
514 return PTR_ERR(auth);
517 server->client->cl_softrtry = 0;
518 if (server->flags & NFS_MOUNT_SOFT)
519 server->client->cl_softrtry = 1;
521 server->client->cl_intr = 0;
522 if (server->flags & NFS4_MOUNT_INTR)
523 server->client->cl_intr = 1;
529 * Initialise an NFS2 or NFS3 client
531 static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
533 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
536 if (clp->cl_cons_state == NFS_CS_READY) {
537 /* the client is already initialised */
538 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
542 /* Check NFS protocol revision and initialize RPC op vector */
543 clp->rpc_ops = &nfs_v2_clientops;
545 if (clp->cl_nfsversion == 3)
546 clp->rpc_ops = &nfs_v3_clientops;
549 * Create a client RPC handle for doing FSSTAT with UNIX auth only
550 * - RFC 2623, sec 2.3.2
552 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
556 nfs_mark_client_ready(clp, NFS_CS_READY);
560 nfs_mark_client_ready(clp, error);
561 dprintk("<-- nfs_init_client() = xerror %d\n", error);
566 * Create a version 2 or 3 client
568 static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
570 struct nfs_client *clp;
571 int error, nfsvers = 2;
573 dprintk("--> nfs_init_server()\n");
576 if (data->flags & NFS_MOUNT_VER3)
580 /* Allocate or find a client reference we can use */
581 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
583 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
587 error = nfs_init_client(clp, data);
591 server->nfs_client = clp;
593 /* Initialise the client representation from the mount data */
594 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
597 server->rsize = nfs_block_size(data->rsize, NULL);
599 server->wsize = nfs_block_size(data->wsize, NULL);
601 server->acregmin = data->acregmin * HZ;
602 server->acregmax = data->acregmax * HZ;
603 server->acdirmin = data->acdirmin * HZ;
604 server->acdirmax = data->acdirmax * HZ;
606 /* Start lockd here, before we might error out */
607 error = nfs_start_lockd(server);
611 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
615 server->namelen = data->namlen;
616 /* Create a client RPC handle for the NFSv3 ACL management interface */
617 nfs_init_server_aclclient(server);
618 if (clp->cl_nfsversion == 3) {
619 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
620 server->namelen = NFS3_MAXNAMLEN;
621 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
622 server->caps |= NFS_CAP_READDIRPLUS;
624 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
625 server->namelen = NFS2_MAXNAMLEN;
628 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
632 server->nfs_client = NULL;
634 dprintk("<-- nfs_init_server() = xerror %d\n", error);
639 * Load up the server record from information gained in an fsinfo record
641 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
643 unsigned long max_rpc_payload;
645 /* Work out a lot of parameters */
646 if (server->rsize == 0)
647 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
648 if (server->wsize == 0)
649 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
651 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
652 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
653 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
654 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
656 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
657 if (server->rsize > max_rpc_payload)
658 server->rsize = max_rpc_payload;
659 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
660 server->rsize = NFS_MAX_FILE_IO_SIZE;
661 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
662 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
664 if (server->wsize > max_rpc_payload)
665 server->wsize = max_rpc_payload;
666 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
667 server->wsize = NFS_MAX_FILE_IO_SIZE;
668 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
669 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
671 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
672 if (server->dtsize > PAGE_CACHE_SIZE)
673 server->dtsize = PAGE_CACHE_SIZE;
674 if (server->dtsize > server->rsize)
675 server->dtsize = server->rsize;
677 if (server->flags & NFS_MOUNT_NOAC) {
678 server->acregmin = server->acregmax = 0;
679 server->acdirmin = server->acdirmax = 0;
682 server->maxfilesize = fsinfo->maxfilesize;
684 /* We're airborne Set socket buffersize */
685 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
689 * Probe filesystem information, including the FSID on v2/v3
691 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
693 struct nfs_fsinfo fsinfo;
694 struct nfs_client *clp = server->nfs_client;
697 dprintk("--> nfs_probe_fsinfo()\n");
699 if (clp->rpc_ops->set_capabilities != NULL) {
700 error = clp->rpc_ops->set_capabilities(server, mntfh);
705 fsinfo.fattr = fattr;
706 nfs_fattr_init(fattr);
707 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
711 nfs_server_set_fsinfo(server, &fsinfo);
713 /* Get some general file system info */
714 if (server->namelen == 0) {
715 struct nfs_pathconf pathinfo;
717 pathinfo.fattr = fattr;
718 nfs_fattr_init(fattr);
720 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
721 server->namelen = pathinfo.max_namelen;
724 dprintk("<-- nfs_probe_fsinfo() = 0\n");
728 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
733 * Copy useful information when duplicating a server record
735 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
737 target->flags = source->flags;
738 target->acregmin = source->acregmin;
739 target->acregmax = source->acregmax;
740 target->acdirmin = source->acdirmin;
741 target->acdirmax = source->acdirmax;
742 target->caps = source->caps;
746 * Allocate and initialise a server record
748 static struct nfs_server *nfs_alloc_server(void)
750 struct nfs_server *server;
752 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
756 server->client = server->client_acl = ERR_PTR(-EINVAL);
758 /* Zero out the NFS state stuff */
759 INIT_LIST_HEAD(&server->client_link);
760 INIT_LIST_HEAD(&server->master_link);
762 server->io_stats = nfs_alloc_iostats();
763 if (!server->io_stats) {
772 * Free up a server record
774 void nfs_free_server(struct nfs_server *server)
776 dprintk("--> nfs_free_server()\n");
778 spin_lock(&nfs_client_lock);
779 list_del(&server->client_link);
780 list_del(&server->master_link);
781 spin_unlock(&nfs_client_lock);
783 if (server->destroy != NULL)
784 server->destroy(server);
785 if (!IS_ERR(server->client))
786 rpc_shutdown_client(server->client);
788 nfs_put_client(server->nfs_client);
790 nfs_free_iostats(server->io_stats);
792 nfs_release_automount_timer();
793 dprintk("<-- nfs_free_server()\n");
797 * Create a version 2 or 3 volume record
798 * - keyed on server and FSID
800 struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
801 struct nfs_fh *mntfh)
803 struct nfs_server *server;
804 struct nfs_fattr fattr;
807 server = nfs_alloc_server();
809 return ERR_PTR(-ENOMEM);
811 /* Get a client representation */
812 error = nfs_init_server(server, data);
816 BUG_ON(!server->nfs_client);
817 BUG_ON(!server->nfs_client->rpc_ops);
818 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
820 /* Probe the root fh to retrieve its FSID */
821 error = nfs_probe_fsinfo(server, mntfh, &fattr);
824 if (!(fattr.valid & NFS_ATTR_FATTR)) {
825 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
827 dprintk("nfs_create_server: getattr error = %d\n", -error);
831 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
833 dprintk("Server FSID: %llx:%llx\n",
834 (unsigned long long) server->fsid.major,
835 (unsigned long long) server->fsid.minor);
837 BUG_ON(!server->nfs_client);
838 BUG_ON(!server->nfs_client->rpc_ops);
839 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
841 spin_lock(&nfs_client_lock);
842 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
843 list_add_tail(&server->master_link, &nfs_volume_list);
844 spin_unlock(&nfs_client_lock);
846 server->mount_time = jiffies;
850 nfs_free_server(server);
851 return ERR_PTR(error);
856 * Initialise an NFS4 client record
858 static int nfs4_init_client(struct nfs_client *clp,
859 int proto, int timeo, int retrans,
861 rpc_authflavor_t authflavour)
865 if (clp->cl_cons_state == NFS_CS_READY) {
866 /* the client is initialised already */
867 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
871 /* Check NFS protocol revision and initialize RPC op vector */
872 clp->rpc_ops = &nfs_v4_clientops;
874 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
875 RPC_CLNT_CREATE_DISCRTRY);
878 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
880 error = nfs_idmap_new(clp);
882 dprintk("%s: failed to create idmapper. Error = %d\n",
883 __FUNCTION__, error);
886 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
888 nfs_mark_client_ready(clp, NFS_CS_READY);
892 nfs_mark_client_ready(clp, error);
893 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
898 * Set up an NFS4 client
900 static int nfs4_set_client(struct nfs_server *server,
901 const char *hostname, const struct sockaddr_in *addr,
903 rpc_authflavor_t authflavour,
904 int proto, int timeo, int retrans)
906 struct nfs_client *clp;
909 dprintk("--> nfs4_set_client()\n");
911 /* Allocate or find a client reference we can use */
912 clp = nfs_get_client(hostname, addr, 4);
914 error = PTR_ERR(clp);
917 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
921 server->nfs_client = clp;
922 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
928 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
933 * Create a version 4 volume record
935 static int nfs4_init_server(struct nfs_server *server,
936 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
940 dprintk("--> nfs4_init_server()\n");
942 /* Initialise the client representation from the mount data */
943 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
944 server->caps |= NFS_CAP_ATOMIC_OPEN;
947 server->rsize = nfs_block_size(data->rsize, NULL);
949 server->wsize = nfs_block_size(data->wsize, NULL);
951 server->acregmin = data->acregmin * HZ;
952 server->acregmax = data->acregmax * HZ;
953 server->acdirmin = data->acdirmin * HZ;
954 server->acdirmax = data->acdirmax * HZ;
956 error = nfs_init_server_rpcclient(server, authflavour);
959 dprintk("<-- nfs4_init_server() = %d\n", error);
964 * Create a version 4 volume record
965 * - keyed on server and FSID
967 struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
968 const char *hostname,
969 const struct sockaddr_in *addr,
972 rpc_authflavor_t authflavour,
973 struct nfs_fh *mntfh)
975 struct nfs_fattr fattr;
976 struct nfs_server *server;
979 dprintk("--> nfs4_create_server()\n");
981 server = nfs_alloc_server();
983 return ERR_PTR(-ENOMEM);
985 /* Get a client record */
986 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
987 data->proto, data->timeo, data->retrans);
991 /* set up the general RPC client */
992 error = nfs4_init_server(server, data, authflavour);
996 BUG_ON(!server->nfs_client);
997 BUG_ON(!server->nfs_client->rpc_ops);
998 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1000 /* Probe the root fh to retrieve its FSID */
1001 error = nfs4_path_walk(server, mntfh, mntpath);
1005 dprintk("Server FSID: %llx:%llx\n",
1006 (unsigned long long) server->fsid.major,
1007 (unsigned long long) server->fsid.minor);
1008 dprintk("Mount FH: %d\n", mntfh->size);
1010 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1014 BUG_ON(!server->nfs_client);
1015 BUG_ON(!server->nfs_client->rpc_ops);
1016 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1018 spin_lock(&nfs_client_lock);
1019 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1020 list_add_tail(&server->master_link, &nfs_volume_list);
1021 spin_unlock(&nfs_client_lock);
1023 server->mount_time = jiffies;
1024 dprintk("<-- nfs4_create_server() = %p\n", server);
1028 nfs_free_server(server);
1029 dprintk("<-- nfs4_create_server() = error %d\n", error);
1030 return ERR_PTR(error);
1034 * Create an NFS4 referral server record
1036 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1037 struct nfs_fh *mntfh)
1039 struct nfs_client *parent_client;
1040 struct nfs_server *server, *parent_server;
1041 struct nfs_fattr fattr;
1044 dprintk("--> nfs4_create_referral_server()\n");
1046 server = nfs_alloc_server();
1048 return ERR_PTR(-ENOMEM);
1050 parent_server = NFS_SB(data->sb);
1051 parent_client = parent_server->nfs_client;
1053 /* Get a client representation.
1054 * Note: NFSv4 always uses TCP, */
1055 error = nfs4_set_client(server, data->hostname, data->addr,
1056 parent_client->cl_ipaddr,
1058 parent_server->client->cl_xprt->prot,
1059 parent_client->retrans_timeo,
1060 parent_client->retrans_count);
1064 /* Initialise the client representation from the parent server */
1065 nfs_server_copy_userdata(server, parent_server);
1066 server->caps |= NFS_CAP_ATOMIC_OPEN;
1068 error = nfs_init_server_rpcclient(server, data->authflavor);
1072 BUG_ON(!server->nfs_client);
1073 BUG_ON(!server->nfs_client->rpc_ops);
1074 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1076 /* Probe the root fh to retrieve its FSID and filehandle */
1077 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1081 /* probe the filesystem info for this server filesystem */
1082 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1086 dprintk("Referral FSID: %llx:%llx\n",
1087 (unsigned long long) server->fsid.major,
1088 (unsigned long long) server->fsid.minor);
1090 spin_lock(&nfs_client_lock);
1091 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1092 list_add_tail(&server->master_link, &nfs_volume_list);
1093 spin_unlock(&nfs_client_lock);
1095 server->mount_time = jiffies;
1097 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1101 nfs_free_server(server);
1102 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1103 return ERR_PTR(error);
1106 #endif /* CONFIG_NFS_V4 */
1109 * Clone an NFS2, NFS3 or NFS4 server record
1111 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1113 struct nfs_fattr *fattr)
1115 struct nfs_server *server;
1116 struct nfs_fattr fattr_fsinfo;
1119 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1120 (unsigned long long) fattr->fsid.major,
1121 (unsigned long long) fattr->fsid.minor);
1123 server = nfs_alloc_server();
1125 return ERR_PTR(-ENOMEM);
1127 /* Copy data from the source */
1128 server->nfs_client = source->nfs_client;
1129 atomic_inc(&server->nfs_client->cl_count);
1130 nfs_server_copy_userdata(server, source);
1132 server->fsid = fattr->fsid;
1134 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1136 goto out_free_server;
1137 if (!IS_ERR(source->client_acl))
1138 nfs_init_server_aclclient(server);
1140 /* probe the filesystem info for this server filesystem */
1141 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1143 goto out_free_server;
1145 dprintk("Cloned FSID: %llx:%llx\n",
1146 (unsigned long long) server->fsid.major,
1147 (unsigned long long) server->fsid.minor);
1149 error = nfs_start_lockd(server);
1151 goto out_free_server;
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;
1160 dprintk("<-- nfs_clone_server() = %p\n", server);
1164 nfs_free_server(server);
1165 dprintk("<-- nfs_clone_server() = error %d\n", error);
1166 return ERR_PTR(error);
1169 #ifdef CONFIG_PROC_FS
1170 static struct proc_dir_entry *proc_fs_nfs;
1172 static int nfs_server_list_open(struct inode *inode, struct file *file);
1173 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1174 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1175 static void nfs_server_list_stop(struct seq_file *p, void *v);
1176 static int nfs_server_list_show(struct seq_file *m, void *v);
1178 static struct seq_operations nfs_server_list_ops = {
1179 .start = nfs_server_list_start,
1180 .next = nfs_server_list_next,
1181 .stop = nfs_server_list_stop,
1182 .show = nfs_server_list_show,
1185 static const struct file_operations nfs_server_list_fops = {
1186 .open = nfs_server_list_open,
1188 .llseek = seq_lseek,
1189 .release = seq_release,
1192 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1193 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1194 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1195 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1196 static int nfs_volume_list_show(struct seq_file *m, void *v);
1198 static struct seq_operations nfs_volume_list_ops = {
1199 .start = nfs_volume_list_start,
1200 .next = nfs_volume_list_next,
1201 .stop = nfs_volume_list_stop,
1202 .show = nfs_volume_list_show,
1205 static const struct file_operations nfs_volume_list_fops = {
1206 .open = nfs_volume_list_open,
1208 .llseek = seq_lseek,
1209 .release = seq_release,
1213 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1216 static int nfs_server_list_open(struct inode *inode, struct file *file)
1221 ret = seq_open(file, &nfs_server_list_ops);
1225 m = file->private_data;
1226 m->private = PDE(inode)->data;
1232 * set up the iterator to start reading from the server list and return the first item
1234 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1236 struct list_head *_p;
1239 /* lock the list against modification */
1240 spin_lock(&nfs_client_lock);
1242 /* allow for the header line */
1244 return SEQ_START_TOKEN;
1247 /* find the n'th element in the list */
1248 list_for_each(_p, &nfs_client_list)
1252 return _p != &nfs_client_list ? _p : NULL;
1256 * move to next server
1258 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1260 struct list_head *_p;
1265 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1267 return _p != &nfs_client_list ? _p : NULL;
1271 * clean up after reading from the transports list
1273 static void nfs_server_list_stop(struct seq_file *p, void *v)
1275 spin_unlock(&nfs_client_lock);
1279 * display a header line followed by a load of call lines
1281 static int nfs_server_list_show(struct seq_file *m, void *v)
1283 struct nfs_client *clp;
1285 /* display header on line 1 */
1286 if (v == SEQ_START_TOKEN) {
1287 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1291 /* display one transport per line on subsequent lines */
1292 clp = list_entry(v, struct nfs_client, cl_share_link);
1294 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1296 NIPQUAD(clp->cl_addr.sin_addr),
1297 ntohs(clp->cl_addr.sin_port),
1298 atomic_read(&clp->cl_count),
1305 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1307 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1312 ret = seq_open(file, &nfs_volume_list_ops);
1316 m = file->private_data;
1317 m->private = PDE(inode)->data;
1323 * set up the iterator to start reading from the volume list and return the first item
1325 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1327 struct list_head *_p;
1330 /* lock the list against modification */
1331 spin_lock(&nfs_client_lock);
1333 /* allow for the header line */
1335 return SEQ_START_TOKEN;
1338 /* find the n'th element in the list */
1339 list_for_each(_p, &nfs_volume_list)
1343 return _p != &nfs_volume_list ? _p : NULL;
1347 * move to next volume
1349 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1351 struct list_head *_p;
1356 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1358 return _p != &nfs_volume_list ? _p : NULL;
1362 * clean up after reading from the transports list
1364 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1366 spin_unlock(&nfs_client_lock);
1370 * display a header line followed by a load of call lines
1372 static int nfs_volume_list_show(struct seq_file *m, void *v)
1374 struct nfs_server *server;
1375 struct nfs_client *clp;
1376 char dev[8], fsid[17];
1378 /* display header on line 1 */
1379 if (v == SEQ_START_TOKEN) {
1380 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1383 /* display one transport per line on subsequent lines */
1384 server = list_entry(v, struct nfs_server, master_link);
1385 clp = server->nfs_client;
1387 snprintf(dev, 8, "%u:%u",
1388 MAJOR(server->s_dev), MINOR(server->s_dev));
1390 snprintf(fsid, 17, "%llx:%llx",
1391 (unsigned long long) server->fsid.major,
1392 (unsigned long long) server->fsid.minor);
1394 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1396 NIPQUAD(clp->cl_addr.sin_addr),
1397 ntohs(clp->cl_addr.sin_port),
1405 * initialise the /proc/fs/nfsfs/ directory
1407 int __init nfs_fs_proc_init(void)
1409 struct proc_dir_entry *p;
1411 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1415 proc_fs_nfs->owner = THIS_MODULE;
1417 /* a file of servers with which we're dealing */
1418 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1422 p->proc_fops = &nfs_server_list_fops;
1423 p->owner = THIS_MODULE;
1425 /* a file of volumes that we have mounted */
1426 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1430 p->proc_fops = &nfs_volume_list_fops;
1431 p->owner = THIS_MODULE;
1435 remove_proc_entry("servers", proc_fs_nfs);
1437 remove_proc_entry("nfsfs", proc_root_fs);
1443 * clean up the /proc/fs/nfsfs/ directory
1445 void nfs_fs_proc_exit(void)
1447 remove_proc_entry("volumes", proc_fs_nfs);
1448 remove_proc_entry("servers", proc_fs_nfs);
1449 remove_proc_entry("nfsfs", proc_root_fs);
1452 #endif /* CONFIG_PROC_FS */