blob: a736160046c387f54219f82385472ee401e9829a [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/*
187 * Destroy a shared client record
188 */
189static void nfs_free_client(struct nfs_client *clp)
190{
Trond Myklebust40c553192007-12-14 14:56:07 -0500191 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400192
Trond Myklebust5dd31772006-08-24 01:03:05 -0400193 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400194
David Howells14727282009-04-03 16:42:42 +0100195 nfs_fscache_release_client_cookie(clp);
196
David Howells24c8dbb2006-08-22 20:06:10 -0400197 /* -EIO all pending I/O */
198 if (!IS_ERR(clp->cl_rpcclient))
199 rpc_shutdown_client(clp->cl_rpcclient);
200
201 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
202 nfs_callback_down();
203
Trond Myklebust7c67db32008-04-07 20:50:11 -0400204 if (clp->cl_machine_cred != NULL)
205 put_rpccred(clp->cl_machine_cred);
206
David Howells24c8dbb2006-08-22 20:06:10 -0400207 kfree(clp->cl_hostname);
208 kfree(clp);
209
210 dprintk("<-- nfs_free_client()\n");
211}
212
213/*
214 * Release a reference to a shared client record
215 */
216void nfs_put_client(struct nfs_client *clp)
217{
David Howells27ba8512006-07-30 14:40:56 -0400218 if (!clp)
219 return;
220
David Howells24c8dbb2006-08-22 20:06:10 -0400221 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
222
223 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
224 list_del(&clp->cl_share_link);
225 spin_unlock(&nfs_client_lock);
226
227 BUG_ON(!list_empty(&clp->cl_superblocks));
228
229 nfs_free_client(clp);
230 }
231}
232
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500233#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400234/*
235 * Test if two ip6 socket addresses refer to the same socket by
236 * comparing relevant fields. The padding bytes specifically, are not
237 * compared. sin6_flowinfo is not compared because it only affects QoS
238 * and sin6_scope_id is only compared if the address is "link local"
239 * because "link local" addresses need only be unique to a specific
240 * link. Conversely, ordinary unicast addresses might have different
241 * sin6_scope_id.
242 *
243 * The caller should ensure both socket addresses are AF_INET6.
244 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400245static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
246 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400247{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400248 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
249 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400250
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400251 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
252 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400253 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500254
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400255 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500256}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400257#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
258static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
259 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400260{
261 return 0;
262}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500263#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500264
David Howells24c8dbb2006-08-22 20:06:10 -0400265/*
Ian Dalld7371c42009-03-10 20:33:22 -0400266 * Test if two ip4 socket addresses refer to the same socket, by
267 * comparing relevant fields. The padding bytes specifically, are
268 * not compared.
269 *
270 * The caller should ensure both socket addresses are AF_INET.
271 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
273 const struct sockaddr *sa2)
274{
275 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
276 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
277
278 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
279}
280
281static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
282 const struct sockaddr *sa2)
283{
284 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
285 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
286
287 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
288 (sin1->sin6_port == sin2->sin6_port);
289}
290
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400291static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
292 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400293{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400294 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
295 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400296
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400297 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
298 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400299}
300
301/*
Ian Dalld7371c42009-03-10 20:33:22 -0400302 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400303 * by comparing (only) relevant fields, excluding the port number.
304 */
305static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
306 const struct sockaddr *sa2)
307{
308 if (sa1->sa_family != sa2->sa_family)
309 return 0;
310
311 switch (sa1->sa_family) {
312 case AF_INET:
313 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
314 case AF_INET6:
315 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
316 }
317 return 0;
318}
319
320/*
321 * Test if two socket addresses represent the same actual socket,
322 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400323 */
324static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
325 const struct sockaddr *sa2)
326{
327 if (sa1->sa_family != sa2->sa_family)
328 return 0;
329
330 switch (sa1->sa_family) {
331 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400332 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400333 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400334 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400335 }
336 return 0;
337}
338
339/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500340 * Find a client by IP address and protocol version
341 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400342 */
Chuck Leverff052642007-12-10 14:58:44 -0500343struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500344{
345 struct nfs_client *clp;
346
347 spin_lock(&nfs_client_lock);
348 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500349 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
350
Trond Myklebustc81468a2007-12-14 14:56:05 -0500351 /* Don't match clients that failed to initialise properly */
352 if (clp->cl_cons_state != NFS_CS_READY)
353 continue;
354
355 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500356 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500357 continue;
358
359 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500360 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500361 continue;
362
363 atomic_inc(&clp->cl_count);
364 spin_unlock(&nfs_client_lock);
365 return clp;
366 }
367 spin_unlock(&nfs_client_lock);
368 return NULL;
369}
370
371/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500372 * Find a client by IP address and protocol version
373 * - returns NULL if no such client
374 */
375struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
376{
377 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
378 u32 nfsvers = clp->rpc_ops->version;
379
380 spin_lock(&nfs_client_lock);
381 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
382 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
383
384 /* Don't match clients that failed to initialise properly */
385 if (clp->cl_cons_state != NFS_CS_READY)
386 continue;
387
388 /* Different NFS versions cannot share the same nfs_client */
389 if (clp->rpc_ops->version != nfsvers)
390 continue;
391
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500392 /* Match only the IP address, not the port number */
393 if (!nfs_sockaddr_match_ipaddr(sap, clap))
394 continue;
395
396 atomic_inc(&clp->cl_count);
397 spin_unlock(&nfs_client_lock);
398 return clp;
399 }
400 spin_unlock(&nfs_client_lock);
401 return NULL;
402}
403
404/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500405 * Find an nfs_client on the list that matches the initialisation data
406 * that is supplied.
407 */
408static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400409{
410 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400411 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400412
413 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400414 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700415 /* Don't match clients that failed to initialise properly */
416 if (clp->cl_cons_state < 0)
417 continue;
418
David Howells24c8dbb2006-08-22 20:06:10 -0400419 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500420 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400421 continue;
422
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500423 if (clp->cl_proto != data->proto)
424 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400425 /* Match nfsv4 minorversion */
426 if (clp->cl_minorversion != data->minorversion)
427 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500428 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400429 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400430 continue;
431
Trond Myklebustc81468a2007-12-14 14:56:05 -0500432 atomic_inc(&clp->cl_count);
433 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400434 }
David Howells24c8dbb2006-08-22 20:06:10 -0400435 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400436}
437
438/*
439 * Look up a client by IP address and protocol version
440 * - creates a new record if one doesn't yet exist
441 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500442static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400443{
444 struct nfs_client *clp, *new = NULL;
445 int error;
446
Chuck Leverd7422c42007-12-10 14:58:51 -0500447 dprintk("--> nfs_get_client(%s,v%u)\n",
448 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400449
450 /* see if the client already exists */
451 do {
452 spin_lock(&nfs_client_lock);
453
Trond Myklebustc81468a2007-12-14 14:56:05 -0500454 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400455 if (clp)
456 goto found_client;
457 if (new)
458 goto install_client;
459
460 spin_unlock(&nfs_client_lock);
461
Trond Myklebust3a498022007-12-14 14:56:04 -0500462 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400463 } while (new);
464
465 return ERR_PTR(-ENOMEM);
466
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,
Benny Halevy94a417f2009-04-01 09:21:49 -04001108 int proto, const struct rpc_timeout *timeparms,
1109 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001110{
Trond Myklebust3a498022007-12-14 14:56:04 -05001111 struct nfs_client_initdata cl_init = {
1112 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001113 .addr = addr,
1114 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001115 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001116 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001117 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001118 };
David Howells54ceac42006-08-22 20:06:13 -04001119 struct nfs_client *clp;
1120 int error;
1121
1122 dprintk("--> nfs4_set_client()\n");
1123
1124 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001125 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001126 if (IS_ERR(clp)) {
1127 error = PTR_ERR(clp);
1128 goto error;
1129 }
Chuck Leverd7403512008-12-23 15:21:37 -05001130 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1131 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001132 if (error < 0)
1133 goto error_put;
1134
1135 server->nfs_client = clp;
1136 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1137 return 0;
1138
1139error_put:
1140 nfs_put_client(clp);
1141error:
1142 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1143 return error;
1144}
1145
1146/*
1147 * Create a version 4 volume record
1148 */
1149static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001150 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001151{
Trond Myklebust33170232007-12-20 16:03:59 -05001152 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001153 int error;
1154
1155 dprintk("--> nfs4_init_server()\n");
1156
Trond Myklebust33170232007-12-20 16:03:59 -05001157 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1158 data->timeo, data->retrans);
1159
Chuck Lever542fcc32008-12-23 15:21:36 -05001160 /* Initialise the client representation from the mount data */
1161 server->flags = data->flags;
1162 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001163 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001164
Trond Myklebust33170232007-12-20 16:03:59 -05001165 /* Get a client record */
1166 error = nfs4_set_client(server,
1167 data->nfs_server.hostname,
1168 (const struct sockaddr *)&data->nfs_server.address,
1169 data->nfs_server.addrlen,
1170 data->client_address,
1171 data->auth_flavors[0],
1172 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001173 &timeparms,
1174 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001175 if (error < 0)
1176 goto error;
1177
David Howells54ceac42006-08-22 20:06:13 -04001178 if (data->rsize)
1179 server->rsize = nfs_block_size(data->rsize, NULL);
1180 if (data->wsize)
1181 server->wsize = nfs_block_size(data->wsize, NULL);
1182
1183 server->acregmin = data->acregmin * HZ;
1184 server->acregmax = data->acregmax * HZ;
1185 server->acdirmin = data->acdirmin * HZ;
1186 server->acdirmax = data->acdirmax * HZ;
1187
Chuck Leverf22d6d72008-03-14 14:10:22 -04001188 server->port = data->nfs_server.port;
1189
Trond Myklebust33170232007-12-20 16:03:59 -05001190 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001191
Trond Myklebust33170232007-12-20 16:03:59 -05001192error:
David Howells54ceac42006-08-22 20:06:13 -04001193 /* Done */
1194 dprintk("<-- nfs4_init_server() = %d\n", error);
1195 return error;
1196}
1197
1198/*
1199 * Create a version 4 volume record
1200 * - keyed on server and FSID
1201 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001202struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001203 struct nfs_fh *mntfh)
1204{
1205 struct nfs_fattr fattr;
1206 struct nfs_server *server;
1207 int error;
1208
1209 dprintk("--> nfs4_create_server()\n");
1210
1211 server = nfs_alloc_server();
1212 if (!server)
1213 return ERR_PTR(-ENOMEM);
1214
David Howells54ceac42006-08-22 20:06:13 -04001215 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001216 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001217 if (error < 0)
1218 goto error;
1219
1220 BUG_ON(!server->nfs_client);
1221 BUG_ON(!server->nfs_client->rpc_ops);
1222 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1223
1224 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001225 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001226 if (error < 0)
1227 goto error;
1228
David Howells6daabf12006-08-24 15:44:16 -04001229 dprintk("Server FSID: %llx:%llx\n",
1230 (unsigned long long) server->fsid.major,
1231 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001232 dprintk("Mount FH: %d\n", mntfh->size);
1233
1234 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1235 if (error < 0)
1236 goto error;
1237
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001238 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1239 server->namelen = NFS4_MAXNAMLEN;
1240
David Howells54ceac42006-08-22 20:06:13 -04001241 BUG_ON(!server->nfs_client);
1242 BUG_ON(!server->nfs_client->rpc_ops);
1243 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1244
1245 spin_lock(&nfs_client_lock);
1246 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1247 list_add_tail(&server->master_link, &nfs_volume_list);
1248 spin_unlock(&nfs_client_lock);
1249
1250 server->mount_time = jiffies;
1251 dprintk("<-- nfs4_create_server() = %p\n", server);
1252 return server;
1253
1254error:
1255 nfs_free_server(server);
1256 dprintk("<-- nfs4_create_server() = error %d\n", error);
1257 return ERR_PTR(error);
1258}
1259
1260/*
1261 * Create an NFS4 referral server record
1262 */
1263struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001264 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001265{
1266 struct nfs_client *parent_client;
1267 struct nfs_server *server, *parent_server;
1268 struct nfs_fattr fattr;
1269 int error;
1270
1271 dprintk("--> nfs4_create_referral_server()\n");
1272
1273 server = nfs_alloc_server();
1274 if (!server)
1275 return ERR_PTR(-ENOMEM);
1276
1277 parent_server = NFS_SB(data->sb);
1278 parent_client = parent_server->nfs_client;
1279
Chuck Lever542fcc32008-12-23 15:21:36 -05001280 /* Initialise the client representation from the parent server */
1281 nfs_server_copy_userdata(server, parent_server);
1282 server->caps |= NFS_CAP_ATOMIC_OPEN;
1283
David Howells54ceac42006-08-22 20:06:13 -04001284 /* Get a client representation.
1285 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001286 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001287 data->addr,
1288 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001289 parent_client->cl_ipaddr,
1290 data->authflavor,
1291 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001292 parent_server->client->cl_timeout,
1293 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001294 if (error < 0)
1295 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001296
Trond Myklebust33170232007-12-20 16:03:59 -05001297 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001298 if (error < 0)
1299 goto error;
1300
1301 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
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001305 /* Probe the root fh to retrieve its FSID and filehandle */
1306 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1307 if (error < 0)
1308 goto error;
1309
David Howells54ceac42006-08-22 20:06:13 -04001310 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001311 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001312 if (error < 0)
1313 goto error;
1314
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001315 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1316 server->namelen = NFS4_MAXNAMLEN;
1317
David Howells54ceac42006-08-22 20:06:13 -04001318 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001319 (unsigned long long) server->fsid.major,
1320 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001321
1322 spin_lock(&nfs_client_lock);
1323 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1324 list_add_tail(&server->master_link, &nfs_volume_list);
1325 spin_unlock(&nfs_client_lock);
1326
1327 server->mount_time = jiffies;
1328
1329 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1330 return server;
1331
1332error:
1333 nfs_free_server(server);
1334 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1335 return ERR_PTR(error);
1336}
1337
1338#endif /* CONFIG_NFS_V4 */
1339
1340/*
1341 * Clone an NFS2, NFS3 or NFS4 server record
1342 */
1343struct nfs_server *nfs_clone_server(struct nfs_server *source,
1344 struct nfs_fh *fh,
1345 struct nfs_fattr *fattr)
1346{
1347 struct nfs_server *server;
1348 struct nfs_fattr fattr_fsinfo;
1349 int error;
1350
1351 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001352 (unsigned long long) fattr->fsid.major,
1353 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001354
1355 server = nfs_alloc_server();
1356 if (!server)
1357 return ERR_PTR(-ENOMEM);
1358
1359 /* Copy data from the source */
1360 server->nfs_client = source->nfs_client;
1361 atomic_inc(&server->nfs_client->cl_count);
1362 nfs_server_copy_userdata(server, source);
1363
1364 server->fsid = fattr->fsid;
1365
Trond Myklebust33170232007-12-20 16:03:59 -05001366 error = nfs_init_server_rpcclient(server,
1367 source->client->cl_timeout,
1368 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001369 if (error < 0)
1370 goto out_free_server;
1371 if (!IS_ERR(source->client_acl))
1372 nfs_init_server_aclclient(server);
1373
1374 /* probe the filesystem info for this server filesystem */
1375 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1376 if (error < 0)
1377 goto out_free_server;
1378
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001379 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1380 server->namelen = NFS4_MAXNAMLEN;
1381
David Howells54ceac42006-08-22 20:06:13 -04001382 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001383 (unsigned long long) server->fsid.major,
1384 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001385
1386 error = nfs_start_lockd(server);
1387 if (error < 0)
1388 goto out_free_server;
1389
1390 spin_lock(&nfs_client_lock);
1391 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1392 list_add_tail(&server->master_link, &nfs_volume_list);
1393 spin_unlock(&nfs_client_lock);
1394
1395 server->mount_time = jiffies;
1396
1397 dprintk("<-- nfs_clone_server() = %p\n", server);
1398 return server;
1399
1400out_free_server:
1401 nfs_free_server(server);
1402 dprintk("<-- nfs_clone_server() = error %d\n", error);
1403 return ERR_PTR(error);
1404}
David Howells6aaca562006-08-22 20:06:13 -04001405
1406#ifdef CONFIG_PROC_FS
1407static struct proc_dir_entry *proc_fs_nfs;
1408
1409static int nfs_server_list_open(struct inode *inode, struct file *file);
1410static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1411static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1412static void nfs_server_list_stop(struct seq_file *p, void *v);
1413static int nfs_server_list_show(struct seq_file *m, void *v);
1414
1415static struct seq_operations nfs_server_list_ops = {
1416 .start = nfs_server_list_start,
1417 .next = nfs_server_list_next,
1418 .stop = nfs_server_list_stop,
1419 .show = nfs_server_list_show,
1420};
1421
Arjan van de Ven00977a52007-02-12 00:55:34 -08001422static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001423 .open = nfs_server_list_open,
1424 .read = seq_read,
1425 .llseek = seq_lseek,
1426 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001427 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001428};
1429
1430static int nfs_volume_list_open(struct inode *inode, struct file *file);
1431static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1432static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1433static void nfs_volume_list_stop(struct seq_file *p, void *v);
1434static int nfs_volume_list_show(struct seq_file *m, void *v);
1435
1436static struct seq_operations nfs_volume_list_ops = {
1437 .start = nfs_volume_list_start,
1438 .next = nfs_volume_list_next,
1439 .stop = nfs_volume_list_stop,
1440 .show = nfs_volume_list_show,
1441};
1442
Arjan van de Ven00977a52007-02-12 00:55:34 -08001443static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001444 .open = nfs_volume_list_open,
1445 .read = seq_read,
1446 .llseek = seq_lseek,
1447 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001448 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001449};
1450
1451/*
1452 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1453 * we're dealing
1454 */
1455static int nfs_server_list_open(struct inode *inode, struct file *file)
1456{
1457 struct seq_file *m;
1458 int ret;
1459
1460 ret = seq_open(file, &nfs_server_list_ops);
1461 if (ret < 0)
1462 return ret;
1463
1464 m = file->private_data;
1465 m->private = PDE(inode)->data;
1466
1467 return 0;
1468}
1469
1470/*
1471 * set up the iterator to start reading from the server list and return the first item
1472 */
1473static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1474{
David Howells6aaca562006-08-22 20:06:13 -04001475 /* lock the list against modification */
1476 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001477 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001478}
1479
1480/*
1481 * move to next server
1482 */
1483static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1484{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001485 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001486}
1487
1488/*
1489 * clean up after reading from the transports list
1490 */
1491static void nfs_server_list_stop(struct seq_file *p, void *v)
1492{
1493 spin_unlock(&nfs_client_lock);
1494}
1495
1496/*
1497 * display a header line followed by a load of call lines
1498 */
1499static int nfs_server_list_show(struct seq_file *m, void *v)
1500{
1501 struct nfs_client *clp;
1502
1503 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001504 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001505 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1506 return 0;
1507 }
1508
1509 /* display one transport per line on subsequent lines */
1510 clp = list_entry(v, struct nfs_client, cl_share_link);
1511
Chuck Lever5d8515c2007-12-10 14:57:16 -05001512 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001513 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001514 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1515 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001516 atomic_read(&clp->cl_count),
1517 clp->cl_hostname);
1518
1519 return 0;
1520}
1521
1522/*
1523 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1524 */
1525static int nfs_volume_list_open(struct inode *inode, struct file *file)
1526{
1527 struct seq_file *m;
1528 int ret;
1529
1530 ret = seq_open(file, &nfs_volume_list_ops);
1531 if (ret < 0)
1532 return ret;
1533
1534 m = file->private_data;
1535 m->private = PDE(inode)->data;
1536
1537 return 0;
1538}
1539
1540/*
1541 * set up the iterator to start reading from the volume list and return the first item
1542 */
1543static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1544{
David Howells6aaca562006-08-22 20:06:13 -04001545 /* lock the list against modification */
1546 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001547 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001548}
1549
1550/*
1551 * move to next volume
1552 */
1553static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1554{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001555 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001556}
1557
1558/*
1559 * clean up after reading from the transports list
1560 */
1561static void nfs_volume_list_stop(struct seq_file *p, void *v)
1562{
1563 spin_unlock(&nfs_client_lock);
1564}
1565
1566/*
1567 * display a header line followed by a load of call lines
1568 */
1569static int nfs_volume_list_show(struct seq_file *m, void *v)
1570{
1571 struct nfs_server *server;
1572 struct nfs_client *clp;
1573 char dev[8], fsid[17];
1574
1575 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001576 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001577 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001578 return 0;
1579 }
1580 /* display one transport per line on subsequent lines */
1581 server = list_entry(v, struct nfs_server, master_link);
1582 clp = server->nfs_client;
1583
1584 snprintf(dev, 8, "%u:%u",
1585 MAJOR(server->s_dev), MINOR(server->s_dev));
1586
1587 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001588 (unsigned long long) server->fsid.major,
1589 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001590
David Howells5d1acff2009-04-03 16:42:47 +01001591 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001592 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001593 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1594 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001595 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001596 fsid,
1597 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001598
1599 return 0;
1600}
1601
1602/*
1603 * initialise the /proc/fs/nfsfs/ directory
1604 */
1605int __init nfs_fs_proc_init(void)
1606{
1607 struct proc_dir_entry *p;
1608
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001609 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001610 if (!proc_fs_nfs)
1611 goto error_0;
1612
David Howells6aaca562006-08-22 20:06:13 -04001613 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001614 p = proc_create("servers", S_IFREG|S_IRUGO,
1615 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001616 if (!p)
1617 goto error_1;
1618
David Howells6aaca562006-08-22 20:06:13 -04001619 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001620 p = proc_create("volumes", S_IFREG|S_IRUGO,
1621 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001622 if (!p)
1623 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001624 return 0;
1625
1626error_2:
1627 remove_proc_entry("servers", proc_fs_nfs);
1628error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001629 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001630error_0:
1631 return -ENOMEM;
1632}
1633
1634/*
1635 * clean up the /proc/fs/nfsfs/ directory
1636 */
1637void nfs_fs_proc_exit(void)
1638{
1639 remove_proc_entry("volumes", proc_fs_nfs);
1640 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001641 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001642}
1643
1644#endif /* CONFIG_PROC_FS */