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