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