David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 1 | /* client.c: NFS client sharing and management code |
| 2 | * |
| 3 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 16 | #include <linux/time.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/string.h> |
| 20 | #include <linux/stat.h> |
| 21 | #include <linux/errno.h> |
| 22 | #include <linux/unistd.h> |
| 23 | #include <linux/sunrpc/clnt.h> |
| 24 | #include <linux/sunrpc/stats.h> |
| 25 | #include <linux/sunrpc/metrics.h> |
\"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 26 | #include <linux/sunrpc/xprtsock.h> |
\"Talpey, Thomas\ | 2cf7ff7 | 2007-09-10 13:49:41 -0400 | [diff] [blame] | 27 | #include <linux/sunrpc/xprtrdma.h> |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 28 | #include <linux/nfs_fs.h> |
| 29 | #include <linux/nfs_mount.h> |
| 30 | #include <linux/nfs4_mount.h> |
| 31 | #include <linux/lockd/bind.h> |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 32 | #include <linux/seq_file.h> |
| 33 | #include <linux/mount.h> |
| 34 | #include <linux/nfs_idmap.h> |
| 35 | #include <linux/vfs.h> |
| 36 | #include <linux/inet.h> |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 37 | #include <linux/in6.h> |
| 38 | #include <net/ipv6.h> |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 39 | #include <linux/nfs_xdr.h> |
| 40 | |
| 41 | #include <asm/system.h> |
| 42 | |
| 43 | #include "nfs4_fs.h" |
| 44 | #include "callback.h" |
| 45 | #include "delegation.h" |
| 46 | #include "iostat.h" |
| 47 | #include "internal.h" |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 48 | #include "fscache.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 49 | |
| 50 | #define NFSDBG_FACILITY NFSDBG_CLIENT |
| 51 | |
| 52 | static DEFINE_SPINLOCK(nfs_client_lock); |
| 53 | static LIST_HEAD(nfs_client_list); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 54 | static LIST_HEAD(nfs_volume_list); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 55 | static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); |
| 56 | |
| 57 | /* |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 58 | * RPC cruft for NFS |
| 59 | */ |
| 60 | static struct rpc_version *nfs_version[5] = { |
| 61 | [2] = &nfs_version2, |
| 62 | #ifdef CONFIG_NFS_V3 |
| 63 | [3] = &nfs_version3, |
| 64 | #endif |
| 65 | #ifdef CONFIG_NFS_V4 |
| 66 | [4] = &nfs_version4, |
| 67 | #endif |
| 68 | }; |
| 69 | |
| 70 | struct rpc_program nfs_program = { |
| 71 | .name = "nfs", |
| 72 | .number = NFS_PROGRAM, |
| 73 | .nrvers = ARRAY_SIZE(nfs_version), |
| 74 | .version = nfs_version, |
| 75 | .stats = &nfs_rpcstat, |
| 76 | .pipe_dir_name = "/nfs", |
| 77 | }; |
| 78 | |
| 79 | struct rpc_stat nfs_rpcstat = { |
| 80 | .program = &nfs_program |
| 81 | }; |
| 82 | |
| 83 | |
| 84 | #ifdef CONFIG_NFS_V3_ACL |
| 85 | static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program }; |
| 86 | static struct rpc_version * nfsacl_version[] = { |
| 87 | [3] = &nfsacl_version3, |
| 88 | }; |
| 89 | |
| 90 | struct rpc_program nfsacl_program = { |
| 91 | .name = "nfsacl", |
| 92 | .number = NFS_ACL_PROGRAM, |
| 93 | .nrvers = ARRAY_SIZE(nfsacl_version), |
| 94 | .version = nfsacl_version, |
| 95 | .stats = &nfsacl_rpcstat, |
| 96 | }; |
| 97 | #endif /* CONFIG_NFS_V3_ACL */ |
| 98 | |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 99 | struct nfs_client_initdata { |
| 100 | const char *hostname; |
Chuck Lever | d7422c4 | 2007-12-10 14:58:51 -0500 | [diff] [blame] | 101 | const struct sockaddr *addr; |
Chuck Lever | 6e4cffd | 2007-12-10 14:58:15 -0500 | [diff] [blame] | 102 | size_t addrlen; |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 103 | const struct nfs_rpc_ops *rpc_ops; |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 104 | int proto; |
Benny Halevy | 5aae4a9 | 2009-04-01 09:21:50 -0400 | [diff] [blame] | 105 | u32 minorversion; |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 106 | }; |
| 107 | |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 108 | /* |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 109 | * Allocate a shared client record |
| 110 | * |
| 111 | * Since these are allocated/deallocated very rarely, we don't |
| 112 | * bother putting them in a slab cache... |
| 113 | */ |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 114 | static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 115 | { |
| 116 | struct nfs_client *clp; |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 117 | struct rpc_cred *cred; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 118 | |
| 119 | if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) |
| 120 | goto error_0; |
| 121 | |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 122 | clp->rpc_ops = cl_init->rpc_ops; |
| 123 | |
| 124 | if (cl_init->rpc_ops->version == 4) { |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 125 | if (nfs_callback_up() < 0) |
| 126 | goto error_2; |
| 127 | __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); |
| 128 | } |
| 129 | |
| 130 | atomic_set(&clp->cl_count, 1); |
| 131 | clp->cl_cons_state = NFS_CS_INITING; |
| 132 | |
Chuck Lever | 6e4cffd | 2007-12-10 14:58:15 -0500 | [diff] [blame] | 133 | memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); |
| 134 | clp->cl_addrlen = cl_init->addrlen; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 135 | |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 136 | if (cl_init->hostname) { |
| 137 | clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 138 | if (!clp->cl_hostname) |
| 139 | goto error_3; |
| 140 | } |
| 141 | |
| 142 | INIT_LIST_HEAD(&clp->cl_superblocks); |
| 143 | clp->cl_rpcclient = ERR_PTR(-EINVAL); |
| 144 | |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 145 | clp->cl_proto = cl_init->proto; |
| 146 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 147 | #ifdef CONFIG_NFS_V4 |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 148 | INIT_LIST_HEAD(&clp->cl_delegations); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 149 | spin_lock_init(&clp->cl_lock); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 150 | INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 151 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); |
| 152 | clp->cl_boot_time = CURRENT_TIME; |
| 153 | clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; |
Benny Halevy | 5aae4a9 | 2009-04-01 09:21:50 -0400 | [diff] [blame] | 154 | clp->cl_minorversion = cl_init->minorversion; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 155 | #endif |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 156 | cred = rpc_lookup_machine_cred(); |
| 157 | if (!IS_ERR(cred)) |
| 158 | clp->cl_machine_cred = cred; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 159 | |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 160 | nfs_fscache_get_client_cookie(clp); |
| 161 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 162 | return clp; |
| 163 | |
| 164 | error_3: |
Trond Myklebust | 9c5bf38 | 2006-08-22 20:06:14 -0400 | [diff] [blame] | 165 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) |
| 166 | nfs_callback_down(); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 167 | error_2: |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 168 | kfree(clp); |
| 169 | error_0: |
| 170 | return NULL; |
| 171 | } |
| 172 | |
Trond Myklebust | 5dd3177 | 2006-08-24 01:03:05 -0400 | [diff] [blame] | 173 | static void nfs4_shutdown_client(struct nfs_client *clp) |
| 174 | { |
| 175 | #ifdef CONFIG_NFS_V4 |
| 176 | if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) |
| 177 | nfs4_kill_renewd(clp); |
Trond Myklebust | 9f958ab | 2007-07-02 13:58:33 -0400 | [diff] [blame] | 178 | BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners)); |
Trond Myklebust | 5dd3177 | 2006-08-24 01:03:05 -0400 | [diff] [blame] | 179 | if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) |
| 180 | nfs_idmap_delete(clp); |
Trond Myklebust | f6a1cc8 | 2008-02-22 17:06:55 -0500 | [diff] [blame] | 181 | |
| 182 | rpc_destroy_wait_queue(&clp->cl_rpcwaitq); |
Trond Myklebust | 5dd3177 | 2006-08-24 01:03:05 -0400 | [diff] [blame] | 183 | #endif |
| 184 | } |
| 185 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 186 | /* |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 187 | * Clears/puts all minor version specific parts from an nfs_client struct |
| 188 | * reverting it to minorversion 0. |
| 189 | */ |
| 190 | static void nfs4_clear_client_minor_version(struct nfs_client *clp) |
| 191 | { |
| 192 | #ifdef CONFIG_NFS_V4_1 |
| 193 | if (nfs4_has_session(clp)) { |
| 194 | nfs4_destroy_session(clp->cl_session); |
| 195 | clp->cl_session = NULL; |
| 196 | } |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame^] | 197 | |
| 198 | clp->cl_call_sync = _nfs4_call_sync; |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 199 | #endif /* CONFIG_NFS_V4_1 */ |
| 200 | } |
| 201 | |
| 202 | /* |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 203 | * Destroy a shared client record |
| 204 | */ |
| 205 | static void nfs_free_client(struct nfs_client *clp) |
| 206 | { |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 207 | dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 208 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 209 | nfs4_clear_client_minor_version(clp); |
Trond Myklebust | 5dd3177 | 2006-08-24 01:03:05 -0400 | [diff] [blame] | 210 | nfs4_shutdown_client(clp); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 211 | |
David Howells | 1472728 | 2009-04-03 16:42:42 +0100 | [diff] [blame] | 212 | nfs_fscache_release_client_cookie(clp); |
| 213 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 214 | /* -EIO all pending I/O */ |
| 215 | if (!IS_ERR(clp->cl_rpcclient)) |
| 216 | rpc_shutdown_client(clp->cl_rpcclient); |
| 217 | |
| 218 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) |
| 219 | nfs_callback_down(); |
| 220 | |
Trond Myklebust | 7c67db3 | 2008-04-07 20:50:11 -0400 | [diff] [blame] | 221 | if (clp->cl_machine_cred != NULL) |
| 222 | put_rpccred(clp->cl_machine_cred); |
| 223 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 224 | kfree(clp->cl_hostname); |
| 225 | kfree(clp); |
| 226 | |
| 227 | dprintk("<-- nfs_free_client()\n"); |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * Release a reference to a shared client record |
| 232 | */ |
| 233 | void nfs_put_client(struct nfs_client *clp) |
| 234 | { |
David Howells | 27ba851 | 2006-07-30 14:40:56 -0400 | [diff] [blame] | 235 | if (!clp) |
| 236 | return; |
| 237 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 238 | dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count)); |
| 239 | |
| 240 | if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) { |
| 241 | list_del(&clp->cl_share_link); |
| 242 | spin_unlock(&nfs_client_lock); |
| 243 | |
| 244 | BUG_ON(!list_empty(&clp->cl_superblocks)); |
| 245 | |
| 246 | nfs_free_client(clp); |
| 247 | } |
| 248 | } |
| 249 | |
Trond Myklebust | 9082a5c | 2008-12-23 15:21:53 -0500 | [diff] [blame] | 250 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 251 | /* |
| 252 | * Test if two ip6 socket addresses refer to the same socket by |
| 253 | * comparing relevant fields. The padding bytes specifically, are not |
| 254 | * compared. sin6_flowinfo is not compared because it only affects QoS |
| 255 | * and sin6_scope_id is only compared if the address is "link local" |
| 256 | * because "link local" addresses need only be unique to a specific |
| 257 | * link. Conversely, ordinary unicast addresses might have different |
| 258 | * sin6_scope_id. |
| 259 | * |
| 260 | * The caller should ensure both socket addresses are AF_INET6. |
| 261 | */ |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 262 | static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, |
| 263 | const struct sockaddr *sa2) |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 264 | { |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 265 | const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; |
| 266 | const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 267 | |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 268 | if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL && |
| 269 | sin1->sin6_scope_id != sin2->sin6_scope_id) |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 270 | return 0; |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 271 | |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 272 | return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr); |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 273 | } |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 274 | #else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */ |
| 275 | static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1, |
| 276 | const struct sockaddr *sa2) |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 277 | { |
| 278 | return 0; |
| 279 | } |
Trond Myklebust | 9082a5c | 2008-12-23 15:21:53 -0500 | [diff] [blame] | 280 | #endif |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 281 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 282 | /* |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 283 | * Test if two ip4 socket addresses refer to the same socket, by |
| 284 | * comparing relevant fields. The padding bytes specifically, are |
| 285 | * not compared. |
| 286 | * |
| 287 | * The caller should ensure both socket addresses are AF_INET. |
| 288 | */ |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 289 | static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1, |
| 290 | const struct sockaddr *sa2) |
| 291 | { |
| 292 | const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; |
| 293 | const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; |
| 294 | |
| 295 | return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr; |
| 296 | } |
| 297 | |
| 298 | static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1, |
| 299 | const struct sockaddr *sa2) |
| 300 | { |
| 301 | const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1; |
| 302 | const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2; |
| 303 | |
| 304 | return nfs_sockaddr_match_ipaddr6(sa1, sa2) && |
| 305 | (sin1->sin6_port == sin2->sin6_port); |
| 306 | } |
| 307 | |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 308 | static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1, |
| 309 | const struct sockaddr *sa2) |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 310 | { |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 311 | const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1; |
| 312 | const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2; |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 313 | |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 314 | return nfs_sockaddr_match_ipaddr4(sa1, sa2) && |
| 315 | (sin1->sin_port == sin2->sin_port); |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | /* |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 319 | * Test if two socket addresses represent the same actual socket, |
Chuck Lever | 3c8c45d | 2009-03-18 20:48:14 -0400 | [diff] [blame] | 320 | * by comparing (only) relevant fields, excluding the port number. |
| 321 | */ |
| 322 | static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1, |
| 323 | const struct sockaddr *sa2) |
| 324 | { |
| 325 | if (sa1->sa_family != sa2->sa_family) |
| 326 | return 0; |
| 327 | |
| 328 | switch (sa1->sa_family) { |
| 329 | case AF_INET: |
| 330 | return nfs_sockaddr_match_ipaddr4(sa1, sa2); |
| 331 | case AF_INET6: |
| 332 | return nfs_sockaddr_match_ipaddr6(sa1, sa2); |
| 333 | } |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Test if two socket addresses represent the same actual socket, |
| 339 | * by comparing (only) relevant fields, including the port number. |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 340 | */ |
| 341 | static int nfs_sockaddr_cmp(const struct sockaddr *sa1, |
| 342 | const struct sockaddr *sa2) |
| 343 | { |
| 344 | if (sa1->sa_family != sa2->sa_family) |
| 345 | return 0; |
| 346 | |
| 347 | switch (sa1->sa_family) { |
| 348 | case AF_INET: |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 349 | return nfs_sockaddr_cmp_ip4(sa1, sa2); |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 350 | case AF_INET6: |
Trond Myklebust | 9f4c899 | 2009-03-12 14:51:32 -0400 | [diff] [blame] | 351 | return nfs_sockaddr_cmp_ip6(sa1, sa2); |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 352 | } |
| 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | /* |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 357 | * Find a client by IP address and protocol version |
| 358 | * - returns NULL if no such client |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 359 | */ |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 360 | struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion) |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 361 | { |
| 362 | struct nfs_client *clp; |
| 363 | |
| 364 | spin_lock(&nfs_client_lock); |
| 365 | list_for_each_entry(clp, &nfs_client_list, cl_share_link) { |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 366 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
| 367 | |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 368 | /* Don't match clients that failed to initialise properly */ |
| 369 | if (clp->cl_cons_state != NFS_CS_READY) |
| 370 | continue; |
| 371 | |
| 372 | /* Different NFS versions cannot share the same nfs_client */ |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 373 | if (clp->rpc_ops->version != nfsversion) |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 374 | continue; |
| 375 | |
| 376 | /* Match only the IP address, not the port number */ |
Trond Myklebust | 3b0d3f9 | 2008-01-03 13:28:58 -0500 | [diff] [blame] | 377 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 378 | continue; |
| 379 | |
| 380 | atomic_inc(&clp->cl_count); |
| 381 | spin_unlock(&nfs_client_lock); |
| 382 | return clp; |
| 383 | } |
| 384 | spin_unlock(&nfs_client_lock); |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | /* |
Trond Myklebust | 3fbd67a | 2008-01-26 01:06:40 -0500 | [diff] [blame] | 389 | * Find a client by IP address and protocol version |
| 390 | * - returns NULL if no such client |
| 391 | */ |
| 392 | struct nfs_client *nfs_find_client_next(struct nfs_client *clp) |
| 393 | { |
| 394 | struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr; |
| 395 | u32 nfsvers = clp->rpc_ops->version; |
| 396 | |
| 397 | spin_lock(&nfs_client_lock); |
| 398 | list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) { |
| 399 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
| 400 | |
| 401 | /* Don't match clients that failed to initialise properly */ |
| 402 | if (clp->cl_cons_state != NFS_CS_READY) |
| 403 | continue; |
| 404 | |
| 405 | /* Different NFS versions cannot share the same nfs_client */ |
| 406 | if (clp->rpc_ops->version != nfsvers) |
| 407 | continue; |
| 408 | |
Trond Myklebust | 3fbd67a | 2008-01-26 01:06:40 -0500 | [diff] [blame] | 409 | /* Match only the IP address, not the port number */ |
| 410 | if (!nfs_sockaddr_match_ipaddr(sap, clap)) |
| 411 | continue; |
| 412 | |
| 413 | atomic_inc(&clp->cl_count); |
| 414 | spin_unlock(&nfs_client_lock); |
| 415 | return clp; |
| 416 | } |
| 417 | spin_unlock(&nfs_client_lock); |
| 418 | return NULL; |
| 419 | } |
| 420 | |
| 421 | /* |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 422 | * Find an nfs_client on the list that matches the initialisation data |
| 423 | * that is supplied. |
| 424 | */ |
| 425 | static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 426 | { |
| 427 | struct nfs_client *clp; |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 428 | const struct sockaddr *sap = data->addr; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 429 | |
| 430 | list_for_each_entry(clp, &nfs_client_list, cl_share_link) { |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 431 | const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
Trond Myklebust | 13bbc06 | 2006-10-19 23:28:40 -0700 | [diff] [blame] | 432 | /* Don't match clients that failed to initialise properly */ |
| 433 | if (clp->cl_cons_state < 0) |
| 434 | continue; |
| 435 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 436 | /* Different NFS versions cannot share the same nfs_client */ |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 437 | if (clp->rpc_ops != data->rpc_ops) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 438 | continue; |
| 439 | |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 440 | if (clp->cl_proto != data->proto) |
| 441 | continue; |
Benny Halevy | 5aae4a9 | 2009-04-01 09:21:50 -0400 | [diff] [blame] | 442 | /* Match nfsv4 minorversion */ |
| 443 | if (clp->cl_minorversion != data->minorversion) |
| 444 | continue; |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 445 | /* Match the full socket address */ |
Ian Dall | d7371c4 | 2009-03-10 20:33:22 -0400 | [diff] [blame] | 446 | if (!nfs_sockaddr_cmp(sap, clap)) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 447 | continue; |
| 448 | |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 449 | atomic_inc(&clp->cl_count); |
| 450 | return clp; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 451 | } |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 452 | return NULL; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | /* |
| 456 | * Look up a client by IP address and protocol version |
| 457 | * - creates a new record if one doesn't yet exist |
| 458 | */ |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 459 | static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 460 | { |
| 461 | struct nfs_client *clp, *new = NULL; |
| 462 | int error; |
| 463 | |
Chuck Lever | d7422c4 | 2007-12-10 14:58:51 -0500 | [diff] [blame] | 464 | dprintk("--> nfs_get_client(%s,v%u)\n", |
| 465 | cl_init->hostname ?: "", cl_init->rpc_ops->version); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 466 | |
| 467 | /* see if the client already exists */ |
| 468 | do { |
| 469 | spin_lock(&nfs_client_lock); |
| 470 | |
Trond Myklebust | c81468a | 2007-12-14 14:56:05 -0500 | [diff] [blame] | 471 | clp = nfs_match_client(cl_init); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 472 | if (clp) |
| 473 | goto found_client; |
| 474 | if (new) |
| 475 | goto install_client; |
| 476 | |
| 477 | spin_unlock(&nfs_client_lock); |
| 478 | |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 479 | new = nfs_alloc_client(cl_init); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 480 | } while (new); |
| 481 | |
| 482 | return ERR_PTR(-ENOMEM); |
| 483 | |
| 484 | /* install a new client and return with it unready */ |
| 485 | install_client: |
| 486 | clp = new; |
| 487 | list_add(&clp->cl_share_link, &nfs_client_list); |
| 488 | spin_unlock(&nfs_client_lock); |
| 489 | dprintk("--> nfs_get_client() = %p [new]\n", clp); |
| 490 | return clp; |
| 491 | |
| 492 | /* found an existing client |
| 493 | * - make sure it's ready before returning |
| 494 | */ |
| 495 | found_client: |
| 496 | spin_unlock(&nfs_client_lock); |
| 497 | |
| 498 | if (new) |
| 499 | nfs_free_client(new); |
| 500 | |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 501 | error = wait_event_killable(nfs_client_active_wq, |
Trond Myklebust | 0bae89e | 2006-10-08 14:33:24 -0400 | [diff] [blame] | 502 | clp->cl_cons_state != NFS_CS_INITING); |
| 503 | if (error < 0) { |
| 504 | nfs_put_client(clp); |
| 505 | return ERR_PTR(-ERESTARTSYS); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | if (clp->cl_cons_state < NFS_CS_READY) { |
| 509 | error = clp->cl_cons_state; |
| 510 | nfs_put_client(clp); |
| 511 | return ERR_PTR(error); |
| 512 | } |
| 513 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 514 | BUG_ON(clp->cl_cons_state != NFS_CS_READY); |
| 515 | |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 516 | dprintk("--> nfs_get_client() = %p [share]\n", clp); |
| 517 | return clp; |
| 518 | } |
| 519 | |
| 520 | /* |
| 521 | * Mark a server as ready or failed |
| 522 | */ |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 523 | static void nfs_mark_client_ready(struct nfs_client *clp, int state) |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 524 | { |
| 525 | clp->cl_cons_state = state; |
| 526 | wake_up_all(&nfs_client_active_wq); |
| 527 | } |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 528 | |
| 529 | /* |
| 530 | * Initialise the timeout values for a connection |
| 531 | */ |
| 532 | static void nfs_init_timeout_values(struct rpc_timeout *to, int proto, |
| 533 | unsigned int timeo, unsigned int retrans) |
| 534 | { |
| 535 | to->to_initval = timeo * HZ / 10; |
| 536 | to->to_retries = retrans; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 537 | |
| 538 | switch (proto) { |
\"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 539 | case XPRT_TRANSPORT_TCP: |
\"Talpey, Thomas\ | 2cf7ff7 | 2007-09-10 13:49:41 -0400 | [diff] [blame] | 540 | case XPRT_TRANSPORT_RDMA: |
Trond Myklebust | 259875e | 2008-07-02 14:43:47 -0400 | [diff] [blame] | 541 | if (to->to_retries == 0) |
| 542 | to->to_retries = NFS_DEF_TCP_RETRANS; |
Trond Myklebust | 7a3e3e1 | 2007-12-20 16:03:57 -0500 | [diff] [blame] | 543 | if (to->to_initval == 0) |
Trond Myklebust | 259875e | 2008-07-02 14:43:47 -0400 | [diff] [blame] | 544 | to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 545 | if (to->to_initval > NFS_MAX_TCP_TIMEOUT) |
| 546 | to->to_initval = NFS_MAX_TCP_TIMEOUT; |
| 547 | to->to_increment = to->to_initval; |
| 548 | to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); |
Trond Myklebust | 7a3e3e1 | 2007-12-20 16:03:57 -0500 | [diff] [blame] | 549 | if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) |
| 550 | to->to_maxval = NFS_MAX_TCP_TIMEOUT; |
| 551 | if (to->to_maxval < to->to_initval) |
| 552 | to->to_maxval = to->to_initval; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 553 | to->to_exponential = 0; |
| 554 | break; |
\"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 555 | case XPRT_TRANSPORT_UDP: |
Trond Myklebust | 259875e | 2008-07-02 14:43:47 -0400 | [diff] [blame] | 556 | if (to->to_retries == 0) |
| 557 | to->to_retries = NFS_DEF_UDP_RETRANS; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 558 | if (!to->to_initval) |
Trond Myklebust | 259875e | 2008-07-02 14:43:47 -0400 | [diff] [blame] | 559 | to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 560 | if (to->to_initval > NFS_MAX_UDP_TIMEOUT) |
| 561 | to->to_initval = NFS_MAX_UDP_TIMEOUT; |
| 562 | to->to_maxval = NFS_MAX_UDP_TIMEOUT; |
| 563 | to->to_exponential = 1; |
| 564 | break; |
Trond Myklebust | 259875e | 2008-07-02 14:43:47 -0400 | [diff] [blame] | 565 | default: |
| 566 | BUG(); |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 567 | } |
| 568 | } |
| 569 | |
| 570 | /* |
| 571 | * Create an RPC client handle |
| 572 | */ |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 573 | static int nfs_create_rpc_client(struct nfs_client *clp, |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 574 | const struct rpc_timeout *timeparms, |
| 575 | rpc_authflavor_t flavor, |
Chuck Lever | 4a01b8a | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 576 | int discrtry, int noresvport) |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 577 | { |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 578 | struct rpc_clnt *clnt = NULL; |
Chuck Lever | 41877d2 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 579 | struct rpc_create_args args = { |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 580 | .protocol = clp->cl_proto, |
Chuck Lever | 41877d2 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 581 | .address = (struct sockaddr *)&clp->cl_addr, |
Chuck Lever | 6e4cffd | 2007-12-10 14:58:15 -0500 | [diff] [blame] | 582 | .addrsize = clp->cl_addrlen, |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 583 | .timeout = timeparms, |
Chuck Lever | 41877d2 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 584 | .servername = clp->cl_hostname, |
| 585 | .program = &nfs_program, |
| 586 | .version = clp->rpc_ops->version, |
| 587 | .authflavor = flavor, |
| 588 | }; |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 589 | |
Chuck Lever | 4a01b8a | 2008-12-23 15:21:35 -0500 | [diff] [blame] | 590 | if (discrtry) |
| 591 | args.flags |= RPC_CLNT_CREATE_DISCRTRY; |
| 592 | if (noresvport) |
| 593 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 594 | |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 595 | if (!IS_ERR(clp->cl_rpcclient)) |
| 596 | return 0; |
| 597 | |
Chuck Lever | 41877d2 | 2006-08-22 20:06:20 -0400 | [diff] [blame] | 598 | clnt = rpc_create(&args); |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 599 | if (IS_ERR(clnt)) { |
| 600 | dprintk("%s: cannot create RPC client. Error = %ld\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 601 | __func__, PTR_ERR(clnt)); |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 602 | return PTR_ERR(clnt); |
| 603 | } |
| 604 | |
David Howells | 5006a76 | 2006-08-22 20:06:12 -0400 | [diff] [blame] | 605 | clp->cl_rpcclient = clnt; |
| 606 | return 0; |
| 607 | } |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * Version 2 or 3 client destruction |
| 611 | */ |
| 612 | static void nfs_destroy_server(struct nfs_server *server) |
| 613 | { |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 614 | if (!(server->flags & NFS_MOUNT_NONLM)) |
Chuck Lever | 9289e7f | 2008-01-11 17:09:52 -0500 | [diff] [blame] | 615 | nlmclnt_done(server->nlm_host); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Version 2 or 3 lockd setup |
| 620 | */ |
| 621 | static int nfs_start_lockd(struct nfs_server *server) |
| 622 | { |
Chuck Lever | 9289e7f | 2008-01-11 17:09:52 -0500 | [diff] [blame] | 623 | struct nlm_host *host; |
| 624 | struct nfs_client *clp = server->nfs_client; |
Chuck Lever | 883bb16 | 2008-01-15 16:04:20 -0500 | [diff] [blame] | 625 | struct nlmclnt_initdata nlm_init = { |
| 626 | .hostname = clp->cl_hostname, |
| 627 | .address = (struct sockaddr *)&clp->cl_addr, |
| 628 | .addrlen = clp->cl_addrlen, |
| 629 | .protocol = server->flags & NFS_MOUNT_TCP ? |
| 630 | IPPROTO_TCP : IPPROTO_UDP, |
| 631 | .nfs_version = clp->rpc_ops->version, |
Chuck Lever | 0cb2659 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 632 | .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? |
| 633 | 1 : 0, |
Chuck Lever | 883bb16 | 2008-01-15 16:04:20 -0500 | [diff] [blame] | 634 | }; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 635 | |
Chuck Lever | 883bb16 | 2008-01-15 16:04:20 -0500 | [diff] [blame] | 636 | if (nlm_init.nfs_version > 3) |
Chuck Lever | 9289e7f | 2008-01-11 17:09:52 -0500 | [diff] [blame] | 637 | return 0; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 638 | if (server->flags & NFS_MOUNT_NONLM) |
Chuck Lever | 9289e7f | 2008-01-11 17:09:52 -0500 | [diff] [blame] | 639 | return 0; |
| 640 | |
Chuck Lever | 883bb16 | 2008-01-15 16:04:20 -0500 | [diff] [blame] | 641 | host = nlmclnt_init(&nlm_init); |
Chuck Lever | 9289e7f | 2008-01-11 17:09:52 -0500 | [diff] [blame] | 642 | if (IS_ERR(host)) |
| 643 | return PTR_ERR(host); |
| 644 | |
| 645 | server->nlm_host = host; |
| 646 | server->destroy = nfs_destroy_server; |
| 647 | return 0; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Initialise an NFSv3 ACL client connection |
| 652 | */ |
| 653 | #ifdef CONFIG_NFS_V3_ACL |
| 654 | static void nfs_init_server_aclclient(struct nfs_server *server) |
| 655 | { |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 656 | if (server->nfs_client->rpc_ops->version != 3) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 657 | goto out_noacl; |
| 658 | if (server->flags & NFS_MOUNT_NOACL) |
| 659 | goto out_noacl; |
| 660 | |
| 661 | server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3); |
| 662 | if (IS_ERR(server->client_acl)) |
| 663 | goto out_noacl; |
| 664 | |
| 665 | /* No errors! Assume that Sun nfsacls are supported */ |
| 666 | server->caps |= NFS_CAP_ACLS; |
| 667 | return; |
| 668 | |
| 669 | out_noacl: |
| 670 | server->caps &= ~NFS_CAP_ACLS; |
| 671 | } |
| 672 | #else |
| 673 | static inline void nfs_init_server_aclclient(struct nfs_server *server) |
| 674 | { |
| 675 | server->flags &= ~NFS_MOUNT_NOACL; |
| 676 | server->caps &= ~NFS_CAP_ACLS; |
| 677 | } |
| 678 | #endif |
| 679 | |
| 680 | /* |
| 681 | * Create a general RPC client |
| 682 | */ |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 683 | static int nfs_init_server_rpcclient(struct nfs_server *server, |
| 684 | const struct rpc_timeout *timeo, |
| 685 | rpc_authflavor_t pseudoflavour) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 686 | { |
| 687 | struct nfs_client *clp = server->nfs_client; |
| 688 | |
| 689 | server->client = rpc_clone_client(clp->cl_rpcclient); |
| 690 | if (IS_ERR(server->client)) { |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 691 | dprintk("%s: couldn't create rpc_client!\n", __func__); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 692 | return PTR_ERR(server->client); |
| 693 | } |
| 694 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 695 | memcpy(&server->client->cl_timeout_default, |
| 696 | timeo, |
| 697 | sizeof(server->client->cl_timeout_default)); |
| 698 | server->client->cl_timeout = &server->client->cl_timeout_default; |
| 699 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 700 | if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) { |
| 701 | struct rpc_auth *auth; |
| 702 | |
| 703 | auth = rpcauth_create(pseudoflavour, server->client); |
| 704 | if (IS_ERR(auth)) { |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 705 | dprintk("%s: couldn't create credcache!\n", __func__); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 706 | return PTR_ERR(auth); |
| 707 | } |
| 708 | } |
| 709 | server->client->cl_softrtry = 0; |
| 710 | if (server->flags & NFS_MOUNT_SOFT) |
| 711 | server->client->cl_softrtry = 1; |
| 712 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * Initialise an NFS2 or NFS3 client |
| 718 | */ |
\"Talpey, Thomas\ | 2283f8d | 2007-09-10 13:43:56 -0400 | [diff] [blame] | 719 | static int nfs_init_client(struct nfs_client *clp, |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 720 | const struct rpc_timeout *timeparms, |
\"Talpey, Thomas\ | 2283f8d | 2007-09-10 13:43:56 -0400 | [diff] [blame] | 721 | const struct nfs_parsed_mount_data *data) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 722 | { |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 723 | int error; |
| 724 | |
| 725 | if (clp->cl_cons_state == NFS_CS_READY) { |
| 726 | /* the client is already initialised */ |
| 727 | dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp); |
| 728 | return 0; |
| 729 | } |
| 730 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 731 | /* |
| 732 | * Create a client RPC handle for doing FSSTAT with UNIX auth only |
| 733 | * - RFC 2623, sec 2.3.2 |
| 734 | */ |
Chuck Lever | d740351 | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 735 | error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, |
| 736 | 0, data->flags & NFS_MOUNT_NORESVPORT); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 737 | if (error < 0) |
| 738 | goto error; |
| 739 | nfs_mark_client_ready(clp, NFS_CS_READY); |
| 740 | return 0; |
| 741 | |
| 742 | error: |
| 743 | nfs_mark_client_ready(clp, error); |
| 744 | dprintk("<-- nfs_init_client() = xerror %d\n", error); |
| 745 | return error; |
| 746 | } |
| 747 | |
| 748 | /* |
| 749 | * Create a version 2 or 3 client |
| 750 | */ |
\"Talpey, Thomas\ | 2283f8d | 2007-09-10 13:43:56 -0400 | [diff] [blame] | 751 | static int nfs_init_server(struct nfs_server *server, |
| 752 | const struct nfs_parsed_mount_data *data) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 753 | { |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 754 | struct nfs_client_initdata cl_init = { |
| 755 | .hostname = data->nfs_server.hostname, |
Chuck Lever | d7422c4 | 2007-12-10 14:58:51 -0500 | [diff] [blame] | 756 | .addr = (const struct sockaddr *)&data->nfs_server.address, |
Chuck Lever | 4c56801 | 2007-12-10 14:59:28 -0500 | [diff] [blame] | 757 | .addrlen = data->nfs_server.addrlen, |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 758 | .rpc_ops = &nfs_v2_clientops, |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 759 | .proto = data->nfs_server.protocol, |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 760 | }; |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 761 | struct rpc_timeout timeparms; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 762 | struct nfs_client *clp; |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 763 | int error; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 764 | |
| 765 | dprintk("--> nfs_init_server()\n"); |
| 766 | |
| 767 | #ifdef CONFIG_NFS_V3 |
| 768 | if (data->flags & NFS_MOUNT_VER3) |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 769 | cl_init.rpc_ops = &nfs_v3_clientops; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 770 | #endif |
| 771 | |
| 772 | /* Allocate or find a client reference we can use */ |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 773 | clp = nfs_get_client(&cl_init); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 774 | if (IS_ERR(clp)) { |
| 775 | dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp)); |
| 776 | return PTR_ERR(clp); |
| 777 | } |
| 778 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 779 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, |
| 780 | data->timeo, data->retrans); |
| 781 | error = nfs_init_client(clp, &timeparms, data); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 782 | if (error < 0) |
| 783 | goto error; |
| 784 | |
| 785 | server->nfs_client = clp; |
| 786 | |
| 787 | /* Initialise the client representation from the mount data */ |
Trond Myklebust | ff3525a | 2008-08-15 16:59:14 -0400 | [diff] [blame] | 788 | server->flags = data->flags; |
David Howells | b797cac | 2009-04-03 16:42:48 +0100 | [diff] [blame] | 789 | server->options = data->options; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 790 | |
| 791 | if (data->rsize) |
| 792 | server->rsize = nfs_block_size(data->rsize, NULL); |
| 793 | if (data->wsize) |
| 794 | server->wsize = nfs_block_size(data->wsize, NULL); |
| 795 | |
| 796 | server->acregmin = data->acregmin * HZ; |
| 797 | server->acregmax = data->acregmax * HZ; |
| 798 | server->acdirmin = data->acdirmin * HZ; |
| 799 | server->acdirmax = data->acdirmax * HZ; |
| 800 | |
| 801 | /* Start lockd here, before we might error out */ |
| 802 | error = nfs_start_lockd(server); |
| 803 | if (error < 0) |
| 804 | goto error; |
| 805 | |
Chuck Lever | f22d6d7 | 2008-03-14 14:10:22 -0400 | [diff] [blame] | 806 | server->port = data->nfs_server.port; |
| 807 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 808 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 809 | if (error < 0) |
| 810 | goto error; |
| 811 | |
Chuck Lever | 3f8400d | 2008-03-14 14:10:30 -0400 | [diff] [blame] | 812 | /* Preserve the values of mount_server-related mount options */ |
| 813 | if (data->mount_server.addrlen) { |
| 814 | memcpy(&server->mountd_address, &data->mount_server.address, |
| 815 | data->mount_server.addrlen); |
| 816 | server->mountd_addrlen = data->mount_server.addrlen; |
| 817 | } |
| 818 | server->mountd_version = data->mount_server.version; |
| 819 | server->mountd_port = data->mount_server.port; |
| 820 | server->mountd_protocol = data->mount_server.protocol; |
| 821 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 822 | server->namelen = data->namlen; |
| 823 | /* Create a client RPC handle for the NFSv3 ACL management interface */ |
| 824 | nfs_init_server_aclclient(server); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 825 | dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp); |
| 826 | return 0; |
| 827 | |
| 828 | error: |
| 829 | server->nfs_client = NULL; |
| 830 | nfs_put_client(clp); |
| 831 | dprintk("<-- nfs_init_server() = xerror %d\n", error); |
| 832 | return error; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * Load up the server record from information gained in an fsinfo record |
| 837 | */ |
| 838 | static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo) |
| 839 | { |
| 840 | unsigned long max_rpc_payload; |
| 841 | |
| 842 | /* Work out a lot of parameters */ |
| 843 | if (server->rsize == 0) |
| 844 | server->rsize = nfs_block_size(fsinfo->rtpref, NULL); |
| 845 | if (server->wsize == 0) |
| 846 | server->wsize = nfs_block_size(fsinfo->wtpref, NULL); |
| 847 | |
| 848 | if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) |
| 849 | server->rsize = nfs_block_size(fsinfo->rtmax, NULL); |
| 850 | if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) |
| 851 | server->wsize = nfs_block_size(fsinfo->wtmax, NULL); |
| 852 | |
| 853 | max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL); |
| 854 | if (server->rsize > max_rpc_payload) |
| 855 | server->rsize = max_rpc_payload; |
| 856 | if (server->rsize > NFS_MAX_FILE_IO_SIZE) |
| 857 | server->rsize = NFS_MAX_FILE_IO_SIZE; |
| 858 | server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 859 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 860 | server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD; |
| 861 | |
| 862 | if (server->wsize > max_rpc_payload) |
| 863 | server->wsize = max_rpc_payload; |
| 864 | if (server->wsize > NFS_MAX_FILE_IO_SIZE) |
| 865 | server->wsize = NFS_MAX_FILE_IO_SIZE; |
| 866 | server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; |
| 867 | server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); |
| 868 | |
| 869 | server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); |
| 870 | if (server->dtsize > PAGE_CACHE_SIZE) |
| 871 | server->dtsize = PAGE_CACHE_SIZE; |
| 872 | if (server->dtsize > server->rsize) |
| 873 | server->dtsize = server->rsize; |
| 874 | |
| 875 | if (server->flags & NFS_MOUNT_NOAC) { |
| 876 | server->acregmin = server->acregmax = 0; |
| 877 | server->acdirmin = server->acdirmax = 0; |
| 878 | } |
| 879 | |
| 880 | server->maxfilesize = fsinfo->maxfilesize; |
| 881 | |
| 882 | /* We're airborne Set socket buffersize */ |
| 883 | rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); |
| 884 | } |
| 885 | |
| 886 | /* |
| 887 | * Probe filesystem information, including the FSID on v2/v3 |
| 888 | */ |
| 889 | static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) |
| 890 | { |
| 891 | struct nfs_fsinfo fsinfo; |
| 892 | struct nfs_client *clp = server->nfs_client; |
| 893 | int error; |
| 894 | |
| 895 | dprintk("--> nfs_probe_fsinfo()\n"); |
| 896 | |
| 897 | if (clp->rpc_ops->set_capabilities != NULL) { |
| 898 | error = clp->rpc_ops->set_capabilities(server, mntfh); |
| 899 | if (error < 0) |
| 900 | goto out_error; |
| 901 | } |
| 902 | |
| 903 | fsinfo.fattr = fattr; |
| 904 | nfs_fattr_init(fattr); |
| 905 | error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); |
| 906 | if (error < 0) |
| 907 | goto out_error; |
| 908 | |
| 909 | nfs_server_set_fsinfo(server, &fsinfo); |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 910 | error = bdi_init(&server->backing_dev_info); |
| 911 | if (error) |
| 912 | goto out_error; |
| 913 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 914 | |
| 915 | /* Get some general file system info */ |
| 916 | if (server->namelen == 0) { |
| 917 | struct nfs_pathconf pathinfo; |
| 918 | |
| 919 | pathinfo.fattr = fattr; |
| 920 | nfs_fattr_init(fattr); |
| 921 | |
| 922 | if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) |
| 923 | server->namelen = pathinfo.max_namelen; |
| 924 | } |
| 925 | |
| 926 | dprintk("<-- nfs_probe_fsinfo() = 0\n"); |
| 927 | return 0; |
| 928 | |
| 929 | out_error: |
| 930 | dprintk("nfs_probe_fsinfo: error = %d\n", -error); |
| 931 | return error; |
| 932 | } |
| 933 | |
| 934 | /* |
| 935 | * Copy useful information when duplicating a server record |
| 936 | */ |
| 937 | static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) |
| 938 | { |
| 939 | target->flags = source->flags; |
| 940 | target->acregmin = source->acregmin; |
| 941 | target->acregmax = source->acregmax; |
| 942 | target->acdirmin = source->acdirmin; |
| 943 | target->acdirmax = source->acdirmax; |
| 944 | target->caps = source->caps; |
| 945 | } |
| 946 | |
| 947 | /* |
| 948 | * Allocate and initialise a server record |
| 949 | */ |
| 950 | static struct nfs_server *nfs_alloc_server(void) |
| 951 | { |
| 952 | struct nfs_server *server; |
| 953 | |
| 954 | server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); |
| 955 | if (!server) |
| 956 | return NULL; |
| 957 | |
| 958 | server->client = server->client_acl = ERR_PTR(-EINVAL); |
| 959 | |
| 960 | /* Zero out the NFS state stuff */ |
| 961 | INIT_LIST_HEAD(&server->client_link); |
| 962 | INIT_LIST_HEAD(&server->master_link); |
| 963 | |
Steve Dickson | ef818a2 | 2007-11-08 04:05:04 -0500 | [diff] [blame] | 964 | atomic_set(&server->active, 0); |
| 965 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 966 | server->io_stats = nfs_alloc_iostats(); |
| 967 | if (!server->io_stats) { |
| 968 | kfree(server); |
| 969 | return NULL; |
| 970 | } |
| 971 | |
| 972 | return server; |
| 973 | } |
| 974 | |
| 975 | /* |
| 976 | * Free up a server record |
| 977 | */ |
| 978 | void nfs_free_server(struct nfs_server *server) |
| 979 | { |
| 980 | dprintk("--> nfs_free_server()\n"); |
| 981 | |
| 982 | spin_lock(&nfs_client_lock); |
| 983 | list_del(&server->client_link); |
| 984 | list_del(&server->master_link); |
| 985 | spin_unlock(&nfs_client_lock); |
| 986 | |
| 987 | if (server->destroy != NULL) |
| 988 | server->destroy(server); |
Trond Myklebust | 5cef338 | 2007-12-11 22:01:56 -0500 | [diff] [blame] | 989 | |
| 990 | if (!IS_ERR(server->client_acl)) |
| 991 | rpc_shutdown_client(server->client_acl); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 992 | if (!IS_ERR(server->client)) |
| 993 | rpc_shutdown_client(server->client); |
| 994 | |
| 995 | nfs_put_client(server->nfs_client); |
| 996 | |
| 997 | nfs_free_iostats(server->io_stats); |
Peter Zijlstra | e0bf68d | 2007-10-16 23:25:46 -0700 | [diff] [blame] | 998 | bdi_destroy(&server->backing_dev_info); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 999 | kfree(server); |
| 1000 | nfs_release_automount_timer(); |
| 1001 | dprintk("<-- nfs_free_server()\n"); |
| 1002 | } |
| 1003 | |
| 1004 | /* |
| 1005 | * Create a version 2 or 3 volume record |
| 1006 | * - keyed on server and FSID |
| 1007 | */ |
\"Talpey, Thomas\ | 2283f8d | 2007-09-10 13:43:56 -0400 | [diff] [blame] | 1008 | struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data, |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1009 | struct nfs_fh *mntfh) |
| 1010 | { |
| 1011 | struct nfs_server *server; |
| 1012 | struct nfs_fattr fattr; |
| 1013 | int error; |
| 1014 | |
| 1015 | server = nfs_alloc_server(); |
| 1016 | if (!server) |
| 1017 | return ERR_PTR(-ENOMEM); |
| 1018 | |
| 1019 | /* Get a client representation */ |
| 1020 | error = nfs_init_server(server, data); |
| 1021 | if (error < 0) |
| 1022 | goto error; |
| 1023 | |
| 1024 | BUG_ON(!server->nfs_client); |
| 1025 | BUG_ON(!server->nfs_client->rpc_ops); |
| 1026 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); |
| 1027 | |
| 1028 | /* Probe the root fh to retrieve its FSID */ |
| 1029 | error = nfs_probe_fsinfo(server, mntfh, &fattr); |
| 1030 | if (error < 0) |
| 1031 | goto error; |
Trond Myklebust | 54af3bb | 2007-09-28 12:27:41 -0400 | [diff] [blame] | 1032 | if (server->nfs_client->rpc_ops->version == 3) { |
| 1033 | if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) |
| 1034 | server->namelen = NFS3_MAXNAMLEN; |
| 1035 | if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) |
| 1036 | server->caps |= NFS_CAP_READDIRPLUS; |
| 1037 | } else { |
| 1038 | if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) |
| 1039 | server->namelen = NFS2_MAXNAMLEN; |
| 1040 | } |
| 1041 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1042 | if (!(fattr.valid & NFS_ATTR_FATTR)) { |
| 1043 | error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr); |
| 1044 | if (error < 0) { |
| 1045 | dprintk("nfs_create_server: getattr error = %d\n", -error); |
| 1046 | goto error; |
| 1047 | } |
| 1048 | } |
| 1049 | memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid)); |
| 1050 | |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1051 | dprintk("Server FSID: %llx:%llx\n", |
| 1052 | (unsigned long long) server->fsid.major, |
| 1053 | (unsigned long long) server->fsid.minor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1054 | |
| 1055 | BUG_ON(!server->nfs_client); |
| 1056 | BUG_ON(!server->nfs_client->rpc_ops); |
| 1057 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); |
| 1058 | |
| 1059 | spin_lock(&nfs_client_lock); |
| 1060 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); |
| 1061 | list_add_tail(&server->master_link, &nfs_volume_list); |
| 1062 | spin_unlock(&nfs_client_lock); |
| 1063 | |
| 1064 | server->mount_time = jiffies; |
| 1065 | return server; |
| 1066 | |
| 1067 | error: |
| 1068 | nfs_free_server(server); |
| 1069 | return ERR_PTR(error); |
| 1070 | } |
| 1071 | |
| 1072 | #ifdef CONFIG_NFS_V4 |
| 1073 | /* |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1074 | * Initialize the minor version specific parts of an NFS4 client record |
| 1075 | */ |
| 1076 | static int nfs4_init_client_minor_version(struct nfs_client *clp) |
| 1077 | { |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame^] | 1078 | clp->cl_call_sync = _nfs4_call_sync; |
| 1079 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1080 | #if defined(CONFIG_NFS_V4_1) |
| 1081 | if (clp->cl_minorversion) { |
| 1082 | struct nfs4_session *session = NULL; |
| 1083 | /* |
| 1084 | * Create the session and mark it expired. |
| 1085 | * When a SEQUENCE operation encounters the expired session |
| 1086 | * it will do session recovery to initialize it. |
| 1087 | */ |
| 1088 | session = nfs4_alloc_session(clp); |
| 1089 | if (!session) |
| 1090 | return -ENOMEM; |
| 1091 | |
| 1092 | clp->cl_session = session; |
Andy Adamson | cccef3b | 2009-04-01 09:22:03 -0400 | [diff] [blame^] | 1093 | clp->cl_call_sync = _nfs4_call_sync_session; |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1094 | } |
| 1095 | #endif /* CONFIG_NFS_V4_1 */ |
| 1096 | |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1101 | * Initialise an NFS4 client record |
| 1102 | */ |
| 1103 | static int nfs4_init_client(struct nfs_client *clp, |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1104 | const struct rpc_timeout *timeparms, |
J. Bruce Fields | 7d9ac06 | 2006-10-19 23:28:39 -0700 | [diff] [blame] | 1105 | const char *ip_addr, |
Chuck Lever | d740351 | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 1106 | rpc_authflavor_t authflavour, |
| 1107 | int flags) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1108 | { |
| 1109 | int error; |
| 1110 | |
| 1111 | if (clp->cl_cons_state == NFS_CS_READY) { |
| 1112 | /* the client is initialised already */ |
| 1113 | dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | /* Check NFS protocol revision and initialize RPC op vector */ |
| 1118 | clp->rpc_ops = &nfs_v4_clientops; |
| 1119 | |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 1120 | error = nfs_create_rpc_client(clp, timeparms, authflavour, |
Chuck Lever | d740351 | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 1121 | 1, flags & NFS_MOUNT_NORESVPORT); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1122 | if (error < 0) |
| 1123 | goto error; |
J. Bruce Fields | 7d9ac06 | 2006-10-19 23:28:39 -0700 | [diff] [blame] | 1124 | memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1125 | |
| 1126 | error = nfs_idmap_new(clp); |
| 1127 | if (error < 0) { |
| 1128 | dprintk("%s: failed to create idmapper. Error = %d\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 1129 | __func__, error); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1130 | goto error; |
| 1131 | } |
Trond Myklebust | 9c5bf38 | 2006-08-22 20:06:14 -0400 | [diff] [blame] | 1132 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1133 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1134 | error = nfs4_init_client_minor_version(clp); |
| 1135 | if (error < 0) |
| 1136 | goto error; |
| 1137 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1138 | nfs_mark_client_ready(clp, NFS_CS_READY); |
| 1139 | return 0; |
| 1140 | |
| 1141 | error: |
| 1142 | nfs_mark_client_ready(clp, error); |
| 1143 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); |
| 1144 | return error; |
| 1145 | } |
| 1146 | |
| 1147 | /* |
| 1148 | * Set up an NFS4 client |
| 1149 | */ |
| 1150 | static int nfs4_set_client(struct nfs_server *server, |
Chuck Lever | dcecae0 | 2007-12-10 14:58:59 -0500 | [diff] [blame] | 1151 | const char *hostname, |
| 1152 | const struct sockaddr *addr, |
| 1153 | const size_t addrlen, |
J. Bruce Fields | 7d9ac06 | 2006-10-19 23:28:39 -0700 | [diff] [blame] | 1154 | const char *ip_addr, |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1155 | rpc_authflavor_t authflavour, |
Benny Halevy | 94a417f | 2009-04-01 09:21:49 -0400 | [diff] [blame] | 1156 | int proto, const struct rpc_timeout *timeparms, |
| 1157 | u32 minorversion) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1158 | { |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 1159 | struct nfs_client_initdata cl_init = { |
| 1160 | .hostname = hostname, |
Chuck Lever | dcecae0 | 2007-12-10 14:58:59 -0500 | [diff] [blame] | 1161 | .addr = addr, |
| 1162 | .addrlen = addrlen, |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 1163 | .rpc_ops = &nfs_v4_clientops, |
Trond Myklebust | 59dca3b | 2008-01-03 16:29:06 -0500 | [diff] [blame] | 1164 | .proto = proto, |
Benny Halevy | 5aae4a9 | 2009-04-01 09:21:50 -0400 | [diff] [blame] | 1165 | .minorversion = minorversion, |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 1166 | }; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1167 | struct nfs_client *clp; |
| 1168 | int error; |
| 1169 | |
| 1170 | dprintk("--> nfs4_set_client()\n"); |
| 1171 | |
| 1172 | /* Allocate or find a client reference we can use */ |
Trond Myklebust | 3a49802 | 2007-12-14 14:56:04 -0500 | [diff] [blame] | 1173 | clp = nfs_get_client(&cl_init); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1174 | if (IS_ERR(clp)) { |
| 1175 | error = PTR_ERR(clp); |
| 1176 | goto error; |
| 1177 | } |
Chuck Lever | d740351 | 2008-12-23 15:21:37 -0500 | [diff] [blame] | 1178 | error = nfs4_init_client(clp, timeparms, ip_addr, authflavour, |
| 1179 | server->flags); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1180 | if (error < 0) |
| 1181 | goto error_put; |
| 1182 | |
| 1183 | server->nfs_client = clp; |
| 1184 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); |
| 1185 | return 0; |
| 1186 | |
| 1187 | error_put: |
| 1188 | nfs_put_client(clp); |
| 1189 | error: |
| 1190 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); |
| 1191 | return error; |
| 1192 | } |
| 1193 | |
| 1194 | /* |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1195 | * Initialize a session. |
| 1196 | * Note: save the mount rsize and wsize for create_server negotiation. |
| 1197 | */ |
| 1198 | static void nfs4_init_session(struct nfs_client *clp, |
| 1199 | unsigned int wsize, unsigned int rsize) |
| 1200 | { |
| 1201 | #if defined(CONFIG_NFS_V4_1) |
| 1202 | if (nfs4_has_session(clp)) { |
| 1203 | clp->cl_session->fc_attrs.max_rqst_sz = wsize; |
| 1204 | clp->cl_session->fc_attrs.max_resp_sz = rsize; |
| 1205 | } |
| 1206 | #endif /* CONFIG_NFS_V4_1 */ |
| 1207 | } |
| 1208 | |
| 1209 | /* |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1210 | * Create a version 4 volume record |
| 1211 | */ |
| 1212 | static int nfs4_init_server(struct nfs_server *server, |
\"Talpey, Thomas\ | 91ea40b | 2007-09-10 13:44:33 -0400 | [diff] [blame] | 1213 | const struct nfs_parsed_mount_data *data) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1214 | { |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1215 | struct rpc_timeout timeparms; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1216 | int error; |
| 1217 | |
| 1218 | dprintk("--> nfs4_init_server()\n"); |
| 1219 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1220 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, |
| 1221 | data->timeo, data->retrans); |
| 1222 | |
Chuck Lever | 542fcc3 | 2008-12-23 15:21:36 -0500 | [diff] [blame] | 1223 | /* Initialise the client representation from the mount data */ |
| 1224 | server->flags = data->flags; |
| 1225 | server->caps |= NFS_CAP_ATOMIC_OPEN; |
David Howells | b797cac | 2009-04-03 16:42:48 +0100 | [diff] [blame] | 1226 | server->options = data->options; |
Chuck Lever | 542fcc3 | 2008-12-23 15:21:36 -0500 | [diff] [blame] | 1227 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1228 | /* Get a client record */ |
| 1229 | error = nfs4_set_client(server, |
| 1230 | data->nfs_server.hostname, |
| 1231 | (const struct sockaddr *)&data->nfs_server.address, |
| 1232 | data->nfs_server.addrlen, |
| 1233 | data->client_address, |
| 1234 | data->auth_flavors[0], |
| 1235 | data->nfs_server.protocol, |
Benny Halevy | 94a417f | 2009-04-01 09:21:49 -0400 | [diff] [blame] | 1236 | &timeparms, |
| 1237 | data->minorversion); |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1238 | if (error < 0) |
| 1239 | goto error; |
| 1240 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1241 | if (data->rsize) |
| 1242 | server->rsize = nfs_block_size(data->rsize, NULL); |
| 1243 | if (data->wsize) |
| 1244 | server->wsize = nfs_block_size(data->wsize, NULL); |
| 1245 | |
| 1246 | server->acregmin = data->acregmin * HZ; |
| 1247 | server->acregmax = data->acregmax * HZ; |
| 1248 | server->acdirmin = data->acdirmin * HZ; |
| 1249 | server->acdirmax = data->acdirmax * HZ; |
| 1250 | |
Chuck Lever | f22d6d7 | 2008-03-14 14:10:22 -0400 | [diff] [blame] | 1251 | server->port = data->nfs_server.port; |
| 1252 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1253 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1254 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1255 | error: |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1256 | /* Done */ |
| 1257 | dprintk("<-- nfs4_init_server() = %d\n", error); |
| 1258 | return error; |
| 1259 | } |
| 1260 | |
| 1261 | /* |
| 1262 | * Create a version 4 volume record |
| 1263 | * - keyed on server and FSID |
| 1264 | */ |
\"Talpey, Thomas\ | 91ea40b | 2007-09-10 13:44:33 -0400 | [diff] [blame] | 1265 | struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1266 | struct nfs_fh *mntfh) |
| 1267 | { |
| 1268 | struct nfs_fattr fattr; |
| 1269 | struct nfs_server *server; |
| 1270 | int error; |
| 1271 | |
| 1272 | dprintk("--> nfs4_create_server()\n"); |
| 1273 | |
| 1274 | server = nfs_alloc_server(); |
| 1275 | if (!server) |
| 1276 | return ERR_PTR(-ENOMEM); |
| 1277 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1278 | /* set up the general RPC client */ |
\"Talpey, Thomas\ | 91ea40b | 2007-09-10 13:44:33 -0400 | [diff] [blame] | 1279 | error = nfs4_init_server(server, data); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1280 | if (error < 0) |
| 1281 | goto error; |
| 1282 | |
| 1283 | BUG_ON(!server->nfs_client); |
| 1284 | BUG_ON(!server->nfs_client->rpc_ops); |
| 1285 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); |
| 1286 | |
Andy Adamson | 557134a | 2009-04-01 09:21:53 -0400 | [diff] [blame] | 1287 | nfs4_init_session(server->nfs_client, server->wsize, server->rsize); |
| 1288 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1289 | /* Probe the root fh to retrieve its FSID */ |
\"Talpey, Thomas\ | 91ea40b | 2007-09-10 13:44:33 -0400 | [diff] [blame] | 1290 | error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1291 | if (error < 0) |
| 1292 | goto error; |
| 1293 | |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1294 | dprintk("Server FSID: %llx:%llx\n", |
| 1295 | (unsigned long long) server->fsid.major, |
| 1296 | (unsigned long long) server->fsid.minor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1297 | dprintk("Mount FH: %d\n", mntfh->size); |
| 1298 | |
| 1299 | error = nfs_probe_fsinfo(server, mntfh, &fattr); |
| 1300 | if (error < 0) |
| 1301 | goto error; |
| 1302 | |
Trond Myklebust | 54af3bb | 2007-09-28 12:27:41 -0400 | [diff] [blame] | 1303 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
| 1304 | server->namelen = NFS4_MAXNAMLEN; |
| 1305 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1306 | BUG_ON(!server->nfs_client); |
| 1307 | BUG_ON(!server->nfs_client->rpc_ops); |
| 1308 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); |
| 1309 | |
| 1310 | spin_lock(&nfs_client_lock); |
| 1311 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); |
| 1312 | list_add_tail(&server->master_link, &nfs_volume_list); |
| 1313 | spin_unlock(&nfs_client_lock); |
| 1314 | |
| 1315 | server->mount_time = jiffies; |
| 1316 | dprintk("<-- nfs4_create_server() = %p\n", server); |
| 1317 | return server; |
| 1318 | |
| 1319 | error: |
| 1320 | nfs_free_server(server); |
| 1321 | dprintk("<-- nfs4_create_server() = error %d\n", error); |
| 1322 | return ERR_PTR(error); |
| 1323 | } |
| 1324 | |
| 1325 | /* |
| 1326 | * Create an NFS4 referral server record |
| 1327 | */ |
| 1328 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, |
Trond Myklebust | f2d0d85 | 2007-02-02 14:46:09 -0800 | [diff] [blame] | 1329 | struct nfs_fh *mntfh) |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1330 | { |
| 1331 | struct nfs_client *parent_client; |
| 1332 | struct nfs_server *server, *parent_server; |
| 1333 | struct nfs_fattr fattr; |
| 1334 | int error; |
| 1335 | |
| 1336 | dprintk("--> nfs4_create_referral_server()\n"); |
| 1337 | |
| 1338 | server = nfs_alloc_server(); |
| 1339 | if (!server) |
| 1340 | return ERR_PTR(-ENOMEM); |
| 1341 | |
| 1342 | parent_server = NFS_SB(data->sb); |
| 1343 | parent_client = parent_server->nfs_client; |
| 1344 | |
Chuck Lever | 542fcc3 | 2008-12-23 15:21:36 -0500 | [diff] [blame] | 1345 | /* Initialise the client representation from the parent server */ |
| 1346 | nfs_server_copy_userdata(server, parent_server); |
| 1347 | server->caps |= NFS_CAP_ATOMIC_OPEN; |
| 1348 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1349 | /* Get a client representation. |
| 1350 | * Note: NFSv4 always uses TCP, */ |
Chuck Lever | dcecae0 | 2007-12-10 14:58:59 -0500 | [diff] [blame] | 1351 | error = nfs4_set_client(server, data->hostname, |
Chuck Lever | 6677d09 | 2007-12-10 14:59:06 -0500 | [diff] [blame] | 1352 | data->addr, |
| 1353 | data->addrlen, |
Chuck Lever | dcecae0 | 2007-12-10 14:58:59 -0500 | [diff] [blame] | 1354 | parent_client->cl_ipaddr, |
| 1355 | data->authflavor, |
| 1356 | parent_server->client->cl_xprt->prot, |
Benny Halevy | 94a417f | 2009-04-01 09:21:49 -0400 | [diff] [blame] | 1357 | parent_server->client->cl_timeout, |
| 1358 | parent_client->cl_minorversion); |
andros@citi.umich.edu | 297de4f | 2006-08-29 12:19:41 -0400 | [diff] [blame] | 1359 | if (error < 0) |
| 1360 | goto error; |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1361 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1362 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1363 | if (error < 0) |
| 1364 | goto error; |
| 1365 | |
| 1366 | BUG_ON(!server->nfs_client); |
| 1367 | BUG_ON(!server->nfs_client->rpc_ops); |
| 1368 | BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops); |
| 1369 | |
Trond Myklebust | f2d0d85 | 2007-02-02 14:46:09 -0800 | [diff] [blame] | 1370 | /* Probe the root fh to retrieve its FSID and filehandle */ |
| 1371 | error = nfs4_path_walk(server, mntfh, data->mnt_path); |
| 1372 | if (error < 0) |
| 1373 | goto error; |
| 1374 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1375 | /* probe the filesystem info for this server filesystem */ |
Trond Myklebust | f2d0d85 | 2007-02-02 14:46:09 -0800 | [diff] [blame] | 1376 | error = nfs_probe_fsinfo(server, mntfh, &fattr); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1377 | if (error < 0) |
| 1378 | goto error; |
| 1379 | |
Trond Myklebust | 54af3bb | 2007-09-28 12:27:41 -0400 | [diff] [blame] | 1380 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
| 1381 | server->namelen = NFS4_MAXNAMLEN; |
| 1382 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1383 | dprintk("Referral FSID: %llx:%llx\n", |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1384 | (unsigned long long) server->fsid.major, |
| 1385 | (unsigned long long) server->fsid.minor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1386 | |
| 1387 | spin_lock(&nfs_client_lock); |
| 1388 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); |
| 1389 | list_add_tail(&server->master_link, &nfs_volume_list); |
| 1390 | spin_unlock(&nfs_client_lock); |
| 1391 | |
| 1392 | server->mount_time = jiffies; |
| 1393 | |
| 1394 | dprintk("<-- nfs_create_referral_server() = %p\n", server); |
| 1395 | return server; |
| 1396 | |
| 1397 | error: |
| 1398 | nfs_free_server(server); |
| 1399 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); |
| 1400 | return ERR_PTR(error); |
| 1401 | } |
| 1402 | |
| 1403 | #endif /* CONFIG_NFS_V4 */ |
| 1404 | |
| 1405 | /* |
| 1406 | * Clone an NFS2, NFS3 or NFS4 server record |
| 1407 | */ |
| 1408 | struct nfs_server *nfs_clone_server(struct nfs_server *source, |
| 1409 | struct nfs_fh *fh, |
| 1410 | struct nfs_fattr *fattr) |
| 1411 | { |
| 1412 | struct nfs_server *server; |
| 1413 | struct nfs_fattr fattr_fsinfo; |
| 1414 | int error; |
| 1415 | |
| 1416 | dprintk("--> nfs_clone_server(,%llx:%llx,)\n", |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1417 | (unsigned long long) fattr->fsid.major, |
| 1418 | (unsigned long long) fattr->fsid.minor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1419 | |
| 1420 | server = nfs_alloc_server(); |
| 1421 | if (!server) |
| 1422 | return ERR_PTR(-ENOMEM); |
| 1423 | |
| 1424 | /* Copy data from the source */ |
| 1425 | server->nfs_client = source->nfs_client; |
| 1426 | atomic_inc(&server->nfs_client->cl_count); |
| 1427 | nfs_server_copy_userdata(server, source); |
| 1428 | |
| 1429 | server->fsid = fattr->fsid; |
| 1430 | |
Trond Myklebust | 3317023 | 2007-12-20 16:03:59 -0500 | [diff] [blame] | 1431 | error = nfs_init_server_rpcclient(server, |
| 1432 | source->client->cl_timeout, |
| 1433 | source->client->cl_auth->au_flavor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1434 | if (error < 0) |
| 1435 | goto out_free_server; |
| 1436 | if (!IS_ERR(source->client_acl)) |
| 1437 | nfs_init_server_aclclient(server); |
| 1438 | |
| 1439 | /* probe the filesystem info for this server filesystem */ |
| 1440 | error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo); |
| 1441 | if (error < 0) |
| 1442 | goto out_free_server; |
| 1443 | |
Trond Myklebust | 54af3bb | 2007-09-28 12:27:41 -0400 | [diff] [blame] | 1444 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
| 1445 | server->namelen = NFS4_MAXNAMLEN; |
| 1446 | |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1447 | dprintk("Cloned FSID: %llx:%llx\n", |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1448 | (unsigned long long) server->fsid.major, |
| 1449 | (unsigned long long) server->fsid.minor); |
David Howells | 54ceac4 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1450 | |
| 1451 | error = nfs_start_lockd(server); |
| 1452 | if (error < 0) |
| 1453 | goto out_free_server; |
| 1454 | |
| 1455 | spin_lock(&nfs_client_lock); |
| 1456 | list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks); |
| 1457 | list_add_tail(&server->master_link, &nfs_volume_list); |
| 1458 | spin_unlock(&nfs_client_lock); |
| 1459 | |
| 1460 | server->mount_time = jiffies; |
| 1461 | |
| 1462 | dprintk("<-- nfs_clone_server() = %p\n", server); |
| 1463 | return server; |
| 1464 | |
| 1465 | out_free_server: |
| 1466 | nfs_free_server(server); |
| 1467 | dprintk("<-- nfs_clone_server() = error %d\n", error); |
| 1468 | return ERR_PTR(error); |
| 1469 | } |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1470 | |
| 1471 | #ifdef CONFIG_PROC_FS |
| 1472 | static struct proc_dir_entry *proc_fs_nfs; |
| 1473 | |
| 1474 | static int nfs_server_list_open(struct inode *inode, struct file *file); |
| 1475 | static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); |
| 1476 | static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); |
| 1477 | static void nfs_server_list_stop(struct seq_file *p, void *v); |
| 1478 | static int nfs_server_list_show(struct seq_file *m, void *v); |
| 1479 | |
| 1480 | static struct seq_operations nfs_server_list_ops = { |
| 1481 | .start = nfs_server_list_start, |
| 1482 | .next = nfs_server_list_next, |
| 1483 | .stop = nfs_server_list_stop, |
| 1484 | .show = nfs_server_list_show, |
| 1485 | }; |
| 1486 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1487 | static const struct file_operations nfs_server_list_fops = { |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1488 | .open = nfs_server_list_open, |
| 1489 | .read = seq_read, |
| 1490 | .llseek = seq_lseek, |
| 1491 | .release = seq_release, |
Denis V. Lunev | 34b3723 | 2008-04-29 01:02:07 -0700 | [diff] [blame] | 1492 | .owner = THIS_MODULE, |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1493 | }; |
| 1494 | |
| 1495 | static int nfs_volume_list_open(struct inode *inode, struct file *file); |
| 1496 | static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); |
| 1497 | static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); |
| 1498 | static void nfs_volume_list_stop(struct seq_file *p, void *v); |
| 1499 | static int nfs_volume_list_show(struct seq_file *m, void *v); |
| 1500 | |
| 1501 | static struct seq_operations nfs_volume_list_ops = { |
| 1502 | .start = nfs_volume_list_start, |
| 1503 | .next = nfs_volume_list_next, |
| 1504 | .stop = nfs_volume_list_stop, |
| 1505 | .show = nfs_volume_list_show, |
| 1506 | }; |
| 1507 | |
Arjan van de Ven | 00977a5 | 2007-02-12 00:55:34 -0800 | [diff] [blame] | 1508 | static const struct file_operations nfs_volume_list_fops = { |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1509 | .open = nfs_volume_list_open, |
| 1510 | .read = seq_read, |
| 1511 | .llseek = seq_lseek, |
| 1512 | .release = seq_release, |
Denis V. Lunev | 34b3723 | 2008-04-29 01:02:07 -0700 | [diff] [blame] | 1513 | .owner = THIS_MODULE, |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1514 | }; |
| 1515 | |
| 1516 | /* |
| 1517 | * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which |
| 1518 | * we're dealing |
| 1519 | */ |
| 1520 | static int nfs_server_list_open(struct inode *inode, struct file *file) |
| 1521 | { |
| 1522 | struct seq_file *m; |
| 1523 | int ret; |
| 1524 | |
| 1525 | ret = seq_open(file, &nfs_server_list_ops); |
| 1526 | if (ret < 0) |
| 1527 | return ret; |
| 1528 | |
| 1529 | m = file->private_data; |
| 1530 | m->private = PDE(inode)->data; |
| 1531 | |
| 1532 | return 0; |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * set up the iterator to start reading from the server list and return the first item |
| 1537 | */ |
| 1538 | static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) |
| 1539 | { |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1540 | /* lock the list against modification */ |
| 1541 | spin_lock(&nfs_client_lock); |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1542 | return seq_list_start_head(&nfs_client_list, *_pos); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | /* |
| 1546 | * move to next server |
| 1547 | */ |
| 1548 | static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) |
| 1549 | { |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1550 | return seq_list_next(v, &nfs_client_list, pos); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1551 | } |
| 1552 | |
| 1553 | /* |
| 1554 | * clean up after reading from the transports list |
| 1555 | */ |
| 1556 | static void nfs_server_list_stop(struct seq_file *p, void *v) |
| 1557 | { |
| 1558 | spin_unlock(&nfs_client_lock); |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * display a header line followed by a load of call lines |
| 1563 | */ |
| 1564 | static int nfs_server_list_show(struct seq_file *m, void *v) |
| 1565 | { |
| 1566 | struct nfs_client *clp; |
| 1567 | |
| 1568 | /* display header on line 1 */ |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1569 | if (v == &nfs_client_list) { |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1570 | seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); |
| 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | /* display one transport per line on subsequent lines */ |
| 1575 | clp = list_entry(v, struct nfs_client, cl_share_link); |
| 1576 | |
Chuck Lever | 5d8515c | 2007-12-10 14:57:16 -0500 | [diff] [blame] | 1577 | seq_printf(m, "v%u %s %s %3d %s\n", |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 1578 | clp->rpc_ops->version, |
Chuck Lever | 5d8515c | 2007-12-10 14:57:16 -0500 | [diff] [blame] | 1579 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), |
| 1580 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1581 | atomic_read(&clp->cl_count), |
| 1582 | clp->cl_hostname); |
| 1583 | |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
| 1587 | /* |
| 1588 | * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes |
| 1589 | */ |
| 1590 | static int nfs_volume_list_open(struct inode *inode, struct file *file) |
| 1591 | { |
| 1592 | struct seq_file *m; |
| 1593 | int ret; |
| 1594 | |
| 1595 | ret = seq_open(file, &nfs_volume_list_ops); |
| 1596 | if (ret < 0) |
| 1597 | return ret; |
| 1598 | |
| 1599 | m = file->private_data; |
| 1600 | m->private = PDE(inode)->data; |
| 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | /* |
| 1606 | * set up the iterator to start reading from the volume list and return the first item |
| 1607 | */ |
| 1608 | static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) |
| 1609 | { |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1610 | /* lock the list against modification */ |
| 1611 | spin_lock(&nfs_client_lock); |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1612 | return seq_list_start_head(&nfs_volume_list, *_pos); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * move to next volume |
| 1617 | */ |
| 1618 | static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) |
| 1619 | { |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1620 | return seq_list_next(v, &nfs_volume_list, pos); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | /* |
| 1624 | * clean up after reading from the transports list |
| 1625 | */ |
| 1626 | static void nfs_volume_list_stop(struct seq_file *p, void *v) |
| 1627 | { |
| 1628 | spin_unlock(&nfs_client_lock); |
| 1629 | } |
| 1630 | |
| 1631 | /* |
| 1632 | * display a header line followed by a load of call lines |
| 1633 | */ |
| 1634 | static int nfs_volume_list_show(struct seq_file *m, void *v) |
| 1635 | { |
| 1636 | struct nfs_server *server; |
| 1637 | struct nfs_client *clp; |
| 1638 | char dev[8], fsid[17]; |
| 1639 | |
| 1640 | /* display header on line 1 */ |
Pavel Emelianov | 259902e | 2007-07-15 23:39:56 -0700 | [diff] [blame] | 1641 | if (v == &nfs_volume_list) { |
David Howells | 5d1acff | 2009-04-03 16:42:47 +0100 | [diff] [blame] | 1642 | seq_puts(m, "NV SERVER PORT DEV FSID FSC\n"); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1643 | return 0; |
| 1644 | } |
| 1645 | /* display one transport per line on subsequent lines */ |
| 1646 | server = list_entry(v, struct nfs_server, master_link); |
| 1647 | clp = server->nfs_client; |
| 1648 | |
| 1649 | snprintf(dev, 8, "%u:%u", |
| 1650 | MAJOR(server->s_dev), MINOR(server->s_dev)); |
| 1651 | |
| 1652 | snprintf(fsid, 17, "%llx:%llx", |
David Howells | 6daabf1 | 2006-08-24 15:44:16 -0400 | [diff] [blame] | 1653 | (unsigned long long) server->fsid.major, |
| 1654 | (unsigned long long) server->fsid.minor); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1655 | |
David Howells | 5d1acff | 2009-04-03 16:42:47 +0100 | [diff] [blame] | 1656 | seq_printf(m, "v%u %s %s %-7s %-17s %s\n", |
Trond Myklebust | 40c55319 | 2007-12-14 14:56:07 -0500 | [diff] [blame] | 1657 | clp->rpc_ops->version, |
Chuck Lever | 5d8515c | 2007-12-10 14:57:16 -0500 | [diff] [blame] | 1658 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), |
| 1659 | rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1660 | dev, |
David Howells | 5d1acff | 2009-04-03 16:42:47 +0100 | [diff] [blame] | 1661 | fsid, |
| 1662 | nfs_server_fscache_state(server)); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1663 | |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /* |
| 1668 | * initialise the /proc/fs/nfsfs/ directory |
| 1669 | */ |
| 1670 | int __init nfs_fs_proc_init(void) |
| 1671 | { |
| 1672 | struct proc_dir_entry *p; |
| 1673 | |
Alexey Dobriyan | 36a5aeb | 2008-04-29 01:01:42 -0700 | [diff] [blame] | 1674 | proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1675 | if (!proc_fs_nfs) |
| 1676 | goto error_0; |
| 1677 | |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1678 | /* a file of servers with which we're dealing */ |
Denis V. Lunev | 34b3723 | 2008-04-29 01:02:07 -0700 | [diff] [blame] | 1679 | p = proc_create("servers", S_IFREG|S_IRUGO, |
| 1680 | proc_fs_nfs, &nfs_server_list_fops); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1681 | if (!p) |
| 1682 | goto error_1; |
| 1683 | |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1684 | /* a file of volumes that we have mounted */ |
Denis V. Lunev | 34b3723 | 2008-04-29 01:02:07 -0700 | [diff] [blame] | 1685 | p = proc_create("volumes", S_IFREG|S_IRUGO, |
| 1686 | proc_fs_nfs, &nfs_volume_list_fops); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1687 | if (!p) |
| 1688 | goto error_2; |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1689 | return 0; |
| 1690 | |
| 1691 | error_2: |
| 1692 | remove_proc_entry("servers", proc_fs_nfs); |
| 1693 | error_1: |
Alexey Dobriyan | 36a5aeb | 2008-04-29 01:01:42 -0700 | [diff] [blame] | 1694 | remove_proc_entry("fs/nfsfs", NULL); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1695 | error_0: |
| 1696 | return -ENOMEM; |
| 1697 | } |
| 1698 | |
| 1699 | /* |
| 1700 | * clean up the /proc/fs/nfsfs/ directory |
| 1701 | */ |
| 1702 | void nfs_fs_proc_exit(void) |
| 1703 | { |
| 1704 | remove_proc_entry("volumes", proc_fs_nfs); |
| 1705 | remove_proc_entry("servers", proc_fs_nfs); |
Alexey Dobriyan | 36a5aeb | 2008-04-29 01:01:42 -0700 | [diff] [blame] | 1706 | remove_proc_entry("fs/nfsfs", NULL); |
David Howells | 6aaca56 | 2006-08-22 20:06:13 -0400 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | #endif /* CONFIG_PROC_FS */ |