blob: 0efcb55c2caad0e880c8a3384165453dbbb52832 [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010048#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040049
50#define NFSDBG_FACILITY NFSDBG_CLIENT
51
52static DEFINE_SPINLOCK(nfs_client_lock);
53static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040054static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040055static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56
57/*
David Howells5006a762006-08-22 20:06:12 -040058 * RPC cruft for NFS
59 */
60static struct rpc_version *nfs_version[5] = {
61 [2] = &nfs_version2,
62#ifdef CONFIG_NFS_V3
63 [3] = &nfs_version3,
64#endif
65#ifdef CONFIG_NFS_V4
66 [4] = &nfs_version4,
67#endif
68};
69
70struct rpc_program nfs_program = {
71 .name = "nfs",
72 .number = NFS_PROGRAM,
73 .nrvers = ARRAY_SIZE(nfs_version),
74 .version = nfs_version,
75 .stats = &nfs_rpcstat,
76 .pipe_dir_name = "/nfs",
77};
78
79struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
81};
82
83
84#ifdef CONFIG_NFS_V3_ACL
85static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
86static struct rpc_version * nfsacl_version[] = {
87 [3] = &nfsacl_version3,
88};
89
90struct rpc_program nfsacl_program = {
91 .name = "nfsacl",
92 .number = NFS_ACL_PROGRAM,
93 .nrvers = ARRAY_SIZE(nfsacl_version),
94 .version = nfsacl_version,
95 .stats = &nfsacl_rpcstat,
96};
97#endif /* CONFIG_NFS_V3_ACL */
98
Trond Myklebust3a498022007-12-14 14:56:04 -050099struct nfs_client_initdata {
100 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500101 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500102 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500103 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500104 int proto;
Trond Myklebust3a498022007-12-14 14:56:04 -0500105};
106
David Howells5006a762006-08-22 20:06:12 -0400107/*
David Howells24c8dbb2006-08-22 20:06:10 -0400108 * Allocate a shared client record
109 *
110 * Since these are allocated/deallocated very rarely, we don't
111 * bother putting them in a slab cache...
112 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500113static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400114{
115 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400116 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400117
118 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
119 goto error_0;
120
Trond Myklebust40c553192007-12-14 14:56:07 -0500121 clp->rpc_ops = cl_init->rpc_ops;
122
123 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400124 if (nfs_callback_up() < 0)
125 goto error_2;
126 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
127 }
128
129 atomic_set(&clp->cl_count, 1);
130 clp->cl_cons_state = NFS_CS_INITING;
131
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500132 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
133 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400134
Trond Myklebust3a498022007-12-14 14:56:04 -0500135 if (cl_init->hostname) {
136 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400137 if (!clp->cl_hostname)
138 goto error_3;
139 }
140
141 INIT_LIST_HEAD(&clp->cl_superblocks);
142 clp->cl_rpcclient = ERR_PTR(-EINVAL);
143
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500144 clp->cl_proto = cl_init->proto;
145
David Howells24c8dbb2006-08-22 20:06:10 -0400146#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400147 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400148 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000149 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400150 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
151 clp->cl_boot_time = CURRENT_TIME;
152 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
153#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400154 cred = rpc_lookup_machine_cred();
155 if (!IS_ERR(cred))
156 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400157
David Howells14727282009-04-03 16:42:42 +0100158 nfs_fscache_get_client_cookie(clp);
159
David Howells24c8dbb2006-08-22 20:06:10 -0400160 return clp;
161
162error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400163 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
164 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400165error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400166 kfree(clp);
167error_0:
168 return NULL;
169}
170
Trond Myklebust5dd31772006-08-24 01:03:05 -0400171static void nfs4_shutdown_client(struct nfs_client *clp)
172{
173#ifdef CONFIG_NFS_V4
174 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
175 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400176 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400177 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
178 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500179
180 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400181#endif
182}
183
David Howells24c8dbb2006-08-22 20:06:10 -0400184/*
185 * Destroy a shared client record
186 */
187static void nfs_free_client(struct nfs_client *clp)
188{
Trond Myklebust40c553192007-12-14 14:56:07 -0500189 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400190
Trond Myklebust5dd31772006-08-24 01:03:05 -0400191 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400192
David Howells14727282009-04-03 16:42:42 +0100193 nfs_fscache_release_client_cookie(clp);
194
David Howells24c8dbb2006-08-22 20:06:10 -0400195 /* -EIO all pending I/O */
196 if (!IS_ERR(clp->cl_rpcclient))
197 rpc_shutdown_client(clp->cl_rpcclient);
198
199 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
200 nfs_callback_down();
201
Trond Myklebust7c67db32008-04-07 20:50:11 -0400202 if (clp->cl_machine_cred != NULL)
203 put_rpccred(clp->cl_machine_cred);
204
David Howells24c8dbb2006-08-22 20:06:10 -0400205 kfree(clp->cl_hostname);
206 kfree(clp);
207
208 dprintk("<-- nfs_free_client()\n");
209}
210
211/*
212 * Release a reference to a shared client record
213 */
214void nfs_put_client(struct nfs_client *clp)
215{
David Howells27ba8512006-07-30 14:40:56 -0400216 if (!clp)
217 return;
218
David Howells24c8dbb2006-08-22 20:06:10 -0400219 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
220
221 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
222 list_del(&clp->cl_share_link);
223 spin_unlock(&nfs_client_lock);
224
225 BUG_ON(!list_empty(&clp->cl_superblocks));
226
227 nfs_free_client(clp);
228 }
229}
230
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500231#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400232/*
233 * Test if two ip6 socket addresses refer to the same socket by
234 * comparing relevant fields. The padding bytes specifically, are not
235 * compared. sin6_flowinfo is not compared because it only affects QoS
236 * and sin6_scope_id is only compared if the address is "link local"
237 * because "link local" addresses need only be unique to a specific
238 * link. Conversely, ordinary unicast addresses might have different
239 * sin6_scope_id.
240 *
241 * The caller should ensure both socket addresses are AF_INET6.
242 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400243static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
244 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400245{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400246 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
247 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400248
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400249 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
250 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400251 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500252
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400253 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500254}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400255#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
256static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
257 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400258{
259 return 0;
260}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500261#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500262
David Howells24c8dbb2006-08-22 20:06:10 -0400263/*
Ian Dalld7371c42009-03-10 20:33:22 -0400264 * Test if two ip4 socket addresses refer to the same socket, by
265 * comparing relevant fields. The padding bytes specifically, are
266 * not compared.
267 *
268 * The caller should ensure both socket addresses are AF_INET.
269 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400270static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
271 const struct sockaddr *sa2)
272{
273 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
274 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
275
276 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
277}
278
279static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
280 const struct sockaddr *sa2)
281{
282 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
283 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
284
285 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
286 (sin1->sin6_port == sin2->sin6_port);
287}
288
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400289static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
290 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400291{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400292 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
293 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400294
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400295 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
296 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400297}
298
299/*
Ian Dalld7371c42009-03-10 20:33:22 -0400300 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400301 * by comparing (only) relevant fields, excluding the port number.
302 */
303static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
304 const struct sockaddr *sa2)
305{
306 if (sa1->sa_family != sa2->sa_family)
307 return 0;
308
309 switch (sa1->sa_family) {
310 case AF_INET:
311 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
312 case AF_INET6:
313 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
314 }
315 return 0;
316}
317
318/*
319 * Test if two socket addresses represent the same actual socket,
320 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400321 */
322static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
323 const struct sockaddr *sa2)
324{
325 if (sa1->sa_family != sa2->sa_family)
326 return 0;
327
328 switch (sa1->sa_family) {
329 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400330 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400331 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400332 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400333 }
334 return 0;
335}
336
337/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500338 * Find a client by IP address and protocol version
339 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400340 */
Chuck Leverff052642007-12-10 14:58:44 -0500341struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500342{
343 struct nfs_client *clp;
344
345 spin_lock(&nfs_client_lock);
346 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500347 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
348
Trond Myklebustc81468a2007-12-14 14:56:05 -0500349 /* Don't match clients that failed to initialise properly */
350 if (clp->cl_cons_state != NFS_CS_READY)
351 continue;
352
353 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500354 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500355 continue;
356
357 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500358 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500359 continue;
360
361 atomic_inc(&clp->cl_count);
362 spin_unlock(&nfs_client_lock);
363 return clp;
364 }
365 spin_unlock(&nfs_client_lock);
366 return NULL;
367}
368
369/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500370 * Find a client by IP address and protocol version
371 * - returns NULL if no such client
372 */
373struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
374{
375 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
376 u32 nfsvers = clp->rpc_ops->version;
377
378 spin_lock(&nfs_client_lock);
379 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
380 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
381
382 /* Don't match clients that failed to initialise properly */
383 if (clp->cl_cons_state != NFS_CS_READY)
384 continue;
385
386 /* Different NFS versions cannot share the same nfs_client */
387 if (clp->rpc_ops->version != nfsvers)
388 continue;
389
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500390 /* Match only the IP address, not the port number */
391 if (!nfs_sockaddr_match_ipaddr(sap, clap))
392 continue;
393
394 atomic_inc(&clp->cl_count);
395 spin_unlock(&nfs_client_lock);
396 return clp;
397 }
398 spin_unlock(&nfs_client_lock);
399 return NULL;
400}
401
402/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500403 * Find an nfs_client on the list that matches the initialisation data
404 * that is supplied.
405 */
406static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400407{
408 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400409 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400410
411 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400412 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700413 /* Don't match clients that failed to initialise properly */
414 if (clp->cl_cons_state < 0)
415 continue;
416
David Howells24c8dbb2006-08-22 20:06:10 -0400417 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500418 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400419 continue;
420
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500421 if (clp->cl_proto != data->proto)
422 continue;
423
Trond Myklebustc81468a2007-12-14 14:56:05 -0500424 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400425 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400426 continue;
427
Trond Myklebustc81468a2007-12-14 14:56:05 -0500428 atomic_inc(&clp->cl_count);
429 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400430 }
David Howells24c8dbb2006-08-22 20:06:10 -0400431 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400432}
433
434/*
435 * Look up a client by IP address and protocol version
436 * - creates a new record if one doesn't yet exist
437 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500438static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400439{
440 struct nfs_client *clp, *new = NULL;
441 int error;
442
Chuck Leverd7422c42007-12-10 14:58:51 -0500443 dprintk("--> nfs_get_client(%s,v%u)\n",
444 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400445
446 /* see if the client already exists */
447 do {
448 spin_lock(&nfs_client_lock);
449
Trond Myklebustc81468a2007-12-14 14:56:05 -0500450 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400451 if (clp)
452 goto found_client;
453 if (new)
454 goto install_client;
455
456 spin_unlock(&nfs_client_lock);
457
Trond Myklebust3a498022007-12-14 14:56:04 -0500458 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400459 } while (new);
460
461 return ERR_PTR(-ENOMEM);
462
463 /* install a new client and return with it unready */
464install_client:
465 clp = new;
466 list_add(&clp->cl_share_link, &nfs_client_list);
467 spin_unlock(&nfs_client_lock);
468 dprintk("--> nfs_get_client() = %p [new]\n", clp);
469 return clp;
470
471 /* found an existing client
472 * - make sure it's ready before returning
473 */
474found_client:
475 spin_unlock(&nfs_client_lock);
476
477 if (new)
478 nfs_free_client(new);
479
Matthew Wilcox150030b2007-12-06 16:24:39 -0500480 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400481 clp->cl_cons_state != NFS_CS_INITING);
482 if (error < 0) {
483 nfs_put_client(clp);
484 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400485 }
486
487 if (clp->cl_cons_state < NFS_CS_READY) {
488 error = clp->cl_cons_state;
489 nfs_put_client(clp);
490 return ERR_PTR(error);
491 }
492
David Howells54ceac42006-08-22 20:06:13 -0400493 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
494
David Howells24c8dbb2006-08-22 20:06:10 -0400495 dprintk("--> nfs_get_client() = %p [share]\n", clp);
496 return clp;
497}
498
499/*
500 * Mark a server as ready or failed
501 */
David Howells54ceac42006-08-22 20:06:13 -0400502static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400503{
504 clp->cl_cons_state = state;
505 wake_up_all(&nfs_client_active_wq);
506}
David Howells5006a762006-08-22 20:06:12 -0400507
508/*
509 * Initialise the timeout values for a connection
510 */
511static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
512 unsigned int timeo, unsigned int retrans)
513{
514 to->to_initval = timeo * HZ / 10;
515 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400516
517 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400518 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400519 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400520 if (to->to_retries == 0)
521 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500522 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400523 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400524 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
525 to->to_initval = NFS_MAX_TCP_TIMEOUT;
526 to->to_increment = to->to_initval;
527 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500528 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
529 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
530 if (to->to_maxval < to->to_initval)
531 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400532 to->to_exponential = 0;
533 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400534 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400535 if (to->to_retries == 0)
536 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400537 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400538 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400539 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
540 to->to_initval = NFS_MAX_UDP_TIMEOUT;
541 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
542 to->to_exponential = 1;
543 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400544 default:
545 BUG();
David Howells5006a762006-08-22 20:06:12 -0400546 }
547}
548
549/*
550 * Create an RPC client handle
551 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500552static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500553 const struct rpc_timeout *timeparms,
554 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500555 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400556{
David Howells5006a762006-08-22 20:06:12 -0400557 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400558 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500559 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400560 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500561 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500562 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400563 .servername = clp->cl_hostname,
564 .program = &nfs_program,
565 .version = clp->rpc_ops->version,
566 .authflavor = flavor,
567 };
David Howells5006a762006-08-22 20:06:12 -0400568
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500569 if (discrtry)
570 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
571 if (noresvport)
572 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
573
David Howells5006a762006-08-22 20:06:12 -0400574 if (!IS_ERR(clp->cl_rpcclient))
575 return 0;
576
Chuck Lever41877d22006-08-22 20:06:20 -0400577 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400578 if (IS_ERR(clnt)) {
579 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700580 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400581 return PTR_ERR(clnt);
582 }
583
David Howells5006a762006-08-22 20:06:12 -0400584 clp->cl_rpcclient = clnt;
585 return 0;
586}
David Howells54ceac42006-08-22 20:06:13 -0400587
588/*
589 * Version 2 or 3 client destruction
590 */
591static void nfs_destroy_server(struct nfs_server *server)
592{
David Howells54ceac42006-08-22 20:06:13 -0400593 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500594 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400595}
596
597/*
598 * Version 2 or 3 lockd setup
599 */
600static int nfs_start_lockd(struct nfs_server *server)
601{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500602 struct nlm_host *host;
603 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500604 struct nlmclnt_initdata nlm_init = {
605 .hostname = clp->cl_hostname,
606 .address = (struct sockaddr *)&clp->cl_addr,
607 .addrlen = clp->cl_addrlen,
608 .protocol = server->flags & NFS_MOUNT_TCP ?
609 IPPROTO_TCP : IPPROTO_UDP,
610 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500611 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
612 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500613 };
David Howells54ceac42006-08-22 20:06:13 -0400614
Chuck Lever883bb162008-01-15 16:04:20 -0500615 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500616 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400617 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500618 return 0;
619
Chuck Lever883bb162008-01-15 16:04:20 -0500620 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500621 if (IS_ERR(host))
622 return PTR_ERR(host);
623
624 server->nlm_host = host;
625 server->destroy = nfs_destroy_server;
626 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400627}
628
629/*
630 * Initialise an NFSv3 ACL client connection
631 */
632#ifdef CONFIG_NFS_V3_ACL
633static void nfs_init_server_aclclient(struct nfs_server *server)
634{
Trond Myklebust40c553192007-12-14 14:56:07 -0500635 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400636 goto out_noacl;
637 if (server->flags & NFS_MOUNT_NOACL)
638 goto out_noacl;
639
640 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
641 if (IS_ERR(server->client_acl))
642 goto out_noacl;
643
644 /* No errors! Assume that Sun nfsacls are supported */
645 server->caps |= NFS_CAP_ACLS;
646 return;
647
648out_noacl:
649 server->caps &= ~NFS_CAP_ACLS;
650}
651#else
652static inline void nfs_init_server_aclclient(struct nfs_server *server)
653{
654 server->flags &= ~NFS_MOUNT_NOACL;
655 server->caps &= ~NFS_CAP_ACLS;
656}
657#endif
658
659/*
660 * Create a general RPC client
661 */
Trond Myklebust33170232007-12-20 16:03:59 -0500662static int nfs_init_server_rpcclient(struct nfs_server *server,
663 const struct rpc_timeout *timeo,
664 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400665{
666 struct nfs_client *clp = server->nfs_client;
667
668 server->client = rpc_clone_client(clp->cl_rpcclient);
669 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700670 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400671 return PTR_ERR(server->client);
672 }
673
Trond Myklebust33170232007-12-20 16:03:59 -0500674 memcpy(&server->client->cl_timeout_default,
675 timeo,
676 sizeof(server->client->cl_timeout_default));
677 server->client->cl_timeout = &server->client->cl_timeout_default;
678
David Howells54ceac42006-08-22 20:06:13 -0400679 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
680 struct rpc_auth *auth;
681
682 auth = rpcauth_create(pseudoflavour, server->client);
683 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700684 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400685 return PTR_ERR(auth);
686 }
687 }
688 server->client->cl_softrtry = 0;
689 if (server->flags & NFS_MOUNT_SOFT)
690 server->client->cl_softrtry = 1;
691
David Howells54ceac42006-08-22 20:06:13 -0400692 return 0;
693}
694
695/*
696 * Initialise an NFS2 or NFS3 client
697 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400698static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500699 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400700 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400701{
David Howells54ceac42006-08-22 20:06:13 -0400702 int error;
703
704 if (clp->cl_cons_state == NFS_CS_READY) {
705 /* the client is already initialised */
706 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
707 return 0;
708 }
709
David Howells54ceac42006-08-22 20:06:13 -0400710 /*
711 * Create a client RPC handle for doing FSSTAT with UNIX auth only
712 * - RFC 2623, sec 2.3.2
713 */
Chuck Leverd7403512008-12-23 15:21:37 -0500714 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
715 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400716 if (error < 0)
717 goto error;
718 nfs_mark_client_ready(clp, NFS_CS_READY);
719 return 0;
720
721error:
722 nfs_mark_client_ready(clp, error);
723 dprintk("<-- nfs_init_client() = xerror %d\n", error);
724 return error;
725}
726
727/*
728 * Create a version 2 or 3 client
729 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400730static int nfs_init_server(struct nfs_server *server,
731 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400732{
Trond Myklebust3a498022007-12-14 14:56:04 -0500733 struct nfs_client_initdata cl_init = {
734 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500735 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500736 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500737 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500738 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500739 };
Trond Myklebust33170232007-12-20 16:03:59 -0500740 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400741 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500742 int error;
David Howells54ceac42006-08-22 20:06:13 -0400743
744 dprintk("--> nfs_init_server()\n");
745
746#ifdef CONFIG_NFS_V3
747 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500748 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400749#endif
750
751 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500752 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400753 if (IS_ERR(clp)) {
754 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
755 return PTR_ERR(clp);
756 }
757
Trond Myklebust33170232007-12-20 16:03:59 -0500758 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
759 data->timeo, data->retrans);
760 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400761 if (error < 0)
762 goto error;
763
764 server->nfs_client = clp;
765
766 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400767 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100768 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400769
770 if (data->rsize)
771 server->rsize = nfs_block_size(data->rsize, NULL);
772 if (data->wsize)
773 server->wsize = nfs_block_size(data->wsize, NULL);
774
775 server->acregmin = data->acregmin * HZ;
776 server->acregmax = data->acregmax * HZ;
777 server->acdirmin = data->acdirmin * HZ;
778 server->acdirmax = data->acdirmax * HZ;
779
780 /* Start lockd here, before we might error out */
781 error = nfs_start_lockd(server);
782 if (error < 0)
783 goto error;
784
Chuck Leverf22d6d72008-03-14 14:10:22 -0400785 server->port = data->nfs_server.port;
786
Trond Myklebust33170232007-12-20 16:03:59 -0500787 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400788 if (error < 0)
789 goto error;
790
Chuck Lever3f8400d2008-03-14 14:10:30 -0400791 /* Preserve the values of mount_server-related mount options */
792 if (data->mount_server.addrlen) {
793 memcpy(&server->mountd_address, &data->mount_server.address,
794 data->mount_server.addrlen);
795 server->mountd_addrlen = data->mount_server.addrlen;
796 }
797 server->mountd_version = data->mount_server.version;
798 server->mountd_port = data->mount_server.port;
799 server->mountd_protocol = data->mount_server.protocol;
800
David Howells54ceac42006-08-22 20:06:13 -0400801 server->namelen = data->namlen;
802 /* Create a client RPC handle for the NFSv3 ACL management interface */
803 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400804 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
805 return 0;
806
807error:
808 server->nfs_client = NULL;
809 nfs_put_client(clp);
810 dprintk("<-- nfs_init_server() = xerror %d\n", error);
811 return error;
812}
813
814/*
815 * Load up the server record from information gained in an fsinfo record
816 */
817static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
818{
819 unsigned long max_rpc_payload;
820
821 /* Work out a lot of parameters */
822 if (server->rsize == 0)
823 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
824 if (server->wsize == 0)
825 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
826
827 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
828 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
829 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
830 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
831
832 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
833 if (server->rsize > max_rpc_payload)
834 server->rsize = max_rpc_payload;
835 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
836 server->rsize = NFS_MAX_FILE_IO_SIZE;
837 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700838
David Howells54ceac42006-08-22 20:06:13 -0400839 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
840
841 if (server->wsize > max_rpc_payload)
842 server->wsize = max_rpc_payload;
843 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
844 server->wsize = NFS_MAX_FILE_IO_SIZE;
845 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
846 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
847
848 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
849 if (server->dtsize > PAGE_CACHE_SIZE)
850 server->dtsize = PAGE_CACHE_SIZE;
851 if (server->dtsize > server->rsize)
852 server->dtsize = server->rsize;
853
854 if (server->flags & NFS_MOUNT_NOAC) {
855 server->acregmin = server->acregmax = 0;
856 server->acdirmin = server->acdirmax = 0;
857 }
858
859 server->maxfilesize = fsinfo->maxfilesize;
860
861 /* We're airborne Set socket buffersize */
862 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
863}
864
865/*
866 * Probe filesystem information, including the FSID on v2/v3
867 */
868static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
869{
870 struct nfs_fsinfo fsinfo;
871 struct nfs_client *clp = server->nfs_client;
872 int error;
873
874 dprintk("--> nfs_probe_fsinfo()\n");
875
876 if (clp->rpc_ops->set_capabilities != NULL) {
877 error = clp->rpc_ops->set_capabilities(server, mntfh);
878 if (error < 0)
879 goto out_error;
880 }
881
882 fsinfo.fattr = fattr;
883 nfs_fattr_init(fattr);
884 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
885 if (error < 0)
886 goto out_error;
887
888 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700889 error = bdi_init(&server->backing_dev_info);
890 if (error)
891 goto out_error;
892
David Howells54ceac42006-08-22 20:06:13 -0400893
894 /* Get some general file system info */
895 if (server->namelen == 0) {
896 struct nfs_pathconf pathinfo;
897
898 pathinfo.fattr = fattr;
899 nfs_fattr_init(fattr);
900
901 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
902 server->namelen = pathinfo.max_namelen;
903 }
904
905 dprintk("<-- nfs_probe_fsinfo() = 0\n");
906 return 0;
907
908out_error:
909 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
910 return error;
911}
912
913/*
914 * Copy useful information when duplicating a server record
915 */
916static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
917{
918 target->flags = source->flags;
919 target->acregmin = source->acregmin;
920 target->acregmax = source->acregmax;
921 target->acdirmin = source->acdirmin;
922 target->acdirmax = source->acdirmax;
923 target->caps = source->caps;
924}
925
926/*
927 * Allocate and initialise a server record
928 */
929static struct nfs_server *nfs_alloc_server(void)
930{
931 struct nfs_server *server;
932
933 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
934 if (!server)
935 return NULL;
936
937 server->client = server->client_acl = ERR_PTR(-EINVAL);
938
939 /* Zero out the NFS state stuff */
940 INIT_LIST_HEAD(&server->client_link);
941 INIT_LIST_HEAD(&server->master_link);
942
Steve Dicksonef818a22007-11-08 04:05:04 -0500943 atomic_set(&server->active, 0);
944
David Howells54ceac42006-08-22 20:06:13 -0400945 server->io_stats = nfs_alloc_iostats();
946 if (!server->io_stats) {
947 kfree(server);
948 return NULL;
949 }
950
951 return server;
952}
953
954/*
955 * Free up a server record
956 */
957void nfs_free_server(struct nfs_server *server)
958{
959 dprintk("--> nfs_free_server()\n");
960
961 spin_lock(&nfs_client_lock);
962 list_del(&server->client_link);
963 list_del(&server->master_link);
964 spin_unlock(&nfs_client_lock);
965
966 if (server->destroy != NULL)
967 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500968
969 if (!IS_ERR(server->client_acl))
970 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400971 if (!IS_ERR(server->client))
972 rpc_shutdown_client(server->client);
973
974 nfs_put_client(server->nfs_client);
975
976 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700977 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400978 kfree(server);
979 nfs_release_automount_timer();
980 dprintk("<-- nfs_free_server()\n");
981}
982
983/*
984 * Create a version 2 or 3 volume record
985 * - keyed on server and FSID
986 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400987struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400988 struct nfs_fh *mntfh)
989{
990 struct nfs_server *server;
991 struct nfs_fattr fattr;
992 int error;
993
994 server = nfs_alloc_server();
995 if (!server)
996 return ERR_PTR(-ENOMEM);
997
998 /* Get a client representation */
999 error = nfs_init_server(server, data);
1000 if (error < 0)
1001 goto error;
1002
1003 BUG_ON(!server->nfs_client);
1004 BUG_ON(!server->nfs_client->rpc_ops);
1005 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1006
1007 /* Probe the root fh to retrieve its FSID */
1008 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1009 if (error < 0)
1010 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001011 if (server->nfs_client->rpc_ops->version == 3) {
1012 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1013 server->namelen = NFS3_MAXNAMLEN;
1014 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1015 server->caps |= NFS_CAP_READDIRPLUS;
1016 } else {
1017 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1018 server->namelen = NFS2_MAXNAMLEN;
1019 }
1020
David Howells54ceac42006-08-22 20:06:13 -04001021 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1022 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1023 if (error < 0) {
1024 dprintk("nfs_create_server: getattr error = %d\n", -error);
1025 goto error;
1026 }
1027 }
1028 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1029
David Howells6daabf12006-08-24 15:44:16 -04001030 dprintk("Server FSID: %llx:%llx\n",
1031 (unsigned long long) server->fsid.major,
1032 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001033
1034 BUG_ON(!server->nfs_client);
1035 BUG_ON(!server->nfs_client->rpc_ops);
1036 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1037
1038 spin_lock(&nfs_client_lock);
1039 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1040 list_add_tail(&server->master_link, &nfs_volume_list);
1041 spin_unlock(&nfs_client_lock);
1042
1043 server->mount_time = jiffies;
1044 return server;
1045
1046error:
1047 nfs_free_server(server);
1048 return ERR_PTR(error);
1049}
1050
1051#ifdef CONFIG_NFS_V4
1052/*
1053 * Initialise an NFS4 client record
1054 */
1055static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001056 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001057 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001058 rpc_authflavor_t authflavour,
1059 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001060{
1061 int error;
1062
1063 if (clp->cl_cons_state == NFS_CS_READY) {
1064 /* the client is initialised already */
1065 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1066 return 0;
1067 }
1068
1069 /* Check NFS protocol revision and initialize RPC op vector */
1070 clp->rpc_ops = &nfs_v4_clientops;
1071
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001072 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001073 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001074 if (error < 0)
1075 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001076 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001077
1078 error = nfs_idmap_new(clp);
1079 if (error < 0) {
1080 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001081 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001082 goto error;
1083 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001084 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001085
1086 nfs_mark_client_ready(clp, NFS_CS_READY);
1087 return 0;
1088
1089error:
1090 nfs_mark_client_ready(clp, error);
1091 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1092 return error;
1093}
1094
1095/*
1096 * Set up an NFS4 client
1097 */
1098static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001099 const char *hostname,
1100 const struct sockaddr *addr,
1101 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001102 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001103 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001104 int proto, const struct rpc_timeout *timeparms,
1105 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001106{
Trond Myklebust3a498022007-12-14 14:56:04 -05001107 struct nfs_client_initdata cl_init = {
1108 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001109 .addr = addr,
1110 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001111 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001112 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -05001113 };
David Howells54ceac42006-08-22 20:06:13 -04001114 struct nfs_client *clp;
1115 int error;
1116
1117 dprintk("--> nfs4_set_client()\n");
1118
1119 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001120 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001121 if (IS_ERR(clp)) {
1122 error = PTR_ERR(clp);
1123 goto error;
1124 }
Chuck Leverd7403512008-12-23 15:21:37 -05001125 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1126 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001127 if (error < 0)
1128 goto error_put;
1129
1130 server->nfs_client = clp;
1131 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1132 return 0;
1133
1134error_put:
1135 nfs_put_client(clp);
1136error:
1137 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1138 return error;
1139}
1140
1141/*
1142 * Create a version 4 volume record
1143 */
1144static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001145 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001146{
Trond Myklebust33170232007-12-20 16:03:59 -05001147 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001148 int error;
1149
1150 dprintk("--> nfs4_init_server()\n");
1151
Trond Myklebust33170232007-12-20 16:03:59 -05001152 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1153 data->timeo, data->retrans);
1154
Chuck Lever542fcc32008-12-23 15:21:36 -05001155 /* Initialise the client representation from the mount data */
1156 server->flags = data->flags;
1157 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001158 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001159
Trond Myklebust33170232007-12-20 16:03:59 -05001160 /* Get a client record */
1161 error = nfs4_set_client(server,
1162 data->nfs_server.hostname,
1163 (const struct sockaddr *)&data->nfs_server.address,
1164 data->nfs_server.addrlen,
1165 data->client_address,
1166 data->auth_flavors[0],
1167 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001168 &timeparms,
1169 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001170 if (error < 0)
1171 goto error;
1172
David Howells54ceac42006-08-22 20:06:13 -04001173 if (data->rsize)
1174 server->rsize = nfs_block_size(data->rsize, NULL);
1175 if (data->wsize)
1176 server->wsize = nfs_block_size(data->wsize, NULL);
1177
1178 server->acregmin = data->acregmin * HZ;
1179 server->acregmax = data->acregmax * HZ;
1180 server->acdirmin = data->acdirmin * HZ;
1181 server->acdirmax = data->acdirmax * HZ;
1182
Chuck Leverf22d6d72008-03-14 14:10:22 -04001183 server->port = data->nfs_server.port;
1184
Trond Myklebust33170232007-12-20 16:03:59 -05001185 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001186
Trond Myklebust33170232007-12-20 16:03:59 -05001187error:
David Howells54ceac42006-08-22 20:06:13 -04001188 /* Done */
1189 dprintk("<-- nfs4_init_server() = %d\n", error);
1190 return error;
1191}
1192
1193/*
1194 * Create a version 4 volume record
1195 * - keyed on server and FSID
1196 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001197struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001198 struct nfs_fh *mntfh)
1199{
1200 struct nfs_fattr fattr;
1201 struct nfs_server *server;
1202 int error;
1203
1204 dprintk("--> nfs4_create_server()\n");
1205
1206 server = nfs_alloc_server();
1207 if (!server)
1208 return ERR_PTR(-ENOMEM);
1209
David Howells54ceac42006-08-22 20:06:13 -04001210 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001211 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001212 if (error < 0)
1213 goto error;
1214
1215 BUG_ON(!server->nfs_client);
1216 BUG_ON(!server->nfs_client->rpc_ops);
1217 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1218
1219 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001220 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001221 if (error < 0)
1222 goto error;
1223
David Howells6daabf12006-08-24 15:44:16 -04001224 dprintk("Server FSID: %llx:%llx\n",
1225 (unsigned long long) server->fsid.major,
1226 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001227 dprintk("Mount FH: %d\n", mntfh->size);
1228
1229 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1230 if (error < 0)
1231 goto error;
1232
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001233 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1234 server->namelen = NFS4_MAXNAMLEN;
1235
David Howells54ceac42006-08-22 20:06:13 -04001236 BUG_ON(!server->nfs_client);
1237 BUG_ON(!server->nfs_client->rpc_ops);
1238 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1239
1240 spin_lock(&nfs_client_lock);
1241 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1242 list_add_tail(&server->master_link, &nfs_volume_list);
1243 spin_unlock(&nfs_client_lock);
1244
1245 server->mount_time = jiffies;
1246 dprintk("<-- nfs4_create_server() = %p\n", server);
1247 return server;
1248
1249error:
1250 nfs_free_server(server);
1251 dprintk("<-- nfs4_create_server() = error %d\n", error);
1252 return ERR_PTR(error);
1253}
1254
1255/*
1256 * Create an NFS4 referral server record
1257 */
1258struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001259 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001260{
1261 struct nfs_client *parent_client;
1262 struct nfs_server *server, *parent_server;
1263 struct nfs_fattr fattr;
1264 int error;
1265
1266 dprintk("--> nfs4_create_referral_server()\n");
1267
1268 server = nfs_alloc_server();
1269 if (!server)
1270 return ERR_PTR(-ENOMEM);
1271
1272 parent_server = NFS_SB(data->sb);
1273 parent_client = parent_server->nfs_client;
1274
Chuck Lever542fcc32008-12-23 15:21:36 -05001275 /* Initialise the client representation from the parent server */
1276 nfs_server_copy_userdata(server, parent_server);
1277 server->caps |= NFS_CAP_ATOMIC_OPEN;
1278
David Howells54ceac42006-08-22 20:06:13 -04001279 /* Get a client representation.
1280 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001281 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001282 data->addr,
1283 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001284 parent_client->cl_ipaddr,
1285 data->authflavor,
1286 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001287 parent_server->client->cl_timeout,
1288 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001289 if (error < 0)
1290 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001291
Trond Myklebust33170232007-12-20 16:03:59 -05001292 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001293 if (error < 0)
1294 goto error;
1295
1296 BUG_ON(!server->nfs_client);
1297 BUG_ON(!server->nfs_client->rpc_ops);
1298 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1299
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001300 /* Probe the root fh to retrieve its FSID and filehandle */
1301 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1302 if (error < 0)
1303 goto error;
1304
David Howells54ceac42006-08-22 20:06:13 -04001305 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001306 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001307 if (error < 0)
1308 goto error;
1309
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001310 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1311 server->namelen = NFS4_MAXNAMLEN;
1312
David Howells54ceac42006-08-22 20:06:13 -04001313 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001314 (unsigned long long) server->fsid.major,
1315 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001316
1317 spin_lock(&nfs_client_lock);
1318 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1319 list_add_tail(&server->master_link, &nfs_volume_list);
1320 spin_unlock(&nfs_client_lock);
1321
1322 server->mount_time = jiffies;
1323
1324 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1325 return server;
1326
1327error:
1328 nfs_free_server(server);
1329 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1330 return ERR_PTR(error);
1331}
1332
1333#endif /* CONFIG_NFS_V4 */
1334
1335/*
1336 * Clone an NFS2, NFS3 or NFS4 server record
1337 */
1338struct nfs_server *nfs_clone_server(struct nfs_server *source,
1339 struct nfs_fh *fh,
1340 struct nfs_fattr *fattr)
1341{
1342 struct nfs_server *server;
1343 struct nfs_fattr fattr_fsinfo;
1344 int error;
1345
1346 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001347 (unsigned long long) fattr->fsid.major,
1348 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001349
1350 server = nfs_alloc_server();
1351 if (!server)
1352 return ERR_PTR(-ENOMEM);
1353
1354 /* Copy data from the source */
1355 server->nfs_client = source->nfs_client;
1356 atomic_inc(&server->nfs_client->cl_count);
1357 nfs_server_copy_userdata(server, source);
1358
1359 server->fsid = fattr->fsid;
1360
Trond Myklebust33170232007-12-20 16:03:59 -05001361 error = nfs_init_server_rpcclient(server,
1362 source->client->cl_timeout,
1363 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001364 if (error < 0)
1365 goto out_free_server;
1366 if (!IS_ERR(source->client_acl))
1367 nfs_init_server_aclclient(server);
1368
1369 /* probe the filesystem info for this server filesystem */
1370 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1371 if (error < 0)
1372 goto out_free_server;
1373
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001374 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1375 server->namelen = NFS4_MAXNAMLEN;
1376
David Howells54ceac42006-08-22 20:06:13 -04001377 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001378 (unsigned long long) server->fsid.major,
1379 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001380
1381 error = nfs_start_lockd(server);
1382 if (error < 0)
1383 goto out_free_server;
1384
1385 spin_lock(&nfs_client_lock);
1386 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1387 list_add_tail(&server->master_link, &nfs_volume_list);
1388 spin_unlock(&nfs_client_lock);
1389
1390 server->mount_time = jiffies;
1391
1392 dprintk("<-- nfs_clone_server() = %p\n", server);
1393 return server;
1394
1395out_free_server:
1396 nfs_free_server(server);
1397 dprintk("<-- nfs_clone_server() = error %d\n", error);
1398 return ERR_PTR(error);
1399}
David Howells6aaca562006-08-22 20:06:13 -04001400
1401#ifdef CONFIG_PROC_FS
1402static struct proc_dir_entry *proc_fs_nfs;
1403
1404static int nfs_server_list_open(struct inode *inode, struct file *file);
1405static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1406static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1407static void nfs_server_list_stop(struct seq_file *p, void *v);
1408static int nfs_server_list_show(struct seq_file *m, void *v);
1409
1410static struct seq_operations nfs_server_list_ops = {
1411 .start = nfs_server_list_start,
1412 .next = nfs_server_list_next,
1413 .stop = nfs_server_list_stop,
1414 .show = nfs_server_list_show,
1415};
1416
Arjan van de Ven00977a52007-02-12 00:55:34 -08001417static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001418 .open = nfs_server_list_open,
1419 .read = seq_read,
1420 .llseek = seq_lseek,
1421 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001422 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001423};
1424
1425static int nfs_volume_list_open(struct inode *inode, struct file *file);
1426static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1427static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1428static void nfs_volume_list_stop(struct seq_file *p, void *v);
1429static int nfs_volume_list_show(struct seq_file *m, void *v);
1430
1431static struct seq_operations nfs_volume_list_ops = {
1432 .start = nfs_volume_list_start,
1433 .next = nfs_volume_list_next,
1434 .stop = nfs_volume_list_stop,
1435 .show = nfs_volume_list_show,
1436};
1437
Arjan van de Ven00977a52007-02-12 00:55:34 -08001438static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001439 .open = nfs_volume_list_open,
1440 .read = seq_read,
1441 .llseek = seq_lseek,
1442 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001443 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001444};
1445
1446/*
1447 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1448 * we're dealing
1449 */
1450static int nfs_server_list_open(struct inode *inode, struct file *file)
1451{
1452 struct seq_file *m;
1453 int ret;
1454
1455 ret = seq_open(file, &nfs_server_list_ops);
1456 if (ret < 0)
1457 return ret;
1458
1459 m = file->private_data;
1460 m->private = PDE(inode)->data;
1461
1462 return 0;
1463}
1464
1465/*
1466 * set up the iterator to start reading from the server list and return the first item
1467 */
1468static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1469{
David Howells6aaca562006-08-22 20:06:13 -04001470 /* lock the list against modification */
1471 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001472 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001473}
1474
1475/*
1476 * move to next server
1477 */
1478static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1479{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001480 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001481}
1482
1483/*
1484 * clean up after reading from the transports list
1485 */
1486static void nfs_server_list_stop(struct seq_file *p, void *v)
1487{
1488 spin_unlock(&nfs_client_lock);
1489}
1490
1491/*
1492 * display a header line followed by a load of call lines
1493 */
1494static int nfs_server_list_show(struct seq_file *m, void *v)
1495{
1496 struct nfs_client *clp;
1497
1498 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001499 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001500 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1501 return 0;
1502 }
1503
1504 /* display one transport per line on subsequent lines */
1505 clp = list_entry(v, struct nfs_client, cl_share_link);
1506
Chuck Lever5d8515c2007-12-10 14:57:16 -05001507 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001508 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001509 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1510 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001511 atomic_read(&clp->cl_count),
1512 clp->cl_hostname);
1513
1514 return 0;
1515}
1516
1517/*
1518 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1519 */
1520static int nfs_volume_list_open(struct inode *inode, struct file *file)
1521{
1522 struct seq_file *m;
1523 int ret;
1524
1525 ret = seq_open(file, &nfs_volume_list_ops);
1526 if (ret < 0)
1527 return ret;
1528
1529 m = file->private_data;
1530 m->private = PDE(inode)->data;
1531
1532 return 0;
1533}
1534
1535/*
1536 * set up the iterator to start reading from the volume list and return the first item
1537 */
1538static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1539{
David Howells6aaca562006-08-22 20:06:13 -04001540 /* lock the list against modification */
1541 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001542 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001543}
1544
1545/*
1546 * move to next volume
1547 */
1548static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1549{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001550 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001551}
1552
1553/*
1554 * clean up after reading from the transports list
1555 */
1556static void nfs_volume_list_stop(struct seq_file *p, void *v)
1557{
1558 spin_unlock(&nfs_client_lock);
1559}
1560
1561/*
1562 * display a header line followed by a load of call lines
1563 */
1564static int nfs_volume_list_show(struct seq_file *m, void *v)
1565{
1566 struct nfs_server *server;
1567 struct nfs_client *clp;
1568 char dev[8], fsid[17];
1569
1570 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001571 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001572 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001573 return 0;
1574 }
1575 /* display one transport per line on subsequent lines */
1576 server = list_entry(v, struct nfs_server, master_link);
1577 clp = server->nfs_client;
1578
1579 snprintf(dev, 8, "%u:%u",
1580 MAJOR(server->s_dev), MINOR(server->s_dev));
1581
1582 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001583 (unsigned long long) server->fsid.major,
1584 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001585
David Howells5d1acff2009-04-03 16:42:47 +01001586 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001587 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001588 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1589 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001590 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001591 fsid,
1592 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001593
1594 return 0;
1595}
1596
1597/*
1598 * initialise the /proc/fs/nfsfs/ directory
1599 */
1600int __init nfs_fs_proc_init(void)
1601{
1602 struct proc_dir_entry *p;
1603
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001604 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001605 if (!proc_fs_nfs)
1606 goto error_0;
1607
David Howells6aaca562006-08-22 20:06:13 -04001608 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001609 p = proc_create("servers", S_IFREG|S_IRUGO,
1610 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001611 if (!p)
1612 goto error_1;
1613
David Howells6aaca562006-08-22 20:06:13 -04001614 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001615 p = proc_create("volumes", S_IFREG|S_IRUGO,
1616 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001617 if (!p)
1618 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001619 return 0;
1620
1621error_2:
1622 remove_proc_entry("servers", proc_fs_nfs);
1623error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001624 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001625error_0:
1626 return -ENOMEM;
1627}
1628
1629/*
1630 * clean up the /proc/fs/nfsfs/ directory
1631 */
1632void nfs_fs_proc_exit(void)
1633{
1634 remove_proc_entry("volumes", proc_fs_nfs);
1635 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001636 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001637}
1638
1639#endif /* CONFIG_PROC_FS */