blob: 876ba85927068a86af31af2bf8007328887d7400 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050039#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040040#include <linux/nfs_xdr.h>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040041#include <linux/sunrpc/bc_xprt.h>
David Howells24c8dbb2006-08-22 20:06:10 -040042
43#include <asm/system.h>
44
45#include "nfs4_fs.h"
46#include "callback.h"
47#include "delegation.h"
48#include "iostat.h"
49#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010050#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040051
52#define NFSDBG_FACILITY NFSDBG_CLIENT
53
54static DEFINE_SPINLOCK(nfs_client_lock);
55static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040056static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040057static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
58
59/*
David Howells5006a762006-08-22 20:06:12 -040060 * RPC cruft for NFS
61 */
62static struct rpc_version *nfs_version[5] = {
63 [2] = &nfs_version2,
64#ifdef CONFIG_NFS_V3
65 [3] = &nfs_version3,
66#endif
67#ifdef CONFIG_NFS_V4
68 [4] = &nfs_version4,
69#endif
70};
71
72struct rpc_program nfs_program = {
73 .name = "nfs",
74 .number = NFS_PROGRAM,
75 .nrvers = ARRAY_SIZE(nfs_version),
76 .version = nfs_version,
77 .stats = &nfs_rpcstat,
78 .pipe_dir_name = "/nfs",
79};
80
81struct rpc_stat nfs_rpcstat = {
82 .program = &nfs_program
83};
84
85
86#ifdef CONFIG_NFS_V3_ACL
87static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
88static struct rpc_version * nfsacl_version[] = {
89 [3] = &nfsacl_version3,
90};
91
92struct rpc_program nfsacl_program = {
93 .name = "nfsacl",
94 .number = NFS_ACL_PROGRAM,
95 .nrvers = ARRAY_SIZE(nfsacl_version),
96 .version = nfsacl_version,
97 .stats = &nfsacl_rpcstat,
98};
99#endif /* CONFIG_NFS_V3_ACL */
100
Trond Myklebust3a498022007-12-14 14:56:04 -0500101struct nfs_client_initdata {
102 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500103 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500104 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500105 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500106 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400107 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500108};
109
David Howells5006a762006-08-22 20:06:12 -0400110/*
David Howells24c8dbb2006-08-22 20:06:10 -0400111 * Allocate a shared client record
112 *
113 * Since these are allocated/deallocated very rarely, we don't
114 * bother putting them in a slab cache...
115 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500116static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400117{
118 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400119 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700120 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400121
122 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
123 goto error_0;
124
Trond Myklebust40c553192007-12-14 14:56:07 -0500125 clp->rpc_ops = cl_init->rpc_ops;
126
David Howells24c8dbb2006-08-22 20:06:10 -0400127 atomic_set(&clp->cl_count, 1);
128 clp->cl_cons_state = NFS_CS_INITING;
129
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500130 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
131 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400132
Trond Myklebust3a498022007-12-14 14:56:04 -0500133 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700134 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500135 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400136 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400137 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400138 }
139
140 INIT_LIST_HEAD(&clp->cl_superblocks);
141 clp->cl_rpcclient = ERR_PTR(-EINVAL);
142
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500143 clp->cl_proto = cl_init->proto;
144
David Howells24c8dbb2006-08-22 20:06:10 -0400145#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400146 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400147 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400152 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400153 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400154#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400155 cred = rpc_lookup_machine_cred();
156 if (!IS_ERR(cred))
157 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400158
David Howells14727282009-04-03 16:42:42 +0100159 nfs_fscache_get_client_cookie(clp);
160
David Howells24c8dbb2006-08-22 20:06:10 -0400161 return clp;
162
Benny Halevy71468512009-04-01 09:22:56 -0400163error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400164 kfree(clp);
165error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700166 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400167}
168
Trond Myklebust5dd31772006-08-24 01:03:05 -0400169#ifdef CONFIG_NFS_V4
Benny Halevy9bdaa862009-04-01 09:22:55 -0400170/*
Andy Adamson557134a2009-04-01 09:21:53 -0400171 * Clears/puts all minor version specific parts from an nfs_client struct
172 * reverting it to minorversion 0.
173 */
174static void nfs4_clear_client_minor_version(struct nfs_client *clp)
175{
176#ifdef CONFIG_NFS_V4_1
177 if (nfs4_has_session(clp)) {
178 nfs4_destroy_session(clp->cl_session);
179 clp->cl_session = NULL;
180 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400181
Trond Myklebust97dc1352010-06-16 09:52:26 -0400182 clp->cl_mvops = nfs_v4_minor_ops[0];
Andy Adamson557134a2009-04-01 09:21:53 -0400183#endif /* CONFIG_NFS_V4_1 */
184}
185
186/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800187 * Destroy the NFS4 callback service
188 */
189static void nfs4_destroy_callback(struct nfs_client *clp)
190{
191 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400192 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800193}
194
195static void nfs4_shutdown_client(struct nfs_client *clp)
196{
197 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
198 nfs4_kill_renewd(clp);
199 nfs4_clear_client_minor_version(clp);
200 nfs4_destroy_callback(clp);
201 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
202 nfs_idmap_delete(clp);
203
204 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
205}
206#else
207static void nfs4_shutdown_client(struct nfs_client *clp)
208{
209}
210#endif /* CONFIG_NFS_V4 */
211
212/*
David Howells24c8dbb2006-08-22 20:06:10 -0400213 * Destroy a shared client record
214 */
215static void nfs_free_client(struct nfs_client *clp)
216{
Trond Myklebust40c553192007-12-14 14:56:07 -0500217 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400218
Trond Myklebust5dd31772006-08-24 01:03:05 -0400219 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400220
David Howells14727282009-04-03 16:42:42 +0100221 nfs_fscache_release_client_cookie(clp);
222
David Howells24c8dbb2006-08-22 20:06:10 -0400223 /* -EIO all pending I/O */
224 if (!IS_ERR(clp->cl_rpcclient))
225 rpc_shutdown_client(clp->cl_rpcclient);
226
Trond Myklebust7c67db32008-04-07 20:50:11 -0400227 if (clp->cl_machine_cred != NULL)
228 put_rpccred(clp->cl_machine_cred);
229
David Howells24c8dbb2006-08-22 20:06:10 -0400230 kfree(clp->cl_hostname);
231 kfree(clp);
232
233 dprintk("<-- nfs_free_client()\n");
234}
235
236/*
237 * Release a reference to a shared client record
238 */
239void nfs_put_client(struct nfs_client *clp)
240{
David Howells27ba8512006-07-30 14:40:56 -0400241 if (!clp)
242 return;
243
David Howells24c8dbb2006-08-22 20:06:10 -0400244 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
245
246 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
247 list_del(&clp->cl_share_link);
248 spin_unlock(&nfs_client_lock);
249
250 BUG_ON(!list_empty(&clp->cl_superblocks));
251
252 nfs_free_client(clp);
253 }
254}
255
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500256#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400257/*
258 * Test if two ip6 socket addresses refer to the same socket by
259 * comparing relevant fields. The padding bytes specifically, are not
260 * compared. sin6_flowinfo is not compared because it only affects QoS
261 * and sin6_scope_id is only compared if the address is "link local"
262 * because "link local" addresses need only be unique to a specific
263 * link. Conversely, ordinary unicast addresses might have different
264 * sin6_scope_id.
265 *
266 * The caller should ensure both socket addresses are AF_INET6.
267 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400268static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
269 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400270{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400271 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
272 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400273
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400274 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
275 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400276 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500277
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400278 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500279}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400280#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
281static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
282 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400283{
284 return 0;
285}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500286#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500287
David Howells24c8dbb2006-08-22 20:06:10 -0400288/*
Ian Dalld7371c42009-03-10 20:33:22 -0400289 * Test if two ip4 socket addresses refer to the same socket, by
290 * comparing relevant fields. The padding bytes specifically, are
291 * not compared.
292 *
293 * The caller should ensure both socket addresses are AF_INET.
294 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400295static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
296 const struct sockaddr *sa2)
297{
298 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
299 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
300
301 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
302}
303
304static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
305 const struct sockaddr *sa2)
306{
307 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
308 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
309
310 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
311 (sin1->sin6_port == sin2->sin6_port);
312}
313
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400314static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
315 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400316{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400317 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
318 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400319
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400320 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
321 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400322}
323
324/*
Ian Dalld7371c42009-03-10 20:33:22 -0400325 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400326 * by comparing (only) relevant fields, excluding the port number.
327 */
328static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
329 const struct sockaddr *sa2)
330{
331 if (sa1->sa_family != sa2->sa_family)
332 return 0;
333
334 switch (sa1->sa_family) {
335 case AF_INET:
336 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
337 case AF_INET6:
338 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
339 }
340 return 0;
341}
342
343/*
344 * Test if two socket addresses represent the same actual socket,
345 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400346 */
347static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
348 const struct sockaddr *sa2)
349{
350 if (sa1->sa_family != sa2->sa_family)
351 return 0;
352
353 switch (sa1->sa_family) {
354 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400355 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400356 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400357 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400358 }
359 return 0;
360}
361
362/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500363 * Find a client by IP address and protocol version
364 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400365 */
Chuck Leverff052642007-12-10 14:58:44 -0500366struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500367{
368 struct nfs_client *clp;
369
370 spin_lock(&nfs_client_lock);
371 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500372 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
373
Trond Myklebustc81468a2007-12-14 14:56:05 -0500374 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400375 if (!(clp->cl_cons_state == NFS_CS_READY ||
376 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500377 continue;
378
379 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500380 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500381 continue;
382
383 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500384 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500385 continue;
386
387 atomic_inc(&clp->cl_count);
388 spin_unlock(&nfs_client_lock);
389 return clp;
390 }
391 spin_unlock(&nfs_client_lock);
392 return NULL;
393}
394
395/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500396 * Find a client by IP address and protocol version
397 * - returns NULL if no such client
398 */
399struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
400{
401 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
402 u32 nfsvers = clp->rpc_ops->version;
403
404 spin_lock(&nfs_client_lock);
405 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
406 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
407
408 /* Don't match clients that failed to initialise properly */
409 if (clp->cl_cons_state != NFS_CS_READY)
410 continue;
411
412 /* Different NFS versions cannot share the same nfs_client */
413 if (clp->rpc_ops->version != nfsvers)
414 continue;
415
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500416 /* Match only the IP address, not the port number */
417 if (!nfs_sockaddr_match_ipaddr(sap, clap))
418 continue;
419
420 atomic_inc(&clp->cl_count);
421 spin_unlock(&nfs_client_lock);
422 return clp;
423 }
424 spin_unlock(&nfs_client_lock);
425 return NULL;
426}
427
428/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500429 * Find an nfs_client on the list that matches the initialisation data
430 * that is supplied.
431 */
432static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400433{
434 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400435 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400436
437 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400438 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700439 /* Don't match clients that failed to initialise properly */
440 if (clp->cl_cons_state < 0)
441 continue;
442
David Howells24c8dbb2006-08-22 20:06:10 -0400443 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500444 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400445 continue;
446
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500447 if (clp->cl_proto != data->proto)
448 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400449 /* Match nfsv4 minorversion */
450 if (clp->cl_minorversion != data->minorversion)
451 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500452 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400453 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400454 continue;
455
Trond Myklebustc81468a2007-12-14 14:56:05 -0500456 atomic_inc(&clp->cl_count);
457 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400458 }
David Howells24c8dbb2006-08-22 20:06:10 -0400459 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400460}
461
462/*
463 * Look up a client by IP address and protocol version
464 * - creates a new record if one doesn't yet exist
465 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500466static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400467{
468 struct nfs_client *clp, *new = NULL;
469 int error;
470
Chuck Leverd7422c42007-12-10 14:58:51 -0500471 dprintk("--> nfs_get_client(%s,v%u)\n",
472 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400473
474 /* see if the client already exists */
475 do {
476 spin_lock(&nfs_client_lock);
477
Trond Myklebustc81468a2007-12-14 14:56:05 -0500478 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400479 if (clp)
480 goto found_client;
481 if (new)
482 goto install_client;
483
484 spin_unlock(&nfs_client_lock);
485
Trond Myklebust3a498022007-12-14 14:56:04 -0500486 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700487 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400488
Chuck Levera21bdd92009-06-17 18:02:10 -0700489 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
490 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400491
492 /* install a new client and return with it unready */
493install_client:
494 clp = new;
495 list_add(&clp->cl_share_link, &nfs_client_list);
496 spin_unlock(&nfs_client_lock);
497 dprintk("--> nfs_get_client() = %p [new]\n", clp);
498 return clp;
499
500 /* found an existing client
501 * - make sure it's ready before returning
502 */
503found_client:
504 spin_unlock(&nfs_client_lock);
505
506 if (new)
507 nfs_free_client(new);
508
Matthew Wilcox150030b2007-12-06 16:24:39 -0500509 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400510 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400511 if (error < 0) {
512 nfs_put_client(clp);
513 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400514 }
515
516 if (clp->cl_cons_state < NFS_CS_READY) {
517 error = clp->cl_cons_state;
518 nfs_put_client(clp);
519 return ERR_PTR(error);
520 }
521
David Howells54ceac42006-08-22 20:06:13 -0400522 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
523
David Howells24c8dbb2006-08-22 20:06:10 -0400524 dprintk("--> nfs_get_client() = %p [share]\n", clp);
525 return clp;
526}
527
528/*
529 * Mark a server as ready or failed
530 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400531void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400532{
533 clp->cl_cons_state = state;
534 wake_up_all(&nfs_client_active_wq);
535}
David Howells5006a762006-08-22 20:06:12 -0400536
537/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400538 * With sessions, the client is not marked ready until after a
539 * successful EXCHANGE_ID and CREATE_SESSION.
540 *
541 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
542 * other versions of NFS can be tried.
543 */
544int nfs4_check_client_ready(struct nfs_client *clp)
545{
546 if (!nfs4_has_session(clp))
547 return 0;
548 if (clp->cl_cons_state < NFS_CS_READY)
549 return -EPROTONOSUPPORT;
550 return 0;
551}
552
553/*
David Howells5006a762006-08-22 20:06:12 -0400554 * Initialise the timeout values for a connection
555 */
556static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
557 unsigned int timeo, unsigned int retrans)
558{
559 to->to_initval = timeo * HZ / 10;
560 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400561
562 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400563 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400564 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400565 if (to->to_retries == 0)
566 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500567 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400568 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400569 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
570 to->to_initval = NFS_MAX_TCP_TIMEOUT;
571 to->to_increment = to->to_initval;
572 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500573 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
574 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
575 if (to->to_maxval < to->to_initval)
576 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400577 to->to_exponential = 0;
578 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400579 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400580 if (to->to_retries == 0)
581 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400582 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400583 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400584 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
585 to->to_initval = NFS_MAX_UDP_TIMEOUT;
586 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
587 to->to_exponential = 1;
588 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400589 default:
590 BUG();
David Howells5006a762006-08-22 20:06:12 -0400591 }
592}
593
594/*
595 * Create an RPC client handle
596 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500597static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500598 const struct rpc_timeout *timeparms,
599 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500600 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400601{
David Howells5006a762006-08-22 20:06:12 -0400602 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400603 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500604 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400605 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500606 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500607 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400608 .servername = clp->cl_hostname,
609 .program = &nfs_program,
610 .version = clp->rpc_ops->version,
611 .authflavor = flavor,
612 };
David Howells5006a762006-08-22 20:06:12 -0400613
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500614 if (discrtry)
615 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
616 if (noresvport)
617 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
618
David Howells5006a762006-08-22 20:06:12 -0400619 if (!IS_ERR(clp->cl_rpcclient))
620 return 0;
621
Chuck Lever41877d22006-08-22 20:06:20 -0400622 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400623 if (IS_ERR(clnt)) {
624 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700625 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400626 return PTR_ERR(clnt);
627 }
628
David Howells5006a762006-08-22 20:06:12 -0400629 clp->cl_rpcclient = clnt;
630 return 0;
631}
David Howells54ceac42006-08-22 20:06:13 -0400632
633/*
634 * Version 2 or 3 client destruction
635 */
636static void nfs_destroy_server(struct nfs_server *server)
637{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400638 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
639 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500640 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400641}
642
643/*
644 * Version 2 or 3 lockd setup
645 */
646static int nfs_start_lockd(struct nfs_server *server)
647{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500648 struct nlm_host *host;
649 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500650 struct nlmclnt_initdata nlm_init = {
651 .hostname = clp->cl_hostname,
652 .address = (struct sockaddr *)&clp->cl_addr,
653 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500654 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500655 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
656 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500657 };
David Howells54ceac42006-08-22 20:06:13 -0400658
Chuck Lever883bb162008-01-15 16:04:20 -0500659 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500660 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400661 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
662 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500663 return 0;
664
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400665 switch (clp->cl_proto) {
666 default:
667 nlm_init.protocol = IPPROTO_TCP;
668 break;
669 case XPRT_TRANSPORT_UDP:
670 nlm_init.protocol = IPPROTO_UDP;
671 }
672
Chuck Lever883bb162008-01-15 16:04:20 -0500673 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500674 if (IS_ERR(host))
675 return PTR_ERR(host);
676
677 server->nlm_host = host;
678 server->destroy = nfs_destroy_server;
679 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400680}
681
682/*
683 * Initialise an NFSv3 ACL client connection
684 */
685#ifdef CONFIG_NFS_V3_ACL
686static void nfs_init_server_aclclient(struct nfs_server *server)
687{
Trond Myklebust40c553192007-12-14 14:56:07 -0500688 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400689 goto out_noacl;
690 if (server->flags & NFS_MOUNT_NOACL)
691 goto out_noacl;
692
693 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
694 if (IS_ERR(server->client_acl))
695 goto out_noacl;
696
697 /* No errors! Assume that Sun nfsacls are supported */
698 server->caps |= NFS_CAP_ACLS;
699 return;
700
701out_noacl:
702 server->caps &= ~NFS_CAP_ACLS;
703}
704#else
705static inline void nfs_init_server_aclclient(struct nfs_server *server)
706{
707 server->flags &= ~NFS_MOUNT_NOACL;
708 server->caps &= ~NFS_CAP_ACLS;
709}
710#endif
711
712/*
713 * Create a general RPC client
714 */
Trond Myklebust33170232007-12-20 16:03:59 -0500715static int nfs_init_server_rpcclient(struct nfs_server *server,
716 const struct rpc_timeout *timeo,
717 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400718{
719 struct nfs_client *clp = server->nfs_client;
720
721 server->client = rpc_clone_client(clp->cl_rpcclient);
722 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700723 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400724 return PTR_ERR(server->client);
725 }
726
Trond Myklebust33170232007-12-20 16:03:59 -0500727 memcpy(&server->client->cl_timeout_default,
728 timeo,
729 sizeof(server->client->cl_timeout_default));
730 server->client->cl_timeout = &server->client->cl_timeout_default;
731
David Howells54ceac42006-08-22 20:06:13 -0400732 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
733 struct rpc_auth *auth;
734
735 auth = rpcauth_create(pseudoflavour, server->client);
736 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700737 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400738 return PTR_ERR(auth);
739 }
740 }
741 server->client->cl_softrtry = 0;
742 if (server->flags & NFS_MOUNT_SOFT)
743 server->client->cl_softrtry = 1;
744
David Howells54ceac42006-08-22 20:06:13 -0400745 return 0;
746}
747
748/*
749 * Initialise an NFS2 or NFS3 client
750 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400751static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500752 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400753 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400754{
David Howells54ceac42006-08-22 20:06:13 -0400755 int error;
756
757 if (clp->cl_cons_state == NFS_CS_READY) {
758 /* the client is already initialised */
759 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
760 return 0;
761 }
762
David Howells54ceac42006-08-22 20:06:13 -0400763 /*
764 * Create a client RPC handle for doing FSSTAT with UNIX auth only
765 * - RFC 2623, sec 2.3.2
766 */
Chuck Leverd7403512008-12-23 15:21:37 -0500767 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
768 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400769 if (error < 0)
770 goto error;
771 nfs_mark_client_ready(clp, NFS_CS_READY);
772 return 0;
773
774error:
775 nfs_mark_client_ready(clp, error);
776 dprintk("<-- nfs_init_client() = xerror %d\n", error);
777 return error;
778}
779
780/*
781 * Create a version 2 or 3 client
782 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400783static int nfs_init_server(struct nfs_server *server,
784 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400785{
Trond Myklebust3a498022007-12-14 14:56:04 -0500786 struct nfs_client_initdata cl_init = {
787 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500788 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500789 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500790 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500791 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500792 };
Trond Myklebust33170232007-12-20 16:03:59 -0500793 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400794 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500795 int error;
David Howells54ceac42006-08-22 20:06:13 -0400796
797 dprintk("--> nfs_init_server()\n");
798
799#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400800 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500801 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400802#endif
803
804 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500805 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400806 if (IS_ERR(clp)) {
807 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
808 return PTR_ERR(clp);
809 }
810
Trond Myklebust33170232007-12-20 16:03:59 -0500811 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
812 data->timeo, data->retrans);
813 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400814 if (error < 0)
815 goto error;
816
817 server->nfs_client = clp;
818
819 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400820 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100821 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400822 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
823 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
824 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400825
826 if (data->rsize)
827 server->rsize = nfs_block_size(data->rsize, NULL);
828 if (data->wsize)
829 server->wsize = nfs_block_size(data->wsize, NULL);
830
831 server->acregmin = data->acregmin * HZ;
832 server->acregmax = data->acregmax * HZ;
833 server->acdirmin = data->acdirmin * HZ;
834 server->acdirmax = data->acdirmax * HZ;
835
836 /* Start lockd here, before we might error out */
837 error = nfs_start_lockd(server);
838 if (error < 0)
839 goto error;
840
Chuck Leverf22d6d72008-03-14 14:10:22 -0400841 server->port = data->nfs_server.port;
842
Trond Myklebust33170232007-12-20 16:03:59 -0500843 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400844 if (error < 0)
845 goto error;
846
Chuck Lever3f8400d2008-03-14 14:10:30 -0400847 /* Preserve the values of mount_server-related mount options */
848 if (data->mount_server.addrlen) {
849 memcpy(&server->mountd_address, &data->mount_server.address,
850 data->mount_server.addrlen);
851 server->mountd_addrlen = data->mount_server.addrlen;
852 }
853 server->mountd_version = data->mount_server.version;
854 server->mountd_port = data->mount_server.port;
855 server->mountd_protocol = data->mount_server.protocol;
856
David Howells54ceac42006-08-22 20:06:13 -0400857 server->namelen = data->namlen;
858 /* Create a client RPC handle for the NFSv3 ACL management interface */
859 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400860 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
861 return 0;
862
863error:
864 server->nfs_client = NULL;
865 nfs_put_client(clp);
866 dprintk("<-- nfs_init_server() = xerror %d\n", error);
867 return error;
868}
869
870/*
871 * Load up the server record from information gained in an fsinfo record
872 */
873static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
874{
875 unsigned long max_rpc_payload;
876
877 /* Work out a lot of parameters */
878 if (server->rsize == 0)
879 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
880 if (server->wsize == 0)
881 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
882
883 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
884 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
885 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
886 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
887
888 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
889 if (server->rsize > max_rpc_payload)
890 server->rsize = max_rpc_payload;
891 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
892 server->rsize = NFS_MAX_FILE_IO_SIZE;
893 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700894
Jens Axboed9938312009-06-12 14:45:52 +0200895 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400896 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
897
898 if (server->wsize > max_rpc_payload)
899 server->wsize = max_rpc_payload;
900 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
901 server->wsize = NFS_MAX_FILE_IO_SIZE;
902 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
903 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
904
905 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400906 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
907 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400908 if (server->dtsize > server->rsize)
909 server->dtsize = server->rsize;
910
911 if (server->flags & NFS_MOUNT_NOAC) {
912 server->acregmin = server->acregmax = 0;
913 server->acdirmin = server->acdirmax = 0;
914 }
915
916 server->maxfilesize = fsinfo->maxfilesize;
917
918 /* We're airborne Set socket buffersize */
919 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
920}
921
922/*
923 * Probe filesystem information, including the FSID on v2/v3
924 */
925static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
926{
927 struct nfs_fsinfo fsinfo;
928 struct nfs_client *clp = server->nfs_client;
929 int error;
930
931 dprintk("--> nfs_probe_fsinfo()\n");
932
933 if (clp->rpc_ops->set_capabilities != NULL) {
934 error = clp->rpc_ops->set_capabilities(server, mntfh);
935 if (error < 0)
936 goto out_error;
937 }
938
939 fsinfo.fattr = fattr;
David Howells54ceac42006-08-22 20:06:13 -0400940 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
941 if (error < 0)
942 goto out_error;
943
944 nfs_server_set_fsinfo(server, &fsinfo);
945
946 /* Get some general file system info */
947 if (server->namelen == 0) {
948 struct nfs_pathconf pathinfo;
949
950 pathinfo.fattr = fattr;
951 nfs_fattr_init(fattr);
952
953 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
954 server->namelen = pathinfo.max_namelen;
955 }
956
957 dprintk("<-- nfs_probe_fsinfo() = 0\n");
958 return 0;
959
960out_error:
961 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
962 return error;
963}
964
965/*
966 * Copy useful information when duplicating a server record
967 */
968static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
969{
970 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400971 target->rsize = source->rsize;
972 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400973 target->acregmin = source->acregmin;
974 target->acregmax = source->acregmax;
975 target->acdirmin = source->acdirmin;
976 target->acdirmax = source->acdirmax;
977 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -0400978 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -0400979}
980
981/*
982 * Allocate and initialise a server record
983 */
984static struct nfs_server *nfs_alloc_server(void)
985{
986 struct nfs_server *server;
987
988 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
989 if (!server)
990 return NULL;
991
992 server->client = server->client_acl = ERR_PTR(-EINVAL);
993
994 /* Zero out the NFS state stuff */
995 INIT_LIST_HEAD(&server->client_link);
996 INIT_LIST_HEAD(&server->master_link);
997
Steve Dicksonef818a22007-11-08 04:05:04 -0500998 atomic_set(&server->active, 0);
999
David Howells54ceac42006-08-22 20:06:13 -04001000 server->io_stats = nfs_alloc_iostats();
1001 if (!server->io_stats) {
1002 kfree(server);
1003 return NULL;
1004 }
1005
Jens Axboe48d07642009-09-21 09:59:39 +02001006 if (bdi_init(&server->backing_dev_info)) {
1007 nfs_free_iostats(server->io_stats);
1008 kfree(server);
1009 return NULL;
1010 }
1011
David Howells54ceac42006-08-22 20:06:13 -04001012 return server;
1013}
1014
1015/*
1016 * Free up a server record
1017 */
1018void nfs_free_server(struct nfs_server *server)
1019{
1020 dprintk("--> nfs_free_server()\n");
1021
1022 spin_lock(&nfs_client_lock);
1023 list_del(&server->client_link);
1024 list_del(&server->master_link);
1025 spin_unlock(&nfs_client_lock);
1026
1027 if (server->destroy != NULL)
1028 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001029
1030 if (!IS_ERR(server->client_acl))
1031 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001032 if (!IS_ERR(server->client))
1033 rpc_shutdown_client(server->client);
1034
1035 nfs_put_client(server->nfs_client);
1036
1037 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001038 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001039 kfree(server);
1040 nfs_release_automount_timer();
1041 dprintk("<-- nfs_free_server()\n");
1042}
1043
1044/*
1045 * Create a version 2 or 3 volume record
1046 * - keyed on server and FSID
1047 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001048struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001049 struct nfs_fh *mntfh)
1050{
1051 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001052 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001053 int error;
1054
1055 server = nfs_alloc_server();
1056 if (!server)
1057 return ERR_PTR(-ENOMEM);
1058
Trond Myklebustfbca7792010-04-16 16:22:46 -04001059 error = -ENOMEM;
1060 fattr = nfs_alloc_fattr();
1061 if (fattr == NULL)
1062 goto error;
1063
David Howells54ceac42006-08-22 20:06:13 -04001064 /* Get a client representation */
1065 error = nfs_init_server(server, data);
1066 if (error < 0)
1067 goto error;
1068
1069 BUG_ON(!server->nfs_client);
1070 BUG_ON(!server->nfs_client->rpc_ops);
1071 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1072
1073 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001074 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001075 if (error < 0)
1076 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001077 if (server->nfs_client->rpc_ops->version == 3) {
1078 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1079 server->namelen = NFS3_MAXNAMLEN;
1080 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1081 server->caps |= NFS_CAP_READDIRPLUS;
1082 } else {
1083 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1084 server->namelen = NFS2_MAXNAMLEN;
1085 }
1086
Trond Myklebustfbca7792010-04-16 16:22:46 -04001087 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1088 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001089 if (error < 0) {
1090 dprintk("nfs_create_server: getattr error = %d\n", -error);
1091 goto error;
1092 }
1093 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001094 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001095
David Howells6daabf12006-08-24 15:44:16 -04001096 dprintk("Server FSID: %llx:%llx\n",
1097 (unsigned long long) server->fsid.major,
1098 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001099
David Howells54ceac42006-08-22 20:06:13 -04001100 spin_lock(&nfs_client_lock);
1101 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1102 list_add_tail(&server->master_link, &nfs_volume_list);
1103 spin_unlock(&nfs_client_lock);
1104
1105 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001106 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001107 return server;
1108
1109error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001110 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001111 nfs_free_server(server);
1112 return ERR_PTR(error);
1113}
1114
1115#ifdef CONFIG_NFS_V4
1116/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001117 * Initialize the NFS4 callback service
1118 */
1119static int nfs4_init_callback(struct nfs_client *clp)
1120{
1121 int error;
1122
1123 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001124 if (nfs4_has_session(clp)) {
1125 error = xprt_setup_backchannel(
1126 clp->cl_rpcclient->cl_xprt,
1127 NFS41_BC_MIN_CALLBACKS);
1128 if (error < 0)
1129 return error;
1130 }
1131
Trond Myklebust97dc1352010-06-16 09:52:26 -04001132 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001133 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001134 if (error < 0) {
1135 dprintk("%s: failed to start callback. Error = %d\n",
1136 __func__, error);
1137 return error;
1138 }
1139 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1140 }
1141 return 0;
1142}
1143
1144/*
Andy Adamson557134a2009-04-01 09:21:53 -04001145 * Initialize the minor version specific parts of an NFS4 client record
1146 */
1147static int nfs4_init_client_minor_version(struct nfs_client *clp)
1148{
1149#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001150 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001151 struct nfs4_session *session = NULL;
1152 /*
1153 * Create the session and mark it expired.
1154 * When a SEQUENCE operation encounters the expired session
1155 * it will do session recovery to initialize it.
1156 */
1157 session = nfs4_alloc_session(clp);
1158 if (!session)
1159 return -ENOMEM;
1160
1161 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001162 /*
1163 * The create session reply races with the server back
1164 * channel probe. Mark the client NFS_CS_SESSION_INITING
1165 * so that the client back channel can find the
1166 * nfs_client struct
1167 */
1168 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001169 }
1170#endif /* CONFIG_NFS_V4_1 */
1171
Benny Halevy71468512009-04-01 09:22:56 -04001172 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001173}
1174
1175/*
David Howells54ceac42006-08-22 20:06:13 -04001176 * Initialise an NFS4 client record
1177 */
1178static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001179 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001180 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001181 rpc_authflavor_t authflavour,
1182 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001183{
1184 int error;
1185
1186 if (clp->cl_cons_state == NFS_CS_READY) {
1187 /* the client is initialised already */
1188 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1189 return 0;
1190 }
1191
1192 /* Check NFS protocol revision and initialize RPC op vector */
1193 clp->rpc_ops = &nfs_v4_clientops;
1194
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001195 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001196 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001197 if (error < 0)
1198 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001199 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001200
1201 error = nfs_idmap_new(clp);
1202 if (error < 0) {
1203 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001204 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001205 goto error;
1206 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001207 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001208
Andy Adamson557134a2009-04-01 09:21:53 -04001209 error = nfs4_init_client_minor_version(clp);
1210 if (error < 0)
1211 goto error;
1212
Andy Adamson76db6d92009-04-01 09:22:38 -04001213 if (!nfs4_has_session(clp))
1214 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001215 return 0;
1216
1217error:
1218 nfs_mark_client_ready(clp, error);
1219 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1220 return error;
1221}
1222
1223/*
1224 * Set up an NFS4 client
1225 */
1226static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001227 const char *hostname,
1228 const struct sockaddr *addr,
1229 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001230 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001231 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001232 int proto, const struct rpc_timeout *timeparms,
1233 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001234{
Trond Myklebust3a498022007-12-14 14:56:04 -05001235 struct nfs_client_initdata cl_init = {
1236 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001237 .addr = addr,
1238 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001239 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001240 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001241 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001242 };
David Howells54ceac42006-08-22 20:06:13 -04001243 struct nfs_client *clp;
1244 int error;
1245
1246 dprintk("--> nfs4_set_client()\n");
1247
1248 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001249 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001250 if (IS_ERR(clp)) {
1251 error = PTR_ERR(clp);
1252 goto error;
1253 }
Chuck Leverd7403512008-12-23 15:21:37 -05001254 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1255 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001256 if (error < 0)
1257 goto error_put;
1258
1259 server->nfs_client = clp;
1260 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1261 return 0;
1262
1263error_put:
1264 nfs_put_client(clp);
1265error:
1266 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1267 return error;
1268}
1269
Andy Adamson557134a2009-04-01 09:21:53 -04001270
1271/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001272 * Session has been established, and the client marked ready.
1273 * Set the mount rsize and wsize with negotiated fore channel
1274 * attributes which will be bound checked in nfs_server_set_fsinfo.
1275 */
1276static void nfs4_session_set_rwsize(struct nfs_server *server)
1277{
1278#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001279 struct nfs4_session *sess;
1280 u32 server_resp_sz;
1281 u32 server_rqst_sz;
1282
Andy Adamson96b09e02009-04-01 09:22:33 -04001283 if (!nfs4_has_session(server->nfs_client))
1284 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001285 sess = server->nfs_client->cl_session;
1286 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1287 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1288
1289 if (server->rsize > server_resp_sz)
1290 server->rsize = server_resp_sz;
1291 if (server->wsize > server_rqst_sz)
1292 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001293#endif /* CONFIG_NFS_V4_1 */
1294}
1295
Trond Myklebust44950b62010-06-17 11:45:12 -04001296static int nfs4_server_common_setup(struct nfs_server *server,
1297 struct nfs_fh *mntfh)
1298{
1299 struct nfs_fattr *fattr;
1300 int error;
1301
1302 BUG_ON(!server->nfs_client);
1303 BUG_ON(!server->nfs_client->rpc_ops);
1304 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1305
1306 fattr = nfs_alloc_fattr();
1307 if (fattr == NULL)
1308 return -ENOMEM;
1309
1310 /* We must ensure the session is initialised first */
1311 error = nfs4_init_session(server);
1312 if (error < 0)
1313 goto out;
1314
1315 /* Probe the root fh to retrieve its FSID and filehandle */
1316 error = nfs4_get_rootfh(server, mntfh);
1317 if (error < 0)
1318 goto out;
1319
1320 dprintk("Server FSID: %llx:%llx\n",
1321 (unsigned long long) server->fsid.major,
1322 (unsigned long long) server->fsid.minor);
1323 dprintk("Mount FH: %d\n", mntfh->size);
1324
1325 nfs4_session_set_rwsize(server);
1326
1327 error = nfs_probe_fsinfo(server, mntfh, fattr);
1328 if (error < 0)
1329 goto out;
1330
1331 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1332 server->namelen = NFS4_MAXNAMLEN;
1333
1334 spin_lock(&nfs_client_lock);
1335 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1336 list_add_tail(&server->master_link, &nfs_volume_list);
1337 spin_unlock(&nfs_client_lock);
1338
1339 server->mount_time = jiffies;
1340out:
1341 nfs_free_fattr(fattr);
1342 return error;
1343}
1344
Andy Adamson96b09e02009-04-01 09:22:33 -04001345/*
David Howells54ceac42006-08-22 20:06:13 -04001346 * Create a version 4 volume record
1347 */
1348static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001349 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001350{
Trond Myklebust33170232007-12-20 16:03:59 -05001351 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001352 int error;
1353
1354 dprintk("--> nfs4_init_server()\n");
1355
Trond Myklebust33170232007-12-20 16:03:59 -05001356 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1357 data->timeo, data->retrans);
1358
Chuck Lever542fcc32008-12-23 15:21:36 -05001359 /* Initialise the client representation from the mount data */
1360 server->flags = data->flags;
Trond Myklebust0df5dd42010-04-11 16:48:44 -04001361 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|
1362 NFS_CAP_POSIX_LOCK;
David Howellsb797cac2009-04-03 16:42:48 +01001363 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001364
Trond Myklebust33170232007-12-20 16:03:59 -05001365 /* Get a client record */
1366 error = nfs4_set_client(server,
1367 data->nfs_server.hostname,
1368 (const struct sockaddr *)&data->nfs_server.address,
1369 data->nfs_server.addrlen,
1370 data->client_address,
1371 data->auth_flavors[0],
1372 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001373 &timeparms,
1374 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001375 if (error < 0)
1376 goto error;
1377
David Howells54ceac42006-08-22 20:06:13 -04001378 if (data->rsize)
1379 server->rsize = nfs_block_size(data->rsize, NULL);
1380 if (data->wsize)
1381 server->wsize = nfs_block_size(data->wsize, NULL);
1382
1383 server->acregmin = data->acregmin * HZ;
1384 server->acregmax = data->acregmax * HZ;
1385 server->acdirmin = data->acdirmin * HZ;
1386 server->acdirmax = data->acdirmax * HZ;
1387
Chuck Leverf22d6d72008-03-14 14:10:22 -04001388 server->port = data->nfs_server.port;
1389
Trond Myklebust33170232007-12-20 16:03:59 -05001390 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001391
Trond Myklebust33170232007-12-20 16:03:59 -05001392error:
David Howells54ceac42006-08-22 20:06:13 -04001393 /* Done */
1394 dprintk("<-- nfs4_init_server() = %d\n", error);
1395 return error;
1396}
1397
1398/*
1399 * Create a version 4 volume record
1400 * - keyed on server and FSID
1401 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001402struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001403 struct nfs_fh *mntfh)
1404{
David Howells54ceac42006-08-22 20:06:13 -04001405 struct nfs_server *server;
1406 int error;
1407
1408 dprintk("--> nfs4_create_server()\n");
1409
1410 server = nfs_alloc_server();
1411 if (!server)
1412 return ERR_PTR(-ENOMEM);
1413
David Howells54ceac42006-08-22 20:06:13 -04001414 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001415 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001416 if (error < 0)
1417 goto error;
1418
Trond Myklebust44950b62010-06-17 11:45:12 -04001419 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001420 if (error < 0)
1421 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001422
David Howells54ceac42006-08-22 20:06:13 -04001423 dprintk("<-- nfs4_create_server() = %p\n", server);
1424 return server;
1425
1426error:
1427 nfs_free_server(server);
1428 dprintk("<-- nfs4_create_server() = error %d\n", error);
1429 return ERR_PTR(error);
1430}
1431
1432/*
1433 * Create an NFS4 referral server record
1434 */
1435struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001436 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001437{
1438 struct nfs_client *parent_client;
1439 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001440 int error;
1441
1442 dprintk("--> nfs4_create_referral_server()\n");
1443
1444 server = nfs_alloc_server();
1445 if (!server)
1446 return ERR_PTR(-ENOMEM);
1447
1448 parent_server = NFS_SB(data->sb);
1449 parent_client = parent_server->nfs_client;
1450
Chuck Lever542fcc32008-12-23 15:21:36 -05001451 /* Initialise the client representation from the parent server */
1452 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001453 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001454
David Howells54ceac42006-08-22 20:06:13 -04001455 /* Get a client representation.
1456 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001457 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001458 data->addr,
1459 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001460 parent_client->cl_ipaddr,
1461 data->authflavor,
1462 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001463 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001464 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001465 if (error < 0)
1466 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001467
Trond Myklebust33170232007-12-20 16:03:59 -05001468 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001469 if (error < 0)
1470 goto error;
1471
Trond Myklebust44950b62010-06-17 11:45:12 -04001472 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001473 if (error < 0)
1474 goto error;
1475
David Howells54ceac42006-08-22 20:06:13 -04001476 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1477 return server;
1478
1479error:
1480 nfs_free_server(server);
1481 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1482 return ERR_PTR(error);
1483}
1484
1485#endif /* CONFIG_NFS_V4 */
1486
1487/*
1488 * Clone an NFS2, NFS3 or NFS4 server record
1489 */
1490struct nfs_server *nfs_clone_server(struct nfs_server *source,
1491 struct nfs_fh *fh,
1492 struct nfs_fattr *fattr)
1493{
1494 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001495 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001496 int error;
1497
1498 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001499 (unsigned long long) fattr->fsid.major,
1500 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001501
1502 server = nfs_alloc_server();
1503 if (!server)
1504 return ERR_PTR(-ENOMEM);
1505
Trond Myklebustfbca7792010-04-16 16:22:46 -04001506 error = -ENOMEM;
1507 fattr_fsinfo = nfs_alloc_fattr();
1508 if (fattr_fsinfo == NULL)
1509 goto out_free_server;
1510
David Howells54ceac42006-08-22 20:06:13 -04001511 /* Copy data from the source */
1512 server->nfs_client = source->nfs_client;
1513 atomic_inc(&server->nfs_client->cl_count);
1514 nfs_server_copy_userdata(server, source);
1515
1516 server->fsid = fattr->fsid;
1517
Trond Myklebust33170232007-12-20 16:03:59 -05001518 error = nfs_init_server_rpcclient(server,
1519 source->client->cl_timeout,
1520 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001521 if (error < 0)
1522 goto out_free_server;
1523 if (!IS_ERR(source->client_acl))
1524 nfs_init_server_aclclient(server);
1525
1526 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001527 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001528 if (error < 0)
1529 goto out_free_server;
1530
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001531 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1532 server->namelen = NFS4_MAXNAMLEN;
1533
David Howells54ceac42006-08-22 20:06:13 -04001534 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001535 (unsigned long long) server->fsid.major,
1536 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001537
1538 error = nfs_start_lockd(server);
1539 if (error < 0)
1540 goto out_free_server;
1541
1542 spin_lock(&nfs_client_lock);
1543 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1544 list_add_tail(&server->master_link, &nfs_volume_list);
1545 spin_unlock(&nfs_client_lock);
1546
1547 server->mount_time = jiffies;
1548
Trond Myklebustfbca7792010-04-16 16:22:46 -04001549 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001550 dprintk("<-- nfs_clone_server() = %p\n", server);
1551 return server;
1552
1553out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001554 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001555 nfs_free_server(server);
1556 dprintk("<-- nfs_clone_server() = error %d\n", error);
1557 return ERR_PTR(error);
1558}
David Howells6aaca562006-08-22 20:06:13 -04001559
1560#ifdef CONFIG_PROC_FS
1561static struct proc_dir_entry *proc_fs_nfs;
1562
1563static int nfs_server_list_open(struct inode *inode, struct file *file);
1564static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1565static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1566static void nfs_server_list_stop(struct seq_file *p, void *v);
1567static int nfs_server_list_show(struct seq_file *m, void *v);
1568
James Morris88e9d342009-09-22 16:43:43 -07001569static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001570 .start = nfs_server_list_start,
1571 .next = nfs_server_list_next,
1572 .stop = nfs_server_list_stop,
1573 .show = nfs_server_list_show,
1574};
1575
Arjan van de Ven00977a52007-02-12 00:55:34 -08001576static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001577 .open = nfs_server_list_open,
1578 .read = seq_read,
1579 .llseek = seq_lseek,
1580 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001581 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001582};
1583
1584static int nfs_volume_list_open(struct inode *inode, struct file *file);
1585static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1586static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1587static void nfs_volume_list_stop(struct seq_file *p, void *v);
1588static int nfs_volume_list_show(struct seq_file *m, void *v);
1589
James Morris88e9d342009-09-22 16:43:43 -07001590static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001591 .start = nfs_volume_list_start,
1592 .next = nfs_volume_list_next,
1593 .stop = nfs_volume_list_stop,
1594 .show = nfs_volume_list_show,
1595};
1596
Arjan van de Ven00977a52007-02-12 00:55:34 -08001597static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001598 .open = nfs_volume_list_open,
1599 .read = seq_read,
1600 .llseek = seq_lseek,
1601 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001602 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001603};
1604
1605/*
1606 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1607 * we're dealing
1608 */
1609static int nfs_server_list_open(struct inode *inode, struct file *file)
1610{
1611 struct seq_file *m;
1612 int ret;
1613
1614 ret = seq_open(file, &nfs_server_list_ops);
1615 if (ret < 0)
1616 return ret;
1617
1618 m = file->private_data;
1619 m->private = PDE(inode)->data;
1620
1621 return 0;
1622}
1623
1624/*
1625 * set up the iterator to start reading from the server list and return the first item
1626 */
1627static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1628{
David Howells6aaca562006-08-22 20:06:13 -04001629 /* lock the list against modification */
1630 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001631 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001632}
1633
1634/*
1635 * move to next server
1636 */
1637static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1638{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001639 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001640}
1641
1642/*
1643 * clean up after reading from the transports list
1644 */
1645static void nfs_server_list_stop(struct seq_file *p, void *v)
1646{
1647 spin_unlock(&nfs_client_lock);
1648}
1649
1650/*
1651 * display a header line followed by a load of call lines
1652 */
1653static int nfs_server_list_show(struct seq_file *m, void *v)
1654{
1655 struct nfs_client *clp;
1656
1657 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001658 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001659 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1660 return 0;
1661 }
1662
1663 /* display one transport per line on subsequent lines */
1664 clp = list_entry(v, struct nfs_client, cl_share_link);
1665
Chuck Lever5d8515c2007-12-10 14:57:16 -05001666 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001667 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001668 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1669 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001670 atomic_read(&clp->cl_count),
1671 clp->cl_hostname);
1672
1673 return 0;
1674}
1675
1676/*
1677 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1678 */
1679static int nfs_volume_list_open(struct inode *inode, struct file *file)
1680{
1681 struct seq_file *m;
1682 int ret;
1683
1684 ret = seq_open(file, &nfs_volume_list_ops);
1685 if (ret < 0)
1686 return ret;
1687
1688 m = file->private_data;
1689 m->private = PDE(inode)->data;
1690
1691 return 0;
1692}
1693
1694/*
1695 * set up the iterator to start reading from the volume list and return the first item
1696 */
1697static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1698{
David Howells6aaca562006-08-22 20:06:13 -04001699 /* lock the list against modification */
1700 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001701 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001702}
1703
1704/*
1705 * move to next volume
1706 */
1707static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1708{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001709 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001710}
1711
1712/*
1713 * clean up after reading from the transports list
1714 */
1715static void nfs_volume_list_stop(struct seq_file *p, void *v)
1716{
1717 spin_unlock(&nfs_client_lock);
1718}
1719
1720/*
1721 * display a header line followed by a load of call lines
1722 */
1723static int nfs_volume_list_show(struct seq_file *m, void *v)
1724{
1725 struct nfs_server *server;
1726 struct nfs_client *clp;
1727 char dev[8], fsid[17];
1728
1729 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001730 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001731 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001732 return 0;
1733 }
1734 /* display one transport per line on subsequent lines */
1735 server = list_entry(v, struct nfs_server, master_link);
1736 clp = server->nfs_client;
1737
1738 snprintf(dev, 8, "%u:%u",
1739 MAJOR(server->s_dev), MINOR(server->s_dev));
1740
1741 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001742 (unsigned long long) server->fsid.major,
1743 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001744
David Howells5d1acff2009-04-03 16:42:47 +01001745 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001746 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001747 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1748 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001749 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001750 fsid,
1751 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001752
1753 return 0;
1754}
1755
1756/*
1757 * initialise the /proc/fs/nfsfs/ directory
1758 */
1759int __init nfs_fs_proc_init(void)
1760{
1761 struct proc_dir_entry *p;
1762
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001763 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001764 if (!proc_fs_nfs)
1765 goto error_0;
1766
David Howells6aaca562006-08-22 20:06:13 -04001767 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001768 p = proc_create("servers", S_IFREG|S_IRUGO,
1769 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001770 if (!p)
1771 goto error_1;
1772
David Howells6aaca562006-08-22 20:06:13 -04001773 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001774 p = proc_create("volumes", S_IFREG|S_IRUGO,
1775 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001776 if (!p)
1777 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001778 return 0;
1779
1780error_2:
1781 remove_proc_entry("servers", proc_fs_nfs);
1782error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001783 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001784error_0:
1785 return -ENOMEM;
1786}
1787
1788/*
1789 * clean up the /proc/fs/nfsfs/ directory
1790 */
1791void nfs_fs_proc_exit(void)
1792{
1793 remove_proc_entry("volumes", proc_fs_nfs);
1794 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001795 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001796}
1797
1798#endif /* CONFIG_PROC_FS */