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