blob: df2b40d1883d803e8fa5c1bddb588a1d39f20c87 [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010048#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040049
50#define NFSDBG_FACILITY NFSDBG_CLIENT
51
52static DEFINE_SPINLOCK(nfs_client_lock);
53static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040054static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040055static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56
57/*
David Howells5006a762006-08-22 20:06:12 -040058 * RPC cruft for NFS
59 */
60static struct rpc_version *nfs_version[5] = {
61 [2] = &nfs_version2,
62#ifdef CONFIG_NFS_V3
63 [3] = &nfs_version3,
64#endif
65#ifdef CONFIG_NFS_V4
66 [4] = &nfs_version4,
67#endif
68};
69
70struct rpc_program nfs_program = {
71 .name = "nfs",
72 .number = NFS_PROGRAM,
73 .nrvers = ARRAY_SIZE(nfs_version),
74 .version = nfs_version,
75 .stats = &nfs_rpcstat,
76 .pipe_dir_name = "/nfs",
77};
78
79struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
81};
82
83
84#ifdef CONFIG_NFS_V3_ACL
85static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
86static struct rpc_version * nfsacl_version[] = {
87 [3] = &nfsacl_version3,
88};
89
90struct rpc_program nfsacl_program = {
91 .name = "nfsacl",
92 .number = NFS_ACL_PROGRAM,
93 .nrvers = ARRAY_SIZE(nfsacl_version),
94 .version = nfsacl_version,
95 .stats = &nfsacl_rpcstat,
96};
97#endif /* CONFIG_NFS_V3_ACL */
98
Trond Myklebust3a498022007-12-14 14:56:04 -050099struct nfs_client_initdata {
100 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500101 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500102 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500103 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500104 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400105 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500106};
107
David Howells5006a762006-08-22 20:06:12 -0400108/*
David Howells24c8dbb2006-08-22 20:06:10 -0400109 * Allocate a shared client record
110 *
111 * Since these are allocated/deallocated very rarely, we don't
112 * bother putting them in a slab cache...
113 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500114static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400115{
116 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400117 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400118
119 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
120 goto error_0;
121
Trond Myklebust40c553192007-12-14 14:56:07 -0500122 clp->rpc_ops = cl_init->rpc_ops;
123
David Howells24c8dbb2006-08-22 20:06:10 -0400124 atomic_set(&clp->cl_count, 1);
125 clp->cl_cons_state = NFS_CS_INITING;
126
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500127 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
128 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400129
Trond Myklebust3a498022007-12-14 14:56:04 -0500130 if (cl_init->hostname) {
131 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400132 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400133 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400134 }
135
136 INIT_LIST_HEAD(&clp->cl_superblocks);
137 clp->cl_rpcclient = ERR_PTR(-EINVAL);
138
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500139 clp->cl_proto = cl_init->proto;
140
David Howells24c8dbb2006-08-22 20:06:10 -0400141#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400142 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400143 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000144 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400145 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
146 clp->cl_boot_time = CURRENT_TIME;
147 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400148 clp->cl_minorversion = cl_init->minorversion;
David Howells24c8dbb2006-08-22 20:06:10 -0400149#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400150 cred = rpc_lookup_machine_cred();
151 if (!IS_ERR(cred))
152 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400153
David Howells14727282009-04-03 16:42:42 +0100154 nfs_fscache_get_client_cookie(clp);
155
David Howells24c8dbb2006-08-22 20:06:10 -0400156 return clp;
157
Benny Halevy71468512009-04-01 09:22:56 -0400158error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400159 kfree(clp);
160error_0:
161 return NULL;
162}
163
Trond Myklebust5dd31772006-08-24 01:03:05 -0400164static void nfs4_shutdown_client(struct nfs_client *clp)
165{
166#ifdef CONFIG_NFS_V4
167 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
168 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400169 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400170 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
171 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500172
173 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400174#endif
175}
176
David Howells24c8dbb2006-08-22 20:06:10 -0400177/*
Benny Halevy9bdaa862009-04-01 09:22:55 -0400178 * Destroy the NFS4 callback service
179 */
180static void nfs4_destroy_callback(struct nfs_client *clp)
181{
182#ifdef CONFIG_NFS_V4
183 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
184 nfs_callback_down();
185#endif /* CONFIG_NFS_V4 */
186}
187
188/*
Andy Adamson557134a2009-04-01 09:21:53 -0400189 * Clears/puts all minor version specific parts from an nfs_client struct
190 * reverting it to minorversion 0.
191 */
192static void nfs4_clear_client_minor_version(struct nfs_client *clp)
193{
194#ifdef CONFIG_NFS_V4_1
195 if (nfs4_has_session(clp)) {
196 nfs4_destroy_session(clp->cl_session);
197 clp->cl_session = NULL;
198 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400199
200 clp->cl_call_sync = _nfs4_call_sync;
Andy Adamson557134a2009-04-01 09:21:53 -0400201#endif /* CONFIG_NFS_V4_1 */
Benny Halevy71468512009-04-01 09:22:56 -0400202
203 nfs4_destroy_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -0400204}
205
206/*
David Howells24c8dbb2006-08-22 20:06:10 -0400207 * Destroy a shared client record
208 */
209static void nfs_free_client(struct nfs_client *clp)
210{
Trond Myklebust40c553192007-12-14 14:56:07 -0500211 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400212
Andy Adamson557134a2009-04-01 09:21:53 -0400213 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400214 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400215
David Howells14727282009-04-03 16:42:42 +0100216 nfs_fscache_release_client_cookie(clp);
217
David Howells24c8dbb2006-08-22 20:06:10 -0400218 /* -EIO all pending I/O */
219 if (!IS_ERR(clp->cl_rpcclient))
220 rpc_shutdown_client(clp->cl_rpcclient);
221
Trond Myklebust7c67db32008-04-07 20:50:11 -0400222 if (clp->cl_machine_cred != NULL)
223 put_rpccred(clp->cl_machine_cred);
224
David Howells24c8dbb2006-08-22 20:06:10 -0400225 kfree(clp->cl_hostname);
226 kfree(clp);
227
228 dprintk("<-- nfs_free_client()\n");
229}
230
231/*
232 * Release a reference to a shared client record
233 */
234void nfs_put_client(struct nfs_client *clp)
235{
David Howells27ba8512006-07-30 14:40:56 -0400236 if (!clp)
237 return;
238
David Howells24c8dbb2006-08-22 20:06:10 -0400239 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
240
241 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
242 list_del(&clp->cl_share_link);
243 spin_unlock(&nfs_client_lock);
244
245 BUG_ON(!list_empty(&clp->cl_superblocks));
246
247 nfs_free_client(clp);
248 }
249}
250
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500251#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400252/*
253 * Test if two ip6 socket addresses refer to the same socket by
254 * comparing relevant fields. The padding bytes specifically, are not
255 * compared. sin6_flowinfo is not compared because it only affects QoS
256 * and sin6_scope_id is only compared if the address is "link local"
257 * because "link local" addresses need only be unique to a specific
258 * link. Conversely, ordinary unicast addresses might have different
259 * sin6_scope_id.
260 *
261 * The caller should ensure both socket addresses are AF_INET6.
262 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400263static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
264 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400265{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400266 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
267 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400268
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400269 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
270 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400271 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500272
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400273 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500274}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400275#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
276static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
277 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400278{
279 return 0;
280}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500281#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500282
David Howells24c8dbb2006-08-22 20:06:10 -0400283/*
Ian Dalld7371c42009-03-10 20:33:22 -0400284 * Test if two ip4 socket addresses refer to the same socket, by
285 * comparing relevant fields. The padding bytes specifically, are
286 * not compared.
287 *
288 * The caller should ensure both socket addresses are AF_INET.
289 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400290static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
291 const struct sockaddr *sa2)
292{
293 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
294 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
295
296 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
297}
298
299static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
300 const struct sockaddr *sa2)
301{
302 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
303 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
304
305 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
306 (sin1->sin6_port == sin2->sin6_port);
307}
308
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400309static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
310 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400311{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400312 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
313 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400314
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400315 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
316 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400317}
318
319/*
Ian Dalld7371c42009-03-10 20:33:22 -0400320 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400321 * by comparing (only) relevant fields, excluding the port number.
322 */
323static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
324 const struct sockaddr *sa2)
325{
326 if (sa1->sa_family != sa2->sa_family)
327 return 0;
328
329 switch (sa1->sa_family) {
330 case AF_INET:
331 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
332 case AF_INET6:
333 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
334 }
335 return 0;
336}
337
338/*
339 * Test if two socket addresses represent the same actual socket,
340 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400341 */
342static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
343 const struct sockaddr *sa2)
344{
345 if (sa1->sa_family != sa2->sa_family)
346 return 0;
347
348 switch (sa1->sa_family) {
349 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400350 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400351 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400352 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400353 }
354 return 0;
355}
356
357/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500358 * Find a client by IP address and protocol version
359 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400360 */
Chuck Leverff052642007-12-10 14:58:44 -0500361struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500362{
363 struct nfs_client *clp;
364
365 spin_lock(&nfs_client_lock);
366 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500367 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
368
Trond Myklebustc81468a2007-12-14 14:56:05 -0500369 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400370 if (!(clp->cl_cons_state == NFS_CS_READY ||
371 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500372 continue;
373
374 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500375 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500376 continue;
377
378 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500379 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500380 continue;
381
382 atomic_inc(&clp->cl_count);
383 spin_unlock(&nfs_client_lock);
384 return clp;
385 }
386 spin_unlock(&nfs_client_lock);
387 return NULL;
388}
389
390/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500391 * Find a client by IP address and protocol version
392 * - returns NULL if no such client
393 */
394struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
395{
396 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
397 u32 nfsvers = clp->rpc_ops->version;
398
399 spin_lock(&nfs_client_lock);
400 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
401 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
402
403 /* Don't match clients that failed to initialise properly */
404 if (clp->cl_cons_state != NFS_CS_READY)
405 continue;
406
407 /* Different NFS versions cannot share the same nfs_client */
408 if (clp->rpc_ops->version != nfsvers)
409 continue;
410
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500411 /* Match only the IP address, not the port number */
412 if (!nfs_sockaddr_match_ipaddr(sap, clap))
413 continue;
414
415 atomic_inc(&clp->cl_count);
416 spin_unlock(&nfs_client_lock);
417 return clp;
418 }
419 spin_unlock(&nfs_client_lock);
420 return NULL;
421}
422
423/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500424 * Find an nfs_client on the list that matches the initialisation data
425 * that is supplied.
426 */
427static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400428{
429 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400430 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400431
432 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400433 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700434 /* Don't match clients that failed to initialise properly */
435 if (clp->cl_cons_state < 0)
436 continue;
437
David Howells24c8dbb2006-08-22 20:06:10 -0400438 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500439 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400440 continue;
441
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500442 if (clp->cl_proto != data->proto)
443 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400444 /* Match nfsv4 minorversion */
445 if (clp->cl_minorversion != data->minorversion)
446 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500447 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400448 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400449 continue;
450
Trond Myklebustc81468a2007-12-14 14:56:05 -0500451 atomic_inc(&clp->cl_count);
452 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400453 }
David Howells24c8dbb2006-08-22 20:06:10 -0400454 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400455}
456
457/*
458 * Look up a client by IP address and protocol version
459 * - creates a new record if one doesn't yet exist
460 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500461static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400462{
463 struct nfs_client *clp, *new = NULL;
464 int error;
465
Chuck Leverd7422c42007-12-10 14:58:51 -0500466 dprintk("--> nfs_get_client(%s,v%u)\n",
467 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400468
469 /* see if the client already exists */
470 do {
471 spin_lock(&nfs_client_lock);
472
Trond Myklebustc81468a2007-12-14 14:56:05 -0500473 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400474 if (clp)
475 goto found_client;
476 if (new)
477 goto install_client;
478
479 spin_unlock(&nfs_client_lock);
480
Trond Myklebust3a498022007-12-14 14:56:04 -0500481 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400482 } while (new);
483
484 return ERR_PTR(-ENOMEM);
485
486 /* install a new client and return with it unready */
487install_client:
488 clp = new;
489 list_add(&clp->cl_share_link, &nfs_client_list);
490 spin_unlock(&nfs_client_lock);
491 dprintk("--> nfs_get_client() = %p [new]\n", clp);
492 return clp;
493
494 /* found an existing client
495 * - make sure it's ready before returning
496 */
497found_client:
498 spin_unlock(&nfs_client_lock);
499
500 if (new)
501 nfs_free_client(new);
502
Matthew Wilcox150030b2007-12-06 16:24:39 -0500503 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400504 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400505 if (error < 0) {
506 nfs_put_client(clp);
507 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400508 }
509
510 if (clp->cl_cons_state < NFS_CS_READY) {
511 error = clp->cl_cons_state;
512 nfs_put_client(clp);
513 return ERR_PTR(error);
514 }
515
David Howells54ceac42006-08-22 20:06:13 -0400516 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
517
David Howells24c8dbb2006-08-22 20:06:10 -0400518 dprintk("--> nfs_get_client() = %p [share]\n", clp);
519 return clp;
520}
521
522/*
523 * Mark a server as ready or failed
524 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400525void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400526{
527 clp->cl_cons_state = state;
528 wake_up_all(&nfs_client_active_wq);
529}
David Howells5006a762006-08-22 20:06:12 -0400530
531/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400532 * With sessions, the client is not marked ready until after a
533 * successful EXCHANGE_ID and CREATE_SESSION.
534 *
535 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
536 * other versions of NFS can be tried.
537 */
538int nfs4_check_client_ready(struct nfs_client *clp)
539{
540 if (!nfs4_has_session(clp))
541 return 0;
542 if (clp->cl_cons_state < NFS_CS_READY)
543 return -EPROTONOSUPPORT;
544 return 0;
545}
546
547/*
David Howells5006a762006-08-22 20:06:12 -0400548 * Initialise the timeout values for a connection
549 */
550static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
551 unsigned int timeo, unsigned int retrans)
552{
553 to->to_initval = timeo * HZ / 10;
554 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400555
556 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400557 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400558 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400559 if (to->to_retries == 0)
560 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500561 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400562 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400563 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
564 to->to_initval = NFS_MAX_TCP_TIMEOUT;
565 to->to_increment = to->to_initval;
566 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500567 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
568 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
569 if (to->to_maxval < to->to_initval)
570 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400571 to->to_exponential = 0;
572 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400573 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400574 if (to->to_retries == 0)
575 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400576 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400577 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400578 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
579 to->to_initval = NFS_MAX_UDP_TIMEOUT;
580 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
581 to->to_exponential = 1;
582 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400583 default:
584 BUG();
David Howells5006a762006-08-22 20:06:12 -0400585 }
586}
587
588/*
589 * Create an RPC client handle
590 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500591static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500592 const struct rpc_timeout *timeparms,
593 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500594 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400595{
David Howells5006a762006-08-22 20:06:12 -0400596 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400597 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500598 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400599 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500600 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500601 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400602 .servername = clp->cl_hostname,
603 .program = &nfs_program,
604 .version = clp->rpc_ops->version,
605 .authflavor = flavor,
606 };
David Howells5006a762006-08-22 20:06:12 -0400607
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500608 if (discrtry)
609 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
610 if (noresvport)
611 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
612
David Howells5006a762006-08-22 20:06:12 -0400613 if (!IS_ERR(clp->cl_rpcclient))
614 return 0;
615
Chuck Lever41877d22006-08-22 20:06:20 -0400616 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400617 if (IS_ERR(clnt)) {
618 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700619 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400620 return PTR_ERR(clnt);
621 }
622
David Howells5006a762006-08-22 20:06:12 -0400623 clp->cl_rpcclient = clnt;
624 return 0;
625}
David Howells54ceac42006-08-22 20:06:13 -0400626
627/*
628 * Version 2 or 3 client destruction
629 */
630static void nfs_destroy_server(struct nfs_server *server)
631{
David Howells54ceac42006-08-22 20:06:13 -0400632 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500633 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400634}
635
636/*
637 * Version 2 or 3 lockd setup
638 */
639static int nfs_start_lockd(struct nfs_server *server)
640{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500641 struct nlm_host *host;
642 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500643 struct nlmclnt_initdata nlm_init = {
644 .hostname = clp->cl_hostname,
645 .address = (struct sockaddr *)&clp->cl_addr,
646 .addrlen = clp->cl_addrlen,
647 .protocol = server->flags & NFS_MOUNT_TCP ?
648 IPPROTO_TCP : IPPROTO_UDP,
649 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500650 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
651 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500652 };
David Howells54ceac42006-08-22 20:06:13 -0400653
Chuck Lever883bb162008-01-15 16:04:20 -0500654 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500655 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400656 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500657 return 0;
658
Chuck Lever883bb162008-01-15 16:04:20 -0500659 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500660 if (IS_ERR(host))
661 return PTR_ERR(host);
662
663 server->nlm_host = host;
664 server->destroy = nfs_destroy_server;
665 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400666}
667
668/*
669 * Initialise an NFSv3 ACL client connection
670 */
671#ifdef CONFIG_NFS_V3_ACL
672static void nfs_init_server_aclclient(struct nfs_server *server)
673{
Trond Myklebust40c553192007-12-14 14:56:07 -0500674 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400675 goto out_noacl;
676 if (server->flags & NFS_MOUNT_NOACL)
677 goto out_noacl;
678
679 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
680 if (IS_ERR(server->client_acl))
681 goto out_noacl;
682
683 /* No errors! Assume that Sun nfsacls are supported */
684 server->caps |= NFS_CAP_ACLS;
685 return;
686
687out_noacl:
688 server->caps &= ~NFS_CAP_ACLS;
689}
690#else
691static inline void nfs_init_server_aclclient(struct nfs_server *server)
692{
693 server->flags &= ~NFS_MOUNT_NOACL;
694 server->caps &= ~NFS_CAP_ACLS;
695}
696#endif
697
698/*
699 * Create a general RPC client
700 */
Trond Myklebust33170232007-12-20 16:03:59 -0500701static int nfs_init_server_rpcclient(struct nfs_server *server,
702 const struct rpc_timeout *timeo,
703 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400704{
705 struct nfs_client *clp = server->nfs_client;
706
707 server->client = rpc_clone_client(clp->cl_rpcclient);
708 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700709 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400710 return PTR_ERR(server->client);
711 }
712
Trond Myklebust33170232007-12-20 16:03:59 -0500713 memcpy(&server->client->cl_timeout_default,
714 timeo,
715 sizeof(server->client->cl_timeout_default));
716 server->client->cl_timeout = &server->client->cl_timeout_default;
717
David Howells54ceac42006-08-22 20:06:13 -0400718 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
719 struct rpc_auth *auth;
720
721 auth = rpcauth_create(pseudoflavour, server->client);
722 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700723 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400724 return PTR_ERR(auth);
725 }
726 }
727 server->client->cl_softrtry = 0;
728 if (server->flags & NFS_MOUNT_SOFT)
729 server->client->cl_softrtry = 1;
730
David Howells54ceac42006-08-22 20:06:13 -0400731 return 0;
732}
733
734/*
735 * Initialise an NFS2 or NFS3 client
736 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400737static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500738 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400739 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400740{
David Howells54ceac42006-08-22 20:06:13 -0400741 int error;
742
743 if (clp->cl_cons_state == NFS_CS_READY) {
744 /* the client is already initialised */
745 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
746 return 0;
747 }
748
David Howells54ceac42006-08-22 20:06:13 -0400749 /*
750 * Create a client RPC handle for doing FSSTAT with UNIX auth only
751 * - RFC 2623, sec 2.3.2
752 */
Chuck Leverd7403512008-12-23 15:21:37 -0500753 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
754 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400755 if (error < 0)
756 goto error;
757 nfs_mark_client_ready(clp, NFS_CS_READY);
758 return 0;
759
760error:
761 nfs_mark_client_ready(clp, error);
762 dprintk("<-- nfs_init_client() = xerror %d\n", error);
763 return error;
764}
765
766/*
767 * Create a version 2 or 3 client
768 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400769static int nfs_init_server(struct nfs_server *server,
770 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400771{
Trond Myklebust3a498022007-12-14 14:56:04 -0500772 struct nfs_client_initdata cl_init = {
773 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500774 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500775 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500776 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500777 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500778 };
Trond Myklebust33170232007-12-20 16:03:59 -0500779 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400780 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500781 int error;
David Howells54ceac42006-08-22 20:06:13 -0400782
783 dprintk("--> nfs_init_server()\n");
784
785#ifdef CONFIG_NFS_V3
786 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500787 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400788#endif
789
790 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500791 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400792 if (IS_ERR(clp)) {
793 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
794 return PTR_ERR(clp);
795 }
796
Trond Myklebust33170232007-12-20 16:03:59 -0500797 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
798 data->timeo, data->retrans);
799 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400800 if (error < 0)
801 goto error;
802
803 server->nfs_client = clp;
804
805 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400806 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100807 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400808
809 if (data->rsize)
810 server->rsize = nfs_block_size(data->rsize, NULL);
811 if (data->wsize)
812 server->wsize = nfs_block_size(data->wsize, NULL);
813
814 server->acregmin = data->acregmin * HZ;
815 server->acregmax = data->acregmax * HZ;
816 server->acdirmin = data->acdirmin * HZ;
817 server->acdirmax = data->acdirmax * HZ;
818
819 /* Start lockd here, before we might error out */
820 error = nfs_start_lockd(server);
821 if (error < 0)
822 goto error;
823
Chuck Leverf22d6d72008-03-14 14:10:22 -0400824 server->port = data->nfs_server.port;
825
Trond Myklebust33170232007-12-20 16:03:59 -0500826 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400827 if (error < 0)
828 goto error;
829
Chuck Lever3f8400d2008-03-14 14:10:30 -0400830 /* Preserve the values of mount_server-related mount options */
831 if (data->mount_server.addrlen) {
832 memcpy(&server->mountd_address, &data->mount_server.address,
833 data->mount_server.addrlen);
834 server->mountd_addrlen = data->mount_server.addrlen;
835 }
836 server->mountd_version = data->mount_server.version;
837 server->mountd_port = data->mount_server.port;
838 server->mountd_protocol = data->mount_server.protocol;
839
David Howells54ceac42006-08-22 20:06:13 -0400840 server->namelen = data->namlen;
841 /* Create a client RPC handle for the NFSv3 ACL management interface */
842 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400843 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
844 return 0;
845
846error:
847 server->nfs_client = NULL;
848 nfs_put_client(clp);
849 dprintk("<-- nfs_init_server() = xerror %d\n", error);
850 return error;
851}
852
853/*
854 * Load up the server record from information gained in an fsinfo record
855 */
856static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
857{
858 unsigned long max_rpc_payload;
859
860 /* Work out a lot of parameters */
861 if (server->rsize == 0)
862 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
863 if (server->wsize == 0)
864 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
865
866 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
867 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
868 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
869 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
870
871 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
872 if (server->rsize > max_rpc_payload)
873 server->rsize = max_rpc_payload;
874 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
875 server->rsize = NFS_MAX_FILE_IO_SIZE;
876 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700877
David Howells54ceac42006-08-22 20:06:13 -0400878 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
879
880 if (server->wsize > max_rpc_payload)
881 server->wsize = max_rpc_payload;
882 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
883 server->wsize = NFS_MAX_FILE_IO_SIZE;
884 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
885 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
886
887 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
888 if (server->dtsize > PAGE_CACHE_SIZE)
889 server->dtsize = PAGE_CACHE_SIZE;
890 if (server->dtsize > server->rsize)
891 server->dtsize = server->rsize;
892
893 if (server->flags & NFS_MOUNT_NOAC) {
894 server->acregmin = server->acregmax = 0;
895 server->acdirmin = server->acdirmax = 0;
896 }
897
898 server->maxfilesize = fsinfo->maxfilesize;
899
900 /* We're airborne Set socket buffersize */
901 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
902}
903
904/*
905 * Probe filesystem information, including the FSID on v2/v3
906 */
907static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
908{
909 struct nfs_fsinfo fsinfo;
910 struct nfs_client *clp = server->nfs_client;
911 int error;
912
913 dprintk("--> nfs_probe_fsinfo()\n");
914
915 if (clp->rpc_ops->set_capabilities != NULL) {
916 error = clp->rpc_ops->set_capabilities(server, mntfh);
917 if (error < 0)
918 goto out_error;
919 }
920
921 fsinfo.fattr = fattr;
922 nfs_fattr_init(fattr);
923 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
924 if (error < 0)
925 goto out_error;
926
927 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700928 error = bdi_init(&server->backing_dev_info);
929 if (error)
930 goto out_error;
931
David Howells54ceac42006-08-22 20:06:13 -0400932
933 /* Get some general file system info */
934 if (server->namelen == 0) {
935 struct nfs_pathconf pathinfo;
936
937 pathinfo.fattr = fattr;
938 nfs_fattr_init(fattr);
939
940 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
941 server->namelen = pathinfo.max_namelen;
942 }
943
944 dprintk("<-- nfs_probe_fsinfo() = 0\n");
945 return 0;
946
947out_error:
948 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
949 return error;
950}
951
952/*
953 * Copy useful information when duplicating a server record
954 */
955static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
956{
957 target->flags = source->flags;
958 target->acregmin = source->acregmin;
959 target->acregmax = source->acregmax;
960 target->acdirmin = source->acdirmin;
961 target->acdirmax = source->acdirmax;
962 target->caps = source->caps;
963}
964
965/*
966 * Allocate and initialise a server record
967 */
968static struct nfs_server *nfs_alloc_server(void)
969{
970 struct nfs_server *server;
971
972 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
973 if (!server)
974 return NULL;
975
976 server->client = server->client_acl = ERR_PTR(-EINVAL);
977
978 /* Zero out the NFS state stuff */
979 INIT_LIST_HEAD(&server->client_link);
980 INIT_LIST_HEAD(&server->master_link);
981
Steve Dicksonef818a22007-11-08 04:05:04 -0500982 atomic_set(&server->active, 0);
983
David Howells54ceac42006-08-22 20:06:13 -0400984 server->io_stats = nfs_alloc_iostats();
985 if (!server->io_stats) {
986 kfree(server);
987 return NULL;
988 }
989
990 return server;
991}
992
993/*
994 * Free up a server record
995 */
996void nfs_free_server(struct nfs_server *server)
997{
998 dprintk("--> nfs_free_server()\n");
999
1000 spin_lock(&nfs_client_lock);
1001 list_del(&server->client_link);
1002 list_del(&server->master_link);
1003 spin_unlock(&nfs_client_lock);
1004
1005 if (server->destroy != NULL)
1006 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001007
1008 if (!IS_ERR(server->client_acl))
1009 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001010 if (!IS_ERR(server->client))
1011 rpc_shutdown_client(server->client);
1012
1013 nfs_put_client(server->nfs_client);
1014
1015 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001016 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001017 kfree(server);
1018 nfs_release_automount_timer();
1019 dprintk("<-- nfs_free_server()\n");
1020}
1021
1022/*
1023 * Create a version 2 or 3 volume record
1024 * - keyed on server and FSID
1025 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001026struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001027 struct nfs_fh *mntfh)
1028{
1029 struct nfs_server *server;
1030 struct nfs_fattr fattr;
1031 int error;
1032
1033 server = nfs_alloc_server();
1034 if (!server)
1035 return ERR_PTR(-ENOMEM);
1036
1037 /* Get a client representation */
1038 error = nfs_init_server(server, data);
1039 if (error < 0)
1040 goto error;
1041
1042 BUG_ON(!server->nfs_client);
1043 BUG_ON(!server->nfs_client->rpc_ops);
1044 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1045
1046 /* Probe the root fh to retrieve its FSID */
1047 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1048 if (error < 0)
1049 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001050 if (server->nfs_client->rpc_ops->version == 3) {
1051 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1052 server->namelen = NFS3_MAXNAMLEN;
1053 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1054 server->caps |= NFS_CAP_READDIRPLUS;
1055 } else {
1056 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1057 server->namelen = NFS2_MAXNAMLEN;
1058 }
1059
David Howells54ceac42006-08-22 20:06:13 -04001060 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1061 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1062 if (error < 0) {
1063 dprintk("nfs_create_server: getattr error = %d\n", -error);
1064 goto error;
1065 }
1066 }
1067 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1068
David Howells6daabf12006-08-24 15:44:16 -04001069 dprintk("Server FSID: %llx:%llx\n",
1070 (unsigned long long) server->fsid.major,
1071 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001072
1073 BUG_ON(!server->nfs_client);
1074 BUG_ON(!server->nfs_client->rpc_ops);
1075 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1076
1077 spin_lock(&nfs_client_lock);
1078 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1079 list_add_tail(&server->master_link, &nfs_volume_list);
1080 spin_unlock(&nfs_client_lock);
1081
1082 server->mount_time = jiffies;
1083 return server;
1084
1085error:
1086 nfs_free_server(server);
1087 return ERR_PTR(error);
1088}
1089
1090#ifdef CONFIG_NFS_V4
1091/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001092 * Initialize the NFS4 callback service
1093 */
1094static int nfs4_init_callback(struct nfs_client *clp)
1095{
1096 int error;
1097
1098 if (clp->rpc_ops->version == 4) {
Benny Halevy71468512009-04-01 09:22:56 -04001099 error = nfs_callback_up(clp->cl_minorversion,
1100 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001101 if (error < 0) {
1102 dprintk("%s: failed to start callback. Error = %d\n",
1103 __func__, error);
1104 return error;
1105 }
1106 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1107 }
1108 return 0;
1109}
1110
1111/*
Andy Adamson557134a2009-04-01 09:21:53 -04001112 * Initialize the minor version specific parts of an NFS4 client record
1113 */
1114static int nfs4_init_client_minor_version(struct nfs_client *clp)
1115{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001116 clp->cl_call_sync = _nfs4_call_sync;
1117
Andy Adamson557134a2009-04-01 09:21:53 -04001118#if defined(CONFIG_NFS_V4_1)
1119 if (clp->cl_minorversion) {
1120 struct nfs4_session *session = NULL;
1121 /*
1122 * Create the session and mark it expired.
1123 * When a SEQUENCE operation encounters the expired session
1124 * it will do session recovery to initialize it.
1125 */
1126 session = nfs4_alloc_session(clp);
1127 if (!session)
1128 return -ENOMEM;
1129
1130 clp->cl_session = session;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001131 clp->cl_call_sync = _nfs4_call_sync_session;
Andy Adamson557134a2009-04-01 09:21:53 -04001132 }
1133#endif /* CONFIG_NFS_V4_1 */
1134
Benny Halevy71468512009-04-01 09:22:56 -04001135 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001136}
1137
1138/*
David Howells54ceac42006-08-22 20:06:13 -04001139 * Initialise an NFS4 client record
1140 */
1141static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001142 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001143 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001144 rpc_authflavor_t authflavour,
1145 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001146{
1147 int error;
1148
1149 if (clp->cl_cons_state == NFS_CS_READY) {
1150 /* the client is initialised already */
1151 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1152 return 0;
1153 }
1154
1155 /* Check NFS protocol revision and initialize RPC op vector */
1156 clp->rpc_ops = &nfs_v4_clientops;
1157
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001158 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001159 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001160 if (error < 0)
1161 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001162 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001163
1164 error = nfs_idmap_new(clp);
1165 if (error < 0) {
1166 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001167 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001168 goto error;
1169 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001170 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001171
Andy Adamson557134a2009-04-01 09:21:53 -04001172 error = nfs4_init_client_minor_version(clp);
1173 if (error < 0)
1174 goto error;
1175
Andy Adamson76db6d92009-04-01 09:22:38 -04001176 if (!nfs4_has_session(clp))
1177 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001178 return 0;
1179
1180error:
1181 nfs_mark_client_ready(clp, error);
1182 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1183 return error;
1184}
1185
1186/*
1187 * Set up an NFS4 client
1188 */
1189static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001190 const char *hostname,
1191 const struct sockaddr *addr,
1192 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001193 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001194 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001195 int proto, const struct rpc_timeout *timeparms,
1196 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001197{
Trond Myklebust3a498022007-12-14 14:56:04 -05001198 struct nfs_client_initdata cl_init = {
1199 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001200 .addr = addr,
1201 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001202 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001203 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001204 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001205 };
David Howells54ceac42006-08-22 20:06:13 -04001206 struct nfs_client *clp;
1207 int error;
1208
1209 dprintk("--> nfs4_set_client()\n");
1210
1211 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001212 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001213 if (IS_ERR(clp)) {
1214 error = PTR_ERR(clp);
1215 goto error;
1216 }
Chuck Leverd7403512008-12-23 15:21:37 -05001217 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1218 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001219 if (error < 0)
1220 goto error_put;
1221
1222 server->nfs_client = clp;
1223 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1224 return 0;
1225
1226error_put:
1227 nfs_put_client(clp);
1228error:
1229 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1230 return error;
1231}
1232
1233/*
Andy Adamson557134a2009-04-01 09:21:53 -04001234 * Initialize a session.
1235 * Note: save the mount rsize and wsize for create_server negotiation.
1236 */
1237static void nfs4_init_session(struct nfs_client *clp,
1238 unsigned int wsize, unsigned int rsize)
1239{
1240#if defined(CONFIG_NFS_V4_1)
1241 if (nfs4_has_session(clp)) {
1242 clp->cl_session->fc_attrs.max_rqst_sz = wsize;
1243 clp->cl_session->fc_attrs.max_resp_sz = rsize;
1244 }
1245#endif /* CONFIG_NFS_V4_1 */
1246}
1247
1248/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001249 * Session has been established, and the client marked ready.
1250 * Set the mount rsize and wsize with negotiated fore channel
1251 * attributes which will be bound checked in nfs_server_set_fsinfo.
1252 */
1253static void nfs4_session_set_rwsize(struct nfs_server *server)
1254{
1255#ifdef CONFIG_NFS_V4_1
1256 if (!nfs4_has_session(server->nfs_client))
1257 return;
1258 server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1259 server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
1260#endif /* CONFIG_NFS_V4_1 */
1261}
1262
1263/*
David Howells54ceac42006-08-22 20:06:13 -04001264 * Create a version 4 volume record
1265 */
1266static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001267 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001268{
Trond Myklebust33170232007-12-20 16:03:59 -05001269 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001270 int error;
1271
1272 dprintk("--> nfs4_init_server()\n");
1273
Trond Myklebust33170232007-12-20 16:03:59 -05001274 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1275 data->timeo, data->retrans);
1276
Chuck Lever542fcc32008-12-23 15:21:36 -05001277 /* Initialise the client representation from the mount data */
1278 server->flags = data->flags;
1279 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001280 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001281
Trond Myklebust33170232007-12-20 16:03:59 -05001282 /* Get a client record */
1283 error = nfs4_set_client(server,
1284 data->nfs_server.hostname,
1285 (const struct sockaddr *)&data->nfs_server.address,
1286 data->nfs_server.addrlen,
1287 data->client_address,
1288 data->auth_flavors[0],
1289 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001290 &timeparms,
1291 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001292 if (error < 0)
1293 goto error;
1294
David Howells54ceac42006-08-22 20:06:13 -04001295 if (data->rsize)
1296 server->rsize = nfs_block_size(data->rsize, NULL);
1297 if (data->wsize)
1298 server->wsize = nfs_block_size(data->wsize, NULL);
1299
1300 server->acregmin = data->acregmin * HZ;
1301 server->acregmax = data->acregmax * HZ;
1302 server->acdirmin = data->acdirmin * HZ;
1303 server->acdirmax = data->acdirmax * HZ;
1304
Chuck Leverf22d6d72008-03-14 14:10:22 -04001305 server->port = data->nfs_server.port;
1306
Trond Myklebust33170232007-12-20 16:03:59 -05001307 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001308
Trond Myklebust33170232007-12-20 16:03:59 -05001309error:
David Howells54ceac42006-08-22 20:06:13 -04001310 /* Done */
1311 dprintk("<-- nfs4_init_server() = %d\n", error);
1312 return error;
1313}
1314
1315/*
1316 * Create a version 4 volume record
1317 * - keyed on server and FSID
1318 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001319struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001320 struct nfs_fh *mntfh)
1321{
1322 struct nfs_fattr fattr;
1323 struct nfs_server *server;
1324 int error;
1325
1326 dprintk("--> nfs4_create_server()\n");
1327
1328 server = nfs_alloc_server();
1329 if (!server)
1330 return ERR_PTR(-ENOMEM);
1331
David Howells54ceac42006-08-22 20:06:13 -04001332 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001333 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001334 if (error < 0)
1335 goto error;
1336
1337 BUG_ON(!server->nfs_client);
1338 BUG_ON(!server->nfs_client->rpc_ops);
1339 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1340
Andy Adamson557134a2009-04-01 09:21:53 -04001341 nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
1342
David Howells54ceac42006-08-22 20:06:13 -04001343 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001344 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001345 if (error < 0)
1346 goto error;
1347
David Howells6daabf12006-08-24 15:44:16 -04001348 dprintk("Server FSID: %llx:%llx\n",
1349 (unsigned long long) server->fsid.major,
1350 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001351 dprintk("Mount FH: %d\n", mntfh->size);
1352
Andy Adamson96b09e02009-04-01 09:22:33 -04001353 nfs4_session_set_rwsize(server);
1354
David Howells54ceac42006-08-22 20:06:13 -04001355 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1356 if (error < 0)
1357 goto error;
1358
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001359 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1360 server->namelen = NFS4_MAXNAMLEN;
1361
David Howells54ceac42006-08-22 20:06:13 -04001362 BUG_ON(!server->nfs_client);
1363 BUG_ON(!server->nfs_client->rpc_ops);
1364 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1365
1366 spin_lock(&nfs_client_lock);
1367 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1368 list_add_tail(&server->master_link, &nfs_volume_list);
1369 spin_unlock(&nfs_client_lock);
1370
1371 server->mount_time = jiffies;
1372 dprintk("<-- nfs4_create_server() = %p\n", server);
1373 return server;
1374
1375error:
1376 nfs_free_server(server);
1377 dprintk("<-- nfs4_create_server() = error %d\n", error);
1378 return ERR_PTR(error);
1379}
1380
1381/*
1382 * Create an NFS4 referral server record
1383 */
1384struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001385 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001386{
1387 struct nfs_client *parent_client;
1388 struct nfs_server *server, *parent_server;
1389 struct nfs_fattr fattr;
1390 int error;
1391
1392 dprintk("--> nfs4_create_referral_server()\n");
1393
1394 server = nfs_alloc_server();
1395 if (!server)
1396 return ERR_PTR(-ENOMEM);
1397
1398 parent_server = NFS_SB(data->sb);
1399 parent_client = parent_server->nfs_client;
1400
Chuck Lever542fcc32008-12-23 15:21:36 -05001401 /* Initialise the client representation from the parent server */
1402 nfs_server_copy_userdata(server, parent_server);
1403 server->caps |= NFS_CAP_ATOMIC_OPEN;
1404
David Howells54ceac42006-08-22 20:06:13 -04001405 /* Get a client representation.
1406 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001407 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001408 data->addr,
1409 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001410 parent_client->cl_ipaddr,
1411 data->authflavor,
1412 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001413 parent_server->client->cl_timeout,
1414 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001415 if (error < 0)
1416 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001417
Trond Myklebust33170232007-12-20 16:03:59 -05001418 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001419 if (error < 0)
1420 goto error;
1421
1422 BUG_ON(!server->nfs_client);
1423 BUG_ON(!server->nfs_client->rpc_ops);
1424 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1425
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001426 /* Probe the root fh to retrieve its FSID and filehandle */
1427 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1428 if (error < 0)
1429 goto error;
1430
David Howells54ceac42006-08-22 20:06:13 -04001431 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001432 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001433 if (error < 0)
1434 goto error;
1435
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001436 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1437 server->namelen = NFS4_MAXNAMLEN;
1438
David Howells54ceac42006-08-22 20:06:13 -04001439 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001440 (unsigned long long) server->fsid.major,
1441 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001442
1443 spin_lock(&nfs_client_lock);
1444 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1445 list_add_tail(&server->master_link, &nfs_volume_list);
1446 spin_unlock(&nfs_client_lock);
1447
1448 server->mount_time = jiffies;
1449
1450 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1451 return server;
1452
1453error:
1454 nfs_free_server(server);
1455 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1456 return ERR_PTR(error);
1457}
1458
1459#endif /* CONFIG_NFS_V4 */
1460
1461/*
1462 * Clone an NFS2, NFS3 or NFS4 server record
1463 */
1464struct nfs_server *nfs_clone_server(struct nfs_server *source,
1465 struct nfs_fh *fh,
1466 struct nfs_fattr *fattr)
1467{
1468 struct nfs_server *server;
1469 struct nfs_fattr fattr_fsinfo;
1470 int error;
1471
1472 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001473 (unsigned long long) fattr->fsid.major,
1474 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001475
1476 server = nfs_alloc_server();
1477 if (!server)
1478 return ERR_PTR(-ENOMEM);
1479
1480 /* Copy data from the source */
1481 server->nfs_client = source->nfs_client;
1482 atomic_inc(&server->nfs_client->cl_count);
1483 nfs_server_copy_userdata(server, source);
1484
1485 server->fsid = fattr->fsid;
1486
Trond Myklebust33170232007-12-20 16:03:59 -05001487 error = nfs_init_server_rpcclient(server,
1488 source->client->cl_timeout,
1489 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001490 if (error < 0)
1491 goto out_free_server;
1492 if (!IS_ERR(source->client_acl))
1493 nfs_init_server_aclclient(server);
1494
1495 /* probe the filesystem info for this server filesystem */
1496 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1497 if (error < 0)
1498 goto out_free_server;
1499
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001500 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1501 server->namelen = NFS4_MAXNAMLEN;
1502
David Howells54ceac42006-08-22 20:06:13 -04001503 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001504 (unsigned long long) server->fsid.major,
1505 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001506
1507 error = nfs_start_lockd(server);
1508 if (error < 0)
1509 goto out_free_server;
1510
1511 spin_lock(&nfs_client_lock);
1512 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1513 list_add_tail(&server->master_link, &nfs_volume_list);
1514 spin_unlock(&nfs_client_lock);
1515
1516 server->mount_time = jiffies;
1517
1518 dprintk("<-- nfs_clone_server() = %p\n", server);
1519 return server;
1520
1521out_free_server:
1522 nfs_free_server(server);
1523 dprintk("<-- nfs_clone_server() = error %d\n", error);
1524 return ERR_PTR(error);
1525}
David Howells6aaca562006-08-22 20:06:13 -04001526
1527#ifdef CONFIG_PROC_FS
1528static struct proc_dir_entry *proc_fs_nfs;
1529
1530static int nfs_server_list_open(struct inode *inode, struct file *file);
1531static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1532static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1533static void nfs_server_list_stop(struct seq_file *p, void *v);
1534static int nfs_server_list_show(struct seq_file *m, void *v);
1535
1536static struct seq_operations nfs_server_list_ops = {
1537 .start = nfs_server_list_start,
1538 .next = nfs_server_list_next,
1539 .stop = nfs_server_list_stop,
1540 .show = nfs_server_list_show,
1541};
1542
Arjan van de Ven00977a52007-02-12 00:55:34 -08001543static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001544 .open = nfs_server_list_open,
1545 .read = seq_read,
1546 .llseek = seq_lseek,
1547 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001548 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001549};
1550
1551static int nfs_volume_list_open(struct inode *inode, struct file *file);
1552static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1553static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1554static void nfs_volume_list_stop(struct seq_file *p, void *v);
1555static int nfs_volume_list_show(struct seq_file *m, void *v);
1556
1557static struct seq_operations nfs_volume_list_ops = {
1558 .start = nfs_volume_list_start,
1559 .next = nfs_volume_list_next,
1560 .stop = nfs_volume_list_stop,
1561 .show = nfs_volume_list_show,
1562};
1563
Arjan van de Ven00977a52007-02-12 00:55:34 -08001564static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001565 .open = nfs_volume_list_open,
1566 .read = seq_read,
1567 .llseek = seq_lseek,
1568 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001569 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001570};
1571
1572/*
1573 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1574 * we're dealing
1575 */
1576static int nfs_server_list_open(struct inode *inode, struct file *file)
1577{
1578 struct seq_file *m;
1579 int ret;
1580
1581 ret = seq_open(file, &nfs_server_list_ops);
1582 if (ret < 0)
1583 return ret;
1584
1585 m = file->private_data;
1586 m->private = PDE(inode)->data;
1587
1588 return 0;
1589}
1590
1591/*
1592 * set up the iterator to start reading from the server list and return the first item
1593 */
1594static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1595{
David Howells6aaca562006-08-22 20:06:13 -04001596 /* lock the list against modification */
1597 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001598 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001599}
1600
1601/*
1602 * move to next server
1603 */
1604static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1605{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001606 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001607}
1608
1609/*
1610 * clean up after reading from the transports list
1611 */
1612static void nfs_server_list_stop(struct seq_file *p, void *v)
1613{
1614 spin_unlock(&nfs_client_lock);
1615}
1616
1617/*
1618 * display a header line followed by a load of call lines
1619 */
1620static int nfs_server_list_show(struct seq_file *m, void *v)
1621{
1622 struct nfs_client *clp;
1623
1624 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001625 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001626 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1627 return 0;
1628 }
1629
1630 /* display one transport per line on subsequent lines */
1631 clp = list_entry(v, struct nfs_client, cl_share_link);
1632
Chuck Lever5d8515c2007-12-10 14:57:16 -05001633 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001634 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001635 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1636 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001637 atomic_read(&clp->cl_count),
1638 clp->cl_hostname);
1639
1640 return 0;
1641}
1642
1643/*
1644 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1645 */
1646static int nfs_volume_list_open(struct inode *inode, struct file *file)
1647{
1648 struct seq_file *m;
1649 int ret;
1650
1651 ret = seq_open(file, &nfs_volume_list_ops);
1652 if (ret < 0)
1653 return ret;
1654
1655 m = file->private_data;
1656 m->private = PDE(inode)->data;
1657
1658 return 0;
1659}
1660
1661/*
1662 * set up the iterator to start reading from the volume list and return the first item
1663 */
1664static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1665{
David Howells6aaca562006-08-22 20:06:13 -04001666 /* lock the list against modification */
1667 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001668 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001669}
1670
1671/*
1672 * move to next volume
1673 */
1674static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1675{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001676 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001677}
1678
1679/*
1680 * clean up after reading from the transports list
1681 */
1682static void nfs_volume_list_stop(struct seq_file *p, void *v)
1683{
1684 spin_unlock(&nfs_client_lock);
1685}
1686
1687/*
1688 * display a header line followed by a load of call lines
1689 */
1690static int nfs_volume_list_show(struct seq_file *m, void *v)
1691{
1692 struct nfs_server *server;
1693 struct nfs_client *clp;
1694 char dev[8], fsid[17];
1695
1696 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001697 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001698 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001699 return 0;
1700 }
1701 /* display one transport per line on subsequent lines */
1702 server = list_entry(v, struct nfs_server, master_link);
1703 clp = server->nfs_client;
1704
1705 snprintf(dev, 8, "%u:%u",
1706 MAJOR(server->s_dev), MINOR(server->s_dev));
1707
1708 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001709 (unsigned long long) server->fsid.major,
1710 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001711
David Howells5d1acff2009-04-03 16:42:47 +01001712 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001713 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001714 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1715 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001716 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001717 fsid,
1718 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001719
1720 return 0;
1721}
1722
1723/*
1724 * initialise the /proc/fs/nfsfs/ directory
1725 */
1726int __init nfs_fs_proc_init(void)
1727{
1728 struct proc_dir_entry *p;
1729
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001730 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001731 if (!proc_fs_nfs)
1732 goto error_0;
1733
David Howells6aaca562006-08-22 20:06:13 -04001734 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001735 p = proc_create("servers", S_IFREG|S_IRUGO,
1736 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001737 if (!p)
1738 goto error_1;
1739
David Howells6aaca562006-08-22 20:06:13 -04001740 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001741 p = proc_create("volumes", S_IFREG|S_IRUGO,
1742 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001743 if (!p)
1744 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001745 return 0;
1746
1747error_2:
1748 remove_proc_entry("servers", proc_fs_nfs);
1749error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001750 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001751error_0:
1752 return -ENOMEM;
1753}
1754
1755/*
1756 * clean up the /proc/fs/nfsfs/ directory
1757 */
1758void nfs_fs_proc_exit(void)
1759{
1760 remove_proc_entry("volumes", proc_fs_nfs);
1761 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001762 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001763}
1764
1765#endif /* CONFIG_PROC_FS */