blob: 8934b2e339df5acfe39f04bfaa213c168e71d652 [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;
Andy Adamson974cec82010-10-20 00:18:02 -0400159#if defined(CONFIG_NFS_V4_1)
160 INIT_LIST_HEAD(&clp->cl_layouts);
161#endif
David Howells14727282009-04-03 16:42:42 +0100162 nfs_fscache_get_client_cookie(clp);
163
David Howells24c8dbb2006-08-22 20:06:10 -0400164 return clp;
165
Benny Halevy71468512009-04-01 09:22:56 -0400166error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400167 kfree(clp);
168error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700169 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400170}
171
Trond Myklebust5dd31772006-08-24 01:03:05 -0400172#ifdef CONFIG_NFS_V4
Benny Halevy9bdaa862009-04-01 09:22:55 -0400173/*
Andy Adamson557134a2009-04-01 09:21:53 -0400174 * Clears/puts all minor version specific parts from an nfs_client struct
175 * reverting it to minorversion 0.
176 */
177static void nfs4_clear_client_minor_version(struct nfs_client *clp)
178{
179#ifdef CONFIG_NFS_V4_1
180 if (nfs4_has_session(clp)) {
181 nfs4_destroy_session(clp->cl_session);
182 clp->cl_session = NULL;
183 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400184
Trond Myklebust97dc1352010-06-16 09:52:26 -0400185 clp->cl_mvops = nfs_v4_minor_ops[0];
Andy Adamson557134a2009-04-01 09:21:53 -0400186#endif /* CONFIG_NFS_V4_1 */
187}
188
189/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800190 * Destroy the NFS4 callback service
191 */
192static void nfs4_destroy_callback(struct nfs_client *clp)
193{
194 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400195 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800196}
197
198static void nfs4_shutdown_client(struct nfs_client *clp)
199{
200 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
201 nfs4_kill_renewd(clp);
202 nfs4_clear_client_minor_version(clp);
203 nfs4_destroy_callback(clp);
204 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
205 nfs_idmap_delete(clp);
206
207 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
208}
209#else
210static void nfs4_shutdown_client(struct nfs_client *clp)
211{
212}
213#endif /* CONFIG_NFS_V4 */
214
215/*
David Howells24c8dbb2006-08-22 20:06:10 -0400216 * Destroy a shared client record
217 */
218static void nfs_free_client(struct nfs_client *clp)
219{
Trond Myklebust40c553192007-12-14 14:56:07 -0500220 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400221
Trond Myklebust5dd31772006-08-24 01:03:05 -0400222 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400223
David Howells14727282009-04-03 16:42:42 +0100224 nfs_fscache_release_client_cookie(clp);
225
David Howells24c8dbb2006-08-22 20:06:10 -0400226 /* -EIO all pending I/O */
227 if (!IS_ERR(clp->cl_rpcclient))
228 rpc_shutdown_client(clp->cl_rpcclient);
229
Trond Myklebust7c67db32008-04-07 20:50:11 -0400230 if (clp->cl_machine_cred != NULL)
231 put_rpccred(clp->cl_machine_cred);
232
David Howells24c8dbb2006-08-22 20:06:10 -0400233 kfree(clp->cl_hostname);
234 kfree(clp);
235
236 dprintk("<-- nfs_free_client()\n");
237}
238
239/*
240 * Release a reference to a shared client record
241 */
242void nfs_put_client(struct nfs_client *clp)
243{
David Howells27ba8512006-07-30 14:40:56 -0400244 if (!clp)
245 return;
246
David Howells24c8dbb2006-08-22 20:06:10 -0400247 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
248
249 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
250 list_del(&clp->cl_share_link);
251 spin_unlock(&nfs_client_lock);
252
253 BUG_ON(!list_empty(&clp->cl_superblocks));
254
255 nfs_free_client(clp);
256 }
257}
Andy Adamson16b374c2010-10-20 00:18:04 -0400258EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400259
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500260#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400261/*
262 * Test if two ip6 socket addresses refer to the same socket by
263 * comparing relevant fields. The padding bytes specifically, are not
264 * compared. sin6_flowinfo is not compared because it only affects QoS
265 * and sin6_scope_id is only compared if the address is "link local"
266 * because "link local" addresses need only be unique to a specific
267 * link. Conversely, ordinary unicast addresses might have different
268 * sin6_scope_id.
269 *
270 * The caller should ensure both socket addresses are AF_INET6.
271 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
273 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400274{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400275 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
276 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400277
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400278 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
279 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400280 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500281
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400282 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500283}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400284#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
285static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
286 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400287{
288 return 0;
289}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500290#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500291
David Howells24c8dbb2006-08-22 20:06:10 -0400292/*
Ian Dalld7371c42009-03-10 20:33:22 -0400293 * Test if two ip4 socket addresses refer to the same socket, by
294 * comparing relevant fields. The padding bytes specifically, are
295 * not compared.
296 *
297 * The caller should ensure both socket addresses are AF_INET.
298 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400299static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
300 const struct sockaddr *sa2)
301{
302 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
303 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
304
305 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
306}
307
308static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
309 const struct sockaddr *sa2)
310{
311 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
312 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
313
314 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
315 (sin1->sin6_port == sin2->sin6_port);
316}
317
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400318static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
319 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400320{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400321 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
322 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400323
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400324 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
325 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400326}
327
328/*
Ian Dalld7371c42009-03-10 20:33:22 -0400329 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400330 * by comparing (only) relevant fields, excluding the port number.
331 */
332static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
333 const struct sockaddr *sa2)
334{
335 if (sa1->sa_family != sa2->sa_family)
336 return 0;
337
338 switch (sa1->sa_family) {
339 case AF_INET:
340 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
341 case AF_INET6:
342 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
343 }
344 return 0;
345}
346
347/*
348 * Test if two socket addresses represent the same actual socket,
349 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400350 */
351static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
352 const struct sockaddr *sa2)
353{
354 if (sa1->sa_family != sa2->sa_family)
355 return 0;
356
357 switch (sa1->sa_family) {
358 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400359 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400360 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400361 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400362 }
363 return 0;
364}
365
366/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500367 * Find a client by IP address and protocol version
368 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400369 */
Chuck Leverff052642007-12-10 14:58:44 -0500370struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500371{
372 struct nfs_client *clp;
373
374 spin_lock(&nfs_client_lock);
375 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500376 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
377
Trond Myklebustc81468a2007-12-14 14:56:05 -0500378 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400379 if (!(clp->cl_cons_state == NFS_CS_READY ||
380 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500381 continue;
382
383 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500384 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500385 continue;
386
387 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500388 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500389 continue;
390
391 atomic_inc(&clp->cl_count);
392 spin_unlock(&nfs_client_lock);
393 return clp;
394 }
395 spin_unlock(&nfs_client_lock);
396 return NULL;
397}
398
399/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500400 * Find a client by IP address and protocol version
401 * - returns NULL if no such client
402 */
403struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
404{
405 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
406 u32 nfsvers = clp->rpc_ops->version;
407
408 spin_lock(&nfs_client_lock);
409 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
410 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
411
412 /* Don't match clients that failed to initialise properly */
413 if (clp->cl_cons_state != NFS_CS_READY)
414 continue;
415
416 /* Different NFS versions cannot share the same nfs_client */
417 if (clp->rpc_ops->version != nfsvers)
418 continue;
419
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500420 /* Match only the IP address, not the port number */
421 if (!nfs_sockaddr_match_ipaddr(sap, clap))
422 continue;
423
424 atomic_inc(&clp->cl_count);
425 spin_unlock(&nfs_client_lock);
426 return clp;
427 }
428 spin_unlock(&nfs_client_lock);
429 return NULL;
430}
431
432/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500433 * Find an nfs_client on the list that matches the initialisation data
434 * that is supplied.
435 */
436static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400437{
438 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400439 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400440
441 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400442 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700443 /* Don't match clients that failed to initialise properly */
444 if (clp->cl_cons_state < 0)
445 continue;
446
David Howells24c8dbb2006-08-22 20:06:10 -0400447 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500448 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400449 continue;
450
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500451 if (clp->cl_proto != data->proto)
452 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400453 /* Match nfsv4 minorversion */
454 if (clp->cl_minorversion != data->minorversion)
455 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500456 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400457 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400458 continue;
459
Trond Myklebustc81468a2007-12-14 14:56:05 -0500460 atomic_inc(&clp->cl_count);
461 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400462 }
David Howells24c8dbb2006-08-22 20:06:10 -0400463 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400464}
465
466/*
467 * Look up a client by IP address and protocol version
468 * - creates a new record if one doesn't yet exist
469 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500470static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400471{
472 struct nfs_client *clp, *new = NULL;
473 int error;
474
Chuck Leverd7422c42007-12-10 14:58:51 -0500475 dprintk("--> nfs_get_client(%s,v%u)\n",
476 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400477
478 /* see if the client already exists */
479 do {
480 spin_lock(&nfs_client_lock);
481
Trond Myklebustc81468a2007-12-14 14:56:05 -0500482 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400483 if (clp)
484 goto found_client;
485 if (new)
486 goto install_client;
487
488 spin_unlock(&nfs_client_lock);
489
Trond Myklebust3a498022007-12-14 14:56:04 -0500490 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700491 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400492
Chuck Levera21bdd92009-06-17 18:02:10 -0700493 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
494 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400495
496 /* install a new client and return with it unready */
497install_client:
498 clp = new;
499 list_add(&clp->cl_share_link, &nfs_client_list);
500 spin_unlock(&nfs_client_lock);
501 dprintk("--> nfs_get_client() = %p [new]\n", clp);
502 return clp;
503
504 /* found an existing client
505 * - make sure it's ready before returning
506 */
507found_client:
508 spin_unlock(&nfs_client_lock);
509
510 if (new)
511 nfs_free_client(new);
512
Matthew Wilcox150030b2007-12-06 16:24:39 -0500513 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400514 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400515 if (error < 0) {
516 nfs_put_client(clp);
517 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400518 }
519
520 if (clp->cl_cons_state < NFS_CS_READY) {
521 error = clp->cl_cons_state;
522 nfs_put_client(clp);
523 return ERR_PTR(error);
524 }
525
David Howells54ceac42006-08-22 20:06:13 -0400526 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
527
David Howells24c8dbb2006-08-22 20:06:10 -0400528 dprintk("--> nfs_get_client() = %p [share]\n", clp);
529 return clp;
530}
531
532/*
533 * Mark a server as ready or failed
534 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400535void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400536{
537 clp->cl_cons_state = state;
538 wake_up_all(&nfs_client_active_wq);
539}
David Howells5006a762006-08-22 20:06:12 -0400540
541/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400542 * With sessions, the client is not marked ready until after a
543 * successful EXCHANGE_ID and CREATE_SESSION.
544 *
545 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
546 * other versions of NFS can be tried.
547 */
548int nfs4_check_client_ready(struct nfs_client *clp)
549{
550 if (!nfs4_has_session(clp))
551 return 0;
552 if (clp->cl_cons_state < NFS_CS_READY)
553 return -EPROTONOSUPPORT;
554 return 0;
555}
556
557/*
David Howells5006a762006-08-22 20:06:12 -0400558 * Initialise the timeout values for a connection
559 */
560static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
561 unsigned int timeo, unsigned int retrans)
562{
563 to->to_initval = timeo * HZ / 10;
564 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400565
566 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400567 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400568 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400569 if (to->to_retries == 0)
570 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500571 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400572 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400573 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
574 to->to_initval = NFS_MAX_TCP_TIMEOUT;
575 to->to_increment = to->to_initval;
576 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500577 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
578 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
579 if (to->to_maxval < to->to_initval)
580 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400581 to->to_exponential = 0;
582 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400583 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400584 if (to->to_retries == 0)
585 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400586 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400587 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400588 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
589 to->to_initval = NFS_MAX_UDP_TIMEOUT;
590 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
591 to->to_exponential = 1;
592 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400593 default:
594 BUG();
David Howells5006a762006-08-22 20:06:12 -0400595 }
596}
597
598/*
599 * Create an RPC client handle
600 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500601static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500602 const struct rpc_timeout *timeparms,
603 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500604 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400605{
David Howells5006a762006-08-22 20:06:12 -0400606 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400607 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500608 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400609 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500610 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500611 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400612 .servername = clp->cl_hostname,
613 .program = &nfs_program,
614 .version = clp->rpc_ops->version,
615 .authflavor = flavor,
616 };
David Howells5006a762006-08-22 20:06:12 -0400617
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500618 if (discrtry)
619 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
620 if (noresvport)
621 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
622
David Howells5006a762006-08-22 20:06:12 -0400623 if (!IS_ERR(clp->cl_rpcclient))
624 return 0;
625
Chuck Lever41877d22006-08-22 20:06:20 -0400626 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400627 if (IS_ERR(clnt)) {
628 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700629 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400630 return PTR_ERR(clnt);
631 }
632
David Howells5006a762006-08-22 20:06:12 -0400633 clp->cl_rpcclient = clnt;
634 return 0;
635}
David Howells54ceac42006-08-22 20:06:13 -0400636
637/*
638 * Version 2 or 3 client destruction
639 */
640static void nfs_destroy_server(struct nfs_server *server)
641{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400642 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
643 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500644 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400645}
646
647/*
648 * Version 2 or 3 lockd setup
649 */
650static int nfs_start_lockd(struct nfs_server *server)
651{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500652 struct nlm_host *host;
653 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500654 struct nlmclnt_initdata nlm_init = {
655 .hostname = clp->cl_hostname,
656 .address = (struct sockaddr *)&clp->cl_addr,
657 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500658 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500659 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
660 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500661 };
David Howells54ceac42006-08-22 20:06:13 -0400662
Chuck Lever883bb162008-01-15 16:04:20 -0500663 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500664 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400665 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
666 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500667 return 0;
668
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400669 switch (clp->cl_proto) {
670 default:
671 nlm_init.protocol = IPPROTO_TCP;
672 break;
673 case XPRT_TRANSPORT_UDP:
674 nlm_init.protocol = IPPROTO_UDP;
675 }
676
Chuck Lever883bb162008-01-15 16:04:20 -0500677 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500678 if (IS_ERR(host))
679 return PTR_ERR(host);
680
681 server->nlm_host = host;
682 server->destroy = nfs_destroy_server;
683 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400684}
685
686/*
687 * Initialise an NFSv3 ACL client connection
688 */
689#ifdef CONFIG_NFS_V3_ACL
690static void nfs_init_server_aclclient(struct nfs_server *server)
691{
Trond Myklebust40c553192007-12-14 14:56:07 -0500692 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400693 goto out_noacl;
694 if (server->flags & NFS_MOUNT_NOACL)
695 goto out_noacl;
696
697 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
698 if (IS_ERR(server->client_acl))
699 goto out_noacl;
700
701 /* No errors! Assume that Sun nfsacls are supported */
702 server->caps |= NFS_CAP_ACLS;
703 return;
704
705out_noacl:
706 server->caps &= ~NFS_CAP_ACLS;
707}
708#else
709static inline void nfs_init_server_aclclient(struct nfs_server *server)
710{
711 server->flags &= ~NFS_MOUNT_NOACL;
712 server->caps &= ~NFS_CAP_ACLS;
713}
714#endif
715
716/*
717 * Create a general RPC client
718 */
Trond Myklebust33170232007-12-20 16:03:59 -0500719static int nfs_init_server_rpcclient(struct nfs_server *server,
720 const struct rpc_timeout *timeo,
721 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400722{
723 struct nfs_client *clp = server->nfs_client;
724
725 server->client = rpc_clone_client(clp->cl_rpcclient);
726 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700727 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400728 return PTR_ERR(server->client);
729 }
730
Trond Myklebust33170232007-12-20 16:03:59 -0500731 memcpy(&server->client->cl_timeout_default,
732 timeo,
733 sizeof(server->client->cl_timeout_default));
734 server->client->cl_timeout = &server->client->cl_timeout_default;
735
David Howells54ceac42006-08-22 20:06:13 -0400736 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
737 struct rpc_auth *auth;
738
739 auth = rpcauth_create(pseudoflavour, server->client);
740 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700741 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400742 return PTR_ERR(auth);
743 }
744 }
745 server->client->cl_softrtry = 0;
746 if (server->flags & NFS_MOUNT_SOFT)
747 server->client->cl_softrtry = 1;
748
David Howells54ceac42006-08-22 20:06:13 -0400749 return 0;
750}
751
752/*
753 * Initialise an NFS2 or NFS3 client
754 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400755static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500756 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400757 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400758{
David Howells54ceac42006-08-22 20:06:13 -0400759 int error;
760
761 if (clp->cl_cons_state == NFS_CS_READY) {
762 /* the client is already initialised */
763 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
764 return 0;
765 }
766
David Howells54ceac42006-08-22 20:06:13 -0400767 /*
768 * Create a client RPC handle for doing FSSTAT with UNIX auth only
769 * - RFC 2623, sec 2.3.2
770 */
Chuck Leverd7403512008-12-23 15:21:37 -0500771 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
772 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400773 if (error < 0)
774 goto error;
775 nfs_mark_client_ready(clp, NFS_CS_READY);
776 return 0;
777
778error:
779 nfs_mark_client_ready(clp, error);
780 dprintk("<-- nfs_init_client() = xerror %d\n", error);
781 return error;
782}
783
784/*
785 * Create a version 2 or 3 client
786 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400787static int nfs_init_server(struct nfs_server *server,
788 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400789{
Trond Myklebust3a498022007-12-14 14:56:04 -0500790 struct nfs_client_initdata cl_init = {
791 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500792 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500793 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500794 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500795 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500796 };
Trond Myklebust33170232007-12-20 16:03:59 -0500797 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400798 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500799 int error;
David Howells54ceac42006-08-22 20:06:13 -0400800
801 dprintk("--> nfs_init_server()\n");
802
803#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400804 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500805 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400806#endif
807
808 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500809 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400810 if (IS_ERR(clp)) {
811 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
812 return PTR_ERR(clp);
813 }
814
Trond Myklebust33170232007-12-20 16:03:59 -0500815 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
816 data->timeo, data->retrans);
817 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400818 if (error < 0)
819 goto error;
820
821 server->nfs_client = clp;
822
823 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400824 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100825 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400826 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
827 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
828 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400829
830 if (data->rsize)
831 server->rsize = nfs_block_size(data->rsize, NULL);
832 if (data->wsize)
833 server->wsize = nfs_block_size(data->wsize, NULL);
834
835 server->acregmin = data->acregmin * HZ;
836 server->acregmax = data->acregmax * HZ;
837 server->acdirmin = data->acdirmin * HZ;
838 server->acdirmax = data->acdirmax * HZ;
839
840 /* Start lockd here, before we might error out */
841 error = nfs_start_lockd(server);
842 if (error < 0)
843 goto error;
844
Chuck Leverf22d6d72008-03-14 14:10:22 -0400845 server->port = data->nfs_server.port;
846
Trond Myklebust33170232007-12-20 16:03:59 -0500847 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400848 if (error < 0)
849 goto error;
850
Chuck Lever3f8400d2008-03-14 14:10:30 -0400851 /* Preserve the values of mount_server-related mount options */
852 if (data->mount_server.addrlen) {
853 memcpy(&server->mountd_address, &data->mount_server.address,
854 data->mount_server.addrlen);
855 server->mountd_addrlen = data->mount_server.addrlen;
856 }
857 server->mountd_version = data->mount_server.version;
858 server->mountd_port = data->mount_server.port;
859 server->mountd_protocol = data->mount_server.protocol;
860
David Howells54ceac42006-08-22 20:06:13 -0400861 server->namelen = data->namlen;
862 /* Create a client RPC handle for the NFSv3 ACL management interface */
863 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400864 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
865 return 0;
866
867error:
868 server->nfs_client = NULL;
869 nfs_put_client(clp);
870 dprintk("<-- nfs_init_server() = xerror %d\n", error);
871 return error;
872}
873
874/*
875 * Load up the server record from information gained in an fsinfo record
876 */
877static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
878{
879 unsigned long max_rpc_payload;
880
881 /* Work out a lot of parameters */
882 if (server->rsize == 0)
883 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
884 if (server->wsize == 0)
885 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
886
887 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
888 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
889 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
890 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
891
892 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
893 if (server->rsize > max_rpc_payload)
894 server->rsize = max_rpc_payload;
895 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
896 server->rsize = NFS_MAX_FILE_IO_SIZE;
897 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700898
Jens Axboed9938312009-06-12 14:45:52 +0200899 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400900 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
901
902 if (server->wsize > max_rpc_payload)
903 server->wsize = max_rpc_payload;
904 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
905 server->wsize = NFS_MAX_FILE_IO_SIZE;
906 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400907 set_pnfs_layoutdriver(server, fsinfo->layouttype);
908
David Howells54ceac42006-08-22 20:06:13 -0400909 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
910
911 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400912 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
913 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400914 if (server->dtsize > server->rsize)
915 server->dtsize = server->rsize;
916
917 if (server->flags & NFS_MOUNT_NOAC) {
918 server->acregmin = server->acregmax = 0;
919 server->acdirmin = server->acdirmax = 0;
920 }
921
922 server->maxfilesize = fsinfo->maxfilesize;
923
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700924 server->time_delta = fsinfo->time_delta;
925
David Howells54ceac42006-08-22 20:06:13 -0400926 /* We're airborne Set socket buffersize */
927 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
928}
929
930/*
931 * Probe filesystem information, including the FSID on v2/v3
932 */
933static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
934{
935 struct nfs_fsinfo fsinfo;
936 struct nfs_client *clp = server->nfs_client;
937 int error;
938
939 dprintk("--> nfs_probe_fsinfo()\n");
940
941 if (clp->rpc_ops->set_capabilities != NULL) {
942 error = clp->rpc_ops->set_capabilities(server, mntfh);
943 if (error < 0)
944 goto out_error;
945 }
946
947 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400948 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400949 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
950 if (error < 0)
951 goto out_error;
952
953 nfs_server_set_fsinfo(server, &fsinfo);
954
955 /* Get some general file system info */
956 if (server->namelen == 0) {
957 struct nfs_pathconf pathinfo;
958
959 pathinfo.fattr = fattr;
960 nfs_fattr_init(fattr);
961
962 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
963 server->namelen = pathinfo.max_namelen;
964 }
965
966 dprintk("<-- nfs_probe_fsinfo() = 0\n");
967 return 0;
968
969out_error:
970 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
971 return error;
972}
973
974/*
975 * Copy useful information when duplicating a server record
976 */
977static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
978{
979 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400980 target->rsize = source->rsize;
981 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400982 target->acregmin = source->acregmin;
983 target->acregmax = source->acregmax;
984 target->acdirmin = source->acdirmin;
985 target->acdirmax = source->acdirmax;
986 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -0400987 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -0400988}
989
990/*
991 * Allocate and initialise a server record
992 */
993static struct nfs_server *nfs_alloc_server(void)
994{
995 struct nfs_server *server;
996
997 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
998 if (!server)
999 return NULL;
1000
1001 server->client = server->client_acl = ERR_PTR(-EINVAL);
1002
1003 /* Zero out the NFS state stuff */
1004 INIT_LIST_HEAD(&server->client_link);
1005 INIT_LIST_HEAD(&server->master_link);
1006
Steve Dicksonef818a22007-11-08 04:05:04 -05001007 atomic_set(&server->active, 0);
1008
David Howells54ceac42006-08-22 20:06:13 -04001009 server->io_stats = nfs_alloc_iostats();
1010 if (!server->io_stats) {
1011 kfree(server);
1012 return NULL;
1013 }
1014
Jens Axboe48d07642009-09-21 09:59:39 +02001015 if (bdi_init(&server->backing_dev_info)) {
1016 nfs_free_iostats(server->io_stats);
1017 kfree(server);
1018 return NULL;
1019 }
1020
David Howells54ceac42006-08-22 20:06:13 -04001021 return server;
1022}
1023
1024/*
1025 * Free up a server record
1026 */
1027void nfs_free_server(struct nfs_server *server)
1028{
1029 dprintk("--> nfs_free_server()\n");
1030
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001031 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001032 spin_lock(&nfs_client_lock);
1033 list_del(&server->client_link);
1034 list_del(&server->master_link);
1035 spin_unlock(&nfs_client_lock);
1036
1037 if (server->destroy != NULL)
1038 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001039
1040 if (!IS_ERR(server->client_acl))
1041 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001042 if (!IS_ERR(server->client))
1043 rpc_shutdown_client(server->client);
1044
1045 nfs_put_client(server->nfs_client);
1046
1047 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001048 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001049 kfree(server);
1050 nfs_release_automount_timer();
1051 dprintk("<-- nfs_free_server()\n");
1052}
1053
1054/*
1055 * Create a version 2 or 3 volume record
1056 * - keyed on server and FSID
1057 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001058struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001059 struct nfs_fh *mntfh)
1060{
1061 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001062 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001063 int error;
1064
1065 server = nfs_alloc_server();
1066 if (!server)
1067 return ERR_PTR(-ENOMEM);
1068
Trond Myklebustfbca7792010-04-16 16:22:46 -04001069 error = -ENOMEM;
1070 fattr = nfs_alloc_fattr();
1071 if (fattr == NULL)
1072 goto error;
1073
David Howells54ceac42006-08-22 20:06:13 -04001074 /* Get a client representation */
1075 error = nfs_init_server(server, data);
1076 if (error < 0)
1077 goto error;
1078
1079 BUG_ON(!server->nfs_client);
1080 BUG_ON(!server->nfs_client->rpc_ops);
1081 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1082
1083 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001084 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001085 if (error < 0)
1086 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001087 if (server->nfs_client->rpc_ops->version == 3) {
1088 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1089 server->namelen = NFS3_MAXNAMLEN;
1090 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1091 server->caps |= NFS_CAP_READDIRPLUS;
1092 } else {
1093 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1094 server->namelen = NFS2_MAXNAMLEN;
1095 }
1096
Trond Myklebustfbca7792010-04-16 16:22:46 -04001097 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1098 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001099 if (error < 0) {
1100 dprintk("nfs_create_server: getattr error = %d\n", -error);
1101 goto error;
1102 }
1103 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001104 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001105
David Howells6daabf12006-08-24 15:44:16 -04001106 dprintk("Server FSID: %llx:%llx\n",
1107 (unsigned long long) server->fsid.major,
1108 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001109
David Howells54ceac42006-08-22 20:06:13 -04001110 spin_lock(&nfs_client_lock);
1111 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1112 list_add_tail(&server->master_link, &nfs_volume_list);
1113 spin_unlock(&nfs_client_lock);
1114
1115 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001116 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001117 return server;
1118
1119error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001120 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001121 nfs_free_server(server);
1122 return ERR_PTR(error);
1123}
1124
1125#ifdef CONFIG_NFS_V4
1126/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001127 * Initialize the NFS4 callback service
1128 */
1129static int nfs4_init_callback(struct nfs_client *clp)
1130{
1131 int error;
1132
1133 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001134 if (nfs4_has_session(clp)) {
1135 error = xprt_setup_backchannel(
1136 clp->cl_rpcclient->cl_xprt,
1137 NFS41_BC_MIN_CALLBACKS);
1138 if (error < 0)
1139 return error;
1140 }
1141
Trond Myklebust97dc1352010-06-16 09:52:26 -04001142 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001143 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001144 if (error < 0) {
1145 dprintk("%s: failed to start callback. Error = %d\n",
1146 __func__, error);
1147 return error;
1148 }
1149 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1150 }
1151 return 0;
1152}
1153
1154/*
Andy Adamson557134a2009-04-01 09:21:53 -04001155 * Initialize the minor version specific parts of an NFS4 client record
1156 */
1157static int nfs4_init_client_minor_version(struct nfs_client *clp)
1158{
1159#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001160 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001161 struct nfs4_session *session = NULL;
1162 /*
1163 * Create the session and mark it expired.
1164 * When a SEQUENCE operation encounters the expired session
1165 * it will do session recovery to initialize it.
1166 */
1167 session = nfs4_alloc_session(clp);
1168 if (!session)
1169 return -ENOMEM;
1170
1171 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001172 /*
1173 * The create session reply races with the server back
1174 * channel probe. Mark the client NFS_CS_SESSION_INITING
1175 * so that the client back channel can find the
1176 * nfs_client struct
1177 */
1178 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001179 }
1180#endif /* CONFIG_NFS_V4_1 */
1181
Benny Halevy71468512009-04-01 09:22:56 -04001182 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001183}
1184
1185/*
David Howells54ceac42006-08-22 20:06:13 -04001186 * Initialise an NFS4 client record
1187 */
1188static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001189 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001190 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001191 rpc_authflavor_t authflavour,
1192 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001193{
1194 int error;
1195
1196 if (clp->cl_cons_state == NFS_CS_READY) {
1197 /* the client is initialised already */
1198 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1199 return 0;
1200 }
1201
1202 /* Check NFS protocol revision and initialize RPC op vector */
1203 clp->rpc_ops = &nfs_v4_clientops;
1204
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001205 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001206 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001207 if (error < 0)
1208 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001209 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001210
1211 error = nfs_idmap_new(clp);
1212 if (error < 0) {
1213 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001214 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001215 goto error;
1216 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001217 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001218
Andy Adamson557134a2009-04-01 09:21:53 -04001219 error = nfs4_init_client_minor_version(clp);
1220 if (error < 0)
1221 goto error;
1222
Andy Adamson76db6d92009-04-01 09:22:38 -04001223 if (!nfs4_has_session(clp))
1224 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001225 return 0;
1226
1227error:
1228 nfs_mark_client_ready(clp, error);
1229 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1230 return error;
1231}
1232
1233/*
1234 * Set up an NFS4 client
1235 */
1236static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001237 const char *hostname,
1238 const struct sockaddr *addr,
1239 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001240 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001241 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001242 int proto, const struct rpc_timeout *timeparms,
1243 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001244{
Trond Myklebust3a498022007-12-14 14:56:04 -05001245 struct nfs_client_initdata cl_init = {
1246 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001247 .addr = addr,
1248 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001249 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001250 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001251 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001252 };
David Howells54ceac42006-08-22 20:06:13 -04001253 struct nfs_client *clp;
1254 int error;
1255
1256 dprintk("--> nfs4_set_client()\n");
1257
1258 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001259 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001260 if (IS_ERR(clp)) {
1261 error = PTR_ERR(clp);
1262 goto error;
1263 }
Chuck Leverd7403512008-12-23 15:21:37 -05001264 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1265 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001266 if (error < 0)
1267 goto error_put;
1268
1269 server->nfs_client = clp;
1270 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1271 return 0;
1272
1273error_put:
1274 nfs_put_client(clp);
1275error:
1276 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1277 return error;
1278}
1279
Andy Adamson557134a2009-04-01 09:21:53 -04001280
1281/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001282 * Session has been established, and the client marked ready.
1283 * Set the mount rsize and wsize with negotiated fore channel
1284 * attributes which will be bound checked in nfs_server_set_fsinfo.
1285 */
1286static void nfs4_session_set_rwsize(struct nfs_server *server)
1287{
1288#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001289 struct nfs4_session *sess;
1290 u32 server_resp_sz;
1291 u32 server_rqst_sz;
1292
Andy Adamson96b09e02009-04-01 09:22:33 -04001293 if (!nfs4_has_session(server->nfs_client))
1294 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001295 sess = server->nfs_client->cl_session;
1296 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1297 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1298
1299 if (server->rsize > server_resp_sz)
1300 server->rsize = server_resp_sz;
1301 if (server->wsize > server_rqst_sz)
1302 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001303#endif /* CONFIG_NFS_V4_1 */
1304}
1305
Trond Myklebust44950b62010-06-17 11:45:12 -04001306static int nfs4_server_common_setup(struct nfs_server *server,
1307 struct nfs_fh *mntfh)
1308{
1309 struct nfs_fattr *fattr;
1310 int error;
1311
1312 BUG_ON(!server->nfs_client);
1313 BUG_ON(!server->nfs_client->rpc_ops);
1314 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1315
1316 fattr = nfs_alloc_fattr();
1317 if (fattr == NULL)
1318 return -ENOMEM;
1319
1320 /* We must ensure the session is initialised first */
1321 error = nfs4_init_session(server);
1322 if (error < 0)
1323 goto out;
1324
1325 /* Probe the root fh to retrieve its FSID and filehandle */
1326 error = nfs4_get_rootfh(server, mntfh);
1327 if (error < 0)
1328 goto out;
1329
1330 dprintk("Server FSID: %llx:%llx\n",
1331 (unsigned long long) server->fsid.major,
1332 (unsigned long long) server->fsid.minor);
1333 dprintk("Mount FH: %d\n", mntfh->size);
1334
1335 nfs4_session_set_rwsize(server);
1336
1337 error = nfs_probe_fsinfo(server, mntfh, fattr);
1338 if (error < 0)
1339 goto out;
1340
1341 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1342 server->namelen = NFS4_MAXNAMLEN;
1343
1344 spin_lock(&nfs_client_lock);
1345 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1346 list_add_tail(&server->master_link, &nfs_volume_list);
1347 spin_unlock(&nfs_client_lock);
1348
1349 server->mount_time = jiffies;
1350out:
1351 nfs_free_fattr(fattr);
1352 return error;
1353}
1354
Andy Adamson96b09e02009-04-01 09:22:33 -04001355/*
David Howells54ceac42006-08-22 20:06:13 -04001356 * Create a version 4 volume record
1357 */
1358static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001359 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001360{
Trond Myklebust33170232007-12-20 16:03:59 -05001361 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001362 int error;
1363
1364 dprintk("--> nfs4_init_server()\n");
1365
Trond Myklebust33170232007-12-20 16:03:59 -05001366 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1367 data->timeo, data->retrans);
1368
Chuck Lever542fcc32008-12-23 15:21:36 -05001369 /* Initialise the client representation from the mount data */
1370 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001371 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1372 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1373 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001374 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001375
Trond Myklebust33170232007-12-20 16:03:59 -05001376 /* Get a client record */
1377 error = nfs4_set_client(server,
1378 data->nfs_server.hostname,
1379 (const struct sockaddr *)&data->nfs_server.address,
1380 data->nfs_server.addrlen,
1381 data->client_address,
1382 data->auth_flavors[0],
1383 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001384 &timeparms,
1385 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001386 if (error < 0)
1387 goto error;
1388
David Howells54ceac42006-08-22 20:06:13 -04001389 if (data->rsize)
1390 server->rsize = nfs_block_size(data->rsize, NULL);
1391 if (data->wsize)
1392 server->wsize = nfs_block_size(data->wsize, NULL);
1393
1394 server->acregmin = data->acregmin * HZ;
1395 server->acregmax = data->acregmax * HZ;
1396 server->acdirmin = data->acdirmin * HZ;
1397 server->acdirmax = data->acdirmax * HZ;
1398
Chuck Leverf22d6d72008-03-14 14:10:22 -04001399 server->port = data->nfs_server.port;
1400
Trond Myklebust33170232007-12-20 16:03:59 -05001401 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001402
Trond Myklebust33170232007-12-20 16:03:59 -05001403error:
David Howells54ceac42006-08-22 20:06:13 -04001404 /* Done */
1405 dprintk("<-- nfs4_init_server() = %d\n", error);
1406 return error;
1407}
1408
1409/*
1410 * Create a version 4 volume record
1411 * - keyed on server and FSID
1412 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001413struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001414 struct nfs_fh *mntfh)
1415{
David Howells54ceac42006-08-22 20:06:13 -04001416 struct nfs_server *server;
1417 int error;
1418
1419 dprintk("--> nfs4_create_server()\n");
1420
1421 server = nfs_alloc_server();
1422 if (!server)
1423 return ERR_PTR(-ENOMEM);
1424
David Howells54ceac42006-08-22 20:06:13 -04001425 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001426 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001427 if (error < 0)
1428 goto error;
1429
Trond Myklebust44950b62010-06-17 11:45:12 -04001430 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001431 if (error < 0)
1432 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001433
David Howells54ceac42006-08-22 20:06:13 -04001434 dprintk("<-- nfs4_create_server() = %p\n", server);
1435 return server;
1436
1437error:
1438 nfs_free_server(server);
1439 dprintk("<-- nfs4_create_server() = error %d\n", error);
1440 return ERR_PTR(error);
1441}
1442
1443/*
1444 * Create an NFS4 referral server record
1445 */
1446struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001447 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001448{
1449 struct nfs_client *parent_client;
1450 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001451 int error;
1452
1453 dprintk("--> nfs4_create_referral_server()\n");
1454
1455 server = nfs_alloc_server();
1456 if (!server)
1457 return ERR_PTR(-ENOMEM);
1458
1459 parent_server = NFS_SB(data->sb);
1460 parent_client = parent_server->nfs_client;
1461
Chuck Lever542fcc32008-12-23 15:21:36 -05001462 /* Initialise the client representation from the parent server */
1463 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001464 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001465
David Howells54ceac42006-08-22 20:06:13 -04001466 /* Get a client representation.
1467 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001468 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001469 data->addr,
1470 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001471 parent_client->cl_ipaddr,
1472 data->authflavor,
1473 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001474 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001475 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001476 if (error < 0)
1477 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001478
Trond Myklebust33170232007-12-20 16:03:59 -05001479 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001480 if (error < 0)
1481 goto error;
1482
Trond Myklebust44950b62010-06-17 11:45:12 -04001483 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001484 if (error < 0)
1485 goto error;
1486
David Howells54ceac42006-08-22 20:06:13 -04001487 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1488 return server;
1489
1490error:
1491 nfs_free_server(server);
1492 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1493 return ERR_PTR(error);
1494}
1495
1496#endif /* CONFIG_NFS_V4 */
1497
1498/*
1499 * Clone an NFS2, NFS3 or NFS4 server record
1500 */
1501struct nfs_server *nfs_clone_server(struct nfs_server *source,
1502 struct nfs_fh *fh,
1503 struct nfs_fattr *fattr)
1504{
1505 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001506 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001507 int error;
1508
1509 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001510 (unsigned long long) fattr->fsid.major,
1511 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001512
1513 server = nfs_alloc_server();
1514 if (!server)
1515 return ERR_PTR(-ENOMEM);
1516
Trond Myklebustfbca7792010-04-16 16:22:46 -04001517 error = -ENOMEM;
1518 fattr_fsinfo = nfs_alloc_fattr();
1519 if (fattr_fsinfo == NULL)
1520 goto out_free_server;
1521
David Howells54ceac42006-08-22 20:06:13 -04001522 /* Copy data from the source */
1523 server->nfs_client = source->nfs_client;
1524 atomic_inc(&server->nfs_client->cl_count);
1525 nfs_server_copy_userdata(server, source);
1526
1527 server->fsid = fattr->fsid;
1528
Trond Myklebust33170232007-12-20 16:03:59 -05001529 error = nfs_init_server_rpcclient(server,
1530 source->client->cl_timeout,
1531 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001532 if (error < 0)
1533 goto out_free_server;
1534 if (!IS_ERR(source->client_acl))
1535 nfs_init_server_aclclient(server);
1536
1537 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001538 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001539 if (error < 0)
1540 goto out_free_server;
1541
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001542 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1543 server->namelen = NFS4_MAXNAMLEN;
1544
David Howells54ceac42006-08-22 20:06:13 -04001545 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001546 (unsigned long long) server->fsid.major,
1547 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001548
1549 error = nfs_start_lockd(server);
1550 if (error < 0)
1551 goto out_free_server;
1552
1553 spin_lock(&nfs_client_lock);
1554 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1555 list_add_tail(&server->master_link, &nfs_volume_list);
1556 spin_unlock(&nfs_client_lock);
1557
1558 server->mount_time = jiffies;
1559
Trond Myklebustfbca7792010-04-16 16:22:46 -04001560 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001561 dprintk("<-- nfs_clone_server() = %p\n", server);
1562 return server;
1563
1564out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001565 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001566 nfs_free_server(server);
1567 dprintk("<-- nfs_clone_server() = error %d\n", error);
1568 return ERR_PTR(error);
1569}
David Howells6aaca562006-08-22 20:06:13 -04001570
1571#ifdef CONFIG_PROC_FS
1572static struct proc_dir_entry *proc_fs_nfs;
1573
1574static int nfs_server_list_open(struct inode *inode, struct file *file);
1575static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1576static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1577static void nfs_server_list_stop(struct seq_file *p, void *v);
1578static int nfs_server_list_show(struct seq_file *m, void *v);
1579
James Morris88e9d342009-09-22 16:43:43 -07001580static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001581 .start = nfs_server_list_start,
1582 .next = nfs_server_list_next,
1583 .stop = nfs_server_list_stop,
1584 .show = nfs_server_list_show,
1585};
1586
Arjan van de Ven00977a52007-02-12 00:55:34 -08001587static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001588 .open = nfs_server_list_open,
1589 .read = seq_read,
1590 .llseek = seq_lseek,
1591 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001592 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001593};
1594
1595static int nfs_volume_list_open(struct inode *inode, struct file *file);
1596static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1597static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1598static void nfs_volume_list_stop(struct seq_file *p, void *v);
1599static int nfs_volume_list_show(struct seq_file *m, void *v);
1600
James Morris88e9d342009-09-22 16:43:43 -07001601static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001602 .start = nfs_volume_list_start,
1603 .next = nfs_volume_list_next,
1604 .stop = nfs_volume_list_stop,
1605 .show = nfs_volume_list_show,
1606};
1607
Arjan van de Ven00977a52007-02-12 00:55:34 -08001608static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001609 .open = nfs_volume_list_open,
1610 .read = seq_read,
1611 .llseek = seq_lseek,
1612 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001613 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001614};
1615
1616/*
1617 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1618 * we're dealing
1619 */
1620static int nfs_server_list_open(struct inode *inode, struct file *file)
1621{
1622 struct seq_file *m;
1623 int ret;
1624
1625 ret = seq_open(file, &nfs_server_list_ops);
1626 if (ret < 0)
1627 return ret;
1628
1629 m = file->private_data;
1630 m->private = PDE(inode)->data;
1631
1632 return 0;
1633}
1634
1635/*
1636 * set up the iterator to start reading from the server list and return the first item
1637 */
1638static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1639{
David Howells6aaca562006-08-22 20:06:13 -04001640 /* lock the list against modification */
1641 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001642 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001643}
1644
1645/*
1646 * move to next server
1647 */
1648static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1649{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001650 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001651}
1652
1653/*
1654 * clean up after reading from the transports list
1655 */
1656static void nfs_server_list_stop(struct seq_file *p, void *v)
1657{
1658 spin_unlock(&nfs_client_lock);
1659}
1660
1661/*
1662 * display a header line followed by a load of call lines
1663 */
1664static int nfs_server_list_show(struct seq_file *m, void *v)
1665{
1666 struct nfs_client *clp;
1667
1668 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001669 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001670 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1671 return 0;
1672 }
1673
1674 /* display one transport per line on subsequent lines */
1675 clp = list_entry(v, struct nfs_client, cl_share_link);
1676
Chuck Lever5d8515c2007-12-10 14:57:16 -05001677 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001678 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001679 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1680 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001681 atomic_read(&clp->cl_count),
1682 clp->cl_hostname);
1683
1684 return 0;
1685}
1686
1687/*
1688 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1689 */
1690static int nfs_volume_list_open(struct inode *inode, struct file *file)
1691{
1692 struct seq_file *m;
1693 int ret;
1694
1695 ret = seq_open(file, &nfs_volume_list_ops);
1696 if (ret < 0)
1697 return ret;
1698
1699 m = file->private_data;
1700 m->private = PDE(inode)->data;
1701
1702 return 0;
1703}
1704
1705/*
1706 * set up the iterator to start reading from the volume list and return the first item
1707 */
1708static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1709{
David Howells6aaca562006-08-22 20:06:13 -04001710 /* lock the list against modification */
1711 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001712 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001713}
1714
1715/*
1716 * move to next volume
1717 */
1718static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1719{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001720 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001721}
1722
1723/*
1724 * clean up after reading from the transports list
1725 */
1726static void nfs_volume_list_stop(struct seq_file *p, void *v)
1727{
1728 spin_unlock(&nfs_client_lock);
1729}
1730
1731/*
1732 * display a header line followed by a load of call lines
1733 */
1734static int nfs_volume_list_show(struct seq_file *m, void *v)
1735{
1736 struct nfs_server *server;
1737 struct nfs_client *clp;
1738 char dev[8], fsid[17];
1739
1740 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001741 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001742 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001743 return 0;
1744 }
1745 /* display one transport per line on subsequent lines */
1746 server = list_entry(v, struct nfs_server, master_link);
1747 clp = server->nfs_client;
1748
1749 snprintf(dev, 8, "%u:%u",
1750 MAJOR(server->s_dev), MINOR(server->s_dev));
1751
1752 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001753 (unsigned long long) server->fsid.major,
1754 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001755
David Howells5d1acff2009-04-03 16:42:47 +01001756 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001757 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001758 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1759 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001760 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001761 fsid,
1762 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001763
1764 return 0;
1765}
1766
1767/*
1768 * initialise the /proc/fs/nfsfs/ directory
1769 */
1770int __init nfs_fs_proc_init(void)
1771{
1772 struct proc_dir_entry *p;
1773
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001774 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001775 if (!proc_fs_nfs)
1776 goto error_0;
1777
David Howells6aaca562006-08-22 20:06:13 -04001778 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001779 p = proc_create("servers", S_IFREG|S_IRUGO,
1780 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001781 if (!p)
1782 goto error_1;
1783
David Howells6aaca562006-08-22 20:06:13 -04001784 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001785 p = proc_create("volumes", S_IFREG|S_IRUGO,
1786 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001787 if (!p)
1788 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001789 return 0;
1790
1791error_2:
1792 remove_proc_entry("servers", proc_fs_nfs);
1793error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001794 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001795error_0:
1796 return -ENOMEM;
1797}
1798
1799/*
1800 * clean up the /proc/fs/nfsfs/ directory
1801 */
1802void nfs_fs_proc_exit(void)
1803{
1804 remove_proc_entry("volumes", proc_fs_nfs);
1805 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001806 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001807}
1808
1809#endif /* CONFIG_PROC_FS */