OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / fs / nfs / client.c
blob31778f74357de4bb3f923f6fb6d37382b598ddd1
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 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
87 static bool nfs4_disable_idmapping = true;
90 * RPC cruft for NFS
92 static struct rpc_version *nfs_version[5] = {
93 [2] = &nfs_version2,
94 #ifdef CONFIG_NFS_V3
95 [3] = &nfs_version3,
96 #endif
97 #ifdef CONFIG_NFS_V4
98 [4] = &nfs_version4,
99 #endif
102 struct rpc_program nfs_program = {
103 .name = "nfs",
104 .number = NFS_PROGRAM,
105 .nrvers = ARRAY_SIZE(nfs_version),
106 .version = nfs_version,
107 .stats = &nfs_rpcstat,
108 .pipe_dir_name = NFS_PIPE_DIRNAME,
111 struct rpc_stat nfs_rpcstat = {
112 .program = &nfs_program
116 #ifdef CONFIG_NFS_V3_ACL
117 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
118 static struct rpc_version * nfsacl_version[] = {
119 [3] = &nfsacl_version3,
122 struct rpc_program nfsacl_program = {
123 .name = "nfsacl",
124 .number = NFS_ACL_PROGRAM,
125 .nrvers = ARRAY_SIZE(nfsacl_version),
126 .version = nfsacl_version,
127 .stats = &nfsacl_rpcstat,
129 #endif /* CONFIG_NFS_V3_ACL */
131 struct nfs_client_initdata {
132 const char *hostname;
133 const struct sockaddr *addr;
134 size_t addrlen;
135 const struct nfs_rpc_ops *rpc_ops;
136 int proto;
137 u32 minorversion;
141 * Allocate a shared client record
143 * Since these are allocated/deallocated very rarely, we don't
144 * bother putting them in a slab cache...
146 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
148 struct nfs_client *clp;
149 struct rpc_cred *cred;
150 int err = -ENOMEM;
152 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
153 goto error_0;
155 clp->rpc_ops = cl_init->rpc_ops;
157 atomic_set(&clp->cl_count, 1);
158 clp->cl_cons_state = NFS_CS_INITING;
160 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
161 clp->cl_addrlen = cl_init->addrlen;
163 if (cl_init->hostname) {
164 err = -ENOMEM;
165 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
166 if (!clp->cl_hostname)
167 goto error_cleanup;
170 INIT_LIST_HEAD(&clp->cl_superblocks);
171 clp->cl_rpcclient = ERR_PTR(-EINVAL);
173 clp->cl_proto = cl_init->proto;
175 #ifdef CONFIG_NFS_V4
176 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
177 if (err)
178 goto error_cleanup;
180 spin_lock_init(&clp->cl_lock);
181 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
182 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
183 clp->cl_boot_time = CURRENT_TIME;
184 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
185 clp->cl_minorversion = cl_init->minorversion;
186 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
187 #endif
188 cred = rpc_lookup_machine_cred("*");
189 if (!IS_ERR(cred))
190 clp->cl_machine_cred = cred;
191 nfs_fscache_get_client_cookie(clp);
193 return clp;
195 error_cleanup:
196 kfree(clp);
197 error_0:
198 return ERR_PTR(err);
201 #ifdef CONFIG_NFS_V4
202 #ifdef CONFIG_NFS_V4_1
203 static void nfs4_shutdown_session(struct nfs_client *clp)
205 if (nfs4_has_session(clp))
206 nfs4_destroy_session(clp->cl_session);
208 #else /* CONFIG_NFS_V4_1 */
209 static void nfs4_shutdown_session(struct nfs_client *clp)
212 #endif /* CONFIG_NFS_V4_1 */
215 * Destroy the NFS4 callback service
217 static void nfs4_destroy_callback(struct nfs_client *clp)
219 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
220 nfs_callback_down(clp->cl_mvops->minor_version);
223 static void nfs4_shutdown_client(struct nfs_client *clp)
225 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
226 nfs4_kill_renewd(clp);
227 nfs4_shutdown_session(clp);
228 nfs4_destroy_callback(clp);
229 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
230 nfs_idmap_delete(clp);
232 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
235 /* idr_remove_all is not needed as all id's are removed by nfs_put_client */
236 void nfs_cleanup_cb_ident_idr(void)
238 idr_destroy(&cb_ident_idr);
241 /* nfs_client_lock held */
242 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
244 if (clp->cl_cb_ident)
245 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
248 static void pnfs_init_server(struct nfs_server *server)
250 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
253 static void nfs4_destroy_server(struct nfs_server *server)
255 nfs4_purge_state_owners(server);
258 #else
259 static void nfs4_shutdown_client(struct nfs_client *clp)
263 void nfs_cleanup_cb_ident_idr(void)
267 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
271 static void pnfs_init_server(struct nfs_server *server)
275 #endif /* CONFIG_NFS_V4 */
278 * Destroy a shared client record
280 static void nfs_free_client(struct nfs_client *clp)
282 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
284 nfs4_shutdown_client(clp);
286 nfs_fscache_release_client_cookie(clp);
288 /* -EIO all pending I/O */
289 if (!IS_ERR(clp->cl_rpcclient))
290 rpc_shutdown_client(clp->cl_rpcclient);
292 if (clp->cl_machine_cred != NULL)
293 put_rpccred(clp->cl_machine_cred);
295 nfs4_deviceid_purge_client(clp);
297 kfree(clp->cl_hostname);
298 kfree(clp->server_scope);
299 kfree(clp);
301 dprintk("<-- nfs_free_client()\n");
305 * Release a reference to a shared client record
307 void nfs_put_client(struct nfs_client *clp)
309 if (!clp)
310 return;
312 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
314 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
315 list_del(&clp->cl_share_link);
316 nfs_cb_idr_remove_locked(clp);
317 spin_unlock(&nfs_client_lock);
319 BUG_ON(!list_empty(&clp->cl_superblocks));
321 nfs_free_client(clp);
324 EXPORT_SYMBOL_GPL(nfs_put_client);
326 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
328 * Test if two ip6 socket addresses refer to the same socket by
329 * comparing relevant fields. The padding bytes specifically, are not
330 * compared. sin6_flowinfo is not compared because it only affects QoS
331 * and sin6_scope_id is only compared if the address is "link local"
332 * because "link local" addresses need only be unique to a specific
333 * link. Conversely, ordinary unicast addresses might have different
334 * sin6_scope_id.
336 * The caller should ensure both socket addresses are AF_INET6.
338 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
339 const struct sockaddr *sa2)
341 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
342 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
344 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
345 return 0;
346 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
347 return sin1->sin6_scope_id == sin2->sin6_scope_id;
349 return 1;
351 #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
352 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
353 const struct sockaddr *sa2)
355 return 0;
357 #endif
360 * Test if two ip4 socket addresses refer to the same socket, by
361 * comparing relevant fields. The padding bytes specifically, are
362 * not compared.
364 * The caller should ensure both socket addresses are AF_INET.
366 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
367 const struct sockaddr *sa2)
369 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
370 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
372 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
375 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
376 const struct sockaddr *sa2)
378 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
379 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
381 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
382 (sin1->sin6_port == sin2->sin6_port);
385 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
386 const struct sockaddr *sa2)
388 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
389 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
391 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
392 (sin1->sin_port == sin2->sin_port);
396 * Test if two socket addresses represent the same actual socket,
397 * by comparing (only) relevant fields, excluding the port number.
399 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
400 const struct sockaddr *sa2)
402 if (sa1->sa_family != sa2->sa_family)
403 return 0;
405 switch (sa1->sa_family) {
406 case AF_INET:
407 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
408 case AF_INET6:
409 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
411 return 0;
415 * Test if two socket addresses represent the same actual socket,
416 * by comparing (only) relevant fields, including the port number.
418 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
419 const struct sockaddr *sa2)
421 if (sa1->sa_family != sa2->sa_family)
422 return 0;
424 switch (sa1->sa_family) {
425 case AF_INET:
426 return nfs_sockaddr_cmp_ip4(sa1, sa2);
427 case AF_INET6:
428 return nfs_sockaddr_cmp_ip6(sa1, sa2);
430 return 0;
433 /* Common match routine for v4.0 and v4.1 callback services */
434 bool
435 nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
436 u32 minorversion)
438 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
440 /* Don't match clients that failed to initialise */
441 if (!(clp->cl_cons_state == NFS_CS_READY ||
442 clp->cl_cons_state == NFS_CS_SESSION_INITING))
443 return false;
445 /* Match the version and minorversion */
446 if (clp->rpc_ops->version != 4 ||
447 clp->cl_minorversion != minorversion)
448 return false;
450 /* Match only the IP address, not the port number */
451 if (!nfs_sockaddr_match_ipaddr(addr, clap))
452 return false;
454 return true;
458 * Find an nfs_client on the list that matches the initialisation data
459 * that is supplied.
461 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
463 struct nfs_client *clp;
464 const struct sockaddr *sap = data->addr;
466 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
467 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
468 /* Don't match clients that failed to initialise properly */
469 if (clp->cl_cons_state < 0)
470 continue;
472 /* Different NFS versions cannot share the same nfs_client */
473 if (clp->rpc_ops != data->rpc_ops)
474 continue;
476 if (clp->cl_proto != data->proto)
477 continue;
478 /* Match nfsv4 minorversion */
479 if (clp->cl_minorversion != data->minorversion)
480 continue;
481 /* Match the full socket address */
482 if (!nfs_sockaddr_cmp(sap, clap))
483 continue;
485 atomic_inc(&clp->cl_count);
486 return clp;
488 return NULL;
492 * Look up a client by IP address and protocol version
493 * - creates a new record if one doesn't yet exist
495 static struct nfs_client *
496 nfs_get_client(const struct nfs_client_initdata *cl_init,
497 const struct rpc_timeout *timeparms,
498 const char *ip_addr,
499 rpc_authflavor_t authflavour,
500 int noresvport)
502 struct nfs_client *clp, *new = NULL;
503 int error;
505 dprintk("--> nfs_get_client(%s,v%u)\n",
506 cl_init->hostname ?: "", cl_init->rpc_ops->version);
508 /* see if the client already exists */
509 do {
510 spin_lock(&nfs_client_lock);
512 clp = nfs_match_client(cl_init);
513 if (clp)
514 goto found_client;
515 if (new)
516 goto install_client;
518 spin_unlock(&nfs_client_lock);
520 new = nfs_alloc_client(cl_init);
521 } while (!IS_ERR(new));
523 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
524 return new;
526 /* install a new client and return with it unready */
527 install_client:
528 clp = new;
529 list_add(&clp->cl_share_link, &nfs_client_list);
530 spin_unlock(&nfs_client_lock);
532 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
533 authflavour, noresvport);
534 if (error < 0) {
535 nfs_put_client(clp);
536 return ERR_PTR(error);
538 dprintk("--> nfs_get_client() = %p [new]\n", clp);
539 return clp;
541 /* found an existing client
542 * - make sure it's ready before returning
544 found_client:
545 spin_unlock(&nfs_client_lock);
547 if (new)
548 nfs_free_client(new);
550 error = wait_event_killable(nfs_client_active_wq,
551 clp->cl_cons_state < NFS_CS_INITING);
552 if (error < 0) {
553 nfs_put_client(clp);
554 return ERR_PTR(-ERESTARTSYS);
557 if (clp->cl_cons_state < NFS_CS_READY) {
558 error = clp->cl_cons_state;
559 nfs_put_client(clp);
560 return ERR_PTR(error);
563 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
565 dprintk("--> nfs_get_client() = %p [share]\n", clp);
566 return clp;
570 * Mark a server as ready or failed
572 void nfs_mark_client_ready(struct nfs_client *clp, int state)
574 clp->cl_cons_state = state;
575 wake_up_all(&nfs_client_active_wq);
579 * With sessions, the client is not marked ready until after a
580 * successful EXCHANGE_ID and CREATE_SESSION.
582 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
583 * other versions of NFS can be tried.
585 int nfs4_check_client_ready(struct nfs_client *clp)
587 if (!nfs4_has_session(clp))
588 return 0;
589 if (clp->cl_cons_state < NFS_CS_READY)
590 return -EPROTONOSUPPORT;
591 return 0;
595 * Initialise the timeout values for a connection
597 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
598 unsigned int timeo, unsigned int retrans)
600 to->to_initval = timeo * HZ / 10;
601 to->to_retries = retrans;
603 switch (proto) {
604 case XPRT_TRANSPORT_TCP:
605 case XPRT_TRANSPORT_RDMA:
606 if (to->to_retries == 0)
607 to->to_retries = NFS_DEF_TCP_RETRANS;
608 if (to->to_initval == 0)
609 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
610 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
611 to->to_initval = NFS_MAX_TCP_TIMEOUT;
612 to->to_increment = to->to_initval;
613 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
614 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
615 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
616 if (to->to_maxval < to->to_initval)
617 to->to_maxval = to->to_initval;
618 to->to_exponential = 0;
619 break;
620 case XPRT_TRANSPORT_UDP:
621 if (to->to_retries == 0)
622 to->to_retries = NFS_DEF_UDP_RETRANS;
623 if (!to->to_initval)
624 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
625 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
626 to->to_initval = NFS_MAX_UDP_TIMEOUT;
627 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
628 to->to_exponential = 1;
629 break;
630 default:
631 BUG();
636 * Create an RPC client handle
638 static int nfs_create_rpc_client(struct nfs_client *clp,
639 const struct rpc_timeout *timeparms,
640 rpc_authflavor_t flavor,
641 int discrtry, int noresvport)
643 struct rpc_clnt *clnt = NULL;
644 struct rpc_create_args args = {
645 .net = &init_net,
646 .protocol = clp->cl_proto,
647 .address = (struct sockaddr *)&clp->cl_addr,
648 .addrsize = clp->cl_addrlen,
649 .timeout = timeparms,
650 .servername = clp->cl_hostname,
651 .program = &nfs_program,
652 .version = clp->rpc_ops->version,
653 .authflavor = flavor,
656 if (discrtry)
657 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
658 if (noresvport)
659 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
661 if (!IS_ERR(clp->cl_rpcclient))
662 return 0;
664 clnt = rpc_create(&args);
665 if (IS_ERR(clnt)) {
666 dprintk("%s: cannot create RPC client. Error = %ld\n",
667 __func__, PTR_ERR(clnt));
668 return PTR_ERR(clnt);
671 clp->cl_rpcclient = clnt;
672 return 0;
676 * Version 2 or 3 client destruction
678 static void nfs_destroy_server(struct nfs_server *server)
680 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
681 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
682 nlmclnt_done(server->nlm_host);
686 * Version 2 or 3 lockd setup
688 static int nfs_start_lockd(struct nfs_server *server)
690 struct nlm_host *host;
691 struct nfs_client *clp = server->nfs_client;
692 struct nlmclnt_initdata nlm_init = {
693 .hostname = clp->cl_hostname,
694 .address = (struct sockaddr *)&clp->cl_addr,
695 .addrlen = clp->cl_addrlen,
696 .nfs_version = clp->rpc_ops->version,
697 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
698 1 : 0,
701 if (nlm_init.nfs_version > 3)
702 return 0;
703 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
704 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
705 return 0;
707 switch (clp->cl_proto) {
708 default:
709 nlm_init.protocol = IPPROTO_TCP;
710 break;
711 case XPRT_TRANSPORT_UDP:
712 nlm_init.protocol = IPPROTO_UDP;
715 host = nlmclnt_init(&nlm_init);
716 if (IS_ERR(host))
717 return PTR_ERR(host);
719 server->nlm_host = host;
720 server->destroy = nfs_destroy_server;
721 return 0;
725 * Initialise an NFSv3 ACL client connection
727 #ifdef CONFIG_NFS_V3_ACL
728 static void nfs_init_server_aclclient(struct nfs_server *server)
730 if (server->nfs_client->rpc_ops->version != 3)
731 goto out_noacl;
732 if (server->flags & NFS_MOUNT_NOACL)
733 goto out_noacl;
735 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
736 if (IS_ERR(server->client_acl))
737 goto out_noacl;
739 /* No errors! Assume that Sun nfsacls are supported */
740 server->caps |= NFS_CAP_ACLS;
741 return;
743 out_noacl:
744 server->caps &= ~NFS_CAP_ACLS;
746 #else
747 static inline void nfs_init_server_aclclient(struct nfs_server *server)
749 server->flags &= ~NFS_MOUNT_NOACL;
750 server->caps &= ~NFS_CAP_ACLS;
752 #endif
755 * Create a general RPC client
757 static int nfs_init_server_rpcclient(struct nfs_server *server,
758 const struct rpc_timeout *timeo,
759 rpc_authflavor_t pseudoflavour)
761 struct nfs_client *clp = server->nfs_client;
763 server->client = rpc_clone_client(clp->cl_rpcclient);
764 if (IS_ERR(server->client)) {
765 dprintk("%s: couldn't create rpc_client!\n", __func__);
766 return PTR_ERR(server->client);
769 memcpy(&server->client->cl_timeout_default,
770 timeo,
771 sizeof(server->client->cl_timeout_default));
772 server->client->cl_timeout = &server->client->cl_timeout_default;
774 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
775 struct rpc_auth *auth;
777 auth = rpcauth_create(pseudoflavour, server->client);
778 if (IS_ERR(auth)) {
779 dprintk("%s: couldn't create credcache!\n", __func__);
780 return PTR_ERR(auth);
783 server->client->cl_softrtry = 0;
784 if (server->flags & NFS_MOUNT_SOFT)
785 server->client->cl_softrtry = 1;
787 return 0;
791 * Initialise an NFS2 or NFS3 client
793 int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
794 const char *ip_addr, rpc_authflavor_t authflavour,
795 int noresvport)
797 int error;
799 if (clp->cl_cons_state == NFS_CS_READY) {
800 /* the client is already initialised */
801 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
802 return 0;
806 * Create a client RPC handle for doing FSSTAT with UNIX auth only
807 * - RFC 2623, sec 2.3.2
809 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
810 0, noresvport);
811 if (error < 0)
812 goto error;
813 nfs_mark_client_ready(clp, NFS_CS_READY);
814 return 0;
816 error:
817 nfs_mark_client_ready(clp, error);
818 dprintk("<-- nfs_init_client() = xerror %d\n", error);
819 return error;
823 * Create a version 2 or 3 client
825 static int nfs_init_server(struct nfs_server *server,
826 const struct nfs_parsed_mount_data *data)
828 struct nfs_client_initdata cl_init = {
829 .hostname = data->nfs_server.hostname,
830 .addr = (const struct sockaddr *)&data->nfs_server.address,
831 .addrlen = data->nfs_server.addrlen,
832 .rpc_ops = &nfs_v2_clientops,
833 .proto = data->nfs_server.protocol,
835 struct rpc_timeout timeparms;
836 struct nfs_client *clp;
837 int error;
839 dprintk("--> nfs_init_server()\n");
841 #ifdef CONFIG_NFS_V3
842 if (data->version == 3)
843 cl_init.rpc_ops = &nfs_v3_clientops;
844 #endif
846 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
847 data->timeo, data->retrans);
849 /* Allocate or find a client reference we can use */
850 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
851 data->flags & NFS_MOUNT_NORESVPORT);
852 if (IS_ERR(clp)) {
853 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
854 return PTR_ERR(clp);
857 server->nfs_client = clp;
859 /* Initialise the client representation from the mount data */
860 server->flags = data->flags;
861 server->options = data->options;
862 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
863 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
864 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
866 if (data->rsize)
867 server->rsize = nfs_block_size(data->rsize, NULL);
868 if (data->wsize)
869 server->wsize = nfs_block_size(data->wsize, NULL);
871 server->acregmin = data->acregmin * HZ;
872 server->acregmax = data->acregmax * HZ;
873 server->acdirmin = data->acdirmin * HZ;
874 server->acdirmax = data->acdirmax * HZ;
876 /* Start lockd here, before we might error out */
877 error = nfs_start_lockd(server);
878 if (error < 0)
879 goto error;
881 server->port = data->nfs_server.port;
883 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
884 if (error < 0)
885 goto error;
887 /* Preserve the values of mount_server-related mount options */
888 if (data->mount_server.addrlen) {
889 memcpy(&server->mountd_address, &data->mount_server.address,
890 data->mount_server.addrlen);
891 server->mountd_addrlen = data->mount_server.addrlen;
893 server->mountd_version = data->mount_server.version;
894 server->mountd_port = data->mount_server.port;
895 server->mountd_protocol = data->mount_server.protocol;
897 server->namelen = data->namlen;
898 /* Create a client RPC handle for the NFSv3 ACL management interface */
899 nfs_init_server_aclclient(server);
900 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
901 return 0;
903 error:
904 server->nfs_client = NULL;
905 nfs_put_client(clp);
906 dprintk("<-- nfs_init_server() = xerror %d\n", error);
907 return error;
911 * Load up the server record from information gained in an fsinfo record
913 static void nfs_server_set_fsinfo(struct nfs_server *server,
914 struct nfs_fh *mntfh,
915 struct nfs_fsinfo *fsinfo)
917 unsigned long max_rpc_payload;
919 /* Work out a lot of parameters */
920 if (server->rsize == 0)
921 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
922 if (server->wsize == 0)
923 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
925 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
926 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
927 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
928 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
930 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
931 if (server->rsize > max_rpc_payload)
932 server->rsize = max_rpc_payload;
933 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
934 server->rsize = NFS_MAX_FILE_IO_SIZE;
935 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
937 server->backing_dev_info.name = "nfs";
938 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
940 if (server->wsize > max_rpc_payload)
941 server->wsize = max_rpc_payload;
942 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
943 server->wsize = NFS_MAX_FILE_IO_SIZE;
944 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
945 server->pnfs_blksize = fsinfo->blksize;
946 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
948 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
950 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
951 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
952 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
953 if (server->dtsize > server->rsize)
954 server->dtsize = server->rsize;
956 if (server->flags & NFS_MOUNT_NOAC) {
957 server->acregmin = server->acregmax = 0;
958 server->acdirmin = server->acdirmax = 0;
961 server->maxfilesize = fsinfo->maxfilesize;
963 server->time_delta = fsinfo->time_delta;
965 /* We're airborne Set socket buffersize */
966 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
970 * Probe filesystem information, including the FSID on v2/v3
972 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
974 struct nfs_fsinfo fsinfo;
975 struct nfs_client *clp = server->nfs_client;
976 int error;
978 dprintk("--> nfs_probe_fsinfo()\n");
980 if (clp->rpc_ops->set_capabilities != NULL) {
981 error = clp->rpc_ops->set_capabilities(server, mntfh);
982 if (error < 0)
983 goto out_error;
986 fsinfo.fattr = fattr;
987 fsinfo.layouttype = 0;
988 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
989 if (error < 0)
990 goto out_error;
992 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
994 /* Get some general file system info */
995 if (server->namelen == 0) {
996 struct nfs_pathconf pathinfo;
998 pathinfo.fattr = fattr;
999 nfs_fattr_init(fattr);
1001 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1002 server->namelen = pathinfo.max_namelen;
1005 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1006 return 0;
1008 out_error:
1009 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1010 return error;
1014 * Copy useful information when duplicating a server record
1016 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1018 target->flags = source->flags;
1019 target->rsize = source->rsize;
1020 target->wsize = source->wsize;
1021 target->acregmin = source->acregmin;
1022 target->acregmax = source->acregmax;
1023 target->acdirmin = source->acdirmin;
1024 target->acdirmax = source->acdirmax;
1025 target->caps = source->caps;
1026 target->options = source->options;
1029 static void nfs_server_insert_lists(struct nfs_server *server)
1031 struct nfs_client *clp = server->nfs_client;
1033 spin_lock(&nfs_client_lock);
1034 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1035 list_add_tail(&server->master_link, &nfs_volume_list);
1036 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1037 spin_unlock(&nfs_client_lock);
1041 static void nfs_server_remove_lists(struct nfs_server *server)
1043 struct nfs_client *clp = server->nfs_client;
1045 spin_lock(&nfs_client_lock);
1046 list_del_rcu(&server->client_link);
1047 if (clp && list_empty(&clp->cl_superblocks))
1048 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1049 list_del(&server->master_link);
1050 spin_unlock(&nfs_client_lock);
1052 synchronize_rcu();
1056 * Allocate and initialise a server record
1058 static struct nfs_server *nfs_alloc_server(void)
1060 struct nfs_server *server;
1062 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1063 if (!server)
1064 return NULL;
1066 server->client = server->client_acl = ERR_PTR(-EINVAL);
1068 /* Zero out the NFS state stuff */
1069 INIT_LIST_HEAD(&server->client_link);
1070 INIT_LIST_HEAD(&server->master_link);
1071 INIT_LIST_HEAD(&server->delegations);
1072 INIT_LIST_HEAD(&server->layouts);
1073 INIT_LIST_HEAD(&server->state_owners_lru);
1075 atomic_set(&server->active, 0);
1077 server->io_stats = nfs_alloc_iostats();
1078 if (!server->io_stats) {
1079 kfree(server);
1080 return NULL;
1083 if (bdi_init(&server->backing_dev_info)) {
1084 nfs_free_iostats(server->io_stats);
1085 kfree(server);
1086 return NULL;
1089 pnfs_init_server(server);
1091 return server;
1095 * Free up a server record
1097 void nfs_free_server(struct nfs_server *server)
1099 dprintk("--> nfs_free_server()\n");
1101 nfs_server_remove_lists(server);
1102 unset_pnfs_layoutdriver(server);
1104 if (server->destroy != NULL)
1105 server->destroy(server);
1107 if (!IS_ERR(server->client_acl))
1108 rpc_shutdown_client(server->client_acl);
1109 if (!IS_ERR(server->client))
1110 rpc_shutdown_client(server->client);
1112 nfs_put_client(server->nfs_client);
1114 nfs_free_iostats(server->io_stats);
1115 bdi_destroy(&server->backing_dev_info);
1116 kfree(server);
1117 nfs_release_automount_timer();
1118 dprintk("<-- nfs_free_server()\n");
1122 * Create a version 2 or 3 volume record
1123 * - keyed on server and FSID
1125 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
1126 struct nfs_fh *mntfh)
1128 struct nfs_server *server;
1129 struct nfs_fattr *fattr;
1130 int error;
1132 server = nfs_alloc_server();
1133 if (!server)
1134 return ERR_PTR(-ENOMEM);
1136 error = -ENOMEM;
1137 fattr = nfs_alloc_fattr();
1138 if (fattr == NULL)
1139 goto error;
1141 /* Get a client representation */
1142 error = nfs_init_server(server, data);
1143 if (error < 0)
1144 goto error;
1146 BUG_ON(!server->nfs_client);
1147 BUG_ON(!server->nfs_client->rpc_ops);
1148 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1150 /* Probe the root fh to retrieve its FSID */
1151 error = nfs_probe_fsinfo(server, mntfh, fattr);
1152 if (error < 0)
1153 goto error;
1154 if (server->nfs_client->rpc_ops->version == 3) {
1155 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1156 server->namelen = NFS3_MAXNAMLEN;
1157 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1158 server->caps |= NFS_CAP_READDIRPLUS;
1159 } else {
1160 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1161 server->namelen = NFS2_MAXNAMLEN;
1164 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1165 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
1166 if (error < 0) {
1167 dprintk("nfs_create_server: getattr error = %d\n", -error);
1168 goto error;
1171 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1173 dprintk("Server FSID: %llx:%llx\n",
1174 (unsigned long long) server->fsid.major,
1175 (unsigned long long) server->fsid.minor);
1177 nfs_server_insert_lists(server);
1178 server->mount_time = jiffies;
1179 nfs_free_fattr(fattr);
1180 return server;
1182 error:
1183 nfs_free_fattr(fattr);
1184 nfs_free_server(server);
1185 return ERR_PTR(error);
1188 #ifdef CONFIG_NFS_V4
1190 * NFSv4.0 callback thread helper
1192 * Find a client by IP address, protocol version, and minorversion
1194 * Called from the pg_authenticate method. The callback identifier
1195 * is not used as it has not been decoded.
1197 * Returns NULL if no such client
1199 struct nfs_client *
1200 nfs4_find_client_no_ident(const struct sockaddr *addr)
1202 struct nfs_client *clp;
1204 spin_lock(&nfs_client_lock);
1205 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1206 if (nfs4_cb_match_client(addr, clp, 0) == false)
1207 continue;
1208 atomic_inc(&clp->cl_count);
1209 spin_unlock(&nfs_client_lock);
1210 return clp;
1212 spin_unlock(&nfs_client_lock);
1213 return NULL;
1217 * NFSv4.0 callback thread helper
1219 * Find a client by callback identifier
1221 struct nfs_client *
1222 nfs4_find_client_ident(int cb_ident)
1224 struct nfs_client *clp;
1226 spin_lock(&nfs_client_lock);
1227 clp = idr_find(&cb_ident_idr, cb_ident);
1228 if (clp)
1229 atomic_inc(&clp->cl_count);
1230 spin_unlock(&nfs_client_lock);
1231 return clp;
1234 #if defined(CONFIG_NFS_V4_1)
1236 * NFSv4.1 callback thread helper
1237 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1238 * minorversion, and sessionID
1240 * Returns NULL if no such client
1242 struct nfs_client *
1243 nfs4_find_client_sessionid(const struct sockaddr *addr,
1244 struct nfs4_sessionid *sid)
1246 struct nfs_client *clp;
1248 spin_lock(&nfs_client_lock);
1249 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1250 if (nfs4_cb_match_client(addr, clp, 1) == false)
1251 continue;
1253 if (!nfs4_has_session(clp))
1254 continue;
1256 /* Match sessionid*/
1257 if (memcmp(clp->cl_session->sess_id.data,
1258 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1259 continue;
1261 atomic_inc(&clp->cl_count);
1262 spin_unlock(&nfs_client_lock);
1263 return clp;
1265 spin_unlock(&nfs_client_lock);
1266 return NULL;
1269 #else /* CONFIG_NFS_V4_1 */
1271 struct nfs_client *
1272 nfs4_find_client_sessionid(const struct sockaddr *addr,
1273 struct nfs4_sessionid *sid)
1275 return NULL;
1277 #endif /* CONFIG_NFS_V4_1 */
1280 * Initialize the NFS4 callback service
1282 static int nfs4_init_callback(struct nfs_client *clp)
1284 int error;
1286 if (clp->rpc_ops->version == 4) {
1287 if (nfs4_has_session(clp)) {
1288 error = xprt_setup_backchannel(
1289 clp->cl_rpcclient->cl_xprt,
1290 NFS41_BC_MIN_CALLBACKS);
1291 if (error < 0)
1292 return error;
1295 error = nfs_callback_up(clp->cl_mvops->minor_version,
1296 clp->cl_rpcclient->cl_xprt);
1297 if (error < 0) {
1298 dprintk("%s: failed to start callback. Error = %d\n",
1299 __func__, error);
1300 return error;
1302 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1304 return 0;
1308 * Initialize the minor version specific parts of an NFS4 client record
1310 static int nfs4_init_client_minor_version(struct nfs_client *clp)
1312 #if defined(CONFIG_NFS_V4_1)
1313 if (clp->cl_mvops->minor_version) {
1314 struct nfs4_session *session = NULL;
1316 * Create the session and mark it expired.
1317 * When a SEQUENCE operation encounters the expired session
1318 * it will do session recovery to initialize it.
1320 session = nfs4_alloc_session(clp);
1321 if (!session)
1322 return -ENOMEM;
1324 clp->cl_session = session;
1326 * The create session reply races with the server back
1327 * channel probe. Mark the client NFS_CS_SESSION_INITING
1328 * so that the client back channel can find the
1329 * nfs_client struct
1331 clp->cl_cons_state = NFS_CS_SESSION_INITING;
1333 #endif /* CONFIG_NFS_V4_1 */
1335 return nfs4_init_callback(clp);
1339 * Initialise an NFS4 client record
1341 int nfs4_init_client(struct nfs_client *clp,
1342 const struct rpc_timeout *timeparms,
1343 const char *ip_addr,
1344 rpc_authflavor_t authflavour,
1345 int noresvport)
1347 int error;
1349 if (clp->cl_cons_state == NFS_CS_READY) {
1350 /* the client is initialised already */
1351 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1352 return 0;
1355 /* Check NFS protocol revision and initialize RPC op vector */
1356 clp->rpc_ops = &nfs_v4_clientops;
1358 error = nfs_create_rpc_client(clp, timeparms, authflavour,
1359 1, noresvport);
1360 if (error < 0)
1361 goto error;
1362 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1364 error = nfs_idmap_new(clp);
1365 if (error < 0) {
1366 dprintk("%s: failed to create idmapper. Error = %d\n",
1367 __func__, error);
1368 goto error;
1370 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1372 error = nfs4_init_client_minor_version(clp);
1373 if (error < 0)
1374 goto error;
1376 if (!nfs4_has_session(clp))
1377 nfs_mark_client_ready(clp, NFS_CS_READY);
1378 return 0;
1380 error:
1381 nfs_mark_client_ready(clp, error);
1382 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1383 return error;
1387 * Set up an NFS4 client
1389 static int nfs4_set_client(struct nfs_server *server,
1390 const char *hostname,
1391 const struct sockaddr *addr,
1392 const size_t addrlen,
1393 const char *ip_addr,
1394 rpc_authflavor_t authflavour,
1395 int proto, const struct rpc_timeout *timeparms,
1396 u32 minorversion)
1398 struct nfs_client_initdata cl_init = {
1399 .hostname = hostname,
1400 .addr = addr,
1401 .addrlen = addrlen,
1402 .rpc_ops = &nfs_v4_clientops,
1403 .proto = proto,
1404 .minorversion = minorversion,
1406 struct nfs_client *clp;
1407 int error;
1409 dprintk("--> nfs4_set_client()\n");
1411 /* Allocate or find a client reference we can use */
1412 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1413 server->flags & NFS_MOUNT_NORESVPORT);
1414 if (IS_ERR(clp)) {
1415 error = PTR_ERR(clp);
1416 goto error;
1420 * Query for the lease time on clientid setup or renewal
1422 * Note that this will be set on nfs_clients that were created
1423 * only for the DS role and did not set this bit, but now will
1424 * serve a dual role.
1426 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1428 server->nfs_client = clp;
1429 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1430 return 0;
1431 error:
1432 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1433 return error;
1437 * Set up a pNFS Data Server client.
1439 * Return any existing nfs_client that matches server address,port,version
1440 * and minorversion.
1442 * For a new nfs_client, use a soft mount (default), a low retrans and a
1443 * low timeout interval so that if a connection is lost, we retry through
1444 * the MDS.
1446 struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1447 const struct sockaddr *ds_addr,
1448 int ds_addrlen, int ds_proto)
1450 struct nfs_client_initdata cl_init = {
1451 .addr = ds_addr,
1452 .addrlen = ds_addrlen,
1453 .rpc_ops = &nfs_v4_clientops,
1454 .proto = ds_proto,
1455 .minorversion = mds_clp->cl_minorversion,
1457 struct rpc_timeout ds_timeout = {
1458 .to_initval = 15 * HZ,
1459 .to_maxval = 15 * HZ,
1460 .to_retries = 1,
1461 .to_exponential = 1,
1463 struct nfs_client *clp;
1466 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1467 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1468 * (section 13.1 RFC 5661).
1470 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1471 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1473 dprintk("<-- %s %p\n", __func__, clp);
1474 return clp;
1476 EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
1479 * Session has been established, and the client marked ready.
1480 * Set the mount rsize and wsize with negotiated fore channel
1481 * attributes which will be bound checked in nfs_server_set_fsinfo.
1483 static void nfs4_session_set_rwsize(struct nfs_server *server)
1485 #ifdef CONFIG_NFS_V4_1
1486 struct nfs4_session *sess;
1487 u32 server_resp_sz;
1488 u32 server_rqst_sz;
1490 if (!nfs4_has_session(server->nfs_client))
1491 return;
1492 sess = server->nfs_client->cl_session;
1493 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1494 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1496 if (server->rsize > server_resp_sz)
1497 server->rsize = server_resp_sz;
1498 if (server->wsize > server_rqst_sz)
1499 server->wsize = server_rqst_sz;
1500 #endif /* CONFIG_NFS_V4_1 */
1503 static int nfs4_server_common_setup(struct nfs_server *server,
1504 struct nfs_fh *mntfh)
1506 struct nfs_fattr *fattr;
1507 int error;
1509 BUG_ON(!server->nfs_client);
1510 BUG_ON(!server->nfs_client->rpc_ops);
1511 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1513 /* data servers support only a subset of NFSv4.1 */
1514 if (is_ds_only_client(server->nfs_client))
1515 return -EPROTONOSUPPORT;
1517 fattr = nfs_alloc_fattr();
1518 if (fattr == NULL)
1519 return -ENOMEM;
1521 /* We must ensure the session is initialised first */
1522 error = nfs4_init_session(server);
1523 if (error < 0)
1524 goto out;
1526 /* Probe the root fh to retrieve its FSID and filehandle */
1527 error = nfs4_get_rootfh(server, mntfh);
1528 if (error < 0)
1529 goto out;
1531 dprintk("Server FSID: %llx:%llx\n",
1532 (unsigned long long) server->fsid.major,
1533 (unsigned long long) server->fsid.minor);
1534 dprintk("Mount FH: %d\n", mntfh->size);
1536 nfs4_session_set_rwsize(server);
1538 error = nfs_probe_fsinfo(server, mntfh, fattr);
1539 if (error < 0)
1540 goto out;
1542 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1543 server->namelen = NFS4_MAXNAMLEN;
1545 nfs_server_insert_lists(server);
1546 server->mount_time = jiffies;
1547 server->destroy = nfs4_destroy_server;
1548 out:
1549 nfs_free_fattr(fattr);
1550 return error;
1554 * Create a version 4 volume record
1556 static int nfs4_init_server(struct nfs_server *server,
1557 const struct nfs_parsed_mount_data *data)
1559 struct rpc_timeout timeparms;
1560 int error;
1562 dprintk("--> nfs4_init_server()\n");
1564 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1565 data->timeo, data->retrans);
1567 /* Initialise the client representation from the mount data */
1568 server->flags = data->flags;
1569 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1570 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1571 server->caps |= NFS_CAP_READDIRPLUS;
1572 server->options = data->options;
1574 /* Get a client record */
1575 error = nfs4_set_client(server,
1576 data->nfs_server.hostname,
1577 (const struct sockaddr *)&data->nfs_server.address,
1578 data->nfs_server.addrlen,
1579 data->client_address,
1580 data->auth_flavors[0],
1581 data->nfs_server.protocol,
1582 &timeparms,
1583 data->minorversion);
1584 if (error < 0)
1585 goto error;
1588 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1589 * authentication.
1591 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1592 server->caps |= NFS_CAP_UIDGID_NOMAP;
1594 if (data->rsize)
1595 server->rsize = nfs_block_size(data->rsize, NULL);
1596 if (data->wsize)
1597 server->wsize = nfs_block_size(data->wsize, NULL);
1599 server->acregmin = data->acregmin * HZ;
1600 server->acregmax = data->acregmax * HZ;
1601 server->acdirmin = data->acdirmin * HZ;
1602 server->acdirmax = data->acdirmax * HZ;
1604 server->port = data->nfs_server.port;
1606 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1608 error:
1609 /* Done */
1610 dprintk("<-- nfs4_init_server() = %d\n", error);
1611 return error;
1615 * Create a version 4 volume record
1616 * - keyed on server and FSID
1618 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1619 struct nfs_fh *mntfh)
1621 struct nfs_server *server;
1622 int error;
1624 dprintk("--> nfs4_create_server()\n");
1626 server = nfs_alloc_server();
1627 if (!server)
1628 return ERR_PTR(-ENOMEM);
1630 /* set up the general RPC client */
1631 error = nfs4_init_server(server, data);
1632 if (error < 0)
1633 goto error;
1635 error = nfs4_server_common_setup(server, mntfh);
1636 if (error < 0)
1637 goto error;
1639 dprintk("<-- nfs4_create_server() = %p\n", server);
1640 return server;
1642 error:
1643 nfs_free_server(server);
1644 dprintk("<-- nfs4_create_server() = error %d\n", error);
1645 return ERR_PTR(error);
1649 * Create an NFS4 referral server record
1651 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1652 struct nfs_fh *mntfh)
1654 struct nfs_client *parent_client;
1655 struct nfs_server *server, *parent_server;
1656 int error;
1658 dprintk("--> nfs4_create_referral_server()\n");
1660 server = nfs_alloc_server();
1661 if (!server)
1662 return ERR_PTR(-ENOMEM);
1664 parent_server = NFS_SB(data->sb);
1665 parent_client = parent_server->nfs_client;
1667 /* Initialise the client representation from the parent server */
1668 nfs_server_copy_userdata(server, parent_server);
1669 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1671 /* Get a client representation.
1672 * Note: NFSv4 always uses TCP, */
1673 error = nfs4_set_client(server, data->hostname,
1674 data->addr,
1675 data->addrlen,
1676 parent_client->cl_ipaddr,
1677 data->authflavor,
1678 parent_server->client->cl_xprt->prot,
1679 parent_server->client->cl_timeout,
1680 parent_client->cl_mvops->minor_version);
1681 if (error < 0)
1682 goto error;
1684 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1685 if (error < 0)
1686 goto error;
1688 error = nfs4_server_common_setup(server, mntfh);
1689 if (error < 0)
1690 goto error;
1692 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1693 return server;
1695 error:
1696 nfs_free_server(server);
1697 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1698 return ERR_PTR(error);
1701 #endif /* CONFIG_NFS_V4 */
1704 * Clone an NFS2, NFS3 or NFS4 server record
1706 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1707 struct nfs_fh *fh,
1708 struct nfs_fattr *fattr)
1710 struct nfs_server *server;
1711 struct nfs_fattr *fattr_fsinfo;
1712 int error;
1714 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1715 (unsigned long long) fattr->fsid.major,
1716 (unsigned long long) fattr->fsid.minor);
1718 server = nfs_alloc_server();
1719 if (!server)
1720 return ERR_PTR(-ENOMEM);
1722 error = -ENOMEM;
1723 fattr_fsinfo = nfs_alloc_fattr();
1724 if (fattr_fsinfo == NULL)
1725 goto out_free_server;
1727 /* Copy data from the source */
1728 server->nfs_client = source->nfs_client;
1729 server->destroy = source->destroy;
1730 atomic_inc(&server->nfs_client->cl_count);
1731 nfs_server_copy_userdata(server, source);
1733 server->fsid = fattr->fsid;
1735 error = nfs_init_server_rpcclient(server,
1736 source->client->cl_timeout,
1737 source->client->cl_auth->au_flavor);
1738 if (error < 0)
1739 goto out_free_server;
1740 if (!IS_ERR(source->client_acl))
1741 nfs_init_server_aclclient(server);
1743 /* probe the filesystem info for this server filesystem */
1744 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1745 if (error < 0)
1746 goto out_free_server;
1748 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1749 server->namelen = NFS4_MAXNAMLEN;
1751 dprintk("Cloned FSID: %llx:%llx\n",
1752 (unsigned long long) server->fsid.major,
1753 (unsigned long long) server->fsid.minor);
1755 error = nfs_start_lockd(server);
1756 if (error < 0)
1757 goto out_free_server;
1759 nfs_server_insert_lists(server);
1760 server->mount_time = jiffies;
1762 nfs_free_fattr(fattr_fsinfo);
1763 dprintk("<-- nfs_clone_server() = %p\n", server);
1764 return server;
1766 out_free_server:
1767 nfs_free_fattr(fattr_fsinfo);
1768 nfs_free_server(server);
1769 dprintk("<-- nfs_clone_server() = error %d\n", error);
1770 return ERR_PTR(error);
1773 #ifdef CONFIG_PROC_FS
1774 static struct proc_dir_entry *proc_fs_nfs;
1776 static int nfs_server_list_open(struct inode *inode, struct file *file);
1777 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1778 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1779 static void nfs_server_list_stop(struct seq_file *p, void *v);
1780 static int nfs_server_list_show(struct seq_file *m, void *v);
1782 static const struct seq_operations nfs_server_list_ops = {
1783 .start = nfs_server_list_start,
1784 .next = nfs_server_list_next,
1785 .stop = nfs_server_list_stop,
1786 .show = nfs_server_list_show,
1789 static const struct file_operations nfs_server_list_fops = {
1790 .open = nfs_server_list_open,
1791 .read = seq_read,
1792 .llseek = seq_lseek,
1793 .release = seq_release,
1794 .owner = THIS_MODULE,
1797 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1798 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1799 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1800 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1801 static int nfs_volume_list_show(struct seq_file *m, void *v);
1803 static const struct seq_operations nfs_volume_list_ops = {
1804 .start = nfs_volume_list_start,
1805 .next = nfs_volume_list_next,
1806 .stop = nfs_volume_list_stop,
1807 .show = nfs_volume_list_show,
1810 static const struct file_operations nfs_volume_list_fops = {
1811 .open = nfs_volume_list_open,
1812 .read = seq_read,
1813 .llseek = seq_lseek,
1814 .release = seq_release,
1815 .owner = THIS_MODULE,
1819 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1820 * we're dealing
1822 static int nfs_server_list_open(struct inode *inode, struct file *file)
1824 struct seq_file *m;
1825 int ret;
1827 ret = seq_open(file, &nfs_server_list_ops);
1828 if (ret < 0)
1829 return ret;
1831 m = file->private_data;
1832 m->private = PDE(inode)->data;
1834 return 0;
1838 * set up the iterator to start reading from the server list and return the first item
1840 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1842 /* lock the list against modification */
1843 spin_lock(&nfs_client_lock);
1844 return seq_list_start_head(&nfs_client_list, *_pos);
1848 * move to next server
1850 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1852 return seq_list_next(v, &nfs_client_list, pos);
1856 * clean up after reading from the transports list
1858 static void nfs_server_list_stop(struct seq_file *p, void *v)
1860 spin_unlock(&nfs_client_lock);
1864 * display a header line followed by a load of call lines
1866 static int nfs_server_list_show(struct seq_file *m, void *v)
1868 struct nfs_client *clp;
1870 /* display header on line 1 */
1871 if (v == &nfs_client_list) {
1872 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1873 return 0;
1876 /* display one transport per line on subsequent lines */
1877 clp = list_entry(v, struct nfs_client, cl_share_link);
1879 /* Check if the client is initialized */
1880 if (clp->cl_cons_state != NFS_CS_READY)
1881 return 0;
1883 seq_printf(m, "v%u %s %s %3d %s\n",
1884 clp->rpc_ops->version,
1885 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1886 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1887 atomic_read(&clp->cl_count),
1888 clp->cl_hostname);
1890 return 0;
1894 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1896 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1898 struct seq_file *m;
1899 int ret;
1901 ret = seq_open(file, &nfs_volume_list_ops);
1902 if (ret < 0)
1903 return ret;
1905 m = file->private_data;
1906 m->private = PDE(inode)->data;
1908 return 0;
1912 * set up the iterator to start reading from the volume list and return the first item
1914 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1916 /* lock the list against modification */
1917 spin_lock(&nfs_client_lock);
1918 return seq_list_start_head(&nfs_volume_list, *_pos);
1922 * move to next volume
1924 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1926 return seq_list_next(v, &nfs_volume_list, pos);
1930 * clean up after reading from the transports list
1932 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1934 spin_unlock(&nfs_client_lock);
1938 * display a header line followed by a load of call lines
1940 static int nfs_volume_list_show(struct seq_file *m, void *v)
1942 struct nfs_server *server;
1943 struct nfs_client *clp;
1944 char dev[8], fsid[17];
1946 /* display header on line 1 */
1947 if (v == &nfs_volume_list) {
1948 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
1949 return 0;
1951 /* display one transport per line on subsequent lines */
1952 server = list_entry(v, struct nfs_server, master_link);
1953 clp = server->nfs_client;
1955 snprintf(dev, 8, "%u:%u",
1956 MAJOR(server->s_dev), MINOR(server->s_dev));
1958 snprintf(fsid, 17, "%llx:%llx",
1959 (unsigned long long) server->fsid.major,
1960 (unsigned long long) server->fsid.minor);
1962 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
1963 clp->rpc_ops->version,
1964 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1965 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1966 dev,
1967 fsid,
1968 nfs_server_fscache_state(server));
1970 return 0;
1974 * initialise the /proc/fs/nfsfs/ directory
1976 int __init nfs_fs_proc_init(void)
1978 struct proc_dir_entry *p;
1980 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1981 if (!proc_fs_nfs)
1982 goto error_0;
1984 /* a file of servers with which we're dealing */
1985 p = proc_create("servers", S_IFREG|S_IRUGO,
1986 proc_fs_nfs, &nfs_server_list_fops);
1987 if (!p)
1988 goto error_1;
1990 /* a file of volumes that we have mounted */
1991 p = proc_create("volumes", S_IFREG|S_IRUGO,
1992 proc_fs_nfs, &nfs_volume_list_fops);
1993 if (!p)
1994 goto error_2;
1995 return 0;
1997 error_2:
1998 remove_proc_entry("servers", proc_fs_nfs);
1999 error_1:
2000 remove_proc_entry("fs/nfsfs", NULL);
2001 error_0:
2002 return -ENOMEM;
2006 * clean up the /proc/fs/nfsfs/ directory
2008 void nfs_fs_proc_exit(void)
2010 remove_proc_entry("volumes", proc_fs_nfs);
2011 remove_proc_entry("servers", proc_fs_nfs);
2012 remove_proc_entry("fs/nfsfs", NULL);
2015 #endif /* CONFIG_PROC_FS */
2017 module_param(nfs4_disable_idmapping, bool, 0644);
2018 MODULE_PARM_DESC(nfs4_disable_idmapping,
2019 "Turn off NFSv4 idmapping when using 'sec=sys'");