[PATCH] mark struct file_operations const 6
[usb.git] / fs / nfs / client.c
blobae9f36e393cf0a857064bdff9809d13226eb4905
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>
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>
38 #include <asm/system.h>
40 #include "nfs4_fs.h"
41 #include "callback.h"
42 #include "delegation.h"
43 #include "iostat.h"
44 #include "internal.h"
46 #define NFSDBG_FACILITY NFSDBG_CLIENT
48 static DEFINE_SPINLOCK(nfs_client_lock);
49 static LIST_HEAD(nfs_client_list);
50 static LIST_HEAD(nfs_volume_list);
51 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
54 * RPC cruft for NFS
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
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",
75 struct rpc_stat nfs_rpcstat = {
76 .program = &nfs_program
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,
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,
93 #endif /* CONFIG_NFS_V3_ACL */
96 * Allocate a shared client record
98 * Since these are allocated/deallocated very rarely, we don't
99 * bother putting them in a slab cache...
101 static struct nfs_client *nfs_alloc_client(const char *hostname,
102 const struct sockaddr_in *addr,
103 int nfsversion)
105 struct nfs_client *clp;
106 int error;
108 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
109 goto error_0;
111 error = rpciod_up();
112 if (error < 0) {
113 dprintk("%s: couldn't start rpciod! Error = %d\n",
114 __FUNCTION__, error);
115 goto error_1;
117 __set_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
119 if (nfsversion == 4) {
120 if (nfs_callback_up() < 0)
121 goto error_2;
122 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
125 atomic_set(&clp->cl_count, 1);
126 clp->cl_cons_state = NFS_CS_INITING;
128 clp->cl_nfsversion = nfsversion;
129 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
131 if (hostname) {
132 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
133 if (!clp->cl_hostname)
134 goto error_3;
137 INIT_LIST_HEAD(&clp->cl_superblocks);
138 clp->cl_rpcclient = ERR_PTR(-EINVAL);
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_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
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
152 return clp;
154 error_3:
155 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
156 nfs_callback_down();
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;
166 static void nfs4_shutdown_client(struct nfs_client *clp)
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;
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);
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
187 * Destroy a shared client record
189 static void nfs_free_client(struct nfs_client *clp)
191 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
193 nfs4_shutdown_client(clp);
195 /* -EIO all pending I/O */
196 if (!IS_ERR(clp->cl_rpcclient))
197 rpc_shutdown_client(clp->cl_rpcclient);
199 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
200 nfs_callback_down();
202 if (__test_and_clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state))
203 rpciod_down();
205 kfree(clp->cl_hostname);
206 kfree(clp);
208 dprintk("<-- nfs_free_client()\n");
212 * Release a reference to a shared client record
214 void nfs_put_client(struct nfs_client *clp)
216 if (!clp)
217 return;
219 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
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);
225 BUG_ON(!list_empty(&clp->cl_superblocks));
227 nfs_free_client(clp);
232 * Find a client by address
233 * - caller must hold nfs_client_lock
235 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
237 struct nfs_client *clp;
239 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
240 /* Don't match clients that failed to initialise properly */
241 if (clp->cl_cons_state < 0)
242 continue;
244 /* Different NFS versions cannot share the same nfs_client */
245 if (clp->cl_nfsversion != nfsversion)
246 continue;
248 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
249 sizeof(clp->cl_addr.sin_addr)) != 0)
250 continue;
252 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
253 goto found;
256 return NULL;
258 found:
259 atomic_inc(&clp->cl_count);
260 return clp;
264 * Find a client by IP address and protocol version
265 * - returns NULL if no such client
267 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
269 struct nfs_client *clp;
271 spin_lock(&nfs_client_lock);
272 clp = __nfs_find_client(addr, nfsversion, 0);
273 spin_unlock(&nfs_client_lock);
274 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
275 nfs_put_client(clp);
276 clp = NULL;
278 return clp;
282 * Look up a client by IP address and protocol version
283 * - creates a new record if one doesn't yet exist
285 static struct nfs_client *nfs_get_client(const char *hostname,
286 const struct sockaddr_in *addr,
287 int nfsversion)
289 struct nfs_client *clp, *new = NULL;
290 int error;
292 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
293 hostname ?: "", NIPQUAD(addr->sin_addr),
294 addr->sin_port, nfsversion);
296 /* see if the client already exists */
297 do {
298 spin_lock(&nfs_client_lock);
300 clp = __nfs_find_client(addr, nfsversion, 1);
301 if (clp)
302 goto found_client;
303 if (new)
304 goto install_client;
306 spin_unlock(&nfs_client_lock);
308 new = nfs_alloc_client(hostname, addr, nfsversion);
309 } while (new);
311 return ERR_PTR(-ENOMEM);
313 /* install a new client and return with it unready */
314 install_client:
315 clp = new;
316 list_add(&clp->cl_share_link, &nfs_client_list);
317 spin_unlock(&nfs_client_lock);
318 dprintk("--> nfs_get_client() = %p [new]\n", clp);
319 return clp;
321 /* found an existing client
322 * - make sure it's ready before returning
324 found_client:
325 spin_unlock(&nfs_client_lock);
327 if (new)
328 nfs_free_client(new);
330 error = wait_event_interruptible(nfs_client_active_wq,
331 clp->cl_cons_state != NFS_CS_INITING);
332 if (error < 0) {
333 nfs_put_client(clp);
334 return ERR_PTR(-ERESTARTSYS);
337 if (clp->cl_cons_state < NFS_CS_READY) {
338 error = clp->cl_cons_state;
339 nfs_put_client(clp);
340 return ERR_PTR(error);
343 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
345 dprintk("--> nfs_get_client() = %p [share]\n", clp);
346 return clp;
350 * Mark a server as ready or failed
352 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
354 clp->cl_cons_state = state;
355 wake_up_all(&nfs_client_active_wq);
359 * Initialise the timeout values for a connection
361 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
362 unsigned int timeo, unsigned int retrans)
364 to->to_initval = timeo * HZ / 10;
365 to->to_retries = retrans;
366 if (!to->to_retries)
367 to->to_retries = 2;
369 switch (proto) {
370 case IPPROTO_TCP:
371 if (!to->to_initval)
372 to->to_initval = 60 * HZ;
373 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
374 to->to_initval = NFS_MAX_TCP_TIMEOUT;
375 to->to_increment = to->to_initval;
376 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
377 to->to_exponential = 0;
378 break;
379 case IPPROTO_UDP:
380 default:
381 if (!to->to_initval)
382 to->to_initval = 11 * HZ / 10;
383 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
384 to->to_initval = NFS_MAX_UDP_TIMEOUT;
385 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
386 to->to_exponential = 1;
387 break;
392 * Create an RPC client handle
394 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
395 unsigned int timeo,
396 unsigned int retrans,
397 rpc_authflavor_t flavor)
399 struct rpc_timeout timeparms;
400 struct rpc_clnt *clnt = NULL;
401 struct rpc_create_args args = {
402 .protocol = proto,
403 .address = (struct sockaddr *)&clp->cl_addr,
404 .addrsize = sizeof(clp->cl_addr),
405 .timeout = &timeparms,
406 .servername = clp->cl_hostname,
407 .program = &nfs_program,
408 .version = clp->rpc_ops->version,
409 .authflavor = flavor,
412 if (!IS_ERR(clp->cl_rpcclient))
413 return 0;
415 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
416 clp->retrans_timeo = timeparms.to_initval;
417 clp->retrans_count = timeparms.to_retries;
419 clnt = rpc_create(&args);
420 if (IS_ERR(clnt)) {
421 dprintk("%s: cannot create RPC client. Error = %ld\n",
422 __FUNCTION__, PTR_ERR(clnt));
423 return PTR_ERR(clnt);
426 clp->cl_rpcclient = clnt;
427 return 0;
431 * Version 2 or 3 client destruction
433 static void nfs_destroy_server(struct nfs_server *server)
435 if (!IS_ERR(server->client_acl))
436 rpc_shutdown_client(server->client_acl);
438 if (!(server->flags & NFS_MOUNT_NONLM))
439 lockd_down(); /* release rpc.lockd */
443 * Version 2 or 3 lockd setup
445 static int nfs_start_lockd(struct nfs_server *server)
447 int error = 0;
449 if (server->nfs_client->cl_nfsversion > 3)
450 goto out;
451 if (server->flags & NFS_MOUNT_NONLM)
452 goto out;
453 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
454 IPPROTO_TCP : IPPROTO_UDP);
455 if (error < 0)
456 server->flags |= NFS_MOUNT_NONLM;
457 else
458 server->destroy = nfs_destroy_server;
459 out:
460 return error;
464 * Initialise an NFSv3 ACL client connection
466 #ifdef CONFIG_NFS_V3_ACL
467 static void nfs_init_server_aclclient(struct nfs_server *server)
469 if (server->nfs_client->cl_nfsversion != 3)
470 goto out_noacl;
471 if (server->flags & NFS_MOUNT_NOACL)
472 goto out_noacl;
474 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
475 if (IS_ERR(server->client_acl))
476 goto out_noacl;
478 /* No errors! Assume that Sun nfsacls are supported */
479 server->caps |= NFS_CAP_ACLS;
480 return;
482 out_noacl:
483 server->caps &= ~NFS_CAP_ACLS;
485 #else
486 static inline void nfs_init_server_aclclient(struct nfs_server *server)
488 server->flags &= ~NFS_MOUNT_NOACL;
489 server->caps &= ~NFS_CAP_ACLS;
491 #endif
494 * Create a general RPC client
496 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
498 struct nfs_client *clp = server->nfs_client;
500 server->client = rpc_clone_client(clp->cl_rpcclient);
501 if (IS_ERR(server->client)) {
502 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
503 return PTR_ERR(server->client);
506 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
507 struct rpc_auth *auth;
509 auth = rpcauth_create(pseudoflavour, server->client);
510 if (IS_ERR(auth)) {
511 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
512 return PTR_ERR(auth);
515 server->client->cl_softrtry = 0;
516 if (server->flags & NFS_MOUNT_SOFT)
517 server->client->cl_softrtry = 1;
519 server->client->cl_intr = 0;
520 if (server->flags & NFS4_MOUNT_INTR)
521 server->client->cl_intr = 1;
523 return 0;
527 * Initialise an NFS2 or NFS3 client
529 static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
531 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
532 int error;
534 if (clp->cl_cons_state == NFS_CS_READY) {
535 /* the client is already initialised */
536 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
537 return 0;
540 /* Check NFS protocol revision and initialize RPC op vector */
541 clp->rpc_ops = &nfs_v2_clientops;
542 #ifdef CONFIG_NFS_V3
543 if (clp->cl_nfsversion == 3)
544 clp->rpc_ops = &nfs_v3_clientops;
545 #endif
547 * Create a client RPC handle for doing FSSTAT with UNIX auth only
548 * - RFC 2623, sec 2.3.2
550 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
551 RPC_AUTH_UNIX);
552 if (error < 0)
553 goto error;
554 nfs_mark_client_ready(clp, NFS_CS_READY);
555 return 0;
557 error:
558 nfs_mark_client_ready(clp, error);
559 dprintk("<-- nfs_init_client() = xerror %d\n", error);
560 return error;
564 * Create a version 2 or 3 client
566 static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
568 struct nfs_client *clp;
569 int error, nfsvers = 2;
571 dprintk("--> nfs_init_server()\n");
573 #ifdef CONFIG_NFS_V3
574 if (data->flags & NFS_MOUNT_VER3)
575 nfsvers = 3;
576 #endif
578 /* Allocate or find a client reference we can use */
579 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
580 if (IS_ERR(clp)) {
581 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
582 return PTR_ERR(clp);
585 error = nfs_init_client(clp, data);
586 if (error < 0)
587 goto error;
589 server->nfs_client = clp;
591 /* Initialise the client representation from the mount data */
592 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
594 if (data->rsize)
595 server->rsize = nfs_block_size(data->rsize, NULL);
596 if (data->wsize)
597 server->wsize = nfs_block_size(data->wsize, NULL);
599 server->acregmin = data->acregmin * HZ;
600 server->acregmax = data->acregmax * HZ;
601 server->acdirmin = data->acdirmin * HZ;
602 server->acdirmax = data->acdirmax * HZ;
604 /* Start lockd here, before we might error out */
605 error = nfs_start_lockd(server);
606 if (error < 0)
607 goto error;
609 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
610 if (error < 0)
611 goto error;
613 server->namelen = data->namlen;
614 /* Create a client RPC handle for the NFSv3 ACL management interface */
615 nfs_init_server_aclclient(server);
616 if (clp->cl_nfsversion == 3) {
617 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
618 server->namelen = NFS3_MAXNAMLEN;
619 server->caps |= NFS_CAP_READDIRPLUS;
620 } else {
621 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
622 server->namelen = NFS2_MAXNAMLEN;
625 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
626 return 0;
628 error:
629 server->nfs_client = NULL;
630 nfs_put_client(clp);
631 dprintk("<-- nfs_init_server() = xerror %d\n", error);
632 return error;
636 * Load up the server record from information gained in an fsinfo record
638 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
640 unsigned long max_rpc_payload;
642 /* Work out a lot of parameters */
643 if (server->rsize == 0)
644 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
645 if (server->wsize == 0)
646 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
648 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
649 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
650 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
651 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
653 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
654 if (server->rsize > max_rpc_payload)
655 server->rsize = max_rpc_payload;
656 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
657 server->rsize = NFS_MAX_FILE_IO_SIZE;
658 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
659 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
661 if (server->wsize > max_rpc_payload)
662 server->wsize = max_rpc_payload;
663 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
664 server->wsize = NFS_MAX_FILE_IO_SIZE;
665 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
666 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
668 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
669 if (server->dtsize > PAGE_CACHE_SIZE)
670 server->dtsize = PAGE_CACHE_SIZE;
671 if (server->dtsize > server->rsize)
672 server->dtsize = server->rsize;
674 if (server->flags & NFS_MOUNT_NOAC) {
675 server->acregmin = server->acregmax = 0;
676 server->acdirmin = server->acdirmax = 0;
679 server->maxfilesize = fsinfo->maxfilesize;
681 /* We're airborne Set socket buffersize */
682 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
686 * Probe filesystem information, including the FSID on v2/v3
688 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
690 struct nfs_fsinfo fsinfo;
691 struct nfs_client *clp = server->nfs_client;
692 int error;
694 dprintk("--> nfs_probe_fsinfo()\n");
696 if (clp->rpc_ops->set_capabilities != NULL) {
697 error = clp->rpc_ops->set_capabilities(server, mntfh);
698 if (error < 0)
699 goto out_error;
702 fsinfo.fattr = fattr;
703 nfs_fattr_init(fattr);
704 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
705 if (error < 0)
706 goto out_error;
708 nfs_server_set_fsinfo(server, &fsinfo);
710 /* Get some general file system info */
711 if (server->namelen == 0) {
712 struct nfs_pathconf pathinfo;
714 pathinfo.fattr = fattr;
715 nfs_fattr_init(fattr);
717 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
718 server->namelen = pathinfo.max_namelen;
721 dprintk("<-- nfs_probe_fsinfo() = 0\n");
722 return 0;
724 out_error:
725 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
726 return error;
730 * Copy useful information when duplicating a server record
732 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
734 target->flags = source->flags;
735 target->acregmin = source->acregmin;
736 target->acregmax = source->acregmax;
737 target->acdirmin = source->acdirmin;
738 target->acdirmax = source->acdirmax;
739 target->caps = source->caps;
743 * Allocate and initialise a server record
745 static struct nfs_server *nfs_alloc_server(void)
747 struct nfs_server *server;
749 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
750 if (!server)
751 return NULL;
753 server->client = server->client_acl = ERR_PTR(-EINVAL);
755 /* Zero out the NFS state stuff */
756 INIT_LIST_HEAD(&server->client_link);
757 INIT_LIST_HEAD(&server->master_link);
759 server->io_stats = nfs_alloc_iostats();
760 if (!server->io_stats) {
761 kfree(server);
762 return NULL;
765 return server;
769 * Free up a server record
771 void nfs_free_server(struct nfs_server *server)
773 dprintk("--> nfs_free_server()\n");
775 spin_lock(&nfs_client_lock);
776 list_del(&server->client_link);
777 list_del(&server->master_link);
778 spin_unlock(&nfs_client_lock);
780 if (server->destroy != NULL)
781 server->destroy(server);
782 if (!IS_ERR(server->client))
783 rpc_shutdown_client(server->client);
785 nfs_put_client(server->nfs_client);
787 nfs_free_iostats(server->io_stats);
788 kfree(server);
789 nfs_release_automount_timer();
790 dprintk("<-- nfs_free_server()\n");
794 * Create a version 2 or 3 volume record
795 * - keyed on server and FSID
797 struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
798 struct nfs_fh *mntfh)
800 struct nfs_server *server;
801 struct nfs_fattr fattr;
802 int error;
804 server = nfs_alloc_server();
805 if (!server)
806 return ERR_PTR(-ENOMEM);
808 /* Get a client representation */
809 error = nfs_init_server(server, data);
810 if (error < 0)
811 goto error;
813 BUG_ON(!server->nfs_client);
814 BUG_ON(!server->nfs_client->rpc_ops);
815 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
817 /* Probe the root fh to retrieve its FSID */
818 error = nfs_probe_fsinfo(server, mntfh, &fattr);
819 if (error < 0)
820 goto error;
821 if (!(fattr.valid & NFS_ATTR_FATTR)) {
822 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
823 if (error < 0) {
824 dprintk("nfs_create_server: getattr error = %d\n", -error);
825 goto error;
828 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
830 dprintk("Server FSID: %llx:%llx\n",
831 (unsigned long long) server->fsid.major,
832 (unsigned long long) server->fsid.minor);
834 BUG_ON(!server->nfs_client);
835 BUG_ON(!server->nfs_client->rpc_ops);
836 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
838 spin_lock(&nfs_client_lock);
839 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
840 list_add_tail(&server->master_link, &nfs_volume_list);
841 spin_unlock(&nfs_client_lock);
843 server->mount_time = jiffies;
844 return server;
846 error:
847 nfs_free_server(server);
848 return ERR_PTR(error);
851 #ifdef CONFIG_NFS_V4
853 * Initialise an NFS4 client record
855 static int nfs4_init_client(struct nfs_client *clp,
856 int proto, int timeo, int retrans,
857 const char *ip_addr,
858 rpc_authflavor_t authflavour)
860 int error;
862 if (clp->cl_cons_state == NFS_CS_READY) {
863 /* the client is initialised already */
864 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
865 return 0;
868 /* Check NFS protocol revision and initialize RPC op vector */
869 clp->rpc_ops = &nfs_v4_clientops;
871 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour);
872 if (error < 0)
873 goto error;
874 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
876 error = nfs_idmap_new(clp);
877 if (error < 0) {
878 dprintk("%s: failed to create idmapper. Error = %d\n",
879 __FUNCTION__, error);
880 goto error;
882 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
884 nfs_mark_client_ready(clp, NFS_CS_READY);
885 return 0;
887 error:
888 nfs_mark_client_ready(clp, error);
889 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
890 return error;
894 * Set up an NFS4 client
896 static int nfs4_set_client(struct nfs_server *server,
897 const char *hostname, const struct sockaddr_in *addr,
898 const char *ip_addr,
899 rpc_authflavor_t authflavour,
900 int proto, int timeo, int retrans)
902 struct nfs_client *clp;
903 int error;
905 dprintk("--> nfs4_set_client()\n");
907 /* Allocate or find a client reference we can use */
908 clp = nfs_get_client(hostname, addr, 4);
909 if (IS_ERR(clp)) {
910 error = PTR_ERR(clp);
911 goto error;
913 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
914 if (error < 0)
915 goto error_put;
917 server->nfs_client = clp;
918 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
919 return 0;
921 error_put:
922 nfs_put_client(clp);
923 error:
924 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
925 return error;
929 * Create a version 4 volume record
931 static int nfs4_init_server(struct nfs_server *server,
932 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
934 int error;
936 dprintk("--> nfs4_init_server()\n");
938 /* Initialise the client representation from the mount data */
939 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
940 server->caps |= NFS_CAP_ATOMIC_OPEN;
942 if (data->rsize)
943 server->rsize = nfs_block_size(data->rsize, NULL);
944 if (data->wsize)
945 server->wsize = nfs_block_size(data->wsize, NULL);
947 server->acregmin = data->acregmin * HZ;
948 server->acregmax = data->acregmax * HZ;
949 server->acdirmin = data->acdirmin * HZ;
950 server->acdirmax = data->acdirmax * HZ;
952 error = nfs_init_server_rpcclient(server, authflavour);
954 /* Done */
955 dprintk("<-- nfs4_init_server() = %d\n", error);
956 return error;
960 * Create a version 4 volume record
961 * - keyed on server and FSID
963 struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
964 const char *hostname,
965 const struct sockaddr_in *addr,
966 const char *mntpath,
967 const char *ip_addr,
968 rpc_authflavor_t authflavour,
969 struct nfs_fh *mntfh)
971 struct nfs_fattr fattr;
972 struct nfs_server *server;
973 int error;
975 dprintk("--> nfs4_create_server()\n");
977 server = nfs_alloc_server();
978 if (!server)
979 return ERR_PTR(-ENOMEM);
981 /* Get a client record */
982 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
983 data->proto, data->timeo, data->retrans);
984 if (error < 0)
985 goto error;
987 /* set up the general RPC client */
988 error = nfs4_init_server(server, data, authflavour);
989 if (error < 0)
990 goto error;
992 BUG_ON(!server->nfs_client);
993 BUG_ON(!server->nfs_client->rpc_ops);
994 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
996 /* Probe the root fh to retrieve its FSID */
997 error = nfs4_path_walk(server, mntfh, mntpath);
998 if (error < 0)
999 goto error;
1001 dprintk("Server FSID: %llx:%llx\n",
1002 (unsigned long long) server->fsid.major,
1003 (unsigned long long) server->fsid.minor);
1004 dprintk("Mount FH: %d\n", mntfh->size);
1006 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1007 if (error < 0)
1008 goto error;
1010 BUG_ON(!server->nfs_client);
1011 BUG_ON(!server->nfs_client->rpc_ops);
1012 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1014 spin_lock(&nfs_client_lock);
1015 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1016 list_add_tail(&server->master_link, &nfs_volume_list);
1017 spin_unlock(&nfs_client_lock);
1019 server->mount_time = jiffies;
1020 dprintk("<-- nfs4_create_server() = %p\n", server);
1021 return server;
1023 error:
1024 nfs_free_server(server);
1025 dprintk("<-- nfs4_create_server() = error %d\n", error);
1026 return ERR_PTR(error);
1030 * Create an NFS4 referral server record
1032 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1033 struct nfs_fh *fh)
1035 struct nfs_client *parent_client;
1036 struct nfs_server *server, *parent_server;
1037 struct nfs_fattr fattr;
1038 int error;
1040 dprintk("--> nfs4_create_referral_server()\n");
1042 server = nfs_alloc_server();
1043 if (!server)
1044 return ERR_PTR(-ENOMEM);
1046 parent_server = NFS_SB(data->sb);
1047 parent_client = parent_server->nfs_client;
1049 /* Get a client representation.
1050 * Note: NFSv4 always uses TCP, */
1051 error = nfs4_set_client(server, data->hostname, data->addr,
1052 parent_client->cl_ipaddr,
1053 data->authflavor,
1054 parent_server->client->cl_xprt->prot,
1055 parent_client->retrans_timeo,
1056 parent_client->retrans_count);
1057 if (error < 0)
1058 goto error;
1060 /* Initialise the client representation from the parent server */
1061 nfs_server_copy_userdata(server, parent_server);
1062 server->caps |= NFS_CAP_ATOMIC_OPEN;
1064 error = nfs_init_server_rpcclient(server, data->authflavor);
1065 if (error < 0)
1066 goto error;
1068 BUG_ON(!server->nfs_client);
1069 BUG_ON(!server->nfs_client->rpc_ops);
1070 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1072 /* probe the filesystem info for this server filesystem */
1073 error = nfs_probe_fsinfo(server, fh, &fattr);
1074 if (error < 0)
1075 goto error;
1077 dprintk("Referral FSID: %llx:%llx\n",
1078 (unsigned long long) server->fsid.major,
1079 (unsigned long long) server->fsid.minor);
1081 spin_lock(&nfs_client_lock);
1082 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1083 list_add_tail(&server->master_link, &nfs_volume_list);
1084 spin_unlock(&nfs_client_lock);
1086 server->mount_time = jiffies;
1088 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1089 return server;
1091 error:
1092 nfs_free_server(server);
1093 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1094 return ERR_PTR(error);
1097 #endif /* CONFIG_NFS_V4 */
1100 * Clone an NFS2, NFS3 or NFS4 server record
1102 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1103 struct nfs_fh *fh,
1104 struct nfs_fattr *fattr)
1106 struct nfs_server *server;
1107 struct nfs_fattr fattr_fsinfo;
1108 int error;
1110 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1111 (unsigned long long) fattr->fsid.major,
1112 (unsigned long long) fattr->fsid.minor);
1114 server = nfs_alloc_server();
1115 if (!server)
1116 return ERR_PTR(-ENOMEM);
1118 /* Copy data from the source */
1119 server->nfs_client = source->nfs_client;
1120 atomic_inc(&server->nfs_client->cl_count);
1121 nfs_server_copy_userdata(server, source);
1123 server->fsid = fattr->fsid;
1125 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1126 if (error < 0)
1127 goto out_free_server;
1128 if (!IS_ERR(source->client_acl))
1129 nfs_init_server_aclclient(server);
1131 /* probe the filesystem info for this server filesystem */
1132 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1133 if (error < 0)
1134 goto out_free_server;
1136 dprintk("Cloned FSID: %llx:%llx\n",
1137 (unsigned long long) server->fsid.major,
1138 (unsigned long long) server->fsid.minor);
1140 error = nfs_start_lockd(server);
1141 if (error < 0)
1142 goto out_free_server;
1144 spin_lock(&nfs_client_lock);
1145 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1146 list_add_tail(&server->master_link, &nfs_volume_list);
1147 spin_unlock(&nfs_client_lock);
1149 server->mount_time = jiffies;
1151 dprintk("<-- nfs_clone_server() = %p\n", server);
1152 return server;
1154 out_free_server:
1155 nfs_free_server(server);
1156 dprintk("<-- nfs_clone_server() = error %d\n", error);
1157 return ERR_PTR(error);
1160 #ifdef CONFIG_PROC_FS
1161 static struct proc_dir_entry *proc_fs_nfs;
1163 static int nfs_server_list_open(struct inode *inode, struct file *file);
1164 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1165 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1166 static void nfs_server_list_stop(struct seq_file *p, void *v);
1167 static int nfs_server_list_show(struct seq_file *m, void *v);
1169 static struct seq_operations nfs_server_list_ops = {
1170 .start = nfs_server_list_start,
1171 .next = nfs_server_list_next,
1172 .stop = nfs_server_list_stop,
1173 .show = nfs_server_list_show,
1176 static const struct file_operations nfs_server_list_fops = {
1177 .open = nfs_server_list_open,
1178 .read = seq_read,
1179 .llseek = seq_lseek,
1180 .release = seq_release,
1183 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1184 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1185 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1186 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1187 static int nfs_volume_list_show(struct seq_file *m, void *v);
1189 static struct seq_operations nfs_volume_list_ops = {
1190 .start = nfs_volume_list_start,
1191 .next = nfs_volume_list_next,
1192 .stop = nfs_volume_list_stop,
1193 .show = nfs_volume_list_show,
1196 static const struct file_operations nfs_volume_list_fops = {
1197 .open = nfs_volume_list_open,
1198 .read = seq_read,
1199 .llseek = seq_lseek,
1200 .release = seq_release,
1204 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1205 * we're dealing
1207 static int nfs_server_list_open(struct inode *inode, struct file *file)
1209 struct seq_file *m;
1210 int ret;
1212 ret = seq_open(file, &nfs_server_list_ops);
1213 if (ret < 0)
1214 return ret;
1216 m = file->private_data;
1217 m->private = PDE(inode)->data;
1219 return 0;
1223 * set up the iterator to start reading from the server list and return the first item
1225 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1227 struct list_head *_p;
1228 loff_t pos = *_pos;
1230 /* lock the list against modification */
1231 spin_lock(&nfs_client_lock);
1233 /* allow for the header line */
1234 if (!pos)
1235 return SEQ_START_TOKEN;
1236 pos--;
1238 /* find the n'th element in the list */
1239 list_for_each(_p, &nfs_client_list)
1240 if (!pos--)
1241 break;
1243 return _p != &nfs_client_list ? _p : NULL;
1247 * move to next server
1249 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1251 struct list_head *_p;
1253 (*pos)++;
1255 _p = v;
1256 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1258 return _p != &nfs_client_list ? _p : NULL;
1262 * clean up after reading from the transports list
1264 static void nfs_server_list_stop(struct seq_file *p, void *v)
1266 spin_unlock(&nfs_client_lock);
1270 * display a header line followed by a load of call lines
1272 static int nfs_server_list_show(struct seq_file *m, void *v)
1274 struct nfs_client *clp;
1276 /* display header on line 1 */
1277 if (v == SEQ_START_TOKEN) {
1278 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1279 return 0;
1282 /* display one transport per line on subsequent lines */
1283 clp = list_entry(v, struct nfs_client, cl_share_link);
1285 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1286 clp->cl_nfsversion,
1287 NIPQUAD(clp->cl_addr.sin_addr),
1288 ntohs(clp->cl_addr.sin_port),
1289 atomic_read(&clp->cl_count),
1290 clp->cl_hostname);
1292 return 0;
1296 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1298 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1300 struct seq_file *m;
1301 int ret;
1303 ret = seq_open(file, &nfs_volume_list_ops);
1304 if (ret < 0)
1305 return ret;
1307 m = file->private_data;
1308 m->private = PDE(inode)->data;
1310 return 0;
1314 * set up the iterator to start reading from the volume list and return the first item
1316 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1318 struct list_head *_p;
1319 loff_t pos = *_pos;
1321 /* lock the list against modification */
1322 spin_lock(&nfs_client_lock);
1324 /* allow for the header line */
1325 if (!pos)
1326 return SEQ_START_TOKEN;
1327 pos--;
1329 /* find the n'th element in the list */
1330 list_for_each(_p, &nfs_volume_list)
1331 if (!pos--)
1332 break;
1334 return _p != &nfs_volume_list ? _p : NULL;
1338 * move to next volume
1340 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1342 struct list_head *_p;
1344 (*pos)++;
1346 _p = v;
1347 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1349 return _p != &nfs_volume_list ? _p : NULL;
1353 * clean up after reading from the transports list
1355 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1357 spin_unlock(&nfs_client_lock);
1361 * display a header line followed by a load of call lines
1363 static int nfs_volume_list_show(struct seq_file *m, void *v)
1365 struct nfs_server *server;
1366 struct nfs_client *clp;
1367 char dev[8], fsid[17];
1369 /* display header on line 1 */
1370 if (v == SEQ_START_TOKEN) {
1371 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1372 return 0;
1374 /* display one transport per line on subsequent lines */
1375 server = list_entry(v, struct nfs_server, master_link);
1376 clp = server->nfs_client;
1378 snprintf(dev, 8, "%u:%u",
1379 MAJOR(server->s_dev), MINOR(server->s_dev));
1381 snprintf(fsid, 17, "%llx:%llx",
1382 (unsigned long long) server->fsid.major,
1383 (unsigned long long) server->fsid.minor);
1385 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1386 clp->cl_nfsversion,
1387 NIPQUAD(clp->cl_addr.sin_addr),
1388 ntohs(clp->cl_addr.sin_port),
1389 dev,
1390 fsid);
1392 return 0;
1396 * initialise the /proc/fs/nfsfs/ directory
1398 int __init nfs_fs_proc_init(void)
1400 struct proc_dir_entry *p;
1402 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1403 if (!proc_fs_nfs)
1404 goto error_0;
1406 proc_fs_nfs->owner = THIS_MODULE;
1408 /* a file of servers with which we're dealing */
1409 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1410 if (!p)
1411 goto error_1;
1413 p->proc_fops = &nfs_server_list_fops;
1414 p->owner = THIS_MODULE;
1416 /* a file of volumes that we have mounted */
1417 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1418 if (!p)
1419 goto error_2;
1421 p->proc_fops = &nfs_volume_list_fops;
1422 p->owner = THIS_MODULE;
1423 return 0;
1425 error_2:
1426 remove_proc_entry("servers", proc_fs_nfs);
1427 error_1:
1428 remove_proc_entry("nfsfs", proc_root_fs);
1429 error_0:
1430 return -ENOMEM;
1434 * clean up the /proc/fs/nfsfs/ directory
1436 void nfs_fs_proc_exit(void)
1438 remove_proc_entry("volumes", proc_fs_nfs);
1439 remove_proc_entry("servers", proc_fs_nfs);
1440 remove_proc_entry("nfsfs", proc_root_fs);
1443 #endif /* CONFIG_PROC_FS */