blob: 8f34fd8028fc730fc39fcfb9dd499c9dfe3d730e [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>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040040#include <linux/sunrpc/bc_xprt.h>
David Howells24c8dbb2006-08-22 20:06:10 -040041
42#include <asm/system.h>
43
44#include "nfs4_fs.h"
45#include "callback.h"
46#include "delegation.h"
47#include "iostat.h"
48#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010049#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040050
51#define NFSDBG_FACILITY NFSDBG_CLIENT
52
53static DEFINE_SPINLOCK(nfs_client_lock);
54static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040055static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040056static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
57
58/*
David Howells5006a762006-08-22 20:06:12 -040059 * RPC cruft for NFS
60 */
61static struct rpc_version *nfs_version[5] = {
62 [2] = &nfs_version2,
63#ifdef CONFIG_NFS_V3
64 [3] = &nfs_version3,
65#endif
66#ifdef CONFIG_NFS_V4
67 [4] = &nfs_version4,
68#endif
69};
70
71struct rpc_program nfs_program = {
72 .name = "nfs",
73 .number = NFS_PROGRAM,
74 .nrvers = ARRAY_SIZE(nfs_version),
75 .version = nfs_version,
76 .stats = &nfs_rpcstat,
77 .pipe_dir_name = "/nfs",
78};
79
80struct rpc_stat nfs_rpcstat = {
81 .program = &nfs_program
82};
83
84
85#ifdef CONFIG_NFS_V3_ACL
86static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
87static struct rpc_version * nfsacl_version[] = {
88 [3] = &nfsacl_version3,
89};
90
91struct rpc_program nfsacl_program = {
92 .name = "nfsacl",
93 .number = NFS_ACL_PROGRAM,
94 .nrvers = ARRAY_SIZE(nfsacl_version),
95 .version = nfsacl_version,
96 .stats = &nfsacl_rpcstat,
97};
98#endif /* CONFIG_NFS_V3_ACL */
99
Trond Myklebust3a498022007-12-14 14:56:04 -0500100struct nfs_client_initdata {
101 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500102 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500103 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500104 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500105 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400106 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500107};
108
David Howells5006a762006-08-22 20:06:12 -0400109/*
David Howells24c8dbb2006-08-22 20:06:10 -0400110 * Allocate a shared client record
111 *
112 * Since these are allocated/deallocated very rarely, we don't
113 * bother putting them in a slab cache...
114 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500115static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400116{
117 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400118 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700119 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400120
121 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
122 goto error_0;
123
Trond Myklebust40c553192007-12-14 14:56:07 -0500124 clp->rpc_ops = cl_init->rpc_ops;
125
David Howells24c8dbb2006-08-22 20:06:10 -0400126 atomic_set(&clp->cl_count, 1);
127 clp->cl_cons_state = NFS_CS_INITING;
128
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500129 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
130 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400131
Trond Myklebust3a498022007-12-14 14:56:04 -0500132 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700133 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500134 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400135 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400136 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400137 }
138
139 INIT_LIST_HEAD(&clp->cl_superblocks);
140 clp->cl_rpcclient = ERR_PTR(-EINVAL);
141
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500142 clp->cl_proto = cl_init->proto;
143
David Howells24c8dbb2006-08-22 20:06:10 -0400144#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400145 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400146 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000147 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400148 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
149 clp->cl_boot_time = CURRENT_TIME;
150 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400151 clp->cl_minorversion = cl_init->minorversion;
David Howells24c8dbb2006-08-22 20:06:10 -0400152#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
David Howells14727282009-04-03 16:42:42 +0100157 nfs_fscache_get_client_cookie(clp);
158
David Howells24c8dbb2006-08-22 20:06:10 -0400159 return clp;
160
Benny Halevy71468512009-04-01 09:22:56 -0400161error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400162 kfree(clp);
163error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700164 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400165}
166
Trond Myklebust5dd31772006-08-24 01:03:05 -0400167static void nfs4_shutdown_client(struct nfs_client *clp)
168{
169#ifdef CONFIG_NFS_V4
170 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
171 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400172 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400173 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
174 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500175
176 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400177#endif
178}
179
David Howells24c8dbb2006-08-22 20:06:10 -0400180/*
Benny Halevy9bdaa862009-04-01 09:22:55 -0400181 * Destroy the NFS4 callback service
182 */
183static void nfs4_destroy_callback(struct nfs_client *clp)
184{
185#ifdef CONFIG_NFS_V4
186 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Andy Adamsone82dc222009-04-01 09:23:14 -0400187 nfs_callback_down(clp->cl_minorversion);
Benny Halevy9bdaa862009-04-01 09:22:55 -0400188#endif /* CONFIG_NFS_V4 */
189}
190
191/*
Andy Adamson557134a2009-04-01 09:21:53 -0400192 * Clears/puts all minor version specific parts from an nfs_client struct
193 * reverting it to minorversion 0.
194 */
195static void nfs4_clear_client_minor_version(struct nfs_client *clp)
196{
197#ifdef CONFIG_NFS_V4_1
198 if (nfs4_has_session(clp)) {
199 nfs4_destroy_session(clp->cl_session);
200 clp->cl_session = NULL;
201 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400202
203 clp->cl_call_sync = _nfs4_call_sync;
Andy Adamson557134a2009-04-01 09:21:53 -0400204#endif /* CONFIG_NFS_V4_1 */
Benny Halevy71468512009-04-01 09:22:56 -0400205
206 nfs4_destroy_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -0400207}
208
209/*
David Howells24c8dbb2006-08-22 20:06:10 -0400210 * Destroy a shared client record
211 */
212static void nfs_free_client(struct nfs_client *clp)
213{
Trond Myklebust40c553192007-12-14 14:56:07 -0500214 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400215
Andy Adamson557134a2009-04-01 09:21:53 -0400216 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400217 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400218
David Howells14727282009-04-03 16:42:42 +0100219 nfs_fscache_release_client_cookie(clp);
220
David Howells24c8dbb2006-08-22 20:06:10 -0400221 /* -EIO all pending I/O */
222 if (!IS_ERR(clp->cl_rpcclient))
223 rpc_shutdown_client(clp->cl_rpcclient);
224
Trond Myklebust7c67db32008-04-07 20:50:11 -0400225 if (clp->cl_machine_cred != NULL)
226 put_rpccred(clp->cl_machine_cred);
227
David Howells24c8dbb2006-08-22 20:06:10 -0400228 kfree(clp->cl_hostname);
229 kfree(clp);
230
231 dprintk("<-- nfs_free_client()\n");
232}
233
234/*
235 * Release a reference to a shared client record
236 */
237void nfs_put_client(struct nfs_client *clp)
238{
David Howells27ba8512006-07-30 14:40:56 -0400239 if (!clp)
240 return;
241
David Howells24c8dbb2006-08-22 20:06:10 -0400242 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
243
244 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
245 list_del(&clp->cl_share_link);
246 spin_unlock(&nfs_client_lock);
247
248 BUG_ON(!list_empty(&clp->cl_superblocks));
249
250 nfs_free_client(clp);
251 }
252}
253
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500254#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400255/*
256 * Test if two ip6 socket addresses refer to the same socket by
257 * comparing relevant fields. The padding bytes specifically, are not
258 * compared. sin6_flowinfo is not compared because it only affects QoS
259 * and sin6_scope_id is only compared if the address is "link local"
260 * because "link local" addresses need only be unique to a specific
261 * link. Conversely, ordinary unicast addresses might have different
262 * sin6_scope_id.
263 *
264 * The caller should ensure both socket addresses are AF_INET6.
265 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400266static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
267 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400268{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400269 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
270 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400271
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
273 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400274 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500275
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400276 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500277}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400278#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
279static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
280 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400281{
282 return 0;
283}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500284#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500285
David Howells24c8dbb2006-08-22 20:06:10 -0400286/*
Ian Dalld7371c42009-03-10 20:33:22 -0400287 * Test if two ip4 socket addresses refer to the same socket, by
288 * comparing relevant fields. The padding bytes specifically, are
289 * not compared.
290 *
291 * The caller should ensure both socket addresses are AF_INET.
292 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400293static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
294 const struct sockaddr *sa2)
295{
296 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
297 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
298
299 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
300}
301
302static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
303 const struct sockaddr *sa2)
304{
305 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
306 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
307
308 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
309 (sin1->sin6_port == sin2->sin6_port);
310}
311
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400312static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
313 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400314{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400315 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
316 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400317
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400318 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
319 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400320}
321
322/*
Ian Dalld7371c42009-03-10 20:33:22 -0400323 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400324 * by comparing (only) relevant fields, excluding the port number.
325 */
326static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
327 const struct sockaddr *sa2)
328{
329 if (sa1->sa_family != sa2->sa_family)
330 return 0;
331
332 switch (sa1->sa_family) {
333 case AF_INET:
334 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
335 case AF_INET6:
336 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
337 }
338 return 0;
339}
340
341/*
342 * Test if two socket addresses represent the same actual socket,
343 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400344 */
345static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
346 const struct sockaddr *sa2)
347{
348 if (sa1->sa_family != sa2->sa_family)
349 return 0;
350
351 switch (sa1->sa_family) {
352 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400353 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400354 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400355 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400356 }
357 return 0;
358}
359
360/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500361 * Find a client by IP address and protocol version
362 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400363 */
Chuck Leverff052642007-12-10 14:58:44 -0500364struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500365{
366 struct nfs_client *clp;
367
368 spin_lock(&nfs_client_lock);
369 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500370 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
371
Trond Myklebustc81468a2007-12-14 14:56:05 -0500372 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400373 if (!(clp->cl_cons_state == NFS_CS_READY ||
374 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500375 continue;
376
377 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500378 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500379 continue;
380
381 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500382 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500383 continue;
384
385 atomic_inc(&clp->cl_count);
386 spin_unlock(&nfs_client_lock);
387 return clp;
388 }
389 spin_unlock(&nfs_client_lock);
390 return NULL;
391}
392
393/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500394 * Find a client by IP address and protocol version
395 * - returns NULL if no such client
396 */
397struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
398{
399 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
400 u32 nfsvers = clp->rpc_ops->version;
401
402 spin_lock(&nfs_client_lock);
403 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
404 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
405
406 /* Don't match clients that failed to initialise properly */
407 if (clp->cl_cons_state != NFS_CS_READY)
408 continue;
409
410 /* Different NFS versions cannot share the same nfs_client */
411 if (clp->rpc_ops->version != nfsvers)
412 continue;
413
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500414 /* Match only the IP address, not the port number */
415 if (!nfs_sockaddr_match_ipaddr(sap, clap))
416 continue;
417
418 atomic_inc(&clp->cl_count);
419 spin_unlock(&nfs_client_lock);
420 return clp;
421 }
422 spin_unlock(&nfs_client_lock);
423 return NULL;
424}
425
426/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500427 * Find an nfs_client on the list that matches the initialisation data
428 * that is supplied.
429 */
430static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400431{
432 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400433 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400434
435 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400436 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700437 /* Don't match clients that failed to initialise properly */
438 if (clp->cl_cons_state < 0)
439 continue;
440
David Howells24c8dbb2006-08-22 20:06:10 -0400441 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500442 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400443 continue;
444
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500445 if (clp->cl_proto != data->proto)
446 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400447 /* Match nfsv4 minorversion */
448 if (clp->cl_minorversion != data->minorversion)
449 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500450 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400451 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400452 continue;
453
Trond Myklebustc81468a2007-12-14 14:56:05 -0500454 atomic_inc(&clp->cl_count);
455 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400456 }
David Howells24c8dbb2006-08-22 20:06:10 -0400457 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400458}
459
460/*
461 * Look up a client by IP address and protocol version
462 * - creates a new record if one doesn't yet exist
463 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500464static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400465{
466 struct nfs_client *clp, *new = NULL;
467 int error;
468
Chuck Leverd7422c42007-12-10 14:58:51 -0500469 dprintk("--> nfs_get_client(%s,v%u)\n",
470 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400471
472 /* see if the client already exists */
473 do {
474 spin_lock(&nfs_client_lock);
475
Trond Myklebustc81468a2007-12-14 14:56:05 -0500476 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400477 if (clp)
478 goto found_client;
479 if (new)
480 goto install_client;
481
482 spin_unlock(&nfs_client_lock);
483
Trond Myklebust3a498022007-12-14 14:56:04 -0500484 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700485 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400486
Chuck Levera21bdd92009-06-17 18:02:10 -0700487 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
488 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400489
490 /* install a new client and return with it unready */
491install_client:
492 clp = new;
493 list_add(&clp->cl_share_link, &nfs_client_list);
494 spin_unlock(&nfs_client_lock);
495 dprintk("--> nfs_get_client() = %p [new]\n", clp);
496 return clp;
497
498 /* found an existing client
499 * - make sure it's ready before returning
500 */
501found_client:
502 spin_unlock(&nfs_client_lock);
503
504 if (new)
505 nfs_free_client(new);
506
Matthew Wilcox150030b2007-12-06 16:24:39 -0500507 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400508 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400509 if (error < 0) {
510 nfs_put_client(clp);
511 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400512 }
513
514 if (clp->cl_cons_state < NFS_CS_READY) {
515 error = clp->cl_cons_state;
516 nfs_put_client(clp);
517 return ERR_PTR(error);
518 }
519
David Howells54ceac42006-08-22 20:06:13 -0400520 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
521
David Howells24c8dbb2006-08-22 20:06:10 -0400522 dprintk("--> nfs_get_client() = %p [share]\n", clp);
523 return clp;
524}
525
526/*
527 * Mark a server as ready or failed
528 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400529void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400530{
531 clp->cl_cons_state = state;
532 wake_up_all(&nfs_client_active_wq);
533}
David Howells5006a762006-08-22 20:06:12 -0400534
535/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400536 * With sessions, the client is not marked ready until after a
537 * successful EXCHANGE_ID and CREATE_SESSION.
538 *
539 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
540 * other versions of NFS can be tried.
541 */
542int nfs4_check_client_ready(struct nfs_client *clp)
543{
544 if (!nfs4_has_session(clp))
545 return 0;
546 if (clp->cl_cons_state < NFS_CS_READY)
547 return -EPROTONOSUPPORT;
548 return 0;
549}
550
551/*
David Howells5006a762006-08-22 20:06:12 -0400552 * Initialise the timeout values for a connection
553 */
554static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
555 unsigned int timeo, unsigned int retrans)
556{
557 to->to_initval = timeo * HZ / 10;
558 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400559
560 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400561 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400562 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400563 if (to->to_retries == 0)
564 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500565 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400566 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400567 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
568 to->to_initval = NFS_MAX_TCP_TIMEOUT;
569 to->to_increment = to->to_initval;
570 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500571 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
572 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
573 if (to->to_maxval < to->to_initval)
574 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400575 to->to_exponential = 0;
576 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400577 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400578 if (to->to_retries == 0)
579 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400580 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400581 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400582 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
583 to->to_initval = NFS_MAX_UDP_TIMEOUT;
584 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
585 to->to_exponential = 1;
586 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400587 default:
588 BUG();
David Howells5006a762006-08-22 20:06:12 -0400589 }
590}
591
592/*
593 * Create an RPC client handle
594 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500595static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500596 const struct rpc_timeout *timeparms,
597 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500598 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400599{
David Howells5006a762006-08-22 20:06:12 -0400600 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400601 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500602 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400603 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500604 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500605 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400606 .servername = clp->cl_hostname,
607 .program = &nfs_program,
608 .version = clp->rpc_ops->version,
609 .authflavor = flavor,
610 };
David Howells5006a762006-08-22 20:06:12 -0400611
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500612 if (discrtry)
613 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
614 if (noresvport)
615 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
616
David Howells5006a762006-08-22 20:06:12 -0400617 if (!IS_ERR(clp->cl_rpcclient))
618 return 0;
619
Chuck Lever41877d22006-08-22 20:06:20 -0400620 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400621 if (IS_ERR(clnt)) {
622 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700623 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400624 return PTR_ERR(clnt);
625 }
626
David Howells5006a762006-08-22 20:06:12 -0400627 clp->cl_rpcclient = clnt;
628 return 0;
629}
David Howells54ceac42006-08-22 20:06:13 -0400630
631/*
632 * Version 2 or 3 client destruction
633 */
634static void nfs_destroy_server(struct nfs_server *server)
635{
David Howells54ceac42006-08-22 20:06:13 -0400636 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500637 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400638}
639
640/*
641 * Version 2 or 3 lockd setup
642 */
643static int nfs_start_lockd(struct nfs_server *server)
644{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500645 struct nlm_host *host;
646 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500647 struct nlmclnt_initdata nlm_init = {
648 .hostname = clp->cl_hostname,
649 .address = (struct sockaddr *)&clp->cl_addr,
650 .addrlen = clp->cl_addrlen,
651 .protocol = server->flags & NFS_MOUNT_TCP ?
652 IPPROTO_TCP : IPPROTO_UDP,
653 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500654 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
655 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500656 };
David Howells54ceac42006-08-22 20:06:13 -0400657
Chuck Lever883bb162008-01-15 16:04:20 -0500658 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500659 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400660 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500661 return 0;
662
Chuck Lever883bb162008-01-15 16:04:20 -0500663 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500664 if (IS_ERR(host))
665 return PTR_ERR(host);
666
667 server->nlm_host = host;
668 server->destroy = nfs_destroy_server;
669 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400670}
671
672/*
673 * Initialise an NFSv3 ACL client connection
674 */
675#ifdef CONFIG_NFS_V3_ACL
676static void nfs_init_server_aclclient(struct nfs_server *server)
677{
Trond Myklebust40c553192007-12-14 14:56:07 -0500678 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400679 goto out_noacl;
680 if (server->flags & NFS_MOUNT_NOACL)
681 goto out_noacl;
682
683 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
684 if (IS_ERR(server->client_acl))
685 goto out_noacl;
686
687 /* No errors! Assume that Sun nfsacls are supported */
688 server->caps |= NFS_CAP_ACLS;
689 return;
690
691out_noacl:
692 server->caps &= ~NFS_CAP_ACLS;
693}
694#else
695static inline void nfs_init_server_aclclient(struct nfs_server *server)
696{
697 server->flags &= ~NFS_MOUNT_NOACL;
698 server->caps &= ~NFS_CAP_ACLS;
699}
700#endif
701
702/*
703 * Create a general RPC client
704 */
Trond Myklebust33170232007-12-20 16:03:59 -0500705static int nfs_init_server_rpcclient(struct nfs_server *server,
706 const struct rpc_timeout *timeo,
707 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400708{
709 struct nfs_client *clp = server->nfs_client;
710
711 server->client = rpc_clone_client(clp->cl_rpcclient);
712 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700713 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400714 return PTR_ERR(server->client);
715 }
716
Trond Myklebust33170232007-12-20 16:03:59 -0500717 memcpy(&server->client->cl_timeout_default,
718 timeo,
719 sizeof(server->client->cl_timeout_default));
720 server->client->cl_timeout = &server->client->cl_timeout_default;
721
David Howells54ceac42006-08-22 20:06:13 -0400722 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
723 struct rpc_auth *auth;
724
725 auth = rpcauth_create(pseudoflavour, server->client);
726 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700727 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400728 return PTR_ERR(auth);
729 }
730 }
731 server->client->cl_softrtry = 0;
732 if (server->flags & NFS_MOUNT_SOFT)
733 server->client->cl_softrtry = 1;
734
David Howells54ceac42006-08-22 20:06:13 -0400735 return 0;
736}
737
738/*
739 * Initialise an NFS2 or NFS3 client
740 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400741static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500742 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400743 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400744{
David Howells54ceac42006-08-22 20:06:13 -0400745 int error;
746
747 if (clp->cl_cons_state == NFS_CS_READY) {
748 /* the client is already initialised */
749 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
750 return 0;
751 }
752
David Howells54ceac42006-08-22 20:06:13 -0400753 /*
754 * Create a client RPC handle for doing FSSTAT with UNIX auth only
755 * - RFC 2623, sec 2.3.2
756 */
Chuck Leverd7403512008-12-23 15:21:37 -0500757 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
758 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400759 if (error < 0)
760 goto error;
761 nfs_mark_client_ready(clp, NFS_CS_READY);
762 return 0;
763
764error:
765 nfs_mark_client_ready(clp, error);
766 dprintk("<-- nfs_init_client() = xerror %d\n", error);
767 return error;
768}
769
770/*
771 * Create a version 2 or 3 client
772 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400773static int nfs_init_server(struct nfs_server *server,
774 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400775{
Trond Myklebust3a498022007-12-14 14:56:04 -0500776 struct nfs_client_initdata cl_init = {
777 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500778 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500779 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500780 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500781 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500782 };
Trond Myklebust33170232007-12-20 16:03:59 -0500783 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400784 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500785 int error;
David Howells54ceac42006-08-22 20:06:13 -0400786
787 dprintk("--> nfs_init_server()\n");
788
789#ifdef CONFIG_NFS_V3
790 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500791 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400792#endif
793
794 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500795 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400796 if (IS_ERR(clp)) {
797 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
798 return PTR_ERR(clp);
799 }
800
Trond Myklebust33170232007-12-20 16:03:59 -0500801 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
802 data->timeo, data->retrans);
803 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400804 if (error < 0)
805 goto error;
806
807 server->nfs_client = clp;
808
809 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400810 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100811 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400812 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
813 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
814 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400815
816 if (data->rsize)
817 server->rsize = nfs_block_size(data->rsize, NULL);
818 if (data->wsize)
819 server->wsize = nfs_block_size(data->wsize, NULL);
820
821 server->acregmin = data->acregmin * HZ;
822 server->acregmax = data->acregmax * HZ;
823 server->acdirmin = data->acdirmin * HZ;
824 server->acdirmax = data->acdirmax * HZ;
825
826 /* Start lockd here, before we might error out */
827 error = nfs_start_lockd(server);
828 if (error < 0)
829 goto error;
830
Chuck Leverf22d6d72008-03-14 14:10:22 -0400831 server->port = data->nfs_server.port;
832
Trond Myklebust33170232007-12-20 16:03:59 -0500833 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400834 if (error < 0)
835 goto error;
836
Chuck Lever3f8400d2008-03-14 14:10:30 -0400837 /* Preserve the values of mount_server-related mount options */
838 if (data->mount_server.addrlen) {
839 memcpy(&server->mountd_address, &data->mount_server.address,
840 data->mount_server.addrlen);
841 server->mountd_addrlen = data->mount_server.addrlen;
842 }
843 server->mountd_version = data->mount_server.version;
844 server->mountd_port = data->mount_server.port;
845 server->mountd_protocol = data->mount_server.protocol;
846
David Howells54ceac42006-08-22 20:06:13 -0400847 server->namelen = data->namlen;
848 /* Create a client RPC handle for the NFSv3 ACL management interface */
849 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400850 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
851 return 0;
852
853error:
854 server->nfs_client = NULL;
855 nfs_put_client(clp);
856 dprintk("<-- nfs_init_server() = xerror %d\n", error);
857 return error;
858}
859
860/*
861 * Load up the server record from information gained in an fsinfo record
862 */
863static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
864{
865 unsigned long max_rpc_payload;
866
867 /* Work out a lot of parameters */
868 if (server->rsize == 0)
869 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
870 if (server->wsize == 0)
871 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
872
873 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
874 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
875 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
876 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
877
878 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
879 if (server->rsize > max_rpc_payload)
880 server->rsize = max_rpc_payload;
881 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
882 server->rsize = NFS_MAX_FILE_IO_SIZE;
883 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700884
David Howells54ceac42006-08-22 20:06:13 -0400885 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
886
887 if (server->wsize > max_rpc_payload)
888 server->wsize = max_rpc_payload;
889 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
890 server->wsize = NFS_MAX_FILE_IO_SIZE;
891 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
892 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
893
894 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
895 if (server->dtsize > PAGE_CACHE_SIZE)
896 server->dtsize = PAGE_CACHE_SIZE;
897 if (server->dtsize > server->rsize)
898 server->dtsize = server->rsize;
899
900 if (server->flags & NFS_MOUNT_NOAC) {
901 server->acregmin = server->acregmax = 0;
902 server->acdirmin = server->acdirmax = 0;
903 }
904
905 server->maxfilesize = fsinfo->maxfilesize;
906
907 /* We're airborne Set socket buffersize */
908 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
909}
910
911/*
912 * Probe filesystem information, including the FSID on v2/v3
913 */
914static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
915{
916 struct nfs_fsinfo fsinfo;
917 struct nfs_client *clp = server->nfs_client;
918 int error;
919
920 dprintk("--> nfs_probe_fsinfo()\n");
921
922 if (clp->rpc_ops->set_capabilities != NULL) {
923 error = clp->rpc_ops->set_capabilities(server, mntfh);
924 if (error < 0)
925 goto out_error;
926 }
927
928 fsinfo.fattr = fattr;
929 nfs_fattr_init(fattr);
930 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
931 if (error < 0)
932 goto out_error;
933
934 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700935 error = bdi_init(&server->backing_dev_info);
936 if (error)
937 goto out_error;
938
David Howells54ceac42006-08-22 20:06:13 -0400939
940 /* Get some general file system info */
941 if (server->namelen == 0) {
942 struct nfs_pathconf pathinfo;
943
944 pathinfo.fattr = fattr;
945 nfs_fattr_init(fattr);
946
947 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
948 server->namelen = pathinfo.max_namelen;
949 }
950
951 dprintk("<-- nfs_probe_fsinfo() = 0\n");
952 return 0;
953
954out_error:
955 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
956 return error;
957}
958
959/*
960 * Copy useful information when duplicating a server record
961 */
962static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
963{
964 target->flags = source->flags;
965 target->acregmin = source->acregmin;
966 target->acregmax = source->acregmax;
967 target->acdirmin = source->acdirmin;
968 target->acdirmax = source->acdirmax;
969 target->caps = source->caps;
970}
971
972/*
973 * Allocate and initialise a server record
974 */
975static struct nfs_server *nfs_alloc_server(void)
976{
977 struct nfs_server *server;
978
979 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
980 if (!server)
981 return NULL;
982
983 server->client = server->client_acl = ERR_PTR(-EINVAL);
984
985 /* Zero out the NFS state stuff */
986 INIT_LIST_HEAD(&server->client_link);
987 INIT_LIST_HEAD(&server->master_link);
988
Steve Dicksonef818a22007-11-08 04:05:04 -0500989 atomic_set(&server->active, 0);
990
David Howells54ceac42006-08-22 20:06:13 -0400991 server->io_stats = nfs_alloc_iostats();
992 if (!server->io_stats) {
993 kfree(server);
994 return NULL;
995 }
996
997 return server;
998}
999
1000/*
1001 * Free up a server record
1002 */
1003void nfs_free_server(struct nfs_server *server)
1004{
1005 dprintk("--> nfs_free_server()\n");
1006
1007 spin_lock(&nfs_client_lock);
1008 list_del(&server->client_link);
1009 list_del(&server->master_link);
1010 spin_unlock(&nfs_client_lock);
1011
1012 if (server->destroy != NULL)
1013 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001014
1015 if (!IS_ERR(server->client_acl))
1016 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001017 if (!IS_ERR(server->client))
1018 rpc_shutdown_client(server->client);
1019
1020 nfs_put_client(server->nfs_client);
1021
1022 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001023 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001024 kfree(server);
1025 nfs_release_automount_timer();
1026 dprintk("<-- nfs_free_server()\n");
1027}
1028
1029/*
1030 * Create a version 2 or 3 volume record
1031 * - keyed on server and FSID
1032 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001033struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001034 struct nfs_fh *mntfh)
1035{
1036 struct nfs_server *server;
1037 struct nfs_fattr fattr;
1038 int error;
1039
1040 server = nfs_alloc_server();
1041 if (!server)
1042 return ERR_PTR(-ENOMEM);
1043
1044 /* Get a client representation */
1045 error = nfs_init_server(server, data);
1046 if (error < 0)
1047 goto error;
1048
1049 BUG_ON(!server->nfs_client);
1050 BUG_ON(!server->nfs_client->rpc_ops);
1051 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1052
1053 /* Probe the root fh to retrieve its FSID */
1054 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1055 if (error < 0)
1056 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001057 if (server->nfs_client->rpc_ops->version == 3) {
1058 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1059 server->namelen = NFS3_MAXNAMLEN;
1060 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1061 server->caps |= NFS_CAP_READDIRPLUS;
1062 } else {
1063 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1064 server->namelen = NFS2_MAXNAMLEN;
1065 }
1066
David Howells54ceac42006-08-22 20:06:13 -04001067 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1068 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1069 if (error < 0) {
1070 dprintk("nfs_create_server: getattr error = %d\n", -error);
1071 goto error;
1072 }
1073 }
1074 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1075
David Howells6daabf12006-08-24 15:44:16 -04001076 dprintk("Server FSID: %llx:%llx\n",
1077 (unsigned long long) server->fsid.major,
1078 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001079
1080 BUG_ON(!server->nfs_client);
1081 BUG_ON(!server->nfs_client->rpc_ops);
1082 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1083
1084 spin_lock(&nfs_client_lock);
1085 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1086 list_add_tail(&server->master_link, &nfs_volume_list);
1087 spin_unlock(&nfs_client_lock);
1088
1089 server->mount_time = jiffies;
1090 return server;
1091
1092error:
1093 nfs_free_server(server);
1094 return ERR_PTR(error);
1095}
1096
1097#ifdef CONFIG_NFS_V4
1098/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001099 * Initialize the NFS4 callback service
1100 */
1101static int nfs4_init_callback(struct nfs_client *clp)
1102{
1103 int error;
1104
1105 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001106 if (nfs4_has_session(clp)) {
1107 error = xprt_setup_backchannel(
1108 clp->cl_rpcclient->cl_xprt,
1109 NFS41_BC_MIN_CALLBACKS);
1110 if (error < 0)
1111 return error;
1112 }
1113
Benny Halevy71468512009-04-01 09:22:56 -04001114 error = nfs_callback_up(clp->cl_minorversion,
1115 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001116 if (error < 0) {
1117 dprintk("%s: failed to start callback. Error = %d\n",
1118 __func__, error);
1119 return error;
1120 }
1121 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1122 }
1123 return 0;
1124}
1125
1126/*
Andy Adamson557134a2009-04-01 09:21:53 -04001127 * Initialize the minor version specific parts of an NFS4 client record
1128 */
1129static int nfs4_init_client_minor_version(struct nfs_client *clp)
1130{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001131 clp->cl_call_sync = _nfs4_call_sync;
1132
Andy Adamson557134a2009-04-01 09:21:53 -04001133#if defined(CONFIG_NFS_V4_1)
1134 if (clp->cl_minorversion) {
1135 struct nfs4_session *session = NULL;
1136 /*
1137 * Create the session and mark it expired.
1138 * When a SEQUENCE operation encounters the expired session
1139 * it will do session recovery to initialize it.
1140 */
1141 session = nfs4_alloc_session(clp);
1142 if (!session)
1143 return -ENOMEM;
1144
1145 clp->cl_session = session;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001146 clp->cl_call_sync = _nfs4_call_sync_session;
Andy Adamson557134a2009-04-01 09:21:53 -04001147 }
1148#endif /* CONFIG_NFS_V4_1 */
1149
Benny Halevy71468512009-04-01 09:22:56 -04001150 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001151}
1152
1153/*
David Howells54ceac42006-08-22 20:06:13 -04001154 * Initialise an NFS4 client record
1155 */
1156static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001157 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001158 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001159 rpc_authflavor_t authflavour,
1160 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001161{
1162 int error;
1163
1164 if (clp->cl_cons_state == NFS_CS_READY) {
1165 /* the client is initialised already */
1166 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1167 return 0;
1168 }
1169
1170 /* Check NFS protocol revision and initialize RPC op vector */
1171 clp->rpc_ops = &nfs_v4_clientops;
1172
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001173 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001174 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001175 if (error < 0)
1176 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001177 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001178
1179 error = nfs_idmap_new(clp);
1180 if (error < 0) {
1181 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001182 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001183 goto error;
1184 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001185 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001186
Andy Adamson557134a2009-04-01 09:21:53 -04001187 error = nfs4_init_client_minor_version(clp);
1188 if (error < 0)
1189 goto error;
1190
Andy Adamson76db6d92009-04-01 09:22:38 -04001191 if (!nfs4_has_session(clp))
1192 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001193 return 0;
1194
1195error:
1196 nfs_mark_client_ready(clp, error);
1197 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1198 return error;
1199}
1200
1201/*
1202 * Set up an NFS4 client
1203 */
1204static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001205 const char *hostname,
1206 const struct sockaddr *addr,
1207 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001208 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001209 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001210 int proto, const struct rpc_timeout *timeparms,
1211 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001212{
Trond Myklebust3a498022007-12-14 14:56:04 -05001213 struct nfs_client_initdata cl_init = {
1214 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001215 .addr = addr,
1216 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001217 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001218 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001219 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001220 };
David Howells54ceac42006-08-22 20:06:13 -04001221 struct nfs_client *clp;
1222 int error;
1223
1224 dprintk("--> nfs4_set_client()\n");
1225
1226 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001227 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001228 if (IS_ERR(clp)) {
1229 error = PTR_ERR(clp);
1230 goto error;
1231 }
Chuck Leverd7403512008-12-23 15:21:37 -05001232 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1233 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001234 if (error < 0)
1235 goto error_put;
1236
1237 server->nfs_client = clp;
1238 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1239 return 0;
1240
1241error_put:
1242 nfs_put_client(clp);
1243error:
1244 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1245 return error;
1246}
1247
Andy Adamson557134a2009-04-01 09:21:53 -04001248
1249/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001250 * Session has been established, and the client marked ready.
1251 * Set the mount rsize and wsize with negotiated fore channel
1252 * attributes which will be bound checked in nfs_server_set_fsinfo.
1253 */
1254static void nfs4_session_set_rwsize(struct nfs_server *server)
1255{
1256#ifdef CONFIG_NFS_V4_1
1257 if (!nfs4_has_session(server->nfs_client))
1258 return;
1259 server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1260 server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
1261#endif /* CONFIG_NFS_V4_1 */
1262}
1263
1264/*
David Howells54ceac42006-08-22 20:06:13 -04001265 * Create a version 4 volume record
1266 */
1267static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001268 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001269{
Trond Myklebust33170232007-12-20 16:03:59 -05001270 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001271 int error;
1272
1273 dprintk("--> nfs4_init_server()\n");
1274
Trond Myklebust33170232007-12-20 16:03:59 -05001275 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1276 data->timeo, data->retrans);
1277
Chuck Lever542fcc32008-12-23 15:21:36 -05001278 /* Initialise the client representation from the mount data */
1279 server->flags = data->flags;
Trond Myklebust62ab4602009-08-09 15:06:19 -04001280 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
David Howellsb797cac2009-04-03 16:42:48 +01001281 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001282
Trond Myklebust33170232007-12-20 16:03:59 -05001283 /* Get a client record */
1284 error = nfs4_set_client(server,
1285 data->nfs_server.hostname,
1286 (const struct sockaddr *)&data->nfs_server.address,
1287 data->nfs_server.addrlen,
1288 data->client_address,
1289 data->auth_flavors[0],
1290 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001291 &timeparms,
1292 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001293 if (error < 0)
1294 goto error;
1295
David Howells54ceac42006-08-22 20:06:13 -04001296 if (data->rsize)
1297 server->rsize = nfs_block_size(data->rsize, NULL);
1298 if (data->wsize)
1299 server->wsize = nfs_block_size(data->wsize, NULL);
1300
1301 server->acregmin = data->acregmin * HZ;
1302 server->acregmax = data->acregmax * HZ;
1303 server->acdirmin = data->acdirmin * HZ;
1304 server->acdirmax = data->acdirmax * HZ;
1305
Chuck Leverf22d6d72008-03-14 14:10:22 -04001306 server->port = data->nfs_server.port;
1307
Trond Myklebust33170232007-12-20 16:03:59 -05001308 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001309
Trond Myklebust33170232007-12-20 16:03:59 -05001310error:
David Howells54ceac42006-08-22 20:06:13 -04001311 /* Done */
1312 dprintk("<-- nfs4_init_server() = %d\n", error);
1313 return error;
1314}
1315
1316/*
1317 * Create a version 4 volume record
1318 * - keyed on server and FSID
1319 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001320struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001321 struct nfs_fh *mntfh)
1322{
1323 struct nfs_fattr fattr;
1324 struct nfs_server *server;
1325 int error;
1326
1327 dprintk("--> nfs4_create_server()\n");
1328
1329 server = nfs_alloc_server();
1330 if (!server)
1331 return ERR_PTR(-ENOMEM);
1332
David Howells54ceac42006-08-22 20:06:13 -04001333 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001334 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001335 if (error < 0)
1336 goto error;
1337
1338 BUG_ON(!server->nfs_client);
1339 BUG_ON(!server->nfs_client->rpc_ops);
1340 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1341
Trond Myklebustfccba802009-07-21 16:48:07 -04001342 error = nfs4_init_session(server);
1343 if (error < 0)
1344 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001345
David Howells54ceac42006-08-22 20:06:13 -04001346 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001347 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001348 if (error < 0)
1349 goto error;
1350
David Howells6daabf12006-08-24 15:44:16 -04001351 dprintk("Server FSID: %llx:%llx\n",
1352 (unsigned long long) server->fsid.major,
1353 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001354 dprintk("Mount FH: %d\n", mntfh->size);
1355
Andy Adamson96b09e02009-04-01 09:22:33 -04001356 nfs4_session_set_rwsize(server);
1357
David Howells54ceac42006-08-22 20:06:13 -04001358 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1359 if (error < 0)
1360 goto error;
1361
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001362 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1363 server->namelen = NFS4_MAXNAMLEN;
1364
David Howells54ceac42006-08-22 20:06:13 -04001365 BUG_ON(!server->nfs_client);
1366 BUG_ON(!server->nfs_client->rpc_ops);
1367 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1368
1369 spin_lock(&nfs_client_lock);
1370 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1371 list_add_tail(&server->master_link, &nfs_volume_list);
1372 spin_unlock(&nfs_client_lock);
1373
1374 server->mount_time = jiffies;
1375 dprintk("<-- nfs4_create_server() = %p\n", server);
1376 return server;
1377
1378error:
1379 nfs_free_server(server);
1380 dprintk("<-- nfs4_create_server() = error %d\n", error);
1381 return ERR_PTR(error);
1382}
1383
1384/*
1385 * Create an NFS4 referral server record
1386 */
1387struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001388 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001389{
1390 struct nfs_client *parent_client;
1391 struct nfs_server *server, *parent_server;
1392 struct nfs_fattr fattr;
1393 int error;
1394
1395 dprintk("--> nfs4_create_referral_server()\n");
1396
1397 server = nfs_alloc_server();
1398 if (!server)
1399 return ERR_PTR(-ENOMEM);
1400
1401 parent_server = NFS_SB(data->sb);
1402 parent_client = parent_server->nfs_client;
1403
Chuck Lever542fcc32008-12-23 15:21:36 -05001404 /* Initialise the client representation from the parent server */
1405 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001406 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001407
David Howells54ceac42006-08-22 20:06:13 -04001408 /* Get a client representation.
1409 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001410 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001411 data->addr,
1412 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001413 parent_client->cl_ipaddr,
1414 data->authflavor,
1415 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001416 parent_server->client->cl_timeout,
1417 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001418 if (error < 0)
1419 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001420
Trond Myklebust33170232007-12-20 16:03:59 -05001421 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001422 if (error < 0)
1423 goto error;
1424
1425 BUG_ON(!server->nfs_client);
1426 BUG_ON(!server->nfs_client->rpc_ops);
1427 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1428
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001429 /* Probe the root fh to retrieve its FSID and filehandle */
1430 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1431 if (error < 0)
1432 goto error;
1433
David Howells54ceac42006-08-22 20:06:13 -04001434 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001435 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001436 if (error < 0)
1437 goto error;
1438
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001439 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1440 server->namelen = NFS4_MAXNAMLEN;
1441
David Howells54ceac42006-08-22 20:06:13 -04001442 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001443 (unsigned long long) server->fsid.major,
1444 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001445
1446 spin_lock(&nfs_client_lock);
1447 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1448 list_add_tail(&server->master_link, &nfs_volume_list);
1449 spin_unlock(&nfs_client_lock);
1450
1451 server->mount_time = jiffies;
1452
1453 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1454 return server;
1455
1456error:
1457 nfs_free_server(server);
1458 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1459 return ERR_PTR(error);
1460}
1461
1462#endif /* CONFIG_NFS_V4 */
1463
1464/*
1465 * Clone an NFS2, NFS3 or NFS4 server record
1466 */
1467struct nfs_server *nfs_clone_server(struct nfs_server *source,
1468 struct nfs_fh *fh,
1469 struct nfs_fattr *fattr)
1470{
1471 struct nfs_server *server;
1472 struct nfs_fattr fattr_fsinfo;
1473 int error;
1474
1475 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001476 (unsigned long long) fattr->fsid.major,
1477 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001478
1479 server = nfs_alloc_server();
1480 if (!server)
1481 return ERR_PTR(-ENOMEM);
1482
1483 /* Copy data from the source */
1484 server->nfs_client = source->nfs_client;
1485 atomic_inc(&server->nfs_client->cl_count);
1486 nfs_server_copy_userdata(server, source);
1487
1488 server->fsid = fattr->fsid;
1489
Trond Myklebust33170232007-12-20 16:03:59 -05001490 error = nfs_init_server_rpcclient(server,
1491 source->client->cl_timeout,
1492 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001493 if (error < 0)
1494 goto out_free_server;
1495 if (!IS_ERR(source->client_acl))
1496 nfs_init_server_aclclient(server);
1497
1498 /* probe the filesystem info for this server filesystem */
1499 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1500 if (error < 0)
1501 goto out_free_server;
1502
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001503 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1504 server->namelen = NFS4_MAXNAMLEN;
1505
David Howells54ceac42006-08-22 20:06:13 -04001506 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001507 (unsigned long long) server->fsid.major,
1508 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001509
1510 error = nfs_start_lockd(server);
1511 if (error < 0)
1512 goto out_free_server;
1513
1514 spin_lock(&nfs_client_lock);
1515 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1516 list_add_tail(&server->master_link, &nfs_volume_list);
1517 spin_unlock(&nfs_client_lock);
1518
1519 server->mount_time = jiffies;
1520
1521 dprintk("<-- nfs_clone_server() = %p\n", server);
1522 return server;
1523
1524out_free_server:
1525 nfs_free_server(server);
1526 dprintk("<-- nfs_clone_server() = error %d\n", error);
1527 return ERR_PTR(error);
1528}
David Howells6aaca562006-08-22 20:06:13 -04001529
1530#ifdef CONFIG_PROC_FS
1531static struct proc_dir_entry *proc_fs_nfs;
1532
1533static int nfs_server_list_open(struct inode *inode, struct file *file);
1534static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1535static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1536static void nfs_server_list_stop(struct seq_file *p, void *v);
1537static int nfs_server_list_show(struct seq_file *m, void *v);
1538
1539static struct seq_operations nfs_server_list_ops = {
1540 .start = nfs_server_list_start,
1541 .next = nfs_server_list_next,
1542 .stop = nfs_server_list_stop,
1543 .show = nfs_server_list_show,
1544};
1545
Arjan van de Ven00977a52007-02-12 00:55:34 -08001546static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001547 .open = nfs_server_list_open,
1548 .read = seq_read,
1549 .llseek = seq_lseek,
1550 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001551 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001552};
1553
1554static int nfs_volume_list_open(struct inode *inode, struct file *file);
1555static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1556static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1557static void nfs_volume_list_stop(struct seq_file *p, void *v);
1558static int nfs_volume_list_show(struct seq_file *m, void *v);
1559
1560static struct seq_operations nfs_volume_list_ops = {
1561 .start = nfs_volume_list_start,
1562 .next = nfs_volume_list_next,
1563 .stop = nfs_volume_list_stop,
1564 .show = nfs_volume_list_show,
1565};
1566
Arjan van de Ven00977a52007-02-12 00:55:34 -08001567static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001568 .open = nfs_volume_list_open,
1569 .read = seq_read,
1570 .llseek = seq_lseek,
1571 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001572 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001573};
1574
1575/*
1576 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1577 * we're dealing
1578 */
1579static int nfs_server_list_open(struct inode *inode, struct file *file)
1580{
1581 struct seq_file *m;
1582 int ret;
1583
1584 ret = seq_open(file, &nfs_server_list_ops);
1585 if (ret < 0)
1586 return ret;
1587
1588 m = file->private_data;
1589 m->private = PDE(inode)->data;
1590
1591 return 0;
1592}
1593
1594/*
1595 * set up the iterator to start reading from the server list and return the first item
1596 */
1597static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1598{
David Howells6aaca562006-08-22 20:06:13 -04001599 /* lock the list against modification */
1600 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001601 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001602}
1603
1604/*
1605 * move to next server
1606 */
1607static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1608{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001609 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001610}
1611
1612/*
1613 * clean up after reading from the transports list
1614 */
1615static void nfs_server_list_stop(struct seq_file *p, void *v)
1616{
1617 spin_unlock(&nfs_client_lock);
1618}
1619
1620/*
1621 * display a header line followed by a load of call lines
1622 */
1623static int nfs_server_list_show(struct seq_file *m, void *v)
1624{
1625 struct nfs_client *clp;
1626
1627 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001628 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001629 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1630 return 0;
1631 }
1632
1633 /* display one transport per line on subsequent lines */
1634 clp = list_entry(v, struct nfs_client, cl_share_link);
1635
Chuck Lever5d8515c2007-12-10 14:57:16 -05001636 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001637 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001638 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1639 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001640 atomic_read(&clp->cl_count),
1641 clp->cl_hostname);
1642
1643 return 0;
1644}
1645
1646/*
1647 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1648 */
1649static int nfs_volume_list_open(struct inode *inode, struct file *file)
1650{
1651 struct seq_file *m;
1652 int ret;
1653
1654 ret = seq_open(file, &nfs_volume_list_ops);
1655 if (ret < 0)
1656 return ret;
1657
1658 m = file->private_data;
1659 m->private = PDE(inode)->data;
1660
1661 return 0;
1662}
1663
1664/*
1665 * set up the iterator to start reading from the volume list and return the first item
1666 */
1667static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1668{
David Howells6aaca562006-08-22 20:06:13 -04001669 /* lock the list against modification */
1670 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001671 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001672}
1673
1674/*
1675 * move to next volume
1676 */
1677static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1678{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001679 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001680}
1681
1682/*
1683 * clean up after reading from the transports list
1684 */
1685static void nfs_volume_list_stop(struct seq_file *p, void *v)
1686{
1687 spin_unlock(&nfs_client_lock);
1688}
1689
1690/*
1691 * display a header line followed by a load of call lines
1692 */
1693static int nfs_volume_list_show(struct seq_file *m, void *v)
1694{
1695 struct nfs_server *server;
1696 struct nfs_client *clp;
1697 char dev[8], fsid[17];
1698
1699 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001700 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001701 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001702 return 0;
1703 }
1704 /* display one transport per line on subsequent lines */
1705 server = list_entry(v, struct nfs_server, master_link);
1706 clp = server->nfs_client;
1707
1708 snprintf(dev, 8, "%u:%u",
1709 MAJOR(server->s_dev), MINOR(server->s_dev));
1710
1711 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001712 (unsigned long long) server->fsid.major,
1713 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001714
David Howells5d1acff2009-04-03 16:42:47 +01001715 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001716 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001717 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1718 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001719 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001720 fsid,
1721 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001722
1723 return 0;
1724}
1725
1726/*
1727 * initialise the /proc/fs/nfsfs/ directory
1728 */
1729int __init nfs_fs_proc_init(void)
1730{
1731 struct proc_dir_entry *p;
1732
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001733 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001734 if (!proc_fs_nfs)
1735 goto error_0;
1736
David Howells6aaca562006-08-22 20:06:13 -04001737 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001738 p = proc_create("servers", S_IFREG|S_IRUGO,
1739 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001740 if (!p)
1741 goto error_1;
1742
David Howells6aaca562006-08-22 20:06:13 -04001743 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001744 p = proc_create("volumes", S_IFREG|S_IRUGO,
1745 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001746 if (!p)
1747 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001748 return 0;
1749
1750error_2:
1751 remove_proc_entry("servers", proc_fs_nfs);
1752error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001753 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001754error_0:
1755 return -ENOMEM;
1756}
1757
1758/*
1759 * clean up the /proc/fs/nfsfs/ directory
1760 */
1761void nfs_fs_proc_exit(void)
1762{
1763 remove_proc_entry("volumes", proc_fs_nfs);
1764 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001765 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001766}
1767
1768#endif /* CONFIG_PROC_FS */