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