blob: eba0bcc1bab03b4ec43c62e161af69b041e5249e [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"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040051#include "pnfs.h"
David Howells24c8dbb2006-08-22 20:06:10 -040052
53#define NFSDBG_FACILITY NFSDBG_CLIENT
54
55static DEFINE_SPINLOCK(nfs_client_lock);
56static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040057static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040058static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59
60/*
David Howells5006a762006-08-22 20:06:12 -040061 * RPC cruft for NFS
62 */
63static struct rpc_version *nfs_version[5] = {
64 [2] = &nfs_version2,
65#ifdef CONFIG_NFS_V3
66 [3] = &nfs_version3,
67#endif
68#ifdef CONFIG_NFS_V4
69 [4] = &nfs_version4,
70#endif
71};
72
73struct rpc_program nfs_program = {
74 .name = "nfs",
75 .number = NFS_PROGRAM,
76 .nrvers = ARRAY_SIZE(nfs_version),
77 .version = nfs_version,
78 .stats = &nfs_rpcstat,
79 .pipe_dir_name = "/nfs",
80};
81
82struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
84};
85
86
87#ifdef CONFIG_NFS_V3_ACL
88static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
89static struct rpc_version * nfsacl_version[] = {
90 [3] = &nfsacl_version3,
91};
92
93struct rpc_program nfsacl_program = {
94 .name = "nfsacl",
95 .number = NFS_ACL_PROGRAM,
96 .nrvers = ARRAY_SIZE(nfsacl_version),
97 .version = nfsacl_version,
98 .stats = &nfsacl_rpcstat,
99};
100#endif /* CONFIG_NFS_V3_ACL */
101
Trond Myklebust3a498022007-12-14 14:56:04 -0500102struct nfs_client_initdata {
103 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500104 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500105 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500106 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500107 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400108 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500109};
110
David Howells5006a762006-08-22 20:06:12 -0400111/*
David Howells24c8dbb2006-08-22 20:06:10 -0400112 * Allocate a shared client record
113 *
114 * Since these are allocated/deallocated very rarely, we don't
115 * bother putting them in a slab cache...
116 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500117static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400118{
119 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400120 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700121 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400122
123 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
124 goto error_0;
125
Trond Myklebust40c553192007-12-14 14:56:07 -0500126 clp->rpc_ops = cl_init->rpc_ops;
127
David Howells24c8dbb2006-08-22 20:06:10 -0400128 atomic_set(&clp->cl_count, 1);
129 clp->cl_cons_state = NFS_CS_INITING;
130
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500131 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
132 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400133
Trond Myklebust3a498022007-12-14 14:56:04 -0500134 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700135 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500136 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400137 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400138 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400139 }
140
141 INIT_LIST_HEAD(&clp->cl_superblocks);
142 clp->cl_rpcclient = ERR_PTR(-EINVAL);
143
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500144 clp->cl_proto = cl_init->proto;
145
David Howells24c8dbb2006-08-22 20:06:10 -0400146#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400147 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400148 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000149 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400150 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
151 clp->cl_boot_time = CURRENT_TIME;
152 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400153 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400154 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400155#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400156 cred = rpc_lookup_machine_cred();
157 if (!IS_ERR(cred))
158 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400159
David Howells14727282009-04-03 16:42:42 +0100160 nfs_fscache_get_client_cookie(clp);
161
David Howells24c8dbb2006-08-22 20:06:10 -0400162 return clp;
163
Benny Halevy71468512009-04-01 09:22:56 -0400164error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400165 kfree(clp);
166error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700167 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400168}
169
Trond Myklebust5dd31772006-08-24 01:03:05 -0400170#ifdef CONFIG_NFS_V4
Benny Halevy9bdaa862009-04-01 09:22:55 -0400171/*
Andy Adamson557134a2009-04-01 09:21:53 -0400172 * Clears/puts all minor version specific parts from an nfs_client struct
173 * reverting it to minorversion 0.
174 */
175static void nfs4_clear_client_minor_version(struct nfs_client *clp)
176{
177#ifdef CONFIG_NFS_V4_1
178 if (nfs4_has_session(clp)) {
179 nfs4_destroy_session(clp->cl_session);
180 clp->cl_session = NULL;
181 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400182
Trond Myklebust97dc1352010-06-16 09:52:26 -0400183 clp->cl_mvops = nfs_v4_minor_ops[0];
Andy Adamson557134a2009-04-01 09:21:53 -0400184#endif /* CONFIG_NFS_V4_1 */
185}
186
187/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800188 * Destroy the NFS4 callback service
189 */
190static void nfs4_destroy_callback(struct nfs_client *clp)
191{
192 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400193 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800194}
195
196static void nfs4_shutdown_client(struct nfs_client *clp)
197{
198 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
199 nfs4_kill_renewd(clp);
200 nfs4_clear_client_minor_version(clp);
201 nfs4_destroy_callback(clp);
202 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
203 nfs_idmap_delete(clp);
204
205 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
206}
207#else
208static void nfs4_shutdown_client(struct nfs_client *clp)
209{
210}
211#endif /* CONFIG_NFS_V4 */
212
213/*
David Howells24c8dbb2006-08-22 20:06:10 -0400214 * Destroy a shared client record
215 */
216static void nfs_free_client(struct nfs_client *clp)
217{
Trond Myklebust40c553192007-12-14 14:56:07 -0500218 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400219
Trond Myklebust5dd31772006-08-24 01:03:05 -0400220 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400221
David Howells14727282009-04-03 16:42:42 +0100222 nfs_fscache_release_client_cookie(clp);
223
David Howells24c8dbb2006-08-22 20:06:10 -0400224 /* -EIO all pending I/O */
225 if (!IS_ERR(clp->cl_rpcclient))
226 rpc_shutdown_client(clp->cl_rpcclient);
227
Trond Myklebust7c67db32008-04-07 20:50:11 -0400228 if (clp->cl_machine_cred != NULL)
229 put_rpccred(clp->cl_machine_cred);
230
David Howells24c8dbb2006-08-22 20:06:10 -0400231 kfree(clp->cl_hostname);
232 kfree(clp);
233
234 dprintk("<-- nfs_free_client()\n");
235}
236
237/*
238 * Release a reference to a shared client record
239 */
240void nfs_put_client(struct nfs_client *clp)
241{
David Howells27ba8512006-07-30 14:40:56 -0400242 if (!clp)
243 return;
244
David Howells24c8dbb2006-08-22 20:06:10 -0400245 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
246
247 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
248 list_del(&clp->cl_share_link);
249 spin_unlock(&nfs_client_lock);
250
251 BUG_ON(!list_empty(&clp->cl_superblocks));
252
253 nfs_free_client(clp);
254 }
255}
256
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500257#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400258/*
259 * Test if two ip6 socket addresses refer to the same socket by
260 * comparing relevant fields. The padding bytes specifically, are not
261 * compared. sin6_flowinfo is not compared because it only affects QoS
262 * and sin6_scope_id is only compared if the address is "link local"
263 * because "link local" addresses need only be unique to a specific
264 * link. Conversely, ordinary unicast addresses might have different
265 * sin6_scope_id.
266 *
267 * The caller should ensure both socket addresses are AF_INET6.
268 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400269static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
270 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400271{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
273 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400274
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400275 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
276 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400277 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500278
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400279 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500280}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400281#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
282static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
283 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400284{
285 return 0;
286}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500287#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500288
David Howells24c8dbb2006-08-22 20:06:10 -0400289/*
Ian Dalld7371c42009-03-10 20:33:22 -0400290 * Test if two ip4 socket addresses refer to the same socket, by
291 * comparing relevant fields. The padding bytes specifically, are
292 * not compared.
293 *
294 * The caller should ensure both socket addresses are AF_INET.
295 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400296static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
297 const struct sockaddr *sa2)
298{
299 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
300 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
301
302 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
303}
304
305static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
306 const struct sockaddr *sa2)
307{
308 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
309 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
310
311 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
312 (sin1->sin6_port == sin2->sin6_port);
313}
314
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400315static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
316 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400317{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400318 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
319 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400320
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400321 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
322 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400323}
324
325/*
Ian Dalld7371c42009-03-10 20:33:22 -0400326 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400327 * by comparing (only) relevant fields, excluding the port number.
328 */
329static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
330 const struct sockaddr *sa2)
331{
332 if (sa1->sa_family != sa2->sa_family)
333 return 0;
334
335 switch (sa1->sa_family) {
336 case AF_INET:
337 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
338 case AF_INET6:
339 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
340 }
341 return 0;
342}
343
344/*
345 * Test if two socket addresses represent the same actual socket,
346 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400347 */
348static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
349 const struct sockaddr *sa2)
350{
351 if (sa1->sa_family != sa2->sa_family)
352 return 0;
353
354 switch (sa1->sa_family) {
355 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400356 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400357 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400358 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400359 }
360 return 0;
361}
362
363/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500364 * Find a client by IP address and protocol version
365 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400366 */
Chuck Leverff052642007-12-10 14:58:44 -0500367struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500368{
369 struct nfs_client *clp;
370
371 spin_lock(&nfs_client_lock);
372 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500373 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
374
Trond Myklebustc81468a2007-12-14 14:56:05 -0500375 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400376 if (!(clp->cl_cons_state == NFS_CS_READY ||
377 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500378 continue;
379
380 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500381 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500382 continue;
383
384 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500385 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500386 continue;
387
388 atomic_inc(&clp->cl_count);
389 spin_unlock(&nfs_client_lock);
390 return clp;
391 }
392 spin_unlock(&nfs_client_lock);
393 return NULL;
394}
395
396/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500397 * Find a client by IP address and protocol version
398 * - returns NULL if no such client
399 */
400struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
401{
402 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
403 u32 nfsvers = clp->rpc_ops->version;
404
405 spin_lock(&nfs_client_lock);
406 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
407 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
408
409 /* Don't match clients that failed to initialise properly */
410 if (clp->cl_cons_state != NFS_CS_READY)
411 continue;
412
413 /* Different NFS versions cannot share the same nfs_client */
414 if (clp->rpc_ops->version != nfsvers)
415 continue;
416
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500417 /* Match only the IP address, not the port number */
418 if (!nfs_sockaddr_match_ipaddr(sap, clap))
419 continue;
420
421 atomic_inc(&clp->cl_count);
422 spin_unlock(&nfs_client_lock);
423 return clp;
424 }
425 spin_unlock(&nfs_client_lock);
426 return NULL;
427}
428
429/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500430 * Find an nfs_client on the list that matches the initialisation data
431 * that is supplied.
432 */
433static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400434{
435 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400436 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400437
438 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400439 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700440 /* Don't match clients that failed to initialise properly */
441 if (clp->cl_cons_state < 0)
442 continue;
443
David Howells24c8dbb2006-08-22 20:06:10 -0400444 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500445 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400446 continue;
447
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500448 if (clp->cl_proto != data->proto)
449 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400450 /* Match nfsv4 minorversion */
451 if (clp->cl_minorversion != data->minorversion)
452 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500453 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400454 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400455 continue;
456
Trond Myklebustc81468a2007-12-14 14:56:05 -0500457 atomic_inc(&clp->cl_count);
458 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400459 }
David Howells24c8dbb2006-08-22 20:06:10 -0400460 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400461}
462
463/*
464 * Look up a client by IP address and protocol version
465 * - creates a new record if one doesn't yet exist
466 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500467static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400468{
469 struct nfs_client *clp, *new = NULL;
470 int error;
471
Chuck Leverd7422c42007-12-10 14:58:51 -0500472 dprintk("--> nfs_get_client(%s,v%u)\n",
473 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400474
475 /* see if the client already exists */
476 do {
477 spin_lock(&nfs_client_lock);
478
Trond Myklebustc81468a2007-12-14 14:56:05 -0500479 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400480 if (clp)
481 goto found_client;
482 if (new)
483 goto install_client;
484
485 spin_unlock(&nfs_client_lock);
486
Trond Myklebust3a498022007-12-14 14:56:04 -0500487 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700488 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400489
Chuck Levera21bdd92009-06-17 18:02:10 -0700490 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
491 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400492
493 /* install a new client and return with it unready */
494install_client:
495 clp = new;
496 list_add(&clp->cl_share_link, &nfs_client_list);
497 spin_unlock(&nfs_client_lock);
498 dprintk("--> nfs_get_client() = %p [new]\n", clp);
499 return clp;
500
501 /* found an existing client
502 * - make sure it's ready before returning
503 */
504found_client:
505 spin_unlock(&nfs_client_lock);
506
507 if (new)
508 nfs_free_client(new);
509
Matthew Wilcox150030b2007-12-06 16:24:39 -0500510 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400511 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400512 if (error < 0) {
513 nfs_put_client(clp);
514 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400515 }
516
517 if (clp->cl_cons_state < NFS_CS_READY) {
518 error = clp->cl_cons_state;
519 nfs_put_client(clp);
520 return ERR_PTR(error);
521 }
522
David Howells54ceac42006-08-22 20:06:13 -0400523 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
524
David Howells24c8dbb2006-08-22 20:06:10 -0400525 dprintk("--> nfs_get_client() = %p [share]\n", clp);
526 return clp;
527}
528
529/*
530 * Mark a server as ready or failed
531 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400532void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400533{
534 clp->cl_cons_state = state;
535 wake_up_all(&nfs_client_active_wq);
536}
David Howells5006a762006-08-22 20:06:12 -0400537
538/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400539 * With sessions, the client is not marked ready until after a
540 * successful EXCHANGE_ID and CREATE_SESSION.
541 *
542 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
543 * other versions of NFS can be tried.
544 */
545int nfs4_check_client_ready(struct nfs_client *clp)
546{
547 if (!nfs4_has_session(clp))
548 return 0;
549 if (clp->cl_cons_state < NFS_CS_READY)
550 return -EPROTONOSUPPORT;
551 return 0;
552}
553
554/*
David Howells5006a762006-08-22 20:06:12 -0400555 * Initialise the timeout values for a connection
556 */
557static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
558 unsigned int timeo, unsigned int retrans)
559{
560 to->to_initval = timeo * HZ / 10;
561 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400562
563 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400564 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400565 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400566 if (to->to_retries == 0)
567 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500568 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400569 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400570 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
571 to->to_initval = NFS_MAX_TCP_TIMEOUT;
572 to->to_increment = to->to_initval;
573 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500574 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
575 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
576 if (to->to_maxval < to->to_initval)
577 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400578 to->to_exponential = 0;
579 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400580 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400581 if (to->to_retries == 0)
582 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400583 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400584 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400585 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
586 to->to_initval = NFS_MAX_UDP_TIMEOUT;
587 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
588 to->to_exponential = 1;
589 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400590 default:
591 BUG();
David Howells5006a762006-08-22 20:06:12 -0400592 }
593}
594
595/*
596 * Create an RPC client handle
597 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500598static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500599 const struct rpc_timeout *timeparms,
600 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500601 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400602{
David Howells5006a762006-08-22 20:06:12 -0400603 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400604 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500605 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400606 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500607 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500608 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400609 .servername = clp->cl_hostname,
610 .program = &nfs_program,
611 .version = clp->rpc_ops->version,
612 .authflavor = flavor,
613 };
David Howells5006a762006-08-22 20:06:12 -0400614
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500615 if (discrtry)
616 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
617 if (noresvport)
618 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
619
David Howells5006a762006-08-22 20:06:12 -0400620 if (!IS_ERR(clp->cl_rpcclient))
621 return 0;
622
Chuck Lever41877d22006-08-22 20:06:20 -0400623 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400624 if (IS_ERR(clnt)) {
625 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700626 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400627 return PTR_ERR(clnt);
628 }
629
David Howells5006a762006-08-22 20:06:12 -0400630 clp->cl_rpcclient = clnt;
631 return 0;
632}
David Howells54ceac42006-08-22 20:06:13 -0400633
634/*
635 * Version 2 or 3 client destruction
636 */
637static void nfs_destroy_server(struct nfs_server *server)
638{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400639 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
640 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500641 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400642}
643
644/*
645 * Version 2 or 3 lockd setup
646 */
647static int nfs_start_lockd(struct nfs_server *server)
648{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500649 struct nlm_host *host;
650 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500651 struct nlmclnt_initdata nlm_init = {
652 .hostname = clp->cl_hostname,
653 .address = (struct sockaddr *)&clp->cl_addr,
654 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500655 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500656 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
657 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500658 };
David Howells54ceac42006-08-22 20:06:13 -0400659
Chuck Lever883bb162008-01-15 16:04:20 -0500660 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500661 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400662 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
663 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500664 return 0;
665
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400666 switch (clp->cl_proto) {
667 default:
668 nlm_init.protocol = IPPROTO_TCP;
669 break;
670 case XPRT_TRANSPORT_UDP:
671 nlm_init.protocol = IPPROTO_UDP;
672 }
673
Chuck Lever883bb162008-01-15 16:04:20 -0500674 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500675 if (IS_ERR(host))
676 return PTR_ERR(host);
677
678 server->nlm_host = host;
679 server->destroy = nfs_destroy_server;
680 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400681}
682
683/*
684 * Initialise an NFSv3 ACL client connection
685 */
686#ifdef CONFIG_NFS_V3_ACL
687static void nfs_init_server_aclclient(struct nfs_server *server)
688{
Trond Myklebust40c553192007-12-14 14:56:07 -0500689 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400690 goto out_noacl;
691 if (server->flags & NFS_MOUNT_NOACL)
692 goto out_noacl;
693
694 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
695 if (IS_ERR(server->client_acl))
696 goto out_noacl;
697
698 /* No errors! Assume that Sun nfsacls are supported */
699 server->caps |= NFS_CAP_ACLS;
700 return;
701
702out_noacl:
703 server->caps &= ~NFS_CAP_ACLS;
704}
705#else
706static inline void nfs_init_server_aclclient(struct nfs_server *server)
707{
708 server->flags &= ~NFS_MOUNT_NOACL;
709 server->caps &= ~NFS_CAP_ACLS;
710}
711#endif
712
713/*
714 * Create a general RPC client
715 */
Trond Myklebust33170232007-12-20 16:03:59 -0500716static int nfs_init_server_rpcclient(struct nfs_server *server,
717 const struct rpc_timeout *timeo,
718 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400719{
720 struct nfs_client *clp = server->nfs_client;
721
722 server->client = rpc_clone_client(clp->cl_rpcclient);
723 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700724 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400725 return PTR_ERR(server->client);
726 }
727
Trond Myklebust33170232007-12-20 16:03:59 -0500728 memcpy(&server->client->cl_timeout_default,
729 timeo,
730 sizeof(server->client->cl_timeout_default));
731 server->client->cl_timeout = &server->client->cl_timeout_default;
732
David Howells54ceac42006-08-22 20:06:13 -0400733 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
734 struct rpc_auth *auth;
735
736 auth = rpcauth_create(pseudoflavour, server->client);
737 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700738 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400739 return PTR_ERR(auth);
740 }
741 }
742 server->client->cl_softrtry = 0;
743 if (server->flags & NFS_MOUNT_SOFT)
744 server->client->cl_softrtry = 1;
745
David Howells54ceac42006-08-22 20:06:13 -0400746 return 0;
747}
748
749/*
750 * Initialise an NFS2 or NFS3 client
751 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400752static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500753 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400754 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400755{
David Howells54ceac42006-08-22 20:06:13 -0400756 int error;
757
758 if (clp->cl_cons_state == NFS_CS_READY) {
759 /* the client is already initialised */
760 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
761 return 0;
762 }
763
David Howells54ceac42006-08-22 20:06:13 -0400764 /*
765 * Create a client RPC handle for doing FSSTAT with UNIX auth only
766 * - RFC 2623, sec 2.3.2
767 */
Chuck Leverd7403512008-12-23 15:21:37 -0500768 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
769 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400770 if (error < 0)
771 goto error;
772 nfs_mark_client_ready(clp, NFS_CS_READY);
773 return 0;
774
775error:
776 nfs_mark_client_ready(clp, error);
777 dprintk("<-- nfs_init_client() = xerror %d\n", error);
778 return error;
779}
780
781/*
782 * Create a version 2 or 3 client
783 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400784static int nfs_init_server(struct nfs_server *server,
785 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400786{
Trond Myklebust3a498022007-12-14 14:56:04 -0500787 struct nfs_client_initdata cl_init = {
788 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500789 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500790 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500791 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500792 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500793 };
Trond Myklebust33170232007-12-20 16:03:59 -0500794 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400795 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500796 int error;
David Howells54ceac42006-08-22 20:06:13 -0400797
798 dprintk("--> nfs_init_server()\n");
799
800#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400801 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500802 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400803#endif
804
805 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500806 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400807 if (IS_ERR(clp)) {
808 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
809 return PTR_ERR(clp);
810 }
811
Trond Myklebust33170232007-12-20 16:03:59 -0500812 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
813 data->timeo, data->retrans);
814 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400815 if (error < 0)
816 goto error;
817
818 server->nfs_client = clp;
819
820 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400821 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100822 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400823 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
824 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
825 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400826
827 if (data->rsize)
828 server->rsize = nfs_block_size(data->rsize, NULL);
829 if (data->wsize)
830 server->wsize = nfs_block_size(data->wsize, NULL);
831
832 server->acregmin = data->acregmin * HZ;
833 server->acregmax = data->acregmax * HZ;
834 server->acdirmin = data->acdirmin * HZ;
835 server->acdirmax = data->acdirmax * HZ;
836
837 /* Start lockd here, before we might error out */
838 error = nfs_start_lockd(server);
839 if (error < 0)
840 goto error;
841
Chuck Leverf22d6d72008-03-14 14:10:22 -0400842 server->port = data->nfs_server.port;
843
Trond Myklebust33170232007-12-20 16:03:59 -0500844 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400845 if (error < 0)
846 goto error;
847
Chuck Lever3f8400d2008-03-14 14:10:30 -0400848 /* Preserve the values of mount_server-related mount options */
849 if (data->mount_server.addrlen) {
850 memcpy(&server->mountd_address, &data->mount_server.address,
851 data->mount_server.addrlen);
852 server->mountd_addrlen = data->mount_server.addrlen;
853 }
854 server->mountd_version = data->mount_server.version;
855 server->mountd_port = data->mount_server.port;
856 server->mountd_protocol = data->mount_server.protocol;
857
David Howells54ceac42006-08-22 20:06:13 -0400858 server->namelen = data->namlen;
859 /* Create a client RPC handle for the NFSv3 ACL management interface */
860 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400861 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
862 return 0;
863
864error:
865 server->nfs_client = NULL;
866 nfs_put_client(clp);
867 dprintk("<-- nfs_init_server() = xerror %d\n", error);
868 return error;
869}
870
871/*
872 * Load up the server record from information gained in an fsinfo record
873 */
874static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
875{
876 unsigned long max_rpc_payload;
877
878 /* Work out a lot of parameters */
879 if (server->rsize == 0)
880 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
881 if (server->wsize == 0)
882 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
883
884 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
885 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
886 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
887 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
888
889 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
890 if (server->rsize > max_rpc_payload)
891 server->rsize = max_rpc_payload;
892 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
893 server->rsize = NFS_MAX_FILE_IO_SIZE;
894 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700895
Jens Axboed9938312009-06-12 14:45:52 +0200896 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400897 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
898
899 if (server->wsize > max_rpc_payload)
900 server->wsize = max_rpc_payload;
901 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
902 server->wsize = NFS_MAX_FILE_IO_SIZE;
903 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400904 set_pnfs_layoutdriver(server, fsinfo->layouttype);
905
David Howells54ceac42006-08-22 20:06:13 -0400906 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
907
908 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400909 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
910 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400911 if (server->dtsize > server->rsize)
912 server->dtsize = server->rsize;
913
914 if (server->flags & NFS_MOUNT_NOAC) {
915 server->acregmin = server->acregmax = 0;
916 server->acdirmin = server->acdirmax = 0;
917 }
918
919 server->maxfilesize = fsinfo->maxfilesize;
920
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700921 server->time_delta = fsinfo->time_delta;
922
David Howells54ceac42006-08-22 20:06:13 -0400923 /* We're airborne Set socket buffersize */
924 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
925}
926
927/*
928 * Probe filesystem information, including the FSID on v2/v3
929 */
930static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
931{
932 struct nfs_fsinfo fsinfo;
933 struct nfs_client *clp = server->nfs_client;
934 int error;
935
936 dprintk("--> nfs_probe_fsinfo()\n");
937
938 if (clp->rpc_ops->set_capabilities != NULL) {
939 error = clp->rpc_ops->set_capabilities(server, mntfh);
940 if (error < 0)
941 goto out_error;
942 }
943
944 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400945 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400946 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
947 if (error < 0)
948 goto out_error;
949
950 nfs_server_set_fsinfo(server, &fsinfo);
951
952 /* Get some general file system info */
953 if (server->namelen == 0) {
954 struct nfs_pathconf pathinfo;
955
956 pathinfo.fattr = fattr;
957 nfs_fattr_init(fattr);
958
959 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
960 server->namelen = pathinfo.max_namelen;
961 }
962
963 dprintk("<-- nfs_probe_fsinfo() = 0\n");
964 return 0;
965
966out_error:
967 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
968 return error;
969}
970
971/*
972 * Copy useful information when duplicating a server record
973 */
974static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
975{
976 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400977 target->rsize = source->rsize;
978 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400979 target->acregmin = source->acregmin;
980 target->acregmax = source->acregmax;
981 target->acdirmin = source->acdirmin;
982 target->acdirmax = source->acdirmax;
983 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -0400984 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -0400985}
986
987/*
988 * Allocate and initialise a server record
989 */
990static struct nfs_server *nfs_alloc_server(void)
991{
992 struct nfs_server *server;
993
994 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
995 if (!server)
996 return NULL;
997
998 server->client = server->client_acl = ERR_PTR(-EINVAL);
999
1000 /* Zero out the NFS state stuff */
1001 INIT_LIST_HEAD(&server->client_link);
1002 INIT_LIST_HEAD(&server->master_link);
1003
Steve Dicksonef818a22007-11-08 04:05:04 -05001004 atomic_set(&server->active, 0);
1005
David Howells54ceac42006-08-22 20:06:13 -04001006 server->io_stats = nfs_alloc_iostats();
1007 if (!server->io_stats) {
1008 kfree(server);
1009 return NULL;
1010 }
1011
Jens Axboe48d07642009-09-21 09:59:39 +02001012 if (bdi_init(&server->backing_dev_info)) {
1013 nfs_free_iostats(server->io_stats);
1014 kfree(server);
1015 return NULL;
1016 }
1017
David Howells54ceac42006-08-22 20:06:13 -04001018 return server;
1019}
1020
1021/*
1022 * Free up a server record
1023 */
1024void nfs_free_server(struct nfs_server *server)
1025{
1026 dprintk("--> nfs_free_server()\n");
1027
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001028 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001029 spin_lock(&nfs_client_lock);
1030 list_del(&server->client_link);
1031 list_del(&server->master_link);
1032 spin_unlock(&nfs_client_lock);
1033
1034 if (server->destroy != NULL)
1035 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001036
1037 if (!IS_ERR(server->client_acl))
1038 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001039 if (!IS_ERR(server->client))
1040 rpc_shutdown_client(server->client);
1041
1042 nfs_put_client(server->nfs_client);
1043
1044 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001045 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001046 kfree(server);
1047 nfs_release_automount_timer();
1048 dprintk("<-- nfs_free_server()\n");
1049}
1050
1051/*
1052 * Create a version 2 or 3 volume record
1053 * - keyed on server and FSID
1054 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001055struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001056 struct nfs_fh *mntfh)
1057{
1058 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001059 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001060 int error;
1061
1062 server = nfs_alloc_server();
1063 if (!server)
1064 return ERR_PTR(-ENOMEM);
1065
Trond Myklebustfbca7792010-04-16 16:22:46 -04001066 error = -ENOMEM;
1067 fattr = nfs_alloc_fattr();
1068 if (fattr == NULL)
1069 goto error;
1070
David Howells54ceac42006-08-22 20:06:13 -04001071 /* Get a client representation */
1072 error = nfs_init_server(server, data);
1073 if (error < 0)
1074 goto error;
1075
1076 BUG_ON(!server->nfs_client);
1077 BUG_ON(!server->nfs_client->rpc_ops);
1078 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1079
1080 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001081 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001082 if (error < 0)
1083 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001084 if (server->nfs_client->rpc_ops->version == 3) {
1085 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1086 server->namelen = NFS3_MAXNAMLEN;
1087 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1088 server->caps |= NFS_CAP_READDIRPLUS;
1089 } else {
1090 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1091 server->namelen = NFS2_MAXNAMLEN;
1092 }
1093
Trond Myklebustfbca7792010-04-16 16:22:46 -04001094 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1095 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001096 if (error < 0) {
1097 dprintk("nfs_create_server: getattr error = %d\n", -error);
1098 goto error;
1099 }
1100 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001101 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001102
David Howells6daabf12006-08-24 15:44:16 -04001103 dprintk("Server FSID: %llx:%llx\n",
1104 (unsigned long long) server->fsid.major,
1105 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001106
David Howells54ceac42006-08-22 20:06:13 -04001107 spin_lock(&nfs_client_lock);
1108 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1109 list_add_tail(&server->master_link, &nfs_volume_list);
1110 spin_unlock(&nfs_client_lock);
1111
1112 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001113 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001114 return server;
1115
1116error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001117 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001118 nfs_free_server(server);
1119 return ERR_PTR(error);
1120}
1121
1122#ifdef CONFIG_NFS_V4
1123/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001124 * Initialize the NFS4 callback service
1125 */
1126static int nfs4_init_callback(struct nfs_client *clp)
1127{
1128 int error;
1129
1130 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001131 if (nfs4_has_session(clp)) {
1132 error = xprt_setup_backchannel(
1133 clp->cl_rpcclient->cl_xprt,
1134 NFS41_BC_MIN_CALLBACKS);
1135 if (error < 0)
1136 return error;
1137 }
1138
Trond Myklebust97dc1352010-06-16 09:52:26 -04001139 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001140 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001141 if (error < 0) {
1142 dprintk("%s: failed to start callback. Error = %d\n",
1143 __func__, error);
1144 return error;
1145 }
1146 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1147 }
1148 return 0;
1149}
1150
1151/*
Andy Adamson557134a2009-04-01 09:21:53 -04001152 * Initialize the minor version specific parts of an NFS4 client record
1153 */
1154static int nfs4_init_client_minor_version(struct nfs_client *clp)
1155{
1156#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001157 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001158 struct nfs4_session *session = NULL;
1159 /*
1160 * Create the session and mark it expired.
1161 * When a SEQUENCE operation encounters the expired session
1162 * it will do session recovery to initialize it.
1163 */
1164 session = nfs4_alloc_session(clp);
1165 if (!session)
1166 return -ENOMEM;
1167
1168 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001169 /*
1170 * The create session reply races with the server back
1171 * channel probe. Mark the client NFS_CS_SESSION_INITING
1172 * so that the client back channel can find the
1173 * nfs_client struct
1174 */
1175 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001176 }
1177#endif /* CONFIG_NFS_V4_1 */
1178
Benny Halevy71468512009-04-01 09:22:56 -04001179 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001180}
1181
1182/*
David Howells54ceac42006-08-22 20:06:13 -04001183 * Initialise an NFS4 client record
1184 */
1185static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001186 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001187 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001188 rpc_authflavor_t authflavour,
1189 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001190{
1191 int error;
1192
1193 if (clp->cl_cons_state == NFS_CS_READY) {
1194 /* the client is initialised already */
1195 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1196 return 0;
1197 }
1198
1199 /* Check NFS protocol revision and initialize RPC op vector */
1200 clp->rpc_ops = &nfs_v4_clientops;
1201
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001202 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001203 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001204 if (error < 0)
1205 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001206 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001207
1208 error = nfs_idmap_new(clp);
1209 if (error < 0) {
1210 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001211 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001212 goto error;
1213 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001214 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001215
Andy Adamson557134a2009-04-01 09:21:53 -04001216 error = nfs4_init_client_minor_version(clp);
1217 if (error < 0)
1218 goto error;
1219
Andy Adamson76db6d92009-04-01 09:22:38 -04001220 if (!nfs4_has_session(clp))
1221 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001222 return 0;
1223
1224error:
1225 nfs_mark_client_ready(clp, error);
1226 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1227 return error;
1228}
1229
1230/*
1231 * Set up an NFS4 client
1232 */
1233static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001234 const char *hostname,
1235 const struct sockaddr *addr,
1236 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001237 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001238 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001239 int proto, const struct rpc_timeout *timeparms,
1240 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001241{
Trond Myklebust3a498022007-12-14 14:56:04 -05001242 struct nfs_client_initdata cl_init = {
1243 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001244 .addr = addr,
1245 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001246 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001247 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001248 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001249 };
David Howells54ceac42006-08-22 20:06:13 -04001250 struct nfs_client *clp;
1251 int error;
1252
1253 dprintk("--> nfs4_set_client()\n");
1254
1255 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001256 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001257 if (IS_ERR(clp)) {
1258 error = PTR_ERR(clp);
1259 goto error;
1260 }
Chuck Leverd7403512008-12-23 15:21:37 -05001261 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1262 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001263 if (error < 0)
1264 goto error_put;
1265
1266 server->nfs_client = clp;
1267 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1268 return 0;
1269
1270error_put:
1271 nfs_put_client(clp);
1272error:
1273 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1274 return error;
1275}
1276
Andy Adamson557134a2009-04-01 09:21:53 -04001277
1278/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001279 * Session has been established, and the client marked ready.
1280 * Set the mount rsize and wsize with negotiated fore channel
1281 * attributes which will be bound checked in nfs_server_set_fsinfo.
1282 */
1283static void nfs4_session_set_rwsize(struct nfs_server *server)
1284{
1285#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001286 struct nfs4_session *sess;
1287 u32 server_resp_sz;
1288 u32 server_rqst_sz;
1289
Andy Adamson96b09e02009-04-01 09:22:33 -04001290 if (!nfs4_has_session(server->nfs_client))
1291 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001292 sess = server->nfs_client->cl_session;
1293 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1294 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1295
1296 if (server->rsize > server_resp_sz)
1297 server->rsize = server_resp_sz;
1298 if (server->wsize > server_rqst_sz)
1299 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001300#endif /* CONFIG_NFS_V4_1 */
1301}
1302
Trond Myklebust44950b62010-06-17 11:45:12 -04001303static int nfs4_server_common_setup(struct nfs_server *server,
1304 struct nfs_fh *mntfh)
1305{
1306 struct nfs_fattr *fattr;
1307 int error;
1308
1309 BUG_ON(!server->nfs_client);
1310 BUG_ON(!server->nfs_client->rpc_ops);
1311 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1312
1313 fattr = nfs_alloc_fattr();
1314 if (fattr == NULL)
1315 return -ENOMEM;
1316
1317 /* We must ensure the session is initialised first */
1318 error = nfs4_init_session(server);
1319 if (error < 0)
1320 goto out;
1321
1322 /* Probe the root fh to retrieve its FSID and filehandle */
1323 error = nfs4_get_rootfh(server, mntfh);
1324 if (error < 0)
1325 goto out;
1326
1327 dprintk("Server FSID: %llx:%llx\n",
1328 (unsigned long long) server->fsid.major,
1329 (unsigned long long) server->fsid.minor);
1330 dprintk("Mount FH: %d\n", mntfh->size);
1331
1332 nfs4_session_set_rwsize(server);
1333
1334 error = nfs_probe_fsinfo(server, mntfh, fattr);
1335 if (error < 0)
1336 goto out;
1337
1338 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1339 server->namelen = NFS4_MAXNAMLEN;
1340
1341 spin_lock(&nfs_client_lock);
1342 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1343 list_add_tail(&server->master_link, &nfs_volume_list);
1344 spin_unlock(&nfs_client_lock);
1345
1346 server->mount_time = jiffies;
1347out:
1348 nfs_free_fattr(fattr);
1349 return error;
1350}
1351
Andy Adamson96b09e02009-04-01 09:22:33 -04001352/*
David Howells54ceac42006-08-22 20:06:13 -04001353 * Create a version 4 volume record
1354 */
1355static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001356 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001357{
Trond Myklebust33170232007-12-20 16:03:59 -05001358 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001359 int error;
1360
1361 dprintk("--> nfs4_init_server()\n");
1362
Trond Myklebust33170232007-12-20 16:03:59 -05001363 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1364 data->timeo, data->retrans);
1365
Chuck Lever542fcc32008-12-23 15:21:36 -05001366 /* Initialise the client representation from the mount data */
1367 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001368 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1369 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1370 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001371 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001372
Trond Myklebust33170232007-12-20 16:03:59 -05001373 /* Get a client record */
1374 error = nfs4_set_client(server,
1375 data->nfs_server.hostname,
1376 (const struct sockaddr *)&data->nfs_server.address,
1377 data->nfs_server.addrlen,
1378 data->client_address,
1379 data->auth_flavors[0],
1380 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001381 &timeparms,
1382 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001383 if (error < 0)
1384 goto error;
1385
David Howells54ceac42006-08-22 20:06:13 -04001386 if (data->rsize)
1387 server->rsize = nfs_block_size(data->rsize, NULL);
1388 if (data->wsize)
1389 server->wsize = nfs_block_size(data->wsize, NULL);
1390
1391 server->acregmin = data->acregmin * HZ;
1392 server->acregmax = data->acregmax * HZ;
1393 server->acdirmin = data->acdirmin * HZ;
1394 server->acdirmax = data->acdirmax * HZ;
1395
Chuck Leverf22d6d72008-03-14 14:10:22 -04001396 server->port = data->nfs_server.port;
1397
Trond Myklebust33170232007-12-20 16:03:59 -05001398 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001399
Trond Myklebust33170232007-12-20 16:03:59 -05001400error:
David Howells54ceac42006-08-22 20:06:13 -04001401 /* Done */
1402 dprintk("<-- nfs4_init_server() = %d\n", error);
1403 return error;
1404}
1405
1406/*
1407 * Create a version 4 volume record
1408 * - keyed on server and FSID
1409 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001410struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001411 struct nfs_fh *mntfh)
1412{
David Howells54ceac42006-08-22 20:06:13 -04001413 struct nfs_server *server;
1414 int error;
1415
1416 dprintk("--> nfs4_create_server()\n");
1417
1418 server = nfs_alloc_server();
1419 if (!server)
1420 return ERR_PTR(-ENOMEM);
1421
David Howells54ceac42006-08-22 20:06:13 -04001422 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001423 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001424 if (error < 0)
1425 goto error;
1426
Trond Myklebust44950b62010-06-17 11:45:12 -04001427 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001428 if (error < 0)
1429 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001430
David Howells54ceac42006-08-22 20:06:13 -04001431 dprintk("<-- nfs4_create_server() = %p\n", server);
1432 return server;
1433
1434error:
1435 nfs_free_server(server);
1436 dprintk("<-- nfs4_create_server() = error %d\n", error);
1437 return ERR_PTR(error);
1438}
1439
1440/*
1441 * Create an NFS4 referral server record
1442 */
1443struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001444 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001445{
1446 struct nfs_client *parent_client;
1447 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001448 int error;
1449
1450 dprintk("--> nfs4_create_referral_server()\n");
1451
1452 server = nfs_alloc_server();
1453 if (!server)
1454 return ERR_PTR(-ENOMEM);
1455
1456 parent_server = NFS_SB(data->sb);
1457 parent_client = parent_server->nfs_client;
1458
Chuck Lever542fcc32008-12-23 15:21:36 -05001459 /* Initialise the client representation from the parent server */
1460 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001461 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001462
David Howells54ceac42006-08-22 20:06:13 -04001463 /* Get a client representation.
1464 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001465 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001466 data->addr,
1467 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001468 parent_client->cl_ipaddr,
1469 data->authflavor,
1470 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001471 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001472 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001473 if (error < 0)
1474 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001475
Trond Myklebust33170232007-12-20 16:03:59 -05001476 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001477 if (error < 0)
1478 goto error;
1479
Trond Myklebust44950b62010-06-17 11:45:12 -04001480 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001481 if (error < 0)
1482 goto error;
1483
David Howells54ceac42006-08-22 20:06:13 -04001484 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1485 return server;
1486
1487error:
1488 nfs_free_server(server);
1489 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1490 return ERR_PTR(error);
1491}
1492
1493#endif /* CONFIG_NFS_V4 */
1494
1495/*
1496 * Clone an NFS2, NFS3 or NFS4 server record
1497 */
1498struct nfs_server *nfs_clone_server(struct nfs_server *source,
1499 struct nfs_fh *fh,
1500 struct nfs_fattr *fattr)
1501{
1502 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001503 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001504 int error;
1505
1506 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001507 (unsigned long long) fattr->fsid.major,
1508 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001509
1510 server = nfs_alloc_server();
1511 if (!server)
1512 return ERR_PTR(-ENOMEM);
1513
Trond Myklebustfbca7792010-04-16 16:22:46 -04001514 error = -ENOMEM;
1515 fattr_fsinfo = nfs_alloc_fattr();
1516 if (fattr_fsinfo == NULL)
1517 goto out_free_server;
1518
David Howells54ceac42006-08-22 20:06:13 -04001519 /* Copy data from the source */
1520 server->nfs_client = source->nfs_client;
1521 atomic_inc(&server->nfs_client->cl_count);
1522 nfs_server_copy_userdata(server, source);
1523
1524 server->fsid = fattr->fsid;
1525
Trond Myklebust33170232007-12-20 16:03:59 -05001526 error = nfs_init_server_rpcclient(server,
1527 source->client->cl_timeout,
1528 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001529 if (error < 0)
1530 goto out_free_server;
1531 if (!IS_ERR(source->client_acl))
1532 nfs_init_server_aclclient(server);
1533
1534 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001535 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001536 if (error < 0)
1537 goto out_free_server;
1538
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001539 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1540 server->namelen = NFS4_MAXNAMLEN;
1541
David Howells54ceac42006-08-22 20:06:13 -04001542 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001543 (unsigned long long) server->fsid.major,
1544 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001545
1546 error = nfs_start_lockd(server);
1547 if (error < 0)
1548 goto out_free_server;
1549
1550 spin_lock(&nfs_client_lock);
1551 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1552 list_add_tail(&server->master_link, &nfs_volume_list);
1553 spin_unlock(&nfs_client_lock);
1554
1555 server->mount_time = jiffies;
1556
Trond Myklebustfbca7792010-04-16 16:22:46 -04001557 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001558 dprintk("<-- nfs_clone_server() = %p\n", server);
1559 return server;
1560
1561out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001562 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001563 nfs_free_server(server);
1564 dprintk("<-- nfs_clone_server() = error %d\n", error);
1565 return ERR_PTR(error);
1566}
David Howells6aaca562006-08-22 20:06:13 -04001567
1568#ifdef CONFIG_PROC_FS
1569static struct proc_dir_entry *proc_fs_nfs;
1570
1571static int nfs_server_list_open(struct inode *inode, struct file *file);
1572static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1573static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1574static void nfs_server_list_stop(struct seq_file *p, void *v);
1575static int nfs_server_list_show(struct seq_file *m, void *v);
1576
James Morris88e9d342009-09-22 16:43:43 -07001577static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001578 .start = nfs_server_list_start,
1579 .next = nfs_server_list_next,
1580 .stop = nfs_server_list_stop,
1581 .show = nfs_server_list_show,
1582};
1583
Arjan van de Ven00977a52007-02-12 00:55:34 -08001584static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001585 .open = nfs_server_list_open,
1586 .read = seq_read,
1587 .llseek = seq_lseek,
1588 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001589 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001590};
1591
1592static int nfs_volume_list_open(struct inode *inode, struct file *file);
1593static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1594static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1595static void nfs_volume_list_stop(struct seq_file *p, void *v);
1596static int nfs_volume_list_show(struct seq_file *m, void *v);
1597
James Morris88e9d342009-09-22 16:43:43 -07001598static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001599 .start = nfs_volume_list_start,
1600 .next = nfs_volume_list_next,
1601 .stop = nfs_volume_list_stop,
1602 .show = nfs_volume_list_show,
1603};
1604
Arjan van de Ven00977a52007-02-12 00:55:34 -08001605static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001606 .open = nfs_volume_list_open,
1607 .read = seq_read,
1608 .llseek = seq_lseek,
1609 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001610 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001611};
1612
1613/*
1614 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1615 * we're dealing
1616 */
1617static int nfs_server_list_open(struct inode *inode, struct file *file)
1618{
1619 struct seq_file *m;
1620 int ret;
1621
1622 ret = seq_open(file, &nfs_server_list_ops);
1623 if (ret < 0)
1624 return ret;
1625
1626 m = file->private_data;
1627 m->private = PDE(inode)->data;
1628
1629 return 0;
1630}
1631
1632/*
1633 * set up the iterator to start reading from the server list and return the first item
1634 */
1635static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1636{
David Howells6aaca562006-08-22 20:06:13 -04001637 /* lock the list against modification */
1638 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001639 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001640}
1641
1642/*
1643 * move to next server
1644 */
1645static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1646{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001647 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001648}
1649
1650/*
1651 * clean up after reading from the transports list
1652 */
1653static void nfs_server_list_stop(struct seq_file *p, void *v)
1654{
1655 spin_unlock(&nfs_client_lock);
1656}
1657
1658/*
1659 * display a header line followed by a load of call lines
1660 */
1661static int nfs_server_list_show(struct seq_file *m, void *v)
1662{
1663 struct nfs_client *clp;
1664
1665 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001666 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001667 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1668 return 0;
1669 }
1670
1671 /* display one transport per line on subsequent lines */
1672 clp = list_entry(v, struct nfs_client, cl_share_link);
1673
Chuck Lever5d8515c2007-12-10 14:57:16 -05001674 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001675 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001676 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1677 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001678 atomic_read(&clp->cl_count),
1679 clp->cl_hostname);
1680
1681 return 0;
1682}
1683
1684/*
1685 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1686 */
1687static int nfs_volume_list_open(struct inode *inode, struct file *file)
1688{
1689 struct seq_file *m;
1690 int ret;
1691
1692 ret = seq_open(file, &nfs_volume_list_ops);
1693 if (ret < 0)
1694 return ret;
1695
1696 m = file->private_data;
1697 m->private = PDE(inode)->data;
1698
1699 return 0;
1700}
1701
1702/*
1703 * set up the iterator to start reading from the volume list and return the first item
1704 */
1705static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1706{
David Howells6aaca562006-08-22 20:06:13 -04001707 /* lock the list against modification */
1708 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001709 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001710}
1711
1712/*
1713 * move to next volume
1714 */
1715static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1716{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001717 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001718}
1719
1720/*
1721 * clean up after reading from the transports list
1722 */
1723static void nfs_volume_list_stop(struct seq_file *p, void *v)
1724{
1725 spin_unlock(&nfs_client_lock);
1726}
1727
1728/*
1729 * display a header line followed by a load of call lines
1730 */
1731static int nfs_volume_list_show(struct seq_file *m, void *v)
1732{
1733 struct nfs_server *server;
1734 struct nfs_client *clp;
1735 char dev[8], fsid[17];
1736
1737 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001738 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001739 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001740 return 0;
1741 }
1742 /* display one transport per line on subsequent lines */
1743 server = list_entry(v, struct nfs_server, master_link);
1744 clp = server->nfs_client;
1745
1746 snprintf(dev, 8, "%u:%u",
1747 MAJOR(server->s_dev), MINOR(server->s_dev));
1748
1749 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001750 (unsigned long long) server->fsid.major,
1751 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001752
David Howells5d1acff2009-04-03 16:42:47 +01001753 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001754 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001755 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1756 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001757 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001758 fsid,
1759 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001760
1761 return 0;
1762}
1763
1764/*
1765 * initialise the /proc/fs/nfsfs/ directory
1766 */
1767int __init nfs_fs_proc_init(void)
1768{
1769 struct proc_dir_entry *p;
1770
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001771 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001772 if (!proc_fs_nfs)
1773 goto error_0;
1774
David Howells6aaca562006-08-22 20:06:13 -04001775 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001776 p = proc_create("servers", S_IFREG|S_IRUGO,
1777 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001778 if (!p)
1779 goto error_1;
1780
David Howells6aaca562006-08-22 20:06:13 -04001781 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001782 p = proc_create("volumes", S_IFREG|S_IRUGO,
1783 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001784 if (!p)
1785 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001786 return 0;
1787
1788error_2:
1789 remove_proc_entry("servers", proc_fs_nfs);
1790error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001791 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001792error_0:
1793 return -ENOMEM;
1794}
1795
1796/*
1797 * clean up the /proc/fs/nfsfs/ directory
1798 */
1799void nfs_fs_proc_exit(void)
1800{
1801 remove_proc_entry("volumes", proc_fs_nfs);
1802 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001803 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001804}
1805
1806#endif /* CONFIG_PROC_FS */