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 server->caps |= NFS_CAP_READDIRPLUS;
623 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
624 server->namelen = NFS2_MAXNAMLEN;
627 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
631 server->nfs_client = NULL;
633 dprintk("<-- nfs_init_server() = xerror %d\n", error);
638 * Load up the server record from information gained in an fsinfo record
640 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
642 unsigned long max_rpc_payload;
644 /* Work out a lot of parameters */
645 if (server->rsize == 0)
646 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
647 if (server->wsize == 0)
648 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
650 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
651 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
652 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
653 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
655 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
656 if (server->rsize > max_rpc_payload)
657 server->rsize = max_rpc_payload;
658 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
659 server->rsize = NFS_MAX_FILE_IO_SIZE;
660 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
661 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
663 if (server->wsize > max_rpc_payload)
664 server->wsize = max_rpc_payload;
665 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
666 server->wsize = NFS_MAX_FILE_IO_SIZE;
667 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
668 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
670 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
671 if (server->dtsize > PAGE_CACHE_SIZE)
672 server->dtsize = PAGE_CACHE_SIZE;
673 if (server->dtsize > server->rsize)
674 server->dtsize = server->rsize;
676 if (server->flags & NFS_MOUNT_NOAC) {
677 server->acregmin = server->acregmax = 0;
678 server->acdirmin = server->acdirmax = 0;
681 server->maxfilesize = fsinfo->maxfilesize;
683 /* We're airborne Set socket buffersize */
684 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
688 * Probe filesystem information, including the FSID on v2/v3
690 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
692 struct nfs_fsinfo fsinfo;
693 struct nfs_client *clp = server->nfs_client;
696 dprintk("--> nfs_probe_fsinfo()\n");
698 if (clp->rpc_ops->set_capabilities != NULL) {
699 error = clp->rpc_ops->set_capabilities(server, mntfh);
704 fsinfo.fattr = fattr;
705 nfs_fattr_init(fattr);
706 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
710 nfs_server_set_fsinfo(server, &fsinfo);
712 /* Get some general file system info */
713 if (server->namelen == 0) {
714 struct nfs_pathconf pathinfo;
716 pathinfo.fattr = fattr;
717 nfs_fattr_init(fattr);
719 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
720 server->namelen = pathinfo.max_namelen;
723 dprintk("<-- nfs_probe_fsinfo() = 0\n");
727 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
732 * Copy useful information when duplicating a server record
734 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
736 target->flags = source->flags;
737 target->acregmin = source->acregmin;
738 target->acregmax = source->acregmax;
739 target->acdirmin = source->acdirmin;
740 target->acdirmax = source->acdirmax;
741 target->caps = source->caps;
745 * Allocate and initialise a server record
747 static struct nfs_server *nfs_alloc_server(void)
749 struct nfs_server *server;
751 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
755 server->client = server->client_acl = ERR_PTR(-EINVAL);
757 /* Zero out the NFS state stuff */
758 INIT_LIST_HEAD(&server->client_link);
759 INIT_LIST_HEAD(&server->master_link);
761 server->io_stats = nfs_alloc_iostats();
762 if (!server->io_stats) {
771 * Free up a server record
773 void nfs_free_server(struct nfs_server *server)
775 dprintk("--> nfs_free_server()\n");
777 spin_lock(&nfs_client_lock);
778 list_del(&server->client_link);
779 list_del(&server->master_link);
780 spin_unlock(&nfs_client_lock);
782 if (server->destroy != NULL)
783 server->destroy(server);
784 if (!IS_ERR(server->client))
785 rpc_shutdown_client(server->client);
787 nfs_put_client(server->nfs_client);
789 nfs_free_iostats(server->io_stats);
791 nfs_release_automount_timer();
792 dprintk("<-- nfs_free_server()\n");
796 * Create a version 2 or 3 volume record
797 * - keyed on server and FSID
799 struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
800 struct nfs_fh *mntfh)
802 struct nfs_server *server;
803 struct nfs_fattr fattr;
806 server = nfs_alloc_server();
808 return ERR_PTR(-ENOMEM);
810 /* Get a client representation */
811 error = nfs_init_server(server, data);
815 BUG_ON(!server->nfs_client);
816 BUG_ON(!server->nfs_client->rpc_ops);
817 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
819 /* Probe the root fh to retrieve its FSID */
820 error = nfs_probe_fsinfo(server, mntfh, &fattr);
823 if (!(fattr.valid & NFS_ATTR_FATTR)) {
824 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
826 dprintk("nfs_create_server: getattr error = %d\n", -error);
830 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
832 dprintk("Server FSID: %llx:%llx\n",
833 (unsigned long long) server->fsid.major,
834 (unsigned long long) server->fsid.minor);
836 BUG_ON(!server->nfs_client);
837 BUG_ON(!server->nfs_client->rpc_ops);
838 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
840 spin_lock(&nfs_client_lock);
841 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
842 list_add_tail(&server->master_link, &nfs_volume_list);
843 spin_unlock(&nfs_client_lock);
845 server->mount_time = jiffies;
849 nfs_free_server(server);
850 return ERR_PTR(error);
855 * Initialise an NFS4 client record
857 static int nfs4_init_client(struct nfs_client *clp,
858 int proto, int timeo, int retrans,
860 rpc_authflavor_t authflavour)
864 if (clp->cl_cons_state == NFS_CS_READY) {
865 /* the client is initialised already */
866 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
870 /* Check NFS protocol revision and initialize RPC op vector */
871 clp->rpc_ops = &nfs_v4_clientops;
873 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
874 RPC_CLNT_CREATE_DISCRTRY);
877 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
879 error = nfs_idmap_new(clp);
881 dprintk("%s: failed to create idmapper. Error = %d\n",
882 __FUNCTION__, error);
885 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
887 nfs_mark_client_ready(clp, NFS_CS_READY);
891 nfs_mark_client_ready(clp, error);
892 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
897 * Set up an NFS4 client
899 static int nfs4_set_client(struct nfs_server *server,
900 const char *hostname, const struct sockaddr_in *addr,
902 rpc_authflavor_t authflavour,
903 int proto, int timeo, int retrans)
905 struct nfs_client *clp;
908 dprintk("--> nfs4_set_client()\n");
910 /* Allocate or find a client reference we can use */
911 clp = nfs_get_client(hostname, addr, 4);
913 error = PTR_ERR(clp);
916 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
920 server->nfs_client = clp;
921 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
927 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
932 * Create a version 4 volume record
934 static int nfs4_init_server(struct nfs_server *server,
935 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
939 dprintk("--> nfs4_init_server()\n");
941 /* Initialise the client representation from the mount data */
942 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
943 server->caps |= NFS_CAP_ATOMIC_OPEN;
946 server->rsize = nfs_block_size(data->rsize, NULL);
948 server->wsize = nfs_block_size(data->wsize, NULL);
950 server->acregmin = data->acregmin * HZ;
951 server->acregmax = data->acregmax * HZ;
952 server->acdirmin = data->acdirmin * HZ;
953 server->acdirmax = data->acdirmax * HZ;
955 error = nfs_init_server_rpcclient(server, authflavour);
958 dprintk("<-- nfs4_init_server() = %d\n", error);
963 * Create a version 4 volume record
964 * - keyed on server and FSID
966 struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
967 const char *hostname,
968 const struct sockaddr_in *addr,
971 rpc_authflavor_t authflavour,
972 struct nfs_fh *mntfh)
974 struct nfs_fattr fattr;
975 struct nfs_server *server;
978 dprintk("--> nfs4_create_server()\n");
980 server = nfs_alloc_server();
982 return ERR_PTR(-ENOMEM);
984 /* Get a client record */
985 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
986 data->proto, data->timeo, data->retrans);
990 /* set up the general RPC client */
991 error = nfs4_init_server(server, data, authflavour);
995 BUG_ON(!server->nfs_client);
996 BUG_ON(!server->nfs_client->rpc_ops);
997 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
999 /* Probe the root fh to retrieve its FSID */
1000 error = nfs4_path_walk(server, mntfh, mntpath);
1004 dprintk("Server FSID: %llx:%llx\n",
1005 (unsigned long long) server->fsid.major,
1006 (unsigned long long) server->fsid.minor);
1007 dprintk("Mount FH: %d\n", mntfh->size);
1009 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1013 BUG_ON(!server->nfs_client);
1014 BUG_ON(!server->nfs_client->rpc_ops);
1015 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1017 spin_lock(&nfs_client_lock);
1018 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1019 list_add_tail(&server->master_link, &nfs_volume_list);
1020 spin_unlock(&nfs_client_lock);
1022 server->mount_time = jiffies;
1023 dprintk("<-- nfs4_create_server() = %p\n", server);
1027 nfs_free_server(server);
1028 dprintk("<-- nfs4_create_server() = error %d\n", error);
1029 return ERR_PTR(error);
1033 * Create an NFS4 referral server record
1035 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1036 struct nfs_fh *mntfh)
1038 struct nfs_client *parent_client;
1039 struct nfs_server *server, *parent_server;
1040 struct nfs_fattr fattr;
1043 dprintk("--> nfs4_create_referral_server()\n");
1045 server = nfs_alloc_server();
1047 return ERR_PTR(-ENOMEM);
1049 parent_server = NFS_SB(data->sb);
1050 parent_client = parent_server->nfs_client;
1052 /* Get a client representation.
1053 * Note: NFSv4 always uses TCP, */
1054 error = nfs4_set_client(server, data->hostname, data->addr,
1055 parent_client->cl_ipaddr,
1057 parent_server->client->cl_xprt->prot,
1058 parent_client->retrans_timeo,
1059 parent_client->retrans_count);
1063 /* Initialise the client representation from the parent server */
1064 nfs_server_copy_userdata(server, parent_server);
1065 server->caps |= NFS_CAP_ATOMIC_OPEN;
1067 error = nfs_init_server_rpcclient(server, data->authflavor);
1071 BUG_ON(!server->nfs_client);
1072 BUG_ON(!server->nfs_client->rpc_ops);
1073 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1075 /* Probe the root fh to retrieve its FSID and filehandle */
1076 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1080 /* probe the filesystem info for this server filesystem */
1081 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1085 dprintk("Referral FSID: %llx:%llx\n",
1086 (unsigned long long) server->fsid.major,
1087 (unsigned long long) server->fsid.minor);
1089 spin_lock(&nfs_client_lock);
1090 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1091 list_add_tail(&server->master_link, &nfs_volume_list);
1092 spin_unlock(&nfs_client_lock);
1094 server->mount_time = jiffies;
1096 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1100 nfs_free_server(server);
1101 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1102 return ERR_PTR(error);
1105 #endif /* CONFIG_NFS_V4 */
1108 * Clone an NFS2, NFS3 or NFS4 server record
1110 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1112 struct nfs_fattr *fattr)
1114 struct nfs_server *server;
1115 struct nfs_fattr fattr_fsinfo;
1118 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1119 (unsigned long long) fattr->fsid.major,
1120 (unsigned long long) fattr->fsid.minor);
1122 server = nfs_alloc_server();
1124 return ERR_PTR(-ENOMEM);
1126 /* Copy data from the source */
1127 server->nfs_client = source->nfs_client;
1128 atomic_inc(&server->nfs_client->cl_count);
1129 nfs_server_copy_userdata(server, source);
1131 server->fsid = fattr->fsid;
1133 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1135 goto out_free_server;
1136 if (!IS_ERR(source->client_acl))
1137 nfs_init_server_aclclient(server);
1139 /* probe the filesystem info for this server filesystem */
1140 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1142 goto out_free_server;
1144 dprintk("Cloned FSID: %llx:%llx\n",
1145 (unsigned long long) server->fsid.major,
1146 (unsigned long long) server->fsid.minor);
1148 error = nfs_start_lockd(server);
1150 goto out_free_server;
1152 spin_lock(&nfs_client_lock);
1153 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1154 list_add_tail(&server->master_link, &nfs_volume_list);
1155 spin_unlock(&nfs_client_lock);
1157 server->mount_time = jiffies;
1159 dprintk("<-- nfs_clone_server() = %p\n", server);
1163 nfs_free_server(server);
1164 dprintk("<-- nfs_clone_server() = error %d\n", error);
1165 return ERR_PTR(error);
1168 #ifdef CONFIG_PROC_FS
1169 static struct proc_dir_entry *proc_fs_nfs;
1171 static int nfs_server_list_open(struct inode *inode, struct file *file);
1172 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1173 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1174 static void nfs_server_list_stop(struct seq_file *p, void *v);
1175 static int nfs_server_list_show(struct seq_file *m, void *v);
1177 static struct seq_operations nfs_server_list_ops = {
1178 .start = nfs_server_list_start,
1179 .next = nfs_server_list_next,
1180 .stop = nfs_server_list_stop,
1181 .show = nfs_server_list_show,
1184 static const struct file_operations nfs_server_list_fops = {
1185 .open = nfs_server_list_open,
1187 .llseek = seq_lseek,
1188 .release = seq_release,
1191 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1192 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1193 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1194 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1195 static int nfs_volume_list_show(struct seq_file *m, void *v);
1197 static struct seq_operations nfs_volume_list_ops = {
1198 .start = nfs_volume_list_start,
1199 .next = nfs_volume_list_next,
1200 .stop = nfs_volume_list_stop,
1201 .show = nfs_volume_list_show,
1204 static const struct file_operations nfs_volume_list_fops = {
1205 .open = nfs_volume_list_open,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1212 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1215 static int nfs_server_list_open(struct inode *inode, struct file *file)
1220 ret = seq_open(file, &nfs_server_list_ops);
1224 m = file->private_data;
1225 m->private = PDE(inode)->data;
1231 * set up the iterator to start reading from the server list and return the first item
1233 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1235 struct list_head *_p;
1238 /* lock the list against modification */
1239 spin_lock(&nfs_client_lock);
1241 /* allow for the header line */
1243 return SEQ_START_TOKEN;
1246 /* find the n'th element in the list */
1247 list_for_each(_p, &nfs_client_list)
1251 return _p != &nfs_client_list ? _p : NULL;
1255 * move to next server
1257 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1259 struct list_head *_p;
1264 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1266 return _p != &nfs_client_list ? _p : NULL;
1270 * clean up after reading from the transports list
1272 static void nfs_server_list_stop(struct seq_file *p, void *v)
1274 spin_unlock(&nfs_client_lock);
1278 * display a header line followed by a load of call lines
1280 static int nfs_server_list_show(struct seq_file *m, void *v)
1282 struct nfs_client *clp;
1284 /* display header on line 1 */
1285 if (v == SEQ_START_TOKEN) {
1286 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1290 /* display one transport per line on subsequent lines */
1291 clp = list_entry(v, struct nfs_client, cl_share_link);
1293 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1295 NIPQUAD(clp->cl_addr.sin_addr),
1296 ntohs(clp->cl_addr.sin_port),
1297 atomic_read(&clp->cl_count),
1304 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1306 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1311 ret = seq_open(file, &nfs_volume_list_ops);
1315 m = file->private_data;
1316 m->private = PDE(inode)->data;
1322 * set up the iterator to start reading from the volume list and return the first item
1324 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1326 struct list_head *_p;
1329 /* lock the list against modification */
1330 spin_lock(&nfs_client_lock);
1332 /* allow for the header line */
1334 return SEQ_START_TOKEN;
1337 /* find the n'th element in the list */
1338 list_for_each(_p, &nfs_volume_list)
1342 return _p != &nfs_volume_list ? _p : NULL;
1346 * move to next volume
1348 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1350 struct list_head *_p;
1355 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1357 return _p != &nfs_volume_list ? _p : NULL;
1361 * clean up after reading from the transports list
1363 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1365 spin_unlock(&nfs_client_lock);
1369 * display a header line followed by a load of call lines
1371 static int nfs_volume_list_show(struct seq_file *m, void *v)
1373 struct nfs_server *server;
1374 struct nfs_client *clp;
1375 char dev[8], fsid[17];
1377 /* display header on line 1 */
1378 if (v == SEQ_START_TOKEN) {
1379 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1382 /* display one transport per line on subsequent lines */
1383 server = list_entry(v, struct nfs_server, master_link);
1384 clp = server->nfs_client;
1386 snprintf(dev, 8, "%u:%u",
1387 MAJOR(server->s_dev), MINOR(server->s_dev));
1389 snprintf(fsid, 17, "%llx:%llx",
1390 (unsigned long long) server->fsid.major,
1391 (unsigned long long) server->fsid.minor);
1393 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1395 NIPQUAD(clp->cl_addr.sin_addr),
1396 ntohs(clp->cl_addr.sin_port),
1404 * initialise the /proc/fs/nfsfs/ directory
1406 int __init nfs_fs_proc_init(void)
1408 struct proc_dir_entry *p;
1410 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1414 proc_fs_nfs->owner = THIS_MODULE;
1416 /* a file of servers with which we're dealing */
1417 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1421 p->proc_fops = &nfs_server_list_fops;
1422 p->owner = THIS_MODULE;
1424 /* a file of volumes that we have mounted */
1425 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1429 p->proc_fops = &nfs_volume_list_fops;
1430 p->owner = THIS_MODULE;
1434 remove_proc_entry("servers", proc_fs_nfs);
1436 remove_proc_entry("nfsfs", proc_root_fs);
1442 * clean up the /proc/fs/nfsfs/ directory
1444 void nfs_fs_proc_exit(void)
1446 remove_proc_entry("volumes", proc_fs_nfs);
1447 remove_proc_entry("servers", proc_fs_nfs);
1448 remove_proc_entry("nfsfs", proc_root_fs);
1451 #endif /* CONFIG_PROC_FS */