blob: f1506f1485216fb8c141190e25a0f495d99ac165 [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;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400105 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500106};
107
David Howells5006a762006-08-22 20:06:12 -0400108/*
David Howells24c8dbb2006-08-22 20:06:10 -0400109 * Allocate a shared client record
110 *
111 * Since these are allocated/deallocated very rarely, we don't
112 * bother putting them in a slab cache...
113 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500114static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400115{
116 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400117 struct rpc_cred *cred;
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) {
David Howells24c8dbb2006-08-22 20:06:10 -0400125 if (nfs_callback_up() < 0)
126 goto error_2;
127 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
128 }
129
130 atomic_set(&clp->cl_count, 1);
131 clp->cl_cons_state = NFS_CS_INITING;
132
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500133 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
134 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400135
Trond Myklebust3a498022007-12-14 14:56:04 -0500136 if (cl_init->hostname) {
137 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400138 if (!clp->cl_hostname)
139 goto error_3;
140 }
141
142 INIT_LIST_HEAD(&clp->cl_superblocks);
143 clp->cl_rpcclient = ERR_PTR(-EINVAL);
144
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500145 clp->cl_proto = cl_init->proto;
146
David Howells24c8dbb2006-08-22 20:06:10 -0400147#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400148 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000150 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400151 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
152 clp->cl_boot_time = CURRENT_TIME;
153 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400154 clp->cl_minorversion = cl_init->minorversion;
David Howells24c8dbb2006-08-22 20:06:10 -0400155#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400156 cred = rpc_lookup_machine_cred();
157 if (!IS_ERR(cred))
158 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400159
David Howells14727282009-04-03 16:42:42 +0100160 nfs_fscache_get_client_cookie(clp);
161
David Howells24c8dbb2006-08-22 20:06:10 -0400162 return clp;
163
164error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400165 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
166 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400167error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400168 kfree(clp);
169error_0:
170 return NULL;
171}
172
Trond Myklebust5dd31772006-08-24 01:03:05 -0400173static void nfs4_shutdown_client(struct nfs_client *clp)
174{
175#ifdef CONFIG_NFS_V4
176 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
177 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400178 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400179 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
180 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500181
182 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400183#endif
184}
185
David Howells24c8dbb2006-08-22 20:06:10 -0400186/*
Andy Adamson557134a2009-04-01 09:21:53 -0400187 * Clears/puts all minor version specific parts from an nfs_client struct
188 * reverting it to minorversion 0.
189 */
190static void nfs4_clear_client_minor_version(struct nfs_client *clp)
191{
192#ifdef CONFIG_NFS_V4_1
193 if (nfs4_has_session(clp)) {
194 nfs4_destroy_session(clp->cl_session);
195 clp->cl_session = NULL;
196 }
197#endif /* CONFIG_NFS_V4_1 */
198}
199
200/*
David Howells24c8dbb2006-08-22 20:06:10 -0400201 * Destroy a shared client record
202 */
203static void nfs_free_client(struct nfs_client *clp)
204{
Trond Myklebust40c553192007-12-14 14:56:07 -0500205 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400206
Andy Adamson557134a2009-04-01 09:21:53 -0400207 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400208 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400209
David Howells14727282009-04-03 16:42:42 +0100210 nfs_fscache_release_client_cookie(clp);
211
David Howells24c8dbb2006-08-22 20:06:10 -0400212 /* -EIO all pending I/O */
213 if (!IS_ERR(clp->cl_rpcclient))
214 rpc_shutdown_client(clp->cl_rpcclient);
215
216 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
217 nfs_callback_down();
218
Trond Myklebust7c67db32008-04-07 20:50:11 -0400219 if (clp->cl_machine_cred != NULL)
220 put_rpccred(clp->cl_machine_cred);
221
David Howells24c8dbb2006-08-22 20:06:10 -0400222 kfree(clp->cl_hostname);
223 kfree(clp);
224
225 dprintk("<-- nfs_free_client()\n");
226}
227
228/*
229 * Release a reference to a shared client record
230 */
231void nfs_put_client(struct nfs_client *clp)
232{
David Howells27ba8512006-07-30 14:40:56 -0400233 if (!clp)
234 return;
235
David Howells24c8dbb2006-08-22 20:06:10 -0400236 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
237
238 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
239 list_del(&clp->cl_share_link);
240 spin_unlock(&nfs_client_lock);
241
242 BUG_ON(!list_empty(&clp->cl_superblocks));
243
244 nfs_free_client(clp);
245 }
246}
247
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500248#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400249/*
250 * Test if two ip6 socket addresses refer to the same socket by
251 * comparing relevant fields. The padding bytes specifically, are not
252 * compared. sin6_flowinfo is not compared because it only affects QoS
253 * and sin6_scope_id is only compared if the address is "link local"
254 * because "link local" addresses need only be unique to a specific
255 * link. Conversely, ordinary unicast addresses might have different
256 * sin6_scope_id.
257 *
258 * The caller should ensure both socket addresses are AF_INET6.
259 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400260static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
261 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400262{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400263 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
264 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400265
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400266 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
267 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400268 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500269
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400270 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500271}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
273static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
274 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400275{
276 return 0;
277}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500278#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500279
David Howells24c8dbb2006-08-22 20:06:10 -0400280/*
Ian Dalld7371c42009-03-10 20:33:22 -0400281 * Test if two ip4 socket addresses refer to the same socket, by
282 * comparing relevant fields. The padding bytes specifically, are
283 * not compared.
284 *
285 * The caller should ensure both socket addresses are AF_INET.
286 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400287static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
288 const struct sockaddr *sa2)
289{
290 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
291 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
292
293 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
294}
295
296static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
297 const struct sockaddr *sa2)
298{
299 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
300 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
301
302 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
303 (sin1->sin6_port == sin2->sin6_port);
304}
305
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400306static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
307 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400308{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400309 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
310 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400311
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400312 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
313 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400314}
315
316/*
Ian Dalld7371c42009-03-10 20:33:22 -0400317 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400318 * by comparing (only) relevant fields, excluding the port number.
319 */
320static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
321 const struct sockaddr *sa2)
322{
323 if (sa1->sa_family != sa2->sa_family)
324 return 0;
325
326 switch (sa1->sa_family) {
327 case AF_INET:
328 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
329 case AF_INET6:
330 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
331 }
332 return 0;
333}
334
335/*
336 * Test if two socket addresses represent the same actual socket,
337 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400338 */
339static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
340 const struct sockaddr *sa2)
341{
342 if (sa1->sa_family != sa2->sa_family)
343 return 0;
344
345 switch (sa1->sa_family) {
346 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400347 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400348 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400349 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400350 }
351 return 0;
352}
353
354/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500355 * Find a client by IP address and protocol version
356 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400357 */
Chuck Leverff052642007-12-10 14:58:44 -0500358struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500359{
360 struct nfs_client *clp;
361
362 spin_lock(&nfs_client_lock);
363 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500364 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
365
Trond Myklebustc81468a2007-12-14 14:56:05 -0500366 /* Don't match clients that failed to initialise properly */
367 if (clp->cl_cons_state != NFS_CS_READY)
368 continue;
369
370 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500371 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500372 continue;
373
374 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500375 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500376 continue;
377
378 atomic_inc(&clp->cl_count);
379 spin_unlock(&nfs_client_lock);
380 return clp;
381 }
382 spin_unlock(&nfs_client_lock);
383 return NULL;
384}
385
386/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500387 * Find a client by IP address and protocol version
388 * - returns NULL if no such client
389 */
390struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
391{
392 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
393 u32 nfsvers = clp->rpc_ops->version;
394
395 spin_lock(&nfs_client_lock);
396 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
397 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
398
399 /* Don't match clients that failed to initialise properly */
400 if (clp->cl_cons_state != NFS_CS_READY)
401 continue;
402
403 /* Different NFS versions cannot share the same nfs_client */
404 if (clp->rpc_ops->version != nfsvers)
405 continue;
406
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500407 /* Match only the IP address, not the port number */
408 if (!nfs_sockaddr_match_ipaddr(sap, clap))
409 continue;
410
411 atomic_inc(&clp->cl_count);
412 spin_unlock(&nfs_client_lock);
413 return clp;
414 }
415 spin_unlock(&nfs_client_lock);
416 return NULL;
417}
418
419/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500420 * Find an nfs_client on the list that matches the initialisation data
421 * that is supplied.
422 */
423static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400424{
425 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400426 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400427
428 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400429 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700430 /* Don't match clients that failed to initialise properly */
431 if (clp->cl_cons_state < 0)
432 continue;
433
David Howells24c8dbb2006-08-22 20:06:10 -0400434 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500435 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400436 continue;
437
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500438 if (clp->cl_proto != data->proto)
439 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400440 /* Match nfsv4 minorversion */
441 if (clp->cl_minorversion != data->minorversion)
442 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500443 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400444 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400445 continue;
446
Trond Myklebustc81468a2007-12-14 14:56:05 -0500447 atomic_inc(&clp->cl_count);
448 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400449 }
David Howells24c8dbb2006-08-22 20:06:10 -0400450 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400451}
452
453/*
454 * Look up a client by IP address and protocol version
455 * - creates a new record if one doesn't yet exist
456 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500457static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400458{
459 struct nfs_client *clp, *new = NULL;
460 int error;
461
Chuck Leverd7422c42007-12-10 14:58:51 -0500462 dprintk("--> nfs_get_client(%s,v%u)\n",
463 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400464
465 /* see if the client already exists */
466 do {
467 spin_lock(&nfs_client_lock);
468
Trond Myklebustc81468a2007-12-14 14:56:05 -0500469 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400470 if (clp)
471 goto found_client;
472 if (new)
473 goto install_client;
474
475 spin_unlock(&nfs_client_lock);
476
Trond Myklebust3a498022007-12-14 14:56:04 -0500477 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400478 } while (new);
479
480 return ERR_PTR(-ENOMEM);
481
482 /* install a new client and return with it unready */
483install_client:
484 clp = new;
485 list_add(&clp->cl_share_link, &nfs_client_list);
486 spin_unlock(&nfs_client_lock);
487 dprintk("--> nfs_get_client() = %p [new]\n", clp);
488 return clp;
489
490 /* found an existing client
491 * - make sure it's ready before returning
492 */
493found_client:
494 spin_unlock(&nfs_client_lock);
495
496 if (new)
497 nfs_free_client(new);
498
Matthew Wilcox150030b2007-12-06 16:24:39 -0500499 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400500 clp->cl_cons_state != NFS_CS_INITING);
501 if (error < 0) {
502 nfs_put_client(clp);
503 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400504 }
505
506 if (clp->cl_cons_state < NFS_CS_READY) {
507 error = clp->cl_cons_state;
508 nfs_put_client(clp);
509 return ERR_PTR(error);
510 }
511
David Howells54ceac42006-08-22 20:06:13 -0400512 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
513
David Howells24c8dbb2006-08-22 20:06:10 -0400514 dprintk("--> nfs_get_client() = %p [share]\n", clp);
515 return clp;
516}
517
518/*
519 * Mark a server as ready or failed
520 */
David Howells54ceac42006-08-22 20:06:13 -0400521static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400522{
523 clp->cl_cons_state = state;
524 wake_up_all(&nfs_client_active_wq);
525}
David Howells5006a762006-08-22 20:06:12 -0400526
527/*
528 * Initialise the timeout values for a connection
529 */
530static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
531 unsigned int timeo, unsigned int retrans)
532{
533 to->to_initval = timeo * HZ / 10;
534 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400535
536 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400537 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400538 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400539 if (to->to_retries == 0)
540 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500541 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400542 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400543 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
544 to->to_initval = NFS_MAX_TCP_TIMEOUT;
545 to->to_increment = to->to_initval;
546 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500547 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
548 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
549 if (to->to_maxval < to->to_initval)
550 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400551 to->to_exponential = 0;
552 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400553 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400554 if (to->to_retries == 0)
555 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400556 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400557 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400558 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
559 to->to_initval = NFS_MAX_UDP_TIMEOUT;
560 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
561 to->to_exponential = 1;
562 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400563 default:
564 BUG();
David Howells5006a762006-08-22 20:06:12 -0400565 }
566}
567
568/*
569 * Create an RPC client handle
570 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500571static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500572 const struct rpc_timeout *timeparms,
573 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500574 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400575{
David Howells5006a762006-08-22 20:06:12 -0400576 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400577 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500578 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400579 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500580 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500581 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400582 .servername = clp->cl_hostname,
583 .program = &nfs_program,
584 .version = clp->rpc_ops->version,
585 .authflavor = flavor,
586 };
David Howells5006a762006-08-22 20:06:12 -0400587
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500588 if (discrtry)
589 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
590 if (noresvport)
591 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
592
David Howells5006a762006-08-22 20:06:12 -0400593 if (!IS_ERR(clp->cl_rpcclient))
594 return 0;
595
Chuck Lever41877d22006-08-22 20:06:20 -0400596 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400597 if (IS_ERR(clnt)) {
598 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700599 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400600 return PTR_ERR(clnt);
601 }
602
David Howells5006a762006-08-22 20:06:12 -0400603 clp->cl_rpcclient = clnt;
604 return 0;
605}
David Howells54ceac42006-08-22 20:06:13 -0400606
607/*
608 * Version 2 or 3 client destruction
609 */
610static void nfs_destroy_server(struct nfs_server *server)
611{
David Howells54ceac42006-08-22 20:06:13 -0400612 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500613 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400614}
615
616/*
617 * Version 2 or 3 lockd setup
618 */
619static int nfs_start_lockd(struct nfs_server *server)
620{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500621 struct nlm_host *host;
622 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500623 struct nlmclnt_initdata nlm_init = {
624 .hostname = clp->cl_hostname,
625 .address = (struct sockaddr *)&clp->cl_addr,
626 .addrlen = clp->cl_addrlen,
627 .protocol = server->flags & NFS_MOUNT_TCP ?
628 IPPROTO_TCP : IPPROTO_UDP,
629 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500630 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
631 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500632 };
David Howells54ceac42006-08-22 20:06:13 -0400633
Chuck Lever883bb162008-01-15 16:04:20 -0500634 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500635 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400636 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500637 return 0;
638
Chuck Lever883bb162008-01-15 16:04:20 -0500639 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500640 if (IS_ERR(host))
641 return PTR_ERR(host);
642
643 server->nlm_host = host;
644 server->destroy = nfs_destroy_server;
645 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400646}
647
648/*
649 * Initialise an NFSv3 ACL client connection
650 */
651#ifdef CONFIG_NFS_V3_ACL
652static void nfs_init_server_aclclient(struct nfs_server *server)
653{
Trond Myklebust40c553192007-12-14 14:56:07 -0500654 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400655 goto out_noacl;
656 if (server->flags & NFS_MOUNT_NOACL)
657 goto out_noacl;
658
659 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
660 if (IS_ERR(server->client_acl))
661 goto out_noacl;
662
663 /* No errors! Assume that Sun nfsacls are supported */
664 server->caps |= NFS_CAP_ACLS;
665 return;
666
667out_noacl:
668 server->caps &= ~NFS_CAP_ACLS;
669}
670#else
671static inline void nfs_init_server_aclclient(struct nfs_server *server)
672{
673 server->flags &= ~NFS_MOUNT_NOACL;
674 server->caps &= ~NFS_CAP_ACLS;
675}
676#endif
677
678/*
679 * Create a general RPC client
680 */
Trond Myklebust33170232007-12-20 16:03:59 -0500681static int nfs_init_server_rpcclient(struct nfs_server *server,
682 const struct rpc_timeout *timeo,
683 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400684{
685 struct nfs_client *clp = server->nfs_client;
686
687 server->client = rpc_clone_client(clp->cl_rpcclient);
688 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700689 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400690 return PTR_ERR(server->client);
691 }
692
Trond Myklebust33170232007-12-20 16:03:59 -0500693 memcpy(&server->client->cl_timeout_default,
694 timeo,
695 sizeof(server->client->cl_timeout_default));
696 server->client->cl_timeout = &server->client->cl_timeout_default;
697
David Howells54ceac42006-08-22 20:06:13 -0400698 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
699 struct rpc_auth *auth;
700
701 auth = rpcauth_create(pseudoflavour, server->client);
702 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700703 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400704 return PTR_ERR(auth);
705 }
706 }
707 server->client->cl_softrtry = 0;
708 if (server->flags & NFS_MOUNT_SOFT)
709 server->client->cl_softrtry = 1;
710
David Howells54ceac42006-08-22 20:06:13 -0400711 return 0;
712}
713
714/*
715 * Initialise an NFS2 or NFS3 client
716 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400717static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500718 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400719 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400720{
David Howells54ceac42006-08-22 20:06:13 -0400721 int error;
722
723 if (clp->cl_cons_state == NFS_CS_READY) {
724 /* the client is already initialised */
725 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
726 return 0;
727 }
728
David Howells54ceac42006-08-22 20:06:13 -0400729 /*
730 * Create a client RPC handle for doing FSSTAT with UNIX auth only
731 * - RFC 2623, sec 2.3.2
732 */
Chuck Leverd7403512008-12-23 15:21:37 -0500733 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
734 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400735 if (error < 0)
736 goto error;
737 nfs_mark_client_ready(clp, NFS_CS_READY);
738 return 0;
739
740error:
741 nfs_mark_client_ready(clp, error);
742 dprintk("<-- nfs_init_client() = xerror %d\n", error);
743 return error;
744}
745
746/*
747 * Create a version 2 or 3 client
748 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400749static int nfs_init_server(struct nfs_server *server,
750 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400751{
Trond Myklebust3a498022007-12-14 14:56:04 -0500752 struct nfs_client_initdata cl_init = {
753 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500754 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500755 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500756 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500757 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500758 };
Trond Myklebust33170232007-12-20 16:03:59 -0500759 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400760 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500761 int error;
David Howells54ceac42006-08-22 20:06:13 -0400762
763 dprintk("--> nfs_init_server()\n");
764
765#ifdef CONFIG_NFS_V3
766 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500767 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400768#endif
769
770 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500771 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400772 if (IS_ERR(clp)) {
773 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
774 return PTR_ERR(clp);
775 }
776
Trond Myklebust33170232007-12-20 16:03:59 -0500777 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
778 data->timeo, data->retrans);
779 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400780 if (error < 0)
781 goto error;
782
783 server->nfs_client = clp;
784
785 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400786 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100787 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400788
789 if (data->rsize)
790 server->rsize = nfs_block_size(data->rsize, NULL);
791 if (data->wsize)
792 server->wsize = nfs_block_size(data->wsize, NULL);
793
794 server->acregmin = data->acregmin * HZ;
795 server->acregmax = data->acregmax * HZ;
796 server->acdirmin = data->acdirmin * HZ;
797 server->acdirmax = data->acdirmax * HZ;
798
799 /* Start lockd here, before we might error out */
800 error = nfs_start_lockd(server);
801 if (error < 0)
802 goto error;
803
Chuck Leverf22d6d72008-03-14 14:10:22 -0400804 server->port = data->nfs_server.port;
805
Trond Myklebust33170232007-12-20 16:03:59 -0500806 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400807 if (error < 0)
808 goto error;
809
Chuck Lever3f8400d2008-03-14 14:10:30 -0400810 /* Preserve the values of mount_server-related mount options */
811 if (data->mount_server.addrlen) {
812 memcpy(&server->mountd_address, &data->mount_server.address,
813 data->mount_server.addrlen);
814 server->mountd_addrlen = data->mount_server.addrlen;
815 }
816 server->mountd_version = data->mount_server.version;
817 server->mountd_port = data->mount_server.port;
818 server->mountd_protocol = data->mount_server.protocol;
819
David Howells54ceac42006-08-22 20:06:13 -0400820 server->namelen = data->namlen;
821 /* Create a client RPC handle for the NFSv3 ACL management interface */
822 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400823 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
824 return 0;
825
826error:
827 server->nfs_client = NULL;
828 nfs_put_client(clp);
829 dprintk("<-- nfs_init_server() = xerror %d\n", error);
830 return error;
831}
832
833/*
834 * Load up the server record from information gained in an fsinfo record
835 */
836static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
837{
838 unsigned long max_rpc_payload;
839
840 /* Work out a lot of parameters */
841 if (server->rsize == 0)
842 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
843 if (server->wsize == 0)
844 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
845
846 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
847 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
848 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
849 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
850
851 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
852 if (server->rsize > max_rpc_payload)
853 server->rsize = max_rpc_payload;
854 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
855 server->rsize = NFS_MAX_FILE_IO_SIZE;
856 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700857
David Howells54ceac42006-08-22 20:06:13 -0400858 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
859
860 if (server->wsize > max_rpc_payload)
861 server->wsize = max_rpc_payload;
862 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
863 server->wsize = NFS_MAX_FILE_IO_SIZE;
864 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
865 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
866
867 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
868 if (server->dtsize > PAGE_CACHE_SIZE)
869 server->dtsize = PAGE_CACHE_SIZE;
870 if (server->dtsize > server->rsize)
871 server->dtsize = server->rsize;
872
873 if (server->flags & NFS_MOUNT_NOAC) {
874 server->acregmin = server->acregmax = 0;
875 server->acdirmin = server->acdirmax = 0;
876 }
877
878 server->maxfilesize = fsinfo->maxfilesize;
879
880 /* We're airborne Set socket buffersize */
881 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
882}
883
884/*
885 * Probe filesystem information, including the FSID on v2/v3
886 */
887static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
888{
889 struct nfs_fsinfo fsinfo;
890 struct nfs_client *clp = server->nfs_client;
891 int error;
892
893 dprintk("--> nfs_probe_fsinfo()\n");
894
895 if (clp->rpc_ops->set_capabilities != NULL) {
896 error = clp->rpc_ops->set_capabilities(server, mntfh);
897 if (error < 0)
898 goto out_error;
899 }
900
901 fsinfo.fattr = fattr;
902 nfs_fattr_init(fattr);
903 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
904 if (error < 0)
905 goto out_error;
906
907 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700908 error = bdi_init(&server->backing_dev_info);
909 if (error)
910 goto out_error;
911
David Howells54ceac42006-08-22 20:06:13 -0400912
913 /* Get some general file system info */
914 if (server->namelen == 0) {
915 struct nfs_pathconf pathinfo;
916
917 pathinfo.fattr = fattr;
918 nfs_fattr_init(fattr);
919
920 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
921 server->namelen = pathinfo.max_namelen;
922 }
923
924 dprintk("<-- nfs_probe_fsinfo() = 0\n");
925 return 0;
926
927out_error:
928 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
929 return error;
930}
931
932/*
933 * Copy useful information when duplicating a server record
934 */
935static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
936{
937 target->flags = source->flags;
938 target->acregmin = source->acregmin;
939 target->acregmax = source->acregmax;
940 target->acdirmin = source->acdirmin;
941 target->acdirmax = source->acdirmax;
942 target->caps = source->caps;
943}
944
945/*
946 * Allocate and initialise a server record
947 */
948static struct nfs_server *nfs_alloc_server(void)
949{
950 struct nfs_server *server;
951
952 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
953 if (!server)
954 return NULL;
955
956 server->client = server->client_acl = ERR_PTR(-EINVAL);
957
958 /* Zero out the NFS state stuff */
959 INIT_LIST_HEAD(&server->client_link);
960 INIT_LIST_HEAD(&server->master_link);
961
Steve Dicksonef818a22007-11-08 04:05:04 -0500962 atomic_set(&server->active, 0);
963
David Howells54ceac42006-08-22 20:06:13 -0400964 server->io_stats = nfs_alloc_iostats();
965 if (!server->io_stats) {
966 kfree(server);
967 return NULL;
968 }
969
970 return server;
971}
972
973/*
974 * Free up a server record
975 */
976void nfs_free_server(struct nfs_server *server)
977{
978 dprintk("--> nfs_free_server()\n");
979
980 spin_lock(&nfs_client_lock);
981 list_del(&server->client_link);
982 list_del(&server->master_link);
983 spin_unlock(&nfs_client_lock);
984
985 if (server->destroy != NULL)
986 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500987
988 if (!IS_ERR(server->client_acl))
989 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400990 if (!IS_ERR(server->client))
991 rpc_shutdown_client(server->client);
992
993 nfs_put_client(server->nfs_client);
994
995 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700996 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400997 kfree(server);
998 nfs_release_automount_timer();
999 dprintk("<-- nfs_free_server()\n");
1000}
1001
1002/*
1003 * Create a version 2 or 3 volume record
1004 * - keyed on server and FSID
1005 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001006struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001007 struct nfs_fh *mntfh)
1008{
1009 struct nfs_server *server;
1010 struct nfs_fattr fattr;
1011 int error;
1012
1013 server = nfs_alloc_server();
1014 if (!server)
1015 return ERR_PTR(-ENOMEM);
1016
1017 /* Get a client representation */
1018 error = nfs_init_server(server, data);
1019 if (error < 0)
1020 goto error;
1021
1022 BUG_ON(!server->nfs_client);
1023 BUG_ON(!server->nfs_client->rpc_ops);
1024 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1025
1026 /* Probe the root fh to retrieve its FSID */
1027 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1028 if (error < 0)
1029 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001030 if (server->nfs_client->rpc_ops->version == 3) {
1031 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1032 server->namelen = NFS3_MAXNAMLEN;
1033 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1034 server->caps |= NFS_CAP_READDIRPLUS;
1035 } else {
1036 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1037 server->namelen = NFS2_MAXNAMLEN;
1038 }
1039
David Howells54ceac42006-08-22 20:06:13 -04001040 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1041 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1042 if (error < 0) {
1043 dprintk("nfs_create_server: getattr error = %d\n", -error);
1044 goto error;
1045 }
1046 }
1047 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1048
David Howells6daabf12006-08-24 15:44:16 -04001049 dprintk("Server FSID: %llx:%llx\n",
1050 (unsigned long long) server->fsid.major,
1051 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001052
1053 BUG_ON(!server->nfs_client);
1054 BUG_ON(!server->nfs_client->rpc_ops);
1055 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1056
1057 spin_lock(&nfs_client_lock);
1058 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1059 list_add_tail(&server->master_link, &nfs_volume_list);
1060 spin_unlock(&nfs_client_lock);
1061
1062 server->mount_time = jiffies;
1063 return server;
1064
1065error:
1066 nfs_free_server(server);
1067 return ERR_PTR(error);
1068}
1069
1070#ifdef CONFIG_NFS_V4
1071/*
Andy Adamson557134a2009-04-01 09:21:53 -04001072 * Initialize the minor version specific parts of an NFS4 client record
1073 */
1074static int nfs4_init_client_minor_version(struct nfs_client *clp)
1075{
1076#if defined(CONFIG_NFS_V4_1)
1077 if (clp->cl_minorversion) {
1078 struct nfs4_session *session = NULL;
1079 /*
1080 * Create the session and mark it expired.
1081 * When a SEQUENCE operation encounters the expired session
1082 * it will do session recovery to initialize it.
1083 */
1084 session = nfs4_alloc_session(clp);
1085 if (!session)
1086 return -ENOMEM;
1087
1088 clp->cl_session = session;
1089 }
1090#endif /* CONFIG_NFS_V4_1 */
1091
1092 return 0;
1093}
1094
1095/*
David Howells54ceac42006-08-22 20:06:13 -04001096 * Initialise an NFS4 client record
1097 */
1098static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001099 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001100 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001101 rpc_authflavor_t authflavour,
1102 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001103{
1104 int error;
1105
1106 if (clp->cl_cons_state == NFS_CS_READY) {
1107 /* the client is initialised already */
1108 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1109 return 0;
1110 }
1111
1112 /* Check NFS protocol revision and initialize RPC op vector */
1113 clp->rpc_ops = &nfs_v4_clientops;
1114
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001115 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001116 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001117 if (error < 0)
1118 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001119 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001120
1121 error = nfs_idmap_new(clp);
1122 if (error < 0) {
1123 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001124 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001125 goto error;
1126 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001127 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001128
Andy Adamson557134a2009-04-01 09:21:53 -04001129 error = nfs4_init_client_minor_version(clp);
1130 if (error < 0)
1131 goto error;
1132
David Howells54ceac42006-08-22 20:06:13 -04001133 nfs_mark_client_ready(clp, NFS_CS_READY);
1134 return 0;
1135
1136error:
1137 nfs_mark_client_ready(clp, error);
1138 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1139 return error;
1140}
1141
1142/*
1143 * Set up an NFS4 client
1144 */
1145static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001146 const char *hostname,
1147 const struct sockaddr *addr,
1148 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001149 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001150 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001151 int proto, const struct rpc_timeout *timeparms,
1152 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001153{
Trond Myklebust3a498022007-12-14 14:56:04 -05001154 struct nfs_client_initdata cl_init = {
1155 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001156 .addr = addr,
1157 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001158 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001159 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001160 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001161 };
David Howells54ceac42006-08-22 20:06:13 -04001162 struct nfs_client *clp;
1163 int error;
1164
1165 dprintk("--> nfs4_set_client()\n");
1166
1167 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001168 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001169 if (IS_ERR(clp)) {
1170 error = PTR_ERR(clp);
1171 goto error;
1172 }
Chuck Leverd7403512008-12-23 15:21:37 -05001173 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1174 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001175 if (error < 0)
1176 goto error_put;
1177
1178 server->nfs_client = clp;
1179 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1180 return 0;
1181
1182error_put:
1183 nfs_put_client(clp);
1184error:
1185 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1186 return error;
1187}
1188
1189/*
Andy Adamson557134a2009-04-01 09:21:53 -04001190 * Initialize a session.
1191 * Note: save the mount rsize and wsize for create_server negotiation.
1192 */
1193static void nfs4_init_session(struct nfs_client *clp,
1194 unsigned int wsize, unsigned int rsize)
1195{
1196#if defined(CONFIG_NFS_V4_1)
1197 if (nfs4_has_session(clp)) {
1198 clp->cl_session->fc_attrs.max_rqst_sz = wsize;
1199 clp->cl_session->fc_attrs.max_resp_sz = rsize;
1200 }
1201#endif /* CONFIG_NFS_V4_1 */
1202}
1203
1204/*
David Howells54ceac42006-08-22 20:06:13 -04001205 * Create a version 4 volume record
1206 */
1207static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001208 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001209{
Trond Myklebust33170232007-12-20 16:03:59 -05001210 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001211 int error;
1212
1213 dprintk("--> nfs4_init_server()\n");
1214
Trond Myklebust33170232007-12-20 16:03:59 -05001215 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1216 data->timeo, data->retrans);
1217
Chuck Lever542fcc32008-12-23 15:21:36 -05001218 /* Initialise the client representation from the mount data */
1219 server->flags = data->flags;
1220 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001221 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001222
Trond Myklebust33170232007-12-20 16:03:59 -05001223 /* Get a client record */
1224 error = nfs4_set_client(server,
1225 data->nfs_server.hostname,
1226 (const struct sockaddr *)&data->nfs_server.address,
1227 data->nfs_server.addrlen,
1228 data->client_address,
1229 data->auth_flavors[0],
1230 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001231 &timeparms,
1232 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001233 if (error < 0)
1234 goto error;
1235
David Howells54ceac42006-08-22 20:06:13 -04001236 if (data->rsize)
1237 server->rsize = nfs_block_size(data->rsize, NULL);
1238 if (data->wsize)
1239 server->wsize = nfs_block_size(data->wsize, NULL);
1240
1241 server->acregmin = data->acregmin * HZ;
1242 server->acregmax = data->acregmax * HZ;
1243 server->acdirmin = data->acdirmin * HZ;
1244 server->acdirmax = data->acdirmax * HZ;
1245
Chuck Leverf22d6d72008-03-14 14:10:22 -04001246 server->port = data->nfs_server.port;
1247
Trond Myklebust33170232007-12-20 16:03:59 -05001248 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001249
Trond Myklebust33170232007-12-20 16:03:59 -05001250error:
David Howells54ceac42006-08-22 20:06:13 -04001251 /* Done */
1252 dprintk("<-- nfs4_init_server() = %d\n", error);
1253 return error;
1254}
1255
1256/*
1257 * Create a version 4 volume record
1258 * - keyed on server and FSID
1259 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001260struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001261 struct nfs_fh *mntfh)
1262{
1263 struct nfs_fattr fattr;
1264 struct nfs_server *server;
1265 int error;
1266
1267 dprintk("--> nfs4_create_server()\n");
1268
1269 server = nfs_alloc_server();
1270 if (!server)
1271 return ERR_PTR(-ENOMEM);
1272
David Howells54ceac42006-08-22 20:06:13 -04001273 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001274 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001275 if (error < 0)
1276 goto error;
1277
1278 BUG_ON(!server->nfs_client);
1279 BUG_ON(!server->nfs_client->rpc_ops);
1280 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1281
Andy Adamson557134a2009-04-01 09:21:53 -04001282 nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
1283
David Howells54ceac42006-08-22 20:06:13 -04001284 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001285 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001286 if (error < 0)
1287 goto error;
1288
David Howells6daabf12006-08-24 15:44:16 -04001289 dprintk("Server FSID: %llx:%llx\n",
1290 (unsigned long long) server->fsid.major,
1291 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001292 dprintk("Mount FH: %d\n", mntfh->size);
1293
1294 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1295 if (error < 0)
1296 goto error;
1297
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001298 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1299 server->namelen = NFS4_MAXNAMLEN;
1300
David Howells54ceac42006-08-22 20:06:13 -04001301 BUG_ON(!server->nfs_client);
1302 BUG_ON(!server->nfs_client->rpc_ops);
1303 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1304
1305 spin_lock(&nfs_client_lock);
1306 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1307 list_add_tail(&server->master_link, &nfs_volume_list);
1308 spin_unlock(&nfs_client_lock);
1309
1310 server->mount_time = jiffies;
1311 dprintk("<-- nfs4_create_server() = %p\n", server);
1312 return server;
1313
1314error:
1315 nfs_free_server(server);
1316 dprintk("<-- nfs4_create_server() = error %d\n", error);
1317 return ERR_PTR(error);
1318}
1319
1320/*
1321 * Create an NFS4 referral server record
1322 */
1323struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001324 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001325{
1326 struct nfs_client *parent_client;
1327 struct nfs_server *server, *parent_server;
1328 struct nfs_fattr fattr;
1329 int error;
1330
1331 dprintk("--> nfs4_create_referral_server()\n");
1332
1333 server = nfs_alloc_server();
1334 if (!server)
1335 return ERR_PTR(-ENOMEM);
1336
1337 parent_server = NFS_SB(data->sb);
1338 parent_client = parent_server->nfs_client;
1339
Chuck Lever542fcc32008-12-23 15:21:36 -05001340 /* Initialise the client representation from the parent server */
1341 nfs_server_copy_userdata(server, parent_server);
1342 server->caps |= NFS_CAP_ATOMIC_OPEN;
1343
David Howells54ceac42006-08-22 20:06:13 -04001344 /* Get a client representation.
1345 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001346 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001347 data->addr,
1348 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001349 parent_client->cl_ipaddr,
1350 data->authflavor,
1351 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001352 parent_server->client->cl_timeout,
1353 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001354 if (error < 0)
1355 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001356
Trond Myklebust33170232007-12-20 16:03:59 -05001357 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001358 if (error < 0)
1359 goto error;
1360
1361 BUG_ON(!server->nfs_client);
1362 BUG_ON(!server->nfs_client->rpc_ops);
1363 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1364
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001365 /* Probe the root fh to retrieve its FSID and filehandle */
1366 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1367 if (error < 0)
1368 goto error;
1369
David Howells54ceac42006-08-22 20:06:13 -04001370 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001371 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001372 if (error < 0)
1373 goto error;
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("Referral 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 spin_lock(&nfs_client_lock);
1383 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1384 list_add_tail(&server->master_link, &nfs_volume_list);
1385 spin_unlock(&nfs_client_lock);
1386
1387 server->mount_time = jiffies;
1388
1389 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1390 return server;
1391
1392error:
1393 nfs_free_server(server);
1394 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1395 return ERR_PTR(error);
1396}
1397
1398#endif /* CONFIG_NFS_V4 */
1399
1400/*
1401 * Clone an NFS2, NFS3 or NFS4 server record
1402 */
1403struct nfs_server *nfs_clone_server(struct nfs_server *source,
1404 struct nfs_fh *fh,
1405 struct nfs_fattr *fattr)
1406{
1407 struct nfs_server *server;
1408 struct nfs_fattr fattr_fsinfo;
1409 int error;
1410
1411 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001412 (unsigned long long) fattr->fsid.major,
1413 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001414
1415 server = nfs_alloc_server();
1416 if (!server)
1417 return ERR_PTR(-ENOMEM);
1418
1419 /* Copy data from the source */
1420 server->nfs_client = source->nfs_client;
1421 atomic_inc(&server->nfs_client->cl_count);
1422 nfs_server_copy_userdata(server, source);
1423
1424 server->fsid = fattr->fsid;
1425
Trond Myklebust33170232007-12-20 16:03:59 -05001426 error = nfs_init_server_rpcclient(server,
1427 source->client->cl_timeout,
1428 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001429 if (error < 0)
1430 goto out_free_server;
1431 if (!IS_ERR(source->client_acl))
1432 nfs_init_server_aclclient(server);
1433
1434 /* probe the filesystem info for this server filesystem */
1435 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1436 if (error < 0)
1437 goto out_free_server;
1438
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001439 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1440 server->namelen = NFS4_MAXNAMLEN;
1441
David Howells54ceac42006-08-22 20:06:13 -04001442 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001443 (unsigned long long) server->fsid.major,
1444 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001445
1446 error = nfs_start_lockd(server);
1447 if (error < 0)
1448 goto out_free_server;
1449
1450 spin_lock(&nfs_client_lock);
1451 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1452 list_add_tail(&server->master_link, &nfs_volume_list);
1453 spin_unlock(&nfs_client_lock);
1454
1455 server->mount_time = jiffies;
1456
1457 dprintk("<-- nfs_clone_server() = %p\n", server);
1458 return server;
1459
1460out_free_server:
1461 nfs_free_server(server);
1462 dprintk("<-- nfs_clone_server() = error %d\n", error);
1463 return ERR_PTR(error);
1464}
David Howells6aaca562006-08-22 20:06:13 -04001465
1466#ifdef CONFIG_PROC_FS
1467static struct proc_dir_entry *proc_fs_nfs;
1468
1469static int nfs_server_list_open(struct inode *inode, struct file *file);
1470static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1471static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1472static void nfs_server_list_stop(struct seq_file *p, void *v);
1473static int nfs_server_list_show(struct seq_file *m, void *v);
1474
1475static struct seq_operations nfs_server_list_ops = {
1476 .start = nfs_server_list_start,
1477 .next = nfs_server_list_next,
1478 .stop = nfs_server_list_stop,
1479 .show = nfs_server_list_show,
1480};
1481
Arjan van de Ven00977a52007-02-12 00:55:34 -08001482static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001483 .open = nfs_server_list_open,
1484 .read = seq_read,
1485 .llseek = seq_lseek,
1486 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001487 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001488};
1489
1490static int nfs_volume_list_open(struct inode *inode, struct file *file);
1491static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1492static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1493static void nfs_volume_list_stop(struct seq_file *p, void *v);
1494static int nfs_volume_list_show(struct seq_file *m, void *v);
1495
1496static struct seq_operations nfs_volume_list_ops = {
1497 .start = nfs_volume_list_start,
1498 .next = nfs_volume_list_next,
1499 .stop = nfs_volume_list_stop,
1500 .show = nfs_volume_list_show,
1501};
1502
Arjan van de Ven00977a52007-02-12 00:55:34 -08001503static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001504 .open = nfs_volume_list_open,
1505 .read = seq_read,
1506 .llseek = seq_lseek,
1507 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001508 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001509};
1510
1511/*
1512 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1513 * we're dealing
1514 */
1515static int nfs_server_list_open(struct inode *inode, struct file *file)
1516{
1517 struct seq_file *m;
1518 int ret;
1519
1520 ret = seq_open(file, &nfs_server_list_ops);
1521 if (ret < 0)
1522 return ret;
1523
1524 m = file->private_data;
1525 m->private = PDE(inode)->data;
1526
1527 return 0;
1528}
1529
1530/*
1531 * set up the iterator to start reading from the server list and return the first item
1532 */
1533static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1534{
David Howells6aaca562006-08-22 20:06:13 -04001535 /* lock the list against modification */
1536 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001537 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001538}
1539
1540/*
1541 * move to next server
1542 */
1543static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1544{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001545 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001546}
1547
1548/*
1549 * clean up after reading from the transports list
1550 */
1551static void nfs_server_list_stop(struct seq_file *p, void *v)
1552{
1553 spin_unlock(&nfs_client_lock);
1554}
1555
1556/*
1557 * display a header line followed by a load of call lines
1558 */
1559static int nfs_server_list_show(struct seq_file *m, void *v)
1560{
1561 struct nfs_client *clp;
1562
1563 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001564 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001565 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1566 return 0;
1567 }
1568
1569 /* display one transport per line on subsequent lines */
1570 clp = list_entry(v, struct nfs_client, cl_share_link);
1571
Chuck Lever5d8515c2007-12-10 14:57:16 -05001572 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001573 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001574 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1575 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001576 atomic_read(&clp->cl_count),
1577 clp->cl_hostname);
1578
1579 return 0;
1580}
1581
1582/*
1583 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1584 */
1585static int nfs_volume_list_open(struct inode *inode, struct file *file)
1586{
1587 struct seq_file *m;
1588 int ret;
1589
1590 ret = seq_open(file, &nfs_volume_list_ops);
1591 if (ret < 0)
1592 return ret;
1593
1594 m = file->private_data;
1595 m->private = PDE(inode)->data;
1596
1597 return 0;
1598}
1599
1600/*
1601 * set up the iterator to start reading from the volume list and return the first item
1602 */
1603static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1604{
David Howells6aaca562006-08-22 20:06:13 -04001605 /* lock the list against modification */
1606 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001607 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001608}
1609
1610/*
1611 * move to next volume
1612 */
1613static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1614{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001615 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001616}
1617
1618/*
1619 * clean up after reading from the transports list
1620 */
1621static void nfs_volume_list_stop(struct seq_file *p, void *v)
1622{
1623 spin_unlock(&nfs_client_lock);
1624}
1625
1626/*
1627 * display a header line followed by a load of call lines
1628 */
1629static int nfs_volume_list_show(struct seq_file *m, void *v)
1630{
1631 struct nfs_server *server;
1632 struct nfs_client *clp;
1633 char dev[8], fsid[17];
1634
1635 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001636 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001637 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001638 return 0;
1639 }
1640 /* display one transport per line on subsequent lines */
1641 server = list_entry(v, struct nfs_server, master_link);
1642 clp = server->nfs_client;
1643
1644 snprintf(dev, 8, "%u:%u",
1645 MAJOR(server->s_dev), MINOR(server->s_dev));
1646
1647 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001648 (unsigned long long) server->fsid.major,
1649 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001650
David Howells5d1acff2009-04-03 16:42:47 +01001651 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001652 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001653 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1654 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001655 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001656 fsid,
1657 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001658
1659 return 0;
1660}
1661
1662/*
1663 * initialise the /proc/fs/nfsfs/ directory
1664 */
1665int __init nfs_fs_proc_init(void)
1666{
1667 struct proc_dir_entry *p;
1668
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001669 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001670 if (!proc_fs_nfs)
1671 goto error_0;
1672
David Howells6aaca562006-08-22 20:06:13 -04001673 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001674 p = proc_create("servers", S_IFREG|S_IRUGO,
1675 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001676 if (!p)
1677 goto error_1;
1678
David Howells6aaca562006-08-22 20:06:13 -04001679 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001680 p = proc_create("volumes", S_IFREG|S_IRUGO,
1681 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001682 if (!p)
1683 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001684 return 0;
1685
1686error_2:
1687 remove_proc_entry("servers", proc_fs_nfs);
1688error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001689 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001690error_0:
1691 return -ENOMEM;
1692}
1693
1694/*
1695 * clean up the /proc/fs/nfsfs/ directory
1696 */
1697void nfs_fs_proc_exit(void)
1698{
1699 remove_proc_entry("volumes", proc_fs_nfs);
1700 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001701 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001702}
1703
1704#endif /* CONFIG_PROC_FS */