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