blob: 03d9bed7849a4a566a611d5c08ec1ad0a1467e26 [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
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#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>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#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>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
48
49#define NFSDBG_FACILITY NFSDBG_CLIENT
50
51static DEFINE_SPINLOCK(nfs_client_lock);
52static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040053static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040054static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55
56/*
David Howells5006a762006-08-22 20:06:12 -040057 * RPC cruft for NFS
58 */
59static struct rpc_version *nfs_version[5] = {
60 [2] = &nfs_version2,
61#ifdef CONFIG_NFS_V3
62 [3] = &nfs_version3,
63#endif
64#ifdef CONFIG_NFS_V4
65 [4] = &nfs_version4,
66#endif
67};
68
69struct rpc_program nfs_program = {
70 .name = "nfs",
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
76};
77
78struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
80};
81
82
83#ifdef CONFIG_NFS_V3_ACL
84static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
87};
88
89struct rpc_program nfsacl_program = {
90 .name = "nfsacl",
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
95};
96#endif /* CONFIG_NFS_V3_ACL */
97
Trond Myklebust3a498022007-12-14 14:56:04 -050098struct nfs_client_initdata {
99 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500100 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500101 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500102 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust3a498022007-12-14 14:56:04 -0500103};
104
David Howells5006a762006-08-22 20:06:12 -0400105/*
David Howells24c8dbb2006-08-22 20:06:10 -0400106 * Allocate a shared client record
107 *
108 * Since these are allocated/deallocated very rarely, we don't
109 * bother putting them in a slab cache...
110 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500111static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400112{
113 struct nfs_client *clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400114
115 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
116 goto error_0;
117
Trond Myklebust40c553192007-12-14 14:56:07 -0500118 clp->rpc_ops = cl_init->rpc_ops;
119
120 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400121 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
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500129 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
130 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400131
Trond Myklebust3a498022007-12-14 14:56:04 -0500132 if (cl_init->hostname) {
133 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400134 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);
David Howells24c8dbb2006-08-22 20:06:10 -0400144 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000145 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400146 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
147 clp->cl_boot_time = CURRENT_TIME;
148 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
149#endif
150
151 return clp;
152
153error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400154 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
155 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400156error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400157 kfree(clp);
158error_0:
159 return NULL;
160}
161
Trond Myklebust5dd31772006-08-24 01:03:05 -0400162static void nfs4_shutdown_client(struct nfs_client *clp)
163{
164#ifdef CONFIG_NFS_V4
165 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
166 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400167 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400168 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
169 nfs_idmap_delete(clp);
170#endif
171}
172
David Howells24c8dbb2006-08-22 20:06:10 -0400173/*
174 * Destroy a shared client record
175 */
176static void nfs_free_client(struct nfs_client *clp)
177{
Trond Myklebust40c553192007-12-14 14:56:07 -0500178 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400179
Trond Myklebust5dd31772006-08-24 01:03:05 -0400180 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400181
182 /* -EIO all pending I/O */
183 if (!IS_ERR(clp->cl_rpcclient))
184 rpc_shutdown_client(clp->cl_rpcclient);
185
186 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
187 nfs_callback_down();
188
David Howells24c8dbb2006-08-22 20:06:10 -0400189 kfree(clp->cl_hostname);
190 kfree(clp);
191
192 dprintk("<-- nfs_free_client()\n");
193}
194
195/*
196 * Release a reference to a shared client record
197 */
198void nfs_put_client(struct nfs_client *clp)
199{
David Howells27ba8512006-07-30 14:40:56 -0400200 if (!clp)
201 return;
202
David Howells24c8dbb2006-08-22 20:06:10 -0400203 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
204
205 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
206 list_del(&clp->cl_share_link);
207 spin_unlock(&nfs_client_lock);
208
209 BUG_ON(!list_empty(&clp->cl_superblocks));
210
211 nfs_free_client(clp);
212 }
213}
214
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500215static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
216 const struct sockaddr_in *sa2)
217{
218 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
219}
220
221static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
222 const struct sockaddr_in6 *sa2)
223{
224 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
225}
226
227static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
228 const struct sockaddr *sa2)
229{
230 switch (sa1->sa_family) {
231 case AF_INET:
232 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
233 (const struct sockaddr_in *)sa2);
234 case AF_INET6:
235 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
236 (const struct sockaddr_in6 *)sa2);
237 }
238 BUG();
239}
240
David Howells24c8dbb2006-08-22 20:06:10 -0400241/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500242 * Find a client by IP address and protocol version
243 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400244 */
Chuck Leverff052642007-12-10 14:58:44 -0500245struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500246{
247 struct nfs_client *clp;
248
249 spin_lock(&nfs_client_lock);
250 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500251 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
252
Trond Myklebustc81468a2007-12-14 14:56:05 -0500253 /* Don't match clients that failed to initialise properly */
254 if (clp->cl_cons_state != NFS_CS_READY)
255 continue;
256
257 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500258 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500259 continue;
260
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500261 if (addr->sa_family != clap->sa_family)
262 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500263 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500264 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500265 continue;
266
267 atomic_inc(&clp->cl_count);
268 spin_unlock(&nfs_client_lock);
269 return clp;
270 }
271 spin_unlock(&nfs_client_lock);
272 return NULL;
273}
274
275/*
276 * Find an nfs_client on the list that matches the initialisation data
277 * that is supplied.
278 */
279static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400280{
281 struct nfs_client *clp;
282
283 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700284 /* Don't match clients that failed to initialise properly */
285 if (clp->cl_cons_state < 0)
286 continue;
287
David Howells24c8dbb2006-08-22 20:06:10 -0400288 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500289 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400290 continue;
291
Trond Myklebustc81468a2007-12-14 14:56:05 -0500292 /* Match the full socket address */
293 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400294 continue;
295
Trond Myklebustc81468a2007-12-14 14:56:05 -0500296 atomic_inc(&clp->cl_count);
297 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400298 }
David Howells24c8dbb2006-08-22 20:06:10 -0400299 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400300}
301
302/*
303 * Look up a client by IP address and protocol version
304 * - creates a new record if one doesn't yet exist
305 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500306static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400307{
308 struct nfs_client *clp, *new = NULL;
309 int error;
310
Chuck Leverd7422c42007-12-10 14:58:51 -0500311 dprintk("--> nfs_get_client(%s,v%u)\n",
312 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400313
314 /* see if the client already exists */
315 do {
316 spin_lock(&nfs_client_lock);
317
Trond Myklebustc81468a2007-12-14 14:56:05 -0500318 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400319 if (clp)
320 goto found_client;
321 if (new)
322 goto install_client;
323
324 spin_unlock(&nfs_client_lock);
325
Trond Myklebust3a498022007-12-14 14:56:04 -0500326 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400327 } while (new);
328
329 return ERR_PTR(-ENOMEM);
330
331 /* install a new client and return with it unready */
332install_client:
333 clp = new;
334 list_add(&clp->cl_share_link, &nfs_client_list);
335 spin_unlock(&nfs_client_lock);
336 dprintk("--> nfs_get_client() = %p [new]\n", clp);
337 return clp;
338
339 /* found an existing client
340 * - make sure it's ready before returning
341 */
342found_client:
343 spin_unlock(&nfs_client_lock);
344
345 if (new)
346 nfs_free_client(new);
347
Linus Torvalds83250492006-10-08 17:28:25 -0700348 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400349 clp->cl_cons_state != NFS_CS_INITING);
350 if (error < 0) {
351 nfs_put_client(clp);
352 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400353 }
354
355 if (clp->cl_cons_state < NFS_CS_READY) {
356 error = clp->cl_cons_state;
357 nfs_put_client(clp);
358 return ERR_PTR(error);
359 }
360
David Howells54ceac42006-08-22 20:06:13 -0400361 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
362
David Howells24c8dbb2006-08-22 20:06:10 -0400363 dprintk("--> nfs_get_client() = %p [share]\n", clp);
364 return clp;
365}
366
367/*
368 * Mark a server as ready or failed
369 */
David Howells54ceac42006-08-22 20:06:13 -0400370static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400371{
372 clp->cl_cons_state = state;
373 wake_up_all(&nfs_client_active_wq);
374}
David Howells5006a762006-08-22 20:06:12 -0400375
376/*
377 * Initialise the timeout values for a connection
378 */
379static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
380 unsigned int timeo, unsigned int retrans)
381{
382 to->to_initval = timeo * HZ / 10;
383 to->to_retries = retrans;
384 if (!to->to_retries)
385 to->to_retries = 2;
386
387 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400388 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400389 case XPRT_TRANSPORT_RDMA:
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500390 if (to->to_initval == 0)
David Howells5006a762006-08-22 20:06:12 -0400391 to->to_initval = 60 * HZ;
392 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
393 to->to_initval = NFS_MAX_TCP_TIMEOUT;
394 to->to_increment = to->to_initval;
395 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500396 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
397 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
398 if (to->to_maxval < to->to_initval)
399 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400400 to->to_exponential = 0;
401 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400402 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400403 default:
404 if (!to->to_initval)
405 to->to_initval = 11 * HZ / 10;
406 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
407 to->to_initval = NFS_MAX_UDP_TIMEOUT;
408 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
409 to->to_exponential = 1;
410 break;
411 }
412}
413
414/*
415 * Create an RPC client handle
416 */
David Howells54ceac42006-08-22 20:06:13 -0400417static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
Trond Myklebust33170232007-12-20 16:03:59 -0500418 const struct rpc_timeout *timeparms,
419 rpc_authflavor_t flavor,
420 int flags)
David Howells5006a762006-08-22 20:06:12 -0400421{
David Howells5006a762006-08-22 20:06:12 -0400422 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400423 struct rpc_create_args args = {
424 .protocol = proto,
425 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500426 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500427 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400428 .servername = clp->cl_hostname,
429 .program = &nfs_program,
430 .version = clp->rpc_ops->version,
431 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500432 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400433 };
David Howells5006a762006-08-22 20:06:12 -0400434
435 if (!IS_ERR(clp->cl_rpcclient))
436 return 0;
437
Chuck Lever41877d22006-08-22 20:06:20 -0400438 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400439 if (IS_ERR(clnt)) {
440 dprintk("%s: cannot create RPC client. Error = %ld\n",
441 __FUNCTION__, PTR_ERR(clnt));
442 return PTR_ERR(clnt);
443 }
444
David Howells5006a762006-08-22 20:06:12 -0400445 clp->cl_rpcclient = clnt;
446 return 0;
447}
David Howells54ceac42006-08-22 20:06:13 -0400448
449/*
450 * Version 2 or 3 client destruction
451 */
452static void nfs_destroy_server(struct nfs_server *server)
453{
David Howells54ceac42006-08-22 20:06:13 -0400454 if (!(server->flags & NFS_MOUNT_NONLM))
455 lockd_down(); /* release rpc.lockd */
456}
457
458/*
459 * Version 2 or 3 lockd setup
460 */
461static int nfs_start_lockd(struct nfs_server *server)
462{
463 int error = 0;
464
Trond Myklebust40c553192007-12-14 14:56:07 -0500465 if (server->nfs_client->rpc_ops->version > 3)
David Howells54ceac42006-08-22 20:06:13 -0400466 goto out;
467 if (server->flags & NFS_MOUNT_NONLM)
468 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700469 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
470 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400471 if (error < 0)
472 server->flags |= NFS_MOUNT_NONLM;
473 else
474 server->destroy = nfs_destroy_server;
475out:
476 return error;
477}
478
479/*
480 * Initialise an NFSv3 ACL client connection
481 */
482#ifdef CONFIG_NFS_V3_ACL
483static void nfs_init_server_aclclient(struct nfs_server *server)
484{
Trond Myklebust40c553192007-12-14 14:56:07 -0500485 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400486 goto out_noacl;
487 if (server->flags & NFS_MOUNT_NOACL)
488 goto out_noacl;
489
490 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
491 if (IS_ERR(server->client_acl))
492 goto out_noacl;
493
494 /* No errors! Assume that Sun nfsacls are supported */
495 server->caps |= NFS_CAP_ACLS;
496 return;
497
498out_noacl:
499 server->caps &= ~NFS_CAP_ACLS;
500}
501#else
502static inline void nfs_init_server_aclclient(struct nfs_server *server)
503{
504 server->flags &= ~NFS_MOUNT_NOACL;
505 server->caps &= ~NFS_CAP_ACLS;
506}
507#endif
508
509/*
510 * Create a general RPC client
511 */
Trond Myklebust33170232007-12-20 16:03:59 -0500512static int nfs_init_server_rpcclient(struct nfs_server *server,
513 const struct rpc_timeout *timeo,
514 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400515{
516 struct nfs_client *clp = server->nfs_client;
517
518 server->client = rpc_clone_client(clp->cl_rpcclient);
519 if (IS_ERR(server->client)) {
520 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
521 return PTR_ERR(server->client);
522 }
523
Trond Myklebust33170232007-12-20 16:03:59 -0500524 memcpy(&server->client->cl_timeout_default,
525 timeo,
526 sizeof(server->client->cl_timeout_default));
527 server->client->cl_timeout = &server->client->cl_timeout_default;
528
David Howells54ceac42006-08-22 20:06:13 -0400529 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
530 struct rpc_auth *auth;
531
532 auth = rpcauth_create(pseudoflavour, server->client);
533 if (IS_ERR(auth)) {
534 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
535 return PTR_ERR(auth);
536 }
537 }
538 server->client->cl_softrtry = 0;
539 if (server->flags & NFS_MOUNT_SOFT)
540 server->client->cl_softrtry = 1;
541
542 server->client->cl_intr = 0;
543 if (server->flags & NFS4_MOUNT_INTR)
544 server->client->cl_intr = 1;
545
546 return 0;
547}
548
549/*
550 * Initialise an NFS2 or NFS3 client
551 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400552static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500553 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400554 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400555{
David Howells54ceac42006-08-22 20:06:13 -0400556 int error;
557
558 if (clp->cl_cons_state == NFS_CS_READY) {
559 /* the client is already initialised */
560 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
561 return 0;
562 }
563
David Howells54ceac42006-08-22 20:06:13 -0400564 /*
565 * Create a client RPC handle for doing FSSTAT with UNIX auth only
566 * - RFC 2623, sec 2.3.2
567 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400568 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
Trond Myklebust33170232007-12-20 16:03:59 -0500569 timeparms, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400570 if (error < 0)
571 goto error;
572 nfs_mark_client_ready(clp, NFS_CS_READY);
573 return 0;
574
575error:
576 nfs_mark_client_ready(clp, error);
577 dprintk("<-- nfs_init_client() = xerror %d\n", error);
578 return error;
579}
580
581/*
582 * Create a version 2 or 3 client
583 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400584static int nfs_init_server(struct nfs_server *server,
585 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400586{
Trond Myklebust3a498022007-12-14 14:56:04 -0500587 struct nfs_client_initdata cl_init = {
588 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500589 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500590 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500591 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust3a498022007-12-14 14:56:04 -0500592 };
Trond Myklebust33170232007-12-20 16:03:59 -0500593 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400594 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500595 int error;
David Howells54ceac42006-08-22 20:06:13 -0400596
597 dprintk("--> nfs_init_server()\n");
598
599#ifdef CONFIG_NFS_V3
600 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500601 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400602#endif
603
604 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500605 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400606 if (IS_ERR(clp)) {
607 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
608 return PTR_ERR(clp);
609 }
610
Trond Myklebust33170232007-12-20 16:03:59 -0500611 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
612 data->timeo, data->retrans);
613 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400614 if (error < 0)
615 goto error;
616
617 server->nfs_client = clp;
618
619 /* Initialise the client representation from the mount data */
620 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
621
622 if (data->rsize)
623 server->rsize = nfs_block_size(data->rsize, NULL);
624 if (data->wsize)
625 server->wsize = nfs_block_size(data->wsize, NULL);
626
627 server->acregmin = data->acregmin * HZ;
628 server->acregmax = data->acregmax * HZ;
629 server->acdirmin = data->acdirmin * HZ;
630 server->acdirmax = data->acdirmax * HZ;
631
632 /* Start lockd here, before we might error out */
633 error = nfs_start_lockd(server);
634 if (error < 0)
635 goto error;
636
Trond Myklebust33170232007-12-20 16:03:59 -0500637 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400638 if (error < 0)
639 goto error;
640
641 server->namelen = data->namlen;
642 /* Create a client RPC handle for the NFSv3 ACL management interface */
643 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400644 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
645 return 0;
646
647error:
648 server->nfs_client = NULL;
649 nfs_put_client(clp);
650 dprintk("<-- nfs_init_server() = xerror %d\n", error);
651 return error;
652}
653
654/*
655 * Load up the server record from information gained in an fsinfo record
656 */
657static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
658{
659 unsigned long max_rpc_payload;
660
661 /* Work out a lot of parameters */
662 if (server->rsize == 0)
663 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
664 if (server->wsize == 0)
665 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
666
667 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
668 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
669 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
670 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
671
672 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
673 if (server->rsize > max_rpc_payload)
674 server->rsize = max_rpc_payload;
675 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
676 server->rsize = NFS_MAX_FILE_IO_SIZE;
677 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700678
David Howells54ceac42006-08-22 20:06:13 -0400679 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
680
681 if (server->wsize > max_rpc_payload)
682 server->wsize = max_rpc_payload;
683 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
684 server->wsize = NFS_MAX_FILE_IO_SIZE;
685 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
686 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
687
688 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
689 if (server->dtsize > PAGE_CACHE_SIZE)
690 server->dtsize = PAGE_CACHE_SIZE;
691 if (server->dtsize > server->rsize)
692 server->dtsize = server->rsize;
693
694 if (server->flags & NFS_MOUNT_NOAC) {
695 server->acregmin = server->acregmax = 0;
696 server->acdirmin = server->acdirmax = 0;
697 }
698
699 server->maxfilesize = fsinfo->maxfilesize;
700
701 /* We're airborne Set socket buffersize */
702 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
703}
704
705/*
706 * Probe filesystem information, including the FSID on v2/v3
707 */
708static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
709{
710 struct nfs_fsinfo fsinfo;
711 struct nfs_client *clp = server->nfs_client;
712 int error;
713
714 dprintk("--> nfs_probe_fsinfo()\n");
715
716 if (clp->rpc_ops->set_capabilities != NULL) {
717 error = clp->rpc_ops->set_capabilities(server, mntfh);
718 if (error < 0)
719 goto out_error;
720 }
721
722 fsinfo.fattr = fattr;
723 nfs_fattr_init(fattr);
724 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
725 if (error < 0)
726 goto out_error;
727
728 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700729 error = bdi_init(&server->backing_dev_info);
730 if (error)
731 goto out_error;
732
David Howells54ceac42006-08-22 20:06:13 -0400733
734 /* Get some general file system info */
735 if (server->namelen == 0) {
736 struct nfs_pathconf pathinfo;
737
738 pathinfo.fattr = fattr;
739 nfs_fattr_init(fattr);
740
741 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
742 server->namelen = pathinfo.max_namelen;
743 }
744
745 dprintk("<-- nfs_probe_fsinfo() = 0\n");
746 return 0;
747
748out_error:
749 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
750 return error;
751}
752
753/*
754 * Copy useful information when duplicating a server record
755 */
756static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
757{
758 target->flags = source->flags;
759 target->acregmin = source->acregmin;
760 target->acregmax = source->acregmax;
761 target->acdirmin = source->acdirmin;
762 target->acdirmax = source->acdirmax;
763 target->caps = source->caps;
764}
765
766/*
767 * Allocate and initialise a server record
768 */
769static struct nfs_server *nfs_alloc_server(void)
770{
771 struct nfs_server *server;
772
773 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
774 if (!server)
775 return NULL;
776
777 server->client = server->client_acl = ERR_PTR(-EINVAL);
778
779 /* Zero out the NFS state stuff */
780 INIT_LIST_HEAD(&server->client_link);
781 INIT_LIST_HEAD(&server->master_link);
782
Steve Dicksonef818a22007-11-08 04:05:04 -0500783 init_waitqueue_head(&server->active_wq);
784 atomic_set(&server->active, 0);
785
David Howells54ceac42006-08-22 20:06:13 -0400786 server->io_stats = nfs_alloc_iostats();
787 if (!server->io_stats) {
788 kfree(server);
789 return NULL;
790 }
791
792 return server;
793}
794
795/*
796 * Free up a server record
797 */
798void nfs_free_server(struct nfs_server *server)
799{
800 dprintk("--> nfs_free_server()\n");
801
802 spin_lock(&nfs_client_lock);
803 list_del(&server->client_link);
804 list_del(&server->master_link);
805 spin_unlock(&nfs_client_lock);
806
807 if (server->destroy != NULL)
808 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500809
810 if (!IS_ERR(server->client_acl))
811 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400812 if (!IS_ERR(server->client))
813 rpc_shutdown_client(server->client);
814
815 nfs_put_client(server->nfs_client);
816
817 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700818 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400819 kfree(server);
820 nfs_release_automount_timer();
821 dprintk("<-- nfs_free_server()\n");
822}
823
824/*
825 * Create a version 2 or 3 volume record
826 * - keyed on server and FSID
827 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400828struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400829 struct nfs_fh *mntfh)
830{
831 struct nfs_server *server;
832 struct nfs_fattr fattr;
833 int error;
834
835 server = nfs_alloc_server();
836 if (!server)
837 return ERR_PTR(-ENOMEM);
838
839 /* Get a client representation */
840 error = nfs_init_server(server, data);
841 if (error < 0)
842 goto error;
843
844 BUG_ON(!server->nfs_client);
845 BUG_ON(!server->nfs_client->rpc_ops);
846 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
847
848 /* Probe the root fh to retrieve its FSID */
849 error = nfs_probe_fsinfo(server, mntfh, &fattr);
850 if (error < 0)
851 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400852 if (server->nfs_client->rpc_ops->version == 3) {
853 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
854 server->namelen = NFS3_MAXNAMLEN;
855 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
856 server->caps |= NFS_CAP_READDIRPLUS;
857 } else {
858 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
859 server->namelen = NFS2_MAXNAMLEN;
860 }
861
David Howells54ceac42006-08-22 20:06:13 -0400862 if (!(fattr.valid & NFS_ATTR_FATTR)) {
863 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
864 if (error < 0) {
865 dprintk("nfs_create_server: getattr error = %d\n", -error);
866 goto error;
867 }
868 }
869 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
870
David Howells6daabf12006-08-24 15:44:16 -0400871 dprintk("Server FSID: %llx:%llx\n",
872 (unsigned long long) server->fsid.major,
873 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400874
875 BUG_ON(!server->nfs_client);
876 BUG_ON(!server->nfs_client->rpc_ops);
877 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
878
879 spin_lock(&nfs_client_lock);
880 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
881 list_add_tail(&server->master_link, &nfs_volume_list);
882 spin_unlock(&nfs_client_lock);
883
884 server->mount_time = jiffies;
885 return server;
886
887error:
888 nfs_free_server(server);
889 return ERR_PTR(error);
890}
891
892#ifdef CONFIG_NFS_V4
893/*
894 * Initialise an NFS4 client record
895 */
896static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500897 int proto,
898 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700899 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400900 rpc_authflavor_t authflavour)
901{
902 int error;
903
904 if (clp->cl_cons_state == NFS_CS_READY) {
905 /* the client is initialised already */
906 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
907 return 0;
908 }
909
910 /* Check NFS protocol revision and initialize RPC op vector */
911 clp->rpc_ops = &nfs_v4_clientops;
912
Trond Myklebust33170232007-12-20 16:03:59 -0500913 error = nfs_create_rpc_client(clp, proto, timeparms, authflavour,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500914 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400915 if (error < 0)
916 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700917 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400918
919 error = nfs_idmap_new(clp);
920 if (error < 0) {
921 dprintk("%s: failed to create idmapper. Error = %d\n",
922 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400923 goto error;
924 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400925 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400926
927 nfs_mark_client_ready(clp, NFS_CS_READY);
928 return 0;
929
930error:
931 nfs_mark_client_ready(clp, error);
932 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
933 return error;
934}
935
936/*
937 * Set up an NFS4 client
938 */
939static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500940 const char *hostname,
941 const struct sockaddr *addr,
942 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700943 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400944 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -0500945 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -0400946{
Trond Myklebust3a498022007-12-14 14:56:04 -0500947 struct nfs_client_initdata cl_init = {
948 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500949 .addr = addr,
950 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500951 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust3a498022007-12-14 14:56:04 -0500952 };
David Howells54ceac42006-08-22 20:06:13 -0400953 struct nfs_client *clp;
954 int error;
955
956 dprintk("--> nfs4_set_client()\n");
957
958 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500959 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400960 if (IS_ERR(clp)) {
961 error = PTR_ERR(clp);
962 goto error;
963 }
Trond Myklebust33170232007-12-20 16:03:59 -0500964 error = nfs4_init_client(clp, proto, timeparms, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400965 if (error < 0)
966 goto error_put;
967
968 server->nfs_client = clp;
969 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
970 return 0;
971
972error_put:
973 nfs_put_client(clp);
974error:
975 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
976 return error;
977}
978
979/*
980 * Create a version 4 volume record
981 */
982static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400983 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400984{
Trond Myklebust33170232007-12-20 16:03:59 -0500985 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400986 int error;
987
988 dprintk("--> nfs4_init_server()\n");
989
Trond Myklebust33170232007-12-20 16:03:59 -0500990 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
991 data->timeo, data->retrans);
992
993 /* Get a client record */
994 error = nfs4_set_client(server,
995 data->nfs_server.hostname,
996 (const struct sockaddr *)&data->nfs_server.address,
997 data->nfs_server.addrlen,
998 data->client_address,
999 data->auth_flavors[0],
1000 data->nfs_server.protocol,
1001 &timeparms);
1002 if (error < 0)
1003 goto error;
1004
David Howells54ceac42006-08-22 20:06:13 -04001005 /* Initialise the client representation from the mount data */
1006 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1007 server->caps |= NFS_CAP_ATOMIC_OPEN;
1008
1009 if (data->rsize)
1010 server->rsize = nfs_block_size(data->rsize, NULL);
1011 if (data->wsize)
1012 server->wsize = nfs_block_size(data->wsize, NULL);
1013
1014 server->acregmin = data->acregmin * HZ;
1015 server->acregmax = data->acregmax * HZ;
1016 server->acdirmin = data->acdirmin * HZ;
1017 server->acdirmax = data->acdirmax * HZ;
1018
Trond Myklebust33170232007-12-20 16:03:59 -05001019 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001020
Trond Myklebust33170232007-12-20 16:03:59 -05001021error:
David Howells54ceac42006-08-22 20:06:13 -04001022 /* Done */
1023 dprintk("<-- nfs4_init_server() = %d\n", error);
1024 return error;
1025}
1026
1027/*
1028 * Create a version 4 volume record
1029 * - keyed on server and FSID
1030 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001031struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001032 struct nfs_fh *mntfh)
1033{
1034 struct nfs_fattr fattr;
1035 struct nfs_server *server;
1036 int error;
1037
1038 dprintk("--> nfs4_create_server()\n");
1039
1040 server = nfs_alloc_server();
1041 if (!server)
1042 return ERR_PTR(-ENOMEM);
1043
David Howells54ceac42006-08-22 20:06:13 -04001044 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001045 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001046 if (error < 0)
1047 goto error;
1048
1049 BUG_ON(!server->nfs_client);
1050 BUG_ON(!server->nfs_client->rpc_ops);
1051 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1052
1053 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001054 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001055 if (error < 0)
1056 goto error;
1057
David Howells6daabf12006-08-24 15:44:16 -04001058 dprintk("Server FSID: %llx:%llx\n",
1059 (unsigned long long) server->fsid.major,
1060 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001061 dprintk("Mount FH: %d\n", mntfh->size);
1062
1063 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1064 if (error < 0)
1065 goto error;
1066
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001067 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1068 server->namelen = NFS4_MAXNAMLEN;
1069
David Howells54ceac42006-08-22 20:06:13 -04001070 BUG_ON(!server->nfs_client);
1071 BUG_ON(!server->nfs_client->rpc_ops);
1072 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1073
1074 spin_lock(&nfs_client_lock);
1075 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1076 list_add_tail(&server->master_link, &nfs_volume_list);
1077 spin_unlock(&nfs_client_lock);
1078
1079 server->mount_time = jiffies;
1080 dprintk("<-- nfs4_create_server() = %p\n", server);
1081 return server;
1082
1083error:
1084 nfs_free_server(server);
1085 dprintk("<-- nfs4_create_server() = error %d\n", error);
1086 return ERR_PTR(error);
1087}
1088
1089/*
1090 * Create an NFS4 referral server record
1091 */
1092struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001093 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001094{
1095 struct nfs_client *parent_client;
1096 struct nfs_server *server, *parent_server;
1097 struct nfs_fattr fattr;
1098 int error;
1099
1100 dprintk("--> nfs4_create_referral_server()\n");
1101
1102 server = nfs_alloc_server();
1103 if (!server)
1104 return ERR_PTR(-ENOMEM);
1105
1106 parent_server = NFS_SB(data->sb);
1107 parent_client = parent_server->nfs_client;
1108
1109 /* Get a client representation.
1110 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001111 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001112 data->addr,
1113 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001114 parent_client->cl_ipaddr,
1115 data->authflavor,
1116 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001117 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001118 if (error < 0)
1119 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001120
1121 /* Initialise the client representation from the parent server */
1122 nfs_server_copy_userdata(server, parent_server);
1123 server->caps |= NFS_CAP_ATOMIC_OPEN;
1124
Trond Myklebust33170232007-12-20 16:03:59 -05001125 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001126 if (error < 0)
1127 goto error;
1128
1129 BUG_ON(!server->nfs_client);
1130 BUG_ON(!server->nfs_client->rpc_ops);
1131 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1132
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001133 /* Probe the root fh to retrieve its FSID and filehandle */
1134 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1135 if (error < 0)
1136 goto error;
1137
David Howells54ceac42006-08-22 20:06:13 -04001138 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001139 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001140 if (error < 0)
1141 goto error;
1142
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001143 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1144 server->namelen = NFS4_MAXNAMLEN;
1145
David Howells54ceac42006-08-22 20:06:13 -04001146 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001147 (unsigned long long) server->fsid.major,
1148 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001149
1150 spin_lock(&nfs_client_lock);
1151 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1152 list_add_tail(&server->master_link, &nfs_volume_list);
1153 spin_unlock(&nfs_client_lock);
1154
1155 server->mount_time = jiffies;
1156
1157 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1158 return server;
1159
1160error:
1161 nfs_free_server(server);
1162 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1163 return ERR_PTR(error);
1164}
1165
1166#endif /* CONFIG_NFS_V4 */
1167
1168/*
1169 * Clone an NFS2, NFS3 or NFS4 server record
1170 */
1171struct nfs_server *nfs_clone_server(struct nfs_server *source,
1172 struct nfs_fh *fh,
1173 struct nfs_fattr *fattr)
1174{
1175 struct nfs_server *server;
1176 struct nfs_fattr fattr_fsinfo;
1177 int error;
1178
1179 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001180 (unsigned long long) fattr->fsid.major,
1181 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001182
1183 server = nfs_alloc_server();
1184 if (!server)
1185 return ERR_PTR(-ENOMEM);
1186
1187 /* Copy data from the source */
1188 server->nfs_client = source->nfs_client;
1189 atomic_inc(&server->nfs_client->cl_count);
1190 nfs_server_copy_userdata(server, source);
1191
1192 server->fsid = fattr->fsid;
1193
Trond Myklebust33170232007-12-20 16:03:59 -05001194 error = nfs_init_server_rpcclient(server,
1195 source->client->cl_timeout,
1196 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001197 if (error < 0)
1198 goto out_free_server;
1199 if (!IS_ERR(source->client_acl))
1200 nfs_init_server_aclclient(server);
1201
1202 /* probe the filesystem info for this server filesystem */
1203 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1204 if (error < 0)
1205 goto out_free_server;
1206
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001207 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1208 server->namelen = NFS4_MAXNAMLEN;
1209
David Howells54ceac42006-08-22 20:06:13 -04001210 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001211 (unsigned long long) server->fsid.major,
1212 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001213
1214 error = nfs_start_lockd(server);
1215 if (error < 0)
1216 goto out_free_server;
1217
1218 spin_lock(&nfs_client_lock);
1219 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1220 list_add_tail(&server->master_link, &nfs_volume_list);
1221 spin_unlock(&nfs_client_lock);
1222
1223 server->mount_time = jiffies;
1224
1225 dprintk("<-- nfs_clone_server() = %p\n", server);
1226 return server;
1227
1228out_free_server:
1229 nfs_free_server(server);
1230 dprintk("<-- nfs_clone_server() = error %d\n", error);
1231 return ERR_PTR(error);
1232}
David Howells6aaca562006-08-22 20:06:13 -04001233
1234#ifdef CONFIG_PROC_FS
1235static struct proc_dir_entry *proc_fs_nfs;
1236
1237static int nfs_server_list_open(struct inode *inode, struct file *file);
1238static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1239static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1240static void nfs_server_list_stop(struct seq_file *p, void *v);
1241static int nfs_server_list_show(struct seq_file *m, void *v);
1242
1243static struct seq_operations nfs_server_list_ops = {
1244 .start = nfs_server_list_start,
1245 .next = nfs_server_list_next,
1246 .stop = nfs_server_list_stop,
1247 .show = nfs_server_list_show,
1248};
1249
Arjan van de Ven00977a52007-02-12 00:55:34 -08001250static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001251 .open = nfs_server_list_open,
1252 .read = seq_read,
1253 .llseek = seq_lseek,
1254 .release = seq_release,
1255};
1256
1257static int nfs_volume_list_open(struct inode *inode, struct file *file);
1258static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1259static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1260static void nfs_volume_list_stop(struct seq_file *p, void *v);
1261static int nfs_volume_list_show(struct seq_file *m, void *v);
1262
1263static struct seq_operations nfs_volume_list_ops = {
1264 .start = nfs_volume_list_start,
1265 .next = nfs_volume_list_next,
1266 .stop = nfs_volume_list_stop,
1267 .show = nfs_volume_list_show,
1268};
1269
Arjan van de Ven00977a52007-02-12 00:55:34 -08001270static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001271 .open = nfs_volume_list_open,
1272 .read = seq_read,
1273 .llseek = seq_lseek,
1274 .release = seq_release,
1275};
1276
1277/*
1278 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1279 * we're dealing
1280 */
1281static int nfs_server_list_open(struct inode *inode, struct file *file)
1282{
1283 struct seq_file *m;
1284 int ret;
1285
1286 ret = seq_open(file, &nfs_server_list_ops);
1287 if (ret < 0)
1288 return ret;
1289
1290 m = file->private_data;
1291 m->private = PDE(inode)->data;
1292
1293 return 0;
1294}
1295
1296/*
1297 * set up the iterator to start reading from the server list and return the first item
1298 */
1299static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1300{
David Howells6aaca562006-08-22 20:06:13 -04001301 /* lock the list against modification */
1302 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001303 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001304}
1305
1306/*
1307 * move to next server
1308 */
1309static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1310{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001311 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001312}
1313
1314/*
1315 * clean up after reading from the transports list
1316 */
1317static void nfs_server_list_stop(struct seq_file *p, void *v)
1318{
1319 spin_unlock(&nfs_client_lock);
1320}
1321
1322/*
1323 * display a header line followed by a load of call lines
1324 */
1325static int nfs_server_list_show(struct seq_file *m, void *v)
1326{
1327 struct nfs_client *clp;
1328
1329 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001330 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001331 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1332 return 0;
1333 }
1334
1335 /* display one transport per line on subsequent lines */
1336 clp = list_entry(v, struct nfs_client, cl_share_link);
1337
Chuck Lever5d8515c2007-12-10 14:57:16 -05001338 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001339 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001340 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1341 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001342 atomic_read(&clp->cl_count),
1343 clp->cl_hostname);
1344
1345 return 0;
1346}
1347
1348/*
1349 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1350 */
1351static int nfs_volume_list_open(struct inode *inode, struct file *file)
1352{
1353 struct seq_file *m;
1354 int ret;
1355
1356 ret = seq_open(file, &nfs_volume_list_ops);
1357 if (ret < 0)
1358 return ret;
1359
1360 m = file->private_data;
1361 m->private = PDE(inode)->data;
1362
1363 return 0;
1364}
1365
1366/*
1367 * set up the iterator to start reading from the volume list and return the first item
1368 */
1369static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1370{
David Howells6aaca562006-08-22 20:06:13 -04001371 /* lock the list against modification */
1372 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001373 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001374}
1375
1376/*
1377 * move to next volume
1378 */
1379static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1380{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001381 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001382}
1383
1384/*
1385 * clean up after reading from the transports list
1386 */
1387static void nfs_volume_list_stop(struct seq_file *p, void *v)
1388{
1389 spin_unlock(&nfs_client_lock);
1390}
1391
1392/*
1393 * display a header line followed by a load of call lines
1394 */
1395static int nfs_volume_list_show(struct seq_file *m, void *v)
1396{
1397 struct nfs_server *server;
1398 struct nfs_client *clp;
1399 char dev[8], fsid[17];
1400
1401 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001402 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001403 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1404 return 0;
1405 }
1406 /* display one transport per line on subsequent lines */
1407 server = list_entry(v, struct nfs_server, master_link);
1408 clp = server->nfs_client;
1409
1410 snprintf(dev, 8, "%u:%u",
1411 MAJOR(server->s_dev), MINOR(server->s_dev));
1412
1413 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001414 (unsigned long long) server->fsid.major,
1415 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001416
Chuck Lever5d8515c2007-12-10 14:57:16 -05001417 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001418 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001419 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1420 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001421 dev,
1422 fsid);
1423
1424 return 0;
1425}
1426
1427/*
1428 * initialise the /proc/fs/nfsfs/ directory
1429 */
1430int __init nfs_fs_proc_init(void)
1431{
1432 struct proc_dir_entry *p;
1433
1434 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1435 if (!proc_fs_nfs)
1436 goto error_0;
1437
1438 proc_fs_nfs->owner = THIS_MODULE;
1439
1440 /* a file of servers with which we're dealing */
1441 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1442 if (!p)
1443 goto error_1;
1444
1445 p->proc_fops = &nfs_server_list_fops;
1446 p->owner = THIS_MODULE;
1447
1448 /* a file of volumes that we have mounted */
1449 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1450 if (!p)
1451 goto error_2;
1452
1453 p->proc_fops = &nfs_volume_list_fops;
1454 p->owner = THIS_MODULE;
1455 return 0;
1456
1457error_2:
1458 remove_proc_entry("servers", proc_fs_nfs);
1459error_1:
1460 remove_proc_entry("nfsfs", proc_root_fs);
1461error_0:
1462 return -ENOMEM;
1463}
1464
1465/*
1466 * clean up the /proc/fs/nfsfs/ directory
1467 */
1468void nfs_fs_proc_exit(void)
1469{
1470 remove_proc_entry("volumes", proc_fs_nfs);
1471 remove_proc_entry("servers", proc_fs_nfs);
1472 remove_proc_entry("nfsfs", proc_root_fs);
1473}
1474
1475#endif /* CONFIG_PROC_FS */