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