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> 393b0d3f93STrond Myklebust #include <net/ipv6.h> 4024c8dbbbSDavid Howells #include <linux/nfs_xdr.h> 410b5b7ae0SAndy Adamson #include <linux/sunrpc/bc_xprt.h> 4224c8dbbbSDavid Howells 4324c8dbbbSDavid Howells #include <asm/system.h> 4424c8dbbbSDavid Howells 4524c8dbbbSDavid Howells #include "nfs4_fs.h" 4624c8dbbbSDavid Howells #include "callback.h" 4724c8dbbbSDavid Howells #include "delegation.h" 4824c8dbbbSDavid Howells #include "iostat.h" 4924c8dbbbSDavid Howells #include "internal.h" 5014727281SDavid Howells #include "fscache.h" 5185e174baSRicardo Labiaga #include "pnfs.h" 5224c8dbbbSDavid Howells 5324c8dbbbSDavid Howells #define NFSDBG_FACILITY NFSDBG_CLIENT 5424c8dbbbSDavid Howells 5524c8dbbbSDavid Howells static DEFINE_SPINLOCK(nfs_client_lock); 5624c8dbbbSDavid Howells static LIST_HEAD(nfs_client_list); 5754ceac45SDavid Howells static LIST_HEAD(nfs_volume_list); 5824c8dbbbSDavid Howells static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); 59f4eecd5dSAndy Adamson #ifdef CONFIG_NFS_V4 60f4eecd5dSAndy Adamson static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */ 61f4eecd5dSAndy Adamson 62f4eecd5dSAndy Adamson /* 63f4eecd5dSAndy Adamson * Get a unique NFSv4.0 callback identifier which will be used 64f4eecd5dSAndy Adamson * by the V4.0 callback service to lookup the nfs_client struct 65f4eecd5dSAndy Adamson */ 66f4eecd5dSAndy Adamson static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) 67f4eecd5dSAndy Adamson { 68f4eecd5dSAndy Adamson int ret = 0; 69f4eecd5dSAndy Adamson 70f4eecd5dSAndy Adamson if (clp->rpc_ops->version != 4 || minorversion != 0) 71f4eecd5dSAndy Adamson return ret; 72f4eecd5dSAndy Adamson retry: 73f4eecd5dSAndy Adamson if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL)) 74f4eecd5dSAndy Adamson return -ENOMEM; 75f4eecd5dSAndy Adamson spin_lock(&nfs_client_lock); 76f4eecd5dSAndy Adamson ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident); 77f4eecd5dSAndy Adamson spin_unlock(&nfs_client_lock); 78f4eecd5dSAndy Adamson if (ret == -EAGAIN) 79f4eecd5dSAndy Adamson goto retry; 80f4eecd5dSAndy Adamson return ret; 81f4eecd5dSAndy Adamson } 82f4eecd5dSAndy Adamson #endif /* CONFIG_NFS_V4 */ 8324c8dbbbSDavid Howells 8424c8dbbbSDavid Howells /* 855006a76cSDavid Howells * RPC cruft for NFS 865006a76cSDavid Howells */ 875006a76cSDavid Howells static struct rpc_version *nfs_version[5] = { 885006a76cSDavid Howells [2] = &nfs_version2, 895006a76cSDavid Howells #ifdef CONFIG_NFS_V3 905006a76cSDavid Howells [3] = &nfs_version3, 915006a76cSDavid Howells #endif 925006a76cSDavid Howells #ifdef CONFIG_NFS_V4 935006a76cSDavid Howells [4] = &nfs_version4, 945006a76cSDavid Howells #endif 955006a76cSDavid Howells }; 965006a76cSDavid Howells 975006a76cSDavid Howells struct rpc_program nfs_program = { 985006a76cSDavid Howells .name = "nfs", 995006a76cSDavid Howells .number = NFS_PROGRAM, 1005006a76cSDavid Howells .nrvers = ARRAY_SIZE(nfs_version), 1015006a76cSDavid Howells .version = nfs_version, 1025006a76cSDavid Howells .stats = &nfs_rpcstat, 1035006a76cSDavid Howells .pipe_dir_name = "/nfs", 1045006a76cSDavid Howells }; 1055006a76cSDavid Howells 1065006a76cSDavid Howells struct rpc_stat nfs_rpcstat = { 1075006a76cSDavid Howells .program = &nfs_program 1085006a76cSDavid Howells }; 1095006a76cSDavid Howells 1105006a76cSDavid Howells 1115006a76cSDavid Howells #ifdef CONFIG_NFS_V3_ACL 1125006a76cSDavid Howells static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program }; 1135006a76cSDavid Howells static struct rpc_version * nfsacl_version[] = { 1145006a76cSDavid Howells [3] = &nfsacl_version3, 1155006a76cSDavid Howells }; 1165006a76cSDavid Howells 1175006a76cSDavid Howells struct rpc_program nfsacl_program = { 1185006a76cSDavid Howells .name = "nfsacl", 1195006a76cSDavid Howells .number = NFS_ACL_PROGRAM, 1205006a76cSDavid Howells .nrvers = ARRAY_SIZE(nfsacl_version), 1215006a76cSDavid Howells .version = nfsacl_version, 1225006a76cSDavid Howells .stats = &nfsacl_rpcstat, 1235006a76cSDavid Howells }; 1245006a76cSDavid Howells #endif /* CONFIG_NFS_V3_ACL */ 1255006a76cSDavid Howells 1263a498026STrond Myklebust struct nfs_client_initdata { 1273a498026STrond Myklebust const char *hostname; 128d7422c47SChuck Lever const struct sockaddr *addr; 1296e4cffd7SChuck Lever size_t addrlen; 13040c55319STrond Myklebust const struct nfs_rpc_ops *rpc_ops; 13159dca3b2STrond Myklebust int proto; 1325aae4a9aSBenny Halevy u32 minorversion; 1333a498026STrond Myklebust }; 1343a498026STrond Myklebust 1355006a76cSDavid Howells /* 13624c8dbbbSDavid Howells * Allocate a shared client record 13724c8dbbbSDavid Howells * 13824c8dbbbSDavid Howells * Since these are allocated/deallocated very rarely, we don't 13924c8dbbbSDavid Howells * bother putting them in a slab cache... 14024c8dbbbSDavid Howells */ 1413a498026STrond Myklebust static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) 14224c8dbbbSDavid Howells { 14324c8dbbbSDavid Howells struct nfs_client *clp; 1447c67db3aSTrond Myklebust struct rpc_cred *cred; 145a21bdd9bSChuck Lever int err = -ENOMEM; 14624c8dbbbSDavid Howells 14724c8dbbbSDavid Howells if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) 14824c8dbbbSDavid Howells goto error_0; 14924c8dbbbSDavid Howells 15040c55319STrond Myklebust clp->rpc_ops = cl_init->rpc_ops; 15140c55319STrond Myklebust 15224c8dbbbSDavid Howells atomic_set(&clp->cl_count, 1); 15324c8dbbbSDavid Howells clp->cl_cons_state = NFS_CS_INITING; 15424c8dbbbSDavid Howells 1556e4cffd7SChuck Lever memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); 1566e4cffd7SChuck Lever clp->cl_addrlen = cl_init->addrlen; 15724c8dbbbSDavid Howells 1583a498026STrond Myklebust if (cl_init->hostname) { 159a21bdd9bSChuck Lever err = -ENOMEM; 1603a498026STrond Myklebust clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); 16124c8dbbbSDavid Howells if (!clp->cl_hostname) 16271468513SBenny Halevy goto error_cleanup; 16324c8dbbbSDavid Howells } 16424c8dbbbSDavid Howells 16524c8dbbbSDavid Howells INIT_LIST_HEAD(&clp->cl_superblocks); 16624c8dbbbSDavid Howells clp->cl_rpcclient = ERR_PTR(-EINVAL); 16724c8dbbbSDavid Howells 16859dca3b2STrond Myklebust clp->cl_proto = cl_init->proto; 16959dca3b2STrond Myklebust 17024c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4 171f4eecd5dSAndy Adamson err = nfs_get_cb_ident_idr(clp, cl_init->minorversion); 172f4eecd5dSAndy Adamson if (err) 173f4eecd5dSAndy Adamson goto error_cleanup; 174f4eecd5dSAndy Adamson 17524c8dbbbSDavid Howells INIT_LIST_HEAD(&clp->cl_delegations); 17624c8dbbbSDavid Howells spin_lock_init(&clp->cl_lock); 17765f27f38SDavid Howells INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); 17824c8dbbbSDavid Howells rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); 17924c8dbbbSDavid Howells clp->cl_boot_time = CURRENT_TIME; 18024c8dbbbSDavid Howells clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; 1815aae4a9aSBenny Halevy clp->cl_minorversion = cl_init->minorversion; 18297dc1359STrond Myklebust clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion]; 18324c8dbbbSDavid Howells #endif 1847c67db3aSTrond Myklebust cred = rpc_lookup_machine_cred(); 1857c67db3aSTrond Myklebust if (!IS_ERR(cred)) 1867c67db3aSTrond Myklebust clp->cl_machine_cred = cred; 187974cec8cSAndy Adamson #if defined(CONFIG_NFS_V4_1) 188974cec8cSAndy Adamson INIT_LIST_HEAD(&clp->cl_layouts); 189974cec8cSAndy Adamson #endif 19014727281SDavid Howells nfs_fscache_get_client_cookie(clp); 19114727281SDavid Howells 19224c8dbbbSDavid Howells return clp; 19324c8dbbbSDavid Howells 19471468513SBenny Halevy error_cleanup: 19524c8dbbbSDavid Howells kfree(clp); 19624c8dbbbSDavid Howells error_0: 197a21bdd9bSChuck Lever return ERR_PTR(err); 19824c8dbbbSDavid Howells } 19924c8dbbbSDavid Howells 20024c8dbbbSDavid Howells #ifdef CONFIG_NFS_V4 201557134a3SAndy Adamson #ifdef CONFIG_NFS_V4_1 202ea005281SAndy Adamson static void nfs4_shutdown_session(struct nfs_client *clp) 203ea005281SAndy Adamson { 204ea005281SAndy Adamson if (nfs4_has_session(clp)) 205557134a3SAndy Adamson nfs4_destroy_session(clp->cl_session); 206557134a3SAndy Adamson } 207ea005281SAndy Adamson #else /* CONFIG_NFS_V4_1 */ 208ea005281SAndy Adamson static void nfs4_shutdown_session(struct nfs_client *clp) 209ea005281SAndy Adamson { 210ea005281SAndy Adamson } 211557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 212557134a3SAndy Adamson 213557134a3SAndy Adamson /* 214888ef2e3SAlexandros Batsakis * Destroy the NFS4 callback service 215888ef2e3SAlexandros Batsakis */ 216888ef2e3SAlexandros Batsakis static void nfs4_destroy_callback(struct nfs_client *clp) 217888ef2e3SAlexandros Batsakis { 218888ef2e3SAlexandros Batsakis if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) 21997dc1359STrond Myklebust nfs_callback_down(clp->cl_mvops->minor_version); 220888ef2e3SAlexandros Batsakis } 221888ef2e3SAlexandros Batsakis 222888ef2e3SAlexandros Batsakis static void nfs4_shutdown_client(struct nfs_client *clp) 223888ef2e3SAlexandros Batsakis { 224888ef2e3SAlexandros Batsakis if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) 225888ef2e3SAlexandros Batsakis nfs4_kill_renewd(clp); 226ea005281SAndy Adamson nfs4_shutdown_session(clp); 227888ef2e3SAlexandros Batsakis nfs4_destroy_callback(clp); 228888ef2e3SAlexandros Batsakis if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) 229888ef2e3SAlexandros Batsakis nfs_idmap_delete(clp); 230888ef2e3SAlexandros Batsakis 231888ef2e3SAlexandros Batsakis rpc_destroy_wait_queue(&clp->cl_rpcwaitq); 232888ef2e3SAlexandros Batsakis } 233f4eecd5dSAndy Adamson 234f4eecd5dSAndy Adamson /* idr_remove_all is not needed as all id's are removed by nfs_put_client */ 235f4eecd5dSAndy Adamson void nfs_cleanup_cb_ident_idr(void) 236f4eecd5dSAndy Adamson { 237f4eecd5dSAndy Adamson idr_destroy(&cb_ident_idr); 238f4eecd5dSAndy Adamson } 239f4eecd5dSAndy Adamson 240f4eecd5dSAndy Adamson /* nfs_client_lock held */ 241f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 242f4eecd5dSAndy Adamson { 243f4eecd5dSAndy Adamson if (clp->cl_cb_ident) 244f4eecd5dSAndy Adamson idr_remove(&cb_ident_idr, clp->cl_cb_ident); 245f4eecd5dSAndy Adamson } 246f4eecd5dSAndy Adamson 247*f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 248*f7e8917aSFred Isaman { 249*f7e8917aSFred Isaman rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 250*f7e8917aSFred Isaman } 251*f7e8917aSFred Isaman 252888ef2e3SAlexandros Batsakis #else 253888ef2e3SAlexandros Batsakis static void nfs4_shutdown_client(struct nfs_client *clp) 254888ef2e3SAlexandros Batsakis { 255888ef2e3SAlexandros Batsakis } 256f4eecd5dSAndy Adamson 257f4eecd5dSAndy Adamson void nfs_cleanup_cb_ident_idr(void) 258f4eecd5dSAndy Adamson { 259f4eecd5dSAndy Adamson } 260f4eecd5dSAndy Adamson 261f4eecd5dSAndy Adamson static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 262f4eecd5dSAndy Adamson { 263f4eecd5dSAndy Adamson } 264*f7e8917aSFred Isaman 265*f7e8917aSFred Isaman static void pnfs_init_server(struct nfs_server *server) 266*f7e8917aSFred Isaman { 267*f7e8917aSFred Isaman } 268*f7e8917aSFred Isaman 269888ef2e3SAlexandros Batsakis #endif /* CONFIG_NFS_V4 */ 270888ef2e3SAlexandros Batsakis 271888ef2e3SAlexandros Batsakis /* 2725dd3177aSTrond Myklebust * Destroy a shared client record 2735dd3177aSTrond Myklebust */ 2745dd3177aSTrond Myklebust static void nfs_free_client(struct nfs_client *clp) 2755dd3177aSTrond Myklebust { 27640c55319STrond Myklebust dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version); 2775dd3177aSTrond Myklebust 2785dd3177aSTrond Myklebust nfs4_shutdown_client(clp); 27924c8dbbbSDavid Howells 28014727281SDavid Howells nfs_fscache_release_client_cookie(clp); 28114727281SDavid Howells 28224c8dbbbSDavid Howells /* -EIO all pending I/O */ 28324c8dbbbSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 28424c8dbbbSDavid Howells rpc_shutdown_client(clp->cl_rpcclient); 28524c8dbbbSDavid Howells 2867c67db3aSTrond Myklebust if (clp->cl_machine_cred != NULL) 2877c67db3aSTrond Myklebust put_rpccred(clp->cl_machine_cred); 2887c67db3aSTrond Myklebust 28924c8dbbbSDavid Howells kfree(clp->cl_hostname); 29024c8dbbbSDavid Howells kfree(clp); 29124c8dbbbSDavid Howells 29224c8dbbbSDavid Howells dprintk("<-- nfs_free_client()\n"); 29324c8dbbbSDavid Howells } 29424c8dbbbSDavid Howells 29524c8dbbbSDavid Howells /* 29624c8dbbbSDavid Howells * Release a reference to a shared client record 29724c8dbbbSDavid Howells */ 29824c8dbbbSDavid Howells void nfs_put_client(struct nfs_client *clp) 29924c8dbbbSDavid Howells { 30027ba8512SDavid Howells if (!clp) 30127ba8512SDavid Howells return; 30227ba8512SDavid Howells 30324c8dbbbSDavid Howells dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count)); 30424c8dbbbSDavid Howells 30524c8dbbbSDavid Howells if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) { 30624c8dbbbSDavid Howells list_del(&clp->cl_share_link); 307f4eecd5dSAndy Adamson nfs_cb_idr_remove_locked(clp); 30824c8dbbbSDavid Howells spin_unlock(&nfs_client_lock); 30924c8dbbbSDavid Howells 31024c8dbbbSDavid Howells BUG_ON(!list_empty(&clp->cl_superblocks)); 31124c8dbbbSDavid Howells 31224c8dbbbSDavid Howells nfs_free_client(clp); 31324c8dbbbSDavid Howells } 31424c8dbbbSDavid Howells } 31516b374caSAndy Adamson EXPORT_SYMBOL_GPL(nfs_put_client); 31624c8dbbbSDavid Howells 3179082a5ccSTrond Myklebust #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 3189f4c899cSTrond Myklebust /* 3199f4c899cSTrond Myklebust * Test if two ip6 socket addresses refer to the same socket by 3209f4c899cSTrond Myklebust * comparing relevant fields. The padding bytes specifically, are not 3219f4c899cSTrond Myklebust * compared. sin6_flowinfo is not compared because it only affects QoS 3229f4c899cSTrond Myklebust * and sin6_scope_id is only compared if the address is "link local" 3239f4c899cSTrond Myklebust * because "link local" addresses need only be unique to a specific 3249f4c899cSTrond Myklebust * link. Conversely, ordinary unicast addresses might have different 3259f4c899cSTrond Myklebust * sin6_scope_id. 3269f4c899cSTrond Myklebust * 3279f4c899cSTrond Myklebust * The caller should ensure both socket addresses are AF_INET6. 3289f4c899cSTrond Myklebust */ 3293c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, 3309f4c899cSTrond Myklebust const struct sockaddr *sa2) 3319f4c899cSTrond Myklebust { 3323c8c45dfSChuck Lever const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; 3333c8c45dfSChuck Lever const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; 3349f4c899cSTrond Myklebust 3353c8c45dfSChuck Lever if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL && 3363c8c45dfSChuck Lever sin1->sin6_scope_id != sin2->sin6_scope_id) 3379f4c899cSTrond Myklebust return 0; 3383b0d3f93STrond Myklebust 339b20d37caSTrond Myklebust return ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr); 3403b0d3f93STrond Myklebust } 3413c8c45dfSChuck Lever #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */ 3423c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, 3439f4c899cSTrond Myklebust const struct sockaddr *sa2) 3449f4c899cSTrond Myklebust { 3459f4c899cSTrond Myklebust return 0; 3469f4c899cSTrond Myklebust } 3479082a5ccSTrond Myklebust #endif 3483b0d3f93STrond Myklebust 34924c8dbbbSDavid Howells /* 350d7371c41SIan Dall * Test if two ip4 socket addresses refer to the same socket, by 351d7371c41SIan Dall * comparing relevant fields. The padding bytes specifically, are 352d7371c41SIan Dall * not compared. 353d7371c41SIan Dall * 354d7371c41SIan Dall * The caller should ensure both socket addresses are AF_INET. 355d7371c41SIan Dall */ 3563c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1, 3573c8c45dfSChuck Lever const struct sockaddr *sa2) 3583c8c45dfSChuck Lever { 3593c8c45dfSChuck Lever const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; 3603c8c45dfSChuck Lever const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; 3613c8c45dfSChuck Lever 3623c8c45dfSChuck Lever return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr; 3633c8c45dfSChuck Lever } 3643c8c45dfSChuck Lever 3653c8c45dfSChuck Lever static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1, 3663c8c45dfSChuck Lever const struct sockaddr *sa2) 3673c8c45dfSChuck Lever { 3683c8c45dfSChuck Lever const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; 3693c8c45dfSChuck Lever const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; 3703c8c45dfSChuck Lever 3713c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr6(sa1, sa2) && 3723c8c45dfSChuck Lever (sin1->sin6_port == sin2->sin6_port); 3733c8c45dfSChuck Lever } 3743c8c45dfSChuck Lever 3759f4c899cSTrond Myklebust static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1, 3769f4c899cSTrond Myklebust const struct sockaddr *sa2) 377d7371c41SIan Dall { 3783c8c45dfSChuck Lever const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; 3793c8c45dfSChuck Lever const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; 3809f4c899cSTrond Myklebust 3813c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr4(sa1, sa2) && 3823c8c45dfSChuck Lever (sin1->sin_port == sin2->sin_port); 383d7371c41SIan Dall } 384d7371c41SIan Dall 385d7371c41SIan Dall /* 386d7371c41SIan Dall * Test if two socket addresses represent the same actual socket, 3873c8c45dfSChuck Lever * by comparing (only) relevant fields, excluding the port number. 3883c8c45dfSChuck Lever */ 3893c8c45dfSChuck Lever static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1, 3903c8c45dfSChuck Lever const struct sockaddr *sa2) 3913c8c45dfSChuck Lever { 3923c8c45dfSChuck Lever if (sa1->sa_family != sa2->sa_family) 3933c8c45dfSChuck Lever return 0; 3943c8c45dfSChuck Lever 3953c8c45dfSChuck Lever switch (sa1->sa_family) { 3963c8c45dfSChuck Lever case AF_INET: 3973c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr4(sa1, sa2); 3983c8c45dfSChuck Lever case AF_INET6: 3993c8c45dfSChuck Lever return nfs_sockaddr_match_ipaddr6(sa1, sa2); 4003c8c45dfSChuck Lever } 4013c8c45dfSChuck Lever return 0; 4023c8c45dfSChuck Lever } 4033c8c45dfSChuck Lever 4043c8c45dfSChuck Lever /* 4053c8c45dfSChuck Lever * Test if two socket addresses represent the same actual socket, 4063c8c45dfSChuck Lever * by comparing (only) relevant fields, including the port number. 407d7371c41SIan Dall */ 408d7371c41SIan Dall static int nfs_sockaddr_cmp(const struct sockaddr *sa1, 409d7371c41SIan Dall const struct sockaddr *sa2) 410d7371c41SIan Dall { 411d7371c41SIan Dall if (sa1->sa_family != sa2->sa_family) 412d7371c41SIan Dall return 0; 413d7371c41SIan Dall 414d7371c41SIan Dall switch (sa1->sa_family) { 415d7371c41SIan Dall case AF_INET: 4169f4c899cSTrond Myklebust return nfs_sockaddr_cmp_ip4(sa1, sa2); 417d7371c41SIan Dall case AF_INET6: 4189f4c899cSTrond Myklebust return nfs_sockaddr_cmp_ip6(sa1, sa2); 419d7371c41SIan Dall } 420d7371c41SIan Dall return 0; 421d7371c41SIan Dall } 422d7371c41SIan Dall 423c36fca52SAndy Adamson /* Common match routine for v4.0 and v4.1 callback services */ 424c36fca52SAndy Adamson bool 425c36fca52SAndy Adamson nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp, 426c36fca52SAndy Adamson u32 minorversion) 427c81468a1STrond Myklebust { 4283b0d3f93STrond Myklebust struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 4293b0d3f93STrond Myklebust 430c36fca52SAndy Adamson /* Don't match clients that failed to initialise */ 43176db6d95SAndy Adamson if (!(clp->cl_cons_state == NFS_CS_READY || 43276db6d95SAndy Adamson clp->cl_cons_state == NFS_CS_SESSION_INITING)) 433c36fca52SAndy Adamson return false; 434c81468a1STrond Myklebust 435c36fca52SAndy Adamson /* Match the version and minorversion */ 436c36fca52SAndy Adamson if (clp->rpc_ops->version != 4 || 437c36fca52SAndy Adamson clp->cl_minorversion != minorversion) 438c36fca52SAndy Adamson return false; 439c81468a1STrond Myklebust 440c81468a1STrond Myklebust /* Match only the IP address, not the port number */ 4413b0d3f93STrond Myklebust if (!nfs_sockaddr_match_ipaddr(addr, clap)) 442c36fca52SAndy Adamson return false; 443c81468a1STrond Myklebust 444c36fca52SAndy Adamson return true; 4453fbd67adSTrond Myklebust } 4463fbd67adSTrond Myklebust 4473fbd67adSTrond Myklebust /* 448c81468a1STrond Myklebust * Find an nfs_client on the list that matches the initialisation data 449c81468a1STrond Myklebust * that is supplied. 450c81468a1STrond Myklebust */ 451c81468a1STrond Myklebust static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) 45224c8dbbbSDavid Howells { 45324c8dbbbSDavid Howells struct nfs_client *clp; 454d7371c41SIan Dall const struct sockaddr *sap = data->addr; 45524c8dbbbSDavid Howells 45624c8dbbbSDavid Howells list_for_each_entry(clp, &nfs_client_list, cl_share_link) { 457d7371c41SIan Dall const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 45813bbc06aSTrond Myklebust /* Don't match clients that failed to initialise properly */ 45913bbc06aSTrond Myklebust if (clp->cl_cons_state < 0) 46013bbc06aSTrond Myklebust continue; 46113bbc06aSTrond Myklebust 46224c8dbbbSDavid Howells /* Different NFS versions cannot share the same nfs_client */ 46340c55319STrond Myklebust if (clp->rpc_ops != data->rpc_ops) 46424c8dbbbSDavid Howells continue; 46524c8dbbbSDavid Howells 46659dca3b2STrond Myklebust if (clp->cl_proto != data->proto) 46759dca3b2STrond Myklebust continue; 4685aae4a9aSBenny Halevy /* Match nfsv4 minorversion */ 4695aae4a9aSBenny Halevy if (clp->cl_minorversion != data->minorversion) 4705aae4a9aSBenny Halevy continue; 471c81468a1STrond Myklebust /* Match the full socket address */ 472d7371c41SIan Dall if (!nfs_sockaddr_cmp(sap, clap)) 47324c8dbbbSDavid Howells continue; 47424c8dbbbSDavid Howells 47524c8dbbbSDavid Howells atomic_inc(&clp->cl_count); 47624c8dbbbSDavid Howells return clp; 47724c8dbbbSDavid Howells } 478c81468a1STrond Myklebust return NULL; 47924c8dbbbSDavid Howells } 48024c8dbbbSDavid Howells 48124c8dbbbSDavid Howells /* 48224c8dbbbSDavid Howells * Look up a client by IP address and protocol version 48324c8dbbbSDavid Howells * - creates a new record if one doesn't yet exist 48424c8dbbbSDavid Howells */ 4853a498026STrond Myklebust static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) 48624c8dbbbSDavid Howells { 48724c8dbbbSDavid Howells struct nfs_client *clp, *new = NULL; 48824c8dbbbSDavid Howells int error; 48924c8dbbbSDavid Howells 490d7422c47SChuck Lever dprintk("--> nfs_get_client(%s,v%u)\n", 491d7422c47SChuck Lever cl_init->hostname ?: "", cl_init->rpc_ops->version); 49224c8dbbbSDavid Howells 49324c8dbbbSDavid Howells /* see if the client already exists */ 49424c8dbbbSDavid Howells do { 49524c8dbbbSDavid Howells spin_lock(&nfs_client_lock); 49624c8dbbbSDavid Howells 497c81468a1STrond Myklebust clp = nfs_match_client(cl_init); 49824c8dbbbSDavid Howells if (clp) 49924c8dbbbSDavid Howells goto found_client; 50024c8dbbbSDavid Howells if (new) 50124c8dbbbSDavid Howells goto install_client; 50224c8dbbbSDavid Howells 50324c8dbbbSDavid Howells spin_unlock(&nfs_client_lock); 50424c8dbbbSDavid Howells 5053a498026STrond Myklebust new = nfs_alloc_client(cl_init); 506a21bdd9bSChuck Lever } while (!IS_ERR(new)); 50724c8dbbbSDavid Howells 508a21bdd9bSChuck Lever dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new)); 509a21bdd9bSChuck Lever return new; 51024c8dbbbSDavid Howells 51124c8dbbbSDavid Howells /* install a new client and return with it unready */ 51224c8dbbbSDavid Howells install_client: 51324c8dbbbSDavid Howells clp = new; 51424c8dbbbSDavid Howells list_add(&clp->cl_share_link, &nfs_client_list); 51524c8dbbbSDavid Howells spin_unlock(&nfs_client_lock); 51624c8dbbbSDavid Howells dprintk("--> nfs_get_client() = %p [new]\n", clp); 51724c8dbbbSDavid Howells return clp; 51824c8dbbbSDavid Howells 51924c8dbbbSDavid Howells /* found an existing client 52024c8dbbbSDavid Howells * - make sure it's ready before returning 52124c8dbbbSDavid Howells */ 52224c8dbbbSDavid Howells found_client: 52324c8dbbbSDavid Howells spin_unlock(&nfs_client_lock); 52424c8dbbbSDavid Howells 52524c8dbbbSDavid Howells if (new) 52624c8dbbbSDavid Howells nfs_free_client(new); 52724c8dbbbSDavid Howells 528150030b7SMatthew Wilcox error = wait_event_killable(nfs_client_active_wq, 52976db6d95SAndy Adamson clp->cl_cons_state < NFS_CS_INITING); 5300bae89ecSTrond Myklebust if (error < 0) { 53124c8dbbbSDavid Howells nfs_put_client(clp); 53224c8dbbbSDavid Howells return ERR_PTR(-ERESTARTSYS); 53324c8dbbbSDavid Howells } 53424c8dbbbSDavid Howells 53524c8dbbbSDavid Howells if (clp->cl_cons_state < NFS_CS_READY) { 53624c8dbbbSDavid Howells error = clp->cl_cons_state; 53724c8dbbbSDavid Howells nfs_put_client(clp); 53824c8dbbbSDavid Howells return ERR_PTR(error); 53924c8dbbbSDavid Howells } 54024c8dbbbSDavid Howells 54154ceac45SDavid Howells BUG_ON(clp->cl_cons_state != NFS_CS_READY); 54254ceac45SDavid Howells 54324c8dbbbSDavid Howells dprintk("--> nfs_get_client() = %p [share]\n", clp); 54424c8dbbbSDavid Howells return clp; 54524c8dbbbSDavid Howells } 54624c8dbbbSDavid Howells 54724c8dbbbSDavid Howells /* 54824c8dbbbSDavid Howells * Mark a server as ready or failed 54924c8dbbbSDavid Howells */ 55076db6d95SAndy Adamson void nfs_mark_client_ready(struct nfs_client *clp, int state) 55124c8dbbbSDavid Howells { 55224c8dbbbSDavid Howells clp->cl_cons_state = state; 55324c8dbbbSDavid Howells wake_up_all(&nfs_client_active_wq); 55424c8dbbbSDavid Howells } 5555006a76cSDavid Howells 5565006a76cSDavid Howells /* 557008f55d0SBenny Halevy * With sessions, the client is not marked ready until after a 558008f55d0SBenny Halevy * successful EXCHANGE_ID and CREATE_SESSION. 559008f55d0SBenny Halevy * 560008f55d0SBenny Halevy * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate 561008f55d0SBenny Halevy * other versions of NFS can be tried. 562008f55d0SBenny Halevy */ 563008f55d0SBenny Halevy int nfs4_check_client_ready(struct nfs_client *clp) 564008f55d0SBenny Halevy { 565008f55d0SBenny Halevy if (!nfs4_has_session(clp)) 566008f55d0SBenny Halevy return 0; 567008f55d0SBenny Halevy if (clp->cl_cons_state < NFS_CS_READY) 568008f55d0SBenny Halevy return -EPROTONOSUPPORT; 569008f55d0SBenny Halevy return 0; 570008f55d0SBenny Halevy } 571008f55d0SBenny Halevy 572008f55d0SBenny Halevy /* 5735006a76cSDavid Howells * Initialise the timeout values for a connection 5745006a76cSDavid Howells */ 5755006a76cSDavid Howells static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 5765006a76cSDavid Howells unsigned int timeo, unsigned int retrans) 5775006a76cSDavid Howells { 5785006a76cSDavid Howells to->to_initval = timeo * HZ / 10; 5795006a76cSDavid Howells to->to_retries = retrans; 5805006a76cSDavid Howells 5815006a76cSDavid Howells switch (proto) { 5820896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_TCP: 5832cf7ff7aS\"Talpey, Thomas\ case XPRT_TRANSPORT_RDMA: 584259875efSTrond Myklebust if (to->to_retries == 0) 585259875efSTrond Myklebust to->to_retries = NFS_DEF_TCP_RETRANS; 5867a3e3e18STrond Myklebust if (to->to_initval == 0) 587259875efSTrond Myklebust to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; 5885006a76cSDavid Howells if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 5895006a76cSDavid Howells to->to_initval = NFS_MAX_TCP_TIMEOUT; 5905006a76cSDavid Howells to->to_increment = to->to_initval; 5915006a76cSDavid Howells to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); 5927a3e3e18STrond Myklebust if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) 5937a3e3e18STrond Myklebust to->to_maxval = NFS_MAX_TCP_TIMEOUT; 5947a3e3e18STrond Myklebust if (to->to_maxval < to->to_initval) 5957a3e3e18STrond Myklebust to->to_maxval = to->to_initval; 5965006a76cSDavid Howells to->to_exponential = 0; 5975006a76cSDavid Howells break; 5980896a725S\"Talpey, Thomas\ case XPRT_TRANSPORT_UDP: 599259875efSTrond Myklebust if (to->to_retries == 0) 600259875efSTrond Myklebust to->to_retries = NFS_DEF_UDP_RETRANS; 6015006a76cSDavid Howells if (!to->to_initval) 602259875efSTrond Myklebust to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; 6035006a76cSDavid Howells if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 6045006a76cSDavid Howells to->to_initval = NFS_MAX_UDP_TIMEOUT; 6055006a76cSDavid Howells to->to_maxval = NFS_MAX_UDP_TIMEOUT; 6065006a76cSDavid Howells to->to_exponential = 1; 6075006a76cSDavid Howells break; 608259875efSTrond Myklebust default: 609259875efSTrond Myklebust BUG(); 6105006a76cSDavid Howells } 6115006a76cSDavid Howells } 6125006a76cSDavid Howells 6135006a76cSDavid Howells /* 6145006a76cSDavid Howells * Create an RPC client handle 6155006a76cSDavid Howells */ 61659dca3b2STrond Myklebust static int nfs_create_rpc_client(struct nfs_client *clp, 61733170233STrond Myklebust const struct rpc_timeout *timeparms, 61843d78ef2SChuck Lever rpc_authflavor_t flavor, 6194a01b8a4SChuck Lever int discrtry, int noresvport) 6205006a76cSDavid Howells { 6215006a76cSDavid Howells struct rpc_clnt *clnt = NULL; 62241877d20SChuck Lever struct rpc_create_args args = { 623c653ce3fSPavel Emelyanov .net = &init_net, 62459dca3b2STrond Myklebust .protocol = clp->cl_proto, 62541877d20SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 6266e4cffd7SChuck Lever .addrsize = clp->cl_addrlen, 62733170233STrond Myklebust .timeout = timeparms, 62841877d20SChuck Lever .servername = clp->cl_hostname, 62941877d20SChuck Lever .program = &nfs_program, 63041877d20SChuck Lever .version = clp->rpc_ops->version, 63141877d20SChuck Lever .authflavor = flavor, 63241877d20SChuck Lever }; 6335006a76cSDavid Howells 6344a01b8a4SChuck Lever if (discrtry) 6354a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_DISCRTRY; 6364a01b8a4SChuck Lever if (noresvport) 6374a01b8a4SChuck Lever args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; 6384a01b8a4SChuck Lever 6395006a76cSDavid Howells if (!IS_ERR(clp->cl_rpcclient)) 6405006a76cSDavid Howells return 0; 6415006a76cSDavid Howells 64241877d20SChuck Lever clnt = rpc_create(&args); 6435006a76cSDavid Howells if (IS_ERR(clnt)) { 6445006a76cSDavid Howells dprintk("%s: cannot create RPC client. Error = %ld\n", 6453110ff80SHarvey Harrison __func__, PTR_ERR(clnt)); 6465006a76cSDavid Howells return PTR_ERR(clnt); 6475006a76cSDavid Howells } 6485006a76cSDavid Howells 6495006a76cSDavid Howells clp->cl_rpcclient = clnt; 6505006a76cSDavid Howells return 0; 6515006a76cSDavid Howells } 65254ceac45SDavid Howells 65354ceac45SDavid Howells /* 65454ceac45SDavid Howells * Version 2 or 3 client destruction 65554ceac45SDavid Howells */ 65654ceac45SDavid Howells static void nfs_destroy_server(struct nfs_server *server) 65754ceac45SDavid Howells { 6585eebde23SSuresh Jayaraman if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) || 6595eebde23SSuresh Jayaraman !(server->flags & NFS_MOUNT_LOCAL_FCNTL)) 6609289e7f9SChuck Lever nlmclnt_done(server->nlm_host); 66154ceac45SDavid Howells } 66254ceac45SDavid Howells 66354ceac45SDavid Howells /* 66454ceac45SDavid Howells * Version 2 or 3 lockd setup 66554ceac45SDavid Howells */ 66654ceac45SDavid Howells static int nfs_start_lockd(struct nfs_server *server) 66754ceac45SDavid Howells { 6689289e7f9SChuck Lever struct nlm_host *host; 6699289e7f9SChuck Lever struct nfs_client *clp = server->nfs_client; 670883bb163SChuck Lever struct nlmclnt_initdata nlm_init = { 671883bb163SChuck Lever .hostname = clp->cl_hostname, 672883bb163SChuck Lever .address = (struct sockaddr *)&clp->cl_addr, 673883bb163SChuck Lever .addrlen = clp->cl_addrlen, 674883bb163SChuck Lever .nfs_version = clp->rpc_ops->version, 6750cb2659bSChuck Lever .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? 6760cb2659bSChuck Lever 1 : 0, 677883bb163SChuck Lever }; 67854ceac45SDavid Howells 679883bb163SChuck Lever if (nlm_init.nfs_version > 3) 6809289e7f9SChuck Lever return 0; 6815eebde23SSuresh Jayaraman if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) && 6825eebde23SSuresh Jayaraman (server->flags & NFS_MOUNT_LOCAL_FCNTL)) 6839289e7f9SChuck Lever return 0; 6849289e7f9SChuck Lever 6858a6e5debSTrond Myklebust switch (clp->cl_proto) { 6868a6e5debSTrond Myklebust default: 6878a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_TCP; 6888a6e5debSTrond Myklebust break; 6898a6e5debSTrond Myklebust case XPRT_TRANSPORT_UDP: 6908a6e5debSTrond Myklebust nlm_init.protocol = IPPROTO_UDP; 6918a6e5debSTrond Myklebust } 6928a6e5debSTrond Myklebust 693883bb163SChuck Lever host = nlmclnt_init(&nlm_init); 6949289e7f9SChuck Lever if (IS_ERR(host)) 6959289e7f9SChuck Lever return PTR_ERR(host); 6969289e7f9SChuck Lever 6979289e7f9SChuck Lever server->nlm_host = host; 69854ceac45SDavid Howells server->destroy = nfs_destroy_server; 6999289e7f9SChuck Lever return 0; 70054ceac45SDavid Howells } 70154ceac45SDavid Howells 70254ceac45SDavid Howells /* 70354ceac45SDavid Howells * Initialise an NFSv3 ACL client connection 70454ceac45SDavid Howells */ 70554ceac45SDavid Howells #ifdef CONFIG_NFS_V3_ACL 70654ceac45SDavid Howells static void nfs_init_server_aclclient(struct nfs_server *server) 70754ceac45SDavid Howells { 70840c55319STrond Myklebust if (server->nfs_client->rpc_ops->version != 3) 70954ceac45SDavid Howells goto out_noacl; 71054ceac45SDavid Howells if (server->flags & NFS_MOUNT_NOACL) 71154ceac45SDavid Howells goto out_noacl; 71254ceac45SDavid Howells 71354ceac45SDavid Howells server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3); 71454ceac45SDavid Howells if (IS_ERR(server->client_acl)) 71554ceac45SDavid Howells goto out_noacl; 71654ceac45SDavid Howells 71754ceac45SDavid Howells /* No errors! Assume that Sun nfsacls are supported */ 71854ceac45SDavid Howells server->caps |= NFS_CAP_ACLS; 71954ceac45SDavid Howells return; 72054ceac45SDavid Howells 72154ceac45SDavid Howells out_noacl: 72254ceac45SDavid Howells server->caps &= ~NFS_CAP_ACLS; 72354ceac45SDavid Howells } 72454ceac45SDavid Howells #else 72554ceac45SDavid Howells static inline void nfs_init_server_aclclient(struct nfs_server *server) 72654ceac45SDavid Howells { 72754ceac45SDavid Howells server->flags &= ~NFS_MOUNT_NOACL; 72854ceac45SDavid Howells server->caps &= ~NFS_CAP_ACLS; 72954ceac45SDavid Howells } 73054ceac45SDavid Howells #endif 73154ceac45SDavid Howells 73254ceac45SDavid Howells /* 73354ceac45SDavid Howells * Create a general RPC client 73454ceac45SDavid Howells */ 73533170233STrond Myklebust static int nfs_init_server_rpcclient(struct nfs_server *server, 73633170233STrond Myklebust const struct rpc_timeout *timeo, 73733170233STrond Myklebust rpc_authflavor_t pseudoflavour) 73854ceac45SDavid Howells { 73954ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 74054ceac45SDavid Howells 74154ceac45SDavid Howells server->client = rpc_clone_client(clp->cl_rpcclient); 74254ceac45SDavid Howells if (IS_ERR(server->client)) { 7433110ff80SHarvey Harrison dprintk("%s: couldn't create rpc_client!\n", __func__); 74454ceac45SDavid Howells return PTR_ERR(server->client); 74554ceac45SDavid Howells } 74654ceac45SDavid Howells 74733170233STrond Myklebust memcpy(&server->client->cl_timeout_default, 74833170233STrond Myklebust timeo, 74933170233STrond Myklebust sizeof(server->client->cl_timeout_default)); 75033170233STrond Myklebust server->client->cl_timeout = &server->client->cl_timeout_default; 75133170233STrond Myklebust 75254ceac45SDavid Howells if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) { 75354ceac45SDavid Howells struct rpc_auth *auth; 75454ceac45SDavid Howells 75554ceac45SDavid Howells auth = rpcauth_create(pseudoflavour, server->client); 75654ceac45SDavid Howells if (IS_ERR(auth)) { 7573110ff80SHarvey Harrison dprintk("%s: couldn't create credcache!\n", __func__); 75854ceac45SDavid Howells return PTR_ERR(auth); 75954ceac45SDavid Howells } 76054ceac45SDavid Howells } 76154ceac45SDavid Howells server->client->cl_softrtry = 0; 76254ceac45SDavid Howells if (server->flags & NFS_MOUNT_SOFT) 76354ceac45SDavid Howells server->client->cl_softrtry = 1; 76454ceac45SDavid Howells 76554ceac45SDavid Howells return 0; 76654ceac45SDavid Howells } 76754ceac45SDavid Howells 76854ceac45SDavid Howells /* 76954ceac45SDavid Howells * Initialise an NFS2 or NFS3 client 77054ceac45SDavid Howells */ 7712283f8d6S\"Talpey, Thomas\ static int nfs_init_client(struct nfs_client *clp, 77233170233STrond Myklebust const struct rpc_timeout *timeparms, 7732283f8d6S\"Talpey, Thomas\ const struct nfs_parsed_mount_data *data) 77454ceac45SDavid Howells { 77554ceac45SDavid Howells int error; 77654ceac45SDavid Howells 77754ceac45SDavid Howells if (clp->cl_cons_state == NFS_CS_READY) { 77854ceac45SDavid Howells /* the client is already initialised */ 77954ceac45SDavid Howells dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp); 78054ceac45SDavid Howells return 0; 78154ceac45SDavid Howells } 78254ceac45SDavid Howells 78354ceac45SDavid Howells /* 78454ceac45SDavid Howells * Create a client RPC handle for doing FSSTAT with UNIX auth only 78554ceac45SDavid Howells * - RFC 2623, sec 2.3.2 78654ceac45SDavid Howells */ 787d740351bSChuck Lever error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 788d740351bSChuck Lever 0, data->flags & NFS_MOUNT_NORESVPORT); 78954ceac45SDavid Howells if (error < 0) 79054ceac45SDavid Howells goto error; 79154ceac45SDavid Howells nfs_mark_client_ready(clp, NFS_CS_READY); 79254ceac45SDavid Howells return 0; 79354ceac45SDavid Howells 79454ceac45SDavid Howells error: 79554ceac45SDavid Howells nfs_mark_client_ready(clp, error); 79654ceac45SDavid Howells dprintk("<-- nfs_init_client() = xerror %d\n", error); 79754ceac45SDavid Howells return error; 79854ceac45SDavid Howells } 79954ceac45SDavid Howells 80054ceac45SDavid Howells /* 80154ceac45SDavid Howells * Create a version 2 or 3 client 80254ceac45SDavid Howells */ 8032283f8d6S\"Talpey, Thomas\ static int nfs_init_server(struct nfs_server *server, 8042283f8d6S\"Talpey, Thomas\ const struct nfs_parsed_mount_data *data) 80554ceac45SDavid Howells { 8063a498026STrond Myklebust struct nfs_client_initdata cl_init = { 8073a498026STrond Myklebust .hostname = data->nfs_server.hostname, 808d7422c47SChuck Lever .addr = (const struct sockaddr *)&data->nfs_server.address, 8094c568017SChuck Lever .addrlen = data->nfs_server.addrlen, 81040c55319STrond Myklebust .rpc_ops = &nfs_v2_clientops, 81159dca3b2STrond Myklebust .proto = data->nfs_server.protocol, 8123a498026STrond Myklebust }; 81333170233STrond Myklebust struct rpc_timeout timeparms; 81454ceac45SDavid Howells struct nfs_client *clp; 8153a498026STrond Myklebust int error; 81654ceac45SDavid Howells 81754ceac45SDavid Howells dprintk("--> nfs_init_server()\n"); 81854ceac45SDavid Howells 81954ceac45SDavid Howells #ifdef CONFIG_NFS_V3 8208a6e5debSTrond Myklebust if (data->version == 3) 82140c55319STrond Myklebust cl_init.rpc_ops = &nfs_v3_clientops; 82254ceac45SDavid Howells #endif 82354ceac45SDavid Howells 82454ceac45SDavid Howells /* Allocate or find a client reference we can use */ 8253a498026STrond Myklebust clp = nfs_get_client(&cl_init); 82654ceac45SDavid Howells if (IS_ERR(clp)) { 82754ceac45SDavid Howells dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp)); 82854ceac45SDavid Howells return PTR_ERR(clp); 82954ceac45SDavid Howells } 83054ceac45SDavid Howells 83133170233STrond Myklebust nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, 83233170233STrond Myklebust data->timeo, data->retrans); 83333170233STrond Myklebust error = nfs_init_client(clp, &timeparms, data); 83454ceac45SDavid Howells if (error < 0) 83554ceac45SDavid Howells goto error; 83654ceac45SDavid Howells 83754ceac45SDavid Howells server->nfs_client = clp; 83854ceac45SDavid Howells 83954ceac45SDavid Howells /* Initialise the client representation from the mount data */ 840ff3525a5STrond Myklebust server->flags = data->flags; 841b797cac7SDavid Howells server->options = data->options; 84262ab460cSTrond Myklebust server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID| 84362ab460cSTrond Myklebust NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP| 84462ab460cSTrond Myklebust NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME; 84554ceac45SDavid Howells 84654ceac45SDavid Howells if (data->rsize) 84754ceac45SDavid Howells server->rsize = nfs_block_size(data->rsize, NULL); 84854ceac45SDavid Howells if (data->wsize) 84954ceac45SDavid Howells server->wsize = nfs_block_size(data->wsize, NULL); 85054ceac45SDavid Howells 85154ceac45SDavid Howells server->acregmin = data->acregmin * HZ; 85254ceac45SDavid Howells server->acregmax = data->acregmax * HZ; 85354ceac45SDavid Howells server->acdirmin = data->acdirmin * HZ; 85454ceac45SDavid Howells server->acdirmax = data->acdirmax * HZ; 85554ceac45SDavid Howells 85654ceac45SDavid Howells /* Start lockd here, before we might error out */ 85754ceac45SDavid Howells error = nfs_start_lockd(server); 85854ceac45SDavid Howells if (error < 0) 85954ceac45SDavid Howells goto error; 86054ceac45SDavid Howells 861f22d6d79SChuck Lever server->port = data->nfs_server.port; 862f22d6d79SChuck Lever 86333170233STrond Myklebust error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); 86454ceac45SDavid Howells if (error < 0) 86554ceac45SDavid Howells goto error; 86654ceac45SDavid Howells 8673f8400d1SChuck Lever /* Preserve the values of mount_server-related mount options */ 8683f8400d1SChuck Lever if (data->mount_server.addrlen) { 8693f8400d1SChuck Lever memcpy(&server->mountd_address, &data->mount_server.address, 8703f8400d1SChuck Lever data->mount_server.addrlen); 8713f8400d1SChuck Lever server->mountd_addrlen = data->mount_server.addrlen; 8723f8400d1SChuck Lever } 8733f8400d1SChuck Lever server->mountd_version = data->mount_server.version; 8743f8400d1SChuck Lever server->mountd_port = data->mount_server.port; 8753f8400d1SChuck Lever server->mountd_protocol = data->mount_server.protocol; 8763f8400d1SChuck Lever 87754ceac45SDavid Howells server->namelen = data->namlen; 87854ceac45SDavid Howells /* Create a client RPC handle for the NFSv3 ACL management interface */ 87954ceac45SDavid Howells nfs_init_server_aclclient(server); 88054ceac45SDavid Howells dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp); 88154ceac45SDavid Howells return 0; 88254ceac45SDavid Howells 88354ceac45SDavid Howells error: 88454ceac45SDavid Howells server->nfs_client = NULL; 88554ceac45SDavid Howells nfs_put_client(clp); 88654ceac45SDavid Howells dprintk("<-- nfs_init_server() = xerror %d\n", error); 88754ceac45SDavid Howells return error; 88854ceac45SDavid Howells } 88954ceac45SDavid Howells 89054ceac45SDavid Howells /* 89154ceac45SDavid Howells * Load up the server record from information gained in an fsinfo record 89254ceac45SDavid Howells */ 89354ceac45SDavid Howells static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo) 89454ceac45SDavid Howells { 89554ceac45SDavid Howells unsigned long max_rpc_payload; 89654ceac45SDavid Howells 89754ceac45SDavid Howells /* Work out a lot of parameters */ 89854ceac45SDavid Howells if (server->rsize == 0) 89954ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtpref, NULL); 90054ceac45SDavid Howells if (server->wsize == 0) 90154ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtpref, NULL); 90254ceac45SDavid Howells 90354ceac45SDavid Howells if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) 90454ceac45SDavid Howells server->rsize = nfs_block_size(fsinfo->rtmax, NULL); 90554ceac45SDavid Howells if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) 90654ceac45SDavid Howells server->wsize = nfs_block_size(fsinfo->wtmax, NULL); 90754ceac45SDavid Howells 90854ceac45SDavid Howells max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL); 90954ceac45SDavid Howells if (server->rsize > max_rpc_payload) 91054ceac45SDavid Howells server->rsize = max_rpc_payload; 91154ceac45SDavid Howells if (server->rsize > NFS_MAX_FILE_IO_SIZE) 91254ceac45SDavid Howells server->rsize = NFS_MAX_FILE_IO_SIZE; 91354ceac45SDavid Howells server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 914e0bf68ddSPeter Zijlstra 915d993831fSJens Axboe server->backing_dev_info.name = "nfs"; 91654ceac45SDavid Howells server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD; 91754ceac45SDavid Howells 91854ceac45SDavid Howells if (server->wsize > max_rpc_payload) 91954ceac45SDavid Howells server->wsize = max_rpc_payload; 92054ceac45SDavid Howells if (server->wsize > NFS_MAX_FILE_IO_SIZE) 92154ceac45SDavid Howells server->wsize = NFS_MAX_FILE_IO_SIZE; 92254ceac45SDavid Howells server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 92385e174baSRicardo Labiaga set_pnfs_layoutdriver(server, fsinfo->layouttype); 92485e174baSRicardo Labiaga 92554ceac45SDavid Howells server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); 92654ceac45SDavid Howells 92754ceac45SDavid Howells server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); 92856e4ebf8SBryan Schumaker if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES) 92956e4ebf8SBryan Schumaker server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES; 93054ceac45SDavid Howells if (server->dtsize > server->rsize) 93154ceac45SDavid Howells server->dtsize = server->rsize; 93254ceac45SDavid Howells 93354ceac45SDavid Howells if (server->flags & NFS_MOUNT_NOAC) { 93454ceac45SDavid Howells server->acregmin = server->acregmax = 0; 93554ceac45SDavid Howells server->acdirmin = server->acdirmax = 0; 93654ceac45SDavid Howells } 93754ceac45SDavid Howells 93854ceac45SDavid Howells server->maxfilesize = fsinfo->maxfilesize; 93954ceac45SDavid Howells 9406b96724eSRicardo Labiaga server->time_delta = fsinfo->time_delta; 9416b96724eSRicardo Labiaga 94254ceac45SDavid Howells /* We're airborne Set socket buffersize */ 94354ceac45SDavid Howells rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); 94454ceac45SDavid Howells } 94554ceac45SDavid Howells 94654ceac45SDavid Howells /* 94754ceac45SDavid Howells * Probe filesystem information, including the FSID on v2/v3 94854ceac45SDavid Howells */ 94954ceac45SDavid Howells static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 95054ceac45SDavid Howells { 95154ceac45SDavid Howells struct nfs_fsinfo fsinfo; 95254ceac45SDavid Howells struct nfs_client *clp = server->nfs_client; 95354ceac45SDavid Howells int error; 95454ceac45SDavid Howells 95554ceac45SDavid Howells dprintk("--> nfs_probe_fsinfo()\n"); 95654ceac45SDavid Howells 95754ceac45SDavid Howells if (clp->rpc_ops->set_capabilities != NULL) { 95854ceac45SDavid Howells error = clp->rpc_ops->set_capabilities(server, mntfh); 95954ceac45SDavid Howells if (error < 0) 96054ceac45SDavid Howells goto out_error; 96154ceac45SDavid Howells } 96254ceac45SDavid Howells 96354ceac45SDavid Howells fsinfo.fattr = fattr; 96485e174baSRicardo Labiaga fsinfo.layouttype = 0; 96554ceac45SDavid Howells error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); 96654ceac45SDavid Howells if (error < 0) 96754ceac45SDavid Howells goto out_error; 96854ceac45SDavid Howells 96954ceac45SDavid Howells nfs_server_set_fsinfo(server, &fsinfo); 97054ceac45SDavid Howells 97154ceac45SDavid Howells /* Get some general file system info */ 97254ceac45SDavid Howells if (server->namelen == 0) { 97354ceac45SDavid Howells struct nfs_pathconf pathinfo; 97454ceac45SDavid Howells 97554ceac45SDavid Howells pathinfo.fattr = fattr; 97654ceac45SDavid Howells nfs_fattr_init(fattr); 97754ceac45SDavid Howells 97854ceac45SDavid Howells if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) 97954ceac45SDavid Howells server->namelen = pathinfo.max_namelen; 98054ceac45SDavid Howells } 98154ceac45SDavid Howells 98254ceac45SDavid Howells dprintk("<-- nfs_probe_fsinfo() = 0\n"); 98354ceac45SDavid Howells return 0; 98454ceac45SDavid Howells 98554ceac45SDavid Howells out_error: 98654ceac45SDavid Howells dprintk("nfs_probe_fsinfo: error = %d\n", -error); 98754ceac45SDavid Howells return error; 98854ceac45SDavid Howells } 98954ceac45SDavid Howells 99054ceac45SDavid Howells /* 99154ceac45SDavid Howells * Copy useful information when duplicating a server record 99254ceac45SDavid Howells */ 99354ceac45SDavid Howells static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 99454ceac45SDavid Howells { 99554ceac45SDavid Howells target->flags = source->flags; 996356e76b8SChuck Lever target->rsize = source->rsize; 997356e76b8SChuck Lever target->wsize = source->wsize; 99854ceac45SDavid Howells target->acregmin = source->acregmin; 99954ceac45SDavid Howells target->acregmax = source->acregmax; 100054ceac45SDavid Howells target->acdirmin = source->acdirmin; 100154ceac45SDavid Howells target->acdirmax = source->acdirmax; 100254ceac45SDavid Howells target->caps = source->caps; 10032df54806SDavid Howells target->options = source->options; 100454ceac45SDavid Howells } 100554ceac45SDavid Howells 100654ceac45SDavid Howells /* 100754ceac45SDavid Howells * Allocate and initialise a server record 100854ceac45SDavid Howells */ 100954ceac45SDavid Howells static struct nfs_server *nfs_alloc_server(void) 101054ceac45SDavid Howells { 101154ceac45SDavid Howells struct nfs_server *server; 101254ceac45SDavid Howells 101354ceac45SDavid Howells server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); 101454ceac45SDavid Howells if (!server) 101554ceac45SDavid Howells return NULL; 101654ceac45SDavid Howells 101754ceac45SDavid Howells server->client = server->client_acl = ERR_PTR(-EINVAL); 101854ceac45SDavid Howells 101954ceac45SDavid Howells /* Zero out the NFS state stuff */ 102054ceac45SDavid Howells INIT_LIST_HEAD(&server->client_link); 102154ceac45SDavid Howells INIT_LIST_HEAD(&server->master_link); 102254ceac45SDavid Howells 1023ef818a28SSteve Dickson atomic_set(&server->active, 0); 1024ef818a28SSteve Dickson 102554ceac45SDavid Howells server->io_stats = nfs_alloc_iostats(); 102654ceac45SDavid Howells if (!server->io_stats) { 102754ceac45SDavid Howells kfree(server); 102854ceac45SDavid Howells return NULL; 102954ceac45SDavid Howells } 103054ceac45SDavid Howells 103148d07649SJens Axboe if (bdi_init(&server->backing_dev_info)) { 103248d07649SJens Axboe nfs_free_iostats(server->io_stats); 103348d07649SJens Axboe kfree(server); 103448d07649SJens Axboe return NULL; 103548d07649SJens Axboe } 103648d07649SJens Axboe 1037*f7e8917aSFred Isaman pnfs_init_server(server); 1038*f7e8917aSFred Isaman 103954ceac45SDavid Howells return server; 104054ceac45SDavid Howells } 104154ceac45SDavid Howells 104254ceac45SDavid Howells /* 104354ceac45SDavid Howells * Free up a server record 104454ceac45SDavid Howells */ 104554ceac45SDavid Howells void nfs_free_server(struct nfs_server *server) 104654ceac45SDavid Howells { 104754ceac45SDavid Howells dprintk("--> nfs_free_server()\n"); 104854ceac45SDavid Howells 104985e174baSRicardo Labiaga unset_pnfs_layoutdriver(server); 105054ceac45SDavid Howells spin_lock(&nfs_client_lock); 105154ceac45SDavid Howells list_del(&server->client_link); 105254ceac45SDavid Howells list_del(&server->master_link); 105354ceac45SDavid Howells spin_unlock(&nfs_client_lock); 105454ceac45SDavid Howells 105554ceac45SDavid Howells if (server->destroy != NULL) 105654ceac45SDavid Howells server->destroy(server); 10575cef338bSTrond Myklebust 10585cef338bSTrond Myklebust if (!IS_ERR(server->client_acl)) 10595cef338bSTrond Myklebust rpc_shutdown_client(server->client_acl); 106054ceac45SDavid Howells if (!IS_ERR(server->client)) 106154ceac45SDavid Howells rpc_shutdown_client(server->client); 106254ceac45SDavid Howells 106354ceac45SDavid Howells nfs_put_client(server->nfs_client); 106454ceac45SDavid Howells 106554ceac45SDavid Howells nfs_free_iostats(server->io_stats); 1066e0bf68ddSPeter Zijlstra bdi_destroy(&server->backing_dev_info); 106754ceac45SDavid Howells kfree(server); 106854ceac45SDavid Howells nfs_release_automount_timer(); 106954ceac45SDavid Howells dprintk("<-- nfs_free_server()\n"); 107054ceac45SDavid Howells } 107154ceac45SDavid Howells 107254ceac45SDavid Howells /* 107354ceac45SDavid Howells * Create a version 2 or 3 volume record 107454ceac45SDavid Howells * - keyed on server and FSID 107554ceac45SDavid Howells */ 10762283f8d6S\"Talpey, Thomas\ struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data, 107754ceac45SDavid Howells struct nfs_fh *mntfh) 107854ceac45SDavid Howells { 107954ceac45SDavid Howells struct nfs_server *server; 1080fbca779aSTrond Myklebust struct nfs_fattr *fattr; 108154ceac45SDavid Howells int error; 108254ceac45SDavid Howells 108354ceac45SDavid Howells server = nfs_alloc_server(); 108454ceac45SDavid Howells if (!server) 108554ceac45SDavid Howells return ERR_PTR(-ENOMEM); 108654ceac45SDavid Howells 1087fbca779aSTrond Myklebust error = -ENOMEM; 1088fbca779aSTrond Myklebust fattr = nfs_alloc_fattr(); 1089fbca779aSTrond Myklebust if (fattr == NULL) 1090fbca779aSTrond Myklebust goto error; 1091fbca779aSTrond Myklebust 109254ceac45SDavid Howells /* Get a client representation */ 109354ceac45SDavid Howells error = nfs_init_server(server, data); 109454ceac45SDavid Howells if (error < 0) 109554ceac45SDavid Howells goto error; 109654ceac45SDavid Howells 109754ceac45SDavid Howells BUG_ON(!server->nfs_client); 109854ceac45SDavid Howells BUG_ON(!server->nfs_client->rpc_ops); 109954ceac45SDavid Howells BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); 110054ceac45SDavid Howells 110154ceac45SDavid Howells /* Probe the root fh to retrieve its FSID */ 1102fbca779aSTrond Myklebust error = nfs_probe_fsinfo(server, mntfh, fattr); 110354ceac45SDavid Howells if (error < 0) 110454ceac45SDavid Howells goto error; 110554af3bb5STrond Myklebust if (server->nfs_client->rpc_ops->version == 3) { 110654af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) 110754af3bb5STrond Myklebust server->namelen = NFS3_MAXNAMLEN; 110854af3bb5STrond Myklebust if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) 110954af3bb5STrond Myklebust server->caps |= NFS_CAP_READDIRPLUS; 111054af3bb5STrond Myklebust } else { 111154af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) 111254af3bb5STrond Myklebust server->namelen = NFS2_MAXNAMLEN; 111354af3bb5STrond Myklebust } 111454af3bb5STrond Myklebust 1115fbca779aSTrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR)) { 1116fbca779aSTrond Myklebust error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr); 111754ceac45SDavid Howells if (error < 0) { 111854ceac45SDavid Howells dprintk("nfs_create_server: getattr error = %d\n", -error); 111954ceac45SDavid Howells goto error; 112054ceac45SDavid Howells } 112154ceac45SDavid Howells } 1122fbca779aSTrond Myklebust memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 112354ceac45SDavid Howells 11246daabf1bSDavid Howells dprintk("Server FSID: %llx:%llx\n", 11256daabf1bSDavid Howells (unsigned long long) server->fsid.major, 11266daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 112754ceac45SDavid Howells 112854ceac45SDavid Howells spin_lock(&nfs_client_lock); 112954ceac45SDavid Howells list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); 113054ceac45SDavid Howells list_add_tail(&server->master_link, &nfs_volume_list); 113154ceac45SDavid Howells spin_unlock(&nfs_client_lock); 113254ceac45SDavid Howells 113354ceac45SDavid Howells server->mount_time = jiffies; 1134fbca779aSTrond Myklebust nfs_free_fattr(fattr); 113554ceac45SDavid Howells return server; 113654ceac45SDavid Howells 113754ceac45SDavid Howells error: 1138fbca779aSTrond Myklebust nfs_free_fattr(fattr); 113954ceac45SDavid Howells nfs_free_server(server); 114054ceac45SDavid Howells return ERR_PTR(error); 114154ceac45SDavid Howells } 114254ceac45SDavid Howells 114354ceac45SDavid Howells #ifdef CONFIG_NFS_V4 114454ceac45SDavid Howells /* 1145c36fca52SAndy Adamson * NFSv4.0 callback thread helper 1146c36fca52SAndy Adamson * 1147c36fca52SAndy Adamson * Find a client by IP address, protocol version, and minorversion 1148c36fca52SAndy Adamson * 1149c36fca52SAndy Adamson * Called from the pg_authenticate method. The callback identifier 1150c36fca52SAndy Adamson * is not used as it has not been decoded. 1151c36fca52SAndy Adamson * 1152c36fca52SAndy Adamson * Returns NULL if no such client 1153c36fca52SAndy Adamson */ 1154c36fca52SAndy Adamson struct nfs_client * 1155c36fca52SAndy Adamson nfs4_find_client_no_ident(const struct sockaddr *addr) 1156c36fca52SAndy Adamson { 1157c36fca52SAndy Adamson struct nfs_client *clp; 1158c36fca52SAndy Adamson 1159c36fca52SAndy Adamson spin_lock(&nfs_client_lock); 1160c36fca52SAndy Adamson list_for_each_entry(clp, &nfs_client_list, cl_share_link) { 1161c36fca52SAndy Adamson if (nfs4_cb_match_client(addr, clp, 0) == false) 1162c36fca52SAndy Adamson continue; 1163c36fca52SAndy Adamson atomic_inc(&clp->cl_count); 1164c36fca52SAndy Adamson spin_unlock(&nfs_client_lock); 1165c36fca52SAndy Adamson return clp; 1166c36fca52SAndy Adamson } 1167c36fca52SAndy Adamson spin_unlock(&nfs_client_lock); 1168c36fca52SAndy Adamson return NULL; 1169c36fca52SAndy Adamson } 1170c36fca52SAndy Adamson 1171c36fca52SAndy Adamson /* 1172c36fca52SAndy Adamson * NFSv4.0 callback thread helper 1173c36fca52SAndy Adamson * 1174c36fca52SAndy Adamson * Find a client by callback identifier 1175c36fca52SAndy Adamson */ 1176c36fca52SAndy Adamson struct nfs_client * 1177c36fca52SAndy Adamson nfs4_find_client_ident(int cb_ident) 1178c36fca52SAndy Adamson { 1179c36fca52SAndy Adamson struct nfs_client *clp; 1180c36fca52SAndy Adamson 1181c36fca52SAndy Adamson spin_lock(&nfs_client_lock); 1182c36fca52SAndy Adamson clp = idr_find(&cb_ident_idr, cb_ident); 1183c36fca52SAndy Adamson if (clp) 1184c36fca52SAndy Adamson atomic_inc(&clp->cl_count); 1185c36fca52SAndy Adamson spin_unlock(&nfs_client_lock); 1186c36fca52SAndy Adamson return clp; 1187c36fca52SAndy Adamson } 1188c36fca52SAndy Adamson 1189c36fca52SAndy Adamson #if defined(CONFIG_NFS_V4_1) 1190c36fca52SAndy Adamson /* 1191c36fca52SAndy Adamson * NFSv4.1 callback thread helper 1192c36fca52SAndy Adamson * For CB_COMPOUND calls, find a client by IP address, protocol version, 1193c36fca52SAndy Adamson * minorversion, and sessionID 1194c36fca52SAndy Adamson * 1195c36fca52SAndy Adamson * CREATE_SESSION triggers a CB_NULL ping from servers. The callback service 1196c36fca52SAndy Adamson * sessionid can only be set after the CREATE_SESSION return, so a CB_NULL 1197c36fca52SAndy Adamson * can arrive before the callback sessionid is set. For CB_NULL calls, 1198c36fca52SAndy Adamson * find a client by IP address protocol version, and minorversion. 1199c36fca52SAndy Adamson * 1200c36fca52SAndy Adamson * Returns NULL if no such client 1201c36fca52SAndy Adamson */ 1202c36fca52SAndy Adamson struct nfs_client * 1203c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr, 1204c36fca52SAndy Adamson struct nfs4_sessionid *sid, int is_cb_compound) 1205c36fca52SAndy Adamson { 1206c36fca52SAndy Adamson struct nfs_client *clp; 1207c36fca52SAndy Adamson 1208c36fca52SAndy Adamson spin_lock(&nfs_client_lock); 1209c36fca52SAndy Adamson list_for_each_entry(clp, &nfs_client_list, cl_share_link) { 1210c36fca52SAndy Adamson if (nfs4_cb_match_client(addr, clp, 1) == false) 1211c36fca52SAndy Adamson continue; 1212c36fca52SAndy Adamson 1213c36fca52SAndy Adamson if (!nfs4_has_session(clp)) 1214c36fca52SAndy Adamson continue; 1215c36fca52SAndy Adamson 1216c36fca52SAndy Adamson /* Match sessionid unless cb_null call*/ 1217c36fca52SAndy Adamson if (is_cb_compound && (memcmp(clp->cl_session->sess_id.data, 1218c36fca52SAndy Adamson sid->data, NFS4_MAX_SESSIONID_LEN) != 0)) 1219c36fca52SAndy Adamson continue; 1220c36fca52SAndy Adamson 1221c36fca52SAndy Adamson atomic_inc(&clp->cl_count); 1222c36fca52SAndy Adamson spin_unlock(&nfs_client_lock); 1223c36fca52SAndy Adamson return clp; 1224c36fca52SAndy Adamson } 1225c36fca52SAndy Adamson spin_unlock(&nfs_client_lock); 1226c36fca52SAndy Adamson return NULL; 1227c36fca52SAndy Adamson } 1228c36fca52SAndy Adamson 1229c36fca52SAndy Adamson #else /* CONFIG_NFS_V4_1 */ 1230c36fca52SAndy Adamson 1231c36fca52SAndy Adamson struct nfs_client * 1232c36fca52SAndy Adamson nfs4_find_client_sessionid(const struct sockaddr *addr, 1233c36fca52SAndy Adamson struct nfs4_sessionid *sid, int is_cb_compound) 1234c36fca52SAndy Adamson { 1235c36fca52SAndy Adamson return NULL; 1236c36fca52SAndy Adamson } 1237c36fca52SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1238c36fca52SAndy Adamson 1239c36fca52SAndy Adamson /* 12409bdaa86dSBenny Halevy * Initialize the NFS4 callback service 12419bdaa86dSBenny Halevy */ 12429bdaa86dSBenny Halevy static int nfs4_init_callback(struct nfs_client *clp) 12439bdaa86dSBenny Halevy { 12449bdaa86dSBenny Halevy int error; 12459bdaa86dSBenny Halevy 12469bdaa86dSBenny Halevy if (clp->rpc_ops->version == 4) { 12470b5b7ae0SAndy Adamson if (nfs4_has_session(clp)) { 12480b5b7ae0SAndy Adamson error = xprt_setup_backchannel( 12490b5b7ae0SAndy Adamson clp->cl_rpcclient->cl_xprt, 12500b5b7ae0SAndy Adamson NFS41_BC_MIN_CALLBACKS); 12510b5b7ae0SAndy Adamson if (error < 0) 12520b5b7ae0SAndy Adamson return error; 12530b5b7ae0SAndy Adamson } 12540b5b7ae0SAndy Adamson 125597dc1359STrond Myklebust error = nfs_callback_up(clp->cl_mvops->minor_version, 125671468513SBenny Halevy clp->cl_rpcclient->cl_xprt); 12579bdaa86dSBenny Halevy if (error < 0) { 12589bdaa86dSBenny Halevy dprintk("%s: failed to start callback. Error = %d\n", 12599bdaa86dSBenny Halevy __func__, error); 12609bdaa86dSBenny Halevy return error; 12619bdaa86dSBenny Halevy } 12629bdaa86dSBenny Halevy __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); 12639bdaa86dSBenny Halevy } 12649bdaa86dSBenny Halevy return 0; 12659bdaa86dSBenny Halevy } 12669bdaa86dSBenny Halevy 12679bdaa86dSBenny Halevy /* 1268557134a3SAndy Adamson * Initialize the minor version specific parts of an NFS4 client record 1269557134a3SAndy Adamson */ 1270557134a3SAndy Adamson static int nfs4_init_client_minor_version(struct nfs_client *clp) 1271557134a3SAndy Adamson { 1272557134a3SAndy Adamson #if defined(CONFIG_NFS_V4_1) 127397dc1359STrond Myklebust if (clp->cl_mvops->minor_version) { 1274557134a3SAndy Adamson struct nfs4_session *session = NULL; 1275557134a3SAndy Adamson /* 1276557134a3SAndy Adamson * Create the session and mark it expired. 1277557134a3SAndy Adamson * When a SEQUENCE operation encounters the expired session 1278557134a3SAndy Adamson * it will do session recovery to initialize it. 1279557134a3SAndy Adamson */ 1280557134a3SAndy Adamson session = nfs4_alloc_session(clp); 1281557134a3SAndy Adamson if (!session) 1282557134a3SAndy Adamson return -ENOMEM; 1283557134a3SAndy Adamson 1284557134a3SAndy Adamson clp->cl_session = session; 1285fe74ba3aSTrond Myklebust /* 1286fe74ba3aSTrond Myklebust * The create session reply races with the server back 1287fe74ba3aSTrond Myklebust * channel probe. Mark the client NFS_CS_SESSION_INITING 1288fe74ba3aSTrond Myklebust * so that the client back channel can find the 1289fe74ba3aSTrond Myklebust * nfs_client struct 1290fe74ba3aSTrond Myklebust */ 1291fe74ba3aSTrond Myklebust clp->cl_cons_state = NFS_CS_SESSION_INITING; 1292557134a3SAndy Adamson } 1293557134a3SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 1294557134a3SAndy Adamson 129571468513SBenny Halevy return nfs4_init_callback(clp); 1296557134a3SAndy Adamson } 1297557134a3SAndy Adamson 1298557134a3SAndy Adamson /* 129954ceac45SDavid Howells * Initialise an NFS4 client record 130054ceac45SDavid Howells */ 130154ceac45SDavid Howells static int nfs4_init_client(struct nfs_client *clp, 130233170233STrond Myklebust const struct rpc_timeout *timeparms, 13037d9ac06fSJ. Bruce Fields const char *ip_addr, 1304d740351bSChuck Lever rpc_authflavor_t authflavour, 1305d740351bSChuck Lever int flags) 130654ceac45SDavid Howells { 130754ceac45SDavid Howells int error; 130854ceac45SDavid Howells 130954ceac45SDavid Howells if (clp->cl_cons_state == NFS_CS_READY) { 131054ceac45SDavid Howells /* the client is initialised already */ 131154ceac45SDavid Howells dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); 131254ceac45SDavid Howells return 0; 131354ceac45SDavid Howells } 131454ceac45SDavid Howells 131554ceac45SDavid Howells /* Check NFS protocol revision and initialize RPC op vector */ 131654ceac45SDavid Howells clp->rpc_ops = &nfs_v4_clientops; 131754ceac45SDavid Howells 131859dca3b2STrond Myklebust error = nfs_create_rpc_client(clp, timeparms, authflavour, 1319d740351bSChuck Lever 1, flags & NFS_MOUNT_NORESVPORT); 132054ceac45SDavid Howells if (error < 0) 132154ceac45SDavid Howells goto error; 1322f4373bf9SBen Hutchings strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); 132354ceac45SDavid Howells 132454ceac45SDavid Howells error = nfs_idmap_new(clp); 132554ceac45SDavid Howells if (error < 0) { 132654ceac45SDavid Howells dprintk("%s: failed to create idmapper. Error = %d\n", 13273110ff80SHarvey Harrison __func__, error); 132854ceac45SDavid Howells goto error; 132954ceac45SDavid Howells } 13309c5bf38dSTrond Myklebust __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); 133154ceac45SDavid Howells 1332557134a3SAndy Adamson error = nfs4_init_client_minor_version(clp); 1333557134a3SAndy Adamson if (error < 0) 1334557134a3SAndy Adamson goto error; 1335557134a3SAndy Adamson 133676db6d95SAndy Adamson if (!nfs4_has_session(clp)) 133754ceac45SDavid Howells nfs_mark_client_ready(clp, NFS_CS_READY); 133854ceac45SDavid Howells return 0; 133954ceac45SDavid Howells 134054ceac45SDavid Howells error: 134154ceac45SDavid Howells nfs_mark_client_ready(clp, error); 134254ceac45SDavid Howells dprintk("<-- nfs4_init_client() = xerror %d\n", error); 134354ceac45SDavid Howells return error; 134454ceac45SDavid Howells } 134554ceac45SDavid Howells 134654ceac45SDavid Howells /* 134754ceac45SDavid Howells * Set up an NFS4 client 134854ceac45SDavid Howells */ 134954ceac45SDavid Howells static int nfs4_set_client(struct nfs_server *server, 1350dcecae0fSChuck Lever const char *hostname, 1351dcecae0fSChuck Lever const struct sockaddr *addr, 1352dcecae0fSChuck Lever const size_t addrlen, 13537d9ac06fSJ. Bruce Fields const char *ip_addr, 135454ceac45SDavid Howells rpc_authflavor_t authflavour, 135594a417f3SBenny Halevy int proto, const struct rpc_timeout *timeparms, 135694a417f3SBenny Halevy u32 minorversion) 135754ceac45SDavid Howells { 13583a498026STrond Myklebust struct nfs_client_initdata cl_init = { 13593a498026STrond Myklebust .hostname = hostname, 1360dcecae0fSChuck Lever .addr = addr, 1361dcecae0fSChuck Lever .addrlen = addrlen, 136240c55319STrond Myklebust .rpc_ops = &nfs_v4_clientops, 136359dca3b2STrond Myklebust .proto = proto, 13645aae4a9aSBenny Halevy .minorversion = minorversion, 13653a498026STrond Myklebust }; 136654ceac45SDavid Howells struct nfs_client *clp; 136754ceac45SDavid Howells int error; 136854ceac45SDavid Howells 136954ceac45SDavid Howells dprintk("--> nfs4_set_client()\n"); 137054ceac45SDavid Howells 137154ceac45SDavid Howells /* Allocate or find a client reference we can use */ 13723a498026STrond Myklebust clp = nfs_get_client(&cl_init); 137354ceac45SDavid Howells if (IS_ERR(clp)) { 137454ceac45SDavid Howells error = PTR_ERR(clp); 137554ceac45SDavid Howells goto error; 137654ceac45SDavid Howells } 1377d740351bSChuck Lever error = nfs4_init_client(clp, timeparms, ip_addr, authflavour, 1378d740351bSChuck Lever server->flags); 137954ceac45SDavid Howells if (error < 0) 138054ceac45SDavid Howells goto error_put; 138154ceac45SDavid Howells 138254ceac45SDavid Howells server->nfs_client = clp; 138354ceac45SDavid Howells dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); 138454ceac45SDavid Howells return 0; 138554ceac45SDavid Howells 138654ceac45SDavid Howells error_put: 138754ceac45SDavid Howells nfs_put_client(clp); 138854ceac45SDavid Howells error: 138954ceac45SDavid Howells dprintk("<-- nfs4_set_client() = xerror %d\n", error); 139054ceac45SDavid Howells return error; 139154ceac45SDavid Howells } 139254ceac45SDavid Howells 1393557134a3SAndy Adamson 1394557134a3SAndy Adamson /* 139596b09e02SAndy Adamson * Session has been established, and the client marked ready. 139696b09e02SAndy Adamson * Set the mount rsize and wsize with negotiated fore channel 139796b09e02SAndy Adamson * attributes which will be bound checked in nfs_server_set_fsinfo. 139896b09e02SAndy Adamson */ 139996b09e02SAndy Adamson static void nfs4_session_set_rwsize(struct nfs_server *server) 140096b09e02SAndy Adamson { 140196b09e02SAndy Adamson #ifdef CONFIG_NFS_V4_1 14022449ea2eSAlexandros Batsakis struct nfs4_session *sess; 14032449ea2eSAlexandros Batsakis u32 server_resp_sz; 14042449ea2eSAlexandros Batsakis u32 server_rqst_sz; 14052449ea2eSAlexandros Batsakis 140696b09e02SAndy Adamson if (!nfs4_has_session(server->nfs_client)) 140796b09e02SAndy Adamson return; 14082449ea2eSAlexandros Batsakis sess = server->nfs_client->cl_session; 14092449ea2eSAlexandros Batsakis server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; 14102449ea2eSAlexandros Batsakis server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; 14112449ea2eSAlexandros Batsakis 14122449ea2eSAlexandros Batsakis if (server->rsize > server_resp_sz) 14132449ea2eSAlexandros Batsakis server->rsize = server_resp_sz; 14142449ea2eSAlexandros Batsakis if (server->wsize > server_rqst_sz) 14152449ea2eSAlexandros Batsakis server->wsize = server_rqst_sz; 141696b09e02SAndy Adamson #endif /* CONFIG_NFS_V4_1 */ 141796b09e02SAndy Adamson } 141896b09e02SAndy Adamson 141944950b67STrond Myklebust static int nfs4_server_common_setup(struct nfs_server *server, 142044950b67STrond Myklebust struct nfs_fh *mntfh) 142144950b67STrond Myklebust { 142244950b67STrond Myklebust struct nfs_fattr *fattr; 142344950b67STrond Myklebust int error; 142444950b67STrond Myklebust 142544950b67STrond Myklebust BUG_ON(!server->nfs_client); 142644950b67STrond Myklebust BUG_ON(!server->nfs_client->rpc_ops); 142744950b67STrond Myklebust BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); 142844950b67STrond Myklebust 142944950b67STrond Myklebust fattr = nfs_alloc_fattr(); 143044950b67STrond Myklebust if (fattr == NULL) 143144950b67STrond Myklebust return -ENOMEM; 143244950b67STrond Myklebust 143344950b67STrond Myklebust /* We must ensure the session is initialised first */ 143444950b67STrond Myklebust error = nfs4_init_session(server); 143544950b67STrond Myklebust if (error < 0) 143644950b67STrond Myklebust goto out; 143744950b67STrond Myklebust 143844950b67STrond Myklebust /* Probe the root fh to retrieve its FSID and filehandle */ 143944950b67STrond Myklebust error = nfs4_get_rootfh(server, mntfh); 144044950b67STrond Myklebust if (error < 0) 144144950b67STrond Myklebust goto out; 144244950b67STrond Myklebust 144344950b67STrond Myklebust dprintk("Server FSID: %llx:%llx\n", 144444950b67STrond Myklebust (unsigned long long) server->fsid.major, 144544950b67STrond Myklebust (unsigned long long) server->fsid.minor); 144644950b67STrond Myklebust dprintk("Mount FH: %d\n", mntfh->size); 144744950b67STrond Myklebust 144844950b67STrond Myklebust nfs4_session_set_rwsize(server); 144944950b67STrond Myklebust 145044950b67STrond Myklebust error = nfs_probe_fsinfo(server, mntfh, fattr); 145144950b67STrond Myklebust if (error < 0) 145244950b67STrond Myklebust goto out; 145344950b67STrond Myklebust 145444950b67STrond Myklebust if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 145544950b67STrond Myklebust server->namelen = NFS4_MAXNAMLEN; 145644950b67STrond Myklebust 145744950b67STrond Myklebust spin_lock(&nfs_client_lock); 145844950b67STrond Myklebust list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); 145944950b67STrond Myklebust list_add_tail(&server->master_link, &nfs_volume_list); 146044950b67STrond Myklebust spin_unlock(&nfs_client_lock); 146144950b67STrond Myklebust 146244950b67STrond Myklebust server->mount_time = jiffies; 146344950b67STrond Myklebust out: 146444950b67STrond Myklebust nfs_free_fattr(fattr); 146544950b67STrond Myklebust return error; 146644950b67STrond Myklebust } 146744950b67STrond Myklebust 146896b09e02SAndy Adamson /* 146954ceac45SDavid Howells * Create a version 4 volume record 147054ceac45SDavid Howells */ 147154ceac45SDavid Howells static int nfs4_init_server(struct nfs_server *server, 147291ea40b9S\"Talpey, Thomas\ const struct nfs_parsed_mount_data *data) 147354ceac45SDavid Howells { 147433170233STrond Myklebust struct rpc_timeout timeparms; 147554ceac45SDavid Howells int error; 147654ceac45SDavid Howells 147754ceac45SDavid Howells dprintk("--> nfs4_init_server()\n"); 147854ceac45SDavid Howells 147933170233STrond Myklebust nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, 148033170233STrond Myklebust data->timeo, data->retrans); 148133170233STrond Myklebust 1482542fcc33SChuck Lever /* Initialise the client representation from the mount data */ 1483542fcc33SChuck Lever server->flags = data->flags; 148482f2e547SBryan Schumaker server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK; 148582f2e547SBryan Schumaker if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) 148682f2e547SBryan Schumaker server->caps |= NFS_CAP_READDIRPLUS; 1487b797cac7SDavid Howells server->options = data->options; 1488542fcc33SChuck Lever 148933170233STrond Myklebust /* Get a client record */ 149033170233STrond Myklebust error = nfs4_set_client(server, 149133170233STrond Myklebust data->nfs_server.hostname, 149233170233STrond Myklebust (const struct sockaddr *)&data->nfs_server.address, 149333170233STrond Myklebust data->nfs_server.addrlen, 149433170233STrond Myklebust data->client_address, 149533170233STrond Myklebust data->auth_flavors[0], 149633170233STrond Myklebust data->nfs_server.protocol, 149794a417f3SBenny Halevy &timeparms, 149894a417f3SBenny Halevy data->minorversion); 149933170233STrond Myklebust if (error < 0) 150033170233STrond Myklebust goto error; 150133170233STrond Myklebust 150254ceac45SDavid Howells if (data->rsize) 150354ceac45SDavid Howells server->rsize = nfs_block_size(data->rsize, NULL); 150454ceac45SDavid Howells if (data->wsize) 150554ceac45SDavid Howells server->wsize = nfs_block_size(data->wsize, NULL); 150654ceac45SDavid Howells 150754ceac45SDavid Howells server->acregmin = data->acregmin * HZ; 150854ceac45SDavid Howells server->acregmax = data->acregmax * HZ; 150954ceac45SDavid Howells server->acdirmin = data->acdirmin * HZ; 151054ceac45SDavid Howells server->acdirmax = data->acdirmax * HZ; 151154ceac45SDavid Howells 1512f22d6d79SChuck Lever server->port = data->nfs_server.port; 1513f22d6d79SChuck Lever 151433170233STrond Myklebust error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); 151554ceac45SDavid Howells 151633170233STrond Myklebust error: 151754ceac45SDavid Howells /* Done */ 151854ceac45SDavid Howells dprintk("<-- nfs4_init_server() = %d\n", error); 151954ceac45SDavid Howells return error; 152054ceac45SDavid Howells } 152154ceac45SDavid Howells 152254ceac45SDavid Howells /* 152354ceac45SDavid Howells * Create a version 4 volume record 152454ceac45SDavid Howells * - keyed on server and FSID 152554ceac45SDavid Howells */ 152691ea40b9S\"Talpey, Thomas\ struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, 152754ceac45SDavid Howells struct nfs_fh *mntfh) 152854ceac45SDavid Howells { 152954ceac45SDavid Howells struct nfs_server *server; 153054ceac45SDavid Howells int error; 153154ceac45SDavid Howells 153254ceac45SDavid Howells dprintk("--> nfs4_create_server()\n"); 153354ceac45SDavid Howells 153454ceac45SDavid Howells server = nfs_alloc_server(); 153554ceac45SDavid Howells if (!server) 153654ceac45SDavid Howells return ERR_PTR(-ENOMEM); 153754ceac45SDavid Howells 153854ceac45SDavid Howells /* set up the general RPC client */ 153991ea40b9S\"Talpey, Thomas\ error = nfs4_init_server(server, data); 154054ceac45SDavid Howells if (error < 0) 154154ceac45SDavid Howells goto error; 154254ceac45SDavid Howells 154344950b67STrond Myklebust error = nfs4_server_common_setup(server, mntfh); 1544fccba804STrond Myklebust if (error < 0) 1545fccba804STrond Myklebust goto error; 1546557134a3SAndy Adamson 154754ceac45SDavid Howells dprintk("<-- nfs4_create_server() = %p\n", server); 154854ceac45SDavid Howells return server; 154954ceac45SDavid Howells 155054ceac45SDavid Howells error: 155154ceac45SDavid Howells nfs_free_server(server); 155254ceac45SDavid Howells dprintk("<-- nfs4_create_server() = error %d\n", error); 155354ceac45SDavid Howells return ERR_PTR(error); 155454ceac45SDavid Howells } 155554ceac45SDavid Howells 155654ceac45SDavid Howells /* 155754ceac45SDavid Howells * Create an NFS4 referral server record 155854ceac45SDavid Howells */ 155954ceac45SDavid Howells struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, 1560f2d0d85eSTrond Myklebust struct nfs_fh *mntfh) 156154ceac45SDavid Howells { 156254ceac45SDavid Howells struct nfs_client *parent_client; 156354ceac45SDavid Howells struct nfs_server *server, *parent_server; 156454ceac45SDavid Howells int error; 156554ceac45SDavid Howells 156654ceac45SDavid Howells dprintk("--> nfs4_create_referral_server()\n"); 156754ceac45SDavid Howells 156854ceac45SDavid Howells server = nfs_alloc_server(); 156954ceac45SDavid Howells if (!server) 157054ceac45SDavid Howells return ERR_PTR(-ENOMEM); 157154ceac45SDavid Howells 157254ceac45SDavid Howells parent_server = NFS_SB(data->sb); 157354ceac45SDavid Howells parent_client = parent_server->nfs_client; 157454ceac45SDavid Howells 1575542fcc33SChuck Lever /* Initialise the client representation from the parent server */ 1576542fcc33SChuck Lever nfs_server_copy_userdata(server, parent_server); 157762ab460cSTrond Myklebust server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; 1578542fcc33SChuck Lever 157954ceac45SDavid Howells /* Get a client representation. 158054ceac45SDavid Howells * Note: NFSv4 always uses TCP, */ 1581dcecae0fSChuck Lever error = nfs4_set_client(server, data->hostname, 15826677d095SChuck Lever data->addr, 15836677d095SChuck Lever data->addrlen, 15847d9ac06fSJ. Bruce Fields parent_client->cl_ipaddr, 158554ceac45SDavid Howells data->authflavor, 158654ceac45SDavid Howells parent_server->client->cl_xprt->prot, 158794a417f3SBenny Halevy parent_server->client->cl_timeout, 158897dc1359STrond Myklebust parent_client->cl_mvops->minor_version); 1589297de4f6Sandros@citi.umich.edu if (error < 0) 1590297de4f6Sandros@citi.umich.edu goto error; 159154ceac45SDavid Howells 159233170233STrond Myklebust error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); 159354ceac45SDavid Howells if (error < 0) 159454ceac45SDavid Howells goto error; 159554ceac45SDavid Howells 159644950b67STrond Myklebust error = nfs4_server_common_setup(server, mntfh); 1597f2d0d85eSTrond Myklebust if (error < 0) 1598f2d0d85eSTrond Myklebust goto error; 1599f2d0d85eSTrond Myklebust 160054ceac45SDavid Howells dprintk("<-- nfs_create_referral_server() = %p\n", server); 160154ceac45SDavid Howells return server; 160254ceac45SDavid Howells 160354ceac45SDavid Howells error: 160454ceac45SDavid Howells nfs_free_server(server); 160554ceac45SDavid Howells dprintk("<-- nfs4_create_referral_server() = error %d\n", error); 160654ceac45SDavid Howells return ERR_PTR(error); 160754ceac45SDavid Howells } 160854ceac45SDavid Howells 160954ceac45SDavid Howells #endif /* CONFIG_NFS_V4 */ 161054ceac45SDavid Howells 161154ceac45SDavid Howells /* 161254ceac45SDavid Howells * Clone an NFS2, NFS3 or NFS4 server record 161354ceac45SDavid Howells */ 161454ceac45SDavid Howells struct nfs_server *nfs_clone_server(struct nfs_server *source, 161554ceac45SDavid Howells struct nfs_fh *fh, 161654ceac45SDavid Howells struct nfs_fattr *fattr) 161754ceac45SDavid Howells { 161854ceac45SDavid Howells struct nfs_server *server; 1619fbca779aSTrond Myklebust struct nfs_fattr *fattr_fsinfo; 162054ceac45SDavid Howells int error; 162154ceac45SDavid Howells 162254ceac45SDavid Howells dprintk("--> nfs_clone_server(,%llx:%llx,)\n", 16236daabf1bSDavid Howells (unsigned long long) fattr->fsid.major, 16246daabf1bSDavid Howells (unsigned long long) fattr->fsid.minor); 162554ceac45SDavid Howells 162654ceac45SDavid Howells server = nfs_alloc_server(); 162754ceac45SDavid Howells if (!server) 162854ceac45SDavid Howells return ERR_PTR(-ENOMEM); 162954ceac45SDavid Howells 1630fbca779aSTrond Myklebust error = -ENOMEM; 1631fbca779aSTrond Myklebust fattr_fsinfo = nfs_alloc_fattr(); 1632fbca779aSTrond Myklebust if (fattr_fsinfo == NULL) 1633fbca779aSTrond Myklebust goto out_free_server; 1634fbca779aSTrond Myklebust 163554ceac45SDavid Howells /* Copy data from the source */ 163654ceac45SDavid Howells server->nfs_client = source->nfs_client; 163754ceac45SDavid Howells atomic_inc(&server->nfs_client->cl_count); 163854ceac45SDavid Howells nfs_server_copy_userdata(server, source); 163954ceac45SDavid Howells 164054ceac45SDavid Howells server->fsid = fattr->fsid; 164154ceac45SDavid Howells 164233170233STrond Myklebust error = nfs_init_server_rpcclient(server, 164333170233STrond Myklebust source->client->cl_timeout, 164433170233STrond Myklebust source->client->cl_auth->au_flavor); 164554ceac45SDavid Howells if (error < 0) 164654ceac45SDavid Howells goto out_free_server; 164754ceac45SDavid Howells if (!IS_ERR(source->client_acl)) 164854ceac45SDavid Howells nfs_init_server_aclclient(server); 164954ceac45SDavid Howells 165054ceac45SDavid Howells /* probe the filesystem info for this server filesystem */ 1651fbca779aSTrond Myklebust error = nfs_probe_fsinfo(server, fh, fattr_fsinfo); 165254ceac45SDavid Howells if (error < 0) 165354ceac45SDavid Howells goto out_free_server; 165454ceac45SDavid Howells 165554af3bb5STrond Myklebust if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 165654af3bb5STrond Myklebust server->namelen = NFS4_MAXNAMLEN; 165754af3bb5STrond Myklebust 165854ceac45SDavid Howells dprintk("Cloned FSID: %llx:%llx\n", 16596daabf1bSDavid Howells (unsigned long long) server->fsid.major, 16606daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 166154ceac45SDavid Howells 166254ceac45SDavid Howells error = nfs_start_lockd(server); 166354ceac45SDavid Howells if (error < 0) 166454ceac45SDavid Howells goto out_free_server; 166554ceac45SDavid Howells 166654ceac45SDavid Howells spin_lock(&nfs_client_lock); 166754ceac45SDavid Howells list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); 166854ceac45SDavid Howells list_add_tail(&server->master_link, &nfs_volume_list); 166954ceac45SDavid Howells spin_unlock(&nfs_client_lock); 167054ceac45SDavid Howells 167154ceac45SDavid Howells server->mount_time = jiffies; 167254ceac45SDavid Howells 1673fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 167454ceac45SDavid Howells dprintk("<-- nfs_clone_server() = %p\n", server); 167554ceac45SDavid Howells return server; 167654ceac45SDavid Howells 167754ceac45SDavid Howells out_free_server: 1678fbca779aSTrond Myklebust nfs_free_fattr(fattr_fsinfo); 167954ceac45SDavid Howells nfs_free_server(server); 168054ceac45SDavid Howells dprintk("<-- nfs_clone_server() = error %d\n", error); 168154ceac45SDavid Howells return ERR_PTR(error); 168254ceac45SDavid Howells } 16836aaca566SDavid Howells 16846aaca566SDavid Howells #ifdef CONFIG_PROC_FS 16856aaca566SDavid Howells static struct proc_dir_entry *proc_fs_nfs; 16866aaca566SDavid Howells 16876aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file); 16886aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); 16896aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); 16906aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v); 16916aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v); 16926aaca566SDavid Howells 169388e9d34cSJames Morris static const struct seq_operations nfs_server_list_ops = { 16946aaca566SDavid Howells .start = nfs_server_list_start, 16956aaca566SDavid Howells .next = nfs_server_list_next, 16966aaca566SDavid Howells .stop = nfs_server_list_stop, 16976aaca566SDavid Howells .show = nfs_server_list_show, 16986aaca566SDavid Howells }; 16996aaca566SDavid Howells 170000977a59SArjan van de Ven static const struct file_operations nfs_server_list_fops = { 17016aaca566SDavid Howells .open = nfs_server_list_open, 17026aaca566SDavid Howells .read = seq_read, 17036aaca566SDavid Howells .llseek = seq_lseek, 17046aaca566SDavid Howells .release = seq_release, 170534b37235SDenis V. Lunev .owner = THIS_MODULE, 17066aaca566SDavid Howells }; 17076aaca566SDavid Howells 17086aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file); 17096aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); 17106aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); 17116aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v); 17126aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v); 17136aaca566SDavid Howells 171488e9d34cSJames Morris static const struct seq_operations nfs_volume_list_ops = { 17156aaca566SDavid Howells .start = nfs_volume_list_start, 17166aaca566SDavid Howells .next = nfs_volume_list_next, 17176aaca566SDavid Howells .stop = nfs_volume_list_stop, 17186aaca566SDavid Howells .show = nfs_volume_list_show, 17196aaca566SDavid Howells }; 17206aaca566SDavid Howells 172100977a59SArjan van de Ven static const struct file_operations nfs_volume_list_fops = { 17226aaca566SDavid Howells .open = nfs_volume_list_open, 17236aaca566SDavid Howells .read = seq_read, 17246aaca566SDavid Howells .llseek = seq_lseek, 17256aaca566SDavid Howells .release = seq_release, 172634b37235SDenis V. Lunev .owner = THIS_MODULE, 17276aaca566SDavid Howells }; 17286aaca566SDavid Howells 17296aaca566SDavid Howells /* 17306aaca566SDavid Howells * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which 17316aaca566SDavid Howells * we're dealing 17326aaca566SDavid Howells */ 17336aaca566SDavid Howells static int nfs_server_list_open(struct inode *inode, struct file *file) 17346aaca566SDavid Howells { 17356aaca566SDavid Howells struct seq_file *m; 17366aaca566SDavid Howells int ret; 17376aaca566SDavid Howells 17386aaca566SDavid Howells ret = seq_open(file, &nfs_server_list_ops); 17396aaca566SDavid Howells if (ret < 0) 17406aaca566SDavid Howells return ret; 17416aaca566SDavid Howells 17426aaca566SDavid Howells m = file->private_data; 17436aaca566SDavid Howells m->private = PDE(inode)->data; 17446aaca566SDavid Howells 17456aaca566SDavid Howells return 0; 17466aaca566SDavid Howells } 17476aaca566SDavid Howells 17486aaca566SDavid Howells /* 17496aaca566SDavid Howells * set up the iterator to start reading from the server list and return the first item 17506aaca566SDavid Howells */ 17516aaca566SDavid Howells static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) 17526aaca566SDavid Howells { 17536aaca566SDavid Howells /* lock the list against modification */ 17546aaca566SDavid Howells spin_lock(&nfs_client_lock); 1755259902eaSPavel Emelianov return seq_list_start_head(&nfs_client_list, *_pos); 17566aaca566SDavid Howells } 17576aaca566SDavid Howells 17586aaca566SDavid Howells /* 17596aaca566SDavid Howells * move to next server 17606aaca566SDavid Howells */ 17616aaca566SDavid Howells static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) 17626aaca566SDavid Howells { 1763259902eaSPavel Emelianov return seq_list_next(v, &nfs_client_list, pos); 17646aaca566SDavid Howells } 17656aaca566SDavid Howells 17666aaca566SDavid Howells /* 17676aaca566SDavid Howells * clean up after reading from the transports list 17686aaca566SDavid Howells */ 17696aaca566SDavid Howells static void nfs_server_list_stop(struct seq_file *p, void *v) 17706aaca566SDavid Howells { 17716aaca566SDavid Howells spin_unlock(&nfs_client_lock); 17726aaca566SDavid Howells } 17736aaca566SDavid Howells 17746aaca566SDavid Howells /* 17756aaca566SDavid Howells * display a header line followed by a load of call lines 17766aaca566SDavid Howells */ 17776aaca566SDavid Howells static int nfs_server_list_show(struct seq_file *m, void *v) 17786aaca566SDavid Howells { 17796aaca566SDavid Howells struct nfs_client *clp; 17806aaca566SDavid Howells 17816aaca566SDavid Howells /* display header on line 1 */ 1782259902eaSPavel Emelianov if (v == &nfs_client_list) { 17836aaca566SDavid Howells seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); 17846aaca566SDavid Howells return 0; 17856aaca566SDavid Howells } 17866aaca566SDavid Howells 17876aaca566SDavid Howells /* display one transport per line on subsequent lines */ 17886aaca566SDavid Howells clp = list_entry(v, struct nfs_client, cl_share_link); 17896aaca566SDavid Howells 17905d8515caSChuck Lever seq_printf(m, "v%u %s %s %3d %s\n", 179140c55319STrond Myklebust clp->rpc_ops->version, 17925d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 17935d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 17946aaca566SDavid Howells atomic_read(&clp->cl_count), 17956aaca566SDavid Howells clp->cl_hostname); 17966aaca566SDavid Howells 17976aaca566SDavid Howells return 0; 17986aaca566SDavid Howells } 17996aaca566SDavid Howells 18006aaca566SDavid Howells /* 18016aaca566SDavid Howells * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes 18026aaca566SDavid Howells */ 18036aaca566SDavid Howells static int nfs_volume_list_open(struct inode *inode, struct file *file) 18046aaca566SDavid Howells { 18056aaca566SDavid Howells struct seq_file *m; 18066aaca566SDavid Howells int ret; 18076aaca566SDavid Howells 18086aaca566SDavid Howells ret = seq_open(file, &nfs_volume_list_ops); 18096aaca566SDavid Howells if (ret < 0) 18106aaca566SDavid Howells return ret; 18116aaca566SDavid Howells 18126aaca566SDavid Howells m = file->private_data; 18136aaca566SDavid Howells m->private = PDE(inode)->data; 18146aaca566SDavid Howells 18156aaca566SDavid Howells return 0; 18166aaca566SDavid Howells } 18176aaca566SDavid Howells 18186aaca566SDavid Howells /* 18196aaca566SDavid Howells * set up the iterator to start reading from the volume list and return the first item 18206aaca566SDavid Howells */ 18216aaca566SDavid Howells static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) 18226aaca566SDavid Howells { 18236aaca566SDavid Howells /* lock the list against modification */ 18246aaca566SDavid Howells spin_lock(&nfs_client_lock); 1825259902eaSPavel Emelianov return seq_list_start_head(&nfs_volume_list, *_pos); 18266aaca566SDavid Howells } 18276aaca566SDavid Howells 18286aaca566SDavid Howells /* 18296aaca566SDavid Howells * move to next volume 18306aaca566SDavid Howells */ 18316aaca566SDavid Howells static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) 18326aaca566SDavid Howells { 1833259902eaSPavel Emelianov return seq_list_next(v, &nfs_volume_list, pos); 18346aaca566SDavid Howells } 18356aaca566SDavid Howells 18366aaca566SDavid Howells /* 18376aaca566SDavid Howells * clean up after reading from the transports list 18386aaca566SDavid Howells */ 18396aaca566SDavid Howells static void nfs_volume_list_stop(struct seq_file *p, void *v) 18406aaca566SDavid Howells { 18416aaca566SDavid Howells spin_unlock(&nfs_client_lock); 18426aaca566SDavid Howells } 18436aaca566SDavid Howells 18446aaca566SDavid Howells /* 18456aaca566SDavid Howells * display a header line followed by a load of call lines 18466aaca566SDavid Howells */ 18476aaca566SDavid Howells static int nfs_volume_list_show(struct seq_file *m, void *v) 18486aaca566SDavid Howells { 18496aaca566SDavid Howells struct nfs_server *server; 18506aaca566SDavid Howells struct nfs_client *clp; 18516aaca566SDavid Howells char dev[8], fsid[17]; 18526aaca566SDavid Howells 18536aaca566SDavid Howells /* display header on line 1 */ 1854259902eaSPavel Emelianov if (v == &nfs_volume_list) { 18555d1acff1SDavid Howells seq_puts(m, "NV SERVER PORT DEV FSID FSC\n"); 18566aaca566SDavid Howells return 0; 18576aaca566SDavid Howells } 18586aaca566SDavid Howells /* display one transport per line on subsequent lines */ 18596aaca566SDavid Howells server = list_entry(v, struct nfs_server, master_link); 18606aaca566SDavid Howells clp = server->nfs_client; 18616aaca566SDavid Howells 18626aaca566SDavid Howells snprintf(dev, 8, "%u:%u", 18636aaca566SDavid Howells MAJOR(server->s_dev), MINOR(server->s_dev)); 18646aaca566SDavid Howells 18656aaca566SDavid Howells snprintf(fsid, 17, "%llx:%llx", 18666daabf1bSDavid Howells (unsigned long long) server->fsid.major, 18676daabf1bSDavid Howells (unsigned long long) server->fsid.minor); 18686aaca566SDavid Howells 18695d1acff1SDavid Howells seq_printf(m, "v%u %s %s %-7s %-17s %s\n", 187040c55319STrond Myklebust clp->rpc_ops->version, 18715d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 18725d8515caSChuck Lever rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 18736aaca566SDavid Howells dev, 18745d1acff1SDavid Howells fsid, 18755d1acff1SDavid Howells nfs_server_fscache_state(server)); 18766aaca566SDavid Howells 18776aaca566SDavid Howells return 0; 18786aaca566SDavid Howells } 18796aaca566SDavid Howells 18806aaca566SDavid Howells /* 18816aaca566SDavid Howells * initialise the /proc/fs/nfsfs/ directory 18826aaca566SDavid Howells */ 18836aaca566SDavid Howells int __init nfs_fs_proc_init(void) 18846aaca566SDavid Howells { 18856aaca566SDavid Howells struct proc_dir_entry *p; 18866aaca566SDavid Howells 188736a5aeb8SAlexey Dobriyan proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL); 18886aaca566SDavid Howells if (!proc_fs_nfs) 18896aaca566SDavid Howells goto error_0; 18906aaca566SDavid Howells 18916aaca566SDavid Howells /* a file of servers with which we're dealing */ 189234b37235SDenis V. Lunev p = proc_create("servers", S_IFREG|S_IRUGO, 189334b37235SDenis V. Lunev proc_fs_nfs, &nfs_server_list_fops); 18946aaca566SDavid Howells if (!p) 18956aaca566SDavid Howells goto error_1; 18966aaca566SDavid Howells 18976aaca566SDavid Howells /* a file of volumes that we have mounted */ 189834b37235SDenis V. Lunev p = proc_create("volumes", S_IFREG|S_IRUGO, 189934b37235SDenis V. Lunev proc_fs_nfs, &nfs_volume_list_fops); 19006aaca566SDavid Howells if (!p) 19016aaca566SDavid Howells goto error_2; 19026aaca566SDavid Howells return 0; 19036aaca566SDavid Howells 19046aaca566SDavid Howells error_2: 19056aaca566SDavid Howells remove_proc_entry("servers", proc_fs_nfs); 19066aaca566SDavid Howells error_1: 190736a5aeb8SAlexey Dobriyan remove_proc_entry("fs/nfsfs", NULL); 19086aaca566SDavid Howells error_0: 19096aaca566SDavid Howells return -ENOMEM; 19106aaca566SDavid Howells } 19116aaca566SDavid Howells 19126aaca566SDavid Howells /* 19136aaca566SDavid Howells * clean up the /proc/fs/nfsfs/ directory 19146aaca566SDavid Howells */ 19156aaca566SDavid Howells void nfs_fs_proc_exit(void) 19166aaca566SDavid Howells { 19176aaca566SDavid Howells remove_proc_entry("volumes", proc_fs_nfs); 19186aaca566SDavid Howells remove_proc_entry("servers", proc_fs_nfs); 191936a5aeb8SAlexey Dobriyan remove_proc_entry("fs/nfsfs", NULL); 19206aaca566SDavid Howells } 19216aaca566SDavid Howells 19226aaca566SDavid Howells #endif /* CONFIG_PROC_FS */ 1923