exportfs: remove old methods
[wrt350n-kernel.git] / fs / nfs / client.c
blob70587f383f10708ba33b5f0d7b3492fc20359487
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/sunrpc/xprtsock.h>
27 #include <linux/sunrpc/xprtrdma.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/nfs4_mount.h>
31 #include <linux/lockd/bind.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>
39 #include <asm/system.h>
41 #include "nfs4_fs.h"
42 #include "callback.h"
43 #include "delegation.h"
44 #include "iostat.h"
45 #include "internal.h"
47 #define NFSDBG_FACILITY NFSDBG_CLIENT
49 static DEFINE_SPINLOCK(nfs_client_lock);
50 static LIST_HEAD(nfs_client_list);
51 static LIST_HEAD(nfs_volume_list);
52 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55 * RPC cruft for NFS
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
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",
76 struct rpc_stat nfs_rpcstat = {
77 .program = &nfs_program
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,
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,
94 #endif /* CONFIG_NFS_V3_ACL */
97 * Allocate a shared client record
99 * Since these are allocated/deallocated very rarely, we don't
100 * bother putting them in a slab cache...
102 static struct nfs_client *nfs_alloc_client(const char *hostname,
103 const struct sockaddr_in *addr,
104 int nfsversion)
106 struct nfs_client *clp;
108 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
109 goto error_0;
111 if (nfsversion == 4) {
112 if (nfs_callback_up() < 0)
113 goto error_2;
114 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
117 atomic_set(&clp->cl_count, 1);
118 clp->cl_cons_state = NFS_CS_INITING;
120 clp->cl_nfsversion = nfsversion;
121 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
123 if (hostname) {
124 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
125 if (!clp->cl_hostname)
126 goto error_3;
129 INIT_LIST_HEAD(&clp->cl_superblocks);
130 clp->cl_rpcclient = ERR_PTR(-EINVAL);
132 #ifdef CONFIG_NFS_V4
133 init_rwsem(&clp->cl_sem);
134 INIT_LIST_HEAD(&clp->cl_delegations);
135 spin_lock_init(&clp->cl_lock);
136 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
137 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
138 clp->cl_boot_time = CURRENT_TIME;
139 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
140 #endif
142 return clp;
144 error_3:
145 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
146 nfs_callback_down();
147 error_2:
148 kfree(clp);
149 error_0:
150 return NULL;
153 static void nfs4_shutdown_client(struct nfs_client *clp)
155 #ifdef CONFIG_NFS_V4
156 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
157 nfs4_kill_renewd(clp);
158 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
159 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
160 nfs_idmap_delete(clp);
161 #endif
165 * Destroy a shared client record
167 static void nfs_free_client(struct nfs_client *clp)
169 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
171 nfs4_shutdown_client(clp);
173 /* -EIO all pending I/O */
174 if (!IS_ERR(clp->cl_rpcclient))
175 rpc_shutdown_client(clp->cl_rpcclient);
177 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
178 nfs_callback_down();
180 kfree(clp->cl_hostname);
181 kfree(clp);
183 dprintk("<-- nfs_free_client()\n");
187 * Release a reference to a shared client record
189 void nfs_put_client(struct nfs_client *clp)
191 if (!clp)
192 return;
194 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
196 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
197 list_del(&clp->cl_share_link);
198 spin_unlock(&nfs_client_lock);
200 BUG_ON(!list_empty(&clp->cl_superblocks));
202 nfs_free_client(clp);
207 * Find a client by address
208 * - caller must hold nfs_client_lock
210 static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
212 struct nfs_client *clp;
214 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
215 /* Don't match clients that failed to initialise properly */
216 if (clp->cl_cons_state < 0)
217 continue;
219 /* Different NFS versions cannot share the same nfs_client */
220 if (clp->cl_nfsversion != nfsversion)
221 continue;
223 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
224 sizeof(clp->cl_addr.sin_addr)) != 0)
225 continue;
227 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
228 goto found;
231 return NULL;
233 found:
234 atomic_inc(&clp->cl_count);
235 return clp;
239 * Find a client by IP address and protocol version
240 * - returns NULL if no such client
242 struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
244 struct nfs_client *clp;
246 spin_lock(&nfs_client_lock);
247 clp = __nfs_find_client(addr, nfsversion, 0);
248 spin_unlock(&nfs_client_lock);
249 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
250 nfs_put_client(clp);
251 clp = NULL;
253 return clp;
257 * Look up a client by IP address and protocol version
258 * - creates a new record if one doesn't yet exist
260 static struct nfs_client *nfs_get_client(const char *hostname,
261 const struct sockaddr_in *addr,
262 int nfsversion)
264 struct nfs_client *clp, *new = NULL;
265 int error;
267 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
268 hostname ?: "", NIPQUAD(addr->sin_addr),
269 addr->sin_port, nfsversion);
271 /* see if the client already exists */
272 do {
273 spin_lock(&nfs_client_lock);
275 clp = __nfs_find_client(addr, nfsversion, 1);
276 if (clp)
277 goto found_client;
278 if (new)
279 goto install_client;
281 spin_unlock(&nfs_client_lock);
283 new = nfs_alloc_client(hostname, addr, nfsversion);
284 } while (new);
286 return ERR_PTR(-ENOMEM);
288 /* install a new client and return with it unready */
289 install_client:
290 clp = new;
291 list_add(&clp->cl_share_link, &nfs_client_list);
292 spin_unlock(&nfs_client_lock);
293 dprintk("--> nfs_get_client() = %p [new]\n", clp);
294 return clp;
296 /* found an existing client
297 * - make sure it's ready before returning
299 found_client:
300 spin_unlock(&nfs_client_lock);
302 if (new)
303 nfs_free_client(new);
305 error = wait_event_interruptible(nfs_client_active_wq,
306 clp->cl_cons_state != NFS_CS_INITING);
307 if (error < 0) {
308 nfs_put_client(clp);
309 return ERR_PTR(-ERESTARTSYS);
312 if (clp->cl_cons_state < NFS_CS_READY) {
313 error = clp->cl_cons_state;
314 nfs_put_client(clp);
315 return ERR_PTR(error);
318 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
320 dprintk("--> nfs_get_client() = %p [share]\n", clp);
321 return clp;
325 * Mark a server as ready or failed
327 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
329 clp->cl_cons_state = state;
330 wake_up_all(&nfs_client_active_wq);
334 * Initialise the timeout values for a connection
336 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
337 unsigned int timeo, unsigned int retrans)
339 to->to_initval = timeo * HZ / 10;
340 to->to_retries = retrans;
341 if (!to->to_retries)
342 to->to_retries = 2;
344 switch (proto) {
345 case XPRT_TRANSPORT_TCP:
346 case XPRT_TRANSPORT_RDMA:
347 if (!to->to_initval)
348 to->to_initval = 60 * HZ;
349 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
350 to->to_initval = NFS_MAX_TCP_TIMEOUT;
351 to->to_increment = to->to_initval;
352 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
353 to->to_exponential = 0;
354 break;
355 case XPRT_TRANSPORT_UDP:
356 default:
357 if (!to->to_initval)
358 to->to_initval = 11 * HZ / 10;
359 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
360 to->to_initval = NFS_MAX_UDP_TIMEOUT;
361 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
362 to->to_exponential = 1;
363 break;
368 * Create an RPC client handle
370 static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
371 unsigned int timeo,
372 unsigned int retrans,
373 rpc_authflavor_t flavor,
374 int flags)
376 struct rpc_timeout timeparms;
377 struct rpc_clnt *clnt = NULL;
378 struct rpc_create_args args = {
379 .protocol = proto,
380 .address = (struct sockaddr *)&clp->cl_addr,
381 .addrsize = sizeof(clp->cl_addr),
382 .timeout = &timeparms,
383 .servername = clp->cl_hostname,
384 .program = &nfs_program,
385 .version = clp->rpc_ops->version,
386 .authflavor = flavor,
387 .flags = flags,
390 if (!IS_ERR(clp->cl_rpcclient))
391 return 0;
393 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
394 clp->retrans_timeo = timeparms.to_initval;
395 clp->retrans_count = timeparms.to_retries;
397 clnt = rpc_create(&args);
398 if (IS_ERR(clnt)) {
399 dprintk("%s: cannot create RPC client. Error = %ld\n",
400 __FUNCTION__, PTR_ERR(clnt));
401 return PTR_ERR(clnt);
404 clp->cl_rpcclient = clnt;
405 return 0;
409 * Version 2 or 3 client destruction
411 static void nfs_destroy_server(struct nfs_server *server)
413 if (!IS_ERR(server->client_acl))
414 rpc_shutdown_client(server->client_acl);
416 if (!(server->flags & NFS_MOUNT_NONLM))
417 lockd_down(); /* release rpc.lockd */
421 * Version 2 or 3 lockd setup
423 static int nfs_start_lockd(struct nfs_server *server)
425 int error = 0;
427 if (server->nfs_client->cl_nfsversion > 3)
428 goto out;
429 if (server->flags & NFS_MOUNT_NONLM)
430 goto out;
431 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
432 IPPROTO_TCP : IPPROTO_UDP);
433 if (error < 0)
434 server->flags |= NFS_MOUNT_NONLM;
435 else
436 server->destroy = nfs_destroy_server;
437 out:
438 return error;
442 * Initialise an NFSv3 ACL client connection
444 #ifdef CONFIG_NFS_V3_ACL
445 static void nfs_init_server_aclclient(struct nfs_server *server)
447 if (server->nfs_client->cl_nfsversion != 3)
448 goto out_noacl;
449 if (server->flags & NFS_MOUNT_NOACL)
450 goto out_noacl;
452 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
453 if (IS_ERR(server->client_acl))
454 goto out_noacl;
456 /* No errors! Assume that Sun nfsacls are supported */
457 server->caps |= NFS_CAP_ACLS;
458 return;
460 out_noacl:
461 server->caps &= ~NFS_CAP_ACLS;
463 #else
464 static inline void nfs_init_server_aclclient(struct nfs_server *server)
466 server->flags &= ~NFS_MOUNT_NOACL;
467 server->caps &= ~NFS_CAP_ACLS;
469 #endif
472 * Create a general RPC client
474 static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
476 struct nfs_client *clp = server->nfs_client;
478 server->client = rpc_clone_client(clp->cl_rpcclient);
479 if (IS_ERR(server->client)) {
480 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
481 return PTR_ERR(server->client);
484 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
485 struct rpc_auth *auth;
487 auth = rpcauth_create(pseudoflavour, server->client);
488 if (IS_ERR(auth)) {
489 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
490 return PTR_ERR(auth);
493 server->client->cl_softrtry = 0;
494 if (server->flags & NFS_MOUNT_SOFT)
495 server->client->cl_softrtry = 1;
497 server->client->cl_intr = 0;
498 if (server->flags & NFS4_MOUNT_INTR)
499 server->client->cl_intr = 1;
501 return 0;
505 * Initialise an NFS2 or NFS3 client
507 static int nfs_init_client(struct nfs_client *clp,
508 const struct nfs_parsed_mount_data *data)
510 int error;
512 if (clp->cl_cons_state == NFS_CS_READY) {
513 /* the client is already initialised */
514 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
515 return 0;
518 /* Check NFS protocol revision and initialize RPC op vector */
519 clp->rpc_ops = &nfs_v2_clientops;
520 #ifdef CONFIG_NFS_V3
521 if (clp->cl_nfsversion == 3)
522 clp->rpc_ops = &nfs_v3_clientops;
523 #endif
525 * Create a client RPC handle for doing FSSTAT with UNIX auth only
526 * - RFC 2623, sec 2.3.2
528 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
529 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
530 if (error < 0)
531 goto error;
532 nfs_mark_client_ready(clp, NFS_CS_READY);
533 return 0;
535 error:
536 nfs_mark_client_ready(clp, error);
537 dprintk("<-- nfs_init_client() = xerror %d\n", error);
538 return error;
542 * Create a version 2 or 3 client
544 static int nfs_init_server(struct nfs_server *server,
545 const struct nfs_parsed_mount_data *data)
547 struct nfs_client *clp;
548 int error, nfsvers = 2;
550 dprintk("--> nfs_init_server()\n");
552 #ifdef CONFIG_NFS_V3
553 if (data->flags & NFS_MOUNT_VER3)
554 nfsvers = 3;
555 #endif
557 /* Allocate or find a client reference we can use */
558 clp = nfs_get_client(data->nfs_server.hostname,
559 &data->nfs_server.address, nfsvers);
560 if (IS_ERR(clp)) {
561 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
562 return PTR_ERR(clp);
565 error = nfs_init_client(clp, data);
566 if (error < 0)
567 goto error;
569 server->nfs_client = clp;
571 /* Initialise the client representation from the mount data */
572 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
574 if (data->rsize)
575 server->rsize = nfs_block_size(data->rsize, NULL);
576 if (data->wsize)
577 server->wsize = nfs_block_size(data->wsize, NULL);
579 server->acregmin = data->acregmin * HZ;
580 server->acregmax = data->acregmax * HZ;
581 server->acdirmin = data->acdirmin * HZ;
582 server->acdirmax = data->acdirmax * HZ;
584 /* Start lockd here, before we might error out */
585 error = nfs_start_lockd(server);
586 if (error < 0)
587 goto error;
589 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
590 if (error < 0)
591 goto error;
593 server->namelen = data->namlen;
594 /* Create a client RPC handle for the NFSv3 ACL management interface */
595 nfs_init_server_aclclient(server);
596 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
597 return 0;
599 error:
600 server->nfs_client = NULL;
601 nfs_put_client(clp);
602 dprintk("<-- nfs_init_server() = xerror %d\n", error);
603 return error;
607 * Load up the server record from information gained in an fsinfo record
609 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
611 unsigned long max_rpc_payload;
613 /* Work out a lot of parameters */
614 if (server->rsize == 0)
615 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
616 if (server->wsize == 0)
617 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
619 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
620 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
621 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
622 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
624 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
625 if (server->rsize > max_rpc_payload)
626 server->rsize = max_rpc_payload;
627 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
628 server->rsize = NFS_MAX_FILE_IO_SIZE;
629 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
631 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
633 if (server->wsize > max_rpc_payload)
634 server->wsize = max_rpc_payload;
635 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
636 server->wsize = NFS_MAX_FILE_IO_SIZE;
637 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
638 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
640 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
641 if (server->dtsize > PAGE_CACHE_SIZE)
642 server->dtsize = PAGE_CACHE_SIZE;
643 if (server->dtsize > server->rsize)
644 server->dtsize = server->rsize;
646 if (server->flags & NFS_MOUNT_NOAC) {
647 server->acregmin = server->acregmax = 0;
648 server->acdirmin = server->acdirmax = 0;
651 server->maxfilesize = fsinfo->maxfilesize;
653 /* We're airborne Set socket buffersize */
654 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
658 * Probe filesystem information, including the FSID on v2/v3
660 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
662 struct nfs_fsinfo fsinfo;
663 struct nfs_client *clp = server->nfs_client;
664 int error;
666 dprintk("--> nfs_probe_fsinfo()\n");
668 if (clp->rpc_ops->set_capabilities != NULL) {
669 error = clp->rpc_ops->set_capabilities(server, mntfh);
670 if (error < 0)
671 goto out_error;
674 fsinfo.fattr = fattr;
675 nfs_fattr_init(fattr);
676 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
677 if (error < 0)
678 goto out_error;
680 nfs_server_set_fsinfo(server, &fsinfo);
681 error = bdi_init(&server->backing_dev_info);
682 if (error)
683 goto out_error;
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 bdi_destroy(&server->backing_dev_info);
765 kfree(server);
766 nfs_release_automount_timer();
767 dprintk("<-- nfs_free_server()\n");
771 * Create a version 2 or 3 volume record
772 * - keyed on server and FSID
774 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
775 struct nfs_fh *mntfh)
777 struct nfs_server *server;
778 struct nfs_fattr fattr;
779 int error;
781 server = nfs_alloc_server();
782 if (!server)
783 return ERR_PTR(-ENOMEM);
785 /* Get a client representation */
786 error = nfs_init_server(server, data);
787 if (error < 0)
788 goto error;
790 BUG_ON(!server->nfs_client);
791 BUG_ON(!server->nfs_client->rpc_ops);
792 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
794 /* Probe the root fh to retrieve its FSID */
795 error = nfs_probe_fsinfo(server, mntfh, &fattr);
796 if (error < 0)
797 goto error;
798 if (server->nfs_client->rpc_ops->version == 3) {
799 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
800 server->namelen = NFS3_MAXNAMLEN;
801 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
802 server->caps |= NFS_CAP_READDIRPLUS;
803 } else {
804 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
805 server->namelen = NFS2_MAXNAMLEN;
808 if (!(fattr.valid & NFS_ATTR_FATTR)) {
809 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
810 if (error < 0) {
811 dprintk("nfs_create_server: getattr error = %d\n", -error);
812 goto error;
815 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
817 dprintk("Server FSID: %llx:%llx\n",
818 (unsigned long long) server->fsid.major,
819 (unsigned long long) server->fsid.minor);
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);
825 spin_lock(&nfs_client_lock);
826 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
827 list_add_tail(&server->master_link, &nfs_volume_list);
828 spin_unlock(&nfs_client_lock);
830 server->mount_time = jiffies;
831 return server;
833 error:
834 nfs_free_server(server);
835 return ERR_PTR(error);
838 #ifdef CONFIG_NFS_V4
840 * Initialise an NFS4 client record
842 static int nfs4_init_client(struct nfs_client *clp,
843 int proto, int timeo, int retrans,
844 const char *ip_addr,
845 rpc_authflavor_t authflavour)
847 int error;
849 if (clp->cl_cons_state == NFS_CS_READY) {
850 /* the client is initialised already */
851 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
852 return 0;
855 /* Check NFS protocol revision and initialize RPC op vector */
856 clp->rpc_ops = &nfs_v4_clientops;
858 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
859 RPC_CLNT_CREATE_DISCRTRY);
860 if (error < 0)
861 goto error;
862 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
864 error = nfs_idmap_new(clp);
865 if (error < 0) {
866 dprintk("%s: failed to create idmapper. Error = %d\n",
867 __FUNCTION__, error);
868 goto error;
870 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
872 nfs_mark_client_ready(clp, NFS_CS_READY);
873 return 0;
875 error:
876 nfs_mark_client_ready(clp, error);
877 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
878 return error;
882 * Set up an NFS4 client
884 static int nfs4_set_client(struct nfs_server *server,
885 const char *hostname, const struct sockaddr_in *addr,
886 const char *ip_addr,
887 rpc_authflavor_t authflavour,
888 int proto, int timeo, int retrans)
890 struct nfs_client *clp;
891 int error;
893 dprintk("--> nfs4_set_client()\n");
895 /* Allocate or find a client reference we can use */
896 clp = nfs_get_client(hostname, addr, 4);
897 if (IS_ERR(clp)) {
898 error = PTR_ERR(clp);
899 goto error;
901 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
902 if (error < 0)
903 goto error_put;
905 server->nfs_client = clp;
906 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
907 return 0;
909 error_put:
910 nfs_put_client(clp);
911 error:
912 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
913 return error;
917 * Create a version 4 volume record
919 static int nfs4_init_server(struct nfs_server *server,
920 const struct nfs_parsed_mount_data *data)
922 int error;
924 dprintk("--> nfs4_init_server()\n");
926 /* Initialise the client representation from the mount data */
927 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
928 server->caps |= NFS_CAP_ATOMIC_OPEN;
930 if (data->rsize)
931 server->rsize = nfs_block_size(data->rsize, NULL);
932 if (data->wsize)
933 server->wsize = nfs_block_size(data->wsize, NULL);
935 server->acregmin = data->acregmin * HZ;
936 server->acregmax = data->acregmax * HZ;
937 server->acdirmin = data->acdirmin * HZ;
938 server->acdirmax = data->acdirmax * HZ;
940 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
942 /* Done */
943 dprintk("<-- nfs4_init_server() = %d\n", error);
944 return error;
948 * Create a version 4 volume record
949 * - keyed on server and FSID
951 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
952 struct nfs_fh *mntfh)
954 struct nfs_fattr fattr;
955 struct nfs_server *server;
956 int error;
958 dprintk("--> nfs4_create_server()\n");
960 server = nfs_alloc_server();
961 if (!server)
962 return ERR_PTR(-ENOMEM);
964 /* Get a client record */
965 error = nfs4_set_client(server,
966 data->nfs_server.hostname,
967 &data->nfs_server.address,
968 data->client_address,
969 data->auth_flavors[0],
970 data->nfs_server.protocol,
971 data->timeo, data->retrans);
972 if (error < 0)
973 goto error;
975 /* set up the general RPC client */
976 error = nfs4_init_server(server, data);
977 if (error < 0)
978 goto error;
980 BUG_ON(!server->nfs_client);
981 BUG_ON(!server->nfs_client->rpc_ops);
982 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
984 /* Probe the root fh to retrieve its FSID */
985 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
986 if (error < 0)
987 goto error;
989 dprintk("Server FSID: %llx:%llx\n",
990 (unsigned long long) server->fsid.major,
991 (unsigned long long) server->fsid.minor);
992 dprintk("Mount FH: %d\n", mntfh->size);
994 error = nfs_probe_fsinfo(server, mntfh, &fattr);
995 if (error < 0)
996 goto error;
998 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
999 server->namelen = NFS4_MAXNAMLEN;
1001 BUG_ON(!server->nfs_client);
1002 BUG_ON(!server->nfs_client->rpc_ops);
1003 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1005 spin_lock(&nfs_client_lock);
1006 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1007 list_add_tail(&server->master_link, &nfs_volume_list);
1008 spin_unlock(&nfs_client_lock);
1010 server->mount_time = jiffies;
1011 dprintk("<-- nfs4_create_server() = %p\n", server);
1012 return server;
1014 error:
1015 nfs_free_server(server);
1016 dprintk("<-- nfs4_create_server() = error %d\n", error);
1017 return ERR_PTR(error);
1021 * Create an NFS4 referral server record
1023 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1024 struct nfs_fh *mntfh)
1026 struct nfs_client *parent_client;
1027 struct nfs_server *server, *parent_server;
1028 struct nfs_fattr fattr;
1029 int error;
1031 dprintk("--> nfs4_create_referral_server()\n");
1033 server = nfs_alloc_server();
1034 if (!server)
1035 return ERR_PTR(-ENOMEM);
1037 parent_server = NFS_SB(data->sb);
1038 parent_client = parent_server->nfs_client;
1040 /* Get a client representation.
1041 * Note: NFSv4 always uses TCP, */
1042 error = nfs4_set_client(server, data->hostname, data->addr,
1043 parent_client->cl_ipaddr,
1044 data->authflavor,
1045 parent_server->client->cl_xprt->prot,
1046 parent_client->retrans_timeo,
1047 parent_client->retrans_count);
1048 if (error < 0)
1049 goto error;
1051 /* Initialise the client representation from the parent server */
1052 nfs_server_copy_userdata(server, parent_server);
1053 server->caps |= NFS_CAP_ATOMIC_OPEN;
1055 error = nfs_init_server_rpcclient(server, data->authflavor);
1056 if (error < 0)
1057 goto error;
1059 BUG_ON(!server->nfs_client);
1060 BUG_ON(!server->nfs_client->rpc_ops);
1061 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1063 /* Probe the root fh to retrieve its FSID and filehandle */
1064 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1065 if (error < 0)
1066 goto error;
1068 /* probe the filesystem info for this server filesystem */
1069 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1070 if (error < 0)
1071 goto error;
1073 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1074 server->namelen = NFS4_MAXNAMLEN;
1076 dprintk("Referral FSID: %llx:%llx\n",
1077 (unsigned long long) server->fsid.major,
1078 (unsigned long long) server->fsid.minor);
1080 spin_lock(&nfs_client_lock);
1081 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1082 list_add_tail(&server->master_link, &nfs_volume_list);
1083 spin_unlock(&nfs_client_lock);
1085 server->mount_time = jiffies;
1087 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1088 return server;
1090 error:
1091 nfs_free_server(server);
1092 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1093 return ERR_PTR(error);
1096 #endif /* CONFIG_NFS_V4 */
1099 * Clone an NFS2, NFS3 or NFS4 server record
1101 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1102 struct nfs_fh *fh,
1103 struct nfs_fattr *fattr)
1105 struct nfs_server *server;
1106 struct nfs_fattr fattr_fsinfo;
1107 int error;
1109 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1110 (unsigned long long) fattr->fsid.major,
1111 (unsigned long long) fattr->fsid.minor);
1113 server = nfs_alloc_server();
1114 if (!server)
1115 return ERR_PTR(-ENOMEM);
1117 /* Copy data from the source */
1118 server->nfs_client = source->nfs_client;
1119 atomic_inc(&server->nfs_client->cl_count);
1120 nfs_server_copy_userdata(server, source);
1122 server->fsid = fattr->fsid;
1124 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1125 if (error < 0)
1126 goto out_free_server;
1127 if (!IS_ERR(source->client_acl))
1128 nfs_init_server_aclclient(server);
1130 /* probe the filesystem info for this server filesystem */
1131 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1132 if (error < 0)
1133 goto out_free_server;
1135 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1136 server->namelen = NFS4_MAXNAMLEN;
1138 dprintk("Cloned FSID: %llx:%llx\n",
1139 (unsigned long long) server->fsid.major,
1140 (unsigned long long) server->fsid.minor);
1142 error = nfs_start_lockd(server);
1143 if (error < 0)
1144 goto out_free_server;
1146 spin_lock(&nfs_client_lock);
1147 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1148 list_add_tail(&server->master_link, &nfs_volume_list);
1149 spin_unlock(&nfs_client_lock);
1151 server->mount_time = jiffies;
1153 dprintk("<-- nfs_clone_server() = %p\n", server);
1154 return server;
1156 out_free_server:
1157 nfs_free_server(server);
1158 dprintk("<-- nfs_clone_server() = error %d\n", error);
1159 return ERR_PTR(error);
1162 #ifdef CONFIG_PROC_FS
1163 static struct proc_dir_entry *proc_fs_nfs;
1165 static int nfs_server_list_open(struct inode *inode, struct file *file);
1166 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1167 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1168 static void nfs_server_list_stop(struct seq_file *p, void *v);
1169 static int nfs_server_list_show(struct seq_file *m, void *v);
1171 static struct seq_operations nfs_server_list_ops = {
1172 .start = nfs_server_list_start,
1173 .next = nfs_server_list_next,
1174 .stop = nfs_server_list_stop,
1175 .show = nfs_server_list_show,
1178 static const struct file_operations nfs_server_list_fops = {
1179 .open = nfs_server_list_open,
1180 .read = seq_read,
1181 .llseek = seq_lseek,
1182 .release = seq_release,
1185 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1186 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1187 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1188 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1189 static int nfs_volume_list_show(struct seq_file *m, void *v);
1191 static struct seq_operations nfs_volume_list_ops = {
1192 .start = nfs_volume_list_start,
1193 .next = nfs_volume_list_next,
1194 .stop = nfs_volume_list_stop,
1195 .show = nfs_volume_list_show,
1198 static const struct file_operations nfs_volume_list_fops = {
1199 .open = nfs_volume_list_open,
1200 .read = seq_read,
1201 .llseek = seq_lseek,
1202 .release = seq_release,
1206 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1207 * we're dealing
1209 static int nfs_server_list_open(struct inode *inode, struct file *file)
1211 struct seq_file *m;
1212 int ret;
1214 ret = seq_open(file, &nfs_server_list_ops);
1215 if (ret < 0)
1216 return ret;
1218 m = file->private_data;
1219 m->private = PDE(inode)->data;
1221 return 0;
1225 * set up the iterator to start reading from the server list and return the first item
1227 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1229 /* lock the list against modification */
1230 spin_lock(&nfs_client_lock);
1231 return seq_list_start_head(&nfs_client_list, *_pos);
1235 * move to next server
1237 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1239 return seq_list_next(v, &nfs_client_list, pos);
1243 * clean up after reading from the transports list
1245 static void nfs_server_list_stop(struct seq_file *p, void *v)
1247 spin_unlock(&nfs_client_lock);
1251 * display a header line followed by a load of call lines
1253 static int nfs_server_list_show(struct seq_file *m, void *v)
1255 struct nfs_client *clp;
1257 /* display header on line 1 */
1258 if (v == &nfs_client_list) {
1259 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1260 return 0;
1263 /* display one transport per line on subsequent lines */
1264 clp = list_entry(v, struct nfs_client, cl_share_link);
1266 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1267 clp->cl_nfsversion,
1268 NIPQUAD(clp->cl_addr.sin_addr),
1269 ntohs(clp->cl_addr.sin_port),
1270 atomic_read(&clp->cl_count),
1271 clp->cl_hostname);
1273 return 0;
1277 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1279 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1281 struct seq_file *m;
1282 int ret;
1284 ret = seq_open(file, &nfs_volume_list_ops);
1285 if (ret < 0)
1286 return ret;
1288 m = file->private_data;
1289 m->private = PDE(inode)->data;
1291 return 0;
1295 * set up the iterator to start reading from the volume list and return the first item
1297 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1299 /* lock the list against modification */
1300 spin_lock(&nfs_client_lock);
1301 return seq_list_start_head(&nfs_volume_list, *_pos);
1305 * move to next volume
1307 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1309 return seq_list_next(v, &nfs_volume_list, pos);
1313 * clean up after reading from the transports list
1315 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1317 spin_unlock(&nfs_client_lock);
1321 * display a header line followed by a load of call lines
1323 static int nfs_volume_list_show(struct seq_file *m, void *v)
1325 struct nfs_server *server;
1326 struct nfs_client *clp;
1327 char dev[8], fsid[17];
1329 /* display header on line 1 */
1330 if (v == &nfs_volume_list) {
1331 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1332 return 0;
1334 /* display one transport per line on subsequent lines */
1335 server = list_entry(v, struct nfs_server, master_link);
1336 clp = server->nfs_client;
1338 snprintf(dev, 8, "%u:%u",
1339 MAJOR(server->s_dev), MINOR(server->s_dev));
1341 snprintf(fsid, 17, "%llx:%llx",
1342 (unsigned long long) server->fsid.major,
1343 (unsigned long long) server->fsid.minor);
1345 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1346 clp->cl_nfsversion,
1347 NIPQUAD(clp->cl_addr.sin_addr),
1348 ntohs(clp->cl_addr.sin_port),
1349 dev,
1350 fsid);
1352 return 0;
1356 * initialise the /proc/fs/nfsfs/ directory
1358 int __init nfs_fs_proc_init(void)
1360 struct proc_dir_entry *p;
1362 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1363 if (!proc_fs_nfs)
1364 goto error_0;
1366 proc_fs_nfs->owner = THIS_MODULE;
1368 /* a file of servers with which we're dealing */
1369 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1370 if (!p)
1371 goto error_1;
1373 p->proc_fops = &nfs_server_list_fops;
1374 p->owner = THIS_MODULE;
1376 /* a file of volumes that we have mounted */
1377 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1378 if (!p)
1379 goto error_2;
1381 p->proc_fops = &nfs_volume_list_fops;
1382 p->owner = THIS_MODULE;
1383 return 0;
1385 error_2:
1386 remove_proc_entry("servers", proc_fs_nfs);
1387 error_1:
1388 remove_proc_entry("nfsfs", proc_root_fs);
1389 error_0:
1390 return -ENOMEM;
1394 * clean up the /proc/fs/nfsfs/ directory
1396 void nfs_fs_proc_exit(void)
1398 remove_proc_entry("volumes", proc_fs_nfs);
1399 remove_proc_entry("servers", proc_fs_nfs);
1400 remove_proc_entry("nfsfs", proc_root_fs);
1403 #endif /* CONFIG_PROC_FS */