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