blob: 0e9a1f9743b058dfc053f5c7fa859817cdc8bd3b [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"
David Howells14727282009-04-03 16:42:42 +010048#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040049
50#define NFSDBG_FACILITY NFSDBG_CLIENT
51
52static DEFINE_SPINLOCK(nfs_client_lock);
53static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040054static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040055static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56
57/*
David Howells5006a762006-08-22 20:06:12 -040058 * RPC cruft for NFS
59 */
60static struct rpc_version *nfs_version[5] = {
61 [2] = &nfs_version2,
62#ifdef CONFIG_NFS_V3
63 [3] = &nfs_version3,
64#endif
65#ifdef CONFIG_NFS_V4
66 [4] = &nfs_version4,
67#endif
68};
69
70struct rpc_program nfs_program = {
71 .name = "nfs",
72 .number = NFS_PROGRAM,
73 .nrvers = ARRAY_SIZE(nfs_version),
74 .version = nfs_version,
75 .stats = &nfs_rpcstat,
76 .pipe_dir_name = "/nfs",
77};
78
79struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
81};
82
83
84#ifdef CONFIG_NFS_V3_ACL
85static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
86static struct rpc_version * nfsacl_version[] = {
87 [3] = &nfsacl_version3,
88};
89
90struct rpc_program nfsacl_program = {
91 .name = "nfsacl",
92 .number = NFS_ACL_PROGRAM,
93 .nrvers = ARRAY_SIZE(nfsacl_version),
94 .version = nfsacl_version,
95 .stats = &nfsacl_rpcstat,
96};
97#endif /* CONFIG_NFS_V3_ACL */
98
Trond Myklebust3a498022007-12-14 14:56:04 -050099struct nfs_client_initdata {
100 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500101 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500102 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500103 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500104 int proto;
Trond Myklebust3a498022007-12-14 14:56:04 -0500105};
106
David Howells5006a762006-08-22 20:06:12 -0400107/*
David Howells24c8dbb2006-08-22 20:06:10 -0400108 * Allocate a shared client record
109 *
110 * Since these are allocated/deallocated very rarely, we don't
111 * bother putting them in a slab cache...
112 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500113static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400114{
115 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400116 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700117 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400118
119 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
120 goto error_0;
121
Trond Myklebust40c553192007-12-14 14:56:07 -0500122 clp->rpc_ops = cl_init->rpc_ops;
123
124 if (cl_init->rpc_ops->version == 4) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700125 err = nfs_callback_up();
126 if (err < 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400127 goto error_2;
128 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
129 }
130
131 atomic_set(&clp->cl_count, 1);
132 clp->cl_cons_state = NFS_CS_INITING;
133
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500134 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
135 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400136
Trond Myklebust3a498022007-12-14 14:56:04 -0500137 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700138 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500139 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400140 if (!clp->cl_hostname)
141 goto error_3;
142 }
143
144 INIT_LIST_HEAD(&clp->cl_superblocks);
145 clp->cl_rpcclient = ERR_PTR(-EINVAL);
146
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500147 clp->cl_proto = cl_init->proto;
148
David Howells24c8dbb2006-08-22 20:06:10 -0400149#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400150 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400151 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000152 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400153 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
154 clp->cl_boot_time = CURRENT_TIME;
155 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
156#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400157 cred = rpc_lookup_machine_cred();
158 if (!IS_ERR(cred))
159 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400160
David Howells14727282009-04-03 16:42:42 +0100161 nfs_fscache_get_client_cookie(clp);
162
David Howells24c8dbb2006-08-22 20:06:10 -0400163 return clp;
164
165error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400166 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
167 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400168error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400169 kfree(clp);
170error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700171 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400172}
173
Trond Myklebust5dd31772006-08-24 01:03:05 -0400174static void nfs4_shutdown_client(struct nfs_client *clp)
175{
176#ifdef CONFIG_NFS_V4
177 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
178 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400179 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400180 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
181 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500182
183 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400184#endif
185}
186
David Howells24c8dbb2006-08-22 20:06:10 -0400187/*
188 * Destroy a shared client record
189 */
190static void nfs_free_client(struct nfs_client *clp)
191{
Trond Myklebust40c553192007-12-14 14:56:07 -0500192 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400193
Trond Myklebust5dd31772006-08-24 01:03:05 -0400194 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400195
David Howells14727282009-04-03 16:42:42 +0100196 nfs_fscache_release_client_cookie(clp);
197
David Howells24c8dbb2006-08-22 20:06:10 -0400198 /* -EIO all pending I/O */
199 if (!IS_ERR(clp->cl_rpcclient))
200 rpc_shutdown_client(clp->cl_rpcclient);
201
202 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
203 nfs_callback_down();
204
Trond Myklebust7c67db32008-04-07 20:50:11 -0400205 if (clp->cl_machine_cred != NULL)
206 put_rpccred(clp->cl_machine_cred);
207
David Howells24c8dbb2006-08-22 20:06:10 -0400208 kfree(clp->cl_hostname);
209 kfree(clp);
210
211 dprintk("<-- nfs_free_client()\n");
212}
213
214/*
215 * Release a reference to a shared client record
216 */
217void nfs_put_client(struct nfs_client *clp)
218{
David Howells27ba8512006-07-30 14:40:56 -0400219 if (!clp)
220 return;
221
David Howells24c8dbb2006-08-22 20:06:10 -0400222 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
223
224 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
225 list_del(&clp->cl_share_link);
226 spin_unlock(&nfs_client_lock);
227
228 BUG_ON(!list_empty(&clp->cl_superblocks));
229
230 nfs_free_client(clp);
231 }
232}
233
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500234#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400235/*
236 * Test if two ip6 socket addresses refer to the same socket by
237 * comparing relevant fields. The padding bytes specifically, are not
238 * compared. sin6_flowinfo is not compared because it only affects QoS
239 * and sin6_scope_id is only compared if the address is "link local"
240 * because "link local" addresses need only be unique to a specific
241 * link. Conversely, ordinary unicast addresses might have different
242 * sin6_scope_id.
243 *
244 * The caller should ensure both socket addresses are AF_INET6.
245 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400246static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
247 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400248{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400249 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
250 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400251
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400252 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
253 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400254 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500255
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400256 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500257}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400258#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
259static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
260 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400261{
262 return 0;
263}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500264#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500265
David Howells24c8dbb2006-08-22 20:06:10 -0400266/*
Ian Dalld7371c42009-03-10 20:33:22 -0400267 * Test if two ip4 socket addresses refer to the same socket, by
268 * comparing relevant fields. The padding bytes specifically, are
269 * not compared.
270 *
271 * The caller should ensure both socket addresses are AF_INET.
272 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400273static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
274 const struct sockaddr *sa2)
275{
276 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
277 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
278
279 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
280}
281
282static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
283 const struct sockaddr *sa2)
284{
285 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
286 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
287
288 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
289 (sin1->sin6_port == sin2->sin6_port);
290}
291
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400292static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
293 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400294{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400295 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
296 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400297
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400298 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
299 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400300}
301
302/*
Ian Dalld7371c42009-03-10 20:33:22 -0400303 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400304 * by comparing (only) relevant fields, excluding the port number.
305 */
306static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
307 const struct sockaddr *sa2)
308{
309 if (sa1->sa_family != sa2->sa_family)
310 return 0;
311
312 switch (sa1->sa_family) {
313 case AF_INET:
314 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
315 case AF_INET6:
316 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
317 }
318 return 0;
319}
320
321/*
322 * Test if two socket addresses represent the same actual socket,
323 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400324 */
325static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
326 const struct sockaddr *sa2)
327{
328 if (sa1->sa_family != sa2->sa_family)
329 return 0;
330
331 switch (sa1->sa_family) {
332 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400333 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400334 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400335 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400336 }
337 return 0;
338}
339
340/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500341 * Find a client by IP address and protocol version
342 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400343 */
Chuck Leverff052642007-12-10 14:58:44 -0500344struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500345{
346 struct nfs_client *clp;
347
348 spin_lock(&nfs_client_lock);
349 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500350 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
351
Trond Myklebustc81468a2007-12-14 14:56:05 -0500352 /* Don't match clients that failed to initialise properly */
353 if (clp->cl_cons_state != NFS_CS_READY)
354 continue;
355
356 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500357 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500358 continue;
359
360 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500361 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500362 continue;
363
364 atomic_inc(&clp->cl_count);
365 spin_unlock(&nfs_client_lock);
366 return clp;
367 }
368 spin_unlock(&nfs_client_lock);
369 return NULL;
370}
371
372/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500373 * Find a client by IP address and protocol version
374 * - returns NULL if no such client
375 */
376struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
377{
378 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
379 u32 nfsvers = clp->rpc_ops->version;
380
381 spin_lock(&nfs_client_lock);
382 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
383 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
384
385 /* Don't match clients that failed to initialise properly */
386 if (clp->cl_cons_state != NFS_CS_READY)
387 continue;
388
389 /* Different NFS versions cannot share the same nfs_client */
390 if (clp->rpc_ops->version != nfsvers)
391 continue;
392
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500393 /* Match only the IP address, not the port number */
394 if (!nfs_sockaddr_match_ipaddr(sap, clap))
395 continue;
396
397 atomic_inc(&clp->cl_count);
398 spin_unlock(&nfs_client_lock);
399 return clp;
400 }
401 spin_unlock(&nfs_client_lock);
402 return NULL;
403}
404
405/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500406 * Find an nfs_client on the list that matches the initialisation data
407 * that is supplied.
408 */
409static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400410{
411 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400412 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400413
414 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400415 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700416 /* Don't match clients that failed to initialise properly */
417 if (clp->cl_cons_state < 0)
418 continue;
419
David Howells24c8dbb2006-08-22 20:06:10 -0400420 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500421 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400422 continue;
423
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500424 if (clp->cl_proto != data->proto)
425 continue;
426
Trond Myklebustc81468a2007-12-14 14:56:05 -0500427 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400428 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400429 continue;
430
Trond Myklebustc81468a2007-12-14 14:56:05 -0500431 atomic_inc(&clp->cl_count);
432 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400433 }
David Howells24c8dbb2006-08-22 20:06:10 -0400434 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400435}
436
437/*
438 * Look up a client by IP address and protocol version
439 * - creates a new record if one doesn't yet exist
440 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500441static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400442{
443 struct nfs_client *clp, *new = NULL;
444 int error;
445
Chuck Leverd7422c42007-12-10 14:58:51 -0500446 dprintk("--> nfs_get_client(%s,v%u)\n",
447 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400448
449 /* see if the client already exists */
450 do {
451 spin_lock(&nfs_client_lock);
452
Trond Myklebustc81468a2007-12-14 14:56:05 -0500453 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400454 if (clp)
455 goto found_client;
456 if (new)
457 goto install_client;
458
459 spin_unlock(&nfs_client_lock);
460
Trond Myklebust3a498022007-12-14 14:56:04 -0500461 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700462 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400463
Chuck Levera21bdd92009-06-17 18:02:10 -0700464 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
465 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400466
467 /* install a new client and return with it unready */
468install_client:
469 clp = new;
470 list_add(&clp->cl_share_link, &nfs_client_list);
471 spin_unlock(&nfs_client_lock);
472 dprintk("--> nfs_get_client() = %p [new]\n", clp);
473 return clp;
474
475 /* found an existing client
476 * - make sure it's ready before returning
477 */
478found_client:
479 spin_unlock(&nfs_client_lock);
480
481 if (new)
482 nfs_free_client(new);
483
Matthew Wilcox150030b2007-12-06 16:24:39 -0500484 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400485 clp->cl_cons_state != NFS_CS_INITING);
486 if (error < 0) {
487 nfs_put_client(clp);
488 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400489 }
490
491 if (clp->cl_cons_state < NFS_CS_READY) {
492 error = clp->cl_cons_state;
493 nfs_put_client(clp);
494 return ERR_PTR(error);
495 }
496
David Howells54ceac42006-08-22 20:06:13 -0400497 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
498
David Howells24c8dbb2006-08-22 20:06:10 -0400499 dprintk("--> nfs_get_client() = %p [share]\n", clp);
500 return clp;
501}
502
503/*
504 * Mark a server as ready or failed
505 */
David Howells54ceac42006-08-22 20:06:13 -0400506static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400507{
508 clp->cl_cons_state = state;
509 wake_up_all(&nfs_client_active_wq);
510}
David Howells5006a762006-08-22 20:06:12 -0400511
512/*
513 * Initialise the timeout values for a connection
514 */
515static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
516 unsigned int timeo, unsigned int retrans)
517{
518 to->to_initval = timeo * HZ / 10;
519 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400520
521 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400522 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400523 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400524 if (to->to_retries == 0)
525 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500526 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400527 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400528 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
529 to->to_initval = NFS_MAX_TCP_TIMEOUT;
530 to->to_increment = to->to_initval;
531 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500532 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
533 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
534 if (to->to_maxval < to->to_initval)
535 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400536 to->to_exponential = 0;
537 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400538 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400539 if (to->to_retries == 0)
540 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400541 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400542 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400543 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
544 to->to_initval = NFS_MAX_UDP_TIMEOUT;
545 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
546 to->to_exponential = 1;
547 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400548 default:
549 BUG();
David Howells5006a762006-08-22 20:06:12 -0400550 }
551}
552
553/*
554 * Create an RPC client handle
555 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500556static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500557 const struct rpc_timeout *timeparms,
558 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500559 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400560{
David Howells5006a762006-08-22 20:06:12 -0400561 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400562 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500563 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400564 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500565 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500566 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400567 .servername = clp->cl_hostname,
568 .program = &nfs_program,
569 .version = clp->rpc_ops->version,
570 .authflavor = flavor,
571 };
David Howells5006a762006-08-22 20:06:12 -0400572
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500573 if (discrtry)
574 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
575 if (noresvport)
576 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
577
David Howells5006a762006-08-22 20:06:12 -0400578 if (!IS_ERR(clp->cl_rpcclient))
579 return 0;
580
Chuck Lever41877d22006-08-22 20:06:20 -0400581 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400582 if (IS_ERR(clnt)) {
583 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700584 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400585 return PTR_ERR(clnt);
586 }
587
David Howells5006a762006-08-22 20:06:12 -0400588 clp->cl_rpcclient = clnt;
589 return 0;
590}
David Howells54ceac42006-08-22 20:06:13 -0400591
592/*
593 * Version 2 or 3 client destruction
594 */
595static void nfs_destroy_server(struct nfs_server *server)
596{
David Howells54ceac42006-08-22 20:06:13 -0400597 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500598 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400599}
600
601/*
602 * Version 2 or 3 lockd setup
603 */
604static int nfs_start_lockd(struct nfs_server *server)
605{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500606 struct nlm_host *host;
607 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500608 struct nlmclnt_initdata nlm_init = {
609 .hostname = clp->cl_hostname,
610 .address = (struct sockaddr *)&clp->cl_addr,
611 .addrlen = clp->cl_addrlen,
612 .protocol = server->flags & NFS_MOUNT_TCP ?
613 IPPROTO_TCP : IPPROTO_UDP,
614 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500615 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
616 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500617 };
David Howells54ceac42006-08-22 20:06:13 -0400618
Chuck Lever883bb162008-01-15 16:04:20 -0500619 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500620 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400621 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500622 return 0;
623
Chuck Lever883bb162008-01-15 16:04:20 -0500624 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500625 if (IS_ERR(host))
626 return PTR_ERR(host);
627
628 server->nlm_host = host;
629 server->destroy = nfs_destroy_server;
630 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400631}
632
633/*
634 * Initialise an NFSv3 ACL client connection
635 */
636#ifdef CONFIG_NFS_V3_ACL
637static void nfs_init_server_aclclient(struct nfs_server *server)
638{
Trond Myklebust40c553192007-12-14 14:56:07 -0500639 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400640 goto out_noacl;
641 if (server->flags & NFS_MOUNT_NOACL)
642 goto out_noacl;
643
644 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
645 if (IS_ERR(server->client_acl))
646 goto out_noacl;
647
648 /* No errors! Assume that Sun nfsacls are supported */
649 server->caps |= NFS_CAP_ACLS;
650 return;
651
652out_noacl:
653 server->caps &= ~NFS_CAP_ACLS;
654}
655#else
656static inline void nfs_init_server_aclclient(struct nfs_server *server)
657{
658 server->flags &= ~NFS_MOUNT_NOACL;
659 server->caps &= ~NFS_CAP_ACLS;
660}
661#endif
662
663/*
664 * Create a general RPC client
665 */
Trond Myklebust33170232007-12-20 16:03:59 -0500666static int nfs_init_server_rpcclient(struct nfs_server *server,
667 const struct rpc_timeout *timeo,
668 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400669{
670 struct nfs_client *clp = server->nfs_client;
671
672 server->client = rpc_clone_client(clp->cl_rpcclient);
673 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700674 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400675 return PTR_ERR(server->client);
676 }
677
Trond Myklebust33170232007-12-20 16:03:59 -0500678 memcpy(&server->client->cl_timeout_default,
679 timeo,
680 sizeof(server->client->cl_timeout_default));
681 server->client->cl_timeout = &server->client->cl_timeout_default;
682
David Howells54ceac42006-08-22 20:06:13 -0400683 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
684 struct rpc_auth *auth;
685
686 auth = rpcauth_create(pseudoflavour, server->client);
687 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700688 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400689 return PTR_ERR(auth);
690 }
691 }
692 server->client->cl_softrtry = 0;
693 if (server->flags & NFS_MOUNT_SOFT)
694 server->client->cl_softrtry = 1;
695
David Howells54ceac42006-08-22 20:06:13 -0400696 return 0;
697}
698
699/*
700 * Initialise an NFS2 or NFS3 client
701 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400702static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500703 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400704 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400705{
David Howells54ceac42006-08-22 20:06:13 -0400706 int error;
707
708 if (clp->cl_cons_state == NFS_CS_READY) {
709 /* the client is already initialised */
710 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
711 return 0;
712 }
713
David Howells54ceac42006-08-22 20:06:13 -0400714 /*
715 * Create a client RPC handle for doing FSSTAT with UNIX auth only
716 * - RFC 2623, sec 2.3.2
717 */
Chuck Leverd7403512008-12-23 15:21:37 -0500718 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
719 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400720 if (error < 0)
721 goto error;
722 nfs_mark_client_ready(clp, NFS_CS_READY);
723 return 0;
724
725error:
726 nfs_mark_client_ready(clp, error);
727 dprintk("<-- nfs_init_client() = xerror %d\n", error);
728 return error;
729}
730
731/*
732 * Create a version 2 or 3 client
733 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400734static int nfs_init_server(struct nfs_server *server,
735 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400736{
Trond Myklebust3a498022007-12-14 14:56:04 -0500737 struct nfs_client_initdata cl_init = {
738 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500739 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500740 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500741 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500742 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500743 };
Trond Myklebust33170232007-12-20 16:03:59 -0500744 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400745 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500746 int error;
David Howells54ceac42006-08-22 20:06:13 -0400747
748 dprintk("--> nfs_init_server()\n");
749
750#ifdef CONFIG_NFS_V3
751 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500752 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400753#endif
754
755 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500756 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400757 if (IS_ERR(clp)) {
758 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
759 return PTR_ERR(clp);
760 }
761
Trond Myklebust33170232007-12-20 16:03:59 -0500762 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
763 data->timeo, data->retrans);
764 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400765 if (error < 0)
766 goto error;
767
768 server->nfs_client = clp;
769
770 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400771 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100772 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400773
774 if (data->rsize)
775 server->rsize = nfs_block_size(data->rsize, NULL);
776 if (data->wsize)
777 server->wsize = nfs_block_size(data->wsize, NULL);
778
779 server->acregmin = data->acregmin * HZ;
780 server->acregmax = data->acregmax * HZ;
781 server->acdirmin = data->acdirmin * HZ;
782 server->acdirmax = data->acdirmax * HZ;
783
784 /* Start lockd here, before we might error out */
785 error = nfs_start_lockd(server);
786 if (error < 0)
787 goto error;
788
Chuck Leverf22d6d72008-03-14 14:10:22 -0400789 server->port = data->nfs_server.port;
790
Trond Myklebust33170232007-12-20 16:03:59 -0500791 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400792 if (error < 0)
793 goto error;
794
Chuck Lever3f8400d2008-03-14 14:10:30 -0400795 /* Preserve the values of mount_server-related mount options */
796 if (data->mount_server.addrlen) {
797 memcpy(&server->mountd_address, &data->mount_server.address,
798 data->mount_server.addrlen);
799 server->mountd_addrlen = data->mount_server.addrlen;
800 }
801 server->mountd_version = data->mount_server.version;
802 server->mountd_port = data->mount_server.port;
803 server->mountd_protocol = data->mount_server.protocol;
804
David Howells54ceac42006-08-22 20:06:13 -0400805 server->namelen = data->namlen;
806 /* Create a client RPC handle for the NFSv3 ACL management interface */
807 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400808 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
809 return 0;
810
811error:
812 server->nfs_client = NULL;
813 nfs_put_client(clp);
814 dprintk("<-- nfs_init_server() = xerror %d\n", error);
815 return error;
816}
817
818/*
819 * Load up the server record from information gained in an fsinfo record
820 */
821static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
822{
823 unsigned long max_rpc_payload;
824
825 /* Work out a lot of parameters */
826 if (server->rsize == 0)
827 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
828 if (server->wsize == 0)
829 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
830
831 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
832 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
833 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
834 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
835
836 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
837 if (server->rsize > max_rpc_payload)
838 server->rsize = max_rpc_payload;
839 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
840 server->rsize = NFS_MAX_FILE_IO_SIZE;
841 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700842
David Howells54ceac42006-08-22 20:06:13 -0400843 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
844
845 if (server->wsize > max_rpc_payload)
846 server->wsize = max_rpc_payload;
847 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
848 server->wsize = NFS_MAX_FILE_IO_SIZE;
849 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
850 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
851
852 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
853 if (server->dtsize > PAGE_CACHE_SIZE)
854 server->dtsize = PAGE_CACHE_SIZE;
855 if (server->dtsize > server->rsize)
856 server->dtsize = server->rsize;
857
858 if (server->flags & NFS_MOUNT_NOAC) {
859 server->acregmin = server->acregmax = 0;
860 server->acdirmin = server->acdirmax = 0;
861 }
862
863 server->maxfilesize = fsinfo->maxfilesize;
864
865 /* We're airborne Set socket buffersize */
866 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
867}
868
869/*
870 * Probe filesystem information, including the FSID on v2/v3
871 */
872static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
873{
874 struct nfs_fsinfo fsinfo;
875 struct nfs_client *clp = server->nfs_client;
876 int error;
877
878 dprintk("--> nfs_probe_fsinfo()\n");
879
880 if (clp->rpc_ops->set_capabilities != NULL) {
881 error = clp->rpc_ops->set_capabilities(server, mntfh);
882 if (error < 0)
883 goto out_error;
884 }
885
886 fsinfo.fattr = fattr;
887 nfs_fattr_init(fattr);
888 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
889 if (error < 0)
890 goto out_error;
891
892 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700893 error = bdi_init(&server->backing_dev_info);
894 if (error)
895 goto out_error;
896
David Howells54ceac42006-08-22 20:06:13 -0400897
898 /* Get some general file system info */
899 if (server->namelen == 0) {
900 struct nfs_pathconf pathinfo;
901
902 pathinfo.fattr = fattr;
903 nfs_fattr_init(fattr);
904
905 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
906 server->namelen = pathinfo.max_namelen;
907 }
908
909 dprintk("<-- nfs_probe_fsinfo() = 0\n");
910 return 0;
911
912out_error:
913 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
914 return error;
915}
916
917/*
918 * Copy useful information when duplicating a server record
919 */
920static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
921{
922 target->flags = source->flags;
923 target->acregmin = source->acregmin;
924 target->acregmax = source->acregmax;
925 target->acdirmin = source->acdirmin;
926 target->acdirmax = source->acdirmax;
927 target->caps = source->caps;
928}
929
930/*
931 * Allocate and initialise a server record
932 */
933static struct nfs_server *nfs_alloc_server(void)
934{
935 struct nfs_server *server;
936
937 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
938 if (!server)
939 return NULL;
940
941 server->client = server->client_acl = ERR_PTR(-EINVAL);
942
943 /* Zero out the NFS state stuff */
944 INIT_LIST_HEAD(&server->client_link);
945 INIT_LIST_HEAD(&server->master_link);
946
Steve Dicksonef818a22007-11-08 04:05:04 -0500947 atomic_set(&server->active, 0);
948
David Howells54ceac42006-08-22 20:06:13 -0400949 server->io_stats = nfs_alloc_iostats();
950 if (!server->io_stats) {
951 kfree(server);
952 return NULL;
953 }
954
955 return server;
956}
957
958/*
959 * Free up a server record
960 */
961void nfs_free_server(struct nfs_server *server)
962{
963 dprintk("--> nfs_free_server()\n");
964
965 spin_lock(&nfs_client_lock);
966 list_del(&server->client_link);
967 list_del(&server->master_link);
968 spin_unlock(&nfs_client_lock);
969
970 if (server->destroy != NULL)
971 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500972
973 if (!IS_ERR(server->client_acl))
974 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400975 if (!IS_ERR(server->client))
976 rpc_shutdown_client(server->client);
977
978 nfs_put_client(server->nfs_client);
979
980 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700981 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400982 kfree(server);
983 nfs_release_automount_timer();
984 dprintk("<-- nfs_free_server()\n");
985}
986
987/*
988 * Create a version 2 or 3 volume record
989 * - keyed on server and FSID
990 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400991struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400992 struct nfs_fh *mntfh)
993{
994 struct nfs_server *server;
995 struct nfs_fattr fattr;
996 int error;
997
998 server = nfs_alloc_server();
999 if (!server)
1000 return ERR_PTR(-ENOMEM);
1001
1002 /* Get a client representation */
1003 error = nfs_init_server(server, data);
1004 if (error < 0)
1005 goto error;
1006
1007 BUG_ON(!server->nfs_client);
1008 BUG_ON(!server->nfs_client->rpc_ops);
1009 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1010
1011 /* Probe the root fh to retrieve its FSID */
1012 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1013 if (error < 0)
1014 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001015 if (server->nfs_client->rpc_ops->version == 3) {
1016 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1017 server->namelen = NFS3_MAXNAMLEN;
1018 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1019 server->caps |= NFS_CAP_READDIRPLUS;
1020 } else {
1021 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1022 server->namelen = NFS2_MAXNAMLEN;
1023 }
1024
David Howells54ceac42006-08-22 20:06:13 -04001025 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1026 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1027 if (error < 0) {
1028 dprintk("nfs_create_server: getattr error = %d\n", -error);
1029 goto error;
1030 }
1031 }
1032 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1033
David Howells6daabf12006-08-24 15:44:16 -04001034 dprintk("Server FSID: %llx:%llx\n",
1035 (unsigned long long) server->fsid.major,
1036 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001037
1038 BUG_ON(!server->nfs_client);
1039 BUG_ON(!server->nfs_client->rpc_ops);
1040 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1041
1042 spin_lock(&nfs_client_lock);
1043 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1044 list_add_tail(&server->master_link, &nfs_volume_list);
1045 spin_unlock(&nfs_client_lock);
1046
1047 server->mount_time = jiffies;
1048 return server;
1049
1050error:
1051 nfs_free_server(server);
1052 return ERR_PTR(error);
1053}
1054
1055#ifdef CONFIG_NFS_V4
1056/*
1057 * Initialise an NFS4 client record
1058 */
1059static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001060 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001061 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001062 rpc_authflavor_t authflavour,
1063 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001064{
1065 int error;
1066
1067 if (clp->cl_cons_state == NFS_CS_READY) {
1068 /* the client is initialised already */
1069 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1070 return 0;
1071 }
1072
1073 /* Check NFS protocol revision and initialize RPC op vector */
1074 clp->rpc_ops = &nfs_v4_clientops;
1075
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001076 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001077 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001078 if (error < 0)
1079 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001080 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001081
1082 error = nfs_idmap_new(clp);
1083 if (error < 0) {
1084 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001085 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001086 goto error;
1087 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001088 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001089
1090 nfs_mark_client_ready(clp, NFS_CS_READY);
1091 return 0;
1092
1093error:
1094 nfs_mark_client_ready(clp, error);
1095 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1096 return error;
1097}
1098
1099/*
1100 * Set up an NFS4 client
1101 */
1102static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001103 const char *hostname,
1104 const struct sockaddr *addr,
1105 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001106 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001107 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -05001108 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -04001109{
Trond Myklebust3a498022007-12-14 14:56:04 -05001110 struct nfs_client_initdata cl_init = {
1111 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001112 .addr = addr,
1113 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001114 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001115 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -05001116 };
David Howells54ceac42006-08-22 20:06:13 -04001117 struct nfs_client *clp;
1118 int error;
1119
1120 dprintk("--> nfs4_set_client()\n");
1121
1122 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001123 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001124 if (IS_ERR(clp)) {
1125 error = PTR_ERR(clp);
1126 goto error;
1127 }
Chuck Leverd7403512008-12-23 15:21:37 -05001128 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1129 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001130 if (error < 0)
1131 goto error_put;
1132
1133 server->nfs_client = clp;
1134 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1135 return 0;
1136
1137error_put:
1138 nfs_put_client(clp);
1139error:
1140 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1141 return error;
1142}
1143
1144/*
1145 * Create a version 4 volume record
1146 */
1147static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001148 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001149{
Trond Myklebust33170232007-12-20 16:03:59 -05001150 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001151 int error;
1152
1153 dprintk("--> nfs4_init_server()\n");
1154
Trond Myklebust33170232007-12-20 16:03:59 -05001155 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1156 data->timeo, data->retrans);
1157
Chuck Lever542fcc32008-12-23 15:21:36 -05001158 /* Initialise the client representation from the mount data */
1159 server->flags = data->flags;
1160 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001161 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001162
Trond Myklebust33170232007-12-20 16:03:59 -05001163 /* Get a client record */
1164 error = nfs4_set_client(server,
1165 data->nfs_server.hostname,
1166 (const struct sockaddr *)&data->nfs_server.address,
1167 data->nfs_server.addrlen,
1168 data->client_address,
1169 data->auth_flavors[0],
1170 data->nfs_server.protocol,
1171 &timeparms);
1172 if (error < 0)
1173 goto error;
1174
David Howells54ceac42006-08-22 20:06:13 -04001175 if (data->rsize)
1176 server->rsize = nfs_block_size(data->rsize, NULL);
1177 if (data->wsize)
1178 server->wsize = nfs_block_size(data->wsize, NULL);
1179
1180 server->acregmin = data->acregmin * HZ;
1181 server->acregmax = data->acregmax * HZ;
1182 server->acdirmin = data->acdirmin * HZ;
1183 server->acdirmax = data->acdirmax * HZ;
1184
Chuck Leverf22d6d72008-03-14 14:10:22 -04001185 server->port = data->nfs_server.port;
1186
Trond Myklebust33170232007-12-20 16:03:59 -05001187 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001188
Trond Myklebust33170232007-12-20 16:03:59 -05001189error:
David Howells54ceac42006-08-22 20:06:13 -04001190 /* Done */
1191 dprintk("<-- nfs4_init_server() = %d\n", error);
1192 return error;
1193}
1194
1195/*
1196 * Create a version 4 volume record
1197 * - keyed on server and FSID
1198 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001199struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001200 struct nfs_fh *mntfh)
1201{
1202 struct nfs_fattr fattr;
1203 struct nfs_server *server;
1204 int error;
1205
1206 dprintk("--> nfs4_create_server()\n");
1207
1208 server = nfs_alloc_server();
1209 if (!server)
1210 return ERR_PTR(-ENOMEM);
1211
David Howells54ceac42006-08-22 20:06:13 -04001212 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001213 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001214 if (error < 0)
1215 goto error;
1216
1217 BUG_ON(!server->nfs_client);
1218 BUG_ON(!server->nfs_client->rpc_ops);
1219 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1220
1221 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001222 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001223 if (error < 0)
1224 goto error;
1225
David Howells6daabf12006-08-24 15:44:16 -04001226 dprintk("Server FSID: %llx:%llx\n",
1227 (unsigned long long) server->fsid.major,
1228 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001229 dprintk("Mount FH: %d\n", mntfh->size);
1230
1231 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1232 if (error < 0)
1233 goto error;
1234
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001235 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1236 server->namelen = NFS4_MAXNAMLEN;
1237
David Howells54ceac42006-08-22 20:06:13 -04001238 BUG_ON(!server->nfs_client);
1239 BUG_ON(!server->nfs_client->rpc_ops);
1240 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1241
1242 spin_lock(&nfs_client_lock);
1243 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1244 list_add_tail(&server->master_link, &nfs_volume_list);
1245 spin_unlock(&nfs_client_lock);
1246
1247 server->mount_time = jiffies;
1248 dprintk("<-- nfs4_create_server() = %p\n", server);
1249 return server;
1250
1251error:
1252 nfs_free_server(server);
1253 dprintk("<-- nfs4_create_server() = error %d\n", error);
1254 return ERR_PTR(error);
1255}
1256
1257/*
1258 * Create an NFS4 referral server record
1259 */
1260struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001261 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001262{
1263 struct nfs_client *parent_client;
1264 struct nfs_server *server, *parent_server;
1265 struct nfs_fattr fattr;
1266 int error;
1267
1268 dprintk("--> nfs4_create_referral_server()\n");
1269
1270 server = nfs_alloc_server();
1271 if (!server)
1272 return ERR_PTR(-ENOMEM);
1273
1274 parent_server = NFS_SB(data->sb);
1275 parent_client = parent_server->nfs_client;
1276
Chuck Lever542fcc32008-12-23 15:21:36 -05001277 /* Initialise the client representation from the parent server */
1278 nfs_server_copy_userdata(server, parent_server);
1279 server->caps |= NFS_CAP_ATOMIC_OPEN;
1280
David Howells54ceac42006-08-22 20:06:13 -04001281 /* Get a client representation.
1282 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001283 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001284 data->addr,
1285 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001286 parent_client->cl_ipaddr,
1287 data->authflavor,
1288 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001289 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001290 if (error < 0)
1291 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001292
Trond Myklebust33170232007-12-20 16:03:59 -05001293 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001294 if (error < 0)
1295 goto error;
1296
1297 BUG_ON(!server->nfs_client);
1298 BUG_ON(!server->nfs_client->rpc_ops);
1299 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1300
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001301 /* Probe the root fh to retrieve its FSID and filehandle */
1302 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1303 if (error < 0)
1304 goto error;
1305
David Howells54ceac42006-08-22 20:06:13 -04001306 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001307 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001308 if (error < 0)
1309 goto error;
1310
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001311 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1312 server->namelen = NFS4_MAXNAMLEN;
1313
David Howells54ceac42006-08-22 20:06:13 -04001314 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001315 (unsigned long long) server->fsid.major,
1316 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001317
1318 spin_lock(&nfs_client_lock);
1319 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1320 list_add_tail(&server->master_link, &nfs_volume_list);
1321 spin_unlock(&nfs_client_lock);
1322
1323 server->mount_time = jiffies;
1324
1325 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1326 return server;
1327
1328error:
1329 nfs_free_server(server);
1330 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1331 return ERR_PTR(error);
1332}
1333
1334#endif /* CONFIG_NFS_V4 */
1335
1336/*
1337 * Clone an NFS2, NFS3 or NFS4 server record
1338 */
1339struct nfs_server *nfs_clone_server(struct nfs_server *source,
1340 struct nfs_fh *fh,
1341 struct nfs_fattr *fattr)
1342{
1343 struct nfs_server *server;
1344 struct nfs_fattr fattr_fsinfo;
1345 int error;
1346
1347 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001348 (unsigned long long) fattr->fsid.major,
1349 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001350
1351 server = nfs_alloc_server();
1352 if (!server)
1353 return ERR_PTR(-ENOMEM);
1354
1355 /* Copy data from the source */
1356 server->nfs_client = source->nfs_client;
1357 atomic_inc(&server->nfs_client->cl_count);
1358 nfs_server_copy_userdata(server, source);
1359
1360 server->fsid = fattr->fsid;
1361
Trond Myklebust33170232007-12-20 16:03:59 -05001362 error = nfs_init_server_rpcclient(server,
1363 source->client->cl_timeout,
1364 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001365 if (error < 0)
1366 goto out_free_server;
1367 if (!IS_ERR(source->client_acl))
1368 nfs_init_server_aclclient(server);
1369
1370 /* probe the filesystem info for this server filesystem */
1371 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1372 if (error < 0)
1373 goto out_free_server;
1374
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001375 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1376 server->namelen = NFS4_MAXNAMLEN;
1377
David Howells54ceac42006-08-22 20:06:13 -04001378 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001379 (unsigned long long) server->fsid.major,
1380 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001381
1382 error = nfs_start_lockd(server);
1383 if (error < 0)
1384 goto out_free_server;
1385
1386 spin_lock(&nfs_client_lock);
1387 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1388 list_add_tail(&server->master_link, &nfs_volume_list);
1389 spin_unlock(&nfs_client_lock);
1390
1391 server->mount_time = jiffies;
1392
1393 dprintk("<-- nfs_clone_server() = %p\n", server);
1394 return server;
1395
1396out_free_server:
1397 nfs_free_server(server);
1398 dprintk("<-- nfs_clone_server() = error %d\n", error);
1399 return ERR_PTR(error);
1400}
David Howells6aaca562006-08-22 20:06:13 -04001401
1402#ifdef CONFIG_PROC_FS
1403static struct proc_dir_entry *proc_fs_nfs;
1404
1405static int nfs_server_list_open(struct inode *inode, struct file *file);
1406static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1407static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1408static void nfs_server_list_stop(struct seq_file *p, void *v);
1409static int nfs_server_list_show(struct seq_file *m, void *v);
1410
1411static struct seq_operations nfs_server_list_ops = {
1412 .start = nfs_server_list_start,
1413 .next = nfs_server_list_next,
1414 .stop = nfs_server_list_stop,
1415 .show = nfs_server_list_show,
1416};
1417
Arjan van de Ven00977a52007-02-12 00:55:34 -08001418static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001419 .open = nfs_server_list_open,
1420 .read = seq_read,
1421 .llseek = seq_lseek,
1422 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001423 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001424};
1425
1426static int nfs_volume_list_open(struct inode *inode, struct file *file);
1427static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1428static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1429static void nfs_volume_list_stop(struct seq_file *p, void *v);
1430static int nfs_volume_list_show(struct seq_file *m, void *v);
1431
1432static struct seq_operations nfs_volume_list_ops = {
1433 .start = nfs_volume_list_start,
1434 .next = nfs_volume_list_next,
1435 .stop = nfs_volume_list_stop,
1436 .show = nfs_volume_list_show,
1437};
1438
Arjan van de Ven00977a52007-02-12 00:55:34 -08001439static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001440 .open = nfs_volume_list_open,
1441 .read = seq_read,
1442 .llseek = seq_lseek,
1443 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001444 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001445};
1446
1447/*
1448 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1449 * we're dealing
1450 */
1451static int nfs_server_list_open(struct inode *inode, struct file *file)
1452{
1453 struct seq_file *m;
1454 int ret;
1455
1456 ret = seq_open(file, &nfs_server_list_ops);
1457 if (ret < 0)
1458 return ret;
1459
1460 m = file->private_data;
1461 m->private = PDE(inode)->data;
1462
1463 return 0;
1464}
1465
1466/*
1467 * set up the iterator to start reading from the server list and return the first item
1468 */
1469static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1470{
David Howells6aaca562006-08-22 20:06:13 -04001471 /* lock the list against modification */
1472 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001473 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001474}
1475
1476/*
1477 * move to next server
1478 */
1479static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1480{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001481 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001482}
1483
1484/*
1485 * clean up after reading from the transports list
1486 */
1487static void nfs_server_list_stop(struct seq_file *p, void *v)
1488{
1489 spin_unlock(&nfs_client_lock);
1490}
1491
1492/*
1493 * display a header line followed by a load of call lines
1494 */
1495static int nfs_server_list_show(struct seq_file *m, void *v)
1496{
1497 struct nfs_client *clp;
1498
1499 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001500 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001501 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1502 return 0;
1503 }
1504
1505 /* display one transport per line on subsequent lines */
1506 clp = list_entry(v, struct nfs_client, cl_share_link);
1507
Chuck Lever5d8515c2007-12-10 14:57:16 -05001508 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001509 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001510 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1511 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001512 atomic_read(&clp->cl_count),
1513 clp->cl_hostname);
1514
1515 return 0;
1516}
1517
1518/*
1519 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1520 */
1521static int nfs_volume_list_open(struct inode *inode, struct file *file)
1522{
1523 struct seq_file *m;
1524 int ret;
1525
1526 ret = seq_open(file, &nfs_volume_list_ops);
1527 if (ret < 0)
1528 return ret;
1529
1530 m = file->private_data;
1531 m->private = PDE(inode)->data;
1532
1533 return 0;
1534}
1535
1536/*
1537 * set up the iterator to start reading from the volume list and return the first item
1538 */
1539static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1540{
David Howells6aaca562006-08-22 20:06:13 -04001541 /* lock the list against modification */
1542 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001543 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001544}
1545
1546/*
1547 * move to next volume
1548 */
1549static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1550{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001551 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001552}
1553
1554/*
1555 * clean up after reading from the transports list
1556 */
1557static void nfs_volume_list_stop(struct seq_file *p, void *v)
1558{
1559 spin_unlock(&nfs_client_lock);
1560}
1561
1562/*
1563 * display a header line followed by a load of call lines
1564 */
1565static int nfs_volume_list_show(struct seq_file *m, void *v)
1566{
1567 struct nfs_server *server;
1568 struct nfs_client *clp;
1569 char dev[8], fsid[17];
1570
1571 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001572 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001573 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001574 return 0;
1575 }
1576 /* display one transport per line on subsequent lines */
1577 server = list_entry(v, struct nfs_server, master_link);
1578 clp = server->nfs_client;
1579
1580 snprintf(dev, 8, "%u:%u",
1581 MAJOR(server->s_dev), MINOR(server->s_dev));
1582
1583 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001584 (unsigned long long) server->fsid.major,
1585 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001586
David Howells5d1acff2009-04-03 16:42:47 +01001587 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001588 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001589 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1590 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001591 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001592 fsid,
1593 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001594
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
David Howells6aaca562006-08-22 20:06:13 -04001609 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001610 p = proc_create("servers", S_IFREG|S_IRUGO,
1611 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001612 if (!p)
1613 goto error_1;
1614
David Howells6aaca562006-08-22 20:06:13 -04001615 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001616 p = proc_create("volumes", S_IFREG|S_IRUGO,
1617 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001618 if (!p)
1619 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001620 return 0;
1621
1622error_2:
1623 remove_proc_entry("servers", proc_fs_nfs);
1624error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001625 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001626error_0:
1627 return -ENOMEM;
1628}
1629
1630/*
1631 * clean up the /proc/fs/nfsfs/ directory
1632 */
1633void nfs_fs_proc_exit(void)
1634{
1635 remove_proc_entry("volumes", proc_fs_nfs);
1636 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001637 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001638}
1639
1640#endif /* CONFIG_PROC_FS */