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/nfs_xdr.h>
39 #include <asm/system.h>
43 #include "delegation.h"
47 #define NFSDBG_FACILITY NFSDBG_CLIENT
49 static DEFINE_SPINLOCK(nfs_client_lock);
50 static LIST_HEAD(nfs_client_list);
51 static LIST_HEAD(nfs_volume_list);
52 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
57 static struct rpc_version *nfs_version[5] = {
67 struct rpc_program nfs_program = {
69 .number = NFS_PROGRAM,
70 .nrvers = ARRAY_SIZE(nfs_version),
71 .version = nfs_version,
72 .stats = &nfs_rpcstat,
73 .pipe_dir_name = "/nfs",
76 struct rpc_stat nfs_rpcstat = {
77 .program = &nfs_program
81 #ifdef CONFIG_NFS_V3_ACL
82 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
83 static struct rpc_version * nfsacl_version[] = {
84 [3] = &nfsacl_version3,
87 struct rpc_program nfsacl_program = {
89 .number = NFS_ACL_PROGRAM,
90 .nrvers = ARRAY_SIZE(nfsacl_version),
91 .version = nfsacl_version,
92 .stats = &nfsacl_rpcstat,
94 #endif /* CONFIG_NFS_V3_ACL */
96 struct nfs_client_initdata {
98 const struct sockaddr_in *addr;
99 const struct nfs_rpc_ops *rpc_ops;
103 * Allocate a shared client record
105 * Since these are allocated/deallocated very rarely, we don't
106 * bother putting them in a slab cache...
108 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
110 struct nfs_client *clp;
112 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
115 clp->rpc_ops = cl_init->rpc_ops;
117 if (cl_init->rpc_ops->version == 4) {
118 if (nfs_callback_up() < 0)
120 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
123 atomic_set(&clp->cl_count, 1);
124 clp->cl_cons_state = NFS_CS_INITING;
126 memcpy(&clp->cl_addr, cl_init->addr, sizeof(clp->cl_addr));
128 if (cl_init->hostname) {
129 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
130 if (!clp->cl_hostname)
134 INIT_LIST_HEAD(&clp->cl_superblocks);
135 clp->cl_rpcclient = ERR_PTR(-EINVAL);
138 init_rwsem(&clp->cl_sem);
139 INIT_LIST_HEAD(&clp->cl_delegations);
140 spin_lock_init(&clp->cl_lock);
141 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
142 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
143 clp->cl_boot_time = CURRENT_TIME;
144 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
150 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
158 static void nfs4_shutdown_client(struct nfs_client *clp)
161 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
162 nfs4_kill_renewd(clp);
163 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
164 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
165 nfs_idmap_delete(clp);
170 * Destroy a shared client record
172 static void nfs_free_client(struct nfs_client *clp)
174 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
176 nfs4_shutdown_client(clp);
178 /* -EIO all pending I/O */
179 if (!IS_ERR(clp->cl_rpcclient))
180 rpc_shutdown_client(clp->cl_rpcclient);
182 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
185 kfree(clp->cl_hostname);
188 dprintk("<-- nfs_free_client()\n");
192 * Release a reference to a shared client record
194 void nfs_put_client(struct nfs_client *clp)
199 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
201 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
202 list_del(&clp->cl_share_link);
203 spin_unlock(&nfs_client_lock);
205 BUG_ON(!list_empty(&clp->cl_superblocks));
207 nfs_free_client(clp);
212 * Find a client by IP address and protocol version
213 * - returns NULL if no such client
215 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
217 struct nfs_client *clp;
219 spin_lock(&nfs_client_lock);
220 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
221 /* Don't match clients that failed to initialise properly */
222 if (clp->cl_cons_state != NFS_CS_READY)
225 /* Different NFS versions cannot share the same nfs_client */
226 if (clp->rpc_ops->version != nfsversion)
229 /* Match only the IP address, not the port number */
230 if (clp->cl_addr.sin_addr.s_addr != addr->sin_addr.s_addr)
233 atomic_inc(&clp->cl_count);
234 spin_unlock(&nfs_client_lock);
237 spin_unlock(&nfs_client_lock);
242 * Find an nfs_client on the list that matches the initialisation data
245 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
247 struct nfs_client *clp;
249 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
250 /* Don't match clients that failed to initialise properly */
251 if (clp->cl_cons_state < 0)
254 /* Different NFS versions cannot share the same nfs_client */
255 if (clp->rpc_ops != data->rpc_ops)
258 /* Match the full socket address */
259 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
262 atomic_inc(&clp->cl_count);
269 * Look up a client by IP address and protocol version
270 * - creates a new record if one doesn't yet exist
272 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
274 struct nfs_client *clp, *new = NULL;
277 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%u)\n",
278 cl_init->hostname ?: "", NIPQUAD(cl_init->addr->sin_addr),
279 cl_init->addr->sin_port, cl_init->rpc_ops->version);
281 /* see if the client already exists */
283 spin_lock(&nfs_client_lock);
285 clp = nfs_match_client(cl_init);
291 spin_unlock(&nfs_client_lock);
293 new = nfs_alloc_client(cl_init);
296 return ERR_PTR(-ENOMEM);
298 /* install a new client and return with it unready */
301 list_add(&clp->cl_share_link, &nfs_client_list);
302 spin_unlock(&nfs_client_lock);
303 dprintk("--> nfs_get_client() = %p [new]\n", clp);
306 /* found an existing client
307 * - make sure it's ready before returning
310 spin_unlock(&nfs_client_lock);
313 nfs_free_client(new);
315 error = wait_event_interruptible(nfs_client_active_wq,
316 clp->cl_cons_state != NFS_CS_INITING);
319 return ERR_PTR(-ERESTARTSYS);
322 if (clp->cl_cons_state < NFS_CS_READY) {
323 error = clp->cl_cons_state;
325 return ERR_PTR(error);
328 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
330 dprintk("--> nfs_get_client() = %p [share]\n", clp);
335 * Mark a server as ready or failed
337 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
339 clp->cl_cons_state = state;
340 wake_up_all(&nfs_client_active_wq);
344 * Initialise the timeout values for a connection
346 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
347 unsigned int timeo, unsigned int retrans)
349 to->to_initval = timeo * HZ / 10;
350 to->to_retries = retrans;
355 case XPRT_TRANSPORT_TCP:
356 case XPRT_TRANSPORT_RDMA:
358 to->to_initval = 60 * HZ;
359 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
360 to->to_initval = NFS_MAX_TCP_TIMEOUT;
361 to->to_increment = to->to_initval;
362 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
363 to->to_exponential = 0;
365 case XPRT_TRANSPORT_UDP:
368 to->to_initval = 11 * HZ / 10;
369 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
370 to->to_initval = NFS_MAX_UDP_TIMEOUT;
371 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
372 to->to_exponential = 1;
378 * Create an RPC client handle
380 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
382 unsigned int retrans,
383 rpc_authflavor_t flavor,
386 struct rpc_timeout timeparms;
387 struct rpc_clnt *clnt = NULL;
388 struct rpc_create_args args = {
390 .address = (struct sockaddr *)&clp->cl_addr,
391 .addrsize = sizeof(clp->cl_addr),
392 .timeout = &timeparms,
393 .servername = clp->cl_hostname,
394 .program = &nfs_program,
395 .version = clp->rpc_ops->version,
396 .authflavor = flavor,
400 if (!IS_ERR(clp->cl_rpcclient))
403 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
404 clp->retrans_timeo = timeparms.to_initval;
405 clp->retrans_count = timeparms.to_retries;
407 clnt = rpc_create(&args);
409 dprintk("%s: cannot create RPC client. Error = %ld\n",
410 __FUNCTION__, PTR_ERR(clnt));
411 return PTR_ERR(clnt);
414 clp->cl_rpcclient = clnt;
419 * Version 2 or 3 client destruction
421 static void nfs_destroy_server(struct nfs_server *server)
423 if (!(server->flags & NFS_MOUNT_NONLM))
424 lockd_down(); /* release rpc.lockd */
428 * Version 2 or 3 lockd setup
430 static int nfs_start_lockd(struct nfs_server *server)
434 if (server->nfs_client->rpc_ops->version > 3)
436 if (server->flags & NFS_MOUNT_NONLM)
438 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
439 IPPROTO_TCP : IPPROTO_UDP);
441 server->flags |= NFS_MOUNT_NONLM;
443 server->destroy = nfs_destroy_server;
449 * Initialise an NFSv3 ACL client connection
451 #ifdef CONFIG_NFS_V3_ACL
452 static void nfs_init_server_aclclient(struct nfs_server *server)
454 if (server->nfs_client->rpc_ops->version != 3)
456 if (server->flags & NFS_MOUNT_NOACL)
459 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
460 if (IS_ERR(server->client_acl))
463 /* No errors! Assume that Sun nfsacls are supported */
464 server->caps |= NFS_CAP_ACLS;
468 server->caps &= ~NFS_CAP_ACLS;
471 static inline void nfs_init_server_aclclient(struct nfs_server *server)
473 server->flags &= ~NFS_MOUNT_NOACL;
474 server->caps &= ~NFS_CAP_ACLS;
479 * Create a general RPC client
481 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
483 struct nfs_client *clp = server->nfs_client;
485 server->client = rpc_clone_client(clp->cl_rpcclient);
486 if (IS_ERR(server->client)) {
487 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
488 return PTR_ERR(server->client);
491 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
492 struct rpc_auth *auth;
494 auth = rpcauth_create(pseudoflavour, server->client);
496 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
497 return PTR_ERR(auth);
500 server->client->cl_softrtry = 0;
501 if (server->flags & NFS_MOUNT_SOFT)
502 server->client->cl_softrtry = 1;
504 server->client->cl_intr = 0;
505 if (server->flags & NFS4_MOUNT_INTR)
506 server->client->cl_intr = 1;
512 * Initialise an NFS2 or NFS3 client
514 static int nfs_init_client(struct nfs_client *clp,
515 const struct nfs_parsed_mount_data *data)
519 if (clp->cl_cons_state == NFS_CS_READY) {
520 /* the client is already initialised */
521 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
526 * Create a client RPC handle for doing FSSTAT with UNIX auth only
527 * - RFC 2623, sec 2.3.2
529 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
530 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
533 nfs_mark_client_ready(clp, NFS_CS_READY);
537 nfs_mark_client_ready(clp, error);
538 dprintk("<-- nfs_init_client() = xerror %d\n", error);
543 * Create a version 2 or 3 client
545 static int nfs_init_server(struct nfs_server *server,
546 const struct nfs_parsed_mount_data *data)
548 struct nfs_client_initdata cl_init = {
549 .hostname = data->nfs_server.hostname,
550 .addr = &data->nfs_server.address,
551 .rpc_ops = &nfs_v2_clientops,
553 struct nfs_client *clp;
556 dprintk("--> nfs_init_server()\n");
559 if (data->flags & NFS_MOUNT_VER3)
560 cl_init.rpc_ops = &nfs_v3_clientops;
563 /* Allocate or find a client reference we can use */
564 clp = nfs_get_client(&cl_init);
566 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
570 error = nfs_init_client(clp, data);
574 server->nfs_client = clp;
576 /* Initialise the client representation from the mount data */
577 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
580 server->rsize = nfs_block_size(data->rsize, NULL);
582 server->wsize = nfs_block_size(data->wsize, NULL);
584 server->acregmin = data->acregmin * HZ;
585 server->acregmax = data->acregmax * HZ;
586 server->acdirmin = data->acdirmin * HZ;
587 server->acdirmax = data->acdirmax * HZ;
589 /* Start lockd here, before we might error out */
590 error = nfs_start_lockd(server);
594 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
598 server->namelen = data->namlen;
599 /* Create a client RPC handle for the NFSv3 ACL management interface */
600 nfs_init_server_aclclient(server);
601 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
605 server->nfs_client = NULL;
607 dprintk("<-- nfs_init_server() = xerror %d\n", error);
612 * Load up the server record from information gained in an fsinfo record
614 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
616 unsigned long max_rpc_payload;
618 /* Work out a lot of parameters */
619 if (server->rsize == 0)
620 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
621 if (server->wsize == 0)
622 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
624 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
625 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
626 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
627 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
629 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
630 if (server->rsize > max_rpc_payload)
631 server->rsize = max_rpc_payload;
632 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
633 server->rsize = NFS_MAX_FILE_IO_SIZE;
634 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
636 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
638 if (server->wsize > max_rpc_payload)
639 server->wsize = max_rpc_payload;
640 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
641 server->wsize = NFS_MAX_FILE_IO_SIZE;
642 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
643 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
645 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
646 if (server->dtsize > PAGE_CACHE_SIZE)
647 server->dtsize = PAGE_CACHE_SIZE;
648 if (server->dtsize > server->rsize)
649 server->dtsize = server->rsize;
651 if (server->flags & NFS_MOUNT_NOAC) {
652 server->acregmin = server->acregmax = 0;
653 server->acdirmin = server->acdirmax = 0;
656 server->maxfilesize = fsinfo->maxfilesize;
658 /* We're airborne Set socket buffersize */
659 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
663 * Probe filesystem information, including the FSID on v2/v3
665 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
667 struct nfs_fsinfo fsinfo;
668 struct nfs_client *clp = server->nfs_client;
671 dprintk("--> nfs_probe_fsinfo()\n");
673 if (clp->rpc_ops->set_capabilities != NULL) {
674 error = clp->rpc_ops->set_capabilities(server, mntfh);
679 fsinfo.fattr = fattr;
680 nfs_fattr_init(fattr);
681 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
685 nfs_server_set_fsinfo(server, &fsinfo);
686 error = bdi_init(&server->backing_dev_info);
691 /* Get some general file system info */
692 if (server->namelen == 0) {
693 struct nfs_pathconf pathinfo;
695 pathinfo.fattr = fattr;
696 nfs_fattr_init(fattr);
698 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
699 server->namelen = pathinfo.max_namelen;
702 dprintk("<-- nfs_probe_fsinfo() = 0\n");
706 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
711 * Copy useful information when duplicating a server record
713 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
715 target->flags = source->flags;
716 target->acregmin = source->acregmin;
717 target->acregmax = source->acregmax;
718 target->acdirmin = source->acdirmin;
719 target->acdirmax = source->acdirmax;
720 target->caps = source->caps;
724 * Allocate and initialise a server record
726 static struct nfs_server *nfs_alloc_server(void)
728 struct nfs_server *server;
730 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
734 server->client = server->client_acl = ERR_PTR(-EINVAL);
736 /* Zero out the NFS state stuff */
737 INIT_LIST_HEAD(&server->client_link);
738 INIT_LIST_HEAD(&server->master_link);
740 init_waitqueue_head(&server->active_wq);
741 atomic_set(&server->active, 0);
743 server->io_stats = nfs_alloc_iostats();
744 if (!server->io_stats) {
753 * Free up a server record
755 void nfs_free_server(struct nfs_server *server)
757 dprintk("--> nfs_free_server()\n");
759 spin_lock(&nfs_client_lock);
760 list_del(&server->client_link);
761 list_del(&server->master_link);
762 spin_unlock(&nfs_client_lock);
764 if (server->destroy != NULL)
765 server->destroy(server);
767 if (!IS_ERR(server->client_acl))
768 rpc_shutdown_client(server->client_acl);
769 if (!IS_ERR(server->client))
770 rpc_shutdown_client(server->client);
772 nfs_put_client(server->nfs_client);
774 nfs_free_iostats(server->io_stats);
775 bdi_destroy(&server->backing_dev_info);
777 nfs_release_automount_timer();
778 dprintk("<-- nfs_free_server()\n");
782 * Create a version 2 or 3 volume record
783 * - keyed on server and FSID
785 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
786 struct nfs_fh *mntfh)
788 struct nfs_server *server;
789 struct nfs_fattr fattr;
792 server = nfs_alloc_server();
794 return ERR_PTR(-ENOMEM);
796 /* Get a client representation */
797 error = nfs_init_server(server, data);
801 BUG_ON(!server->nfs_client);
802 BUG_ON(!server->nfs_client->rpc_ops);
803 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
805 /* Probe the root fh to retrieve its FSID */
806 error = nfs_probe_fsinfo(server, mntfh, &fattr);
809 if (server->nfs_client->rpc_ops->version == 3) {
810 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
811 server->namelen = NFS3_MAXNAMLEN;
812 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
813 server->caps |= NFS_CAP_READDIRPLUS;
815 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
816 server->namelen = NFS2_MAXNAMLEN;
819 if (!(fattr.valid & NFS_ATTR_FATTR)) {
820 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
822 dprintk("nfs_create_server: getattr error = %d\n", -error);
826 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
828 dprintk("Server FSID: %llx:%llx\n",
829 (unsigned long long) server->fsid.major,
830 (unsigned long long) server->fsid.minor);
832 BUG_ON(!server->nfs_client);
833 BUG_ON(!server->nfs_client->rpc_ops);
834 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
836 spin_lock(&nfs_client_lock);
837 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
838 list_add_tail(&server->master_link, &nfs_volume_list);
839 spin_unlock(&nfs_client_lock);
841 server->mount_time = jiffies;
845 nfs_free_server(server);
846 return ERR_PTR(error);
851 * Initialise an NFS4 client record
853 static int nfs4_init_client(struct nfs_client *clp,
854 int proto, int timeo, int retrans,
856 rpc_authflavor_t authflavour)
860 if (clp->cl_cons_state == NFS_CS_READY) {
861 /* the client is initialised already */
862 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
866 /* Check NFS protocol revision and initialize RPC op vector */
867 clp->rpc_ops = &nfs_v4_clientops;
869 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
870 RPC_CLNT_CREATE_DISCRTRY);
873 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
875 error = nfs_idmap_new(clp);
877 dprintk("%s: failed to create idmapper. Error = %d\n",
878 __FUNCTION__, error);
881 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
883 nfs_mark_client_ready(clp, NFS_CS_READY);
887 nfs_mark_client_ready(clp, error);
888 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
893 * Set up an NFS4 client
895 static int nfs4_set_client(struct nfs_server *server,
896 const char *hostname, const struct sockaddr_in *addr,
898 rpc_authflavor_t authflavour,
899 int proto, int timeo, int retrans)
901 struct nfs_client_initdata cl_init = {
902 .hostname = hostname,
904 .rpc_ops = &nfs_v4_clientops,
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(&cl_init);
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 nfs_parsed_mount_data *data)
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, data->auth_flavors[0]);
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 nfs_parsed_mount_data *data,
968 struct nfs_fh *mntfh)
970 struct nfs_fattr fattr;
971 struct nfs_server *server;
974 dprintk("--> nfs4_create_server()\n");
976 server = nfs_alloc_server();
978 return ERR_PTR(-ENOMEM);
980 /* Get a client record */
981 error = nfs4_set_client(server,
982 data->nfs_server.hostname,
983 &data->nfs_server.address,
984 data->client_address,
985 data->auth_flavors[0],
986 data->nfs_server.protocol,
987 data->timeo, data->retrans);
991 /* set up the general RPC client */
992 error = nfs4_init_server(server, data);
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, data->nfs_server.export_path);
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 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1015 server->namelen = NFS4_MAXNAMLEN;
1017 BUG_ON(!server->nfs_client);
1018 BUG_ON(!server->nfs_client->rpc_ops);
1019 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1021 spin_lock(&nfs_client_lock);
1022 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1023 list_add_tail(&server->master_link, &nfs_volume_list);
1024 spin_unlock(&nfs_client_lock);
1026 server->mount_time = jiffies;
1027 dprintk("<-- nfs4_create_server() = %p\n", server);
1031 nfs_free_server(server);
1032 dprintk("<-- nfs4_create_server() = error %d\n", error);
1033 return ERR_PTR(error);
1037 * Create an NFS4 referral server record
1039 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1040 struct nfs_fh *mntfh)
1042 struct nfs_client *parent_client;
1043 struct nfs_server *server, *parent_server;
1044 struct nfs_fattr fattr;
1047 dprintk("--> nfs4_create_referral_server()\n");
1049 server = nfs_alloc_server();
1051 return ERR_PTR(-ENOMEM);
1053 parent_server = NFS_SB(data->sb);
1054 parent_client = parent_server->nfs_client;
1056 /* Get a client representation.
1057 * Note: NFSv4 always uses TCP, */
1058 error = nfs4_set_client(server, data->hostname, data->addr,
1059 parent_client->cl_ipaddr,
1061 parent_server->client->cl_xprt->prot,
1062 parent_client->retrans_timeo,
1063 parent_client->retrans_count);
1067 /* Initialise the client representation from the parent server */
1068 nfs_server_copy_userdata(server, parent_server);
1069 server->caps |= NFS_CAP_ATOMIC_OPEN;
1071 error = nfs_init_server_rpcclient(server, data->authflavor);
1075 BUG_ON(!server->nfs_client);
1076 BUG_ON(!server->nfs_client->rpc_ops);
1077 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1079 /* Probe the root fh to retrieve its FSID and filehandle */
1080 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1084 /* probe the filesystem info for this server filesystem */
1085 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1089 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1090 server->namelen = NFS4_MAXNAMLEN;
1092 dprintk("Referral FSID: %llx:%llx\n",
1093 (unsigned long long) server->fsid.major,
1094 (unsigned long long) server->fsid.minor);
1096 spin_lock(&nfs_client_lock);
1097 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1098 list_add_tail(&server->master_link, &nfs_volume_list);
1099 spin_unlock(&nfs_client_lock);
1101 server->mount_time = jiffies;
1103 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1107 nfs_free_server(server);
1108 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1109 return ERR_PTR(error);
1112 #endif /* CONFIG_NFS_V4 */
1115 * Clone an NFS2, NFS3 or NFS4 server record
1117 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1119 struct nfs_fattr *fattr)
1121 struct nfs_server *server;
1122 struct nfs_fattr fattr_fsinfo;
1125 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1126 (unsigned long long) fattr->fsid.major,
1127 (unsigned long long) fattr->fsid.minor);
1129 server = nfs_alloc_server();
1131 return ERR_PTR(-ENOMEM);
1133 /* Copy data from the source */
1134 server->nfs_client = source->nfs_client;
1135 atomic_inc(&server->nfs_client->cl_count);
1136 nfs_server_copy_userdata(server, source);
1138 server->fsid = fattr->fsid;
1140 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1142 goto out_free_server;
1143 if (!IS_ERR(source->client_acl))
1144 nfs_init_server_aclclient(server);
1146 /* probe the filesystem info for this server filesystem */
1147 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1149 goto out_free_server;
1151 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1152 server->namelen = NFS4_MAXNAMLEN;
1154 dprintk("Cloned FSID: %llx:%llx\n",
1155 (unsigned long long) server->fsid.major,
1156 (unsigned long long) server->fsid.minor);
1158 error = nfs_start_lockd(server);
1160 goto out_free_server;
1162 spin_lock(&nfs_client_lock);
1163 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1164 list_add_tail(&server->master_link, &nfs_volume_list);
1165 spin_unlock(&nfs_client_lock);
1167 server->mount_time = jiffies;
1169 dprintk("<-- nfs_clone_server() = %p\n", server);
1173 nfs_free_server(server);
1174 dprintk("<-- nfs_clone_server() = error %d\n", error);
1175 return ERR_PTR(error);
1178 #ifdef CONFIG_PROC_FS
1179 static struct proc_dir_entry *proc_fs_nfs;
1181 static int nfs_server_list_open(struct inode *inode, struct file *file);
1182 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1183 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1184 static void nfs_server_list_stop(struct seq_file *p, void *v);
1185 static int nfs_server_list_show(struct seq_file *m, void *v);
1187 static struct seq_operations nfs_server_list_ops = {
1188 .start = nfs_server_list_start,
1189 .next = nfs_server_list_next,
1190 .stop = nfs_server_list_stop,
1191 .show = nfs_server_list_show,
1194 static const struct file_operations nfs_server_list_fops = {
1195 .open = nfs_server_list_open,
1197 .llseek = seq_lseek,
1198 .release = seq_release,
1201 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1202 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1203 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1204 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1205 static int nfs_volume_list_show(struct seq_file *m, void *v);
1207 static struct seq_operations nfs_volume_list_ops = {
1208 .start = nfs_volume_list_start,
1209 .next = nfs_volume_list_next,
1210 .stop = nfs_volume_list_stop,
1211 .show = nfs_volume_list_show,
1214 static const struct file_operations nfs_volume_list_fops = {
1215 .open = nfs_volume_list_open,
1217 .llseek = seq_lseek,
1218 .release = seq_release,
1222 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1225 static int nfs_server_list_open(struct inode *inode, struct file *file)
1230 ret = seq_open(file, &nfs_server_list_ops);
1234 m = file->private_data;
1235 m->private = PDE(inode)->data;
1241 * set up the iterator to start reading from the server list and return the first item
1243 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1245 /* lock the list against modification */
1246 spin_lock(&nfs_client_lock);
1247 return seq_list_start_head(&nfs_client_list, *_pos);
1251 * move to next server
1253 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1255 return seq_list_next(v, &nfs_client_list, pos);
1259 * clean up after reading from the transports list
1261 static void nfs_server_list_stop(struct seq_file *p, void *v)
1263 spin_unlock(&nfs_client_lock);
1267 * display a header line followed by a load of call lines
1269 static int nfs_server_list_show(struct seq_file *m, void *v)
1271 struct nfs_client *clp;
1273 /* display header on line 1 */
1274 if (v == &nfs_client_list) {
1275 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1279 /* display one transport per line on subsequent lines */
1280 clp = list_entry(v, struct nfs_client, cl_share_link);
1282 seq_printf(m, "v%u %02x%02x%02x%02x %4hx %3d %s\n",
1283 clp->rpc_ops->version,
1284 NIPQUAD(clp->cl_addr.sin_addr),
1285 ntohs(clp->cl_addr.sin_port),
1286 atomic_read(&clp->cl_count),
1293 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1295 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1300 ret = seq_open(file, &nfs_volume_list_ops);
1304 m = file->private_data;
1305 m->private = PDE(inode)->data;
1311 * set up the iterator to start reading from the volume list and return the first item
1313 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1315 /* lock the list against modification */
1316 spin_lock(&nfs_client_lock);
1317 return seq_list_start_head(&nfs_volume_list, *_pos);
1321 * move to next volume
1323 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1325 return seq_list_next(v, &nfs_volume_list, pos);
1329 * clean up after reading from the transports list
1331 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1333 spin_unlock(&nfs_client_lock);
1337 * display a header line followed by a load of call lines
1339 static int nfs_volume_list_show(struct seq_file *m, void *v)
1341 struct nfs_server *server;
1342 struct nfs_client *clp;
1343 char dev[8], fsid[17];
1345 /* display header on line 1 */
1346 if (v == &nfs_volume_list) {
1347 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1350 /* display one transport per line on subsequent lines */
1351 server = list_entry(v, struct nfs_server, master_link);
1352 clp = server->nfs_client;
1354 snprintf(dev, 8, "%u:%u",
1355 MAJOR(server->s_dev), MINOR(server->s_dev));
1357 snprintf(fsid, 17, "%llx:%llx",
1358 (unsigned long long) server->fsid.major,
1359 (unsigned long long) server->fsid.minor);
1361 seq_printf(m, "v%u %02x%02x%02x%02x %4hx %-7s %-17s\n",
1362 clp->rpc_ops->version,
1363 NIPQUAD(clp->cl_addr.sin_addr),
1364 ntohs(clp->cl_addr.sin_port),
1372 * initialise the /proc/fs/nfsfs/ directory
1374 int __init nfs_fs_proc_init(void)
1376 struct proc_dir_entry *p;
1378 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1382 proc_fs_nfs->owner = THIS_MODULE;
1384 /* a file of servers with which we're dealing */
1385 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1389 p->proc_fops = &nfs_server_list_fops;
1390 p->owner = THIS_MODULE;
1392 /* a file of volumes that we have mounted */
1393 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1397 p->proc_fops = &nfs_volume_list_fops;
1398 p->owner = THIS_MODULE;
1402 remove_proc_entry("servers", proc_fs_nfs);
1404 remove_proc_entry("nfsfs", proc_root_fs);
1410 * clean up the /proc/fs/nfsfs/ directory
1412 void nfs_fs_proc_exit(void)
1414 remove_proc_entry("volumes", proc_fs_nfs);
1415 remove_proc_entry("servers", proc_fs_nfs);
1416 remove_proc_entry("nfsfs", proc_root_fs);
1419 #endif /* CONFIG_PROC_FS */