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)
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>
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>
37 #include <linux/nfs_xdr.h>
38 #include <linux/sunrpc/bc_xprt.h>
39 #include <linux/nsproxy.h>
40 #include <linux/pid_namespace.h>
45 #include "delegation.h"
55 #define NFSDBG_FACILITY NFSDBG_CLIENT
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);
65 static const struct rpc_version *nfs_version[5] = {
71 const struct rpc_program nfs_program = {
73 .number = NFS_PROGRAM,
74 .nrvers = ARRAY_SIZE(nfs_version),
75 .version = nfs_version,
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);
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);
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);
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;
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
156 clp->cl_minorversion = cl_init->minorversion;
157 clp->cl_nfs_mod = cl_init->nfs_mod;
158 if (!try_module_get(clp->cl_nfs_mod->owner))
161 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
163 refcount_set(&clp->cl_count, 1);
164 clp->cl_cons_state = NFS_CS_INITING;
166 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
167 clp->cl_addrlen = cl_init->addrlen;
169 if (cl_init->hostname) {
171 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
172 if (!clp->cl_hostname)
176 INIT_LIST_HEAD(&clp->cl_superblocks);
177 clp->cl_rpcclient = ERR_PTR(-EINVAL);
179 clp->cl_flags = cl_init->init_flags;
180 clp->cl_proto = cl_init->proto;
181 clp->cl_nconnect = cl_init->nconnect;
182 clp->cl_net = get_net(cl_init->net);
184 clp->cl_principal = "*";
185 nfs_fscache_get_client_cookie(clp);
190 put_nfs_version(clp->cl_nfs_mod);
196 EXPORT_SYMBOL_GPL(nfs_alloc_client);
198 #if IS_ENABLED(CONFIG_NFS_V4)
199 static void nfs_cleanup_cb_ident_idr(struct net *net)
201 struct nfs_net *nn = net_generic(net, nfs_net_id);
203 idr_destroy(&nn->cb_ident_idr);
206 /* nfs_client_lock held */
207 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
209 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
211 if (clp->cl_cb_ident)
212 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
215 static void pnfs_init_server(struct nfs_server *server)
217 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
221 static void nfs_cleanup_cb_ident_idr(struct net *net)
225 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
229 static void pnfs_init_server(struct nfs_server *server)
233 #endif /* CONFIG_NFS_V4 */
236 * Destroy a shared client record
238 void nfs_free_client(struct nfs_client *clp)
240 nfs_fscache_release_client_cookie(clp);
242 /* -EIO all pending I/O */
243 if (!IS_ERR(clp->cl_rpcclient))
244 rpc_shutdown_client(clp->cl_rpcclient);
246 put_net(clp->cl_net);
247 put_nfs_version(clp->cl_nfs_mod);
248 kfree(clp->cl_hostname);
249 kfree(clp->cl_acceptor);
252 EXPORT_SYMBOL_GPL(nfs_free_client);
255 * Release a reference to a shared client record
257 void nfs_put_client(struct nfs_client *clp)
264 nn = net_generic(clp->cl_net, nfs_net_id);
266 if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
267 list_del(&clp->cl_share_link);
268 nfs_cb_idr_remove_locked(clp);
269 spin_unlock(&nn->nfs_client_lock);
271 WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
273 clp->rpc_ops->free_client(clp);
276 EXPORT_SYMBOL_GPL(nfs_put_client);
279 * Find an nfs_client on the list that matches the initialisation data
282 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
284 struct nfs_client *clp;
285 const struct sockaddr *sap = data->addr;
286 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
290 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
291 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
292 /* Don't match clients that failed to initialise properly */
293 if (clp->cl_cons_state < 0)
296 /* If a client is still initializing then we need to wait */
297 if (clp->cl_cons_state > NFS_CS_READY) {
298 refcount_inc(&clp->cl_count);
299 spin_unlock(&nn->nfs_client_lock);
300 error = nfs_wait_client_init_complete(clp);
302 spin_lock(&nn->nfs_client_lock);
304 return ERR_PTR(error);
308 /* Different NFS versions cannot share the same nfs_client */
309 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
312 if (clp->cl_proto != data->proto)
314 /* Match nfsv4 minorversion */
315 if (clp->cl_minorversion != data->minorversion)
318 /* Match request for a dedicated DS */
319 if (test_bit(NFS_CS_DS, &data->init_flags) !=
320 test_bit(NFS_CS_DS, &clp->cl_flags))
323 /* Match the full socket address */
324 if (!rpc_cmp_addr_port(sap, clap))
325 /* Match all xprt_switch full socket addresses */
326 if (IS_ERR(clp->cl_rpcclient) ||
327 !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
331 refcount_inc(&clp->cl_count);
338 * Return true if @clp is done initializing, false if still working on it.
340 * Use nfs_client_init_status to check if it was successful.
342 bool nfs_client_init_is_complete(const struct nfs_client *clp)
344 return clp->cl_cons_state <= NFS_CS_READY;
346 EXPORT_SYMBOL_GPL(nfs_client_init_is_complete);
349 * Return 0 if @clp was successfully initialized, -errno otherwise.
351 * This must be called *after* nfs_client_init_is_complete() returns true,
352 * otherwise it will pop WARN_ON_ONCE and return -EINVAL
354 int nfs_client_init_status(const struct nfs_client *clp)
356 /* called without checking nfs_client_init_is_complete */
357 if (clp->cl_cons_state > NFS_CS_READY) {
361 return clp->cl_cons_state;
363 EXPORT_SYMBOL_GPL(nfs_client_init_status);
365 int nfs_wait_client_init_complete(const struct nfs_client *clp)
367 return wait_event_killable(nfs_client_active_wq,
368 nfs_client_init_is_complete(clp));
370 EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
373 * Found an existing client. Make sure it's ready before returning.
375 static struct nfs_client *
376 nfs_found_client(const struct nfs_client_initdata *cl_init,
377 struct nfs_client *clp)
381 error = nfs_wait_client_init_complete(clp);
384 return ERR_PTR(-ERESTARTSYS);
387 if (clp->cl_cons_state < NFS_CS_READY) {
388 error = clp->cl_cons_state;
390 return ERR_PTR(error);
398 * Look up a client by IP address and protocol version
399 * - creates a new record if one doesn't yet exist
401 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
403 struct nfs_client *clp, *new = NULL;
404 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
405 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
407 if (cl_init->hostname == NULL) {
409 return ERR_PTR(-EINVAL);
412 /* see if the client already exists */
414 spin_lock(&nn->nfs_client_lock);
416 clp = nfs_match_client(cl_init);
418 spin_unlock(&nn->nfs_client_lock);
420 new->rpc_ops->free_client(new);
423 return nfs_found_client(cl_init, clp);
426 list_add_tail(&new->cl_share_link,
427 &nn->nfs_client_list);
428 spin_unlock(&nn->nfs_client_lock);
429 return rpc_ops->init_client(new, cl_init);
432 spin_unlock(&nn->nfs_client_lock);
434 new = rpc_ops->alloc_client(cl_init);
435 } while (!IS_ERR(new));
439 EXPORT_SYMBOL_GPL(nfs_get_client);
442 * Mark a server as ready or failed
444 void nfs_mark_client_ready(struct nfs_client *clp, int state)
447 clp->cl_cons_state = state;
448 wake_up_all(&nfs_client_active_wq);
450 EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
453 * Initialise the timeout values for a connection
455 void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
456 int timeo, int retrans)
458 to->to_initval = timeo * HZ / 10;
459 to->to_retries = retrans;
462 case XPRT_TRANSPORT_TCP:
463 case XPRT_TRANSPORT_RDMA:
464 if (retrans == NFS_UNSPEC_RETRANS)
465 to->to_retries = NFS_DEF_TCP_RETRANS;
466 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
467 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
468 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
469 to->to_initval = NFS_MAX_TCP_TIMEOUT;
470 to->to_increment = to->to_initval;
471 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
472 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
473 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
474 if (to->to_maxval < to->to_initval)
475 to->to_maxval = to->to_initval;
476 to->to_exponential = 0;
478 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
479 case XPRT_TRANSPORT_UDP:
480 if (retrans == NFS_UNSPEC_RETRANS)
481 to->to_retries = NFS_DEF_UDP_RETRANS;
482 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
483 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
484 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
485 to->to_initval = NFS_MAX_UDP_TIMEOUT;
486 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
487 to->to_exponential = 1;
494 EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
497 * Create an RPC client handle
499 int nfs_create_rpc_client(struct nfs_client *clp,
500 const struct nfs_client_initdata *cl_init,
501 rpc_authflavor_t flavor)
503 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
504 struct rpc_clnt *clnt = NULL;
505 struct rpc_create_args args = {
507 .protocol = clp->cl_proto,
508 .nconnect = clp->cl_nconnect,
509 .address = (struct sockaddr *)&clp->cl_addr,
510 .addrsize = clp->cl_addrlen,
511 .timeout = cl_init->timeparms,
512 .servername = clp->cl_hostname,
513 .nodename = cl_init->nodename,
514 .program = &nfs_program,
515 .stats = &nn->rpcstats,
516 .version = clp->rpc_ops->version,
517 .authflavor = flavor,
518 .cred = cl_init->cred,
521 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
522 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
523 if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
524 args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
525 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
526 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
527 if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
528 args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
529 if (test_bit(NFS_CS_NOPING, &clp->cl_flags))
530 args.flags |= RPC_CLNT_CREATE_NOPING;
531 if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags))
532 args.flags |= RPC_CLNT_CREATE_REUSEPORT;
534 if (!IS_ERR(clp->cl_rpcclient))
537 clnt = rpc_create(&args);
539 dprintk("%s: cannot create RPC client. Error = %ld\n",
540 __func__, PTR_ERR(clnt));
541 return PTR_ERR(clnt);
544 clnt->cl_principal = clp->cl_principal;
545 clp->cl_rpcclient = clnt;
548 EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
551 * Version 2 or 3 client destruction
553 static void nfs_destroy_server(struct nfs_server *server)
555 if (server->nlm_host)
556 nlmclnt_done(server->nlm_host);
560 * Version 2 or 3 lockd setup
562 static int nfs_start_lockd(struct nfs_server *server)
564 struct nlm_host *host;
565 struct nfs_client *clp = server->nfs_client;
566 struct nlmclnt_initdata nlm_init = {
567 .hostname = clp->cl_hostname,
568 .address = (struct sockaddr *)&clp->cl_addr,
569 .addrlen = clp->cl_addrlen,
570 .nfs_version = clp->rpc_ops->version,
571 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
574 .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops,
575 .cred = current_cred(),
578 if (nlm_init.nfs_version > 3)
580 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
581 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
584 switch (clp->cl_proto) {
586 nlm_init.protocol = IPPROTO_TCP;
588 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT
589 case XPRT_TRANSPORT_UDP:
590 nlm_init.protocol = IPPROTO_UDP;
594 host = nlmclnt_init(&nlm_init);
596 return PTR_ERR(host);
598 server->nlm_host = host;
599 server->destroy = nfs_destroy_server;
604 * Create a general RPC client
606 int nfs_init_server_rpcclient(struct nfs_server *server,
607 const struct rpc_timeout *timeo,
608 rpc_authflavor_t pseudoflavour)
610 struct nfs_client *clp = server->nfs_client;
612 server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
614 if (IS_ERR(server->client)) {
615 dprintk("%s: couldn't create rpc_client!\n", __func__);
616 return PTR_ERR(server->client);
619 memcpy(&server->client->cl_timeout_default,
621 sizeof(server->client->cl_timeout_default));
622 server->client->cl_timeout = &server->client->cl_timeout_default;
623 server->client->cl_softrtry = 0;
624 if (server->flags & NFS_MOUNT_SOFTERR)
625 server->client->cl_softerr = 1;
626 if (server->flags & NFS_MOUNT_SOFT)
627 server->client->cl_softrtry = 1;
631 EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
634 * nfs_init_client - Initialise an NFS2 or NFS3 client
636 * @clp: nfs_client to initialise
637 * @cl_init: Initialisation parameters
639 * Returns pointer to an NFS client, or an ERR_PTR value.
641 struct nfs_client *nfs_init_client(struct nfs_client *clp,
642 const struct nfs_client_initdata *cl_init)
646 /* the client is already initialised */
647 if (clp->cl_cons_state == NFS_CS_READY)
651 * Create a client RPC handle for doing FSSTAT with UNIX auth only
652 * - RFC 2623, sec 2.3.2
654 error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
655 nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error);
658 clp = ERR_PTR(error);
662 EXPORT_SYMBOL_GPL(nfs_init_client);
665 * Create a version 2 or 3 client
667 static int nfs_init_server(struct nfs_server *server,
668 const struct fs_context *fc)
670 const struct nfs_fs_context *ctx = nfs_fc2context(fc);
671 struct rpc_timeout timeparms;
672 struct nfs_client_initdata cl_init = {
673 .hostname = ctx->nfs_server.hostname,
674 .addr = (const struct sockaddr *)&ctx->nfs_server.address,
675 .addrlen = ctx->nfs_server.addrlen,
676 .nfs_mod = ctx->nfs_mod,
677 .proto = ctx->nfs_server.protocol,
679 .timeparms = &timeparms,
680 .cred = server->cred,
681 .nconnect = ctx->nfs_server.nconnect,
682 .init_flags = (1UL << NFS_CS_REUSEPORT),
684 struct nfs_client *clp;
687 nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
688 ctx->timeo, ctx->retrans);
689 if (ctx->flags & NFS_MOUNT_NORESVPORT)
690 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
692 /* Allocate or find a client reference we can use */
693 clp = nfs_get_client(&cl_init);
697 server->nfs_client = clp;
699 /* Initialise the client representation from the mount data */
700 server->flags = ctx->flags;
701 server->options = ctx->options;
702 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
703 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
704 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
707 server->rsize = nfs_block_size(ctx->rsize, NULL);
709 server->wsize = nfs_block_size(ctx->wsize, NULL);
711 server->acregmin = ctx->acregmin * HZ;
712 server->acregmax = ctx->acregmax * HZ;
713 server->acdirmin = ctx->acdirmin * HZ;
714 server->acdirmax = ctx->acdirmax * HZ;
716 /* Start lockd here, before we might error out */
717 error = nfs_start_lockd(server);
721 server->port = ctx->nfs_server.port;
722 server->auth_info = ctx->auth_info;
724 error = nfs_init_server_rpcclient(server, &timeparms,
725 ctx->selected_flavor);
729 /* Preserve the values of mount_server-related mount options */
730 if (ctx->mount_server.addrlen) {
731 memcpy(&server->mountd_address, &ctx->mount_server.address,
732 ctx->mount_server.addrlen);
733 server->mountd_addrlen = ctx->mount_server.addrlen;
735 server->mountd_version = ctx->mount_server.version;
736 server->mountd_port = ctx->mount_server.port;
737 server->mountd_protocol = ctx->mount_server.protocol;
739 server->namelen = ctx->namlen;
743 server->nfs_client = NULL;
749 * Load up the server record from information gained in an fsinfo record
751 static void nfs_server_set_fsinfo(struct nfs_server *server,
752 struct nfs_fsinfo *fsinfo)
754 unsigned long max_rpc_payload, raw_max_rpc_payload;
756 /* Work out a lot of parameters */
757 if (server->rsize == 0)
758 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
759 if (server->wsize == 0)
760 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
762 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
763 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
764 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
765 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
767 raw_max_rpc_payload = rpc_max_payload(server->client);
768 max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL);
770 if (server->rsize > max_rpc_payload)
771 server->rsize = max_rpc_payload;
772 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
773 server->rsize = NFS_MAX_FILE_IO_SIZE;
774 server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
776 if (server->wsize > max_rpc_payload)
777 server->wsize = max_rpc_payload;
778 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
779 server->wsize = NFS_MAX_FILE_IO_SIZE;
780 server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
782 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
784 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
785 if (server->dtsize > PAGE_SIZE * NFS_MAX_READDIR_PAGES)
786 server->dtsize = PAGE_SIZE * NFS_MAX_READDIR_PAGES;
787 if (server->dtsize > server->rsize)
788 server->dtsize = server->rsize;
790 if (server->flags & NFS_MOUNT_NOAC) {
791 server->acregmin = server->acregmax = 0;
792 server->acdirmin = server->acdirmax = 0;
795 server->maxfilesize = fsinfo->maxfilesize;
797 server->time_delta = fsinfo->time_delta;
799 server->clone_blksize = fsinfo->clone_blksize;
800 /* We're airborne Set socket buffersize */
801 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
803 #ifdef CONFIG_NFS_V4_2
805 * Defaults until limited by the session parameters.
807 server->gxasize = min_t(unsigned int, raw_max_rpc_payload,
809 server->sxasize = min_t(unsigned int, raw_max_rpc_payload,
811 server->lxasize = min_t(unsigned int, raw_max_rpc_payload,
812 nfs42_listxattr_xdrsize(XATTR_LIST_MAX));
814 if (fsinfo->xattr_support)
815 server->caps |= NFS_CAP_XATTR;
820 * Probe filesystem information, including the FSID on v2/v3
822 int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
824 struct nfs_fsinfo fsinfo;
825 struct nfs_client *clp = server->nfs_client;
828 if (clp->rpc_ops->set_capabilities != NULL) {
829 error = clp->rpc_ops->set_capabilities(server, mntfh);
834 fsinfo.fattr = fattr;
835 fsinfo.nlayouttypes = 0;
836 memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
837 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
841 nfs_server_set_fsinfo(server, &fsinfo);
843 /* Get some general file system info */
844 if (server->namelen == 0) {
845 struct nfs_pathconf pathinfo;
847 pathinfo.fattr = fattr;
848 nfs_fattr_init(fattr);
850 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
851 server->namelen = pathinfo.max_namelen;
856 EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
859 * Copy useful information when duplicating a server record
861 void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
863 target->flags = source->flags;
864 target->rsize = source->rsize;
865 target->wsize = source->wsize;
866 target->acregmin = source->acregmin;
867 target->acregmax = source->acregmax;
868 target->acdirmin = source->acdirmin;
869 target->acdirmax = source->acdirmax;
870 target->caps = source->caps;
871 target->options = source->options;
872 target->auth_info = source->auth_info;
873 target->port = source->port;
875 EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
877 void nfs_server_insert_lists(struct nfs_server *server)
879 struct nfs_client *clp = server->nfs_client;
880 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
882 spin_lock(&nn->nfs_client_lock);
883 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
884 list_add_tail(&server->master_link, &nn->nfs_volume_list);
885 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
886 spin_unlock(&nn->nfs_client_lock);
889 EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
891 void nfs_server_remove_lists(struct nfs_server *server)
893 struct nfs_client *clp = server->nfs_client;
898 nn = net_generic(clp->cl_net, nfs_net_id);
899 spin_lock(&nn->nfs_client_lock);
900 list_del_rcu(&server->client_link);
901 if (list_empty(&clp->cl_superblocks))
902 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
903 list_del(&server->master_link);
904 spin_unlock(&nn->nfs_client_lock);
908 EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
911 * Allocate and initialise a server record
913 struct nfs_server *nfs_alloc_server(void)
915 struct nfs_server *server;
917 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
921 server->client = server->client_acl = ERR_PTR(-EINVAL);
923 /* Zero out the NFS state stuff */
924 INIT_LIST_HEAD(&server->client_link);
925 INIT_LIST_HEAD(&server->master_link);
926 INIT_LIST_HEAD(&server->delegations);
927 INIT_LIST_HEAD(&server->layouts);
928 INIT_LIST_HEAD(&server->state_owners_lru);
929 INIT_LIST_HEAD(&server->ss_copies);
931 atomic_set(&server->active, 0);
933 server->io_stats = nfs_alloc_iostats();
934 if (!server->io_stats) {
939 ida_init(&server->openowner_id);
940 ida_init(&server->lockowner_id);
941 pnfs_init_server(server);
942 rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC");
946 EXPORT_SYMBOL_GPL(nfs_alloc_server);
949 * Free up a server record
951 void nfs_free_server(struct nfs_server *server)
953 nfs_server_remove_lists(server);
955 if (server->destroy != NULL)
956 server->destroy(server);
958 if (!IS_ERR(server->client_acl))
959 rpc_shutdown_client(server->client_acl);
960 if (!IS_ERR(server->client))
961 rpc_shutdown_client(server->client);
963 nfs_put_client(server->nfs_client);
965 ida_destroy(&server->lockowner_id);
966 ida_destroy(&server->openowner_id);
967 nfs_free_iostats(server->io_stats);
968 put_cred(server->cred);
970 nfs_release_automount_timer();
972 EXPORT_SYMBOL_GPL(nfs_free_server);
975 * Create a version 2 or 3 volume record
976 * - keyed on server and FSID
978 struct nfs_server *nfs_create_server(struct fs_context *fc)
980 struct nfs_fs_context *ctx = nfs_fc2context(fc);
981 struct nfs_server *server;
982 struct nfs_fattr *fattr;
985 server = nfs_alloc_server();
987 return ERR_PTR(-ENOMEM);
989 server->cred = get_cred(current_cred());
992 fattr = nfs_alloc_fattr();
996 /* Get a client representation */
997 error = nfs_init_server(server, fc);
1001 /* Probe the root fh to retrieve its FSID */
1002 error = nfs_probe_fsinfo(server, ctx->mntfh, fattr);
1005 if (server->nfs_client->rpc_ops->version == 3) {
1006 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1007 server->namelen = NFS3_MAXNAMLEN;
1008 if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS))
1009 server->caps |= NFS_CAP_READDIRPLUS;
1011 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1012 server->namelen = NFS2_MAXNAMLEN;
1015 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1016 error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh,
1019 dprintk("nfs_create_server: getattr error = %d\n", -error);
1023 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
1025 dprintk("Server FSID: %llx:%llx\n",
1026 (unsigned long long) server->fsid.major,
1027 (unsigned long long) server->fsid.minor);
1029 nfs_server_insert_lists(server);
1030 server->mount_time = jiffies;
1031 nfs_free_fattr(fattr);
1035 nfs_free_fattr(fattr);
1036 nfs_free_server(server);
1037 return ERR_PTR(error);
1039 EXPORT_SYMBOL_GPL(nfs_create_server);
1042 * Clone an NFS2, NFS3 or NFS4 server record
1044 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1046 struct nfs_fattr *fattr,
1047 rpc_authflavor_t flavor)
1049 struct nfs_server *server;
1050 struct nfs_fattr *fattr_fsinfo;
1053 server = nfs_alloc_server();
1055 return ERR_PTR(-ENOMEM);
1057 server->cred = get_cred(source->cred);
1060 fattr_fsinfo = nfs_alloc_fattr();
1061 if (fattr_fsinfo == NULL)
1062 goto out_free_server;
1064 /* Copy data from the source */
1065 server->nfs_client = source->nfs_client;
1066 server->destroy = source->destroy;
1067 refcount_inc(&server->nfs_client->cl_count);
1068 nfs_server_copy_userdata(server, source);
1070 server->fsid = fattr->fsid;
1072 error = nfs_init_server_rpcclient(server,
1073 source->client->cl_timeout,
1076 goto out_free_server;
1078 /* probe the filesystem info for this server filesystem */
1079 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1081 goto out_free_server;
1083 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1084 server->namelen = NFS4_MAXNAMLEN;
1086 error = nfs_start_lockd(server);
1088 goto out_free_server;
1090 nfs_server_insert_lists(server);
1091 server->mount_time = jiffies;
1093 nfs_free_fattr(fattr_fsinfo);
1097 nfs_free_fattr(fattr_fsinfo);
1098 nfs_free_server(server);
1099 return ERR_PTR(error);
1101 EXPORT_SYMBOL_GPL(nfs_clone_server);
1103 void nfs_clients_init(struct net *net)
1105 struct nfs_net *nn = net_generic(net, nfs_net_id);
1107 INIT_LIST_HEAD(&nn->nfs_client_list);
1108 INIT_LIST_HEAD(&nn->nfs_volume_list);
1109 #if IS_ENABLED(CONFIG_NFS_V4)
1110 idr_init(&nn->cb_ident_idr);
1112 spin_lock_init(&nn->nfs_client_lock);
1113 nn->boot_time = ktime_get_real();
1114 memset(&nn->rpcstats, 0, sizeof(nn->rpcstats));
1115 nn->rpcstats.program = &nfs_program;
1117 nfs_netns_sysfs_setup(nn, net);
1120 void nfs_clients_exit(struct net *net)
1122 struct nfs_net *nn = net_generic(net, nfs_net_id);
1124 nfs_netns_sysfs_destroy(nn);
1125 nfs_cleanup_cb_ident_idr(net);
1126 WARN_ON_ONCE(!list_empty(&nn->nfs_client_list));
1127 WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list));
1130 #ifdef CONFIG_PROC_FS
1131 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1132 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1133 static void nfs_server_list_stop(struct seq_file *p, void *v);
1134 static int nfs_server_list_show(struct seq_file *m, void *v);
1136 static const struct seq_operations nfs_server_list_ops = {
1137 .start = nfs_server_list_start,
1138 .next = nfs_server_list_next,
1139 .stop = nfs_server_list_stop,
1140 .show = nfs_server_list_show,
1143 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1144 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1145 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1146 static int nfs_volume_list_show(struct seq_file *m, void *v);
1148 static const struct seq_operations nfs_volume_list_ops = {
1149 .start = nfs_volume_list_start,
1150 .next = nfs_volume_list_next,
1151 .stop = nfs_volume_list_stop,
1152 .show = nfs_volume_list_show,
1156 * set up the iterator to start reading from the server list and return the first item
1158 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1159 __acquires(&nn->nfs_client_lock)
1161 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1163 /* lock the list against modification */
1164 spin_lock(&nn->nfs_client_lock);
1165 return seq_list_start_head(&nn->nfs_client_list, *_pos);
1169 * move to next server
1171 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1173 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1175 return seq_list_next(v, &nn->nfs_client_list, pos);
1179 * clean up after reading from the transports list
1181 static void nfs_server_list_stop(struct seq_file *p, void *v)
1182 __releases(&nn->nfs_client_lock)
1184 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1186 spin_unlock(&nn->nfs_client_lock);
1190 * display a header line followed by a load of call lines
1192 static int nfs_server_list_show(struct seq_file *m, void *v)
1194 struct nfs_client *clp;
1195 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1197 /* display header on line 1 */
1198 if (v == &nn->nfs_client_list) {
1199 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1203 /* display one transport per line on subsequent lines */
1204 clp = list_entry(v, struct nfs_client, cl_share_link);
1206 /* Check if the client is initialized */
1207 if (clp->cl_cons_state != NFS_CS_READY)
1211 seq_printf(m, "v%u %s %s %3d %s\n",
1212 clp->rpc_ops->version,
1213 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1214 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1215 refcount_read(&clp->cl_count),
1223 * set up the iterator to start reading from the volume list and return the first item
1225 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1226 __acquires(&nn->nfs_client_lock)
1228 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1230 /* lock the list against modification */
1231 spin_lock(&nn->nfs_client_lock);
1232 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1236 * move to next volume
1238 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1240 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1242 return seq_list_next(v, &nn->nfs_volume_list, pos);
1246 * clean up after reading from the transports list
1248 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1249 __releases(&nn->nfs_client_lock)
1251 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1253 spin_unlock(&nn->nfs_client_lock);
1257 * display a header line followed by a load of call lines
1259 static int nfs_volume_list_show(struct seq_file *m, void *v)
1261 struct nfs_server *server;
1262 struct nfs_client *clp;
1263 char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1264 char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0'
1265 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1267 /* display header on line 1 */
1268 if (v == &nn->nfs_volume_list) {
1269 seq_puts(m, "NV SERVER PORT DEV FSID"
1273 /* display one transport per line on subsequent lines */
1274 server = list_entry(v, struct nfs_server, master_link);
1275 clp = server->nfs_client;
1277 snprintf(dev, sizeof(dev), "%u:%u",
1278 MAJOR(server->s_dev), MINOR(server->s_dev));
1280 snprintf(fsid, sizeof(fsid), "%llx:%llx",
1281 (unsigned long long) server->fsid.major,
1282 (unsigned long long) server->fsid.minor);
1285 seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
1286 clp->rpc_ops->version,
1287 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1288 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1291 nfs_server_fscache_state(server));
1297 int nfs_fs_proc_net_init(struct net *net)
1299 struct nfs_net *nn = net_generic(net, nfs_net_id);
1300 struct proc_dir_entry *p;
1302 nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
1303 if (!nn->proc_nfsfs)
1306 /* a file of servers with which we're dealing */
1307 p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1308 &nfs_server_list_ops, sizeof(struct seq_net_private));
1312 /* a file of volumes that we have mounted */
1313 p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs,
1314 &nfs_volume_list_ops, sizeof(struct seq_net_private));
1320 remove_proc_subtree("nfsfs", net->proc_net);
1325 void nfs_fs_proc_net_exit(struct net *net)
1327 remove_proc_subtree("nfsfs", net->proc_net);
1331 * initialise the /proc/fs/nfsfs/ directory
1333 int __init nfs_fs_proc_init(void)
1335 if (!proc_mkdir("fs/nfsfs", NULL))
1338 /* a file of servers with which we're dealing */
1339 if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
1342 /* a file of volumes that we have mounted */
1343 if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
1348 remove_proc_subtree("fs/nfsfs", NULL);
1354 * clean up the /proc/fs/nfsfs/ directory
1356 void nfs_fs_proc_exit(void)
1358 remove_proc_subtree("fs/nfsfs", NULL);
1361 #endif /* CONFIG_PROC_FS */