blob: 06654b831d194b7d60f000559a2f5e150a9c1654 [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}
258#else
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500259static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
260 const struct sockaddr_in *sa2)
261{
262 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
263}
264
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500265static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
266 const struct sockaddr *sa2)
267{
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500268 if (unlikely(sa1->sa_family != AF_INET || sa2->sa_family != AF_INET))
269 return 0;
270 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
271 (const struct sockaddr_in *)sa2);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500272}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500273#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500274
David Howells24c8dbb2006-08-22 20:06:10 -0400275/*
Ian Dalld7371c42009-03-10 20:33:22 -0400276 * Test if two ip4 socket addresses refer to the same socket, by
277 * comparing relevant fields. The padding bytes specifically, are
278 * not compared.
279 *
280 * The caller should ensure both socket addresses are AF_INET.
281 */
282static int nfs_sockaddr_cmp_ip4(const struct sockaddr_in * saddr1,
283 const struct sockaddr_in * saddr2)
284{
285 if (saddr1->sin_addr.s_addr != saddr2->sin_addr.s_addr)
286 return 0;
287 return saddr1->sin_port == saddr2->sin_port;
288}
289
290/*
291 * Test if two ip6 socket addresses refer to the same socket by
292 * comparing relevant fields. The padding bytes specifically, are not
293 * compared. sin6_flowinfo is not compared because it only affects QoS
294 * and sin6_scope_id is only compared if the address is "link local"
295 * because "link local" addresses need only be unique to a specific
296 * link. Conversely, ordinary unicast addresses might have different
297 * sin6_scope_id.
298 *
299 * The caller should ensure both socket addresses are AF_INET6.
300 */
301static int nfs_sockaddr_cmp_ip6 (const struct sockaddr_in6 * saddr1,
302 const struct sockaddr_in6 * saddr2)
303{
304 if (!ipv6_addr_equal(&saddr1->sin6_addr,
305 &saddr1->sin6_addr))
306 return 0;
307 if (ipv6_addr_scope(&saddr1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
308 saddr1->sin6_scope_id != saddr2->sin6_scope_id)
309 return 0;
310 return saddr1->sin6_port == saddr2->sin6_port;
311}
312
313/*
314 * Test if two socket addresses represent the same actual socket,
315 * by comparing (only) relevant fields.
316 */
317static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
319{
320 if (sa1->sa_family != sa2->sa_family)
321 return 0;
322
323 switch (sa1->sa_family) {
324 case AF_INET:
325 return nfs_sockaddr_cmp_ip4((const struct sockaddr_in *) sa1,
326 (const struct sockaddr_in *) sa2);
327 case AF_INET6:
328 return nfs_sockaddr_cmp_ip6((const struct sockaddr_in6 *) sa1,
329 (const struct sockaddr_in6 *) sa2);
330 }
331 return 0;
332}
333
334/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500335 * Find a client by IP address and protocol version
336 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400337 */
Chuck Leverff052642007-12-10 14:58:44 -0500338struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500339{
340 struct nfs_client *clp;
341
342 spin_lock(&nfs_client_lock);
343 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500344 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
345
Trond Myklebustc81468a2007-12-14 14:56:05 -0500346 /* Don't match clients that failed to initialise properly */
347 if (clp->cl_cons_state != NFS_CS_READY)
348 continue;
349
350 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500351 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500352 continue;
353
354 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500355 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500356 continue;
357
358 atomic_inc(&clp->cl_count);
359 spin_unlock(&nfs_client_lock);
360 return clp;
361 }
362 spin_unlock(&nfs_client_lock);
363 return NULL;
364}
365
366/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500367 * Find a client by IP address and protocol version
368 * - returns NULL if no such client
369 */
370struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
371{
372 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
373 u32 nfsvers = clp->rpc_ops->version;
374
375 spin_lock(&nfs_client_lock);
376 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
377 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
378
379 /* Don't match clients that failed to initialise properly */
380 if (clp->cl_cons_state != NFS_CS_READY)
381 continue;
382
383 /* Different NFS versions cannot share the same nfs_client */
384 if (clp->rpc_ops->version != nfsvers)
385 continue;
386
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500387 /* Match only the IP address, not the port number */
388 if (!nfs_sockaddr_match_ipaddr(sap, clap))
389 continue;
390
391 atomic_inc(&clp->cl_count);
392 spin_unlock(&nfs_client_lock);
393 return clp;
394 }
395 spin_unlock(&nfs_client_lock);
396 return NULL;
397}
398
399/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500400 * Find an nfs_client on the list that matches the initialisation data
401 * that is supplied.
402 */
403static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400404{
405 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400406 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400407
408 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400409 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700410 /* Don't match clients that failed to initialise properly */
411 if (clp->cl_cons_state < 0)
412 continue;
413
David Howells24c8dbb2006-08-22 20:06:10 -0400414 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500415 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400416 continue;
417
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500418 if (clp->cl_proto != data->proto)
419 continue;
420
Trond Myklebustc81468a2007-12-14 14:56:05 -0500421 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400422 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400423 continue;
424
Trond Myklebustc81468a2007-12-14 14:56:05 -0500425 atomic_inc(&clp->cl_count);
426 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400427 }
David Howells24c8dbb2006-08-22 20:06:10 -0400428 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400429}
430
431/*
432 * Look up a client by IP address and protocol version
433 * - creates a new record if one doesn't yet exist
434 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500435static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400436{
437 struct nfs_client *clp, *new = NULL;
438 int error;
439
Chuck Leverd7422c42007-12-10 14:58:51 -0500440 dprintk("--> nfs_get_client(%s,v%u)\n",
441 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400442
443 /* see if the client already exists */
444 do {
445 spin_lock(&nfs_client_lock);
446
Trond Myklebustc81468a2007-12-14 14:56:05 -0500447 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400448 if (clp)
449 goto found_client;
450 if (new)
451 goto install_client;
452
453 spin_unlock(&nfs_client_lock);
454
Trond Myklebust3a498022007-12-14 14:56:04 -0500455 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400456 } while (new);
457
458 return ERR_PTR(-ENOMEM);
459
460 /* install a new client and return with it unready */
461install_client:
462 clp = new;
463 list_add(&clp->cl_share_link, &nfs_client_list);
464 spin_unlock(&nfs_client_lock);
465 dprintk("--> nfs_get_client() = %p [new]\n", clp);
466 return clp;
467
468 /* found an existing client
469 * - make sure it's ready before returning
470 */
471found_client:
472 spin_unlock(&nfs_client_lock);
473
474 if (new)
475 nfs_free_client(new);
476
Matthew Wilcox150030b2007-12-06 16:24:39 -0500477 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400478 clp->cl_cons_state != NFS_CS_INITING);
479 if (error < 0) {
480 nfs_put_client(clp);
481 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400482 }
483
484 if (clp->cl_cons_state < NFS_CS_READY) {
485 error = clp->cl_cons_state;
486 nfs_put_client(clp);
487 return ERR_PTR(error);
488 }
489
David Howells54ceac42006-08-22 20:06:13 -0400490 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
491
David Howells24c8dbb2006-08-22 20:06:10 -0400492 dprintk("--> nfs_get_client() = %p [share]\n", clp);
493 return clp;
494}
495
496/*
497 * Mark a server as ready or failed
498 */
David Howells54ceac42006-08-22 20:06:13 -0400499static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400500{
501 clp->cl_cons_state = state;
502 wake_up_all(&nfs_client_active_wq);
503}
David Howells5006a762006-08-22 20:06:12 -0400504
505/*
506 * Initialise the timeout values for a connection
507 */
508static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
509 unsigned int timeo, unsigned int retrans)
510{
511 to->to_initval = timeo * HZ / 10;
512 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400513
514 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400515 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400516 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400517 if (to->to_retries == 0)
518 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500519 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400520 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400521 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
522 to->to_initval = NFS_MAX_TCP_TIMEOUT;
523 to->to_increment = to->to_initval;
524 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500525 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
526 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
527 if (to->to_maxval < to->to_initval)
528 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400529 to->to_exponential = 0;
530 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400531 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400532 if (to->to_retries == 0)
533 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400534 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400535 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400536 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
537 to->to_initval = NFS_MAX_UDP_TIMEOUT;
538 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
539 to->to_exponential = 1;
540 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400541 default:
542 BUG();
David Howells5006a762006-08-22 20:06:12 -0400543 }
544}
545
546/*
547 * Create an RPC client handle
548 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500549static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500550 const struct rpc_timeout *timeparms,
551 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500552 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400553{
David Howells5006a762006-08-22 20:06:12 -0400554 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400555 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500556 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400557 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500558 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500559 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400560 .servername = clp->cl_hostname,
561 .program = &nfs_program,
562 .version = clp->rpc_ops->version,
563 .authflavor = flavor,
564 };
David Howells5006a762006-08-22 20:06:12 -0400565
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500566 if (discrtry)
567 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
568 if (noresvport)
569 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
570
David Howells5006a762006-08-22 20:06:12 -0400571 if (!IS_ERR(clp->cl_rpcclient))
572 return 0;
573
Chuck Lever41877d22006-08-22 20:06:20 -0400574 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400575 if (IS_ERR(clnt)) {
576 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700577 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400578 return PTR_ERR(clnt);
579 }
580
David Howells5006a762006-08-22 20:06:12 -0400581 clp->cl_rpcclient = clnt;
582 return 0;
583}
David Howells54ceac42006-08-22 20:06:13 -0400584
585/*
586 * Version 2 or 3 client destruction
587 */
588static void nfs_destroy_server(struct nfs_server *server)
589{
David Howells54ceac42006-08-22 20:06:13 -0400590 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500591 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400592}
593
594/*
595 * Version 2 or 3 lockd setup
596 */
597static int nfs_start_lockd(struct nfs_server *server)
598{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500599 struct nlm_host *host;
600 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500601 struct nlmclnt_initdata nlm_init = {
602 .hostname = clp->cl_hostname,
603 .address = (struct sockaddr *)&clp->cl_addr,
604 .addrlen = clp->cl_addrlen,
605 .protocol = server->flags & NFS_MOUNT_TCP ?
606 IPPROTO_TCP : IPPROTO_UDP,
607 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500608 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
609 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500610 };
David Howells54ceac42006-08-22 20:06:13 -0400611
Chuck Lever883bb162008-01-15 16:04:20 -0500612 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500613 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400614 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500615 return 0;
616
Chuck Lever883bb162008-01-15 16:04:20 -0500617 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500618 if (IS_ERR(host))
619 return PTR_ERR(host);
620
621 server->nlm_host = host;
622 server->destroy = nfs_destroy_server;
623 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400624}
625
626/*
627 * Initialise an NFSv3 ACL client connection
628 */
629#ifdef CONFIG_NFS_V3_ACL
630static void nfs_init_server_aclclient(struct nfs_server *server)
631{
Trond Myklebust40c553192007-12-14 14:56:07 -0500632 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400633 goto out_noacl;
634 if (server->flags & NFS_MOUNT_NOACL)
635 goto out_noacl;
636
637 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
638 if (IS_ERR(server->client_acl))
639 goto out_noacl;
640
641 /* No errors! Assume that Sun nfsacls are supported */
642 server->caps |= NFS_CAP_ACLS;
643 return;
644
645out_noacl:
646 server->caps &= ~NFS_CAP_ACLS;
647}
648#else
649static inline void nfs_init_server_aclclient(struct nfs_server *server)
650{
651 server->flags &= ~NFS_MOUNT_NOACL;
652 server->caps &= ~NFS_CAP_ACLS;
653}
654#endif
655
656/*
657 * Create a general RPC client
658 */
Trond Myklebust33170232007-12-20 16:03:59 -0500659static int nfs_init_server_rpcclient(struct nfs_server *server,
660 const struct rpc_timeout *timeo,
661 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400662{
663 struct nfs_client *clp = server->nfs_client;
664
665 server->client = rpc_clone_client(clp->cl_rpcclient);
666 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700667 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400668 return PTR_ERR(server->client);
669 }
670
Trond Myklebust33170232007-12-20 16:03:59 -0500671 memcpy(&server->client->cl_timeout_default,
672 timeo,
673 sizeof(server->client->cl_timeout_default));
674 server->client->cl_timeout = &server->client->cl_timeout_default;
675
David Howells54ceac42006-08-22 20:06:13 -0400676 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
677 struct rpc_auth *auth;
678
679 auth = rpcauth_create(pseudoflavour, server->client);
680 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700681 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400682 return PTR_ERR(auth);
683 }
684 }
685 server->client->cl_softrtry = 0;
686 if (server->flags & NFS_MOUNT_SOFT)
687 server->client->cl_softrtry = 1;
688
David Howells54ceac42006-08-22 20:06:13 -0400689 return 0;
690}
691
692/*
693 * Initialise an NFS2 or NFS3 client
694 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400695static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500696 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400697 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400698{
David Howells54ceac42006-08-22 20:06:13 -0400699 int error;
700
701 if (clp->cl_cons_state == NFS_CS_READY) {
702 /* the client is already initialised */
703 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
704 return 0;
705 }
706
David Howells54ceac42006-08-22 20:06:13 -0400707 /*
708 * Create a client RPC handle for doing FSSTAT with UNIX auth only
709 * - RFC 2623, sec 2.3.2
710 */
Chuck Leverd7403512008-12-23 15:21:37 -0500711 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
712 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400713 if (error < 0)
714 goto error;
715 nfs_mark_client_ready(clp, NFS_CS_READY);
716 return 0;
717
718error:
719 nfs_mark_client_ready(clp, error);
720 dprintk("<-- nfs_init_client() = xerror %d\n", error);
721 return error;
722}
723
724/*
725 * Create a version 2 or 3 client
726 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400727static int nfs_init_server(struct nfs_server *server,
728 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400729{
Trond Myklebust3a498022007-12-14 14:56:04 -0500730 struct nfs_client_initdata cl_init = {
731 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500732 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500733 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500734 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500735 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500736 };
Trond Myklebust33170232007-12-20 16:03:59 -0500737 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400738 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500739 int error;
David Howells54ceac42006-08-22 20:06:13 -0400740
741 dprintk("--> nfs_init_server()\n");
742
743#ifdef CONFIG_NFS_V3
744 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500745 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400746#endif
747
748 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500749 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400750 if (IS_ERR(clp)) {
751 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
752 return PTR_ERR(clp);
753 }
754
Trond Myklebust33170232007-12-20 16:03:59 -0500755 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
756 data->timeo, data->retrans);
757 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400758 if (error < 0)
759 goto error;
760
761 server->nfs_client = clp;
762
763 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400764 server->flags = data->flags;
David Howells54ceac42006-08-22 20:06:13 -0400765
766 if (data->rsize)
767 server->rsize = nfs_block_size(data->rsize, NULL);
768 if (data->wsize)
769 server->wsize = nfs_block_size(data->wsize, NULL);
770
771 server->acregmin = data->acregmin * HZ;
772 server->acregmax = data->acregmax * HZ;
773 server->acdirmin = data->acdirmin * HZ;
774 server->acdirmax = data->acdirmax * HZ;
775
776 /* Start lockd here, before we might error out */
777 error = nfs_start_lockd(server);
778 if (error < 0)
779 goto error;
780
Chuck Leverf22d6d72008-03-14 14:10:22 -0400781 server->port = data->nfs_server.port;
782
Trond Myklebust33170232007-12-20 16:03:59 -0500783 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400784 if (error < 0)
785 goto error;
786
Chuck Lever3f8400d2008-03-14 14:10:30 -0400787 /* Preserve the values of mount_server-related mount options */
788 if (data->mount_server.addrlen) {
789 memcpy(&server->mountd_address, &data->mount_server.address,
790 data->mount_server.addrlen);
791 server->mountd_addrlen = data->mount_server.addrlen;
792 }
793 server->mountd_version = data->mount_server.version;
794 server->mountd_port = data->mount_server.port;
795 server->mountd_protocol = data->mount_server.protocol;
796
David Howells54ceac42006-08-22 20:06:13 -0400797 server->namelen = data->namlen;
798 /* Create a client RPC handle for the NFSv3 ACL management interface */
799 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400800 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
801 return 0;
802
803error:
804 server->nfs_client = NULL;
805 nfs_put_client(clp);
806 dprintk("<-- nfs_init_server() = xerror %d\n", error);
807 return error;
808}
809
810/*
811 * Load up the server record from information gained in an fsinfo record
812 */
813static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
814{
815 unsigned long max_rpc_payload;
816
817 /* Work out a lot of parameters */
818 if (server->rsize == 0)
819 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
820 if (server->wsize == 0)
821 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
822
823 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
824 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
825 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
826 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
827
828 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
829 if (server->rsize > max_rpc_payload)
830 server->rsize = max_rpc_payload;
831 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
832 server->rsize = NFS_MAX_FILE_IO_SIZE;
833 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700834
David Howells54ceac42006-08-22 20:06:13 -0400835 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
836
837 if (server->wsize > max_rpc_payload)
838 server->wsize = max_rpc_payload;
839 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
840 server->wsize = NFS_MAX_FILE_IO_SIZE;
841 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
842 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
843
844 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
845 if (server->dtsize > PAGE_CACHE_SIZE)
846 server->dtsize = PAGE_CACHE_SIZE;
847 if (server->dtsize > server->rsize)
848 server->dtsize = server->rsize;
849
850 if (server->flags & NFS_MOUNT_NOAC) {
851 server->acregmin = server->acregmax = 0;
852 server->acdirmin = server->acdirmax = 0;
853 }
854
855 server->maxfilesize = fsinfo->maxfilesize;
856
857 /* We're airborne Set socket buffersize */
858 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
859}
860
861/*
862 * Probe filesystem information, including the FSID on v2/v3
863 */
864static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
865{
866 struct nfs_fsinfo fsinfo;
867 struct nfs_client *clp = server->nfs_client;
868 int error;
869
870 dprintk("--> nfs_probe_fsinfo()\n");
871
872 if (clp->rpc_ops->set_capabilities != NULL) {
873 error = clp->rpc_ops->set_capabilities(server, mntfh);
874 if (error < 0)
875 goto out_error;
876 }
877
878 fsinfo.fattr = fattr;
879 nfs_fattr_init(fattr);
880 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
881 if (error < 0)
882 goto out_error;
883
884 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700885 error = bdi_init(&server->backing_dev_info);
886 if (error)
887 goto out_error;
888
David Howells54ceac42006-08-22 20:06:13 -0400889
890 /* Get some general file system info */
891 if (server->namelen == 0) {
892 struct nfs_pathconf pathinfo;
893
894 pathinfo.fattr = fattr;
895 nfs_fattr_init(fattr);
896
897 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
898 server->namelen = pathinfo.max_namelen;
899 }
900
901 dprintk("<-- nfs_probe_fsinfo() = 0\n");
902 return 0;
903
904out_error:
905 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
906 return error;
907}
908
909/*
910 * Copy useful information when duplicating a server record
911 */
912static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
913{
914 target->flags = source->flags;
915 target->acregmin = source->acregmin;
916 target->acregmax = source->acregmax;
917 target->acdirmin = source->acdirmin;
918 target->acdirmax = source->acdirmax;
919 target->caps = source->caps;
920}
921
922/*
923 * Allocate and initialise a server record
924 */
925static struct nfs_server *nfs_alloc_server(void)
926{
927 struct nfs_server *server;
928
929 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
930 if (!server)
931 return NULL;
932
933 server->client = server->client_acl = ERR_PTR(-EINVAL);
934
935 /* Zero out the NFS state stuff */
936 INIT_LIST_HEAD(&server->client_link);
937 INIT_LIST_HEAD(&server->master_link);
938
Steve Dicksonef818a22007-11-08 04:05:04 -0500939 atomic_set(&server->active, 0);
940
David Howells54ceac42006-08-22 20:06:13 -0400941 server->io_stats = nfs_alloc_iostats();
942 if (!server->io_stats) {
943 kfree(server);
944 return NULL;
945 }
946
947 return server;
948}
949
950/*
951 * Free up a server record
952 */
953void nfs_free_server(struct nfs_server *server)
954{
955 dprintk("--> nfs_free_server()\n");
956
957 spin_lock(&nfs_client_lock);
958 list_del(&server->client_link);
959 list_del(&server->master_link);
960 spin_unlock(&nfs_client_lock);
961
962 if (server->destroy != NULL)
963 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500964
965 if (!IS_ERR(server->client_acl))
966 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400967 if (!IS_ERR(server->client))
968 rpc_shutdown_client(server->client);
969
970 nfs_put_client(server->nfs_client);
971
972 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700973 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400974 kfree(server);
975 nfs_release_automount_timer();
976 dprintk("<-- nfs_free_server()\n");
977}
978
979/*
980 * Create a version 2 or 3 volume record
981 * - keyed on server and FSID
982 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400983struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400984 struct nfs_fh *mntfh)
985{
986 struct nfs_server *server;
987 struct nfs_fattr fattr;
988 int error;
989
990 server = nfs_alloc_server();
991 if (!server)
992 return ERR_PTR(-ENOMEM);
993
994 /* Get a client representation */
995 error = nfs_init_server(server, data);
996 if (error < 0)
997 goto error;
998
999 BUG_ON(!server->nfs_client);
1000 BUG_ON(!server->nfs_client->rpc_ops);
1001 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1002
1003 /* Probe the root fh to retrieve its FSID */
1004 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1005 if (error < 0)
1006 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001007 if (server->nfs_client->rpc_ops->version == 3) {
1008 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1009 server->namelen = NFS3_MAXNAMLEN;
1010 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1011 server->caps |= NFS_CAP_READDIRPLUS;
1012 } else {
1013 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1014 server->namelen = NFS2_MAXNAMLEN;
1015 }
1016
David Howells54ceac42006-08-22 20:06:13 -04001017 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1018 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1019 if (error < 0) {
1020 dprintk("nfs_create_server: getattr error = %d\n", -error);
1021 goto error;
1022 }
1023 }
1024 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1025
David Howells6daabf12006-08-24 15:44:16 -04001026 dprintk("Server FSID: %llx:%llx\n",
1027 (unsigned long long) server->fsid.major,
1028 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001029
1030 BUG_ON(!server->nfs_client);
1031 BUG_ON(!server->nfs_client->rpc_ops);
1032 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1033
1034 spin_lock(&nfs_client_lock);
1035 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1036 list_add_tail(&server->master_link, &nfs_volume_list);
1037 spin_unlock(&nfs_client_lock);
1038
1039 server->mount_time = jiffies;
1040 return server;
1041
1042error:
1043 nfs_free_server(server);
1044 return ERR_PTR(error);
1045}
1046
1047#ifdef CONFIG_NFS_V4
1048/*
1049 * Initialise an NFS4 client record
1050 */
1051static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001052 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001053 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001054 rpc_authflavor_t authflavour,
1055 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001056{
1057 int error;
1058
1059 if (clp->cl_cons_state == NFS_CS_READY) {
1060 /* the client is initialised already */
1061 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1062 return 0;
1063 }
1064
1065 /* Check NFS protocol revision and initialize RPC op vector */
1066 clp->rpc_ops = &nfs_v4_clientops;
1067
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001068 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001069 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001070 if (error < 0)
1071 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001072 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001073
1074 error = nfs_idmap_new(clp);
1075 if (error < 0) {
1076 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001077 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001078 goto error;
1079 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001080 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001081
1082 nfs_mark_client_ready(clp, NFS_CS_READY);
1083 return 0;
1084
1085error:
1086 nfs_mark_client_ready(clp, error);
1087 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1088 return error;
1089}
1090
1091/*
1092 * Set up an NFS4 client
1093 */
1094static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001095 const char *hostname,
1096 const struct sockaddr *addr,
1097 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001098 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001099 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -05001100 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -04001101{
Trond Myklebust3a498022007-12-14 14:56:04 -05001102 struct nfs_client_initdata cl_init = {
1103 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001104 .addr = addr,
1105 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001106 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001107 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -05001108 };
David Howells54ceac42006-08-22 20:06:13 -04001109 struct nfs_client *clp;
1110 int error;
1111
1112 dprintk("--> nfs4_set_client()\n");
1113
1114 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001115 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001116 if (IS_ERR(clp)) {
1117 error = PTR_ERR(clp);
1118 goto error;
1119 }
Chuck Leverd7403512008-12-23 15:21:37 -05001120 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1121 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001122 if (error < 0)
1123 goto error_put;
1124
1125 server->nfs_client = clp;
1126 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1127 return 0;
1128
1129error_put:
1130 nfs_put_client(clp);
1131error:
1132 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1133 return error;
1134}
1135
1136/*
1137 * Create a version 4 volume record
1138 */
1139static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001140 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001141{
Trond Myklebust33170232007-12-20 16:03:59 -05001142 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001143 int error;
1144
1145 dprintk("--> nfs4_init_server()\n");
1146
Trond Myklebust33170232007-12-20 16:03:59 -05001147 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1148 data->timeo, data->retrans);
1149
Chuck Lever542fcc32008-12-23 15:21:36 -05001150 /* Initialise the client representation from the mount data */
1151 server->flags = data->flags;
1152 server->caps |= NFS_CAP_ATOMIC_OPEN;
1153
Trond Myklebust33170232007-12-20 16:03:59 -05001154 /* Get a client record */
1155 error = nfs4_set_client(server,
1156 data->nfs_server.hostname,
1157 (const struct sockaddr *)&data->nfs_server.address,
1158 data->nfs_server.addrlen,
1159 data->client_address,
1160 data->auth_flavors[0],
1161 data->nfs_server.protocol,
1162 &timeparms);
1163 if (error < 0)
1164 goto error;
1165
David Howells54ceac42006-08-22 20:06:13 -04001166 if (data->rsize)
1167 server->rsize = nfs_block_size(data->rsize, NULL);
1168 if (data->wsize)
1169 server->wsize = nfs_block_size(data->wsize, NULL);
1170
1171 server->acregmin = data->acregmin * HZ;
1172 server->acregmax = data->acregmax * HZ;
1173 server->acdirmin = data->acdirmin * HZ;
1174 server->acdirmax = data->acdirmax * HZ;
1175
Chuck Leverf22d6d72008-03-14 14:10:22 -04001176 server->port = data->nfs_server.port;
1177
Trond Myklebust33170232007-12-20 16:03:59 -05001178 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001179
Trond Myklebust33170232007-12-20 16:03:59 -05001180error:
David Howells54ceac42006-08-22 20:06:13 -04001181 /* Done */
1182 dprintk("<-- nfs4_init_server() = %d\n", error);
1183 return error;
1184}
1185
1186/*
1187 * Create a version 4 volume record
1188 * - keyed on server and FSID
1189 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001190struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001191 struct nfs_fh *mntfh)
1192{
1193 struct nfs_fattr fattr;
1194 struct nfs_server *server;
1195 int error;
1196
1197 dprintk("--> nfs4_create_server()\n");
1198
1199 server = nfs_alloc_server();
1200 if (!server)
1201 return ERR_PTR(-ENOMEM);
1202
David Howells54ceac42006-08-22 20:06:13 -04001203 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001204 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001205 if (error < 0)
1206 goto error;
1207
1208 BUG_ON(!server->nfs_client);
1209 BUG_ON(!server->nfs_client->rpc_ops);
1210 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1211
1212 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001213 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001214 if (error < 0)
1215 goto error;
1216
David Howells6daabf12006-08-24 15:44:16 -04001217 dprintk("Server FSID: %llx:%llx\n",
1218 (unsigned long long) server->fsid.major,
1219 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001220 dprintk("Mount FH: %d\n", mntfh->size);
1221
1222 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1223 if (error < 0)
1224 goto error;
1225
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001226 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1227 server->namelen = NFS4_MAXNAMLEN;
1228
David Howells54ceac42006-08-22 20:06:13 -04001229 BUG_ON(!server->nfs_client);
1230 BUG_ON(!server->nfs_client->rpc_ops);
1231 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
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 dprintk("<-- nfs4_create_server() = %p\n", server);
1240 return server;
1241
1242error:
1243 nfs_free_server(server);
1244 dprintk("<-- nfs4_create_server() = error %d\n", error);
1245 return ERR_PTR(error);
1246}
1247
1248/*
1249 * Create an NFS4 referral server record
1250 */
1251struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001252 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001253{
1254 struct nfs_client *parent_client;
1255 struct nfs_server *server, *parent_server;
1256 struct nfs_fattr fattr;
1257 int error;
1258
1259 dprintk("--> nfs4_create_referral_server()\n");
1260
1261 server = nfs_alloc_server();
1262 if (!server)
1263 return ERR_PTR(-ENOMEM);
1264
1265 parent_server = NFS_SB(data->sb);
1266 parent_client = parent_server->nfs_client;
1267
Chuck Lever542fcc32008-12-23 15:21:36 -05001268 /* Initialise the client representation from the parent server */
1269 nfs_server_copy_userdata(server, parent_server);
1270 server->caps |= NFS_CAP_ATOMIC_OPEN;
1271
David Howells54ceac42006-08-22 20:06:13 -04001272 /* Get a client representation.
1273 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001274 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001275 data->addr,
1276 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001277 parent_client->cl_ipaddr,
1278 data->authflavor,
1279 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001280 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001281 if (error < 0)
1282 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001283
Trond Myklebust33170232007-12-20 16:03:59 -05001284 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001285 if (error < 0)
1286 goto error;
1287
1288 BUG_ON(!server->nfs_client);
1289 BUG_ON(!server->nfs_client->rpc_ops);
1290 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1291
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001292 /* Probe the root fh to retrieve its FSID and filehandle */
1293 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1294 if (error < 0)
1295 goto error;
1296
David Howells54ceac42006-08-22 20:06:13 -04001297 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001298 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001299 if (error < 0)
1300 goto error;
1301
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001302 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1303 server->namelen = NFS4_MAXNAMLEN;
1304
David Howells54ceac42006-08-22 20:06:13 -04001305 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001306 (unsigned long long) server->fsid.major,
1307 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001308
1309 spin_lock(&nfs_client_lock);
1310 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1311 list_add_tail(&server->master_link, &nfs_volume_list);
1312 spin_unlock(&nfs_client_lock);
1313
1314 server->mount_time = jiffies;
1315
1316 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1317 return server;
1318
1319error:
1320 nfs_free_server(server);
1321 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1322 return ERR_PTR(error);
1323}
1324
1325#endif /* CONFIG_NFS_V4 */
1326
1327/*
1328 * Clone an NFS2, NFS3 or NFS4 server record
1329 */
1330struct nfs_server *nfs_clone_server(struct nfs_server *source,
1331 struct nfs_fh *fh,
1332 struct nfs_fattr *fattr)
1333{
1334 struct nfs_server *server;
1335 struct nfs_fattr fattr_fsinfo;
1336 int error;
1337
1338 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001339 (unsigned long long) fattr->fsid.major,
1340 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001341
1342 server = nfs_alloc_server();
1343 if (!server)
1344 return ERR_PTR(-ENOMEM);
1345
1346 /* Copy data from the source */
1347 server->nfs_client = source->nfs_client;
1348 atomic_inc(&server->nfs_client->cl_count);
1349 nfs_server_copy_userdata(server, source);
1350
1351 server->fsid = fattr->fsid;
1352
Trond Myklebust33170232007-12-20 16:03:59 -05001353 error = nfs_init_server_rpcclient(server,
1354 source->client->cl_timeout,
1355 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001356 if (error < 0)
1357 goto out_free_server;
1358 if (!IS_ERR(source->client_acl))
1359 nfs_init_server_aclclient(server);
1360
1361 /* probe the filesystem info for this server filesystem */
1362 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1363 if (error < 0)
1364 goto out_free_server;
1365
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001366 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1367 server->namelen = NFS4_MAXNAMLEN;
1368
David Howells54ceac42006-08-22 20:06:13 -04001369 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001370 (unsigned long long) server->fsid.major,
1371 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001372
1373 error = nfs_start_lockd(server);
1374 if (error < 0)
1375 goto out_free_server;
1376
1377 spin_lock(&nfs_client_lock);
1378 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1379 list_add_tail(&server->master_link, &nfs_volume_list);
1380 spin_unlock(&nfs_client_lock);
1381
1382 server->mount_time = jiffies;
1383
1384 dprintk("<-- nfs_clone_server() = %p\n", server);
1385 return server;
1386
1387out_free_server:
1388 nfs_free_server(server);
1389 dprintk("<-- nfs_clone_server() = error %d\n", error);
1390 return ERR_PTR(error);
1391}
David Howells6aaca562006-08-22 20:06:13 -04001392
1393#ifdef CONFIG_PROC_FS
1394static struct proc_dir_entry *proc_fs_nfs;
1395
1396static int nfs_server_list_open(struct inode *inode, struct file *file);
1397static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1398static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1399static void nfs_server_list_stop(struct seq_file *p, void *v);
1400static int nfs_server_list_show(struct seq_file *m, void *v);
1401
1402static struct seq_operations nfs_server_list_ops = {
1403 .start = nfs_server_list_start,
1404 .next = nfs_server_list_next,
1405 .stop = nfs_server_list_stop,
1406 .show = nfs_server_list_show,
1407};
1408
Arjan van de Ven00977a52007-02-12 00:55:34 -08001409static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001410 .open = nfs_server_list_open,
1411 .read = seq_read,
1412 .llseek = seq_lseek,
1413 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001414 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001415};
1416
1417static int nfs_volume_list_open(struct inode *inode, struct file *file);
1418static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1419static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1420static void nfs_volume_list_stop(struct seq_file *p, void *v);
1421static int nfs_volume_list_show(struct seq_file *m, void *v);
1422
1423static struct seq_operations nfs_volume_list_ops = {
1424 .start = nfs_volume_list_start,
1425 .next = nfs_volume_list_next,
1426 .stop = nfs_volume_list_stop,
1427 .show = nfs_volume_list_show,
1428};
1429
Arjan van de Ven00977a52007-02-12 00:55:34 -08001430static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001431 .open = nfs_volume_list_open,
1432 .read = seq_read,
1433 .llseek = seq_lseek,
1434 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001435 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001436};
1437
1438/*
1439 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1440 * we're dealing
1441 */
1442static int nfs_server_list_open(struct inode *inode, struct file *file)
1443{
1444 struct seq_file *m;
1445 int ret;
1446
1447 ret = seq_open(file, &nfs_server_list_ops);
1448 if (ret < 0)
1449 return ret;
1450
1451 m = file->private_data;
1452 m->private = PDE(inode)->data;
1453
1454 return 0;
1455}
1456
1457/*
1458 * set up the iterator to start reading from the server list and return the first item
1459 */
1460static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1461{
David Howells6aaca562006-08-22 20:06:13 -04001462 /* lock the list against modification */
1463 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001464 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001465}
1466
1467/*
1468 * move to next server
1469 */
1470static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1471{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001472 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001473}
1474
1475/*
1476 * clean up after reading from the transports list
1477 */
1478static void nfs_server_list_stop(struct seq_file *p, void *v)
1479{
1480 spin_unlock(&nfs_client_lock);
1481}
1482
1483/*
1484 * display a header line followed by a load of call lines
1485 */
1486static int nfs_server_list_show(struct seq_file *m, void *v)
1487{
1488 struct nfs_client *clp;
1489
1490 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001491 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001492 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1493 return 0;
1494 }
1495
1496 /* display one transport per line on subsequent lines */
1497 clp = list_entry(v, struct nfs_client, cl_share_link);
1498
Chuck Lever5d8515c2007-12-10 14:57:16 -05001499 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001500 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001501 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1502 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001503 atomic_read(&clp->cl_count),
1504 clp->cl_hostname);
1505
1506 return 0;
1507}
1508
1509/*
1510 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1511 */
1512static int nfs_volume_list_open(struct inode *inode, struct file *file)
1513{
1514 struct seq_file *m;
1515 int ret;
1516
1517 ret = seq_open(file, &nfs_volume_list_ops);
1518 if (ret < 0)
1519 return ret;
1520
1521 m = file->private_data;
1522 m->private = PDE(inode)->data;
1523
1524 return 0;
1525}
1526
1527/*
1528 * set up the iterator to start reading from the volume list and return the first item
1529 */
1530static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1531{
David Howells6aaca562006-08-22 20:06:13 -04001532 /* lock the list against modification */
1533 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001534 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001535}
1536
1537/*
1538 * move to next volume
1539 */
1540static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1541{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001542 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001543}
1544
1545/*
1546 * clean up after reading from the transports list
1547 */
1548static void nfs_volume_list_stop(struct seq_file *p, void *v)
1549{
1550 spin_unlock(&nfs_client_lock);
1551}
1552
1553/*
1554 * display a header line followed by a load of call lines
1555 */
1556static int nfs_volume_list_show(struct seq_file *m, void *v)
1557{
1558 struct nfs_server *server;
1559 struct nfs_client *clp;
1560 char dev[8], fsid[17];
1561
1562 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001563 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001564 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1565 return 0;
1566 }
1567 /* display one transport per line on subsequent lines */
1568 server = list_entry(v, struct nfs_server, master_link);
1569 clp = server->nfs_client;
1570
1571 snprintf(dev, 8, "%u:%u",
1572 MAJOR(server->s_dev), MINOR(server->s_dev));
1573
1574 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001575 (unsigned long long) server->fsid.major,
1576 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001577
Chuck Lever5d8515c2007-12-10 14:57:16 -05001578 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001579 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001580 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1581 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001582 dev,
1583 fsid);
1584
1585 return 0;
1586}
1587
1588/*
1589 * initialise the /proc/fs/nfsfs/ directory
1590 */
1591int __init nfs_fs_proc_init(void)
1592{
1593 struct proc_dir_entry *p;
1594
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001595 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001596 if (!proc_fs_nfs)
1597 goto error_0;
1598
1599 proc_fs_nfs->owner = THIS_MODULE;
1600
1601 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001602 p = proc_create("servers", S_IFREG|S_IRUGO,
1603 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001604 if (!p)
1605 goto error_1;
1606
David Howells6aaca562006-08-22 20:06:13 -04001607 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001608 p = proc_create("volumes", S_IFREG|S_IRUGO,
1609 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001610 if (!p)
1611 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001612 return 0;
1613
1614error_2:
1615 remove_proc_entry("servers", proc_fs_nfs);
1616error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001617 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001618error_0:
1619 return -ENOMEM;
1620}
1621
1622/*
1623 * clean up the /proc/fs/nfsfs/ directory
1624 */
1625void nfs_fs_proc_exit(void)
1626{
1627 remove_proc_entry("volumes", proc_fs_nfs);
1628 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001629 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001630}
1631
1632#endif /* CONFIG_PROC_FS */