[PATCH] sched: remove dead code from task_stime()
[usb.git] / fs / nfs / client.c
blobccb455053ee48090ab21b4bdd55294d3f4694010
1 /* client.c: NFS client sharing and management code
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
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/seq_file.h>
31 #include <linux/mount.h>
32 #include <linux/nfs_idmap.h>
33 #include <linux/vfs.h>
34 #include <linux/inet.h>
35 #include <linux/nfs_xdr.h>
37 #include <asm/system.h>
39 #include "nfs4_fs.h"
40 #include "callback.h"
41 #include "delegation.h"
42 #include "iostat.h"
43 #include "internal.h"
45 #define NFSDBG_FACILITY NFSDBG_CLIENT
47 static DEFINE_SPINLOCK(nfs_client_lock);
48 static LIST_HEAD(nfs_client_list);
49 static LIST_HEAD(nfs_volume_list);
50 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
53 * RPC cruft for NFS
55 static struct rpc_version *nfs_version[5] = {
56 [2] = &nfs_version2,
57 #ifdef CONFIG_NFS_V3
58 [3] = &nfs_version3,
59 #endif
60 #ifdef CONFIG_NFS_V4
61 [4] = &nfs_version4,
62 #endif
65 struct rpc_program nfs_program = {
66 .name = "nfs",
67 .number = NFS_PROGRAM,
68 .nrvers = ARRAY_SIZE(nfs_version),
69 .version = nfs_version,
70 .stats = &nfs_rpcstat,
71 .pipe_dir_name = "/nfs",
74 struct rpc_stat nfs_rpcstat = {
75 .program = &nfs_program
79 #ifdef CONFIG_NFS_V3_ACL
80 static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
81 static struct rpc_version * nfsacl_version[] = {
82 [3] = &nfsacl_version3,
85 struct rpc_program nfsacl_program = {
86 .name = "nfsacl",
87 .number = NFS_ACL_PROGRAM,
88 .nrvers = ARRAY_SIZE(nfsacl_version),
89 .version = nfsacl_version,
90 .stats = &nfsacl_rpcstat,
92 #endif /* CONFIG_NFS_V3_ACL */
95 * Allocate a shared client record
97 * Since these are allocated/deallocated very rarely, we don't
98 * bother putting them in a slab cache...
100 static struct nfs_client *nfs_alloc_client(const char *hostname,
101 const struct sockaddr_in *addr,
102 int nfsversion)
104 struct nfs_client *clp;
106 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
107 goto error_0;
109 if (nfsversion == 4) {
110 if (nfs_callback_up() < 0)
111 goto error_2;
112 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
115 atomic_set(&clp->cl_count, 1);
116 clp->cl_cons_state = NFS_CS_INITING;
118 clp->cl_nfsversion = nfsversion;
119 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
121 if (hostname) {
122 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
123 if (!clp->cl_hostname)
124 goto error_3;
127 INIT_LIST_HEAD(&clp->cl_superblocks);
128 clp->cl_rpcclient = ERR_PTR(-EINVAL);
130 #ifdef CONFIG_NFS_V4
131 init_rwsem(&clp->cl_sem);
132 INIT_LIST_HEAD(&clp->cl_delegations);
133 spin_lock_init(&clp->cl_lock);
134 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
135 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
136 clp->cl_boot_time = CURRENT_TIME;
137 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
138 #endif
140 return clp;
142 error_3:
143 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
144 nfs_callback_down();
145 error_2:
146 kfree(clp);
147 error_0:
148 return NULL;
151 static void nfs4_shutdown_client(struct nfs_client *clp)
153 #ifdef CONFIG_NFS_V4
154 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
155 nfs4_kill_renewd(clp);
156 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
157 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
158 nfs_idmap_delete(clp);
159 #endif
163 * Destroy a shared client record
165 static void nfs_free_client(struct nfs_client *clp)
167 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
169 nfs4_shutdown_client(clp);
171 /* -EIO all pending I/O */
172 if (!IS_ERR(clp->cl_rpcclient))
173 rpc_shutdown_client(clp->cl_rpcclient);
175 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
176 nfs_callback_down();
178 kfree(clp->cl_hostname);
179 kfree(clp);
181 dprintk("<-- nfs_free_client()\n");
185 * Release a reference to a shared client record
187 void nfs_put_client(struct nfs_client *clp)
189 if (!clp)
190 return;
192 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
194 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
195 list_del(&clp->cl_share_link);
196 spin_unlock(&nfs_client_lock);
198 BUG_ON(!list_empty(&clp->cl_superblocks));
200 nfs_free_client(clp);
205 * Find a client by address
206 * - caller must hold nfs_client_lock
208 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
210 struct nfs_client *clp;
212 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
213 /* Don't match clients that failed to initialise properly */
214 if (clp->cl_cons_state < 0)
215 continue;
217 /* Different NFS versions cannot share the same nfs_client */
218 if (clp->cl_nfsversion != nfsversion)
219 continue;
221 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
222 sizeof(clp->cl_addr.sin_addr)) != 0)
223 continue;
225 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
226 goto found;
229 return NULL;
231 found:
232 atomic_inc(&clp->cl_count);
233 return clp;
237 * Find a client by IP address and protocol version
238 * - returns NULL if no such client
240 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
242 struct nfs_client *clp;
244 spin_lock(&nfs_client_lock);
245 clp = __nfs_find_client(addr, nfsversion, 0);
246 spin_unlock(&nfs_client_lock);
247 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
248 nfs_put_client(clp);
249 clp = NULL;
251 return clp;
255 * Look up a client by IP address and protocol version
256 * - creates a new record if one doesn't yet exist
258 static struct nfs_client *nfs_get_client(const char *hostname,
259 const struct sockaddr_in *addr,
260 int nfsversion)
262 struct nfs_client *clp, *new = NULL;
263 int error;
265 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
266 hostname ?: "", NIPQUAD(addr->sin_addr),
267 addr->sin_port, nfsversion);
269 /* see if the client already exists */
270 do {
271 spin_lock(&nfs_client_lock);
273 clp = __nfs_find_client(addr, nfsversion, 1);
274 if (clp)
275 goto found_client;
276 if (new)
277 goto install_client;
279 spin_unlock(&nfs_client_lock);
281 new = nfs_alloc_client(hostname, addr, nfsversion);
282 } while (new);
284 return ERR_PTR(-ENOMEM);
286 /* install a new client and return with it unready */
287 install_client:
288 clp = new;
289 list_add(&clp->cl_share_link, &nfs_client_list);
290 spin_unlock(&nfs_client_lock);
291 dprintk("--> nfs_get_client() = %p [new]\n", clp);
292 return clp;
294 /* found an existing client
295 * - make sure it's ready before returning
297 found_client:
298 spin_unlock(&nfs_client_lock);
300 if (new)
301 nfs_free_client(new);
303 error = wait_event_interruptible(nfs_client_active_wq,
304 clp->cl_cons_state != NFS_CS_INITING);
305 if (error < 0) {
306 nfs_put_client(clp);
307 return ERR_PTR(-ERESTARTSYS);
310 if (clp->cl_cons_state < NFS_CS_READY) {
311 error = clp->cl_cons_state;
312 nfs_put_client(clp);
313 return ERR_PTR(error);
316 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
318 dprintk("--> nfs_get_client() = %p [share]\n", clp);
319 return clp;
323 * Mark a server as ready or failed
325 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
327 clp->cl_cons_state = state;
328 wake_up_all(&nfs_client_active_wq);
332 * Initialise the timeout values for a connection
334 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
335 unsigned int timeo, unsigned int retrans)
337 to->to_initval = timeo * HZ / 10;
338 to->to_retries = retrans;
339 if (!to->to_retries)
340 to->to_retries = 2;
342 switch (proto) {
343 case IPPROTO_TCP:
344 if (!to->to_initval)
345 to->to_initval = 60 * HZ;
346 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
347 to->to_initval = NFS_MAX_TCP_TIMEOUT;
348 to->to_increment = to->to_initval;
349 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
350 to->to_exponential = 0;
351 break;
352 case IPPROTO_UDP:
353 default:
354 if (!to->to_initval)
355 to->to_initval = 11 * HZ / 10;
356 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
357 to->to_initval = NFS_MAX_UDP_TIMEOUT;
358 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
359 to->to_exponential = 1;
360 break;
365 * Create an RPC client handle
367 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
368 unsigned int timeo,
369 unsigned int retrans,
370 rpc_authflavor_t flavor,
371 int flags)
373 struct rpc_timeout timeparms;
374 struct rpc_clnt *clnt = NULL;
375 struct rpc_create_args args = {
376 .protocol = proto,
377 .address = (struct sockaddr *)&clp->cl_addr,
378 .addrsize = sizeof(clp->cl_addr),
379 .timeout = &timeparms,
380 .servername = clp->cl_hostname,
381 .program = &nfs_program,
382 .version = clp->rpc_ops->version,
383 .authflavor = flavor,
384 .flags = flags,
387 if (!IS_ERR(clp->cl_rpcclient))
388 return 0;
390 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
391 clp->retrans_timeo = timeparms.to_initval;
392 clp->retrans_count = timeparms.to_retries;
394 clnt = rpc_create(&args);
395 if (IS_ERR(clnt)) {
396 dprintk("%s: cannot create RPC client. Error = %ld\n",
397 __FUNCTION__, PTR_ERR(clnt));
398 return PTR_ERR(clnt);
401 clp->cl_rpcclient = clnt;
402 return 0;
406 * Version 2 or 3 client destruction
408 static void nfs_destroy_server(struct nfs_server *server)
410 if (!IS_ERR(server->client_acl))
411 rpc_shutdown_client(server->client_acl);
413 if (!(server->flags & NFS_MOUNT_NONLM))
414 lockd_down(); /* release rpc.lockd */
418 * Version 2 or 3 lockd setup
420 static int nfs_start_lockd(struct nfs_server *server)
422 int error = 0;
424 if (server->nfs_client->cl_nfsversion > 3)
425 goto out;
426 if (server->flags & NFS_MOUNT_NONLM)
427 goto out;
428 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
429 IPPROTO_TCP : IPPROTO_UDP);
430 if (error < 0)
431 server->flags |= NFS_MOUNT_NONLM;
432 else
433 server->destroy = nfs_destroy_server;
434 out:
435 return error;
439 * Initialise an NFSv3 ACL client connection
441 #ifdef CONFIG_NFS_V3_ACL
442 static void nfs_init_server_aclclient(struct nfs_server *server)
444 if (server->nfs_client->cl_nfsversion != 3)
445 goto out_noacl;
446 if (server->flags & NFS_MOUNT_NOACL)
447 goto out_noacl;
449 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
450 if (IS_ERR(server->client_acl))
451 goto out_noacl;
453 /* No errors! Assume that Sun nfsacls are supported */
454 server->caps |= NFS_CAP_ACLS;
455 return;
457 out_noacl:
458 server->caps &= ~NFS_CAP_ACLS;
460 #else
461 static inline void nfs_init_server_aclclient(struct nfs_server *server)
463 server->flags &= ~NFS_MOUNT_NOACL;
464 server->caps &= ~NFS_CAP_ACLS;
466 #endif
469 * Create a general RPC client
471 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
473 struct nfs_client *clp = server->nfs_client;
475 server->client = rpc_clone_client(clp->cl_rpcclient);
476 if (IS_ERR(server->client)) {
477 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
478 return PTR_ERR(server->client);
481 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
482 struct rpc_auth *auth;
484 auth = rpcauth_create(pseudoflavour, server->client);
485 if (IS_ERR(auth)) {
486 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
487 return PTR_ERR(auth);
490 server->client->cl_softrtry = 0;
491 if (server->flags & NFS_MOUNT_SOFT)
492 server->client->cl_softrtry = 1;
494 server->client->cl_intr = 0;
495 if (server->flags & NFS4_MOUNT_INTR)
496 server->client->cl_intr = 1;
498 return 0;
502 * Initialise an NFS2 or NFS3 client
504 static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
506 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
507 int error;
509 if (clp->cl_cons_state == NFS_CS_READY) {
510 /* the client is already initialised */
511 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
512 return 0;
515 /* Check NFS protocol revision and initialize RPC op vector */
516 clp->rpc_ops = &nfs_v2_clientops;
517 #ifdef CONFIG_NFS_V3
518 if (clp->cl_nfsversion == 3)
519 clp->rpc_ops = &nfs_v3_clientops;
520 #endif
522 * Create a client RPC handle for doing FSSTAT with UNIX auth only
523 * - RFC 2623, sec 2.3.2
525 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
526 RPC_AUTH_UNIX, 0);
527 if (error < 0)
528 goto error;
529 nfs_mark_client_ready(clp, NFS_CS_READY);
530 return 0;
532 error:
533 nfs_mark_client_ready(clp, error);
534 dprintk("<-- nfs_init_client() = xerror %d\n", error);
535 return error;
539 * Create a version 2 or 3 client
541 static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
543 struct nfs_client *clp;
544 int error, nfsvers = 2;
546 dprintk("--> nfs_init_server()\n");
548 #ifdef CONFIG_NFS_V3
549 if (data->flags & NFS_MOUNT_VER3)
550 nfsvers = 3;
551 #endif
553 /* Allocate or find a client reference we can use */
554 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
555 if (IS_ERR(clp)) {
556 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
557 return PTR_ERR(clp);
560 error = nfs_init_client(clp, data);
561 if (error < 0)
562 goto error;
564 server->nfs_client = clp;
566 /* Initialise the client representation from the mount data */
567 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
569 if (data->rsize)
570 server->rsize = nfs_block_size(data->rsize, NULL);
571 if (data->wsize)
572 server->wsize = nfs_block_size(data->wsize, NULL);
574 server->acregmin = data->acregmin * HZ;
575 server->acregmax = data->acregmax * HZ;
576 server->acdirmin = data->acdirmin * HZ;
577 server->acdirmax = data->acdirmax * HZ;
579 /* Start lockd here, before we might error out */
580 error = nfs_start_lockd(server);
581 if (error < 0)
582 goto error;
584 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
585 if (error < 0)
586 goto error;
588 server->namelen = data->namlen;
589 /* Create a client RPC handle for the NFSv3 ACL management interface */
590 nfs_init_server_aclclient(server);
591 if (clp->cl_nfsversion == 3) {
592 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
593 server->namelen = NFS3_MAXNAMLEN;
594 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
595 server->caps |= NFS_CAP_READDIRPLUS;
596 } else {
597 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
598 server->namelen = NFS2_MAXNAMLEN;
601 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
602 return 0;
604 error:
605 server->nfs_client = NULL;
606 nfs_put_client(clp);
607 dprintk("<-- nfs_init_server() = xerror %d\n", error);
608 return error;
612 * Load up the server record from information gained in an fsinfo record
614 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
616 unsigned long max_rpc_payload;
618 /* Work out a lot of parameters */
619 if (server->rsize == 0)
620 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
621 if (server->wsize == 0)
622 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
624 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
625 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
626 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
627 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
629 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
630 if (server->rsize > max_rpc_payload)
631 server->rsize = max_rpc_payload;
632 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
633 server->rsize = NFS_MAX_FILE_IO_SIZE;
634 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
635 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
637 if (server->wsize > max_rpc_payload)
638 server->wsize = max_rpc_payload;
639 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
640 server->wsize = NFS_MAX_FILE_IO_SIZE;
641 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
642 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
644 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
645 if (server->dtsize > PAGE_CACHE_SIZE)
646 server->dtsize = PAGE_CACHE_SIZE;
647 if (server->dtsize > server->rsize)
648 server->dtsize = server->rsize;
650 if (server->flags & NFS_MOUNT_NOAC) {
651 server->acregmin = server->acregmax = 0;
652 server->acdirmin = server->acdirmax = 0;
655 server->maxfilesize = fsinfo->maxfilesize;
657 /* We're airborne Set socket buffersize */
658 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
662 * Probe filesystem information, including the FSID on v2/v3
664 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
666 struct nfs_fsinfo fsinfo;
667 struct nfs_client *clp = server->nfs_client;
668 int error;
670 dprintk("--> nfs_probe_fsinfo()\n");
672 if (clp->rpc_ops->set_capabilities != NULL) {
673 error = clp->rpc_ops->set_capabilities(server, mntfh);
674 if (error < 0)
675 goto out_error;
678 fsinfo.fattr = fattr;
679 nfs_fattr_init(fattr);
680 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
681 if (error < 0)
682 goto out_error;
684 nfs_server_set_fsinfo(server, &fsinfo);
686 /* Get some general file system info */
687 if (server->namelen == 0) {
688 struct nfs_pathconf pathinfo;
690 pathinfo.fattr = fattr;
691 nfs_fattr_init(fattr);
693 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
694 server->namelen = pathinfo.max_namelen;
697 dprintk("<-- nfs_probe_fsinfo() = 0\n");
698 return 0;
700 out_error:
701 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
702 return error;
706 * Copy useful information when duplicating a server record
708 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
710 target->flags = source->flags;
711 target->acregmin = source->acregmin;
712 target->acregmax = source->acregmax;
713 target->acdirmin = source->acdirmin;
714 target->acdirmax = source->acdirmax;
715 target->caps = source->caps;
719 * Allocate and initialise a server record
721 static struct nfs_server *nfs_alloc_server(void)
723 struct nfs_server *server;
725 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
726 if (!server)
727 return NULL;
729 server->client = server->client_acl = ERR_PTR(-EINVAL);
731 /* Zero out the NFS state stuff */
732 INIT_LIST_HEAD(&server->client_link);
733 INIT_LIST_HEAD(&server->master_link);
735 server->io_stats = nfs_alloc_iostats();
736 if (!server->io_stats) {
737 kfree(server);
738 return NULL;
741 return server;
745 * Free up a server record
747 void nfs_free_server(struct nfs_server *server)
749 dprintk("--> nfs_free_server()\n");
751 spin_lock(&nfs_client_lock);
752 list_del(&server->client_link);
753 list_del(&server->master_link);
754 spin_unlock(&nfs_client_lock);
756 if (server->destroy != NULL)
757 server->destroy(server);
758 if (!IS_ERR(server->client))
759 rpc_shutdown_client(server->client);
761 nfs_put_client(server->nfs_client);
763 nfs_free_iostats(server->io_stats);
764 kfree(server);
765 nfs_release_automount_timer();
766 dprintk("<-- nfs_free_server()\n");
770 * Create a version 2 or 3 volume record
771 * - keyed on server and FSID
773 struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
774 struct nfs_fh *mntfh)
776 struct nfs_server *server;
777 struct nfs_fattr fattr;
778 int error;
780 server = nfs_alloc_server();
781 if (!server)
782 return ERR_PTR(-ENOMEM);
784 /* Get a client representation */
785 error = nfs_init_server(server, data);
786 if (error < 0)
787 goto error;
789 BUG_ON(!server->nfs_client);
790 BUG_ON(!server->nfs_client->rpc_ops);
791 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
793 /* Probe the root fh to retrieve its FSID */
794 error = nfs_probe_fsinfo(server, mntfh, &fattr);
795 if (error < 0)
796 goto error;
797 if (!(fattr.valid & NFS_ATTR_FATTR)) {
798 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
799 if (error < 0) {
800 dprintk("nfs_create_server: getattr error = %d\n", -error);
801 goto error;
804 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
806 dprintk("Server FSID: %llx:%llx\n",
807 (unsigned long long) server->fsid.major,
808 (unsigned long long) server->fsid.minor);
810 BUG_ON(!server->nfs_client);
811 BUG_ON(!server->nfs_client->rpc_ops);
812 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
814 spin_lock(&nfs_client_lock);
815 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
816 list_add_tail(&server->master_link, &nfs_volume_list);
817 spin_unlock(&nfs_client_lock);
819 server->mount_time = jiffies;
820 return server;
822 error:
823 nfs_free_server(server);
824 return ERR_PTR(error);
827 #ifdef CONFIG_NFS_V4
829 * Initialise an NFS4 client record
831 static int nfs4_init_client(struct nfs_client *clp,
832 int proto, int timeo, int retrans,
833 const char *ip_addr,
834 rpc_authflavor_t authflavour)
836 int error;
838 if (clp->cl_cons_state == NFS_CS_READY) {
839 /* the client is initialised already */
840 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
841 return 0;
844 /* Check NFS protocol revision and initialize RPC op vector */
845 clp->rpc_ops = &nfs_v4_clientops;
847 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
848 RPC_CLNT_CREATE_DISCRTRY);
849 if (error < 0)
850 goto error;
851 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
853 error = nfs_idmap_new(clp);
854 if (error < 0) {
855 dprintk("%s: failed to create idmapper. Error = %d\n",
856 __FUNCTION__, error);
857 goto error;
859 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
861 nfs_mark_client_ready(clp, NFS_CS_READY);
862 return 0;
864 error:
865 nfs_mark_client_ready(clp, error);
866 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
867 return error;
871 * Set up an NFS4 client
873 static int nfs4_set_client(struct nfs_server *server,
874 const char *hostname, const struct sockaddr_in *addr,
875 const char *ip_addr,
876 rpc_authflavor_t authflavour,
877 int proto, int timeo, int retrans)
879 struct nfs_client *clp;
880 int error;
882 dprintk("--> nfs4_set_client()\n");
884 /* Allocate or find a client reference we can use */
885 clp = nfs_get_client(hostname, addr, 4);
886 if (IS_ERR(clp)) {
887 error = PTR_ERR(clp);
888 goto error;
890 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
891 if (error < 0)
892 goto error_put;
894 server->nfs_client = clp;
895 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
896 return 0;
898 error_put:
899 nfs_put_client(clp);
900 error:
901 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
902 return error;
906 * Create a version 4 volume record
908 static int nfs4_init_server(struct nfs_server *server,
909 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
911 int error;
913 dprintk("--> nfs4_init_server()\n");
915 /* Initialise the client representation from the mount data */
916 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
917 server->caps |= NFS_CAP_ATOMIC_OPEN;
919 if (data->rsize)
920 server->rsize = nfs_block_size(data->rsize, NULL);
921 if (data->wsize)
922 server->wsize = nfs_block_size(data->wsize, NULL);
924 server->acregmin = data->acregmin * HZ;
925 server->acregmax = data->acregmax * HZ;
926 server->acdirmin = data->acdirmin * HZ;
927 server->acdirmax = data->acdirmax * HZ;
929 error = nfs_init_server_rpcclient(server, authflavour);
931 /* Done */
932 dprintk("<-- nfs4_init_server() = %d\n", error);
933 return error;
937 * Create a version 4 volume record
938 * - keyed on server and FSID
940 struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
941 const char *hostname,
942 const struct sockaddr_in *addr,
943 const char *mntpath,
944 const char *ip_addr,
945 rpc_authflavor_t authflavour,
946 struct nfs_fh *mntfh)
948 struct nfs_fattr fattr;
949 struct nfs_server *server;
950 int error;
952 dprintk("--> nfs4_create_server()\n");
954 server = nfs_alloc_server();
955 if (!server)
956 return ERR_PTR(-ENOMEM);
958 /* Get a client record */
959 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
960 data->proto, data->timeo, data->retrans);
961 if (error < 0)
962 goto error;
964 /* set up the general RPC client */
965 error = nfs4_init_server(server, data, authflavour);
966 if (error < 0)
967 goto error;
969 BUG_ON(!server->nfs_client);
970 BUG_ON(!server->nfs_client->rpc_ops);
971 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
973 /* Probe the root fh to retrieve its FSID */
974 error = nfs4_path_walk(server, mntfh, mntpath);
975 if (error < 0)
976 goto error;
978 dprintk("Server FSID: %llx:%llx\n",
979 (unsigned long long) server->fsid.major,
980 (unsigned long long) server->fsid.minor);
981 dprintk("Mount FH: %d\n", mntfh->size);
983 error = nfs_probe_fsinfo(server, mntfh, &fattr);
984 if (error < 0)
985 goto error;
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);
991 spin_lock(&nfs_client_lock);
992 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
993 list_add_tail(&server->master_link, &nfs_volume_list);
994 spin_unlock(&nfs_client_lock);
996 server->mount_time = jiffies;
997 dprintk("<-- nfs4_create_server() = %p\n", server);
998 return server;
1000 error:
1001 nfs_free_server(server);
1002 dprintk("<-- nfs4_create_server() = error %d\n", error);
1003 return ERR_PTR(error);
1007 * Create an NFS4 referral server record
1009 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1010 struct nfs_fh *mntfh)
1012 struct nfs_client *parent_client;
1013 struct nfs_server *server, *parent_server;
1014 struct nfs_fattr fattr;
1015 int error;
1017 dprintk("--> nfs4_create_referral_server()\n");
1019 server = nfs_alloc_server();
1020 if (!server)
1021 return ERR_PTR(-ENOMEM);
1023 parent_server = NFS_SB(data->sb);
1024 parent_client = parent_server->nfs_client;
1026 /* Get a client representation.
1027 * Note: NFSv4 always uses TCP, */
1028 error = nfs4_set_client(server, data->hostname, data->addr,
1029 parent_client->cl_ipaddr,
1030 data->authflavor,
1031 parent_server->client->cl_xprt->prot,
1032 parent_client->retrans_timeo,
1033 parent_client->retrans_count);
1034 if (error < 0)
1035 goto error;
1037 /* Initialise the client representation from the parent server */
1038 nfs_server_copy_userdata(server, parent_server);
1039 server->caps |= NFS_CAP_ATOMIC_OPEN;
1041 error = nfs_init_server_rpcclient(server, data->authflavor);
1042 if (error < 0)
1043 goto error;
1045 BUG_ON(!server->nfs_client);
1046 BUG_ON(!server->nfs_client->rpc_ops);
1047 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1049 /* Probe the root fh to retrieve its FSID and filehandle */
1050 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1051 if (error < 0)
1052 goto error;
1054 /* probe the filesystem info for this server filesystem */
1055 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1056 if (error < 0)
1057 goto error;
1059 dprintk("Referral FSID: %llx:%llx\n",
1060 (unsigned long long) server->fsid.major,
1061 (unsigned long long) server->fsid.minor);
1063 spin_lock(&nfs_client_lock);
1064 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1065 list_add_tail(&server->master_link, &nfs_volume_list);
1066 spin_unlock(&nfs_client_lock);
1068 server->mount_time = jiffies;
1070 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1071 return server;
1073 error:
1074 nfs_free_server(server);
1075 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1076 return ERR_PTR(error);
1079 #endif /* CONFIG_NFS_V4 */
1082 * Clone an NFS2, NFS3 or NFS4 server record
1084 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1085 struct nfs_fh *fh,
1086 struct nfs_fattr *fattr)
1088 struct nfs_server *server;
1089 struct nfs_fattr fattr_fsinfo;
1090 int error;
1092 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1093 (unsigned long long) fattr->fsid.major,
1094 (unsigned long long) fattr->fsid.minor);
1096 server = nfs_alloc_server();
1097 if (!server)
1098 return ERR_PTR(-ENOMEM);
1100 /* Copy data from the source */
1101 server->nfs_client = source->nfs_client;
1102 atomic_inc(&server->nfs_client->cl_count);
1103 nfs_server_copy_userdata(server, source);
1105 server->fsid = fattr->fsid;
1107 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1108 if (error < 0)
1109 goto out_free_server;
1110 if (!IS_ERR(source->client_acl))
1111 nfs_init_server_aclclient(server);
1113 /* probe the filesystem info for this server filesystem */
1114 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1115 if (error < 0)
1116 goto out_free_server;
1118 dprintk("Cloned FSID: %llx:%llx\n",
1119 (unsigned long long) server->fsid.major,
1120 (unsigned long long) server->fsid.minor);
1122 error = nfs_start_lockd(server);
1123 if (error < 0)
1124 goto out_free_server;
1126 spin_lock(&nfs_client_lock);
1127 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1128 list_add_tail(&server->master_link, &nfs_volume_list);
1129 spin_unlock(&nfs_client_lock);
1131 server->mount_time = jiffies;
1133 dprintk("<-- nfs_clone_server() = %p\n", server);
1134 return server;
1136 out_free_server:
1137 nfs_free_server(server);
1138 dprintk("<-- nfs_clone_server() = error %d\n", error);
1139 return ERR_PTR(error);
1142 #ifdef CONFIG_PROC_FS
1143 static struct proc_dir_entry *proc_fs_nfs;
1145 static int nfs_server_list_open(struct inode *inode, struct file *file);
1146 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1147 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1148 static void nfs_server_list_stop(struct seq_file *p, void *v);
1149 static int nfs_server_list_show(struct seq_file *m, void *v);
1151 static struct seq_operations nfs_server_list_ops = {
1152 .start = nfs_server_list_start,
1153 .next = nfs_server_list_next,
1154 .stop = nfs_server_list_stop,
1155 .show = nfs_server_list_show,
1158 static const struct file_operations nfs_server_list_fops = {
1159 .open = nfs_server_list_open,
1160 .read = seq_read,
1161 .llseek = seq_lseek,
1162 .release = seq_release,
1165 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1166 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1167 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1168 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1169 static int nfs_volume_list_show(struct seq_file *m, void *v);
1171 static struct seq_operations nfs_volume_list_ops = {
1172 .start = nfs_volume_list_start,
1173 .next = nfs_volume_list_next,
1174 .stop = nfs_volume_list_stop,
1175 .show = nfs_volume_list_show,
1178 static const struct file_operations nfs_volume_list_fops = {
1179 .open = nfs_volume_list_open,
1180 .read = seq_read,
1181 .llseek = seq_lseek,
1182 .release = seq_release,
1186 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1187 * we're dealing
1189 static int nfs_server_list_open(struct inode *inode, struct file *file)
1191 struct seq_file *m;
1192 int ret;
1194 ret = seq_open(file, &nfs_server_list_ops);
1195 if (ret < 0)
1196 return ret;
1198 m = file->private_data;
1199 m->private = PDE(inode)->data;
1201 return 0;
1205 * set up the iterator to start reading from the server list and return the first item
1207 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1209 struct list_head *_p;
1210 loff_t pos = *_pos;
1212 /* lock the list against modification */
1213 spin_lock(&nfs_client_lock);
1215 /* allow for the header line */
1216 if (!pos)
1217 return SEQ_START_TOKEN;
1218 pos--;
1220 /* find the n'th element in the list */
1221 list_for_each(_p, &nfs_client_list)
1222 if (!pos--)
1223 break;
1225 return _p != &nfs_client_list ? _p : NULL;
1229 * move to next server
1231 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1233 struct list_head *_p;
1235 (*pos)++;
1237 _p = v;
1238 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1240 return _p != &nfs_client_list ? _p : NULL;
1244 * clean up after reading from the transports list
1246 static void nfs_server_list_stop(struct seq_file *p, void *v)
1248 spin_unlock(&nfs_client_lock);
1252 * display a header line followed by a load of call lines
1254 static int nfs_server_list_show(struct seq_file *m, void *v)
1256 struct nfs_client *clp;
1258 /* display header on line 1 */
1259 if (v == SEQ_START_TOKEN) {
1260 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1261 return 0;
1264 /* display one transport per line on subsequent lines */
1265 clp = list_entry(v, struct nfs_client, cl_share_link);
1267 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1268 clp->cl_nfsversion,
1269 NIPQUAD(clp->cl_addr.sin_addr),
1270 ntohs(clp->cl_addr.sin_port),
1271 atomic_read(&clp->cl_count),
1272 clp->cl_hostname);
1274 return 0;
1278 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1280 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1282 struct seq_file *m;
1283 int ret;
1285 ret = seq_open(file, &nfs_volume_list_ops);
1286 if (ret < 0)
1287 return ret;
1289 m = file->private_data;
1290 m->private = PDE(inode)->data;
1292 return 0;
1296 * set up the iterator to start reading from the volume list and return the first item
1298 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1300 struct list_head *_p;
1301 loff_t pos = *_pos;
1303 /* lock the list against modification */
1304 spin_lock(&nfs_client_lock);
1306 /* allow for the header line */
1307 if (!pos)
1308 return SEQ_START_TOKEN;
1309 pos--;
1311 /* find the n'th element in the list */
1312 list_for_each(_p, &nfs_volume_list)
1313 if (!pos--)
1314 break;
1316 return _p != &nfs_volume_list ? _p : NULL;
1320 * move to next volume
1322 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1324 struct list_head *_p;
1326 (*pos)++;
1328 _p = v;
1329 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1331 return _p != &nfs_volume_list ? _p : NULL;
1335 * clean up after reading from the transports list
1337 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1339 spin_unlock(&nfs_client_lock);
1343 * display a header line followed by a load of call lines
1345 static int nfs_volume_list_show(struct seq_file *m, void *v)
1347 struct nfs_server *server;
1348 struct nfs_client *clp;
1349 char dev[8], fsid[17];
1351 /* display header on line 1 */
1352 if (v == SEQ_START_TOKEN) {
1353 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1354 return 0;
1356 /* display one transport per line on subsequent lines */
1357 server = list_entry(v, struct nfs_server, master_link);
1358 clp = server->nfs_client;
1360 snprintf(dev, 8, "%u:%u",
1361 MAJOR(server->s_dev), MINOR(server->s_dev));
1363 snprintf(fsid, 17, "%llx:%llx",
1364 (unsigned long long) server->fsid.major,
1365 (unsigned long long) server->fsid.minor);
1367 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1368 clp->cl_nfsversion,
1369 NIPQUAD(clp->cl_addr.sin_addr),
1370 ntohs(clp->cl_addr.sin_port),
1371 dev,
1372 fsid);
1374 return 0;
1378 * initialise the /proc/fs/nfsfs/ directory
1380 int __init nfs_fs_proc_init(void)
1382 struct proc_dir_entry *p;
1384 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1385 if (!proc_fs_nfs)
1386 goto error_0;
1388 proc_fs_nfs->owner = THIS_MODULE;
1390 /* a file of servers with which we're dealing */
1391 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1392 if (!p)
1393 goto error_1;
1395 p->proc_fops = &nfs_server_list_fops;
1396 p->owner = THIS_MODULE;
1398 /* a file of volumes that we have mounted */
1399 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1400 if (!p)
1401 goto error_2;
1403 p->proc_fops = &nfs_volume_list_fops;
1404 p->owner = THIS_MODULE;
1405 return 0;
1407 error_2:
1408 remove_proc_entry("servers", proc_fs_nfs);
1409 error_1:
1410 remove_proc_entry("nfsfs", proc_root_fs);
1411 error_0:
1412 return -ENOMEM;
1416 * clean up the /proc/fs/nfsfs/ directory
1418 void nfs_fs_proc_exit(void)
1420 remove_proc_entry("volumes", proc_fs_nfs);
1421 remove_proc_entry("servers", proc_fs_nfs);
1422 remove_proc_entry("nfsfs", proc_root_fs);
1425 #endif /* CONFIG_PROC_FS */