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/nfs_fs.h>
27 #include <linux/nfs_mount.h>
28 #include <linux/nfs4_mount.h>
29 #include <linux/lockd/bind.h>
30 #include <linux/seq_file.h>
31 #include <linux/mount.h>
32 #include <linux/nfs_idmap.h>
33 #include <linux/vfs.h>
34 #include <linux/inet.h>
35 #include <linux/nfs_xdr.h>
37 #include <asm/system.h>
41 #include "delegation.h"
45 #define NFSDBG_FACILITY NFSDBG_CLIENT
47 static DEFINE_SPINLOCK(nfs_client_lock);
48 static LIST_HEAD(nfs_client_list);
49 static LIST_HEAD(nfs_volume_list);
50 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55 static struct rpc_version *nfs_version[5] = {
65 struct rpc_program nfs_program = {
67 .number = NFS_PROGRAM,
68 .nrvers = ARRAY_SIZE(nfs_version),
69 .version = nfs_version,
70 .stats = &nfs_rpcstat,
71 .pipe_dir_name = "/nfs",
74 struct rpc_stat nfs_rpcstat = {
75 .program = &nfs_program
79 #ifdef CONFIG_NFS_V3_ACL
80 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
81 static struct rpc_version * nfsacl_version[] = {
82 [3] = &nfsacl_version3,
85 struct rpc_program nfsacl_program = {
87 .number = NFS_ACL_PROGRAM,
88 .nrvers = ARRAY_SIZE(nfsacl_version),
89 .version = nfsacl_version,
90 .stats = &nfsacl_rpcstat,
92 #endif /* CONFIG_NFS_V3_ACL */
95 * Allocate a shared client record
97 * Since these are allocated/deallocated very rarely, we don't
98 * bother putting them in a slab cache...
100 static struct nfs_client *nfs_alloc_client(const char *hostname,
101 const struct sockaddr_in *addr,
104 struct nfs_client *clp;
106 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
109 if (nfsversion == 4) {
110 if (nfs_callback_up() < 0)
112 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
115 atomic_set(&clp->cl_count, 1);
116 clp->cl_cons_state = NFS_CS_INITING;
118 clp->cl_nfsversion = nfsversion;
119 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
122 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
123 if (!clp->cl_hostname)
127 INIT_LIST_HEAD(&clp->cl_superblocks);
128 clp->cl_rpcclient = ERR_PTR(-EINVAL);
131 init_rwsem(&clp->cl_sem);
132 INIT_LIST_HEAD(&clp->cl_delegations);
133 spin_lock_init(&clp->cl_lock);
134 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
135 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
136 clp->cl_boot_time = CURRENT_TIME;
137 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
143 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
151 static void nfs4_shutdown_client(struct nfs_client *clp)
154 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
155 nfs4_kill_renewd(clp);
156 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
157 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
158 nfs_idmap_delete(clp);
163 * Destroy a shared client record
165 static void nfs_free_client(struct nfs_client *clp)
167 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
169 nfs4_shutdown_client(clp);
171 /* -EIO all pending I/O */
172 if (!IS_ERR(clp->cl_rpcclient))
173 rpc_shutdown_client(clp->cl_rpcclient);
175 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
178 kfree(clp->cl_hostname);
181 dprintk("<-- nfs_free_client()\n");
185 * Release a reference to a shared client record
187 void nfs_put_client(struct nfs_client *clp)
192 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
194 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
195 list_del(&clp->cl_share_link);
196 spin_unlock(&nfs_client_lock);
198 BUG_ON(!list_empty(&clp->cl_superblocks));
200 nfs_free_client(clp);
205 * Find a client by address
206 * - caller must hold nfs_client_lock
208 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
210 struct nfs_client *clp;
212 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
213 /* Don't match clients that failed to initialise properly */
214 if (clp->cl_cons_state < 0)
217 /* Different NFS versions cannot share the same nfs_client */
218 if (clp->cl_nfsversion != nfsversion)
221 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
222 sizeof(clp->cl_addr.sin_addr)) != 0)
225 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
232 atomic_inc(&clp->cl_count);
237 * Find a client by IP address and protocol version
238 * - returns NULL if no such client
240 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
242 struct nfs_client *clp;
244 spin_lock(&nfs_client_lock);
245 clp = __nfs_find_client(addr, nfsversion, 0);
246 spin_unlock(&nfs_client_lock);
247 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
255 * Look up a client by IP address and protocol version
256 * - creates a new record if one doesn't yet exist
258 static struct nfs_client *nfs_get_client(const char *hostname,
259 const struct sockaddr_in *addr,
262 struct nfs_client *clp, *new = NULL;
265 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
266 hostname ?: "", NIPQUAD(addr->sin_addr),
267 addr->sin_port, nfsversion);
269 /* see if the client already exists */
271 spin_lock(&nfs_client_lock);
273 clp = __nfs_find_client(addr, nfsversion, 1);
279 spin_unlock(&nfs_client_lock);
281 new = nfs_alloc_client(hostname, addr, nfsversion);
284 return ERR_PTR(-ENOMEM);
286 /* install a new client and return with it unready */
289 list_add(&clp->cl_share_link, &nfs_client_list);
290 spin_unlock(&nfs_client_lock);
291 dprintk("--> nfs_get_client() = %p [new]\n", clp);
294 /* found an existing client
295 * - make sure it's ready before returning
298 spin_unlock(&nfs_client_lock);
301 nfs_free_client(new);
303 error = wait_event_interruptible(nfs_client_active_wq,
304 clp->cl_cons_state != NFS_CS_INITING);
307 return ERR_PTR(-ERESTARTSYS);
310 if (clp->cl_cons_state < NFS_CS_READY) {
311 error = clp->cl_cons_state;
313 return ERR_PTR(error);
316 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
318 dprintk("--> nfs_get_client() = %p [share]\n", clp);
323 * Mark a server as ready or failed
325 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
327 clp->cl_cons_state = state;
328 wake_up_all(&nfs_client_active_wq);
332 * Initialise the timeout values for a connection
334 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
335 unsigned int timeo, unsigned int retrans)
337 to->to_initval = timeo * HZ / 10;
338 to->to_retries = retrans;
345 to->to_initval = 60 * HZ;
346 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
347 to->to_initval = NFS_MAX_TCP_TIMEOUT;
348 to->to_increment = to->to_initval;
349 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
350 to->to_exponential = 0;
355 to->to_initval = 11 * HZ / 10;
356 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
357 to->to_initval = NFS_MAX_UDP_TIMEOUT;
358 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
359 to->to_exponential = 1;
365 * Create an RPC client handle
367 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
369 unsigned int retrans,
370 rpc_authflavor_t flavor,
373 struct rpc_timeout timeparms;
374 struct rpc_clnt *clnt = NULL;
375 struct rpc_create_args args = {
377 .address = (struct sockaddr *)&clp->cl_addr,
378 .addrsize = sizeof(clp->cl_addr),
379 .timeout = &timeparms,
380 .servername = clp->cl_hostname,
381 .program = &nfs_program,
382 .version = clp->rpc_ops->version,
383 .authflavor = flavor,
387 if (!IS_ERR(clp->cl_rpcclient))
390 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
391 clp->retrans_timeo = timeparms.to_initval;
392 clp->retrans_count = timeparms.to_retries;
394 clnt = rpc_create(&args);
396 dprintk("%s: cannot create RPC client. Error = %ld\n",
397 __FUNCTION__, PTR_ERR(clnt));
398 return PTR_ERR(clnt);
401 clp->cl_rpcclient = clnt;
406 * Version 2 or 3 client destruction
408 static void nfs_destroy_server(struct nfs_server *server)
410 if (!IS_ERR(server->client_acl))
411 rpc_shutdown_client(server->client_acl);
413 if (!(server->flags & NFS_MOUNT_NONLM))
414 lockd_down(); /* release rpc.lockd */
418 * Version 2 or 3 lockd setup
420 static int nfs_start_lockd(struct nfs_server *server)
424 if (server->nfs_client->cl_nfsversion > 3)
426 if (server->flags & NFS_MOUNT_NONLM)
428 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
429 IPPROTO_TCP : IPPROTO_UDP);
431 server->flags |= NFS_MOUNT_NONLM;
433 server->destroy = nfs_destroy_server;
439 * Initialise an NFSv3 ACL client connection
441 #ifdef CONFIG_NFS_V3_ACL
442 static void nfs_init_server_aclclient(struct nfs_server *server)
444 if (server->nfs_client->cl_nfsversion != 3)
446 if (server->flags & NFS_MOUNT_NOACL)
449 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
450 if (IS_ERR(server->client_acl))
453 /* No errors! Assume that Sun nfsacls are supported */
454 server->caps |= NFS_CAP_ACLS;
458 server->caps &= ~NFS_CAP_ACLS;
461 static inline void nfs_init_server_aclclient(struct nfs_server *server)
463 server->flags &= ~NFS_MOUNT_NOACL;
464 server->caps &= ~NFS_CAP_ACLS;
469 * Create a general RPC client
471 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
473 struct nfs_client *clp = server->nfs_client;
475 server->client = rpc_clone_client(clp->cl_rpcclient);
476 if (IS_ERR(server->client)) {
477 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
478 return PTR_ERR(server->client);
481 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
482 struct rpc_auth *auth;
484 auth = rpcauth_create(pseudoflavour, server->client);
486 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
487 return PTR_ERR(auth);
490 server->client->cl_softrtry = 0;
491 if (server->flags & NFS_MOUNT_SOFT)
492 server->client->cl_softrtry = 1;
494 server->client->cl_intr = 0;
495 if (server->flags & NFS4_MOUNT_INTR)
496 server->client->cl_intr = 1;
502 * Initialise an NFS2 or NFS3 client
504 static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
506 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
509 if (clp->cl_cons_state == NFS_CS_READY) {
510 /* the client is already initialised */
511 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
515 /* Check NFS protocol revision and initialize RPC op vector */
516 clp->rpc_ops = &nfs_v2_clientops;
518 if (clp->cl_nfsversion == 3)
519 clp->rpc_ops = &nfs_v3_clientops;
522 * Create a client RPC handle for doing FSSTAT with UNIX auth only
523 * - RFC 2623, sec 2.3.2
525 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
529 nfs_mark_client_ready(clp, NFS_CS_READY);
533 nfs_mark_client_ready(clp, error);
534 dprintk("<-- nfs_init_client() = xerror %d\n", error);
539 * Create a version 2 or 3 client
541 static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
543 struct nfs_client *clp;
544 int error, nfsvers = 2;
546 dprintk("--> nfs_init_server()\n");
549 if (data->flags & NFS_MOUNT_VER3)
553 /* Allocate or find a client reference we can use */
554 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
556 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
560 error = nfs_init_client(clp, data);
564 server->nfs_client = clp;
566 /* Initialise the client representation from the mount data */
567 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
570 server->rsize = nfs_block_size(data->rsize, NULL);
572 server->wsize = nfs_block_size(data->wsize, NULL);
574 server->acregmin = data->acregmin * HZ;
575 server->acregmax = data->acregmax * HZ;
576 server->acdirmin = data->acdirmin * HZ;
577 server->acdirmax = data->acdirmax * HZ;
579 /* Start lockd here, before we might error out */
580 error = nfs_start_lockd(server);
584 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
588 server->namelen = data->namlen;
589 /* Create a client RPC handle for the NFSv3 ACL management interface */
590 nfs_init_server_aclclient(server);
591 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
595 server->nfs_client = NULL;
597 dprintk("<-- nfs_init_server() = xerror %d\n", error);
602 * Load up the server record from information gained in an fsinfo record
604 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
606 unsigned long max_rpc_payload;
608 /* Work out a lot of parameters */
609 if (server->rsize == 0)
610 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
611 if (server->wsize == 0)
612 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
614 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
615 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
616 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
617 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
619 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
620 if (server->rsize > max_rpc_payload)
621 server->rsize = max_rpc_payload;
622 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
623 server->rsize = NFS_MAX_FILE_IO_SIZE;
624 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
625 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
627 if (server->wsize > max_rpc_payload)
628 server->wsize = max_rpc_payload;
629 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
630 server->wsize = NFS_MAX_FILE_IO_SIZE;
631 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
632 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
634 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
635 if (server->dtsize > PAGE_CACHE_SIZE)
636 server->dtsize = PAGE_CACHE_SIZE;
637 if (server->dtsize > server->rsize)
638 server->dtsize = server->rsize;
640 if (server->flags & NFS_MOUNT_NOAC) {
641 server->acregmin = server->acregmax = 0;
642 server->acdirmin = server->acdirmax = 0;
645 server->maxfilesize = fsinfo->maxfilesize;
647 /* We're airborne Set socket buffersize */
648 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
652 * Probe filesystem information, including the FSID on v2/v3
654 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
656 struct nfs_fsinfo fsinfo;
657 struct nfs_client *clp = server->nfs_client;
660 dprintk("--> nfs_probe_fsinfo()\n");
662 if (clp->rpc_ops->set_capabilities != NULL) {
663 error = clp->rpc_ops->set_capabilities(server, mntfh);
668 fsinfo.fattr = fattr;
669 nfs_fattr_init(fattr);
670 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
674 nfs_server_set_fsinfo(server, &fsinfo);
676 /* Get some general file system info */
677 if (server->namelen == 0) {
678 struct nfs_pathconf pathinfo;
680 pathinfo.fattr = fattr;
681 nfs_fattr_init(fattr);
683 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
684 server->namelen = pathinfo.max_namelen;
687 dprintk("<-- nfs_probe_fsinfo() = 0\n");
691 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
696 * Copy useful information when duplicating a server record
698 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
700 target->flags = source->flags;
701 target->acregmin = source->acregmin;
702 target->acregmax = source->acregmax;
703 target->acdirmin = source->acdirmin;
704 target->acdirmax = source->acdirmax;
705 target->caps = source->caps;
709 * Allocate and initialise a server record
711 static struct nfs_server *nfs_alloc_server(void)
713 struct nfs_server *server;
715 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
719 server->client = server->client_acl = ERR_PTR(-EINVAL);
721 /* Zero out the NFS state stuff */
722 INIT_LIST_HEAD(&server->client_link);
723 INIT_LIST_HEAD(&server->master_link);
725 server->io_stats = nfs_alloc_iostats();
726 if (!server->io_stats) {
735 * Free up a server record
737 void nfs_free_server(struct nfs_server *server)
739 dprintk("--> nfs_free_server()\n");
741 spin_lock(&nfs_client_lock);
742 list_del(&server->client_link);
743 list_del(&server->master_link);
744 spin_unlock(&nfs_client_lock);
746 if (server->destroy != NULL)
747 server->destroy(server);
748 if (!IS_ERR(server->client))
749 rpc_shutdown_client(server->client);
751 nfs_put_client(server->nfs_client);
753 nfs_free_iostats(server->io_stats);
755 nfs_release_automount_timer();
756 dprintk("<-- nfs_free_server()\n");
760 * Create a version 2 or 3 volume record
761 * - keyed on server and FSID
763 struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
764 struct nfs_fh *mntfh)
766 struct nfs_server *server;
767 struct nfs_fattr fattr;
770 server = nfs_alloc_server();
772 return ERR_PTR(-ENOMEM);
774 /* Get a client representation */
775 error = nfs_init_server(server, data);
779 BUG_ON(!server->nfs_client);
780 BUG_ON(!server->nfs_client->rpc_ops);
781 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
783 /* Probe the root fh to retrieve its FSID */
784 error = nfs_probe_fsinfo(server, mntfh, &fattr);
787 if (server->nfs_client->rpc_ops->version == 3) {
788 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
789 server->namelen = NFS3_MAXNAMLEN;
790 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
791 server->caps |= NFS_CAP_READDIRPLUS;
793 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
794 server->namelen = NFS2_MAXNAMLEN;
797 if (!(fattr.valid & NFS_ATTR_FATTR)) {
798 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
800 dprintk("nfs_create_server: getattr error = %d\n", -error);
804 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
806 dprintk("Server FSID: %llx:%llx\n",
807 (unsigned long long) server->fsid.major,
808 (unsigned long long) server->fsid.minor);
810 BUG_ON(!server->nfs_client);
811 BUG_ON(!server->nfs_client->rpc_ops);
812 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
814 spin_lock(&nfs_client_lock);
815 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
816 list_add_tail(&server->master_link, &nfs_volume_list);
817 spin_unlock(&nfs_client_lock);
819 server->mount_time = jiffies;
823 nfs_free_server(server);
824 return ERR_PTR(error);
829 * Initialise an NFS4 client record
831 static int nfs4_init_client(struct nfs_client *clp,
832 int proto, int timeo, int retrans,
834 rpc_authflavor_t authflavour)
838 if (clp->cl_cons_state == NFS_CS_READY) {
839 /* the client is initialised already */
840 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
844 /* Check NFS protocol revision and initialize RPC op vector */
845 clp->rpc_ops = &nfs_v4_clientops;
847 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
848 RPC_CLNT_CREATE_DISCRTRY);
851 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
853 error = nfs_idmap_new(clp);
855 dprintk("%s: failed to create idmapper. Error = %d\n",
856 __FUNCTION__, error);
859 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
861 nfs_mark_client_ready(clp, NFS_CS_READY);
865 nfs_mark_client_ready(clp, error);
866 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
871 * Set up an NFS4 client
873 static int nfs4_set_client(struct nfs_server *server,
874 const char *hostname, const struct sockaddr_in *addr,
876 rpc_authflavor_t authflavour,
877 int proto, int timeo, int retrans)
879 struct nfs_client *clp;
882 dprintk("--> nfs4_set_client()\n");
884 /* Allocate or find a client reference we can use */
885 clp = nfs_get_client(hostname, addr, 4);
887 error = PTR_ERR(clp);
890 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
894 server->nfs_client = clp;
895 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
901 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
906 * Create a version 4 volume record
908 static int nfs4_init_server(struct nfs_server *server,
909 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
913 dprintk("--> nfs4_init_server()\n");
915 /* Initialise the client representation from the mount data */
916 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
917 server->caps |= NFS_CAP_ATOMIC_OPEN;
920 server->rsize = nfs_block_size(data->rsize, NULL);
922 server->wsize = nfs_block_size(data->wsize, NULL);
924 server->acregmin = data->acregmin * HZ;
925 server->acregmax = data->acregmax * HZ;
926 server->acdirmin = data->acdirmin * HZ;
927 server->acdirmax = data->acdirmax * HZ;
929 error = nfs_init_server_rpcclient(server, authflavour);
932 dprintk("<-- nfs4_init_server() = %d\n", error);
937 * Create a version 4 volume record
938 * - keyed on server and FSID
940 struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
941 const char *hostname,
942 const struct sockaddr_in *addr,
945 rpc_authflavor_t authflavour,
946 struct nfs_fh *mntfh)
948 struct nfs_fattr fattr;
949 struct nfs_server *server;
952 dprintk("--> nfs4_create_server()\n");
954 server = nfs_alloc_server();
956 return ERR_PTR(-ENOMEM);
958 /* Get a client record */
959 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
960 data->proto, data->timeo, data->retrans);
964 /* set up the general RPC client */
965 error = nfs4_init_server(server, data, authflavour);
969 BUG_ON(!server->nfs_client);
970 BUG_ON(!server->nfs_client->rpc_ops);
971 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
973 /* Probe the root fh to retrieve its FSID */
974 error = nfs4_path_walk(server, mntfh, mntpath);
978 dprintk("Server FSID: %llx:%llx\n",
979 (unsigned long long) server->fsid.major,
980 (unsigned long long) server->fsid.minor);
981 dprintk("Mount FH: %d\n", mntfh->size);
983 error = nfs_probe_fsinfo(server, mntfh, &fattr);
987 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
988 server->namelen = NFS4_MAXNAMLEN;
990 BUG_ON(!server->nfs_client);
991 BUG_ON(!server->nfs_client->rpc_ops);
992 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
994 spin_lock(&nfs_client_lock);
995 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
996 list_add_tail(&server->master_link, &nfs_volume_list);
997 spin_unlock(&nfs_client_lock);
999 server->mount_time = jiffies;
1000 dprintk("<-- nfs4_create_server() = %p\n", server);
1004 nfs_free_server(server);
1005 dprintk("<-- nfs4_create_server() = error %d\n", error);
1006 return ERR_PTR(error);
1010 * Create an NFS4 referral server record
1012 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1013 struct nfs_fh *mntfh)
1015 struct nfs_client *parent_client;
1016 struct nfs_server *server, *parent_server;
1017 struct nfs_fattr fattr;
1020 dprintk("--> nfs4_create_referral_server()\n");
1022 server = nfs_alloc_server();
1024 return ERR_PTR(-ENOMEM);
1026 parent_server = NFS_SB(data->sb);
1027 parent_client = parent_server->nfs_client;
1029 /* Get a client representation.
1030 * Note: NFSv4 always uses TCP, */
1031 error = nfs4_set_client(server, data->hostname, data->addr,
1032 parent_client->cl_ipaddr,
1034 parent_server->client->cl_xprt->prot,
1035 parent_client->retrans_timeo,
1036 parent_client->retrans_count);
1040 /* Initialise the client representation from the parent server */
1041 nfs_server_copy_userdata(server, parent_server);
1042 server->caps |= NFS_CAP_ATOMIC_OPEN;
1044 error = nfs_init_server_rpcclient(server, data->authflavor);
1048 BUG_ON(!server->nfs_client);
1049 BUG_ON(!server->nfs_client->rpc_ops);
1050 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1052 /* Probe the root fh to retrieve its FSID and filehandle */
1053 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1057 /* probe the filesystem info for this server filesystem */
1058 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1062 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1063 server->namelen = NFS4_MAXNAMLEN;
1065 dprintk("Referral FSID: %llx:%llx\n",
1066 (unsigned long long) server->fsid.major,
1067 (unsigned long long) server->fsid.minor);
1069 spin_lock(&nfs_client_lock);
1070 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1071 list_add_tail(&server->master_link, &nfs_volume_list);
1072 spin_unlock(&nfs_client_lock);
1074 server->mount_time = jiffies;
1076 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1080 nfs_free_server(server);
1081 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1082 return ERR_PTR(error);
1085 #endif /* CONFIG_NFS_V4 */
1088 * Clone an NFS2, NFS3 or NFS4 server record
1090 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1092 struct nfs_fattr *fattr)
1094 struct nfs_server *server;
1095 struct nfs_fattr fattr_fsinfo;
1098 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1099 (unsigned long long) fattr->fsid.major,
1100 (unsigned long long) fattr->fsid.minor);
1102 server = nfs_alloc_server();
1104 return ERR_PTR(-ENOMEM);
1106 /* Copy data from the source */
1107 server->nfs_client = source->nfs_client;
1108 atomic_inc(&server->nfs_client->cl_count);
1109 nfs_server_copy_userdata(server, source);
1111 server->fsid = fattr->fsid;
1113 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1115 goto out_free_server;
1116 if (!IS_ERR(source->client_acl))
1117 nfs_init_server_aclclient(server);
1119 /* probe the filesystem info for this server filesystem */
1120 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1122 goto out_free_server;
1124 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1125 server->namelen = NFS4_MAXNAMLEN;
1127 dprintk("Cloned FSID: %llx:%llx\n",
1128 (unsigned long long) server->fsid.major,
1129 (unsigned long long) server->fsid.minor);
1131 error = nfs_start_lockd(server);
1133 goto out_free_server;
1135 spin_lock(&nfs_client_lock);
1136 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1137 list_add_tail(&server->master_link, &nfs_volume_list);
1138 spin_unlock(&nfs_client_lock);
1140 server->mount_time = jiffies;
1142 dprintk("<-- nfs_clone_server() = %p\n", server);
1146 nfs_free_server(server);
1147 dprintk("<-- nfs_clone_server() = error %d\n", error);
1148 return ERR_PTR(error);
1151 #ifdef CONFIG_PROC_FS
1152 static struct proc_dir_entry *proc_fs_nfs;
1154 static int nfs_server_list_open(struct inode *inode, struct file *file);
1155 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1156 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1157 static void nfs_server_list_stop(struct seq_file *p, void *v);
1158 static int nfs_server_list_show(struct seq_file *m, void *v);
1160 static struct seq_operations nfs_server_list_ops = {
1161 .start = nfs_server_list_start,
1162 .next = nfs_server_list_next,
1163 .stop = nfs_server_list_stop,
1164 .show = nfs_server_list_show,
1167 static const struct file_operations nfs_server_list_fops = {
1168 .open = nfs_server_list_open,
1170 .llseek = seq_lseek,
1171 .release = seq_release,
1174 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1175 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1176 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1177 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1178 static int nfs_volume_list_show(struct seq_file *m, void *v);
1180 static struct seq_operations nfs_volume_list_ops = {
1181 .start = nfs_volume_list_start,
1182 .next = nfs_volume_list_next,
1183 .stop = nfs_volume_list_stop,
1184 .show = nfs_volume_list_show,
1187 static const struct file_operations nfs_volume_list_fops = {
1188 .open = nfs_volume_list_open,
1190 .llseek = seq_lseek,
1191 .release = seq_release,
1195 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1198 static int nfs_server_list_open(struct inode *inode, struct file *file)
1203 ret = seq_open(file, &nfs_server_list_ops);
1207 m = file->private_data;
1208 m->private = PDE(inode)->data;
1214 * set up the iterator to start reading from the server list and return the first item
1216 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1218 /* lock the list against modification */
1219 spin_lock(&nfs_client_lock);
1220 return seq_list_start_head(&nfs_client_list, *_pos);
1224 * move to next server
1226 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1228 return seq_list_next(v, &nfs_client_list, pos);
1232 * clean up after reading from the transports list
1234 static void nfs_server_list_stop(struct seq_file *p, void *v)
1236 spin_unlock(&nfs_client_lock);
1240 * display a header line followed by a load of call lines
1242 static int nfs_server_list_show(struct seq_file *m, void *v)
1244 struct nfs_client *clp;
1246 /* display header on line 1 */
1247 if (v == &nfs_client_list) {
1248 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1252 /* display one transport per line on subsequent lines */
1253 clp = list_entry(v, struct nfs_client, cl_share_link);
1255 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1257 NIPQUAD(clp->cl_addr.sin_addr),
1258 ntohs(clp->cl_addr.sin_port),
1259 atomic_read(&clp->cl_count),
1266 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1268 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1273 ret = seq_open(file, &nfs_volume_list_ops);
1277 m = file->private_data;
1278 m->private = PDE(inode)->data;
1284 * set up the iterator to start reading from the volume list and return the first item
1286 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1288 /* lock the list against modification */
1289 spin_lock(&nfs_client_lock);
1290 return seq_list_start_head(&nfs_volume_list, *_pos);
1294 * move to next volume
1296 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1298 return seq_list_next(v, &nfs_volume_list, pos);
1302 * clean up after reading from the transports list
1304 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1306 spin_unlock(&nfs_client_lock);
1310 * display a header line followed by a load of call lines
1312 static int nfs_volume_list_show(struct seq_file *m, void *v)
1314 struct nfs_server *server;
1315 struct nfs_client *clp;
1316 char dev[8], fsid[17];
1318 /* display header on line 1 */
1319 if (v == &nfs_volume_list) {
1320 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1323 /* display one transport per line on subsequent lines */
1324 server = list_entry(v, struct nfs_server, master_link);
1325 clp = server->nfs_client;
1327 snprintf(dev, 8, "%u:%u",
1328 MAJOR(server->s_dev), MINOR(server->s_dev));
1330 snprintf(fsid, 17, "%llx:%llx",
1331 (unsigned long long) server->fsid.major,
1332 (unsigned long long) server->fsid.minor);
1334 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1336 NIPQUAD(clp->cl_addr.sin_addr),
1337 ntohs(clp->cl_addr.sin_port),
1345 * initialise the /proc/fs/nfsfs/ directory
1347 int __init nfs_fs_proc_init(void)
1349 struct proc_dir_entry *p;
1351 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1355 proc_fs_nfs->owner = THIS_MODULE;
1357 /* a file of servers with which we're dealing */
1358 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1362 p->proc_fops = &nfs_server_list_fops;
1363 p->owner = THIS_MODULE;
1365 /* a file of volumes that we have mounted */
1366 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1370 p->proc_fops = &nfs_volume_list_fops;
1371 p->owner = THIS_MODULE;
1375 remove_proc_entry("servers", proc_fs_nfs);
1377 remove_proc_entry("nfsfs", proc_root_fs);
1383 * clean up the /proc/fs/nfsfs/ directory
1385 void nfs_fs_proc_exit(void)
1387 remove_proc_entry("volumes", proc_fs_nfs);
1388 remove_proc_entry("servers", proc_fs_nfs);
1389 remove_proc_entry("nfsfs", proc_root_fs);
1392 #endif /* CONFIG_PROC_FS */