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 <linux/slab.h>
39 #include <linux/idr.h>
40 #include <net/ipv6.h>
41 #include <linux/nfs_xdr.h>
42 #include <linux/sunrpc/bc_xprt.h>
43 #include <linux/nsproxy.h>
44 #include <linux/pid_namespace.h>
45
46
47 #include "nfs4_fs.h"
48 #include "callback.h"
49 #include "delegation.h"
50 #include "iostat.h"
51 #include "internal.h"
52 #include "fscache.h"
53 #include "pnfs.h"
54 #include "netns.h"
55
56 #define NFSDBG_FACILITY NFSDBG_CLIENT
57
58 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59 #ifdef CONFIG_NFS_V4
60
61 /*
62 * Get a unique NFSv4.0 callback identifier which will be used
63 * by the V4.0 callback service to lookup the nfs_client struct
64 */
nfs_get_cb_ident_idr(struct nfs_client * clp,int minorversion)65 static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
66 {
67 int ret = 0;
68 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
69
70 if (clp->rpc_ops->version != 4 || minorversion != 0)
71 return ret;
72 retry:
73 if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
74 return -ENOMEM;
75 spin_lock(&nn->nfs_client_lock);
76 ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
77 spin_unlock(&nn->nfs_client_lock);
78 if (ret == -EAGAIN)
79 goto retry;
80 return ret;
81 }
82 #endif /* CONFIG_NFS_V4 */
83
84 /*
85 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
86 */
87 static bool nfs4_disable_idmapping = true;
88
89 /*
90 * RPC cruft for NFS
91 */
92 static const struct rpc_version *nfs_version[5] = {
93 [2] = &nfs_version2,
94 #ifdef CONFIG_NFS_V3
95 [3] = &nfs_version3,
96 #endif
97 #ifdef CONFIG_NFS_V4
98 [4] = &nfs_version4,
99 #endif
100 };
101
102 const struct rpc_program nfs_program = {
103 .name = "nfs",
104 .number = NFS_PROGRAM,
105 .nrvers = ARRAY_SIZE(nfs_version),
106 .version = nfs_version,
107 .stats = &nfs_rpcstat,
108 .pipe_dir_name = NFS_PIPE_DIRNAME,
109 };
110
111 struct rpc_stat nfs_rpcstat = {
112 .program = &nfs_program
113 };
114
115
116 #ifdef CONFIG_NFS_V3_ACL
117 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
118 static const struct rpc_version *nfsacl_version[] = {
119 [3] = &nfsacl_version3,
120 };
121
122 const struct rpc_program nfsacl_program = {
123 .name = "nfsacl",
124 .number = NFS_ACL_PROGRAM,
125 .nrvers = ARRAY_SIZE(nfsacl_version),
126 .version = nfsacl_version,
127 .stats = &nfsacl_rpcstat,
128 };
129 #endif /* CONFIG_NFS_V3_ACL */
130
131 struct nfs_client_initdata {
132 const char *hostname;
133 const struct sockaddr *addr;
134 size_t addrlen;
135 const struct nfs_rpc_ops *rpc_ops;
136 int proto;
137 u32 minorversion;
138 struct net *net;
139 };
140
141 /*
142 * Allocate a shared client record
143 *
144 * Since these are allocated/deallocated very rarely, we don't
145 * bother putting them in a slab cache...
146 */
nfs_alloc_client(const struct nfs_client_initdata * cl_init)147 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
148 {
149 struct nfs_client *clp;
150 struct rpc_cred *cred;
151 int err = -ENOMEM;
152
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
154 goto error_0;
155
156 clp->rpc_ops = cl_init->rpc_ops;
157
158 atomic_set(&clp->cl_count, 1);
159 clp->cl_cons_state = NFS_CS_INITING;
160
161 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
162 clp->cl_addrlen = cl_init->addrlen;
163
164 if (cl_init->hostname) {
165 err = -ENOMEM;
166 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
167 if (!clp->cl_hostname)
168 goto error_cleanup;
169 }
170
171 INIT_LIST_HEAD(&clp->cl_superblocks);
172 clp->cl_rpcclient = ERR_PTR(-EINVAL);
173
174 clp->cl_proto = cl_init->proto;
175 clp->net = get_net(cl_init->net);
176
177 #ifdef CONFIG_NFS_V4
178 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
179 if (err)
180 goto error_cleanup;
181
182 spin_lock_init(&clp->cl_lock);
183 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
184 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
185 clp->cl_boot_time = CURRENT_TIME;
186 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
187 clp->cl_minorversion = cl_init->minorversion;
188 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
189 #endif
190 cred = rpc_lookup_machine_cred("*");
191 if (!IS_ERR(cred))
192 clp->cl_machine_cred = cred;
193 nfs_fscache_get_client_cookie(clp);
194
195 return clp;
196
197 error_cleanup:
198 kfree(clp);
199 error_0:
200 return ERR_PTR(err);
201 }
202
203 #ifdef CONFIG_NFS_V4
204 #ifdef CONFIG_NFS_V4_1
nfs4_shutdown_session(struct nfs_client * clp)205 static void nfs4_shutdown_session(struct nfs_client *clp)
206 {
207 if (nfs4_has_session(clp)) {
208 nfs4_deviceid_purge_client(clp);
209 nfs4_destroy_session(clp->cl_session);
210 }
211
212 }
213 #else /* CONFIG_NFS_V4_1 */
nfs4_shutdown_session(struct nfs_client * clp)214 static void nfs4_shutdown_session(struct nfs_client *clp)
215 {
216 }
217 #endif /* CONFIG_NFS_V4_1 */
218
219 /*
220 * Destroy the NFS4 callback service
221 */
nfs4_destroy_callback(struct nfs_client * clp)222 static void nfs4_destroy_callback(struct nfs_client *clp)
223 {
224 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
225 nfs_callback_down(clp->cl_mvops->minor_version);
226 }
227
nfs4_shutdown_client(struct nfs_client * clp)228 static void nfs4_shutdown_client(struct nfs_client *clp)
229 {
230 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
231 nfs4_kill_renewd(clp);
232 nfs4_shutdown_session(clp);
233 nfs4_destroy_callback(clp);
234 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
235 nfs_idmap_delete(clp);
236
237 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
238 }
239
240 /* idr_remove_all is not needed as all id's are removed by nfs_put_client */
nfs_cleanup_cb_ident_idr(struct net * net)241 void nfs_cleanup_cb_ident_idr(struct net *net)
242 {
243 struct nfs_net *nn = net_generic(net, nfs_net_id);
244
245 idr_destroy(&nn->cb_ident_idr);
246 }
247
248 /* nfs_client_lock held */
nfs_cb_idr_remove_locked(struct nfs_client * clp)249 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
250 {
251 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
252
253 if (clp->cl_cb_ident)
254 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
255 }
256
pnfs_init_server(struct nfs_server * server)257 static void pnfs_init_server(struct nfs_server *server)
258 {
259 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
260 }
261
nfs4_destroy_server(struct nfs_server * server)262 static void nfs4_destroy_server(struct nfs_server *server)
263 {
264 nfs4_purge_state_owners(server);
265 }
266
267 #else
nfs4_shutdown_client(struct nfs_client * clp)268 static void nfs4_shutdown_client(struct nfs_client *clp)
269 {
270 }
271
nfs_cleanup_cb_ident_idr(struct net * net)272 void nfs_cleanup_cb_ident_idr(struct net *net)
273 {
274 }
275
nfs_cb_idr_remove_locked(struct nfs_client * clp)276 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
277 {
278 }
279
pnfs_init_server(struct nfs_server * server)280 static void pnfs_init_server(struct nfs_server *server)
281 {
282 }
283
284 #endif /* CONFIG_NFS_V4 */
285
286 /*
287 * Destroy a shared client record
288 */
nfs_free_client(struct nfs_client * clp)289 static void nfs_free_client(struct nfs_client *clp)
290 {
291 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
292
293 nfs4_shutdown_client(clp);
294
295 nfs_fscache_release_client_cookie(clp);
296
297 /* -EIO all pending I/O */
298 if (!IS_ERR(clp->cl_rpcclient))
299 rpc_shutdown_client(clp->cl_rpcclient);
300
301 if (clp->cl_machine_cred != NULL)
302 put_rpccred(clp->cl_machine_cred);
303
304 put_net(clp->net);
305 kfree(clp->cl_hostname);
306 kfree(clp->server_scope);
307 kfree(clp->impl_id);
308 kfree(clp);
309
310 dprintk("<-- nfs_free_client()\n");
311 }
312
313 /*
314 * Release a reference to a shared client record
315 */
nfs_put_client(struct nfs_client * clp)316 void nfs_put_client(struct nfs_client *clp)
317 {
318 struct nfs_net *nn;
319
320 if (!clp)
321 return;
322
323 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
324 nn = net_generic(clp->net, nfs_net_id);
325
326 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
327 list_del(&clp->cl_share_link);
328 nfs_cb_idr_remove_locked(clp);
329 spin_unlock(&nn->nfs_client_lock);
330
331 BUG_ON(!list_empty(&clp->cl_superblocks));
332
333 nfs_free_client(clp);
334 }
335 }
336 EXPORT_SYMBOL_GPL(nfs_put_client);
337
338 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
339 /*
340 * Test if two ip6 socket addresses refer to the same socket by
341 * comparing relevant fields. The padding bytes specifically, are not
342 * compared. sin6_flowinfo is not compared because it only affects QoS
343 * and sin6_scope_id is only compared if the address is "link local"
344 * because "link local" addresses need only be unique to a specific
345 * link. Conversely, ordinary unicast addresses might have different
346 * sin6_scope_id.
347 *
348 * The caller should ensure both socket addresses are AF_INET6.
349 */
nfs_sockaddr_match_ipaddr6(const struct sockaddr * sa1,const struct sockaddr * sa2)350 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
351 const struct sockaddr *sa2)
352 {
353 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
354 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
355
356 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
357 return 0;
358 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
359 return sin1->sin6_scope_id == sin2->sin6_scope_id;
360
361 return 1;
362 }
363 #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
nfs_sockaddr_match_ipaddr6(const struct sockaddr * sa1,const struct sockaddr * sa2)364 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
365 const struct sockaddr *sa2)
366 {
367 return 0;
368 }
369 #endif
370
371 /*
372 * Test if two ip4 socket addresses refer to the same socket, by
373 * comparing relevant fields. The padding bytes specifically, are
374 * not compared.
375 *
376 * The caller should ensure both socket addresses are AF_INET.
377 */
nfs_sockaddr_match_ipaddr4(const struct sockaddr * sa1,const struct sockaddr * sa2)378 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
379 const struct sockaddr *sa2)
380 {
381 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
382 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
383
384 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
385 }
386
nfs_sockaddr_cmp_ip6(const struct sockaddr * sa1,const struct sockaddr * sa2)387 static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
388 const struct sockaddr *sa2)
389 {
390 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
391 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
392
393 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
394 (sin1->sin6_port == sin2->sin6_port);
395 }
396
nfs_sockaddr_cmp_ip4(const struct sockaddr * sa1,const struct sockaddr * sa2)397 static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
398 const struct sockaddr *sa2)
399 {
400 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
401 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
402
403 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
404 (sin1->sin_port == sin2->sin_port);
405 }
406
407 #if defined(CONFIG_NFS_V4_1)
408 /*
409 * Test if two socket addresses represent the same actual socket,
410 * by comparing (only) relevant fields, excluding the port number.
411 */
nfs_sockaddr_match_ipaddr(const struct sockaddr * sa1,const struct sockaddr * sa2)412 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
413 const struct sockaddr *sa2)
414 {
415 if (sa1->sa_family != sa2->sa_family)
416 return 0;
417
418 switch (sa1->sa_family) {
419 case AF_INET:
420 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
421 case AF_INET6:
422 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
423 }
424 return 0;
425 }
426 #endif /* CONFIG_NFS_V4_1 */
427
428 /*
429 * Test if two socket addresses represent the same actual socket,
430 * by comparing (only) relevant fields, including the port number.
431 */
nfs_sockaddr_cmp(const struct sockaddr * sa1,const struct sockaddr * sa2)432 static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
433 const struct sockaddr *sa2)
434 {
435 if (sa1->sa_family != sa2->sa_family)
436 return 0;
437
438 switch (sa1->sa_family) {
439 case AF_INET:
440 return nfs_sockaddr_cmp_ip4(sa1, sa2);
441 case AF_INET6:
442 return nfs_sockaddr_cmp_ip6(sa1, sa2);
443 }
444 return 0;
445 }
446
447 #if defined(CONFIG_NFS_V4_1)
448 /* Common match routine for v4.0 and v4.1 callback services */
nfs4_cb_match_client(const struct sockaddr * addr,struct nfs_client * clp,u32 minorversion)449 static bool nfs4_cb_match_client(const struct sockaddr *addr,
450 struct nfs_client *clp, u32 minorversion)
451 {
452 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
453
454 /* Don't match clients that failed to initialise */
455 if (!(clp->cl_cons_state == NFS_CS_READY ||
456 clp->cl_cons_state == NFS_CS_SESSION_INITING))
457 return false;
458
459 /* Match the version and minorversion */
460 if (clp->rpc_ops->version != 4 ||
461 clp->cl_minorversion != minorversion)
462 return false;
463
464 /* Match only the IP address, not the port number */
465 if (!nfs_sockaddr_match_ipaddr(addr, clap))
466 return false;
467
468 return true;
469 }
470 #endif /* CONFIG_NFS_V4_1 */
471
472 /*
473 * Find an nfs_client on the list that matches the initialisation data
474 * that is supplied.
475 */
nfs_match_client(const struct nfs_client_initdata * data)476 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
477 {
478 struct nfs_client *clp;
479 const struct sockaddr *sap = data->addr;
480 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
481
482 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
483 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
484 /* Don't match clients that failed to initialise properly */
485 if (clp->cl_cons_state < 0)
486 continue;
487
488 /* Different NFS versions cannot share the same nfs_client */
489 if (clp->rpc_ops != data->rpc_ops)
490 continue;
491
492 if (clp->cl_proto != data->proto)
493 continue;
494 /* Match nfsv4 minorversion */
495 if (clp->cl_minorversion != data->minorversion)
496 continue;
497 /* Match the full socket address */
498 if (!nfs_sockaddr_cmp(sap, clap))
499 continue;
500
501 atomic_inc(&clp->cl_count);
502 return clp;
503 }
504 return NULL;
505 }
506
507 /*
508 * Look up a client by IP address and protocol version
509 * - creates a new record if one doesn't yet exist
510 */
511 static struct nfs_client *
nfs_get_client(const struct nfs_client_initdata * cl_init,const struct rpc_timeout * timeparms,const char * ip_addr,rpc_authflavor_t authflavour,int noresvport)512 nfs_get_client(const struct nfs_client_initdata *cl_init,
513 const struct rpc_timeout *timeparms,
514 const char *ip_addr,
515 rpc_authflavor_t authflavour,
516 int noresvport)
517 {
518 struct nfs_client *clp, *new = NULL;
519 int error;
520 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
521
522 dprintk("--> nfs_get_client(%s,v%u)\n",
523 cl_init->hostname ?: "", cl_init->rpc_ops->version);
524
525 /* see if the client already exists */
526 do {
527 spin_lock(&nn->nfs_client_lock);
528
529 clp = nfs_match_client(cl_init);
530 if (clp)
531 goto found_client;
532 if (new)
533 goto install_client;
534
535 spin_unlock(&nn->nfs_client_lock);
536
537 new = nfs_alloc_client(cl_init);
538 } while (!IS_ERR(new));
539
540 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
541 return new;
542
543 /* install a new client and return with it unready */
544 install_client:
545 clp = new;
546 list_add(&clp->cl_share_link, &nn->nfs_client_list);
547 spin_unlock(&nn->nfs_client_lock);
548
549 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
550 authflavour, noresvport);
551 if (error < 0) {
552 nfs_put_client(clp);
553 return ERR_PTR(error);
554 }
555 dprintk("--> nfs_get_client() = %p [new]\n", clp);
556 return clp;
557
558 /* found an existing client
559 * - make sure it's ready before returning
560 */
561 found_client:
562 spin_unlock(&nn->nfs_client_lock);
563
564 if (new)
565 nfs_free_client(new);
566
567 error = wait_event_killable(nfs_client_active_wq,
568 clp->cl_cons_state < NFS_CS_INITING);
569 if (error < 0) {
570 nfs_put_client(clp);
571 return ERR_PTR(-ERESTARTSYS);
572 }
573
574 if (clp->cl_cons_state < NFS_CS_READY) {
575 error = clp->cl_cons_state;
576 nfs_put_client(clp);
577 return ERR_PTR(error);
578 }
579
580 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
581
582 dprintk("--> nfs_get_client() = %p [share]\n", clp);
583 return clp;
584 }
585
586 /*
587 * Mark a server as ready or failed
588 */
nfs_mark_client_ready(struct nfs_client * clp,int state)589 void nfs_mark_client_ready(struct nfs_client *clp, int state)
590 {
591 clp->cl_cons_state = state;
592 wake_up_all(&nfs_client_active_wq);
593 }
594
595 /*
596 * With sessions, the client is not marked ready until after a
597 * successful EXCHANGE_ID and CREATE_SESSION.
598 *
599 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
600 * other versions of NFS can be tried.
601 */
nfs4_check_client_ready(struct nfs_client * clp)602 int nfs4_check_client_ready(struct nfs_client *clp)
603 {
604 if (!nfs4_has_session(clp))
605 return 0;
606 if (clp->cl_cons_state < NFS_CS_READY)
607 return -EPROTONOSUPPORT;
608 return 0;
609 }
610
611 /*
612 * Initialise the timeout values for a connection
613 */
nfs_init_timeout_values(struct rpc_timeout * to,int proto,unsigned int timeo,unsigned int retrans)614 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
615 unsigned int timeo, unsigned int retrans)
616 {
617 to->to_initval = timeo * HZ / 10;
618 to->to_retries = retrans;
619
620 switch (proto) {
621 case XPRT_TRANSPORT_TCP:
622 case XPRT_TRANSPORT_RDMA:
623 if (to->to_retries == 0)
624 to->to_retries = NFS_DEF_TCP_RETRANS;
625 if (to->to_initval == 0)
626 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
627 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
628 to->to_initval = NFS_MAX_TCP_TIMEOUT;
629 to->to_increment = to->to_initval;
630 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
631 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
632 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
633 if (to->to_maxval < to->to_initval)
634 to->to_maxval = to->to_initval;
635 to->to_exponential = 0;
636 break;
637 case XPRT_TRANSPORT_UDP:
638 if (to->to_retries == 0)
639 to->to_retries = NFS_DEF_UDP_RETRANS;
640 if (!to->to_initval)
641 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
642 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
643 to->to_initval = NFS_MAX_UDP_TIMEOUT;
644 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
645 to->to_exponential = 1;
646 break;
647 default:
648 BUG();
649 }
650 }
651
652 /*
653 * Create an RPC client handle
654 */
nfs_create_rpc_client(struct nfs_client * clp,const struct rpc_timeout * timeparms,rpc_authflavor_t flavor,int discrtry,int noresvport)655 static int nfs_create_rpc_client(struct nfs_client *clp,
656 const struct rpc_timeout *timeparms,
657 rpc_authflavor_t flavor,
658 int discrtry, int noresvport)
659 {
660 struct rpc_clnt *clnt = NULL;
661 struct rpc_create_args args = {
662 .net = clp->net,
663 .protocol = clp->cl_proto,
664 .address = (struct sockaddr *)&clp->cl_addr,
665 .addrsize = clp->cl_addrlen,
666 .timeout = timeparms,
667 .servername = clp->cl_hostname,
668 .program = &nfs_program,
669 .version = clp->rpc_ops->version,
670 .authflavor = flavor,
671 };
672
673 if (discrtry)
674 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
675 if (noresvport)
676 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
677
678 if (!IS_ERR(clp->cl_rpcclient))
679 return 0;
680
681 clnt = rpc_create(&args);
682 if (IS_ERR(clnt)) {
683 dprintk("%s: cannot create RPC client. Error = %ld\n",
684 __func__, PTR_ERR(clnt));
685 return PTR_ERR(clnt);
686 }
687
688 clp->cl_rpcclient = clnt;
689 return 0;
690 }
691
692 /*
693 * Version 2 or 3 client destruction
694 */
nfs_destroy_server(struct nfs_server * server)695 static void nfs_destroy_server(struct nfs_server *server)
696 {
697 if (server->nlm_host)
698 nlmclnt_done(server->nlm_host);
699 }
700
701 /*
702 * Version 2 or 3 lockd setup
703 */
nfs_start_lockd(struct nfs_server * server)704 static int nfs_start_lockd(struct nfs_server *server)
705 {
706 struct nlm_host *host;
707 struct nfs_client *clp = server->nfs_client;
708 struct nlmclnt_initdata nlm_init = {
709 .hostname = clp->cl_hostname,
710 .address = (struct sockaddr *)&clp->cl_addr,
711 .addrlen = clp->cl_addrlen,
712 .nfs_version = clp->rpc_ops->version,
713 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
714 1 : 0,
715 .net = clp->net,
716 };
717
718 if (nlm_init.nfs_version > 3)
719 return 0;
720 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
721 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
722 return 0;
723
724 switch (clp->cl_proto) {
725 default:
726 nlm_init.protocol = IPPROTO_TCP;
727 break;
728 case XPRT_TRANSPORT_UDP:
729 nlm_init.protocol = IPPROTO_UDP;
730 }
731
732 host = nlmclnt_init(&nlm_init);
733 if (IS_ERR(host))
734 return PTR_ERR(host);
735
736 server->nlm_host = host;
737 server->destroy = nfs_destroy_server;
738 return 0;
739 }
740
741 /*
742 * Initialise an NFSv3 ACL client connection
743 */
744 #ifdef CONFIG_NFS_V3_ACL
nfs_init_server_aclclient(struct nfs_server * server)745 static void nfs_init_server_aclclient(struct nfs_server *server)
746 {
747 if (server->nfs_client->rpc_ops->version != 3)
748 goto out_noacl;
749 if (server->flags & NFS_MOUNT_NOACL)
750 goto out_noacl;
751
752 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
753 if (IS_ERR(server->client_acl))
754 goto out_noacl;
755
756 /* No errors! Assume that Sun nfsacls are supported */
757 server->caps |= NFS_CAP_ACLS;
758 return;
759
760 out_noacl:
761 server->caps &= ~NFS_CAP_ACLS;
762 }
763 #else
nfs_init_server_aclclient(struct nfs_server * server)764 static inline void nfs_init_server_aclclient(struct nfs_server *server)
765 {
766 server->flags &= ~NFS_MOUNT_NOACL;
767 server->caps &= ~NFS_CAP_ACLS;
768 }
769 #endif
770
771 /*
772 * Create a general RPC client
773 */
nfs_init_server_rpcclient(struct nfs_server * server,const struct rpc_timeout * timeo,rpc_authflavor_t pseudoflavour)774 static int nfs_init_server_rpcclient(struct nfs_server *server,
775 const struct rpc_timeout *timeo,
776 rpc_authflavor_t pseudoflavour)
777 {
778 struct nfs_client *clp = server->nfs_client;
779
780 server->client = rpc_clone_client(clp->cl_rpcclient);
781 if (IS_ERR(server->client)) {
782 dprintk("%s: couldn't create rpc_client!\n", __func__);
783 return PTR_ERR(server->client);
784 }
785
786 memcpy(&server->client->cl_timeout_default,
787 timeo,
788 sizeof(server->client->cl_timeout_default));
789 server->client->cl_timeout = &server->client->cl_timeout_default;
790
791 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
792 struct rpc_auth *auth;
793
794 auth = rpcauth_create(pseudoflavour, server->client);
795 if (IS_ERR(auth)) {
796 dprintk("%s: couldn't create credcache!\n", __func__);
797 return PTR_ERR(auth);
798 }
799 }
800 server->client->cl_softrtry = 0;
801 if (server->flags & NFS_MOUNT_SOFT)
802 server->client->cl_softrtry = 1;
803
804 return 0;
805 }
806
807 /*
808 * Initialise an NFS2 or NFS3 client
809 */
nfs_init_client(struct nfs_client * clp,const struct rpc_timeout * timeparms,const char * ip_addr,rpc_authflavor_t authflavour,int noresvport)810 int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
811 const char *ip_addr, rpc_authflavor_t authflavour,
812 int noresvport)
813 {
814 int error;
815
816 if (clp->cl_cons_state == NFS_CS_READY) {
817 /* the client is already initialised */
818 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
819 return 0;
820 }
821
822 /*
823 * Create a client RPC handle for doing FSSTAT with UNIX auth only
824 * - RFC 2623, sec 2.3.2
825 */
826 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
827 0, noresvport);
828 if (error < 0)
829 goto error;
830 nfs_mark_client_ready(clp, NFS_CS_READY);
831 return 0;
832
833 error:
834 nfs_mark_client_ready(clp, error);
835 dprintk("<-- nfs_init_client() = xerror %d\n", error);
836 return error;
837 }
838
839 /*
840 * Create a version 2 or 3 client
841 */
nfs_init_server(struct nfs_server * server,const struct nfs_parsed_mount_data * data)842 static int nfs_init_server(struct nfs_server *server,
843 const struct nfs_parsed_mount_data *data)
844 {
845 struct nfs_client_initdata cl_init = {
846 .hostname = data->nfs_server.hostname,
847 .addr = (const struct sockaddr *)&data->nfs_server.address,
848 .addrlen = data->nfs_server.addrlen,
849 .rpc_ops = &nfs_v2_clientops,
850 .proto = data->nfs_server.protocol,
851 .net = data->net,
852 };
853 struct rpc_timeout timeparms;
854 struct nfs_client *clp;
855 int error;
856
857 dprintk("--> nfs_init_server()\n");
858
859 #ifdef CONFIG_NFS_V3
860 if (data->version == 3)
861 cl_init.rpc_ops = &nfs_v3_clientops;
862 #endif
863
864 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
865 data->timeo, data->retrans);
866
867 /* Allocate or find a client reference we can use */
868 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
869 data->flags & NFS_MOUNT_NORESVPORT);
870 if (IS_ERR(clp)) {
871 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
872 return PTR_ERR(clp);
873 }
874
875 server->nfs_client = clp;
876
877 /* Initialise the client representation from the mount data */
878 server->flags = data->flags;
879 server->options = data->options;
880 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
881 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
882 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
883
884 if (data->rsize)
885 server->rsize = nfs_block_size(data->rsize, NULL);
886 if (data->wsize)
887 server->wsize = nfs_block_size(data->wsize, NULL);
888
889 server->acregmin = data->acregmin * HZ;
890 server->acregmax = data->acregmax * HZ;
891 server->acdirmin = data->acdirmin * HZ;
892 server->acdirmax = data->acdirmax * HZ;
893
894 /* Start lockd here, before we might error out */
895 error = nfs_start_lockd(server);
896 if (error < 0)
897 goto error;
898
899 server->port = data->nfs_server.port;
900
901 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
902 if (error < 0)
903 goto error;
904
905 /* Preserve the values of mount_server-related mount options */
906 if (data->mount_server.addrlen) {
907 memcpy(&server->mountd_address, &data->mount_server.address,
908 data->mount_server.addrlen);
909 server->mountd_addrlen = data->mount_server.addrlen;
910 }
911 server->mountd_version = data->mount_server.version;
912 server->mountd_port = data->mount_server.port;
913 server->mountd_protocol = data->mount_server.protocol;
914
915 server->namelen = data->namlen;
916 /* Create a client RPC handle for the NFSv3 ACL management interface */
917 nfs_init_server_aclclient(server);
918 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
919 return 0;
920
921 error:
922 server->nfs_client = NULL;
923 nfs_put_client(clp);
924 dprintk("<-- nfs_init_server() = xerror %d\n", error);
925 return error;
926 }
927
928 /*
929 * Load up the server record from information gained in an fsinfo record
930 */
nfs_server_set_fsinfo(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fsinfo * fsinfo)931 static void nfs_server_set_fsinfo(struct nfs_server *server,
932 struct nfs_fh *mntfh,
933 struct nfs_fsinfo *fsinfo)
934 {
935 unsigned long max_rpc_payload;
936
937 /* Work out a lot of parameters */
938 if (server->rsize == 0)
939 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
940 if (server->wsize == 0)
941 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
942
943 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
944 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
945 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
946 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
947
948 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
949 if (server->rsize > max_rpc_payload)
950 server->rsize = max_rpc_payload;
951 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
952 server->rsize = NFS_MAX_FILE_IO_SIZE;
953 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
954
955 server->backing_dev_info.name = "nfs";
956 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
957
958 if (server->wsize > max_rpc_payload)
959 server->wsize = max_rpc_payload;
960 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
961 server->wsize = NFS_MAX_FILE_IO_SIZE;
962 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
963 server->pnfs_blksize = fsinfo->blksize;
964 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
965
966 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
967
968 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
969 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
970 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
971 if (server->dtsize > server->rsize)
972 server->dtsize = server->rsize;
973
974 if (server->flags & NFS_MOUNT_NOAC) {
975 server->acregmin = server->acregmax = 0;
976 server->acdirmin = server->acdirmax = 0;
977 }
978
979 server->maxfilesize = fsinfo->maxfilesize;
980
981 server->time_delta = fsinfo->time_delta;
982
983 /* We're airborne Set socket buffersize */
984 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
985 }
986
987 /*
988 * Probe filesystem information, including the FSID on v2/v3
989 */
nfs_probe_fsinfo(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fattr * fattr)990 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
991 {
992 struct nfs_fsinfo fsinfo;
993 struct nfs_client *clp = server->nfs_client;
994 int error;
995
996 dprintk("--> nfs_probe_fsinfo()\n");
997
998 if (clp->rpc_ops->set_capabilities != NULL) {
999 error = clp->rpc_ops->set_capabilities(server, mntfh);
1000 if (error < 0)
1001 goto out_error;
1002 }
1003
1004 fsinfo.fattr = fattr;
1005 fsinfo.layouttype = 0;
1006 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
1007 if (error < 0)
1008 goto out_error;
1009
1010 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
1011
1012 /* Get some general file system info */
1013 if (server->namelen == 0) {
1014 struct nfs_pathconf pathinfo;
1015
1016 pathinfo.fattr = fattr;
1017 nfs_fattr_init(fattr);
1018
1019 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1020 server->namelen = pathinfo.max_namelen;
1021 }
1022
1023 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1024 return 0;
1025
1026 out_error:
1027 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1028 return error;
1029 }
1030
1031 /*
1032 * Copy useful information when duplicating a server record
1033 */
nfs_server_copy_userdata(struct nfs_server * target,struct nfs_server * source)1034 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1035 {
1036 target->flags = source->flags;
1037 target->rsize = source->rsize;
1038 target->wsize = source->wsize;
1039 target->acregmin = source->acregmin;
1040 target->acregmax = source->acregmax;
1041 target->acdirmin = source->acdirmin;
1042 target->acdirmax = source->acdirmax;
1043 target->caps = source->caps;
1044 target->options = source->options;
1045 }
1046
nfs_server_insert_lists(struct nfs_server * server)1047 static void nfs_server_insert_lists(struct nfs_server *server)
1048 {
1049 struct nfs_client *clp = server->nfs_client;
1050 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
1051
1052 spin_lock(&nn->nfs_client_lock);
1053 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1054 list_add_tail(&server->master_link, &nn->nfs_volume_list);
1055 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1056 spin_unlock(&nn->nfs_client_lock);
1057
1058 }
1059
nfs_server_remove_lists(struct nfs_server * server)1060 static void nfs_server_remove_lists(struct nfs_server *server)
1061 {
1062 struct nfs_client *clp = server->nfs_client;
1063 struct nfs_net *nn;
1064
1065 if (clp == NULL)
1066 return;
1067 nn = net_generic(clp->net, nfs_net_id);
1068 spin_lock(&nn->nfs_client_lock);
1069 list_del_rcu(&server->client_link);
1070 if (list_empty(&clp->cl_superblocks))
1071 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
1072 list_del(&server->master_link);
1073 spin_unlock(&nn->nfs_client_lock);
1074
1075 synchronize_rcu();
1076 }
1077
1078 /*
1079 * Allocate and initialise a server record
1080 */
nfs_alloc_server(void)1081 static struct nfs_server *nfs_alloc_server(void)
1082 {
1083 struct nfs_server *server;
1084
1085 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1086 if (!server)
1087 return NULL;
1088
1089 server->client = server->client_acl = ERR_PTR(-EINVAL);
1090
1091 /* Zero out the NFS state stuff */
1092 INIT_LIST_HEAD(&server->client_link);
1093 INIT_LIST_HEAD(&server->master_link);
1094 INIT_LIST_HEAD(&server->delegations);
1095 INIT_LIST_HEAD(&server->layouts);
1096 INIT_LIST_HEAD(&server->state_owners_lru);
1097
1098 atomic_set(&server->active, 0);
1099
1100 server->io_stats = nfs_alloc_iostats();
1101 if (!server->io_stats) {
1102 kfree(server);
1103 return NULL;
1104 }
1105
1106 if (bdi_init(&server->backing_dev_info)) {
1107 nfs_free_iostats(server->io_stats);
1108 kfree(server);
1109 return NULL;
1110 }
1111
1112 ida_init(&server->openowner_id);
1113 ida_init(&server->lockowner_id);
1114 pnfs_init_server(server);
1115
1116 return server;
1117 }
1118
1119 /*
1120 * Free up a server record
1121 */
nfs_free_server(struct nfs_server * server)1122 void nfs_free_server(struct nfs_server *server)
1123 {
1124 dprintk("--> nfs_free_server()\n");
1125
1126 nfs_server_remove_lists(server);
1127 unset_pnfs_layoutdriver(server);
1128
1129 if (server->destroy != NULL)
1130 server->destroy(server);
1131
1132 if (!IS_ERR(server->client_acl))
1133 rpc_shutdown_client(server->client_acl);
1134 if (!IS_ERR(server->client))
1135 rpc_shutdown_client(server->client);
1136
1137 nfs_put_client(server->nfs_client);
1138
1139 ida_destroy(&server->lockowner_id);
1140 ida_destroy(&server->openowner_id);
1141 nfs_free_iostats(server->io_stats);
1142 bdi_destroy(&server->backing_dev_info);
1143 kfree(server);
1144 nfs_release_automount_timer();
1145 dprintk("<-- nfs_free_server()\n");
1146 }
1147
1148 /*
1149 * Create a version 2 or 3 volume record
1150 * - keyed on server and FSID
1151 */
nfs_create_server(const struct nfs_parsed_mount_data * data,struct nfs_fh * mntfh)1152 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
1153 struct nfs_fh *mntfh)
1154 {
1155 struct nfs_server *server;
1156 struct nfs_fattr *fattr;
1157 int error;
1158
1159 server = nfs_alloc_server();
1160 if (!server)
1161 return ERR_PTR(-ENOMEM);
1162
1163 error = -ENOMEM;
1164 fattr = nfs_alloc_fattr();
1165 if (fattr == NULL)
1166 goto error;
1167
1168 /* Get a client representation */
1169 error = nfs_init_server(server, data);
1170 if (error < 0)
1171 goto error;
1172
1173 BUG_ON(!server->nfs_client);
1174 BUG_ON(!server->nfs_client->rpc_ops);
1175 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1176
1177 /* Probe the root fh to retrieve its FSID */
1178 error = nfs_probe_fsinfo(server, mntfh, fattr);
1179 if (error < 0)
1180 goto error;
1181 if (server->nfs_client->rpc_ops->version == 3) {
1182 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1183 server->namelen = NFS3_MAXNAMLEN;
1184 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1185 server->caps |= NFS_CAP_READDIRPLUS;
1186 } else {
1187 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1188 server->namelen = NFS2_MAXNAMLEN;
1189 }
1190
1191 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1192 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
1193 if (error < 0) {
1194 dprintk("nfs_create_server: getattr error = %d\n", -error);
1195 goto error;
1196 }
1197 }
1198 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1199
1200 dprintk("Server FSID: %llx:%llx\n",
1201 (unsigned long long) server->fsid.major,
1202 (unsigned long long) server->fsid.minor);
1203
1204 nfs_server_insert_lists(server);
1205 server->mount_time = jiffies;
1206 nfs_free_fattr(fattr);
1207 return server;
1208
1209 error:
1210 nfs_free_fattr(fattr);
1211 nfs_free_server(server);
1212 return ERR_PTR(error);
1213 }
1214
1215 #ifdef CONFIG_NFS_V4
1216 /*
1217 * NFSv4.0 callback thread helper
1218 *
1219 * Find a client by callback identifier
1220 */
1221 struct nfs_client *
nfs4_find_client_ident(struct net * net,int cb_ident)1222 nfs4_find_client_ident(struct net *net, int cb_ident)
1223 {
1224 struct nfs_client *clp;
1225 struct nfs_net *nn = net_generic(net, nfs_net_id);
1226
1227 spin_lock(&nn->nfs_client_lock);
1228 clp = idr_find(&nn->cb_ident_idr, cb_ident);
1229 if (clp)
1230 atomic_inc(&clp->cl_count);
1231 spin_unlock(&nn->nfs_client_lock);
1232 return clp;
1233 }
1234
1235 #if defined(CONFIG_NFS_V4_1)
1236 /*
1237 * NFSv4.1 callback thread helper
1238 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1239 * minorversion, and sessionID
1240 *
1241 * Returns NULL if no such client
1242 */
1243 struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid)1244 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
1245 struct nfs4_sessionid *sid)
1246 {
1247 struct nfs_client *clp;
1248 struct nfs_net *nn = net_generic(net, nfs_net_id);
1249
1250 spin_lock(&nn->nfs_client_lock);
1251 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
1252 if (nfs4_cb_match_client(addr, clp, 1) == false)
1253 continue;
1254
1255 if (!nfs4_has_session(clp))
1256 continue;
1257
1258 /* Match sessionid*/
1259 if (memcmp(clp->cl_session->sess_id.data,
1260 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
1261 continue;
1262
1263 atomic_inc(&clp->cl_count);
1264 spin_unlock(&nn->nfs_client_lock);
1265 return clp;
1266 }
1267 spin_unlock(&nn->nfs_client_lock);
1268 return NULL;
1269 }
1270
1271 #else /* CONFIG_NFS_V4_1 */
1272
1273 struct nfs_client *
nfs4_find_client_sessionid(struct net * net,const struct sockaddr * addr,struct nfs4_sessionid * sid)1274 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
1275 struct nfs4_sessionid *sid)
1276 {
1277 return NULL;
1278 }
1279 #endif /* CONFIG_NFS_V4_1 */
1280
1281 /*
1282 * Initialize the NFS4 callback service
1283 */
nfs4_init_callback(struct nfs_client * clp)1284 static int nfs4_init_callback(struct nfs_client *clp)
1285 {
1286 int error;
1287
1288 if (clp->rpc_ops->version == 4) {
1289 struct rpc_xprt *xprt;
1290
1291 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
1292
1293 if (nfs4_has_session(clp)) {
1294 error = xprt_setup_backchannel(xprt,
1295 NFS41_BC_MIN_CALLBACKS);
1296 if (error < 0)
1297 return error;
1298 }
1299
1300 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
1301 if (error < 0) {
1302 dprintk("%s: failed to start callback. Error = %d\n",
1303 __func__, error);
1304 return error;
1305 }
1306 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1307 }
1308 return 0;
1309 }
1310
1311 /*
1312 * Initialize the minor version specific parts of an NFS4 client record
1313 */
nfs4_init_client_minor_version(struct nfs_client * clp)1314 static int nfs4_init_client_minor_version(struct nfs_client *clp)
1315 {
1316 #if defined(CONFIG_NFS_V4_1)
1317 if (clp->cl_mvops->minor_version) {
1318 struct nfs4_session *session = NULL;
1319 /*
1320 * Create the session and mark it expired.
1321 * When a SEQUENCE operation encounters the expired session
1322 * it will do session recovery to initialize it.
1323 */
1324 session = nfs4_alloc_session(clp);
1325 if (!session)
1326 return -ENOMEM;
1327
1328 clp->cl_session = session;
1329 /*
1330 * The create session reply races with the server back
1331 * channel probe. Mark the client NFS_CS_SESSION_INITING
1332 * so that the client back channel can find the
1333 * nfs_client struct
1334 */
1335 clp->cl_cons_state = NFS_CS_SESSION_INITING;
1336 }
1337 #endif /* CONFIG_NFS_V4_1 */
1338
1339 return nfs4_init_callback(clp);
1340 }
1341
1342 /*
1343 * Initialise an NFS4 client record
1344 */
nfs4_init_client(struct nfs_client * clp,const struct rpc_timeout * timeparms,const char * ip_addr,rpc_authflavor_t authflavour,int noresvport)1345 int nfs4_init_client(struct nfs_client *clp,
1346 const struct rpc_timeout *timeparms,
1347 const char *ip_addr,
1348 rpc_authflavor_t authflavour,
1349 int noresvport)
1350 {
1351 char buf[INET6_ADDRSTRLEN + 1];
1352 int error;
1353
1354 if (clp->cl_cons_state == NFS_CS_READY) {
1355 /* the client is initialised already */
1356 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1357 return 0;
1358 }
1359
1360 /* Check NFS protocol revision and initialize RPC op vector */
1361 clp->rpc_ops = &nfs_v4_clientops;
1362
1363 error = nfs_create_rpc_client(clp, timeparms, authflavour,
1364 1, noresvport);
1365 if (error < 0)
1366 goto error;
1367
1368 /* If no clientaddr= option was specified, find a usable cb address */
1369 if (ip_addr == NULL) {
1370 struct sockaddr_storage cb_addr;
1371 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
1372
1373 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
1374 if (error < 0)
1375 goto error;
1376 error = rpc_ntop(sap, buf, sizeof(buf));
1377 if (error < 0)
1378 goto error;
1379 ip_addr = (const char *)buf;
1380 }
1381 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
1382
1383 error = nfs_idmap_new(clp);
1384 if (error < 0) {
1385 dprintk("%s: failed to create idmapper. Error = %d\n",
1386 __func__, error);
1387 goto error;
1388 }
1389 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
1390
1391 error = nfs4_init_client_minor_version(clp);
1392 if (error < 0)
1393 goto error;
1394
1395 if (!nfs4_has_session(clp))
1396 nfs_mark_client_ready(clp, NFS_CS_READY);
1397 return 0;
1398
1399 error:
1400 nfs_mark_client_ready(clp, error);
1401 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1402 return error;
1403 }
1404
1405 /*
1406 * Set up an NFS4 client
1407 */
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,u32 minorversion,struct net * net)1408 static int nfs4_set_client(struct nfs_server *server,
1409 const char *hostname,
1410 const struct sockaddr *addr,
1411 const size_t addrlen,
1412 const char *ip_addr,
1413 rpc_authflavor_t authflavour,
1414 int proto, const struct rpc_timeout *timeparms,
1415 u32 minorversion, struct net *net)
1416 {
1417 struct nfs_client_initdata cl_init = {
1418 .hostname = hostname,
1419 .addr = addr,
1420 .addrlen = addrlen,
1421 .rpc_ops = &nfs_v4_clientops,
1422 .proto = proto,
1423 .minorversion = minorversion,
1424 .net = net,
1425 };
1426 struct nfs_client *clp;
1427 int error;
1428
1429 dprintk("--> nfs4_set_client()\n");
1430
1431 /* Allocate or find a client reference we can use */
1432 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1433 server->flags & NFS_MOUNT_NORESVPORT);
1434 if (IS_ERR(clp)) {
1435 error = PTR_ERR(clp);
1436 goto error;
1437 }
1438
1439 /*
1440 * Query for the lease time on clientid setup or renewal
1441 *
1442 * Note that this will be set on nfs_clients that were created
1443 * only for the DS role and did not set this bit, but now will
1444 * serve a dual role.
1445 */
1446 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1447
1448 server->nfs_client = clp;
1449 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1450 return 0;
1451 error:
1452 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1453 return error;
1454 }
1455
1456 /*
1457 * Set up a pNFS Data Server client.
1458 *
1459 * Return any existing nfs_client that matches server address,port,version
1460 * and minorversion.
1461 *
1462 * For a new nfs_client, use a soft mount (default), a low retrans and a
1463 * low timeout interval so that if a connection is lost, we retry through
1464 * the MDS.
1465 */
nfs4_set_ds_client(struct nfs_client * mds_clp,const struct sockaddr * ds_addr,int ds_addrlen,int ds_proto)1466 struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1467 const struct sockaddr *ds_addr,
1468 int ds_addrlen, int ds_proto)
1469 {
1470 struct nfs_client_initdata cl_init = {
1471 .addr = ds_addr,
1472 .addrlen = ds_addrlen,
1473 .rpc_ops = &nfs_v4_clientops,
1474 .proto = ds_proto,
1475 .minorversion = mds_clp->cl_minorversion,
1476 .net = mds_clp->net,
1477 };
1478 struct rpc_timeout ds_timeout = {
1479 .to_initval = 15 * HZ,
1480 .to_maxval = 15 * HZ,
1481 .to_retries = 1,
1482 .to_exponential = 1,
1483 };
1484 struct nfs_client *clp;
1485
1486 /*
1487 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1488 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1489 * (section 13.1 RFC 5661).
1490 */
1491 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1492 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1493
1494 dprintk("<-- %s %p\n", __func__, clp);
1495 return clp;
1496 }
1497 EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
1498
1499 /*
1500 * Session has been established, and the client marked ready.
1501 * Set the mount rsize and wsize with negotiated fore channel
1502 * attributes which will be bound checked in nfs_server_set_fsinfo.
1503 */
nfs4_session_set_rwsize(struct nfs_server * server)1504 static void nfs4_session_set_rwsize(struct nfs_server *server)
1505 {
1506 #ifdef CONFIG_NFS_V4_1
1507 struct nfs4_session *sess;
1508 u32 server_resp_sz;
1509 u32 server_rqst_sz;
1510
1511 if (!nfs4_has_session(server->nfs_client))
1512 return;
1513 sess = server->nfs_client->cl_session;
1514 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1515 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1516
1517 if (server->rsize > server_resp_sz)
1518 server->rsize = server_resp_sz;
1519 if (server->wsize > server_rqst_sz)
1520 server->wsize = server_rqst_sz;
1521 #endif /* CONFIG_NFS_V4_1 */
1522 }
1523
nfs4_server_common_setup(struct nfs_server * server,struct nfs_fh * mntfh)1524 static int nfs4_server_common_setup(struct nfs_server *server,
1525 struct nfs_fh *mntfh)
1526 {
1527 struct nfs_fattr *fattr;
1528 int error;
1529
1530 BUG_ON(!server->nfs_client);
1531 BUG_ON(!server->nfs_client->rpc_ops);
1532 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1533
1534 /* data servers support only a subset of NFSv4.1 */
1535 if (is_ds_only_client(server->nfs_client))
1536 return -EPROTONOSUPPORT;
1537
1538 fattr = nfs_alloc_fattr();
1539 if (fattr == NULL)
1540 return -ENOMEM;
1541
1542 /* We must ensure the session is initialised first */
1543 error = nfs4_init_session(server);
1544 if (error < 0)
1545 goto out;
1546
1547 /* Probe the root fh to retrieve its FSID and filehandle */
1548 error = nfs4_get_rootfh(server, mntfh);
1549 if (error < 0)
1550 goto out;
1551
1552 dprintk("Server FSID: %llx:%llx\n",
1553 (unsigned long long) server->fsid.major,
1554 (unsigned long long) server->fsid.minor);
1555 dprintk("Mount FH: %d\n", mntfh->size);
1556
1557 nfs4_session_set_rwsize(server);
1558
1559 error = nfs_probe_fsinfo(server, mntfh, fattr);
1560 if (error < 0)
1561 goto out;
1562
1563 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1564 server->namelen = NFS4_MAXNAMLEN;
1565
1566 nfs_server_insert_lists(server);
1567 server->mount_time = jiffies;
1568 server->destroy = nfs4_destroy_server;
1569 out:
1570 nfs_free_fattr(fattr);
1571 return error;
1572 }
1573
1574 /*
1575 * Create a version 4 volume record
1576 */
nfs4_init_server(struct nfs_server * server,const struct nfs_parsed_mount_data * data)1577 static int nfs4_init_server(struct nfs_server *server,
1578 const struct nfs_parsed_mount_data *data)
1579 {
1580 struct rpc_timeout timeparms;
1581 int error;
1582
1583 dprintk("--> nfs4_init_server()\n");
1584
1585 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1586 data->timeo, data->retrans);
1587
1588 /* Initialise the client representation from the mount data */
1589 server->flags = data->flags;
1590 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1591 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1592 server->caps |= NFS_CAP_READDIRPLUS;
1593 server->options = data->options;
1594
1595 /* Get a client record */
1596 error = nfs4_set_client(server,
1597 data->nfs_server.hostname,
1598 (const struct sockaddr *)&data->nfs_server.address,
1599 data->nfs_server.addrlen,
1600 data->client_address,
1601 data->auth_flavors[0],
1602 data->nfs_server.protocol,
1603 &timeparms,
1604 data->minorversion,
1605 data->net);
1606 if (error < 0)
1607 goto error;
1608
1609 /*
1610 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1611 * authentication.
1612 */
1613 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1614 server->caps |= NFS_CAP_UIDGID_NOMAP;
1615
1616 if (data->rsize)
1617 server->rsize = nfs_block_size(data->rsize, NULL);
1618 if (data->wsize)
1619 server->wsize = nfs_block_size(data->wsize, NULL);
1620
1621 server->acregmin = data->acregmin * HZ;
1622 server->acregmax = data->acregmax * HZ;
1623 server->acdirmin = data->acdirmin * HZ;
1624 server->acdirmax = data->acdirmax * HZ;
1625
1626 server->port = data->nfs_server.port;
1627
1628 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1629
1630 error:
1631 /* Done */
1632 dprintk("<-- nfs4_init_server() = %d\n", error);
1633 return error;
1634 }
1635
1636 /*
1637 * Create a version 4 volume record
1638 * - keyed on server and FSID
1639 */
nfs4_create_server(const struct nfs_parsed_mount_data * data,struct nfs_fh * mntfh)1640 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1641 struct nfs_fh *mntfh)
1642 {
1643 struct nfs_server *server;
1644 int error;
1645
1646 dprintk("--> nfs4_create_server()\n");
1647
1648 server = nfs_alloc_server();
1649 if (!server)
1650 return ERR_PTR(-ENOMEM);
1651
1652 /* set up the general RPC client */
1653 error = nfs4_init_server(server, data);
1654 if (error < 0)
1655 goto error;
1656
1657 error = nfs4_server_common_setup(server, mntfh);
1658 if (error < 0)
1659 goto error;
1660
1661 dprintk("<-- nfs4_create_server() = %p\n", server);
1662 return server;
1663
1664 error:
1665 nfs_free_server(server);
1666 dprintk("<-- nfs4_create_server() = error %d\n", error);
1667 return ERR_PTR(error);
1668 }
1669
1670 /*
1671 * Create an NFS4 referral server record
1672 */
nfs4_create_referral_server(struct nfs_clone_mount * data,struct nfs_fh * mntfh)1673 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1674 struct nfs_fh *mntfh)
1675 {
1676 struct nfs_client *parent_client;
1677 struct nfs_server *server, *parent_server;
1678 int error;
1679
1680 dprintk("--> nfs4_create_referral_server()\n");
1681
1682 server = nfs_alloc_server();
1683 if (!server)
1684 return ERR_PTR(-ENOMEM);
1685
1686 parent_server = NFS_SB(data->sb);
1687 parent_client = parent_server->nfs_client;
1688
1689 /* Initialise the client representation from the parent server */
1690 nfs_server_copy_userdata(server, parent_server);
1691 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
1692
1693 /* Get a client representation.
1694 * Note: NFSv4 always uses TCP, */
1695 error = nfs4_set_client(server, data->hostname,
1696 data->addr,
1697 data->addrlen,
1698 parent_client->cl_ipaddr,
1699 data->authflavor,
1700 rpc_protocol(parent_server->client),
1701 parent_server->client->cl_timeout,
1702 parent_client->cl_mvops->minor_version,
1703 parent_client->net);
1704 if (error < 0)
1705 goto error;
1706
1707 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1708 if (error < 0)
1709 goto error;
1710
1711 error = nfs4_server_common_setup(server, mntfh);
1712 if (error < 0)
1713 goto error;
1714
1715 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1716 return server;
1717
1718 error:
1719 nfs_free_server(server);
1720 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1721 return ERR_PTR(error);
1722 }
1723
1724 #endif /* CONFIG_NFS_V4 */
1725
1726 /*
1727 * Clone an NFS2, NFS3 or NFS4 server record
1728 */
nfs_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)1729 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1730 struct nfs_fh *fh,
1731 struct nfs_fattr *fattr,
1732 rpc_authflavor_t flavor)
1733 {
1734 struct nfs_server *server;
1735 struct nfs_fattr *fattr_fsinfo;
1736 int error;
1737
1738 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1739 (unsigned long long) fattr->fsid.major,
1740 (unsigned long long) fattr->fsid.minor);
1741
1742 server = nfs_alloc_server();
1743 if (!server)
1744 return ERR_PTR(-ENOMEM);
1745
1746 error = -ENOMEM;
1747 fattr_fsinfo = nfs_alloc_fattr();
1748 if (fattr_fsinfo == NULL)
1749 goto out_free_server;
1750
1751 /* Copy data from the source */
1752 server->nfs_client = source->nfs_client;
1753 server->destroy = source->destroy;
1754 atomic_inc(&server->nfs_client->cl_count);
1755 nfs_server_copy_userdata(server, source);
1756
1757 server->fsid = fattr->fsid;
1758
1759 error = nfs_init_server_rpcclient(server,
1760 source->client->cl_timeout,
1761 flavor);
1762 if (error < 0)
1763 goto out_free_server;
1764 if (!IS_ERR(source->client_acl))
1765 nfs_init_server_aclclient(server);
1766
1767 /* probe the filesystem info for this server filesystem */
1768 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1769 if (error < 0)
1770 goto out_free_server;
1771
1772 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1773 server->namelen = NFS4_MAXNAMLEN;
1774
1775 dprintk("Cloned FSID: %llx:%llx\n",
1776 (unsigned long long) server->fsid.major,
1777 (unsigned long long) server->fsid.minor);
1778
1779 error = nfs_start_lockd(server);
1780 if (error < 0)
1781 goto out_free_server;
1782
1783 nfs_server_insert_lists(server);
1784 server->mount_time = jiffies;
1785
1786 nfs_free_fattr(fattr_fsinfo);
1787 dprintk("<-- nfs_clone_server() = %p\n", server);
1788 return server;
1789
1790 out_free_server:
1791 nfs_free_fattr(fattr_fsinfo);
1792 nfs_free_server(server);
1793 dprintk("<-- nfs_clone_server() = error %d\n", error);
1794 return ERR_PTR(error);
1795 }
1796
nfs_clients_init(struct net * net)1797 void nfs_clients_init(struct net *net)
1798 {
1799 struct nfs_net *nn = net_generic(net, nfs_net_id);
1800
1801 INIT_LIST_HEAD(&nn->nfs_client_list);
1802 INIT_LIST_HEAD(&nn->nfs_volume_list);
1803 #ifdef CONFIG_NFS_V4
1804 idr_init(&nn->cb_ident_idr);
1805 #endif
1806 spin_lock_init(&nn->nfs_client_lock);
1807 }
1808
1809 #ifdef CONFIG_PROC_FS
1810 static struct proc_dir_entry *proc_fs_nfs;
1811
1812 static int nfs_server_list_open(struct inode *inode, struct file *file);
1813 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1814 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1815 static void nfs_server_list_stop(struct seq_file *p, void *v);
1816 static int nfs_server_list_show(struct seq_file *m, void *v);
1817
1818 static const struct seq_operations nfs_server_list_ops = {
1819 .start = nfs_server_list_start,
1820 .next = nfs_server_list_next,
1821 .stop = nfs_server_list_stop,
1822 .show = nfs_server_list_show,
1823 };
1824
1825 static const struct file_operations nfs_server_list_fops = {
1826 .open = nfs_server_list_open,
1827 .read = seq_read,
1828 .llseek = seq_lseek,
1829 .release = seq_release,
1830 .owner = THIS_MODULE,
1831 };
1832
1833 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1834 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1835 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1836 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1837 static int nfs_volume_list_show(struct seq_file *m, void *v);
1838
1839 static const struct seq_operations nfs_volume_list_ops = {
1840 .start = nfs_volume_list_start,
1841 .next = nfs_volume_list_next,
1842 .stop = nfs_volume_list_stop,
1843 .show = nfs_volume_list_show,
1844 };
1845
1846 static const struct file_operations nfs_volume_list_fops = {
1847 .open = nfs_volume_list_open,
1848 .read = seq_read,
1849 .llseek = seq_lseek,
1850 .release = seq_release,
1851 .owner = THIS_MODULE,
1852 };
1853
1854 /*
1855 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1856 * we're dealing
1857 */
nfs_server_list_open(struct inode * inode,struct file * file)1858 static int nfs_server_list_open(struct inode *inode, struct file *file)
1859 {
1860 struct seq_file *m;
1861 int ret;
1862 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1863 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
1864
1865 ret = seq_open(file, &nfs_server_list_ops);
1866 if (ret < 0)
1867 return ret;
1868
1869 m = file->private_data;
1870 m->private = net;
1871
1872 return 0;
1873 }
1874
1875 /*
1876 * set up the iterator to start reading from the server list and return the first item
1877 */
nfs_server_list_start(struct seq_file * m,loff_t * _pos)1878 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1879 {
1880 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1881
1882 /* lock the list against modification */
1883 spin_lock(&nn->nfs_client_lock);
1884 return seq_list_start_head(&nn->nfs_client_list, *_pos);
1885 }
1886
1887 /*
1888 * move to next server
1889 */
nfs_server_list_next(struct seq_file * p,void * v,loff_t * pos)1890 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1891 {
1892 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1893
1894 return seq_list_next(v, &nn->nfs_client_list, pos);
1895 }
1896
1897 /*
1898 * clean up after reading from the transports list
1899 */
nfs_server_list_stop(struct seq_file * p,void * v)1900 static void nfs_server_list_stop(struct seq_file *p, void *v)
1901 {
1902 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1903
1904 spin_unlock(&nn->nfs_client_lock);
1905 }
1906
1907 /*
1908 * display a header line followed by a load of call lines
1909 */
nfs_server_list_show(struct seq_file * m,void * v)1910 static int nfs_server_list_show(struct seq_file *m, void *v)
1911 {
1912 struct nfs_client *clp;
1913 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1914
1915 /* display header on line 1 */
1916 if (v == &nn->nfs_client_list) {
1917 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1918 return 0;
1919 }
1920
1921 /* display one transport per line on subsequent lines */
1922 clp = list_entry(v, struct nfs_client, cl_share_link);
1923
1924 /* Check if the client is initialized */
1925 if (clp->cl_cons_state != NFS_CS_READY)
1926 return 0;
1927
1928 rcu_read_lock();
1929 seq_printf(m, "v%u %s %s %3d %s\n",
1930 clp->rpc_ops->version,
1931 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1932 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1933 atomic_read(&clp->cl_count),
1934 clp->cl_hostname);
1935 rcu_read_unlock();
1936
1937 return 0;
1938 }
1939
1940 /*
1941 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1942 */
nfs_volume_list_open(struct inode * inode,struct file * file)1943 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1944 {
1945 struct seq_file *m;
1946 int ret;
1947 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1948 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
1949
1950 ret = seq_open(file, &nfs_volume_list_ops);
1951 if (ret < 0)
1952 return ret;
1953
1954 m = file->private_data;
1955 m->private = net;
1956
1957 return 0;
1958 }
1959
1960 /*
1961 * set up the iterator to start reading from the volume list and return the first item
1962 */
nfs_volume_list_start(struct seq_file * m,loff_t * _pos)1963 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1964 {
1965 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1966
1967 /* lock the list against modification */
1968 spin_lock(&nn->nfs_client_lock);
1969 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1970 }
1971
1972 /*
1973 * move to next volume
1974 */
nfs_volume_list_next(struct seq_file * p,void * v,loff_t * pos)1975 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1976 {
1977 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1978
1979 return seq_list_next(v, &nn->nfs_volume_list, pos);
1980 }
1981
1982 /*
1983 * clean up after reading from the transports list
1984 */
nfs_volume_list_stop(struct seq_file * p,void * v)1985 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1986 {
1987 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1988
1989 spin_unlock(&nn->nfs_client_lock);
1990 }
1991
1992 /*
1993 * display a header line followed by a load of call lines
1994 */
nfs_volume_list_show(struct seq_file * m,void * v)1995 static int nfs_volume_list_show(struct seq_file *m, void *v)
1996 {
1997 struct nfs_server *server;
1998 struct nfs_client *clp;
1999 char dev[8], fsid[17];
2000 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
2001
2002 /* display header on line 1 */
2003 if (v == &nn->nfs_volume_list) {
2004 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
2005 return 0;
2006 }
2007 /* display one transport per line on subsequent lines */
2008 server = list_entry(v, struct nfs_server, master_link);
2009 clp = server->nfs_client;
2010
2011 snprintf(dev, 8, "%u:%u",
2012 MAJOR(server->s_dev), MINOR(server->s_dev));
2013
2014 snprintf(fsid, 17, "%llx:%llx",
2015 (unsigned long long) server->fsid.major,
2016 (unsigned long long) server->fsid.minor);
2017
2018 rcu_read_lock();
2019 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
2020 clp->rpc_ops->version,
2021 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
2022 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
2023 dev,
2024 fsid,
2025 nfs_server_fscache_state(server));
2026 rcu_read_unlock();
2027
2028 return 0;
2029 }
2030
2031 /*
2032 * initialise the /proc/fs/nfsfs/ directory
2033 */
nfs_fs_proc_init(void)2034 int __init nfs_fs_proc_init(void)
2035 {
2036 struct proc_dir_entry *p;
2037
2038 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
2039 if (!proc_fs_nfs)
2040 goto error_0;
2041
2042 /* a file of servers with which we're dealing */
2043 p = proc_create("servers", S_IFREG|S_IRUGO,
2044 proc_fs_nfs, &nfs_server_list_fops);
2045 if (!p)
2046 goto error_1;
2047
2048 /* a file of volumes that we have mounted */
2049 p = proc_create("volumes", S_IFREG|S_IRUGO,
2050 proc_fs_nfs, &nfs_volume_list_fops);
2051 if (!p)
2052 goto error_2;
2053 return 0;
2054
2055 error_2:
2056 remove_proc_entry("servers", proc_fs_nfs);
2057 error_1:
2058 remove_proc_entry("fs/nfsfs", NULL);
2059 error_0:
2060 return -ENOMEM;
2061 }
2062
2063 /*
2064 * clean up the /proc/fs/nfsfs/ directory
2065 */
nfs_fs_proc_exit(void)2066 void nfs_fs_proc_exit(void)
2067 {
2068 remove_proc_entry("volumes", proc_fs_nfs);
2069 remove_proc_entry("servers", proc_fs_nfs);
2070 remove_proc_entry("fs/nfsfs", NULL);
2071 }
2072
2073 #endif /* CONFIG_PROC_FS */
2074
2075 module_param(nfs4_disable_idmapping, bool, 0644);
2076 MODULE_PARM_DESC(nfs4_disable_idmapping,
2077 "Turn off NFSv4 idmapping when using 'sec=sys'");
2078