Staging: hv: mouse_vsc.c: fix brace coding style issues
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / client.c
blobbd3ca32879e79db9de680e650475e2704de7d566
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>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/stats.h>
25 #include <linux/sunrpc/metrics.h>
26 #include <linux/sunrpc/xprtsock.h>
27 #include <linux/sunrpc/xprtrdma.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/nfs4_mount.h>
31 #include <linux/lockd/bind.h>
32 #include <linux/seq_file.h>
33 #include <linux/mount.h>
34 #include <linux/nfs_idmap.h>
35 #include <linux/vfs.h>
36 #include <linux/inet.h>
37 #include <linux/in6.h>
38 #include <linux/slab.h>
39 #include <net/ipv6.h>
40 #include <linux/nfs_xdr.h>
41 #include <linux/sunrpc/bc_xprt.h>
43 #include <asm/system.h>
45 #include "nfs4_fs.h"
46 #include "callback.h"
47 #include "delegation.h"
48 #include "iostat.h"
49 #include "internal.h"
50 #include "fscache.h"
51 #include "pnfs.h"
53 #define NFSDBG_FACILITY NFSDBG_CLIENT
55 static DEFINE_SPINLOCK(nfs_client_lock);
56 static LIST_HEAD(nfs_client_list);
57 static LIST_HEAD(nfs_volume_list);
58 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59 #ifdef CONFIG_NFS_V4
60 static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */
63 * Get a unique NFSv4.0 callback identifier which will be used
64 * by the V4.0 callback service to lookup the nfs_client struct
66 static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
68 int ret = 0;
70 if (clp->rpc_ops->version != 4 || minorversion != 0)
71 return ret;
72 retry:
73 if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL))
74 return -ENOMEM;
75 spin_lock(&nfs_client_lock);
76 ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident);
77 spin_unlock(&nfs_client_lock);
78 if (ret == -EAGAIN)
79 goto retry;
80 return ret;
82 #endif /* CONFIG_NFS_V4 */
85 * RPC cruft for NFS
87 static struct rpc_version *nfs_version[5] = {
88 [2] = &nfs_version2,
89 #ifdef CONFIG_NFS_V3
90 [3] = &nfs_version3,
91 #endif
92 #ifdef CONFIG_NFS_V4
93 [4] = &nfs_version4,
94 #endif
97 struct rpc_program nfs_program = {
98 .name = "nfs",
99 .number = NFS_PROGRAM,
100 .nrvers = ARRAY_SIZE(nfs_version),
101 .version = nfs_version,
102 .stats = &nfs_rpcstat,
103 .pipe_dir_name = "/nfs",
106 struct rpc_stat nfs_rpcstat = {
107 .program = &nfs_program
111 #ifdef CONFIG_NFS_V3_ACL
112 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
113 static struct rpc_version * nfsacl_version[] = {
114 [3] = &nfsacl_version3,
117 struct rpc_program nfsacl_program = {
118 .name = "nfsacl",
119 .number = NFS_ACL_PROGRAM,
120 .nrvers = ARRAY_SIZE(nfsacl_version),
121 .version = nfsacl_version,
122 .stats = &nfsacl_rpcstat,
124 #endif /* CONFIG_NFS_V3_ACL */
126 struct nfs_client_initdata {
127 const char *hostname;
128 const struct sockaddr *addr;
129 size_t addrlen;
130 const struct nfs_rpc_ops *rpc_ops;
131 int proto;
132 u32 minorversion;
136 * Allocate a shared client record
138 * Since these are allocated/deallocated very rarely, we don't
139 * bother putting them in a slab cache...
141 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
143 struct nfs_client *clp;
144 struct rpc_cred *cred;
145 int err = -ENOMEM;
147 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
148 goto error_0;
150 clp->rpc_ops = cl_init->rpc_ops;
152 atomic_set(&clp->cl_count, 1);
153 clp->cl_cons_state = NFS_CS_INITING;
155 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
156 clp->cl_addrlen = cl_init->addrlen;
158 if (cl_init->hostname) {
159 err = -ENOMEM;
160 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
161 if (!clp->cl_hostname)
162 goto error_cleanup;
165 INIT_LIST_HEAD(&clp->cl_superblocks);
166 clp->cl_rpcclient = ERR_PTR(-EINVAL);
168 clp->cl_proto = cl_init->proto;
170 #ifdef CONFIG_NFS_V4
171 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
172 if (err)
173 goto error_cleanup;
175 spin_lock_init(&clp->cl_lock);
176 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
177 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
178 clp->cl_boot_time = CURRENT_TIME;
179 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
180 clp->cl_minorversion = cl_init->minorversion;
181 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
182 #endif
183 cred = rpc_lookup_machine_cred();
184 if (!IS_ERR(cred))
185 clp->cl_machine_cred = cred;
186 #if defined(CONFIG_NFS_V4_1)
187 INIT_LIST_HEAD(&clp->cl_layouts);
188 #endif
189 nfs_fscache_get_client_cookie(clp);
191 return clp;
193 error_cleanup:
194 kfree(clp);
195 error_0:
196 return ERR_PTR(err);
199 #ifdef CONFIG_NFS_V4
200 #ifdef CONFIG_NFS_V4_1
201 static void nfs4_shutdown_session(struct nfs_client *clp)
203 if (nfs4_has_session(clp))
204 nfs4_destroy_session(clp->cl_session);
206 #else /* CONFIG_NFS_V4_1 */
207 static void nfs4_shutdown_session(struct nfs_client *clp)
210 #endif /* CONFIG_NFS_V4_1 */
213 * Destroy the NFS4 callback service
215 static void nfs4_destroy_callback(struct nfs_client *clp)
217 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
218 nfs_callback_down(clp->cl_mvops->minor_version);
221 static void nfs4_shutdown_client(struct nfs_client *clp)
223 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
224 nfs4_kill_renewd(clp);
225 nfs4_shutdown_session(clp);
226 nfs4_destroy_callback(clp);
227 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
228 nfs_idmap_delete(clp);
230 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
233 /* idr_remove_all is not needed as all id's are removed by nfs_put_client */
234 void nfs_cleanup_cb_ident_idr(void)
236 idr_destroy(&cb_ident_idr);
239 /* nfs_client_lock held */
240 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
242 if (clp->cl_cb_ident)
243 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
246 static void pnfs_init_server(struct nfs_server *server)
248 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
251 #else
252 static void nfs4_shutdown_client(struct nfs_client *clp)
256 void nfs_cleanup_cb_ident_idr(void)
260 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
264 static void pnfs_init_server(struct nfs_server *server)
268 #endif /* CONFIG_NFS_V4 */
271 * Destroy a shared client record
273 static void nfs_free_client(struct nfs_client *clp)
275 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
277 nfs4_shutdown_client(clp);
279 nfs_fscache_release_client_cookie(clp);
281 /* -EIO all pending I/O */
282 if (!IS_ERR(clp->cl_rpcclient))
283 rpc_shutdown_client(clp->cl_rpcclient);
285 if (clp->cl_machine_cred != NULL)
286 put_rpccred(clp->cl_machine_cred);
288 kfree(clp->cl_hostname);
289 kfree(clp);
291 dprintk("<-- nfs_free_client()\n");
295 * Release a reference to a shared client record
297 void nfs_put_client(struct nfs_client *clp)
299 if (!clp)
300 return;
302 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
304 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
305 list_del(&clp->cl_share_link);
306 nfs_cb_idr_remove_locked(clp);
307 spin_unlock(&nfs_client_lock);
309 BUG_ON(!list_empty(&clp->cl_superblocks));
311 nfs_free_client(clp);
314 EXPORT_SYMBOL_GPL(nfs_put_client);
316 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
318 * Test if two ip6 socket addresses refer to the same socket by
319 * comparing relevant fields. The padding bytes specifically, are not
320 * compared. sin6_flowinfo is not compared because it only affects QoS
321 * and sin6_scope_id is only compared if the address is "link local"
322 * because "link local" addresses need only be unique to a specific
323 * link. Conversely, ordinary unicast addresses might have different
324 * sin6_scope_id.
326 * The caller should ensure both socket addresses are AF_INET6.
328 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
329 const struct sockaddr *sa2)
331 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
332 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
334 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
335 sin1->sin6_scope_id != sin2->sin6_scope_id)
336 return 0;
338 return ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr);
340 #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
341 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
342 const struct sockaddr *sa2)
344 return 0;
346 #endif
349 * Test if two ip4 socket addresses refer to the same socket, by
350 * comparing relevant fields. The padding bytes specifically, are
351 * not compared.
353 * The caller should ensure both socket addresses are AF_INET.
355 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
356 const struct sockaddr *sa2)
358 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
359 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
361 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
364 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
365 const struct sockaddr *sa2)
367 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
368 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
370 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
371 (sin1->sin6_port == sin2->sin6_port);
374 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
375 const struct sockaddr *sa2)
377 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
378 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
380 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
381 (sin1->sin_port == sin2->sin_port);
385 * Test if two socket addresses represent the same actual socket,
386 * by comparing (only) relevant fields, excluding the port number.
388 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
389 const struct sockaddr *sa2)
391 if (sa1->sa_family != sa2->sa_family)
392 return 0;
394 switch (sa1->sa_family) {
395 case AF_INET:
396 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
397 case AF_INET6:
398 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
400 return 0;
404 * Test if two socket addresses represent the same actual socket,
405 * by comparing (only) relevant fields, including the port number.
407 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
408 const struct sockaddr *sa2)
410 if (sa1->sa_family != sa2->sa_family)
411 return 0;
413 switch (sa1->sa_family) {
414 case AF_INET:
415 return nfs_sockaddr_cmp_ip4(sa1, sa2);
416 case AF_INET6:
417 return nfs_sockaddr_cmp_ip6(sa1, sa2);
419 return 0;
422 /* Common match routine for v4.0 and v4.1 callback services */
423 bool
424 nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
425 u32 minorversion)
427 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
429 /* Don't match clients that failed to initialise */
430 if (!(clp->cl_cons_state == NFS_CS_READY ||
431 clp->cl_cons_state == NFS_CS_SESSION_INITING))
432 return false;
434 /* Match the version and minorversion */
435 if (clp->rpc_ops->version != 4 ||
436 clp->cl_minorversion != minorversion)
437 return false;
439 /* Match only the IP address, not the port number */
440 if (!nfs_sockaddr_match_ipaddr(addr, clap))
441 return false;
443 return true;
447 * Find an nfs_client on the list that matches the initialisation data
448 * that is supplied.
450 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
452 struct nfs_client *clp;
453 const struct sockaddr *sap = data->addr;
455 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
456 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
457 /* Don't match clients that failed to initialise properly */
458 if (clp->cl_cons_state < 0)
459 continue;
461 /* Different NFS versions cannot share the same nfs_client */
462 if (clp->rpc_ops != data->rpc_ops)
463 continue;
465 if (clp->cl_proto != data->proto)
466 continue;
467 /* Match nfsv4 minorversion */
468 if (clp->cl_minorversion != data->minorversion)
469 continue;
470 /* Match the full socket address */
471 if (!nfs_sockaddr_cmp(sap, clap))
472 continue;
474 atomic_inc(&clp->cl_count);
475 return clp;
477 return NULL;
481 * Look up a client by IP address and protocol version
482 * - creates a new record if one doesn't yet exist
484 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
486 struct nfs_client *clp, *new = NULL;
487 int error;
489 dprintk("--> nfs_get_client(%s,v%u)\n",
490 cl_init->hostname ?: "", cl_init->rpc_ops->version);
492 /* see if the client already exists */
493 do {
494 spin_lock(&nfs_client_lock);
496 clp = nfs_match_client(cl_init);
497 if (clp)
498 goto found_client;
499 if (new)
500 goto install_client;
502 spin_unlock(&nfs_client_lock);
504 new = nfs_alloc_client(cl_init);
505 } while (!IS_ERR(new));
507 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
508 return new;
510 /* install a new client and return with it unready */
511 install_client:
512 clp = new;
513 list_add(&clp->cl_share_link, &nfs_client_list);
514 spin_unlock(&nfs_client_lock);
515 dprintk("--> nfs_get_client() = %p [new]\n", clp);
516 return clp;
518 /* found an existing client
519 * - make sure it's ready before returning
521 found_client:
522 spin_unlock(&nfs_client_lock);
524 if (new)
525 nfs_free_client(new);
527 error = wait_event_killable(nfs_client_active_wq,
528 clp->cl_cons_state < NFS_CS_INITING);
529 if (error < 0) {
530 nfs_put_client(clp);
531 return ERR_PTR(-ERESTARTSYS);
534 if (clp->cl_cons_state < NFS_CS_READY) {
535 error = clp->cl_cons_state;
536 nfs_put_client(clp);
537 return ERR_PTR(error);
540 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
542 dprintk("--> nfs_get_client() = %p [share]\n", clp);
543 return clp;
547 * Mark a server as ready or failed
549 void nfs_mark_client_ready(struct nfs_client *clp, int state)
551 clp->cl_cons_state = state;
552 wake_up_all(&nfs_client_active_wq);
556 * With sessions, the client is not marked ready until after a
557 * successful EXCHANGE_ID and CREATE_SESSION.
559 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
560 * other versions of NFS can be tried.
562 int nfs4_check_client_ready(struct nfs_client *clp)
564 if (!nfs4_has_session(clp))
565 return 0;
566 if (clp->cl_cons_state < NFS_CS_READY)
567 return -EPROTONOSUPPORT;
568 return 0;
572 * Initialise the timeout values for a connection
574 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
575 unsigned int timeo, unsigned int retrans)
577 to->to_initval = timeo * HZ / 10;
578 to->to_retries = retrans;
580 switch (proto) {
581 case XPRT_TRANSPORT_TCP:
582 case XPRT_TRANSPORT_RDMA:
583 if (to->to_retries == 0)
584 to->to_retries = NFS_DEF_TCP_RETRANS;
585 if (to->to_initval == 0)
586 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
587 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
588 to->to_initval = NFS_MAX_TCP_TIMEOUT;
589 to->to_increment = to->to_initval;
590 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
591 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
592 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
593 if (to->to_maxval < to->to_initval)
594 to->to_maxval = to->to_initval;
595 to->to_exponential = 0;
596 break;
597 case XPRT_TRANSPORT_UDP:
598 if (to->to_retries == 0)
599 to->to_retries = NFS_DEF_UDP_RETRANS;
600 if (!to->to_initval)
601 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
602 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
603 to->to_initval = NFS_MAX_UDP_TIMEOUT;
604 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
605 to->to_exponential = 1;
606 break;
607 default:
608 BUG();
613 * Create an RPC client handle
615 static int nfs_create_rpc_client(struct nfs_client *clp,
616 const struct rpc_timeout *timeparms,
617 rpc_authflavor_t flavor,
618 int discrtry, int noresvport)
620 struct rpc_clnt *clnt = NULL;
621 struct rpc_create_args args = {
622 .net = &init_net,
623 .protocol = clp->cl_proto,
624 .address = (struct sockaddr *)&clp->cl_addr,
625 .addrsize = clp->cl_addrlen,
626 .timeout = timeparms,
627 .servername = clp->cl_hostname,
628 .program = &nfs_program,
629 .version = clp->rpc_ops->version,
630 .authflavor = flavor,
633 if (discrtry)
634 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
635 if (noresvport)
636 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
638 if (!IS_ERR(clp->cl_rpcclient))
639 return 0;
641 clnt = rpc_create(&args);
642 if (IS_ERR(clnt)) {
643 dprintk("%s: cannot create RPC client. Error = %ld\n",
644 __func__, PTR_ERR(clnt));
645 return PTR_ERR(clnt);
648 clp->cl_rpcclient = clnt;
649 return 0;
653 * Version 2 or 3 client destruction
655 static void nfs_destroy_server(struct nfs_server *server)
657 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
658 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
659 nlmclnt_done(server->nlm_host);
663 * Version 2 or 3 lockd setup
665 static int nfs_start_lockd(struct nfs_server *server)
667 struct nlm_host *host;
668 struct nfs_client *clp = server->nfs_client;
669 struct nlmclnt_initdata nlm_init = {
670 .hostname = clp->cl_hostname,
671 .address = (struct sockaddr *)&clp->cl_addr,
672 .addrlen = clp->cl_addrlen,
673 .nfs_version = clp->rpc_ops->version,
674 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
675 1 : 0,
678 if (nlm_init.nfs_version > 3)
679 return 0;
680 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
681 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
682 return 0;
684 switch (clp->cl_proto) {
685 default:
686 nlm_init.protocol = IPPROTO_TCP;
687 break;
688 case XPRT_TRANSPORT_UDP:
689 nlm_init.protocol = IPPROTO_UDP;
692 host = nlmclnt_init(&nlm_init);
693 if (IS_ERR(host))
694 return PTR_ERR(host);
696 server->nlm_host = host;
697 server->destroy = nfs_destroy_server;
698 return 0;
702 * Initialise an NFSv3 ACL client connection
704 #ifdef CONFIG_NFS_V3_ACL
705 static void nfs_init_server_aclclient(struct nfs_server *server)
707 if (server->nfs_client->rpc_ops->version != 3)
708 goto out_noacl;
709 if (server->flags & NFS_MOUNT_NOACL)
710 goto out_noacl;
712 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
713 if (IS_ERR(server->client_acl))
714 goto out_noacl;
716 /* No errors! Assume that Sun nfsacls are supported */
717 server->caps |= NFS_CAP_ACLS;
718 return;
720 out_noacl:
721 server->caps &= ~NFS_CAP_ACLS;
723 #else
724 static inline void nfs_init_server_aclclient(struct nfs_server *server)
726 server->flags &= ~NFS_MOUNT_NOACL;
727 server->caps &= ~NFS_CAP_ACLS;
729 #endif
732 * Create a general RPC client
734 static int nfs_init_server_rpcclient(struct nfs_server *server,
735 const struct rpc_timeout *timeo,
736 rpc_authflavor_t pseudoflavour)
738 struct nfs_client *clp = server->nfs_client;
740 server->client = rpc_clone_client(clp->cl_rpcclient);
741 if (IS_ERR(server->client)) {
742 dprintk("%s: couldn't create rpc_client!\n", __func__);
743 return PTR_ERR(server->client);
746 memcpy(&server->client->cl_timeout_default,
747 timeo,
748 sizeof(server->client->cl_timeout_default));
749 server->client->cl_timeout = &server->client->cl_timeout_default;
751 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
752 struct rpc_auth *auth;
754 auth = rpcauth_create(pseudoflavour, server->client);
755 if (IS_ERR(auth)) {
756 dprintk("%s: couldn't create credcache!\n", __func__);
757 return PTR_ERR(auth);
760 server->client->cl_softrtry = 0;
761 if (server->flags & NFS_MOUNT_SOFT)
762 server->client->cl_softrtry = 1;
764 return 0;
768 * Initialise an NFS2 or NFS3 client
770 static int nfs_init_client(struct nfs_client *clp,
771 const struct rpc_timeout *timeparms,
772 const struct nfs_parsed_mount_data *data)
774 int error;
776 if (clp->cl_cons_state == NFS_CS_READY) {
777 /* the client is already initialised */
778 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
779 return 0;
783 * Create a client RPC handle for doing FSSTAT with UNIX auth only
784 * - RFC 2623, sec 2.3.2
786 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
787 0, data->flags & NFS_MOUNT_NORESVPORT);
788 if (error < 0)
789 goto error;
790 nfs_mark_client_ready(clp, NFS_CS_READY);
791 return 0;
793 error:
794 nfs_mark_client_ready(clp, error);
795 dprintk("<-- nfs_init_client() = xerror %d\n", error);
796 return error;
800 * Create a version 2 or 3 client
802 static int nfs_init_server(struct nfs_server *server,
803 const struct nfs_parsed_mount_data *data)
805 struct nfs_client_initdata cl_init = {
806 .hostname = data->nfs_server.hostname,
807 .addr = (const struct sockaddr *)&data->nfs_server.address,
808 .addrlen = data->nfs_server.addrlen,
809 .rpc_ops = &nfs_v2_clientops,
810 .proto = data->nfs_server.protocol,
812 struct rpc_timeout timeparms;
813 struct nfs_client *clp;
814 int error;
816 dprintk("--> nfs_init_server()\n");
818 #ifdef CONFIG_NFS_V3
819 if (data->version == 3)
820 cl_init.rpc_ops = &nfs_v3_clientops;
821 #endif
823 /* Allocate or find a client reference we can use */
824 clp = nfs_get_client(&cl_init);
825 if (IS_ERR(clp)) {
826 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
827 return PTR_ERR(clp);
830 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
831 data->timeo, data->retrans);
832 error = nfs_init_client(clp, &timeparms, data);
833 if (error < 0)
834 goto error;
836 server->nfs_client = clp;
838 /* Initialise the client representation from the mount data */
839 server->flags = data->flags;
840 server->options = data->options;
841 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
842 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
843 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
845 if (data->rsize)
846 server->rsize = nfs_block_size(data->rsize, NULL);
847 if (data->wsize)
848 server->wsize = nfs_block_size(data->wsize, NULL);
850 server->acregmin = data->acregmin * HZ;
851 server->acregmax = data->acregmax * HZ;
852 server->acdirmin = data->acdirmin * HZ;
853 server->acdirmax = data->acdirmax * HZ;
855 /* Start lockd here, before we might error out */
856 error = nfs_start_lockd(server);
857 if (error < 0)
858 goto error;
860 server->port = data->nfs_server.port;
862 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
863 if (error < 0)
864 goto error;
866 /* Preserve the values of mount_server-related mount options */
867 if (data->mount_server.addrlen) {
868 memcpy(&server->mountd_address, &data->mount_server.address,
869 data->mount_server.addrlen);
870 server->mountd_addrlen = data->mount_server.addrlen;
872 server->mountd_version = data->mount_server.version;
873 server->mountd_port = data->mount_server.port;
874 server->mountd_protocol = data->mount_server.protocol;
876 server->namelen = data->namlen;
877 /* Create a client RPC handle for the NFSv3 ACL management interface */
878 nfs_init_server_aclclient(server);
879 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
880 return 0;
882 error:
883 server->nfs_client = NULL;
884 nfs_put_client(clp);
885 dprintk("<-- nfs_init_server() = xerror %d\n", error);
886 return error;
890 * Load up the server record from information gained in an fsinfo record
892 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
894 unsigned long max_rpc_payload;
896 /* Work out a lot of parameters */
897 if (server->rsize == 0)
898 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
899 if (server->wsize == 0)
900 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
902 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
903 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
904 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
905 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
907 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
908 if (server->rsize > max_rpc_payload)
909 server->rsize = max_rpc_payload;
910 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
911 server->rsize = NFS_MAX_FILE_IO_SIZE;
912 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
914 server->backing_dev_info.name = "nfs";
915 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
917 if (server->wsize > max_rpc_payload)
918 server->wsize = max_rpc_payload;
919 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
920 server->wsize = NFS_MAX_FILE_IO_SIZE;
921 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
922 set_pnfs_layoutdriver(server, fsinfo->layouttype);
924 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
926 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
927 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
928 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
929 if (server->dtsize > server->rsize)
930 server->dtsize = server->rsize;
932 if (server->flags & NFS_MOUNT_NOAC) {
933 server->acregmin = server->acregmax = 0;
934 server->acdirmin = server->acdirmax = 0;
937 server->maxfilesize = fsinfo->maxfilesize;
939 server->time_delta = fsinfo->time_delta;
941 /* We're airborne Set socket buffersize */
942 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
946 * Probe filesystem information, including the FSID on v2/v3
948 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
950 struct nfs_fsinfo fsinfo;
951 struct nfs_client *clp = server->nfs_client;
952 int error;
954 dprintk("--> nfs_probe_fsinfo()\n");
956 if (clp->rpc_ops->set_capabilities != NULL) {
957 error = clp->rpc_ops->set_capabilities(server, mntfh);
958 if (error < 0)
959 goto out_error;
962 fsinfo.fattr = fattr;
963 fsinfo.layouttype = 0;
964 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
965 if (error < 0)
966 goto out_error;
968 nfs_server_set_fsinfo(server, &fsinfo);
970 /* Get some general file system info */
971 if (server->namelen == 0) {
972 struct nfs_pathconf pathinfo;
974 pathinfo.fattr = fattr;
975 nfs_fattr_init(fattr);
977 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
978 server->namelen = pathinfo.max_namelen;
981 dprintk("<-- nfs_probe_fsinfo() = 0\n");
982 return 0;
984 out_error:
985 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
986 return error;
990 * Copy useful information when duplicating a server record
992 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
994 target->flags = source->flags;
995 target->rsize = source->rsize;
996 target->wsize = source->wsize;
997 target->acregmin = source->acregmin;
998 target->acregmax = source->acregmax;
999 target->acdirmin = source->acdirmin;
1000 target->acdirmax = source->acdirmax;
1001 target->caps = source->caps;
1002 target->options = source->options;
1005 static void nfs_server_insert_lists(struct nfs_server *server)
1007 struct nfs_client *clp = server->nfs_client;
1009 spin_lock(&nfs_client_lock);
1010 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1011 list_add_tail(&server->master_link, &nfs_volume_list);
1012 spin_unlock(&nfs_client_lock);
1016 static void nfs_server_remove_lists(struct nfs_server *server)
1018 spin_lock(&nfs_client_lock);
1019 list_del_rcu(&server->client_link);
1020 list_del(&server->master_link);
1021 spin_unlock(&nfs_client_lock);
1023 synchronize_rcu();
1027 * Allocate and initialise a server record
1029 static struct nfs_server *nfs_alloc_server(void)
1031 struct nfs_server *server;
1033 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1034 if (!server)
1035 return NULL;
1037 server->client = server->client_acl = ERR_PTR(-EINVAL);
1039 /* Zero out the NFS state stuff */
1040 INIT_LIST_HEAD(&server->client_link);
1041 INIT_LIST_HEAD(&server->master_link);
1042 INIT_LIST_HEAD(&server->delegations);
1044 atomic_set(&server->active, 0);
1046 server->io_stats = nfs_alloc_iostats();
1047 if (!server->io_stats) {
1048 kfree(server);
1049 return NULL;
1052 if (bdi_init(&server->backing_dev_info)) {
1053 nfs_free_iostats(server->io_stats);
1054 kfree(server);
1055 return NULL;
1058 pnfs_init_server(server);
1060 return server;
1064 * Free up a server record
1066 void nfs_free_server(struct nfs_server *server)
1068 dprintk("--> nfs_free_server()\n");
1070 nfs_server_remove_lists(server);
1071 unset_pnfs_layoutdriver(server);
1073 if (server->destroy != NULL)
1074 server->destroy(server);
1076 if (!IS_ERR(server->client_acl))
1077 rpc_shutdown_client(server->client_acl);
1078 if (!IS_ERR(server->client))
1079 rpc_shutdown_client(server->client);
1081 nfs_put_client(server->nfs_client);
1083 nfs_free_iostats(server->io_stats);
1084 bdi_destroy(&server->backing_dev_info);
1085 kfree(server);
1086 nfs_release_automount_timer();
1087 dprintk("<-- nfs_free_server()\n");
1091 * Create a version 2 or 3 volume record
1092 * - keyed on server and FSID
1094 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
1095 struct nfs_fh *mntfh)
1097 struct nfs_server *server;
1098 struct nfs_fattr *fattr;
1099 int error;
1101 server = nfs_alloc_server();
1102 if (!server)
1103 return ERR_PTR(-ENOMEM);
1105 error = -ENOMEM;
1106 fattr = nfs_alloc_fattr();
1107 if (fattr == NULL)
1108 goto error;
1110 /* Get a client representation */
1111 error = nfs_init_server(server, data);
1112 if (error < 0)
1113 goto error;
1115 BUG_ON(!server->nfs_client);
1116 BUG_ON(!server->nfs_client->rpc_ops);
1117 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1119 /* Probe the root fh to retrieve its FSID */
1120 error = nfs_probe_fsinfo(server, mntfh, fattr);
1121 if (error < 0)
1122 goto error;
1123 if (server->nfs_client->rpc_ops->version == 3) {
1124 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1125 server->namelen = NFS3_MAXNAMLEN;
1126 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1127 server->caps |= NFS_CAP_READDIRPLUS;
1128 } else {
1129 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1130 server->namelen = NFS2_MAXNAMLEN;
1133 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1134 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
1135 if (error < 0) {
1136 dprintk("nfs_create_server: getattr error = %d\n", -error);
1137 goto error;
1140 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1142 dprintk("Server FSID: %llx:%llx\n",
1143 (unsigned long long) server->fsid.major,
1144 (unsigned long long) server->fsid.minor);
1146 nfs_server_insert_lists(server);
1147 server->mount_time = jiffies;
1148 nfs_free_fattr(fattr);
1149 return server;
1151 error:
1152 nfs_free_fattr(fattr);
1153 nfs_free_server(server);
1154 return ERR_PTR(error);
1157 #ifdef CONFIG_NFS_V4
1159 * NFSv4.0 callback thread helper
1161 * Find a client by IP address, protocol version, and minorversion
1163 * Called from the pg_authenticate method. The callback identifier
1164 * is not used as it has not been decoded.
1166 * Returns NULL if no such client
1168 struct nfs_client *
1169 nfs4_find_client_no_ident(const struct sockaddr *addr)
1171 struct nfs_client *clp;
1173 spin_lock(&nfs_client_lock);
1174 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1175 if (nfs4_cb_match_client(addr, clp, 0) == false)
1176 continue;
1177 atomic_inc(&clp->cl_count);
1178 spin_unlock(&nfs_client_lock);
1179 return clp;
1181 spin_unlock(&nfs_client_lock);
1182 return NULL;
1186 * NFSv4.0 callback thread helper
1188 * Find a client by callback identifier
1190 struct nfs_client *
1191 nfs4_find_client_ident(int cb_ident)
1193 struct nfs_client *clp;
1195 spin_lock(&nfs_client_lock);
1196 clp = idr_find(&cb_ident_idr, cb_ident);
1197 if (clp)
1198 atomic_inc(&clp->cl_count);
1199 spin_unlock(&nfs_client_lock);
1200 return clp;
1203 #if defined(CONFIG_NFS_V4_1)
1205 * NFSv4.1 callback thread helper
1206 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1207 * minorversion, and sessionID
1209 * Returns NULL if no such client
1211 struct nfs_client *
1212 nfs4_find_client_sessionid(const struct sockaddr *addr,
1213 struct nfs4_sessionid *sid)
1215 struct nfs_client *clp;
1217 spin_lock(&nfs_client_lock);
1218 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1219 if (nfs4_cb_match_client(addr, clp, 1) == false)
1220 continue;
1222 if (!nfs4_has_session(clp))
1223 continue;
1225 /* Match sessionid*/
1226 if (memcmp(clp->cl_session->sess_id.data,
1227 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1228 continue;
1230 atomic_inc(&clp->cl_count);
1231 spin_unlock(&nfs_client_lock);
1232 return clp;
1234 spin_unlock(&nfs_client_lock);
1235 return NULL;
1238 #else /* CONFIG_NFS_V4_1 */
1240 struct nfs_client *
1241 nfs4_find_client_sessionid(const struct sockaddr *addr,
1242 struct nfs4_sessionid *sid)
1244 return NULL;
1246 #endif /* CONFIG_NFS_V4_1 */
1249 * Initialize the NFS4 callback service
1251 static int nfs4_init_callback(struct nfs_client *clp)
1253 int error;
1255 if (clp->rpc_ops->version == 4) {
1256 if (nfs4_has_session(clp)) {
1257 error = xprt_setup_backchannel(
1258 clp->cl_rpcclient->cl_xprt,
1259 NFS41_BC_MIN_CALLBACKS);
1260 if (error < 0)
1261 return error;
1264 error = nfs_callback_up(clp->cl_mvops->minor_version,
1265 clp->cl_rpcclient->cl_xprt);
1266 if (error < 0) {
1267 dprintk("%s: failed to start callback. Error = %d\n",
1268 __func__, error);
1269 return error;
1271 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1273 return 0;
1277 * Initialize the minor version specific parts of an NFS4 client record
1279 static int nfs4_init_client_minor_version(struct nfs_client *clp)
1281 #if defined(CONFIG_NFS_V4_1)
1282 if (clp->cl_mvops->minor_version) {
1283 struct nfs4_session *session = NULL;
1285 * Create the session and mark it expired.
1286 * When a SEQUENCE operation encounters the expired session
1287 * it will do session recovery to initialize it.
1289 session = nfs4_alloc_session(clp);
1290 if (!session)
1291 return -ENOMEM;
1293 clp->cl_session = session;
1295 * The create session reply races with the server back
1296 * channel probe. Mark the client NFS_CS_SESSION_INITING
1297 * so that the client back channel can find the
1298 * nfs_client struct
1300 clp->cl_cons_state = NFS_CS_SESSION_INITING;
1302 #endif /* CONFIG_NFS_V4_1 */
1304 return nfs4_init_callback(clp);
1308 * Initialise an NFS4 client record
1310 static int nfs4_init_client(struct nfs_client *clp,
1311 const struct rpc_timeout *timeparms,
1312 const char *ip_addr,
1313 rpc_authflavor_t authflavour,
1314 int flags)
1316 int error;
1318 if (clp->cl_cons_state == NFS_CS_READY) {
1319 /* the client is initialised already */
1320 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1321 return 0;
1324 /* Check NFS protocol revision and initialize RPC op vector */
1325 clp->rpc_ops = &nfs_v4_clientops;
1327 error = nfs_create_rpc_client(clp, timeparms, authflavour,
1328 1, flags & NFS_MOUNT_NORESVPORT);
1329 if (error < 0)
1330 goto error;
1331 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1333 error = nfs_idmap_new(clp);
1334 if (error < 0) {
1335 dprintk("%s: failed to create idmapper. Error = %d\n",
1336 __func__, error);
1337 goto error;
1339 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1341 error = nfs4_init_client_minor_version(clp);
1342 if (error < 0)
1343 goto error;
1345 if (!nfs4_has_session(clp))
1346 nfs_mark_client_ready(clp, NFS_CS_READY);
1347 return 0;
1349 error:
1350 nfs_mark_client_ready(clp, error);
1351 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1352 return error;
1356 * Set up an NFS4 client
1358 static int nfs4_set_client(struct nfs_server *server,
1359 const char *hostname,
1360 const struct sockaddr *addr,
1361 const size_t addrlen,
1362 const char *ip_addr,
1363 rpc_authflavor_t authflavour,
1364 int proto, const struct rpc_timeout *timeparms,
1365 u32 minorversion)
1367 struct nfs_client_initdata cl_init = {
1368 .hostname = hostname,
1369 .addr = addr,
1370 .addrlen = addrlen,
1371 .rpc_ops = &nfs_v4_clientops,
1372 .proto = proto,
1373 .minorversion = minorversion,
1375 struct nfs_client *clp;
1376 int error;
1378 dprintk("--> nfs4_set_client()\n");
1380 /* Allocate or find a client reference we can use */
1381 clp = nfs_get_client(&cl_init);
1382 if (IS_ERR(clp)) {
1383 error = PTR_ERR(clp);
1384 goto error;
1386 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1387 server->flags);
1388 if (error < 0)
1389 goto error_put;
1391 server->nfs_client = clp;
1392 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1393 return 0;
1395 error_put:
1396 nfs_put_client(clp);
1397 error:
1398 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1399 return error;
1404 * Session has been established, and the client marked ready.
1405 * Set the mount rsize and wsize with negotiated fore channel
1406 * attributes which will be bound checked in nfs_server_set_fsinfo.
1408 static void nfs4_session_set_rwsize(struct nfs_server *server)
1410 #ifdef CONFIG_NFS_V4_1
1411 struct nfs4_session *sess;
1412 u32 server_resp_sz;
1413 u32 server_rqst_sz;
1415 if (!nfs4_has_session(server->nfs_client))
1416 return;
1417 sess = server->nfs_client->cl_session;
1418 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1419 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1421 if (server->rsize > server_resp_sz)
1422 server->rsize = server_resp_sz;
1423 if (server->wsize > server_rqst_sz)
1424 server->wsize = server_rqst_sz;
1425 #endif /* CONFIG_NFS_V4_1 */
1428 static int nfs4_server_common_setup(struct nfs_server *server,
1429 struct nfs_fh *mntfh)
1431 struct nfs_fattr *fattr;
1432 int error;
1434 BUG_ON(!server->nfs_client);
1435 BUG_ON(!server->nfs_client->rpc_ops);
1436 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1438 fattr = nfs_alloc_fattr();
1439 if (fattr == NULL)
1440 return -ENOMEM;
1442 /* We must ensure the session is initialised first */
1443 error = nfs4_init_session(server);
1444 if (error < 0)
1445 goto out;
1447 /* Probe the root fh to retrieve its FSID and filehandle */
1448 error = nfs4_get_rootfh(server, mntfh);
1449 if (error < 0)
1450 goto out;
1452 dprintk("Server FSID: %llx:%llx\n",
1453 (unsigned long long) server->fsid.major,
1454 (unsigned long long) server->fsid.minor);
1455 dprintk("Mount FH: %d\n", mntfh->size);
1457 nfs4_session_set_rwsize(server);
1459 error = nfs_probe_fsinfo(server, mntfh, fattr);
1460 if (error < 0)
1461 goto out;
1463 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1464 server->namelen = NFS4_MAXNAMLEN;
1466 nfs_server_insert_lists(server);
1467 server->mount_time = jiffies;
1468 out:
1469 nfs_free_fattr(fattr);
1470 return error;
1474 * Create a version 4 volume record
1476 static int nfs4_init_server(struct nfs_server *server,
1477 const struct nfs_parsed_mount_data *data)
1479 struct rpc_timeout timeparms;
1480 int error;
1482 dprintk("--> nfs4_init_server()\n");
1484 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1485 data->timeo, data->retrans);
1487 /* Initialise the client representation from the mount data */
1488 server->flags = data->flags;
1489 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1490 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1491 server->caps |= NFS_CAP_READDIRPLUS;
1492 server->options = data->options;
1494 /* Get a client record */
1495 error = nfs4_set_client(server,
1496 data->nfs_server.hostname,
1497 (const struct sockaddr *)&data->nfs_server.address,
1498 data->nfs_server.addrlen,
1499 data->client_address,
1500 data->auth_flavors[0],
1501 data->nfs_server.protocol,
1502 &timeparms,
1503 data->minorversion);
1504 if (error < 0)
1505 goto error;
1507 if (data->rsize)
1508 server->rsize = nfs_block_size(data->rsize, NULL);
1509 if (data->wsize)
1510 server->wsize = nfs_block_size(data->wsize, NULL);
1512 server->acregmin = data->acregmin * HZ;
1513 server->acregmax = data->acregmax * HZ;
1514 server->acdirmin = data->acdirmin * HZ;
1515 server->acdirmax = data->acdirmax * HZ;
1517 server->port = data->nfs_server.port;
1519 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1521 error:
1522 /* Done */
1523 dprintk("<-- nfs4_init_server() = %d\n", error);
1524 return error;
1528 * Create a version 4 volume record
1529 * - keyed on server and FSID
1531 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1532 struct nfs_fh *mntfh)
1534 struct nfs_server *server;
1535 int error;
1537 dprintk("--> nfs4_create_server()\n");
1539 server = nfs_alloc_server();
1540 if (!server)
1541 return ERR_PTR(-ENOMEM);
1543 /* set up the general RPC client */
1544 error = nfs4_init_server(server, data);
1545 if (error < 0)
1546 goto error;
1548 error = nfs4_server_common_setup(server, mntfh);
1549 if (error < 0)
1550 goto error;
1552 dprintk("<-- nfs4_create_server() = %p\n", server);
1553 return server;
1555 error:
1556 nfs_free_server(server);
1557 dprintk("<-- nfs4_create_server() = error %d\n", error);
1558 return ERR_PTR(error);
1562 * Create an NFS4 referral server record
1564 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1565 struct nfs_fh *mntfh)
1567 struct nfs_client *parent_client;
1568 struct nfs_server *server, *parent_server;
1569 int error;
1571 dprintk("--> nfs4_create_referral_server()\n");
1573 server = nfs_alloc_server();
1574 if (!server)
1575 return ERR_PTR(-ENOMEM);
1577 parent_server = NFS_SB(data->sb);
1578 parent_client = parent_server->nfs_client;
1580 /* Initialise the client representation from the parent server */
1581 nfs_server_copy_userdata(server, parent_server);
1582 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1584 /* Get a client representation.
1585 * Note: NFSv4 always uses TCP, */
1586 error = nfs4_set_client(server, data->hostname,
1587 data->addr,
1588 data->addrlen,
1589 parent_client->cl_ipaddr,
1590 data->authflavor,
1591 parent_server->client->cl_xprt->prot,
1592 parent_server->client->cl_timeout,
1593 parent_client->cl_mvops->minor_version);
1594 if (error < 0)
1595 goto error;
1597 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1598 if (error < 0)
1599 goto error;
1601 error = nfs4_server_common_setup(server, mntfh);
1602 if (error < 0)
1603 goto error;
1605 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1606 return server;
1608 error:
1609 nfs_free_server(server);
1610 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1611 return ERR_PTR(error);
1614 #endif /* CONFIG_NFS_V4 */
1617 * Clone an NFS2, NFS3 or NFS4 server record
1619 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1620 struct nfs_fh *fh,
1621 struct nfs_fattr *fattr)
1623 struct nfs_server *server;
1624 struct nfs_fattr *fattr_fsinfo;
1625 int error;
1627 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1628 (unsigned long long) fattr->fsid.major,
1629 (unsigned long long) fattr->fsid.minor);
1631 server = nfs_alloc_server();
1632 if (!server)
1633 return ERR_PTR(-ENOMEM);
1635 error = -ENOMEM;
1636 fattr_fsinfo = nfs_alloc_fattr();
1637 if (fattr_fsinfo == NULL)
1638 goto out_free_server;
1640 /* Copy data from the source */
1641 server->nfs_client = source->nfs_client;
1642 atomic_inc(&server->nfs_client->cl_count);
1643 nfs_server_copy_userdata(server, source);
1645 server->fsid = fattr->fsid;
1647 error = nfs_init_server_rpcclient(server,
1648 source->client->cl_timeout,
1649 source->client->cl_auth->au_flavor);
1650 if (error < 0)
1651 goto out_free_server;
1652 if (!IS_ERR(source->client_acl))
1653 nfs_init_server_aclclient(server);
1655 /* probe the filesystem info for this server filesystem */
1656 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1657 if (error < 0)
1658 goto out_free_server;
1660 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1661 server->namelen = NFS4_MAXNAMLEN;
1663 dprintk("Cloned FSID: %llx:%llx\n",
1664 (unsigned long long) server->fsid.major,
1665 (unsigned long long) server->fsid.minor);
1667 error = nfs_start_lockd(server);
1668 if (error < 0)
1669 goto out_free_server;
1671 nfs_server_insert_lists(server);
1672 server->mount_time = jiffies;
1674 nfs_free_fattr(fattr_fsinfo);
1675 dprintk("<-- nfs_clone_server() = %p\n", server);
1676 return server;
1678 out_free_server:
1679 nfs_free_fattr(fattr_fsinfo);
1680 nfs_free_server(server);
1681 dprintk("<-- nfs_clone_server() = error %d\n", error);
1682 return ERR_PTR(error);
1685 #ifdef CONFIG_PROC_FS
1686 static struct proc_dir_entry *proc_fs_nfs;
1688 static int nfs_server_list_open(struct inode *inode, struct file *file);
1689 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1690 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1691 static void nfs_server_list_stop(struct seq_file *p, void *v);
1692 static int nfs_server_list_show(struct seq_file *m, void *v);
1694 static const struct seq_operations nfs_server_list_ops = {
1695 .start = nfs_server_list_start,
1696 .next = nfs_server_list_next,
1697 .stop = nfs_server_list_stop,
1698 .show = nfs_server_list_show,
1701 static const struct file_operations nfs_server_list_fops = {
1702 .open = nfs_server_list_open,
1703 .read = seq_read,
1704 .llseek = seq_lseek,
1705 .release = seq_release,
1706 .owner = THIS_MODULE,
1709 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1710 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1711 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1712 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1713 static int nfs_volume_list_show(struct seq_file *m, void *v);
1715 static const struct seq_operations nfs_volume_list_ops = {
1716 .start = nfs_volume_list_start,
1717 .next = nfs_volume_list_next,
1718 .stop = nfs_volume_list_stop,
1719 .show = nfs_volume_list_show,
1722 static const struct file_operations nfs_volume_list_fops = {
1723 .open = nfs_volume_list_open,
1724 .read = seq_read,
1725 .llseek = seq_lseek,
1726 .release = seq_release,
1727 .owner = THIS_MODULE,
1731 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1732 * we're dealing
1734 static int nfs_server_list_open(struct inode *inode, struct file *file)
1736 struct seq_file *m;
1737 int ret;
1739 ret = seq_open(file, &nfs_server_list_ops);
1740 if (ret < 0)
1741 return ret;
1743 m = file->private_data;
1744 m->private = PDE(inode)->data;
1746 return 0;
1750 * set up the iterator to start reading from the server list and return the first item
1752 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1754 /* lock the list against modification */
1755 spin_lock(&nfs_client_lock);
1756 return seq_list_start_head(&nfs_client_list, *_pos);
1760 * move to next server
1762 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1764 return seq_list_next(v, &nfs_client_list, pos);
1768 * clean up after reading from the transports list
1770 static void nfs_server_list_stop(struct seq_file *p, void *v)
1772 spin_unlock(&nfs_client_lock);
1776 * display a header line followed by a load of call lines
1778 static int nfs_server_list_show(struct seq_file *m, void *v)
1780 struct nfs_client *clp;
1782 /* display header on line 1 */
1783 if (v == &nfs_client_list) {
1784 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1785 return 0;
1788 /* display one transport per line on subsequent lines */
1789 clp = list_entry(v, struct nfs_client, cl_share_link);
1791 seq_printf(m, "v%u %s %s %3d %s\n",
1792 clp->rpc_ops->version,
1793 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1794 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1795 atomic_read(&clp->cl_count),
1796 clp->cl_hostname);
1798 return 0;
1802 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1804 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1806 struct seq_file *m;
1807 int ret;
1809 ret = seq_open(file, &nfs_volume_list_ops);
1810 if (ret < 0)
1811 return ret;
1813 m = file->private_data;
1814 m->private = PDE(inode)->data;
1816 return 0;
1820 * set up the iterator to start reading from the volume list and return the first item
1822 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1824 /* lock the list against modification */
1825 spin_lock(&nfs_client_lock);
1826 return seq_list_start_head(&nfs_volume_list, *_pos);
1830 * move to next volume
1832 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1834 return seq_list_next(v, &nfs_volume_list, pos);
1838 * clean up after reading from the transports list
1840 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1842 spin_unlock(&nfs_client_lock);
1846 * display a header line followed by a load of call lines
1848 static int nfs_volume_list_show(struct seq_file *m, void *v)
1850 struct nfs_server *server;
1851 struct nfs_client *clp;
1852 char dev[8], fsid[17];
1854 /* display header on line 1 */
1855 if (v == &nfs_volume_list) {
1856 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
1857 return 0;
1859 /* display one transport per line on subsequent lines */
1860 server = list_entry(v, struct nfs_server, master_link);
1861 clp = server->nfs_client;
1863 snprintf(dev, 8, "%u:%u",
1864 MAJOR(server->s_dev), MINOR(server->s_dev));
1866 snprintf(fsid, 17, "%llx:%llx",
1867 (unsigned long long) server->fsid.major,
1868 (unsigned long long) server->fsid.minor);
1870 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
1871 clp->rpc_ops->version,
1872 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1873 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1874 dev,
1875 fsid,
1876 nfs_server_fscache_state(server));
1878 return 0;
1882 * initialise the /proc/fs/nfsfs/ directory
1884 int __init nfs_fs_proc_init(void)
1886 struct proc_dir_entry *p;
1888 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1889 if (!proc_fs_nfs)
1890 goto error_0;
1892 /* a file of servers with which we're dealing */
1893 p = proc_create("servers", S_IFREG|S_IRUGO,
1894 proc_fs_nfs, &nfs_server_list_fops);
1895 if (!p)
1896 goto error_1;
1898 /* a file of volumes that we have mounted */
1899 p = proc_create("volumes", S_IFREG|S_IRUGO,
1900 proc_fs_nfs, &nfs_volume_list_fops);
1901 if (!p)
1902 goto error_2;
1903 return 0;
1905 error_2:
1906 remove_proc_entry("servers", proc_fs_nfs);
1907 error_1:
1908 remove_proc_entry("fs/nfsfs", NULL);
1909 error_0:
1910 return -ENOMEM;
1914 * clean up the /proc/fs/nfsfs/ directory
1916 void nfs_fs_proc_exit(void)
1918 remove_proc_entry("volumes", proc_fs_nfs);
1919 remove_proc_entry("servers", proc_fs_nfs);
1920 remove_proc_entry("fs/nfsfs", NULL);
1923 #endif /* CONFIG_PROC_FS */