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