1
2
3
4
5
6
7
8
9
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/addr.h>
24#include <linux/sunrpc/clnt.h>
25#include <linux/sunrpc/stats.h>
26#include <linux/sunrpc/metrics.h>
27#include <linux/sunrpc/xprtsock.h>
28#include <linux/sunrpc/xprtrdma.h>
29#include <linux/nfs_fs.h>
30#include <linux/nfs_mount.h>
31#include <linux/nfs4_mount.h>
32#include <linux/lockd/bind.h>
33#include <linux/seq_file.h>
34#include <linux/mount.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 "nfs.h"
55#include "netns.h"
56
57#define NFSDBG_FACILITY NFSDBG_CLIENT
58
59static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
60static DEFINE_SPINLOCK(nfs_version_lock);
61static DEFINE_MUTEX(nfs_version_mutex);
62static LIST_HEAD(nfs_versions);
63
64
65
66
67static const struct rpc_version *nfs_version[5] = {
68 [2] = NULL,
69 [3] = NULL,
70 [4] = NULL,
71};
72
73const struct rpc_program nfs_program = {
74 .name = "nfs",
75 .number = NFS_PROGRAM,
76 .nrvers = ARRAY_SIZE(nfs_version),
77 .version = nfs_version,
78 .stats = &nfs_rpcstat,
79 .pipe_dir_name = NFS_PIPE_DIRNAME,
80};
81
82struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
84};
85
86static struct nfs_subversion *find_nfs_version(unsigned int version)
87{
88 struct nfs_subversion *nfs;
89 spin_lock(&nfs_version_lock);
90
91 list_for_each_entry(nfs, &nfs_versions, list) {
92 if (nfs->rpc_ops->version == version) {
93 spin_unlock(&nfs_version_lock);
94 return nfs;
95 }
96 }
97
98 spin_unlock(&nfs_version_lock);
99 return ERR_PTR(-EPROTONOSUPPORT);
100}
101
102struct nfs_subversion *get_nfs_version(unsigned int version)
103{
104 struct nfs_subversion *nfs = find_nfs_version(version);
105
106 if (IS_ERR(nfs)) {
107 mutex_lock(&nfs_version_mutex);
108 request_module("nfsv%d", version);
109 nfs = find_nfs_version(version);
110 mutex_unlock(&nfs_version_mutex);
111 }
112
113 if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
114 return ERR_PTR(-EAGAIN);
115 return nfs;
116}
117
118void put_nfs_version(struct nfs_subversion *nfs)
119{
120 module_put(nfs->owner);
121}
122
123void register_nfs_version(struct nfs_subversion *nfs)
124{
125 spin_lock(&nfs_version_lock);
126
127 list_add(&nfs->list, &nfs_versions);
128 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
129
130 spin_unlock(&nfs_version_lock);
131}
132EXPORT_SYMBOL_GPL(register_nfs_version);
133
134void unregister_nfs_version(struct nfs_subversion *nfs)
135{
136 spin_lock(&nfs_version_lock);
137
138 nfs_version[nfs->rpc_ops->version] = NULL;
139 list_del(&nfs->list);
140
141 spin_unlock(&nfs_version_lock);
142}
143EXPORT_SYMBOL_GPL(unregister_nfs_version);
144
145
146
147
148
149
150
151struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
152{
153 struct nfs_client *clp;
154 struct rpc_cred *cred;
155 int err = -ENOMEM;
156
157 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
158 goto error_0;
159
160 clp->cl_nfs_mod = cl_init->nfs_mod;
161 if (!try_module_get(clp->cl_nfs_mod->owner))
162 goto error_dealloc;
163
164 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
165
166 atomic_set(&clp->cl_count, 1);
167 clp->cl_cons_state = NFS_CS_INITING;
168
169 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
170 clp->cl_addrlen = cl_init->addrlen;
171
172 if (cl_init->hostname) {
173 err = -ENOMEM;
174 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
175 if (!clp->cl_hostname)
176 goto error_cleanup;
177 }
178
179 INIT_LIST_HEAD(&clp->cl_superblocks);
180 clp->cl_rpcclient = ERR_PTR(-EINVAL);
181
182 clp->cl_proto = cl_init->proto;
183 clp->cl_net = get_net(cl_init->net);
184
185 cred = rpc_lookup_machine_cred("*");
186 if (!IS_ERR(cred))
187 clp->cl_machine_cred = cred;
188 nfs_fscache_get_client_cookie(clp);
189
190 return clp;
191
192error_cleanup:
193 put_nfs_version(clp->cl_nfs_mod);
194error_dealloc:
195 kfree(clp);
196error_0:
197 return ERR_PTR(err);
198}
199EXPORT_SYMBOL_GPL(nfs_alloc_client);
200
201#if IS_ENABLED(CONFIG_NFS_V4)
202void nfs_cleanup_cb_ident_idr(struct net *net)
203{
204 struct nfs_net *nn = net_generic(net, nfs_net_id);
205
206 idr_destroy(&nn->cb_ident_idr);
207}
208
209
210static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
211{
212 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
213
214 if (clp->cl_cb_ident)
215 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
216}
217
218static void pnfs_init_server(struct nfs_server *server)
219{
220 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
221 rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
222}
223
224#else
225void nfs_cleanup_cb_ident_idr(struct net *net)
226{
227}
228
229static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
230{
231}
232
233static void pnfs_init_server(struct nfs_server *server)
234{
235}
236
237#endif
238
239
240
241
242void nfs_free_client(struct nfs_client *clp)
243{
244 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
245
246 nfs_fscache_release_client_cookie(clp);
247
248
249 if (!IS_ERR(clp->cl_rpcclient))
250 rpc_shutdown_client(clp->cl_rpcclient);
251
252 if (clp->cl_machine_cred != NULL)
253 put_rpccred(clp->cl_machine_cred);
254
255 put_net(clp->cl_net);
256 put_nfs_version(clp->cl_nfs_mod);
257 kfree(clp->cl_hostname);
258 kfree(clp->cl_acceptor);
259 kfree(clp);
260
261 dprintk("<-- nfs_free_client()\n");
262}
263EXPORT_SYMBOL_GPL(nfs_free_client);
264
265
266
267
268void nfs_put_client(struct nfs_client *clp)
269{
270 struct nfs_net *nn;
271
272 if (!clp)
273 return;
274
275 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
276 nn = net_generic(clp->cl_net, nfs_net_id);
277
278 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
279 list_del(&clp->cl_share_link);
280 nfs_cb_idr_remove_locked(clp);
281 spin_unlock(&nn->nfs_client_lock);
282
283 WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
284
285 clp->rpc_ops->free_client(clp);
286 }
287}
288EXPORT_SYMBOL_GPL(nfs_put_client);
289
290
291
292
293
294static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
295{
296 struct nfs_client *clp;
297 const struct sockaddr *sap = data->addr;
298 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
299
300 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
301 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
302
303 if (clp->cl_cons_state < 0)
304 continue;
305
306
307 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
308 continue;
309
310 if (clp->cl_proto != data->proto)
311 continue;
312
313 if (clp->cl_minorversion != data->minorversion)
314 continue;
315
316 if (!rpc_cmp_addr_port(sap, clap))
317
318 if (IS_ERR(clp->cl_rpcclient) ||
319 !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
320 sap))
321 continue;
322
323 atomic_inc(&clp->cl_count);
324 return clp;
325 }
326 return NULL;
327}
328
329
330
331
332
333
334bool nfs_client_init_is_complete(const struct nfs_client *clp)
335{
336 return clp->cl_cons_state <= NFS_CS_READY;
337}
338EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
339
340
341
342
343
344
345
346int nfs_client_init_status(const struct nfs_client *clp)
347{
348
349 if (clp->cl_cons_state > NFS_CS_READY) {
350 WARN_ON_ONCE(1);
351 return -EINVAL;
352 }
353 return clp->cl_cons_state;
354}
355EXPORT_SYMBOL_GPL(nfs_client_init_status);
356
357int nfs_wait_client_init_complete(const struct nfs_client *clp)
358{
359 return wait_event_killable(nfs_client_active_wq,
360 nfs_client_init_is_complete(clp));
361}
362EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
363
364
365
366
367static struct nfs_client *
368nfs_found_client(const struct nfs_client_initdata *cl_init,
369 struct nfs_client *clp)
370{
371 int error;
372
373 error = nfs_wait_client_init_complete(clp);
374 if (error < 0) {
375 nfs_put_client(clp);
376 return ERR_PTR(-ERESTARTSYS);
377 }
378
379 if (clp->cl_cons_state < NFS_CS_READY) {
380 error = clp->cl_cons_state;
381 nfs_put_client(clp);
382 return ERR_PTR(error);
383 }
384
385 smp_rmb();
386
387 dprintk("<-- %s found nfs_client %p for %s\n",
388 __func__, clp, cl_init->hostname ?: "");
389 return clp;
390}
391
392
393
394
395
396struct nfs_client *
397nfs_get_client(const struct nfs_client_initdata *cl_init,
398 rpc_authflavor_t authflavour)
399{
400 struct nfs_client *clp, *new = NULL;
401 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
402 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
403
404 if (cl_init->hostname == NULL) {
405 WARN_ON(1);
406 return NULL;
407 }
408
409 dprintk("--> nfs_get_client(%s,v%u)\n",
410 cl_init->hostname, rpc_ops->version);
411
412
413 do {
414 spin_lock(&nn->nfs_client_lock);
415
416 clp = nfs_match_client(cl_init);
417 if (clp) {
418 spin_unlock(&nn->nfs_client_lock);
419 if (new)
420 new->rpc_ops->free_client(new);
421 return nfs_found_client(cl_init, clp);
422 }
423 if (new) {
424 list_add_tail(&new->cl_share_link,
425 &nn->nfs_client_list);
426 spin_unlock(&nn->nfs_client_lock);
427 new->cl_flags = cl_init->init_flags;
428 return rpc_ops->init_client(new, cl_init);
429 }
430
431 spin_unlock(&nn->nfs_client_lock);
432
433 new = rpc_ops->alloc_client(cl_init);
434 } while (!IS_ERR(new));
435
436 dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
437 cl_init->hostname, PTR_ERR(new));
438 return new;
439}
440EXPORT_SYMBOL_GPL(nfs_get_client);
441
442
443
444
445void nfs_mark_client_ready(struct nfs_client *clp, int state)
446{
447 smp_wmb();
448 clp->cl_cons_state = state;
449 wake_up_all(&nfs_client_active_wq);
450}
451EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
452
453
454
455
456void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
457 int timeo, int retrans)
458{
459 to->to_initval = timeo * HZ / 10;
460 to->to_retries = retrans;
461
462 switch (proto) {
463 case XPRT_TRANSPORT_TCP:
464 case XPRT_TRANSPORT_RDMA:
465 if (retrans == NFS_UNSPEC_RETRANS)
466 to->to_retries = NFS_DEF_TCP_RETRANS;
467 if (timeo == NFS_UNSPEC_TIMEO || to->to_retries == 0)
468 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
469 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
470 to->to_initval = NFS_MAX_TCP_TIMEOUT;
471 to->to_increment = to->to_initval;
472 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
473 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
474 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
475 if (to->to_maxval < to->to_initval)
476 to->to_maxval = to->to_initval;
477 to->to_exponential = 0;
478 break;
479 case XPRT_TRANSPORT_UDP:
480 if (retrans == NFS_UNSPEC_RETRANS)
481 to->to_retries = NFS_DEF_UDP_RETRANS;
482 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
483 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
484 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
485 to->to_initval = NFS_MAX_UDP_TIMEOUT;
486 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
487 to->to_exponential = 1;
488 break;
489 default:
490 BUG();
491 }
492}
493EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
494
495
496
497
498int nfs_create_rpc_client(struct nfs_client *clp,
499 const struct nfs_client_initdata *cl_init,
500 rpc_authflavor_t flavor)
501{
502 struct rpc_clnt *clnt = NULL;
503 struct rpc_create_args args = {
504 .net = clp->cl_net,
505 .protocol = clp->cl_proto,
506 .address = (struct sockaddr *)&clp->cl_addr,
507 .addrsize = clp->cl_addrlen,
508 .timeout = cl_init->timeparms,
509 .servername = clp->cl_hostname,
510 .nodename = cl_init->nodename,
511 .program = &nfs_program,
512 .version = clp->rpc_ops->version,
513 .authflavor = flavor,
514 };
515
516 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
517 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
518 if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
519 args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
520 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
521 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
522 if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
523 args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
524
525 if (!IS_ERR(clp->cl_rpcclient))
526 return 0;
527
528 clnt = rpc_create(&args);
529 if (IS_ERR(clnt)) {
530 dprintk("%s: cannot create RPC client. Error = %ld\n",
531 __func__, PTR_ERR(clnt));
532 return PTR_ERR(clnt);
533 }
534
535 clp->cl_rpcclient = clnt;
536 return 0;
537}
538EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
539
540
541
542
543static void nfs_destroy_server(struct nfs_server *server)
544{
545 if (server->nlm_host)
546 nlmclnt_done(server->nlm_host);
547}
548
549
550
551
552static int nfs_start_lockd(struct nfs_server *server)
553{
554 struct nlm_host *host;
555 struct nfs_client *clp = server->nfs_client;
556 struct nlmclnt_initdata nlm_init = {
557 .hostname = clp->cl_hostname,
558 .address = (struct sockaddr *)&clp->cl_addr,
559 .addrlen = clp->cl_addrlen,
560 .nfs_version = clp->rpc_ops->version,
561 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
562 1 : 0,
563 .net = clp->cl_net,
564 .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops,
565 };
566
567 if (nlm_init.nfs_version > 3)
568 return 0;
569 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
570 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
571 return 0;
572
573 switch (clp->cl_proto) {
574 default:
575 nlm_init.protocol = IPPROTO_TCP;
576 break;
577 case XPRT_TRANSPORT_UDP:
578 nlm_init.protocol = IPPROTO_UDP;
579 }
580
581 host = nlmclnt_init(&nlm_init);
582 if (IS_ERR(host))
583 return PTR_ERR(host);
584
585 server->nlm_host = host;
586 server->destroy = nfs_destroy_server;
587 return 0;
588}
589
590
591
592
593int nfs_init_server_rpcclient(struct nfs_server *server,
594 const struct rpc_timeout *timeo,
595 rpc_authflavor_t pseudoflavour)
596{
597 struct nfs_client *clp = server->nfs_client;
598
599 server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
600 pseudoflavour);
601 if (IS_ERR(server->client)) {
602 dprintk("%s: couldn't create rpc_client!\n", __func__);
603 return PTR_ERR(server->client);
604 }
605
606 memcpy(&server->client->cl_timeout_default,
607 timeo,
608 sizeof(server->client->cl_timeout_default));
609 server->client->cl_timeout = &server->client->cl_timeout_default;
610 server->client->cl_softrtry = 0;
611 if (server->flags & NFS_MOUNT_SOFT)
612 server->client->cl_softrtry = 1;
613
614 return 0;
615}
616EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
617
618
619
620
621
622
623
624
625
626struct nfs_client *nfs_init_client(struct nfs_client *clp,
627 const struct nfs_client_initdata *cl_init)
628{
629 int error;
630
631 if (clp->cl_cons_state == NFS_CS_READY) {
632
633 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
634 return clp;
635 }
636
637
638
639
640
641 error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
642 if (error < 0)
643 goto error;
644 nfs_mark_client_ready(clp, NFS_CS_READY);
645 return clp;
646
647error:
648 nfs_mark_client_ready(clp, error);
649 nfs_put_client(clp);
650 dprintk("<-- nfs_init_client() = xerror %d\n", error);
651 return ERR_PTR(error);
652}
653EXPORT_SYMBOL_GPL(nfs_init_client);
654
655
656
657
658static int nfs_init_server(struct nfs_server *server,
659 const struct nfs_parsed_mount_data *data,
660 struct nfs_subversion *nfs_mod)
661{
662 struct rpc_timeout timeparms;
663 struct nfs_client_initdata cl_init = {
664 .hostname = data->nfs_server.hostname,
665 .addr = (const struct sockaddr *)&data->nfs_server.address,
666 .addrlen = data->nfs_server.addrlen,
667 .nfs_mod = nfs_mod,
668 .proto = data->nfs_server.protocol,
669 .net = data->net,
670 .timeparms = &timeparms,
671 };
672 struct nfs_client *clp;
673 int error;
674
675 dprintk("--> nfs_init_server()\n");
676
677 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
678 data->timeo, data->retrans);
679 if (data->flags & NFS_MOUNT_NORESVPORT)
680 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
681
682
683 clp = nfs_get_client(&cl_init, RPC_AUTH_UNIX);
684 if (IS_ERR(clp)) {
685 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
686 return PTR_ERR(clp);
687 }
688
689 server->nfs_client = clp;
690
691
692 server->flags = data->flags;
693 server->options = data->options;
694 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
695 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
696 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
697
698 if (data->rsize)
699 server->rsize = nfs_block_size(data->rsize, NULL);
700 if (data->wsize)
701 server->wsize = nfs_block_size(data->wsize, NULL);
702
703 server->acregmin = data->acregmin * HZ;
704 server->acregmax = data->acregmax * HZ;
705 server->acdirmin = data->acdirmin * HZ;
706 server->acdirmax = data->acdirmax * HZ;
707
708
709 error = nfs_start_lockd(server);
710 if (error < 0)
711 goto error;
712
713 server->port = data->nfs_server.port;
714 server->auth_info = data->auth_info;
715
716 error = nfs_init_server_rpcclient(server, &timeparms,
717 data->selected_flavor);
718 if (error < 0)
719 goto error;
720
721
722 if (data->mount_server.addrlen) {
723 memcpy(&server->mountd_address, &data->mount_server.address,
724 data->mount_server.addrlen);
725 server->mountd_addrlen = data->mount_server.addrlen;
726 }
727 server->mountd_version = data->mount_server.version;
728 server->mountd_port = data->mount_server.port;
729 server->mountd_protocol = data->mount_server.protocol;
730
731 server->namelen = data->namlen;
732 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
733 return 0;
734
735error:
736 server->nfs_client = NULL;
737 nfs_put_client(clp);
738 dprintk("<-- nfs_init_server() = xerror %d\n", error);
739 return error;
740}
741
742
743
744
745static void nfs_server_set_fsinfo(struct nfs_server *server,
746 struct nfs_fsinfo *fsinfo)
747{
748 unsigned long max_rpc_payload;
749
750
751 if (server->rsize == 0)
752 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
753 if (server->wsize == 0)
754 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
755
756 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
757 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
758 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
759 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
760
761 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
762 if (server->rsize > max_rpc_payload)
763 server->rsize = max_rpc_payload;
764 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
765 server->rsize = NFS_MAX_FILE_IO_SIZE;
766 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
767
768 server->backing_dev_info.name = "nfs";
769 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
770
771 if (server->wsize > max_rpc_payload)
772 server->wsize = max_rpc_payload;
773 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
774 server->wsize = NFS_MAX_FILE_IO_SIZE;
775 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
776
777 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
778
779 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
780 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
781 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
782 if (server->dtsize > server->rsize)
783 server->dtsize = server->rsize;
784
785 if (server->flags & NFS_MOUNT_NOAC) {
786 server->acregmin = server->acregmax = 0;
787 server->acdirmin = server->acdirmax = 0;
788 }
789
790 server->maxfilesize = fsinfo->maxfilesize;
791
792 server->time_delta = fsinfo->time_delta;
793
794 server->clone_blksize = fsinfo->clone_blksize;
795
796 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
797}
798
799
800
801
802int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
803{
804 struct nfs_fsinfo fsinfo;
805 struct nfs_client *clp = server->nfs_client;
806 int error;
807
808 dprintk("--> nfs_probe_fsinfo()\n");
809
810 if (clp->rpc_ops->set_capabilities != NULL) {
811 error = clp->rpc_ops->set_capabilities(server, mntfh);
812 if (error < 0)
813 goto out_error;
814 }
815
816 fsinfo.fattr = fattr;
817 fsinfo.nlayouttypes = 0;
818 memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
819 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
820 if (error < 0)
821 goto out_error;
822
823 nfs_server_set_fsinfo(server, &fsinfo);
824
825
826 if (server->namelen == 0) {
827 struct nfs_pathconf pathinfo;
828
829 pathinfo.fattr = fattr;
830 nfs_fattr_init(fattr);
831
832 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
833 server->namelen = pathinfo.max_namelen;
834 }
835
836 dprintk("<-- nfs_probe_fsinfo() = 0\n");
837 return 0;
838
839out_error:
840 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
841 return error;
842}
843EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
844
845
846
847
848void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
849{
850 target->flags = source->flags;
851 target->rsize = source->rsize;
852 target->wsize = source->wsize;
853 target->acregmin = source->acregmin;
854 target->acregmax = source->acregmax;
855 target->acdirmin = source->acdirmin;
856 target->acdirmax = source->acdirmax;
857 target->caps = source->caps;
858 target->options = source->options;
859 target->auth_info = source->auth_info;
860}
861EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
862
863void nfs_server_insert_lists(struct nfs_server *server)
864{
865 struct nfs_client *clp = server->nfs_client;
866 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
867
868 spin_lock(&nn->nfs_client_lock);
869 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
870 list_add_tail(&server->master_link, &nn->nfs_volume_list);
871 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
872 spin_unlock(&nn->nfs_client_lock);
873
874}
875EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
876
877void nfs_server_remove_lists(struct nfs_server *server)
878{
879 struct nfs_client *clp = server->nfs_client;
880 struct nfs_net *nn;
881
882 if (clp == NULL)
883 return;
884 nn = net_generic(clp->cl_net, nfs_net_id);
885 spin_lock(&nn->nfs_client_lock);
886 list_del_rcu(&server->client_link);
887 if (list_empty(&clp->cl_superblocks))
888 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
889 list_del(&server->master_link);
890 spin_unlock(&nn->nfs_client_lock);
891
892 synchronize_rcu();
893}
894EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
895
896
897
898
899struct nfs_server *nfs_alloc_server(void)
900{
901 struct nfs_server *server;
902
903 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
904 if (!server)
905 return NULL;
906
907 server->client = server->client_acl = ERR_PTR(-EINVAL);
908
909
910 INIT_LIST_HEAD(&server->client_link);
911 INIT_LIST_HEAD(&server->master_link);
912 INIT_LIST_HEAD(&server->delegations);
913 INIT_LIST_HEAD(&server->layouts);
914 INIT_LIST_HEAD(&server->state_owners_lru);
915
916 atomic_set(&server->active, 0);
917
918 server->io_stats = nfs_alloc_iostats();
919 if (!server->io_stats) {
920 kfree(server);
921 return NULL;
922 }
923
924 if (bdi_init(&server->backing_dev_info)) {
925 nfs_free_iostats(server->io_stats);
926 kfree(server);
927 return NULL;
928 }
929
930 ida_init(&server->openowner_id);
931 ida_init(&server->lockowner_id);
932 pnfs_init_server(server);
933
934 return server;
935}
936EXPORT_SYMBOL_GPL(nfs_alloc_server);
937
938
939
940
941void nfs_free_server(struct nfs_server *server)
942{
943 dprintk("--> nfs_free_server()\n");
944
945 nfs_server_remove_lists(server);
946
947 if (server->destroy != NULL)
948 server->destroy(server);
949
950 if (!IS_ERR(server->client_acl))
951 rpc_shutdown_client(server->client_acl);
952 if (!IS_ERR(server->client))
953 rpc_shutdown_client(server->client);
954
955 nfs_put_client(server->nfs_client);
956
957 ida_destroy(&server->lockowner_id);
958 ida_destroy(&server->openowner_id);
959 nfs_free_iostats(server->io_stats);
960 bdi_destroy(&server->backing_dev_info);
961 kfree(server);
962 nfs_release_automount_timer();
963 dprintk("<-- nfs_free_server()\n");
964}
965EXPORT_SYMBOL_GPL(nfs_free_server);
966
967
968
969
970
971struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
972 struct nfs_subversion *nfs_mod)
973{
974 struct nfs_server *server;
975 struct nfs_fattr *fattr;
976 int error;
977
978 server = nfs_alloc_server();
979 if (!server)
980 return ERR_PTR(-ENOMEM);
981
982 error = -ENOMEM;
983 fattr = nfs_alloc_fattr();
984 if (fattr == NULL)
985 goto error;
986
987
988 error = nfs_init_server(server, mount_info->parsed, nfs_mod);
989 if (error < 0)
990 goto error;
991
992
993 error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
994 if (error < 0)
995 goto error;
996 if (server->nfs_client->rpc_ops->version == 3) {
997 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
998 server->namelen = NFS3_MAXNAMLEN;
999 if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
1000 server->caps |= NFS_CAP_READDIRPLUS;
1001 } else {
1002 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1003 server->namelen = NFS2_MAXNAMLEN;
1004 }
1005
1006 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1007 error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr, NULL);
1008 if (error < 0) {
1009 dprintk("nfs_create_server: getattr error = %d\n", -error);
1010 goto error;
1011 }
1012 }
1013 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1014
1015 dprintk("Server FSID: %llx:%llx\n",
1016 (unsigned long long) server->fsid.major,
1017 (unsigned long long) server->fsid.minor);
1018
1019 nfs_server_insert_lists(server);
1020 server->mount_time = jiffies;
1021 nfs_free_fattr(fattr);
1022 return server;
1023
1024error:
1025 nfs_free_fattr(fattr);
1026 nfs_free_server(server);
1027 return ERR_PTR(error);
1028}
1029EXPORT_SYMBOL_GPL(nfs_create_server);
1030
1031
1032
1033
1034struct nfs_server *nfs_clone_server(struct nfs_server *source,
1035 struct nfs_fh *fh,
1036 struct nfs_fattr *fattr,
1037 rpc_authflavor_t flavor)
1038{
1039 struct nfs_server *server;
1040 struct nfs_fattr *fattr_fsinfo;
1041 int error;
1042
1043 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1044 (unsigned long long) fattr->fsid.major,
1045 (unsigned long long) fattr->fsid.minor);
1046
1047 server = nfs_alloc_server();
1048 if (!server)
1049 return ERR_PTR(-ENOMEM);
1050
1051 error = -ENOMEM;
1052 fattr_fsinfo = nfs_alloc_fattr();
1053 if (fattr_fsinfo == NULL)
1054 goto out_free_server;
1055
1056
1057 server->nfs_client = source->nfs_client;
1058 server->destroy = source->destroy;
1059 atomic_inc(&server->nfs_client->cl_count);
1060 nfs_server_copy_userdata(server, source);
1061
1062 server->fsid = fattr->fsid;
1063
1064 error = nfs_init_server_rpcclient(server,
1065 source->client->cl_timeout,
1066 flavor);
1067 if (error < 0)
1068 goto out_free_server;
1069
1070
1071 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1072 if (error < 0)
1073 goto out_free_server;
1074
1075 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1076 server->namelen = NFS4_MAXNAMLEN;
1077
1078 dprintk("Cloned FSID: %llx:%llx\n",
1079 (unsigned long long) server->fsid.major,
1080 (unsigned long long) server->fsid.minor);
1081
1082 error = nfs_start_lockd(server);
1083 if (error < 0)
1084 goto out_free_server;
1085
1086 nfs_server_insert_lists(server);
1087 server->mount_time = jiffies;
1088
1089 nfs_free_fattr(fattr_fsinfo);
1090 dprintk("<-- nfs_clone_server() = %p\n", server);
1091 return server;
1092
1093out_free_server:
1094 nfs_free_fattr(fattr_fsinfo);
1095 nfs_free_server(server);
1096 dprintk("<-- nfs_clone_server() = error %d\n", error);
1097 return ERR_PTR(error);
1098}
1099EXPORT_SYMBOL_GPL(nfs_clone_server);
1100
1101void nfs_clients_init(struct net *net)
1102{
1103 struct nfs_net *nn = net_generic(net, nfs_net_id);
1104
1105 INIT_LIST_HEAD(&nn->nfs_client_list);
1106 INIT_LIST_HEAD(&nn->nfs_volume_list);
1107#if IS_ENABLED(CONFIG_NFS_V4)
1108 idr_init(&nn->cb_ident_idr);
1109#endif
1110 spin_lock_init(&nn->nfs_client_lock);
1111 nn->boot_time = CURRENT_TIME;
1112}
1113
1114#ifdef CONFIG_PROC_FS
1115static int nfs_server_list_open(struct inode *inode, struct file *file);
1116static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1117static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1118static void nfs_server_list_stop(struct seq_file *p, void *v);
1119static int nfs_server_list_show(struct seq_file *m, void *v);
1120
1121static const struct seq_operations nfs_server_list_ops = {
1122 .start = nfs_server_list_start,
1123 .next = nfs_server_list_next,
1124 .stop = nfs_server_list_stop,
1125 .show = nfs_server_list_show,
1126};
1127
1128static const struct file_operations nfs_server_list_fops = {
1129 .open = nfs_server_list_open,
1130 .read = seq_read,
1131 .llseek = seq_lseek,
1132 .release = seq_release_net,
1133 .owner = THIS_MODULE,
1134};
1135
1136static int nfs_volume_list_open(struct inode *inode, struct file *file);
1137static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1138static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1139static void nfs_volume_list_stop(struct seq_file *p, void *v);
1140static int nfs_volume_list_show(struct seq_file *m, void *v);
1141
1142static const struct seq_operations nfs_volume_list_ops = {
1143 .start = nfs_volume_list_start,
1144 .next = nfs_volume_list_next,
1145 .stop = nfs_volume_list_stop,
1146 .show = nfs_volume_list_show,
1147};
1148
1149static const struct file_operations nfs_volume_list_fops = {
1150 .open = nfs_volume_list_open,
1151 .read = seq_read,
1152 .llseek = seq_lseek,
1153 .release = seq_release_net,
1154 .owner = THIS_MODULE,
1155};
1156
1157
1158
1159
1160
1161static int nfs_server_list_open(struct inode *inode, struct file *file)
1162{
1163 return seq_open_net(inode, file, &nfs_server_list_ops,
1164 sizeof(struct seq_net_private));
1165}
1166
1167
1168
1169
1170static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1171 __acquires(&nn->nfs_client_lock)
1172{
1173 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1174
1175
1176 spin_lock(&nn->nfs_client_lock);
1177 return seq_list_start_head(&nn->nfs_client_list, *_pos);
1178}
1179
1180
1181
1182
1183static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1184{
1185 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1186
1187 return seq_list_next(v, &nn->nfs_client_list, pos);
1188}
1189
1190
1191
1192
1193static void nfs_server_list_stop(struct seq_file *p, void *v)
1194 __releases(&nn->nfs_client_lock)
1195{
1196 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1197
1198 spin_unlock(&nn->nfs_client_lock);
1199}
1200
1201
1202
1203
1204static int nfs_server_list_show(struct seq_file *m, void *v)
1205{
1206 struct nfs_client *clp;
1207 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1208
1209
1210 if (v == &nn->nfs_client_list) {
1211 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1212 return 0;
1213 }
1214
1215
1216 clp = list_entry(v, struct nfs_client, cl_share_link);
1217
1218
1219 if (clp->cl_cons_state != NFS_CS_READY)
1220 return 0;
1221
1222 rcu_read_lock();
1223 seq_printf(m, "v%u %s %s %3d %s\n",
1224 clp->rpc_ops->version,
1225 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1226 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1227 atomic_read(&clp->cl_count),
1228 clp->cl_hostname);
1229 rcu_read_unlock();
1230
1231 return 0;
1232}
1233
1234
1235
1236
1237static int nfs_volume_list_open(struct inode *inode, struct file *file)
1238{
1239 return seq_open_net(inode, file, &nfs_volume_list_ops,
1240 sizeof(struct seq_net_private));
1241}
1242
1243
1244
1245
1246static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1247 __acquires(&nn->nfs_client_lock)
1248{
1249 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1250
1251
1252 spin_lock(&nn->nfs_client_lock);
1253 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1254}
1255
1256
1257
1258
1259static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1260{
1261 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1262
1263 return seq_list_next(v, &nn->nfs_volume_list, pos);
1264}
1265
1266
1267
1268
1269static void nfs_volume_list_stop(struct seq_file *p, void *v)
1270 __releases(&nn->nfs_client_lock)
1271{
1272 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1273
1274 spin_unlock(&nn->nfs_client_lock);
1275}
1276
1277
1278
1279
1280static int nfs_volume_list_show(struct seq_file *m, void *v)
1281{
1282 struct nfs_server *server;
1283 struct nfs_client *clp;
1284 char dev[13];
1285 char fsid[34];
1286 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1287
1288
1289 if (v == &nn->nfs_volume_list) {
1290 seq_puts(m, "NV SERVER PORT DEV FSID"
1291 " FSC\n");
1292 return 0;
1293 }
1294
1295 server = list_entry(v, struct nfs_server, master_link);
1296 clp = server->nfs_client;
1297
1298 snprintf(dev, sizeof(dev), "%u:%u",
1299 MAJOR(server->s_dev), MINOR(server->s_dev));
1300
1301 snprintf(fsid, sizeof(fsid), "%llx:%llx",
1302 (unsigned long long) server->fsid.major,
1303 (unsigned long long) server->fsid.minor);
1304
1305 rcu_read_lock();
1306 seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
1307 clp->rpc_ops->version,
1308 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1309 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1310 dev,
1311 fsid,
1312 nfs_server_fscache_state(server));
1313 rcu_read_unlock();
1314
1315 return 0;
1316}
1317
1318int nfs_fs_proc_net_init(struct net *net)
1319{
1320 struct nfs_net *nn = net_generic(net, nfs_net_id);
1321 struct proc_dir_entry *p;
1322
1323 nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
1324 if (!nn->proc_nfsfs)
1325 goto error_0;
1326
1327
1328 p = proc_create("servers", S_IFREG|S_IRUGO,
1329 nn->proc_nfsfs, &nfs_server_list_fops);
1330 if (!p)
1331 goto error_1;
1332
1333
1334 p = proc_create("volumes", S_IFREG|S_IRUGO,
1335 nn->proc_nfsfs, &nfs_volume_list_fops);
1336 if (!p)
1337 goto error_1;
1338 return 0;
1339
1340error_1:
1341 remove_proc_subtree("nfsfs", net->proc_net);
1342error_0:
1343 return -ENOMEM;
1344}
1345
1346void nfs_fs_proc_net_exit(struct net *net)
1347{
1348 remove_proc_subtree("nfsfs", net->proc_net);
1349}
1350
1351
1352
1353
1354int __init nfs_fs_proc_init(void)
1355{
1356 if (!proc_mkdir("fs/nfsfs", NULL))
1357 goto error_0;
1358
1359
1360 if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
1361 goto error_1;
1362
1363
1364 if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
1365 goto error_1;
1366
1367 return 0;
1368error_1:
1369 remove_proc_subtree("fs/nfsfs", NULL);
1370error_0:
1371 return -ENOMEM;
1372}
1373
1374
1375
1376
1377void nfs_fs_proc_exit(void)
1378{
1379 remove_proc_subtree("fs/nfsfs", NULL);
1380}
1381
1382#endif
1383