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