12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 224c8dbbbSDavid Howells /* client.c: NFS client sharing and management code 324c8dbbbSDavid Howells * 424c8dbbbSDavid Howells * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 524c8dbbbSDavid Howells * Written by David Howells (dhowells@redhat.com) 624c8dbbbSDavid Howells */ 724c8dbbbSDavid Howells 824c8dbbbSDavid Howells 924c8dbbbSDavid Howells #include <linux/module.h> 1024c8dbbbSDavid Howells #include <linux/init.h> 11e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 1224c8dbbbSDavid Howells #include <linux/time.h> 1324c8dbbbSDavid Howells #include <linux/kernel.h> 1424c8dbbbSDavid Howells #include <linux/mm.h> 1524c8dbbbSDavid Howells #include <linux/string.h> 1624c8dbbbSDavid Howells #include <linux/stat.h> 1724c8dbbbSDavid Howells #include <linux/errno.h> 1824c8dbbbSDavid Howells #include <linux/unistd.h> 19d8efa4e6SAnna Schumaker #include <linux/sunrpc/addr.h> 2024c8dbbbSDavid Howells #include <linux/sunrpc/clnt.h> 2124c8dbbbSDavid Howells #include <linux/sunrpc/stats.h> 2224c8dbbbSDavid Howells #include <linux/sunrpc/metrics.h> 230896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 242cf7ff7aS\"Talpey, Thomas\ #include <linux/sunrpc/xprtrdma.h> 2524c8dbbbSDavid Howells #include <linux/nfs_fs.h> 2624c8dbbbSDavid Howells #include <linux/nfs_mount.h> 2724c8dbbbSDavid Howells #include <linux/nfs4_mount.h> 2824c8dbbbSDavid Howells #include <linux/lockd/bind.h> 2924c8dbbbSDavid Howells #include <linux/seq_file.h> 3024c8dbbbSDavid Howells #include <linux/mount.h> 3124c8dbbbSDavid Howells #include <linux/vfs.h> 3224c8dbbbSDavid Howells #include <linux/inet.h> 333b0d3f93STrond Myklebust #include <linux/in6.h> 345a0e3ad6STejun Heo #include <linux/slab.h> 3540401530SAl Viro #include <linux/idr.h> 363b0d3f93STrond Myklebust #include <net/ipv6.h> 3724c8dbbbSDavid Howells #include <linux/nfs_xdr.h> 380b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h> 396b13168bSStanislav Kinsbursky #include <linux/nsproxy.h> 406b13168bSStanislav Kinsbursky #include <linux/pid_namespace.h> 4124c8dbbbSDavid Howells 4224c8dbbbSDavid Howells 4324c8dbbbSDavid Howells #include "nfs4_fs.h" 4424c8dbbbSDavid Howells #include "callback.h" 4524c8dbbbSDavid Howells #include "delegation.h" 4624c8dbbbSDavid Howells #include "iostat.h" 4724c8dbbbSDavid Howells #include "internal.h" 4814727281SDavid Howells #include "fscache.h" 4985e174baSRicardo Labiaga #include "pnfs.h" 50ab7017a3SBryan Schumaker #include "nfs.h" 516b13168bSStanislav Kinsbursky #include "netns.h" 52bf11fbdbSTrond Myklebust #include "sysfs.h" 5304a5da69SFrank van der Linden #include "nfs42.h" 5424c8dbbbSDavid Howells 5524c8dbbbSDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 5624c8dbbbSDavid Howells 5724c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); 58ab7017a3SBryan Schumaker static DEFINE_SPINLOCK(nfs_version_lock); 59ab7017a3SBryan Schumaker static DEFINE_MUTEX(nfs_version_mutex); 60ab7017a3SBryan Schumaker static LIST_HEAD(nfs_versions); 6124c8dbbbSDavid Howells 6224c8dbbbSDavid Howells /* 635006a76cSDavid Howells * RPC cruft for NFS 645006a76cSDavid Howells */ 65a613fa16STrond Myklebust static const struct rpc_version *nfs_version[5] = { 66ab7017a3SBryan Schumaker [2] = NULL, 67ab7017a3SBryan Schumaker [3] = NULL, 68ab7017a3SBryan Schumaker [4] = NULL, 695006a76cSDavid Howells }; 705006a76cSDavid Howells 71a613fa16STrond Myklebust const struct rpc_program nfs_program = { 725006a76cSDavid Howells .name = "nfs", 735006a76cSDavid Howells .number = NFS_PROGRAM, 745006a76cSDavid Howells .nrvers = ARRAY_SIZE(nfs_version), 755006a76cSDavid Howells .version = nfs_version, 765006a76cSDavid Howells .stats = &nfs_rpcstat, 77fe0a9b74SJim Rees .pipe_dir_name = NFS_PIPE_DIRNAME, 785006a76cSDavid Howells }; 795006a76cSDavid Howells 805006a76cSDavid Howells struct rpc_stat nfs_rpcstat = { 815006a76cSDavid Howells .program = &nfs_program 825006a76cSDavid Howells }; 835006a76cSDavid Howells 84ab7017a3SBryan Schumaker static struct nfs_subversion *find_nfs_version(unsigned int version) 85ab7017a3SBryan Schumaker { 86ab7017a3SBryan Schumaker struct nfs_subversion *nfs; 87ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 88ab7017a3SBryan Schumaker 89ab7017a3SBryan Schumaker list_for_each_entry(nfs, &nfs_versions, list) { 90ab7017a3SBryan Schumaker if (nfs->rpc_ops->version == version) { 91ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 92ab7017a3SBryan Schumaker return nfs; 93ab7017a3SBryan Schumaker } 94ee34e136SYanchuan Nian } 95ab7017a3SBryan Schumaker 96ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 97ee34e136SYanchuan Nian return ERR_PTR(-EPROTONOSUPPORT); 98ab7017a3SBryan Schumaker } 99ab7017a3SBryan Schumaker 100ab7017a3SBryan Schumaker struct nfs_subversion *get_nfs_version(unsigned int version) 101ab7017a3SBryan Schumaker { 102ab7017a3SBryan Schumaker struct nfs_subversion *nfs = find_nfs_version(version); 103ab7017a3SBryan Schumaker 104ab7017a3SBryan Schumaker if (IS_ERR(nfs)) { 105ab7017a3SBryan Schumaker mutex_lock(&nfs_version_mutex); 1061ae811eeSbjschuma@gmail.com request_module("nfsv%d", version); 107ab7017a3SBryan Schumaker nfs = find_nfs_version(version); 108ab7017a3SBryan Schumaker mutex_unlock(&nfs_version_mutex); 109ab7017a3SBryan Schumaker } 110ab7017a3SBryan Schumaker 1111f70ef96SAlexey Khoroshilov if (!IS_ERR(nfs) && !try_module_get(nfs->owner)) 1121f70ef96SAlexey Khoroshilov return ERR_PTR(-EAGAIN); 113ab7017a3SBryan Schumaker return nfs; 114ab7017a3SBryan Schumaker } 115ab7017a3SBryan Schumaker 116ab7017a3SBryan Schumaker void put_nfs_version(struct nfs_subversion *nfs) 117ab7017a3SBryan Schumaker { 118ab7017a3SBryan Schumaker module_put(nfs->owner); 119ab7017a3SBryan Schumaker } 120ab7017a3SBryan Schumaker 121ab7017a3SBryan Schumaker void register_nfs_version(struct nfs_subversion *nfs) 122ab7017a3SBryan Schumaker { 123ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 124ab7017a3SBryan Schumaker 125ab7017a3SBryan Schumaker list_add(&nfs->list, &nfs_versions); 126ab7017a3SBryan Schumaker nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers; 127ab7017a3SBryan Schumaker 128ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 129ab7017a3SBryan Schumaker } 130ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(register_nfs_version); 131ab7017a3SBryan Schumaker 132ab7017a3SBryan Schumaker void unregister_nfs_version(struct nfs_subversion *nfs) 133ab7017a3SBryan Schumaker { 134ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 135ab7017a3SBryan Schumaker 136ab7017a3SBryan Schumaker nfs_version[nfs->rpc_ops->version] = NULL; 137ab7017a3SBryan Schumaker list_del(&nfs->list); 138ab7017a3SBryan Schumaker 139ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 140ab7017a3SBryan Schumaker } 141ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(unregister_nfs_version); 142ab7017a3SBryan Schumaker 143ab7017a3SBryan Schumaker /* 14424c8dbbbSDavid Howells * Allocate a shared client record 14524c8dbbbSDavid Howells * 14624c8dbbbSDavid Howells * Since these are allocated/deallocated very rarely, we don't 14724c8dbbbSDavid Howells * bother putting them in a slab cache... 14824c8dbbbSDavid Howells */ 1496663ee7fSBryan Schumaker struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) 15024c8dbbbSDavid Howells { 15124c8dbbbSDavid Howells struct nfs_client *clp; 152a21bdd9bSChuck Lever int err = -ENOMEM; 15324c8dbbbSDavid Howells 15424c8dbbbSDavid Howells if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) 15524c8dbbbSDavid Howells goto error_0; 15624c8dbbbSDavid Howells 15755dee1bcSScott Mayhew clp->cl_minorversion = cl_init->minorversion; 158ab7017a3SBryan Schumaker clp->cl_nfs_mod = cl_init->nfs_mod; 1591f70ef96SAlexey Khoroshilov if (!try_module_get(clp->cl_nfs_mod->owner)) 1601f70ef96SAlexey Khoroshilov goto error_dealloc; 161ab7017a3SBryan Schumaker 162ab7017a3SBryan Schumaker clp->rpc_ops = clp->cl_nfs_mod->rpc_ops; 16340c55319STrond Myklebust 164212bf41dSElena Reshetova refcount_set(&clp->cl_count, 1); 16524c8dbbbSDavid Howells clp->cl_cons_state = NFS_CS_INITING; 16624c8dbbbSDavid Howells 1676e4cffd7SChuck Lever memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); 1686e4cffd7SChuck Lever clp->cl_addrlen = cl_init->addrlen; 16924c8dbbbSDavid Howells 1703a498026STrond Myklebust if (cl_init->hostname) { 171a21bdd9bSChuck Lever err = -ENOMEM; 1723a498026STrond Myklebust clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); 17324c8dbbbSDavid Howells if (!clp->cl_hostname) 17471468513SBenny Halevy goto error_cleanup; 17524c8dbbbSDavid Howells } 17624c8dbbbSDavid Howells 17724c8dbbbSDavid Howells INIT_LIST_HEAD(&clp->cl_superblocks); 17824c8dbbbSDavid Howells clp->cl_rpcclient = ERR_PTR(-EINVAL); 17924c8dbbbSDavid Howells 18059dca3b2STrond Myklebust clp->cl_proto = cl_init->proto; 1816619079dSTrond Myklebust clp->cl_nconnect = cl_init->nconnect; 18273ea666cSChuck Lever clp->cl_net = get_net(cl_init->net); 18359dca3b2STrond Myklebust 1845e16923bSNeilBrown clp->cl_principal = "*"; 18514727281SDavid Howells nfs_fscache_get_client_cookie(clp); 18614727281SDavid Howells 18724c8dbbbSDavid Howells return clp; 18824c8dbbbSDavid Howells 18971468513SBenny Halevy error_cleanup: 190ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 1911f70ef96SAlexey Khoroshilov error_dealloc: 19224c8dbbbSDavid Howells kfree(clp); 19324c8dbbbSDavid Howells error_0: 194a21bdd9bSChuck Lever return ERR_PTR(err); 19524c8dbbbSDavid Howells } 196ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_client); 19724c8dbbbSDavid Howells 19889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 19910b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net) 200f4eecd5dSAndy Adamson { 20128cd1b3fSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 20228cd1b3fSStanislav Kinsbursky 20328cd1b3fSStanislav Kinsbursky idr_destroy(&nn->cb_ident_idr); 204f4eecd5dSAndy Adamson } 205f4eecd5dSAndy Adamson 206f4eecd5dSAndy Adamson /* nfs_client_lock held */ 207f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 208f4eecd5dSAndy Adamson { 20973ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 21028cd1b3fSStanislav Kinsbursky 211f4eecd5dSAndy Adamson if (clp->cl_cb_ident) 21228cd1b3fSStanislav Kinsbursky idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident); 213f4eecd5dSAndy Adamson } 214f4eecd5dSAndy Adamson 215f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 216f7e8917aSFred Isaman { 217f7e8917aSFred Isaman rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 218f7e8917aSFred Isaman } 219f7e8917aSFred Isaman 220888ef2e3SAlexandros Batsakis #else 22110b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net) 222f4eecd5dSAndy Adamson { 223f4eecd5dSAndy Adamson } 224f4eecd5dSAndy Adamson 225f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 226f4eecd5dSAndy Adamson { 227f4eecd5dSAndy Adamson } 228f7e8917aSFred Isaman 229f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 230f7e8917aSFred Isaman { 231f7e8917aSFred Isaman } 232f7e8917aSFred Isaman 233888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */ 234888ef2e3SAlexandros Batsakis 235888ef2e3SAlexandros Batsakis /* 2365dd3177aSTrond Myklebust * Destroy a shared client record 2375dd3177aSTrond Myklebust */ 238cdb7ecedSBryan Schumaker void nfs_free_client(struct nfs_client *clp) 2395dd3177aSTrond Myklebust { 24014727281SDavid Howells nfs_fscache_release_client_cookie(clp); 24114727281SDavid Howells 24224c8dbbbSDavid Howells /* -EIO all pending I/O */ 24324c8dbbbSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 24424c8dbbbSDavid Howells rpc_shutdown_client(clp->cl_rpcclient); 24524c8dbbbSDavid Howells 24673ea666cSChuck Lever put_net(clp->cl_net); 247ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 24824c8dbbbSDavid Howells kfree(clp->cl_hostname); 249f11b2a1cSJeff Layton kfree(clp->cl_acceptor); 25024c8dbbbSDavid Howells kfree(clp); 25124c8dbbbSDavid Howells } 252ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_client); 25324c8dbbbSDavid Howells 25424c8dbbbSDavid Howells /* 25524c8dbbbSDavid Howells * Release a reference to a shared client record 25624c8dbbbSDavid Howells */ 25724c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp) 25824c8dbbbSDavid Howells { 259dc030858SStanislav Kinsbursky struct nfs_net *nn; 260dc030858SStanislav Kinsbursky 26127ba8512SDavid Howells if (!clp) 26227ba8512SDavid Howells return; 26327ba8512SDavid Howells 26473ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 26524c8dbbbSDavid Howells 266212bf41dSElena Reshetova if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) { 26724c8dbbbSDavid Howells list_del(&clp->cl_share_link); 268f4eecd5dSAndy Adamson nfs_cb_idr_remove_locked(clp); 269dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 27024c8dbbbSDavid Howells 2711fea73a8STrond Myklebust WARN_ON_ONCE(!list_empty(&clp->cl_superblocks)); 27224c8dbbbSDavid Howells 273cdb7ecedSBryan Schumaker clp->rpc_ops->free_client(clp); 27424c8dbbbSDavid Howells } 27524c8dbbbSDavid Howells } 27616b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client); 27724c8dbbbSDavid Howells 2783fbd67adSTrond Myklebust /* 279c81468a1STrond Myklebust * Find an nfs_client on the list that matches the initialisation data 280c81468a1STrond Myklebust * that is supplied. 281c81468a1STrond Myklebust */ 282c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) 28324c8dbbbSDavid Howells { 28424c8dbbbSDavid Howells struct nfs_client *clp; 285d7371c41SIan Dall const struct sockaddr *sap = data->addr; 2866b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(data->net, nfs_net_id); 287950a578cSRoberto Bergantinos Corpas int error; 28824c8dbbbSDavid Howells 289c156618eSScott Mayhew again: 2906b13168bSStanislav Kinsbursky list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { 291d7371c41SIan Dall const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 29213bbc06aSTrond Myklebust /* Don't match clients that failed to initialise properly */ 29313bbc06aSTrond Myklebust if (clp->cl_cons_state < 0) 29413bbc06aSTrond Myklebust continue; 29513bbc06aSTrond Myklebust 296c156618eSScott Mayhew /* If a client is still initializing then we need to wait */ 297c156618eSScott Mayhew if (clp->cl_cons_state > NFS_CS_READY) { 298c156618eSScott Mayhew refcount_inc(&clp->cl_count); 299c156618eSScott Mayhew spin_unlock(&nn->nfs_client_lock); 300950a578cSRoberto Bergantinos Corpas error = nfs_wait_client_init_complete(clp); 301c156618eSScott Mayhew nfs_put_client(clp); 302c260121aSBenjamin Coddington spin_lock(&nn->nfs_client_lock); 303950a578cSRoberto Bergantinos Corpas if (error < 0) 304950a578cSRoberto Bergantinos Corpas return ERR_PTR(error); 305c156618eSScott Mayhew goto again; 306c156618eSScott Mayhew } 307c156618eSScott Mayhew 30824c8dbbbSDavid Howells /* Different NFS versions cannot share the same nfs_client */ 309ab7017a3SBryan Schumaker if (clp->rpc_ops != data->nfs_mod->rpc_ops) 31024c8dbbbSDavid Howells continue; 31124c8dbbbSDavid Howells 31259dca3b2STrond Myklebust if (clp->cl_proto != data->proto) 31359dca3b2STrond Myklebust continue; 3145aae4a9aSBenny Halevy /* Match nfsv4 minorversion */ 3155aae4a9aSBenny Halevy if (clp->cl_minorversion != data->minorversion) 3165aae4a9aSBenny Halevy continue; 31752f98f1aSTrond Myklebust 31852f98f1aSTrond Myklebust /* Match request for a dedicated DS */ 31952f98f1aSTrond Myklebust if (test_bit(NFS_CS_DS, &data->init_flags) != 32052f98f1aSTrond Myklebust test_bit(NFS_CS_DS, &clp->cl_flags)) 32152f98f1aSTrond Myklebust continue; 32252f98f1aSTrond Myklebust 323c81468a1STrond Myklebust /* Match the full socket address */ 324d8efa4e6SAnna Schumaker if (!rpc_cmp_addr_port(sap, clap)) 32504ea1b3eSAndy Adamson /* Match all xprt_switch full socket addresses */ 3268ef32955SPetr Vandrovec if (IS_ERR(clp->cl_rpcclient) || 3278ef32955SPetr Vandrovec !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient, 32804ea1b3eSAndy Adamson sap)) 32924c8dbbbSDavid Howells continue; 33024c8dbbbSDavid Howells 331212bf41dSElena Reshetova refcount_inc(&clp->cl_count); 33224c8dbbbSDavid Howells return clp; 33324c8dbbbSDavid Howells } 334c81468a1STrond Myklebust return NULL; 33524c8dbbbSDavid Howells } 33624c8dbbbSDavid Howells 337a33e4b03SWeston Andros Adamson /* 338a33e4b03SWeston Andros Adamson * Return true if @clp is done initializing, false if still working on it. 339a33e4b03SWeston Andros Adamson * 340a33e4b03SWeston Andros Adamson * Use nfs_client_init_status to check if it was successful. 341a33e4b03SWeston Andros Adamson */ 342a33e4b03SWeston Andros Adamson bool nfs_client_init_is_complete(const struct nfs_client *clp) 3434697bd5eSTrond Myklebust { 34448d66b97STrond Myklebust return clp->cl_cons_state <= NFS_CS_READY; 3454697bd5eSTrond Myklebust } 346a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_is_complete); 347a33e4b03SWeston Andros Adamson 348a33e4b03SWeston Andros Adamson /* 349a33e4b03SWeston Andros Adamson * Return 0 if @clp was successfully initialized, -errno otherwise. 350a33e4b03SWeston Andros Adamson * 351a33e4b03SWeston Andros Adamson * This must be called *after* nfs_client_init_is_complete() returns true, 352a33e4b03SWeston Andros Adamson * otherwise it will pop WARN_ON_ONCE and return -EINVAL 353a33e4b03SWeston Andros Adamson */ 354a33e4b03SWeston Andros Adamson int nfs_client_init_status(const struct nfs_client *clp) 355a33e4b03SWeston Andros Adamson { 356a33e4b03SWeston Andros Adamson /* called without checking nfs_client_init_is_complete */ 357a33e4b03SWeston Andros Adamson if (clp->cl_cons_state > NFS_CS_READY) { 358a33e4b03SWeston Andros Adamson WARN_ON_ONCE(1); 359a33e4b03SWeston Andros Adamson return -EINVAL; 360a33e4b03SWeston Andros Adamson } 361a33e4b03SWeston Andros Adamson return clp->cl_cons_state; 362a33e4b03SWeston Andros Adamson } 363a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_status); 3644697bd5eSTrond Myklebust 3654697bd5eSTrond Myklebust int nfs_wait_client_init_complete(const struct nfs_client *clp) 3664697bd5eSTrond Myklebust { 3674697bd5eSTrond Myklebust return wait_event_killable(nfs_client_active_wq, 3684697bd5eSTrond Myklebust nfs_client_init_is_complete(clp)); 3694697bd5eSTrond Myklebust } 37089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete); 3714697bd5eSTrond Myklebust 37224c8dbbbSDavid Howells /* 373f411703aSChuck Lever * Found an existing client. Make sure it's ready before returning. 374f411703aSChuck Lever */ 375f411703aSChuck Lever static struct nfs_client * 376f411703aSChuck Lever nfs_found_client(const struct nfs_client_initdata *cl_init, 377f411703aSChuck Lever struct nfs_client *clp) 378f411703aSChuck Lever { 379f411703aSChuck Lever int error; 380f411703aSChuck Lever 3814697bd5eSTrond Myklebust error = nfs_wait_client_init_complete(clp); 382f411703aSChuck Lever if (error < 0) { 383f411703aSChuck Lever nfs_put_client(clp); 384f411703aSChuck Lever return ERR_PTR(-ERESTARTSYS); 385f411703aSChuck Lever } 386f411703aSChuck Lever 387f411703aSChuck Lever if (clp->cl_cons_state < NFS_CS_READY) { 388f411703aSChuck Lever error = clp->cl_cons_state; 389f411703aSChuck Lever nfs_put_client(clp); 390f411703aSChuck Lever return ERR_PTR(error); 391f411703aSChuck Lever } 392f411703aSChuck Lever 39354ac471cSTrond Myklebust smp_rmb(); 394f411703aSChuck Lever return clp; 395f411703aSChuck Lever } 396f411703aSChuck Lever 397f411703aSChuck Lever /* 39824c8dbbbSDavid Howells * Look up a client by IP address and protocol version 39924c8dbbbSDavid Howells * - creates a new record if one doesn't yet exist 40024c8dbbbSDavid Howells */ 4017d38de3fSAnna Schumaker struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) 40224c8dbbbSDavid Howells { 40324c8dbbbSDavid Howells struct nfs_client *clp, *new = NULL; 4046b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id); 405ab7017a3SBryan Schumaker const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops; 40624c8dbbbSDavid Howells 40731434f49SPeng Tao if (cl_init->hostname == NULL) { 40831434f49SPeng Tao WARN_ON(1); 40931434f49SPeng Tao return NULL; 41031434f49SPeng Tao } 41131434f49SPeng Tao 41224c8dbbbSDavid Howells /* see if the client already exists */ 41324c8dbbbSDavid Howells do { 414dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 41524c8dbbbSDavid Howells 416c81468a1STrond Myklebust clp = nfs_match_client(cl_init); 417f411703aSChuck Lever if (clp) { 418f411703aSChuck Lever spin_unlock(&nn->nfs_client_lock); 419f411703aSChuck Lever if (new) 420cdb7ecedSBryan Schumaker new->rpc_ops->free_client(new); 4219f7761cfSBenjamin Coddington if (IS_ERR(clp)) 4229f7761cfSBenjamin Coddington return clp; 423f411703aSChuck Lever return nfs_found_client(cl_init, clp); 424f411703aSChuck Lever } 4258cab4c39SChuck Lever if (new) { 42605f4c350SChuck Lever list_add_tail(&new->cl_share_link, 42705f4c350SChuck Lever &nn->nfs_client_list); 4288cab4c39SChuck Lever spin_unlock(&nn->nfs_client_lock); 4294bf590e0SChuck Lever new->cl_flags = cl_init->init_flags; 4305c6e5b60STrond Myklebust return rpc_ops->init_client(new, cl_init); 4318cab4c39SChuck Lever } 43224c8dbbbSDavid Howells 433dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 43424c8dbbbSDavid Howells 435ab7017a3SBryan Schumaker new = rpc_ops->alloc_client(cl_init); 436a21bdd9bSChuck Lever } while (!IS_ERR(new)); 43724c8dbbbSDavid Howells 438a21bdd9bSChuck Lever return new; 43924c8dbbbSDavid Howells } 44089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_get_client); 44124c8dbbbSDavid Howells 44224c8dbbbSDavid Howells /* 44324c8dbbbSDavid Howells * Mark a server as ready or failed 44424c8dbbbSDavid Howells */ 44576db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state) 44624c8dbbbSDavid Howells { 44754ac471cSTrond Myklebust smp_wmb(); 44824c8dbbbSDavid Howells clp->cl_cons_state = state; 44924c8dbbbSDavid Howells wake_up_all(&nfs_client_active_wq); 45024c8dbbbSDavid Howells } 45189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mark_client_ready); 4525006a76cSDavid Howells 4535006a76cSDavid Howells /* 4545006a76cSDavid Howells * Initialise the timeout values for a connection 4555006a76cSDavid Howells */ 456fcf10398SBryan Schumaker void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 457a956bedaSTrond Myklebust int timeo, int retrans) 4585006a76cSDavid Howells { 4595006a76cSDavid Howells to->to_initval = timeo * HZ / 10; 4605006a76cSDavid Howells to->to_retries = retrans; 4615006a76cSDavid Howells 4625006a76cSDavid Howells switch (proto) { 4630896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_TCP: 4642cf7ff7aS\"Talpey, Thomas\ case XPRT_TRANSPORT_RDMA: 465a956bedaSTrond Myklebust if (retrans == NFS_UNSPEC_RETRANS) 466259875efSTrond Myklebust to->to_retries = NFS_DEF_TCP_RETRANS; 4675a698243STrond Myklebust if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 468259875efSTrond Myklebust to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; 4695006a76cSDavid Howells if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 4705006a76cSDavid Howells to->to_initval = NFS_MAX_TCP_TIMEOUT; 4715006a76cSDavid Howells to->to_increment = to->to_initval; 4725006a76cSDavid Howells to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); 4737a3e3e18STrond Myklebust if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) 4747a3e3e18STrond Myklebust to->to_maxval = NFS_MAX_TCP_TIMEOUT; 4757a3e3e18STrond Myklebust if (to->to_maxval < to->to_initval) 4767a3e3e18STrond Myklebust to->to_maxval = to->to_initval; 4775006a76cSDavid Howells to->to_exponential = 0; 4785006a76cSDavid Howells break; 479b24ee6c6SOlga Kornievskaia #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT 4800896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_UDP: 481a956bedaSTrond Myklebust if (retrans == NFS_UNSPEC_RETRANS) 482259875efSTrond Myklebust to->to_retries = NFS_DEF_UDP_RETRANS; 483a956bedaSTrond Myklebust if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 484259875efSTrond Myklebust to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; 4855006a76cSDavid Howells if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 4865006a76cSDavid Howells to->to_initval = NFS_MAX_UDP_TIMEOUT; 4875006a76cSDavid Howells to->to_maxval = NFS_MAX_UDP_TIMEOUT; 4885006a76cSDavid Howells to->to_exponential = 1; 4895006a76cSDavid Howells break; 490b24ee6c6SOlga Kornievskaia #endif 491259875efSTrond Myklebust default: 492259875efSTrond Myklebust BUG(); 4935006a76cSDavid Howells } 4945006a76cSDavid Howells } 49589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_timeout_values); 4965006a76cSDavid Howells 4975006a76cSDavid Howells /* 4985006a76cSDavid Howells * Create an RPC client handle 4995006a76cSDavid Howells */ 500428360d7SBryan Schumaker int nfs_create_rpc_client(struct nfs_client *clp, 5015c6e5b60STrond Myklebust const struct nfs_client_initdata *cl_init, 5024bf590e0SChuck Lever rpc_authflavor_t flavor) 5035006a76cSDavid Howells { 5045006a76cSDavid Howells struct rpc_clnt *clnt = NULL; 50541877d20SChuck Lever struct rpc_create_args args = { 50673ea666cSChuck Lever .net = clp->cl_net, 50759dca3b2STrond Myklebust .protocol = clp->cl_proto, 5086619079dSTrond Myklebust .nconnect = clp->cl_nconnect, 50941877d20SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 5106e4cffd7SChuck Lever .addrsize = clp->cl_addrlen, 5115c6e5b60STrond Myklebust .timeout = cl_init->timeparms, 51241877d20SChuck Lever .servername = clp->cl_hostname, 5135c6e5b60STrond Myklebust .nodename = cl_init->nodename, 51441877d20SChuck Lever .program = &nfs_program, 51541877d20SChuck Lever .version = clp->rpc_ops->version, 51641877d20SChuck Lever .authflavor = flavor, 5171a58e8a0STrond Myklebust .cred = cl_init->cred, 51841877d20SChuck Lever }; 5195006a76cSDavid Howells 5204bf590e0SChuck Lever if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags)) 5214a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_DISCRTRY; 52299875249STrond Myklebust if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags)) 52399875249STrond Myklebust args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT; 5244bf590e0SChuck Lever if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags)) 5254a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; 52698f98cf5STrond Myklebust if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags)) 52798f98cf5STrond Myklebust args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS; 5284b1b69ceSTrond Myklebust if (test_bit(NFS_CS_NOPING, &clp->cl_flags)) 5294b1b69ceSTrond Myklebust args.flags |= RPC_CLNT_CREATE_NOPING; 530e6237b6fSTrond Myklebust if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags)) 531e6237b6fSTrond Myklebust args.flags |= RPC_CLNT_CREATE_REUSEPORT; 5324a01b8a4SChuck Lever 5335006a76cSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 5345006a76cSDavid Howells return 0; 5355006a76cSDavid Howells 53641877d20SChuck Lever clnt = rpc_create(&args); 5375006a76cSDavid Howells if (IS_ERR(clnt)) { 5385006a76cSDavid Howells dprintk("%s: cannot create RPC client. Error = %ld\n", 5393110ff80SHarvey Harrison __func__, PTR_ERR(clnt)); 5405006a76cSDavid Howells return PTR_ERR(clnt); 5415006a76cSDavid Howells } 5425006a76cSDavid Howells 5435e16923bSNeilBrown clnt->cl_principal = clp->cl_principal; 5445006a76cSDavid Howells clp->cl_rpcclient = clnt; 5455006a76cSDavid Howells return 0; 5465006a76cSDavid Howells } 54789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_rpc_client); 54854ceac45SDavid Howells 54954ceac45SDavid Howells /* 55054ceac45SDavid Howells * Version 2 or 3 client destruction 55154ceac45SDavid Howells */ 55254ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server) 55354ceac45SDavid Howells { 554f259613aSNeilBrown if (server->nlm_host) 5559289e7f9SChuck Lever nlmclnt_done(server->nlm_host); 55654ceac45SDavid Howells } 55754ceac45SDavid Howells 55854ceac45SDavid Howells /* 55954ceac45SDavid Howells * Version 2 or 3 lockd setup 56054ceac45SDavid Howells */ 56154ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server) 56254ceac45SDavid Howells { 5639289e7f9SChuck Lever struct nlm_host *host; 5649289e7f9SChuck Lever struct nfs_client *clp = server->nfs_client; 565883bb163SChuck Lever struct nlmclnt_initdata nlm_init = { 566883bb163SChuck Lever .hostname = clp->cl_hostname, 567883bb163SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 568883bb163SChuck Lever .addrlen = clp->cl_addrlen, 569883bb163SChuck Lever .nfs_version = clp->rpc_ops->version, 5700cb2659bSChuck Lever .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? 5710cb2659bSChuck Lever 1 : 0, 57273ea666cSChuck Lever .net = clp->cl_net, 573b1ece737SBenjamin Coddington .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops, 57440373b12STrond Myklebust .cred = current_cred(), 575883bb163SChuck Lever }; 57654ceac45SDavid Howells 577883bb163SChuck Lever if (nlm_init.nfs_version > 3) 5789289e7f9SChuck Lever return 0; 5795eebde23SSuresh Jayaraman if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) && 5805eebde23SSuresh Jayaraman (server->flags & NFS_MOUNT_LOCAL_FCNTL)) 5819289e7f9SChuck Lever return 0; 5829289e7f9SChuck Lever 5838a6e5debSTrond Myklebust switch (clp->cl_proto) { 5848a6e5debSTrond Myklebust default: 5858a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_TCP; 5868a6e5debSTrond Myklebust break; 587b24ee6c6SOlga Kornievskaia #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT 5888a6e5debSTrond Myklebust case XPRT_TRANSPORT_UDP: 5898a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_UDP; 590b24ee6c6SOlga Kornievskaia #endif 5918a6e5debSTrond Myklebust } 5928a6e5debSTrond Myklebust 593883bb163SChuck Lever host = nlmclnt_init(&nlm_init); 5949289e7f9SChuck Lever if (IS_ERR(host)) 5959289e7f9SChuck Lever return PTR_ERR(host); 5969289e7f9SChuck Lever 5979289e7f9SChuck Lever server->nlm_host = host; 59854ceac45SDavid Howells server->destroy = nfs_destroy_server; 5999289e7f9SChuck Lever return 0; 60054ceac45SDavid Howells } 60154ceac45SDavid Howells 60254ceac45SDavid Howells /* 60354ceac45SDavid Howells * Create a general RPC client 60454ceac45SDavid Howells */ 605fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *server, 60633170233STrond Myklebust const struct rpc_timeout *timeo, 60733170233STrond Myklebust rpc_authflavor_t pseudoflavour) 60854ceac45SDavid Howells { 60954ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 61054ceac45SDavid Howells 611ba9b584cSChuck Lever server->client = rpc_clone_client_set_auth(clp->cl_rpcclient, 612ba9b584cSChuck Lever pseudoflavour); 61354ceac45SDavid Howells if (IS_ERR(server->client)) { 6143110ff80SHarvey Harrison dprintk("%s: couldn't create rpc_client!\n", __func__); 61554ceac45SDavid Howells return PTR_ERR(server->client); 61654ceac45SDavid Howells } 61754ceac45SDavid Howells 61833170233STrond Myklebust memcpy(&server->client->cl_timeout_default, 61933170233STrond Myklebust timeo, 62033170233STrond Myklebust sizeof(server->client->cl_timeout_default)); 62133170233STrond Myklebust server->client->cl_timeout = &server->client->cl_timeout_default; 62254ceac45SDavid Howells server->client->cl_softrtry = 0; 62391a575e1STrond Myklebust if (server->flags & NFS_MOUNT_SOFTERR) 62491a575e1STrond Myklebust server->client->cl_softerr = 1; 62554ceac45SDavid Howells if (server->flags & NFS_MOUNT_SOFT) 62654ceac45SDavid Howells server->client->cl_softrtry = 1; 62754ceac45SDavid Howells 62854ceac45SDavid Howells return 0; 62954ceac45SDavid Howells } 63089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient); 63154ceac45SDavid Howells 6328cab4c39SChuck Lever /** 6338cab4c39SChuck Lever * nfs_init_client - Initialise an NFS2 or NFS3 client 6348cab4c39SChuck Lever * 6358cab4c39SChuck Lever * @clp: nfs_client to initialise 6365c6e5b60STrond Myklebust * @cl_init: Initialisation parameters 6378cab4c39SChuck Lever * 6388cab4c39SChuck Lever * Returns pointer to an NFS client, or an ERR_PTR value. 63954ceac45SDavid Howells */ 6408cab4c39SChuck Lever struct nfs_client *nfs_init_client(struct nfs_client *clp, 6415c6e5b60STrond Myklebust const struct nfs_client_initdata *cl_init) 64254ceac45SDavid Howells { 64354ceac45SDavid Howells int error; 64454ceac45SDavid Howells 64554ceac45SDavid Howells /* the client is already initialised */ 6462844b6aeSAnna Schumaker if (clp->cl_cons_state == NFS_CS_READY) 6478cab4c39SChuck Lever return clp; 64854ceac45SDavid Howells 64954ceac45SDavid Howells /* 65054ceac45SDavid Howells * Create a client RPC handle for doing FSSTAT with UNIX auth only 65154ceac45SDavid Howells * - RFC 2623, sec 2.3.2 65254ceac45SDavid Howells */ 6535c6e5b60STrond Myklebust error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX); 6542844b6aeSAnna Schumaker nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error); 6552844b6aeSAnna Schumaker if (error < 0) { 6568cab4c39SChuck Lever nfs_put_client(clp); 6572844b6aeSAnna Schumaker clp = ERR_PTR(error); 6582844b6aeSAnna Schumaker } 6592844b6aeSAnna Schumaker return clp; 66054ceac45SDavid Howells } 661ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_client); 66254ceac45SDavid Howells 66354ceac45SDavid Howells /* 66454ceac45SDavid Howells * Create a version 2 or 3 client 66554ceac45SDavid Howells */ 6662283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server, 66762a55d08SScott Mayhew const struct fs_context *fc) 66854ceac45SDavid Howells { 66962a55d08SScott Mayhew const struct nfs_fs_context *ctx = nfs_fc2context(fc); 6705c6e5b60STrond Myklebust struct rpc_timeout timeparms; 6713a498026STrond Myklebust struct nfs_client_initdata cl_init = { 67238465f5dSScott Mayhew .hostname = ctx->nfs_server.hostname, 67338465f5dSScott Mayhew .addr = (const struct sockaddr *)&ctx->nfs_server.address, 67438465f5dSScott Mayhew .addrlen = ctx->nfs_server.addrlen, 67562a55d08SScott Mayhew .nfs_mod = ctx->nfs_mod, 67638465f5dSScott Mayhew .proto = ctx->nfs_server.protocol, 67762a55d08SScott Mayhew .net = fc->net_ns, 6785c6e5b60STrond Myklebust .timeparms = &timeparms, 6791a58e8a0STrond Myklebust .cred = server->cred, 68038465f5dSScott Mayhew .nconnect = ctx->nfs_server.nconnect, 681e6237b6fSTrond Myklebust .init_flags = (1UL << NFS_CS_REUSEPORT), 6823a498026STrond Myklebust }; 68354ceac45SDavid Howells struct nfs_client *clp; 6843a498026STrond Myklebust int error; 68554ceac45SDavid Howells 68638465f5dSScott Mayhew nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol, 68738465f5dSScott Mayhew ctx->timeo, ctx->retrans); 68838465f5dSScott Mayhew if (ctx->flags & NFS_MOUNT_NORESVPORT) 6894bf590e0SChuck Lever set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); 69045a52a02SAndy Adamson 69154ceac45SDavid Howells /* Allocate or find a client reference we can use */ 6927d38de3fSAnna Schumaker clp = nfs_get_client(&cl_init); 6934cbb9768SAnna Schumaker if (IS_ERR(clp)) 69454ceac45SDavid Howells return PTR_ERR(clp); 69554ceac45SDavid Howells 69654ceac45SDavid Howells server->nfs_client = clp; 69754ceac45SDavid Howells 69854ceac45SDavid Howells /* Initialise the client representation from the mount data */ 69938465f5dSScott Mayhew server->flags = ctx->flags; 70038465f5dSScott Mayhew server->options = ctx->options; 70162ab460cSTrond Myklebust server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 70262ab460cSTrond Myklebust NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP| 703cd812599STrond Myklebust NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME; 70454ceac45SDavid Howells 70538465f5dSScott Mayhew if (ctx->rsize) 70638465f5dSScott Mayhew server->rsize = nfs_block_size(ctx->rsize, NULL); 70738465f5dSScott Mayhew if (ctx->wsize) 70838465f5dSScott Mayhew server->wsize = nfs_block_size(ctx->wsize, NULL); 70954ceac45SDavid Howells 71038465f5dSScott Mayhew server->acregmin = ctx->acregmin * HZ; 71138465f5dSScott Mayhew server->acregmax = ctx->acregmax * HZ; 71238465f5dSScott Mayhew server->acdirmin = ctx->acdirmin * HZ; 71338465f5dSScott Mayhew server->acdirmax = ctx->acdirmax * HZ; 71454ceac45SDavid Howells 71554ceac45SDavid Howells /* Start lockd here, before we might error out */ 71654ceac45SDavid Howells error = nfs_start_lockd(server); 71754ceac45SDavid Howells if (error < 0) 71854ceac45SDavid Howells goto error; 71954ceac45SDavid Howells 72038465f5dSScott Mayhew server->port = ctx->nfs_server.port; 72138465f5dSScott Mayhew server->auth_info = ctx->auth_info; 722f22d6d79SChuck Lever 723a3f73c27SWeston Andros Adamson error = nfs_init_server_rpcclient(server, &timeparms, 72438465f5dSScott Mayhew ctx->selected_flavor); 72554ceac45SDavid Howells if (error < 0) 72654ceac45SDavid Howells goto error; 72754ceac45SDavid Howells 7283f8400d1SChuck Lever /* Preserve the values of mount_server-related mount options */ 72938465f5dSScott Mayhew if (ctx->mount_server.addrlen) { 73038465f5dSScott Mayhew memcpy(&server->mountd_address, &ctx->mount_server.address, 73138465f5dSScott Mayhew ctx->mount_server.addrlen); 73238465f5dSScott Mayhew server->mountd_addrlen = ctx->mount_server.addrlen; 7333f8400d1SChuck Lever } 73438465f5dSScott Mayhew server->mountd_version = ctx->mount_server.version; 73538465f5dSScott Mayhew server->mountd_port = ctx->mount_server.port; 73638465f5dSScott Mayhew server->mountd_protocol = ctx->mount_server.protocol; 7373f8400d1SChuck Lever 73838465f5dSScott Mayhew server->namelen = ctx->namlen; 73954ceac45SDavid Howells return 0; 74054ceac45SDavid Howells 74154ceac45SDavid Howells error: 74254ceac45SDavid Howells server->nfs_client = NULL; 74354ceac45SDavid Howells nfs_put_client(clp); 74454ceac45SDavid Howells return error; 74554ceac45SDavid Howells } 74654ceac45SDavid Howells 74754ceac45SDavid Howells /* 74854ceac45SDavid Howells * Load up the server record from information gained in an fsinfo record 74954ceac45SDavid Howells */ 750738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server, 751738fd0f3SBenny Halevy struct nfs_fsinfo *fsinfo) 75254ceac45SDavid Howells { 75304a5da69SFrank van der Linden unsigned long max_rpc_payload, raw_max_rpc_payload; 75454ceac45SDavid Howells 75554ceac45SDavid Howells /* Work out a lot of parameters */ 75654ceac45SDavid Howells if (server->rsize == 0) 75754ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtpref, NULL); 75854ceac45SDavid Howells if (server->wsize == 0) 75954ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtpref, NULL); 76054ceac45SDavid Howells 76154ceac45SDavid Howells if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) 76254ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtmax, NULL); 76354ceac45SDavid Howells if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) 76454ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtmax, NULL); 76554ceac45SDavid Howells 76604a5da69SFrank van der Linden raw_max_rpc_payload = rpc_max_payload(server->client); 76704a5da69SFrank van der Linden max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL); 76804a5da69SFrank van der Linden 76954ceac45SDavid Howells if (server->rsize > max_rpc_payload) 77054ceac45SDavid Howells server->rsize = max_rpc_payload; 77154ceac45SDavid Howells if (server->rsize > NFS_MAX_FILE_IO_SIZE) 77254ceac45SDavid Howells server->rsize = NFS_MAX_FILE_IO_SIZE; 77309cbfeafSKirill A. Shutemov server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 774e0bf68ddSPeter Zijlstra 77554ceac45SDavid Howells if (server->wsize > max_rpc_payload) 77654ceac45SDavid Howells server->wsize = max_rpc_payload; 77754ceac45SDavid Howells if (server->wsize > NFS_MAX_FILE_IO_SIZE) 77854ceac45SDavid Howells server->wsize = NFS_MAX_FILE_IO_SIZE; 77909cbfeafSKirill A. Shutemov server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 78085e174baSRicardo Labiaga 78154ceac45SDavid Howells server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); 78254ceac45SDavid Howells 78354ceac45SDavid Howells server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); 784*1a34c8c9STrond Myklebust if (server->dtsize > NFS_MAX_FILE_IO_SIZE) 785*1a34c8c9STrond Myklebust server->dtsize = NFS_MAX_FILE_IO_SIZE; 78654ceac45SDavid Howells if (server->dtsize > server->rsize) 78754ceac45SDavid Howells server->dtsize = server->rsize; 78854ceac45SDavid Howells 78954ceac45SDavid Howells if (server->flags & NFS_MOUNT_NOAC) { 79054ceac45SDavid Howells server->acregmin = server->acregmax = 0; 79154ceac45SDavid Howells server->acdirmin = server->acdirmax = 0; 79254ceac45SDavid Howells } 79354ceac45SDavid Howells 79454ceac45SDavid Howells server->maxfilesize = fsinfo->maxfilesize; 79554ceac45SDavid Howells 7966b96724eSRicardo Labiaga server->time_delta = fsinfo->time_delta; 7976b96724eSRicardo Labiaga 7982a92ee92SPeng Tao server->clone_blksize = fsinfo->clone_blksize; 79954ceac45SDavid Howells /* We're airborne Set socket buffersize */ 80054ceac45SDavid Howells rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); 80104a5da69SFrank van der Linden 80204a5da69SFrank van der Linden #ifdef CONFIG_NFS_V4_2 80304a5da69SFrank van der Linden /* 80404a5da69SFrank van der Linden * Defaults until limited by the session parameters. 80504a5da69SFrank van der Linden */ 80604a5da69SFrank van der Linden server->gxasize = min_t(unsigned int, raw_max_rpc_payload, 80704a5da69SFrank van der Linden XATTR_SIZE_MAX); 80804a5da69SFrank van der Linden server->sxasize = min_t(unsigned int, raw_max_rpc_payload, 80904a5da69SFrank van der Linden XATTR_SIZE_MAX); 81004a5da69SFrank van der Linden server->lxasize = min_t(unsigned int, raw_max_rpc_payload, 81104a5da69SFrank van der Linden nfs42_listxattr_xdrsize(XATTR_LIST_MAX)); 812b78ef845SFrank van der Linden 813b78ef845SFrank van der Linden if (fsinfo->xattr_support) 814b78ef845SFrank van der Linden server->caps |= NFS_CAP_XATTR; 81504a5da69SFrank van der Linden #endif 81654ceac45SDavid Howells } 81754ceac45SDavid Howells 81854ceac45SDavid Howells /* 81954ceac45SDavid Howells * Probe filesystem information, including the FSID on v2/v3 82054ceac45SDavid Howells */ 821fcf10398SBryan Schumaker int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 82254ceac45SDavid Howells { 82354ceac45SDavid Howells struct nfs_fsinfo fsinfo; 82454ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 82554ceac45SDavid Howells int error; 82654ceac45SDavid Howells 82754ceac45SDavid Howells if (clp->rpc_ops->set_capabilities != NULL) { 82854ceac45SDavid Howells error = clp->rpc_ops->set_capabilities(server, mntfh); 82954ceac45SDavid Howells if (error < 0) 8304cbb9768SAnna Schumaker return error; 83154ceac45SDavid Howells } 83254ceac45SDavid Howells 83354ceac45SDavid Howells fsinfo.fattr = fattr; 834ca440c38SJeff Layton fsinfo.nlayouttypes = 0; 8353132e49eSJeff Layton memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype)); 83654ceac45SDavid Howells error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); 83754ceac45SDavid Howells if (error < 0) 8384cbb9768SAnna Schumaker return error; 83954ceac45SDavid Howells 84071f81e51SKinglong Mee nfs_server_set_fsinfo(server, &fsinfo); 84154ceac45SDavid Howells 84254ceac45SDavid Howells /* Get some general file system info */ 84354ceac45SDavid Howells if (server->namelen == 0) { 84454ceac45SDavid Howells struct nfs_pathconf pathinfo; 84554ceac45SDavid Howells 84654ceac45SDavid Howells pathinfo.fattr = fattr; 84754ceac45SDavid Howells nfs_fattr_init(fattr); 84854ceac45SDavid Howells 84954ceac45SDavid Howells if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) 85054ceac45SDavid Howells server->namelen = pathinfo.max_namelen; 85154ceac45SDavid Howells } 85254ceac45SDavid Howells 85354ceac45SDavid Howells return 0; 85454ceac45SDavid Howells } 85589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_probe_fsinfo); 85654ceac45SDavid Howells 85754ceac45SDavid Howells /* 85854ceac45SDavid Howells * Copy useful information when duplicating a server record 85954ceac45SDavid Howells */ 860fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 86154ceac45SDavid Howells { 86254ceac45SDavid Howells target->flags = source->flags; 863356e76b8SChuck Lever target->rsize = source->rsize; 864356e76b8SChuck Lever target->wsize = source->wsize; 86554ceac45SDavid Howells target->acregmin = source->acregmin; 86654ceac45SDavid Howells target->acregmax = source->acregmax; 86754ceac45SDavid Howells target->acdirmin = source->acdirmin; 86854ceac45SDavid Howells target->acdirmax = source->acdirmax; 86954ceac45SDavid Howells target->caps = source->caps; 8702df54806SDavid Howells target->options = source->options; 8710f5f49b8SWeston Andros Adamson target->auth_info = source->auth_info; 87289a6814dSSteve Dickson target->port = source->port; 87354ceac45SDavid Howells } 87489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_copy_userdata); 87554ceac45SDavid Howells 876fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *server) 877fca5238eSChuck Lever { 878fca5238eSChuck Lever struct nfs_client *clp = server->nfs_client; 87973ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 880fca5238eSChuck Lever 881dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 882fca5238eSChuck Lever list_add_tail_rcu(&server->client_link, &clp->cl_superblocks); 883c25d32b2SStanislav Kinsbursky list_add_tail(&server->master_link, &nn->nfs_volume_list); 884d3b4c9d7SAndy Adamson clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 885dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 886fca5238eSChuck Lever 887fca5238eSChuck Lever } 88889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_insert_lists); 889fca5238eSChuck Lever 89032e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *server) 891fca5238eSChuck Lever { 892d3b4c9d7SAndy Adamson struct nfs_client *clp = server->nfs_client; 8934c03ae4aSTrond Myklebust struct nfs_net *nn; 894d3b4c9d7SAndy Adamson 8954c03ae4aSTrond Myklebust if (clp == NULL) 8964c03ae4aSTrond Myklebust return; 89773ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 898dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 899fca5238eSChuck Lever list_del_rcu(&server->client_link); 9004c03ae4aSTrond Myklebust if (list_empty(&clp->cl_superblocks)) 901d3b4c9d7SAndy Adamson set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 902fca5238eSChuck Lever list_del(&server->master_link); 903dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 904fca5238eSChuck Lever 905fca5238eSChuck Lever synchronize_rcu(); 906fca5238eSChuck Lever } 90732e62b7cSChuck Lever EXPORT_SYMBOL_GPL(nfs_server_remove_lists); 908fca5238eSChuck Lever 90954ceac45SDavid Howells /* 91054ceac45SDavid Howells * Allocate and initialise a server record 91154ceac45SDavid Howells */ 912fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void) 91354ceac45SDavid Howells { 91454ceac45SDavid Howells struct nfs_server *server; 91554ceac45SDavid Howells 91654ceac45SDavid Howells server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); 91754ceac45SDavid Howells if (!server) 91854ceac45SDavid Howells return NULL; 91954ceac45SDavid Howells 92054ceac45SDavid Howells server->client = server->client_acl = ERR_PTR(-EINVAL); 92154ceac45SDavid Howells 92254ceac45SDavid Howells /* Zero out the NFS state stuff */ 92354ceac45SDavid Howells INIT_LIST_HEAD(&server->client_link); 92454ceac45SDavid Howells INIT_LIST_HEAD(&server->master_link); 925d3978bb3SChuck Lever INIT_LIST_HEAD(&server->delegations); 9266382a441SWeston Andros Adamson INIT_LIST_HEAD(&server->layouts); 9270aaaf5c4SChuck Lever INIT_LIST_HEAD(&server->state_owners_lru); 92862164f31SOlga Kornievskaia INIT_LIST_HEAD(&server->ss_copies); 92954ceac45SDavid Howells 930ef818a28SSteve Dickson atomic_set(&server->active, 0); 931ef818a28SSteve Dickson 93254ceac45SDavid Howells server->io_stats = nfs_alloc_iostats(); 93354ceac45SDavid Howells if (!server->io_stats) { 93454ceac45SDavid Howells kfree(server); 93554ceac45SDavid Howells return NULL; 93654ceac45SDavid Howells } 93754ceac45SDavid Howells 9389157c31dSTrond Myklebust ida_init(&server->openowner_id); 939d2d7ce28STrond Myklebust ida_init(&server->lockowner_id); 940f7e8917aSFred Isaman pnfs_init_server(server); 94168ebf8feSBenjamin Coddington rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC"); 942f7e8917aSFred Isaman 94354ceac45SDavid Howells return server; 94454ceac45SDavid Howells } 94589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_server); 94654ceac45SDavid Howells 94754ceac45SDavid Howells /* 94854ceac45SDavid Howells * Free up a server record 94954ceac45SDavid Howells */ 95054ceac45SDavid Howells void nfs_free_server(struct nfs_server *server) 95154ceac45SDavid Howells { 952fca5238eSChuck Lever nfs_server_remove_lists(server); 95354ceac45SDavid Howells 95454ceac45SDavid Howells if (server->destroy != NULL) 95554ceac45SDavid Howells server->destroy(server); 9565cef338bSTrond Myklebust 9575cef338bSTrond Myklebust if (!IS_ERR(server->client_acl)) 9585cef338bSTrond Myklebust rpc_shutdown_client(server->client_acl); 95954ceac45SDavid Howells if (!IS_ERR(server->client)) 96054ceac45SDavid Howells rpc_shutdown_client(server->client); 96154ceac45SDavid Howells 96254ceac45SDavid Howells nfs_put_client(server->nfs_client); 96354ceac45SDavid Howells 964d2d7ce28STrond Myklebust ida_destroy(&server->lockowner_id); 9659157c31dSTrond Myklebust ida_destroy(&server->openowner_id); 96654ceac45SDavid Howells nfs_free_iostats(server->io_stats); 9671a58e8a0STrond Myklebust put_cred(server->cred); 96854ceac45SDavid Howells kfree(server); 96954ceac45SDavid Howells nfs_release_automount_timer(); 97054ceac45SDavid Howells } 97189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_server); 97254ceac45SDavid Howells 97354ceac45SDavid Howells /* 97454ceac45SDavid Howells * Create a version 2 or 3 volume record 97554ceac45SDavid Howells * - keyed on server and FSID 97654ceac45SDavid Howells */ 97762a55d08SScott Mayhew struct nfs_server *nfs_create_server(struct fs_context *fc) 97854ceac45SDavid Howells { 97962a55d08SScott Mayhew struct nfs_fs_context *ctx = nfs_fc2context(fc); 98054ceac45SDavid Howells struct nfs_server *server; 981fbca779aSTrond Myklebust struct nfs_fattr *fattr; 98254ceac45SDavid Howells int error; 98354ceac45SDavid Howells 98454ceac45SDavid Howells server = nfs_alloc_server(); 98554ceac45SDavid Howells if (!server) 98654ceac45SDavid Howells return ERR_PTR(-ENOMEM); 98754ceac45SDavid Howells 9881a58e8a0STrond Myklebust server->cred = get_cred(current_cred()); 9891a58e8a0STrond Myklebust 990fbca779aSTrond Myklebust error = -ENOMEM; 991fbca779aSTrond Myklebust fattr = nfs_alloc_fattr(); 992fbca779aSTrond Myklebust if (fattr == NULL) 993fbca779aSTrond Myklebust goto error; 994fbca779aSTrond Myklebust 99554ceac45SDavid Howells /* Get a client representation */ 99662a55d08SScott Mayhew error = nfs_init_server(server, fc); 99754ceac45SDavid Howells if (error < 0) 99854ceac45SDavid Howells goto error; 99954ceac45SDavid Howells 100054ceac45SDavid Howells /* Probe the root fh to retrieve its FSID */ 100162a55d08SScott Mayhew error = nfs_probe_fsinfo(server, ctx->mntfh, fattr); 100254ceac45SDavid Howells if (error < 0) 100354ceac45SDavid Howells goto error; 100454af3bb5STrond Myklebust if (server->nfs_client->rpc_ops->version == 3) { 100554af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) 100654af3bb5STrond Myklebust server->namelen = NFS3_MAXNAMLEN; 100762a55d08SScott Mayhew if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS)) 100854af3bb5STrond Myklebust server->caps |= NFS_CAP_READDIRPLUS; 100954af3bb5STrond Myklebust } else { 101054af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) 101154af3bb5STrond Myklebust server->namelen = NFS2_MAXNAMLEN; 101254af3bb5STrond Myklebust } 101354af3bb5STrond Myklebust 1014fbca779aSTrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR)) { 101562a55d08SScott Mayhew error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh, 1016a841b54dSTrond Myklebust fattr, NULL, NULL); 101754ceac45SDavid Howells if (error < 0) { 101854ceac45SDavid Howells dprintk("nfs_create_server: getattr error = %d\n", -error); 101954ceac45SDavid Howells goto error; 102054ceac45SDavid Howells } 102154ceac45SDavid Howells } 1022fbca779aSTrond Myklebust memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 102354ceac45SDavid Howells 10246daabf1bSDavid Howells dprintk("Server FSID: %llx:%llx\n", 10256daabf1bSDavid Howells (unsigned long long) server->fsid.major, 10266daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 102754ceac45SDavid Howells 1028fca5238eSChuck Lever nfs_server_insert_lists(server); 102954ceac45SDavid Howells server->mount_time = jiffies; 1030fbca779aSTrond Myklebust nfs_free_fattr(fattr); 103154ceac45SDavid Howells return server; 103254ceac45SDavid Howells 103354ceac45SDavid Howells error: 1034fbca779aSTrond Myklebust nfs_free_fattr(fattr); 103554ceac45SDavid Howells nfs_free_server(server); 103654ceac45SDavid Howells return ERR_PTR(error); 103754ceac45SDavid Howells } 1038ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_server); 103954ceac45SDavid Howells 104054ceac45SDavid Howells /* 104154ceac45SDavid Howells * Clone an NFS2, NFS3 or NFS4 server record 104254ceac45SDavid Howells */ 104354ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source, 104454ceac45SDavid Howells struct nfs_fh *fh, 10457e6eb683SBryan Schumaker struct nfs_fattr *fattr, 10467e6eb683SBryan Schumaker rpc_authflavor_t flavor) 104754ceac45SDavid Howells { 104854ceac45SDavid Howells struct nfs_server *server; 1049fbca779aSTrond Myklebust struct nfs_fattr *fattr_fsinfo; 105054ceac45SDavid Howells int error; 105154ceac45SDavid Howells 105254ceac45SDavid Howells server = nfs_alloc_server(); 105354ceac45SDavid Howells if (!server) 105454ceac45SDavid Howells return ERR_PTR(-ENOMEM); 105554ceac45SDavid Howells 10561a58e8a0STrond Myklebust server->cred = get_cred(source->cred); 10571a58e8a0STrond Myklebust 1058fbca779aSTrond Myklebust error = -ENOMEM; 1059fbca779aSTrond Myklebust fattr_fsinfo = nfs_alloc_fattr(); 1060fbca779aSTrond Myklebust if (fattr_fsinfo == NULL) 1061fbca779aSTrond Myklebust goto out_free_server; 1062fbca779aSTrond Myklebust 106354ceac45SDavid Howells /* Copy data from the source */ 106454ceac45SDavid Howells server->nfs_client = source->nfs_client; 10650aaaf5c4SChuck Lever server->destroy = source->destroy; 1066212bf41dSElena Reshetova refcount_inc(&server->nfs_client->cl_count); 106754ceac45SDavid Howells nfs_server_copy_userdata(server, source); 106854ceac45SDavid Howells 106954ceac45SDavid Howells server->fsid = fattr->fsid; 107054ceac45SDavid Howells 107133170233STrond Myklebust error = nfs_init_server_rpcclient(server, 107233170233STrond Myklebust source->client->cl_timeout, 10737e6eb683SBryan Schumaker flavor); 107454ceac45SDavid Howells if (error < 0) 107554ceac45SDavid Howells goto out_free_server; 107654ceac45SDavid Howells 107754ceac45SDavid Howells /* probe the filesystem info for this server filesystem */ 1078fbca779aSTrond Myklebust error = nfs_probe_fsinfo(server, fh, fattr_fsinfo); 107954ceac45SDavid Howells if (error < 0) 108054ceac45SDavid Howells goto out_free_server; 108154ceac45SDavid Howells 108254af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 108354af3bb5STrond Myklebust server->namelen = NFS4_MAXNAMLEN; 108454af3bb5STrond Myklebust 108554ceac45SDavid Howells error = nfs_start_lockd(server); 108654ceac45SDavid Howells if (error < 0) 108754ceac45SDavid Howells goto out_free_server; 108854ceac45SDavid Howells 1089fca5238eSChuck Lever nfs_server_insert_lists(server); 109054ceac45SDavid Howells server->mount_time = jiffies; 109154ceac45SDavid Howells 1092fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 109354ceac45SDavid Howells return server; 109454ceac45SDavid Howells 109554ceac45SDavid Howells out_free_server: 1096fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 109754ceac45SDavid Howells nfs_free_server(server); 109854ceac45SDavid Howells return ERR_PTR(error); 109954ceac45SDavid Howells } 1100ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_clone_server); 11016aaca566SDavid Howells 11026b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net) 11036b13168bSStanislav Kinsbursky { 11046b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 11056b13168bSStanislav Kinsbursky 11066b13168bSStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_client_list); 1107c25d32b2SStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_volume_list); 110889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 110928cd1b3fSStanislav Kinsbursky idr_init(&nn->cb_ident_idr); 111028cd1b3fSStanislav Kinsbursky #endif 11114c03ae4aSTrond Myklebust spin_lock_init(&nn->nfs_client_lock); 11122f86e091SDeepa Dinamani nn->boot_time = ktime_get_real(); 1113bf11fbdbSTrond Myklebust 1114bf11fbdbSTrond Myklebust nfs_netns_sysfs_setup(nn, net); 11156b13168bSStanislav Kinsbursky } 11166b13168bSStanislav Kinsbursky 111710b7a70cSTrond Myklebust void nfs_clients_exit(struct net *net) 111810b7a70cSTrond Myklebust { 111910b7a70cSTrond Myklebust struct nfs_net *nn = net_generic(net, nfs_net_id); 112010b7a70cSTrond Myklebust 1121bf11fbdbSTrond Myklebust nfs_netns_sysfs_destroy(nn); 112210b7a70cSTrond Myklebust nfs_cleanup_cb_ident_idr(net); 112310b7a70cSTrond Myklebust WARN_ON_ONCE(!list_empty(&nn->nfs_client_list)); 112410b7a70cSTrond Myklebust WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list)); 112510b7a70cSTrond Myklebust } 112610b7a70cSTrond Myklebust 11276aaca566SDavid Howells #ifdef CONFIG_PROC_FS 11286aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); 11296aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); 11306aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v); 11316aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v); 11326aaca566SDavid Howells 113388e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = { 11346aaca566SDavid Howells .start = nfs_server_list_start, 11356aaca566SDavid Howells .next = nfs_server_list_next, 11366aaca566SDavid Howells .stop = nfs_server_list_stop, 11376aaca566SDavid Howells .show = nfs_server_list_show, 11386aaca566SDavid Howells }; 11396aaca566SDavid Howells 11406aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); 11416aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); 11426aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v); 11436aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v); 11446aaca566SDavid Howells 114588e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = { 11466aaca566SDavid Howells .start = nfs_volume_list_start, 11476aaca566SDavid Howells .next = nfs_volume_list_next, 11486aaca566SDavid Howells .stop = nfs_volume_list_stop, 11496aaca566SDavid Howells .show = nfs_volume_list_show, 11506aaca566SDavid Howells }; 11516aaca566SDavid Howells 11526aaca566SDavid Howells /* 11536aaca566SDavid Howells * set up the iterator to start reading from the server list and return the first item 11546aaca566SDavid Howells */ 11556aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) 11568d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 11576aaca566SDavid Howells { 115865b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 11596b13168bSStanislav Kinsbursky 11606aaca566SDavid Howells /* lock the list against modification */ 1161dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 11626b13168bSStanislav Kinsbursky return seq_list_start_head(&nn->nfs_client_list, *_pos); 11636aaca566SDavid Howells } 11646aaca566SDavid Howells 11656aaca566SDavid Howells /* 11666aaca566SDavid Howells * move to next server 11676aaca566SDavid Howells */ 11686aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) 11696aaca566SDavid Howells { 117065b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 11716b13168bSStanislav Kinsbursky 11726b13168bSStanislav Kinsbursky return seq_list_next(v, &nn->nfs_client_list, pos); 11736aaca566SDavid Howells } 11746aaca566SDavid Howells 11756aaca566SDavid Howells /* 11766aaca566SDavid Howells * clean up after reading from the transports list 11776aaca566SDavid Howells */ 11786aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v) 11798d11620eSJeff Layton __releases(&nn->nfs_client_lock) 11806aaca566SDavid Howells { 118165b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1182dc030858SStanislav Kinsbursky 1183dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 11846aaca566SDavid Howells } 11856aaca566SDavid Howells 11866aaca566SDavid Howells /* 11876aaca566SDavid Howells * display a header line followed by a load of call lines 11886aaca566SDavid Howells */ 11896aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v) 11906aaca566SDavid Howells { 11916aaca566SDavid Howells struct nfs_client *clp; 119265b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 11936aaca566SDavid Howells 11946aaca566SDavid Howells /* display header on line 1 */ 11956b13168bSStanislav Kinsbursky if (v == &nn->nfs_client_list) { 11966aaca566SDavid Howells seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); 11976aaca566SDavid Howells return 0; 11986aaca566SDavid Howells } 11996aaca566SDavid Howells 12006aaca566SDavid Howells /* display one transport per line on subsequent lines */ 12016aaca566SDavid Howells clp = list_entry(v, struct nfs_client, cl_share_link); 12026aaca566SDavid Howells 1203940aab49SMalahal Naineni /* Check if the client is initialized */ 1204940aab49SMalahal Naineni if (clp->cl_cons_state != NFS_CS_READY) 1205940aab49SMalahal Naineni return 0; 1206940aab49SMalahal Naineni 12072446ab60STrond Myklebust rcu_read_lock(); 12085d8515caSChuck Lever seq_printf(m, "v%u %s %s %3d %s\n", 120940c55319STrond Myklebust clp->rpc_ops->version, 12105d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 12115d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 1212212bf41dSElena Reshetova refcount_read(&clp->cl_count), 12136aaca566SDavid Howells clp->cl_hostname); 12142446ab60STrond Myklebust rcu_read_unlock(); 12156aaca566SDavid Howells 12166aaca566SDavid Howells return 0; 12176aaca566SDavid Howells } 12186aaca566SDavid Howells 12196aaca566SDavid Howells /* 12206aaca566SDavid Howells * set up the iterator to start reading from the volume list and return the first item 12216aaca566SDavid Howells */ 12226aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) 12238d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 12246aaca566SDavid Howells { 122565b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1226c25d32b2SStanislav Kinsbursky 12276aaca566SDavid Howells /* lock the list against modification */ 1228dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 1229c25d32b2SStanislav Kinsbursky return seq_list_start_head(&nn->nfs_volume_list, *_pos); 12306aaca566SDavid Howells } 12316aaca566SDavid Howells 12326aaca566SDavid Howells /* 12336aaca566SDavid Howells * move to next volume 12346aaca566SDavid Howells */ 12356aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) 12366aaca566SDavid Howells { 123765b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1238c25d32b2SStanislav Kinsbursky 1239c25d32b2SStanislav Kinsbursky return seq_list_next(v, &nn->nfs_volume_list, pos); 12406aaca566SDavid Howells } 12416aaca566SDavid Howells 12426aaca566SDavid Howells /* 12436aaca566SDavid Howells * clean up after reading from the transports list 12446aaca566SDavid Howells */ 12456aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v) 12468d11620eSJeff Layton __releases(&nn->nfs_client_lock) 12476aaca566SDavid Howells { 124865b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1249dc030858SStanislav Kinsbursky 1250dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 12516aaca566SDavid Howells } 12526aaca566SDavid Howells 12536aaca566SDavid Howells /* 12546aaca566SDavid Howells * display a header line followed by a load of call lines 12556aaca566SDavid Howells */ 12566aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v) 12576aaca566SDavid Howells { 12586aaca566SDavid Howells struct nfs_server *server; 12596aaca566SDavid Howells struct nfs_client *clp; 1260df05a49fSKinglong Mee char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0' 1261df05a49fSKinglong Mee char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0' 126265b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 12636aaca566SDavid Howells 12646aaca566SDavid Howells /* display header on line 1 */ 1265c25d32b2SStanislav Kinsbursky if (v == &nn->nfs_volume_list) { 1266df05a49fSKinglong Mee seq_puts(m, "NV SERVER PORT DEV FSID" 1267df05a49fSKinglong Mee " FSC\n"); 12686aaca566SDavid Howells return 0; 12696aaca566SDavid Howells } 12706aaca566SDavid Howells /* display one transport per line on subsequent lines */ 12716aaca566SDavid Howells server = list_entry(v, struct nfs_server, master_link); 12726aaca566SDavid Howells clp = server->nfs_client; 12736aaca566SDavid Howells 1274df05a49fSKinglong Mee snprintf(dev, sizeof(dev), "%u:%u", 12756aaca566SDavid Howells MAJOR(server->s_dev), MINOR(server->s_dev)); 12766aaca566SDavid Howells 1277df05a49fSKinglong Mee snprintf(fsid, sizeof(fsid), "%llx:%llx", 12786daabf1bSDavid Howells (unsigned long long) server->fsid.major, 12796daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 12806aaca566SDavid Howells 12812446ab60STrond Myklebust rcu_read_lock(); 1282df05a49fSKinglong Mee seq_printf(m, "v%u %s %s %-12s %-33s %s\n", 128340c55319STrond Myklebust clp->rpc_ops->version, 12845d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 12855d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 12866aaca566SDavid Howells dev, 12875d1acff1SDavid Howells fsid, 12885d1acff1SDavid Howells nfs_server_fscache_state(server)); 12892446ab60STrond Myklebust rcu_read_unlock(); 12906aaca566SDavid Howells 12916aaca566SDavid Howells return 0; 12926aaca566SDavid Howells } 12936aaca566SDavid Howells 129465b38851SEric W. Biederman int nfs_fs_proc_net_init(struct net *net) 129565b38851SEric W. Biederman { 129665b38851SEric W. Biederman struct nfs_net *nn = net_generic(net, nfs_net_id); 129765b38851SEric W. Biederman struct proc_dir_entry *p; 129865b38851SEric W. Biederman 129965b38851SEric W. Biederman nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net); 130065b38851SEric W. Biederman if (!nn->proc_nfsfs) 130165b38851SEric W. Biederman goto error_0; 130265b38851SEric W. Biederman 130365b38851SEric W. Biederman /* a file of servers with which we're dealing */ 1304c3506372SChristoph Hellwig p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1305c3506372SChristoph Hellwig &nfs_server_list_ops, sizeof(struct seq_net_private)); 130665b38851SEric W. Biederman if (!p) 130765b38851SEric W. Biederman goto error_1; 130865b38851SEric W. Biederman 130965b38851SEric W. Biederman /* a file of volumes that we have mounted */ 1310c3506372SChristoph Hellwig p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1311c3506372SChristoph Hellwig &nfs_volume_list_ops, sizeof(struct seq_net_private)); 131265b38851SEric W. Biederman if (!p) 131321e81002SCong Wang goto error_1; 131465b38851SEric W. Biederman return 0; 131565b38851SEric W. Biederman 131665b38851SEric W. Biederman error_1: 131721e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 131865b38851SEric W. Biederman error_0: 131965b38851SEric W. Biederman return -ENOMEM; 132065b38851SEric W. Biederman } 132165b38851SEric W. Biederman 132265b38851SEric W. Biederman void nfs_fs_proc_net_exit(struct net *net) 132365b38851SEric W. Biederman { 132421e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 132565b38851SEric W. Biederman } 132665b38851SEric W. Biederman 13276aaca566SDavid Howells /* 13286aaca566SDavid Howells * initialise the /proc/fs/nfsfs/ directory 13296aaca566SDavid Howells */ 13306aaca566SDavid Howells int __init nfs_fs_proc_init(void) 13316aaca566SDavid Howells { 13326a062a36SKinglong Mee if (!proc_mkdir("fs/nfsfs", NULL)) 13336aaca566SDavid Howells goto error_0; 13346aaca566SDavid Howells 13356aaca566SDavid Howells /* a file of servers with which we're dealing */ 13366a062a36SKinglong Mee if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers")) 13376aaca566SDavid Howells goto error_1; 13386aaca566SDavid Howells 13396aaca566SDavid Howells /* a file of volumes that we have mounted */ 13406a062a36SKinglong Mee if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes")) 13416a062a36SKinglong Mee goto error_1; 13426aaca566SDavid Howells 13436a062a36SKinglong Mee return 0; 13446aaca566SDavid Howells error_1: 13456a062a36SKinglong Mee remove_proc_subtree("fs/nfsfs", NULL); 13466aaca566SDavid Howells error_0: 13476aaca566SDavid Howells return -ENOMEM; 13486aaca566SDavid Howells } 13496aaca566SDavid Howells 13506aaca566SDavid Howells /* 13516aaca566SDavid Howells * clean up the /proc/fs/nfsfs/ directory 13526aaca566SDavid Howells */ 13536aaca566SDavid Howells void nfs_fs_proc_exit(void) 13546aaca566SDavid Howells { 13556a062a36SKinglong Mee remove_proc_subtree("fs/nfsfs", NULL); 13566aaca566SDavid Howells } 13576aaca566SDavid Howells 13586aaca566SDavid Howells #endif /* CONFIG_PROC_FS */ 1359