drm/panfrost: perfcnt: Reserve/use the AS attached to the perfcnt MMU context
[linux/fpc-iii.git] / fs / nfs / client.c
blob989c30c98511d9d8404803803e4d1a9b7a462a9a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* client.c: NFS client sharing and management code
4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
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>
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"
54 #define NFSDBG_FACILITY NFSDBG_CLIENT
56 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
57 static DEFINE_SPINLOCK(nfs_version_lock);
58 static DEFINE_MUTEX(nfs_version_mutex);
59 static LIST_HEAD(nfs_versions);
62 * RPC cruft for NFS
64 static const struct rpc_version *nfs_version[5] = {
65 [2] = NULL,
66 [3] = NULL,
67 [4] = NULL,
70 const struct rpc_program nfs_program = {
71 .name = "nfs",
72 .number = NFS_PROGRAM,
73 .nrvers = ARRAY_SIZE(nfs_version),
74 .version = nfs_version,
75 .stats = &nfs_rpcstat,
76 .pipe_dir_name = NFS_PIPE_DIRNAME,
79 struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
83 static struct nfs_subversion *find_nfs_version(unsigned int version)
85 struct nfs_subversion *nfs;
86 spin_lock(&nfs_version_lock);
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;
95 spin_unlock(&nfs_version_lock);
96 return ERR_PTR(-EPROTONOSUPPORT);
99 struct nfs_subversion *get_nfs_version(unsigned int version)
101 struct nfs_subversion *nfs = find_nfs_version(version);
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);
110 if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
111 return ERR_PTR(-EAGAIN);
112 return nfs;
115 void put_nfs_version(struct nfs_subversion *nfs)
117 module_put(nfs->owner);
120 void register_nfs_version(struct nfs_subversion *nfs)
122 spin_lock(&nfs_version_lock);
124 list_add(&nfs->list, &nfs_versions);
125 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
127 spin_unlock(&nfs_version_lock);
129 EXPORT_SYMBOL_GPL(register_nfs_version);
131 void unregister_nfs_version(struct nfs_subversion *nfs)
133 spin_lock(&nfs_version_lock);
135 nfs_version[nfs->rpc_ops->version] = NULL;
136 list_del(&nfs->list);
138 spin_unlock(&nfs_version_lock);
140 EXPORT_SYMBOL_GPL(unregister_nfs_version);
143 * Allocate a shared client record
145 * Since these are allocated/deallocated very rarely, we don't
146 * bother putting them in a slab cache...
148 struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
150 struct nfs_client *clp;
151 int err = -ENOMEM;
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
154 goto error_0;
156 clp->cl_nfs_mod = cl_init->nfs_mod;
157 if (!try_module_get(clp->cl_nfs_mod->owner))
158 goto error_dealloc;
160 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
162 refcount_set(&clp->cl_count, 1);
163 clp->cl_cons_state = NFS_CS_INITING;
165 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
166 clp->cl_addrlen = cl_init->addrlen;
168 if (cl_init->hostname) {
169 err = -ENOMEM;
170 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
171 if (!clp->cl_hostname)
172 goto error_cleanup;
175 INIT_LIST_HEAD(&clp->cl_superblocks);
176 clp->cl_rpcclient = ERR_PTR(-EINVAL);
178 clp->cl_proto = cl_init->proto;
179 clp->cl_nconnect = cl_init->nconnect;
180 clp->cl_net = get_net(cl_init->net);
182 clp->cl_principal = "*";
183 nfs_fscache_get_client_cookie(clp);
185 return clp;
187 error_cleanup:
188 put_nfs_version(clp->cl_nfs_mod);
189 error_dealloc:
190 kfree(clp);
191 error_0:
192 return ERR_PTR(err);
194 EXPORT_SYMBOL_GPL(nfs_alloc_client);
196 #if IS_ENABLED(CONFIG_NFS_V4)
197 static void nfs_cleanup_cb_ident_idr(struct net *net)
199 struct nfs_net *nn = net_generic(net, nfs_net_id);
201 idr_destroy(&nn->cb_ident_idr);
204 /* nfs_client_lock held */
205 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
207 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
209 if (clp->cl_cb_ident)
210 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
213 static void pnfs_init_server(struct nfs_server *server)
215 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
218 #else
219 static void nfs_cleanup_cb_ident_idr(struct net *net)
223 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
227 static void pnfs_init_server(struct nfs_server *server)
231 #endif /* CONFIG_NFS_V4 */
234 * Destroy a shared client record
236 void nfs_free_client(struct nfs_client *clp)
238 nfs_fscache_release_client_cookie(clp);
240 /* -EIO all pending I/O */
241 if (!IS_ERR(clp->cl_rpcclient))
242 rpc_shutdown_client(clp->cl_rpcclient);
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);
250 EXPORT_SYMBOL_GPL(nfs_free_client);
253 * Release a reference to a shared client record
255 void nfs_put_client(struct nfs_client *clp)
257 struct nfs_net *nn;
259 if (!clp)
260 return;
262 nn = net_generic(clp->cl_net, nfs_net_id);
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);
269 WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
271 clp->rpc_ops->free_client(clp);
274 EXPORT_SYMBOL_GPL(nfs_put_client);
277 * Find an nfs_client on the list that matches the initialisation data
278 * that is supplied.
280 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
282 struct nfs_client *clp;
283 const struct sockaddr *sap = data->addr;
284 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
285 int error;
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;
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;
306 /* Different NFS versions cannot share the same nfs_client */
307 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
308 continue;
310 if (clp->cl_proto != data->proto)
311 continue;
312 /* Match nfsv4 minorversion */
313 if (clp->cl_minorversion != data->minorversion)
314 continue;
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;
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;
329 refcount_inc(&clp->cl_count);
330 return clp;
332 return NULL;
336 * Return true if @clp is done initializing, false if still working on it.
338 * Use nfs_client_init_status to check if it was successful.
340 bool nfs_client_init_is_complete(const struct nfs_client *clp)
342 return clp->cl_cons_state <= NFS_CS_READY;
344 EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
347 * Return 0 if @clp was successfully initialized, -errno otherwise.
349 * This must be called *after* nfs_client_init_is_complete() returns true,
350 * otherwise it will pop WARN_ON_ONCE and return -EINVAL
352 int nfs_client_init_status(const struct nfs_client *clp)
354 /* called without checking nfs_client_init_is_complete */
355 if (clp->cl_cons_state > NFS_CS_READY) {
356 WARN_ON_ONCE(1);
357 return -EINVAL;
359 return clp->cl_cons_state;
361 EXPORT_SYMBOL_GPL(nfs_client_init_status);
363 int nfs_wait_client_init_complete(const struct nfs_client *clp)
365 return wait_event_killable(nfs_client_active_wq,
366 nfs_client_init_is_complete(clp));
368 EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
371 * Found an existing client. Make sure it's ready before returning.
373 static struct nfs_client *
374 nfs_found_client(const struct nfs_client_initdata *cl_init,
375 struct nfs_client *clp)
377 int error;
379 error = nfs_wait_client_init_complete(clp);
380 if (error < 0) {
381 nfs_put_client(clp);
382 return ERR_PTR(-ERESTARTSYS);
385 if (clp->cl_cons_state < NFS_CS_READY) {
386 error = clp->cl_cons_state;
387 nfs_put_client(clp);
388 return ERR_PTR(error);
391 smp_rmb();
392 return clp;
396 * Look up a client by IP address and protocol version
397 * - creates a new record if one doesn't yet exist
399 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
401 struct nfs_client *clp, *new = NULL;
402 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
403 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
405 if (cl_init->hostname == NULL) {
406 WARN_ON(1);
407 return NULL;
410 /* see if the client already exists */
411 do {
412 spin_lock(&nn->nfs_client_lock);
414 clp = nfs_match_client(cl_init);
415 if (clp) {
416 spin_unlock(&nn->nfs_client_lock);
417 if (new)
418 new->rpc_ops->free_client(new);
419 if (IS_ERR(clp))
420 return clp;
421 return nfs_found_client(cl_init, clp);
423 if (new) {
424 list_add_tail(&new->cl_share_link,
425 &nn->nfs_client_list);
426 spin_unlock(&nn->nfs_client_lock);
427 new->cl_flags = cl_init->init_flags;
428 return rpc_ops->init_client(new, cl_init);
431 spin_unlock(&nn->nfs_client_lock);
433 new = rpc_ops->alloc_client(cl_init);
434 } while (!IS_ERR(new));
436 return new;
438 EXPORT_SYMBOL_GPL(nfs_get_client);
441 * Mark a server as ready or failed
443 void nfs_mark_client_ready(struct nfs_client *clp, int state)
445 smp_wmb();
446 clp->cl_cons_state = state;
447 wake_up_all(&nfs_client_active_wq);
449 EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
452 * Initialise the timeout values for a connection
454 void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
455 int timeo, int retrans)
457 to->to_initval = timeo * HZ / 10;
458 to->to_retries = retrans;
460 switch (proto) {
461 case XPRT_TRANSPORT_TCP:
462 case XPRT_TRANSPORT_RDMA:
463 if (retrans == NFS_UNSPEC_RETRANS)
464 to->to_retries = NFS_DEF_TCP_RETRANS;
465 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
466 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
467 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
468 to->to_initval = NFS_MAX_TCP_TIMEOUT;
469 to->to_increment = to->to_initval;
470 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
471 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
472 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
473 if (to->to_maxval < to->to_initval)
474 to->to_maxval = to->to_initval;
475 to->to_exponential = 0;
476 break;
477 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
478 case XPRT_TRANSPORT_UDP:
479 if (retrans == NFS_UNSPEC_RETRANS)
480 to->to_retries = NFS_DEF_UDP_RETRANS;
481 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
482 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
483 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
484 to->to_initval = NFS_MAX_UDP_TIMEOUT;
485 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
486 to->to_exponential = 1;
487 break;
488 #endif
489 default:
490 BUG();
493 EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
496 * Create an RPC client handle
498 int nfs_create_rpc_client(struct nfs_client *clp,
499 const struct nfs_client_initdata *cl_init,
500 rpc_authflavor_t flavor)
502 struct rpc_clnt *clnt = NULL;
503 struct rpc_create_args args = {
504 .net = clp->cl_net,
505 .protocol = clp->cl_proto,
506 .nconnect = clp->cl_nconnect,
507 .address = (struct sockaddr *)&clp->cl_addr,
508 .addrsize = clp->cl_addrlen,
509 .timeout = cl_init->timeparms,
510 .servername = clp->cl_hostname,
511 .nodename = cl_init->nodename,
512 .program = &nfs_program,
513 .version = clp->rpc_ops->version,
514 .authflavor = flavor,
515 .cred = cl_init->cred,
518 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
519 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
520 if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
521 args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
522 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
523 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
524 if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
525 args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
526 if (test_bit(NFS_CS_NOPING, &clp->cl_flags))
527 args.flags |= RPC_CLNT_CREATE_NOPING;
528 if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags))
529 args.flags |= RPC_CLNT_CREATE_REUSEPORT;
531 if (!IS_ERR(clp->cl_rpcclient))
532 return 0;
534 clnt = rpc_create(&args);
535 if (IS_ERR(clnt)) {
536 dprintk("%s: cannot create RPC client. Error = %ld\n",
537 __func__, PTR_ERR(clnt));
538 return PTR_ERR(clnt);
541 clnt->cl_principal = clp->cl_principal;
542 clp->cl_rpcclient = clnt;
543 return 0;
545 EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
548 * Version 2 or 3 client destruction
550 static void nfs_destroy_server(struct nfs_server *server)
552 if (server->nlm_host)
553 nlmclnt_done(server->nlm_host);
557 * Version 2 or 3 lockd setup
559 static int nfs_start_lockd(struct nfs_server *server)
561 struct nlm_host *host;
562 struct nfs_client *clp = server->nfs_client;
563 struct nlmclnt_initdata nlm_init = {
564 .hostname = clp->cl_hostname,
565 .address = (struct sockaddr *)&clp->cl_addr,
566 .addrlen = clp->cl_addrlen,
567 .nfs_version = clp->rpc_ops->version,
568 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
569 1 : 0,
570 .net = clp->cl_net,
571 .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops,
572 .cred = current_cred(),
575 if (nlm_init.nfs_version > 3)
576 return 0;
577 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
578 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
579 return 0;
581 switch (clp->cl_proto) {
582 default:
583 nlm_init.protocol = IPPROTO_TCP;
584 break;
585 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
586 case XPRT_TRANSPORT_UDP:
587 nlm_init.protocol = IPPROTO_UDP;
588 #endif
591 host = nlmclnt_init(&nlm_init);
592 if (IS_ERR(host))
593 return PTR_ERR(host);
595 server->nlm_host = host;
596 server->destroy = nfs_destroy_server;
597 return 0;
601 * Create a general RPC client
603 int nfs_init_server_rpcclient(struct nfs_server *server,
604 const struct rpc_timeout *timeo,
605 rpc_authflavor_t pseudoflavour)
607 struct nfs_client *clp = server->nfs_client;
609 server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
610 pseudoflavour);
611 if (IS_ERR(server->client)) {
612 dprintk("%s: couldn't create rpc_client!\n", __func__);
613 return PTR_ERR(server->client);
616 memcpy(&server->client->cl_timeout_default,
617 timeo,
618 sizeof(server->client->cl_timeout_default));
619 server->client->cl_timeout = &server->client->cl_timeout_default;
620 server->client->cl_softrtry = 0;
621 if (server->flags & NFS_MOUNT_SOFTERR)
622 server->client->cl_softerr = 1;
623 if (server->flags & NFS_MOUNT_SOFT)
624 server->client->cl_softrtry = 1;
626 return 0;
628 EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
631 * nfs_init_client - Initialise an NFS2 or NFS3 client
633 * @clp: nfs_client to initialise
634 * @cl_init: Initialisation parameters
636 * Returns pointer to an NFS client, or an ERR_PTR value.
638 struct nfs_client *nfs_init_client(struct nfs_client *clp,
639 const struct nfs_client_initdata *cl_init)
641 int error;
643 /* the client is already initialised */
644 if (clp->cl_cons_state == NFS_CS_READY)
645 return clp;
648 * Create a client RPC handle for doing FSSTAT with UNIX auth only
649 * - RFC 2623, sec 2.3.2
651 error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
652 nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error);
653 if (error < 0) {
654 nfs_put_client(clp);
655 clp = ERR_PTR(error);
657 return clp;
659 EXPORT_SYMBOL_GPL(nfs_init_client);
662 * Create a version 2 or 3 client
664 static int nfs_init_server(struct nfs_server *server,
665 const struct fs_context *fc)
667 const struct nfs_fs_context *ctx = nfs_fc2context(fc);
668 struct rpc_timeout timeparms;
669 struct nfs_client_initdata cl_init = {
670 .hostname = ctx->nfs_server.hostname,
671 .addr = (const struct sockaddr *)&ctx->nfs_server.address,
672 .addrlen = ctx->nfs_server.addrlen,
673 .nfs_mod = ctx->nfs_mod,
674 .proto = ctx->nfs_server.protocol,
675 .net = fc->net_ns,
676 .timeparms = &timeparms,
677 .cred = server->cred,
678 .nconnect = ctx->nfs_server.nconnect,
679 .init_flags = (1UL << NFS_CS_REUSEPORT),
681 struct nfs_client *clp;
682 int error;
684 nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
685 ctx->timeo, ctx->retrans);
686 if (ctx->flags & NFS_MOUNT_NORESVPORT)
687 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
689 /* Allocate or find a client reference we can use */
690 clp = nfs_get_client(&cl_init);
691 if (IS_ERR(clp))
692 return PTR_ERR(clp);
694 server->nfs_client = clp;
696 /* Initialise the client representation from the mount data */
697 server->flags = ctx->flags;
698 server->options = ctx->options;
699 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
700 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
701 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
703 if (ctx->rsize)
704 server->rsize = nfs_block_size(ctx->rsize, NULL);
705 if (ctx->wsize)
706 server->wsize = nfs_block_size(ctx->wsize, NULL);
708 server->acregmin = ctx->acregmin * HZ;
709 server->acregmax = ctx->acregmax * HZ;
710 server->acdirmin = ctx->acdirmin * HZ;
711 server->acdirmax = ctx->acdirmax * HZ;
713 /* Start lockd here, before we might error out */
714 error = nfs_start_lockd(server);
715 if (error < 0)
716 goto error;
718 server->port = ctx->nfs_server.port;
719 server->auth_info = ctx->auth_info;
721 error = nfs_init_server_rpcclient(server, &timeparms,
722 ctx->selected_flavor);
723 if (error < 0)
724 goto error;
726 /* Preserve the values of mount_server-related mount options */
727 if (ctx->mount_server.addrlen) {
728 memcpy(&server->mountd_address, &ctx->mount_server.address,
729 ctx->mount_server.addrlen);
730 server->mountd_addrlen = ctx->mount_server.addrlen;
732 server->mountd_version = ctx->mount_server.version;
733 server->mountd_port = ctx->mount_server.port;
734 server->mountd_protocol = ctx->mount_server.protocol;
736 server->namelen = ctx->namlen;
737 return 0;
739 error:
740 server->nfs_client = NULL;
741 nfs_put_client(clp);
742 return error;
746 * Load up the server record from information gained in an fsinfo record
748 static void nfs_server_set_fsinfo(struct nfs_server *server,
749 struct nfs_fsinfo *fsinfo)
751 unsigned long max_rpc_payload;
753 /* Work out a lot of parameters */
754 if (server->rsize == 0)
755 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
756 if (server->wsize == 0)
757 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
759 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
760 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
761 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
762 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
764 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
765 if (server->rsize > max_rpc_payload)
766 server->rsize = max_rpc_payload;
767 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
768 server->rsize = NFS_MAX_FILE_IO_SIZE;
769 server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
771 if (server->wsize > max_rpc_payload)
772 server->wsize = max_rpc_payload;
773 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
774 server->wsize = NFS_MAX_FILE_IO_SIZE;
775 server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
777 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
779 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
780 if (server->dtsize > PAGE_SIZE * NFS_MAX_READDIR_PAGES)
781 server->dtsize = PAGE_SIZE * NFS_MAX_READDIR_PAGES;
782 if (server->dtsize > server->rsize)
783 server->dtsize = server->rsize;
785 if (server->flags & NFS_MOUNT_NOAC) {
786 server->acregmin = server->acregmax = 0;
787 server->acdirmin = server->acdirmax = 0;
790 server->maxfilesize = fsinfo->maxfilesize;
792 server->time_delta = fsinfo->time_delta;
794 server->clone_blksize = fsinfo->clone_blksize;
795 /* We're airborne Set socket buffersize */
796 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
800 * Probe filesystem information, including the FSID on v2/v3
802 int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
804 struct nfs_fsinfo fsinfo;
805 struct nfs_client *clp = server->nfs_client;
806 int error;
808 if (clp->rpc_ops->set_capabilities != NULL) {
809 error = clp->rpc_ops->set_capabilities(server, mntfh);
810 if (error < 0)
811 return error;
814 fsinfo.fattr = fattr;
815 fsinfo.nlayouttypes = 0;
816 memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
817 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
818 if (error < 0)
819 return error;
821 nfs_server_set_fsinfo(server, &fsinfo);
823 /* Get some general file system info */
824 if (server->namelen == 0) {
825 struct nfs_pathconf pathinfo;
827 pathinfo.fattr = fattr;
828 nfs_fattr_init(fattr);
830 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
831 server->namelen = pathinfo.max_namelen;
834 return 0;
836 EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
839 * Copy useful information when duplicating a server record
841 void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
843 target->flags = source->flags;
844 target->rsize = source->rsize;
845 target->wsize = source->wsize;
846 target->acregmin = source->acregmin;
847 target->acregmax = source->acregmax;
848 target->acdirmin = source->acdirmin;
849 target->acdirmax = source->acdirmax;
850 target->caps = source->caps;
851 target->options = source->options;
852 target->auth_info = source->auth_info;
853 target->port = source->port;
855 EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
857 void nfs_server_insert_lists(struct nfs_server *server)
859 struct nfs_client *clp = server->nfs_client;
860 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
862 spin_lock(&nn->nfs_client_lock);
863 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
864 list_add_tail(&server->master_link, &nn->nfs_volume_list);
865 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
866 spin_unlock(&nn->nfs_client_lock);
869 EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
871 void nfs_server_remove_lists(struct nfs_server *server)
873 struct nfs_client *clp = server->nfs_client;
874 struct nfs_net *nn;
876 if (clp == NULL)
877 return;
878 nn = net_generic(clp->cl_net, nfs_net_id);
879 spin_lock(&nn->nfs_client_lock);
880 list_del_rcu(&server->client_link);
881 if (list_empty(&clp->cl_superblocks))
882 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
883 list_del(&server->master_link);
884 spin_unlock(&nn->nfs_client_lock);
886 synchronize_rcu();
888 EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
891 * Allocate and initialise a server record
893 struct nfs_server *nfs_alloc_server(void)
895 struct nfs_server *server;
897 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
898 if (!server)
899 return NULL;
901 server->client = server->client_acl = ERR_PTR(-EINVAL);
903 /* Zero out the NFS state stuff */
904 INIT_LIST_HEAD(&server->client_link);
905 INIT_LIST_HEAD(&server->master_link);
906 INIT_LIST_HEAD(&server->delegations);
907 INIT_LIST_HEAD(&server->layouts);
908 INIT_LIST_HEAD(&server->state_owners_lru);
909 INIT_LIST_HEAD(&server->ss_copies);
911 atomic_set(&server->active, 0);
913 server->io_stats = nfs_alloc_iostats();
914 if (!server->io_stats) {
915 kfree(server);
916 return NULL;
919 ida_init(&server->openowner_id);
920 ida_init(&server->lockowner_id);
921 pnfs_init_server(server);
922 rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
924 return server;
926 EXPORT_SYMBOL_GPL(nfs_alloc_server);
929 * Free up a server record
931 void nfs_free_server(struct nfs_server *server)
933 nfs_server_remove_lists(server);
935 if (server->destroy != NULL)
936 server->destroy(server);
938 if (!IS_ERR(server->client_acl))
939 rpc_shutdown_client(server->client_acl);
940 if (!IS_ERR(server->client))
941 rpc_shutdown_client(server->client);
943 nfs_put_client(server->nfs_client);
945 ida_destroy(&server->lockowner_id);
946 ida_destroy(&server->openowner_id);
947 nfs_free_iostats(server->io_stats);
948 put_cred(server->cred);
949 kfree(server);
950 nfs_release_automount_timer();
952 EXPORT_SYMBOL_GPL(nfs_free_server);
955 * Create a version 2 or 3 volume record
956 * - keyed on server and FSID
958 struct nfs_server *nfs_create_server(struct fs_context *fc)
960 struct nfs_fs_context *ctx = nfs_fc2context(fc);
961 struct nfs_server *server;
962 struct nfs_fattr *fattr;
963 int error;
965 server = nfs_alloc_server();
966 if (!server)
967 return ERR_PTR(-ENOMEM);
969 server->cred = get_cred(current_cred());
971 error = -ENOMEM;
972 fattr = nfs_alloc_fattr();
973 if (fattr == NULL)
974 goto error;
976 /* Get a client representation */
977 error = nfs_init_server(server, fc);
978 if (error < 0)
979 goto error;
981 /* Probe the root fh to retrieve its FSID */
982 error = nfs_probe_fsinfo(server, ctx->mntfh, fattr);
983 if (error < 0)
984 goto error;
985 if (server->nfs_client->rpc_ops->version == 3) {
986 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
987 server->namelen = NFS3_MAXNAMLEN;
988 if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS))
989 server->caps |= NFS_CAP_READDIRPLUS;
990 } else {
991 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
992 server->namelen = NFS2_MAXNAMLEN;
995 if (!(fattr->valid & NFS_ATTR_FATTR)) {
996 error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh,
997 fattr, NULL, NULL);
998 if (error < 0) {
999 dprintk("nfs_create_server: getattr error = %d\n", -error);
1000 goto error;
1003 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1005 dprintk("Server FSID: %llx:%llx\n",
1006 (unsigned long long) server->fsid.major,
1007 (unsigned long long) server->fsid.minor);
1009 nfs_server_insert_lists(server);
1010 server->mount_time = jiffies;
1011 nfs_free_fattr(fattr);
1012 return server;
1014 error:
1015 nfs_free_fattr(fattr);
1016 nfs_free_server(server);
1017 return ERR_PTR(error);
1019 EXPORT_SYMBOL_GPL(nfs_create_server);
1022 * Clone an NFS2, NFS3 or NFS4 server record
1024 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1025 struct nfs_fh *fh,
1026 struct nfs_fattr *fattr,
1027 rpc_authflavor_t flavor)
1029 struct nfs_server *server;
1030 struct nfs_fattr *fattr_fsinfo;
1031 int error;
1033 server = nfs_alloc_server();
1034 if (!server)
1035 return ERR_PTR(-ENOMEM);
1037 server->cred = get_cred(source->cred);
1039 error = -ENOMEM;
1040 fattr_fsinfo = nfs_alloc_fattr();
1041 if (fattr_fsinfo == NULL)
1042 goto out_free_server;
1044 /* Copy data from the source */
1045 server->nfs_client = source->nfs_client;
1046 server->destroy = source->destroy;
1047 refcount_inc(&server->nfs_client->cl_count);
1048 nfs_server_copy_userdata(server, source);
1050 server->fsid = fattr->fsid;
1052 error = nfs_init_server_rpcclient(server,
1053 source->client->cl_timeout,
1054 flavor);
1055 if (error < 0)
1056 goto out_free_server;
1058 /* probe the filesystem info for this server filesystem */
1059 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1060 if (error < 0)
1061 goto out_free_server;
1063 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1064 server->namelen = NFS4_MAXNAMLEN;
1066 error = nfs_start_lockd(server);
1067 if (error < 0)
1068 goto out_free_server;
1070 nfs_server_insert_lists(server);
1071 server->mount_time = jiffies;
1073 nfs_free_fattr(fattr_fsinfo);
1074 return server;
1076 out_free_server:
1077 nfs_free_fattr(fattr_fsinfo);
1078 nfs_free_server(server);
1079 return ERR_PTR(error);
1081 EXPORT_SYMBOL_GPL(nfs_clone_server);
1083 void nfs_clients_init(struct net *net)
1085 struct nfs_net *nn = net_generic(net, nfs_net_id);
1087 INIT_LIST_HEAD(&nn->nfs_client_list);
1088 INIT_LIST_HEAD(&nn->nfs_volume_list);
1089 #if IS_ENABLED(CONFIG_NFS_V4)
1090 idr_init(&nn->cb_ident_idr);
1091 #endif
1092 spin_lock_init(&nn->nfs_client_lock);
1093 nn->boot_time = ktime_get_real();
1095 nfs_netns_sysfs_setup(nn, net);
1098 void nfs_clients_exit(struct net *net)
1100 struct nfs_net *nn = net_generic(net, nfs_net_id);
1102 nfs_netns_sysfs_destroy(nn);
1103 nfs_cleanup_cb_ident_idr(net);
1104 WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
1105 WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
1108 #ifdef CONFIG_PROC_FS
1109 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1110 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1111 static void nfs_server_list_stop(struct seq_file *p, void *v);
1112 static int nfs_server_list_show(struct seq_file *m, void *v);
1114 static const struct seq_operations nfs_server_list_ops = {
1115 .start = nfs_server_list_start,
1116 .next = nfs_server_list_next,
1117 .stop = nfs_server_list_stop,
1118 .show = nfs_server_list_show,
1121 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1122 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1123 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1124 static int nfs_volume_list_show(struct seq_file *m, void *v);
1126 static const struct seq_operations nfs_volume_list_ops = {
1127 .start = nfs_volume_list_start,
1128 .next = nfs_volume_list_next,
1129 .stop = nfs_volume_list_stop,
1130 .show = nfs_volume_list_show,
1134 * set up the iterator to start reading from the server list and return the first item
1136 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1137 __acquires(&nn->nfs_client_lock)
1139 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1141 /* lock the list against modification */
1142 spin_lock(&nn->nfs_client_lock);
1143 return seq_list_start_head(&nn->nfs_client_list, *_pos);
1147 * move to next server
1149 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1151 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1153 return seq_list_next(v, &nn->nfs_client_list, pos);
1157 * clean up after reading from the transports list
1159 static void nfs_server_list_stop(struct seq_file *p, void *v)
1160 __releases(&nn->nfs_client_lock)
1162 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1164 spin_unlock(&nn->nfs_client_lock);
1168 * display a header line followed by a load of call lines
1170 static int nfs_server_list_show(struct seq_file *m, void *v)
1172 struct nfs_client *clp;
1173 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1175 /* display header on line 1 */
1176 if (v == &nn->nfs_client_list) {
1177 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1178 return 0;
1181 /* display one transport per line on subsequent lines */
1182 clp = list_entry(v, struct nfs_client, cl_share_link);
1184 /* Check if the client is initialized */
1185 if (clp->cl_cons_state != NFS_CS_READY)
1186 return 0;
1188 rcu_read_lock();
1189 seq_printf(m, "v%u %s %s %3d %s\n",
1190 clp->rpc_ops->version,
1191 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1192 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1193 refcount_read(&clp->cl_count),
1194 clp->cl_hostname);
1195 rcu_read_unlock();
1197 return 0;
1201 * set up the iterator to start reading from the volume list and return the first item
1203 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1204 __acquires(&nn->nfs_client_lock)
1206 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1208 /* lock the list against modification */
1209 spin_lock(&nn->nfs_client_lock);
1210 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1214 * move to next volume
1216 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1218 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1220 return seq_list_next(v, &nn->nfs_volume_list, pos);
1224 * clean up after reading from the transports list
1226 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1227 __releases(&nn->nfs_client_lock)
1229 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1231 spin_unlock(&nn->nfs_client_lock);
1235 * display a header line followed by a load of call lines
1237 static int nfs_volume_list_show(struct seq_file *m, void *v)
1239 struct nfs_server *server;
1240 struct nfs_client *clp;
1241 char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1242 char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0'
1243 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1245 /* display header on line 1 */
1246 if (v == &nn->nfs_volume_list) {
1247 seq_puts(m, "NV SERVER PORT DEV FSID"
1248 " FSC\n");
1249 return 0;
1251 /* display one transport per line on subsequent lines */
1252 server = list_entry(v, struct nfs_server, master_link);
1253 clp = server->nfs_client;
1255 snprintf(dev, sizeof(dev), "%u:%u",
1256 MAJOR(server->s_dev), MINOR(server->s_dev));
1258 snprintf(fsid, sizeof(fsid), "%llx:%llx",
1259 (unsigned long long) server->fsid.major,
1260 (unsigned long long) server->fsid.minor);
1262 rcu_read_lock();
1263 seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
1264 clp->rpc_ops->version,
1265 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1266 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1267 dev,
1268 fsid,
1269 nfs_server_fscache_state(server));
1270 rcu_read_unlock();
1272 return 0;
1275 int nfs_fs_proc_net_init(struct net *net)
1277 struct nfs_net *nn = net_generic(net, nfs_net_id);
1278 struct proc_dir_entry *p;
1280 nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
1281 if (!nn->proc_nfsfs)
1282 goto error_0;
1284 /* a file of servers with which we're dealing */
1285 p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1286 &nfs_server_list_ops, sizeof(struct seq_net_private));
1287 if (!p)
1288 goto error_1;
1290 /* a file of volumes that we have mounted */
1291 p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1292 &nfs_volume_list_ops, sizeof(struct seq_net_private));
1293 if (!p)
1294 goto error_1;
1295 return 0;
1297 error_1:
1298 remove_proc_subtree("nfsfs", net->proc_net);
1299 error_0:
1300 return -ENOMEM;
1303 void nfs_fs_proc_net_exit(struct net *net)
1305 remove_proc_subtree("nfsfs", net->proc_net);
1309 * initialise the /proc/fs/nfsfs/ directory
1311 int __init nfs_fs_proc_init(void)
1313 if (!proc_mkdir("fs/nfsfs", NULL))
1314 goto error_0;
1316 /* a file of servers with which we're dealing */
1317 if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
1318 goto error_1;
1320 /* a file of volumes that we have mounted */
1321 if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
1322 goto error_1;
1324 return 0;
1325 error_1:
1326 remove_proc_subtree("fs/nfsfs", NULL);
1327 error_0:
1328 return -ENOMEM;
1332 * clean up the /proc/fs/nfsfs/ directory
1334 void nfs_fs_proc_exit(void)
1336 remove_proc_subtree("fs/nfsfs", NULL);
1339 #endif /* CONFIG_PROC_FS */