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