124c8dbbbSDavid Howells /* client.c: NFS client sharing and management code 224c8dbbbSDavid Howells * 324c8dbbbSDavid Howells * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 424c8dbbbSDavid Howells * Written by David Howells (dhowells@redhat.com) 524c8dbbbSDavid Howells * 624c8dbbbSDavid Howells * This program is free software; you can redistribute it and/or 724c8dbbbSDavid Howells * modify it under the terms of the GNU General Public License 824c8dbbbSDavid Howells * as published by the Free Software Foundation; either version 924c8dbbbSDavid Howells * 2 of the License, or (at your option) any later version. 1024c8dbbbSDavid Howells */ 1124c8dbbbSDavid Howells 1224c8dbbbSDavid Howells 1324c8dbbbSDavid Howells #include <linux/module.h> 1424c8dbbbSDavid Howells #include <linux/init.h> 15e8edc6e0SAlexey Dobriyan #include <linux/sched.h> 1624c8dbbbSDavid Howells #include <linux/time.h> 1724c8dbbbSDavid Howells #include <linux/kernel.h> 1824c8dbbbSDavid Howells #include <linux/mm.h> 1924c8dbbbSDavid Howells #include <linux/string.h> 2024c8dbbbSDavid Howells #include <linux/stat.h> 2124c8dbbbSDavid Howells #include <linux/errno.h> 2224c8dbbbSDavid Howells #include <linux/unistd.h> 2324c8dbbbSDavid Howells #include <linux/sunrpc/clnt.h> 2424c8dbbbSDavid Howells #include <linux/sunrpc/stats.h> 2524c8dbbbSDavid Howells #include <linux/sunrpc/metrics.h> 260896a725S\"Talpey, Thomas\ #include <linux/sunrpc/xprtsock.h> 272cf7ff7aS\"Talpey, Thomas\ #include <linux/sunrpc/xprtrdma.h> 2824c8dbbbSDavid Howells #include <linux/nfs_fs.h> 2924c8dbbbSDavid Howells #include <linux/nfs_mount.h> 3024c8dbbbSDavid Howells #include <linux/nfs4_mount.h> 3124c8dbbbSDavid Howells #include <linux/lockd/bind.h> 3224c8dbbbSDavid Howells #include <linux/seq_file.h> 3324c8dbbbSDavid Howells #include <linux/mount.h> 3424c8dbbbSDavid Howells #include <linux/nfs_idmap.h> 3524c8dbbbSDavid Howells #include <linux/vfs.h> 3624c8dbbbSDavid Howells #include <linux/inet.h> 373b0d3f93STrond Myklebust #include <linux/in6.h> 385a0e3ad6STejun Heo #include <linux/slab.h> 3940401530SAl Viro #include <linux/idr.h> 403b0d3f93STrond Myklebust #include <net/ipv6.h> 4124c8dbbbSDavid Howells #include <linux/nfs_xdr.h> 420b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h> 436b13168bSStanislav Kinsbursky #include <linux/nsproxy.h> 446b13168bSStanislav Kinsbursky #include <linux/pid_namespace.h> 4524c8dbbbSDavid Howells 4624c8dbbbSDavid Howells 4724c8dbbbSDavid Howells #include "nfs4_fs.h" 4824c8dbbbSDavid Howells #include "callback.h" 4924c8dbbbSDavid Howells #include "delegation.h" 5024c8dbbbSDavid Howells #include "iostat.h" 5124c8dbbbSDavid Howells #include "internal.h" 5214727281SDavid Howells #include "fscache.h" 5385e174baSRicardo Labiaga #include "pnfs.h" 54ab7017a3SBryan Schumaker #include "nfs.h" 556b13168bSStanislav Kinsbursky #include "netns.h" 5624c8dbbbSDavid Howells 5724c8dbbbSDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 5824c8dbbbSDavid Howells 5924c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); 60ab7017a3SBryan Schumaker static DEFINE_SPINLOCK(nfs_version_lock); 61ab7017a3SBryan Schumaker static DEFINE_MUTEX(nfs_version_mutex); 62ab7017a3SBryan Schumaker static LIST_HEAD(nfs_versions); 6324c8dbbbSDavid Howells 6424c8dbbbSDavid Howells /* 655006a76cSDavid Howells * RPC cruft for NFS 665006a76cSDavid Howells */ 67a613fa16STrond Myklebust static const struct rpc_version *nfs_version[5] = { 68ab7017a3SBryan Schumaker [2] = NULL, 69ab7017a3SBryan Schumaker [3] = NULL, 70ab7017a3SBryan Schumaker [4] = NULL, 715006a76cSDavid Howells }; 725006a76cSDavid Howells 73a613fa16STrond Myklebust const struct rpc_program nfs_program = { 745006a76cSDavid Howells .name = "nfs", 755006a76cSDavid Howells .number = NFS_PROGRAM, 765006a76cSDavid Howells .nrvers = ARRAY_SIZE(nfs_version), 775006a76cSDavid Howells .version = nfs_version, 785006a76cSDavid Howells .stats = &nfs_rpcstat, 79fe0a9b74SJim Rees .pipe_dir_name = NFS_PIPE_DIRNAME, 805006a76cSDavid Howells }; 815006a76cSDavid Howells 825006a76cSDavid Howells struct rpc_stat nfs_rpcstat = { 835006a76cSDavid Howells .program = &nfs_program 845006a76cSDavid Howells }; 855006a76cSDavid Howells 86ab7017a3SBryan Schumaker static struct nfs_subversion *find_nfs_version(unsigned int version) 87ab7017a3SBryan Schumaker { 88ab7017a3SBryan Schumaker struct nfs_subversion *nfs; 89ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 90ab7017a3SBryan Schumaker 91ab7017a3SBryan Schumaker list_for_each_entry(nfs, &nfs_versions, list) { 92ab7017a3SBryan Schumaker if (nfs->rpc_ops->version == version) { 93ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 94ab7017a3SBryan Schumaker return nfs; 95ab7017a3SBryan Schumaker } 96ee34e136SYanchuan Nian } 97ab7017a3SBryan Schumaker 98ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 99ee34e136SYanchuan Nian return ERR_PTR(-EPROTONOSUPPORT); 100ab7017a3SBryan Schumaker } 101ab7017a3SBryan Schumaker 102ab7017a3SBryan Schumaker struct nfs_subversion *get_nfs_version(unsigned int version) 103ab7017a3SBryan Schumaker { 104ab7017a3SBryan Schumaker struct nfs_subversion *nfs = find_nfs_version(version); 105ab7017a3SBryan Schumaker 106ab7017a3SBryan Schumaker if (IS_ERR(nfs)) { 107ab7017a3SBryan Schumaker mutex_lock(&nfs_version_mutex); 1081ae811eeSbjschuma@gmail.com request_module("nfsv%d", version); 109ab7017a3SBryan Schumaker nfs = find_nfs_version(version); 110ab7017a3SBryan Schumaker mutex_unlock(&nfs_version_mutex); 111ab7017a3SBryan Schumaker } 112ab7017a3SBryan Schumaker 1131f70ef96SAlexey Khoroshilov if (!IS_ERR(nfs) && !try_module_get(nfs->owner)) 1141f70ef96SAlexey Khoroshilov return ERR_PTR(-EAGAIN); 115ab7017a3SBryan Schumaker return nfs; 116ab7017a3SBryan Schumaker } 117ab7017a3SBryan Schumaker 118ab7017a3SBryan Schumaker void put_nfs_version(struct nfs_subversion *nfs) 119ab7017a3SBryan Schumaker { 120ab7017a3SBryan Schumaker module_put(nfs->owner); 121ab7017a3SBryan Schumaker } 122ab7017a3SBryan Schumaker 123ab7017a3SBryan Schumaker void register_nfs_version(struct nfs_subversion *nfs) 124ab7017a3SBryan Schumaker { 125ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 126ab7017a3SBryan Schumaker 127ab7017a3SBryan Schumaker list_add(&nfs->list, &nfs_versions); 128ab7017a3SBryan Schumaker nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers; 129ab7017a3SBryan Schumaker 130ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 131ab7017a3SBryan Schumaker } 132ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(register_nfs_version); 133ab7017a3SBryan Schumaker 134ab7017a3SBryan Schumaker void unregister_nfs_version(struct nfs_subversion *nfs) 135ab7017a3SBryan Schumaker { 136ab7017a3SBryan Schumaker spin_lock(&nfs_version_lock); 137ab7017a3SBryan Schumaker 138ab7017a3SBryan Schumaker nfs_version[nfs->rpc_ops->version] = NULL; 139ab7017a3SBryan Schumaker list_del(&nfs->list); 140ab7017a3SBryan Schumaker 141ab7017a3SBryan Schumaker spin_unlock(&nfs_version_lock); 142ab7017a3SBryan Schumaker } 143ab7017a3SBryan Schumaker EXPORT_SYMBOL_GPL(unregister_nfs_version); 144ab7017a3SBryan Schumaker 145ab7017a3SBryan Schumaker /* 14624c8dbbbSDavid Howells * Allocate a shared client record 14724c8dbbbSDavid Howells * 14824c8dbbbSDavid Howells * Since these are allocated/deallocated very rarely, we don't 14924c8dbbbSDavid Howells * bother putting them in a slab cache... 15024c8dbbbSDavid Howells */ 1516663ee7fSBryan Schumaker struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) 15224c8dbbbSDavid Howells { 15324c8dbbbSDavid Howells struct nfs_client *clp; 1547c67db3aSTrond Myklebust struct rpc_cred *cred; 155a21bdd9bSChuck Lever int err = -ENOMEM; 15624c8dbbbSDavid Howells 15724c8dbbbSDavid Howells if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) 15824c8dbbbSDavid Howells goto error_0; 15924c8dbbbSDavid Howells 160ab7017a3SBryan Schumaker clp->cl_nfs_mod = cl_init->nfs_mod; 1611f70ef96SAlexey Khoroshilov if (!try_module_get(clp->cl_nfs_mod->owner)) 1621f70ef96SAlexey Khoroshilov goto error_dealloc; 163ab7017a3SBryan Schumaker 164ab7017a3SBryan Schumaker clp->rpc_ops = clp->cl_nfs_mod->rpc_ops; 16540c55319STrond Myklebust 16624c8dbbbSDavid Howells atomic_set(&clp->cl_count, 1); 16724c8dbbbSDavid Howells clp->cl_cons_state = NFS_CS_INITING; 16824c8dbbbSDavid Howells 1696e4cffd7SChuck Lever memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); 1706e4cffd7SChuck Lever clp->cl_addrlen = cl_init->addrlen; 17124c8dbbbSDavid Howells 1723a498026STrond Myklebust if (cl_init->hostname) { 173a21bdd9bSChuck Lever err = -ENOMEM; 1743a498026STrond Myklebust clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); 17524c8dbbbSDavid Howells if (!clp->cl_hostname) 17671468513SBenny Halevy goto error_cleanup; 17724c8dbbbSDavid Howells } 17824c8dbbbSDavid Howells 17924c8dbbbSDavid Howells INIT_LIST_HEAD(&clp->cl_superblocks); 18024c8dbbbSDavid Howells clp->cl_rpcclient = ERR_PTR(-EINVAL); 18124c8dbbbSDavid Howells 18259dca3b2STrond Myklebust clp->cl_proto = cl_init->proto; 18373ea666cSChuck Lever clp->cl_net = get_net(cl_init->net); 18459dca3b2STrond Myklebust 18568c97153STrond Myklebust cred = rpc_lookup_machine_cred("*"); 1867c67db3aSTrond Myklebust if (!IS_ERR(cred)) 1877c67db3aSTrond Myklebust clp->cl_machine_cred = cred; 18814727281SDavid Howells nfs_fscache_get_client_cookie(clp); 18914727281SDavid Howells 19024c8dbbbSDavid Howells return clp; 19124c8dbbbSDavid Howells 19271468513SBenny Halevy error_cleanup: 193ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 1941f70ef96SAlexey Khoroshilov error_dealloc: 19524c8dbbbSDavid Howells kfree(clp); 19624c8dbbbSDavid Howells error_0: 197a21bdd9bSChuck Lever return ERR_PTR(err); 19824c8dbbbSDavid Howells } 199ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_client); 20024c8dbbbSDavid Howells 20189d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 20228cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net) 203f4eecd5dSAndy Adamson { 20428cd1b3fSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 20528cd1b3fSStanislav Kinsbursky 20628cd1b3fSStanislav Kinsbursky idr_destroy(&nn->cb_ident_idr); 207f4eecd5dSAndy Adamson } 208f4eecd5dSAndy Adamson 209f4eecd5dSAndy Adamson /* nfs_client_lock held */ 210f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 211f4eecd5dSAndy Adamson { 21273ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 21328cd1b3fSStanislav Kinsbursky 214f4eecd5dSAndy Adamson if (clp->cl_cb_ident) 21528cd1b3fSStanislav Kinsbursky idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident); 216f4eecd5dSAndy Adamson } 217f4eecd5dSAndy Adamson 218f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 219f7e8917aSFred Isaman { 220f7e8917aSFred Isaman rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 221f7e8917aSFred Isaman } 222f7e8917aSFred Isaman 223888ef2e3SAlexandros Batsakis #else 22428cd1b3fSStanislav Kinsbursky void nfs_cleanup_cb_ident_idr(struct net *net) 225f4eecd5dSAndy Adamson { 226f4eecd5dSAndy Adamson } 227f4eecd5dSAndy Adamson 228f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 229f4eecd5dSAndy Adamson { 230f4eecd5dSAndy Adamson } 231f7e8917aSFred Isaman 232f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 233f7e8917aSFred Isaman { 234f7e8917aSFred Isaman } 235f7e8917aSFred Isaman 236888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */ 237888ef2e3SAlexandros Batsakis 238888ef2e3SAlexandros Batsakis /* 2395dd3177aSTrond Myklebust * Destroy a shared client record 2405dd3177aSTrond Myklebust */ 241cdb7ecedSBryan Schumaker void nfs_free_client(struct nfs_client *clp) 2425dd3177aSTrond Myklebust { 24340c55319STrond Myklebust dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version); 2445dd3177aSTrond Myklebust 24514727281SDavid Howells nfs_fscache_release_client_cookie(clp); 24614727281SDavid Howells 24724c8dbbbSDavid Howells /* -EIO all pending I/O */ 24824c8dbbbSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 24924c8dbbbSDavid Howells rpc_shutdown_client(clp->cl_rpcclient); 25024c8dbbbSDavid Howells 2517c67db3aSTrond Myklebust if (clp->cl_machine_cred != NULL) 2527c67db3aSTrond Myklebust put_rpccred(clp->cl_machine_cred); 2537c67db3aSTrond Myklebust 25473ea666cSChuck Lever put_net(clp->cl_net); 255ab7017a3SBryan Schumaker put_nfs_version(clp->cl_nfs_mod); 25624c8dbbbSDavid Howells kfree(clp->cl_hostname); 257f11b2a1cSJeff Layton kfree(clp->cl_acceptor); 25824c8dbbbSDavid Howells kfree(clp); 25924c8dbbbSDavid Howells 26024c8dbbbSDavid Howells dprintk("<-- nfs_free_client()\n"); 26124c8dbbbSDavid Howells } 262ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_client); 26324c8dbbbSDavid Howells 26424c8dbbbSDavid Howells /* 26524c8dbbbSDavid Howells * Release a reference to a shared client record 26624c8dbbbSDavid Howells */ 26724c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp) 26824c8dbbbSDavid Howells { 269dc030858SStanislav Kinsbursky struct nfs_net *nn; 270dc030858SStanislav Kinsbursky 27127ba8512SDavid Howells if (!clp) 27227ba8512SDavid Howells return; 27327ba8512SDavid Howells 27424c8dbbbSDavid Howells dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count)); 27573ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 27624c8dbbbSDavid Howells 277dc030858SStanislav Kinsbursky if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) { 27824c8dbbbSDavid Howells list_del(&clp->cl_share_link); 279f4eecd5dSAndy Adamson nfs_cb_idr_remove_locked(clp); 280dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 28124c8dbbbSDavid Howells 2821fea73a8STrond Myklebust WARN_ON_ONCE(!list_empty(&clp->cl_superblocks)); 28324c8dbbbSDavid Howells 284cdb7ecedSBryan Schumaker clp->rpc_ops->free_client(clp); 28524c8dbbbSDavid Howells } 28624c8dbbbSDavid Howells } 28716b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client); 28824c8dbbbSDavid Howells 2899082a5ccSTrond Myklebust #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 2909f4c899cSTrond Myklebust /* 2919f4c899cSTrond Myklebust * Test if two ip6 socket addresses refer to the same socket by 2929f4c899cSTrond Myklebust * comparing relevant fields. The padding bytes specifically, are not 2939f4c899cSTrond Myklebust * compared. sin6_flowinfo is not compared because it only affects QoS 2949f4c899cSTrond Myklebust * and sin6_scope_id is only compared if the address is "link local" 2959f4c899cSTrond Myklebust * because "link local" addresses need only be unique to a specific 2969f4c899cSTrond Myklebust * link. Conversely, ordinary unicast addresses might have different 2979f4c899cSTrond Myklebust * sin6_scope_id. 2989f4c899cSTrond Myklebust * 2999f4c899cSTrond Myklebust * The caller should ensure both socket addresses are AF_INET6. 3009f4c899cSTrond Myklebust */ 3013c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, 3029f4c899cSTrond Myklebust const struct sockaddr *sa2) 3039f4c899cSTrond Myklebust { 3043c8c45dfSChuck Lever const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; 3053c8c45dfSChuck Lever const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; 3069f4c899cSTrond Myklebust 307b9dd3abbSMi Jinlong if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr)) 3089f4c899cSTrond Myklebust return 0; 309b9dd3abbSMi Jinlong else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL) 310b9dd3abbSMi Jinlong return sin1->sin6_scope_id == sin2->sin6_scope_id; 3113b0d3f93STrond Myklebust 312b9dd3abbSMi Jinlong return 1; 3133b0d3f93STrond Myklebust } 3143c8c45dfSChuck Lever #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */ 3153c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, 3169f4c899cSTrond Myklebust const struct sockaddr *sa2) 3179f4c899cSTrond Myklebust { 3189f4c899cSTrond Myklebust return 0; 3199f4c899cSTrond Myklebust } 3209082a5ccSTrond Myklebust #endif 3213b0d3f93STrond Myklebust 32224c8dbbbSDavid Howells /* 323d7371c41SIan Dall * Test if two ip4 socket addresses refer to the same socket, by 324d7371c41SIan Dall * comparing relevant fields. The padding bytes specifically, are 325d7371c41SIan Dall * not compared. 326d7371c41SIan Dall * 327d7371c41SIan Dall * The caller should ensure both socket addresses are AF_INET. 328d7371c41SIan Dall */ 3293c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1, 3303c8c45dfSChuck Lever const struct sockaddr *sa2) 3313c8c45dfSChuck Lever { 3323c8c45dfSChuck Lever const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; 3333c8c45dfSChuck Lever const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; 3343c8c45dfSChuck Lever 3353c8c45dfSChuck Lever return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr; 3363c8c45dfSChuck Lever } 3373c8c45dfSChuck Lever 3383c8c45dfSChuck Lever static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1, 3393c8c45dfSChuck Lever const struct sockaddr *sa2) 3403c8c45dfSChuck Lever { 3413c8c45dfSChuck Lever const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; 3423c8c45dfSChuck Lever const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; 3433c8c45dfSChuck Lever 3443c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr6(sa1, sa2) && 3453c8c45dfSChuck Lever (sin1->sin6_port == sin2->sin6_port); 3463c8c45dfSChuck Lever } 3473c8c45dfSChuck Lever 3489f4c899cSTrond Myklebust static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1, 3499f4c899cSTrond Myklebust const struct sockaddr *sa2) 350d7371c41SIan Dall { 3513c8c45dfSChuck Lever const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; 3523c8c45dfSChuck Lever const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; 3539f4c899cSTrond Myklebust 3543c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr4(sa1, sa2) && 3553c8c45dfSChuck Lever (sin1->sin_port == sin2->sin_port); 356d7371c41SIan Dall } 357d7371c41SIan Dall 3584b7c8dd2SBryan Schumaker #if defined(CONFIG_NFS_V4_1) 359d7371c41SIan Dall /* 360d7371c41SIan Dall * Test if two socket addresses represent the same actual socket, 3613c8c45dfSChuck Lever * by comparing (only) relevant fields, excluding the port number. 3623c8c45dfSChuck Lever */ 363fcf10398SBryan Schumaker int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1, 3643c8c45dfSChuck Lever const struct sockaddr *sa2) 3653c8c45dfSChuck Lever { 3663c8c45dfSChuck Lever if (sa1->sa_family != sa2->sa_family) 3673c8c45dfSChuck Lever return 0; 3683c8c45dfSChuck Lever 3693c8c45dfSChuck Lever switch (sa1->sa_family) { 3703c8c45dfSChuck Lever case AF_INET: 3713c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr4(sa1, sa2); 3723c8c45dfSChuck Lever case AF_INET6: 3733c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr6(sa1, sa2); 3743c8c45dfSChuck Lever } 3753c8c45dfSChuck Lever return 0; 3763c8c45dfSChuck Lever } 37789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_sockaddr_match_ipaddr); 3784b7c8dd2SBryan Schumaker #endif /* CONFIG_NFS_V4_1 */ 3793c8c45dfSChuck Lever 3803c8c45dfSChuck Lever /* 3813c8c45dfSChuck Lever * Test if two socket addresses represent the same actual socket, 3823c8c45dfSChuck Lever * by comparing (only) relevant fields, including the port number. 383d7371c41SIan Dall */ 384d7371c41SIan Dall static int nfs_sockaddr_cmp(const struct sockaddr *sa1, 385d7371c41SIan Dall const struct sockaddr *sa2) 386d7371c41SIan Dall { 387d7371c41SIan Dall if (sa1->sa_family != sa2->sa_family) 388d7371c41SIan Dall return 0; 389d7371c41SIan Dall 390d7371c41SIan Dall switch (sa1->sa_family) { 391d7371c41SIan Dall case AF_INET: 3929f4c899cSTrond Myklebust return nfs_sockaddr_cmp_ip4(sa1, sa2); 393d7371c41SIan Dall case AF_INET6: 3949f4c899cSTrond Myklebust return nfs_sockaddr_cmp_ip6(sa1, sa2); 395d7371c41SIan Dall } 396d7371c41SIan Dall return 0; 397d7371c41SIan Dall } 398d7371c41SIan Dall 3993fbd67adSTrond Myklebust /* 400c81468a1STrond Myklebust * Find an nfs_client on the list that matches the initialisation data 401c81468a1STrond Myklebust * that is supplied. 402c81468a1STrond Myklebust */ 403c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) 40424c8dbbbSDavid Howells { 40524c8dbbbSDavid Howells struct nfs_client *clp; 406d7371c41SIan Dall const struct sockaddr *sap = data->addr; 4076b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(data->net, nfs_net_id); 40824c8dbbbSDavid Howells 4096b13168bSStanislav Kinsbursky list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { 410d7371c41SIan Dall const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 41113bbc06aSTrond Myklebust /* Don't match clients that failed to initialise properly */ 41213bbc06aSTrond Myklebust if (clp->cl_cons_state < 0) 41313bbc06aSTrond Myklebust continue; 41413bbc06aSTrond Myklebust 41524c8dbbbSDavid Howells /* Different NFS versions cannot share the same nfs_client */ 416ab7017a3SBryan Schumaker if (clp->rpc_ops != data->nfs_mod->rpc_ops) 41724c8dbbbSDavid Howells continue; 41824c8dbbbSDavid Howells 41959dca3b2STrond Myklebust if (clp->cl_proto != data->proto) 42059dca3b2STrond Myklebust continue; 4215aae4a9aSBenny Halevy /* Match nfsv4 minorversion */ 4225aae4a9aSBenny Halevy if (clp->cl_minorversion != data->minorversion) 4235aae4a9aSBenny Halevy continue; 424c81468a1STrond Myklebust /* Match the full socket address */ 425d7371c41SIan Dall if (!nfs_sockaddr_cmp(sap, clap)) 42624c8dbbbSDavid Howells continue; 42724c8dbbbSDavid Howells 42824c8dbbbSDavid Howells atomic_inc(&clp->cl_count); 42924c8dbbbSDavid Howells return clp; 43024c8dbbbSDavid Howells } 431c81468a1STrond Myklebust return NULL; 43224c8dbbbSDavid Howells } 43324c8dbbbSDavid Howells 4344697bd5eSTrond Myklebust static bool nfs_client_init_is_complete(const struct nfs_client *clp) 4354697bd5eSTrond Myklebust { 4364697bd5eSTrond Myklebust return clp->cl_cons_state != NFS_CS_INITING; 4374697bd5eSTrond Myklebust } 4384697bd5eSTrond Myklebust 4394697bd5eSTrond Myklebust int nfs_wait_client_init_complete(const struct nfs_client *clp) 4404697bd5eSTrond Myklebust { 4414697bd5eSTrond Myklebust return wait_event_killable(nfs_client_active_wq, 4424697bd5eSTrond Myklebust nfs_client_init_is_complete(clp)); 4434697bd5eSTrond Myklebust } 44489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete); 4454697bd5eSTrond Myklebust 44624c8dbbbSDavid Howells /* 447f411703aSChuck Lever * Found an existing client. Make sure it's ready before returning. 448f411703aSChuck Lever */ 449f411703aSChuck Lever static struct nfs_client * 450f411703aSChuck Lever nfs_found_client(const struct nfs_client_initdata *cl_init, 451f411703aSChuck Lever struct nfs_client *clp) 452f411703aSChuck Lever { 453f411703aSChuck Lever int error; 454f411703aSChuck Lever 4554697bd5eSTrond Myklebust error = nfs_wait_client_init_complete(clp); 456f411703aSChuck Lever if (error < 0) { 457f411703aSChuck Lever nfs_put_client(clp); 458f411703aSChuck Lever return ERR_PTR(-ERESTARTSYS); 459f411703aSChuck Lever } 460f411703aSChuck Lever 461f411703aSChuck Lever if (clp->cl_cons_state < NFS_CS_READY) { 462f411703aSChuck Lever error = clp->cl_cons_state; 463f411703aSChuck Lever nfs_put_client(clp); 464f411703aSChuck Lever return ERR_PTR(error); 465f411703aSChuck Lever } 466f411703aSChuck Lever 46754ac471cSTrond Myklebust smp_rmb(); 46854ac471cSTrond Myklebust 469f411703aSChuck Lever dprintk("<-- %s found nfs_client %p for %s\n", 470f411703aSChuck Lever __func__, clp, cl_init->hostname ?: ""); 471f411703aSChuck Lever return clp; 472f411703aSChuck Lever } 473f411703aSChuck Lever 474f411703aSChuck Lever /* 47524c8dbbbSDavid Howells * Look up a client by IP address and protocol version 47624c8dbbbSDavid Howells * - creates a new record if one doesn't yet exist 47724c8dbbbSDavid Howells */ 478fcf10398SBryan Schumaker struct nfs_client * 47945a52a02SAndy Adamson nfs_get_client(const struct nfs_client_initdata *cl_init, 48045a52a02SAndy Adamson const struct rpc_timeout *timeparms, 48145a52a02SAndy Adamson const char *ip_addr, 4824bf590e0SChuck Lever rpc_authflavor_t authflavour) 48324c8dbbbSDavid Howells { 48424c8dbbbSDavid Howells struct nfs_client *clp, *new = NULL; 4856b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id); 486ab7017a3SBryan Schumaker const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops; 48724c8dbbbSDavid Howells 48831434f49SPeng Tao if (cl_init->hostname == NULL) { 48931434f49SPeng Tao WARN_ON(1); 49031434f49SPeng Tao return NULL; 49131434f49SPeng Tao } 49231434f49SPeng Tao 493d7422c47SChuck Lever dprintk("--> nfs_get_client(%s,v%u)\n", 49431434f49SPeng Tao cl_init->hostname, rpc_ops->version); 49524c8dbbbSDavid Howells 49624c8dbbbSDavid Howells /* see if the client already exists */ 49724c8dbbbSDavid Howells do { 498dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 49924c8dbbbSDavid Howells 500c81468a1STrond Myklebust clp = nfs_match_client(cl_init); 501f411703aSChuck Lever if (clp) { 502f411703aSChuck Lever spin_unlock(&nn->nfs_client_lock); 503f411703aSChuck Lever if (new) 504cdb7ecedSBryan Schumaker new->rpc_ops->free_client(new); 505f411703aSChuck Lever return nfs_found_client(cl_init, clp); 506f411703aSChuck Lever } 5078cab4c39SChuck Lever if (new) { 50805f4c350SChuck Lever list_add_tail(&new->cl_share_link, 50905f4c350SChuck Lever &nn->nfs_client_list); 5108cab4c39SChuck Lever spin_unlock(&nn->nfs_client_lock); 5114bf590e0SChuck Lever new->cl_flags = cl_init->init_flags; 512f8407299SAndy Adamson return rpc_ops->init_client(new, timeparms, ip_addr); 5138cab4c39SChuck Lever } 51424c8dbbbSDavid Howells 515dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 51624c8dbbbSDavid Howells 517ab7017a3SBryan Schumaker new = rpc_ops->alloc_client(cl_init); 518a21bdd9bSChuck Lever } while (!IS_ERR(new)); 51924c8dbbbSDavid Howells 520f411703aSChuck Lever dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n", 52131434f49SPeng Tao cl_init->hostname, PTR_ERR(new)); 522a21bdd9bSChuck Lever return new; 52324c8dbbbSDavid Howells } 52489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_get_client); 52524c8dbbbSDavid Howells 52624c8dbbbSDavid Howells /* 52724c8dbbbSDavid Howells * Mark a server as ready or failed 52824c8dbbbSDavid Howells */ 52976db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state) 53024c8dbbbSDavid Howells { 53154ac471cSTrond Myklebust smp_wmb(); 53224c8dbbbSDavid Howells clp->cl_cons_state = state; 53324c8dbbbSDavid Howells wake_up_all(&nfs_client_active_wq); 53424c8dbbbSDavid Howells } 53589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_mark_client_ready); 5365006a76cSDavid Howells 5375006a76cSDavid Howells /* 5385006a76cSDavid Howells * Initialise the timeout values for a connection 5395006a76cSDavid Howells */ 540fcf10398SBryan Schumaker void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 5415006a76cSDavid Howells unsigned int timeo, unsigned int retrans) 5425006a76cSDavid Howells { 5435006a76cSDavid Howells to->to_initval = timeo * HZ / 10; 5445006a76cSDavid Howells to->to_retries = retrans; 5455006a76cSDavid Howells 5465006a76cSDavid Howells switch (proto) { 5470896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_TCP: 5482cf7ff7aS\"Talpey, Thomas\ case XPRT_TRANSPORT_RDMA: 549259875efSTrond Myklebust if (to->to_retries == 0) 550259875efSTrond Myklebust to->to_retries = NFS_DEF_TCP_RETRANS; 5517a3e3e18STrond Myklebust if (to->to_initval == 0) 552259875efSTrond Myklebust to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; 5535006a76cSDavid Howells if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 5545006a76cSDavid Howells to->to_initval = NFS_MAX_TCP_TIMEOUT; 5555006a76cSDavid Howells to->to_increment = to->to_initval; 5565006a76cSDavid Howells to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); 5577a3e3e18STrond Myklebust if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) 5587a3e3e18STrond Myklebust to->to_maxval = NFS_MAX_TCP_TIMEOUT; 5597a3e3e18STrond Myklebust if (to->to_maxval < to->to_initval) 5607a3e3e18STrond Myklebust to->to_maxval = to->to_initval; 5615006a76cSDavid Howells to->to_exponential = 0; 5625006a76cSDavid Howells break; 5630896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_UDP: 564259875efSTrond Myklebust if (to->to_retries == 0) 565259875efSTrond Myklebust to->to_retries = NFS_DEF_UDP_RETRANS; 5665006a76cSDavid Howells if (!to->to_initval) 567259875efSTrond Myklebust to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; 5685006a76cSDavid Howells if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 5695006a76cSDavid Howells to->to_initval = NFS_MAX_UDP_TIMEOUT; 5705006a76cSDavid Howells to->to_maxval = NFS_MAX_UDP_TIMEOUT; 5715006a76cSDavid Howells to->to_exponential = 1; 5725006a76cSDavid Howells break; 573259875efSTrond Myklebust default: 574259875efSTrond Myklebust BUG(); 5755006a76cSDavid Howells } 5765006a76cSDavid Howells } 57789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_timeout_values); 5785006a76cSDavid Howells 5795006a76cSDavid Howells /* 5805006a76cSDavid Howells * Create an RPC client handle 5815006a76cSDavid Howells */ 582428360d7SBryan Schumaker int nfs_create_rpc_client(struct nfs_client *clp, 58333170233STrond Myklebust const struct rpc_timeout *timeparms, 5844bf590e0SChuck Lever rpc_authflavor_t flavor) 5855006a76cSDavid Howells { 5865006a76cSDavid Howells struct rpc_clnt *clnt = NULL; 58741877d20SChuck Lever struct rpc_create_args args = { 58873ea666cSChuck Lever .net = clp->cl_net, 58959dca3b2STrond Myklebust .protocol = clp->cl_proto, 59041877d20SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 5916e4cffd7SChuck Lever .addrsize = clp->cl_addrlen, 59233170233STrond Myklebust .timeout = timeparms, 59341877d20SChuck Lever .servername = clp->cl_hostname, 59441877d20SChuck Lever .program = &nfs_program, 59541877d20SChuck Lever .version = clp->rpc_ops->version, 59641877d20SChuck Lever .authflavor = flavor, 59741877d20SChuck Lever }; 5985006a76cSDavid Howells 5994bf590e0SChuck Lever if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags)) 6004a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_DISCRTRY; 60199875249STrond Myklebust if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags)) 60299875249STrond Myklebust args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT; 6034bf590e0SChuck Lever if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags)) 6044a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; 60598f98cf5STrond Myklebust if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags)) 60698f98cf5STrond Myklebust args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS; 6074a01b8a4SChuck Lever 6085006a76cSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 6095006a76cSDavid Howells return 0; 6105006a76cSDavid Howells 61141877d20SChuck Lever clnt = rpc_create(&args); 6125006a76cSDavid Howells if (IS_ERR(clnt)) { 6135006a76cSDavid Howells dprintk("%s: cannot create RPC client. Error = %ld\n", 6143110ff80SHarvey Harrison __func__, PTR_ERR(clnt)); 6155006a76cSDavid Howells return PTR_ERR(clnt); 6165006a76cSDavid Howells } 6175006a76cSDavid Howells 6185006a76cSDavid Howells clp->cl_rpcclient = clnt; 6195006a76cSDavid Howells return 0; 6205006a76cSDavid Howells } 62189d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_rpc_client); 62254ceac45SDavid Howells 62354ceac45SDavid Howells /* 62454ceac45SDavid Howells * Version 2 or 3 client destruction 62554ceac45SDavid Howells */ 62654ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server) 62754ceac45SDavid Howells { 628f259613aSNeilBrown if (server->nlm_host) 6299289e7f9SChuck Lever nlmclnt_done(server->nlm_host); 63054ceac45SDavid Howells } 63154ceac45SDavid Howells 63254ceac45SDavid Howells /* 63354ceac45SDavid Howells * Version 2 or 3 lockd setup 63454ceac45SDavid Howells */ 63554ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server) 63654ceac45SDavid Howells { 6379289e7f9SChuck Lever struct nlm_host *host; 6389289e7f9SChuck Lever struct nfs_client *clp = server->nfs_client; 639883bb163SChuck Lever struct nlmclnt_initdata nlm_init = { 640883bb163SChuck Lever .hostname = clp->cl_hostname, 641883bb163SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 642883bb163SChuck Lever .addrlen = clp->cl_addrlen, 643883bb163SChuck Lever .nfs_version = clp->rpc_ops->version, 6440cb2659bSChuck Lever .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? 6450cb2659bSChuck Lever 1 : 0, 64673ea666cSChuck Lever .net = clp->cl_net, 647883bb163SChuck Lever }; 64854ceac45SDavid Howells 649883bb163SChuck Lever if (nlm_init.nfs_version > 3) 6509289e7f9SChuck Lever return 0; 6515eebde23SSuresh Jayaraman if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) && 6525eebde23SSuresh Jayaraman (server->flags & NFS_MOUNT_LOCAL_FCNTL)) 6539289e7f9SChuck Lever return 0; 6549289e7f9SChuck Lever 6558a6e5debSTrond Myklebust switch (clp->cl_proto) { 6568a6e5debSTrond Myklebust default: 6578a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_TCP; 6588a6e5debSTrond Myklebust break; 6598a6e5debSTrond Myklebust case XPRT_TRANSPORT_UDP: 6608a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_UDP; 6618a6e5debSTrond Myklebust } 6628a6e5debSTrond Myklebust 663883bb163SChuck Lever host = nlmclnt_init(&nlm_init); 6649289e7f9SChuck Lever if (IS_ERR(host)) 6659289e7f9SChuck Lever return PTR_ERR(host); 6669289e7f9SChuck Lever 6679289e7f9SChuck Lever server->nlm_host = host; 66854ceac45SDavid Howells server->destroy = nfs_destroy_server; 6699289e7f9SChuck Lever return 0; 67054ceac45SDavid Howells } 67154ceac45SDavid Howells 67254ceac45SDavid Howells /* 67354ceac45SDavid Howells * Create a general RPC client 67454ceac45SDavid Howells */ 675fcf10398SBryan Schumaker int nfs_init_server_rpcclient(struct nfs_server *server, 67633170233STrond Myklebust const struct rpc_timeout *timeo, 67733170233STrond Myklebust rpc_authflavor_t pseudoflavour) 67854ceac45SDavid Howells { 67954ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 68054ceac45SDavid Howells 681ba9b584cSChuck Lever server->client = rpc_clone_client_set_auth(clp->cl_rpcclient, 682ba9b584cSChuck Lever pseudoflavour); 68354ceac45SDavid Howells if (IS_ERR(server->client)) { 6843110ff80SHarvey Harrison dprintk("%s: couldn't create rpc_client!\n", __func__); 68554ceac45SDavid Howells return PTR_ERR(server->client); 68654ceac45SDavid Howells } 68754ceac45SDavid Howells 68833170233STrond Myklebust memcpy(&server->client->cl_timeout_default, 68933170233STrond Myklebust timeo, 69033170233STrond Myklebust sizeof(server->client->cl_timeout_default)); 69133170233STrond Myklebust server->client->cl_timeout = &server->client->cl_timeout_default; 69254ceac45SDavid Howells server->client->cl_softrtry = 0; 69354ceac45SDavid Howells if (server->flags & NFS_MOUNT_SOFT) 69454ceac45SDavid Howells server->client->cl_softrtry = 1; 69554ceac45SDavid Howells 69654ceac45SDavid Howells return 0; 69754ceac45SDavid Howells } 69889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient); 69954ceac45SDavid Howells 7008cab4c39SChuck Lever /** 7018cab4c39SChuck Lever * nfs_init_client - Initialise an NFS2 or NFS3 client 7028cab4c39SChuck Lever * 7038cab4c39SChuck Lever * @clp: nfs_client to initialise 7048cab4c39SChuck Lever * @timeparms: timeout parameters for underlying RPC transport 7058cab4c39SChuck Lever * @ip_addr: IP presentation address (not used) 7068cab4c39SChuck Lever * 7078cab4c39SChuck Lever * Returns pointer to an NFS client, or an ERR_PTR value. 70854ceac45SDavid Howells */ 7098cab4c39SChuck Lever struct nfs_client *nfs_init_client(struct nfs_client *clp, 7108cab4c39SChuck Lever const struct rpc_timeout *timeparms, 711f8407299SAndy Adamson const char *ip_addr) 71254ceac45SDavid Howells { 71354ceac45SDavid Howells int error; 71454ceac45SDavid Howells 71554ceac45SDavid Howells if (clp->cl_cons_state == NFS_CS_READY) { 71654ceac45SDavid Howells /* the client is already initialised */ 71754ceac45SDavid Howells dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp); 7188cab4c39SChuck Lever return clp; 71954ceac45SDavid Howells } 72054ceac45SDavid Howells 72154ceac45SDavid Howells /* 72254ceac45SDavid Howells * Create a client RPC handle for doing FSSTAT with UNIX auth only 72354ceac45SDavid Howells * - RFC 2623, sec 2.3.2 72454ceac45SDavid Howells */ 7254bf590e0SChuck Lever error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX); 72654ceac45SDavid Howells if (error < 0) 72754ceac45SDavid Howells goto error; 72854ceac45SDavid Howells nfs_mark_client_ready(clp, NFS_CS_READY); 7298cab4c39SChuck Lever return clp; 73054ceac45SDavid Howells 73154ceac45SDavid Howells error: 73254ceac45SDavid Howells nfs_mark_client_ready(clp, error); 7338cab4c39SChuck Lever nfs_put_client(clp); 73454ceac45SDavid Howells dprintk("<-- nfs_init_client() = xerror %d\n", error); 7358cab4c39SChuck Lever return ERR_PTR(error); 73654ceac45SDavid Howells } 737ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_init_client); 73854ceac45SDavid Howells 73954ceac45SDavid Howells /* 74054ceac45SDavid Howells * Create a version 2 or 3 client 74154ceac45SDavid Howells */ 7422283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server, 743ab7017a3SBryan Schumaker const struct nfs_parsed_mount_data *data, 744ab7017a3SBryan Schumaker struct nfs_subversion *nfs_mod) 74554ceac45SDavid Howells { 7463a498026STrond Myklebust struct nfs_client_initdata cl_init = { 7473a498026STrond Myklebust .hostname = data->nfs_server.hostname, 748d7422c47SChuck Lever .addr = (const struct sockaddr *)&data->nfs_server.address, 7494c568017SChuck Lever .addrlen = data->nfs_server.addrlen, 750ab7017a3SBryan Schumaker .nfs_mod = nfs_mod, 75159dca3b2STrond Myklebust .proto = data->nfs_server.protocol, 752e50a7a1aSStanislav Kinsbursky .net = data->net, 7533a498026STrond Myklebust }; 75433170233STrond Myklebust struct rpc_timeout timeparms; 75554ceac45SDavid Howells struct nfs_client *clp; 7563a498026STrond Myklebust int error; 75754ceac45SDavid Howells 75854ceac45SDavid Howells dprintk("--> nfs_init_server()\n"); 75954ceac45SDavid Howells 76045a52a02SAndy Adamson nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, 76145a52a02SAndy Adamson data->timeo, data->retrans); 7624bf590e0SChuck Lever if (data->flags & NFS_MOUNT_NORESVPORT) 7634bf590e0SChuck Lever set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); 76445a52a02SAndy Adamson 76554ceac45SDavid Howells /* Allocate or find a client reference we can use */ 7664bf590e0SChuck Lever clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX); 76754ceac45SDavid Howells if (IS_ERR(clp)) { 76854ceac45SDavid Howells dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp)); 76954ceac45SDavid Howells return PTR_ERR(clp); 77054ceac45SDavid Howells } 77154ceac45SDavid Howells 77254ceac45SDavid Howells server->nfs_client = clp; 77354ceac45SDavid Howells 77454ceac45SDavid Howells /* Initialise the client representation from the mount data */ 775ff3525a5STrond Myklebust server->flags = data->flags; 776b797cac7SDavid Howells server->options = data->options; 77762ab460cSTrond Myklebust server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 77862ab460cSTrond Myklebust NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP| 7793a1556e8STrond Myklebust NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME|NFS_CAP_CHANGE_ATTR; 78054ceac45SDavid Howells 78154ceac45SDavid Howells if (data->rsize) 78254ceac45SDavid Howells server->rsize = nfs_block_size(data->rsize, NULL); 78354ceac45SDavid Howells if (data->wsize) 78454ceac45SDavid Howells server->wsize = nfs_block_size(data->wsize, NULL); 78554ceac45SDavid Howells 78654ceac45SDavid Howells server->acregmin = data->acregmin * HZ; 78754ceac45SDavid Howells server->acregmax = data->acregmax * HZ; 78854ceac45SDavid Howells server->acdirmin = data->acdirmin * HZ; 78954ceac45SDavid Howells server->acdirmax = data->acdirmax * HZ; 79054ceac45SDavid Howells 79154ceac45SDavid Howells /* Start lockd here, before we might error out */ 79254ceac45SDavid Howells error = nfs_start_lockd(server); 79354ceac45SDavid Howells if (error < 0) 79454ceac45SDavid Howells goto error; 79554ceac45SDavid Howells 796f22d6d79SChuck Lever server->port = data->nfs_server.port; 7970f5f49b8SWeston Andros Adamson server->auth_info = data->auth_info; 798f22d6d79SChuck Lever 799a3f73c27SWeston Andros Adamson error = nfs_init_server_rpcclient(server, &timeparms, 800a3f73c27SWeston Andros Adamson data->selected_flavor); 80154ceac45SDavid Howells if (error < 0) 80254ceac45SDavid Howells goto error; 80354ceac45SDavid Howells 8043f8400d1SChuck Lever /* Preserve the values of mount_server-related mount options */ 8053f8400d1SChuck Lever if (data->mount_server.addrlen) { 8063f8400d1SChuck Lever memcpy(&server->mountd_address, &data->mount_server.address, 8073f8400d1SChuck Lever data->mount_server.addrlen); 8083f8400d1SChuck Lever server->mountd_addrlen = data->mount_server.addrlen; 8093f8400d1SChuck Lever } 8103f8400d1SChuck Lever server->mountd_version = data->mount_server.version; 8113f8400d1SChuck Lever server->mountd_port = data->mount_server.port; 8123f8400d1SChuck Lever server->mountd_protocol = data->mount_server.protocol; 8133f8400d1SChuck Lever 81454ceac45SDavid Howells server->namelen = data->namlen; 81554ceac45SDavid Howells dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp); 81654ceac45SDavid Howells return 0; 81754ceac45SDavid Howells 81854ceac45SDavid Howells error: 81954ceac45SDavid Howells server->nfs_client = NULL; 82054ceac45SDavid Howells nfs_put_client(clp); 82154ceac45SDavid Howells dprintk("<-- nfs_init_server() = xerror %d\n", error); 82254ceac45SDavid Howells return error; 82354ceac45SDavid Howells } 82454ceac45SDavid Howells 82554ceac45SDavid Howells /* 82654ceac45SDavid Howells * Load up the server record from information gained in an fsinfo record 82754ceac45SDavid Howells */ 828738fd0f3SBenny Halevy static void nfs_server_set_fsinfo(struct nfs_server *server, 829738fd0f3SBenny Halevy struct nfs_fh *mntfh, 830738fd0f3SBenny Halevy struct nfs_fsinfo *fsinfo) 83154ceac45SDavid Howells { 83254ceac45SDavid Howells unsigned long max_rpc_payload; 83354ceac45SDavid Howells 83454ceac45SDavid Howells /* Work out a lot of parameters */ 83554ceac45SDavid Howells if (server->rsize == 0) 83654ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtpref, NULL); 83754ceac45SDavid Howells if (server->wsize == 0) 83854ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtpref, NULL); 83954ceac45SDavid Howells 84054ceac45SDavid Howells if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) 84154ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtmax, NULL); 84254ceac45SDavid Howells if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) 84354ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtmax, NULL); 84454ceac45SDavid Howells 84554ceac45SDavid Howells max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL); 84654ceac45SDavid Howells if (server->rsize > max_rpc_payload) 84754ceac45SDavid Howells server->rsize = max_rpc_payload; 84854ceac45SDavid Howells if (server->rsize > NFS_MAX_FILE_IO_SIZE) 84954ceac45SDavid Howells server->rsize = NFS_MAX_FILE_IO_SIZE; 85054ceac45SDavid Howells server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 851e0bf68ddSPeter Zijlstra 852d993831fSJens Axboe server->backing_dev_info.name = "nfs"; 85354ceac45SDavid Howells server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD; 85454ceac45SDavid Howells 85554ceac45SDavid Howells if (server->wsize > max_rpc_payload) 85654ceac45SDavid Howells server->wsize = max_rpc_payload; 85754ceac45SDavid Howells if (server->wsize > NFS_MAX_FILE_IO_SIZE) 85854ceac45SDavid Howells server->wsize = NFS_MAX_FILE_IO_SIZE; 85954ceac45SDavid Howells server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 86085e174baSRicardo Labiaga 86154ceac45SDavid Howells server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); 86254ceac45SDavid Howells 86354ceac45SDavid Howells server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); 86456e4ebf8SBryan Schumaker if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES) 86556e4ebf8SBryan Schumaker server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES; 86654ceac45SDavid Howells if (server->dtsize > server->rsize) 86754ceac45SDavid Howells server->dtsize = server->rsize; 86854ceac45SDavid Howells 86954ceac45SDavid Howells if (server->flags & NFS_MOUNT_NOAC) { 87054ceac45SDavid Howells server->acregmin = server->acregmax = 0; 87154ceac45SDavid Howells server->acdirmin = server->acdirmax = 0; 87254ceac45SDavid Howells } 87354ceac45SDavid Howells 87454ceac45SDavid Howells server->maxfilesize = fsinfo->maxfilesize; 87554ceac45SDavid Howells 8766b96724eSRicardo Labiaga server->time_delta = fsinfo->time_delta; 8776b96724eSRicardo Labiaga 87854ceac45SDavid Howells /* We're airborne Set socket buffersize */ 87954ceac45SDavid Howells rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); 88054ceac45SDavid Howells } 88154ceac45SDavid Howells 88254ceac45SDavid Howells /* 88354ceac45SDavid Howells * Probe filesystem information, including the FSID on v2/v3 88454ceac45SDavid Howells */ 885fcf10398SBryan Schumaker int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 88654ceac45SDavid Howells { 88754ceac45SDavid Howells struct nfs_fsinfo fsinfo; 88854ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 88954ceac45SDavid Howells int error; 89054ceac45SDavid Howells 89154ceac45SDavid Howells dprintk("--> nfs_probe_fsinfo()\n"); 89254ceac45SDavid Howells 89354ceac45SDavid Howells if (clp->rpc_ops->set_capabilities != NULL) { 89454ceac45SDavid Howells error = clp->rpc_ops->set_capabilities(server, mntfh); 89554ceac45SDavid Howells if (error < 0) 89654ceac45SDavid Howells goto out_error; 89754ceac45SDavid Howells } 89854ceac45SDavid Howells 89954ceac45SDavid Howells fsinfo.fattr = fattr; 90085e174baSRicardo Labiaga fsinfo.layouttype = 0; 90154ceac45SDavid Howells error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); 90254ceac45SDavid Howells if (error < 0) 90354ceac45SDavid Howells goto out_error; 90454ceac45SDavid Howells 905738fd0f3SBenny Halevy nfs_server_set_fsinfo(server, mntfh, &fsinfo); 90654ceac45SDavid Howells 90754ceac45SDavid Howells /* Get some general file system info */ 90854ceac45SDavid Howells if (server->namelen == 0) { 90954ceac45SDavid Howells struct nfs_pathconf pathinfo; 91054ceac45SDavid Howells 91154ceac45SDavid Howells pathinfo.fattr = fattr; 91254ceac45SDavid Howells nfs_fattr_init(fattr); 91354ceac45SDavid Howells 91454ceac45SDavid Howells if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) 91554ceac45SDavid Howells server->namelen = pathinfo.max_namelen; 91654ceac45SDavid Howells } 91754ceac45SDavid Howells 91854ceac45SDavid Howells dprintk("<-- nfs_probe_fsinfo() = 0\n"); 91954ceac45SDavid Howells return 0; 92054ceac45SDavid Howells 92154ceac45SDavid Howells out_error: 92254ceac45SDavid Howells dprintk("nfs_probe_fsinfo: error = %d\n", -error); 92354ceac45SDavid Howells return error; 92454ceac45SDavid Howells } 92589d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_probe_fsinfo); 92654ceac45SDavid Howells 92754ceac45SDavid Howells /* 92854ceac45SDavid Howells * Copy useful information when duplicating a server record 92954ceac45SDavid Howells */ 930fcf10398SBryan Schumaker void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 93154ceac45SDavid Howells { 93254ceac45SDavid Howells target->flags = source->flags; 933356e76b8SChuck Lever target->rsize = source->rsize; 934356e76b8SChuck Lever target->wsize = source->wsize; 93554ceac45SDavid Howells target->acregmin = source->acregmin; 93654ceac45SDavid Howells target->acregmax = source->acregmax; 93754ceac45SDavid Howells target->acdirmin = source->acdirmin; 93854ceac45SDavid Howells target->acdirmax = source->acdirmax; 93954ceac45SDavid Howells target->caps = source->caps; 9402df54806SDavid Howells target->options = source->options; 9410f5f49b8SWeston Andros Adamson target->auth_info = source->auth_info; 94254ceac45SDavid Howells } 94389d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_copy_userdata); 94454ceac45SDavid Howells 945fcf10398SBryan Schumaker void nfs_server_insert_lists(struct nfs_server *server) 946fca5238eSChuck Lever { 947fca5238eSChuck Lever struct nfs_client *clp = server->nfs_client; 94873ea666cSChuck Lever struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 949fca5238eSChuck Lever 950dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 951fca5238eSChuck Lever list_add_tail_rcu(&server->client_link, &clp->cl_superblocks); 952c25d32b2SStanislav Kinsbursky list_add_tail(&server->master_link, &nn->nfs_volume_list); 953d3b4c9d7SAndy Adamson clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 954dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 955fca5238eSChuck Lever 956fca5238eSChuck Lever } 95789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_server_insert_lists); 958fca5238eSChuck Lever 95932e62b7cSChuck Lever void nfs_server_remove_lists(struct nfs_server *server) 960fca5238eSChuck Lever { 961d3b4c9d7SAndy Adamson struct nfs_client *clp = server->nfs_client; 9624c03ae4aSTrond Myklebust struct nfs_net *nn; 963d3b4c9d7SAndy Adamson 9644c03ae4aSTrond Myklebust if (clp == NULL) 9654c03ae4aSTrond Myklebust return; 96673ea666cSChuck Lever nn = net_generic(clp->cl_net, nfs_net_id); 967dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 968fca5238eSChuck Lever list_del_rcu(&server->client_link); 9694c03ae4aSTrond Myklebust if (list_empty(&clp->cl_superblocks)) 970d3b4c9d7SAndy Adamson set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 971fca5238eSChuck Lever list_del(&server->master_link); 972dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 973fca5238eSChuck Lever 974fca5238eSChuck Lever synchronize_rcu(); 975fca5238eSChuck Lever } 97632e62b7cSChuck Lever EXPORT_SYMBOL_GPL(nfs_server_remove_lists); 977fca5238eSChuck Lever 97854ceac45SDavid Howells /* 97954ceac45SDavid Howells * Allocate and initialise a server record 98054ceac45SDavid Howells */ 981fcf10398SBryan Schumaker struct nfs_server *nfs_alloc_server(void) 98254ceac45SDavid Howells { 98354ceac45SDavid Howells struct nfs_server *server; 98454ceac45SDavid Howells 98554ceac45SDavid Howells server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); 98654ceac45SDavid Howells if (!server) 98754ceac45SDavid Howells return NULL; 98854ceac45SDavid Howells 98954ceac45SDavid Howells server->client = server->client_acl = ERR_PTR(-EINVAL); 99054ceac45SDavid Howells 99154ceac45SDavid Howells /* Zero out the NFS state stuff */ 99254ceac45SDavid Howells INIT_LIST_HEAD(&server->client_link); 99354ceac45SDavid Howells INIT_LIST_HEAD(&server->master_link); 994d3978bb3SChuck Lever INIT_LIST_HEAD(&server->delegations); 9956382a441SWeston Andros Adamson INIT_LIST_HEAD(&server->layouts); 9960aaaf5c4SChuck Lever INIT_LIST_HEAD(&server->state_owners_lru); 99754ceac45SDavid Howells 998ef818a28SSteve Dickson atomic_set(&server->active, 0); 999ef818a28SSteve Dickson 100054ceac45SDavid Howells server->io_stats = nfs_alloc_iostats(); 100154ceac45SDavid Howells if (!server->io_stats) { 100254ceac45SDavid Howells kfree(server); 100354ceac45SDavid Howells return NULL; 100454ceac45SDavid Howells } 100554ceac45SDavid Howells 100648d07649SJens Axboe if (bdi_init(&server->backing_dev_info)) { 100748d07649SJens Axboe nfs_free_iostats(server->io_stats); 100848d07649SJens Axboe kfree(server); 100948d07649SJens Axboe return NULL; 101048d07649SJens Axboe } 101148d07649SJens Axboe 10129157c31dSTrond Myklebust ida_init(&server->openowner_id); 1013d2d7ce28STrond Myklebust ida_init(&server->lockowner_id); 1014f7e8917aSFred Isaman pnfs_init_server(server); 1015f7e8917aSFred Isaman 101654ceac45SDavid Howells return server; 101754ceac45SDavid Howells } 101889d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_alloc_server); 101954ceac45SDavid Howells 102054ceac45SDavid Howells /* 102154ceac45SDavid Howells * Free up a server record 102254ceac45SDavid Howells */ 102354ceac45SDavid Howells void nfs_free_server(struct nfs_server *server) 102454ceac45SDavid Howells { 102554ceac45SDavid Howells dprintk("--> nfs_free_server()\n"); 102654ceac45SDavid Howells 1027fca5238eSChuck Lever nfs_server_remove_lists(server); 102854ceac45SDavid Howells 102954ceac45SDavid Howells if (server->destroy != NULL) 103054ceac45SDavid Howells server->destroy(server); 10315cef338bSTrond Myklebust 10325cef338bSTrond Myklebust if (!IS_ERR(server->client_acl)) 10335cef338bSTrond Myklebust rpc_shutdown_client(server->client_acl); 103454ceac45SDavid Howells if (!IS_ERR(server->client)) 103554ceac45SDavid Howells rpc_shutdown_client(server->client); 103654ceac45SDavid Howells 103754ceac45SDavid Howells nfs_put_client(server->nfs_client); 103854ceac45SDavid Howells 1039d2d7ce28STrond Myklebust ida_destroy(&server->lockowner_id); 10409157c31dSTrond Myklebust ida_destroy(&server->openowner_id); 104154ceac45SDavid Howells nfs_free_iostats(server->io_stats); 1042e0bf68ddSPeter Zijlstra bdi_destroy(&server->backing_dev_info); 104354ceac45SDavid Howells kfree(server); 104454ceac45SDavid Howells nfs_release_automount_timer(); 104554ceac45SDavid Howells dprintk("<-- nfs_free_server()\n"); 104654ceac45SDavid Howells } 104789d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_free_server); 104854ceac45SDavid Howells 104954ceac45SDavid Howells /* 105054ceac45SDavid Howells * Create a version 2 or 3 volume record 105154ceac45SDavid Howells * - keyed on server and FSID 105254ceac45SDavid Howells */ 10531179acc6SBryan Schumaker struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info, 1054ab7017a3SBryan Schumaker struct nfs_subversion *nfs_mod) 105554ceac45SDavid Howells { 105654ceac45SDavid Howells struct nfs_server *server; 1057fbca779aSTrond Myklebust struct nfs_fattr *fattr; 105854ceac45SDavid Howells int error; 105954ceac45SDavid Howells 106054ceac45SDavid Howells server = nfs_alloc_server(); 106154ceac45SDavid Howells if (!server) 106254ceac45SDavid Howells return ERR_PTR(-ENOMEM); 106354ceac45SDavid Howells 1064fbca779aSTrond Myklebust error = -ENOMEM; 1065fbca779aSTrond Myklebust fattr = nfs_alloc_fattr(); 1066fbca779aSTrond Myklebust if (fattr == NULL) 1067fbca779aSTrond Myklebust goto error; 1068fbca779aSTrond Myklebust 106954ceac45SDavid Howells /* Get a client representation */ 10701179acc6SBryan Schumaker error = nfs_init_server(server, mount_info->parsed, nfs_mod); 107154ceac45SDavid Howells if (error < 0) 107254ceac45SDavid Howells goto error; 107354ceac45SDavid Howells 107454ceac45SDavid Howells /* Probe the root fh to retrieve its FSID */ 10751179acc6SBryan Schumaker error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr); 107654ceac45SDavid Howells if (error < 0) 107754ceac45SDavid Howells goto error; 107854af3bb5STrond Myklebust if (server->nfs_client->rpc_ops->version == 3) { 107954af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) 108054af3bb5STrond Myklebust server->namelen = NFS3_MAXNAMLEN; 10811179acc6SBryan Schumaker if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS)) 108254af3bb5STrond Myklebust server->caps |= NFS_CAP_READDIRPLUS; 108354af3bb5STrond Myklebust } else { 108454af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) 108554af3bb5STrond Myklebust server->namelen = NFS2_MAXNAMLEN; 108654af3bb5STrond Myklebust } 108754af3bb5STrond Myklebust 1088fbca779aSTrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR)) { 10891775fd3eSDavid Quigley error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr, NULL); 109054ceac45SDavid Howells if (error < 0) { 109154ceac45SDavid Howells dprintk("nfs_create_server: getattr error = %d\n", -error); 109254ceac45SDavid Howells goto error; 109354ceac45SDavid Howells } 109454ceac45SDavid Howells } 1095fbca779aSTrond Myklebust memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 109654ceac45SDavid Howells 10976daabf1bSDavid Howells dprintk("Server FSID: %llx:%llx\n", 10986daabf1bSDavid Howells (unsigned long long) server->fsid.major, 10996daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 110054ceac45SDavid Howells 1101fca5238eSChuck Lever nfs_server_insert_lists(server); 110254ceac45SDavid Howells server->mount_time = jiffies; 1103fbca779aSTrond Myklebust nfs_free_fattr(fattr); 110454ceac45SDavid Howells return server; 110554ceac45SDavid Howells 110654ceac45SDavid Howells error: 1107fbca779aSTrond Myklebust nfs_free_fattr(fattr); 110854ceac45SDavid Howells nfs_free_server(server); 110954ceac45SDavid Howells return ERR_PTR(error); 111054ceac45SDavid Howells } 1111ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_create_server); 111254ceac45SDavid Howells 111354ceac45SDavid Howells /* 111454ceac45SDavid Howells * Clone an NFS2, NFS3 or NFS4 server record 111554ceac45SDavid Howells */ 111654ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source, 111754ceac45SDavid Howells struct nfs_fh *fh, 11187e6eb683SBryan Schumaker struct nfs_fattr *fattr, 11197e6eb683SBryan Schumaker rpc_authflavor_t flavor) 112054ceac45SDavid Howells { 112154ceac45SDavid Howells struct nfs_server *server; 1122fbca779aSTrond Myklebust struct nfs_fattr *fattr_fsinfo; 112354ceac45SDavid Howells int error; 112454ceac45SDavid Howells 112554ceac45SDavid Howells dprintk("--> nfs_clone_server(,%llx:%llx,)\n", 11266daabf1bSDavid Howells (unsigned long long) fattr->fsid.major, 11276daabf1bSDavid Howells (unsigned long long) fattr->fsid.minor); 112854ceac45SDavid Howells 112954ceac45SDavid Howells server = nfs_alloc_server(); 113054ceac45SDavid Howells if (!server) 113154ceac45SDavid Howells return ERR_PTR(-ENOMEM); 113254ceac45SDavid Howells 1133fbca779aSTrond Myklebust error = -ENOMEM; 1134fbca779aSTrond Myklebust fattr_fsinfo = nfs_alloc_fattr(); 1135fbca779aSTrond Myklebust if (fattr_fsinfo == NULL) 1136fbca779aSTrond Myklebust goto out_free_server; 1137fbca779aSTrond Myklebust 113854ceac45SDavid Howells /* Copy data from the source */ 113954ceac45SDavid Howells server->nfs_client = source->nfs_client; 11400aaaf5c4SChuck Lever server->destroy = source->destroy; 114154ceac45SDavid Howells atomic_inc(&server->nfs_client->cl_count); 114254ceac45SDavid Howells nfs_server_copy_userdata(server, source); 114354ceac45SDavid Howells 114454ceac45SDavid Howells server->fsid = fattr->fsid; 114554ceac45SDavid Howells 114633170233STrond Myklebust error = nfs_init_server_rpcclient(server, 114733170233STrond Myklebust source->client->cl_timeout, 11487e6eb683SBryan Schumaker flavor); 114954ceac45SDavid Howells if (error < 0) 115054ceac45SDavid Howells goto out_free_server; 115154ceac45SDavid Howells 115254ceac45SDavid Howells /* probe the filesystem info for this server filesystem */ 1153fbca779aSTrond Myklebust error = nfs_probe_fsinfo(server, fh, fattr_fsinfo); 115454ceac45SDavid Howells if (error < 0) 115554ceac45SDavid Howells goto out_free_server; 115654ceac45SDavid Howells 115754af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 115854af3bb5STrond Myklebust server->namelen = NFS4_MAXNAMLEN; 115954af3bb5STrond Myklebust 116054ceac45SDavid Howells dprintk("Cloned FSID: %llx:%llx\n", 11616daabf1bSDavid Howells (unsigned long long) server->fsid.major, 11626daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 116354ceac45SDavid Howells 116454ceac45SDavid Howells error = nfs_start_lockd(server); 116554ceac45SDavid Howells if (error < 0) 116654ceac45SDavid Howells goto out_free_server; 116754ceac45SDavid Howells 1168fca5238eSChuck Lever nfs_server_insert_lists(server); 116954ceac45SDavid Howells server->mount_time = jiffies; 117054ceac45SDavid Howells 1171fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 117254ceac45SDavid Howells dprintk("<-- nfs_clone_server() = %p\n", server); 117354ceac45SDavid Howells return server; 117454ceac45SDavid Howells 117554ceac45SDavid Howells out_free_server: 1176fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 117754ceac45SDavid Howells nfs_free_server(server); 117854ceac45SDavid Howells dprintk("<-- nfs_clone_server() = error %d\n", error); 117954ceac45SDavid Howells return ERR_PTR(error); 118054ceac45SDavid Howells } 1181ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_clone_server); 11826aaca566SDavid Howells 11836b13168bSStanislav Kinsbursky void nfs_clients_init(struct net *net) 11846b13168bSStanislav Kinsbursky { 11856b13168bSStanislav Kinsbursky struct nfs_net *nn = net_generic(net, nfs_net_id); 11866b13168bSStanislav Kinsbursky 11876b13168bSStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_client_list); 1188c25d32b2SStanislav Kinsbursky INIT_LIST_HEAD(&nn->nfs_volume_list); 118989d77c8fSBryan Schumaker #if IS_ENABLED(CONFIG_NFS_V4) 119028cd1b3fSStanislav Kinsbursky idr_init(&nn->cb_ident_idr); 119128cd1b3fSStanislav Kinsbursky #endif 11924c03ae4aSTrond Myklebust spin_lock_init(&nn->nfs_client_lock); 1193f092075dSChuck Lever nn->boot_time = CURRENT_TIME; 11946b13168bSStanislav Kinsbursky } 11956b13168bSStanislav Kinsbursky 11966aaca566SDavid Howells #ifdef CONFIG_PROC_FS 11976aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs; 11986aaca566SDavid Howells 11996aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file); 12006aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); 12016aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); 12026aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v); 12036aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v); 12046aaca566SDavid Howells 120588e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = { 12066aaca566SDavid Howells .start = nfs_server_list_start, 12076aaca566SDavid Howells .next = nfs_server_list_next, 12086aaca566SDavid Howells .stop = nfs_server_list_stop, 12096aaca566SDavid Howells .show = nfs_server_list_show, 12106aaca566SDavid Howells }; 12116aaca566SDavid Howells 121200977a59SArjan van de Ven static const struct file_operations nfs_server_list_fops = { 12136aaca566SDavid Howells .open = nfs_server_list_open, 12146aaca566SDavid Howells .read = seq_read, 12156aaca566SDavid Howells .llseek = seq_lseek, 121665b38851SEric W. Biederman .release = seq_release_net, 121734b37235SDenis V. Lunev .owner = THIS_MODULE, 12186aaca566SDavid Howells }; 12196aaca566SDavid Howells 12206aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file); 12216aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); 12226aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); 12236aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v); 12246aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v); 12256aaca566SDavid Howells 122688e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = { 12276aaca566SDavid Howells .start = nfs_volume_list_start, 12286aaca566SDavid Howells .next = nfs_volume_list_next, 12296aaca566SDavid Howells .stop = nfs_volume_list_stop, 12306aaca566SDavid Howells .show = nfs_volume_list_show, 12316aaca566SDavid Howells }; 12326aaca566SDavid Howells 123300977a59SArjan van de Ven static const struct file_operations nfs_volume_list_fops = { 12346aaca566SDavid Howells .open = nfs_volume_list_open, 12356aaca566SDavid Howells .read = seq_read, 12366aaca566SDavid Howells .llseek = seq_lseek, 123765b38851SEric W. Biederman .release = seq_release_net, 123834b37235SDenis V. Lunev .owner = THIS_MODULE, 12396aaca566SDavid Howells }; 12406aaca566SDavid Howells 12416aaca566SDavid Howells /* 12426aaca566SDavid Howells * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which 12436aaca566SDavid Howells * we're dealing 12446aaca566SDavid Howells */ 12456aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file) 12466aaca566SDavid Howells { 124765b38851SEric W. Biederman return seq_open_net(inode, file, &nfs_server_list_ops, 124865b38851SEric W. Biederman sizeof(struct seq_net_private)); 12496aaca566SDavid Howells } 12506aaca566SDavid Howells 12516aaca566SDavid Howells /* 12526aaca566SDavid Howells * set up the iterator to start reading from the server list and return the first item 12536aaca566SDavid Howells */ 12546aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) 1255*8d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 12566aaca566SDavid Howells { 125765b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 12586b13168bSStanislav Kinsbursky 12596aaca566SDavid Howells /* lock the list against modification */ 1260dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 12616b13168bSStanislav Kinsbursky return seq_list_start_head(&nn->nfs_client_list, *_pos); 12626aaca566SDavid Howells } 12636aaca566SDavid Howells 12646aaca566SDavid Howells /* 12656aaca566SDavid Howells * move to next server 12666aaca566SDavid Howells */ 12676aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) 12686aaca566SDavid Howells { 126965b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 12706b13168bSStanislav Kinsbursky 12716b13168bSStanislav Kinsbursky return seq_list_next(v, &nn->nfs_client_list, pos); 12726aaca566SDavid Howells } 12736aaca566SDavid Howells 12746aaca566SDavid Howells /* 12756aaca566SDavid Howells * clean up after reading from the transports list 12766aaca566SDavid Howells */ 12776aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v) 1278*8d11620eSJeff Layton __releases(&nn->nfs_client_lock) 12796aaca566SDavid Howells { 128065b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1281dc030858SStanislav Kinsbursky 1282dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 12836aaca566SDavid Howells } 12846aaca566SDavid Howells 12856aaca566SDavid Howells /* 12866aaca566SDavid Howells * display a header line followed by a load of call lines 12876aaca566SDavid Howells */ 12886aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v) 12896aaca566SDavid Howells { 12906aaca566SDavid Howells struct nfs_client *clp; 129165b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 12926aaca566SDavid Howells 12936aaca566SDavid Howells /* display header on line 1 */ 12946b13168bSStanislav Kinsbursky if (v == &nn->nfs_client_list) { 12956aaca566SDavid Howells seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); 12966aaca566SDavid Howells return 0; 12976aaca566SDavid Howells } 12986aaca566SDavid Howells 12996aaca566SDavid Howells /* display one transport per line on subsequent lines */ 13006aaca566SDavid Howells clp = list_entry(v, struct nfs_client, cl_share_link); 13016aaca566SDavid Howells 1302940aab49SMalahal Naineni /* Check if the client is initialized */ 1303940aab49SMalahal Naineni if (clp->cl_cons_state != NFS_CS_READY) 1304940aab49SMalahal Naineni return 0; 1305940aab49SMalahal Naineni 13062446ab60STrond Myklebust rcu_read_lock(); 13075d8515caSChuck Lever seq_printf(m, "v%u %s %s %3d %s\n", 130840c55319STrond Myklebust clp->rpc_ops->version, 13095d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 13105d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 13116aaca566SDavid Howells atomic_read(&clp->cl_count), 13126aaca566SDavid Howells clp->cl_hostname); 13132446ab60STrond Myklebust rcu_read_unlock(); 13146aaca566SDavid Howells 13156aaca566SDavid Howells return 0; 13166aaca566SDavid Howells } 13176aaca566SDavid Howells 13186aaca566SDavid Howells /* 13196aaca566SDavid Howells * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes 13206aaca566SDavid Howells */ 13216aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file) 13226aaca566SDavid Howells { 132365b38851SEric W. Biederman return seq_open_net(inode, file, &nfs_server_list_ops, 132465b38851SEric W. Biederman sizeof(struct seq_net_private)); 13256aaca566SDavid Howells } 13266aaca566SDavid Howells 13276aaca566SDavid Howells /* 13286aaca566SDavid Howells * set up the iterator to start reading from the volume list and return the first item 13296aaca566SDavid Howells */ 13306aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) 1331*8d11620eSJeff Layton __acquires(&nn->nfs_client_lock) 13326aaca566SDavid Howells { 133365b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1334c25d32b2SStanislav Kinsbursky 13356aaca566SDavid Howells /* lock the list against modification */ 1336dc030858SStanislav Kinsbursky spin_lock(&nn->nfs_client_lock); 1337c25d32b2SStanislav Kinsbursky return seq_list_start_head(&nn->nfs_volume_list, *_pos); 13386aaca566SDavid Howells } 13396aaca566SDavid Howells 13406aaca566SDavid Howells /* 13416aaca566SDavid Howells * move to next volume 13426aaca566SDavid Howells */ 13436aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) 13446aaca566SDavid Howells { 134565b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1346c25d32b2SStanislav Kinsbursky 1347c25d32b2SStanislav Kinsbursky return seq_list_next(v, &nn->nfs_volume_list, pos); 13486aaca566SDavid Howells } 13496aaca566SDavid Howells 13506aaca566SDavid Howells /* 13516aaca566SDavid Howells * clean up after reading from the transports list 13526aaca566SDavid Howells */ 13536aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v) 1354*8d11620eSJeff Layton __releases(&nn->nfs_client_lock) 13556aaca566SDavid Howells { 135665b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1357dc030858SStanislav Kinsbursky 1358dc030858SStanislav Kinsbursky spin_unlock(&nn->nfs_client_lock); 13596aaca566SDavid Howells } 13606aaca566SDavid Howells 13616aaca566SDavid Howells /* 13626aaca566SDavid Howells * display a header line followed by a load of call lines 13636aaca566SDavid Howells */ 13646aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v) 13656aaca566SDavid Howells { 13666aaca566SDavid Howells struct nfs_server *server; 13676aaca566SDavid Howells struct nfs_client *clp; 13686aaca566SDavid Howells char dev[8], fsid[17]; 136965b38851SEric W. Biederman struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 13706aaca566SDavid Howells 13716aaca566SDavid Howells /* display header on line 1 */ 1372c25d32b2SStanislav Kinsbursky if (v == &nn->nfs_volume_list) { 13735d1acff1SDavid Howells seq_puts(m, "NV SERVER PORT DEV FSID FSC\n"); 13746aaca566SDavid Howells return 0; 13756aaca566SDavid Howells } 13766aaca566SDavid Howells /* display one transport per line on subsequent lines */ 13776aaca566SDavid Howells server = list_entry(v, struct nfs_server, master_link); 13786aaca566SDavid Howells clp = server->nfs_client; 13796aaca566SDavid Howells 13806aaca566SDavid Howells snprintf(dev, 8, "%u:%u", 13816aaca566SDavid Howells MAJOR(server->s_dev), MINOR(server->s_dev)); 13826aaca566SDavid Howells 13836aaca566SDavid Howells snprintf(fsid, 17, "%llx:%llx", 13846daabf1bSDavid Howells (unsigned long long) server->fsid.major, 13856daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 13866aaca566SDavid Howells 13872446ab60STrond Myklebust rcu_read_lock(); 13885d1acff1SDavid Howells seq_printf(m, "v%u %s %s %-7s %-17s %s\n", 138940c55319STrond Myklebust clp->rpc_ops->version, 13905d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 13915d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 13926aaca566SDavid Howells dev, 13935d1acff1SDavid Howells fsid, 13945d1acff1SDavid Howells nfs_server_fscache_state(server)); 13952446ab60STrond Myklebust rcu_read_unlock(); 13966aaca566SDavid Howells 13976aaca566SDavid Howells return 0; 13986aaca566SDavid Howells } 13996aaca566SDavid Howells 140065b38851SEric W. Biederman int nfs_fs_proc_net_init(struct net *net) 140165b38851SEric W. Biederman { 140265b38851SEric W. Biederman struct nfs_net *nn = net_generic(net, nfs_net_id); 140365b38851SEric W. Biederman struct proc_dir_entry *p; 140465b38851SEric W. Biederman 140565b38851SEric W. Biederman nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net); 140665b38851SEric W. Biederman if (!nn->proc_nfsfs) 140765b38851SEric W. Biederman goto error_0; 140865b38851SEric W. Biederman 140965b38851SEric W. Biederman /* a file of servers with which we're dealing */ 141065b38851SEric W. Biederman p = proc_create("servers", S_IFREG|S_IRUGO, 141165b38851SEric W. Biederman nn->proc_nfsfs, &nfs_server_list_fops); 141265b38851SEric W. Biederman if (!p) 141365b38851SEric W. Biederman goto error_1; 141465b38851SEric W. Biederman 141565b38851SEric W. Biederman /* a file of volumes that we have mounted */ 141665b38851SEric W. Biederman p = proc_create("volumes", S_IFREG|S_IRUGO, 141765b38851SEric W. Biederman nn->proc_nfsfs, &nfs_volume_list_fops); 141865b38851SEric W. Biederman if (!p) 141921e81002SCong Wang goto error_1; 142065b38851SEric W. Biederman return 0; 142165b38851SEric W. Biederman 142265b38851SEric W. Biederman error_1: 142321e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 142465b38851SEric W. Biederman error_0: 142565b38851SEric W. Biederman return -ENOMEM; 142665b38851SEric W. Biederman } 142765b38851SEric W. Biederman 142865b38851SEric W. Biederman void nfs_fs_proc_net_exit(struct net *net) 142965b38851SEric W. Biederman { 143021e81002SCong Wang remove_proc_subtree("nfsfs", net->proc_net); 143165b38851SEric W. Biederman } 143265b38851SEric W. Biederman 14336aaca566SDavid Howells /* 14346aaca566SDavid Howells * initialise the /proc/fs/nfsfs/ directory 14356aaca566SDavid Howells */ 14366aaca566SDavid Howells int __init nfs_fs_proc_init(void) 14376aaca566SDavid Howells { 14386aaca566SDavid Howells struct proc_dir_entry *p; 14396aaca566SDavid Howells 144036a5aeb8SAlexey Dobriyan proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL); 14416aaca566SDavid Howells if (!proc_fs_nfs) 14426aaca566SDavid Howells goto error_0; 14436aaca566SDavid Howells 14446aaca566SDavid Howells /* a file of servers with which we're dealing */ 144565b38851SEric W. Biederman p = proc_symlink("servers", proc_fs_nfs, "../../net/nfsfs/servers"); 14466aaca566SDavid Howells if (!p) 14476aaca566SDavid Howells goto error_1; 14486aaca566SDavid Howells 14496aaca566SDavid Howells /* a file of volumes that we have mounted */ 145065b38851SEric W. Biederman p = proc_symlink("volumes", proc_fs_nfs, "../../net/nfsfs/volumes"); 14516aaca566SDavid Howells if (!p) 14526aaca566SDavid Howells goto error_2; 14536aaca566SDavid Howells return 0; 14546aaca566SDavid Howells 14556aaca566SDavid Howells error_2: 14566aaca566SDavid Howells remove_proc_entry("servers", proc_fs_nfs); 14576aaca566SDavid Howells error_1: 145836a5aeb8SAlexey Dobriyan remove_proc_entry("fs/nfsfs", NULL); 14596aaca566SDavid Howells error_0: 14606aaca566SDavid Howells return -ENOMEM; 14616aaca566SDavid Howells } 14626aaca566SDavid Howells 14636aaca566SDavid Howells /* 14646aaca566SDavid Howells * clean up the /proc/fs/nfsfs/ directory 14656aaca566SDavid Howells */ 14666aaca566SDavid Howells void nfs_fs_proc_exit(void) 14676aaca566SDavid Howells { 14686aaca566SDavid Howells remove_proc_entry("volumes", proc_fs_nfs); 14696aaca566SDavid Howells remove_proc_entry("servers", proc_fs_nfs); 147036a5aeb8SAlexey Dobriyan remove_proc_entry("fs/nfsfs", NULL); 14716aaca566SDavid Howells } 14726aaca566SDavid Howells 14736aaca566SDavid Howells #endif /* CONFIG_PROC_FS */ 1474