blob: bc3a8620e8c34285c310e082abb4a5205251d875 [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);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000059#ifdef CONFIG_NFS_V4
60static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */
61
62/*
63 * Get a unique NFSv4.0 callback identifier which will be used
64 * by the V4.0 callback service to lookup the nfs_client struct
65 */
66static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
67{
68 int ret = 0;
69
70 if (clp->rpc_ops->version != 4 || minorversion != 0)
71 return ret;
72retry:
73 if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL))
74 return -ENOMEM;
75 spin_lock(&nfs_client_lock);
76 ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident);
77 spin_unlock(&nfs_client_lock);
78 if (ret == -EAGAIN)
79 goto retry;
80 return ret;
81}
82#endif /* CONFIG_NFS_V4 */
David Howells24c8dbb2006-08-22 20:06:10 -040083
84/*
David Howells5006a762006-08-22 20:06:12 -040085 * RPC cruft for NFS
86 */
87static struct rpc_version *nfs_version[5] = {
88 [2] = &nfs_version2,
89#ifdef CONFIG_NFS_V3
90 [3] = &nfs_version3,
91#endif
92#ifdef CONFIG_NFS_V4
93 [4] = &nfs_version4,
94#endif
95};
96
97struct rpc_program nfs_program = {
98 .name = "nfs",
99 .number = NFS_PROGRAM,
100 .nrvers = ARRAY_SIZE(nfs_version),
101 .version = nfs_version,
102 .stats = &nfs_rpcstat,
103 .pipe_dir_name = "/nfs",
104};
105
106struct rpc_stat nfs_rpcstat = {
107 .program = &nfs_program
108};
109
110
111#ifdef CONFIG_NFS_V3_ACL
112static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
113static struct rpc_version * nfsacl_version[] = {
114 [3] = &nfsacl_version3,
115};
116
117struct rpc_program nfsacl_program = {
118 .name = "nfsacl",
119 .number = NFS_ACL_PROGRAM,
120 .nrvers = ARRAY_SIZE(nfsacl_version),
121 .version = nfsacl_version,
122 .stats = &nfsacl_rpcstat,
123};
124#endif /* CONFIG_NFS_V3_ACL */
125
Trond Myklebust3a498022007-12-14 14:56:04 -0500126struct nfs_client_initdata {
127 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500128 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500129 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500130 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500131 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400132 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500133};
134
David Howells5006a762006-08-22 20:06:12 -0400135/*
David Howells24c8dbb2006-08-22 20:06:10 -0400136 * Allocate a shared client record
137 *
138 * Since these are allocated/deallocated very rarely, we don't
139 * bother putting them in a slab cache...
140 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500141static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400142{
143 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400144 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700145 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400146
147 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
148 goto error_0;
149
Trond Myklebust40c553192007-12-14 14:56:07 -0500150 clp->rpc_ops = cl_init->rpc_ops;
151
David Howells24c8dbb2006-08-22 20:06:10 -0400152 atomic_set(&clp->cl_count, 1);
153 clp->cl_cons_state = NFS_CS_INITING;
154
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500155 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
156 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400157
Trond Myklebust3a498022007-12-14 14:56:04 -0500158 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700159 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500160 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400161 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400162 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400163 }
164
165 INIT_LIST_HEAD(&clp->cl_superblocks);
166 clp->cl_rpcclient = ERR_PTR(-EINVAL);
167
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500168 clp->cl_proto = cl_init->proto;
169
David Howells24c8dbb2006-08-22 20:06:10 -0400170#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000171 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
172 if (err)
173 goto error_cleanup;
174
David Howells24c8dbb2006-08-22 20:06:10 -0400175 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400176 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000177 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400178 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
179 clp->cl_boot_time = CURRENT_TIME;
180 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400181 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400182 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400183#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400184 cred = rpc_lookup_machine_cred();
185 if (!IS_ERR(cred))
186 clp->cl_machine_cred = cred;
Andy Adamson974cec82010-10-20 00:18:02 -0400187#if defined(CONFIG_NFS_V4_1)
188 INIT_LIST_HEAD(&clp->cl_layouts);
189#endif
David Howells14727282009-04-03 16:42:42 +0100190 nfs_fscache_get_client_cookie(clp);
191
David Howells24c8dbb2006-08-22 20:06:10 -0400192 return clp;
193
Benny Halevy71468512009-04-01 09:22:56 -0400194error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400195 kfree(clp);
196error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700197 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400198}
199
Trond Myklebust5dd31772006-08-24 01:03:05 -0400200#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400201#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000202static void nfs4_shutdown_session(struct nfs_client *clp)
203{
204 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400205 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400206}
Andy Adamsonea005282011-01-06 02:04:29 +0000207#else /* CONFIG_NFS_V4_1 */
208static void nfs4_shutdown_session(struct nfs_client *clp)
209{
210}
211#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400212
213/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800214 * Destroy the NFS4 callback service
215 */
216static void nfs4_destroy_callback(struct nfs_client *clp)
217{
218 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400219 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800220}
221
222static void nfs4_shutdown_client(struct nfs_client *clp)
223{
224 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
225 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000226 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800227 nfs4_destroy_callback(clp);
228 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
229 nfs_idmap_delete(clp);
230
231 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
232}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000233
234/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
235void nfs_cleanup_cb_ident_idr(void)
236{
237 idr_destroy(&cb_ident_idr);
238}
239
240/* nfs_client_lock held */
241static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
242{
243 if (clp->cl_cb_ident)
244 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
245}
246
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800247#else
248static void nfs4_shutdown_client(struct nfs_client *clp)
249{
250}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000251
252void nfs_cleanup_cb_ident_idr(void)
253{
254}
255
256static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
257{
258}
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800259#endif /* CONFIG_NFS_V4 */
260
261/*
David Howells24c8dbb2006-08-22 20:06:10 -0400262 * Destroy a shared client record
263 */
264static void nfs_free_client(struct nfs_client *clp)
265{
Trond Myklebust40c553192007-12-14 14:56:07 -0500266 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400267
Trond Myklebust5dd31772006-08-24 01:03:05 -0400268 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400269
David Howells14727282009-04-03 16:42:42 +0100270 nfs_fscache_release_client_cookie(clp);
271
David Howells24c8dbb2006-08-22 20:06:10 -0400272 /* -EIO all pending I/O */
273 if (!IS_ERR(clp->cl_rpcclient))
274 rpc_shutdown_client(clp->cl_rpcclient);
275
Trond Myklebust7c67db32008-04-07 20:50:11 -0400276 if (clp->cl_machine_cred != NULL)
277 put_rpccred(clp->cl_machine_cred);
278
David Howells24c8dbb2006-08-22 20:06:10 -0400279 kfree(clp->cl_hostname);
280 kfree(clp);
281
282 dprintk("<-- nfs_free_client()\n");
283}
284
285/*
286 * Release a reference to a shared client record
287 */
288void nfs_put_client(struct nfs_client *clp)
289{
David Howells27ba8512006-07-30 14:40:56 -0400290 if (!clp)
291 return;
292
David Howells24c8dbb2006-08-22 20:06:10 -0400293 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
294
295 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
296 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000297 nfs_cb_idr_remove_locked(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400298 spin_unlock(&nfs_client_lock);
299
300 BUG_ON(!list_empty(&clp->cl_superblocks));
301
302 nfs_free_client(clp);
303 }
304}
Andy Adamson16b374c2010-10-20 00:18:04 -0400305EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400306
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500307#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400308/*
309 * Test if two ip6 socket addresses refer to the same socket by
310 * comparing relevant fields. The padding bytes specifically, are not
311 * compared. sin6_flowinfo is not compared because it only affects QoS
312 * and sin6_scope_id is only compared if the address is "link local"
313 * because "link local" addresses need only be unique to a specific
314 * link. Conversely, ordinary unicast addresses might have different
315 * sin6_scope_id.
316 *
317 * The caller should ensure both socket addresses are AF_INET6.
318 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400319static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
320 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400321{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400322 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
323 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400324
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400325 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
326 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400327 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500328
Trond Myklebustb20d37c2010-09-12 19:55:26 -0400329 return ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500330}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400331#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
332static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
333 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400334{
335 return 0;
336}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500337#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500338
David Howells24c8dbb2006-08-22 20:06:10 -0400339/*
Ian Dalld7371c42009-03-10 20:33:22 -0400340 * Test if two ip4 socket addresses refer to the same socket, by
341 * comparing relevant fields. The padding bytes specifically, are
342 * not compared.
343 *
344 * The caller should ensure both socket addresses are AF_INET.
345 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400346static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
347 const struct sockaddr *sa2)
348{
349 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
350 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
351
352 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
353}
354
355static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
356 const struct sockaddr *sa2)
357{
358 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
359 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
360
361 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
362 (sin1->sin6_port == sin2->sin6_port);
363}
364
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400365static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
366 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400367{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400368 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
369 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400370
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400371 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
372 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400373}
374
375/*
Ian Dalld7371c42009-03-10 20:33:22 -0400376 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400377 * by comparing (only) relevant fields, excluding the port number.
378 */
379static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
380 const struct sockaddr *sa2)
381{
382 if (sa1->sa_family != sa2->sa_family)
383 return 0;
384
385 switch (sa1->sa_family) {
386 case AF_INET:
387 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
388 case AF_INET6:
389 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
390 }
391 return 0;
392}
393
394/*
395 * Test if two socket addresses represent the same actual socket,
396 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400397 */
398static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
399 const struct sockaddr *sa2)
400{
401 if (sa1->sa_family != sa2->sa_family)
402 return 0;
403
404 switch (sa1->sa_family) {
405 case AF_INET:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400406 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400407 case AF_INET6:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400408 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400409 }
410 return 0;
411}
412
413/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500414 * Find a client by IP address and protocol version
415 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400416 */
Chuck Leverff052642007-12-10 14:58:44 -0500417struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500418{
419 struct nfs_client *clp;
420
421 spin_lock(&nfs_client_lock);
422 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500423 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
424
Trond Myklebustc81468a2007-12-14 14:56:05 -0500425 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400426 if (!(clp->cl_cons_state == NFS_CS_READY ||
427 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500428 continue;
429
430 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500431 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500432 continue;
433
434 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500435 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500436 continue;
437
438 atomic_inc(&clp->cl_count);
439 spin_unlock(&nfs_client_lock);
440 return clp;
441 }
442 spin_unlock(&nfs_client_lock);
443 return NULL;
444}
445
446/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500447 * Find a client by IP address and protocol version
448 * - returns NULL if no such client
449 */
450struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
451{
452 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
453 u32 nfsvers = clp->rpc_ops->version;
454
455 spin_lock(&nfs_client_lock);
456 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
457 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
458
459 /* Don't match clients that failed to initialise properly */
460 if (clp->cl_cons_state != NFS_CS_READY)
461 continue;
462
463 /* Different NFS versions cannot share the same nfs_client */
464 if (clp->rpc_ops->version != nfsvers)
465 continue;
466
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500467 /* Match only the IP address, not the port number */
468 if (!nfs_sockaddr_match_ipaddr(sap, clap))
469 continue;
470
471 atomic_inc(&clp->cl_count);
472 spin_unlock(&nfs_client_lock);
473 return clp;
474 }
475 spin_unlock(&nfs_client_lock);
476 return NULL;
477}
478
479/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500480 * Find an nfs_client on the list that matches the initialisation data
481 * that is supplied.
482 */
483static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400484{
485 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400486 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400487
488 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400489 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700490 /* Don't match clients that failed to initialise properly */
491 if (clp->cl_cons_state < 0)
492 continue;
493
David Howells24c8dbb2006-08-22 20:06:10 -0400494 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500495 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400496 continue;
497
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500498 if (clp->cl_proto != data->proto)
499 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400500 /* Match nfsv4 minorversion */
501 if (clp->cl_minorversion != data->minorversion)
502 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500503 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400504 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400505 continue;
506
Trond Myklebustc81468a2007-12-14 14:56:05 -0500507 atomic_inc(&clp->cl_count);
508 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400509 }
David Howells24c8dbb2006-08-22 20:06:10 -0400510 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400511}
512
513/*
514 * Look up a client by IP address and protocol version
515 * - creates a new record if one doesn't yet exist
516 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500517static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400518{
519 struct nfs_client *clp, *new = NULL;
520 int error;
521
Chuck Leverd7422c42007-12-10 14:58:51 -0500522 dprintk("--> nfs_get_client(%s,v%u)\n",
523 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400524
525 /* see if the client already exists */
526 do {
527 spin_lock(&nfs_client_lock);
528
Trond Myklebustc81468a2007-12-14 14:56:05 -0500529 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400530 if (clp)
531 goto found_client;
532 if (new)
533 goto install_client;
534
535 spin_unlock(&nfs_client_lock);
536
Trond Myklebust3a498022007-12-14 14:56:04 -0500537 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700538 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400539
Chuck Levera21bdd92009-06-17 18:02:10 -0700540 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
541 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400542
543 /* install a new client and return with it unready */
544install_client:
545 clp = new;
546 list_add(&clp->cl_share_link, &nfs_client_list);
547 spin_unlock(&nfs_client_lock);
548 dprintk("--> nfs_get_client() = %p [new]\n", clp);
549 return clp;
550
551 /* found an existing client
552 * - make sure it's ready before returning
553 */
554found_client:
555 spin_unlock(&nfs_client_lock);
556
557 if (new)
558 nfs_free_client(new);
559
Matthew Wilcox150030b2007-12-06 16:24:39 -0500560 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400561 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400562 if (error < 0) {
563 nfs_put_client(clp);
564 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400565 }
566
567 if (clp->cl_cons_state < NFS_CS_READY) {
568 error = clp->cl_cons_state;
569 nfs_put_client(clp);
570 return ERR_PTR(error);
571 }
572
David Howells54ceac42006-08-22 20:06:13 -0400573 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
574
David Howells24c8dbb2006-08-22 20:06:10 -0400575 dprintk("--> nfs_get_client() = %p [share]\n", clp);
576 return clp;
577}
578
579/*
580 * Mark a server as ready or failed
581 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400582void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400583{
584 clp->cl_cons_state = state;
585 wake_up_all(&nfs_client_active_wq);
586}
David Howells5006a762006-08-22 20:06:12 -0400587
588/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400589 * With sessions, the client is not marked ready until after a
590 * successful EXCHANGE_ID and CREATE_SESSION.
591 *
592 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
593 * other versions of NFS can be tried.
594 */
595int nfs4_check_client_ready(struct nfs_client *clp)
596{
597 if (!nfs4_has_session(clp))
598 return 0;
599 if (clp->cl_cons_state < NFS_CS_READY)
600 return -EPROTONOSUPPORT;
601 return 0;
602}
603
604/*
David Howells5006a762006-08-22 20:06:12 -0400605 * Initialise the timeout values for a connection
606 */
607static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
608 unsigned int timeo, unsigned int retrans)
609{
610 to->to_initval = timeo * HZ / 10;
611 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400612
613 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400614 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400615 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400616 if (to->to_retries == 0)
617 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500618 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400619 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400620 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
621 to->to_initval = NFS_MAX_TCP_TIMEOUT;
622 to->to_increment = to->to_initval;
623 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500624 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
625 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
626 if (to->to_maxval < to->to_initval)
627 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400628 to->to_exponential = 0;
629 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400630 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400631 if (to->to_retries == 0)
632 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400633 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400634 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400635 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
636 to->to_initval = NFS_MAX_UDP_TIMEOUT;
637 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
638 to->to_exponential = 1;
639 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400640 default:
641 BUG();
David Howells5006a762006-08-22 20:06:12 -0400642 }
643}
644
645/*
646 * Create an RPC client handle
647 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500648static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500649 const struct rpc_timeout *timeparms,
650 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500651 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400652{
David Howells5006a762006-08-22 20:06:12 -0400653 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400654 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400655 .net = &init_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500656 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400657 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500658 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500659 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400660 .servername = clp->cl_hostname,
661 .program = &nfs_program,
662 .version = clp->rpc_ops->version,
663 .authflavor = flavor,
664 };
David Howells5006a762006-08-22 20:06:12 -0400665
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500666 if (discrtry)
667 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
668 if (noresvport)
669 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
670
David Howells5006a762006-08-22 20:06:12 -0400671 if (!IS_ERR(clp->cl_rpcclient))
672 return 0;
673
Chuck Lever41877d22006-08-22 20:06:20 -0400674 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400675 if (IS_ERR(clnt)) {
676 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700677 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400678 return PTR_ERR(clnt);
679 }
680
David Howells5006a762006-08-22 20:06:12 -0400681 clp->cl_rpcclient = clnt;
682 return 0;
683}
David Howells54ceac42006-08-22 20:06:13 -0400684
685/*
686 * Version 2 or 3 client destruction
687 */
688static void nfs_destroy_server(struct nfs_server *server)
689{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400690 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
691 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500692 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400693}
694
695/*
696 * Version 2 or 3 lockd setup
697 */
698static int nfs_start_lockd(struct nfs_server *server)
699{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500700 struct nlm_host *host;
701 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500702 struct nlmclnt_initdata nlm_init = {
703 .hostname = clp->cl_hostname,
704 .address = (struct sockaddr *)&clp->cl_addr,
705 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500706 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500707 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
708 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500709 };
David Howells54ceac42006-08-22 20:06:13 -0400710
Chuck Lever883bb162008-01-15 16:04:20 -0500711 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500712 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400713 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
714 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500715 return 0;
716
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400717 switch (clp->cl_proto) {
718 default:
719 nlm_init.protocol = IPPROTO_TCP;
720 break;
721 case XPRT_TRANSPORT_UDP:
722 nlm_init.protocol = IPPROTO_UDP;
723 }
724
Chuck Lever883bb162008-01-15 16:04:20 -0500725 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500726 if (IS_ERR(host))
727 return PTR_ERR(host);
728
729 server->nlm_host = host;
730 server->destroy = nfs_destroy_server;
731 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400732}
733
734/*
735 * Initialise an NFSv3 ACL client connection
736 */
737#ifdef CONFIG_NFS_V3_ACL
738static void nfs_init_server_aclclient(struct nfs_server *server)
739{
Trond Myklebust40c553192007-12-14 14:56:07 -0500740 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400741 goto out_noacl;
742 if (server->flags & NFS_MOUNT_NOACL)
743 goto out_noacl;
744
745 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
746 if (IS_ERR(server->client_acl))
747 goto out_noacl;
748
749 /* No errors! Assume that Sun nfsacls are supported */
750 server->caps |= NFS_CAP_ACLS;
751 return;
752
753out_noacl:
754 server->caps &= ~NFS_CAP_ACLS;
755}
756#else
757static inline void nfs_init_server_aclclient(struct nfs_server *server)
758{
759 server->flags &= ~NFS_MOUNT_NOACL;
760 server->caps &= ~NFS_CAP_ACLS;
761}
762#endif
763
764/*
765 * Create a general RPC client
766 */
Trond Myklebust33170232007-12-20 16:03:59 -0500767static int nfs_init_server_rpcclient(struct nfs_server *server,
768 const struct rpc_timeout *timeo,
769 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400770{
771 struct nfs_client *clp = server->nfs_client;
772
773 server->client = rpc_clone_client(clp->cl_rpcclient);
774 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700775 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400776 return PTR_ERR(server->client);
777 }
778
Trond Myklebust33170232007-12-20 16:03:59 -0500779 memcpy(&server->client->cl_timeout_default,
780 timeo,
781 sizeof(server->client->cl_timeout_default));
782 server->client->cl_timeout = &server->client->cl_timeout_default;
783
David Howells54ceac42006-08-22 20:06:13 -0400784 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
785 struct rpc_auth *auth;
786
787 auth = rpcauth_create(pseudoflavour, server->client);
788 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700789 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400790 return PTR_ERR(auth);
791 }
792 }
793 server->client->cl_softrtry = 0;
794 if (server->flags & NFS_MOUNT_SOFT)
795 server->client->cl_softrtry = 1;
796
David Howells54ceac42006-08-22 20:06:13 -0400797 return 0;
798}
799
800/*
801 * Initialise an NFS2 or NFS3 client
802 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400803static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500804 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400805 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400806{
David Howells54ceac42006-08-22 20:06:13 -0400807 int error;
808
809 if (clp->cl_cons_state == NFS_CS_READY) {
810 /* the client is already initialised */
811 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
812 return 0;
813 }
814
David Howells54ceac42006-08-22 20:06:13 -0400815 /*
816 * Create a client RPC handle for doing FSSTAT with UNIX auth only
817 * - RFC 2623, sec 2.3.2
818 */
Chuck Leverd7403512008-12-23 15:21:37 -0500819 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
820 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400821 if (error < 0)
822 goto error;
823 nfs_mark_client_ready(clp, NFS_CS_READY);
824 return 0;
825
826error:
827 nfs_mark_client_ready(clp, error);
828 dprintk("<-- nfs_init_client() = xerror %d\n", error);
829 return error;
830}
831
832/*
833 * Create a version 2 or 3 client
834 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400835static int nfs_init_server(struct nfs_server *server,
836 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400837{
Trond Myklebust3a498022007-12-14 14:56:04 -0500838 struct nfs_client_initdata cl_init = {
839 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500840 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500841 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500842 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500843 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500844 };
Trond Myklebust33170232007-12-20 16:03:59 -0500845 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400846 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500847 int error;
David Howells54ceac42006-08-22 20:06:13 -0400848
849 dprintk("--> nfs_init_server()\n");
850
851#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400852 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500853 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400854#endif
855
856 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500857 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400858 if (IS_ERR(clp)) {
859 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
860 return PTR_ERR(clp);
861 }
862
Trond Myklebust33170232007-12-20 16:03:59 -0500863 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
864 data->timeo, data->retrans);
865 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400866 if (error < 0)
867 goto error;
868
869 server->nfs_client = clp;
870
871 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400872 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100873 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400874 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
875 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
876 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400877
878 if (data->rsize)
879 server->rsize = nfs_block_size(data->rsize, NULL);
880 if (data->wsize)
881 server->wsize = nfs_block_size(data->wsize, NULL);
882
883 server->acregmin = data->acregmin * HZ;
884 server->acregmax = data->acregmax * HZ;
885 server->acdirmin = data->acdirmin * HZ;
886 server->acdirmax = data->acdirmax * HZ;
887
888 /* Start lockd here, before we might error out */
889 error = nfs_start_lockd(server);
890 if (error < 0)
891 goto error;
892
Chuck Leverf22d6d72008-03-14 14:10:22 -0400893 server->port = data->nfs_server.port;
894
Trond Myklebust33170232007-12-20 16:03:59 -0500895 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400896 if (error < 0)
897 goto error;
898
Chuck Lever3f8400d2008-03-14 14:10:30 -0400899 /* Preserve the values of mount_server-related mount options */
900 if (data->mount_server.addrlen) {
901 memcpy(&server->mountd_address, &data->mount_server.address,
902 data->mount_server.addrlen);
903 server->mountd_addrlen = data->mount_server.addrlen;
904 }
905 server->mountd_version = data->mount_server.version;
906 server->mountd_port = data->mount_server.port;
907 server->mountd_protocol = data->mount_server.protocol;
908
David Howells54ceac42006-08-22 20:06:13 -0400909 server->namelen = data->namlen;
910 /* Create a client RPC handle for the NFSv3 ACL management interface */
911 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400912 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
913 return 0;
914
915error:
916 server->nfs_client = NULL;
917 nfs_put_client(clp);
918 dprintk("<-- nfs_init_server() = xerror %d\n", error);
919 return error;
920}
921
922/*
923 * Load up the server record from information gained in an fsinfo record
924 */
925static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
926{
927 unsigned long max_rpc_payload;
928
929 /* Work out a lot of parameters */
930 if (server->rsize == 0)
931 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
932 if (server->wsize == 0)
933 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
934
935 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
936 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
937 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
938 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
939
940 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
941 if (server->rsize > max_rpc_payload)
942 server->rsize = max_rpc_payload;
943 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
944 server->rsize = NFS_MAX_FILE_IO_SIZE;
945 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700946
Jens Axboed9938312009-06-12 14:45:52 +0200947 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400948 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
949
950 if (server->wsize > max_rpc_payload)
951 server->wsize = max_rpc_payload;
952 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
953 server->wsize = NFS_MAX_FILE_IO_SIZE;
954 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400955 set_pnfs_layoutdriver(server, fsinfo->layouttype);
956
David Howells54ceac42006-08-22 20:06:13 -0400957 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
958
959 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400960 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
961 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400962 if (server->dtsize > server->rsize)
963 server->dtsize = server->rsize;
964
965 if (server->flags & NFS_MOUNT_NOAC) {
966 server->acregmin = server->acregmax = 0;
967 server->acdirmin = server->acdirmax = 0;
968 }
969
970 server->maxfilesize = fsinfo->maxfilesize;
971
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700972 server->time_delta = fsinfo->time_delta;
973
David Howells54ceac42006-08-22 20:06:13 -0400974 /* We're airborne Set socket buffersize */
975 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
976}
977
978/*
979 * Probe filesystem information, including the FSID on v2/v3
980 */
981static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
982{
983 struct nfs_fsinfo fsinfo;
984 struct nfs_client *clp = server->nfs_client;
985 int error;
986
987 dprintk("--> nfs_probe_fsinfo()\n");
988
989 if (clp->rpc_ops->set_capabilities != NULL) {
990 error = clp->rpc_ops->set_capabilities(server, mntfh);
991 if (error < 0)
992 goto out_error;
993 }
994
995 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400996 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400997 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
998 if (error < 0)
999 goto out_error;
1000
1001 nfs_server_set_fsinfo(server, &fsinfo);
1002
1003 /* Get some general file system info */
1004 if (server->namelen == 0) {
1005 struct nfs_pathconf pathinfo;
1006
1007 pathinfo.fattr = fattr;
1008 nfs_fattr_init(fattr);
1009
1010 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1011 server->namelen = pathinfo.max_namelen;
1012 }
1013
1014 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1015 return 0;
1016
1017out_error:
1018 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1019 return error;
1020}
1021
1022/*
1023 * Copy useful information when duplicating a server record
1024 */
1025static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1026{
1027 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001028 target->rsize = source->rsize;
1029 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001030 target->acregmin = source->acregmin;
1031 target->acregmax = source->acregmax;
1032 target->acdirmin = source->acdirmin;
1033 target->acdirmax = source->acdirmax;
1034 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001035 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001036}
1037
1038/*
1039 * Allocate and initialise a server record
1040 */
1041static struct nfs_server *nfs_alloc_server(void)
1042{
1043 struct nfs_server *server;
1044
1045 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1046 if (!server)
1047 return NULL;
1048
1049 server->client = server->client_acl = ERR_PTR(-EINVAL);
1050
1051 /* Zero out the NFS state stuff */
1052 INIT_LIST_HEAD(&server->client_link);
1053 INIT_LIST_HEAD(&server->master_link);
1054
Steve Dicksonef818a22007-11-08 04:05:04 -05001055 atomic_set(&server->active, 0);
1056
David Howells54ceac42006-08-22 20:06:13 -04001057 server->io_stats = nfs_alloc_iostats();
1058 if (!server->io_stats) {
1059 kfree(server);
1060 return NULL;
1061 }
1062
Jens Axboe48d07642009-09-21 09:59:39 +02001063 if (bdi_init(&server->backing_dev_info)) {
1064 nfs_free_iostats(server->io_stats);
1065 kfree(server);
1066 return NULL;
1067 }
1068
David Howells54ceac42006-08-22 20:06:13 -04001069 return server;
1070}
1071
1072/*
1073 * Free up a server record
1074 */
1075void nfs_free_server(struct nfs_server *server)
1076{
1077 dprintk("--> nfs_free_server()\n");
1078
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001079 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001080 spin_lock(&nfs_client_lock);
1081 list_del(&server->client_link);
1082 list_del(&server->master_link);
1083 spin_unlock(&nfs_client_lock);
1084
1085 if (server->destroy != NULL)
1086 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001087
1088 if (!IS_ERR(server->client_acl))
1089 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001090 if (!IS_ERR(server->client))
1091 rpc_shutdown_client(server->client);
1092
1093 nfs_put_client(server->nfs_client);
1094
1095 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001096 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001097 kfree(server);
1098 nfs_release_automount_timer();
1099 dprintk("<-- nfs_free_server()\n");
1100}
1101
1102/*
1103 * Create a version 2 or 3 volume record
1104 * - keyed on server and FSID
1105 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001106struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001107 struct nfs_fh *mntfh)
1108{
1109 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001110 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001111 int error;
1112
1113 server = nfs_alloc_server();
1114 if (!server)
1115 return ERR_PTR(-ENOMEM);
1116
Trond Myklebustfbca7792010-04-16 16:22:46 -04001117 error = -ENOMEM;
1118 fattr = nfs_alloc_fattr();
1119 if (fattr == NULL)
1120 goto error;
1121
David Howells54ceac42006-08-22 20:06:13 -04001122 /* Get a client representation */
1123 error = nfs_init_server(server, data);
1124 if (error < 0)
1125 goto error;
1126
1127 BUG_ON(!server->nfs_client);
1128 BUG_ON(!server->nfs_client->rpc_ops);
1129 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1130
1131 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001132 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001133 if (error < 0)
1134 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001135 if (server->nfs_client->rpc_ops->version == 3) {
1136 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1137 server->namelen = NFS3_MAXNAMLEN;
1138 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1139 server->caps |= NFS_CAP_READDIRPLUS;
1140 } else {
1141 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1142 server->namelen = NFS2_MAXNAMLEN;
1143 }
1144
Trond Myklebustfbca7792010-04-16 16:22:46 -04001145 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1146 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001147 if (error < 0) {
1148 dprintk("nfs_create_server: getattr error = %d\n", -error);
1149 goto error;
1150 }
1151 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001152 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001153
David Howells6daabf12006-08-24 15:44:16 -04001154 dprintk("Server FSID: %llx:%llx\n",
1155 (unsigned long long) server->fsid.major,
1156 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001157
David Howells54ceac42006-08-22 20:06:13 -04001158 spin_lock(&nfs_client_lock);
1159 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1160 list_add_tail(&server->master_link, &nfs_volume_list);
1161 spin_unlock(&nfs_client_lock);
1162
1163 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001164 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001165 return server;
1166
1167error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001168 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001169 nfs_free_server(server);
1170 return ERR_PTR(error);
1171}
1172
1173#ifdef CONFIG_NFS_V4
1174/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001175 * Initialize the NFS4 callback service
1176 */
1177static int nfs4_init_callback(struct nfs_client *clp)
1178{
1179 int error;
1180
1181 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001182 if (nfs4_has_session(clp)) {
1183 error = xprt_setup_backchannel(
1184 clp->cl_rpcclient->cl_xprt,
1185 NFS41_BC_MIN_CALLBACKS);
1186 if (error < 0)
1187 return error;
1188 }
1189
Trond Myklebust97dc1352010-06-16 09:52:26 -04001190 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001191 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001192 if (error < 0) {
1193 dprintk("%s: failed to start callback. Error = %d\n",
1194 __func__, error);
1195 return error;
1196 }
1197 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1198 }
1199 return 0;
1200}
1201
1202/*
Andy Adamson557134a2009-04-01 09:21:53 -04001203 * Initialize the minor version specific parts of an NFS4 client record
1204 */
1205static int nfs4_init_client_minor_version(struct nfs_client *clp)
1206{
1207#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001208 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001209 struct nfs4_session *session = NULL;
1210 /*
1211 * Create the session and mark it expired.
1212 * When a SEQUENCE operation encounters the expired session
1213 * it will do session recovery to initialize it.
1214 */
1215 session = nfs4_alloc_session(clp);
1216 if (!session)
1217 return -ENOMEM;
1218
1219 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001220 /*
1221 * The create session reply races with the server back
1222 * channel probe. Mark the client NFS_CS_SESSION_INITING
1223 * so that the client back channel can find the
1224 * nfs_client struct
1225 */
1226 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001227 }
1228#endif /* CONFIG_NFS_V4_1 */
1229
Benny Halevy71468512009-04-01 09:22:56 -04001230 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001231}
1232
1233/*
David Howells54ceac42006-08-22 20:06:13 -04001234 * Initialise an NFS4 client record
1235 */
1236static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001237 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001238 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001239 rpc_authflavor_t authflavour,
1240 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001241{
1242 int error;
1243
1244 if (clp->cl_cons_state == NFS_CS_READY) {
1245 /* the client is initialised already */
1246 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1247 return 0;
1248 }
1249
1250 /* Check NFS protocol revision and initialize RPC op vector */
1251 clp->rpc_ops = &nfs_v4_clientops;
1252
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001253 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001254 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001255 if (error < 0)
1256 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001257 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001258
1259 error = nfs_idmap_new(clp);
1260 if (error < 0) {
1261 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001262 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001263 goto error;
1264 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001265 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001266
Andy Adamson557134a2009-04-01 09:21:53 -04001267 error = nfs4_init_client_minor_version(clp);
1268 if (error < 0)
1269 goto error;
1270
Andy Adamson76db6d92009-04-01 09:22:38 -04001271 if (!nfs4_has_session(clp))
1272 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001273 return 0;
1274
1275error:
1276 nfs_mark_client_ready(clp, error);
1277 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1278 return error;
1279}
1280
1281/*
1282 * Set up an NFS4 client
1283 */
1284static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001285 const char *hostname,
1286 const struct sockaddr *addr,
1287 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001288 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001289 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001290 int proto, const struct rpc_timeout *timeparms,
1291 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001292{
Trond Myklebust3a498022007-12-14 14:56:04 -05001293 struct nfs_client_initdata cl_init = {
1294 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001295 .addr = addr,
1296 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001297 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001298 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001299 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001300 };
David Howells54ceac42006-08-22 20:06:13 -04001301 struct nfs_client *clp;
1302 int error;
1303
1304 dprintk("--> nfs4_set_client()\n");
1305
1306 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001307 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001308 if (IS_ERR(clp)) {
1309 error = PTR_ERR(clp);
1310 goto error;
1311 }
Chuck Leverd7403512008-12-23 15:21:37 -05001312 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1313 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001314 if (error < 0)
1315 goto error_put;
1316
1317 server->nfs_client = clp;
1318 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1319 return 0;
1320
1321error_put:
1322 nfs_put_client(clp);
1323error:
1324 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1325 return error;
1326}
1327
Andy Adamson557134a2009-04-01 09:21:53 -04001328
1329/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001330 * Session has been established, and the client marked ready.
1331 * Set the mount rsize and wsize with negotiated fore channel
1332 * attributes which will be bound checked in nfs_server_set_fsinfo.
1333 */
1334static void nfs4_session_set_rwsize(struct nfs_server *server)
1335{
1336#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001337 struct nfs4_session *sess;
1338 u32 server_resp_sz;
1339 u32 server_rqst_sz;
1340
Andy Adamson96b09e02009-04-01 09:22:33 -04001341 if (!nfs4_has_session(server->nfs_client))
1342 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001343 sess = server->nfs_client->cl_session;
1344 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1345 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1346
1347 if (server->rsize > server_resp_sz)
1348 server->rsize = server_resp_sz;
1349 if (server->wsize > server_rqst_sz)
1350 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001351#endif /* CONFIG_NFS_V4_1 */
1352}
1353
Trond Myklebust44950b62010-06-17 11:45:12 -04001354static int nfs4_server_common_setup(struct nfs_server *server,
1355 struct nfs_fh *mntfh)
1356{
1357 struct nfs_fattr *fattr;
1358 int error;
1359
1360 BUG_ON(!server->nfs_client);
1361 BUG_ON(!server->nfs_client->rpc_ops);
1362 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1363
1364 fattr = nfs_alloc_fattr();
1365 if (fattr == NULL)
1366 return -ENOMEM;
1367
1368 /* We must ensure the session is initialised first */
1369 error = nfs4_init_session(server);
1370 if (error < 0)
1371 goto out;
1372
1373 /* Probe the root fh to retrieve its FSID and filehandle */
1374 error = nfs4_get_rootfh(server, mntfh);
1375 if (error < 0)
1376 goto out;
1377
1378 dprintk("Server FSID: %llx:%llx\n",
1379 (unsigned long long) server->fsid.major,
1380 (unsigned long long) server->fsid.minor);
1381 dprintk("Mount FH: %d\n", mntfh->size);
1382
1383 nfs4_session_set_rwsize(server);
1384
1385 error = nfs_probe_fsinfo(server, mntfh, fattr);
1386 if (error < 0)
1387 goto out;
1388
1389 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1390 server->namelen = NFS4_MAXNAMLEN;
1391
1392 spin_lock(&nfs_client_lock);
1393 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1394 list_add_tail(&server->master_link, &nfs_volume_list);
1395 spin_unlock(&nfs_client_lock);
1396
1397 server->mount_time = jiffies;
1398out:
1399 nfs_free_fattr(fattr);
1400 return error;
1401}
1402
Andy Adamson96b09e02009-04-01 09:22:33 -04001403/*
David Howells54ceac42006-08-22 20:06:13 -04001404 * Create a version 4 volume record
1405 */
1406static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001407 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001408{
Trond Myklebust33170232007-12-20 16:03:59 -05001409 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001410 int error;
1411
1412 dprintk("--> nfs4_init_server()\n");
1413
Trond Myklebust33170232007-12-20 16:03:59 -05001414 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1415 data->timeo, data->retrans);
1416
Chuck Lever542fcc32008-12-23 15:21:36 -05001417 /* Initialise the client representation from the mount data */
1418 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001419 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1420 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1421 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001422 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001423
Trond Myklebust33170232007-12-20 16:03:59 -05001424 /* Get a client record */
1425 error = nfs4_set_client(server,
1426 data->nfs_server.hostname,
1427 (const struct sockaddr *)&data->nfs_server.address,
1428 data->nfs_server.addrlen,
1429 data->client_address,
1430 data->auth_flavors[0],
1431 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001432 &timeparms,
1433 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001434 if (error < 0)
1435 goto error;
1436
David Howells54ceac42006-08-22 20:06:13 -04001437 if (data->rsize)
1438 server->rsize = nfs_block_size(data->rsize, NULL);
1439 if (data->wsize)
1440 server->wsize = nfs_block_size(data->wsize, NULL);
1441
1442 server->acregmin = data->acregmin * HZ;
1443 server->acregmax = data->acregmax * HZ;
1444 server->acdirmin = data->acdirmin * HZ;
1445 server->acdirmax = data->acdirmax * HZ;
1446
Chuck Leverf22d6d72008-03-14 14:10:22 -04001447 server->port = data->nfs_server.port;
1448
Trond Myklebust33170232007-12-20 16:03:59 -05001449 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001450
Trond Myklebust33170232007-12-20 16:03:59 -05001451error:
David Howells54ceac42006-08-22 20:06:13 -04001452 /* Done */
1453 dprintk("<-- nfs4_init_server() = %d\n", error);
1454 return error;
1455}
1456
1457/*
1458 * Create a version 4 volume record
1459 * - keyed on server and FSID
1460 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001461struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001462 struct nfs_fh *mntfh)
1463{
David Howells54ceac42006-08-22 20:06:13 -04001464 struct nfs_server *server;
1465 int error;
1466
1467 dprintk("--> nfs4_create_server()\n");
1468
1469 server = nfs_alloc_server();
1470 if (!server)
1471 return ERR_PTR(-ENOMEM);
1472
David Howells54ceac42006-08-22 20:06:13 -04001473 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001474 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001475 if (error < 0)
1476 goto error;
1477
Trond Myklebust44950b62010-06-17 11:45:12 -04001478 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001479 if (error < 0)
1480 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001481
David Howells54ceac42006-08-22 20:06:13 -04001482 dprintk("<-- nfs4_create_server() = %p\n", server);
1483 return server;
1484
1485error:
1486 nfs_free_server(server);
1487 dprintk("<-- nfs4_create_server() = error %d\n", error);
1488 return ERR_PTR(error);
1489}
1490
1491/*
1492 * Create an NFS4 referral server record
1493 */
1494struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001495 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001496{
1497 struct nfs_client *parent_client;
1498 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001499 int error;
1500
1501 dprintk("--> nfs4_create_referral_server()\n");
1502
1503 server = nfs_alloc_server();
1504 if (!server)
1505 return ERR_PTR(-ENOMEM);
1506
1507 parent_server = NFS_SB(data->sb);
1508 parent_client = parent_server->nfs_client;
1509
Chuck Lever542fcc32008-12-23 15:21:36 -05001510 /* Initialise the client representation from the parent server */
1511 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001512 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001513
David Howells54ceac42006-08-22 20:06:13 -04001514 /* Get a client representation.
1515 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001516 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001517 data->addr,
1518 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001519 parent_client->cl_ipaddr,
1520 data->authflavor,
1521 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001522 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001523 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001524 if (error < 0)
1525 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001526
Trond Myklebust33170232007-12-20 16:03:59 -05001527 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001528 if (error < 0)
1529 goto error;
1530
Trond Myklebust44950b62010-06-17 11:45:12 -04001531 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001532 if (error < 0)
1533 goto error;
1534
David Howells54ceac42006-08-22 20:06:13 -04001535 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1536 return server;
1537
1538error:
1539 nfs_free_server(server);
1540 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1541 return ERR_PTR(error);
1542}
1543
1544#endif /* CONFIG_NFS_V4 */
1545
1546/*
1547 * Clone an NFS2, NFS3 or NFS4 server record
1548 */
1549struct nfs_server *nfs_clone_server(struct nfs_server *source,
1550 struct nfs_fh *fh,
1551 struct nfs_fattr *fattr)
1552{
1553 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001554 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001555 int error;
1556
1557 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001558 (unsigned long long) fattr->fsid.major,
1559 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001560
1561 server = nfs_alloc_server();
1562 if (!server)
1563 return ERR_PTR(-ENOMEM);
1564
Trond Myklebustfbca7792010-04-16 16:22:46 -04001565 error = -ENOMEM;
1566 fattr_fsinfo = nfs_alloc_fattr();
1567 if (fattr_fsinfo == NULL)
1568 goto out_free_server;
1569
David Howells54ceac42006-08-22 20:06:13 -04001570 /* Copy data from the source */
1571 server->nfs_client = source->nfs_client;
1572 atomic_inc(&server->nfs_client->cl_count);
1573 nfs_server_copy_userdata(server, source);
1574
1575 server->fsid = fattr->fsid;
1576
Trond Myklebust33170232007-12-20 16:03:59 -05001577 error = nfs_init_server_rpcclient(server,
1578 source->client->cl_timeout,
1579 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001580 if (error < 0)
1581 goto out_free_server;
1582 if (!IS_ERR(source->client_acl))
1583 nfs_init_server_aclclient(server);
1584
1585 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001586 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001587 if (error < 0)
1588 goto out_free_server;
1589
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001590 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1591 server->namelen = NFS4_MAXNAMLEN;
1592
David Howells54ceac42006-08-22 20:06:13 -04001593 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001594 (unsigned long long) server->fsid.major,
1595 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001596
1597 error = nfs_start_lockd(server);
1598 if (error < 0)
1599 goto out_free_server;
1600
1601 spin_lock(&nfs_client_lock);
1602 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1603 list_add_tail(&server->master_link, &nfs_volume_list);
1604 spin_unlock(&nfs_client_lock);
1605
1606 server->mount_time = jiffies;
1607
Trond Myklebustfbca7792010-04-16 16:22:46 -04001608 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001609 dprintk("<-- nfs_clone_server() = %p\n", server);
1610 return server;
1611
1612out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001613 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001614 nfs_free_server(server);
1615 dprintk("<-- nfs_clone_server() = error %d\n", error);
1616 return ERR_PTR(error);
1617}
David Howells6aaca562006-08-22 20:06:13 -04001618
1619#ifdef CONFIG_PROC_FS
1620static struct proc_dir_entry *proc_fs_nfs;
1621
1622static int nfs_server_list_open(struct inode *inode, struct file *file);
1623static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1624static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1625static void nfs_server_list_stop(struct seq_file *p, void *v);
1626static int nfs_server_list_show(struct seq_file *m, void *v);
1627
James Morris88e9d342009-09-22 16:43:43 -07001628static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001629 .start = nfs_server_list_start,
1630 .next = nfs_server_list_next,
1631 .stop = nfs_server_list_stop,
1632 .show = nfs_server_list_show,
1633};
1634
Arjan van de Ven00977a52007-02-12 00:55:34 -08001635static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001636 .open = nfs_server_list_open,
1637 .read = seq_read,
1638 .llseek = seq_lseek,
1639 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001640 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001641};
1642
1643static int nfs_volume_list_open(struct inode *inode, struct file *file);
1644static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1645static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1646static void nfs_volume_list_stop(struct seq_file *p, void *v);
1647static int nfs_volume_list_show(struct seq_file *m, void *v);
1648
James Morris88e9d342009-09-22 16:43:43 -07001649static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001650 .start = nfs_volume_list_start,
1651 .next = nfs_volume_list_next,
1652 .stop = nfs_volume_list_stop,
1653 .show = nfs_volume_list_show,
1654};
1655
Arjan van de Ven00977a52007-02-12 00:55:34 -08001656static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001657 .open = nfs_volume_list_open,
1658 .read = seq_read,
1659 .llseek = seq_lseek,
1660 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001661 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001662};
1663
1664/*
1665 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1666 * we're dealing
1667 */
1668static int nfs_server_list_open(struct inode *inode, struct file *file)
1669{
1670 struct seq_file *m;
1671 int ret;
1672
1673 ret = seq_open(file, &nfs_server_list_ops);
1674 if (ret < 0)
1675 return ret;
1676
1677 m = file->private_data;
1678 m->private = PDE(inode)->data;
1679
1680 return 0;
1681}
1682
1683/*
1684 * set up the iterator to start reading from the server list and return the first item
1685 */
1686static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1687{
David Howells6aaca562006-08-22 20:06:13 -04001688 /* lock the list against modification */
1689 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001690 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001691}
1692
1693/*
1694 * move to next server
1695 */
1696static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1697{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001698 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001699}
1700
1701/*
1702 * clean up after reading from the transports list
1703 */
1704static void nfs_server_list_stop(struct seq_file *p, void *v)
1705{
1706 spin_unlock(&nfs_client_lock);
1707}
1708
1709/*
1710 * display a header line followed by a load of call lines
1711 */
1712static int nfs_server_list_show(struct seq_file *m, void *v)
1713{
1714 struct nfs_client *clp;
1715
1716 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001717 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001718 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1719 return 0;
1720 }
1721
1722 /* display one transport per line on subsequent lines */
1723 clp = list_entry(v, struct nfs_client, cl_share_link);
1724
Chuck Lever5d8515c2007-12-10 14:57:16 -05001725 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001726 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001727 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1728 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001729 atomic_read(&clp->cl_count),
1730 clp->cl_hostname);
1731
1732 return 0;
1733}
1734
1735/*
1736 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1737 */
1738static int nfs_volume_list_open(struct inode *inode, struct file *file)
1739{
1740 struct seq_file *m;
1741 int ret;
1742
1743 ret = seq_open(file, &nfs_volume_list_ops);
1744 if (ret < 0)
1745 return ret;
1746
1747 m = file->private_data;
1748 m->private = PDE(inode)->data;
1749
1750 return 0;
1751}
1752
1753/*
1754 * set up the iterator to start reading from the volume list and return the first item
1755 */
1756static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1757{
David Howells6aaca562006-08-22 20:06:13 -04001758 /* lock the list against modification */
1759 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001760 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001761}
1762
1763/*
1764 * move to next volume
1765 */
1766static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1767{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001768 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001769}
1770
1771/*
1772 * clean up after reading from the transports list
1773 */
1774static void nfs_volume_list_stop(struct seq_file *p, void *v)
1775{
1776 spin_unlock(&nfs_client_lock);
1777}
1778
1779/*
1780 * display a header line followed by a load of call lines
1781 */
1782static int nfs_volume_list_show(struct seq_file *m, void *v)
1783{
1784 struct nfs_server *server;
1785 struct nfs_client *clp;
1786 char dev[8], fsid[17];
1787
1788 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001789 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001790 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001791 return 0;
1792 }
1793 /* display one transport per line on subsequent lines */
1794 server = list_entry(v, struct nfs_server, master_link);
1795 clp = server->nfs_client;
1796
1797 snprintf(dev, 8, "%u:%u",
1798 MAJOR(server->s_dev), MINOR(server->s_dev));
1799
1800 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001801 (unsigned long long) server->fsid.major,
1802 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001803
David Howells5d1acff2009-04-03 16:42:47 +01001804 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001805 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001806 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1807 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001808 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001809 fsid,
1810 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001811
1812 return 0;
1813}
1814
1815/*
1816 * initialise the /proc/fs/nfsfs/ directory
1817 */
1818int __init nfs_fs_proc_init(void)
1819{
1820 struct proc_dir_entry *p;
1821
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001822 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001823 if (!proc_fs_nfs)
1824 goto error_0;
1825
David Howells6aaca562006-08-22 20:06:13 -04001826 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001827 p = proc_create("servers", S_IFREG|S_IRUGO,
1828 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001829 if (!p)
1830 goto error_1;
1831
David Howells6aaca562006-08-22 20:06:13 -04001832 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001833 p = proc_create("volumes", S_IFREG|S_IRUGO,
1834 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001835 if (!p)
1836 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001837 return 0;
1838
1839error_2:
1840 remove_proc_entry("servers", proc_fs_nfs);
1841error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001842 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001843error_0:
1844 return -ENOMEM;
1845}
1846
1847/*
1848 * clean up the /proc/fs/nfsfs/ directory
1849 */
1850void nfs_fs_proc_exit(void)
1851{
1852 remove_proc_entry("volumes", proc_fs_nfs);
1853 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001854 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001855}
1856
1857#endif /* CONFIG_PROC_FS */