blob: 574158ae2398aaeb1151d76f0a85f2b73871b976 [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;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400115 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400116
117 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
118 goto error_0;
119
Trond Myklebust40c553192007-12-14 14:56:07 -0500120 clp->rpc_ops = cl_init->rpc_ops;
121
122 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400123 if (nfs_callback_up() < 0)
124 goto error_2;
125 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
126 }
127
128 atomic_set(&clp->cl_count, 1);
129 clp->cl_cons_state = NFS_CS_INITING;
130
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500131 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
132 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400133
Trond Myklebust3a498022007-12-14 14:56:04 -0500134 if (cl_init->hostname) {
135 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400136 if (!clp->cl_hostname)
137 goto error_3;
138 }
139
140 INIT_LIST_HEAD(&clp->cl_superblocks);
141 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500143 clp->cl_proto = cl_init->proto;
144
David Howells24c8dbb2006-08-22 20:06:10 -0400145#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400146 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
Trond Myklebust7c67db32008-04-07 20:50:11 -0400153 cred = rpc_lookup_machine_cred();
154 if (!IS_ERR(cred))
155 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400156
157 return clp;
158
159error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400160 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
161 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400162error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400163 kfree(clp);
164error_0:
165 return NULL;
166}
167
Trond Myklebust5dd31772006-08-24 01:03:05 -0400168static void nfs4_shutdown_client(struct nfs_client *clp)
169{
170#ifdef CONFIG_NFS_V4
171 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
172 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400173 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400174 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
175 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500176
177 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400178#endif
179}
180
David Howells24c8dbb2006-08-22 20:06:10 -0400181/*
182 * Destroy a shared client record
183 */
184static void nfs_free_client(struct nfs_client *clp)
185{
Trond Myklebust40c553192007-12-14 14:56:07 -0500186 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400187
Trond Myklebust5dd31772006-08-24 01:03:05 -0400188 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400189
190 /* -EIO all pending I/O */
191 if (!IS_ERR(clp->cl_rpcclient))
192 rpc_shutdown_client(clp->cl_rpcclient);
193
194 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
195 nfs_callback_down();
196
Trond Myklebust7c67db32008-04-07 20:50:11 -0400197 if (clp->cl_machine_cred != NULL)
198 put_rpccred(clp->cl_machine_cred);
199
David Howells24c8dbb2006-08-22 20:06:10 -0400200 kfree(clp->cl_hostname);
201 kfree(clp);
202
203 dprintk("<-- nfs_free_client()\n");
204}
205
206/*
207 * Release a reference to a shared client record
208 */
209void nfs_put_client(struct nfs_client *clp)
210{
David Howells27ba8512006-07-30 14:40:56 -0400211 if (!clp)
212 return;
213
David Howells24c8dbb2006-08-22 20:06:10 -0400214 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
215
216 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
217 list_del(&clp->cl_share_link);
218 spin_unlock(&nfs_client_lock);
219
220 BUG_ON(!list_empty(&clp->cl_superblocks));
221
222 nfs_free_client(clp);
223 }
224}
225
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500226#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
227static const struct in6_addr *nfs_map_ipv4_addr(const struct sockaddr *sa, struct in6_addr *addr_mapped)
228{
229 switch (sa->sa_family) {
230 default:
231 return NULL;
232 case AF_INET6:
233 return &((const struct sockaddr_in6 *)sa)->sin6_addr;
234 break;
235 case AF_INET:
236 ipv6_addr_set_v4mapped(((const struct sockaddr_in *)sa)->sin_addr.s_addr,
237 addr_mapped);
238 return addr_mapped;
239 }
240}
241
242static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
243 const struct sockaddr *sa2)
244{
245 const struct in6_addr *addr1;
246 const struct in6_addr *addr2;
247 struct in6_addr addr1_mapped;
248 struct in6_addr addr2_mapped;
249
250 addr1 = nfs_map_ipv4_addr(sa1, &addr1_mapped);
251 if (likely(addr1 != NULL)) {
252 addr2 = nfs_map_ipv4_addr(sa2, &addr2_mapped);
253 if (likely(addr2 != NULL))
254 return ipv6_addr_equal(addr1, addr2);
255 }
256 return 0;
257}
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400258
259/*
260 * Test if two ip6 socket addresses refer to the same socket by
261 * comparing relevant fields. The padding bytes specifically, are not
262 * compared. sin6_flowinfo is not compared because it only affects QoS
263 * and sin6_scope_id is only compared if the address is "link local"
264 * because "link local" addresses need only be unique to a specific
265 * link. Conversely, ordinary unicast addresses might have different
266 * sin6_scope_id.
267 *
268 * The caller should ensure both socket addresses are AF_INET6.
269 */
270static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
271 const struct sockaddr *sa2)
272{
273 const struct sockaddr_in6 *saddr1 = (const struct sockaddr_in6 *)sa1;
274 const struct sockaddr_in6 *saddr2 = (const struct sockaddr_in6 *)sa2;
275
276 if (!ipv6_addr_equal(&saddr1->sin6_addr,
277 &saddr1->sin6_addr))
278 return 0;
279 if (ipv6_addr_scope(&saddr1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
280 saddr1->sin6_scope_id != saddr2->sin6_scope_id)
281 return 0;
282 return saddr1->sin6_port == saddr2->sin6_port;
283}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500284#else
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500285static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
286 const struct sockaddr_in *sa2)
287{
288 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
289}
290
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500291static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
292 const struct sockaddr *sa2)
293{
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500294 if (unlikely(sa1->sa_family != AF_INET || sa2->sa_family != AF_INET))
295 return 0;
296 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
297 (const struct sockaddr_in *)sa2);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500298}
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400299
300static int nfs_sockaddr_cmp_ip6(const struct sockaddr * sa1,
301 const struct sockaddr * sa2)
302{
303 return 0;
304}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500305#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500306
David Howells24c8dbb2006-08-22 20:06:10 -0400307/*
Ian Dalld7371c42009-03-10 20:33:22 -0400308 * Test if two ip4 socket addresses refer to the same socket, by
309 * comparing relevant fields. The padding bytes specifically, are
310 * not compared.
311 *
312 * The caller should ensure both socket addresses are AF_INET.
313 */
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400314static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
315 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400316{
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400317 const struct sockaddr_in *saddr1 = (const struct sockaddr_in *)sa1;
318 const struct sockaddr_in *saddr2 = (const struct sockaddr_in *)sa2;
319
Ian Dalld7371c42009-03-10 20:33:22 -0400320 if (saddr1->sin_addr.s_addr != saddr2->sin_addr.s_addr)
321 return 0;
322 return saddr1->sin_port == saddr2->sin_port;
323}
324
325/*
Ian Dalld7371c42009-03-10 20:33:22 -0400326 * Test if two socket addresses represent the same actual socket,
327 * by comparing (only) relevant fields.
328 */
329static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
330 const struct sockaddr *sa2)
331{
332 if (sa1->sa_family != sa2->sa_family)
333 return 0;
334
335 switch (sa1->sa_family) {
336 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400337 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400338 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400339 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400340 }
341 return 0;
342}
343
344/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500345 * Find a client by IP address and protocol version
346 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400347 */
Chuck Leverff052642007-12-10 14:58:44 -0500348struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500349{
350 struct nfs_client *clp;
351
352 spin_lock(&nfs_client_lock);
353 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500354 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
355
Trond Myklebustc81468a2007-12-14 14:56:05 -0500356 /* Don't match clients that failed to initialise properly */
357 if (clp->cl_cons_state != NFS_CS_READY)
358 continue;
359
360 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500361 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500362 continue;
363
364 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500365 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500366 continue;
367
368 atomic_inc(&clp->cl_count);
369 spin_unlock(&nfs_client_lock);
370 return clp;
371 }
372 spin_unlock(&nfs_client_lock);
373 return NULL;
374}
375
376/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500377 * Find a client by IP address and protocol version
378 * - returns NULL if no such client
379 */
380struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
381{
382 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
383 u32 nfsvers = clp->rpc_ops->version;
384
385 spin_lock(&nfs_client_lock);
386 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
387 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
388
389 /* Don't match clients that failed to initialise properly */
390 if (clp->cl_cons_state != NFS_CS_READY)
391 continue;
392
393 /* Different NFS versions cannot share the same nfs_client */
394 if (clp->rpc_ops->version != nfsvers)
395 continue;
396
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500397 /* Match only the IP address, not the port number */
398 if (!nfs_sockaddr_match_ipaddr(sap, clap))
399 continue;
400
401 atomic_inc(&clp->cl_count);
402 spin_unlock(&nfs_client_lock);
403 return clp;
404 }
405 spin_unlock(&nfs_client_lock);
406 return NULL;
407}
408
409/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500410 * Find an nfs_client on the list that matches the initialisation data
411 * that is supplied.
412 */
413static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400414{
415 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400416 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400417
418 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400419 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700420 /* Don't match clients that failed to initialise properly */
421 if (clp->cl_cons_state < 0)
422 continue;
423
David Howells24c8dbb2006-08-22 20:06:10 -0400424 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500425 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400426 continue;
427
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500428 if (clp->cl_proto != data->proto)
429 continue;
430
Trond Myklebustc81468a2007-12-14 14:56:05 -0500431 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400432 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400433 continue;
434
Trond Myklebustc81468a2007-12-14 14:56:05 -0500435 atomic_inc(&clp->cl_count);
436 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400437 }
David Howells24c8dbb2006-08-22 20:06:10 -0400438 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400439}
440
441/*
442 * Look up a client by IP address and protocol version
443 * - creates a new record if one doesn't yet exist
444 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500445static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400446{
447 struct nfs_client *clp, *new = NULL;
448 int error;
449
Chuck Leverd7422c42007-12-10 14:58:51 -0500450 dprintk("--> nfs_get_client(%s,v%u)\n",
451 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400452
453 /* see if the client already exists */
454 do {
455 spin_lock(&nfs_client_lock);
456
Trond Myklebustc81468a2007-12-14 14:56:05 -0500457 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400458 if (clp)
459 goto found_client;
460 if (new)
461 goto install_client;
462
463 spin_unlock(&nfs_client_lock);
464
Trond Myklebust3a498022007-12-14 14:56:04 -0500465 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400466 } while (new);
467
468 return ERR_PTR(-ENOMEM);
469
470 /* install a new client and return with it unready */
471install_client:
472 clp = new;
473 list_add(&clp->cl_share_link, &nfs_client_list);
474 spin_unlock(&nfs_client_lock);
475 dprintk("--> nfs_get_client() = %p [new]\n", clp);
476 return clp;
477
478 /* found an existing client
479 * - make sure it's ready before returning
480 */
481found_client:
482 spin_unlock(&nfs_client_lock);
483
484 if (new)
485 nfs_free_client(new);
486
Matthew Wilcox150030b2007-12-06 16:24:39 -0500487 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400488 clp->cl_cons_state != NFS_CS_INITING);
489 if (error < 0) {
490 nfs_put_client(clp);
491 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400492 }
493
494 if (clp->cl_cons_state < NFS_CS_READY) {
495 error = clp->cl_cons_state;
496 nfs_put_client(clp);
497 return ERR_PTR(error);
498 }
499
David Howells54ceac42006-08-22 20:06:13 -0400500 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
501
David Howells24c8dbb2006-08-22 20:06:10 -0400502 dprintk("--> nfs_get_client() = %p [share]\n", clp);
503 return clp;
504}
505
506/*
507 * Mark a server as ready or failed
508 */
David Howells54ceac42006-08-22 20:06:13 -0400509static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400510{
511 clp->cl_cons_state = state;
512 wake_up_all(&nfs_client_active_wq);
513}
David Howells5006a762006-08-22 20:06:12 -0400514
515/*
516 * Initialise the timeout values for a connection
517 */
518static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
519 unsigned int timeo, unsigned int retrans)
520{
521 to->to_initval = timeo * HZ / 10;
522 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400523
524 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400525 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400526 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400527 if (to->to_retries == 0)
528 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500529 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400530 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400531 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
532 to->to_initval = NFS_MAX_TCP_TIMEOUT;
533 to->to_increment = to->to_initval;
534 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500535 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
536 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
537 if (to->to_maxval < to->to_initval)
538 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400539 to->to_exponential = 0;
540 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400541 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400542 if (to->to_retries == 0)
543 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400544 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400545 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400546 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
547 to->to_initval = NFS_MAX_UDP_TIMEOUT;
548 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
549 to->to_exponential = 1;
550 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400551 default:
552 BUG();
David Howells5006a762006-08-22 20:06:12 -0400553 }
554}
555
556/*
557 * Create an RPC client handle
558 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500559static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500560 const struct rpc_timeout *timeparms,
561 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500562 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400563{
David Howells5006a762006-08-22 20:06:12 -0400564 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400565 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500566 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400567 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500568 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500569 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400570 .servername = clp->cl_hostname,
571 .program = &nfs_program,
572 .version = clp->rpc_ops->version,
573 .authflavor = flavor,
574 };
David Howells5006a762006-08-22 20:06:12 -0400575
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500576 if (discrtry)
577 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
578 if (noresvport)
579 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
580
David Howells5006a762006-08-22 20:06:12 -0400581 if (!IS_ERR(clp->cl_rpcclient))
582 return 0;
583
Chuck Lever41877d22006-08-22 20:06:20 -0400584 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400585 if (IS_ERR(clnt)) {
586 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700587 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400588 return PTR_ERR(clnt);
589 }
590
David Howells5006a762006-08-22 20:06:12 -0400591 clp->cl_rpcclient = clnt;
592 return 0;
593}
David Howells54ceac42006-08-22 20:06:13 -0400594
595/*
596 * Version 2 or 3 client destruction
597 */
598static void nfs_destroy_server(struct nfs_server *server)
599{
David Howells54ceac42006-08-22 20:06:13 -0400600 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500601 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400602}
603
604/*
605 * Version 2 or 3 lockd setup
606 */
607static int nfs_start_lockd(struct nfs_server *server)
608{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500609 struct nlm_host *host;
610 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500611 struct nlmclnt_initdata nlm_init = {
612 .hostname = clp->cl_hostname,
613 .address = (struct sockaddr *)&clp->cl_addr,
614 .addrlen = clp->cl_addrlen,
615 .protocol = server->flags & NFS_MOUNT_TCP ?
616 IPPROTO_TCP : IPPROTO_UDP,
617 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500618 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
619 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500620 };
David Howells54ceac42006-08-22 20:06:13 -0400621
Chuck Lever883bb162008-01-15 16:04:20 -0500622 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500623 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400624 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500625 return 0;
626
Chuck Lever883bb162008-01-15 16:04:20 -0500627 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500628 if (IS_ERR(host))
629 return PTR_ERR(host);
630
631 server->nlm_host = host;
632 server->destroy = nfs_destroy_server;
633 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400634}
635
636/*
637 * Initialise an NFSv3 ACL client connection
638 */
639#ifdef CONFIG_NFS_V3_ACL
640static void nfs_init_server_aclclient(struct nfs_server *server)
641{
Trond Myklebust40c553192007-12-14 14:56:07 -0500642 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400643 goto out_noacl;
644 if (server->flags & NFS_MOUNT_NOACL)
645 goto out_noacl;
646
647 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
648 if (IS_ERR(server->client_acl))
649 goto out_noacl;
650
651 /* No errors! Assume that Sun nfsacls are supported */
652 server->caps |= NFS_CAP_ACLS;
653 return;
654
655out_noacl:
656 server->caps &= ~NFS_CAP_ACLS;
657}
658#else
659static inline void nfs_init_server_aclclient(struct nfs_server *server)
660{
661 server->flags &= ~NFS_MOUNT_NOACL;
662 server->caps &= ~NFS_CAP_ACLS;
663}
664#endif
665
666/*
667 * Create a general RPC client
668 */
Trond Myklebust33170232007-12-20 16:03:59 -0500669static int nfs_init_server_rpcclient(struct nfs_server *server,
670 const struct rpc_timeout *timeo,
671 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400672{
673 struct nfs_client *clp = server->nfs_client;
674
675 server->client = rpc_clone_client(clp->cl_rpcclient);
676 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700677 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400678 return PTR_ERR(server->client);
679 }
680
Trond Myklebust33170232007-12-20 16:03:59 -0500681 memcpy(&server->client->cl_timeout_default,
682 timeo,
683 sizeof(server->client->cl_timeout_default));
684 server->client->cl_timeout = &server->client->cl_timeout_default;
685
David Howells54ceac42006-08-22 20:06:13 -0400686 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
687 struct rpc_auth *auth;
688
689 auth = rpcauth_create(pseudoflavour, server->client);
690 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700691 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400692 return PTR_ERR(auth);
693 }
694 }
695 server->client->cl_softrtry = 0;
696 if (server->flags & NFS_MOUNT_SOFT)
697 server->client->cl_softrtry = 1;
698
David Howells54ceac42006-08-22 20:06:13 -0400699 return 0;
700}
701
702/*
703 * Initialise an NFS2 or NFS3 client
704 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400705static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500706 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400707 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400708{
David Howells54ceac42006-08-22 20:06:13 -0400709 int error;
710
711 if (clp->cl_cons_state == NFS_CS_READY) {
712 /* the client is already initialised */
713 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
714 return 0;
715 }
716
David Howells54ceac42006-08-22 20:06:13 -0400717 /*
718 * Create a client RPC handle for doing FSSTAT with UNIX auth only
719 * - RFC 2623, sec 2.3.2
720 */
Chuck Leverd7403512008-12-23 15:21:37 -0500721 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
722 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400723 if (error < 0)
724 goto error;
725 nfs_mark_client_ready(clp, NFS_CS_READY);
726 return 0;
727
728error:
729 nfs_mark_client_ready(clp, error);
730 dprintk("<-- nfs_init_client() = xerror %d\n", error);
731 return error;
732}
733
734/*
735 * Create a version 2 or 3 client
736 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400737static int nfs_init_server(struct nfs_server *server,
738 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400739{
Trond Myklebust3a498022007-12-14 14:56:04 -0500740 struct nfs_client_initdata cl_init = {
741 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500742 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500743 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500744 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500745 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500746 };
Trond Myklebust33170232007-12-20 16:03:59 -0500747 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400748 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500749 int error;
David Howells54ceac42006-08-22 20:06:13 -0400750
751 dprintk("--> nfs_init_server()\n");
752
753#ifdef CONFIG_NFS_V3
754 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500755 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400756#endif
757
758 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500759 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400760 if (IS_ERR(clp)) {
761 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
762 return PTR_ERR(clp);
763 }
764
Trond Myklebust33170232007-12-20 16:03:59 -0500765 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
766 data->timeo, data->retrans);
767 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400768 if (error < 0)
769 goto error;
770
771 server->nfs_client = clp;
772
773 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400774 server->flags = data->flags;
David Howells54ceac42006-08-22 20:06:13 -0400775
776 if (data->rsize)
777 server->rsize = nfs_block_size(data->rsize, NULL);
778 if (data->wsize)
779 server->wsize = nfs_block_size(data->wsize, NULL);
780
781 server->acregmin = data->acregmin * HZ;
782 server->acregmax = data->acregmax * HZ;
783 server->acdirmin = data->acdirmin * HZ;
784 server->acdirmax = data->acdirmax * HZ;
785
786 /* Start lockd here, before we might error out */
787 error = nfs_start_lockd(server);
788 if (error < 0)
789 goto error;
790
Chuck Leverf22d6d72008-03-14 14:10:22 -0400791 server->port = data->nfs_server.port;
792
Trond Myklebust33170232007-12-20 16:03:59 -0500793 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400794 if (error < 0)
795 goto error;
796
Chuck Lever3f8400d2008-03-14 14:10:30 -0400797 /* Preserve the values of mount_server-related mount options */
798 if (data->mount_server.addrlen) {
799 memcpy(&server->mountd_address, &data->mount_server.address,
800 data->mount_server.addrlen);
801 server->mountd_addrlen = data->mount_server.addrlen;
802 }
803 server->mountd_version = data->mount_server.version;
804 server->mountd_port = data->mount_server.port;
805 server->mountd_protocol = data->mount_server.protocol;
806
David Howells54ceac42006-08-22 20:06:13 -0400807 server->namelen = data->namlen;
808 /* Create a client RPC handle for the NFSv3 ACL management interface */
809 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400810 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
811 return 0;
812
813error:
814 server->nfs_client = NULL;
815 nfs_put_client(clp);
816 dprintk("<-- nfs_init_server() = xerror %d\n", error);
817 return error;
818}
819
820/*
821 * Load up the server record from information gained in an fsinfo record
822 */
823static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
824{
825 unsigned long max_rpc_payload;
826
827 /* Work out a lot of parameters */
828 if (server->rsize == 0)
829 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
830 if (server->wsize == 0)
831 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
832
833 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
834 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
835 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
836 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
837
838 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
839 if (server->rsize > max_rpc_payload)
840 server->rsize = max_rpc_payload;
841 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
842 server->rsize = NFS_MAX_FILE_IO_SIZE;
843 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700844
David Howells54ceac42006-08-22 20:06:13 -0400845 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
846
847 if (server->wsize > max_rpc_payload)
848 server->wsize = max_rpc_payload;
849 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
850 server->wsize = NFS_MAX_FILE_IO_SIZE;
851 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
852 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
853
854 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
855 if (server->dtsize > PAGE_CACHE_SIZE)
856 server->dtsize = PAGE_CACHE_SIZE;
857 if (server->dtsize > server->rsize)
858 server->dtsize = server->rsize;
859
860 if (server->flags & NFS_MOUNT_NOAC) {
861 server->acregmin = server->acregmax = 0;
862 server->acdirmin = server->acdirmax = 0;
863 }
864
865 server->maxfilesize = fsinfo->maxfilesize;
866
867 /* We're airborne Set socket buffersize */
868 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
869}
870
871/*
872 * Probe filesystem information, including the FSID on v2/v3
873 */
874static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
875{
876 struct nfs_fsinfo fsinfo;
877 struct nfs_client *clp = server->nfs_client;
878 int error;
879
880 dprintk("--> nfs_probe_fsinfo()\n");
881
882 if (clp->rpc_ops->set_capabilities != NULL) {
883 error = clp->rpc_ops->set_capabilities(server, mntfh);
884 if (error < 0)
885 goto out_error;
886 }
887
888 fsinfo.fattr = fattr;
889 nfs_fattr_init(fattr);
890 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
891 if (error < 0)
892 goto out_error;
893
894 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700895 error = bdi_init(&server->backing_dev_info);
896 if (error)
897 goto out_error;
898
David Howells54ceac42006-08-22 20:06:13 -0400899
900 /* Get some general file system info */
901 if (server->namelen == 0) {
902 struct nfs_pathconf pathinfo;
903
904 pathinfo.fattr = fattr;
905 nfs_fattr_init(fattr);
906
907 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
908 server->namelen = pathinfo.max_namelen;
909 }
910
911 dprintk("<-- nfs_probe_fsinfo() = 0\n");
912 return 0;
913
914out_error:
915 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
916 return error;
917}
918
919/*
920 * Copy useful information when duplicating a server record
921 */
922static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
923{
924 target->flags = source->flags;
925 target->acregmin = source->acregmin;
926 target->acregmax = source->acregmax;
927 target->acdirmin = source->acdirmin;
928 target->acdirmax = source->acdirmax;
929 target->caps = source->caps;
930}
931
932/*
933 * Allocate and initialise a server record
934 */
935static struct nfs_server *nfs_alloc_server(void)
936{
937 struct nfs_server *server;
938
939 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
940 if (!server)
941 return NULL;
942
943 server->client = server->client_acl = ERR_PTR(-EINVAL);
944
945 /* Zero out the NFS state stuff */
946 INIT_LIST_HEAD(&server->client_link);
947 INIT_LIST_HEAD(&server->master_link);
948
Steve Dicksonef818a22007-11-08 04:05:04 -0500949 atomic_set(&server->active, 0);
950
David Howells54ceac42006-08-22 20:06:13 -0400951 server->io_stats = nfs_alloc_iostats();
952 if (!server->io_stats) {
953 kfree(server);
954 return NULL;
955 }
956
957 return server;
958}
959
960/*
961 * Free up a server record
962 */
963void nfs_free_server(struct nfs_server *server)
964{
965 dprintk("--> nfs_free_server()\n");
966
967 spin_lock(&nfs_client_lock);
968 list_del(&server->client_link);
969 list_del(&server->master_link);
970 spin_unlock(&nfs_client_lock);
971
972 if (server->destroy != NULL)
973 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500974
975 if (!IS_ERR(server->client_acl))
976 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400977 if (!IS_ERR(server->client))
978 rpc_shutdown_client(server->client);
979
980 nfs_put_client(server->nfs_client);
981
982 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700983 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400984 kfree(server);
985 nfs_release_automount_timer();
986 dprintk("<-- nfs_free_server()\n");
987}
988
989/*
990 * Create a version 2 or 3 volume record
991 * - keyed on server and FSID
992 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400993struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400994 struct nfs_fh *mntfh)
995{
996 struct nfs_server *server;
997 struct nfs_fattr fattr;
998 int error;
999
1000 server = nfs_alloc_server();
1001 if (!server)
1002 return ERR_PTR(-ENOMEM);
1003
1004 /* Get a client representation */
1005 error = nfs_init_server(server, data);
1006 if (error < 0)
1007 goto error;
1008
1009 BUG_ON(!server->nfs_client);
1010 BUG_ON(!server->nfs_client->rpc_ops);
1011 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1012
1013 /* Probe the root fh to retrieve its FSID */
1014 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1015 if (error < 0)
1016 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001017 if (server->nfs_client->rpc_ops->version == 3) {
1018 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1019 server->namelen = NFS3_MAXNAMLEN;
1020 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1021 server->caps |= NFS_CAP_READDIRPLUS;
1022 } else {
1023 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1024 server->namelen = NFS2_MAXNAMLEN;
1025 }
1026
David Howells54ceac42006-08-22 20:06:13 -04001027 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1028 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1029 if (error < 0) {
1030 dprintk("nfs_create_server: getattr error = %d\n", -error);
1031 goto error;
1032 }
1033 }
1034 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1035
David Howells6daabf12006-08-24 15:44:16 -04001036 dprintk("Server FSID: %llx:%llx\n",
1037 (unsigned long long) server->fsid.major,
1038 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001039
1040 BUG_ON(!server->nfs_client);
1041 BUG_ON(!server->nfs_client->rpc_ops);
1042 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1043
1044 spin_lock(&nfs_client_lock);
1045 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1046 list_add_tail(&server->master_link, &nfs_volume_list);
1047 spin_unlock(&nfs_client_lock);
1048
1049 server->mount_time = jiffies;
1050 return server;
1051
1052error:
1053 nfs_free_server(server);
1054 return ERR_PTR(error);
1055}
1056
1057#ifdef CONFIG_NFS_V4
1058/*
1059 * Initialise an NFS4 client record
1060 */
1061static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001062 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001063 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001064 rpc_authflavor_t authflavour,
1065 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001066{
1067 int error;
1068
1069 if (clp->cl_cons_state == NFS_CS_READY) {
1070 /* the client is initialised already */
1071 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1072 return 0;
1073 }
1074
1075 /* Check NFS protocol revision and initialize RPC op vector */
1076 clp->rpc_ops = &nfs_v4_clientops;
1077
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001078 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001079 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001080 if (error < 0)
1081 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001082 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001083
1084 error = nfs_idmap_new(clp);
1085 if (error < 0) {
1086 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001087 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001088 goto error;
1089 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001090 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001091
1092 nfs_mark_client_ready(clp, NFS_CS_READY);
1093 return 0;
1094
1095error:
1096 nfs_mark_client_ready(clp, error);
1097 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1098 return error;
1099}
1100
1101/*
1102 * Set up an NFS4 client
1103 */
1104static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001105 const char *hostname,
1106 const struct sockaddr *addr,
1107 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001108 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001109 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -05001110 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -04001111{
Trond Myklebust3a498022007-12-14 14:56:04 -05001112 struct nfs_client_initdata cl_init = {
1113 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001114 .addr = addr,
1115 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001116 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001117 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -05001118 };
David Howells54ceac42006-08-22 20:06:13 -04001119 struct nfs_client *clp;
1120 int error;
1121
1122 dprintk("--> nfs4_set_client()\n");
1123
1124 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001125 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001126 if (IS_ERR(clp)) {
1127 error = PTR_ERR(clp);
1128 goto error;
1129 }
Chuck Leverd7403512008-12-23 15:21:37 -05001130 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1131 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001132 if (error < 0)
1133 goto error_put;
1134
1135 server->nfs_client = clp;
1136 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1137 return 0;
1138
1139error_put:
1140 nfs_put_client(clp);
1141error:
1142 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1143 return error;
1144}
1145
1146/*
1147 * Create a version 4 volume record
1148 */
1149static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001150 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001151{
Trond Myklebust33170232007-12-20 16:03:59 -05001152 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001153 int error;
1154
1155 dprintk("--> nfs4_init_server()\n");
1156
Trond Myklebust33170232007-12-20 16:03:59 -05001157 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1158 data->timeo, data->retrans);
1159
Chuck Lever542fcc32008-12-23 15:21:36 -05001160 /* Initialise the client representation from the mount data */
1161 server->flags = data->flags;
1162 server->caps |= NFS_CAP_ATOMIC_OPEN;
1163
Trond Myklebust33170232007-12-20 16:03:59 -05001164 /* Get a client record */
1165 error = nfs4_set_client(server,
1166 data->nfs_server.hostname,
1167 (const struct sockaddr *)&data->nfs_server.address,
1168 data->nfs_server.addrlen,
1169 data->client_address,
1170 data->auth_flavors[0],
1171 data->nfs_server.protocol,
1172 &timeparms);
1173 if (error < 0)
1174 goto error;
1175
David Howells54ceac42006-08-22 20:06:13 -04001176 if (data->rsize)
1177 server->rsize = nfs_block_size(data->rsize, NULL);
1178 if (data->wsize)
1179 server->wsize = nfs_block_size(data->wsize, NULL);
1180
1181 server->acregmin = data->acregmin * HZ;
1182 server->acregmax = data->acregmax * HZ;
1183 server->acdirmin = data->acdirmin * HZ;
1184 server->acdirmax = data->acdirmax * HZ;
1185
Chuck Leverf22d6d72008-03-14 14:10:22 -04001186 server->port = data->nfs_server.port;
1187
Trond Myklebust33170232007-12-20 16:03:59 -05001188 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001189
Trond Myklebust33170232007-12-20 16:03:59 -05001190error:
David Howells54ceac42006-08-22 20:06:13 -04001191 /* Done */
1192 dprintk("<-- nfs4_init_server() = %d\n", error);
1193 return error;
1194}
1195
1196/*
1197 * Create a version 4 volume record
1198 * - keyed on server and FSID
1199 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001200struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001201 struct nfs_fh *mntfh)
1202{
1203 struct nfs_fattr fattr;
1204 struct nfs_server *server;
1205 int error;
1206
1207 dprintk("--> nfs4_create_server()\n");
1208
1209 server = nfs_alloc_server();
1210 if (!server)
1211 return ERR_PTR(-ENOMEM);
1212
David Howells54ceac42006-08-22 20:06:13 -04001213 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001214 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001215 if (error < 0)
1216 goto error;
1217
1218 BUG_ON(!server->nfs_client);
1219 BUG_ON(!server->nfs_client->rpc_ops);
1220 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1221
1222 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001223 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001224 if (error < 0)
1225 goto error;
1226
David Howells6daabf12006-08-24 15:44:16 -04001227 dprintk("Server FSID: %llx:%llx\n",
1228 (unsigned long long) server->fsid.major,
1229 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001230 dprintk("Mount FH: %d\n", mntfh->size);
1231
1232 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1233 if (error < 0)
1234 goto error;
1235
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001236 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1237 server->namelen = NFS4_MAXNAMLEN;
1238
David Howells54ceac42006-08-22 20:06:13 -04001239 BUG_ON(!server->nfs_client);
1240 BUG_ON(!server->nfs_client->rpc_ops);
1241 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1242
1243 spin_lock(&nfs_client_lock);
1244 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1245 list_add_tail(&server->master_link, &nfs_volume_list);
1246 spin_unlock(&nfs_client_lock);
1247
1248 server->mount_time = jiffies;
1249 dprintk("<-- nfs4_create_server() = %p\n", server);
1250 return server;
1251
1252error:
1253 nfs_free_server(server);
1254 dprintk("<-- nfs4_create_server() = error %d\n", error);
1255 return ERR_PTR(error);
1256}
1257
1258/*
1259 * Create an NFS4 referral server record
1260 */
1261struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001262 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001263{
1264 struct nfs_client *parent_client;
1265 struct nfs_server *server, *parent_server;
1266 struct nfs_fattr fattr;
1267 int error;
1268
1269 dprintk("--> nfs4_create_referral_server()\n");
1270
1271 server = nfs_alloc_server();
1272 if (!server)
1273 return ERR_PTR(-ENOMEM);
1274
1275 parent_server = NFS_SB(data->sb);
1276 parent_client = parent_server->nfs_client;
1277
Chuck Lever542fcc32008-12-23 15:21:36 -05001278 /* Initialise the client representation from the parent server */
1279 nfs_server_copy_userdata(server, parent_server);
1280 server->caps |= NFS_CAP_ATOMIC_OPEN;
1281
David Howells54ceac42006-08-22 20:06:13 -04001282 /* Get a client representation.
1283 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001284 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001285 data->addr,
1286 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001287 parent_client->cl_ipaddr,
1288 data->authflavor,
1289 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001290 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001291 if (error < 0)
1292 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001293
Trond Myklebust33170232007-12-20 16:03:59 -05001294 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001295 if (error < 0)
1296 goto error;
1297
1298 BUG_ON(!server->nfs_client);
1299 BUG_ON(!server->nfs_client->rpc_ops);
1300 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1301
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001302 /* Probe the root fh to retrieve its FSID and filehandle */
1303 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1304 if (error < 0)
1305 goto error;
1306
David Howells54ceac42006-08-22 20:06:13 -04001307 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001308 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001309 if (error < 0)
1310 goto error;
1311
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001312 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1313 server->namelen = NFS4_MAXNAMLEN;
1314
David Howells54ceac42006-08-22 20:06:13 -04001315 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001316 (unsigned long long) server->fsid.major,
1317 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001318
1319 spin_lock(&nfs_client_lock);
1320 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1321 list_add_tail(&server->master_link, &nfs_volume_list);
1322 spin_unlock(&nfs_client_lock);
1323
1324 server->mount_time = jiffies;
1325
1326 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1327 return server;
1328
1329error:
1330 nfs_free_server(server);
1331 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1332 return ERR_PTR(error);
1333}
1334
1335#endif /* CONFIG_NFS_V4 */
1336
1337/*
1338 * Clone an NFS2, NFS3 or NFS4 server record
1339 */
1340struct nfs_server *nfs_clone_server(struct nfs_server *source,
1341 struct nfs_fh *fh,
1342 struct nfs_fattr *fattr)
1343{
1344 struct nfs_server *server;
1345 struct nfs_fattr fattr_fsinfo;
1346 int error;
1347
1348 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001349 (unsigned long long) fattr->fsid.major,
1350 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001351
1352 server = nfs_alloc_server();
1353 if (!server)
1354 return ERR_PTR(-ENOMEM);
1355
1356 /* Copy data from the source */
1357 server->nfs_client = source->nfs_client;
1358 atomic_inc(&server->nfs_client->cl_count);
1359 nfs_server_copy_userdata(server, source);
1360
1361 server->fsid = fattr->fsid;
1362
Trond Myklebust33170232007-12-20 16:03:59 -05001363 error = nfs_init_server_rpcclient(server,
1364 source->client->cl_timeout,
1365 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001366 if (error < 0)
1367 goto out_free_server;
1368 if (!IS_ERR(source->client_acl))
1369 nfs_init_server_aclclient(server);
1370
1371 /* probe the filesystem info for this server filesystem */
1372 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1373 if (error < 0)
1374 goto out_free_server;
1375
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001376 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1377 server->namelen = NFS4_MAXNAMLEN;
1378
David Howells54ceac42006-08-22 20:06:13 -04001379 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001380 (unsigned long long) server->fsid.major,
1381 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001382
1383 error = nfs_start_lockd(server);
1384 if (error < 0)
1385 goto out_free_server;
1386
1387 spin_lock(&nfs_client_lock);
1388 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1389 list_add_tail(&server->master_link, &nfs_volume_list);
1390 spin_unlock(&nfs_client_lock);
1391
1392 server->mount_time = jiffies;
1393
1394 dprintk("<-- nfs_clone_server() = %p\n", server);
1395 return server;
1396
1397out_free_server:
1398 nfs_free_server(server);
1399 dprintk("<-- nfs_clone_server() = error %d\n", error);
1400 return ERR_PTR(error);
1401}
David Howells6aaca562006-08-22 20:06:13 -04001402
1403#ifdef CONFIG_PROC_FS
1404static struct proc_dir_entry *proc_fs_nfs;
1405
1406static int nfs_server_list_open(struct inode *inode, struct file *file);
1407static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1408static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1409static void nfs_server_list_stop(struct seq_file *p, void *v);
1410static int nfs_server_list_show(struct seq_file *m, void *v);
1411
1412static struct seq_operations nfs_server_list_ops = {
1413 .start = nfs_server_list_start,
1414 .next = nfs_server_list_next,
1415 .stop = nfs_server_list_stop,
1416 .show = nfs_server_list_show,
1417};
1418
Arjan van de Ven00977a52007-02-12 00:55:34 -08001419static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001420 .open = nfs_server_list_open,
1421 .read = seq_read,
1422 .llseek = seq_lseek,
1423 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001424 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001425};
1426
1427static int nfs_volume_list_open(struct inode *inode, struct file *file);
1428static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1429static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1430static void nfs_volume_list_stop(struct seq_file *p, void *v);
1431static int nfs_volume_list_show(struct seq_file *m, void *v);
1432
1433static struct seq_operations nfs_volume_list_ops = {
1434 .start = nfs_volume_list_start,
1435 .next = nfs_volume_list_next,
1436 .stop = nfs_volume_list_stop,
1437 .show = nfs_volume_list_show,
1438};
1439
Arjan van de Ven00977a52007-02-12 00:55:34 -08001440static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001441 .open = nfs_volume_list_open,
1442 .read = seq_read,
1443 .llseek = seq_lseek,
1444 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001445 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001446};
1447
1448/*
1449 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1450 * we're dealing
1451 */
1452static int nfs_server_list_open(struct inode *inode, struct file *file)
1453{
1454 struct seq_file *m;
1455 int ret;
1456
1457 ret = seq_open(file, &nfs_server_list_ops);
1458 if (ret < 0)
1459 return ret;
1460
1461 m = file->private_data;
1462 m->private = PDE(inode)->data;
1463
1464 return 0;
1465}
1466
1467/*
1468 * set up the iterator to start reading from the server list and return the first item
1469 */
1470static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1471{
David Howells6aaca562006-08-22 20:06:13 -04001472 /* lock the list against modification */
1473 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001474 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001475}
1476
1477/*
1478 * move to next server
1479 */
1480static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1481{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001482 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001483}
1484
1485/*
1486 * clean up after reading from the transports list
1487 */
1488static void nfs_server_list_stop(struct seq_file *p, void *v)
1489{
1490 spin_unlock(&nfs_client_lock);
1491}
1492
1493/*
1494 * display a header line followed by a load of call lines
1495 */
1496static int nfs_server_list_show(struct seq_file *m, void *v)
1497{
1498 struct nfs_client *clp;
1499
1500 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001501 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001502 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1503 return 0;
1504 }
1505
1506 /* display one transport per line on subsequent lines */
1507 clp = list_entry(v, struct nfs_client, cl_share_link);
1508
Chuck Lever5d8515c2007-12-10 14:57:16 -05001509 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001510 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001511 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1512 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001513 atomic_read(&clp->cl_count),
1514 clp->cl_hostname);
1515
1516 return 0;
1517}
1518
1519/*
1520 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1521 */
1522static int nfs_volume_list_open(struct inode *inode, struct file *file)
1523{
1524 struct seq_file *m;
1525 int ret;
1526
1527 ret = seq_open(file, &nfs_volume_list_ops);
1528 if (ret < 0)
1529 return ret;
1530
1531 m = file->private_data;
1532 m->private = PDE(inode)->data;
1533
1534 return 0;
1535}
1536
1537/*
1538 * set up the iterator to start reading from the volume list and return the first item
1539 */
1540static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1541{
David Howells6aaca562006-08-22 20:06:13 -04001542 /* lock the list against modification */
1543 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001544 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001545}
1546
1547/*
1548 * move to next volume
1549 */
1550static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1551{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001552 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001553}
1554
1555/*
1556 * clean up after reading from the transports list
1557 */
1558static void nfs_volume_list_stop(struct seq_file *p, void *v)
1559{
1560 spin_unlock(&nfs_client_lock);
1561}
1562
1563/*
1564 * display a header line followed by a load of call lines
1565 */
1566static int nfs_volume_list_show(struct seq_file *m, void *v)
1567{
1568 struct nfs_server *server;
1569 struct nfs_client *clp;
1570 char dev[8], fsid[17];
1571
1572 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001573 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001574 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1575 return 0;
1576 }
1577 /* display one transport per line on subsequent lines */
1578 server = list_entry(v, struct nfs_server, master_link);
1579 clp = server->nfs_client;
1580
1581 snprintf(dev, 8, "%u:%u",
1582 MAJOR(server->s_dev), MINOR(server->s_dev));
1583
1584 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001585 (unsigned long long) server->fsid.major,
1586 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001587
Chuck Lever5d8515c2007-12-10 14:57:16 -05001588 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001589 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001590 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1591 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001592 dev,
1593 fsid);
1594
1595 return 0;
1596}
1597
1598/*
1599 * initialise the /proc/fs/nfsfs/ directory
1600 */
1601int __init nfs_fs_proc_init(void)
1602{
1603 struct proc_dir_entry *p;
1604
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001605 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001606 if (!proc_fs_nfs)
1607 goto error_0;
1608
1609 proc_fs_nfs->owner = THIS_MODULE;
1610
1611 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001612 p = proc_create("servers", S_IFREG|S_IRUGO,
1613 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001614 if (!p)
1615 goto error_1;
1616
David Howells6aaca562006-08-22 20:06:13 -04001617 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001618 p = proc_create("volumes", S_IFREG|S_IRUGO,
1619 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001620 if (!p)
1621 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001622 return 0;
1623
1624error_2:
1625 remove_proc_entry("servers", proc_fs_nfs);
1626error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001627 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001628error_0:
1629 return -ENOMEM;
1630}
1631
1632/*
1633 * clean up the /proc/fs/nfsfs/ directory
1634 */
1635void nfs_fs_proc_exit(void)
1636{
1637 remove_proc_entry("volumes", proc_fs_nfs);
1638 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001639 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001640}
1641
1642#endif /* CONFIG_PROC_FS */