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 180468d126dSTrond Myklebust clp->cl_flags = cl_init->init_flags; 18159dca3b2STrond Myklebust clp->cl_proto = cl_init->proto; 1826619079dSTrond Myklebust clp->cl_nconnect = cl_init->nconnect; 1837e134205SOlga Kornievskaia clp->cl_max_connect = cl_init->max_connect ? cl_init->max_connect : 1; 18473ea666cSChuck Lever clp->cl_net = get_net(cl_init->net); 18559dca3b2STrond Myklebust 1865e16923bSNeilBrown clp->cl_principal = "*"; 1876c0a8c5fSChuck Lever clp->cl_xprtsec = cl_init->xprtsec; 18824c8dbbbSDavid Howells return clp; 18924c8dbbbSDavid Howells 19071468513SBenny Halevy error_cleanup: 191ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 1921f70ef96SAlexey Khoroshilov error_dealloc: 19324c8dbbbSDavid Howells kfree(clp); 19424c8dbbbSDavid Howells error_0: 195a21bdd9bSChuck Lever return ERR_PTR(err); 19624c8dbbbSDavid Howells } 197ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_client); 19824c8dbbbSDavid Howells 19989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 20010b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net) 201f4eecd5dSAndy Adamson { 20228cd1b3fSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 20328cd1b3fSStanislav Kinsbursky 20428cd1b3fSStanislav Kinsbursky idr_destroy(&nn->cb_ident_idr); 205f4eecd5dSAndy Adamson } 206f4eecd5dSAndy Adamson 207f4eecd5dSAndy Adamson /* nfs_client_lock held */ 208f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 209f4eecd5dSAndy Adamson { 21073ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 21128cd1b3fSStanislav Kinsbursky 212f4eecd5dSAndy Adamson if (clp->cl_cb_ident) 21328cd1b3fSStanislav Kinsbursky idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident); 214f4eecd5dSAndy Adamson } 215f4eecd5dSAndy Adamson 216f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 217f7e8917aSFred Isaman { 218f7e8917aSFred Isaman rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 219f7e8917aSFred Isaman } 220f7e8917aSFred Isaman 221888ef2e3SAlexandros Batsakis #else 22210b7a70cSTrond Myklebust static void nfs_cleanup_cb_ident_idr(struct net *net) 223f4eecd5dSAndy Adamson { 224f4eecd5dSAndy Adamson } 225f4eecd5dSAndy Adamson 226f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 227f4eecd5dSAndy Adamson { 228f4eecd5dSAndy Adamson } 229f7e8917aSFred Isaman 230f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 231f7e8917aSFred Isaman { 232f7e8917aSFred Isaman } 233f7e8917aSFred Isaman 234888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */ 235888ef2e3SAlexandros Batsakis 236888ef2e3SAlexandros Batsakis /* 2375dd3177aSTrond Myklebust * Destroy a shared client record 2385dd3177aSTrond Myklebust */ 239cdb7ecedSBryan Schumaker void nfs_free_client(struct nfs_client *clp) 2405dd3177aSTrond Myklebust { 24124c8dbbbSDavid Howells /* -EIO all pending I/O */ 24224c8dbbbSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 24324c8dbbbSDavid Howells rpc_shutdown_client(clp->cl_rpcclient); 24424c8dbbbSDavid Howells 24573ea666cSChuck Lever put_net(clp->cl_net); 246ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 24724c8dbbbSDavid Howells kfree(clp->cl_hostname); 248f11b2a1cSJeff Layton kfree(clp->cl_acceptor); 24924c8dbbbSDavid Howells kfree(clp); 25024c8dbbbSDavid Howells } 251ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_client); 25224c8dbbbSDavid Howells 25324c8dbbbSDavid Howells /* 25424c8dbbbSDavid Howells * Release a reference to a shared client record 25524c8dbbbSDavid Howells */ 25624c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp) 25724c8dbbbSDavid Howells { 258dc030858SStanislav Kinsbursky struct nfs_net *nn; 259dc030858SStanislav Kinsbursky 26027ba8512SDavid Howells if (!clp) 26127ba8512SDavid Howells return; 26227ba8512SDavid Howells 26373ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 26424c8dbbbSDavid Howells 265212bf41dSElena Reshetova if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) { 26624c8dbbbSDavid Howells list_del(&clp->cl_share_link); 267f4eecd5dSAndy Adamson nfs_cb_idr_remove_locked(clp); 268dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 26924c8dbbbSDavid Howells 2701fea73a8STrond Myklebust WARN_ON_ONCE(!list_empty(&clp->cl_superblocks)); 27124c8dbbbSDavid Howells 272cdb7ecedSBryan Schumaker clp->rpc_ops->free_client(clp); 27324c8dbbbSDavid Howells } 27424c8dbbbSDavid Howells } 27516b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client); 27624c8dbbbSDavid Howells 2773fbd67adSTrond Myklebust /* 278c81468a1STrond Myklebust * Find an nfs_client on the list that matches the initialisation data 279c81468a1STrond Myklebust * that is supplied. 280c81468a1STrond Myklebust */ 281c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) 28224c8dbbbSDavid Howells { 28324c8dbbbSDavid Howells struct nfs_client *clp; 284cf0d7e7fSKees Cook const struct sockaddr *sap = (struct sockaddr *)data->addr; 2856b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(data->net, nfs_net_id); 286950a578cSRoberto Bergantinos Corpas int error; 28724c8dbbbSDavid Howells 288c156618eSScott Mayhew again: 2896b13168bSStanislav Kinsbursky list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { 290d7371c41SIan Dall const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 29113bbc06aSTrond Myklebust /* Don't match clients that failed to initialise properly */ 29213bbc06aSTrond Myklebust if (clp->cl_cons_state < 0) 29313bbc06aSTrond Myklebust continue; 29413bbc06aSTrond Myklebust 295c156618eSScott Mayhew /* If a client is still initializing then we need to wait */ 296c156618eSScott Mayhew if (clp->cl_cons_state > NFS_CS_READY) { 297c156618eSScott Mayhew refcount_inc(&clp->cl_count); 298c156618eSScott Mayhew spin_unlock(&nn->nfs_client_lock); 299950a578cSRoberto Bergantinos Corpas error = nfs_wait_client_init_complete(clp); 300c156618eSScott Mayhew nfs_put_client(clp); 301c260121aSBenjamin Coddington spin_lock(&nn->nfs_client_lock); 302950a578cSRoberto Bergantinos Corpas if (error < 0) 303950a578cSRoberto Bergantinos Corpas return ERR_PTR(error); 304c156618eSScott Mayhew goto again; 305c156618eSScott Mayhew } 306c156618eSScott Mayhew 30724c8dbbbSDavid Howells /* Different NFS versions cannot share the same nfs_client */ 308ab7017a3SBryan Schumaker if (clp->rpc_ops != data->nfs_mod->rpc_ops) 30924c8dbbbSDavid Howells continue; 31024c8dbbbSDavid Howells 31159dca3b2STrond Myklebust if (clp->cl_proto != data->proto) 31259dca3b2STrond Myklebust continue; 3135aae4a9aSBenny Halevy /* Match nfsv4 minorversion */ 3145aae4a9aSBenny Halevy if (clp->cl_minorversion != data->minorversion) 3155aae4a9aSBenny Halevy continue; 31652f98f1aSTrond Myklebust 31752f98f1aSTrond Myklebust /* Match request for a dedicated DS */ 31852f98f1aSTrond Myklebust if (test_bit(NFS_CS_DS, &data->init_flags) != 31952f98f1aSTrond Myklebust test_bit(NFS_CS_DS, &clp->cl_flags)) 32052f98f1aSTrond Myklebust continue; 32152f98f1aSTrond Myklebust 322c81468a1STrond Myklebust /* Match the full socket address */ 323d8efa4e6SAnna Schumaker if (!rpc_cmp_addr_port(sap, clap)) 32404ea1b3eSAndy Adamson /* Match all xprt_switch full socket addresses */ 3258ef32955SPetr Vandrovec if (IS_ERR(clp->cl_rpcclient) || 3268ef32955SPetr Vandrovec !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient, 32704ea1b3eSAndy Adamson sap)) 32824c8dbbbSDavid Howells continue; 32924c8dbbbSDavid Howells 3306c0a8c5fSChuck Lever /* Match the xprt security policy */ 3316c0a8c5fSChuck Lever if (clp->cl_xprtsec.policy != data->xprtsec.policy) 3326c0a8c5fSChuck Lever continue; 3336c0a8c5fSChuck Lever 334212bf41dSElena Reshetova refcount_inc(&clp->cl_count); 33524c8dbbbSDavid Howells return clp; 33624c8dbbbSDavid Howells } 337c81468a1STrond Myklebust return NULL; 33824c8dbbbSDavid Howells } 33924c8dbbbSDavid Howells 340a33e4b03SWeston Andros Adamson /* 341a33e4b03SWeston Andros Adamson * Return true if @clp is done initializing, false if still working on it. 342a33e4b03SWeston Andros Adamson * 343a33e4b03SWeston Andros Adamson * Use nfs_client_init_status to check if it was successful. 344a33e4b03SWeston Andros Adamson */ 345a33e4b03SWeston Andros Adamson bool nfs_client_init_is_complete(const struct nfs_client *clp) 3464697bd5eSTrond Myklebust { 34748d66b97STrond Myklebust return clp->cl_cons_state <= NFS_CS_READY; 3484697bd5eSTrond Myklebust } 349a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_is_complete); 350a33e4b03SWeston Andros Adamson 351a33e4b03SWeston Andros Adamson /* 352a33e4b03SWeston Andros Adamson * Return 0 if @clp was successfully initialized, -errno otherwise. 353a33e4b03SWeston Andros Adamson * 354a33e4b03SWeston Andros Adamson * This must be called *after* nfs_client_init_is_complete() returns true, 355a33e4b03SWeston Andros Adamson * otherwise it will pop WARN_ON_ONCE and return -EINVAL 356a33e4b03SWeston Andros Adamson */ 357a33e4b03SWeston Andros Adamson int nfs_client_init_status(const struct nfs_client *clp) 358a33e4b03SWeston Andros Adamson { 359a33e4b03SWeston Andros Adamson /* called without checking nfs_client_init_is_complete */ 360a33e4b03SWeston Andros Adamson if (clp->cl_cons_state > NFS_CS_READY) { 361a33e4b03SWeston Andros Adamson WARN_ON_ONCE(1); 362a33e4b03SWeston Andros Adamson return -EINVAL; 363a33e4b03SWeston Andros Adamson } 364a33e4b03SWeston Andros Adamson return clp->cl_cons_state; 365a33e4b03SWeston Andros Adamson } 366a33e4b03SWeston Andros Adamson EXPORT_SYMBOL_GPL(nfs_client_init_status); 3674697bd5eSTrond Myklebust 3684697bd5eSTrond Myklebust int nfs_wait_client_init_complete(const struct nfs_client *clp) 3694697bd5eSTrond Myklebust { 3704697bd5eSTrond Myklebust return wait_event_killable(nfs_client_active_wq, 3714697bd5eSTrond Myklebust nfs_client_init_is_complete(clp)); 3724697bd5eSTrond Myklebust } 37389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete); 3744697bd5eSTrond Myklebust 37524c8dbbbSDavid Howells /* 376f411703aSChuck Lever * Found an existing client. Make sure it's ready before returning. 377f411703aSChuck Lever */ 378f411703aSChuck Lever static struct nfs_client * 379f411703aSChuck Lever nfs_found_client(const struct nfs_client_initdata *cl_init, 380f411703aSChuck Lever struct nfs_client *clp) 381f411703aSChuck Lever { 382f411703aSChuck Lever int error; 383f411703aSChuck Lever 3844697bd5eSTrond Myklebust error = nfs_wait_client_init_complete(clp); 385f411703aSChuck Lever if (error < 0) { 386f411703aSChuck Lever nfs_put_client(clp); 387f411703aSChuck Lever return ERR_PTR(-ERESTARTSYS); 388f411703aSChuck Lever } 389f411703aSChuck Lever 390f411703aSChuck Lever if (clp->cl_cons_state < NFS_CS_READY) { 391f411703aSChuck Lever error = clp->cl_cons_state; 392f411703aSChuck Lever nfs_put_client(clp); 393f411703aSChuck Lever return ERR_PTR(error); 394f411703aSChuck Lever } 395f411703aSChuck Lever 39654ac471cSTrond Myklebust smp_rmb(); 397f411703aSChuck Lever return clp; 398f411703aSChuck Lever } 399f411703aSChuck Lever 400f411703aSChuck Lever /* 40124c8dbbbSDavid Howells * Look up a client by IP address and protocol version 40224c8dbbbSDavid Howells * - creates a new record if one doesn't yet exist 40324c8dbbbSDavid Howells */ 4047d38de3fSAnna Schumaker struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) 40524c8dbbbSDavid Howells { 40624c8dbbbSDavid Howells struct nfs_client *clp, *new = NULL; 4076b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id); 408ab7017a3SBryan Schumaker const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops; 40924c8dbbbSDavid Howells 41031434f49SPeng Tao if (cl_init->hostname == NULL) { 41131434f49SPeng Tao WARN_ON(1); 41209226e83SDan Carpenter return ERR_PTR(-EINVAL); 41331434f49SPeng Tao } 41431434f49SPeng Tao 41524c8dbbbSDavid Howells /* see if the client already exists */ 41624c8dbbbSDavid Howells do { 417dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 41824c8dbbbSDavid Howells 419c81468a1STrond Myklebust clp = nfs_match_client(cl_init); 420f411703aSChuck Lever if (clp) { 421f411703aSChuck Lever spin_unlock(&nn->nfs_client_lock); 422f411703aSChuck Lever if (new) 423cdb7ecedSBryan Schumaker new->rpc_ops->free_client(new); 4249f7761cfSBenjamin Coddington if (IS_ERR(clp)) 4259f7761cfSBenjamin Coddington return clp; 426f411703aSChuck Lever return nfs_found_client(cl_init, clp); 427f411703aSChuck Lever } 4288cab4c39SChuck Lever if (new) { 42905f4c350SChuck Lever list_add_tail(&new->cl_share_link, 43005f4c350SChuck Lever &nn->nfs_client_list); 4318cab4c39SChuck Lever spin_unlock(&nn->nfs_client_lock); 4325c6e5b60STrond Myklebust return rpc_ops->init_client(new, cl_init); 4338cab4c39SChuck Lever } 43424c8dbbbSDavid Howells 435dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 43624c8dbbbSDavid Howells 437ab7017a3SBryan Schumaker new = rpc_ops->alloc_client(cl_init); 438a21bdd9bSChuck Lever } while (!IS_ERR(new)); 43924c8dbbbSDavid Howells 440a21bdd9bSChuck Lever return new; 44124c8dbbbSDavid Howells } 44289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_get_client); 44324c8dbbbSDavid Howells 44424c8dbbbSDavid Howells /* 44524c8dbbbSDavid Howells * Mark a server as ready or failed 44624c8dbbbSDavid Howells */ 44776db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state) 44824c8dbbbSDavid Howells { 44954ac471cSTrond Myklebust smp_wmb(); 45024c8dbbbSDavid Howells clp->cl_cons_state = state; 45124c8dbbbSDavid Howells wake_up_all(&nfs_client_active_wq); 45224c8dbbbSDavid Howells } 45389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mark_client_ready); 4545006a76cSDavid Howells 4555006a76cSDavid Howells /* 4565006a76cSDavid Howells * Initialise the timeout values for a connection 4575006a76cSDavid Howells */ 458fcf10398SBryan Schumaker void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 459a956bedaSTrond Myklebust int timeo, int retrans) 4605006a76cSDavid Howells { 4615006a76cSDavid Howells to->to_initval = timeo * HZ / 10; 4625006a76cSDavid Howells to->to_retries = retrans; 4635006a76cSDavid Howells 4645006a76cSDavid Howells switch (proto) { 4650896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_TCP: 466c8407f2eSChuck Lever case XPRT_TRANSPORT_TCP_TLS: 4672cf7ff7aS\"Talpey, Thomas\ case XPRT_TRANSPORT_RDMA: 468a956bedaSTrond Myklebust if (retrans == NFS_UNSPEC_RETRANS) 469259875efSTrond Myklebust to->to_retries = NFS_DEF_TCP_RETRANS; 4705a698243STrond Myklebust if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 471259875efSTrond Myklebust to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; 4725006a76cSDavid Howells if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 4735006a76cSDavid Howells to->to_initval = NFS_MAX_TCP_TIMEOUT; 4745006a76cSDavid Howells to->to_increment = to->to_initval; 4755006a76cSDavid Howells to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); 4767a3e3e18STrond Myklebust if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) 4777a3e3e18STrond Myklebust to->to_maxval = NFS_MAX_TCP_TIMEOUT; 4787a3e3e18STrond Myklebust if (to->to_maxval < to->to_initval) 4797a3e3e18STrond Myklebust to->to_maxval = to->to_initval; 4805006a76cSDavid Howells to->to_exponential = 0; 4815006a76cSDavid Howells break; 4820896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_UDP: 483a956bedaSTrond Myklebust if (retrans == NFS_UNSPEC_RETRANS) 484259875efSTrond Myklebust to->to_retries = NFS_DEF_UDP_RETRANS; 485a956bedaSTrond Myklebust if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 486259875efSTrond Myklebust to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; 4875006a76cSDavid Howells if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 4885006a76cSDavid Howells to->to_initval = NFS_MAX_UDP_TIMEOUT; 4895006a76cSDavid Howells to->to_maxval = NFS_MAX_UDP_TIMEOUT; 4905006a76cSDavid Howells to->to_exponential = 1; 4915006a76cSDavid Howells break; 492259875efSTrond Myklebust default: 493259875efSTrond Myklebust BUG(); 4945006a76cSDavid Howells } 4955006a76cSDavid Howells } 49689d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_timeout_values); 4975006a76cSDavid Howells 4985006a76cSDavid Howells /* 4995006a76cSDavid Howells * Create an RPC client handle 5005006a76cSDavid Howells */ 501428360d7SBryan Schumaker int nfs_create_rpc_client(struct nfs_client *clp, 5025c6e5b60STrond Myklebust const struct nfs_client_initdata *cl_init, 5034bf590e0SChuck Lever rpc_authflavor_t flavor) 5045006a76cSDavid Howells { 5055006a76cSDavid Howells struct rpc_clnt *clnt = NULL; 50641877d20SChuck Lever struct rpc_create_args args = { 50773ea666cSChuck Lever .net = clp->cl_net, 50859dca3b2STrond Myklebust .protocol = clp->cl_proto, 5096619079dSTrond Myklebust .nconnect = clp->cl_nconnect, 51041877d20SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 5116e4cffd7SChuck Lever .addrsize = clp->cl_addrlen, 5125c6e5b60STrond Myklebust .timeout = cl_init->timeparms, 51341877d20SChuck Lever .servername = clp->cl_hostname, 5145c6e5b60STrond Myklebust .nodename = cl_init->nodename, 51541877d20SChuck Lever .program = &nfs_program, 51641877d20SChuck Lever .version = clp->rpc_ops->version, 51741877d20SChuck Lever .authflavor = flavor, 5181a58e8a0STrond Myklebust .cred = cl_init->cred, 519c8407f2eSChuck Lever .xprtsec = cl_init->xprtsec, 52041877d20SChuck Lever }; 5215006a76cSDavid Howells 5224bf590e0SChuck Lever if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags)) 5234a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_DISCRTRY; 52499875249STrond Myklebust if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags)) 52599875249STrond Myklebust args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT; 5264bf590e0SChuck Lever if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags)) 5274a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; 52898f98cf5STrond Myklebust if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags)) 52998f98cf5STrond Myklebust args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS; 5304b1b69ceSTrond Myklebust if (test_bit(NFS_CS_NOPING, &clp->cl_flags)) 5314b1b69ceSTrond Myklebust args.flags |= RPC_CLNT_CREATE_NOPING; 532e6237b6fSTrond Myklebust if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags)) 533e6237b6fSTrond Myklebust args.flags |= RPC_CLNT_CREATE_REUSEPORT; 5344a01b8a4SChuck Lever 5355006a76cSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 5365006a76cSDavid Howells return 0; 5375006a76cSDavid Howells 53841877d20SChuck Lever clnt = rpc_create(&args); 5395006a76cSDavid Howells if (IS_ERR(clnt)) { 5405006a76cSDavid Howells dprintk("%s: cannot create RPC client. Error = %ld\n", 5413110ff80SHarvey Harrison __func__, PTR_ERR(clnt)); 5425006a76cSDavid Howells return PTR_ERR(clnt); 5435006a76cSDavid Howells } 5445006a76cSDavid Howells 5455e16923bSNeilBrown clnt->cl_principal = clp->cl_principal; 5465006a76cSDavid Howells clp->cl_rpcclient = clnt; 547dc48e0abSOlga Kornievskaia clnt->cl_max_connect = clp->cl_max_connect; 5485006a76cSDavid Howells return 0; 5495006a76cSDavid Howells } 55089d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_rpc_client); 55154ceac45SDavid Howells 55254ceac45SDavid Howells /* 55354ceac45SDavid Howells * Version 2 or 3 client destruction 55454ceac45SDavid Howells */ 55554ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server) 55654ceac45SDavid Howells { 557f259613aSNeilBrown if (server->nlm_host) 5589289e7f9SChuck Lever nlmclnt_done(server->nlm_host); 55954ceac45SDavid Howells } 56054ceac45SDavid Howells 56154ceac45SDavid Howells /* 56254ceac45SDavid Howells * Version 2 or 3 lockd setup 56354ceac45SDavid Howells */ 56454ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server) 56554ceac45SDavid Howells { 5669289e7f9SChuck Lever struct nlm_host *host; 5679289e7f9SChuck Lever struct nfs_client *clp = server->nfs_client; 568883bb163SChuck Lever struct nlmclnt_initdata nlm_init = { 569883bb163SChuck Lever .hostname = clp->cl_hostname, 570883bb163SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 571883bb163SChuck Lever .addrlen = clp->cl_addrlen, 572883bb163SChuck Lever .nfs_version = clp->rpc_ops->version, 5730cb2659bSChuck Lever .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? 5740cb2659bSChuck Lever 1 : 0, 57573ea666cSChuck Lever .net = clp->cl_net, 576b1ece737SBenjamin Coddington .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops, 577d18a9d3fSSargun Dhillon .cred = server->cred, 578883bb163SChuck Lever }; 57954ceac45SDavid Howells 580883bb163SChuck Lever if (nlm_init.nfs_version > 3) 5819289e7f9SChuck Lever return 0; 5825eebde23SSuresh Jayaraman if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) && 5835eebde23SSuresh Jayaraman (server->flags & NFS_MOUNT_LOCAL_FCNTL)) 5849289e7f9SChuck Lever return 0; 5859289e7f9SChuck Lever 5868a6e5debSTrond Myklebust switch (clp->cl_proto) { 5878a6e5debSTrond Myklebust default: 5888a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_TCP; 5898a6e5debSTrond Myklebust break; 590b24ee6c6SOlga Kornievskaia #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT 5918a6e5debSTrond Myklebust case XPRT_TRANSPORT_UDP: 5928a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_UDP; 593b24ee6c6SOlga Kornievskaia #endif 5948a6e5debSTrond Myklebust } 5958a6e5debSTrond Myklebust 596883bb163SChuck Lever host = nlmclnt_init(&nlm_init); 5979289e7f9SChuck Lever if (IS_ERR(host)) 5989289e7f9SChuck Lever return PTR_ERR(host); 5999289e7f9SChuck Lever 6009289e7f9SChuck Lever server->nlm_host = host; 60154ceac45SDavid Howells server->destroy = nfs_destroy_server; 6029289e7f9SChuck Lever return 0; 60354ceac45SDavid Howells } 60454ceac45SDavid Howells 60554ceac45SDavid Howells /* 60654ceac45SDavid Howells * Create a general RPC client 60754ceac45SDavid Howells */ 608fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *server, 60933170233STrond Myklebust const struct rpc_timeout *timeo, 61033170233STrond Myklebust rpc_authflavor_t pseudoflavour) 61154ceac45SDavid Howells { 61254ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 61354ceac45SDavid Howells 614ba9b584cSChuck Lever server->client = rpc_clone_client_set_auth(clp->cl_rpcclient, 615ba9b584cSChuck Lever pseudoflavour); 61654ceac45SDavid Howells if (IS_ERR(server->client)) { 6173110ff80SHarvey Harrison dprintk("%s: couldn't create rpc_client!\n", __func__); 61854ceac45SDavid Howells return PTR_ERR(server->client); 61954ceac45SDavid Howells } 62054ceac45SDavid Howells 62133170233STrond Myklebust memcpy(&server->client->cl_timeout_default, 62233170233STrond Myklebust timeo, 62333170233STrond Myklebust sizeof(server->client->cl_timeout_default)); 62433170233STrond Myklebust server->client->cl_timeout = &server->client->cl_timeout_default; 62554ceac45SDavid Howells server->client->cl_softrtry = 0; 62691a575e1STrond Myklebust if (server->flags & NFS_MOUNT_SOFTERR) 62791a575e1STrond Myklebust server->client->cl_softerr = 1; 62854ceac45SDavid Howells if (server->flags & NFS_MOUNT_SOFT) 62954ceac45SDavid Howells server->client->cl_softrtry = 1; 63054ceac45SDavid Howells 631*e13b5493SBenjamin Coddington nfs_sysfs_link_rpc_client(server, server->client, NULL); 63254ceac45SDavid Howells return 0; 63354ceac45SDavid Howells } 63489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient); 63554ceac45SDavid Howells 6368cab4c39SChuck Lever /** 6378cab4c39SChuck Lever * nfs_init_client - Initialise an NFS2 or NFS3 client 6388cab4c39SChuck Lever * 6398cab4c39SChuck Lever * @clp: nfs_client to initialise 6405c6e5b60STrond Myklebust * @cl_init: Initialisation parameters 6418cab4c39SChuck Lever * 6428cab4c39SChuck Lever * Returns pointer to an NFS client, or an ERR_PTR value. 64354ceac45SDavid Howells */ 6448cab4c39SChuck Lever struct nfs_client *nfs_init_client(struct nfs_client *clp, 6455c6e5b60STrond Myklebust const struct nfs_client_initdata *cl_init) 64654ceac45SDavid Howells { 64754ceac45SDavid Howells int error; 64854ceac45SDavid Howells 64954ceac45SDavid Howells /* the client is already initialised */ 6502844b6aeSAnna Schumaker if (clp->cl_cons_state == NFS_CS_READY) 6518cab4c39SChuck Lever return clp; 65254ceac45SDavid Howells 65354ceac45SDavid Howells /* 65454ceac45SDavid Howells * Create a client RPC handle for doing FSSTAT with UNIX auth only 65554ceac45SDavid Howells * - RFC 2623, sec 2.3.2 65654ceac45SDavid Howells */ 6575c6e5b60STrond Myklebust error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX); 6582844b6aeSAnna Schumaker nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error); 6592844b6aeSAnna Schumaker if (error < 0) { 6608cab4c39SChuck Lever nfs_put_client(clp); 6612844b6aeSAnna Schumaker clp = ERR_PTR(error); 6622844b6aeSAnna Schumaker } 6632844b6aeSAnna Schumaker return clp; 66454ceac45SDavid Howells } 665ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_client); 66654ceac45SDavid Howells 66754ceac45SDavid Howells /* 66854ceac45SDavid Howells * Create a version 2 or 3 client 66954ceac45SDavid Howells */ 6702283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server, 67162a55d08SScott Mayhew const struct fs_context *fc) 67254ceac45SDavid Howells { 67362a55d08SScott Mayhew const struct nfs_fs_context *ctx = nfs_fc2context(fc); 6745c6e5b60STrond Myklebust struct rpc_timeout timeparms; 6753a498026STrond Myklebust struct nfs_client_initdata cl_init = { 67638465f5dSScott Mayhew .hostname = ctx->nfs_server.hostname, 677cf0d7e7fSKees Cook .addr = &ctx->nfs_server._address, 67838465f5dSScott Mayhew .addrlen = ctx->nfs_server.addrlen, 67962a55d08SScott Mayhew .nfs_mod = ctx->nfs_mod, 68038465f5dSScott Mayhew .proto = ctx->nfs_server.protocol, 68162a55d08SScott Mayhew .net = fc->net_ns, 6825c6e5b60STrond Myklebust .timeparms = &timeparms, 6831a58e8a0STrond Myklebust .cred = server->cred, 68438465f5dSScott Mayhew .nconnect = ctx->nfs_server.nconnect, 685e6237b6fSTrond Myklebust .init_flags = (1UL << NFS_CS_REUSEPORT), 686c8407f2eSChuck Lever .xprtsec = ctx->xprtsec, 6873a498026STrond Myklebust }; 68854ceac45SDavid Howells struct nfs_client *clp; 6893a498026STrond Myklebust int error; 69054ceac45SDavid Howells 69138465f5dSScott Mayhew nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol, 69238465f5dSScott Mayhew ctx->timeo, ctx->retrans); 69338465f5dSScott Mayhew if (ctx->flags & NFS_MOUNT_NORESVPORT) 6944bf590e0SChuck Lever set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); 69545a52a02SAndy Adamson 69654ceac45SDavid Howells /* Allocate or find a client reference we can use */ 6977d38de3fSAnna Schumaker clp = nfs_get_client(&cl_init); 6984cbb9768SAnna Schumaker if (IS_ERR(clp)) 69954ceac45SDavid Howells return PTR_ERR(clp); 70054ceac45SDavid Howells 70154ceac45SDavid Howells server->nfs_client = clp; 7021c725118SBenjamin Coddington nfs_sysfs_add_server(server); 703*e13b5493SBenjamin Coddington nfs_sysfs_link_rpc_client(server, clp->cl_rpcclient, "_state"); 70454ceac45SDavid Howells 70554ceac45SDavid Howells /* Initialise the client representation from the mount data */ 70638465f5dSScott Mayhew server->flags = ctx->flags; 70738465f5dSScott Mayhew server->options = ctx->options; 708ce62b114STrond Myklebust server->caps |= NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS; 709ce62b114STrond Myklebust 710ce62b114STrond Myklebust switch (clp->rpc_ops->version) { 711ce62b114STrond Myklebust case 2: 712ce62b114STrond Myklebust server->fattr_valid = NFS_ATTR_FATTR_V2; 713ce62b114STrond Myklebust break; 714ce62b114STrond Myklebust case 3: 715ce62b114STrond Myklebust server->fattr_valid = NFS_ATTR_FATTR_V3; 716ce62b114STrond Myklebust break; 717ce62b114STrond Myklebust default: 718ce62b114STrond Myklebust server->fattr_valid = NFS_ATTR_FATTR_V4; 719ce62b114STrond Myklebust } 72054ceac45SDavid Howells 72138465f5dSScott Mayhew if (ctx->rsize) 722940261a1SAnna Schumaker server->rsize = nfs_io_size(ctx->rsize, clp->cl_proto); 72338465f5dSScott Mayhew if (ctx->wsize) 724940261a1SAnna Schumaker server->wsize = nfs_io_size(ctx->wsize, clp->cl_proto); 72554ceac45SDavid Howells 72638465f5dSScott Mayhew server->acregmin = ctx->acregmin * HZ; 72738465f5dSScott Mayhew server->acregmax = ctx->acregmax * HZ; 72838465f5dSScott Mayhew server->acdirmin = ctx->acdirmin * HZ; 72938465f5dSScott Mayhew server->acdirmax = ctx->acdirmax * HZ; 73054ceac45SDavid Howells 73154ceac45SDavid Howells /* Start lockd here, before we might error out */ 73254ceac45SDavid Howells error = nfs_start_lockd(server); 73354ceac45SDavid Howells if (error < 0) 73454ceac45SDavid Howells goto error; 73554ceac45SDavid Howells 73638465f5dSScott Mayhew server->port = ctx->nfs_server.port; 73738465f5dSScott Mayhew server->auth_info = ctx->auth_info; 738f22d6d79SChuck Lever 739a3f73c27SWeston Andros Adamson error = nfs_init_server_rpcclient(server, &timeparms, 74038465f5dSScott Mayhew ctx->selected_flavor); 74154ceac45SDavid Howells if (error < 0) 74254ceac45SDavid Howells goto error; 74354ceac45SDavid Howells 7443f8400d1SChuck Lever /* Preserve the values of mount_server-related mount options */ 74538465f5dSScott Mayhew if (ctx->mount_server.addrlen) { 74638465f5dSScott Mayhew memcpy(&server->mountd_address, &ctx->mount_server.address, 74738465f5dSScott Mayhew ctx->mount_server.addrlen); 74838465f5dSScott Mayhew server->mountd_addrlen = ctx->mount_server.addrlen; 7493f8400d1SChuck Lever } 75038465f5dSScott Mayhew server->mountd_version = ctx->mount_server.version; 75138465f5dSScott Mayhew server->mountd_port = ctx->mount_server.port; 75238465f5dSScott Mayhew server->mountd_protocol = ctx->mount_server.protocol; 7533f8400d1SChuck Lever 75438465f5dSScott Mayhew server->namelen = ctx->namlen; 75554ceac45SDavid Howells return 0; 75654ceac45SDavid Howells 75754ceac45SDavid Howells error: 75854ceac45SDavid Howells server->nfs_client = NULL; 75954ceac45SDavid Howells nfs_put_client(clp); 76054ceac45SDavid Howells return error; 76154ceac45SDavid Howells } 76254ceac45SDavid Howells 76354ceac45SDavid Howells /* 76454ceac45SDavid Howells * Load up the server record from information gained in an fsinfo record 76554ceac45SDavid Howells */ 766738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server, 767738fd0f3SBenny Halevy struct nfs_fsinfo *fsinfo) 76854ceac45SDavid Howells { 769940261a1SAnna Schumaker struct nfs_client *clp = server->nfs_client; 77004a5da69SFrank van der Linden unsigned long max_rpc_payload, raw_max_rpc_payload; 77154ceac45SDavid Howells 77254ceac45SDavid Howells /* Work out a lot of parameters */ 77354ceac45SDavid Howells if (server->rsize == 0) 774940261a1SAnna Schumaker server->rsize = nfs_io_size(fsinfo->rtpref, clp->cl_proto); 77554ceac45SDavid Howells if (server->wsize == 0) 776940261a1SAnna Schumaker server->wsize = nfs_io_size(fsinfo->wtpref, clp->cl_proto); 77754ceac45SDavid Howells 77854ceac45SDavid Howells if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) 779940261a1SAnna Schumaker server->rsize = nfs_io_size(fsinfo->rtmax, clp->cl_proto); 78054ceac45SDavid Howells if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) 781940261a1SAnna Schumaker server->wsize = nfs_io_size(fsinfo->wtmax, clp->cl_proto); 78254ceac45SDavid Howells 78304a5da69SFrank van der Linden raw_max_rpc_payload = rpc_max_payload(server->client); 78404a5da69SFrank van der Linden max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL); 78504a5da69SFrank van der Linden 78654ceac45SDavid Howells if (server->rsize > max_rpc_payload) 78754ceac45SDavid Howells server->rsize = max_rpc_payload; 78854ceac45SDavid Howells if (server->rsize > NFS_MAX_FILE_IO_SIZE) 78954ceac45SDavid Howells server->rsize = NFS_MAX_FILE_IO_SIZE; 79009cbfeafSKirill A. Shutemov server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 791e0bf68ddSPeter Zijlstra 79254ceac45SDavid Howells if (server->wsize > max_rpc_payload) 79354ceac45SDavid Howells server->wsize = max_rpc_payload; 79454ceac45SDavid Howells if (server->wsize > NFS_MAX_FILE_IO_SIZE) 79554ceac45SDavid Howells server->wsize = NFS_MAX_FILE_IO_SIZE; 79609cbfeafSKirill A. Shutemov server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 79785e174baSRicardo Labiaga 79854ceac45SDavid Howells server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); 79954ceac45SDavid Howells 80054ceac45SDavid Howells server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); 8011a34c8c9STrond Myklebust if (server->dtsize > NFS_MAX_FILE_IO_SIZE) 8021a34c8c9STrond Myklebust server->dtsize = NFS_MAX_FILE_IO_SIZE; 80354ceac45SDavid Howells if (server->dtsize > server->rsize) 80454ceac45SDavid Howells server->dtsize = server->rsize; 80554ceac45SDavid Howells 80654ceac45SDavid Howells if (server->flags & NFS_MOUNT_NOAC) { 80754ceac45SDavid Howells server->acregmin = server->acregmax = 0; 80854ceac45SDavid Howells server->acdirmin = server->acdirmax = 0; 80954ceac45SDavid Howells } 81054ceac45SDavid Howells 81154ceac45SDavid Howells server->maxfilesize = fsinfo->maxfilesize; 81254ceac45SDavid Howells 8136b96724eSRicardo Labiaga server->time_delta = fsinfo->time_delta; 8147f08a335STrond Myklebust server->change_attr_type = fsinfo->change_attr_type; 8156b96724eSRicardo Labiaga 8162a92ee92SPeng Tao server->clone_blksize = fsinfo->clone_blksize; 81754ceac45SDavid Howells /* We're airborne Set socket buffersize */ 81854ceac45SDavid Howells rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); 81904a5da69SFrank van der Linden 82004a5da69SFrank van der Linden #ifdef CONFIG_NFS_V4_2 82104a5da69SFrank van der Linden /* 82204a5da69SFrank van der Linden * Defaults until limited by the session parameters. 82304a5da69SFrank van der Linden */ 82404a5da69SFrank van der Linden server->gxasize = min_t(unsigned int, raw_max_rpc_payload, 82504a5da69SFrank van der Linden XATTR_SIZE_MAX); 82604a5da69SFrank van der Linden server->sxasize = min_t(unsigned int, raw_max_rpc_payload, 82704a5da69SFrank van der Linden XATTR_SIZE_MAX); 82804a5da69SFrank van der Linden server->lxasize = min_t(unsigned int, raw_max_rpc_payload, 82904a5da69SFrank van der Linden nfs42_listxattr_xdrsize(XATTR_LIST_MAX)); 830b78ef845SFrank van der Linden 831b78ef845SFrank van der Linden if (fsinfo->xattr_support) 832b78ef845SFrank van der Linden server->caps |= NFS_CAP_XATTR; 83304a5da69SFrank van der Linden #endif 83454ceac45SDavid Howells } 83554ceac45SDavid Howells 83654ceac45SDavid Howells /* 83754ceac45SDavid Howells * Probe filesystem information, including the FSID on v2/v3 83854ceac45SDavid Howells */ 8395fe1210dSAnna Schumaker static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 84054ceac45SDavid Howells { 84154ceac45SDavid Howells struct nfs_fsinfo fsinfo; 84254ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 84354ceac45SDavid Howells int error; 84454ceac45SDavid Howells 84554ceac45SDavid Howells if (clp->rpc_ops->set_capabilities != NULL) { 84654ceac45SDavid Howells error = clp->rpc_ops->set_capabilities(server, mntfh); 84754ceac45SDavid Howells if (error < 0) 8484cbb9768SAnna Schumaker return error; 84954ceac45SDavid Howells } 85054ceac45SDavid Howells 85154ceac45SDavid Howells fsinfo.fattr = fattr; 852ca440c38SJeff Layton fsinfo.nlayouttypes = 0; 8533132e49eSJeff Layton memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype)); 85454ceac45SDavid Howells error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); 85554ceac45SDavid Howells if (error < 0) 8564cbb9768SAnna Schumaker return error; 85754ceac45SDavid Howells 85871f81e51SKinglong Mee nfs_server_set_fsinfo(server, &fsinfo); 85954ceac45SDavid Howells 86054ceac45SDavid Howells /* Get some general file system info */ 86154ceac45SDavid Howells if (server->namelen == 0) { 86254ceac45SDavid Howells struct nfs_pathconf pathinfo; 86354ceac45SDavid Howells 86454ceac45SDavid Howells pathinfo.fattr = fattr; 86554ceac45SDavid Howells nfs_fattr_init(fattr); 86654ceac45SDavid Howells 86754ceac45SDavid Howells if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) 86854ceac45SDavid Howells server->namelen = pathinfo.max_namelen; 86954ceac45SDavid Howells } 87054ceac45SDavid Howells 8711976b2b3SOlga Kornievskaia if (clp->rpc_ops->discover_trunking != NULL && 872a43bf604SOlga Kornievskaia (server->caps & NFS_CAP_FS_LOCATIONS && 873a43bf604SOlga Kornievskaia (server->flags & NFS_MOUNT_TRUNK_DISCOVERY))) { 8741976b2b3SOlga Kornievskaia error = clp->rpc_ops->discover_trunking(server, mntfh); 8751976b2b3SOlga Kornievskaia if (error < 0) 8761976b2b3SOlga Kornievskaia return error; 8771976b2b3SOlga Kornievskaia } 8781976b2b3SOlga Kornievskaia 87954ceac45SDavid Howells return 0; 88054ceac45SDavid Howells } 88154ceac45SDavid Howells 88254ceac45SDavid Howells /* 883e5731131SAnna Schumaker * Grab the destination's particulars, including lease expiry time. 884e5731131SAnna Schumaker * 885e5731131SAnna Schumaker * Returns zero if probe succeeded and retrieved FSID matches the FSID 886e5731131SAnna Schumaker * we have cached. 887e5731131SAnna Schumaker */ 888e5731131SAnna Schumaker int nfs_probe_server(struct nfs_server *server, struct nfs_fh *mntfh) 889e5731131SAnna Schumaker { 890e5731131SAnna Schumaker struct nfs_fattr *fattr; 891e5731131SAnna Schumaker int error; 892e5731131SAnna Schumaker 893e5731131SAnna Schumaker fattr = nfs_alloc_fattr(); 894e5731131SAnna Schumaker if (fattr == NULL) 895e5731131SAnna Schumaker return -ENOMEM; 896e5731131SAnna Schumaker 897e5731131SAnna Schumaker /* Sanity: the probe won't work if the destination server 898e5731131SAnna Schumaker * does not recognize the migrated FH. */ 899e5731131SAnna Schumaker error = nfs_probe_fsinfo(server, mntfh, fattr); 900e5731131SAnna Schumaker 901e5731131SAnna Schumaker nfs_free_fattr(fattr); 902e5731131SAnna Schumaker return error; 903e5731131SAnna Schumaker } 904e5731131SAnna Schumaker EXPORT_SYMBOL_GPL(nfs_probe_server); 905e5731131SAnna Schumaker 906e5731131SAnna Schumaker /* 90754ceac45SDavid Howells * Copy useful information when duplicating a server record 90854ceac45SDavid Howells */ 909fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 91054ceac45SDavid Howells { 91154ceac45SDavid Howells target->flags = source->flags; 912356e76b8SChuck Lever target->rsize = source->rsize; 913356e76b8SChuck Lever target->wsize = source->wsize; 91454ceac45SDavid Howells target->acregmin = source->acregmin; 91554ceac45SDavid Howells target->acregmax = source->acregmax; 91654ceac45SDavid Howells target->acdirmin = source->acdirmin; 91754ceac45SDavid Howells target->acdirmax = source->acdirmax; 91854ceac45SDavid Howells target->caps = source->caps; 9192df54806SDavid Howells target->options = source->options; 9200f5f49b8SWeston Andros Adamson target->auth_info = source->auth_info; 92189a6814dSSteve Dickson target->port = source->port; 92254ceac45SDavid Howells } 92389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_copy_userdata); 92454ceac45SDavid Howells 925fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *server) 926fca5238eSChuck Lever { 927fca5238eSChuck Lever struct nfs_client *clp = server->nfs_client; 92873ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 929fca5238eSChuck Lever 930dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 931fca5238eSChuck Lever list_add_tail_rcu(&server->client_link, &clp->cl_superblocks); 932c25d32b2SStanislav Kinsbursky list_add_tail(&server->master_link, &nn->nfs_volume_list); 933d3b4c9d7SAndy Adamson clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 934dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 935fca5238eSChuck Lever 936fca5238eSChuck Lever } 93789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_insert_lists); 938fca5238eSChuck Lever 93932e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *server) 940fca5238eSChuck Lever { 941d3b4c9d7SAndy Adamson struct nfs_client *clp = server->nfs_client; 9424c03ae4aSTrond Myklebust struct nfs_net *nn; 943d3b4c9d7SAndy Adamson 9444c03ae4aSTrond Myklebust if (clp == NULL) 9454c03ae4aSTrond Myklebust return; 94673ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 947dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 948fca5238eSChuck Lever list_del_rcu(&server->client_link); 9494c03ae4aSTrond Myklebust if (list_empty(&clp->cl_superblocks)) 950d3b4c9d7SAndy Adamson set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 951fca5238eSChuck Lever list_del(&server->master_link); 952dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 953fca5238eSChuck Lever 954fca5238eSChuck Lever synchronize_rcu(); 955fca5238eSChuck Lever } 95632e62b7cSChuck Lever EXPORT_SYMBOL_GPL(nfs_server_remove_lists); 957fca5238eSChuck Lever 9581c725118SBenjamin Coddington static DEFINE_IDA(s_sysfs_ids); 9591c725118SBenjamin Coddington 96054ceac45SDavid Howells /* 96154ceac45SDavid Howells * Allocate and initialise a server record 96254ceac45SDavid Howells */ 963fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void) 96454ceac45SDavid Howells { 96554ceac45SDavid Howells struct nfs_server *server; 96654ceac45SDavid Howells 96754ceac45SDavid Howells server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); 96854ceac45SDavid Howells if (!server) 96954ceac45SDavid Howells return NULL; 97054ceac45SDavid Howells 9711c725118SBenjamin Coddington server->s_sysfs_id = ida_alloc(&s_sysfs_ids, GFP_KERNEL); 9721c725118SBenjamin Coddington if (server->s_sysfs_id < 0) { 9731c725118SBenjamin Coddington kfree(server); 9741c725118SBenjamin Coddington return NULL; 9751c725118SBenjamin Coddington } 9761c725118SBenjamin Coddington 97754ceac45SDavid Howells server->client = server->client_acl = ERR_PTR(-EINVAL); 97854ceac45SDavid Howells 97954ceac45SDavid Howells /* Zero out the NFS state stuff */ 98054ceac45SDavid Howells INIT_LIST_HEAD(&server->client_link); 98154ceac45SDavid Howells INIT_LIST_HEAD(&server->master_link); 982d3978bb3SChuck Lever INIT_LIST_HEAD(&server->delegations); 9836382a441SWeston Andros Adamson INIT_LIST_HEAD(&server->layouts); 9840aaaf5c4SChuck Lever INIT_LIST_HEAD(&server->state_owners_lru); 98562164f31SOlga Kornievskaia INIT_LIST_HEAD(&server->ss_copies); 98654ceac45SDavid Howells 987ef818a28SSteve Dickson atomic_set(&server->active, 0); 988ef818a28SSteve Dickson 98954ceac45SDavid Howells server->io_stats = nfs_alloc_iostats(); 99054ceac45SDavid Howells if (!server->io_stats) { 99154ceac45SDavid Howells kfree(server); 99254ceac45SDavid Howells return NULL; 99354ceac45SDavid Howells } 99454ceac45SDavid Howells 9957f08a335STrond Myklebust server->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED; 9967f08a335STrond Myklebust 9979157c31dSTrond Myklebust ida_init(&server->openowner_id); 998d2d7ce28STrond Myklebust ida_init(&server->lockowner_id); 999f7e8917aSFred Isaman pnfs_init_server(server); 100068ebf8feSBenjamin Coddington rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC"); 1001f7e8917aSFred Isaman 100254ceac45SDavid Howells return server; 100354ceac45SDavid Howells } 100489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_server); 100554ceac45SDavid Howells 100654ceac45SDavid Howells /* 100754ceac45SDavid Howells * Free up a server record 100854ceac45SDavid Howells */ 100954ceac45SDavid Howells void nfs_free_server(struct nfs_server *server) 101054ceac45SDavid Howells { 1011fca5238eSChuck Lever nfs_server_remove_lists(server); 101254ceac45SDavid Howells 101354ceac45SDavid Howells if (server->destroy != NULL) 101454ceac45SDavid Howells server->destroy(server); 10155cef338bSTrond Myklebust 10165cef338bSTrond Myklebust if (!IS_ERR(server->client_acl)) 10175cef338bSTrond Myklebust rpc_shutdown_client(server->client_acl); 101854ceac45SDavid Howells if (!IS_ERR(server->client)) 101954ceac45SDavid Howells rpc_shutdown_client(server->client); 102054ceac45SDavid Howells 102154ceac45SDavid Howells nfs_put_client(server->nfs_client); 102254ceac45SDavid Howells 10231c725118SBenjamin Coddington nfs_sysfs_remove_server(server); 10241c725118SBenjamin Coddington kobject_put(&server->kobj); 10251c725118SBenjamin Coddington ida_free(&s_sysfs_ids, server->s_sysfs_id); 10261c725118SBenjamin Coddington 1027d2d7ce28STrond Myklebust ida_destroy(&server->lockowner_id); 10289157c31dSTrond Myklebust ida_destroy(&server->openowner_id); 102954ceac45SDavid Howells nfs_free_iostats(server->io_stats); 10301a58e8a0STrond Myklebust put_cred(server->cred); 103154ceac45SDavid Howells kfree(server); 103254ceac45SDavid Howells nfs_release_automount_timer(); 103354ceac45SDavid Howells } 103489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_server); 103554ceac45SDavid Howells 103654ceac45SDavid Howells /* 103754ceac45SDavid Howells * Create a version 2 or 3 volume record 103854ceac45SDavid Howells * - keyed on server and FSID 103954ceac45SDavid Howells */ 104062a55d08SScott Mayhew struct nfs_server *nfs_create_server(struct fs_context *fc) 104154ceac45SDavid Howells { 104262a55d08SScott Mayhew struct nfs_fs_context *ctx = nfs_fc2context(fc); 104354ceac45SDavid Howells struct nfs_server *server; 1044fbca779aSTrond Myklebust struct nfs_fattr *fattr; 104554ceac45SDavid Howells int error; 104654ceac45SDavid Howells 104754ceac45SDavid Howells server = nfs_alloc_server(); 104854ceac45SDavid Howells if (!server) 104954ceac45SDavid Howells return ERR_PTR(-ENOMEM); 105054ceac45SDavid Howells 1051d18a9d3fSSargun Dhillon server->cred = get_cred(fc->cred); 10521a58e8a0STrond Myklebust 1053fbca779aSTrond Myklebust error = -ENOMEM; 1054fbca779aSTrond Myklebust fattr = nfs_alloc_fattr(); 1055fbca779aSTrond Myklebust if (fattr == NULL) 1056fbca779aSTrond Myklebust goto error; 1057fbca779aSTrond Myklebust 105854ceac45SDavid Howells /* Get a client representation */ 105962a55d08SScott Mayhew error = nfs_init_server(server, fc); 106054ceac45SDavid Howells if (error < 0) 106154ceac45SDavid Howells goto error; 106254ceac45SDavid Howells 106354ceac45SDavid Howells /* Probe the root fh to retrieve its FSID */ 106462a55d08SScott Mayhew error = nfs_probe_fsinfo(server, ctx->mntfh, fattr); 106554ceac45SDavid Howells if (error < 0) 106654ceac45SDavid Howells goto error; 106754af3bb5STrond Myklebust if (server->nfs_client->rpc_ops->version == 3) { 106854af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) 106954af3bb5STrond Myklebust server->namelen = NFS3_MAXNAMLEN; 107062a55d08SScott Mayhew if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS)) 107154af3bb5STrond Myklebust server->caps |= NFS_CAP_READDIRPLUS; 107254af3bb5STrond Myklebust } else { 107354af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) 107454af3bb5STrond Myklebust server->namelen = NFS2_MAXNAMLEN; 107554af3bb5STrond Myklebust } 107654af3bb5STrond Myklebust 1077fbca779aSTrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR)) { 107862a55d08SScott Mayhew error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh, 10792ef61e0eSAnna Schumaker fattr, NULL); 108054ceac45SDavid Howells if (error < 0) { 108154ceac45SDavid Howells dprintk("nfs_create_server: getattr error = %d\n", -error); 108254ceac45SDavid Howells goto error; 108354ceac45SDavid Howells } 108454ceac45SDavid Howells } 1085fbca779aSTrond Myklebust memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 108654ceac45SDavid Howells 10876daabf1bSDavid Howells dprintk("Server FSID: %llx:%llx\n", 10886daabf1bSDavid Howells (unsigned long long) server->fsid.major, 10896daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 109054ceac45SDavid Howells 1091fca5238eSChuck Lever nfs_server_insert_lists(server); 109254ceac45SDavid Howells server->mount_time = jiffies; 1093fbca779aSTrond Myklebust nfs_free_fattr(fattr); 109454ceac45SDavid Howells return server; 109554ceac45SDavid Howells 109654ceac45SDavid Howells error: 1097fbca779aSTrond Myklebust nfs_free_fattr(fattr); 109854ceac45SDavid Howells nfs_free_server(server); 109954ceac45SDavid Howells return ERR_PTR(error); 110054ceac45SDavid Howells } 1101ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_server); 110254ceac45SDavid Howells 110354ceac45SDavid Howells /* 110454ceac45SDavid Howells * Clone an NFS2, NFS3 or NFS4 server record 110554ceac45SDavid Howells */ 110654ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source, 110754ceac45SDavid Howells struct nfs_fh *fh, 11087e6eb683SBryan Schumaker struct nfs_fattr *fattr, 11097e6eb683SBryan Schumaker rpc_authflavor_t flavor) 111054ceac45SDavid Howells { 111154ceac45SDavid Howells struct nfs_server *server; 111254ceac45SDavid Howells int error; 111354ceac45SDavid Howells 111454ceac45SDavid Howells server = nfs_alloc_server(); 111554ceac45SDavid Howells if (!server) 111654ceac45SDavid Howells return ERR_PTR(-ENOMEM); 111754ceac45SDavid Howells 11181a58e8a0STrond Myklebust server->cred = get_cred(source->cred); 11191a58e8a0STrond Myklebust 112054ceac45SDavid Howells /* Copy data from the source */ 112154ceac45SDavid Howells server->nfs_client = source->nfs_client; 11220aaaf5c4SChuck Lever server->destroy = source->destroy; 1123212bf41dSElena Reshetova refcount_inc(&server->nfs_client->cl_count); 112454ceac45SDavid Howells nfs_server_copy_userdata(server, source); 112554ceac45SDavid Howells 112654ceac45SDavid Howells server->fsid = fattr->fsid; 112754ceac45SDavid Howells 11281c725118SBenjamin Coddington nfs_sysfs_add_server(server); 11291c725118SBenjamin Coddington 1130*e13b5493SBenjamin Coddington nfs_sysfs_link_rpc_client(server, 1131*e13b5493SBenjamin Coddington server->nfs_client->cl_rpcclient, "_state"); 1132*e13b5493SBenjamin Coddington 113333170233STrond Myklebust error = nfs_init_server_rpcclient(server, 113433170233STrond Myklebust source->client->cl_timeout, 11357e6eb683SBryan Schumaker flavor); 113654ceac45SDavid Howells if (error < 0) 113754ceac45SDavid Howells goto out_free_server; 113854ceac45SDavid Howells 113954ceac45SDavid Howells /* probe the filesystem info for this server filesystem */ 11404d4cf8d2SAnna Schumaker error = nfs_probe_server(server, fh); 114154ceac45SDavid Howells if (error < 0) 114254ceac45SDavid Howells goto out_free_server; 114354ceac45SDavid Howells 114454af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 114554af3bb5STrond Myklebust server->namelen = NFS4_MAXNAMLEN; 114654af3bb5STrond Myklebust 114754ceac45SDavid Howells error = nfs_start_lockd(server); 114854ceac45SDavid Howells if (error < 0) 114954ceac45SDavid Howells goto out_free_server; 115054ceac45SDavid Howells 1151fca5238eSChuck Lever nfs_server_insert_lists(server); 115254ceac45SDavid Howells server->mount_time = jiffies; 115354ceac45SDavid Howells 115454ceac45SDavid Howells return server; 115554ceac45SDavid Howells 115654ceac45SDavid Howells out_free_server: 115754ceac45SDavid Howells nfs_free_server(server); 115854ceac45SDavid Howells return ERR_PTR(error); 115954ceac45SDavid Howells } 1160ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_clone_server); 11616aaca566SDavid Howells 11626b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net) 11636b13168bSStanislav Kinsbursky { 11646b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 11656b13168bSStanislav Kinsbursky 11666b13168bSStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_client_list); 1167c25d32b2SStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_volume_list); 116889d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 116928cd1b3fSStanislav Kinsbursky idr_init(&nn->cb_ident_idr); 117028cd1b3fSStanislav Kinsbursky #endif 11714c03ae4aSTrond Myklebust spin_lock_init(&nn->nfs_client_lock); 11722f86e091SDeepa Dinamani nn->boot_time = ktime_get_real(); 1173bf11fbdbSTrond Myklebust 1174bf11fbdbSTrond Myklebust nfs_netns_sysfs_setup(nn, net); 11756b13168bSStanislav Kinsbursky } 11766b13168bSStanislav Kinsbursky 117710b7a70cSTrond Myklebust void nfs_clients_exit(struct net *net) 117810b7a70cSTrond Myklebust { 117910b7a70cSTrond Myklebust struct nfs_net *nn = net_generic(net, nfs_net_id); 118010b7a70cSTrond Myklebust 1181bf11fbdbSTrond Myklebust nfs_netns_sysfs_destroy(nn); 118210b7a70cSTrond Myklebust nfs_cleanup_cb_ident_idr(net); 118310b7a70cSTrond Myklebust WARN_ON_ONCE(!list_empty(&nn->nfs_client_list)); 118410b7a70cSTrond Myklebust WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list)); 118510b7a70cSTrond Myklebust } 118610b7a70cSTrond Myklebust 11876aaca566SDavid Howells #ifdef CONFIG_PROC_FS 11886aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); 11896aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); 11906aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v); 11916aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v); 11926aaca566SDavid Howells 119388e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = { 11946aaca566SDavid Howells .start = nfs_server_list_start, 11956aaca566SDavid Howells .next = nfs_server_list_next, 11966aaca566SDavid Howells .stop = nfs_server_list_stop, 11976aaca566SDavid Howells .show = nfs_server_list_show, 11986aaca566SDavid Howells }; 11996aaca566SDavid Howells 12006aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); 12016aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); 12026aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v); 12036aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v); 12046aaca566SDavid Howells 120588e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = { 12066aaca566SDavid Howells .start = nfs_volume_list_start, 12076aaca566SDavid Howells .next = nfs_volume_list_next, 12086aaca566SDavid Howells .stop = nfs_volume_list_stop, 12096aaca566SDavid Howells .show = nfs_volume_list_show, 12106aaca566SDavid Howells }; 12116aaca566SDavid Howells 12126aaca566SDavid Howells /* 12136aaca566SDavid Howells * set up the iterator to start reading from the server list and return the first item 12146aaca566SDavid Howells */ 12156aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) 12168d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 12176aaca566SDavid Howells { 121865b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 12196b13168bSStanislav Kinsbursky 12206aaca566SDavid Howells /* lock the list against modification */ 1221dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 12226b13168bSStanislav Kinsbursky return seq_list_start_head(&nn->nfs_client_list, *_pos); 12236aaca566SDavid Howells } 12246aaca566SDavid Howells 12256aaca566SDavid Howells /* 12266aaca566SDavid Howells * move to next server 12276aaca566SDavid Howells */ 12286aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) 12296aaca566SDavid Howells { 123065b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 12316b13168bSStanislav Kinsbursky 12326b13168bSStanislav Kinsbursky return seq_list_next(v, &nn->nfs_client_list, pos); 12336aaca566SDavid Howells } 12346aaca566SDavid Howells 12356aaca566SDavid Howells /* 12366aaca566SDavid Howells * clean up after reading from the transports list 12376aaca566SDavid Howells */ 12386aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v) 12398d11620eSJeff Layton __releases(&nn->nfs_client_lock) 12406aaca566SDavid Howells { 124165b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1242dc030858SStanislav Kinsbursky 1243dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 12446aaca566SDavid Howells } 12456aaca566SDavid Howells 12466aaca566SDavid Howells /* 12476aaca566SDavid Howells * display a header line followed by a load of call lines 12486aaca566SDavid Howells */ 12496aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v) 12506aaca566SDavid Howells { 12516aaca566SDavid Howells struct nfs_client *clp; 125265b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 12536aaca566SDavid Howells 12546aaca566SDavid Howells /* display header on line 1 */ 12556b13168bSStanislav Kinsbursky if (v == &nn->nfs_client_list) { 12566aaca566SDavid Howells seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); 12576aaca566SDavid Howells return 0; 12586aaca566SDavid Howells } 12596aaca566SDavid Howells 12606aaca566SDavid Howells /* display one transport per line on subsequent lines */ 12616aaca566SDavid Howells clp = list_entry(v, struct nfs_client, cl_share_link); 12626aaca566SDavid Howells 1263940aab49SMalahal Naineni /* Check if the client is initialized */ 1264940aab49SMalahal Naineni if (clp->cl_cons_state != NFS_CS_READY) 1265940aab49SMalahal Naineni return 0; 1266940aab49SMalahal Naineni 12672446ab60STrond Myklebust rcu_read_lock(); 12685d8515caSChuck Lever seq_printf(m, "v%u %s %s %3d %s\n", 126940c55319STrond Myklebust clp->rpc_ops->version, 12705d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 12715d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 1272212bf41dSElena Reshetova refcount_read(&clp->cl_count), 12736aaca566SDavid Howells clp->cl_hostname); 12742446ab60STrond Myklebust rcu_read_unlock(); 12756aaca566SDavid Howells 12766aaca566SDavid Howells return 0; 12776aaca566SDavid Howells } 12786aaca566SDavid Howells 12796aaca566SDavid Howells /* 12806aaca566SDavid Howells * set up the iterator to start reading from the volume list and return the first item 12816aaca566SDavid Howells */ 12826aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) 12838d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 12846aaca566SDavid Howells { 128565b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1286c25d32b2SStanislav Kinsbursky 12876aaca566SDavid Howells /* lock the list against modification */ 1288dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 1289c25d32b2SStanislav Kinsbursky return seq_list_start_head(&nn->nfs_volume_list, *_pos); 12906aaca566SDavid Howells } 12916aaca566SDavid Howells 12926aaca566SDavid Howells /* 12936aaca566SDavid Howells * move to next volume 12946aaca566SDavid Howells */ 12956aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) 12966aaca566SDavid Howells { 129765b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1298c25d32b2SStanislav Kinsbursky 1299c25d32b2SStanislav Kinsbursky return seq_list_next(v, &nn->nfs_volume_list, pos); 13006aaca566SDavid Howells } 13016aaca566SDavid Howells 13026aaca566SDavid Howells /* 13036aaca566SDavid Howells * clean up after reading from the transports list 13046aaca566SDavid Howells */ 13056aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v) 13068d11620eSJeff Layton __releases(&nn->nfs_client_lock) 13076aaca566SDavid Howells { 130865b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1309dc030858SStanislav Kinsbursky 1310dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 13116aaca566SDavid Howells } 13126aaca566SDavid Howells 13136aaca566SDavid Howells /* 13146aaca566SDavid Howells * display a header line followed by a load of call lines 13156aaca566SDavid Howells */ 13166aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v) 13176aaca566SDavid Howells { 13186aaca566SDavid Howells struct nfs_server *server; 13196aaca566SDavid Howells struct nfs_client *clp; 1320df05a49fSKinglong Mee char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0' 1321df05a49fSKinglong Mee char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0' 132265b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 13236aaca566SDavid Howells 13246aaca566SDavid Howells /* display header on line 1 */ 1325c25d32b2SStanislav Kinsbursky if (v == &nn->nfs_volume_list) { 1326df05a49fSKinglong Mee seq_puts(m, "NV SERVER PORT DEV FSID" 1327df05a49fSKinglong Mee " FSC\n"); 13286aaca566SDavid Howells return 0; 13296aaca566SDavid Howells } 13306aaca566SDavid Howells /* display one transport per line on subsequent lines */ 13316aaca566SDavid Howells server = list_entry(v, struct nfs_server, master_link); 13326aaca566SDavid Howells clp = server->nfs_client; 13336aaca566SDavid Howells 1334df05a49fSKinglong Mee snprintf(dev, sizeof(dev), "%u:%u", 13356aaca566SDavid Howells MAJOR(server->s_dev), MINOR(server->s_dev)); 13366aaca566SDavid Howells 1337df05a49fSKinglong Mee snprintf(fsid, sizeof(fsid), "%llx:%llx", 13386daabf1bSDavid Howells (unsigned long long) server->fsid.major, 13396daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 13406aaca566SDavid Howells 13412446ab60STrond Myklebust rcu_read_lock(); 1342df05a49fSKinglong Mee seq_printf(m, "v%u %s %s %-12s %-33s %s\n", 134340c55319STrond Myklebust clp->rpc_ops->version, 13445d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 13455d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 13466aaca566SDavid Howells dev, 13475d1acff1SDavid Howells fsid, 13485d1acff1SDavid Howells nfs_server_fscache_state(server)); 13492446ab60STrond Myklebust rcu_read_unlock(); 13506aaca566SDavid Howells 13516aaca566SDavid Howells return 0; 13526aaca566SDavid Howells } 13536aaca566SDavid Howells 135465b38851SEric W. Biederman int nfs_fs_proc_net_init(struct net *net) 135565b38851SEric W. Biederman { 135665b38851SEric W. Biederman struct nfs_net *nn = net_generic(net, nfs_net_id); 135765b38851SEric W. Biederman struct proc_dir_entry *p; 135865b38851SEric W. Biederman 135965b38851SEric W. Biederman nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net); 136065b38851SEric W. Biederman if (!nn->proc_nfsfs) 136165b38851SEric W. Biederman goto error_0; 136265b38851SEric W. Biederman 136365b38851SEric W. Biederman /* a file of servers with which we're dealing */ 1364c3506372SChristoph Hellwig p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1365c3506372SChristoph Hellwig &nfs_server_list_ops, sizeof(struct seq_net_private)); 136665b38851SEric W. Biederman if (!p) 136765b38851SEric W. Biederman goto error_1; 136865b38851SEric W. Biederman 136965b38851SEric W. Biederman /* a file of volumes that we have mounted */ 1370c3506372SChristoph Hellwig p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1371c3506372SChristoph Hellwig &nfs_volume_list_ops, sizeof(struct seq_net_private)); 137265b38851SEric W. Biederman if (!p) 137321e81002SCong Wang goto error_1; 137465b38851SEric W. Biederman return 0; 137565b38851SEric W. Biederman 137665b38851SEric W. Biederman error_1: 137721e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 137865b38851SEric W. Biederman error_0: 137965b38851SEric W. Biederman return -ENOMEM; 138065b38851SEric W. Biederman } 138165b38851SEric W. Biederman 138265b38851SEric W. Biederman void nfs_fs_proc_net_exit(struct net *net) 138365b38851SEric W. Biederman { 138421e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 138565b38851SEric W. Biederman } 138665b38851SEric W. Biederman 13876aaca566SDavid Howells /* 13886aaca566SDavid Howells * initialise the /proc/fs/nfsfs/ directory 13896aaca566SDavid Howells */ 13906aaca566SDavid Howells int __init nfs_fs_proc_init(void) 13916aaca566SDavid Howells { 13926a062a36SKinglong Mee if (!proc_mkdir("fs/nfsfs", NULL)) 13936aaca566SDavid Howells goto error_0; 13946aaca566SDavid Howells 13956aaca566SDavid Howells /* a file of servers with which we're dealing */ 13966a062a36SKinglong Mee if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers")) 13976aaca566SDavid Howells goto error_1; 13986aaca566SDavid Howells 13996aaca566SDavid Howells /* a file of volumes that we have mounted */ 14006a062a36SKinglong Mee if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes")) 14016a062a36SKinglong Mee goto error_1; 14026aaca566SDavid Howells 14036a062a36SKinglong Mee return 0; 14046aaca566SDavid Howells error_1: 14056a062a36SKinglong Mee remove_proc_subtree("fs/nfsfs", NULL); 14066aaca566SDavid Howells error_0: 14076aaca566SDavid Howells return -ENOMEM; 14086aaca566SDavid Howells } 14096aaca566SDavid Howells 14106aaca566SDavid Howells /* 14116aaca566SDavid Howells * clean up the /proc/fs/nfsfs/ directory 14126aaca566SDavid Howells */ 14136aaca566SDavid Howells void nfs_fs_proc_exit(void) 14146aaca566SDavid Howells { 14156a062a36SKinglong Mee remove_proc_subtree("fs/nfsfs", NULL); 14161c725118SBenjamin Coddington ida_destroy(&s_sysfs_ids); 14176aaca566SDavid Howells } 14186aaca566SDavid Howells 14196aaca566SDavid Howells #endif /* CONFIG_PROC_FS */ 1420