blob: 855daac0f246ad56759bede61000d6e29d1fb857 [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
48
49#define NFSDBG_FACILITY NFSDBG_CLIENT
50
51static DEFINE_SPINLOCK(nfs_client_lock);
52static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040053static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040054static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55
56/*
David Howells5006a762006-08-22 20:06:12 -040057 * RPC cruft for NFS
58 */
59static struct rpc_version *nfs_version[5] = {
60 [2] = &nfs_version2,
61#ifdef CONFIG_NFS_V3
62 [3] = &nfs_version3,
63#endif
64#ifdef CONFIG_NFS_V4
65 [4] = &nfs_version4,
66#endif
67};
68
69struct rpc_program nfs_program = {
70 .name = "nfs",
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
76};
77
78struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
80};
81
82
83#ifdef CONFIG_NFS_V3_ACL
84static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
87};
88
89struct rpc_program nfsacl_program = {
90 .name = "nfsacl",
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
95};
96#endif /* CONFIG_NFS_V3_ACL */
97
Trond Myklebust3a498022007-12-14 14:56:04 -050098struct nfs_client_initdata {
99 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500100 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500101 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500102 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500103 int proto;
Trond Myklebust3a498022007-12-14 14:56:04 -0500104};
105
David Howells5006a762006-08-22 20:06:12 -0400106/*
David Howells24c8dbb2006-08-22 20:06:10 -0400107 * Allocate a shared client record
108 *
109 * Since these are allocated/deallocated very rarely, we don't
110 * bother putting them in a slab cache...
111 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500112static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400113{
114 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400115 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400116
117 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
118 goto error_0;
119
Trond Myklebust40c553192007-12-14 14:56:07 -0500120 clp->rpc_ops = cl_init->rpc_ops;
121
122 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400123 if (nfs_callback_up() < 0)
124 goto error_2;
125 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
126 }
127
128 atomic_set(&clp->cl_count, 1);
129 clp->cl_cons_state = NFS_CS_INITING;
130
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500131 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
132 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400133
Trond Myklebust3a498022007-12-14 14:56:04 -0500134 if (cl_init->hostname) {
135 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400136 if (!clp->cl_hostname)
137 goto error_3;
138 }
139
140 INIT_LIST_HEAD(&clp->cl_superblocks);
141 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500143 clp->cl_proto = cl_init->proto;
144
David Howells24c8dbb2006-08-22 20:06:10 -0400145#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400146 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400147 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
152#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400153 cred = rpc_lookup_machine_cred();
154 if (!IS_ERR(cred))
155 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400156
157 return clp;
158
159error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400160 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
161 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400162error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400163 kfree(clp);
164error_0:
165 return NULL;
166}
167
Trond Myklebust5dd31772006-08-24 01:03:05 -0400168static void nfs4_shutdown_client(struct nfs_client *clp)
169{
170#ifdef CONFIG_NFS_V4
171 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
172 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400173 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400174 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
175 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500176
177 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400178#endif
179}
180
David Howells24c8dbb2006-08-22 20:06:10 -0400181/*
182 * Destroy a shared client record
183 */
184static void nfs_free_client(struct nfs_client *clp)
185{
Trond Myklebust40c553192007-12-14 14:56:07 -0500186 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400187
Trond Myklebust5dd31772006-08-24 01:03:05 -0400188 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400189
190 /* -EIO all pending I/O */
191 if (!IS_ERR(clp->cl_rpcclient))
192 rpc_shutdown_client(clp->cl_rpcclient);
193
194 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
195 nfs_callback_down();
196
Trond Myklebust7c67db32008-04-07 20:50:11 -0400197 if (clp->cl_machine_cred != NULL)
198 put_rpccred(clp->cl_machine_cred);
199
David Howells24c8dbb2006-08-22 20:06:10 -0400200 kfree(clp->cl_hostname);
201 kfree(clp);
202
203 dprintk("<-- nfs_free_client()\n");
204}
205
206/*
207 * Release a reference to a shared client record
208 */
209void nfs_put_client(struct nfs_client *clp)
210{
David Howells27ba8512006-07-30 14:40:56 -0400211 if (!clp)
212 return;
213
David Howells24c8dbb2006-08-22 20:06:10 -0400214 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
215
216 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
217 list_del(&clp->cl_share_link);
218 spin_unlock(&nfs_client_lock);
219
220 BUG_ON(!list_empty(&clp->cl_superblocks));
221
222 nfs_free_client(clp);
223 }
224}
225
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500226#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400227/*
228 * Test if two ip6 socket addresses refer to the same socket by
229 * comparing relevant fields. The padding bytes specifically, are not
230 * compared. sin6_flowinfo is not compared because it only affects QoS
231 * and sin6_scope_id is only compared if the address is "link local"
232 * because "link local" addresses need only be unique to a specific
233 * link. Conversely, ordinary unicast addresses might have different
234 * sin6_scope_id.
235 *
236 * The caller should ensure both socket addresses are AF_INET6.
237 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400238static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
239 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400240{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400241 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
242 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400243
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400244 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
245 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400246 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500247
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400248 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500249}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400250#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
251static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
252 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400253{
254 return 0;
255}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500256#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500257
David Howells24c8dbb2006-08-22 20:06:10 -0400258/*
Ian Dalld7371c42009-03-10 20:33:22 -0400259 * Test if two ip4 socket addresses refer to the same socket, by
260 * comparing relevant fields. The padding bytes specifically, are
261 * not compared.
262 *
263 * The caller should ensure both socket addresses are AF_INET.
264 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400265static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
266 const struct sockaddr *sa2)
267{
268 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
269 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
270
271 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
272}
273
274static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
275 const struct sockaddr *sa2)
276{
277 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
278 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
279
280 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
281 (sin1->sin6_port == sin2->sin6_port);
282}
283
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400284static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
285 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400286{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400287 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
288 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400289
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400290 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
291 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400292}
293
294/*
Ian Dalld7371c42009-03-10 20:33:22 -0400295 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400296 * by comparing (only) relevant fields, excluding the port number.
297 */
298static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
299 const struct sockaddr *sa2)
300{
301 if (sa1->sa_family != sa2->sa_family)
302 return 0;
303
304 switch (sa1->sa_family) {
305 case AF_INET:
306 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
307 case AF_INET6:
308 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
309 }
310 return 0;
311}
312
313/*
314 * Test if two socket addresses represent the same actual socket,
315 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400316 */
317static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
319{
320 if (sa1->sa_family != sa2->sa_family)
321 return 0;
322
323 switch (sa1->sa_family) {
324 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400325 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400326 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400327 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400328 }
329 return 0;
330}
331
332/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500333 * Find a client by IP address and protocol version
334 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400335 */
Chuck Leverff052642007-12-10 14:58:44 -0500336struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500337{
338 struct nfs_client *clp;
339
340 spin_lock(&nfs_client_lock);
341 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500342 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
343
Trond Myklebustc81468a2007-12-14 14:56:05 -0500344 /* Don't match clients that failed to initialise properly */
345 if (clp->cl_cons_state != NFS_CS_READY)
346 continue;
347
348 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500349 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500350 continue;
351
352 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500353 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500354 continue;
355
356 atomic_inc(&clp->cl_count);
357 spin_unlock(&nfs_client_lock);
358 return clp;
359 }
360 spin_unlock(&nfs_client_lock);
361 return NULL;
362}
363
364/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500365 * Find a client by IP address and protocol version
366 * - returns NULL if no such client
367 */
368struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
369{
370 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
371 u32 nfsvers = clp->rpc_ops->version;
372
373 spin_lock(&nfs_client_lock);
374 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
375 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
376
377 /* Don't match clients that failed to initialise properly */
378 if (clp->cl_cons_state != NFS_CS_READY)
379 continue;
380
381 /* Different NFS versions cannot share the same nfs_client */
382 if (clp->rpc_ops->version != nfsvers)
383 continue;
384
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500385 /* Match only the IP address, not the port number */
386 if (!nfs_sockaddr_match_ipaddr(sap, clap))
387 continue;
388
389 atomic_inc(&clp->cl_count);
390 spin_unlock(&nfs_client_lock);
391 return clp;
392 }
393 spin_unlock(&nfs_client_lock);
394 return NULL;
395}
396
397/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500398 * Find an nfs_client on the list that matches the initialisation data
399 * that is supplied.
400 */
401static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400402{
403 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400404 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400405
406 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400407 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700408 /* Don't match clients that failed to initialise properly */
409 if (clp->cl_cons_state < 0)
410 continue;
411
David Howells24c8dbb2006-08-22 20:06:10 -0400412 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500413 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400414 continue;
415
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500416 if (clp->cl_proto != data->proto)
417 continue;
418
Trond Myklebustc81468a2007-12-14 14:56:05 -0500419 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400420 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400421 continue;
422
Trond Myklebustc81468a2007-12-14 14:56:05 -0500423 atomic_inc(&clp->cl_count);
424 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400425 }
David Howells24c8dbb2006-08-22 20:06:10 -0400426 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400427}
428
429/*
430 * Look up a client by IP address and protocol version
431 * - creates a new record if one doesn't yet exist
432 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500433static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400434{
435 struct nfs_client *clp, *new = NULL;
436 int error;
437
Chuck Leverd7422c42007-12-10 14:58:51 -0500438 dprintk("--> nfs_get_client(%s,v%u)\n",
439 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400440
441 /* see if the client already exists */
442 do {
443 spin_lock(&nfs_client_lock);
444
Trond Myklebustc81468a2007-12-14 14:56:05 -0500445 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400446 if (clp)
447 goto found_client;
448 if (new)
449 goto install_client;
450
451 spin_unlock(&nfs_client_lock);
452
Trond Myklebust3a498022007-12-14 14:56:04 -0500453 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400454 } while (new);
455
456 return ERR_PTR(-ENOMEM);
457
458 /* install a new client and return with it unready */
459install_client:
460 clp = new;
461 list_add(&clp->cl_share_link, &nfs_client_list);
462 spin_unlock(&nfs_client_lock);
463 dprintk("--> nfs_get_client() = %p [new]\n", clp);
464 return clp;
465
466 /* found an existing client
467 * - make sure it's ready before returning
468 */
469found_client:
470 spin_unlock(&nfs_client_lock);
471
472 if (new)
473 nfs_free_client(new);
474
Matthew Wilcox150030b2007-12-06 16:24:39 -0500475 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400476 clp->cl_cons_state != NFS_CS_INITING);
477 if (error < 0) {
478 nfs_put_client(clp);
479 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400480 }
481
482 if (clp->cl_cons_state < NFS_CS_READY) {
483 error = clp->cl_cons_state;
484 nfs_put_client(clp);
485 return ERR_PTR(error);
486 }
487
David Howells54ceac42006-08-22 20:06:13 -0400488 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
489
David Howells24c8dbb2006-08-22 20:06:10 -0400490 dprintk("--> nfs_get_client() = %p [share]\n", clp);
491 return clp;
492}
493
494/*
495 * Mark a server as ready or failed
496 */
David Howells54ceac42006-08-22 20:06:13 -0400497static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400498{
499 clp->cl_cons_state = state;
500 wake_up_all(&nfs_client_active_wq);
501}
David Howells5006a762006-08-22 20:06:12 -0400502
503/*
504 * Initialise the timeout values for a connection
505 */
506static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
507 unsigned int timeo, unsigned int retrans)
508{
509 to->to_initval = timeo * HZ / 10;
510 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400511
512 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400513 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400514 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400515 if (to->to_retries == 0)
516 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500517 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400518 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400519 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
520 to->to_initval = NFS_MAX_TCP_TIMEOUT;
521 to->to_increment = to->to_initval;
522 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500523 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
524 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
525 if (to->to_maxval < to->to_initval)
526 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400527 to->to_exponential = 0;
528 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400529 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400530 if (to->to_retries == 0)
531 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400532 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400533 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400534 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
535 to->to_initval = NFS_MAX_UDP_TIMEOUT;
536 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
537 to->to_exponential = 1;
538 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400539 default:
540 BUG();
David Howells5006a762006-08-22 20:06:12 -0400541 }
542}
543
544/*
545 * Create an RPC client handle
546 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500547static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500548 const struct rpc_timeout *timeparms,
549 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500550 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400551{
David Howells5006a762006-08-22 20:06:12 -0400552 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400553 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500554 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400555 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500556 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500557 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400558 .servername = clp->cl_hostname,
559 .program = &nfs_program,
560 .version = clp->rpc_ops->version,
561 .authflavor = flavor,
562 };
David Howells5006a762006-08-22 20:06:12 -0400563
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500564 if (discrtry)
565 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
566 if (noresvport)
567 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
568
David Howells5006a762006-08-22 20:06:12 -0400569 if (!IS_ERR(clp->cl_rpcclient))
570 return 0;
571
Chuck Lever41877d22006-08-22 20:06:20 -0400572 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400573 if (IS_ERR(clnt)) {
574 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700575 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400576 return PTR_ERR(clnt);
577 }
578
David Howells5006a762006-08-22 20:06:12 -0400579 clp->cl_rpcclient = clnt;
580 return 0;
581}
David Howells54ceac42006-08-22 20:06:13 -0400582
583/*
584 * Version 2 or 3 client destruction
585 */
586static void nfs_destroy_server(struct nfs_server *server)
587{
David Howells54ceac42006-08-22 20:06:13 -0400588 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500589 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400590}
591
592/*
593 * Version 2 or 3 lockd setup
594 */
595static int nfs_start_lockd(struct nfs_server *server)
596{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500597 struct nlm_host *host;
598 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500599 struct nlmclnt_initdata nlm_init = {
600 .hostname = clp->cl_hostname,
601 .address = (struct sockaddr *)&clp->cl_addr,
602 .addrlen = clp->cl_addrlen,
603 .protocol = server->flags & NFS_MOUNT_TCP ?
604 IPPROTO_TCP : IPPROTO_UDP,
605 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500606 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
607 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500608 };
David Howells54ceac42006-08-22 20:06:13 -0400609
Chuck Lever883bb162008-01-15 16:04:20 -0500610 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500611 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400612 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500613 return 0;
614
Chuck Lever883bb162008-01-15 16:04:20 -0500615 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500616 if (IS_ERR(host))
617 return PTR_ERR(host);
618
619 server->nlm_host = host;
620 server->destroy = nfs_destroy_server;
621 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400622}
623
624/*
625 * Initialise an NFSv3 ACL client connection
626 */
627#ifdef CONFIG_NFS_V3_ACL
628static void nfs_init_server_aclclient(struct nfs_server *server)
629{
Trond Myklebust40c553192007-12-14 14:56:07 -0500630 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400631 goto out_noacl;
632 if (server->flags & NFS_MOUNT_NOACL)
633 goto out_noacl;
634
635 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
636 if (IS_ERR(server->client_acl))
637 goto out_noacl;
638
639 /* No errors! Assume that Sun nfsacls are supported */
640 server->caps |= NFS_CAP_ACLS;
641 return;
642
643out_noacl:
644 server->caps &= ~NFS_CAP_ACLS;
645}
646#else
647static inline void nfs_init_server_aclclient(struct nfs_server *server)
648{
649 server->flags &= ~NFS_MOUNT_NOACL;
650 server->caps &= ~NFS_CAP_ACLS;
651}
652#endif
653
654/*
655 * Create a general RPC client
656 */
Trond Myklebust33170232007-12-20 16:03:59 -0500657static int nfs_init_server_rpcclient(struct nfs_server *server,
658 const struct rpc_timeout *timeo,
659 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400660{
661 struct nfs_client *clp = server->nfs_client;
662
663 server->client = rpc_clone_client(clp->cl_rpcclient);
664 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700665 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400666 return PTR_ERR(server->client);
667 }
668
Trond Myklebust33170232007-12-20 16:03:59 -0500669 memcpy(&server->client->cl_timeout_default,
670 timeo,
671 sizeof(server->client->cl_timeout_default));
672 server->client->cl_timeout = &server->client->cl_timeout_default;
673
David Howells54ceac42006-08-22 20:06:13 -0400674 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
675 struct rpc_auth *auth;
676
677 auth = rpcauth_create(pseudoflavour, server->client);
678 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700679 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400680 return PTR_ERR(auth);
681 }
682 }
683 server->client->cl_softrtry = 0;
684 if (server->flags & NFS_MOUNT_SOFT)
685 server->client->cl_softrtry = 1;
686
David Howells54ceac42006-08-22 20:06:13 -0400687 return 0;
688}
689
690/*
691 * Initialise an NFS2 or NFS3 client
692 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400693static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500694 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400695 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400696{
David Howells54ceac42006-08-22 20:06:13 -0400697 int error;
698
699 if (clp->cl_cons_state == NFS_CS_READY) {
700 /* the client is already initialised */
701 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
702 return 0;
703 }
704
David Howells54ceac42006-08-22 20:06:13 -0400705 /*
706 * Create a client RPC handle for doing FSSTAT with UNIX auth only
707 * - RFC 2623, sec 2.3.2
708 */
Chuck Leverd7403512008-12-23 15:21:37 -0500709 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
710 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400711 if (error < 0)
712 goto error;
713 nfs_mark_client_ready(clp, NFS_CS_READY);
714 return 0;
715
716error:
717 nfs_mark_client_ready(clp, error);
718 dprintk("<-- nfs_init_client() = xerror %d\n", error);
719 return error;
720}
721
722/*
723 * Create a version 2 or 3 client
724 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400725static int nfs_init_server(struct nfs_server *server,
726 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400727{
Trond Myklebust3a498022007-12-14 14:56:04 -0500728 struct nfs_client_initdata cl_init = {
729 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500730 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500731 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500732 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500733 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500734 };
Trond Myklebust33170232007-12-20 16:03:59 -0500735 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400736 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500737 int error;
David Howells54ceac42006-08-22 20:06:13 -0400738
739 dprintk("--> nfs_init_server()\n");
740
741#ifdef CONFIG_NFS_V3
742 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500743 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400744#endif
745
746 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500747 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400748 if (IS_ERR(clp)) {
749 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
750 return PTR_ERR(clp);
751 }
752
Trond Myklebust33170232007-12-20 16:03:59 -0500753 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
754 data->timeo, data->retrans);
755 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400756 if (error < 0)
757 goto error;
758
759 server->nfs_client = clp;
760
761 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400762 server->flags = data->flags;
David Howells54ceac42006-08-22 20:06:13 -0400763
764 if (data->rsize)
765 server->rsize = nfs_block_size(data->rsize, NULL);
766 if (data->wsize)
767 server->wsize = nfs_block_size(data->wsize, NULL);
768
769 server->acregmin = data->acregmin * HZ;
770 server->acregmax = data->acregmax * HZ;
771 server->acdirmin = data->acdirmin * HZ;
772 server->acdirmax = data->acdirmax * HZ;
773
774 /* Start lockd here, before we might error out */
775 error = nfs_start_lockd(server);
776 if (error < 0)
777 goto error;
778
Chuck Leverf22d6d72008-03-14 14:10:22 -0400779 server->port = data->nfs_server.port;
780
Trond Myklebust33170232007-12-20 16:03:59 -0500781 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400782 if (error < 0)
783 goto error;
784
Chuck Lever3f8400d2008-03-14 14:10:30 -0400785 /* Preserve the values of mount_server-related mount options */
786 if (data->mount_server.addrlen) {
787 memcpy(&server->mountd_address, &data->mount_server.address,
788 data->mount_server.addrlen);
789 server->mountd_addrlen = data->mount_server.addrlen;
790 }
791 server->mountd_version = data->mount_server.version;
792 server->mountd_port = data->mount_server.port;
793 server->mountd_protocol = data->mount_server.protocol;
794
David Howells54ceac42006-08-22 20:06:13 -0400795 server->namelen = data->namlen;
796 /* Create a client RPC handle for the NFSv3 ACL management interface */
797 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400798 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
799 return 0;
800
801error:
802 server->nfs_client = NULL;
803 nfs_put_client(clp);
804 dprintk("<-- nfs_init_server() = xerror %d\n", error);
805 return error;
806}
807
808/*
809 * Load up the server record from information gained in an fsinfo record
810 */
811static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
812{
813 unsigned long max_rpc_payload;
814
815 /* Work out a lot of parameters */
816 if (server->rsize == 0)
817 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
818 if (server->wsize == 0)
819 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
820
821 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
822 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
823 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
824 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
825
826 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
827 if (server->rsize > max_rpc_payload)
828 server->rsize = max_rpc_payload;
829 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
830 server->rsize = NFS_MAX_FILE_IO_SIZE;
831 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700832
David Howells54ceac42006-08-22 20:06:13 -0400833 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
834
835 if (server->wsize > max_rpc_payload)
836 server->wsize = max_rpc_payload;
837 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
838 server->wsize = NFS_MAX_FILE_IO_SIZE;
839 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
840 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
841
842 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
843 if (server->dtsize > PAGE_CACHE_SIZE)
844 server->dtsize = PAGE_CACHE_SIZE;
845 if (server->dtsize > server->rsize)
846 server->dtsize = server->rsize;
847
848 if (server->flags & NFS_MOUNT_NOAC) {
849 server->acregmin = server->acregmax = 0;
850 server->acdirmin = server->acdirmax = 0;
851 }
852
853 server->maxfilesize = fsinfo->maxfilesize;
854
855 /* We're airborne Set socket buffersize */
856 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
857}
858
859/*
860 * Probe filesystem information, including the FSID on v2/v3
861 */
862static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
863{
864 struct nfs_fsinfo fsinfo;
865 struct nfs_client *clp = server->nfs_client;
866 int error;
867
868 dprintk("--> nfs_probe_fsinfo()\n");
869
870 if (clp->rpc_ops->set_capabilities != NULL) {
871 error = clp->rpc_ops->set_capabilities(server, mntfh);
872 if (error < 0)
873 goto out_error;
874 }
875
876 fsinfo.fattr = fattr;
877 nfs_fattr_init(fattr);
878 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
879 if (error < 0)
880 goto out_error;
881
882 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700883 error = bdi_init(&server->backing_dev_info);
884 if (error)
885 goto out_error;
886
David Howells54ceac42006-08-22 20:06:13 -0400887
888 /* Get some general file system info */
889 if (server->namelen == 0) {
890 struct nfs_pathconf pathinfo;
891
892 pathinfo.fattr = fattr;
893 nfs_fattr_init(fattr);
894
895 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
896 server->namelen = pathinfo.max_namelen;
897 }
898
899 dprintk("<-- nfs_probe_fsinfo() = 0\n");
900 return 0;
901
902out_error:
903 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
904 return error;
905}
906
907/*
908 * Copy useful information when duplicating a server record
909 */
910static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
911{
912 target->flags = source->flags;
913 target->acregmin = source->acregmin;
914 target->acregmax = source->acregmax;
915 target->acdirmin = source->acdirmin;
916 target->acdirmax = source->acdirmax;
917 target->caps = source->caps;
918}
919
920/*
921 * Allocate and initialise a server record
922 */
923static struct nfs_server *nfs_alloc_server(void)
924{
925 struct nfs_server *server;
926
927 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
928 if (!server)
929 return NULL;
930
931 server->client = server->client_acl = ERR_PTR(-EINVAL);
932
933 /* Zero out the NFS state stuff */
934 INIT_LIST_HEAD(&server->client_link);
935 INIT_LIST_HEAD(&server->master_link);
936
Steve Dicksonef818a22007-11-08 04:05:04 -0500937 atomic_set(&server->active, 0);
938
David Howells54ceac42006-08-22 20:06:13 -0400939 server->io_stats = nfs_alloc_iostats();
940 if (!server->io_stats) {
941 kfree(server);
942 return NULL;
943 }
944
945 return server;
946}
947
948/*
949 * Free up a server record
950 */
951void nfs_free_server(struct nfs_server *server)
952{
953 dprintk("--> nfs_free_server()\n");
954
955 spin_lock(&nfs_client_lock);
956 list_del(&server->client_link);
957 list_del(&server->master_link);
958 spin_unlock(&nfs_client_lock);
959
960 if (server->destroy != NULL)
961 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500962
963 if (!IS_ERR(server->client_acl))
964 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400965 if (!IS_ERR(server->client))
966 rpc_shutdown_client(server->client);
967
968 nfs_put_client(server->nfs_client);
969
970 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700971 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400972 kfree(server);
973 nfs_release_automount_timer();
974 dprintk("<-- nfs_free_server()\n");
975}
976
977/*
978 * Create a version 2 or 3 volume record
979 * - keyed on server and FSID
980 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400981struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400982 struct nfs_fh *mntfh)
983{
984 struct nfs_server *server;
985 struct nfs_fattr fattr;
986 int error;
987
988 server = nfs_alloc_server();
989 if (!server)
990 return ERR_PTR(-ENOMEM);
991
992 /* Get a client representation */
993 error = nfs_init_server(server, data);
994 if (error < 0)
995 goto error;
996
997 BUG_ON(!server->nfs_client);
998 BUG_ON(!server->nfs_client->rpc_ops);
999 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1000
1001 /* Probe the root fh to retrieve its FSID */
1002 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1003 if (error < 0)
1004 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001005 if (server->nfs_client->rpc_ops->version == 3) {
1006 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1007 server->namelen = NFS3_MAXNAMLEN;
1008 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1009 server->caps |= NFS_CAP_READDIRPLUS;
1010 } else {
1011 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1012 server->namelen = NFS2_MAXNAMLEN;
1013 }
1014
David Howells54ceac42006-08-22 20:06:13 -04001015 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1016 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1017 if (error < 0) {
1018 dprintk("nfs_create_server: getattr error = %d\n", -error);
1019 goto error;
1020 }
1021 }
1022 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1023
David Howells6daabf12006-08-24 15:44:16 -04001024 dprintk("Server FSID: %llx:%llx\n",
1025 (unsigned long long) server->fsid.major,
1026 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001027
1028 BUG_ON(!server->nfs_client);
1029 BUG_ON(!server->nfs_client->rpc_ops);
1030 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1031
1032 spin_lock(&nfs_client_lock);
1033 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1034 list_add_tail(&server->master_link, &nfs_volume_list);
1035 spin_unlock(&nfs_client_lock);
1036
1037 server->mount_time = jiffies;
1038 return server;
1039
1040error:
1041 nfs_free_server(server);
1042 return ERR_PTR(error);
1043}
1044
1045#ifdef CONFIG_NFS_V4
1046/*
1047 * Initialise an NFS4 client record
1048 */
1049static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001050 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001051 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001052 rpc_authflavor_t authflavour,
1053 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001054{
1055 int error;
1056
1057 if (clp->cl_cons_state == NFS_CS_READY) {
1058 /* the client is initialised already */
1059 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1060 return 0;
1061 }
1062
1063 /* Check NFS protocol revision and initialize RPC op vector */
1064 clp->rpc_ops = &nfs_v4_clientops;
1065
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001066 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001067 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001068 if (error < 0)
1069 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001070 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001071
1072 error = nfs_idmap_new(clp);
1073 if (error < 0) {
1074 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001075 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001076 goto error;
1077 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001078 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001079
1080 nfs_mark_client_ready(clp, NFS_CS_READY);
1081 return 0;
1082
1083error:
1084 nfs_mark_client_ready(clp, error);
1085 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1086 return error;
1087}
1088
1089/*
1090 * Set up an NFS4 client
1091 */
1092static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001093 const char *hostname,
1094 const struct sockaddr *addr,
1095 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001096 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001097 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -05001098 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -04001099{
Trond Myklebust3a498022007-12-14 14:56:04 -05001100 struct nfs_client_initdata cl_init = {
1101 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001102 .addr = addr,
1103 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001104 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001105 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -05001106 };
David Howells54ceac42006-08-22 20:06:13 -04001107 struct nfs_client *clp;
1108 int error;
1109
1110 dprintk("--> nfs4_set_client()\n");
1111
1112 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001113 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001114 if (IS_ERR(clp)) {
1115 error = PTR_ERR(clp);
1116 goto error;
1117 }
Chuck Leverd7403512008-12-23 15:21:37 -05001118 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1119 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001120 if (error < 0)
1121 goto error_put;
1122
1123 server->nfs_client = clp;
1124 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1125 return 0;
1126
1127error_put:
1128 nfs_put_client(clp);
1129error:
1130 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1131 return error;
1132}
1133
1134/*
1135 * Create a version 4 volume record
1136 */
1137static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001138 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001139{
Trond Myklebust33170232007-12-20 16:03:59 -05001140 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001141 int error;
1142
1143 dprintk("--> nfs4_init_server()\n");
1144
Trond Myklebust33170232007-12-20 16:03:59 -05001145 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1146 data->timeo, data->retrans);
1147
Chuck Lever542fcc32008-12-23 15:21:36 -05001148 /* Initialise the client representation from the mount data */
1149 server->flags = data->flags;
1150 server->caps |= NFS_CAP_ATOMIC_OPEN;
1151
Trond Myklebust33170232007-12-20 16:03:59 -05001152 /* Get a client record */
1153 error = nfs4_set_client(server,
1154 data->nfs_server.hostname,
1155 (const struct sockaddr *)&data->nfs_server.address,
1156 data->nfs_server.addrlen,
1157 data->client_address,
1158 data->auth_flavors[0],
1159 data->nfs_server.protocol,
1160 &timeparms);
1161 if (error < 0)
1162 goto error;
1163
David Howells54ceac42006-08-22 20:06:13 -04001164 if (data->rsize)
1165 server->rsize = nfs_block_size(data->rsize, NULL);
1166 if (data->wsize)
1167 server->wsize = nfs_block_size(data->wsize, NULL);
1168
1169 server->acregmin = data->acregmin * HZ;
1170 server->acregmax = data->acregmax * HZ;
1171 server->acdirmin = data->acdirmin * HZ;
1172 server->acdirmax = data->acdirmax * HZ;
1173
Chuck Leverf22d6d72008-03-14 14:10:22 -04001174 server->port = data->nfs_server.port;
1175
Trond Myklebust33170232007-12-20 16:03:59 -05001176 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001177
Trond Myklebust33170232007-12-20 16:03:59 -05001178error:
David Howells54ceac42006-08-22 20:06:13 -04001179 /* Done */
1180 dprintk("<-- nfs4_init_server() = %d\n", error);
1181 return error;
1182}
1183
1184/*
1185 * Create a version 4 volume record
1186 * - keyed on server and FSID
1187 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001188struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001189 struct nfs_fh *mntfh)
1190{
1191 struct nfs_fattr fattr;
1192 struct nfs_server *server;
1193 int error;
1194
1195 dprintk("--> nfs4_create_server()\n");
1196
1197 server = nfs_alloc_server();
1198 if (!server)
1199 return ERR_PTR(-ENOMEM);
1200
David Howells54ceac42006-08-22 20:06:13 -04001201 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001202 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001203 if (error < 0)
1204 goto error;
1205
1206 BUG_ON(!server->nfs_client);
1207 BUG_ON(!server->nfs_client->rpc_ops);
1208 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1209
1210 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001211 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001212 if (error < 0)
1213 goto error;
1214
David Howells6daabf12006-08-24 15:44:16 -04001215 dprintk("Server FSID: %llx:%llx\n",
1216 (unsigned long long) server->fsid.major,
1217 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001218 dprintk("Mount FH: %d\n", mntfh->size);
1219
1220 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1221 if (error < 0)
1222 goto error;
1223
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001224 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1225 server->namelen = NFS4_MAXNAMLEN;
1226
David Howells54ceac42006-08-22 20:06:13 -04001227 BUG_ON(!server->nfs_client);
1228 BUG_ON(!server->nfs_client->rpc_ops);
1229 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1230
1231 spin_lock(&nfs_client_lock);
1232 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1233 list_add_tail(&server->master_link, &nfs_volume_list);
1234 spin_unlock(&nfs_client_lock);
1235
1236 server->mount_time = jiffies;
1237 dprintk("<-- nfs4_create_server() = %p\n", server);
1238 return server;
1239
1240error:
1241 nfs_free_server(server);
1242 dprintk("<-- nfs4_create_server() = error %d\n", error);
1243 return ERR_PTR(error);
1244}
1245
1246/*
1247 * Create an NFS4 referral server record
1248 */
1249struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001250 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001251{
1252 struct nfs_client *parent_client;
1253 struct nfs_server *server, *parent_server;
1254 struct nfs_fattr fattr;
1255 int error;
1256
1257 dprintk("--> nfs4_create_referral_server()\n");
1258
1259 server = nfs_alloc_server();
1260 if (!server)
1261 return ERR_PTR(-ENOMEM);
1262
1263 parent_server = NFS_SB(data->sb);
1264 parent_client = parent_server->nfs_client;
1265
Chuck Lever542fcc32008-12-23 15:21:36 -05001266 /* Initialise the client representation from the parent server */
1267 nfs_server_copy_userdata(server, parent_server);
1268 server->caps |= NFS_CAP_ATOMIC_OPEN;
1269
David Howells54ceac42006-08-22 20:06:13 -04001270 /* Get a client representation.
1271 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001272 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001273 data->addr,
1274 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001275 parent_client->cl_ipaddr,
1276 data->authflavor,
1277 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001278 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001279 if (error < 0)
1280 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001281
Trond Myklebust33170232007-12-20 16:03:59 -05001282 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001283 if (error < 0)
1284 goto error;
1285
1286 BUG_ON(!server->nfs_client);
1287 BUG_ON(!server->nfs_client->rpc_ops);
1288 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1289
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001290 /* Probe the root fh to retrieve its FSID and filehandle */
1291 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1292 if (error < 0)
1293 goto error;
1294
David Howells54ceac42006-08-22 20:06:13 -04001295 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001296 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001297 if (error < 0)
1298 goto error;
1299
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001300 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1301 server->namelen = NFS4_MAXNAMLEN;
1302
David Howells54ceac42006-08-22 20:06:13 -04001303 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001304 (unsigned long long) server->fsid.major,
1305 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001306
1307 spin_lock(&nfs_client_lock);
1308 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1309 list_add_tail(&server->master_link, &nfs_volume_list);
1310 spin_unlock(&nfs_client_lock);
1311
1312 server->mount_time = jiffies;
1313
1314 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1315 return server;
1316
1317error:
1318 nfs_free_server(server);
1319 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1320 return ERR_PTR(error);
1321}
1322
1323#endif /* CONFIG_NFS_V4 */
1324
1325/*
1326 * Clone an NFS2, NFS3 or NFS4 server record
1327 */
1328struct nfs_server *nfs_clone_server(struct nfs_server *source,
1329 struct nfs_fh *fh,
1330 struct nfs_fattr *fattr)
1331{
1332 struct nfs_server *server;
1333 struct nfs_fattr fattr_fsinfo;
1334 int error;
1335
1336 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001337 (unsigned long long) fattr->fsid.major,
1338 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001339
1340 server = nfs_alloc_server();
1341 if (!server)
1342 return ERR_PTR(-ENOMEM);
1343
1344 /* Copy data from the source */
1345 server->nfs_client = source->nfs_client;
1346 atomic_inc(&server->nfs_client->cl_count);
1347 nfs_server_copy_userdata(server, source);
1348
1349 server->fsid = fattr->fsid;
1350
Trond Myklebust33170232007-12-20 16:03:59 -05001351 error = nfs_init_server_rpcclient(server,
1352 source->client->cl_timeout,
1353 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001354 if (error < 0)
1355 goto out_free_server;
1356 if (!IS_ERR(source->client_acl))
1357 nfs_init_server_aclclient(server);
1358
1359 /* probe the filesystem info for this server filesystem */
1360 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1361 if (error < 0)
1362 goto out_free_server;
1363
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001364 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1365 server->namelen = NFS4_MAXNAMLEN;
1366
David Howells54ceac42006-08-22 20:06:13 -04001367 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001368 (unsigned long long) server->fsid.major,
1369 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001370
1371 error = nfs_start_lockd(server);
1372 if (error < 0)
1373 goto out_free_server;
1374
1375 spin_lock(&nfs_client_lock);
1376 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1377 list_add_tail(&server->master_link, &nfs_volume_list);
1378 spin_unlock(&nfs_client_lock);
1379
1380 server->mount_time = jiffies;
1381
1382 dprintk("<-- nfs_clone_server() = %p\n", server);
1383 return server;
1384
1385out_free_server:
1386 nfs_free_server(server);
1387 dprintk("<-- nfs_clone_server() = error %d\n", error);
1388 return ERR_PTR(error);
1389}
David Howells6aaca562006-08-22 20:06:13 -04001390
1391#ifdef CONFIG_PROC_FS
1392static struct proc_dir_entry *proc_fs_nfs;
1393
1394static int nfs_server_list_open(struct inode *inode, struct file *file);
1395static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1396static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1397static void nfs_server_list_stop(struct seq_file *p, void *v);
1398static int nfs_server_list_show(struct seq_file *m, void *v);
1399
1400static struct seq_operations nfs_server_list_ops = {
1401 .start = nfs_server_list_start,
1402 .next = nfs_server_list_next,
1403 .stop = nfs_server_list_stop,
1404 .show = nfs_server_list_show,
1405};
1406
Arjan van de Ven00977a52007-02-12 00:55:34 -08001407static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001408 .open = nfs_server_list_open,
1409 .read = seq_read,
1410 .llseek = seq_lseek,
1411 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001412 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001413};
1414
1415static int nfs_volume_list_open(struct inode *inode, struct file *file);
1416static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1417static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1418static void nfs_volume_list_stop(struct seq_file *p, void *v);
1419static int nfs_volume_list_show(struct seq_file *m, void *v);
1420
1421static struct seq_operations nfs_volume_list_ops = {
1422 .start = nfs_volume_list_start,
1423 .next = nfs_volume_list_next,
1424 .stop = nfs_volume_list_stop,
1425 .show = nfs_volume_list_show,
1426};
1427
Arjan van de Ven00977a52007-02-12 00:55:34 -08001428static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001429 .open = nfs_volume_list_open,
1430 .read = seq_read,
1431 .llseek = seq_lseek,
1432 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001433 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001434};
1435
1436/*
1437 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1438 * we're dealing
1439 */
1440static int nfs_server_list_open(struct inode *inode, struct file *file)
1441{
1442 struct seq_file *m;
1443 int ret;
1444
1445 ret = seq_open(file, &nfs_server_list_ops);
1446 if (ret < 0)
1447 return ret;
1448
1449 m = file->private_data;
1450 m->private = PDE(inode)->data;
1451
1452 return 0;
1453}
1454
1455/*
1456 * set up the iterator to start reading from the server list and return the first item
1457 */
1458static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1459{
David Howells6aaca562006-08-22 20:06:13 -04001460 /* lock the list against modification */
1461 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001462 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001463}
1464
1465/*
1466 * move to next server
1467 */
1468static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1469{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001470 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001471}
1472
1473/*
1474 * clean up after reading from the transports list
1475 */
1476static void nfs_server_list_stop(struct seq_file *p, void *v)
1477{
1478 spin_unlock(&nfs_client_lock);
1479}
1480
1481/*
1482 * display a header line followed by a load of call lines
1483 */
1484static int nfs_server_list_show(struct seq_file *m, void *v)
1485{
1486 struct nfs_client *clp;
1487
1488 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001489 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001490 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1491 return 0;
1492 }
1493
1494 /* display one transport per line on subsequent lines */
1495 clp = list_entry(v, struct nfs_client, cl_share_link);
1496
Chuck Lever5d8515c2007-12-10 14:57:16 -05001497 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001498 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001499 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1500 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001501 atomic_read(&clp->cl_count),
1502 clp->cl_hostname);
1503
1504 return 0;
1505}
1506
1507/*
1508 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1509 */
1510static int nfs_volume_list_open(struct inode *inode, struct file *file)
1511{
1512 struct seq_file *m;
1513 int ret;
1514
1515 ret = seq_open(file, &nfs_volume_list_ops);
1516 if (ret < 0)
1517 return ret;
1518
1519 m = file->private_data;
1520 m->private = PDE(inode)->data;
1521
1522 return 0;
1523}
1524
1525/*
1526 * set up the iterator to start reading from the volume list and return the first item
1527 */
1528static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1529{
David Howells6aaca562006-08-22 20:06:13 -04001530 /* lock the list against modification */
1531 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001532 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001533}
1534
1535/*
1536 * move to next volume
1537 */
1538static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1539{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001540 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001541}
1542
1543/*
1544 * clean up after reading from the transports list
1545 */
1546static void nfs_volume_list_stop(struct seq_file *p, void *v)
1547{
1548 spin_unlock(&nfs_client_lock);
1549}
1550
1551/*
1552 * display a header line followed by a load of call lines
1553 */
1554static int nfs_volume_list_show(struct seq_file *m, void *v)
1555{
1556 struct nfs_server *server;
1557 struct nfs_client *clp;
1558 char dev[8], fsid[17];
1559
1560 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001561 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001562 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1563 return 0;
1564 }
1565 /* display one transport per line on subsequent lines */
1566 server = list_entry(v, struct nfs_server, master_link);
1567 clp = server->nfs_client;
1568
1569 snprintf(dev, 8, "%u:%u",
1570 MAJOR(server->s_dev), MINOR(server->s_dev));
1571
1572 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001573 (unsigned long long) server->fsid.major,
1574 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001575
Chuck Lever5d8515c2007-12-10 14:57:16 -05001576 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001577 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001578 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1579 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001580 dev,
1581 fsid);
1582
1583 return 0;
1584}
1585
1586/*
1587 * initialise the /proc/fs/nfsfs/ directory
1588 */
1589int __init nfs_fs_proc_init(void)
1590{
1591 struct proc_dir_entry *p;
1592
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001593 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001594 if (!proc_fs_nfs)
1595 goto error_0;
1596
1597 proc_fs_nfs->owner = THIS_MODULE;
1598
1599 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001600 p = proc_create("servers", S_IFREG|S_IRUGO,
1601 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001602 if (!p)
1603 goto error_1;
1604
David Howells6aaca562006-08-22 20:06:13 -04001605 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001606 p = proc_create("volumes", S_IFREG|S_IRUGO,
1607 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001608 if (!p)
1609 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001610 return 0;
1611
1612error_2:
1613 remove_proc_entry("servers", proc_fs_nfs);
1614error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001615 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001616error_0:
1617 return -ENOMEM;
1618}
1619
1620/*
1621 * clean up the /proc/fs/nfsfs/ directory
1622 */
1623void nfs_fs_proc_exit(void)
1624{
1625 remove_proc_entry("volumes", proc_fs_nfs);
1626 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001627 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001628}
1629
1630#endif /* CONFIG_PROC_FS */