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