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