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