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