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