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