1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* client.c: NFS client sharing and management code
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
7
8
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/sched.h>
12 #include <linux/time.h>
13 #include <linux/kernel.h>
14 #include <linux/mm.h>
15 #include <linux/string.h>
16 #include <linux/stat.h>
17 #include <linux/errno.h>
18 #include <linux/unistd.h>
19 #include <linux/sunrpc/addr.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/sunrpc/stats.h>
22 #include <linux/sunrpc/metrics.h>
23 #include <linux/sunrpc/xprtsock.h>
24 #include <linux/sunrpc/xprtrdma.h>
25 #include <linux/nfs_fs.h>
26 #include <linux/nfs_mount.h>
27 #include <linux/nfs4_mount.h>
28 #include <linux/lockd/bind.h>
29 #include <linux/seq_file.h>
30 #include <linux/mount.h>
31 #include <linux/vfs.h>
32 #include <linux/inet.h>
33 #include <linux/in6.h>
34 #include <linux/slab.h>
35 #include <linux/idr.h>
36 #include <net/ipv6.h>
37 #include <linux/nfs_xdr.h>
38 #include <linux/sunrpc/bc_xprt.h>
39 #include <linux/nsproxy.h>
40 #include <linux/pid_namespace.h>
41
42
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47 #include "internal.h"
48 #include "fscache.h"
49 #include "pnfs.h"
50 #include "nfs.h"
51 #include "netns.h"
52 #include "sysfs.h"
53 #include "nfs42.h"
54
55 #define NFSDBG_FACILITY NFSDBG_CLIENT
56
57 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
58 static DEFINE_SPINLOCK(nfs_version_lock);
59 static DEFINE_MUTEX(nfs_version_mutex);
60 static LIST_HEAD(nfs_versions);
61
62 /*
63 * RPC cruft for NFS
64 */
65 static const struct rpc_version *nfs_version[5] = {
66 [2] = NULL,
67 [3] = NULL,
68 [4] = NULL,
69 };
70
71 const struct rpc_program nfs_program = {
72 .name = "nfs",
73 .number = NFS_PROGRAM,
74 .nrvers = ARRAY_SIZE(nfs_version),
75 .version = nfs_version,
76 .pipe_dir_name = NFS_PIPE_DIRNAME,
77 };
78
79 struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
81 };
82
find_nfs_version(unsigned int version)83 static struct nfs_subversion *find_nfs_version(unsigned int version)
84 {
85 struct nfs_subversion *nfs;
86 spin_lock(&nfs_version_lock);
87
88 list_for_each_entry(nfs, &nfs_versions, list) {
89 if (nfs->rpc_ops->version == version) {
90 spin_unlock(&nfs_version_lock);
91 return nfs;
92 }
93 }
94
95 spin_unlock(&nfs_version_lock);
96 return ERR_PTR(-EPROTONOSUPPORT);
97 }
98
get_nfs_version(unsigned int version)99 struct nfs_subversion *get_nfs_version(unsigned int version)
100 {
101 struct nfs_subversion *nfs = find_nfs_version(version);
102
103 if (IS_ERR(nfs)) {
104 mutex_lock(&nfs_version_mutex);
105 request_module("nfsv%d", version);
106 nfs = find_nfs_version(version);
107 mutex_unlock(&nfs_version_mutex);
108 }
109
110 if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
111 return ERR_PTR(-EAGAIN);
112 return nfs;
113 }
114
put_nfs_version(struct nfs_subversion * nfs)115 void put_nfs_version(struct nfs_subversion *nfs)
116 {
117 module_put(nfs->owner);
118 }
119
register_nfs_version(struct nfs_subversion * nfs)120 void register_nfs_version(struct nfs_subversion *nfs)
121 {
122 spin_lock(&nfs_version_lock);
123
124 list_add(&nfs->list, &nfs_versions);
125 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
126
127 spin_unlock(&nfs_version_lock);
128 }
129 EXPORT_SYMBOL_GPL(register_nfs_version);
130
unregister_nfs_version(struct nfs_subversion * nfs)131 void unregister_nfs_version(struct nfs_subversion *nfs)
132 {
133 spin_lock(&nfs_version_lock);
134
135 nfs_version[nfs->rpc_ops->version] = NULL;
136 list_del(&nfs->list);
137
138 spin_unlock(&nfs_version_lock);
139 }
140 EXPORT_SYMBOL_GPL(unregister_nfs_version);
141
142 /*
143 * Allocate a shared client record
144 *
145 * Since these are allocated/deallocated very rarely, we don't
146 * bother putting them in a slab cache...
147 */
nfs_alloc_client(const struct nfs_client_initdata * cl_init)148 struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
149 {
150 struct nfs_client *clp;
151 int err = -ENOMEM;
152
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
154 goto error_0;
155
156 clp->cl_minorversion = cl_init->minorversion;
157 clp->cl_nfs_mod = cl_init->nfs_mod;
158 if (!try_module_get(clp->cl_nfs_mod->owner))
159 goto error_dealloc;
160
161 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
162
163 refcount_set(&clp->cl_count, 1);
164 clp->cl_cons_state = NFS_CS_INITING;
165
166 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
167 clp->cl_addrlen = cl_init->addrlen;
168
169 if (cl_init->hostname) {
170 err = -ENOMEM;
171 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
172 if (!clp->cl_hostname)
173 goto error_cleanup;
174 }
175
176 INIT_LIST_HEAD(&clp->cl_superblocks);
177 clp->cl_rpcclient = ERR_PTR(-EINVAL);
178
179 clp->cl_flags = cl_init->init_flags;
180 clp->cl_proto = cl_init->proto;
181 clp->cl_nconnect = cl_init->nconnect;
182 clp->cl_max_connect = cl_init->max_connect ? cl_init->max_connect : 1;
183 clp->cl_net = get_net(cl_init->net);
184
185 clp->cl_principal = "*";
186 clp->cl_xprtsec = cl_init->xprtsec;
187 return clp;
188
189 error_cleanup:
190 put_nfs_version(clp->cl_nfs_mod);
191 error_dealloc:
192 kfree(clp);
193 error_0:
194 return ERR_PTR(err);
195 }
196 EXPORT_SYMBOL_GPL(nfs_alloc_client);
197
198 #if IS_ENABLED(CONFIG_NFS_V4)
nfs_cleanup_cb_ident_idr(struct net * net)199 static void nfs_cleanup_cb_ident_idr(struct net *net)
200 {
201 struct nfs_net *nn = net_generic(net, nfs_net_id);
202
203 idr_destroy(&nn->cb_ident_idr);
204 }
205
206 /* nfs_client_lock held */
nfs_cb_idr_remove_locked(struct nfs_client * clp)207 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
208 {
209 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
210
211 if (clp->cl_cb_ident)
212 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
213 }
214
pnfs_init_server(struct nfs_server * server)215 static void pnfs_init_server(struct nfs_server *server)
216 {
217 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
218 }
219
220 #else
nfs_cleanup_cb_ident_idr(struct net * net)221 static void nfs_cleanup_cb_ident_idr(struct net *net)
222 {
223 }
224
nfs_cb_idr_remove_locked(struct nfs_client * clp)225 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
226 {
227 }
228
pnfs_init_server(struct nfs_server * server)229 static void pnfs_init_server(struct nfs_server *server)
230 {
231 }
232
233 #endif /* CONFIG_NFS_V4 */
234
235 /*
236 * Destroy a shared client record
237 */
nfs_free_client(struct nfs_client * clp)238 void nfs_free_client(struct nfs_client *clp)
239 {
240 /* -EIO all pending I/O */
241 if (!IS_ERR(clp->cl_rpcclient))
242 rpc_shutdown_client(clp->cl_rpcclient);
243
244 put_net(clp->cl_net);
245 put_nfs_version(clp->cl_nfs_mod);
246 kfree(clp->cl_hostname);
247 kfree(clp->cl_acceptor);
248 kfree(clp);
249 }
250 EXPORT_SYMBOL_GPL(nfs_free_client);
251
252 /*
253 * Release a reference to a shared client record
254 */
nfs_put_client(struct nfs_client * clp)255 void nfs_put_client(struct nfs_client *clp)
256 {
257 struct nfs_net *nn;
258
259 if (!clp)
260 return;
261
262 nn = net_generic(clp->cl_net, nfs_net_id);
263
264 if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
265 list_del(&clp->cl_share_link);
266 nfs_cb_idr_remove_locked(clp);
267 spin_unlock(&nn->nfs_client_lock);
268
269 WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
270
271 clp->rpc_ops->free_client(clp);
272 }
273 }
274 EXPORT_SYMBOL_GPL(nfs_put_client);
275
276 /*
277 * Find an nfs_client on the list that matches the initialisation data
278 * that is supplied.
279 */
nfs_match_client(const struct nfs_client_initdata * data)280 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
281 {
282 struct nfs_client *clp;
283 const struct sockaddr *sap = (struct sockaddr *)data->addr;
284 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
285 int error;
286
287 again:
288 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
289 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
290 /* Don't match clients that failed to initialise properly */
291 if (clp->cl_cons_state < 0)
292 continue;
293
294 /* If a client is still initializing then we need to wait */
295 if (clp->cl_cons_state > NFS_CS_READY) {
296 refcount_inc(&clp->cl_count);
297 spin_unlock(&nn->nfs_client_lock);
298 error = nfs_wait_client_init_complete(clp);
299 nfs_put_client(clp);
300 spin_lock(&nn->nfs_client_lock);
301 if (error < 0)
302 return ERR_PTR(error);
303 goto again;
304 }
305
306 /* Different NFS versions cannot share the same nfs_client */
307 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
308 continue;
309
310 if (clp->cl_proto != data->proto)
311 continue;
312 /* Match nfsv4 minorversion */
313 if (clp->cl_minorversion != data->minorversion)
314 continue;
315
316 /* Match request for a dedicated DS */
317 if (test_bit(NFS_CS_DS, &data->init_flags) !=
318 test_bit(NFS_CS_DS, &clp->cl_flags))
319 continue;
320
321 /* Match the full socket address */
322 if (!rpc_cmp_addr_port(sap, clap))
323 /* Match all xprt_switch full socket addresses */
324 if (IS_ERR(clp->cl_rpcclient) ||
325 !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
326 sap))
327 continue;
328
329 /* Match the xprt security policy */
330 if (clp->cl_xprtsec.policy != data->xprtsec.policy)
331 continue;
332
333 refcount_inc(&clp->cl_count);
334 return clp;
335 }
336 return NULL;
337 }
338
339 /*
340 * Return true if @clp is done initializing, false if still working on it.
341 *
342 * Use nfs_client_init_status to check if it was successful.
343 */
nfs_client_init_is_complete(const struct nfs_client * clp)344 bool nfs_client_init_is_complete(const struct nfs_client *clp)
345 {
346 return clp->cl_cons_state <= NFS_CS_READY;
347 }
348 EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
349
350 /*
351 * Return 0 if @clp was successfully initialized, -errno otherwise.
352 *
353 * This must be called *after* nfs_client_init_is_complete() returns true,
354 * otherwise it will pop WARN_ON_ONCE and return -EINVAL
355 */
nfs_client_init_status(const struct nfs_client * clp)356 int nfs_client_init_status(const struct nfs_client *clp)
357 {
358 /* called without checking nfs_client_init_is_complete */
359 if (clp->cl_cons_state > NFS_CS_READY) {
360 WARN_ON_ONCE(1);
361 return -EINVAL;
362 }
363 return clp->cl_cons_state;
364 }
365 EXPORT_SYMBOL_GPL(nfs_client_init_status);
366
nfs_wait_client_init_complete(const struct nfs_client * clp)367 int nfs_wait_client_init_complete(const struct nfs_client *clp)
368 {
369 return wait_event_killable(nfs_client_active_wq,
370 nfs_client_init_is_complete(clp));
371 }
372 EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
373
374 /*
375 * Found an existing client. Make sure it's ready before returning.
376 */
377 static struct nfs_client *
nfs_found_client(const struct nfs_client_initdata * cl_init,struct nfs_client * clp)378 nfs_found_client(const struct nfs_client_initdata *cl_init,
379 struct nfs_client *clp)
380 {
381 int error;
382
383 error = nfs_wait_client_init_complete(clp);
384 if (error < 0) {
385 nfs_put_client(clp);
386 return ERR_PTR(-ERESTARTSYS);
387 }
388
389 if (clp->cl_cons_state < NFS_CS_READY) {
390 error = clp->cl_cons_state;
391 nfs_put_client(clp);
392 return ERR_PTR(error);
393 }
394
395 smp_rmb();
396 return clp;
397 }
398
399 /*
400 * Look up a client by IP address and protocol version
401 * - creates a new record if one doesn't yet exist
402 */
nfs_get_client(const struct nfs_client_initdata * cl_init)403 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
404 {
405 struct nfs_client *clp, *new = NULL;
406 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
407 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
408
409 if (cl_init->hostname == NULL) {
410 WARN_ON(1);
411 return ERR_PTR(-EINVAL);
412 }
413
414 /* see if the client already exists */
415 do {
416 spin_lock(&nn->nfs_client_lock);
417
418 clp = nfs_match_client(cl_init);
419 if (clp) {
420 spin_unlock(&nn->nfs_client_lock);
421 if (new)
422 new->rpc_ops->free_client(new);
423 if (IS_ERR(clp))
424 return clp;
425 return nfs_found_client(cl_init, clp);
426 }
427 if (new) {
428 list_add_tail(&new->cl_share_link,
429 &nn->nfs_client_list);
430 spin_unlock(&nn->nfs_client_lock);
431 return rpc_ops->init_client(new, cl_init);
432 }
433
434 spin_unlock(&nn->nfs_client_lock);
435
436 new = rpc_ops->alloc_client(cl_init);
437 } while (!IS_ERR(new));
438
439 return new;
440 }
441 EXPORT_SYMBOL_GPL(nfs_get_client);
442
443 /*
444 * Mark a server as ready or failed
445 */
nfs_mark_client_ready(struct nfs_client * clp,int state)446 void nfs_mark_client_ready(struct nfs_client *clp, int state)
447 {
448 smp_wmb();
449 clp->cl_cons_state = state;
450 wake_up_all(&nfs_client_active_wq);
451 }
452 EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
453
454 /*
455 * Initialise the timeout values for a connection
456 */
nfs_init_timeout_values(struct rpc_timeout * to,int proto,int timeo,int retrans)457 void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
458 int timeo, int retrans)
459 {
460 to->to_initval = timeo * HZ / 10;
461 to->to_retries = retrans;
462
463 switch (proto) {
464 case XPRT_TRANSPORT_TCP:
465 case XPRT_TRANSPORT_TCP_TLS:
466 case XPRT_TRANSPORT_RDMA:
467 if (retrans == NFS_UNSPEC_RETRANS)
468 to->to_retries = NFS_DEF_TCP_RETRANS;
469 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
470 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
471 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
472 to->to_initval = NFS_MAX_TCP_TIMEOUT;
473 to->to_increment = to->to_initval;
474 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
475 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
476 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
477 if (to->to_maxval < to->to_initval)
478 to->to_maxval = to->to_initval;
479 to->to_exponential = 0;
480 break;
481 case XPRT_TRANSPORT_UDP:
482 if (retrans == NFS_UNSPEC_RETRANS)
483 to->to_retries = NFS_DEF_UDP_RETRANS;
484 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
485 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
486 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
487 to->to_initval = NFS_MAX_UDP_TIMEOUT;
488 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
489 to->to_exponential = 1;
490 break;
491 default:
492 BUG();
493 }
494 }
495 EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
496
497 /*
498 * Create an RPC client handle
499 */
nfs_create_rpc_client(struct nfs_client * clp,const struct nfs_client_initdata * cl_init,rpc_authflavor_t flavor)500 int nfs_create_rpc_client(struct nfs_client *clp,
501 const struct nfs_client_initdata *cl_init,
502 rpc_authflavor_t flavor)
503 {
504 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
505 struct rpc_clnt *clnt = NULL;
506 struct rpc_create_args args = {
507 .net = clp->cl_net,
508 .protocol = clp->cl_proto,
509 .nconnect = clp->cl_nconnect,
510 .address = (struct sockaddr *)&clp->cl_addr,
511 .addrsize = clp->cl_addrlen,
512 .timeout = cl_init->timeparms,
513 .servername = clp->cl_hostname,
514 .nodename = cl_init->nodename,
515 .program = &nfs_program,
516 .stats = &nn->rpcstats,
517 .version = clp->rpc_ops->version,
518 .authflavor = flavor,
519 .cred = cl_init->cred,
520 .xprtsec = cl_init->xprtsec,
521 .connect_timeout = cl_init->connect_timeout,
522 .reconnect_timeout = cl_init->reconnect_timeout,
523 };
524
525 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
526 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
527 if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
528 args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
529 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
530 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
531 if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
532 args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
533 if (test_bit(NFS_CS_NOPING, &clp->cl_flags))
534 args.flags |= RPC_CLNT_CREATE_NOPING;
535 if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags))
536 args.flags |= RPC_CLNT_CREATE_REUSEPORT;
537
538 if (!IS_ERR(clp->cl_rpcclient))
539 return 0;
540
541 clnt = rpc_create(&args);
542 if (IS_ERR(clnt)) {
543 dprintk("%s: cannot create RPC client. Error = %ld\n",
544 __func__, PTR_ERR(clnt));
545 return PTR_ERR(clnt);
546 }
547
548 clnt->cl_principal = clp->cl_principal;
549 clp->cl_rpcclient = clnt;
550 clnt->cl_max_connect = clp->cl_max_connect;
551 return 0;
552 }
553 EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
554
555 /*
556 * Version 2 or 3 client destruction
557 */
nfs_destroy_server(struct nfs_server * server)558 static void nfs_destroy_server(struct nfs_server *server)
559 {
560 if (server->nlm_host)
561 nlmclnt_done(server->nlm_host);
562 }
563
564 /*
565 * Version 2 or 3 lockd setup
566 */
nfs_start_lockd(struct nfs_server * server)567 static 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 .cred = server->cred,
581 };
582
583 if (nlm_init.nfs_version > 3)
584 return 0;
585 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
586 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
587 return 0;
588
589 switch (clp->cl_proto) {
590 default:
591 nlm_init.protocol = IPPROTO_TCP;
592 break;
593 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
594 case XPRT_TRANSPORT_UDP:
595 nlm_init.protocol = IPPROTO_UDP;
596 #endif
597 }
598
599 host = nlmclnt_init(&nlm_init);
600 if (IS_ERR(host))
601 return PTR_ERR(host);
602
603 server->nlm_host = host;
604 server->destroy = nfs_destroy_server;
605 nfs_sysfs_link_rpc_client(server, nlmclnt_rpc_clnt(host), NULL);
606 return 0;
607 }
608
609 /*
610 * Create a general RPC client
611 */
nfs_init_server_rpcclient(struct nfs_server * server,const struct rpc_timeout * timeo,rpc_authflavor_t pseudoflavour)612 int nfs_init_server_rpcclient(struct nfs_server *server,
613 const struct rpc_timeout *timeo,
614 rpc_authflavor_t pseudoflavour)
615 {
616 struct nfs_client *clp = server->nfs_client;
617
618 server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
619 pseudoflavour);
620 if (IS_ERR(server->client)) {
621 dprintk("%s: couldn't create rpc_client!\n", __func__);
622 return PTR_ERR(server->client);
623 }
624
625 memcpy(&server->client->cl_timeout_default,
626 timeo,
627 sizeof(server->client->cl_timeout_default));
628 server->client->cl_timeout = &server->client->cl_timeout_default;
629 server->client->cl_softrtry = 0;
630 if (server->flags & NFS_MOUNT_SOFTERR)
631 server->client->cl_softerr = 1;
632 if (server->flags & NFS_MOUNT_SOFT)
633 server->client->cl_softrtry = 1;
634
635 nfs_sysfs_link_rpc_client(server, server->client, NULL);
636 return 0;
637 }
638 EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
639
640 /**
641 * nfs_init_client - Initialise an NFS2 or NFS3 client
642 *
643 * @clp: nfs_client to initialise
644 * @cl_init: Initialisation parameters
645 *
646 * Returns pointer to an NFS client, or an ERR_PTR value.
647 */
nfs_init_client(struct nfs_client * clp,const struct nfs_client_initdata * cl_init)648 struct nfs_client *nfs_init_client(struct nfs_client *clp,
649 const struct nfs_client_initdata *cl_init)
650 {
651 int error;
652
653 /* the client is already initialised */
654 if (clp->cl_cons_state == NFS_CS_READY)
655 return clp;
656
657 /*
658 * Create a client RPC handle for doing FSSTAT with UNIX auth only
659 * - RFC 2623, sec 2.3.2
660 */
661 error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
662 nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error);
663 if (error < 0) {
664 nfs_put_client(clp);
665 clp = ERR_PTR(error);
666 }
667 return clp;
668 }
669 EXPORT_SYMBOL_GPL(nfs_init_client);
670
671 /*
672 * Create a version 2 or 3 client
673 */
nfs_init_server(struct nfs_server * server,const struct fs_context * fc)674 static int nfs_init_server(struct nfs_server *server,
675 const struct fs_context *fc)
676 {
677 const struct nfs_fs_context *ctx = nfs_fc2context(fc);
678 struct rpc_timeout timeparms;
679 struct nfs_client_initdata cl_init = {
680 .hostname = ctx->nfs_server.hostname,
681 .addr = &ctx->nfs_server._address,
682 .addrlen = ctx->nfs_server.addrlen,
683 .nfs_mod = ctx->nfs_mod,
684 .proto = ctx->nfs_server.protocol,
685 .net = fc->net_ns,
686 .timeparms = &timeparms,
687 .cred = server->cred,
688 .nconnect = ctx->nfs_server.nconnect,
689 .init_flags = (1UL << NFS_CS_REUSEPORT),
690 .xprtsec = ctx->xprtsec,
691 };
692 struct nfs_client *clp;
693 int error;
694
695 nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
696 ctx->timeo, ctx->retrans);
697 if (ctx->flags & NFS_MOUNT_NORESVPORT)
698 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
699
700 /* Allocate or find a client reference we can use */
701 clp = nfs_get_client(&cl_init);
702 if (IS_ERR(clp))
703 return PTR_ERR(clp);
704
705 server->nfs_client = clp;
706 nfs_sysfs_add_server(server);
707 nfs_sysfs_link_rpc_client(server, clp->cl_rpcclient, "_state");
708
709 /* Initialise the client representation from the mount data */
710 server->flags = ctx->flags;
711 server->options = ctx->options;
712 server->caps |= NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS;
713
714 switch (clp->rpc_ops->version) {
715 case 2:
716 server->fattr_valid = NFS_ATTR_FATTR_V2;
717 break;
718 case 3:
719 server->fattr_valid = NFS_ATTR_FATTR_V3;
720 break;
721 default:
722 server->fattr_valid = NFS_ATTR_FATTR_V4;
723 }
724
725 if (ctx->rsize)
726 server->rsize = nfs_io_size(ctx->rsize, clp->cl_proto);
727 if (ctx->wsize)
728 server->wsize = nfs_io_size(ctx->wsize, clp->cl_proto);
729
730 server->acregmin = ctx->acregmin * HZ;
731 server->acregmax = ctx->acregmax * HZ;
732 server->acdirmin = ctx->acdirmin * HZ;
733 server->acdirmax = ctx->acdirmax * HZ;
734
735 /* Start lockd here, before we might error out */
736 error = nfs_start_lockd(server);
737 if (error < 0)
738 goto error;
739
740 server->port = ctx->nfs_server.port;
741 server->auth_info = ctx->auth_info;
742
743 error = nfs_init_server_rpcclient(server, &timeparms,
744 ctx->selected_flavor);
745 if (error < 0)
746 goto error;
747
748 /* Preserve the values of mount_server-related mount options */
749 if (ctx->mount_server.addrlen) {
750 memcpy(&server->mountd_address, &ctx->mount_server.address,
751 ctx->mount_server.addrlen);
752 server->mountd_addrlen = ctx->mount_server.addrlen;
753 }
754 server->mountd_version = ctx->mount_server.version;
755 server->mountd_port = ctx->mount_server.port;
756 server->mountd_protocol = ctx->mount_server.protocol;
757
758 server->namelen = ctx->namlen;
759 return 0;
760
761 error:
762 server->nfs_client = NULL;
763 nfs_put_client(clp);
764 return error;
765 }
766
767 /*
768 * Load up the server record from information gained in an fsinfo record
769 */
nfs_server_set_fsinfo(struct nfs_server * server,struct nfs_fsinfo * fsinfo)770 static void nfs_server_set_fsinfo(struct nfs_server *server,
771 struct nfs_fsinfo *fsinfo)
772 {
773 struct nfs_client *clp = server->nfs_client;
774 unsigned long max_rpc_payload, raw_max_rpc_payload;
775
776 /* Work out a lot of parameters */
777 if (server->rsize == 0)
778 server->rsize = nfs_io_size(fsinfo->rtpref, clp->cl_proto);
779 if (server->wsize == 0)
780 server->wsize = nfs_io_size(fsinfo->wtpref, clp->cl_proto);
781
782 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
783 server->rsize = nfs_io_size(fsinfo->rtmax, clp->cl_proto);
784 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
785 server->wsize = nfs_io_size(fsinfo->wtmax, clp->cl_proto);
786
787 raw_max_rpc_payload = rpc_max_payload(server->client);
788 max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL);
789
790 if (server->rsize > max_rpc_payload)
791 server->rsize = max_rpc_payload;
792 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
793 server->rsize = NFS_MAX_FILE_IO_SIZE;
794 server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
795
796 if (server->wsize > max_rpc_payload)
797 server->wsize = max_rpc_payload;
798 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
799 server->wsize = NFS_MAX_FILE_IO_SIZE;
800 server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
801
802 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
803
804 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
805 if (server->dtsize > NFS_MAX_FILE_IO_SIZE)
806 server->dtsize = NFS_MAX_FILE_IO_SIZE;
807 if (server->dtsize > server->rsize)
808 server->dtsize = server->rsize;
809
810 if (server->flags & NFS_MOUNT_NOAC) {
811 server->acregmin = server->acregmax = 0;
812 server->acdirmin = server->acdirmax = 0;
813 }
814
815 server->maxfilesize = fsinfo->maxfilesize;
816
817 server->time_delta = fsinfo->time_delta;
818 server->change_attr_type = fsinfo->change_attr_type;
819
820 server->clone_blksize = fsinfo->clone_blksize;
821 /* We're airborne Set socket buffersize */
822 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
823
824 #ifdef CONFIG_NFS_V4_2
825 /*
826 * Defaults until limited by the session parameters.
827 */
828 server->gxasize = min_t(unsigned int, raw_max_rpc_payload,
829 XATTR_SIZE_MAX);
830 server->sxasize = min_t(unsigned int, raw_max_rpc_payload,
831 XATTR_SIZE_MAX);
832 server->lxasize = min_t(unsigned int, raw_max_rpc_payload,
833 nfs42_listxattr_xdrsize(XATTR_LIST_MAX));
834
835 if (fsinfo->xattr_support)
836 server->caps |= NFS_CAP_XATTR;
837 #endif
838 }
839
840 /*
841 * Probe filesystem information, including the FSID on v2/v3
842 */
nfs_probe_fsinfo(struct nfs_server * server,struct nfs_fh * mntfh,struct nfs_fattr * fattr)843 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
844 {
845 struct nfs_fsinfo fsinfo;
846 struct nfs_client *clp = server->nfs_client;
847 int error;
848
849 if (clp->rpc_ops->set_capabilities != NULL) {
850 error = clp->rpc_ops->set_capabilities(server, mntfh);
851 if (error < 0)
852 return error;
853 }
854
855 fsinfo.fattr = fattr;
856 fsinfo.nlayouttypes = 0;
857 memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
858 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
859 if (error < 0)
860 return error;
861
862 nfs_server_set_fsinfo(server, &fsinfo);
863
864 /* Get some general file system info */
865 if (server->namelen == 0) {
866 struct nfs_pathconf pathinfo;
867
868 pathinfo.fattr = fattr;
869 nfs_fattr_init(fattr);
870
871 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
872 server->namelen = pathinfo.max_namelen;
873 }
874
875 if (clp->rpc_ops->discover_trunking != NULL &&
876 (server->caps & NFS_CAP_FS_LOCATIONS &&
877 (server->flags & NFS_MOUNT_TRUNK_DISCOVERY))) {
878 error = clp->rpc_ops->discover_trunking(server, mntfh);
879 if (error < 0)
880 return error;
881 }
882
883 return 0;
884 }
885
886 /*
887 * Grab the destination's particulars, including lease expiry time.
888 *
889 * Returns zero if probe succeeded and retrieved FSID matches the FSID
890 * we have cached.
891 */
nfs_probe_server(struct nfs_server * server,struct nfs_fh * mntfh)892 int nfs_probe_server(struct nfs_server *server, struct nfs_fh *mntfh)
893 {
894 struct nfs_fattr *fattr;
895 int error;
896
897 fattr = nfs_alloc_fattr();
898 if (fattr == NULL)
899 return -ENOMEM;
900
901 /* Sanity: the probe won't work if the destination server
902 * does not recognize the migrated FH. */
903 error = nfs_probe_fsinfo(server, mntfh, fattr);
904
905 nfs_free_fattr(fattr);
906 return error;
907 }
908 EXPORT_SYMBOL_GPL(nfs_probe_server);
909
910 /*
911 * Copy useful information when duplicating a server record
912 */
nfs_server_copy_userdata(struct nfs_server * target,struct nfs_server * source)913 void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
914 {
915 target->flags = source->flags;
916 target->rsize = source->rsize;
917 target->wsize = source->wsize;
918 target->acregmin = source->acregmin;
919 target->acregmax = source->acregmax;
920 target->acdirmin = source->acdirmin;
921 target->acdirmax = source->acdirmax;
922 target->caps = source->caps;
923 target->options = source->options;
924 target->auth_info = source->auth_info;
925 target->port = source->port;
926 }
927 EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
928
nfs_server_insert_lists(struct nfs_server * server)929 void nfs_server_insert_lists(struct nfs_server *server)
930 {
931 struct nfs_client *clp = server->nfs_client;
932 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
933
934 spin_lock(&nn->nfs_client_lock);
935 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
936 list_add_tail(&server->master_link, &nn->nfs_volume_list);
937 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
938 spin_unlock(&nn->nfs_client_lock);
939
940 }
941 EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
942
nfs_server_remove_lists(struct nfs_server * server)943 void nfs_server_remove_lists(struct nfs_server *server)
944 {
945 struct nfs_client *clp = server->nfs_client;
946 struct nfs_net *nn;
947
948 if (clp == NULL)
949 return;
950 nn = net_generic(clp->cl_net, nfs_net_id);
951 spin_lock(&nn->nfs_client_lock);
952 list_del_rcu(&server->client_link);
953 if (list_empty(&clp->cl_superblocks))
954 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
955 list_del(&server->master_link);
956 spin_unlock(&nn->nfs_client_lock);
957
958 synchronize_rcu();
959 }
960 EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
961
962 static DEFINE_IDA(s_sysfs_ids);
963
964 /*
965 * Allocate and initialise a server record
966 */
nfs_alloc_server(void)967 struct nfs_server *nfs_alloc_server(void)
968 {
969 struct nfs_server *server;
970
971 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
972 if (!server)
973 return NULL;
974
975 server->s_sysfs_id = ida_alloc(&s_sysfs_ids, GFP_KERNEL);
976 if (server->s_sysfs_id < 0) {
977 kfree(server);
978 return NULL;
979 }
980
981 server->client = server->client_acl = ERR_PTR(-EINVAL);
982
983 /* Zero out the NFS state stuff */
984 INIT_LIST_HEAD(&server->client_link);
985 INIT_LIST_HEAD(&server->master_link);
986 INIT_LIST_HEAD(&server->delegations);
987 INIT_LIST_HEAD(&server->layouts);
988 INIT_LIST_HEAD(&server->state_owners_lru);
989 INIT_LIST_HEAD(&server->ss_copies);
990 INIT_LIST_HEAD(&server->ss_src_copies);
991
992 atomic_set(&server->active, 0);
993
994 server->io_stats = nfs_alloc_iostats();
995 if (!server->io_stats) {
996 kfree(server);
997 return NULL;
998 }
999
1000 server->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED;
1001
1002 ida_init(&server->openowner_id);
1003 ida_init(&server->lockowner_id);
1004 pnfs_init_server(server);
1005 rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
1006
1007 return server;
1008 }
1009 EXPORT_SYMBOL_GPL(nfs_alloc_server);
1010
1011 /*
1012 * Free up a server record
1013 */
nfs_free_server(struct nfs_server * server)1014 void nfs_free_server(struct nfs_server *server)
1015 {
1016 nfs_server_remove_lists(server);
1017
1018 if (server->destroy != NULL)
1019 server->destroy(server);
1020
1021 if (!IS_ERR(server->client_acl))
1022 rpc_shutdown_client(server->client_acl);
1023 if (!IS_ERR(server->client))
1024 rpc_shutdown_client(server->client);
1025
1026 nfs_put_client(server->nfs_client);
1027
1028 if (server->kobj.state_initialized) {
1029 nfs_sysfs_remove_server(server);
1030 kobject_put(&server->kobj);
1031 }
1032 ida_free(&s_sysfs_ids, server->s_sysfs_id);
1033
1034 ida_destroy(&server->lockowner_id);
1035 ida_destroy(&server->openowner_id);
1036 nfs_free_iostats(server->io_stats);
1037 put_cred(server->cred);
1038 kfree(server);
1039 nfs_release_automount_timer();
1040 }
1041 EXPORT_SYMBOL_GPL(nfs_free_server);
1042
1043 /*
1044 * Create a version 2 or 3 volume record
1045 * - keyed on server and FSID
1046 */
nfs_create_server(struct fs_context * fc)1047 struct nfs_server *nfs_create_server(struct fs_context *fc)
1048 {
1049 struct nfs_fs_context *ctx = nfs_fc2context(fc);
1050 struct nfs_server *server;
1051 struct nfs_fattr *fattr;
1052 int error;
1053
1054 server = nfs_alloc_server();
1055 if (!server)
1056 return ERR_PTR(-ENOMEM);
1057
1058 server->cred = get_cred(fc->cred);
1059
1060 error = -ENOMEM;
1061 fattr = nfs_alloc_fattr();
1062 if (fattr == NULL)
1063 goto error;
1064
1065 /* Get a client representation */
1066 error = nfs_init_server(server, fc);
1067 if (error < 0)
1068 goto error;
1069
1070 /* Probe the root fh to retrieve its FSID */
1071 error = nfs_probe_fsinfo(server, ctx->mntfh, fattr);
1072 if (error < 0)
1073 goto error;
1074 if (server->nfs_client->rpc_ops->version == 3) {
1075 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1076 server->namelen = NFS3_MAXNAMLEN;
1077 if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS))
1078 server->caps |= NFS_CAP_READDIRPLUS;
1079 } else {
1080 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1081 server->namelen = NFS2_MAXNAMLEN;
1082 }
1083
1084 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1085 error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh,
1086 fattr, NULL);
1087 if (error < 0) {
1088 dprintk("nfs_create_server: getattr error = %d\n", -error);
1089 goto error;
1090 }
1091 }
1092 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1093
1094 dprintk("Server FSID: %llx:%llx\n",
1095 (unsigned long long) server->fsid.major,
1096 (unsigned long long) server->fsid.minor);
1097
1098 nfs_server_insert_lists(server);
1099 server->mount_time = jiffies;
1100 nfs_free_fattr(fattr);
1101 return server;
1102
1103 error:
1104 nfs_free_fattr(fattr);
1105 nfs_free_server(server);
1106 return ERR_PTR(error);
1107 }
1108 EXPORT_SYMBOL_GPL(nfs_create_server);
1109
1110 /*
1111 * Clone an NFS2, NFS3 or NFS4 server record
1112 */
nfs_clone_server(struct nfs_server * source,struct nfs_fh * fh,struct nfs_fattr * fattr,rpc_authflavor_t flavor)1113 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1114 struct nfs_fh *fh,
1115 struct nfs_fattr *fattr,
1116 rpc_authflavor_t flavor)
1117 {
1118 struct nfs_server *server;
1119 int error;
1120
1121 server = nfs_alloc_server();
1122 if (!server)
1123 return ERR_PTR(-ENOMEM);
1124
1125 server->cred = get_cred(source->cred);
1126
1127 /* Copy data from the source */
1128 server->nfs_client = source->nfs_client;
1129 server->destroy = source->destroy;
1130 refcount_inc(&server->nfs_client->cl_count);
1131 nfs_server_copy_userdata(server, source);
1132
1133 server->fsid = fattr->fsid;
1134
1135 nfs_sysfs_add_server(server);
1136
1137 nfs_sysfs_link_rpc_client(server,
1138 server->nfs_client->cl_rpcclient, "_state");
1139
1140 error = nfs_init_server_rpcclient(server,
1141 source->client->cl_timeout,
1142 flavor);
1143 if (error < 0)
1144 goto out_free_server;
1145
1146 /* probe the filesystem info for this server filesystem */
1147 error = nfs_probe_server(server, fh);
1148 if (error < 0)
1149 goto out_free_server;
1150
1151 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1152 server->namelen = NFS4_MAXNAMLEN;
1153
1154 error = nfs_start_lockd(server);
1155 if (error < 0)
1156 goto out_free_server;
1157
1158 nfs_server_insert_lists(server);
1159 server->mount_time = jiffies;
1160
1161 return server;
1162
1163 out_free_server:
1164 nfs_free_server(server);
1165 return ERR_PTR(error);
1166 }
1167 EXPORT_SYMBOL_GPL(nfs_clone_server);
1168
nfs_clients_init(struct net * net)1169 void nfs_clients_init(struct net *net)
1170 {
1171 struct nfs_net *nn = net_generic(net, nfs_net_id);
1172
1173 INIT_LIST_HEAD(&nn->nfs_client_list);
1174 INIT_LIST_HEAD(&nn->nfs_volume_list);
1175 #if IS_ENABLED(CONFIG_NFS_V4)
1176 idr_init(&nn->cb_ident_idr);
1177 #endif
1178 spin_lock_init(&nn->nfs_client_lock);
1179 nn->boot_time = ktime_get_real();
1180 memset(&nn->rpcstats, 0, sizeof(nn->rpcstats));
1181 nn->rpcstats.program = &nfs_program;
1182
1183 nfs_netns_sysfs_setup(nn, net);
1184 }
1185
nfs_clients_exit(struct net * net)1186 void nfs_clients_exit(struct net *net)
1187 {
1188 struct nfs_net *nn = net_generic(net, nfs_net_id);
1189
1190 nfs_netns_sysfs_destroy(nn);
1191 nfs_cleanup_cb_ident_idr(net);
1192 WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
1193 WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
1194 }
1195
1196 #ifdef CONFIG_PROC_FS
1197 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1198 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1199 static void nfs_server_list_stop(struct seq_file *p, void *v);
1200 static int nfs_server_list_show(struct seq_file *m, void *v);
1201
1202 static const struct seq_operations nfs_server_list_ops = {
1203 .start = nfs_server_list_start,
1204 .next = nfs_server_list_next,
1205 .stop = nfs_server_list_stop,
1206 .show = nfs_server_list_show,
1207 };
1208
1209 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1210 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1211 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1212 static int nfs_volume_list_show(struct seq_file *m, void *v);
1213
1214 static const struct seq_operations nfs_volume_list_ops = {
1215 .start = nfs_volume_list_start,
1216 .next = nfs_volume_list_next,
1217 .stop = nfs_volume_list_stop,
1218 .show = nfs_volume_list_show,
1219 };
1220
1221 /*
1222 * set up the iterator to start reading from the server list and return the first item
1223 */
nfs_server_list_start(struct seq_file * m,loff_t * _pos)1224 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1225 __acquires(&nn->nfs_client_lock)
1226 {
1227 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1228
1229 /* lock the list against modification */
1230 spin_lock(&nn->nfs_client_lock);
1231 return seq_list_start_head(&nn->nfs_client_list, *_pos);
1232 }
1233
1234 /*
1235 * move to next server
1236 */
nfs_server_list_next(struct seq_file * p,void * v,loff_t * pos)1237 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1238 {
1239 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1240
1241 return seq_list_next(v, &nn->nfs_client_list, pos);
1242 }
1243
1244 /*
1245 * clean up after reading from the transports list
1246 */
nfs_server_list_stop(struct seq_file * p,void * v)1247 static void nfs_server_list_stop(struct seq_file *p, void *v)
1248 __releases(&nn->nfs_client_lock)
1249 {
1250 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1251
1252 spin_unlock(&nn->nfs_client_lock);
1253 }
1254
1255 /*
1256 * display a header line followed by a load of call lines
1257 */
nfs_server_list_show(struct seq_file * m,void * v)1258 static int nfs_server_list_show(struct seq_file *m, void *v)
1259 {
1260 struct nfs_client *clp;
1261 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1262
1263 /* display header on line 1 */
1264 if (v == &nn->nfs_client_list) {
1265 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1266 return 0;
1267 }
1268
1269 /* display one transport per line on subsequent lines */
1270 clp = list_entry(v, struct nfs_client, cl_share_link);
1271
1272 /* Check if the client is initialized */
1273 if (clp->cl_cons_state != NFS_CS_READY)
1274 return 0;
1275
1276 rcu_read_lock();
1277 seq_printf(m, "v%u %s %s %3d %s\n",
1278 clp->rpc_ops->version,
1279 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1280 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1281 refcount_read(&clp->cl_count),
1282 clp->cl_hostname);
1283 rcu_read_unlock();
1284
1285 return 0;
1286 }
1287
1288 /*
1289 * set up the iterator to start reading from the volume list and return the first item
1290 */
nfs_volume_list_start(struct seq_file * m,loff_t * _pos)1291 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1292 __acquires(&nn->nfs_client_lock)
1293 {
1294 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1295
1296 /* lock the list against modification */
1297 spin_lock(&nn->nfs_client_lock);
1298 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1299 }
1300
1301 /*
1302 * move to next volume
1303 */
nfs_volume_list_next(struct seq_file * p,void * v,loff_t * pos)1304 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1305 {
1306 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1307
1308 return seq_list_next(v, &nn->nfs_volume_list, pos);
1309 }
1310
1311 /*
1312 * clean up after reading from the transports list
1313 */
nfs_volume_list_stop(struct seq_file * p,void * v)1314 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1315 __releases(&nn->nfs_client_lock)
1316 {
1317 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1318
1319 spin_unlock(&nn->nfs_client_lock);
1320 }
1321
1322 /*
1323 * display a header line followed by a load of call lines
1324 */
nfs_volume_list_show(struct seq_file * m,void * v)1325 static int nfs_volume_list_show(struct seq_file *m, void *v)
1326 {
1327 struct nfs_server *server;
1328 struct nfs_client *clp;
1329 char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1330 char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0'
1331 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1332
1333 /* display header on line 1 */
1334 if (v == &nn->nfs_volume_list) {
1335 seq_puts(m, "NV SERVER PORT DEV FSID"
1336 " FSC\n");
1337 return 0;
1338 }
1339 /* display one transport per line on subsequent lines */
1340 server = list_entry(v, struct nfs_server, master_link);
1341 clp = server->nfs_client;
1342
1343 snprintf(dev, sizeof(dev), "%u:%u",
1344 MAJOR(server->s_dev), MINOR(server->s_dev));
1345
1346 snprintf(fsid, sizeof(fsid), "%llx:%llx",
1347 (unsigned long long) server->fsid.major,
1348 (unsigned long long) server->fsid.minor);
1349
1350 rcu_read_lock();
1351 seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
1352 clp->rpc_ops->version,
1353 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1354 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1355 dev,
1356 fsid,
1357 nfs_server_fscache_state(server));
1358 rcu_read_unlock();
1359
1360 return 0;
1361 }
1362
nfs_fs_proc_net_init(struct net * net)1363 int nfs_fs_proc_net_init(struct net *net)
1364 {
1365 struct nfs_net *nn = net_generic(net, nfs_net_id);
1366 struct proc_dir_entry *p;
1367
1368 nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
1369 if (!nn->proc_nfsfs)
1370 goto error_0;
1371
1372 /* a file of servers with which we're dealing */
1373 p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1374 &nfs_server_list_ops, sizeof(struct seq_net_private));
1375 if (!p)
1376 goto error_1;
1377
1378 /* a file of volumes that we have mounted */
1379 p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1380 &nfs_volume_list_ops, sizeof(struct seq_net_private));
1381 if (!p)
1382 goto error_1;
1383 return 0;
1384
1385 error_1:
1386 remove_proc_subtree("nfsfs", net->proc_net);
1387 error_0:
1388 return -ENOMEM;
1389 }
1390
nfs_fs_proc_net_exit(struct net * net)1391 void nfs_fs_proc_net_exit(struct net *net)
1392 {
1393 remove_proc_subtree("nfsfs", net->proc_net);
1394 }
1395
1396 /*
1397 * initialise the /proc/fs/nfsfs/ directory
1398 */
nfs_fs_proc_init(void)1399 int __init nfs_fs_proc_init(void)
1400 {
1401 if (!proc_mkdir("fs/nfsfs", NULL))
1402 goto error_0;
1403
1404 /* a file of servers with which we're dealing */
1405 if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
1406 goto error_1;
1407
1408 /* a file of volumes that we have mounted */
1409 if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
1410 goto error_1;
1411
1412 return 0;
1413 error_1:
1414 remove_proc_subtree("fs/nfsfs", NULL);
1415 error_0:
1416 return -ENOMEM;
1417 }
1418
1419 /*
1420 * clean up the /proc/fs/nfsfs/ directory
1421 */
nfs_fs_proc_exit(void)1422 void nfs_fs_proc_exit(void)
1423 {
1424 remove_proc_subtree("fs/nfsfs", NULL);
1425 ida_destroy(&s_sysfs_ids);
1426 }
1427
1428 #endif /* CONFIG_PROC_FS */
1429