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/config.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
17 #include <linux/time.h>
18 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/stat.h>
22 #include <linux/errno.h>
23 #include <linux/unistd.h>
24 #include <linux/sunrpc/clnt.h>
25 #include <linux/sunrpc/stats.h>
26 #include <linux/sunrpc/metrics.h>
27 #include <linux/nfs_fs.h>
28 #include <linux/nfs_mount.h>
29 #include <linux/nfs4_mount.h>
30 #include <linux/lockd/bind.h>
31 #include <linux/smp_lock.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 */
97 * Allocate a shared client record
99 * Since these are allocated/deallocated very rarely, we don't
100 * bother putting them in a slab cache...
102 static struct nfs_client
*nfs_alloc_client(const char *hostname
,
103 const struct sockaddr_in
*addr
,
106 struct nfs_client
*clp
;
109 if ((clp
= kzalloc(sizeof(*clp
), GFP_KERNEL
)) == NULL
)
114 dprintk("%s: couldn't start rpciod! Error = %d\n",
115 __FUNCTION__
, error
);
118 __set_bit(NFS_CS_RPCIOD
, &clp
->cl_res_state
);
120 if (nfsversion
== 4) {
121 if (nfs_callback_up() < 0)
123 __set_bit(NFS_CS_CALLBACK
, &clp
->cl_res_state
);
126 atomic_set(&clp
->cl_count
, 1);
127 clp
->cl_cons_state
= NFS_CS_INITING
;
129 clp
->cl_nfsversion
= nfsversion
;
130 memcpy(&clp
->cl_addr
, addr
, sizeof(clp
->cl_addr
));
133 clp
->cl_hostname
= kstrdup(hostname
, GFP_KERNEL
);
134 if (!clp
->cl_hostname
)
138 INIT_LIST_HEAD(&clp
->cl_superblocks
);
139 clp
->cl_rpcclient
= ERR_PTR(-EINVAL
);
142 init_rwsem(&clp
->cl_sem
);
143 INIT_LIST_HEAD(&clp
->cl_delegations
);
144 INIT_LIST_HEAD(&clp
->cl_state_owners
);
145 INIT_LIST_HEAD(&clp
->cl_unused
);
146 spin_lock_init(&clp
->cl_lock
);
147 INIT_WORK(&clp
->cl_renewd
, nfs4_renew_state
, clp
);
148 rpc_init_wait_queue(&clp
->cl_rpcwaitq
, "NFS client");
149 clp
->cl_boot_time
= CURRENT_TIME
;
150 clp
->cl_state
= 1 << NFS4CLNT_LEASE_EXPIRED
;
156 if (__test_and_clear_bit(NFS_CS_CALLBACK
, &clp
->cl_res_state
))
160 __clear_bit(NFS_CS_RPCIOD
, &clp
->cl_res_state
);
168 * Destroy a shared client record
170 static void nfs_free_client(struct nfs_client
*clp
)
172 dprintk("--> nfs_free_client(%d)\n", clp
->cl_nfsversion
);
175 if (__test_and_clear_bit(NFS_CS_IDMAP
, &clp
->cl_res_state
)) {
176 while (!list_empty(&clp
->cl_unused
)) {
177 struct nfs4_state_owner
*sp
;
179 sp
= list_entry(clp
->cl_unused
.next
,
180 struct nfs4_state_owner
,
182 list_del(&sp
->so_list
);
185 BUG_ON(!list_empty(&clp
->cl_state_owners
));
186 nfs_idmap_delete(clp
);
190 /* -EIO all pending I/O */
191 if (!IS_ERR(clp
->cl_rpcclient
))
192 rpc_shutdown_client(clp
->cl_rpcclient
);
194 if (__test_and_clear_bit(NFS_CS_CALLBACK
, &clp
->cl_res_state
))
197 if (__test_and_clear_bit(NFS_CS_RPCIOD
, &clp
->cl_res_state
))
200 kfree(clp
->cl_hostname
);
203 dprintk("<-- nfs_free_client()\n");
207 * Release a reference to a shared client record
209 void nfs_put_client(struct nfs_client
*clp
)
214 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp
->cl_count
));
216 if (atomic_dec_and_lock(&clp
->cl_count
, &nfs_client_lock
)) {
217 list_del(&clp
->cl_share_link
);
218 spin_unlock(&nfs_client_lock
);
220 BUG_ON(!list_empty(&clp
->cl_superblocks
));
222 nfs_free_client(clp
);
227 * Find a client by address
228 * - caller must hold nfs_client_lock
230 static struct nfs_client
*__nfs_find_client(const struct sockaddr_in
*addr
, int nfsversion
)
232 struct nfs_client
*clp
;
234 list_for_each_entry(clp
, &nfs_client_list
, cl_share_link
) {
235 /* Different NFS versions cannot share the same nfs_client */
236 if (clp
->cl_nfsversion
!= nfsversion
)
239 if (memcmp(&clp
->cl_addr
.sin_addr
, &addr
->sin_addr
,
240 sizeof(clp
->cl_addr
.sin_addr
)) != 0)
243 if (clp
->cl_addr
.sin_port
== addr
->sin_port
)
250 atomic_inc(&clp
->cl_count
);
255 * Find a client by IP address and protocol version
256 * - returns NULL if no such client
258 struct nfs_client
*nfs_find_client(const struct sockaddr_in
*addr
, int nfsversion
)
260 struct nfs_client
*clp
;
262 spin_lock(&nfs_client_lock
);
263 clp
= __nfs_find_client(addr
, nfsversion
);
264 spin_unlock(&nfs_client_lock
);
266 BUG_ON(clp
->cl_cons_state
== 0);
272 * Look up a client by IP address and protocol version
273 * - creates a new record if one doesn't yet exist
275 static struct nfs_client
*nfs_get_client(const char *hostname
,
276 const struct sockaddr_in
*addr
,
279 struct nfs_client
*clp
, *new = NULL
;
282 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT
":%d,%d)\n",
283 hostname
?: "", NIPQUAD(addr
->sin_addr
),
284 addr
->sin_port
, nfsversion
);
286 /* see if the client already exists */
288 spin_lock(&nfs_client_lock
);
290 clp
= __nfs_find_client(addr
, nfsversion
);
296 spin_unlock(&nfs_client_lock
);
298 new = nfs_alloc_client(hostname
, addr
, nfsversion
);
301 return ERR_PTR(-ENOMEM
);
303 /* install a new client and return with it unready */
306 list_add(&clp
->cl_share_link
, &nfs_client_list
);
307 spin_unlock(&nfs_client_lock
);
308 dprintk("--> nfs_get_client() = %p [new]\n", clp
);
311 /* found an existing client
312 * - make sure it's ready before returning
315 spin_unlock(&nfs_client_lock
);
318 nfs_free_client(new);
320 if (clp
->cl_cons_state
== NFS_CS_INITING
) {
321 DECLARE_WAITQUEUE(myself
, current
);
323 add_wait_queue(&nfs_client_active_wq
, &myself
);
326 set_current_state(TASK_INTERRUPTIBLE
);
327 if (signal_pending(current
) ||
328 clp
->cl_cons_state
> NFS_CS_READY
)
333 remove_wait_queue(&nfs_client_active_wq
, &myself
);
335 if (signal_pending(current
)) {
337 return ERR_PTR(-ERESTARTSYS
);
341 if (clp
->cl_cons_state
< NFS_CS_READY
) {
342 error
= clp
->cl_cons_state
;
344 return ERR_PTR(error
);
347 BUG_ON(clp
->cl_cons_state
!= NFS_CS_READY
);
349 dprintk("--> nfs_get_client() = %p [share]\n", clp
);
354 * Mark a server as ready or failed
356 static void nfs_mark_client_ready(struct nfs_client
*clp
, int state
)
358 clp
->cl_cons_state
= state
;
359 wake_up_all(&nfs_client_active_wq
);
363 * Initialise the timeout values for a connection
365 static void nfs_init_timeout_values(struct rpc_timeout
*to
, int proto
,
366 unsigned int timeo
, unsigned int retrans
)
368 to
->to_initval
= timeo
* HZ
/ 10;
369 to
->to_retries
= retrans
;
376 to
->to_initval
= 60 * HZ
;
377 if (to
->to_initval
> NFS_MAX_TCP_TIMEOUT
)
378 to
->to_initval
= NFS_MAX_TCP_TIMEOUT
;
379 to
->to_increment
= to
->to_initval
;
380 to
->to_maxval
= to
->to_initval
+ (to
->to_increment
* to
->to_retries
);
381 to
->to_exponential
= 0;
386 to
->to_initval
= 11 * HZ
/ 10;
387 if (to
->to_initval
> NFS_MAX_UDP_TIMEOUT
)
388 to
->to_initval
= NFS_MAX_UDP_TIMEOUT
;
389 to
->to_maxval
= NFS_MAX_UDP_TIMEOUT
;
390 to
->to_exponential
= 1;
396 * Create an RPC client handle
398 static int nfs_create_rpc_client(struct nfs_client
*clp
, int proto
,
400 unsigned int retrans
,
401 rpc_authflavor_t flavor
)
403 struct rpc_timeout timeparms
;
404 struct rpc_clnt
*clnt
= NULL
;
405 struct rpc_create_args args
= {
407 .address
= (struct sockaddr
*)&clp
->cl_addr
,
408 .addrsize
= sizeof(clp
->cl_addr
),
409 .timeout
= &timeparms
,
410 .servername
= clp
->cl_hostname
,
411 .program
= &nfs_program
,
412 .version
= clp
->rpc_ops
->version
,
413 .authflavor
= flavor
,
416 if (!IS_ERR(clp
->cl_rpcclient
))
419 nfs_init_timeout_values(&timeparms
, proto
, timeo
, retrans
);
420 clp
->retrans_timeo
= timeparms
.to_initval
;
421 clp
->retrans_count
= timeparms
.to_retries
;
423 clnt
= rpc_create(&args
);
425 dprintk("%s: cannot create RPC client. Error = %ld\n",
426 __FUNCTION__
, PTR_ERR(clnt
));
427 return PTR_ERR(clnt
);
430 clp
->cl_rpcclient
= clnt
;
435 * Version 2 or 3 client destruction
437 static void nfs_destroy_server(struct nfs_server
*server
)
439 if (!IS_ERR(server
->client_acl
))
440 rpc_shutdown_client(server
->client_acl
);
442 if (!(server
->flags
& NFS_MOUNT_NONLM
))
443 lockd_down(); /* release rpc.lockd */
447 * Version 2 or 3 lockd setup
449 static int nfs_start_lockd(struct nfs_server
*server
)
453 if (server
->nfs_client
->cl_nfsversion
> 3)
455 if (server
->flags
& NFS_MOUNT_NONLM
)
459 server
->flags
|= NFS_MOUNT_NONLM
;
461 server
->destroy
= nfs_destroy_server
;
467 * Initialise an NFSv3 ACL client connection
469 #ifdef CONFIG_NFS_V3_ACL
470 static void nfs_init_server_aclclient(struct nfs_server
*server
)
472 if (server
->nfs_client
->cl_nfsversion
!= 3)
474 if (server
->flags
& NFS_MOUNT_NOACL
)
477 server
->client_acl
= rpc_bind_new_program(server
->client
, &nfsacl_program
, 3);
478 if (IS_ERR(server
->client_acl
))
481 /* No errors! Assume that Sun nfsacls are supported */
482 server
->caps
|= NFS_CAP_ACLS
;
486 server
->caps
&= ~NFS_CAP_ACLS
;
489 static inline void nfs_init_server_aclclient(struct nfs_server
*server
)
491 server
->flags
&= ~NFS_MOUNT_NOACL
;
492 server
->caps
&= ~NFS_CAP_ACLS
;
497 * Create a general RPC client
499 static int nfs_init_server_rpcclient(struct nfs_server
*server
, rpc_authflavor_t pseudoflavour
)
501 struct nfs_client
*clp
= server
->nfs_client
;
503 server
->client
= rpc_clone_client(clp
->cl_rpcclient
);
504 if (IS_ERR(server
->client
)) {
505 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__
);
506 return PTR_ERR(server
->client
);
509 if (pseudoflavour
!= clp
->cl_rpcclient
->cl_auth
->au_flavor
) {
510 struct rpc_auth
*auth
;
512 auth
= rpcauth_create(pseudoflavour
, server
->client
);
514 dprintk("%s: couldn't create credcache!\n", __FUNCTION__
);
515 return PTR_ERR(auth
);
518 server
->client
->cl_softrtry
= 0;
519 if (server
->flags
& NFS_MOUNT_SOFT
)
520 server
->client
->cl_softrtry
= 1;
522 server
->client
->cl_intr
= 0;
523 if (server
->flags
& NFS4_MOUNT_INTR
)
524 server
->client
->cl_intr
= 1;
530 * Initialise an NFS2 or NFS3 client
532 static int nfs_init_client(struct nfs_client
*clp
, const struct nfs_mount_data
*data
)
534 int proto
= (data
->flags
& NFS_MOUNT_TCP
) ? IPPROTO_TCP
: IPPROTO_UDP
;
537 if (clp
->cl_cons_state
== NFS_CS_READY
) {
538 /* the client is already initialised */
539 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp
);
543 /* Check NFS protocol revision and initialize RPC op vector */
544 clp
->rpc_ops
= &nfs_v2_clientops
;
546 if (clp
->cl_nfsversion
== 3)
547 clp
->rpc_ops
= &nfs_v3_clientops
;
550 * Create a client RPC handle for doing FSSTAT with UNIX auth only
551 * - RFC 2623, sec 2.3.2
553 error
= nfs_create_rpc_client(clp
, proto
, data
->timeo
, data
->retrans
,
557 nfs_mark_client_ready(clp
, NFS_CS_READY
);
561 nfs_mark_client_ready(clp
, error
);
562 dprintk("<-- nfs_init_client() = xerror %d\n", error
);
567 * Create a version 2 or 3 client
569 static int nfs_init_server(struct nfs_server
*server
, const struct nfs_mount_data
*data
)
571 struct nfs_client
*clp
;
572 int error
, nfsvers
= 2;
574 dprintk("--> nfs_init_server()\n");
577 if (data
->flags
& NFS_MOUNT_VER3
)
581 /* Allocate or find a client reference we can use */
582 clp
= nfs_get_client(data
->hostname
, &data
->addr
, nfsvers
);
584 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp
));
588 error
= nfs_init_client(clp
, data
);
592 server
->nfs_client
= clp
;
594 /* Initialise the client representation from the mount data */
595 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
598 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
600 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
602 server
->acregmin
= data
->acregmin
* HZ
;
603 server
->acregmax
= data
->acregmax
* HZ
;
604 server
->acdirmin
= data
->acdirmin
* HZ
;
605 server
->acdirmax
= data
->acdirmax
* HZ
;
607 /* Start lockd here, before we might error out */
608 error
= nfs_start_lockd(server
);
612 error
= nfs_init_server_rpcclient(server
, data
->pseudoflavor
);
616 server
->namelen
= data
->namlen
;
617 /* Create a client RPC handle for the NFSv3 ACL management interface */
618 nfs_init_server_aclclient(server
);
619 if (clp
->cl_nfsversion
== 3) {
620 if (server
->namelen
== 0 || server
->namelen
> NFS3_MAXNAMLEN
)
621 server
->namelen
= NFS3_MAXNAMLEN
;
622 server
->caps
|= NFS_CAP_READDIRPLUS
;
624 if (server
->namelen
== 0 || server
->namelen
> NFS2_MAXNAMLEN
)
625 server
->namelen
= NFS2_MAXNAMLEN
;
628 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp
);
632 server
->nfs_client
= NULL
;
634 dprintk("<-- nfs_init_server() = xerror %d\n", error
);
639 * Load up the server record from information gained in an fsinfo record
641 static void nfs_server_set_fsinfo(struct nfs_server
*server
, struct nfs_fsinfo
*fsinfo
)
643 unsigned long max_rpc_payload
;
645 /* Work out a lot of parameters */
646 if (server
->rsize
== 0)
647 server
->rsize
= nfs_block_size(fsinfo
->rtpref
, NULL
);
648 if (server
->wsize
== 0)
649 server
->wsize
= nfs_block_size(fsinfo
->wtpref
, NULL
);
651 if (fsinfo
->rtmax
>= 512 && server
->rsize
> fsinfo
->rtmax
)
652 server
->rsize
= nfs_block_size(fsinfo
->rtmax
, NULL
);
653 if (fsinfo
->wtmax
>= 512 && server
->wsize
> fsinfo
->wtmax
)
654 server
->wsize
= nfs_block_size(fsinfo
->wtmax
, NULL
);
656 max_rpc_payload
= nfs_block_size(rpc_max_payload(server
->client
), NULL
);
657 if (server
->rsize
> max_rpc_payload
)
658 server
->rsize
= max_rpc_payload
;
659 if (server
->rsize
> NFS_MAX_FILE_IO_SIZE
)
660 server
->rsize
= NFS_MAX_FILE_IO_SIZE
;
661 server
->rpages
= (server
->rsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
662 server
->backing_dev_info
.ra_pages
= server
->rpages
* NFS_MAX_READAHEAD
;
664 if (server
->wsize
> max_rpc_payload
)
665 server
->wsize
= max_rpc_payload
;
666 if (server
->wsize
> NFS_MAX_FILE_IO_SIZE
)
667 server
->wsize
= NFS_MAX_FILE_IO_SIZE
;
668 server
->wpages
= (server
->wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
669 server
->wtmult
= nfs_block_bits(fsinfo
->wtmult
, NULL
);
671 server
->dtsize
= nfs_block_size(fsinfo
->dtpref
, NULL
);
672 if (server
->dtsize
> PAGE_CACHE_SIZE
)
673 server
->dtsize
= PAGE_CACHE_SIZE
;
674 if (server
->dtsize
> server
->rsize
)
675 server
->dtsize
= server
->rsize
;
677 if (server
->flags
& NFS_MOUNT_NOAC
) {
678 server
->acregmin
= server
->acregmax
= 0;
679 server
->acdirmin
= server
->acdirmax
= 0;
682 server
->maxfilesize
= fsinfo
->maxfilesize
;
684 /* We're airborne Set socket buffersize */
685 rpc_setbufsize(server
->client
, server
->wsize
+ 100, server
->rsize
+ 100);
689 * Probe filesystem information, including the FSID on v2/v3
691 static int nfs_probe_fsinfo(struct nfs_server
*server
, struct nfs_fh
*mntfh
, struct nfs_fattr
*fattr
)
693 struct nfs_fsinfo fsinfo
;
694 struct nfs_client
*clp
= server
->nfs_client
;
697 dprintk("--> nfs_probe_fsinfo()\n");
699 if (clp
->rpc_ops
->set_capabilities
!= NULL
) {
700 error
= clp
->rpc_ops
->set_capabilities(server
, mntfh
);
705 fsinfo
.fattr
= fattr
;
706 nfs_fattr_init(fattr
);
707 error
= clp
->rpc_ops
->fsinfo(server
, mntfh
, &fsinfo
);
711 nfs_server_set_fsinfo(server
, &fsinfo
);
713 /* Get some general file system info */
714 if (server
->namelen
== 0) {
715 struct nfs_pathconf pathinfo
;
717 pathinfo
.fattr
= fattr
;
718 nfs_fattr_init(fattr
);
720 if (clp
->rpc_ops
->pathconf(server
, mntfh
, &pathinfo
) >= 0)
721 server
->namelen
= pathinfo
.max_namelen
;
724 dprintk("<-- nfs_probe_fsinfo() = 0\n");
728 dprintk("nfs_probe_fsinfo: error = %d\n", -error
);
733 * Copy useful information when duplicating a server record
735 static void nfs_server_copy_userdata(struct nfs_server
*target
, struct nfs_server
*source
)
737 target
->flags
= source
->flags
;
738 target
->acregmin
= source
->acregmin
;
739 target
->acregmax
= source
->acregmax
;
740 target
->acdirmin
= source
->acdirmin
;
741 target
->acdirmax
= source
->acdirmax
;
742 target
->caps
= source
->caps
;
746 * Allocate and initialise a server record
748 static struct nfs_server
*nfs_alloc_server(void)
750 struct nfs_server
*server
;
752 server
= kzalloc(sizeof(struct nfs_server
), GFP_KERNEL
);
756 server
->client
= server
->client_acl
= ERR_PTR(-EINVAL
);
758 /* Zero out the NFS state stuff */
759 INIT_LIST_HEAD(&server
->client_link
);
760 INIT_LIST_HEAD(&server
->master_link
);
762 server
->io_stats
= nfs_alloc_iostats();
763 if (!server
->io_stats
) {
772 * Free up a server record
774 void nfs_free_server(struct nfs_server
*server
)
776 dprintk("--> nfs_free_server()\n");
778 spin_lock(&nfs_client_lock
);
779 list_del(&server
->client_link
);
780 list_del(&server
->master_link
);
781 spin_unlock(&nfs_client_lock
);
783 if (server
->destroy
!= NULL
)
784 server
->destroy(server
);
785 if (!IS_ERR(server
->client
))
786 rpc_shutdown_client(server
->client
);
788 nfs_put_client(server
->nfs_client
);
790 nfs_free_iostats(server
->io_stats
);
792 nfs_release_automount_timer();
793 dprintk("<-- nfs_free_server()\n");
797 * Create a version 2 or 3 volume record
798 * - keyed on server and FSID
800 struct nfs_server
*nfs_create_server(const struct nfs_mount_data
*data
,
801 struct nfs_fh
*mntfh
)
803 struct nfs_server
*server
;
804 struct nfs_fattr fattr
;
807 server
= nfs_alloc_server();
809 return ERR_PTR(-ENOMEM
);
811 /* Get a client representation */
812 error
= nfs_init_server(server
, data
);
816 BUG_ON(!server
->nfs_client
);
817 BUG_ON(!server
->nfs_client
->rpc_ops
);
818 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
820 /* Probe the root fh to retrieve its FSID */
821 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
824 if (!(fattr
.valid
& NFS_ATTR_FATTR
)) {
825 error
= server
->nfs_client
->rpc_ops
->getattr(server
, mntfh
, &fattr
);
827 dprintk("nfs_create_server: getattr error = %d\n", -error
);
831 memcpy(&server
->fsid
, &fattr
.fsid
, sizeof(server
->fsid
));
833 dprintk("Server FSID: %llx:%llx\n", server
->fsid
.major
, server
->fsid
.minor
);
835 BUG_ON(!server
->nfs_client
);
836 BUG_ON(!server
->nfs_client
->rpc_ops
);
837 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
839 spin_lock(&nfs_client_lock
);
840 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
841 list_add_tail(&server
->master_link
, &nfs_volume_list
);
842 spin_unlock(&nfs_client_lock
);
844 server
->mount_time
= jiffies
;
848 nfs_free_server(server
);
849 return ERR_PTR(error
);
854 * Initialise an NFS4 client record
856 static int nfs4_init_client(struct nfs_client
*clp
,
857 int proto
, int timeo
, int retrans
,
858 rpc_authflavor_t authflavour
)
862 if (clp
->cl_cons_state
== NFS_CS_READY
) {
863 /* the client is initialised already */
864 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp
);
868 /* Check NFS protocol revision and initialize RPC op vector */
869 clp
->rpc_ops
= &nfs_v4_clientops
;
871 error
= nfs_create_rpc_client(clp
, proto
, timeo
, retrans
, authflavour
);
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
,
897 rpc_authflavor_t authflavour
,
898 int proto
, int timeo
, int retrans
)
900 struct nfs_client
*clp
;
903 dprintk("--> nfs4_set_client()\n");
905 /* Allocate or find a client reference we can use */
906 clp
= nfs_get_client(hostname
, addr
, 4);
908 error
= PTR_ERR(clp
);
911 error
= nfs4_init_client(clp
, proto
, timeo
, retrans
, authflavour
);
915 server
->nfs_client
= clp
;
916 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp
);
922 dprintk("<-- nfs4_set_client() = xerror %d\n", error
);
927 * Create a version 4 volume record
929 static int nfs4_init_server(struct nfs_server
*server
,
930 const struct nfs4_mount_data
*data
, rpc_authflavor_t authflavour
)
934 dprintk("--> nfs4_init_server()\n");
936 /* Initialise the client representation from the mount data */
937 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
938 server
->caps
|= NFS_CAP_ATOMIC_OPEN
;
941 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
943 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
945 server
->acregmin
= data
->acregmin
* HZ
;
946 server
->acregmax
= data
->acregmax
* HZ
;
947 server
->acdirmin
= data
->acdirmin
* HZ
;
948 server
->acdirmax
= data
->acdirmax
* HZ
;
950 error
= nfs_init_server_rpcclient(server
, authflavour
);
953 dprintk("<-- nfs4_init_server() = %d\n", error
);
958 * Create a version 4 volume record
959 * - keyed on server and FSID
961 struct nfs_server
*nfs4_create_server(const struct nfs4_mount_data
*data
,
962 const char *hostname
,
963 const struct sockaddr_in
*addr
,
966 rpc_authflavor_t authflavour
,
967 struct nfs_fh
*mntfh
)
969 struct nfs_fattr fattr
;
970 struct nfs_server
*server
;
973 dprintk("--> nfs4_create_server()\n");
975 server
= nfs_alloc_server();
977 return ERR_PTR(-ENOMEM
);
979 /* Get a client record */
980 error
= nfs4_set_client(server
, hostname
, addr
, authflavour
,
981 data
->proto
, data
->timeo
, data
->retrans
);
985 /* set up the general RPC client */
986 error
= nfs4_init_server(server
, data
, authflavour
);
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 /* Probe the root fh to retrieve its FSID */
995 error
= nfs4_path_walk(server
, mntfh
, mntpath
);
999 dprintk("Server FSID: %llx:%llx\n", server
->fsid
.major
, server
->fsid
.minor
);
1000 dprintk("Mount FH: %d\n", mntfh
->size
);
1002 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
1006 BUG_ON(!server
->nfs_client
);
1007 BUG_ON(!server
->nfs_client
->rpc_ops
);
1008 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1010 spin_lock(&nfs_client_lock
);
1011 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1012 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1013 spin_unlock(&nfs_client_lock
);
1015 server
->mount_time
= jiffies
;
1016 dprintk("<-- nfs4_create_server() = %p\n", server
);
1020 nfs_free_server(server
);
1021 dprintk("<-- nfs4_create_server() = error %d\n", error
);
1022 return ERR_PTR(error
);
1026 * Create an NFS4 referral server record
1028 struct nfs_server
*nfs4_create_referral_server(struct nfs_clone_mount
*data
,
1031 struct nfs_client
*parent_client
;
1032 struct nfs_server
*server
, *parent_server
;
1033 struct nfs_fattr fattr
;
1036 dprintk("--> nfs4_create_referral_server()\n");
1038 server
= nfs_alloc_server();
1040 return ERR_PTR(-ENOMEM
);
1042 parent_server
= NFS_SB(data
->sb
);
1043 parent_client
= parent_server
->nfs_client
;
1045 /* Get a client representation.
1046 * Note: NFSv4 always uses TCP, */
1047 error
= nfs4_set_client(server
, data
->hostname
, data
->addr
,
1049 parent_server
->client
->cl_xprt
->prot
,
1050 parent_client
->retrans_timeo
,
1051 parent_client
->retrans_count
);
1053 /* Initialise the client representation from the parent server */
1054 nfs_server_copy_userdata(server
, parent_server
);
1055 server
->caps
|= NFS_CAP_ATOMIC_OPEN
;
1057 error
= nfs_init_server_rpcclient(server
, data
->authflavor
);
1061 BUG_ON(!server
->nfs_client
);
1062 BUG_ON(!server
->nfs_client
->rpc_ops
);
1063 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1065 /* probe the filesystem info for this server filesystem */
1066 error
= nfs_probe_fsinfo(server
, fh
, &fattr
);
1070 dprintk("Referral FSID: %llx:%llx\n",
1071 server
->fsid
.major
, server
->fsid
.minor
);
1073 spin_lock(&nfs_client_lock
);
1074 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1075 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1076 spin_unlock(&nfs_client_lock
);
1078 server
->mount_time
= jiffies
;
1080 dprintk("<-- nfs_create_referral_server() = %p\n", server
);
1084 nfs_free_server(server
);
1085 dprintk("<-- nfs4_create_referral_server() = error %d\n", error
);
1086 return ERR_PTR(error
);
1089 #endif /* CONFIG_NFS_V4 */
1092 * Clone an NFS2, NFS3 or NFS4 server record
1094 struct nfs_server
*nfs_clone_server(struct nfs_server
*source
,
1096 struct nfs_fattr
*fattr
)
1098 struct nfs_server
*server
;
1099 struct nfs_fattr fattr_fsinfo
;
1102 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1103 fattr
->fsid
.major
, fattr
->fsid
.minor
);
1105 server
= nfs_alloc_server();
1107 return ERR_PTR(-ENOMEM
);
1109 /* Copy data from the source */
1110 server
->nfs_client
= source
->nfs_client
;
1111 atomic_inc(&server
->nfs_client
->cl_count
);
1112 nfs_server_copy_userdata(server
, source
);
1114 server
->fsid
= fattr
->fsid
;
1116 error
= nfs_init_server_rpcclient(server
, source
->client
->cl_auth
->au_flavor
);
1118 goto out_free_server
;
1119 if (!IS_ERR(source
->client_acl
))
1120 nfs_init_server_aclclient(server
);
1122 /* probe the filesystem info for this server filesystem */
1123 error
= nfs_probe_fsinfo(server
, fh
, &fattr_fsinfo
);
1125 goto out_free_server
;
1127 dprintk("Cloned FSID: %llx:%llx\n",
1128 server
->fsid
.major
, server
->fsid
.minor
);
1130 error
= nfs_start_lockd(server
);
1132 goto out_free_server
;
1134 spin_lock(&nfs_client_lock
);
1135 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1136 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1137 spin_unlock(&nfs_client_lock
);
1139 server
->mount_time
= jiffies
;
1141 dprintk("<-- nfs_clone_server() = %p\n", server
);
1145 nfs_free_server(server
);
1146 dprintk("<-- nfs_clone_server() = error %d\n", error
);
1147 return ERR_PTR(error
);
1150 #ifdef CONFIG_PROC_FS
1151 static struct proc_dir_entry
*proc_fs_nfs
;
1153 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
);
1154 static void *nfs_server_list_start(struct seq_file
*p
, loff_t
*pos
);
1155 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1156 static void nfs_server_list_stop(struct seq_file
*p
, void *v
);
1157 static int nfs_server_list_show(struct seq_file
*m
, void *v
);
1159 static struct seq_operations nfs_server_list_ops
= {
1160 .start
= nfs_server_list_start
,
1161 .next
= nfs_server_list_next
,
1162 .stop
= nfs_server_list_stop
,
1163 .show
= nfs_server_list_show
,
1166 static struct file_operations nfs_server_list_fops
= {
1167 .open
= nfs_server_list_open
,
1169 .llseek
= seq_lseek
,
1170 .release
= seq_release
,
1173 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
);
1174 static void *nfs_volume_list_start(struct seq_file
*p
, loff_t
*pos
);
1175 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1176 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
);
1177 static int nfs_volume_list_show(struct seq_file
*m
, void *v
);
1179 static struct seq_operations nfs_volume_list_ops
= {
1180 .start
= nfs_volume_list_start
,
1181 .next
= nfs_volume_list_next
,
1182 .stop
= nfs_volume_list_stop
,
1183 .show
= nfs_volume_list_show
,
1186 static struct file_operations nfs_volume_list_fops
= {
1187 .open
= nfs_volume_list_open
,
1189 .llseek
= seq_lseek
,
1190 .release
= seq_release
,
1194 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1197 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
)
1202 ret
= seq_open(file
, &nfs_server_list_ops
);
1206 m
= file
->private_data
;
1207 m
->private = PDE(inode
)->data
;
1213 * set up the iterator to start reading from the server list and return the first item
1215 static void *nfs_server_list_start(struct seq_file
*m
, loff_t
*_pos
)
1217 struct list_head
*_p
;
1220 /* lock the list against modification */
1221 spin_lock(&nfs_client_lock
);
1223 /* allow for the header line */
1225 return SEQ_START_TOKEN
;
1228 /* find the n'th element in the list */
1229 list_for_each(_p
, &nfs_client_list
)
1233 return _p
!= &nfs_client_list
? _p
: NULL
;
1237 * move to next server
1239 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1241 struct list_head
*_p
;
1246 _p
= (v
== SEQ_START_TOKEN
) ? nfs_client_list
.next
: _p
->next
;
1248 return _p
!= &nfs_client_list
? _p
: NULL
;
1252 * clean up after reading from the transports list
1254 static void nfs_server_list_stop(struct seq_file
*p
, void *v
)
1256 spin_unlock(&nfs_client_lock
);
1260 * display a header line followed by a load of call lines
1262 static int nfs_server_list_show(struct seq_file
*m
, void *v
)
1264 struct nfs_client
*clp
;
1266 /* display header on line 1 */
1267 if (v
== SEQ_START_TOKEN
) {
1268 seq_puts(m
, "NV SERVER PORT USE HOSTNAME\n");
1272 /* display one transport per line on subsequent lines */
1273 clp
= list_entry(v
, struct nfs_client
, cl_share_link
);
1275 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1277 NIPQUAD(clp
->cl_addr
.sin_addr
),
1278 ntohs(clp
->cl_addr
.sin_port
),
1279 atomic_read(&clp
->cl_count
),
1286 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1288 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
)
1293 ret
= seq_open(file
, &nfs_volume_list_ops
);
1297 m
= file
->private_data
;
1298 m
->private = PDE(inode
)->data
;
1304 * set up the iterator to start reading from the volume list and return the first item
1306 static void *nfs_volume_list_start(struct seq_file
*m
, loff_t
*_pos
)
1308 struct list_head
*_p
;
1311 /* lock the list against modification */
1312 spin_lock(&nfs_client_lock
);
1314 /* allow for the header line */
1316 return SEQ_START_TOKEN
;
1319 /* find the n'th element in the list */
1320 list_for_each(_p
, &nfs_volume_list
)
1324 return _p
!= &nfs_volume_list
? _p
: NULL
;
1328 * move to next volume
1330 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1332 struct list_head
*_p
;
1337 _p
= (v
== SEQ_START_TOKEN
) ? nfs_volume_list
.next
: _p
->next
;
1339 return _p
!= &nfs_volume_list
? _p
: NULL
;
1343 * clean up after reading from the transports list
1345 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
)
1347 spin_unlock(&nfs_client_lock
);
1351 * display a header line followed by a load of call lines
1353 static int nfs_volume_list_show(struct seq_file
*m
, void *v
)
1355 struct nfs_server
*server
;
1356 struct nfs_client
*clp
;
1357 char dev
[8], fsid
[17];
1359 /* display header on line 1 */
1360 if (v
== SEQ_START_TOKEN
) {
1361 seq_puts(m
, "NV SERVER PORT DEV FSID\n");
1364 /* display one transport per line on subsequent lines */
1365 server
= list_entry(v
, struct nfs_server
, master_link
);
1366 clp
= server
->nfs_client
;
1368 snprintf(dev
, 8, "%u:%u",
1369 MAJOR(server
->s_dev
), MINOR(server
->s_dev
));
1371 snprintf(fsid
, 17, "%llx:%llx",
1372 server
->fsid
.major
, server
->fsid
.minor
);
1374 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1376 NIPQUAD(clp
->cl_addr
.sin_addr
),
1377 ntohs(clp
->cl_addr
.sin_port
),
1385 * initialise the /proc/fs/nfsfs/ directory
1387 int __init
nfs_fs_proc_init(void)
1389 struct proc_dir_entry
*p
;
1391 proc_fs_nfs
= proc_mkdir("nfsfs", proc_root_fs
);
1395 proc_fs_nfs
->owner
= THIS_MODULE
;
1397 /* a file of servers with which we're dealing */
1398 p
= create_proc_entry("servers", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1402 p
->proc_fops
= &nfs_server_list_fops
;
1403 p
->owner
= THIS_MODULE
;
1405 /* a file of volumes that we have mounted */
1406 p
= create_proc_entry("volumes", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1410 p
->proc_fops
= &nfs_volume_list_fops
;
1411 p
->owner
= THIS_MODULE
;
1415 remove_proc_entry("servers", proc_fs_nfs
);
1417 remove_proc_entry("nfsfs", proc_root_fs
);
1423 * clean up the /proc/fs/nfsfs/ directory
1425 void nfs_fs_proc_exit(void)
1427 remove_proc_entry("volumes", proc_fs_nfs
);
1428 remove_proc_entry("servers", proc_fs_nfs
);
1429 remove_proc_entry("nfsfs", proc_root_fs
);
1432 #endif /* CONFIG_PROC_FS */