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