blob: e55ad211dbed3ec4f0564823097f936913dbfc9a [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}
258
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500259#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400260/*
261 * Test if two ip6 socket addresses refer to the same socket by
262 * comparing relevant fields. The padding bytes specifically, are not
263 * compared. sin6_flowinfo is not compared because it only affects QoS
264 * and sin6_scope_id is only compared if the address is "link local"
265 * because "link local" addresses need only be unique to a specific
266 * link. Conversely, ordinary unicast addresses might have different
267 * sin6_scope_id.
268 *
269 * The caller should ensure both socket addresses are AF_INET6.
270 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400271static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
272 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400273{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400274 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
275 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400276
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400277 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
278 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400279 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500280
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400281 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500282}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400283#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
284static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
285 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400286{
287 return 0;
288}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500289#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500290
David Howells24c8dbb2006-08-22 20:06:10 -0400291/*
Ian Dalld7371c42009-03-10 20:33:22 -0400292 * Test if two ip4 socket addresses refer to the same socket, by
293 * comparing relevant fields. The padding bytes specifically, are
294 * not compared.
295 *
296 * The caller should ensure both socket addresses are AF_INET.
297 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400298static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
299 const struct sockaddr *sa2)
300{
301 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
302 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
303
304 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
305}
306
307static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
308 const struct sockaddr *sa2)
309{
310 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
311 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
312
313 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
314 (sin1->sin6_port == sin2->sin6_port);
315}
316
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400317static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400319{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400320 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
321 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400322
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400323 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
324 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400325}
326
327/*
Ian Dalld7371c42009-03-10 20:33:22 -0400328 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400329 * by comparing (only) relevant fields, excluding the port number.
330 */
331static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
332 const struct sockaddr *sa2)
333{
334 if (sa1->sa_family != sa2->sa_family)
335 return 0;
336
337 switch (sa1->sa_family) {
338 case AF_INET:
339 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
340 case AF_INET6:
341 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
342 }
343 return 0;
344}
345
346/*
347 * Test if two socket addresses represent the same actual socket,
348 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400349 */
350static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
351 const struct sockaddr *sa2)
352{
353 if (sa1->sa_family != sa2->sa_family)
354 return 0;
355
356 switch (sa1->sa_family) {
357 case AF_INET:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400358 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400359 case AF_INET6:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400360 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400361 }
362 return 0;
363}
364
365/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500366 * Find a client by IP address and protocol version
367 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400368 */
Chuck Leverff052642007-12-10 14:58:44 -0500369struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500370{
371 struct nfs_client *clp;
372
373 spin_lock(&nfs_client_lock);
374 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500375 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
376
Trond Myklebustc81468a2007-12-14 14:56:05 -0500377 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400378 if (!(clp->cl_cons_state == NFS_CS_READY ||
379 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500380 continue;
381
382 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500383 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500384 continue;
385
386 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500387 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500388 continue;
389
390 atomic_inc(&clp->cl_count);
391 spin_unlock(&nfs_client_lock);
392 return clp;
393 }
394 spin_unlock(&nfs_client_lock);
395 return NULL;
396}
397
398/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500399 * Find a client by IP address and protocol version
400 * - returns NULL if no such client
401 */
402struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
403{
404 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
405 u32 nfsvers = clp->rpc_ops->version;
406
407 spin_lock(&nfs_client_lock);
408 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
409 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
410
411 /* Don't match clients that failed to initialise properly */
412 if (clp->cl_cons_state != NFS_CS_READY)
413 continue;
414
415 /* Different NFS versions cannot share the same nfs_client */
416 if (clp->rpc_ops->version != nfsvers)
417 continue;
418
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500419 /* Match only the IP address, not the port number */
420 if (!nfs_sockaddr_match_ipaddr(sap, clap))
421 continue;
422
423 atomic_inc(&clp->cl_count);
424 spin_unlock(&nfs_client_lock);
425 return clp;
426 }
427 spin_unlock(&nfs_client_lock);
428 return NULL;
429}
430
431/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500432 * Find an nfs_client on the list that matches the initialisation data
433 * that is supplied.
434 */
435static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400436{
437 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400438 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400439
440 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400441 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700442 /* Don't match clients that failed to initialise properly */
443 if (clp->cl_cons_state < 0)
444 continue;
445
David Howells24c8dbb2006-08-22 20:06:10 -0400446 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500447 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400448 continue;
449
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500450 if (clp->cl_proto != data->proto)
451 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400452 /* Match nfsv4 minorversion */
453 if (clp->cl_minorversion != data->minorversion)
454 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500455 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400456 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400457 continue;
458
Trond Myklebustc81468a2007-12-14 14:56:05 -0500459 atomic_inc(&clp->cl_count);
460 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400461 }
David Howells24c8dbb2006-08-22 20:06:10 -0400462 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400463}
464
465/*
466 * Look up a client by IP address and protocol version
467 * - creates a new record if one doesn't yet exist
468 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500469static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400470{
471 struct nfs_client *clp, *new = NULL;
472 int error;
473
Chuck Leverd7422c42007-12-10 14:58:51 -0500474 dprintk("--> nfs_get_client(%s,v%u)\n",
475 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400476
477 /* see if the client already exists */
478 do {
479 spin_lock(&nfs_client_lock);
480
Trond Myklebustc81468a2007-12-14 14:56:05 -0500481 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400482 if (clp)
483 goto found_client;
484 if (new)
485 goto install_client;
486
487 spin_unlock(&nfs_client_lock);
488
Trond Myklebust3a498022007-12-14 14:56:04 -0500489 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700490 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400491
Chuck Levera21bdd92009-06-17 18:02:10 -0700492 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
493 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400494
495 /* install a new client and return with it unready */
496install_client:
497 clp = new;
498 list_add(&clp->cl_share_link, &nfs_client_list);
499 spin_unlock(&nfs_client_lock);
500 dprintk("--> nfs_get_client() = %p [new]\n", clp);
501 return clp;
502
503 /* found an existing client
504 * - make sure it's ready before returning
505 */
506found_client:
507 spin_unlock(&nfs_client_lock);
508
509 if (new)
510 nfs_free_client(new);
511
Matthew Wilcox150030b2007-12-06 16:24:39 -0500512 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400513 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400514 if (error < 0) {
515 nfs_put_client(clp);
516 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400517 }
518
519 if (clp->cl_cons_state < NFS_CS_READY) {
520 error = clp->cl_cons_state;
521 nfs_put_client(clp);
522 return ERR_PTR(error);
523 }
524
David Howells54ceac42006-08-22 20:06:13 -0400525 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
526
David Howells24c8dbb2006-08-22 20:06:10 -0400527 dprintk("--> nfs_get_client() = %p [share]\n", clp);
528 return clp;
529}
530
531/*
532 * Mark a server as ready or failed
533 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400534void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400535{
536 clp->cl_cons_state = state;
537 wake_up_all(&nfs_client_active_wq);
538}
David Howells5006a762006-08-22 20:06:12 -0400539
540/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400541 * With sessions, the client is not marked ready until after a
542 * successful EXCHANGE_ID and CREATE_SESSION.
543 *
544 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
545 * other versions of NFS can be tried.
546 */
547int nfs4_check_client_ready(struct nfs_client *clp)
548{
549 if (!nfs4_has_session(clp))
550 return 0;
551 if (clp->cl_cons_state < NFS_CS_READY)
552 return -EPROTONOSUPPORT;
553 return 0;
554}
555
556/*
David Howells5006a762006-08-22 20:06:12 -0400557 * Initialise the timeout values for a connection
558 */
559static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
560 unsigned int timeo, unsigned int retrans)
561{
562 to->to_initval = timeo * HZ / 10;
563 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400564
565 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400566 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400567 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400568 if (to->to_retries == 0)
569 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500570 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400571 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400572 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
573 to->to_initval = NFS_MAX_TCP_TIMEOUT;
574 to->to_increment = to->to_initval;
575 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500576 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
577 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
578 if (to->to_maxval < to->to_initval)
579 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400580 to->to_exponential = 0;
581 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400582 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400583 if (to->to_retries == 0)
584 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400585 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400586 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400587 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
588 to->to_initval = NFS_MAX_UDP_TIMEOUT;
589 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
590 to->to_exponential = 1;
591 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400592 default:
593 BUG();
David Howells5006a762006-08-22 20:06:12 -0400594 }
595}
596
597/*
598 * Create an RPC client handle
599 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500600static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500601 const struct rpc_timeout *timeparms,
602 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500603 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400604{
David Howells5006a762006-08-22 20:06:12 -0400605 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400606 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500607 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400608 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500609 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500610 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400611 .servername = clp->cl_hostname,
612 .program = &nfs_program,
613 .version = clp->rpc_ops->version,
614 .authflavor = flavor,
615 };
David Howells5006a762006-08-22 20:06:12 -0400616
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500617 if (discrtry)
618 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
619 if (noresvport)
620 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
621
David Howells5006a762006-08-22 20:06:12 -0400622 if (!IS_ERR(clp->cl_rpcclient))
623 return 0;
624
Chuck Lever41877d22006-08-22 20:06:20 -0400625 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400626 if (IS_ERR(clnt)) {
627 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700628 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400629 return PTR_ERR(clnt);
630 }
631
David Howells5006a762006-08-22 20:06:12 -0400632 clp->cl_rpcclient = clnt;
633 return 0;
634}
David Howells54ceac42006-08-22 20:06:13 -0400635
636/*
637 * Version 2 or 3 client destruction
638 */
639static void nfs_destroy_server(struct nfs_server *server)
640{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400641 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
642 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500643 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400644}
645
646/*
647 * Version 2 or 3 lockd setup
648 */
649static int nfs_start_lockd(struct nfs_server *server)
650{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500651 struct nlm_host *host;
652 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500653 struct nlmclnt_initdata nlm_init = {
654 .hostname = clp->cl_hostname,
655 .address = (struct sockaddr *)&clp->cl_addr,
656 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500657 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500658 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
659 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500660 };
David Howells54ceac42006-08-22 20:06:13 -0400661
Chuck Lever883bb162008-01-15 16:04:20 -0500662 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500663 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400664 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
665 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500666 return 0;
667
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400668 switch (clp->cl_proto) {
669 default:
670 nlm_init.protocol = IPPROTO_TCP;
671 break;
672 case XPRT_TRANSPORT_UDP:
673 nlm_init.protocol = IPPROTO_UDP;
674 }
675
Chuck Lever883bb162008-01-15 16:04:20 -0500676 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500677 if (IS_ERR(host))
678 return PTR_ERR(host);
679
680 server->nlm_host = host;
681 server->destroy = nfs_destroy_server;
682 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400683}
684
685/*
686 * Initialise an NFSv3 ACL client connection
687 */
688#ifdef CONFIG_NFS_V3_ACL
689static void nfs_init_server_aclclient(struct nfs_server *server)
690{
Trond Myklebust40c553192007-12-14 14:56:07 -0500691 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400692 goto out_noacl;
693 if (server->flags & NFS_MOUNT_NOACL)
694 goto out_noacl;
695
696 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
697 if (IS_ERR(server->client_acl))
698 goto out_noacl;
699
700 /* No errors! Assume that Sun nfsacls are supported */
701 server->caps |= NFS_CAP_ACLS;
702 return;
703
704out_noacl:
705 server->caps &= ~NFS_CAP_ACLS;
706}
707#else
708static inline void nfs_init_server_aclclient(struct nfs_server *server)
709{
710 server->flags &= ~NFS_MOUNT_NOACL;
711 server->caps &= ~NFS_CAP_ACLS;
712}
713#endif
714
715/*
716 * Create a general RPC client
717 */
Trond Myklebust33170232007-12-20 16:03:59 -0500718static int nfs_init_server_rpcclient(struct nfs_server *server,
719 const struct rpc_timeout *timeo,
720 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400721{
722 struct nfs_client *clp = server->nfs_client;
723
724 server->client = rpc_clone_client(clp->cl_rpcclient);
725 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700726 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400727 return PTR_ERR(server->client);
728 }
729
Trond Myklebust33170232007-12-20 16:03:59 -0500730 memcpy(&server->client->cl_timeout_default,
731 timeo,
732 sizeof(server->client->cl_timeout_default));
733 server->client->cl_timeout = &server->client->cl_timeout_default;
734
David Howells54ceac42006-08-22 20:06:13 -0400735 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
736 struct rpc_auth *auth;
737
738 auth = rpcauth_create(pseudoflavour, server->client);
739 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700740 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400741 return PTR_ERR(auth);
742 }
743 }
744 server->client->cl_softrtry = 0;
745 if (server->flags & NFS_MOUNT_SOFT)
746 server->client->cl_softrtry = 1;
747
David Howells54ceac42006-08-22 20:06:13 -0400748 return 0;
749}
750
751/*
752 * Initialise an NFS2 or NFS3 client
753 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400754static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500755 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400756 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400757{
David Howells54ceac42006-08-22 20:06:13 -0400758 int error;
759
760 if (clp->cl_cons_state == NFS_CS_READY) {
761 /* the client is already initialised */
762 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
763 return 0;
764 }
765
David Howells54ceac42006-08-22 20:06:13 -0400766 /*
767 * Create a client RPC handle for doing FSSTAT with UNIX auth only
768 * - RFC 2623, sec 2.3.2
769 */
Chuck Leverd7403512008-12-23 15:21:37 -0500770 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
771 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400772 if (error < 0)
773 goto error;
774 nfs_mark_client_ready(clp, NFS_CS_READY);
775 return 0;
776
777error:
778 nfs_mark_client_ready(clp, error);
779 dprintk("<-- nfs_init_client() = xerror %d\n", error);
780 return error;
781}
782
783/*
784 * Create a version 2 or 3 client
785 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400786static int nfs_init_server(struct nfs_server *server,
787 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400788{
Trond Myklebust3a498022007-12-14 14:56:04 -0500789 struct nfs_client_initdata cl_init = {
790 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500791 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500792 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500793 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500794 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500795 };
Trond Myklebust33170232007-12-20 16:03:59 -0500796 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400797 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500798 int error;
David Howells54ceac42006-08-22 20:06:13 -0400799
800 dprintk("--> nfs_init_server()\n");
801
802#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400803 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500804 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400805#endif
806
807 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500808 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400809 if (IS_ERR(clp)) {
810 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
811 return PTR_ERR(clp);
812 }
813
Trond Myklebust33170232007-12-20 16:03:59 -0500814 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
815 data->timeo, data->retrans);
816 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400817 if (error < 0)
818 goto error;
819
820 server->nfs_client = clp;
821
822 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400823 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100824 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400825 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
826 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
827 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400828
829 if (data->rsize)
830 server->rsize = nfs_block_size(data->rsize, NULL);
831 if (data->wsize)
832 server->wsize = nfs_block_size(data->wsize, NULL);
833
834 server->acregmin = data->acregmin * HZ;
835 server->acregmax = data->acregmax * HZ;
836 server->acdirmin = data->acdirmin * HZ;
837 server->acdirmax = data->acdirmax * HZ;
838
839 /* Start lockd here, before we might error out */
840 error = nfs_start_lockd(server);
841 if (error < 0)
842 goto error;
843
Chuck Leverf22d6d72008-03-14 14:10:22 -0400844 server->port = data->nfs_server.port;
845
Trond Myklebust33170232007-12-20 16:03:59 -0500846 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400847 if (error < 0)
848 goto error;
849
Chuck Lever3f8400d2008-03-14 14:10:30 -0400850 /* Preserve the values of mount_server-related mount options */
851 if (data->mount_server.addrlen) {
852 memcpy(&server->mountd_address, &data->mount_server.address,
853 data->mount_server.addrlen);
854 server->mountd_addrlen = data->mount_server.addrlen;
855 }
856 server->mountd_version = data->mount_server.version;
857 server->mountd_port = data->mount_server.port;
858 server->mountd_protocol = data->mount_server.protocol;
859
David Howells54ceac42006-08-22 20:06:13 -0400860 server->namelen = data->namlen;
861 /* Create a client RPC handle for the NFSv3 ACL management interface */
862 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400863 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
864 return 0;
865
866error:
867 server->nfs_client = NULL;
868 nfs_put_client(clp);
869 dprintk("<-- nfs_init_server() = xerror %d\n", error);
870 return error;
871}
872
873/*
874 * Load up the server record from information gained in an fsinfo record
875 */
876static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
877{
878 unsigned long max_rpc_payload;
879
880 /* Work out a lot of parameters */
881 if (server->rsize == 0)
882 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
883 if (server->wsize == 0)
884 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
885
886 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
887 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
888 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
889 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
890
891 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
892 if (server->rsize > max_rpc_payload)
893 server->rsize = max_rpc_payload;
894 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
895 server->rsize = NFS_MAX_FILE_IO_SIZE;
896 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700897
Jens Axboed9938312009-06-12 14:45:52 +0200898 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400899 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
900
901 if (server->wsize > max_rpc_payload)
902 server->wsize = max_rpc_payload;
903 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
904 server->wsize = NFS_MAX_FILE_IO_SIZE;
905 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400906 set_pnfs_layoutdriver(server, fsinfo->layouttype);
907
David Howells54ceac42006-08-22 20:06:13 -0400908 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
909
910 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400911 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
912 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400913 if (server->dtsize > server->rsize)
914 server->dtsize = server->rsize;
915
916 if (server->flags & NFS_MOUNT_NOAC) {
917 server->acregmin = server->acregmax = 0;
918 server->acdirmin = server->acdirmax = 0;
919 }
920
921 server->maxfilesize = fsinfo->maxfilesize;
922
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700923 server->time_delta = fsinfo->time_delta;
924
David Howells54ceac42006-08-22 20:06:13 -0400925 /* We're airborne Set socket buffersize */
926 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
927}
928
929/*
930 * Probe filesystem information, including the FSID on v2/v3
931 */
932static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
933{
934 struct nfs_fsinfo fsinfo;
935 struct nfs_client *clp = server->nfs_client;
936 int error;
937
938 dprintk("--> nfs_probe_fsinfo()\n");
939
940 if (clp->rpc_ops->set_capabilities != NULL) {
941 error = clp->rpc_ops->set_capabilities(server, mntfh);
942 if (error < 0)
943 goto out_error;
944 }
945
946 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400947 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400948 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
949 if (error < 0)
950 goto out_error;
951
952 nfs_server_set_fsinfo(server, &fsinfo);
953
954 /* Get some general file system info */
955 if (server->namelen == 0) {
956 struct nfs_pathconf pathinfo;
957
958 pathinfo.fattr = fattr;
959 nfs_fattr_init(fattr);
960
961 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
962 server->namelen = pathinfo.max_namelen;
963 }
964
965 dprintk("<-- nfs_probe_fsinfo() = 0\n");
966 return 0;
967
968out_error:
969 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
970 return error;
971}
972
973/*
974 * Copy useful information when duplicating a server record
975 */
976static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
977{
978 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400979 target->rsize = source->rsize;
980 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400981 target->acregmin = source->acregmin;
982 target->acregmax = source->acregmax;
983 target->acdirmin = source->acdirmin;
984 target->acdirmax = source->acdirmax;
985 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -0400986 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -0400987}
988
989/*
990 * Allocate and initialise a server record
991 */
992static struct nfs_server *nfs_alloc_server(void)
993{
994 struct nfs_server *server;
995
996 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
997 if (!server)
998 return NULL;
999
1000 server->client = server->client_acl = ERR_PTR(-EINVAL);
1001
1002 /* Zero out the NFS state stuff */
1003 INIT_LIST_HEAD(&server->client_link);
1004 INIT_LIST_HEAD(&server->master_link);
1005
Steve Dicksonef818a22007-11-08 04:05:04 -05001006 atomic_set(&server->active, 0);
1007
David Howells54ceac42006-08-22 20:06:13 -04001008 server->io_stats = nfs_alloc_iostats();
1009 if (!server->io_stats) {
1010 kfree(server);
1011 return NULL;
1012 }
1013
Jens Axboe48d07642009-09-21 09:59:39 +02001014 if (bdi_init(&server->backing_dev_info)) {
1015 nfs_free_iostats(server->io_stats);
1016 kfree(server);
1017 return NULL;
1018 }
1019
David Howells54ceac42006-08-22 20:06:13 -04001020 return server;
1021}
1022
1023/*
1024 * Free up a server record
1025 */
1026void nfs_free_server(struct nfs_server *server)
1027{
1028 dprintk("--> nfs_free_server()\n");
1029
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001030 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001031 spin_lock(&nfs_client_lock);
1032 list_del(&server->client_link);
1033 list_del(&server->master_link);
1034 spin_unlock(&nfs_client_lock);
1035
1036 if (server->destroy != NULL)
1037 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001038
1039 if (!IS_ERR(server->client_acl))
1040 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001041 if (!IS_ERR(server->client))
1042 rpc_shutdown_client(server->client);
1043
1044 nfs_put_client(server->nfs_client);
1045
1046 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001047 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001048 kfree(server);
1049 nfs_release_automount_timer();
1050 dprintk("<-- nfs_free_server()\n");
1051}
1052
1053/*
1054 * Create a version 2 or 3 volume record
1055 * - keyed on server and FSID
1056 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001057struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001058 struct nfs_fh *mntfh)
1059{
1060 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001061 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001062 int error;
1063
1064 server = nfs_alloc_server();
1065 if (!server)
1066 return ERR_PTR(-ENOMEM);
1067
Trond Myklebustfbca7792010-04-16 16:22:46 -04001068 error = -ENOMEM;
1069 fattr = nfs_alloc_fattr();
1070 if (fattr == NULL)
1071 goto error;
1072
David Howells54ceac42006-08-22 20:06:13 -04001073 /* Get a client representation */
1074 error = nfs_init_server(server, data);
1075 if (error < 0)
1076 goto error;
1077
1078 BUG_ON(!server->nfs_client);
1079 BUG_ON(!server->nfs_client->rpc_ops);
1080 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1081
1082 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001083 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001084 if (error < 0)
1085 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001086 if (server->nfs_client->rpc_ops->version == 3) {
1087 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1088 server->namelen = NFS3_MAXNAMLEN;
1089 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1090 server->caps |= NFS_CAP_READDIRPLUS;
1091 } else {
1092 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1093 server->namelen = NFS2_MAXNAMLEN;
1094 }
1095
Trond Myklebustfbca7792010-04-16 16:22:46 -04001096 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1097 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001098 if (error < 0) {
1099 dprintk("nfs_create_server: getattr error = %d\n", -error);
1100 goto error;
1101 }
1102 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001103 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001104
David Howells6daabf12006-08-24 15:44:16 -04001105 dprintk("Server FSID: %llx:%llx\n",
1106 (unsigned long long) server->fsid.major,
1107 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001108
David Howells54ceac42006-08-22 20:06:13 -04001109 spin_lock(&nfs_client_lock);
1110 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1111 list_add_tail(&server->master_link, &nfs_volume_list);
1112 spin_unlock(&nfs_client_lock);
1113
1114 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001115 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001116 return server;
1117
1118error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001119 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001120 nfs_free_server(server);
1121 return ERR_PTR(error);
1122}
1123
1124#ifdef CONFIG_NFS_V4
1125/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001126 * Initialize the NFS4 callback service
1127 */
1128static int nfs4_init_callback(struct nfs_client *clp)
1129{
1130 int error;
1131
1132 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001133 if (nfs4_has_session(clp)) {
1134 error = xprt_setup_backchannel(
1135 clp->cl_rpcclient->cl_xprt,
1136 NFS41_BC_MIN_CALLBACKS);
1137 if (error < 0)
1138 return error;
1139 }
1140
Trond Myklebust97dc1352010-06-16 09:52:26 -04001141 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001142 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001143 if (error < 0) {
1144 dprintk("%s: failed to start callback. Error = %d\n",
1145 __func__, error);
1146 return error;
1147 }
1148 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1149 }
1150 return 0;
1151}
1152
1153/*
Andy Adamson557134a2009-04-01 09:21:53 -04001154 * Initialize the minor version specific parts of an NFS4 client record
1155 */
1156static int nfs4_init_client_minor_version(struct nfs_client *clp)
1157{
1158#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001159 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001160 struct nfs4_session *session = NULL;
1161 /*
1162 * Create the session and mark it expired.
1163 * When a SEQUENCE operation encounters the expired session
1164 * it will do session recovery to initialize it.
1165 */
1166 session = nfs4_alloc_session(clp);
1167 if (!session)
1168 return -ENOMEM;
1169
1170 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001171 /*
1172 * The create session reply races with the server back
1173 * channel probe. Mark the client NFS_CS_SESSION_INITING
1174 * so that the client back channel can find the
1175 * nfs_client struct
1176 */
1177 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001178 }
1179#endif /* CONFIG_NFS_V4_1 */
1180
Benny Halevy71468512009-04-01 09:22:56 -04001181 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001182}
1183
1184/*
David Howells54ceac42006-08-22 20:06:13 -04001185 * Initialise an NFS4 client record
1186 */
1187static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001188 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001189 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001190 rpc_authflavor_t authflavour,
1191 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001192{
1193 int error;
1194
1195 if (clp->cl_cons_state == NFS_CS_READY) {
1196 /* the client is initialised already */
1197 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1198 return 0;
1199 }
1200
1201 /* Check NFS protocol revision and initialize RPC op vector */
1202 clp->rpc_ops = &nfs_v4_clientops;
1203
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001204 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001205 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001206 if (error < 0)
1207 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001208 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001209
1210 error = nfs_idmap_new(clp);
1211 if (error < 0) {
1212 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001213 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001214 goto error;
1215 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001216 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001217
Andy Adamson557134a2009-04-01 09:21:53 -04001218 error = nfs4_init_client_minor_version(clp);
1219 if (error < 0)
1220 goto error;
1221
Andy Adamson76db6d92009-04-01 09:22:38 -04001222 if (!nfs4_has_session(clp))
1223 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001224 return 0;
1225
1226error:
1227 nfs_mark_client_ready(clp, error);
1228 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1229 return error;
1230}
1231
1232/*
1233 * Set up an NFS4 client
1234 */
1235static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001236 const char *hostname,
1237 const struct sockaddr *addr,
1238 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001239 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001240 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001241 int proto, const struct rpc_timeout *timeparms,
1242 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001243{
Trond Myklebust3a498022007-12-14 14:56:04 -05001244 struct nfs_client_initdata cl_init = {
1245 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001246 .addr = addr,
1247 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001248 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001249 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001250 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001251 };
David Howells54ceac42006-08-22 20:06:13 -04001252 struct nfs_client *clp;
1253 int error;
1254
1255 dprintk("--> nfs4_set_client()\n");
1256
1257 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001258 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001259 if (IS_ERR(clp)) {
1260 error = PTR_ERR(clp);
1261 goto error;
1262 }
Chuck Leverd7403512008-12-23 15:21:37 -05001263 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1264 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001265 if (error < 0)
1266 goto error_put;
1267
1268 server->nfs_client = clp;
1269 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1270 return 0;
1271
1272error_put:
1273 nfs_put_client(clp);
1274error:
1275 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1276 return error;
1277}
1278
Andy Adamson557134a2009-04-01 09:21:53 -04001279
1280/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001281 * Session has been established, and the client marked ready.
1282 * Set the mount rsize and wsize with negotiated fore channel
1283 * attributes which will be bound checked in nfs_server_set_fsinfo.
1284 */
1285static void nfs4_session_set_rwsize(struct nfs_server *server)
1286{
1287#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001288 struct nfs4_session *sess;
1289 u32 server_resp_sz;
1290 u32 server_rqst_sz;
1291
Andy Adamson96b09e02009-04-01 09:22:33 -04001292 if (!nfs4_has_session(server->nfs_client))
1293 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001294 sess = server->nfs_client->cl_session;
1295 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1296 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1297
1298 if (server->rsize > server_resp_sz)
1299 server->rsize = server_resp_sz;
1300 if (server->wsize > server_rqst_sz)
1301 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001302#endif /* CONFIG_NFS_V4_1 */
1303}
1304
Trond Myklebust44950b62010-06-17 11:45:12 -04001305static int nfs4_server_common_setup(struct nfs_server *server,
1306 struct nfs_fh *mntfh)
1307{
1308 struct nfs_fattr *fattr;
1309 int error;
1310
1311 BUG_ON(!server->nfs_client);
1312 BUG_ON(!server->nfs_client->rpc_ops);
1313 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1314
1315 fattr = nfs_alloc_fattr();
1316 if (fattr == NULL)
1317 return -ENOMEM;
1318
1319 /* We must ensure the session is initialised first */
1320 error = nfs4_init_session(server);
1321 if (error < 0)
1322 goto out;
1323
1324 /* Probe the root fh to retrieve its FSID and filehandle */
1325 error = nfs4_get_rootfh(server, mntfh);
1326 if (error < 0)
1327 goto out;
1328
1329 dprintk("Server FSID: %llx:%llx\n",
1330 (unsigned long long) server->fsid.major,
1331 (unsigned long long) server->fsid.minor);
1332 dprintk("Mount FH: %d\n", mntfh->size);
1333
1334 nfs4_session_set_rwsize(server);
1335
1336 error = nfs_probe_fsinfo(server, mntfh, fattr);
1337 if (error < 0)
1338 goto out;
1339
1340 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1341 server->namelen = NFS4_MAXNAMLEN;
1342
1343 spin_lock(&nfs_client_lock);
1344 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1345 list_add_tail(&server->master_link, &nfs_volume_list);
1346 spin_unlock(&nfs_client_lock);
1347
1348 server->mount_time = jiffies;
1349out:
1350 nfs_free_fattr(fattr);
1351 return error;
1352}
1353
Andy Adamson96b09e02009-04-01 09:22:33 -04001354/*
David Howells54ceac42006-08-22 20:06:13 -04001355 * Create a version 4 volume record
1356 */
1357static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001358 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001359{
Trond Myklebust33170232007-12-20 16:03:59 -05001360 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001361 int error;
1362
1363 dprintk("--> nfs4_init_server()\n");
1364
Trond Myklebust33170232007-12-20 16:03:59 -05001365 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1366 data->timeo, data->retrans);
1367
Chuck Lever542fcc32008-12-23 15:21:36 -05001368 /* Initialise the client representation from the mount data */
1369 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001370 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1371 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1372 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001373 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001374
Trond Myklebust33170232007-12-20 16:03:59 -05001375 /* Get a client record */
1376 error = nfs4_set_client(server,
1377 data->nfs_server.hostname,
1378 (const struct sockaddr *)&data->nfs_server.address,
1379 data->nfs_server.addrlen,
1380 data->client_address,
1381 data->auth_flavors[0],
1382 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001383 &timeparms,
1384 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001385 if (error < 0)
1386 goto error;
1387
David Howells54ceac42006-08-22 20:06:13 -04001388 if (data->rsize)
1389 server->rsize = nfs_block_size(data->rsize, NULL);
1390 if (data->wsize)
1391 server->wsize = nfs_block_size(data->wsize, NULL);
1392
1393 server->acregmin = data->acregmin * HZ;
1394 server->acregmax = data->acregmax * HZ;
1395 server->acdirmin = data->acdirmin * HZ;
1396 server->acdirmax = data->acdirmax * HZ;
1397
Chuck Leverf22d6d72008-03-14 14:10:22 -04001398 server->port = data->nfs_server.port;
1399
Trond Myklebust33170232007-12-20 16:03:59 -05001400 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001401
Trond Myklebust33170232007-12-20 16:03:59 -05001402error:
David Howells54ceac42006-08-22 20:06:13 -04001403 /* Done */
1404 dprintk("<-- nfs4_init_server() = %d\n", error);
1405 return error;
1406}
1407
1408/*
1409 * Create a version 4 volume record
1410 * - keyed on server and FSID
1411 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001412struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001413 struct nfs_fh *mntfh)
1414{
David Howells54ceac42006-08-22 20:06:13 -04001415 struct nfs_server *server;
1416 int error;
1417
1418 dprintk("--> nfs4_create_server()\n");
1419
1420 server = nfs_alloc_server();
1421 if (!server)
1422 return ERR_PTR(-ENOMEM);
1423
David Howells54ceac42006-08-22 20:06:13 -04001424 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001425 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001426 if (error < 0)
1427 goto error;
1428
Trond Myklebust44950b62010-06-17 11:45:12 -04001429 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001430 if (error < 0)
1431 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001432
David Howells54ceac42006-08-22 20:06:13 -04001433 dprintk("<-- nfs4_create_server() = %p\n", server);
1434 return server;
1435
1436error:
1437 nfs_free_server(server);
1438 dprintk("<-- nfs4_create_server() = error %d\n", error);
1439 return ERR_PTR(error);
1440}
1441
1442/*
1443 * Create an NFS4 referral server record
1444 */
1445struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001446 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001447{
1448 struct nfs_client *parent_client;
1449 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001450 int error;
1451
1452 dprintk("--> nfs4_create_referral_server()\n");
1453
1454 server = nfs_alloc_server();
1455 if (!server)
1456 return ERR_PTR(-ENOMEM);
1457
1458 parent_server = NFS_SB(data->sb);
1459 parent_client = parent_server->nfs_client;
1460
Chuck Lever542fcc32008-12-23 15:21:36 -05001461 /* Initialise the client representation from the parent server */
1462 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001463 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001464
David Howells54ceac42006-08-22 20:06:13 -04001465 /* Get a client representation.
1466 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001467 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001468 data->addr,
1469 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001470 parent_client->cl_ipaddr,
1471 data->authflavor,
1472 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001473 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001474 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001475 if (error < 0)
1476 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001477
Trond Myklebust33170232007-12-20 16:03:59 -05001478 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001479 if (error < 0)
1480 goto error;
1481
Trond Myklebust44950b62010-06-17 11:45:12 -04001482 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001483 if (error < 0)
1484 goto error;
1485
David Howells54ceac42006-08-22 20:06:13 -04001486 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1487 return server;
1488
1489error:
1490 nfs_free_server(server);
1491 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1492 return ERR_PTR(error);
1493}
1494
1495#endif /* CONFIG_NFS_V4 */
1496
1497/*
1498 * Clone an NFS2, NFS3 or NFS4 server record
1499 */
1500struct nfs_server *nfs_clone_server(struct nfs_server *source,
1501 struct nfs_fh *fh,
1502 struct nfs_fattr *fattr)
1503{
1504 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001505 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001506 int error;
1507
1508 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001509 (unsigned long long) fattr->fsid.major,
1510 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001511
1512 server = nfs_alloc_server();
1513 if (!server)
1514 return ERR_PTR(-ENOMEM);
1515
Trond Myklebustfbca7792010-04-16 16:22:46 -04001516 error = -ENOMEM;
1517 fattr_fsinfo = nfs_alloc_fattr();
1518 if (fattr_fsinfo == NULL)
1519 goto out_free_server;
1520
David Howells54ceac42006-08-22 20:06:13 -04001521 /* Copy data from the source */
1522 server->nfs_client = source->nfs_client;
1523 atomic_inc(&server->nfs_client->cl_count);
1524 nfs_server_copy_userdata(server, source);
1525
1526 server->fsid = fattr->fsid;
1527
Trond Myklebust33170232007-12-20 16:03:59 -05001528 error = nfs_init_server_rpcclient(server,
1529 source->client->cl_timeout,
1530 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001531 if (error < 0)
1532 goto out_free_server;
1533 if (!IS_ERR(source->client_acl))
1534 nfs_init_server_aclclient(server);
1535
1536 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001537 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001538 if (error < 0)
1539 goto out_free_server;
1540
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001541 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1542 server->namelen = NFS4_MAXNAMLEN;
1543
David Howells54ceac42006-08-22 20:06:13 -04001544 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001545 (unsigned long long) server->fsid.major,
1546 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001547
1548 error = nfs_start_lockd(server);
1549 if (error < 0)
1550 goto out_free_server;
1551
1552 spin_lock(&nfs_client_lock);
1553 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1554 list_add_tail(&server->master_link, &nfs_volume_list);
1555 spin_unlock(&nfs_client_lock);
1556
1557 server->mount_time = jiffies;
1558
Trond Myklebustfbca7792010-04-16 16:22:46 -04001559 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001560 dprintk("<-- nfs_clone_server() = %p\n", server);
1561 return server;
1562
1563out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001564 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001565 nfs_free_server(server);
1566 dprintk("<-- nfs_clone_server() = error %d\n", error);
1567 return ERR_PTR(error);
1568}
David Howells6aaca562006-08-22 20:06:13 -04001569
1570#ifdef CONFIG_PROC_FS
1571static struct proc_dir_entry *proc_fs_nfs;
1572
1573static int nfs_server_list_open(struct inode *inode, struct file *file);
1574static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1575static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1576static void nfs_server_list_stop(struct seq_file *p, void *v);
1577static int nfs_server_list_show(struct seq_file *m, void *v);
1578
James Morris88e9d342009-09-22 16:43:43 -07001579static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001580 .start = nfs_server_list_start,
1581 .next = nfs_server_list_next,
1582 .stop = nfs_server_list_stop,
1583 .show = nfs_server_list_show,
1584};
1585
Arjan van de Ven00977a52007-02-12 00:55:34 -08001586static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001587 .open = nfs_server_list_open,
1588 .read = seq_read,
1589 .llseek = seq_lseek,
1590 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001591 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001592};
1593
1594static int nfs_volume_list_open(struct inode *inode, struct file *file);
1595static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1596static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1597static void nfs_volume_list_stop(struct seq_file *p, void *v);
1598static int nfs_volume_list_show(struct seq_file *m, void *v);
1599
James Morris88e9d342009-09-22 16:43:43 -07001600static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001601 .start = nfs_volume_list_start,
1602 .next = nfs_volume_list_next,
1603 .stop = nfs_volume_list_stop,
1604 .show = nfs_volume_list_show,
1605};
1606
Arjan van de Ven00977a52007-02-12 00:55:34 -08001607static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001608 .open = nfs_volume_list_open,
1609 .read = seq_read,
1610 .llseek = seq_lseek,
1611 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001612 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001613};
1614
1615/*
1616 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1617 * we're dealing
1618 */
1619static int nfs_server_list_open(struct inode *inode, struct file *file)
1620{
1621 struct seq_file *m;
1622 int ret;
1623
1624 ret = seq_open(file, &nfs_server_list_ops);
1625 if (ret < 0)
1626 return ret;
1627
1628 m = file->private_data;
1629 m->private = PDE(inode)->data;
1630
1631 return 0;
1632}
1633
1634/*
1635 * set up the iterator to start reading from the server list and return the first item
1636 */
1637static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1638{
David Howells6aaca562006-08-22 20:06:13 -04001639 /* lock the list against modification */
1640 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001641 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001642}
1643
1644/*
1645 * move to next server
1646 */
1647static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1648{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001649 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001650}
1651
1652/*
1653 * clean up after reading from the transports list
1654 */
1655static void nfs_server_list_stop(struct seq_file *p, void *v)
1656{
1657 spin_unlock(&nfs_client_lock);
1658}
1659
1660/*
1661 * display a header line followed by a load of call lines
1662 */
1663static int nfs_server_list_show(struct seq_file *m, void *v)
1664{
1665 struct nfs_client *clp;
1666
1667 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001668 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001669 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1670 return 0;
1671 }
1672
1673 /* display one transport per line on subsequent lines */
1674 clp = list_entry(v, struct nfs_client, cl_share_link);
1675
Chuck Lever5d8515c2007-12-10 14:57:16 -05001676 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001677 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001678 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1679 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001680 atomic_read(&clp->cl_count),
1681 clp->cl_hostname);
1682
1683 return 0;
1684}
1685
1686/*
1687 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1688 */
1689static int nfs_volume_list_open(struct inode *inode, struct file *file)
1690{
1691 struct seq_file *m;
1692 int ret;
1693
1694 ret = seq_open(file, &nfs_volume_list_ops);
1695 if (ret < 0)
1696 return ret;
1697
1698 m = file->private_data;
1699 m->private = PDE(inode)->data;
1700
1701 return 0;
1702}
1703
1704/*
1705 * set up the iterator to start reading from the volume list and return the first item
1706 */
1707static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1708{
David Howells6aaca562006-08-22 20:06:13 -04001709 /* lock the list against modification */
1710 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001711 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001712}
1713
1714/*
1715 * move to next volume
1716 */
1717static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1718{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001719 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001720}
1721
1722/*
1723 * clean up after reading from the transports list
1724 */
1725static void nfs_volume_list_stop(struct seq_file *p, void *v)
1726{
1727 spin_unlock(&nfs_client_lock);
1728}
1729
1730/*
1731 * display a header line followed by a load of call lines
1732 */
1733static int nfs_volume_list_show(struct seq_file *m, void *v)
1734{
1735 struct nfs_server *server;
1736 struct nfs_client *clp;
1737 char dev[8], fsid[17];
1738
1739 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001740 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001741 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001742 return 0;
1743 }
1744 /* display one transport per line on subsequent lines */
1745 server = list_entry(v, struct nfs_server, master_link);
1746 clp = server->nfs_client;
1747
1748 snprintf(dev, 8, "%u:%u",
1749 MAJOR(server->s_dev), MINOR(server->s_dev));
1750
1751 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001752 (unsigned long long) server->fsid.major,
1753 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001754
David Howells5d1acff2009-04-03 16:42:47 +01001755 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001756 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001757 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1758 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001759 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001760 fsid,
1761 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001762
1763 return 0;
1764}
1765
1766/*
1767 * initialise the /proc/fs/nfsfs/ directory
1768 */
1769int __init nfs_fs_proc_init(void)
1770{
1771 struct proc_dir_entry *p;
1772
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001773 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001774 if (!proc_fs_nfs)
1775 goto error_0;
1776
David Howells6aaca562006-08-22 20:06:13 -04001777 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001778 p = proc_create("servers", S_IFREG|S_IRUGO,
1779 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001780 if (!p)
1781 goto error_1;
1782
David Howells6aaca562006-08-22 20:06:13 -04001783 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001784 p = proc_create("volumes", S_IFREG|S_IRUGO,
1785 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001786 if (!p)
1787 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001788 return 0;
1789
1790error_2:
1791 remove_proc_entry("servers", proc_fs_nfs);
1792error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001793 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001794error_0:
1795 return -ENOMEM;
1796}
1797
1798/*
1799 * clean up the /proc/fs/nfsfs/ directory
1800 */
1801void nfs_fs_proc_exit(void)
1802{
1803 remove_proc_entry("volumes", proc_fs_nfs);
1804 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001805 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001806}
1807
1808#endif /* CONFIG_PROC_FS */