blob: f51b2795ce07c67af1382556653634298fca1ca3 [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>
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000042#include <linux/nsproxy.h>
43#include <linux/pid_namespace.h>
David Howells24c8dbb2006-08-22 20:06:10 -040044
45#include <asm/system.h>
46
47#include "nfs4_fs.h"
48#include "callback.h"
49#include "delegation.h"
50#include "iostat.h"
51#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010052#include "fscache.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040053#include "pnfs.h"
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000054#include "netns.h"
David Howells24c8dbb2006-08-22 20:06:10 -040055
56#define NFSDBG_FACILITY NFSDBG_CLIENT
57
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +040058DEFINE_SPINLOCK(nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -040059static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000060#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +000061
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;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000069 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000070
71 if (clp->rpc_ops->version != 4 || minorversion != 0)
72 return ret;
73retry:
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000074 if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
Andy Adamsonf4eecd52011-01-06 02:04:30 +000075 return -ENOMEM;
76 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000077 ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000078 spin_unlock(&nfs_client_lock);
79 if (ret == -EAGAIN)
80 goto retry;
81 return ret;
82}
83#endif /* CONFIG_NFS_V4 */
David Howells24c8dbb2006-08-22 20:06:10 -040084
85/*
Trond Myklebustb064eca22011-02-22 15:44:32 -080086 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
87 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103088static bool nfs4_disable_idmapping = true;
Trond Myklebustb064eca22011-02-22 15:44:32 -080089
90/*
David Howells5006a762006-08-22 20:06:12 -040091 * RPC cruft for NFS
92 */
Trond Myklebusta613fa12012-01-20 13:53:56 -050093static const struct rpc_version *nfs_version[5] = {
David Howells5006a762006-08-22 20:06:12 -040094 [2] = &nfs_version2,
95#ifdef CONFIG_NFS_V3
96 [3] = &nfs_version3,
97#endif
98#ifdef CONFIG_NFS_V4
99 [4] = &nfs_version4,
100#endif
101};
102
Trond Myklebusta613fa12012-01-20 13:53:56 -0500103const struct rpc_program nfs_program = {
David Howells5006a762006-08-22 20:06:12 -0400104 .name = "nfs",
105 .number = NFS_PROGRAM,
106 .nrvers = ARRAY_SIZE(nfs_version),
107 .version = nfs_version,
108 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400109 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -0400110};
111
112struct rpc_stat nfs_rpcstat = {
113 .program = &nfs_program
114};
115
116
117#ifdef CONFIG_NFS_V3_ACL
118static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
Trond Myklebusta613fa12012-01-20 13:53:56 -0500119static const struct rpc_version *nfsacl_version[] = {
David Howells5006a762006-08-22 20:06:12 -0400120 [3] = &nfsacl_version3,
121};
122
Trond Myklebusta613fa12012-01-20 13:53:56 -0500123const struct rpc_program nfsacl_program = {
David Howells5006a762006-08-22 20:06:12 -0400124 .name = "nfsacl",
125 .number = NFS_ACL_PROGRAM,
126 .nrvers = ARRAY_SIZE(nfsacl_version),
127 .version = nfsacl_version,
128 .stats = &nfsacl_rpcstat,
129};
130#endif /* CONFIG_NFS_V3_ACL */
131
Trond Myklebust3a498022007-12-14 14:56:04 -0500132struct nfs_client_initdata {
133 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500134 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500135 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500136 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500137 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400138 u32 minorversion;
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400139 struct net *net;
Trond Myklebust3a498022007-12-14 14:56:04 -0500140};
141
David Howells5006a762006-08-22 20:06:12 -0400142/*
David Howells24c8dbb2006-08-22 20:06:10 -0400143 * Allocate a shared client record
144 *
145 * Since these are allocated/deallocated very rarely, we don't
146 * bother putting them in a slab cache...
147 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500148static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400149{
150 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400151 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700152 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400153
154 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
155 goto error_0;
156
Trond Myklebust40c553192007-12-14 14:56:07 -0500157 clp->rpc_ops = cl_init->rpc_ops;
158
David Howells24c8dbb2006-08-22 20:06:10 -0400159 atomic_set(&clp->cl_count, 1);
160 clp->cl_cons_state = NFS_CS_INITING;
161
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500162 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
163 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400164
Trond Myklebust3a498022007-12-14 14:56:04 -0500165 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700166 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500167 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400168 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400169 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400170 }
171
172 INIT_LIST_HEAD(&clp->cl_superblocks);
173 clp->cl_rpcclient = ERR_PTR(-EINVAL);
174
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500175 clp->cl_proto = cl_init->proto;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000176 clp->net = cl_init->net;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500177
David Howells24c8dbb2006-08-22 20:06:10 -0400178#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000179 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
180 if (err)
181 goto error_cleanup;
182
David Howells24c8dbb2006-08-22 20:06:10 -0400183 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000184 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400185 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
186 clp->cl_boot_time = CURRENT_TIME;
187 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400188 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400189 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400190#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500191 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400192 if (!IS_ERR(cred))
193 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100194 nfs_fscache_get_client_cookie(clp);
195
David Howells24c8dbb2006-08-22 20:06:10 -0400196 return clp;
197
Benny Halevy71468512009-04-01 09:22:56 -0400198error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400199 kfree(clp);
200error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700201 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400202}
203
Trond Myklebust5dd31772006-08-24 01:03:05 -0400204#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400205#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000206static void nfs4_shutdown_session(struct nfs_client *clp)
207{
208 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400209 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400210}
Andy Adamsonea005282011-01-06 02:04:29 +0000211#else /* CONFIG_NFS_V4_1 */
212static void nfs4_shutdown_session(struct nfs_client *clp)
213{
214}
215#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400216
217/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800218 * Destroy the NFS4 callback service
219 */
220static void nfs4_destroy_callback(struct nfs_client *clp)
221{
222 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400223 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800224}
225
226static void nfs4_shutdown_client(struct nfs_client *clp)
227{
228 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
229 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000230 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800231 nfs4_destroy_callback(clp);
232 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
233 nfs_idmap_delete(clp);
234
235 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
236}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000237
238/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000239void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000240{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000241 struct nfs_net *nn = net_generic(net, nfs_net_id);
242
243 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000244}
245
246/* nfs_client_lock held */
247static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
248{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000249 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
250
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000251 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000252 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000253}
254
Fred Isamanf7e89172011-01-06 11:36:32 +0000255static void pnfs_init_server(struct nfs_server *server)
256{
257 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
258}
259
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500260static void nfs4_destroy_server(struct nfs_server *server)
261{
262 nfs4_purge_state_owners(server);
263}
264
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800265#else
266static void nfs4_shutdown_client(struct nfs_client *clp)
267{
268}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000269
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000270void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000271{
272}
273
274static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
275{
276}
Fred Isamanf7e89172011-01-06 11:36:32 +0000277
278static void pnfs_init_server(struct nfs_server *server)
279{
280}
281
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800282#endif /* CONFIG_NFS_V4 */
283
284/*
David Howells24c8dbb2006-08-22 20:06:10 -0400285 * Destroy a shared client record
286 */
287static void nfs_free_client(struct nfs_client *clp)
288{
Trond Myklebust40c553192007-12-14 14:56:07 -0500289 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400290
Trond Myklebust5dd31772006-08-24 01:03:05 -0400291 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400292
David Howells14727282009-04-03 16:42:42 +0100293 nfs_fscache_release_client_cookie(clp);
294
David Howells24c8dbb2006-08-22 20:06:10 -0400295 /* -EIO all pending I/O */
296 if (!IS_ERR(clp->cl_rpcclient))
297 rpc_shutdown_client(clp->cl_rpcclient);
298
Trond Myklebust7c67db32008-04-07 20:50:11 -0400299 if (clp->cl_machine_cred != NULL)
300 put_rpccred(clp->cl_machine_cred);
301
Benny Halevy1775bc32011-05-20 13:47:33 +0200302 nfs4_deviceid_purge_client(clp);
303
David Howells24c8dbb2006-08-22 20:06:10 -0400304 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400305 kfree(clp->server_scope);
David Howells24c8dbb2006-08-22 20:06:10 -0400306 kfree(clp);
307
308 dprintk("<-- nfs_free_client()\n");
309}
310
311/*
312 * Release a reference to a shared client record
313 */
314void nfs_put_client(struct nfs_client *clp)
315{
David Howells27ba8512006-07-30 14:40:56 -0400316 if (!clp)
317 return;
318
David Howells24c8dbb2006-08-22 20:06:10 -0400319 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
320
321 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
322 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000323 nfs_cb_idr_remove_locked(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400324 spin_unlock(&nfs_client_lock);
325
326 BUG_ON(!list_empty(&clp->cl_superblocks));
327
328 nfs_free_client(clp);
329 }
330}
Andy Adamson16b374c2010-10-20 00:18:04 -0400331EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400332
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500333#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400334/*
335 * Test if two ip6 socket addresses refer to the same socket by
336 * comparing relevant fields. The padding bytes specifically, are not
337 * compared. sin6_flowinfo is not compared because it only affects QoS
338 * and sin6_scope_id is only compared if the address is "link local"
339 * because "link local" addresses need only be unique to a specific
340 * link. Conversely, ordinary unicast addresses might have different
341 * sin6_scope_id.
342 *
343 * The caller should ensure both socket addresses are AF_INET6.
344 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400345static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
346 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400347{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400348 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
349 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400350
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800351 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400352 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800353 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
354 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500355
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800356 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500357}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400358#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
359static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
360 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400361{
362 return 0;
363}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500364#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500365
David Howells24c8dbb2006-08-22 20:06:10 -0400366/*
Ian Dalld7371c42009-03-10 20:33:22 -0400367 * Test if two ip4 socket addresses refer to the same socket, by
368 * comparing relevant fields. The padding bytes specifically, are
369 * not compared.
370 *
371 * The caller should ensure both socket addresses are AF_INET.
372 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400373static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
374 const struct sockaddr *sa2)
375{
376 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
377 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
378
379 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
380}
381
382static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
383 const struct sockaddr *sa2)
384{
385 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
386 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
387
388 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
389 (sin1->sin6_port == sin2->sin6_port);
390}
391
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400392static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
393 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400394{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400395 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
396 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400397
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400398 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
399 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400400}
401
402/*
Ian Dalld7371c42009-03-10 20:33:22 -0400403 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400404 * by comparing (only) relevant fields, excluding the port number.
405 */
406static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
407 const struct sockaddr *sa2)
408{
409 if (sa1->sa_family != sa2->sa_family)
410 return 0;
411
412 switch (sa1->sa_family) {
413 case AF_INET:
414 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
415 case AF_INET6:
416 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
417 }
418 return 0;
419}
420
421/*
422 * Test if two socket addresses represent the same actual socket,
423 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400424 */
425static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
426 const struct sockaddr *sa2)
427{
428 if (sa1->sa_family != sa2->sa_family)
429 return 0;
430
431 switch (sa1->sa_family) {
432 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400433 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400434 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400435 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400436 }
437 return 0;
438}
439
Andy Adamsonc36fca52011-01-06 02:04:32 +0000440/* Common match routine for v4.0 and v4.1 callback services */
441bool
442nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
443 u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500444{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000445 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500446
Andy Adamsonc36fca52011-01-06 02:04:32 +0000447 /* Don't match clients that failed to initialise */
448 if (!(clp->cl_cons_state == NFS_CS_READY ||
449 clp->cl_cons_state == NFS_CS_SESSION_INITING))
450 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500451
Andy Adamsonc36fca52011-01-06 02:04:32 +0000452 /* Match the version and minorversion */
453 if (clp->rpc_ops->version != 4 ||
454 clp->cl_minorversion != minorversion)
455 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500456
Andy Adamsonc36fca52011-01-06 02:04:32 +0000457 /* Match only the IP address, not the port number */
458 if (!nfs_sockaddr_match_ipaddr(addr, clap))
459 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500460
Andy Adamsonc36fca52011-01-06 02:04:32 +0000461 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500462}
463
464/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500465 * Find an nfs_client on the list that matches the initialisation data
466 * that is supplied.
467 */
468static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400469{
470 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400471 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000472 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400473
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000474 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400475 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700476 /* Don't match clients that failed to initialise properly */
477 if (clp->cl_cons_state < 0)
478 continue;
479
David Howells24c8dbb2006-08-22 20:06:10 -0400480 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500481 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400482 continue;
483
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500484 if (clp->cl_proto != data->proto)
485 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400486 /* Match nfsv4 minorversion */
487 if (clp->cl_minorversion != data->minorversion)
488 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500489 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400490 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400491 continue;
492
Trond Myklebustc81468a2007-12-14 14:56:05 -0500493 atomic_inc(&clp->cl_count);
494 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400495 }
David Howells24c8dbb2006-08-22 20:06:10 -0400496 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400497}
498
499/*
500 * Look up a client by IP address and protocol version
501 * - creates a new record if one doesn't yet exist
502 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000503static struct nfs_client *
504nfs_get_client(const struct nfs_client_initdata *cl_init,
505 const struct rpc_timeout *timeparms,
506 const char *ip_addr,
507 rpc_authflavor_t authflavour,
508 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400509{
510 struct nfs_client *clp, *new = NULL;
511 int error;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000512 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400513
Chuck Leverd7422c42007-12-10 14:58:51 -0500514 dprintk("--> nfs_get_client(%s,v%u)\n",
515 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400516
517 /* see if the client already exists */
518 do {
519 spin_lock(&nfs_client_lock);
520
Trond Myklebustc81468a2007-12-14 14:56:05 -0500521 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400522 if (clp)
523 goto found_client;
524 if (new)
525 goto install_client;
526
527 spin_unlock(&nfs_client_lock);
528
Trond Myklebust3a498022007-12-14 14:56:04 -0500529 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700530 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400531
Chuck Levera21bdd92009-06-17 18:02:10 -0700532 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
533 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400534
535 /* install a new client and return with it unready */
536install_client:
537 clp = new;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000538 list_add(&clp->cl_share_link, &nn->nfs_client_list);
David Howells24c8dbb2006-08-22 20:06:10 -0400539 spin_unlock(&nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000540
541 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
542 authflavour, noresvport);
543 if (error < 0) {
544 nfs_put_client(clp);
545 return ERR_PTR(error);
546 }
David Howells24c8dbb2006-08-22 20:06:10 -0400547 dprintk("--> nfs_get_client() = %p [new]\n", clp);
548 return clp;
549
550 /* found an existing client
551 * - make sure it's ready before returning
552 */
553found_client:
554 spin_unlock(&nfs_client_lock);
555
556 if (new)
557 nfs_free_client(new);
558
Matthew Wilcox150030b2007-12-06 16:24:39 -0500559 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400560 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400561 if (error < 0) {
562 nfs_put_client(clp);
563 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400564 }
565
566 if (clp->cl_cons_state < NFS_CS_READY) {
567 error = clp->cl_cons_state;
568 nfs_put_client(clp);
569 return ERR_PTR(error);
570 }
571
David Howells54ceac42006-08-22 20:06:13 -0400572 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
573
David Howells24c8dbb2006-08-22 20:06:10 -0400574 dprintk("--> nfs_get_client() = %p [share]\n", clp);
575 return clp;
576}
577
578/*
579 * Mark a server as ready or failed
580 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400581void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400582{
583 clp->cl_cons_state = state;
584 wake_up_all(&nfs_client_active_wq);
585}
David Howells5006a762006-08-22 20:06:12 -0400586
587/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400588 * With sessions, the client is not marked ready until after a
589 * successful EXCHANGE_ID and CREATE_SESSION.
590 *
591 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
592 * other versions of NFS can be tried.
593 */
594int nfs4_check_client_ready(struct nfs_client *clp)
595{
596 if (!nfs4_has_session(clp))
597 return 0;
598 if (clp->cl_cons_state < NFS_CS_READY)
599 return -EPROTONOSUPPORT;
600 return 0;
601}
602
603/*
David Howells5006a762006-08-22 20:06:12 -0400604 * Initialise the timeout values for a connection
605 */
606static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
607 unsigned int timeo, unsigned int retrans)
608{
609 to->to_initval = timeo * HZ / 10;
610 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400611
612 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400613 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400614 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400615 if (to->to_retries == 0)
616 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500617 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400618 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400619 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
620 to->to_initval = NFS_MAX_TCP_TIMEOUT;
621 to->to_increment = to->to_initval;
622 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500623 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
624 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
625 if (to->to_maxval < to->to_initval)
626 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400627 to->to_exponential = 0;
628 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400629 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400630 if (to->to_retries == 0)
631 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400632 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400633 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400634 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
635 to->to_initval = NFS_MAX_UDP_TIMEOUT;
636 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
637 to->to_exponential = 1;
638 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400639 default:
640 BUG();
David Howells5006a762006-08-22 20:06:12 -0400641 }
642}
643
644/*
645 * Create an RPC client handle
646 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500647static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500648 const struct rpc_timeout *timeparms,
649 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500650 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400651{
David Howells5006a762006-08-22 20:06:12 -0400652 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400653 struct rpc_create_args args = {
Stanislav Kinsbursky6d59b8d2012-01-10 16:12:54 +0400654 .net = clp->net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500655 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400656 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500657 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500658 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400659 .servername = clp->cl_hostname,
660 .program = &nfs_program,
661 .version = clp->rpc_ops->version,
662 .authflavor = flavor,
663 };
David Howells5006a762006-08-22 20:06:12 -0400664
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500665 if (discrtry)
666 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
667 if (noresvport)
668 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
669
David Howells5006a762006-08-22 20:06:12 -0400670 if (!IS_ERR(clp->cl_rpcclient))
671 return 0;
672
Chuck Lever41877d22006-08-22 20:06:20 -0400673 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400674 if (IS_ERR(clnt)) {
675 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700676 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400677 return PTR_ERR(clnt);
678 }
679
David Howells5006a762006-08-22 20:06:12 -0400680 clp->cl_rpcclient = clnt;
681 return 0;
682}
David Howells54ceac42006-08-22 20:06:13 -0400683
684/*
685 * Version 2 or 3 client destruction
686 */
687static void nfs_destroy_server(struct nfs_server *server)
688{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400689 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
690 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500691 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400692}
693
694/*
695 * Version 2 or 3 lockd setup
696 */
697static int nfs_start_lockd(struct nfs_server *server)
698{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500699 struct nlm_host *host;
700 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500701 struct nlmclnt_initdata nlm_init = {
702 .hostname = clp->cl_hostname,
703 .address = (struct sockaddr *)&clp->cl_addr,
704 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500705 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500706 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
707 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500708 };
David Howells54ceac42006-08-22 20:06:13 -0400709
Chuck Lever883bb162008-01-15 16:04:20 -0500710 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500711 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400712 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
713 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500714 return 0;
715
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400716 switch (clp->cl_proto) {
717 default:
718 nlm_init.protocol = IPPROTO_TCP;
719 break;
720 case XPRT_TRANSPORT_UDP:
721 nlm_init.protocol = IPPROTO_UDP;
722 }
723
Chuck Lever883bb162008-01-15 16:04:20 -0500724 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500725 if (IS_ERR(host))
726 return PTR_ERR(host);
727
728 server->nlm_host = host;
729 server->destroy = nfs_destroy_server;
730 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400731}
732
733/*
734 * Initialise an NFSv3 ACL client connection
735 */
736#ifdef CONFIG_NFS_V3_ACL
737static void nfs_init_server_aclclient(struct nfs_server *server)
738{
Trond Myklebust40c553192007-12-14 14:56:07 -0500739 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400740 goto out_noacl;
741 if (server->flags & NFS_MOUNT_NOACL)
742 goto out_noacl;
743
744 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
745 if (IS_ERR(server->client_acl))
746 goto out_noacl;
747
748 /* No errors! Assume that Sun nfsacls are supported */
749 server->caps |= NFS_CAP_ACLS;
750 return;
751
752out_noacl:
753 server->caps &= ~NFS_CAP_ACLS;
754}
755#else
756static inline void nfs_init_server_aclclient(struct nfs_server *server)
757{
758 server->flags &= ~NFS_MOUNT_NOACL;
759 server->caps &= ~NFS_CAP_ACLS;
760}
761#endif
762
763/*
764 * Create a general RPC client
765 */
Trond Myklebust33170232007-12-20 16:03:59 -0500766static int nfs_init_server_rpcclient(struct nfs_server *server,
767 const struct rpc_timeout *timeo,
768 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400769{
770 struct nfs_client *clp = server->nfs_client;
771
772 server->client = rpc_clone_client(clp->cl_rpcclient);
773 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700774 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400775 return PTR_ERR(server->client);
776 }
777
Trond Myklebust33170232007-12-20 16:03:59 -0500778 memcpy(&server->client->cl_timeout_default,
779 timeo,
780 sizeof(server->client->cl_timeout_default));
781 server->client->cl_timeout = &server->client->cl_timeout_default;
782
David Howells54ceac42006-08-22 20:06:13 -0400783 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
784 struct rpc_auth *auth;
785
786 auth = rpcauth_create(pseudoflavour, server->client);
787 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700788 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400789 return PTR_ERR(auth);
790 }
791 }
792 server->client->cl_softrtry = 0;
793 if (server->flags & NFS_MOUNT_SOFT)
794 server->client->cl_softrtry = 1;
795
David Howells54ceac42006-08-22 20:06:13 -0400796 return 0;
797}
798
799/*
800 * Initialise an NFS2 or NFS3 client
801 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000802int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
803 const char *ip_addr, rpc_authflavor_t authflavour,
804 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400805{
David Howells54ceac42006-08-22 20:06:13 -0400806 int error;
807
808 if (clp->cl_cons_state == NFS_CS_READY) {
809 /* the client is already initialised */
810 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
811 return 0;
812 }
813
David Howells54ceac42006-08-22 20:06:13 -0400814 /*
815 * Create a client RPC handle for doing FSSTAT with UNIX auth only
816 * - RFC 2623, sec 2.3.2
817 */
Chuck Leverd7403512008-12-23 15:21:37 -0500818 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000819 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400820 if (error < 0)
821 goto error;
822 nfs_mark_client_ready(clp, NFS_CS_READY);
823 return 0;
824
825error:
826 nfs_mark_client_ready(clp, error);
827 dprintk("<-- nfs_init_client() = xerror %d\n", error);
828 return error;
829}
830
831/*
832 * Create a version 2 or 3 client
833 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400834static int nfs_init_server(struct nfs_server *server,
835 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400836{
Trond Myklebust3a498022007-12-14 14:56:04 -0500837 struct nfs_client_initdata cl_init = {
838 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500839 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500840 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500841 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500842 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400843 .net = data->net,
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
Andy Adamson45a52a02011-03-01 01:34:08 +0000856 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
857 data->timeo, data->retrans);
858
David Howells54ceac42006-08-22 20:06:13 -0400859 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000860 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
861 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400862 if (IS_ERR(clp)) {
863 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
864 return PTR_ERR(clp);
865 }
866
David Howells54ceac42006-08-22 20:06:13 -0400867 server->nfs_client = clp;
868
869 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400870 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100871 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400872 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
873 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
874 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400875
876 if (data->rsize)
877 server->rsize = nfs_block_size(data->rsize, NULL);
878 if (data->wsize)
879 server->wsize = nfs_block_size(data->wsize, NULL);
880
881 server->acregmin = data->acregmin * HZ;
882 server->acregmax = data->acregmax * HZ;
883 server->acdirmin = data->acdirmin * HZ;
884 server->acdirmax = data->acdirmax * HZ;
885
886 /* Start lockd here, before we might error out */
887 error = nfs_start_lockd(server);
888 if (error < 0)
889 goto error;
890
Chuck Leverf22d6d72008-03-14 14:10:22 -0400891 server->port = data->nfs_server.port;
892
Trond Myklebust33170232007-12-20 16:03:59 -0500893 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400894 if (error < 0)
895 goto error;
896
Chuck Lever3f8400d2008-03-14 14:10:30 -0400897 /* Preserve the values of mount_server-related mount options */
898 if (data->mount_server.addrlen) {
899 memcpy(&server->mountd_address, &data->mount_server.address,
900 data->mount_server.addrlen);
901 server->mountd_addrlen = data->mount_server.addrlen;
902 }
903 server->mountd_version = data->mount_server.version;
904 server->mountd_port = data->mount_server.port;
905 server->mountd_protocol = data->mount_server.protocol;
906
David Howells54ceac42006-08-22 20:06:13 -0400907 server->namelen = data->namlen;
908 /* Create a client RPC handle for the NFSv3 ACL management interface */
909 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400910 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
911 return 0;
912
913error:
914 server->nfs_client = NULL;
915 nfs_put_client(clp);
916 dprintk("<-- nfs_init_server() = xerror %d\n", error);
917 return error;
918}
919
920/*
921 * Load up the server record from information gained in an fsinfo record
922 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400923static void nfs_server_set_fsinfo(struct nfs_server *server,
924 struct nfs_fh *mntfh,
925 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400926{
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;
Fred Isamandae100c2011-07-30 20:52:37 -0400955 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400956 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400957
David Howells54ceac42006-08-22 20:06:13 -0400958 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
959
960 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400961 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
962 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400963 if (server->dtsize > server->rsize)
964 server->dtsize = server->rsize;
965
966 if (server->flags & NFS_MOUNT_NOAC) {
967 server->acregmin = server->acregmax = 0;
968 server->acdirmin = server->acdirmax = 0;
969 }
970
971 server->maxfilesize = fsinfo->maxfilesize;
972
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700973 server->time_delta = fsinfo->time_delta;
974
David Howells54ceac42006-08-22 20:06:13 -0400975 /* We're airborne Set socket buffersize */
976 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
977}
978
979/*
980 * Probe filesystem information, including the FSID on v2/v3
981 */
982static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
983{
984 struct nfs_fsinfo fsinfo;
985 struct nfs_client *clp = server->nfs_client;
986 int error;
987
988 dprintk("--> nfs_probe_fsinfo()\n");
989
990 if (clp->rpc_ops->set_capabilities != NULL) {
991 error = clp->rpc_ops->set_capabilities(server, mntfh);
992 if (error < 0)
993 goto out_error;
994 }
995
996 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400997 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400998 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
999 if (error < 0)
1000 goto out_error;
1001
Benny Halevy738fd0f32011-07-30 20:52:36 -04001002 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001003
1004 /* Get some general file system info */
1005 if (server->namelen == 0) {
1006 struct nfs_pathconf pathinfo;
1007
1008 pathinfo.fattr = fattr;
1009 nfs_fattr_init(fattr);
1010
1011 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1012 server->namelen = pathinfo.max_namelen;
1013 }
1014
1015 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1016 return 0;
1017
1018out_error:
1019 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1020 return error;
1021}
1022
1023/*
1024 * Copy useful information when duplicating a server record
1025 */
1026static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1027{
1028 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001029 target->rsize = source->rsize;
1030 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001031 target->acregmin = source->acregmin;
1032 target->acregmax = source->acregmax;
1033 target->acdirmin = source->acdirmin;
1034 target->acdirmax = source->acdirmax;
1035 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001036 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001037}
1038
Chuck Leverfca52382010-12-24 01:32:32 +00001039static void nfs_server_insert_lists(struct nfs_server *server)
1040{
1041 struct nfs_client *clp = server->nfs_client;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001042 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001043
1044 spin_lock(&nfs_client_lock);
1045 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001046 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001047 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001048 spin_unlock(&nfs_client_lock);
1049
1050}
1051
1052static void nfs_server_remove_lists(struct nfs_server *server)
1053{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001054 struct nfs_client *clp = server->nfs_client;
1055
Chuck Leverfca52382010-12-24 01:32:32 +00001056 spin_lock(&nfs_client_lock);
1057 list_del_rcu(&server->client_link);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001058 if (clp && list_empty(&clp->cl_superblocks))
1059 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001060 list_del(&server->master_link);
1061 spin_unlock(&nfs_client_lock);
1062
1063 synchronize_rcu();
1064}
1065
David Howells54ceac42006-08-22 20:06:13 -04001066/*
1067 * Allocate and initialise a server record
1068 */
1069static struct nfs_server *nfs_alloc_server(void)
1070{
1071 struct nfs_server *server;
1072
1073 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1074 if (!server)
1075 return NULL;
1076
1077 server->client = server->client_acl = ERR_PTR(-EINVAL);
1078
1079 /* Zero out the NFS state stuff */
1080 INIT_LIST_HEAD(&server->client_link);
1081 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001082 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001083 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001084 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001085
Steve Dicksonef818a22007-11-08 04:05:04 -05001086 atomic_set(&server->active, 0);
1087
David Howells54ceac42006-08-22 20:06:13 -04001088 server->io_stats = nfs_alloc_iostats();
1089 if (!server->io_stats) {
1090 kfree(server);
1091 return NULL;
1092 }
1093
Jens Axboe48d07642009-09-21 09:59:39 +02001094 if (bdi_init(&server->backing_dev_info)) {
1095 nfs_free_iostats(server->io_stats);
1096 kfree(server);
1097 return NULL;
1098 }
1099
Trond Myklebust9157c312012-01-17 22:04:24 -05001100 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001101 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001102 pnfs_init_server(server);
1103
David Howells54ceac42006-08-22 20:06:13 -04001104 return server;
1105}
1106
1107/*
1108 * Free up a server record
1109 */
1110void nfs_free_server(struct nfs_server *server)
1111{
1112 dprintk("--> nfs_free_server()\n");
1113
Chuck Leverfca52382010-12-24 01:32:32 +00001114 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001115 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001116
1117 if (server->destroy != NULL)
1118 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001119
1120 if (!IS_ERR(server->client_acl))
1121 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001122 if (!IS_ERR(server->client))
1123 rpc_shutdown_client(server->client);
1124
1125 nfs_put_client(server->nfs_client);
1126
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001127 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001128 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001129 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001130 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001131 kfree(server);
1132 nfs_release_automount_timer();
1133 dprintk("<-- nfs_free_server()\n");
1134}
1135
1136/*
1137 * Create a version 2 or 3 volume record
1138 * - keyed on server and FSID
1139 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001140struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001141 struct nfs_fh *mntfh)
1142{
1143 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001144 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001145 int error;
1146
1147 server = nfs_alloc_server();
1148 if (!server)
1149 return ERR_PTR(-ENOMEM);
1150
Trond Myklebustfbca7792010-04-16 16:22:46 -04001151 error = -ENOMEM;
1152 fattr = nfs_alloc_fattr();
1153 if (fattr == NULL)
1154 goto error;
1155
David Howells54ceac42006-08-22 20:06:13 -04001156 /* Get a client representation */
1157 error = nfs_init_server(server, data);
1158 if (error < 0)
1159 goto error;
1160
1161 BUG_ON(!server->nfs_client);
1162 BUG_ON(!server->nfs_client->rpc_ops);
1163 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1164
1165 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001166 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001167 if (error < 0)
1168 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001169 if (server->nfs_client->rpc_ops->version == 3) {
1170 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1171 server->namelen = NFS3_MAXNAMLEN;
1172 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1173 server->caps |= NFS_CAP_READDIRPLUS;
1174 } else {
1175 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1176 server->namelen = NFS2_MAXNAMLEN;
1177 }
1178
Trond Myklebustfbca7792010-04-16 16:22:46 -04001179 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1180 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001181 if (error < 0) {
1182 dprintk("nfs_create_server: getattr error = %d\n", -error);
1183 goto error;
1184 }
1185 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001186 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001187
David Howells6daabf12006-08-24 15:44:16 -04001188 dprintk("Server FSID: %llx:%llx\n",
1189 (unsigned long long) server->fsid.major,
1190 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001191
Chuck Leverfca52382010-12-24 01:32:32 +00001192 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001193 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001194 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001195 return server;
1196
1197error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001198 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001199 nfs_free_server(server);
1200 return ERR_PTR(error);
1201}
1202
1203#ifdef CONFIG_NFS_V4
1204/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001205 * NFSv4.0 callback thread helper
1206 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001207 * Find a client by callback identifier
1208 */
1209struct nfs_client *
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001210nfs4_find_client_ident(struct net *net, int cb_ident)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001211{
1212 struct nfs_client *clp;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001213 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001214
1215 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001216 clp = idr_find(&nn->cb_ident_idr, cb_ident);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001217 if (clp)
1218 atomic_inc(&clp->cl_count);
1219 spin_unlock(&nfs_client_lock);
1220 return clp;
1221}
1222
1223#if defined(CONFIG_NFS_V4_1)
1224/*
1225 * NFSv4.1 callback thread helper
1226 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1227 * minorversion, and sessionID
1228 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001229 * Returns NULL if no such client
1230 */
1231struct nfs_client *
1232nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001233 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001234{
1235 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001236 struct nfs_net *nn = net_generic(&init_net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001237
1238 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001239 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001240 if (nfs4_cb_match_client(addr, clp, 1) == false)
1241 continue;
1242
1243 if (!nfs4_has_session(clp))
1244 continue;
1245
Andy Adamson778be232011-01-25 15:38:01 +00001246 /* Match sessionid*/
1247 if (memcmp(clp->cl_session->sess_id.data,
1248 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001249 continue;
1250
1251 atomic_inc(&clp->cl_count);
1252 spin_unlock(&nfs_client_lock);
1253 return clp;
1254 }
1255 spin_unlock(&nfs_client_lock);
1256 return NULL;
1257}
1258
1259#else /* CONFIG_NFS_V4_1 */
1260
1261struct nfs_client *
1262nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001263 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001264{
1265 return NULL;
1266}
1267#endif /* CONFIG_NFS_V4_1 */
1268
1269/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001270 * Initialize the NFS4 callback service
1271 */
1272static int nfs4_init_callback(struct nfs_client *clp)
1273{
1274 int error;
1275
1276 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001277 if (nfs4_has_session(clp)) {
1278 error = xprt_setup_backchannel(
1279 clp->cl_rpcclient->cl_xprt,
1280 NFS41_BC_MIN_CALLBACKS);
1281 if (error < 0)
1282 return error;
1283 }
1284
Trond Myklebust97dc1352010-06-16 09:52:26 -04001285 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001286 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001287 if (error < 0) {
1288 dprintk("%s: failed to start callback. Error = %d\n",
1289 __func__, error);
1290 return error;
1291 }
1292 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1293 }
1294 return 0;
1295}
1296
1297/*
Andy Adamson557134a2009-04-01 09:21:53 -04001298 * Initialize the minor version specific parts of an NFS4 client record
1299 */
1300static int nfs4_init_client_minor_version(struct nfs_client *clp)
1301{
1302#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001303 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001304 struct nfs4_session *session = NULL;
1305 /*
1306 * Create the session and mark it expired.
1307 * When a SEQUENCE operation encounters the expired session
1308 * it will do session recovery to initialize it.
1309 */
1310 session = nfs4_alloc_session(clp);
1311 if (!session)
1312 return -ENOMEM;
1313
1314 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001315 /*
1316 * The create session reply races with the server back
1317 * channel probe. Mark the client NFS_CS_SESSION_INITING
1318 * so that the client back channel can find the
1319 * nfs_client struct
1320 */
1321 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001322 }
1323#endif /* CONFIG_NFS_V4_1 */
1324
Benny Halevy71468512009-04-01 09:22:56 -04001325 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001326}
1327
1328/*
David Howells54ceac42006-08-22 20:06:13 -04001329 * Initialise an NFS4 client record
1330 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001331int nfs4_init_client(struct nfs_client *clp,
1332 const struct rpc_timeout *timeparms,
1333 const char *ip_addr,
1334 rpc_authflavor_t authflavour,
1335 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001336{
1337 int error;
1338
1339 if (clp->cl_cons_state == NFS_CS_READY) {
1340 /* the client is initialised already */
1341 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1342 return 0;
1343 }
1344
1345 /* Check NFS protocol revision and initialize RPC op vector */
1346 clp->rpc_ops = &nfs_v4_clientops;
1347
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001348 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001349 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001350 if (error < 0)
1351 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001352 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001353
1354 error = nfs_idmap_new(clp);
1355 if (error < 0) {
1356 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001357 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001358 goto error;
1359 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001360 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001361
Andy Adamson557134a2009-04-01 09:21:53 -04001362 error = nfs4_init_client_minor_version(clp);
1363 if (error < 0)
1364 goto error;
1365
Andy Adamson76db6d92009-04-01 09:22:38 -04001366 if (!nfs4_has_session(clp))
1367 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001368 return 0;
1369
1370error:
1371 nfs_mark_client_ready(clp, error);
1372 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1373 return error;
1374}
1375
1376/*
1377 * Set up an NFS4 client
1378 */
1379static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001380 const char *hostname,
1381 const struct sockaddr *addr,
1382 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001383 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001384 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001385 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001386 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001387{
Trond Myklebust3a498022007-12-14 14:56:04 -05001388 struct nfs_client_initdata cl_init = {
1389 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001390 .addr = addr,
1391 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001392 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001393 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001394 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001395 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001396 };
David Howells54ceac42006-08-22 20:06:13 -04001397 struct nfs_client *clp;
1398 int error;
1399
1400 dprintk("--> nfs4_set_client()\n");
1401
1402 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001403 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1404 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001405 if (IS_ERR(clp)) {
1406 error = PTR_ERR(clp);
1407 goto error;
1408 }
David Howells54ceac42006-08-22 20:06:13 -04001409
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001410 /*
1411 * Query for the lease time on clientid setup or renewal
1412 *
1413 * Note that this will be set on nfs_clients that were created
1414 * only for the DS role and did not set this bit, but now will
1415 * serve a dual role.
1416 */
1417 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1418
David Howells54ceac42006-08-22 20:06:13 -04001419 server->nfs_client = clp;
1420 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1421 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001422error:
1423 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1424 return error;
1425}
1426
Andy Adamsond83217c2011-03-01 01:34:17 +00001427/*
1428 * Set up a pNFS Data Server client.
1429 *
1430 * Return any existing nfs_client that matches server address,port,version
1431 * and minorversion.
1432 *
1433 * For a new nfs_client, use a soft mount (default), a low retrans and a
1434 * low timeout interval so that if a connection is lost, we retry through
1435 * the MDS.
1436 */
1437struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1438 const struct sockaddr *ds_addr,
1439 int ds_addrlen, int ds_proto)
1440{
1441 struct nfs_client_initdata cl_init = {
1442 .addr = ds_addr,
1443 .addrlen = ds_addrlen,
1444 .rpc_ops = &nfs_v4_clientops,
1445 .proto = ds_proto,
1446 .minorversion = mds_clp->cl_minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001447 .net = mds_clp->net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001448 };
1449 struct rpc_timeout ds_timeout = {
1450 .to_initval = 15 * HZ,
1451 .to_maxval = 15 * HZ,
1452 .to_retries = 1,
1453 .to_exponential = 1,
1454 };
1455 struct nfs_client *clp;
1456
1457 /*
1458 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1459 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1460 * (section 13.1 RFC 5661).
1461 */
1462 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1463 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1464
1465 dprintk("<-- %s %p\n", __func__, clp);
1466 return clp;
1467}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001468EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001469
1470/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001471 * Session has been established, and the client marked ready.
1472 * Set the mount rsize and wsize with negotiated fore channel
1473 * attributes which will be bound checked in nfs_server_set_fsinfo.
1474 */
1475static void nfs4_session_set_rwsize(struct nfs_server *server)
1476{
1477#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001478 struct nfs4_session *sess;
1479 u32 server_resp_sz;
1480 u32 server_rqst_sz;
1481
Andy Adamson96b09e02009-04-01 09:22:33 -04001482 if (!nfs4_has_session(server->nfs_client))
1483 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001484 sess = server->nfs_client->cl_session;
1485 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1486 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1487
1488 if (server->rsize > server_resp_sz)
1489 server->rsize = server_resp_sz;
1490 if (server->wsize > server_rqst_sz)
1491 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001492#endif /* CONFIG_NFS_V4_1 */
1493}
1494
Trond Myklebust44950b62010-06-17 11:45:12 -04001495static int nfs4_server_common_setup(struct nfs_server *server,
1496 struct nfs_fh *mntfh)
1497{
1498 struct nfs_fattr *fattr;
1499 int error;
1500
1501 BUG_ON(!server->nfs_client);
1502 BUG_ON(!server->nfs_client->rpc_ops);
1503 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1504
Andy Adamson94de8b22011-03-01 01:34:12 +00001505 /* data servers support only a subset of NFSv4.1 */
1506 if (is_ds_only_client(server->nfs_client))
1507 return -EPROTONOSUPPORT;
1508
Trond Myklebust44950b62010-06-17 11:45:12 -04001509 fattr = nfs_alloc_fattr();
1510 if (fattr == NULL)
1511 return -ENOMEM;
1512
1513 /* We must ensure the session is initialised first */
1514 error = nfs4_init_session(server);
1515 if (error < 0)
1516 goto out;
1517
1518 /* Probe the root fh to retrieve its FSID and filehandle */
1519 error = nfs4_get_rootfh(server, mntfh);
1520 if (error < 0)
1521 goto out;
1522
1523 dprintk("Server FSID: %llx:%llx\n",
1524 (unsigned long long) server->fsid.major,
1525 (unsigned long long) server->fsid.minor);
1526 dprintk("Mount FH: %d\n", mntfh->size);
1527
1528 nfs4_session_set_rwsize(server);
1529
1530 error = nfs_probe_fsinfo(server, mntfh, fattr);
1531 if (error < 0)
1532 goto out;
1533
1534 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1535 server->namelen = NFS4_MAXNAMLEN;
1536
Chuck Leverfca52382010-12-24 01:32:32 +00001537 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001538 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001539 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001540out:
1541 nfs_free_fattr(fattr);
1542 return error;
1543}
1544
Andy Adamson96b09e02009-04-01 09:22:33 -04001545/*
David Howells54ceac42006-08-22 20:06:13 -04001546 * Create a version 4 volume record
1547 */
1548static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001549 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001550{
Trond Myklebust33170232007-12-20 16:03:59 -05001551 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001552 int error;
1553
1554 dprintk("--> nfs4_init_server()\n");
1555
Trond Myklebust33170232007-12-20 16:03:59 -05001556 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1557 data->timeo, data->retrans);
1558
Chuck Lever542fcc32008-12-23 15:21:36 -05001559 /* Initialise the client representation from the mount data */
1560 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001561 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1562 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1563 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001564 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001565
Trond Myklebust33170232007-12-20 16:03:59 -05001566 /* Get a client record */
1567 error = nfs4_set_client(server,
1568 data->nfs_server.hostname,
1569 (const struct sockaddr *)&data->nfs_server.address,
1570 data->nfs_server.addrlen,
1571 data->client_address,
1572 data->auth_flavors[0],
1573 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001574 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001575 data->minorversion,
1576 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001577 if (error < 0)
1578 goto error;
1579
Trond Myklebustb064eca22011-02-22 15:44:32 -08001580 /*
1581 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1582 * authentication.
1583 */
1584 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1585 server->caps |= NFS_CAP_UIDGID_NOMAP;
1586
David Howells54ceac42006-08-22 20:06:13 -04001587 if (data->rsize)
1588 server->rsize = nfs_block_size(data->rsize, NULL);
1589 if (data->wsize)
1590 server->wsize = nfs_block_size(data->wsize, NULL);
1591
1592 server->acregmin = data->acregmin * HZ;
1593 server->acregmax = data->acregmax * HZ;
1594 server->acdirmin = data->acdirmin * HZ;
1595 server->acdirmax = data->acdirmax * HZ;
1596
Chuck Leverf22d6d72008-03-14 14:10:22 -04001597 server->port = data->nfs_server.port;
1598
Trond Myklebust33170232007-12-20 16:03:59 -05001599 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001600
Trond Myklebust33170232007-12-20 16:03:59 -05001601error:
David Howells54ceac42006-08-22 20:06:13 -04001602 /* Done */
1603 dprintk("<-- nfs4_init_server() = %d\n", error);
1604 return error;
1605}
1606
1607/*
1608 * Create a version 4 volume record
1609 * - keyed on server and FSID
1610 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001611struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001612 struct nfs_fh *mntfh)
1613{
David Howells54ceac42006-08-22 20:06:13 -04001614 struct nfs_server *server;
1615 int error;
1616
1617 dprintk("--> nfs4_create_server()\n");
1618
1619 server = nfs_alloc_server();
1620 if (!server)
1621 return ERR_PTR(-ENOMEM);
1622
David Howells54ceac42006-08-22 20:06:13 -04001623 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001624 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001625 if (error < 0)
1626 goto error;
1627
Trond Myklebust44950b62010-06-17 11:45:12 -04001628 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001629 if (error < 0)
1630 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001631
David Howells54ceac42006-08-22 20:06:13 -04001632 dprintk("<-- nfs4_create_server() = %p\n", server);
1633 return server;
1634
1635error:
1636 nfs_free_server(server);
1637 dprintk("<-- nfs4_create_server() = error %d\n", error);
1638 return ERR_PTR(error);
1639}
1640
1641/*
1642 * Create an NFS4 referral server record
1643 */
1644struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001645 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001646{
1647 struct nfs_client *parent_client;
1648 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001649 int error;
1650
1651 dprintk("--> nfs4_create_referral_server()\n");
1652
1653 server = nfs_alloc_server();
1654 if (!server)
1655 return ERR_PTR(-ENOMEM);
1656
1657 parent_server = NFS_SB(data->sb);
1658 parent_client = parent_server->nfs_client;
1659
Chuck Lever542fcc32008-12-23 15:21:36 -05001660 /* Initialise the client representation from the parent server */
1661 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001662 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001663
David Howells54ceac42006-08-22 20:06:13 -04001664 /* Get a client representation.
1665 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001666 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001667 data->addr,
1668 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001669 parent_client->cl_ipaddr,
1670 data->authflavor,
1671 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001672 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001673 parent_client->cl_mvops->minor_version,
1674 parent_client->net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001675 if (error < 0)
1676 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001677
Trond Myklebust33170232007-12-20 16:03:59 -05001678 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001679 if (error < 0)
1680 goto error;
1681
Trond Myklebust44950b62010-06-17 11:45:12 -04001682 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001683 if (error < 0)
1684 goto error;
1685
David Howells54ceac42006-08-22 20:06:13 -04001686 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1687 return server;
1688
1689error:
1690 nfs_free_server(server);
1691 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1692 return ERR_PTR(error);
1693}
1694
1695#endif /* CONFIG_NFS_V4 */
1696
1697/*
1698 * Clone an NFS2, NFS3 or NFS4 server record
1699 */
1700struct nfs_server *nfs_clone_server(struct nfs_server *source,
1701 struct nfs_fh *fh,
1702 struct nfs_fattr *fattr)
1703{
1704 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001705 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001706 int error;
1707
1708 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001709 (unsigned long long) fattr->fsid.major,
1710 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001711
1712 server = nfs_alloc_server();
1713 if (!server)
1714 return ERR_PTR(-ENOMEM);
1715
Trond Myklebustfbca7792010-04-16 16:22:46 -04001716 error = -ENOMEM;
1717 fattr_fsinfo = nfs_alloc_fattr();
1718 if (fattr_fsinfo == NULL)
1719 goto out_free_server;
1720
David Howells54ceac42006-08-22 20:06:13 -04001721 /* Copy data from the source */
1722 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001723 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001724 atomic_inc(&server->nfs_client->cl_count);
1725 nfs_server_copy_userdata(server, source);
1726
1727 server->fsid = fattr->fsid;
1728
Trond Myklebust33170232007-12-20 16:03:59 -05001729 error = nfs_init_server_rpcclient(server,
1730 source->client->cl_timeout,
1731 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001732 if (error < 0)
1733 goto out_free_server;
1734 if (!IS_ERR(source->client_acl))
1735 nfs_init_server_aclclient(server);
1736
1737 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001738 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001739 if (error < 0)
1740 goto out_free_server;
1741
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001742 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1743 server->namelen = NFS4_MAXNAMLEN;
1744
David Howells54ceac42006-08-22 20:06:13 -04001745 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001746 (unsigned long long) server->fsid.major,
1747 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001748
1749 error = nfs_start_lockd(server);
1750 if (error < 0)
1751 goto out_free_server;
1752
Chuck Leverfca52382010-12-24 01:32:32 +00001753 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001754 server->mount_time = jiffies;
1755
Trond Myklebustfbca7792010-04-16 16:22:46 -04001756 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001757 dprintk("<-- nfs_clone_server() = %p\n", server);
1758 return server;
1759
1760out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001761 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001762 nfs_free_server(server);
1763 dprintk("<-- nfs_clone_server() = error %d\n", error);
1764 return ERR_PTR(error);
1765}
David Howells6aaca562006-08-22 20:06:13 -04001766
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001767void nfs_clients_init(struct net *net)
1768{
1769 struct nfs_net *nn = net_generic(net, nfs_net_id);
1770
1771 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001772 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001773#ifdef CONFIG_NFS_V4
1774 idr_init(&nn->cb_ident_idr);
1775#endif
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001776}
1777
David Howells6aaca562006-08-22 20:06:13 -04001778#ifdef CONFIG_PROC_FS
1779static struct proc_dir_entry *proc_fs_nfs;
1780
1781static int nfs_server_list_open(struct inode *inode, struct file *file);
1782static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1783static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1784static void nfs_server_list_stop(struct seq_file *p, void *v);
1785static int nfs_server_list_show(struct seq_file *m, void *v);
1786
James Morris88e9d342009-09-22 16:43:43 -07001787static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001788 .start = nfs_server_list_start,
1789 .next = nfs_server_list_next,
1790 .stop = nfs_server_list_stop,
1791 .show = nfs_server_list_show,
1792};
1793
Arjan van de Ven00977a52007-02-12 00:55:34 -08001794static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001795 .open = nfs_server_list_open,
1796 .read = seq_read,
1797 .llseek = seq_lseek,
1798 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001799 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001800};
1801
1802static int nfs_volume_list_open(struct inode *inode, struct file *file);
1803static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1804static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1805static void nfs_volume_list_stop(struct seq_file *p, void *v);
1806static int nfs_volume_list_show(struct seq_file *m, void *v);
1807
James Morris88e9d342009-09-22 16:43:43 -07001808static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001809 .start = nfs_volume_list_start,
1810 .next = nfs_volume_list_next,
1811 .stop = nfs_volume_list_stop,
1812 .show = nfs_volume_list_show,
1813};
1814
Arjan van de Ven00977a52007-02-12 00:55:34 -08001815static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001816 .open = nfs_volume_list_open,
1817 .read = seq_read,
1818 .llseek = seq_lseek,
1819 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001820 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001821};
1822
1823/*
1824 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1825 * we're dealing
1826 */
1827static int nfs_server_list_open(struct inode *inode, struct file *file)
1828{
1829 struct seq_file *m;
1830 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001831 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1832 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001833
1834 ret = seq_open(file, &nfs_server_list_ops);
1835 if (ret < 0)
1836 return ret;
1837
1838 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001839 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001840
1841 return 0;
1842}
1843
1844/*
1845 * set up the iterator to start reading from the server list and return the first item
1846 */
1847static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1848{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001849 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1850
David Howells6aaca562006-08-22 20:06:13 -04001851 /* lock the list against modification */
1852 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001853 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001854}
1855
1856/*
1857 * move to next server
1858 */
1859static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1860{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001861 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1862
1863 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001864}
1865
1866/*
1867 * clean up after reading from the transports list
1868 */
1869static void nfs_server_list_stop(struct seq_file *p, void *v)
1870{
1871 spin_unlock(&nfs_client_lock);
1872}
1873
1874/*
1875 * display a header line followed by a load of call lines
1876 */
1877static int nfs_server_list_show(struct seq_file *m, void *v)
1878{
1879 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001880 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001881
1882 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001883 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001884 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1885 return 0;
1886 }
1887
1888 /* display one transport per line on subsequent lines */
1889 clp = list_entry(v, struct nfs_client, cl_share_link);
1890
Malahal Naineni940aab42011-09-20 17:27:14 -07001891 /* Check if the client is initialized */
1892 if (clp->cl_cons_state != NFS_CS_READY)
1893 return 0;
1894
Chuck Lever5d8515c2007-12-10 14:57:16 -05001895 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001896 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001897 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1898 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001899 atomic_read(&clp->cl_count),
1900 clp->cl_hostname);
1901
1902 return 0;
1903}
1904
1905/*
1906 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1907 */
1908static int nfs_volume_list_open(struct inode *inode, struct file *file)
1909{
1910 struct seq_file *m;
1911 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001912 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1913 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001914
1915 ret = seq_open(file, &nfs_volume_list_ops);
1916 if (ret < 0)
1917 return ret;
1918
1919 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001920 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001921
1922 return 0;
1923}
1924
1925/*
1926 * set up the iterator to start reading from the volume list and return the first item
1927 */
1928static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1929{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001930 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1931
David Howells6aaca562006-08-22 20:06:13 -04001932 /* lock the list against modification */
1933 spin_lock(&nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001934 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001935}
1936
1937/*
1938 * move to next volume
1939 */
1940static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1941{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001942 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1943
1944 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001945}
1946
1947/*
1948 * clean up after reading from the transports list
1949 */
1950static void nfs_volume_list_stop(struct seq_file *p, void *v)
1951{
1952 spin_unlock(&nfs_client_lock);
1953}
1954
1955/*
1956 * display a header line followed by a load of call lines
1957 */
1958static int nfs_volume_list_show(struct seq_file *m, void *v)
1959{
1960 struct nfs_server *server;
1961 struct nfs_client *clp;
1962 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001963 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001964
1965 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001966 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001967 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001968 return 0;
1969 }
1970 /* display one transport per line on subsequent lines */
1971 server = list_entry(v, struct nfs_server, master_link);
1972 clp = server->nfs_client;
1973
1974 snprintf(dev, 8, "%u:%u",
1975 MAJOR(server->s_dev), MINOR(server->s_dev));
1976
1977 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001978 (unsigned long long) server->fsid.major,
1979 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001980
David Howells5d1acff2009-04-03 16:42:47 +01001981 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001982 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001983 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1984 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001985 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001986 fsid,
1987 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001988
1989 return 0;
1990}
1991
1992/*
1993 * initialise the /proc/fs/nfsfs/ directory
1994 */
1995int __init nfs_fs_proc_init(void)
1996{
1997 struct proc_dir_entry *p;
1998
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001999 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002000 if (!proc_fs_nfs)
2001 goto error_0;
2002
David Howells6aaca562006-08-22 20:06:13 -04002003 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002004 p = proc_create("servers", S_IFREG|S_IRUGO,
2005 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002006 if (!p)
2007 goto error_1;
2008
David Howells6aaca562006-08-22 20:06:13 -04002009 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002010 p = proc_create("volumes", S_IFREG|S_IRUGO,
2011 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002012 if (!p)
2013 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002014 return 0;
2015
2016error_2:
2017 remove_proc_entry("servers", proc_fs_nfs);
2018error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002019 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002020error_0:
2021 return -ENOMEM;
2022}
2023
2024/*
2025 * clean up the /proc/fs/nfsfs/ directory
2026 */
2027void nfs_fs_proc_exit(void)
2028{
2029 remove_proc_entry("volumes", proc_fs_nfs);
2030 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002031 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002032}
2033
2034#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002035
2036module_param(nfs4_disable_idmapping, bool, 0644);
2037MODULE_PARM_DESC(nfs4_disable_idmapping,
2038 "Turn off NFSv4 idmapping when using 'sec=sys'");