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 if (clp
->cl_nfsversion
== 3) {
592 if (server
->namelen
== 0 || server
->namelen
> NFS3_MAXNAMLEN
)
593 server
->namelen
= NFS3_MAXNAMLEN
;
594 if (!(data
->flags
& NFS_MOUNT_NORDIRPLUS
))
595 server
->caps
|= NFS_CAP_READDIRPLUS
;
597 if (server
->namelen
== 0 || server
->namelen
> NFS2_MAXNAMLEN
)
598 server
->namelen
= NFS2_MAXNAMLEN
;
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
;
635 server
->backing_dev_info
.ra_pages
= server
->rpages
* NFS_MAX_READAHEAD
;
637 if (server
->wsize
> max_rpc_payload
)
638 server
->wsize
= max_rpc_payload
;
639 if (server
->wsize
> NFS_MAX_FILE_IO_SIZE
)
640 server
->wsize
= NFS_MAX_FILE_IO_SIZE
;
641 server
->wpages
= (server
->wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
642 server
->wtmult
= nfs_block_bits(fsinfo
->wtmult
, NULL
);
644 server
->dtsize
= nfs_block_size(fsinfo
->dtpref
, NULL
);
645 if (server
->dtsize
> PAGE_CACHE_SIZE
)
646 server
->dtsize
= PAGE_CACHE_SIZE
;
647 if (server
->dtsize
> server
->rsize
)
648 server
->dtsize
= server
->rsize
;
650 if (server
->flags
& NFS_MOUNT_NOAC
) {
651 server
->acregmin
= server
->acregmax
= 0;
652 server
->acdirmin
= server
->acdirmax
= 0;
655 server
->maxfilesize
= fsinfo
->maxfilesize
;
657 /* We're airborne Set socket buffersize */
658 rpc_setbufsize(server
->client
, server
->wsize
+ 100, server
->rsize
+ 100);
662 * Probe filesystem information, including the FSID on v2/v3
664 static int nfs_probe_fsinfo(struct nfs_server
*server
, struct nfs_fh
*mntfh
, struct nfs_fattr
*fattr
)
666 struct nfs_fsinfo fsinfo
;
667 struct nfs_client
*clp
= server
->nfs_client
;
670 dprintk("--> nfs_probe_fsinfo()\n");
672 if (clp
->rpc_ops
->set_capabilities
!= NULL
) {
673 error
= clp
->rpc_ops
->set_capabilities(server
, mntfh
);
678 fsinfo
.fattr
= fattr
;
679 nfs_fattr_init(fattr
);
680 error
= clp
->rpc_ops
->fsinfo(server
, mntfh
, &fsinfo
);
684 nfs_server_set_fsinfo(server
, &fsinfo
);
686 /* Get some general file system info */
687 if (server
->namelen
== 0) {
688 struct nfs_pathconf pathinfo
;
690 pathinfo
.fattr
= fattr
;
691 nfs_fattr_init(fattr
);
693 if (clp
->rpc_ops
->pathconf(server
, mntfh
, &pathinfo
) >= 0)
694 server
->namelen
= pathinfo
.max_namelen
;
697 dprintk("<-- nfs_probe_fsinfo() = 0\n");
701 dprintk("nfs_probe_fsinfo: error = %d\n", -error
);
706 * Copy useful information when duplicating a server record
708 static void nfs_server_copy_userdata(struct nfs_server
*target
, struct nfs_server
*source
)
710 target
->flags
= source
->flags
;
711 target
->acregmin
= source
->acregmin
;
712 target
->acregmax
= source
->acregmax
;
713 target
->acdirmin
= source
->acdirmin
;
714 target
->acdirmax
= source
->acdirmax
;
715 target
->caps
= source
->caps
;
719 * Allocate and initialise a server record
721 static struct nfs_server
*nfs_alloc_server(void)
723 struct nfs_server
*server
;
725 server
= kzalloc(sizeof(struct nfs_server
), GFP_KERNEL
);
729 server
->client
= server
->client_acl
= ERR_PTR(-EINVAL
);
731 /* Zero out the NFS state stuff */
732 INIT_LIST_HEAD(&server
->client_link
);
733 INIT_LIST_HEAD(&server
->master_link
);
735 server
->io_stats
= nfs_alloc_iostats();
736 if (!server
->io_stats
) {
745 * Free up a server record
747 void nfs_free_server(struct nfs_server
*server
)
749 dprintk("--> nfs_free_server()\n");
751 spin_lock(&nfs_client_lock
);
752 list_del(&server
->client_link
);
753 list_del(&server
->master_link
);
754 spin_unlock(&nfs_client_lock
);
756 if (server
->destroy
!= NULL
)
757 server
->destroy(server
);
758 if (!IS_ERR(server
->client
))
759 rpc_shutdown_client(server
->client
);
761 nfs_put_client(server
->nfs_client
);
763 nfs_free_iostats(server
->io_stats
);
765 nfs_release_automount_timer();
766 dprintk("<-- nfs_free_server()\n");
770 * Create a version 2 or 3 volume record
771 * - keyed on server and FSID
773 struct nfs_server
*nfs_create_server(const struct nfs_mount_data
*data
,
774 struct nfs_fh
*mntfh
)
776 struct nfs_server
*server
;
777 struct nfs_fattr fattr
;
780 server
= nfs_alloc_server();
782 return ERR_PTR(-ENOMEM
);
784 /* Get a client representation */
785 error
= nfs_init_server(server
, data
);
789 BUG_ON(!server
->nfs_client
);
790 BUG_ON(!server
->nfs_client
->rpc_ops
);
791 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
793 /* Probe the root fh to retrieve its FSID */
794 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
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 BUG_ON(!server
->nfs_client
);
988 BUG_ON(!server
->nfs_client
->rpc_ops
);
989 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
991 spin_lock(&nfs_client_lock
);
992 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
993 list_add_tail(&server
->master_link
, &nfs_volume_list
);
994 spin_unlock(&nfs_client_lock
);
996 server
->mount_time
= jiffies
;
997 dprintk("<-- nfs4_create_server() = %p\n", server
);
1001 nfs_free_server(server
);
1002 dprintk("<-- nfs4_create_server() = error %d\n", error
);
1003 return ERR_PTR(error
);
1007 * Create an NFS4 referral server record
1009 struct nfs_server
*nfs4_create_referral_server(struct nfs_clone_mount
*data
,
1010 struct nfs_fh
*mntfh
)
1012 struct nfs_client
*parent_client
;
1013 struct nfs_server
*server
, *parent_server
;
1014 struct nfs_fattr fattr
;
1017 dprintk("--> nfs4_create_referral_server()\n");
1019 server
= nfs_alloc_server();
1021 return ERR_PTR(-ENOMEM
);
1023 parent_server
= NFS_SB(data
->sb
);
1024 parent_client
= parent_server
->nfs_client
;
1026 /* Get a client representation.
1027 * Note: NFSv4 always uses TCP, */
1028 error
= nfs4_set_client(server
, data
->hostname
, data
->addr
,
1029 parent_client
->cl_ipaddr
,
1031 parent_server
->client
->cl_xprt
->prot
,
1032 parent_client
->retrans_timeo
,
1033 parent_client
->retrans_count
);
1037 /* Initialise the client representation from the parent server */
1038 nfs_server_copy_userdata(server
, parent_server
);
1039 server
->caps
|= NFS_CAP_ATOMIC_OPEN
;
1041 error
= nfs_init_server_rpcclient(server
, data
->authflavor
);
1045 BUG_ON(!server
->nfs_client
);
1046 BUG_ON(!server
->nfs_client
->rpc_ops
);
1047 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1049 /* Probe the root fh to retrieve its FSID and filehandle */
1050 error
= nfs4_path_walk(server
, mntfh
, data
->mnt_path
);
1054 /* probe the filesystem info for this server filesystem */
1055 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
1059 dprintk("Referral FSID: %llx:%llx\n",
1060 (unsigned long long) server
->fsid
.major
,
1061 (unsigned long long) server
->fsid
.minor
);
1063 spin_lock(&nfs_client_lock
);
1064 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1065 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1066 spin_unlock(&nfs_client_lock
);
1068 server
->mount_time
= jiffies
;
1070 dprintk("<-- nfs_create_referral_server() = %p\n", server
);
1074 nfs_free_server(server
);
1075 dprintk("<-- nfs4_create_referral_server() = error %d\n", error
);
1076 return ERR_PTR(error
);
1079 #endif /* CONFIG_NFS_V4 */
1082 * Clone an NFS2, NFS3 or NFS4 server record
1084 struct nfs_server
*nfs_clone_server(struct nfs_server
*source
,
1086 struct nfs_fattr
*fattr
)
1088 struct nfs_server
*server
;
1089 struct nfs_fattr fattr_fsinfo
;
1092 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1093 (unsigned long long) fattr
->fsid
.major
,
1094 (unsigned long long) fattr
->fsid
.minor
);
1096 server
= nfs_alloc_server();
1098 return ERR_PTR(-ENOMEM
);
1100 /* Copy data from the source */
1101 server
->nfs_client
= source
->nfs_client
;
1102 atomic_inc(&server
->nfs_client
->cl_count
);
1103 nfs_server_copy_userdata(server
, source
);
1105 server
->fsid
= fattr
->fsid
;
1107 error
= nfs_init_server_rpcclient(server
, source
->client
->cl_auth
->au_flavor
);
1109 goto out_free_server
;
1110 if (!IS_ERR(source
->client_acl
))
1111 nfs_init_server_aclclient(server
);
1113 /* probe the filesystem info for this server filesystem */
1114 error
= nfs_probe_fsinfo(server
, fh
, &fattr_fsinfo
);
1116 goto out_free_server
;
1118 dprintk("Cloned FSID: %llx:%llx\n",
1119 (unsigned long long) server
->fsid
.major
,
1120 (unsigned long long) server
->fsid
.minor
);
1122 error
= nfs_start_lockd(server
);
1124 goto out_free_server
;
1126 spin_lock(&nfs_client_lock
);
1127 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1128 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1129 spin_unlock(&nfs_client_lock
);
1131 server
->mount_time
= jiffies
;
1133 dprintk("<-- nfs_clone_server() = %p\n", server
);
1137 nfs_free_server(server
);
1138 dprintk("<-- nfs_clone_server() = error %d\n", error
);
1139 return ERR_PTR(error
);
1142 #ifdef CONFIG_PROC_FS
1143 static struct proc_dir_entry
*proc_fs_nfs
;
1145 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
);
1146 static void *nfs_server_list_start(struct seq_file
*p
, loff_t
*pos
);
1147 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1148 static void nfs_server_list_stop(struct seq_file
*p
, void *v
);
1149 static int nfs_server_list_show(struct seq_file
*m
, void *v
);
1151 static struct seq_operations nfs_server_list_ops
= {
1152 .start
= nfs_server_list_start
,
1153 .next
= nfs_server_list_next
,
1154 .stop
= nfs_server_list_stop
,
1155 .show
= nfs_server_list_show
,
1158 static const struct file_operations nfs_server_list_fops
= {
1159 .open
= nfs_server_list_open
,
1161 .llseek
= seq_lseek
,
1162 .release
= seq_release
,
1165 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
);
1166 static void *nfs_volume_list_start(struct seq_file
*p
, loff_t
*pos
);
1167 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1168 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
);
1169 static int nfs_volume_list_show(struct seq_file
*m
, void *v
);
1171 static struct seq_operations nfs_volume_list_ops
= {
1172 .start
= nfs_volume_list_start
,
1173 .next
= nfs_volume_list_next
,
1174 .stop
= nfs_volume_list_stop
,
1175 .show
= nfs_volume_list_show
,
1178 static const struct file_operations nfs_volume_list_fops
= {
1179 .open
= nfs_volume_list_open
,
1181 .llseek
= seq_lseek
,
1182 .release
= seq_release
,
1186 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1189 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
)
1194 ret
= seq_open(file
, &nfs_server_list_ops
);
1198 m
= file
->private_data
;
1199 m
->private = PDE(inode
)->data
;
1205 * set up the iterator to start reading from the server list and return the first item
1207 static void *nfs_server_list_start(struct seq_file
*m
, loff_t
*_pos
)
1209 struct list_head
*_p
;
1212 /* lock the list against modification */
1213 spin_lock(&nfs_client_lock
);
1215 /* allow for the header line */
1217 return SEQ_START_TOKEN
;
1220 /* find the n'th element in the list */
1221 list_for_each(_p
, &nfs_client_list
)
1225 return _p
!= &nfs_client_list
? _p
: NULL
;
1229 * move to next server
1231 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1233 struct list_head
*_p
;
1238 _p
= (v
== SEQ_START_TOKEN
) ? nfs_client_list
.next
: _p
->next
;
1240 return _p
!= &nfs_client_list
? _p
: NULL
;
1244 * clean up after reading from the transports list
1246 static void nfs_server_list_stop(struct seq_file
*p
, void *v
)
1248 spin_unlock(&nfs_client_lock
);
1252 * display a header line followed by a load of call lines
1254 static int nfs_server_list_show(struct seq_file
*m
, void *v
)
1256 struct nfs_client
*clp
;
1258 /* display header on line 1 */
1259 if (v
== SEQ_START_TOKEN
) {
1260 seq_puts(m
, "NV SERVER PORT USE HOSTNAME\n");
1264 /* display one transport per line on subsequent lines */
1265 clp
= list_entry(v
, struct nfs_client
, cl_share_link
);
1267 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1269 NIPQUAD(clp
->cl_addr
.sin_addr
),
1270 ntohs(clp
->cl_addr
.sin_port
),
1271 atomic_read(&clp
->cl_count
),
1278 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1280 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
)
1285 ret
= seq_open(file
, &nfs_volume_list_ops
);
1289 m
= file
->private_data
;
1290 m
->private = PDE(inode
)->data
;
1296 * set up the iterator to start reading from the volume list and return the first item
1298 static void *nfs_volume_list_start(struct seq_file
*m
, loff_t
*_pos
)
1300 struct list_head
*_p
;
1303 /* lock the list against modification */
1304 spin_lock(&nfs_client_lock
);
1306 /* allow for the header line */
1308 return SEQ_START_TOKEN
;
1311 /* find the n'th element in the list */
1312 list_for_each(_p
, &nfs_volume_list
)
1316 return _p
!= &nfs_volume_list
? _p
: NULL
;
1320 * move to next volume
1322 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1324 struct list_head
*_p
;
1329 _p
= (v
== SEQ_START_TOKEN
) ? nfs_volume_list
.next
: _p
->next
;
1331 return _p
!= &nfs_volume_list
? _p
: NULL
;
1335 * clean up after reading from the transports list
1337 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
)
1339 spin_unlock(&nfs_client_lock
);
1343 * display a header line followed by a load of call lines
1345 static int nfs_volume_list_show(struct seq_file
*m
, void *v
)
1347 struct nfs_server
*server
;
1348 struct nfs_client
*clp
;
1349 char dev
[8], fsid
[17];
1351 /* display header on line 1 */
1352 if (v
== SEQ_START_TOKEN
) {
1353 seq_puts(m
, "NV SERVER PORT DEV FSID\n");
1356 /* display one transport per line on subsequent lines */
1357 server
= list_entry(v
, struct nfs_server
, master_link
);
1358 clp
= server
->nfs_client
;
1360 snprintf(dev
, 8, "%u:%u",
1361 MAJOR(server
->s_dev
), MINOR(server
->s_dev
));
1363 snprintf(fsid
, 17, "%llx:%llx",
1364 (unsigned long long) server
->fsid
.major
,
1365 (unsigned long long) server
->fsid
.minor
);
1367 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1369 NIPQUAD(clp
->cl_addr
.sin_addr
),
1370 ntohs(clp
->cl_addr
.sin_port
),
1378 * initialise the /proc/fs/nfsfs/ directory
1380 int __init
nfs_fs_proc_init(void)
1382 struct proc_dir_entry
*p
;
1384 proc_fs_nfs
= proc_mkdir("nfsfs", proc_root_fs
);
1388 proc_fs_nfs
->owner
= THIS_MODULE
;
1390 /* a file of servers with which we're dealing */
1391 p
= create_proc_entry("servers", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1395 p
->proc_fops
= &nfs_server_list_fops
;
1396 p
->owner
= THIS_MODULE
;
1398 /* a file of volumes that we have mounted */
1399 p
= create_proc_entry("volumes", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1403 p
->proc_fops
= &nfs_volume_list_fops
;
1404 p
->owner
= THIS_MODULE
;
1408 remove_proc_entry("servers", proc_fs_nfs
);
1410 remove_proc_entry("nfsfs", proc_root_fs
);
1416 * clean up the /proc/fs/nfsfs/ directory
1418 void nfs_fs_proc_exit(void)
1420 remove_proc_entry("volumes", proc_fs_nfs
);
1421 remove_proc_entry("servers", proc_fs_nfs
);
1422 remove_proc_entry("nfsfs", proc_root_fs
);
1425 #endif /* CONFIG_PROC_FS */