blob: 7a15832369e901b7c15308571d2eee424fad2fac [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 Myklebust59dca3b2008-01-03 16:29:06 -0500103 int proto;
Trond Myklebust3a498022007-12-14 14:56:04 -0500104};
105
David Howells5006a762006-08-22 20:06:12 -0400106/*
David Howells24c8dbb2006-08-22 20:06:10 -0400107 * Allocate a shared client record
108 *
109 * Since these are allocated/deallocated very rarely, we don't
110 * bother putting them in a slab cache...
111 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500112static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400113{
114 struct nfs_client *clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400115
116 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
117 goto error_0;
118
Trond Myklebust40c553192007-12-14 14:56:07 -0500119 clp->rpc_ops = cl_init->rpc_ops;
120
121 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400122 if (nfs_callback_up() < 0)
123 goto error_2;
124 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
125 }
126
127 atomic_set(&clp->cl_count, 1);
128 clp->cl_cons_state = NFS_CS_INITING;
129
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500130 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
131 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400132
Trond Myklebust3a498022007-12-14 14:56:04 -0500133 if (cl_init->hostname) {
134 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400135 if (!clp->cl_hostname)
136 goto error_3;
137 }
138
139 INIT_LIST_HEAD(&clp->cl_superblocks);
140 clp->cl_rpcclient = ERR_PTR(-EINVAL);
141
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500142 clp->cl_proto = cl_init->proto;
143
David Howells24c8dbb2006-08-22 20:06:10 -0400144#ifdef CONFIG_NFS_V4
145 init_rwsem(&clp->cl_sem);
146 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400147 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
152#endif
153
154 return clp;
155
156error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400157 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
158 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400159error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400160 kfree(clp);
161error_0:
162 return NULL;
163}
164
Trond Myklebust5dd31772006-08-24 01:03:05 -0400165static void nfs4_shutdown_client(struct nfs_client *clp)
166{
167#ifdef CONFIG_NFS_V4
168 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
169 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400170 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400171 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
172 nfs_idmap_delete(clp);
173#endif
174}
175
David Howells24c8dbb2006-08-22 20:06:10 -0400176/*
177 * Destroy a shared client record
178 */
179static void nfs_free_client(struct nfs_client *clp)
180{
Trond Myklebust40c553192007-12-14 14:56:07 -0500181 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400182
Trond Myklebust5dd31772006-08-24 01:03:05 -0400183 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400184
185 /* -EIO all pending I/O */
186 if (!IS_ERR(clp->cl_rpcclient))
187 rpc_shutdown_client(clp->cl_rpcclient);
188
189 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
190 nfs_callback_down();
191
David Howells24c8dbb2006-08-22 20:06:10 -0400192 kfree(clp->cl_hostname);
193 kfree(clp);
194
195 dprintk("<-- nfs_free_client()\n");
196}
197
198/*
199 * Release a reference to a shared client record
200 */
201void nfs_put_client(struct nfs_client *clp)
202{
David Howells27ba8512006-07-30 14:40:56 -0400203 if (!clp)
204 return;
205
David Howells24c8dbb2006-08-22 20:06:10 -0400206 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
207
208 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
209 list_del(&clp->cl_share_link);
210 spin_unlock(&nfs_client_lock);
211
212 BUG_ON(!list_empty(&clp->cl_superblocks));
213
214 nfs_free_client(clp);
215 }
216}
217
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500218static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
219 const struct sockaddr_in *sa2)
220{
221 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
222}
223
224static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
225 const struct sockaddr_in6 *sa2)
226{
227 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
228}
229
230static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
231 const struct sockaddr *sa2)
232{
233 switch (sa1->sa_family) {
234 case AF_INET:
235 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
236 (const struct sockaddr_in *)sa2);
237 case AF_INET6:
238 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
239 (const struct sockaddr_in6 *)sa2);
240 }
241 BUG();
242}
243
David Howells24c8dbb2006-08-22 20:06:10 -0400244/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500245 * Find a client by IP address and protocol version
246 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400247 */
Chuck Leverff052642007-12-10 14:58:44 -0500248struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500249{
250 struct nfs_client *clp;
251
252 spin_lock(&nfs_client_lock);
253 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500254 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
255
Trond Myklebustc81468a2007-12-14 14:56:05 -0500256 /* Don't match clients that failed to initialise properly */
257 if (clp->cl_cons_state != NFS_CS_READY)
258 continue;
259
260 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500261 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500262 continue;
263
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500264 if (addr->sa_family != clap->sa_family)
265 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500266 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500267 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500268 continue;
269
270 atomic_inc(&clp->cl_count);
271 spin_unlock(&nfs_client_lock);
272 return clp;
273 }
274 spin_unlock(&nfs_client_lock);
275 return NULL;
276}
277
278/*
279 * Find an nfs_client on the list that matches the initialisation data
280 * that is supplied.
281 */
282static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400283{
284 struct nfs_client *clp;
285
286 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700287 /* Don't match clients that failed to initialise properly */
288 if (clp->cl_cons_state < 0)
289 continue;
290
David Howells24c8dbb2006-08-22 20:06:10 -0400291 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500292 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400293 continue;
294
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500295 if (clp->cl_proto != data->proto)
296 continue;
297
Trond Myklebustc81468a2007-12-14 14:56:05 -0500298 /* Match the full socket address */
299 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400300 continue;
301
Trond Myklebustc81468a2007-12-14 14:56:05 -0500302 atomic_inc(&clp->cl_count);
303 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400304 }
David Howells24c8dbb2006-08-22 20:06:10 -0400305 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400306}
307
308/*
309 * Look up a client by IP address and protocol version
310 * - creates a new record if one doesn't yet exist
311 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500312static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400313{
314 struct nfs_client *clp, *new = NULL;
315 int error;
316
Chuck Leverd7422c42007-12-10 14:58:51 -0500317 dprintk("--> nfs_get_client(%s,v%u)\n",
318 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400319
320 /* see if the client already exists */
321 do {
322 spin_lock(&nfs_client_lock);
323
Trond Myklebustc81468a2007-12-14 14:56:05 -0500324 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400325 if (clp)
326 goto found_client;
327 if (new)
328 goto install_client;
329
330 spin_unlock(&nfs_client_lock);
331
Trond Myklebust3a498022007-12-14 14:56:04 -0500332 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400333 } while (new);
334
335 return ERR_PTR(-ENOMEM);
336
337 /* install a new client and return with it unready */
338install_client:
339 clp = new;
340 list_add(&clp->cl_share_link, &nfs_client_list);
341 spin_unlock(&nfs_client_lock);
342 dprintk("--> nfs_get_client() = %p [new]\n", clp);
343 return clp;
344
345 /* found an existing client
346 * - make sure it's ready before returning
347 */
348found_client:
349 spin_unlock(&nfs_client_lock);
350
351 if (new)
352 nfs_free_client(new);
353
Linus Torvalds83250492006-10-08 17:28:25 -0700354 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400355 clp->cl_cons_state != NFS_CS_INITING);
356 if (error < 0) {
357 nfs_put_client(clp);
358 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400359 }
360
361 if (clp->cl_cons_state < NFS_CS_READY) {
362 error = clp->cl_cons_state;
363 nfs_put_client(clp);
364 return ERR_PTR(error);
365 }
366
David Howells54ceac42006-08-22 20:06:13 -0400367 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
368
David Howells24c8dbb2006-08-22 20:06:10 -0400369 dprintk("--> nfs_get_client() = %p [share]\n", clp);
370 return clp;
371}
372
373/*
374 * Mark a server as ready or failed
375 */
David Howells54ceac42006-08-22 20:06:13 -0400376static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400377{
378 clp->cl_cons_state = state;
379 wake_up_all(&nfs_client_active_wq);
380}
David Howells5006a762006-08-22 20:06:12 -0400381
382/*
383 * Initialise the timeout values for a connection
384 */
385static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
386 unsigned int timeo, unsigned int retrans)
387{
388 to->to_initval = timeo * HZ / 10;
389 to->to_retries = retrans;
390 if (!to->to_retries)
391 to->to_retries = 2;
392
393 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400394 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400395 case XPRT_TRANSPORT_RDMA:
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500396 if (to->to_initval == 0)
David Howells5006a762006-08-22 20:06:12 -0400397 to->to_initval = 60 * HZ;
398 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
399 to->to_initval = NFS_MAX_TCP_TIMEOUT;
400 to->to_increment = to->to_initval;
401 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500402 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
403 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
404 if (to->to_maxval < to->to_initval)
405 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400406 to->to_exponential = 0;
407 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400408 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400409 default:
410 if (!to->to_initval)
411 to->to_initval = 11 * HZ / 10;
412 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
413 to->to_initval = NFS_MAX_UDP_TIMEOUT;
414 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
415 to->to_exponential = 1;
416 break;
417 }
418}
419
420/*
421 * Create an RPC client handle
422 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500423static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500424 const struct rpc_timeout *timeparms,
425 rpc_authflavor_t flavor,
426 int flags)
David Howells5006a762006-08-22 20:06:12 -0400427{
David Howells5006a762006-08-22 20:06:12 -0400428 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400429 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500430 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400431 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500432 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500433 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400434 .servername = clp->cl_hostname,
435 .program = &nfs_program,
436 .version = clp->rpc_ops->version,
437 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500438 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400439 };
David Howells5006a762006-08-22 20:06:12 -0400440
441 if (!IS_ERR(clp->cl_rpcclient))
442 return 0;
443
Chuck Lever41877d22006-08-22 20:06:20 -0400444 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400445 if (IS_ERR(clnt)) {
446 dprintk("%s: cannot create RPC client. Error = %ld\n",
447 __FUNCTION__, PTR_ERR(clnt));
448 return PTR_ERR(clnt);
449 }
450
David Howells5006a762006-08-22 20:06:12 -0400451 clp->cl_rpcclient = clnt;
452 return 0;
453}
David Howells54ceac42006-08-22 20:06:13 -0400454
455/*
456 * Version 2 or 3 client destruction
457 */
458static void nfs_destroy_server(struct nfs_server *server)
459{
David Howells54ceac42006-08-22 20:06:13 -0400460 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500461 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400462}
463
464/*
465 * Version 2 or 3 lockd setup
466 */
467static int nfs_start_lockd(struct nfs_server *server)
468{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500469 struct nlm_host *host;
470 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500471 struct nlmclnt_initdata nlm_init = {
472 .hostname = clp->cl_hostname,
473 .address = (struct sockaddr *)&clp->cl_addr,
474 .addrlen = clp->cl_addrlen,
475 .protocol = server->flags & NFS_MOUNT_TCP ?
476 IPPROTO_TCP : IPPROTO_UDP,
477 .nfs_version = clp->rpc_ops->version,
478 };
David Howells54ceac42006-08-22 20:06:13 -0400479
Chuck Lever883bb162008-01-15 16:04:20 -0500480 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500481 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400482 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500483 return 0;
484
Chuck Lever883bb162008-01-15 16:04:20 -0500485 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500486 if (IS_ERR(host))
487 return PTR_ERR(host);
488
489 server->nlm_host = host;
490 server->destroy = nfs_destroy_server;
491 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400492}
493
494/*
495 * Initialise an NFSv3 ACL client connection
496 */
497#ifdef CONFIG_NFS_V3_ACL
498static void nfs_init_server_aclclient(struct nfs_server *server)
499{
Trond Myklebust40c553192007-12-14 14:56:07 -0500500 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400501 goto out_noacl;
502 if (server->flags & NFS_MOUNT_NOACL)
503 goto out_noacl;
504
505 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
506 if (IS_ERR(server->client_acl))
507 goto out_noacl;
508
509 /* No errors! Assume that Sun nfsacls are supported */
510 server->caps |= NFS_CAP_ACLS;
511 return;
512
513out_noacl:
514 server->caps &= ~NFS_CAP_ACLS;
515}
516#else
517static inline void nfs_init_server_aclclient(struct nfs_server *server)
518{
519 server->flags &= ~NFS_MOUNT_NOACL;
520 server->caps &= ~NFS_CAP_ACLS;
521}
522#endif
523
524/*
525 * Create a general RPC client
526 */
Trond Myklebust33170232007-12-20 16:03:59 -0500527static int nfs_init_server_rpcclient(struct nfs_server *server,
528 const struct rpc_timeout *timeo,
529 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400530{
531 struct nfs_client *clp = server->nfs_client;
532
533 server->client = rpc_clone_client(clp->cl_rpcclient);
534 if (IS_ERR(server->client)) {
535 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
536 return PTR_ERR(server->client);
537 }
538
Trond Myklebust33170232007-12-20 16:03:59 -0500539 memcpy(&server->client->cl_timeout_default,
540 timeo,
541 sizeof(server->client->cl_timeout_default));
542 server->client->cl_timeout = &server->client->cl_timeout_default;
543
David Howells54ceac42006-08-22 20:06:13 -0400544 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
545 struct rpc_auth *auth;
546
547 auth = rpcauth_create(pseudoflavour, server->client);
548 if (IS_ERR(auth)) {
549 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
550 return PTR_ERR(auth);
551 }
552 }
553 server->client->cl_softrtry = 0;
554 if (server->flags & NFS_MOUNT_SOFT)
555 server->client->cl_softrtry = 1;
556
557 server->client->cl_intr = 0;
558 if (server->flags & NFS4_MOUNT_INTR)
559 server->client->cl_intr = 1;
560
561 return 0;
562}
563
564/*
565 * Initialise an NFS2 or NFS3 client
566 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400567static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500568 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400569 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400570{
David Howells54ceac42006-08-22 20:06:13 -0400571 int error;
572
573 if (clp->cl_cons_state == NFS_CS_READY) {
574 /* the client is already initialised */
575 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
576 return 0;
577 }
578
David Howells54ceac42006-08-22 20:06:13 -0400579 /*
580 * Create a client RPC handle for doing FSSTAT with UNIX auth only
581 * - RFC 2623, sec 2.3.2
582 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500583 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400584 if (error < 0)
585 goto error;
586 nfs_mark_client_ready(clp, NFS_CS_READY);
587 return 0;
588
589error:
590 nfs_mark_client_ready(clp, error);
591 dprintk("<-- nfs_init_client() = xerror %d\n", error);
592 return error;
593}
594
595/*
596 * Create a version 2 or 3 client
597 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400598static int nfs_init_server(struct nfs_server *server,
599 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400600{
Trond Myklebust3a498022007-12-14 14:56:04 -0500601 struct nfs_client_initdata cl_init = {
602 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500603 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500604 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500605 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500606 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500607 };
Trond Myklebust33170232007-12-20 16:03:59 -0500608 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400609 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500610 int error;
David Howells54ceac42006-08-22 20:06:13 -0400611
612 dprintk("--> nfs_init_server()\n");
613
614#ifdef CONFIG_NFS_V3
615 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500616 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400617#endif
618
619 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500620 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400621 if (IS_ERR(clp)) {
622 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
623 return PTR_ERR(clp);
624 }
625
Trond Myklebust33170232007-12-20 16:03:59 -0500626 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
627 data->timeo, data->retrans);
628 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400629 if (error < 0)
630 goto error;
631
632 server->nfs_client = clp;
633
634 /* Initialise the client representation from the mount data */
635 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
636
637 if (data->rsize)
638 server->rsize = nfs_block_size(data->rsize, NULL);
639 if (data->wsize)
640 server->wsize = nfs_block_size(data->wsize, NULL);
641
642 server->acregmin = data->acregmin * HZ;
643 server->acregmax = data->acregmax * HZ;
644 server->acdirmin = data->acdirmin * HZ;
645 server->acdirmax = data->acdirmax * HZ;
646
647 /* Start lockd here, before we might error out */
648 error = nfs_start_lockd(server);
649 if (error < 0)
650 goto error;
651
Trond Myklebust33170232007-12-20 16:03:59 -0500652 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400653 if (error < 0)
654 goto error;
655
656 server->namelen = data->namlen;
657 /* Create a client RPC handle for the NFSv3 ACL management interface */
658 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400659 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
660 return 0;
661
662error:
663 server->nfs_client = NULL;
664 nfs_put_client(clp);
665 dprintk("<-- nfs_init_server() = xerror %d\n", error);
666 return error;
667}
668
669/*
670 * Load up the server record from information gained in an fsinfo record
671 */
672static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
673{
674 unsigned long max_rpc_payload;
675
676 /* Work out a lot of parameters */
677 if (server->rsize == 0)
678 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
679 if (server->wsize == 0)
680 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
681
682 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
683 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
684 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
685 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
686
687 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
688 if (server->rsize > max_rpc_payload)
689 server->rsize = max_rpc_payload;
690 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
691 server->rsize = NFS_MAX_FILE_IO_SIZE;
692 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700693
David Howells54ceac42006-08-22 20:06:13 -0400694 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
695
696 if (server->wsize > max_rpc_payload)
697 server->wsize = max_rpc_payload;
698 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
699 server->wsize = NFS_MAX_FILE_IO_SIZE;
700 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
701 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
702
703 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
704 if (server->dtsize > PAGE_CACHE_SIZE)
705 server->dtsize = PAGE_CACHE_SIZE;
706 if (server->dtsize > server->rsize)
707 server->dtsize = server->rsize;
708
709 if (server->flags & NFS_MOUNT_NOAC) {
710 server->acregmin = server->acregmax = 0;
711 server->acdirmin = server->acdirmax = 0;
712 }
713
714 server->maxfilesize = fsinfo->maxfilesize;
715
716 /* We're airborne Set socket buffersize */
717 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
718}
719
720/*
721 * Probe filesystem information, including the FSID on v2/v3
722 */
723static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
724{
725 struct nfs_fsinfo fsinfo;
726 struct nfs_client *clp = server->nfs_client;
727 int error;
728
729 dprintk("--> nfs_probe_fsinfo()\n");
730
731 if (clp->rpc_ops->set_capabilities != NULL) {
732 error = clp->rpc_ops->set_capabilities(server, mntfh);
733 if (error < 0)
734 goto out_error;
735 }
736
737 fsinfo.fattr = fattr;
738 nfs_fattr_init(fattr);
739 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
740 if (error < 0)
741 goto out_error;
742
743 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700744 error = bdi_init(&server->backing_dev_info);
745 if (error)
746 goto out_error;
747
David Howells54ceac42006-08-22 20:06:13 -0400748
749 /* Get some general file system info */
750 if (server->namelen == 0) {
751 struct nfs_pathconf pathinfo;
752
753 pathinfo.fattr = fattr;
754 nfs_fattr_init(fattr);
755
756 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
757 server->namelen = pathinfo.max_namelen;
758 }
759
760 dprintk("<-- nfs_probe_fsinfo() = 0\n");
761 return 0;
762
763out_error:
764 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
765 return error;
766}
767
768/*
769 * Copy useful information when duplicating a server record
770 */
771static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
772{
773 target->flags = source->flags;
774 target->acregmin = source->acregmin;
775 target->acregmax = source->acregmax;
776 target->acdirmin = source->acdirmin;
777 target->acdirmax = source->acdirmax;
778 target->caps = source->caps;
779}
780
781/*
782 * Allocate and initialise a server record
783 */
784static struct nfs_server *nfs_alloc_server(void)
785{
786 struct nfs_server *server;
787
788 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
789 if (!server)
790 return NULL;
791
792 server->client = server->client_acl = ERR_PTR(-EINVAL);
793
794 /* Zero out the NFS state stuff */
795 INIT_LIST_HEAD(&server->client_link);
796 INIT_LIST_HEAD(&server->master_link);
797
Steve Dicksonef818a22007-11-08 04:05:04 -0500798 init_waitqueue_head(&server->active_wq);
799 atomic_set(&server->active, 0);
800
David Howells54ceac42006-08-22 20:06:13 -0400801 server->io_stats = nfs_alloc_iostats();
802 if (!server->io_stats) {
803 kfree(server);
804 return NULL;
805 }
806
807 return server;
808}
809
810/*
811 * Free up a server record
812 */
813void nfs_free_server(struct nfs_server *server)
814{
815 dprintk("--> nfs_free_server()\n");
816
817 spin_lock(&nfs_client_lock);
818 list_del(&server->client_link);
819 list_del(&server->master_link);
820 spin_unlock(&nfs_client_lock);
821
822 if (server->destroy != NULL)
823 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500824
825 if (!IS_ERR(server->client_acl))
826 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400827 if (!IS_ERR(server->client))
828 rpc_shutdown_client(server->client);
829
830 nfs_put_client(server->nfs_client);
831
832 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700833 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400834 kfree(server);
835 nfs_release_automount_timer();
836 dprintk("<-- nfs_free_server()\n");
837}
838
839/*
840 * Create a version 2 or 3 volume record
841 * - keyed on server and FSID
842 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400843struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400844 struct nfs_fh *mntfh)
845{
846 struct nfs_server *server;
847 struct nfs_fattr fattr;
848 int error;
849
850 server = nfs_alloc_server();
851 if (!server)
852 return ERR_PTR(-ENOMEM);
853
854 /* Get a client representation */
855 error = nfs_init_server(server, data);
856 if (error < 0)
857 goto error;
858
859 BUG_ON(!server->nfs_client);
860 BUG_ON(!server->nfs_client->rpc_ops);
861 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
862
863 /* Probe the root fh to retrieve its FSID */
864 error = nfs_probe_fsinfo(server, mntfh, &fattr);
865 if (error < 0)
866 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400867 if (server->nfs_client->rpc_ops->version == 3) {
868 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
869 server->namelen = NFS3_MAXNAMLEN;
870 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
871 server->caps |= NFS_CAP_READDIRPLUS;
872 } else {
873 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
874 server->namelen = NFS2_MAXNAMLEN;
875 }
876
David Howells54ceac42006-08-22 20:06:13 -0400877 if (!(fattr.valid & NFS_ATTR_FATTR)) {
878 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
879 if (error < 0) {
880 dprintk("nfs_create_server: getattr error = %d\n", -error);
881 goto error;
882 }
883 }
884 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
885
David Howells6daabf12006-08-24 15:44:16 -0400886 dprintk("Server FSID: %llx:%llx\n",
887 (unsigned long long) server->fsid.major,
888 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400889
890 BUG_ON(!server->nfs_client);
891 BUG_ON(!server->nfs_client->rpc_ops);
892 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
893
894 spin_lock(&nfs_client_lock);
895 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
896 list_add_tail(&server->master_link, &nfs_volume_list);
897 spin_unlock(&nfs_client_lock);
898
899 server->mount_time = jiffies;
900 return server;
901
902error:
903 nfs_free_server(server);
904 return ERR_PTR(error);
905}
906
907#ifdef CONFIG_NFS_V4
908/*
909 * Initialise an NFS4 client record
910 */
911static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500912 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700913 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400914 rpc_authflavor_t authflavour)
915{
916 int error;
917
918 if (clp->cl_cons_state == NFS_CS_READY) {
919 /* the client is initialised already */
920 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
921 return 0;
922 }
923
924 /* Check NFS protocol revision and initialize RPC op vector */
925 clp->rpc_ops = &nfs_v4_clientops;
926
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500927 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500928 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400929 if (error < 0)
930 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700931 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400932
933 error = nfs_idmap_new(clp);
934 if (error < 0) {
935 dprintk("%s: failed to create idmapper. Error = %d\n",
936 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400937 goto error;
938 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400939 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400940
941 nfs_mark_client_ready(clp, NFS_CS_READY);
942 return 0;
943
944error:
945 nfs_mark_client_ready(clp, error);
946 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
947 return error;
948}
949
950/*
951 * Set up an NFS4 client
952 */
953static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500954 const char *hostname,
955 const struct sockaddr *addr,
956 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700957 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400958 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -0500959 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -0400960{
Trond Myklebust3a498022007-12-14 14:56:04 -0500961 struct nfs_client_initdata cl_init = {
962 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500963 .addr = addr,
964 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500965 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500966 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -0500967 };
David Howells54ceac42006-08-22 20:06:13 -0400968 struct nfs_client *clp;
969 int error;
970
971 dprintk("--> nfs4_set_client()\n");
972
973 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500974 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400975 if (IS_ERR(clp)) {
976 error = PTR_ERR(clp);
977 goto error;
978 }
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500979 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400980 if (error < 0)
981 goto error_put;
982
983 server->nfs_client = clp;
984 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
985 return 0;
986
987error_put:
988 nfs_put_client(clp);
989error:
990 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
991 return error;
992}
993
994/*
995 * Create a version 4 volume record
996 */
997static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400998 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400999{
Trond Myklebust33170232007-12-20 16:03:59 -05001000 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001001 int error;
1002
1003 dprintk("--> nfs4_init_server()\n");
1004
Trond Myklebust33170232007-12-20 16:03:59 -05001005 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1006 data->timeo, data->retrans);
1007
1008 /* Get a client record */
1009 error = nfs4_set_client(server,
1010 data->nfs_server.hostname,
1011 (const struct sockaddr *)&data->nfs_server.address,
1012 data->nfs_server.addrlen,
1013 data->client_address,
1014 data->auth_flavors[0],
1015 data->nfs_server.protocol,
1016 &timeparms);
1017 if (error < 0)
1018 goto error;
1019
David Howells54ceac42006-08-22 20:06:13 -04001020 /* Initialise the client representation from the mount data */
1021 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1022 server->caps |= NFS_CAP_ATOMIC_OPEN;
1023
1024 if (data->rsize)
1025 server->rsize = nfs_block_size(data->rsize, NULL);
1026 if (data->wsize)
1027 server->wsize = nfs_block_size(data->wsize, NULL);
1028
1029 server->acregmin = data->acregmin * HZ;
1030 server->acregmax = data->acregmax * HZ;
1031 server->acdirmin = data->acdirmin * HZ;
1032 server->acdirmax = data->acdirmax * HZ;
1033
Trond Myklebust33170232007-12-20 16:03:59 -05001034 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001035
Trond Myklebust33170232007-12-20 16:03:59 -05001036error:
David Howells54ceac42006-08-22 20:06:13 -04001037 /* Done */
1038 dprintk("<-- nfs4_init_server() = %d\n", error);
1039 return error;
1040}
1041
1042/*
1043 * Create a version 4 volume record
1044 * - keyed on server and FSID
1045 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001046struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001047 struct nfs_fh *mntfh)
1048{
1049 struct nfs_fattr fattr;
1050 struct nfs_server *server;
1051 int error;
1052
1053 dprintk("--> nfs4_create_server()\n");
1054
1055 server = nfs_alloc_server();
1056 if (!server)
1057 return ERR_PTR(-ENOMEM);
1058
David Howells54ceac42006-08-22 20:06:13 -04001059 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001060 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001061 if (error < 0)
1062 goto error;
1063
1064 BUG_ON(!server->nfs_client);
1065 BUG_ON(!server->nfs_client->rpc_ops);
1066 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1067
1068 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001069 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001070 if (error < 0)
1071 goto error;
1072
David Howells6daabf12006-08-24 15:44:16 -04001073 dprintk("Server FSID: %llx:%llx\n",
1074 (unsigned long long) server->fsid.major,
1075 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001076 dprintk("Mount FH: %d\n", mntfh->size);
1077
1078 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1079 if (error < 0)
1080 goto error;
1081
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001082 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1083 server->namelen = NFS4_MAXNAMLEN;
1084
David Howells54ceac42006-08-22 20:06:13 -04001085 BUG_ON(!server->nfs_client);
1086 BUG_ON(!server->nfs_client->rpc_ops);
1087 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1088
1089 spin_lock(&nfs_client_lock);
1090 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1091 list_add_tail(&server->master_link, &nfs_volume_list);
1092 spin_unlock(&nfs_client_lock);
1093
1094 server->mount_time = jiffies;
1095 dprintk("<-- nfs4_create_server() = %p\n", server);
1096 return server;
1097
1098error:
1099 nfs_free_server(server);
1100 dprintk("<-- nfs4_create_server() = error %d\n", error);
1101 return ERR_PTR(error);
1102}
1103
1104/*
1105 * Create an NFS4 referral server record
1106 */
1107struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001108 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001109{
1110 struct nfs_client *parent_client;
1111 struct nfs_server *server, *parent_server;
1112 struct nfs_fattr fattr;
1113 int error;
1114
1115 dprintk("--> nfs4_create_referral_server()\n");
1116
1117 server = nfs_alloc_server();
1118 if (!server)
1119 return ERR_PTR(-ENOMEM);
1120
1121 parent_server = NFS_SB(data->sb);
1122 parent_client = parent_server->nfs_client;
1123
1124 /* Get a client representation.
1125 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001126 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001127 data->addr,
1128 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001129 parent_client->cl_ipaddr,
1130 data->authflavor,
1131 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001132 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001133 if (error < 0)
1134 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001135
1136 /* Initialise the client representation from the parent server */
1137 nfs_server_copy_userdata(server, parent_server);
1138 server->caps |= NFS_CAP_ATOMIC_OPEN;
1139
Trond Myklebust33170232007-12-20 16:03:59 -05001140 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001141 if (error < 0)
1142 goto error;
1143
1144 BUG_ON(!server->nfs_client);
1145 BUG_ON(!server->nfs_client->rpc_ops);
1146 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1147
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001148 /* Probe the root fh to retrieve its FSID and filehandle */
1149 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1150 if (error < 0)
1151 goto error;
1152
David Howells54ceac42006-08-22 20:06:13 -04001153 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001154 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001155 if (error < 0)
1156 goto error;
1157
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001158 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1159 server->namelen = NFS4_MAXNAMLEN;
1160
David Howells54ceac42006-08-22 20:06:13 -04001161 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001162 (unsigned long long) server->fsid.major,
1163 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001164
1165 spin_lock(&nfs_client_lock);
1166 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1167 list_add_tail(&server->master_link, &nfs_volume_list);
1168 spin_unlock(&nfs_client_lock);
1169
1170 server->mount_time = jiffies;
1171
1172 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1173 return server;
1174
1175error:
1176 nfs_free_server(server);
1177 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1178 return ERR_PTR(error);
1179}
1180
1181#endif /* CONFIG_NFS_V4 */
1182
1183/*
1184 * Clone an NFS2, NFS3 or NFS4 server record
1185 */
1186struct nfs_server *nfs_clone_server(struct nfs_server *source,
1187 struct nfs_fh *fh,
1188 struct nfs_fattr *fattr)
1189{
1190 struct nfs_server *server;
1191 struct nfs_fattr fattr_fsinfo;
1192 int error;
1193
1194 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001195 (unsigned long long) fattr->fsid.major,
1196 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001197
1198 server = nfs_alloc_server();
1199 if (!server)
1200 return ERR_PTR(-ENOMEM);
1201
1202 /* Copy data from the source */
1203 server->nfs_client = source->nfs_client;
1204 atomic_inc(&server->nfs_client->cl_count);
1205 nfs_server_copy_userdata(server, source);
1206
1207 server->fsid = fattr->fsid;
1208
Trond Myklebust33170232007-12-20 16:03:59 -05001209 error = nfs_init_server_rpcclient(server,
1210 source->client->cl_timeout,
1211 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001212 if (error < 0)
1213 goto out_free_server;
1214 if (!IS_ERR(source->client_acl))
1215 nfs_init_server_aclclient(server);
1216
1217 /* probe the filesystem info for this server filesystem */
1218 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1219 if (error < 0)
1220 goto out_free_server;
1221
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001222 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1223 server->namelen = NFS4_MAXNAMLEN;
1224
David Howells54ceac42006-08-22 20:06:13 -04001225 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001226 (unsigned long long) server->fsid.major,
1227 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001228
1229 error = nfs_start_lockd(server);
1230 if (error < 0)
1231 goto out_free_server;
1232
1233 spin_lock(&nfs_client_lock);
1234 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1235 list_add_tail(&server->master_link, &nfs_volume_list);
1236 spin_unlock(&nfs_client_lock);
1237
1238 server->mount_time = jiffies;
1239
1240 dprintk("<-- nfs_clone_server() = %p\n", server);
1241 return server;
1242
1243out_free_server:
1244 nfs_free_server(server);
1245 dprintk("<-- nfs_clone_server() = error %d\n", error);
1246 return ERR_PTR(error);
1247}
David Howells6aaca562006-08-22 20:06:13 -04001248
1249#ifdef CONFIG_PROC_FS
1250static struct proc_dir_entry *proc_fs_nfs;
1251
1252static int nfs_server_list_open(struct inode *inode, struct file *file);
1253static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1254static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1255static void nfs_server_list_stop(struct seq_file *p, void *v);
1256static int nfs_server_list_show(struct seq_file *m, void *v);
1257
1258static struct seq_operations nfs_server_list_ops = {
1259 .start = nfs_server_list_start,
1260 .next = nfs_server_list_next,
1261 .stop = nfs_server_list_stop,
1262 .show = nfs_server_list_show,
1263};
1264
Arjan van de Ven00977a52007-02-12 00:55:34 -08001265static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001266 .open = nfs_server_list_open,
1267 .read = seq_read,
1268 .llseek = seq_lseek,
1269 .release = seq_release,
1270};
1271
1272static int nfs_volume_list_open(struct inode *inode, struct file *file);
1273static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1274static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1275static void nfs_volume_list_stop(struct seq_file *p, void *v);
1276static int nfs_volume_list_show(struct seq_file *m, void *v);
1277
1278static struct seq_operations nfs_volume_list_ops = {
1279 .start = nfs_volume_list_start,
1280 .next = nfs_volume_list_next,
1281 .stop = nfs_volume_list_stop,
1282 .show = nfs_volume_list_show,
1283};
1284
Arjan van de Ven00977a52007-02-12 00:55:34 -08001285static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001286 .open = nfs_volume_list_open,
1287 .read = seq_read,
1288 .llseek = seq_lseek,
1289 .release = seq_release,
1290};
1291
1292/*
1293 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1294 * we're dealing
1295 */
1296static int nfs_server_list_open(struct inode *inode, struct file *file)
1297{
1298 struct seq_file *m;
1299 int ret;
1300
1301 ret = seq_open(file, &nfs_server_list_ops);
1302 if (ret < 0)
1303 return ret;
1304
1305 m = file->private_data;
1306 m->private = PDE(inode)->data;
1307
1308 return 0;
1309}
1310
1311/*
1312 * set up the iterator to start reading from the server list and return the first item
1313 */
1314static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1315{
David Howells6aaca562006-08-22 20:06:13 -04001316 /* lock the list against modification */
1317 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001318 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001319}
1320
1321/*
1322 * move to next server
1323 */
1324static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1325{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001326 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001327}
1328
1329/*
1330 * clean up after reading from the transports list
1331 */
1332static void nfs_server_list_stop(struct seq_file *p, void *v)
1333{
1334 spin_unlock(&nfs_client_lock);
1335}
1336
1337/*
1338 * display a header line followed by a load of call lines
1339 */
1340static int nfs_server_list_show(struct seq_file *m, void *v)
1341{
1342 struct nfs_client *clp;
1343
1344 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001345 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001346 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1347 return 0;
1348 }
1349
1350 /* display one transport per line on subsequent lines */
1351 clp = list_entry(v, struct nfs_client, cl_share_link);
1352
Chuck Lever5d8515c2007-12-10 14:57:16 -05001353 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001354 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001355 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1356 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001357 atomic_read(&clp->cl_count),
1358 clp->cl_hostname);
1359
1360 return 0;
1361}
1362
1363/*
1364 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1365 */
1366static int nfs_volume_list_open(struct inode *inode, struct file *file)
1367{
1368 struct seq_file *m;
1369 int ret;
1370
1371 ret = seq_open(file, &nfs_volume_list_ops);
1372 if (ret < 0)
1373 return ret;
1374
1375 m = file->private_data;
1376 m->private = PDE(inode)->data;
1377
1378 return 0;
1379}
1380
1381/*
1382 * set up the iterator to start reading from the volume list and return the first item
1383 */
1384static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1385{
David Howells6aaca562006-08-22 20:06:13 -04001386 /* lock the list against modification */
1387 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001388 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001389}
1390
1391/*
1392 * move to next volume
1393 */
1394static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1395{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001396 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001397}
1398
1399/*
1400 * clean up after reading from the transports list
1401 */
1402static void nfs_volume_list_stop(struct seq_file *p, void *v)
1403{
1404 spin_unlock(&nfs_client_lock);
1405}
1406
1407/*
1408 * display a header line followed by a load of call lines
1409 */
1410static int nfs_volume_list_show(struct seq_file *m, void *v)
1411{
1412 struct nfs_server *server;
1413 struct nfs_client *clp;
1414 char dev[8], fsid[17];
1415
1416 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001417 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001418 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1419 return 0;
1420 }
1421 /* display one transport per line on subsequent lines */
1422 server = list_entry(v, struct nfs_server, master_link);
1423 clp = server->nfs_client;
1424
1425 snprintf(dev, 8, "%u:%u",
1426 MAJOR(server->s_dev), MINOR(server->s_dev));
1427
1428 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001429 (unsigned long long) server->fsid.major,
1430 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001431
Chuck Lever5d8515c2007-12-10 14:57:16 -05001432 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001433 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001434 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1435 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001436 dev,
1437 fsid);
1438
1439 return 0;
1440}
1441
1442/*
1443 * initialise the /proc/fs/nfsfs/ directory
1444 */
1445int __init nfs_fs_proc_init(void)
1446{
1447 struct proc_dir_entry *p;
1448
1449 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1450 if (!proc_fs_nfs)
1451 goto error_0;
1452
1453 proc_fs_nfs->owner = THIS_MODULE;
1454
1455 /* a file of servers with which we're dealing */
1456 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1457 if (!p)
1458 goto error_1;
1459
1460 p->proc_fops = &nfs_server_list_fops;
1461 p->owner = THIS_MODULE;
1462
1463 /* a file of volumes that we have mounted */
1464 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1465 if (!p)
1466 goto error_2;
1467
1468 p->proc_fops = &nfs_volume_list_fops;
1469 p->owner = THIS_MODULE;
1470 return 0;
1471
1472error_2:
1473 remove_proc_entry("servers", proc_fs_nfs);
1474error_1:
1475 remove_proc_entry("nfsfs", proc_root_fs);
1476error_0:
1477 return -ENOMEM;
1478}
1479
1480/*
1481 * clean up the /proc/fs/nfsfs/ directory
1482 */
1483void nfs_fs_proc_exit(void)
1484{
1485 remove_proc_entry("volumes", proc_fs_nfs);
1486 remove_proc_entry("servers", proc_fs_nfs);
1487 remove_proc_entry("nfsfs", proc_root_fs);
1488}
1489
1490#endif /* CONFIG_PROC_FS */