xref: /openbmc/linux/fs/nfs/client.c (revision b627b4ed)
1 /* client.c: NFS client sharing and management code
2  *
3  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4  * Written by David Howells (dhowells@redhat.com)
5  *
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.
10  */
11 
12 
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 <net/ipv6.h>
39 #include <linux/nfs_xdr.h>
40 
41 #include <asm/system.h>
42 
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47 #include "internal.h"
48 #include "fscache.h"
49 
50 #define NFSDBG_FACILITY		NFSDBG_CLIENT
51 
52 static DEFINE_SPINLOCK(nfs_client_lock);
53 static LIST_HEAD(nfs_client_list);
54 static LIST_HEAD(nfs_volume_list);
55 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56 
57 /*
58  * RPC cruft for NFS
59  */
60 static struct rpc_version *nfs_version[5] = {
61 	[2]			= &nfs_version2,
62 #ifdef CONFIG_NFS_V3
63 	[3]			= &nfs_version3,
64 #endif
65 #ifdef CONFIG_NFS_V4
66 	[4]			= &nfs_version4,
67 #endif
68 };
69 
70 struct rpc_program nfs_program = {
71 	.name			= "nfs",
72 	.number			= NFS_PROGRAM,
73 	.nrvers			= ARRAY_SIZE(nfs_version),
74 	.version		= nfs_version,
75 	.stats			= &nfs_rpcstat,
76 	.pipe_dir_name		= "/nfs",
77 };
78 
79 struct rpc_stat nfs_rpcstat = {
80 	.program		= &nfs_program
81 };
82 
83 
84 #ifdef CONFIG_NFS_V3_ACL
85 static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
86 static struct rpc_version *	nfsacl_version[] = {
87 	[3]			= &nfsacl_version3,
88 };
89 
90 struct rpc_program		nfsacl_program = {
91 	.name			= "nfsacl",
92 	.number			= NFS_ACL_PROGRAM,
93 	.nrvers			= ARRAY_SIZE(nfsacl_version),
94 	.version		= nfsacl_version,
95 	.stats			= &nfsacl_rpcstat,
96 };
97 #endif  /* CONFIG_NFS_V3_ACL */
98 
99 struct nfs_client_initdata {
100 	const char *hostname;
101 	const struct sockaddr *addr;
102 	size_t addrlen;
103 	const struct nfs_rpc_ops *rpc_ops;
104 	int proto;
105 };
106 
107 /*
108  * Allocate a shared client record
109  *
110  * Since these are allocated/deallocated very rarely, we don't
111  * bother putting them in a slab cache...
112  */
113 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
114 {
115 	struct nfs_client *clp;
116 	struct rpc_cred *cred;
117 
118 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
119 		goto error_0;
120 
121 	clp->rpc_ops = cl_init->rpc_ops;
122 
123 	if (cl_init->rpc_ops->version == 4) {
124 		if (nfs_callback_up() < 0)
125 			goto error_2;
126 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
127 	}
128 
129 	atomic_set(&clp->cl_count, 1);
130 	clp->cl_cons_state = NFS_CS_INITING;
131 
132 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
133 	clp->cl_addrlen = cl_init->addrlen;
134 
135 	if (cl_init->hostname) {
136 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
137 		if (!clp->cl_hostname)
138 			goto error_3;
139 	}
140 
141 	INIT_LIST_HEAD(&clp->cl_superblocks);
142 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
143 
144 	clp->cl_proto = cl_init->proto;
145 
146 #ifdef CONFIG_NFS_V4
147 	INIT_LIST_HEAD(&clp->cl_delegations);
148 	spin_lock_init(&clp->cl_lock);
149 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
150 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
151 	clp->cl_boot_time = CURRENT_TIME;
152 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
153 #endif
154 	cred = rpc_lookup_machine_cred();
155 	if (!IS_ERR(cred))
156 		clp->cl_machine_cred = cred;
157 
158 	nfs_fscache_get_client_cookie(clp);
159 
160 	return clp;
161 
162 error_3:
163 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
164 		nfs_callback_down();
165 error_2:
166 	kfree(clp);
167 error_0:
168 	return NULL;
169 }
170 
171 static void nfs4_shutdown_client(struct nfs_client *clp)
172 {
173 #ifdef CONFIG_NFS_V4
174 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
175 		nfs4_kill_renewd(clp);
176 	BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
177 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
178 		nfs_idmap_delete(clp);
179 
180 	rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
181 #endif
182 }
183 
184 /*
185  * Destroy a shared client record
186  */
187 static void nfs_free_client(struct nfs_client *clp)
188 {
189 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
190 
191 	nfs4_shutdown_client(clp);
192 
193 	nfs_fscache_release_client_cookie(clp);
194 
195 	/* -EIO all pending I/O */
196 	if (!IS_ERR(clp->cl_rpcclient))
197 		rpc_shutdown_client(clp->cl_rpcclient);
198 
199 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
200 		nfs_callback_down();
201 
202 	if (clp->cl_machine_cred != NULL)
203 		put_rpccred(clp->cl_machine_cred);
204 
205 	kfree(clp->cl_hostname);
206 	kfree(clp);
207 
208 	dprintk("<-- nfs_free_client()\n");
209 }
210 
211 /*
212  * Release a reference to a shared client record
213  */
214 void nfs_put_client(struct nfs_client *clp)
215 {
216 	if (!clp)
217 		return;
218 
219 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
220 
221 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
222 		list_del(&clp->cl_share_link);
223 		spin_unlock(&nfs_client_lock);
224 
225 		BUG_ON(!list_empty(&clp->cl_superblocks));
226 
227 		nfs_free_client(clp);
228 	}
229 }
230 
231 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
232 /*
233  * Test if two ip6 socket addresses refer to the same socket by
234  * comparing relevant fields. The padding bytes specifically, are not
235  * compared. sin6_flowinfo is not compared because it only affects QoS
236  * and sin6_scope_id is only compared if the address is "link local"
237  * because "link local" addresses need only be unique to a specific
238  * link. Conversely, ordinary unicast addresses might have different
239  * sin6_scope_id.
240  *
241  * The caller should ensure both socket addresses are AF_INET6.
242  */
243 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
244 				      const struct sockaddr *sa2)
245 {
246 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
247 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
248 
249 	if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
250 	    sin1->sin6_scope_id != sin2->sin6_scope_id)
251 		return 0;
252 
253 	return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
254 }
255 #else	/* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
256 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
257 				      const struct sockaddr *sa2)
258 {
259 	return 0;
260 }
261 #endif
262 
263 /*
264  * Test if two ip4 socket addresses refer to the same socket, by
265  * comparing relevant fields. The padding bytes specifically, are
266  * not compared.
267  *
268  * The caller should ensure both socket addresses are AF_INET.
269  */
270 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
271 				      const struct sockaddr *sa2)
272 {
273 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
274 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
275 
276 	return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
277 }
278 
279 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
280 				const struct sockaddr *sa2)
281 {
282 	const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
283 	const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
284 
285 	return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
286 		(sin1->sin6_port == sin2->sin6_port);
287 }
288 
289 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
290 				const struct sockaddr *sa2)
291 {
292 	const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
293 	const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
294 
295 	return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
296 		(sin1->sin_port == sin2->sin_port);
297 }
298 
299 /*
300  * Test if two socket addresses represent the same actual socket,
301  * by comparing (only) relevant fields, excluding the port number.
302  */
303 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
304 				     const struct sockaddr *sa2)
305 {
306 	if (sa1->sa_family != sa2->sa_family)
307 		return 0;
308 
309 	switch (sa1->sa_family) {
310 	case AF_INET:
311 		return nfs_sockaddr_match_ipaddr4(sa1, sa2);
312 	case AF_INET6:
313 		return nfs_sockaddr_match_ipaddr6(sa1, sa2);
314 	}
315 	return 0;
316 }
317 
318 /*
319  * Test if two socket addresses represent the same actual socket,
320  * by comparing (only) relevant fields, including the port number.
321  */
322 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
323 			    const struct sockaddr *sa2)
324 {
325 	if (sa1->sa_family != sa2->sa_family)
326 		return 0;
327 
328 	switch (sa1->sa_family) {
329 	case AF_INET:
330 		return nfs_sockaddr_cmp_ip4(sa1, sa2);
331 	case AF_INET6:
332 		return nfs_sockaddr_cmp_ip6(sa1, sa2);
333 	}
334 	return 0;
335 }
336 
337 /*
338  * Find a client by IP address and protocol version
339  * - returns NULL if no such client
340  */
341 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
342 {
343 	struct nfs_client *clp;
344 
345 	spin_lock(&nfs_client_lock);
346 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
347 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
348 
349 		/* Don't match clients that failed to initialise properly */
350 		if (clp->cl_cons_state != NFS_CS_READY)
351 			continue;
352 
353 		/* Different NFS versions cannot share the same nfs_client */
354 		if (clp->rpc_ops->version != nfsversion)
355 			continue;
356 
357 		/* Match only the IP address, not the port number */
358 		if (!nfs_sockaddr_match_ipaddr(addr, clap))
359 			continue;
360 
361 		atomic_inc(&clp->cl_count);
362 		spin_unlock(&nfs_client_lock);
363 		return clp;
364 	}
365 	spin_unlock(&nfs_client_lock);
366 	return NULL;
367 }
368 
369 /*
370  * Find a client by IP address and protocol version
371  * - returns NULL if no such client
372  */
373 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
374 {
375 	struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
376 	u32 nfsvers = clp->rpc_ops->version;
377 
378 	spin_lock(&nfs_client_lock);
379 	list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
380 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
381 
382 		/* Don't match clients that failed to initialise properly */
383 		if (clp->cl_cons_state != NFS_CS_READY)
384 			continue;
385 
386 		/* Different NFS versions cannot share the same nfs_client */
387 		if (clp->rpc_ops->version != nfsvers)
388 			continue;
389 
390 		/* Match only the IP address, not the port number */
391 		if (!nfs_sockaddr_match_ipaddr(sap, clap))
392 			continue;
393 
394 		atomic_inc(&clp->cl_count);
395 		spin_unlock(&nfs_client_lock);
396 		return clp;
397 	}
398 	spin_unlock(&nfs_client_lock);
399 	return NULL;
400 }
401 
402 /*
403  * Find an nfs_client on the list that matches the initialisation data
404  * that is supplied.
405  */
406 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
407 {
408 	struct nfs_client *clp;
409 	const struct sockaddr *sap = data->addr;
410 
411 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
412 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
413 		/* Don't match clients that failed to initialise properly */
414 		if (clp->cl_cons_state < 0)
415 			continue;
416 
417 		/* Different NFS versions cannot share the same nfs_client */
418 		if (clp->rpc_ops != data->rpc_ops)
419 			continue;
420 
421 		if (clp->cl_proto != data->proto)
422 			continue;
423 
424 		/* Match the full socket address */
425 		if (!nfs_sockaddr_cmp(sap, clap))
426 			continue;
427 
428 		atomic_inc(&clp->cl_count);
429 		return clp;
430 	}
431 	return NULL;
432 }
433 
434 /*
435  * Look up a client by IP address and protocol version
436  * - creates a new record if one doesn't yet exist
437  */
438 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
439 {
440 	struct nfs_client *clp, *new = NULL;
441 	int error;
442 
443 	dprintk("--> nfs_get_client(%s,v%u)\n",
444 		cl_init->hostname ?: "", cl_init->rpc_ops->version);
445 
446 	/* see if the client already exists */
447 	do {
448 		spin_lock(&nfs_client_lock);
449 
450 		clp = nfs_match_client(cl_init);
451 		if (clp)
452 			goto found_client;
453 		if (new)
454 			goto install_client;
455 
456 		spin_unlock(&nfs_client_lock);
457 
458 		new = nfs_alloc_client(cl_init);
459 	} while (new);
460 
461 	return ERR_PTR(-ENOMEM);
462 
463 	/* install a new client and return with it unready */
464 install_client:
465 	clp = new;
466 	list_add(&clp->cl_share_link, &nfs_client_list);
467 	spin_unlock(&nfs_client_lock);
468 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
469 	return clp;
470 
471 	/* found an existing client
472 	 * - make sure it's ready before returning
473 	 */
474 found_client:
475 	spin_unlock(&nfs_client_lock);
476 
477 	if (new)
478 		nfs_free_client(new);
479 
480 	error = wait_event_killable(nfs_client_active_wq,
481 				clp->cl_cons_state != NFS_CS_INITING);
482 	if (error < 0) {
483 		nfs_put_client(clp);
484 		return ERR_PTR(-ERESTARTSYS);
485 	}
486 
487 	if (clp->cl_cons_state < NFS_CS_READY) {
488 		error = clp->cl_cons_state;
489 		nfs_put_client(clp);
490 		return ERR_PTR(error);
491 	}
492 
493 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
494 
495 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
496 	return clp;
497 }
498 
499 /*
500  * Mark a server as ready or failed
501  */
502 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
503 {
504 	clp->cl_cons_state = state;
505 	wake_up_all(&nfs_client_active_wq);
506 }
507 
508 /*
509  * Initialise the timeout values for a connection
510  */
511 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
512 				    unsigned int timeo, unsigned int retrans)
513 {
514 	to->to_initval = timeo * HZ / 10;
515 	to->to_retries = retrans;
516 
517 	switch (proto) {
518 	case XPRT_TRANSPORT_TCP:
519 	case XPRT_TRANSPORT_RDMA:
520 		if (to->to_retries == 0)
521 			to->to_retries = NFS_DEF_TCP_RETRANS;
522 		if (to->to_initval == 0)
523 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
524 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
525 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
526 		to->to_increment = to->to_initval;
527 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
528 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
529 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
530 		if (to->to_maxval < to->to_initval)
531 			to->to_maxval = to->to_initval;
532 		to->to_exponential = 0;
533 		break;
534 	case XPRT_TRANSPORT_UDP:
535 		if (to->to_retries == 0)
536 			to->to_retries = NFS_DEF_UDP_RETRANS;
537 		if (!to->to_initval)
538 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
539 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
540 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
541 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
542 		to->to_exponential = 1;
543 		break;
544 	default:
545 		BUG();
546 	}
547 }
548 
549 /*
550  * Create an RPC client handle
551  */
552 static int nfs_create_rpc_client(struct nfs_client *clp,
553 				 const struct rpc_timeout *timeparms,
554 				 rpc_authflavor_t flavor,
555 				 int discrtry, int noresvport)
556 {
557 	struct rpc_clnt		*clnt = NULL;
558 	struct rpc_create_args args = {
559 		.protocol	= clp->cl_proto,
560 		.address	= (struct sockaddr *)&clp->cl_addr,
561 		.addrsize	= clp->cl_addrlen,
562 		.timeout	= timeparms,
563 		.servername	= clp->cl_hostname,
564 		.program	= &nfs_program,
565 		.version	= clp->rpc_ops->version,
566 		.authflavor	= flavor,
567 	};
568 
569 	if (discrtry)
570 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
571 	if (noresvport)
572 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
573 
574 	if (!IS_ERR(clp->cl_rpcclient))
575 		return 0;
576 
577 	clnt = rpc_create(&args);
578 	if (IS_ERR(clnt)) {
579 		dprintk("%s: cannot create RPC client. Error = %ld\n",
580 				__func__, PTR_ERR(clnt));
581 		return PTR_ERR(clnt);
582 	}
583 
584 	clp->cl_rpcclient = clnt;
585 	return 0;
586 }
587 
588 /*
589  * Version 2 or 3 client destruction
590  */
591 static void nfs_destroy_server(struct nfs_server *server)
592 {
593 	if (!(server->flags & NFS_MOUNT_NONLM))
594 		nlmclnt_done(server->nlm_host);
595 }
596 
597 /*
598  * Version 2 or 3 lockd setup
599  */
600 static int nfs_start_lockd(struct nfs_server *server)
601 {
602 	struct nlm_host *host;
603 	struct nfs_client *clp = server->nfs_client;
604 	struct nlmclnt_initdata nlm_init = {
605 		.hostname	= clp->cl_hostname,
606 		.address	= (struct sockaddr *)&clp->cl_addr,
607 		.addrlen	= clp->cl_addrlen,
608 		.protocol	= server->flags & NFS_MOUNT_TCP ?
609 						IPPROTO_TCP : IPPROTO_UDP,
610 		.nfs_version	= clp->rpc_ops->version,
611 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
612 					1 : 0,
613 	};
614 
615 	if (nlm_init.nfs_version > 3)
616 		return 0;
617 	if (server->flags & NFS_MOUNT_NONLM)
618 		return 0;
619 
620 	host = nlmclnt_init(&nlm_init);
621 	if (IS_ERR(host))
622 		return PTR_ERR(host);
623 
624 	server->nlm_host = host;
625 	server->destroy = nfs_destroy_server;
626 	return 0;
627 }
628 
629 /*
630  * Initialise an NFSv3 ACL client connection
631  */
632 #ifdef CONFIG_NFS_V3_ACL
633 static void nfs_init_server_aclclient(struct nfs_server *server)
634 {
635 	if (server->nfs_client->rpc_ops->version != 3)
636 		goto out_noacl;
637 	if (server->flags & NFS_MOUNT_NOACL)
638 		goto out_noacl;
639 
640 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
641 	if (IS_ERR(server->client_acl))
642 		goto out_noacl;
643 
644 	/* No errors! Assume that Sun nfsacls are supported */
645 	server->caps |= NFS_CAP_ACLS;
646 	return;
647 
648 out_noacl:
649 	server->caps &= ~NFS_CAP_ACLS;
650 }
651 #else
652 static inline void nfs_init_server_aclclient(struct nfs_server *server)
653 {
654 	server->flags &= ~NFS_MOUNT_NOACL;
655 	server->caps &= ~NFS_CAP_ACLS;
656 }
657 #endif
658 
659 /*
660  * Create a general RPC client
661  */
662 static int nfs_init_server_rpcclient(struct nfs_server *server,
663 		const struct rpc_timeout *timeo,
664 		rpc_authflavor_t pseudoflavour)
665 {
666 	struct nfs_client *clp = server->nfs_client;
667 
668 	server->client = rpc_clone_client(clp->cl_rpcclient);
669 	if (IS_ERR(server->client)) {
670 		dprintk("%s: couldn't create rpc_client!\n", __func__);
671 		return PTR_ERR(server->client);
672 	}
673 
674 	memcpy(&server->client->cl_timeout_default,
675 			timeo,
676 			sizeof(server->client->cl_timeout_default));
677 	server->client->cl_timeout = &server->client->cl_timeout_default;
678 
679 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
680 		struct rpc_auth *auth;
681 
682 		auth = rpcauth_create(pseudoflavour, server->client);
683 		if (IS_ERR(auth)) {
684 			dprintk("%s: couldn't create credcache!\n", __func__);
685 			return PTR_ERR(auth);
686 		}
687 	}
688 	server->client->cl_softrtry = 0;
689 	if (server->flags & NFS_MOUNT_SOFT)
690 		server->client->cl_softrtry = 1;
691 
692 	return 0;
693 }
694 
695 /*
696  * Initialise an NFS2 or NFS3 client
697  */
698 static int nfs_init_client(struct nfs_client *clp,
699 			   const struct rpc_timeout *timeparms,
700 			   const struct nfs_parsed_mount_data *data)
701 {
702 	int error;
703 
704 	if (clp->cl_cons_state == NFS_CS_READY) {
705 		/* the client is already initialised */
706 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
707 		return 0;
708 	}
709 
710 	/*
711 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
712 	 * - RFC 2623, sec 2.3.2
713 	 */
714 	error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
715 				      0, data->flags & NFS_MOUNT_NORESVPORT);
716 	if (error < 0)
717 		goto error;
718 	nfs_mark_client_ready(clp, NFS_CS_READY);
719 	return 0;
720 
721 error:
722 	nfs_mark_client_ready(clp, error);
723 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
724 	return error;
725 }
726 
727 /*
728  * Create a version 2 or 3 client
729  */
730 static int nfs_init_server(struct nfs_server *server,
731 			   const struct nfs_parsed_mount_data *data)
732 {
733 	struct nfs_client_initdata cl_init = {
734 		.hostname = data->nfs_server.hostname,
735 		.addr = (const struct sockaddr *)&data->nfs_server.address,
736 		.addrlen = data->nfs_server.addrlen,
737 		.rpc_ops = &nfs_v2_clientops,
738 		.proto = data->nfs_server.protocol,
739 	};
740 	struct rpc_timeout timeparms;
741 	struct nfs_client *clp;
742 	int error;
743 
744 	dprintk("--> nfs_init_server()\n");
745 
746 #ifdef CONFIG_NFS_V3
747 	if (data->flags & NFS_MOUNT_VER3)
748 		cl_init.rpc_ops = &nfs_v3_clientops;
749 #endif
750 
751 	/* Allocate or find a client reference we can use */
752 	clp = nfs_get_client(&cl_init);
753 	if (IS_ERR(clp)) {
754 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
755 		return PTR_ERR(clp);
756 	}
757 
758 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
759 			data->timeo, data->retrans);
760 	error = nfs_init_client(clp, &timeparms, data);
761 	if (error < 0)
762 		goto error;
763 
764 	server->nfs_client = clp;
765 
766 	/* Initialise the client representation from the mount data */
767 	server->flags = data->flags;
768 	server->options = data->options;
769 
770 	if (data->rsize)
771 		server->rsize = nfs_block_size(data->rsize, NULL);
772 	if (data->wsize)
773 		server->wsize = nfs_block_size(data->wsize, NULL);
774 
775 	server->acregmin = data->acregmin * HZ;
776 	server->acregmax = data->acregmax * HZ;
777 	server->acdirmin = data->acdirmin * HZ;
778 	server->acdirmax = data->acdirmax * HZ;
779 
780 	/* Start lockd here, before we might error out */
781 	error = nfs_start_lockd(server);
782 	if (error < 0)
783 		goto error;
784 
785 	server->port = data->nfs_server.port;
786 
787 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
788 	if (error < 0)
789 		goto error;
790 
791 	/* Preserve the values of mount_server-related mount options */
792 	if (data->mount_server.addrlen) {
793 		memcpy(&server->mountd_address, &data->mount_server.address,
794 			data->mount_server.addrlen);
795 		server->mountd_addrlen = data->mount_server.addrlen;
796 	}
797 	server->mountd_version = data->mount_server.version;
798 	server->mountd_port = data->mount_server.port;
799 	server->mountd_protocol = data->mount_server.protocol;
800 
801 	server->namelen  = data->namlen;
802 	/* Create a client RPC handle for the NFSv3 ACL management interface */
803 	nfs_init_server_aclclient(server);
804 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
805 	return 0;
806 
807 error:
808 	server->nfs_client = NULL;
809 	nfs_put_client(clp);
810 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
811 	return error;
812 }
813 
814 /*
815  * Load up the server record from information gained in an fsinfo record
816  */
817 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
818 {
819 	unsigned long max_rpc_payload;
820 
821 	/* Work out a lot of parameters */
822 	if (server->rsize == 0)
823 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
824 	if (server->wsize == 0)
825 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
826 
827 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
828 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
829 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
830 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
831 
832 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
833 	if (server->rsize > max_rpc_payload)
834 		server->rsize = max_rpc_payload;
835 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
836 		server->rsize = NFS_MAX_FILE_IO_SIZE;
837 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
838 
839 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
840 
841 	if (server->wsize > max_rpc_payload)
842 		server->wsize = max_rpc_payload;
843 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
844 		server->wsize = NFS_MAX_FILE_IO_SIZE;
845 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
846 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
847 
848 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
849 	if (server->dtsize > PAGE_CACHE_SIZE)
850 		server->dtsize = PAGE_CACHE_SIZE;
851 	if (server->dtsize > server->rsize)
852 		server->dtsize = server->rsize;
853 
854 	if (server->flags & NFS_MOUNT_NOAC) {
855 		server->acregmin = server->acregmax = 0;
856 		server->acdirmin = server->acdirmax = 0;
857 	}
858 
859 	server->maxfilesize = fsinfo->maxfilesize;
860 
861 	/* We're airborne Set socket buffersize */
862 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
863 }
864 
865 /*
866  * Probe filesystem information, including the FSID on v2/v3
867  */
868 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
869 {
870 	struct nfs_fsinfo fsinfo;
871 	struct nfs_client *clp = server->nfs_client;
872 	int error;
873 
874 	dprintk("--> nfs_probe_fsinfo()\n");
875 
876 	if (clp->rpc_ops->set_capabilities != NULL) {
877 		error = clp->rpc_ops->set_capabilities(server, mntfh);
878 		if (error < 0)
879 			goto out_error;
880 	}
881 
882 	fsinfo.fattr = fattr;
883 	nfs_fattr_init(fattr);
884 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
885 	if (error < 0)
886 		goto out_error;
887 
888 	nfs_server_set_fsinfo(server, &fsinfo);
889 	error = bdi_init(&server->backing_dev_info);
890 	if (error)
891 		goto out_error;
892 
893 
894 	/* Get some general file system info */
895 	if (server->namelen == 0) {
896 		struct nfs_pathconf pathinfo;
897 
898 		pathinfo.fattr = fattr;
899 		nfs_fattr_init(fattr);
900 
901 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
902 			server->namelen = pathinfo.max_namelen;
903 	}
904 
905 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
906 	return 0;
907 
908 out_error:
909 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
910 	return error;
911 }
912 
913 /*
914  * Copy useful information when duplicating a server record
915  */
916 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
917 {
918 	target->flags = source->flags;
919 	target->acregmin = source->acregmin;
920 	target->acregmax = source->acregmax;
921 	target->acdirmin = source->acdirmin;
922 	target->acdirmax = source->acdirmax;
923 	target->caps = source->caps;
924 }
925 
926 /*
927  * Allocate and initialise a server record
928  */
929 static struct nfs_server *nfs_alloc_server(void)
930 {
931 	struct nfs_server *server;
932 
933 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
934 	if (!server)
935 		return NULL;
936 
937 	server->client = server->client_acl = ERR_PTR(-EINVAL);
938 
939 	/* Zero out the NFS state stuff */
940 	INIT_LIST_HEAD(&server->client_link);
941 	INIT_LIST_HEAD(&server->master_link);
942 
943 	atomic_set(&server->active, 0);
944 
945 	server->io_stats = nfs_alloc_iostats();
946 	if (!server->io_stats) {
947 		kfree(server);
948 		return NULL;
949 	}
950 
951 	return server;
952 }
953 
954 /*
955  * Free up a server record
956  */
957 void nfs_free_server(struct nfs_server *server)
958 {
959 	dprintk("--> nfs_free_server()\n");
960 
961 	spin_lock(&nfs_client_lock);
962 	list_del(&server->client_link);
963 	list_del(&server->master_link);
964 	spin_unlock(&nfs_client_lock);
965 
966 	if (server->destroy != NULL)
967 		server->destroy(server);
968 
969 	if (!IS_ERR(server->client_acl))
970 		rpc_shutdown_client(server->client_acl);
971 	if (!IS_ERR(server->client))
972 		rpc_shutdown_client(server->client);
973 
974 	nfs_put_client(server->nfs_client);
975 
976 	nfs_free_iostats(server->io_stats);
977 	bdi_destroy(&server->backing_dev_info);
978 	kfree(server);
979 	nfs_release_automount_timer();
980 	dprintk("<-- nfs_free_server()\n");
981 }
982 
983 /*
984  * Create a version 2 or 3 volume record
985  * - keyed on server and FSID
986  */
987 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
988 				     struct nfs_fh *mntfh)
989 {
990 	struct nfs_server *server;
991 	struct nfs_fattr fattr;
992 	int error;
993 
994 	server = nfs_alloc_server();
995 	if (!server)
996 		return ERR_PTR(-ENOMEM);
997 
998 	/* Get a client representation */
999 	error = nfs_init_server(server, data);
1000 	if (error < 0)
1001 		goto error;
1002 
1003 	BUG_ON(!server->nfs_client);
1004 	BUG_ON(!server->nfs_client->rpc_ops);
1005 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1006 
1007 	/* Probe the root fh to retrieve its FSID */
1008 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
1009 	if (error < 0)
1010 		goto error;
1011 	if (server->nfs_client->rpc_ops->version == 3) {
1012 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1013 			server->namelen = NFS3_MAXNAMLEN;
1014 		if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1015 			server->caps |= NFS_CAP_READDIRPLUS;
1016 	} else {
1017 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1018 			server->namelen = NFS2_MAXNAMLEN;
1019 	}
1020 
1021 	if (!(fattr.valid & NFS_ATTR_FATTR)) {
1022 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1023 		if (error < 0) {
1024 			dprintk("nfs_create_server: getattr error = %d\n", -error);
1025 			goto error;
1026 		}
1027 	}
1028 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1029 
1030 	dprintk("Server FSID: %llx:%llx\n",
1031 		(unsigned long long) server->fsid.major,
1032 		(unsigned long long) server->fsid.minor);
1033 
1034 	BUG_ON(!server->nfs_client);
1035 	BUG_ON(!server->nfs_client->rpc_ops);
1036 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1037 
1038 	spin_lock(&nfs_client_lock);
1039 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1040 	list_add_tail(&server->master_link, &nfs_volume_list);
1041 	spin_unlock(&nfs_client_lock);
1042 
1043 	server->mount_time = jiffies;
1044 	return server;
1045 
1046 error:
1047 	nfs_free_server(server);
1048 	return ERR_PTR(error);
1049 }
1050 
1051 #ifdef CONFIG_NFS_V4
1052 /*
1053  * Initialise an NFS4 client record
1054  */
1055 static int nfs4_init_client(struct nfs_client *clp,
1056 		const struct rpc_timeout *timeparms,
1057 		const char *ip_addr,
1058 		rpc_authflavor_t authflavour,
1059 		int flags)
1060 {
1061 	int error;
1062 
1063 	if (clp->cl_cons_state == NFS_CS_READY) {
1064 		/* the client is initialised already */
1065 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1066 		return 0;
1067 	}
1068 
1069 	/* Check NFS protocol revision and initialize RPC op vector */
1070 	clp->rpc_ops = &nfs_v4_clientops;
1071 
1072 	error = nfs_create_rpc_client(clp, timeparms, authflavour,
1073 				      1, flags & NFS_MOUNT_NORESVPORT);
1074 	if (error < 0)
1075 		goto error;
1076 	memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1077 
1078 	error = nfs_idmap_new(clp);
1079 	if (error < 0) {
1080 		dprintk("%s: failed to create idmapper. Error = %d\n",
1081 			__func__, error);
1082 		goto error;
1083 	}
1084 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1085 
1086 	nfs_mark_client_ready(clp, NFS_CS_READY);
1087 	return 0;
1088 
1089 error:
1090 	nfs_mark_client_ready(clp, error);
1091 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1092 	return error;
1093 }
1094 
1095 /*
1096  * Set up an NFS4 client
1097  */
1098 static int nfs4_set_client(struct nfs_server *server,
1099 		const char *hostname,
1100 		const struct sockaddr *addr,
1101 		const size_t addrlen,
1102 		const char *ip_addr,
1103 		rpc_authflavor_t authflavour,
1104 		int proto, const struct rpc_timeout *timeparms)
1105 {
1106 	struct nfs_client_initdata cl_init = {
1107 		.hostname = hostname,
1108 		.addr = addr,
1109 		.addrlen = addrlen,
1110 		.rpc_ops = &nfs_v4_clientops,
1111 		.proto = proto,
1112 	};
1113 	struct nfs_client *clp;
1114 	int error;
1115 
1116 	dprintk("--> nfs4_set_client()\n");
1117 
1118 	/* Allocate or find a client reference we can use */
1119 	clp = nfs_get_client(&cl_init);
1120 	if (IS_ERR(clp)) {
1121 		error = PTR_ERR(clp);
1122 		goto error;
1123 	}
1124 	error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1125 					server->flags);
1126 	if (error < 0)
1127 		goto error_put;
1128 
1129 	server->nfs_client = clp;
1130 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1131 	return 0;
1132 
1133 error_put:
1134 	nfs_put_client(clp);
1135 error:
1136 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1137 	return error;
1138 }
1139 
1140 /*
1141  * Create a version 4 volume record
1142  */
1143 static int nfs4_init_server(struct nfs_server *server,
1144 		const struct nfs_parsed_mount_data *data)
1145 {
1146 	struct rpc_timeout timeparms;
1147 	int error;
1148 
1149 	dprintk("--> nfs4_init_server()\n");
1150 
1151 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1152 			data->timeo, data->retrans);
1153 
1154 	/* Initialise the client representation from the mount data */
1155 	server->flags = data->flags;
1156 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1157 	server->options = data->options;
1158 
1159 	/* Get a client record */
1160 	error = nfs4_set_client(server,
1161 			data->nfs_server.hostname,
1162 			(const struct sockaddr *)&data->nfs_server.address,
1163 			data->nfs_server.addrlen,
1164 			data->client_address,
1165 			data->auth_flavors[0],
1166 			data->nfs_server.protocol,
1167 			&timeparms);
1168 	if (error < 0)
1169 		goto error;
1170 
1171 	if (data->rsize)
1172 		server->rsize = nfs_block_size(data->rsize, NULL);
1173 	if (data->wsize)
1174 		server->wsize = nfs_block_size(data->wsize, NULL);
1175 
1176 	server->acregmin = data->acregmin * HZ;
1177 	server->acregmax = data->acregmax * HZ;
1178 	server->acdirmin = data->acdirmin * HZ;
1179 	server->acdirmax = data->acdirmax * HZ;
1180 
1181 	server->port = data->nfs_server.port;
1182 
1183 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1184 
1185 error:
1186 	/* Done */
1187 	dprintk("<-- nfs4_init_server() = %d\n", error);
1188 	return error;
1189 }
1190 
1191 /*
1192  * Create a version 4 volume record
1193  * - keyed on server and FSID
1194  */
1195 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1196 				      struct nfs_fh *mntfh)
1197 {
1198 	struct nfs_fattr fattr;
1199 	struct nfs_server *server;
1200 	int error;
1201 
1202 	dprintk("--> nfs4_create_server()\n");
1203 
1204 	server = nfs_alloc_server();
1205 	if (!server)
1206 		return ERR_PTR(-ENOMEM);
1207 
1208 	/* set up the general RPC client */
1209 	error = nfs4_init_server(server, data);
1210 	if (error < 0)
1211 		goto error;
1212 
1213 	BUG_ON(!server->nfs_client);
1214 	BUG_ON(!server->nfs_client->rpc_ops);
1215 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1216 
1217 	/* Probe the root fh to retrieve its FSID */
1218 	error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1219 	if (error < 0)
1220 		goto error;
1221 
1222 	dprintk("Server FSID: %llx:%llx\n",
1223 		(unsigned long long) server->fsid.major,
1224 		(unsigned long long) server->fsid.minor);
1225 	dprintk("Mount FH: %d\n", mntfh->size);
1226 
1227 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
1228 	if (error < 0)
1229 		goto error;
1230 
1231 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1232 		server->namelen = NFS4_MAXNAMLEN;
1233 
1234 	BUG_ON(!server->nfs_client);
1235 	BUG_ON(!server->nfs_client->rpc_ops);
1236 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1237 
1238 	spin_lock(&nfs_client_lock);
1239 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1240 	list_add_tail(&server->master_link, &nfs_volume_list);
1241 	spin_unlock(&nfs_client_lock);
1242 
1243 	server->mount_time = jiffies;
1244 	dprintk("<-- nfs4_create_server() = %p\n", server);
1245 	return server;
1246 
1247 error:
1248 	nfs_free_server(server);
1249 	dprintk("<-- nfs4_create_server() = error %d\n", error);
1250 	return ERR_PTR(error);
1251 }
1252 
1253 /*
1254  * Create an NFS4 referral server record
1255  */
1256 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1257 					       struct nfs_fh *mntfh)
1258 {
1259 	struct nfs_client *parent_client;
1260 	struct nfs_server *server, *parent_server;
1261 	struct nfs_fattr fattr;
1262 	int error;
1263 
1264 	dprintk("--> nfs4_create_referral_server()\n");
1265 
1266 	server = nfs_alloc_server();
1267 	if (!server)
1268 		return ERR_PTR(-ENOMEM);
1269 
1270 	parent_server = NFS_SB(data->sb);
1271 	parent_client = parent_server->nfs_client;
1272 
1273 	/* Initialise the client representation from the parent server */
1274 	nfs_server_copy_userdata(server, parent_server);
1275 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1276 
1277 	/* Get a client representation.
1278 	 * Note: NFSv4 always uses TCP, */
1279 	error = nfs4_set_client(server, data->hostname,
1280 				data->addr,
1281 				data->addrlen,
1282 				parent_client->cl_ipaddr,
1283 				data->authflavor,
1284 				parent_server->client->cl_xprt->prot,
1285 				parent_server->client->cl_timeout);
1286 	if (error < 0)
1287 		goto error;
1288 
1289 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1290 	if (error < 0)
1291 		goto error;
1292 
1293 	BUG_ON(!server->nfs_client);
1294 	BUG_ON(!server->nfs_client->rpc_ops);
1295 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1296 
1297 	/* Probe the root fh to retrieve its FSID and filehandle */
1298 	error = nfs4_path_walk(server, mntfh, data->mnt_path);
1299 	if (error < 0)
1300 		goto error;
1301 
1302 	/* probe the filesystem info for this server filesystem */
1303 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
1304 	if (error < 0)
1305 		goto error;
1306 
1307 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1308 		server->namelen = NFS4_MAXNAMLEN;
1309 
1310 	dprintk("Referral FSID: %llx:%llx\n",
1311 		(unsigned long long) server->fsid.major,
1312 		(unsigned long long) server->fsid.minor);
1313 
1314 	spin_lock(&nfs_client_lock);
1315 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1316 	list_add_tail(&server->master_link, &nfs_volume_list);
1317 	spin_unlock(&nfs_client_lock);
1318 
1319 	server->mount_time = jiffies;
1320 
1321 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
1322 	return server;
1323 
1324 error:
1325 	nfs_free_server(server);
1326 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1327 	return ERR_PTR(error);
1328 }
1329 
1330 #endif /* CONFIG_NFS_V4 */
1331 
1332 /*
1333  * Clone an NFS2, NFS3 or NFS4 server record
1334  */
1335 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1336 				    struct nfs_fh *fh,
1337 				    struct nfs_fattr *fattr)
1338 {
1339 	struct nfs_server *server;
1340 	struct nfs_fattr fattr_fsinfo;
1341 	int error;
1342 
1343 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1344 		(unsigned long long) fattr->fsid.major,
1345 		(unsigned long long) fattr->fsid.minor);
1346 
1347 	server = nfs_alloc_server();
1348 	if (!server)
1349 		return ERR_PTR(-ENOMEM);
1350 
1351 	/* Copy data from the source */
1352 	server->nfs_client = source->nfs_client;
1353 	atomic_inc(&server->nfs_client->cl_count);
1354 	nfs_server_copy_userdata(server, source);
1355 
1356 	server->fsid = fattr->fsid;
1357 
1358 	error = nfs_init_server_rpcclient(server,
1359 			source->client->cl_timeout,
1360 			source->client->cl_auth->au_flavor);
1361 	if (error < 0)
1362 		goto out_free_server;
1363 	if (!IS_ERR(source->client_acl))
1364 		nfs_init_server_aclclient(server);
1365 
1366 	/* probe the filesystem info for this server filesystem */
1367 	error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1368 	if (error < 0)
1369 		goto out_free_server;
1370 
1371 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1372 		server->namelen = NFS4_MAXNAMLEN;
1373 
1374 	dprintk("Cloned FSID: %llx:%llx\n",
1375 		(unsigned long long) server->fsid.major,
1376 		(unsigned long long) server->fsid.minor);
1377 
1378 	error = nfs_start_lockd(server);
1379 	if (error < 0)
1380 		goto out_free_server;
1381 
1382 	spin_lock(&nfs_client_lock);
1383 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1384 	list_add_tail(&server->master_link, &nfs_volume_list);
1385 	spin_unlock(&nfs_client_lock);
1386 
1387 	server->mount_time = jiffies;
1388 
1389 	dprintk("<-- nfs_clone_server() = %p\n", server);
1390 	return server;
1391 
1392 out_free_server:
1393 	nfs_free_server(server);
1394 	dprintk("<-- nfs_clone_server() = error %d\n", error);
1395 	return ERR_PTR(error);
1396 }
1397 
1398 #ifdef CONFIG_PROC_FS
1399 static struct proc_dir_entry *proc_fs_nfs;
1400 
1401 static int nfs_server_list_open(struct inode *inode, struct file *file);
1402 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1403 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1404 static void nfs_server_list_stop(struct seq_file *p, void *v);
1405 static int nfs_server_list_show(struct seq_file *m, void *v);
1406 
1407 static struct seq_operations nfs_server_list_ops = {
1408 	.start	= nfs_server_list_start,
1409 	.next	= nfs_server_list_next,
1410 	.stop	= nfs_server_list_stop,
1411 	.show	= nfs_server_list_show,
1412 };
1413 
1414 static const struct file_operations nfs_server_list_fops = {
1415 	.open		= nfs_server_list_open,
1416 	.read		= seq_read,
1417 	.llseek		= seq_lseek,
1418 	.release	= seq_release,
1419 	.owner		= THIS_MODULE,
1420 };
1421 
1422 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1423 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1424 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1425 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1426 static int nfs_volume_list_show(struct seq_file *m, void *v);
1427 
1428 static struct seq_operations nfs_volume_list_ops = {
1429 	.start	= nfs_volume_list_start,
1430 	.next	= nfs_volume_list_next,
1431 	.stop	= nfs_volume_list_stop,
1432 	.show	= nfs_volume_list_show,
1433 };
1434 
1435 static const struct file_operations nfs_volume_list_fops = {
1436 	.open		= nfs_volume_list_open,
1437 	.read		= seq_read,
1438 	.llseek		= seq_lseek,
1439 	.release	= seq_release,
1440 	.owner		= THIS_MODULE,
1441 };
1442 
1443 /*
1444  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1445  * we're dealing
1446  */
1447 static int nfs_server_list_open(struct inode *inode, struct file *file)
1448 {
1449 	struct seq_file *m;
1450 	int ret;
1451 
1452 	ret = seq_open(file, &nfs_server_list_ops);
1453 	if (ret < 0)
1454 		return ret;
1455 
1456 	m = file->private_data;
1457 	m->private = PDE(inode)->data;
1458 
1459 	return 0;
1460 }
1461 
1462 /*
1463  * set up the iterator to start reading from the server list and return the first item
1464  */
1465 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1466 {
1467 	/* lock the list against modification */
1468 	spin_lock(&nfs_client_lock);
1469 	return seq_list_start_head(&nfs_client_list, *_pos);
1470 }
1471 
1472 /*
1473  * move to next server
1474  */
1475 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1476 {
1477 	return seq_list_next(v, &nfs_client_list, pos);
1478 }
1479 
1480 /*
1481  * clean up after reading from the transports list
1482  */
1483 static void nfs_server_list_stop(struct seq_file *p, void *v)
1484 {
1485 	spin_unlock(&nfs_client_lock);
1486 }
1487 
1488 /*
1489  * display a header line followed by a load of call lines
1490  */
1491 static int nfs_server_list_show(struct seq_file *m, void *v)
1492 {
1493 	struct nfs_client *clp;
1494 
1495 	/* display header on line 1 */
1496 	if (v == &nfs_client_list) {
1497 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
1498 		return 0;
1499 	}
1500 
1501 	/* display one transport per line on subsequent lines */
1502 	clp = list_entry(v, struct nfs_client, cl_share_link);
1503 
1504 	seq_printf(m, "v%u %s %s %3d %s\n",
1505 		   clp->rpc_ops->version,
1506 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1507 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1508 		   atomic_read(&clp->cl_count),
1509 		   clp->cl_hostname);
1510 
1511 	return 0;
1512 }
1513 
1514 /*
1515  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1516  */
1517 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1518 {
1519 	struct seq_file *m;
1520 	int ret;
1521 
1522 	ret = seq_open(file, &nfs_volume_list_ops);
1523 	if (ret < 0)
1524 		return ret;
1525 
1526 	m = file->private_data;
1527 	m->private = PDE(inode)->data;
1528 
1529 	return 0;
1530 }
1531 
1532 /*
1533  * set up the iterator to start reading from the volume list and return the first item
1534  */
1535 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1536 {
1537 	/* lock the list against modification */
1538 	spin_lock(&nfs_client_lock);
1539 	return seq_list_start_head(&nfs_volume_list, *_pos);
1540 }
1541 
1542 /*
1543  * move to next volume
1544  */
1545 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1546 {
1547 	return seq_list_next(v, &nfs_volume_list, pos);
1548 }
1549 
1550 /*
1551  * clean up after reading from the transports list
1552  */
1553 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1554 {
1555 	spin_unlock(&nfs_client_lock);
1556 }
1557 
1558 /*
1559  * display a header line followed by a load of call lines
1560  */
1561 static int nfs_volume_list_show(struct seq_file *m, void *v)
1562 {
1563 	struct nfs_server *server;
1564 	struct nfs_client *clp;
1565 	char dev[8], fsid[17];
1566 
1567 	/* display header on line 1 */
1568 	if (v == &nfs_volume_list) {
1569 		seq_puts(m, "NV SERVER   PORT DEV     FSID              FSC\n");
1570 		return 0;
1571 	}
1572 	/* display one transport per line on subsequent lines */
1573 	server = list_entry(v, struct nfs_server, master_link);
1574 	clp = server->nfs_client;
1575 
1576 	snprintf(dev, 8, "%u:%u",
1577 		 MAJOR(server->s_dev), MINOR(server->s_dev));
1578 
1579 	snprintf(fsid, 17, "%llx:%llx",
1580 		 (unsigned long long) server->fsid.major,
1581 		 (unsigned long long) server->fsid.minor);
1582 
1583 	seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
1584 		   clp->rpc_ops->version,
1585 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1586 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1587 		   dev,
1588 		   fsid,
1589 		   nfs_server_fscache_state(server));
1590 
1591 	return 0;
1592 }
1593 
1594 /*
1595  * initialise the /proc/fs/nfsfs/ directory
1596  */
1597 int __init nfs_fs_proc_init(void)
1598 {
1599 	struct proc_dir_entry *p;
1600 
1601 	proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1602 	if (!proc_fs_nfs)
1603 		goto error_0;
1604 
1605 	/* a file of servers with which we're dealing */
1606 	p = proc_create("servers", S_IFREG|S_IRUGO,
1607 			proc_fs_nfs, &nfs_server_list_fops);
1608 	if (!p)
1609 		goto error_1;
1610 
1611 	/* a file of volumes that we have mounted */
1612 	p = proc_create("volumes", S_IFREG|S_IRUGO,
1613 			proc_fs_nfs, &nfs_volume_list_fops);
1614 	if (!p)
1615 		goto error_2;
1616 	return 0;
1617 
1618 error_2:
1619 	remove_proc_entry("servers", proc_fs_nfs);
1620 error_1:
1621 	remove_proc_entry("fs/nfsfs", NULL);
1622 error_0:
1623 	return -ENOMEM;
1624 }
1625 
1626 /*
1627  * clean up the /proc/fs/nfsfs/ directory
1628  */
1629 void nfs_fs_proc_exit(void)
1630 {
1631 	remove_proc_entry("volumes", proc_fs_nfs);
1632 	remove_proc_entry("servers", proc_fs_nfs);
1633 	remove_proc_entry("fs/nfsfs", NULL);
1634 }
1635 
1636 #endif /* CONFIG_PROC_FS */
1637