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
);
167 static void nfs4_shutdown_client(struct nfs_client
*clp
)
170 if (__test_and_clear_bit(NFS_CS_RENEWD
, &clp
->cl_res_state
))
171 nfs4_kill_renewd(clp
);
172 while (!list_empty(&clp
->cl_unused
)) {
173 struct nfs4_state_owner
*sp
;
175 sp
= list_entry(clp
->cl_unused
.next
,
176 struct nfs4_state_owner
,
178 list_del(&sp
->so_list
);
181 BUG_ON(!list_empty(&clp
->cl_state_owners
));
182 if (__test_and_clear_bit(NFS_CS_IDMAP
, &clp
->cl_res_state
))
183 nfs_idmap_delete(clp
);
188 * Destroy a shared client record
190 static void nfs_free_client(struct nfs_client
*clp
)
192 dprintk("--> nfs_free_client(%d)\n", clp
->cl_nfsversion
);
194 nfs4_shutdown_client(clp
);
196 /* -EIO all pending I/O */
197 if (!IS_ERR(clp
->cl_rpcclient
))
198 rpc_shutdown_client(clp
->cl_rpcclient
);
200 if (__test_and_clear_bit(NFS_CS_CALLBACK
, &clp
->cl_res_state
))
203 if (__test_and_clear_bit(NFS_CS_RPCIOD
, &clp
->cl_res_state
))
206 kfree(clp
->cl_hostname
);
209 dprintk("<-- nfs_free_client()\n");
213 * Release a reference to a shared client record
215 void nfs_put_client(struct nfs_client
*clp
)
220 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp
->cl_count
));
222 if (atomic_dec_and_lock(&clp
->cl_count
, &nfs_client_lock
)) {
223 list_del(&clp
->cl_share_link
);
224 spin_unlock(&nfs_client_lock
);
226 BUG_ON(!list_empty(&clp
->cl_superblocks
));
228 nfs_free_client(clp
);
233 * Find a client by address
234 * - caller must hold nfs_client_lock
236 static struct nfs_client
*__nfs_find_client(const struct sockaddr_in
*addr
, int nfsversion
)
238 struct nfs_client
*clp
;
240 list_for_each_entry(clp
, &nfs_client_list
, cl_share_link
) {
241 /* Different NFS versions cannot share the same nfs_client */
242 if (clp
->cl_nfsversion
!= nfsversion
)
245 if (memcmp(&clp
->cl_addr
.sin_addr
, &addr
->sin_addr
,
246 sizeof(clp
->cl_addr
.sin_addr
)) != 0)
249 if (clp
->cl_addr
.sin_port
== addr
->sin_port
)
256 atomic_inc(&clp
->cl_count
);
261 * Find a client by IP address and protocol version
262 * - returns NULL if no such client
264 struct nfs_client
*nfs_find_client(const struct sockaddr_in
*addr
, int nfsversion
)
266 struct nfs_client
*clp
;
268 spin_lock(&nfs_client_lock
);
269 clp
= __nfs_find_client(addr
, nfsversion
);
270 spin_unlock(&nfs_client_lock
);
272 BUG_ON(clp
->cl_cons_state
== 0);
278 * Look up a client by IP address and protocol version
279 * - creates a new record if one doesn't yet exist
281 static struct nfs_client
*nfs_get_client(const char *hostname
,
282 const struct sockaddr_in
*addr
,
285 struct nfs_client
*clp
, *new = NULL
;
288 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT
":%d,%d)\n",
289 hostname
?: "", NIPQUAD(addr
->sin_addr
),
290 addr
->sin_port
, nfsversion
);
292 /* see if the client already exists */
294 spin_lock(&nfs_client_lock
);
296 clp
= __nfs_find_client(addr
, nfsversion
);
302 spin_unlock(&nfs_client_lock
);
304 new = nfs_alloc_client(hostname
, addr
, nfsversion
);
307 return ERR_PTR(-ENOMEM
);
309 /* install a new client and return with it unready */
312 list_add(&clp
->cl_share_link
, &nfs_client_list
);
313 spin_unlock(&nfs_client_lock
);
314 dprintk("--> nfs_get_client() = %p [new]\n", clp
);
317 /* found an existing client
318 * - make sure it's ready before returning
321 spin_unlock(&nfs_client_lock
);
324 nfs_free_client(new);
326 if (clp
->cl_cons_state
== NFS_CS_INITING
) {
327 DECLARE_WAITQUEUE(myself
, current
);
329 add_wait_queue(&nfs_client_active_wq
, &myself
);
332 set_current_state(TASK_INTERRUPTIBLE
);
333 if (signal_pending(current
) ||
334 clp
->cl_cons_state
> NFS_CS_READY
)
339 remove_wait_queue(&nfs_client_active_wq
, &myself
);
341 if (signal_pending(current
)) {
343 return ERR_PTR(-ERESTARTSYS
);
347 if (clp
->cl_cons_state
< NFS_CS_READY
) {
348 error
= clp
->cl_cons_state
;
350 return ERR_PTR(error
);
353 BUG_ON(clp
->cl_cons_state
!= NFS_CS_READY
);
355 dprintk("--> nfs_get_client() = %p [share]\n", clp
);
360 * Mark a server as ready or failed
362 static void nfs_mark_client_ready(struct nfs_client
*clp
, int state
)
364 clp
->cl_cons_state
= state
;
365 wake_up_all(&nfs_client_active_wq
);
369 * Initialise the timeout values for a connection
371 static void nfs_init_timeout_values(struct rpc_timeout
*to
, int proto
,
372 unsigned int timeo
, unsigned int retrans
)
374 to
->to_initval
= timeo
* HZ
/ 10;
375 to
->to_retries
= retrans
;
382 to
->to_initval
= 60 * HZ
;
383 if (to
->to_initval
> NFS_MAX_TCP_TIMEOUT
)
384 to
->to_initval
= NFS_MAX_TCP_TIMEOUT
;
385 to
->to_increment
= to
->to_initval
;
386 to
->to_maxval
= to
->to_initval
+ (to
->to_increment
* to
->to_retries
);
387 to
->to_exponential
= 0;
392 to
->to_initval
= 11 * HZ
/ 10;
393 if (to
->to_initval
> NFS_MAX_UDP_TIMEOUT
)
394 to
->to_initval
= NFS_MAX_UDP_TIMEOUT
;
395 to
->to_maxval
= NFS_MAX_UDP_TIMEOUT
;
396 to
->to_exponential
= 1;
402 * Create an RPC client handle
404 static int nfs_create_rpc_client(struct nfs_client
*clp
, int proto
,
406 unsigned int retrans
,
407 rpc_authflavor_t flavor
)
409 struct rpc_timeout timeparms
;
410 struct rpc_clnt
*clnt
= NULL
;
411 struct rpc_create_args args
= {
413 .address
= (struct sockaddr
*)&clp
->cl_addr
,
414 .addrsize
= sizeof(clp
->cl_addr
),
415 .timeout
= &timeparms
,
416 .servername
= clp
->cl_hostname
,
417 .program
= &nfs_program
,
418 .version
= clp
->rpc_ops
->version
,
419 .authflavor
= flavor
,
422 if (!IS_ERR(clp
->cl_rpcclient
))
425 nfs_init_timeout_values(&timeparms
, proto
, timeo
, retrans
);
426 clp
->retrans_timeo
= timeparms
.to_initval
;
427 clp
->retrans_count
= timeparms
.to_retries
;
429 clnt
= rpc_create(&args
);
431 dprintk("%s: cannot create RPC client. Error = %ld\n",
432 __FUNCTION__
, PTR_ERR(clnt
));
433 return PTR_ERR(clnt
);
436 clp
->cl_rpcclient
= clnt
;
441 * Version 2 or 3 client destruction
443 static void nfs_destroy_server(struct nfs_server
*server
)
445 if (!IS_ERR(server
->client_acl
))
446 rpc_shutdown_client(server
->client_acl
);
448 if (!(server
->flags
& NFS_MOUNT_NONLM
))
449 lockd_down(); /* release rpc.lockd */
453 * Version 2 or 3 lockd setup
455 static int nfs_start_lockd(struct nfs_server
*server
)
459 if (server
->nfs_client
->cl_nfsversion
> 3)
461 if (server
->flags
& NFS_MOUNT_NONLM
)
465 server
->flags
|= NFS_MOUNT_NONLM
;
467 server
->destroy
= nfs_destroy_server
;
473 * Initialise an NFSv3 ACL client connection
475 #ifdef CONFIG_NFS_V3_ACL
476 static void nfs_init_server_aclclient(struct nfs_server
*server
)
478 if (server
->nfs_client
->cl_nfsversion
!= 3)
480 if (server
->flags
& NFS_MOUNT_NOACL
)
483 server
->client_acl
= rpc_bind_new_program(server
->client
, &nfsacl_program
, 3);
484 if (IS_ERR(server
->client_acl
))
487 /* No errors! Assume that Sun nfsacls are supported */
488 server
->caps
|= NFS_CAP_ACLS
;
492 server
->caps
&= ~NFS_CAP_ACLS
;
495 static inline void nfs_init_server_aclclient(struct nfs_server
*server
)
497 server
->flags
&= ~NFS_MOUNT_NOACL
;
498 server
->caps
&= ~NFS_CAP_ACLS
;
503 * Create a general RPC client
505 static int nfs_init_server_rpcclient(struct nfs_server
*server
, rpc_authflavor_t pseudoflavour
)
507 struct nfs_client
*clp
= server
->nfs_client
;
509 server
->client
= rpc_clone_client(clp
->cl_rpcclient
);
510 if (IS_ERR(server
->client
)) {
511 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__
);
512 return PTR_ERR(server
->client
);
515 if (pseudoflavour
!= clp
->cl_rpcclient
->cl_auth
->au_flavor
) {
516 struct rpc_auth
*auth
;
518 auth
= rpcauth_create(pseudoflavour
, server
->client
);
520 dprintk("%s: couldn't create credcache!\n", __FUNCTION__
);
521 return PTR_ERR(auth
);
524 server
->client
->cl_softrtry
= 0;
525 if (server
->flags
& NFS_MOUNT_SOFT
)
526 server
->client
->cl_softrtry
= 1;
528 server
->client
->cl_intr
= 0;
529 if (server
->flags
& NFS4_MOUNT_INTR
)
530 server
->client
->cl_intr
= 1;
536 * Initialise an NFS2 or NFS3 client
538 static int nfs_init_client(struct nfs_client
*clp
, const struct nfs_mount_data
*data
)
540 int proto
= (data
->flags
& NFS_MOUNT_TCP
) ? IPPROTO_TCP
: IPPROTO_UDP
;
543 if (clp
->cl_cons_state
== NFS_CS_READY
) {
544 /* the client is already initialised */
545 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp
);
549 /* Check NFS protocol revision and initialize RPC op vector */
550 clp
->rpc_ops
= &nfs_v2_clientops
;
552 if (clp
->cl_nfsversion
== 3)
553 clp
->rpc_ops
= &nfs_v3_clientops
;
556 * Create a client RPC handle for doing FSSTAT with UNIX auth only
557 * - RFC 2623, sec 2.3.2
559 error
= nfs_create_rpc_client(clp
, proto
, data
->timeo
, data
->retrans
,
563 nfs_mark_client_ready(clp
, NFS_CS_READY
);
567 nfs_mark_client_ready(clp
, error
);
568 dprintk("<-- nfs_init_client() = xerror %d\n", error
);
573 * Create a version 2 or 3 client
575 static int nfs_init_server(struct nfs_server
*server
, const struct nfs_mount_data
*data
)
577 struct nfs_client
*clp
;
578 int error
, nfsvers
= 2;
580 dprintk("--> nfs_init_server()\n");
583 if (data
->flags
& NFS_MOUNT_VER3
)
587 /* Allocate or find a client reference we can use */
588 clp
= nfs_get_client(data
->hostname
, &data
->addr
, nfsvers
);
590 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp
));
594 error
= nfs_init_client(clp
, data
);
598 server
->nfs_client
= clp
;
600 /* Initialise the client representation from the mount data */
601 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
604 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
606 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
608 server
->acregmin
= data
->acregmin
* HZ
;
609 server
->acregmax
= data
->acregmax
* HZ
;
610 server
->acdirmin
= data
->acdirmin
* HZ
;
611 server
->acdirmax
= data
->acdirmax
* HZ
;
613 /* Start lockd here, before we might error out */
614 error
= nfs_start_lockd(server
);
618 error
= nfs_init_server_rpcclient(server
, data
->pseudoflavor
);
622 server
->namelen
= data
->namlen
;
623 /* Create a client RPC handle for the NFSv3 ACL management interface */
624 nfs_init_server_aclclient(server
);
625 if (clp
->cl_nfsversion
== 3) {
626 if (server
->namelen
== 0 || server
->namelen
> NFS3_MAXNAMLEN
)
627 server
->namelen
= NFS3_MAXNAMLEN
;
628 server
->caps
|= NFS_CAP_READDIRPLUS
;
630 if (server
->namelen
== 0 || server
->namelen
> NFS2_MAXNAMLEN
)
631 server
->namelen
= NFS2_MAXNAMLEN
;
634 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp
);
638 server
->nfs_client
= NULL
;
640 dprintk("<-- nfs_init_server() = xerror %d\n", error
);
645 * Load up the server record from information gained in an fsinfo record
647 static void nfs_server_set_fsinfo(struct nfs_server
*server
, struct nfs_fsinfo
*fsinfo
)
649 unsigned long max_rpc_payload
;
651 /* Work out a lot of parameters */
652 if (server
->rsize
== 0)
653 server
->rsize
= nfs_block_size(fsinfo
->rtpref
, NULL
);
654 if (server
->wsize
== 0)
655 server
->wsize
= nfs_block_size(fsinfo
->wtpref
, NULL
);
657 if (fsinfo
->rtmax
>= 512 && server
->rsize
> fsinfo
->rtmax
)
658 server
->rsize
= nfs_block_size(fsinfo
->rtmax
, NULL
);
659 if (fsinfo
->wtmax
>= 512 && server
->wsize
> fsinfo
->wtmax
)
660 server
->wsize
= nfs_block_size(fsinfo
->wtmax
, NULL
);
662 max_rpc_payload
= nfs_block_size(rpc_max_payload(server
->client
), NULL
);
663 if (server
->rsize
> max_rpc_payload
)
664 server
->rsize
= max_rpc_payload
;
665 if (server
->rsize
> NFS_MAX_FILE_IO_SIZE
)
666 server
->rsize
= NFS_MAX_FILE_IO_SIZE
;
667 server
->rpages
= (server
->rsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
668 server
->backing_dev_info
.ra_pages
= server
->rpages
* NFS_MAX_READAHEAD
;
670 if (server
->wsize
> max_rpc_payload
)
671 server
->wsize
= max_rpc_payload
;
672 if (server
->wsize
> NFS_MAX_FILE_IO_SIZE
)
673 server
->wsize
= NFS_MAX_FILE_IO_SIZE
;
674 server
->wpages
= (server
->wsize
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
675 server
->wtmult
= nfs_block_bits(fsinfo
->wtmult
, NULL
);
677 server
->dtsize
= nfs_block_size(fsinfo
->dtpref
, NULL
);
678 if (server
->dtsize
> PAGE_CACHE_SIZE
)
679 server
->dtsize
= PAGE_CACHE_SIZE
;
680 if (server
->dtsize
> server
->rsize
)
681 server
->dtsize
= server
->rsize
;
683 if (server
->flags
& NFS_MOUNT_NOAC
) {
684 server
->acregmin
= server
->acregmax
= 0;
685 server
->acdirmin
= server
->acdirmax
= 0;
688 server
->maxfilesize
= fsinfo
->maxfilesize
;
690 /* We're airborne Set socket buffersize */
691 rpc_setbufsize(server
->client
, server
->wsize
+ 100, server
->rsize
+ 100);
695 * Probe filesystem information, including the FSID on v2/v3
697 static int nfs_probe_fsinfo(struct nfs_server
*server
, struct nfs_fh
*mntfh
, struct nfs_fattr
*fattr
)
699 struct nfs_fsinfo fsinfo
;
700 struct nfs_client
*clp
= server
->nfs_client
;
703 dprintk("--> nfs_probe_fsinfo()\n");
705 if (clp
->rpc_ops
->set_capabilities
!= NULL
) {
706 error
= clp
->rpc_ops
->set_capabilities(server
, mntfh
);
711 fsinfo
.fattr
= fattr
;
712 nfs_fattr_init(fattr
);
713 error
= clp
->rpc_ops
->fsinfo(server
, mntfh
, &fsinfo
);
717 nfs_server_set_fsinfo(server
, &fsinfo
);
719 /* Get some general file system info */
720 if (server
->namelen
== 0) {
721 struct nfs_pathconf pathinfo
;
723 pathinfo
.fattr
= fattr
;
724 nfs_fattr_init(fattr
);
726 if (clp
->rpc_ops
->pathconf(server
, mntfh
, &pathinfo
) >= 0)
727 server
->namelen
= pathinfo
.max_namelen
;
730 dprintk("<-- nfs_probe_fsinfo() = 0\n");
734 dprintk("nfs_probe_fsinfo: error = %d\n", -error
);
739 * Copy useful information when duplicating a server record
741 static void nfs_server_copy_userdata(struct nfs_server
*target
, struct nfs_server
*source
)
743 target
->flags
= source
->flags
;
744 target
->acregmin
= source
->acregmin
;
745 target
->acregmax
= source
->acregmax
;
746 target
->acdirmin
= source
->acdirmin
;
747 target
->acdirmax
= source
->acdirmax
;
748 target
->caps
= source
->caps
;
752 * Allocate and initialise a server record
754 static struct nfs_server
*nfs_alloc_server(void)
756 struct nfs_server
*server
;
758 server
= kzalloc(sizeof(struct nfs_server
), GFP_KERNEL
);
762 server
->client
= server
->client_acl
= ERR_PTR(-EINVAL
);
764 /* Zero out the NFS state stuff */
765 INIT_LIST_HEAD(&server
->client_link
);
766 INIT_LIST_HEAD(&server
->master_link
);
768 server
->io_stats
= nfs_alloc_iostats();
769 if (!server
->io_stats
) {
778 * Free up a server record
780 void nfs_free_server(struct nfs_server
*server
)
782 dprintk("--> nfs_free_server()\n");
784 spin_lock(&nfs_client_lock
);
785 list_del(&server
->client_link
);
786 list_del(&server
->master_link
);
787 spin_unlock(&nfs_client_lock
);
789 if (server
->destroy
!= NULL
)
790 server
->destroy(server
);
791 if (!IS_ERR(server
->client
))
792 rpc_shutdown_client(server
->client
);
794 nfs_put_client(server
->nfs_client
);
796 nfs_free_iostats(server
->io_stats
);
798 nfs_release_automount_timer();
799 dprintk("<-- nfs_free_server()\n");
803 * Create a version 2 or 3 volume record
804 * - keyed on server and FSID
806 struct nfs_server
*nfs_create_server(const struct nfs_mount_data
*data
,
807 struct nfs_fh
*mntfh
)
809 struct nfs_server
*server
;
810 struct nfs_fattr fattr
;
813 server
= nfs_alloc_server();
815 return ERR_PTR(-ENOMEM
);
817 /* Get a client representation */
818 error
= nfs_init_server(server
, data
);
822 BUG_ON(!server
->nfs_client
);
823 BUG_ON(!server
->nfs_client
->rpc_ops
);
824 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
826 /* Probe the root fh to retrieve its FSID */
827 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
830 if (!(fattr
.valid
& NFS_ATTR_FATTR
)) {
831 error
= server
->nfs_client
->rpc_ops
->getattr(server
, mntfh
, &fattr
);
833 dprintk("nfs_create_server: getattr error = %d\n", -error
);
837 memcpy(&server
->fsid
, &fattr
.fsid
, sizeof(server
->fsid
));
839 dprintk("Server FSID: %llx:%llx\n", server
->fsid
.major
, server
->fsid
.minor
);
841 BUG_ON(!server
->nfs_client
);
842 BUG_ON(!server
->nfs_client
->rpc_ops
);
843 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
845 spin_lock(&nfs_client_lock
);
846 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
847 list_add_tail(&server
->master_link
, &nfs_volume_list
);
848 spin_unlock(&nfs_client_lock
);
850 server
->mount_time
= jiffies
;
854 nfs_free_server(server
);
855 return ERR_PTR(error
);
860 * Initialise an NFS4 client record
862 static int nfs4_init_client(struct nfs_client
*clp
,
863 int proto
, int timeo
, int retrans
,
864 rpc_authflavor_t authflavour
)
868 if (clp
->cl_cons_state
== NFS_CS_READY
) {
869 /* the client is initialised already */
870 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp
);
874 /* Check NFS protocol revision and initialize RPC op vector */
875 clp
->rpc_ops
= &nfs_v4_clientops
;
877 error
= nfs_create_rpc_client(clp
, proto
, timeo
, retrans
, authflavour
);
881 error
= nfs_idmap_new(clp
);
883 dprintk("%s: failed to create idmapper. Error = %d\n",
884 __FUNCTION__
, error
);
887 __set_bit(NFS_CS_IDMAP
, &clp
->cl_res_state
);
889 nfs_mark_client_ready(clp
, NFS_CS_READY
);
893 nfs_mark_client_ready(clp
, error
);
894 dprintk("<-- nfs4_init_client() = xerror %d\n", error
);
899 * Set up an NFS4 client
901 static int nfs4_set_client(struct nfs_server
*server
,
902 const char *hostname
, const struct sockaddr_in
*addr
,
903 rpc_authflavor_t authflavour
,
904 int proto
, int timeo
, int retrans
)
906 struct nfs_client
*clp
;
909 dprintk("--> nfs4_set_client()\n");
911 /* Allocate or find a client reference we can use */
912 clp
= nfs_get_client(hostname
, addr
, 4);
914 error
= PTR_ERR(clp
);
917 error
= nfs4_init_client(clp
, proto
, timeo
, retrans
, authflavour
);
921 server
->nfs_client
= clp
;
922 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp
);
928 dprintk("<-- nfs4_set_client() = xerror %d\n", error
);
933 * Create a version 4 volume record
935 static int nfs4_init_server(struct nfs_server
*server
,
936 const struct nfs4_mount_data
*data
, rpc_authflavor_t authflavour
)
940 dprintk("--> nfs4_init_server()\n");
942 /* Initialise the client representation from the mount data */
943 server
->flags
= data
->flags
& NFS_MOUNT_FLAGMASK
;
944 server
->caps
|= NFS_CAP_ATOMIC_OPEN
;
947 server
->rsize
= nfs_block_size(data
->rsize
, NULL
);
949 server
->wsize
= nfs_block_size(data
->wsize
, NULL
);
951 server
->acregmin
= data
->acregmin
* HZ
;
952 server
->acregmax
= data
->acregmax
* HZ
;
953 server
->acdirmin
= data
->acdirmin
* HZ
;
954 server
->acdirmax
= data
->acdirmax
* HZ
;
956 error
= nfs_init_server_rpcclient(server
, authflavour
);
959 dprintk("<-- nfs4_init_server() = %d\n", error
);
964 * Create a version 4 volume record
965 * - keyed on server and FSID
967 struct nfs_server
*nfs4_create_server(const struct nfs4_mount_data
*data
,
968 const char *hostname
,
969 const struct sockaddr_in
*addr
,
972 rpc_authflavor_t authflavour
,
973 struct nfs_fh
*mntfh
)
975 struct nfs_fattr fattr
;
976 struct nfs_server
*server
;
979 dprintk("--> nfs4_create_server()\n");
981 server
= nfs_alloc_server();
983 return ERR_PTR(-ENOMEM
);
985 /* Get a client record */
986 error
= nfs4_set_client(server
, hostname
, addr
, authflavour
,
987 data
->proto
, data
->timeo
, data
->retrans
);
991 /* set up the general RPC client */
992 error
= nfs4_init_server(server
, data
, authflavour
);
996 BUG_ON(!server
->nfs_client
);
997 BUG_ON(!server
->nfs_client
->rpc_ops
);
998 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1000 /* Probe the root fh to retrieve its FSID */
1001 error
= nfs4_path_walk(server
, mntfh
, mntpath
);
1005 dprintk("Server FSID: %llx:%llx\n", server
->fsid
.major
, server
->fsid
.minor
);
1006 dprintk("Mount FH: %d\n", mntfh
->size
);
1008 error
= nfs_probe_fsinfo(server
, mntfh
, &fattr
);
1012 BUG_ON(!server
->nfs_client
);
1013 BUG_ON(!server
->nfs_client
->rpc_ops
);
1014 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1016 spin_lock(&nfs_client_lock
);
1017 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1018 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1019 spin_unlock(&nfs_client_lock
);
1021 server
->mount_time
= jiffies
;
1022 dprintk("<-- nfs4_create_server() = %p\n", server
);
1026 nfs_free_server(server
);
1027 dprintk("<-- nfs4_create_server() = error %d\n", error
);
1028 return ERR_PTR(error
);
1032 * Create an NFS4 referral server record
1034 struct nfs_server
*nfs4_create_referral_server(struct nfs_clone_mount
*data
,
1037 struct nfs_client
*parent_client
;
1038 struct nfs_server
*server
, *parent_server
;
1039 struct nfs_fattr fattr
;
1042 dprintk("--> nfs4_create_referral_server()\n");
1044 server
= nfs_alloc_server();
1046 return ERR_PTR(-ENOMEM
);
1048 parent_server
= NFS_SB(data
->sb
);
1049 parent_client
= parent_server
->nfs_client
;
1051 /* Get a client representation.
1052 * Note: NFSv4 always uses TCP, */
1053 error
= nfs4_set_client(server
, data
->hostname
, data
->addr
,
1055 parent_server
->client
->cl_xprt
->prot
,
1056 parent_client
->retrans_timeo
,
1057 parent_client
->retrans_count
);
1059 /* Initialise the client representation from the parent server */
1060 nfs_server_copy_userdata(server
, parent_server
);
1061 server
->caps
|= NFS_CAP_ATOMIC_OPEN
;
1063 error
= nfs_init_server_rpcclient(server
, data
->authflavor
);
1067 BUG_ON(!server
->nfs_client
);
1068 BUG_ON(!server
->nfs_client
->rpc_ops
);
1069 BUG_ON(!server
->nfs_client
->rpc_ops
->file_inode_ops
);
1071 /* probe the filesystem info for this server filesystem */
1072 error
= nfs_probe_fsinfo(server
, fh
, &fattr
);
1076 dprintk("Referral FSID: %llx:%llx\n",
1077 server
->fsid
.major
, server
->fsid
.minor
);
1079 spin_lock(&nfs_client_lock
);
1080 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1081 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1082 spin_unlock(&nfs_client_lock
);
1084 server
->mount_time
= jiffies
;
1086 dprintk("<-- nfs_create_referral_server() = %p\n", server
);
1090 nfs_free_server(server
);
1091 dprintk("<-- nfs4_create_referral_server() = error %d\n", error
);
1092 return ERR_PTR(error
);
1095 #endif /* CONFIG_NFS_V4 */
1098 * Clone an NFS2, NFS3 or NFS4 server record
1100 struct nfs_server
*nfs_clone_server(struct nfs_server
*source
,
1102 struct nfs_fattr
*fattr
)
1104 struct nfs_server
*server
;
1105 struct nfs_fattr fattr_fsinfo
;
1108 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1109 fattr
->fsid
.major
, fattr
->fsid
.minor
);
1111 server
= nfs_alloc_server();
1113 return ERR_PTR(-ENOMEM
);
1115 /* Copy data from the source */
1116 server
->nfs_client
= source
->nfs_client
;
1117 atomic_inc(&server
->nfs_client
->cl_count
);
1118 nfs_server_copy_userdata(server
, source
);
1120 server
->fsid
= fattr
->fsid
;
1122 error
= nfs_init_server_rpcclient(server
, source
->client
->cl_auth
->au_flavor
);
1124 goto out_free_server
;
1125 if (!IS_ERR(source
->client_acl
))
1126 nfs_init_server_aclclient(server
);
1128 /* probe the filesystem info for this server filesystem */
1129 error
= nfs_probe_fsinfo(server
, fh
, &fattr_fsinfo
);
1131 goto out_free_server
;
1133 dprintk("Cloned FSID: %llx:%llx\n",
1134 server
->fsid
.major
, server
->fsid
.minor
);
1136 error
= nfs_start_lockd(server
);
1138 goto out_free_server
;
1140 spin_lock(&nfs_client_lock
);
1141 list_add_tail(&server
->client_link
, &server
->nfs_client
->cl_superblocks
);
1142 list_add_tail(&server
->master_link
, &nfs_volume_list
);
1143 spin_unlock(&nfs_client_lock
);
1145 server
->mount_time
= jiffies
;
1147 dprintk("<-- nfs_clone_server() = %p\n", server
);
1151 nfs_free_server(server
);
1152 dprintk("<-- nfs_clone_server() = error %d\n", error
);
1153 return ERR_PTR(error
);
1156 #ifdef CONFIG_PROC_FS
1157 static struct proc_dir_entry
*proc_fs_nfs
;
1159 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
);
1160 static void *nfs_server_list_start(struct seq_file
*p
, loff_t
*pos
);
1161 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1162 static void nfs_server_list_stop(struct seq_file
*p
, void *v
);
1163 static int nfs_server_list_show(struct seq_file
*m
, void *v
);
1165 static struct seq_operations nfs_server_list_ops
= {
1166 .start
= nfs_server_list_start
,
1167 .next
= nfs_server_list_next
,
1168 .stop
= nfs_server_list_stop
,
1169 .show
= nfs_server_list_show
,
1172 static struct file_operations nfs_server_list_fops
= {
1173 .open
= nfs_server_list_open
,
1175 .llseek
= seq_lseek
,
1176 .release
= seq_release
,
1179 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
);
1180 static void *nfs_volume_list_start(struct seq_file
*p
, loff_t
*pos
);
1181 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
);
1182 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
);
1183 static int nfs_volume_list_show(struct seq_file
*m
, void *v
);
1185 static struct seq_operations nfs_volume_list_ops
= {
1186 .start
= nfs_volume_list_start
,
1187 .next
= nfs_volume_list_next
,
1188 .stop
= nfs_volume_list_stop
,
1189 .show
= nfs_volume_list_show
,
1192 static struct file_operations nfs_volume_list_fops
= {
1193 .open
= nfs_volume_list_open
,
1195 .llseek
= seq_lseek
,
1196 .release
= seq_release
,
1200 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1203 static int nfs_server_list_open(struct inode
*inode
, struct file
*file
)
1208 ret
= seq_open(file
, &nfs_server_list_ops
);
1212 m
= file
->private_data
;
1213 m
->private = PDE(inode
)->data
;
1219 * set up the iterator to start reading from the server list and return the first item
1221 static void *nfs_server_list_start(struct seq_file
*m
, loff_t
*_pos
)
1223 struct list_head
*_p
;
1226 /* lock the list against modification */
1227 spin_lock(&nfs_client_lock
);
1229 /* allow for the header line */
1231 return SEQ_START_TOKEN
;
1234 /* find the n'th element in the list */
1235 list_for_each(_p
, &nfs_client_list
)
1239 return _p
!= &nfs_client_list
? _p
: NULL
;
1243 * move to next server
1245 static void *nfs_server_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1247 struct list_head
*_p
;
1252 _p
= (v
== SEQ_START_TOKEN
) ? nfs_client_list
.next
: _p
->next
;
1254 return _p
!= &nfs_client_list
? _p
: NULL
;
1258 * clean up after reading from the transports list
1260 static void nfs_server_list_stop(struct seq_file
*p
, void *v
)
1262 spin_unlock(&nfs_client_lock
);
1266 * display a header line followed by a load of call lines
1268 static int nfs_server_list_show(struct seq_file
*m
, void *v
)
1270 struct nfs_client
*clp
;
1272 /* display header on line 1 */
1273 if (v
== SEQ_START_TOKEN
) {
1274 seq_puts(m
, "NV SERVER PORT USE HOSTNAME\n");
1278 /* display one transport per line on subsequent lines */
1279 clp
= list_entry(v
, struct nfs_client
, cl_share_link
);
1281 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1283 NIPQUAD(clp
->cl_addr
.sin_addr
),
1284 ntohs(clp
->cl_addr
.sin_port
),
1285 atomic_read(&clp
->cl_count
),
1292 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1294 static int nfs_volume_list_open(struct inode
*inode
, struct file
*file
)
1299 ret
= seq_open(file
, &nfs_volume_list_ops
);
1303 m
= file
->private_data
;
1304 m
->private = PDE(inode
)->data
;
1310 * set up the iterator to start reading from the volume list and return the first item
1312 static void *nfs_volume_list_start(struct seq_file
*m
, loff_t
*_pos
)
1314 struct list_head
*_p
;
1317 /* lock the list against modification */
1318 spin_lock(&nfs_client_lock
);
1320 /* allow for the header line */
1322 return SEQ_START_TOKEN
;
1325 /* find the n'th element in the list */
1326 list_for_each(_p
, &nfs_volume_list
)
1330 return _p
!= &nfs_volume_list
? _p
: NULL
;
1334 * move to next volume
1336 static void *nfs_volume_list_next(struct seq_file
*p
, void *v
, loff_t
*pos
)
1338 struct list_head
*_p
;
1343 _p
= (v
== SEQ_START_TOKEN
) ? nfs_volume_list
.next
: _p
->next
;
1345 return _p
!= &nfs_volume_list
? _p
: NULL
;
1349 * clean up after reading from the transports list
1351 static void nfs_volume_list_stop(struct seq_file
*p
, void *v
)
1353 spin_unlock(&nfs_client_lock
);
1357 * display a header line followed by a load of call lines
1359 static int nfs_volume_list_show(struct seq_file
*m
, void *v
)
1361 struct nfs_server
*server
;
1362 struct nfs_client
*clp
;
1363 char dev
[8], fsid
[17];
1365 /* display header on line 1 */
1366 if (v
== SEQ_START_TOKEN
) {
1367 seq_puts(m
, "NV SERVER PORT DEV FSID\n");
1370 /* display one transport per line on subsequent lines */
1371 server
= list_entry(v
, struct nfs_server
, master_link
);
1372 clp
= server
->nfs_client
;
1374 snprintf(dev
, 8, "%u:%u",
1375 MAJOR(server
->s_dev
), MINOR(server
->s_dev
));
1377 snprintf(fsid
, 17, "%llx:%llx",
1378 server
->fsid
.major
, server
->fsid
.minor
);
1380 seq_printf(m
, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1382 NIPQUAD(clp
->cl_addr
.sin_addr
),
1383 ntohs(clp
->cl_addr
.sin_port
),
1391 * initialise the /proc/fs/nfsfs/ directory
1393 int __init
nfs_fs_proc_init(void)
1395 struct proc_dir_entry
*p
;
1397 proc_fs_nfs
= proc_mkdir("nfsfs", proc_root_fs
);
1401 proc_fs_nfs
->owner
= THIS_MODULE
;
1403 /* a file of servers with which we're dealing */
1404 p
= create_proc_entry("servers", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1408 p
->proc_fops
= &nfs_server_list_fops
;
1409 p
->owner
= THIS_MODULE
;
1411 /* a file of volumes that we have mounted */
1412 p
= create_proc_entry("volumes", S_IFREG
|S_IRUGO
, proc_fs_nfs
);
1416 p
->proc_fops
= &nfs_volume_list_fops
;
1417 p
->owner
= THIS_MODULE
;
1421 remove_proc_entry("servers", proc_fs_nfs
);
1423 remove_proc_entry("nfsfs", proc_root_fs
);
1429 * clean up the /proc/fs/nfsfs/ directory
1431 void nfs_fs_proc_exit(void)
1433 remove_proc_entry("volumes", proc_fs_nfs
);
1434 remove_proc_entry("servers", proc_fs_nfs
);
1435 remove_proc_entry("nfsfs", proc_root_fs
);
1438 #endif /* CONFIG_PROC_FS */