blob: 1a5cd49dff800763a62063879a4fa24be94eeccc [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
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
Andy Adamsonf4eecd52011-01-06 02:04:30 +000060
61/*
62 * Get a unique NFSv4.0 callback identifier which will be used
63 * by the V4.0 callback service to lookup the nfs_client struct
64 */
65static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
66{
67 int ret = 0;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000068 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000069
70 if (clp->rpc_ops->version != 4 || minorversion != 0)
71 return ret;
72retry:
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000073 if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
Andy Adamsonf4eecd52011-01-06 02:04:30 +000074 return -ENOMEM;
Stanislav Kinsburskydc030852012-01-23 17:26:31 +000075 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +000076 ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +000077 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000078 if (ret == -EAGAIN)
79 goto retry;
80 return ret;
81}
82#endif /* CONFIG_NFS_V4 */
David Howells24c8dbb2006-08-22 20:06:10 -040083
84/*
Trond Myklebustb064eca22011-02-22 15:44:32 -080085 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
86 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103087static bool nfs4_disable_idmapping = true;
Trond Myklebustb064eca22011-02-22 15:44:32 -080088
89/*
David Howells5006a762006-08-22 20:06:12 -040090 * RPC cruft for NFS
91 */
Trond Myklebusta613fa12012-01-20 13:53:56 -050092static const struct rpc_version *nfs_version[5] = {
David Howells5006a762006-08-22 20:06:12 -040093 [2] = &nfs_version2,
94#ifdef CONFIG_NFS_V3
95 [3] = &nfs_version3,
96#endif
97#ifdef CONFIG_NFS_V4
98 [4] = &nfs_version4,
99#endif
100};
101
Trond Myklebusta613fa12012-01-20 13:53:56 -0500102const struct rpc_program nfs_program = {
David Howells5006a762006-08-22 20:06:12 -0400103 .name = "nfs",
104 .number = NFS_PROGRAM,
105 .nrvers = ARRAY_SIZE(nfs_version),
106 .version = nfs_version,
107 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400108 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -0400109};
110
111struct rpc_stat nfs_rpcstat = {
112 .program = &nfs_program
113};
114
115
116#ifdef CONFIG_NFS_V3_ACL
117static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
Trond Myklebusta613fa12012-01-20 13:53:56 -0500118static const struct rpc_version *nfsacl_version[] = {
David Howells5006a762006-08-22 20:06:12 -0400119 [3] = &nfsacl_version3,
120};
121
Trond Myklebusta613fa12012-01-20 13:53:56 -0500122const struct rpc_program nfsacl_program = {
David Howells5006a762006-08-22 20:06:12 -0400123 .name = "nfsacl",
124 .number = NFS_ACL_PROGRAM,
125 .nrvers = ARRAY_SIZE(nfsacl_version),
126 .version = nfsacl_version,
127 .stats = &nfsacl_rpcstat,
128};
129#endif /* CONFIG_NFS_V3_ACL */
130
Trond Myklebust3a498022007-12-14 14:56:04 -0500131struct nfs_client_initdata {
132 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500133 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500134 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500135 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500136 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400137 u32 minorversion;
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400138 struct net *net;
Trond Myklebust3a498022007-12-14 14:56:04 -0500139};
140
David Howells5006a762006-08-22 20:06:12 -0400141/*
David Howells24c8dbb2006-08-22 20:06:10 -0400142 * Allocate a shared client record
143 *
144 * Since these are allocated/deallocated very rarely, we don't
145 * bother putting them in a slab cache...
146 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500147static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400148{
149 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400150 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700151 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400152
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
154 goto error_0;
155
Trond Myklebust40c553192007-12-14 14:56:07 -0500156 clp->rpc_ops = cl_init->rpc_ops;
157
David Howells24c8dbb2006-08-22 20:06:10 -0400158 atomic_set(&clp->cl_count, 1);
159 clp->cl_cons_state = NFS_CS_INITING;
160
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500161 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
162 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400163
Trond Myklebust3a498022007-12-14 14:56:04 -0500164 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700165 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500166 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400167 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400168 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400169 }
170
171 INIT_LIST_HEAD(&clp->cl_superblocks);
172 clp->cl_rpcclient = ERR_PTR(-EINVAL);
173
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500174 clp->cl_proto = cl_init->proto;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000175 clp->net = cl_init->net;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500176
David Howells24c8dbb2006-08-22 20:06:10 -0400177#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000178 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
179 if (err)
180 goto error_cleanup;
181
David Howells24c8dbb2006-08-22 20:06:10 -0400182 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000183 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400184 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
185 clp->cl_boot_time = CURRENT_TIME;
186 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400187 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400188 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400189#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500190 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400191 if (!IS_ERR(cred))
192 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100193 nfs_fscache_get_client_cookie(clp);
194
David Howells24c8dbb2006-08-22 20:06:10 -0400195 return clp;
196
Benny Halevy71468512009-04-01 09:22:56 -0400197error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400198 kfree(clp);
199error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700200 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400201}
202
Trond Myklebust5dd31772006-08-24 01:03:05 -0400203#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400204#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000205static void nfs4_shutdown_session(struct nfs_client *clp)
206{
207 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400208 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400209}
Andy Adamsonea005282011-01-06 02:04:29 +0000210#else /* CONFIG_NFS_V4_1 */
211static void nfs4_shutdown_session(struct nfs_client *clp)
212{
213}
214#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400215
216/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800217 * Destroy the NFS4 callback service
218 */
219static void nfs4_destroy_callback(struct nfs_client *clp)
220{
221 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400222 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800223}
224
225static void nfs4_shutdown_client(struct nfs_client *clp)
226{
227 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
228 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000229 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800230 nfs4_destroy_callback(clp);
231 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
232 nfs_idmap_delete(clp);
233
234 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
235}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000236
237/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000238void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000239{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000240 struct nfs_net *nn = net_generic(net, nfs_net_id);
241
242 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000243}
244
245/* nfs_client_lock held */
246static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
247{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000248 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
249
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000250 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000251 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000252}
253
Fred Isamanf7e89172011-01-06 11:36:32 +0000254static void pnfs_init_server(struct nfs_server *server)
255{
256 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
257}
258
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500259static void nfs4_destroy_server(struct nfs_server *server)
260{
261 nfs4_purge_state_owners(server);
262}
263
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800264#else
265static void nfs4_shutdown_client(struct nfs_client *clp)
266{
267}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000268
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000269void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000270{
271}
272
273static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
274{
275}
Fred Isamanf7e89172011-01-06 11:36:32 +0000276
277static void pnfs_init_server(struct nfs_server *server)
278{
279}
280
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800281#endif /* CONFIG_NFS_V4 */
282
283/*
David Howells24c8dbb2006-08-22 20:06:10 -0400284 * Destroy a shared client record
285 */
286static void nfs_free_client(struct nfs_client *clp)
287{
Trond Myklebust40c553192007-12-14 14:56:07 -0500288 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400289
Trond Myklebust5dd31772006-08-24 01:03:05 -0400290 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400291
David Howells14727282009-04-03 16:42:42 +0100292 nfs_fscache_release_client_cookie(clp);
293
David Howells24c8dbb2006-08-22 20:06:10 -0400294 /* -EIO all pending I/O */
295 if (!IS_ERR(clp->cl_rpcclient))
296 rpc_shutdown_client(clp->cl_rpcclient);
297
Trond Myklebust7c67db32008-04-07 20:50:11 -0400298 if (clp->cl_machine_cred != NULL)
299 put_rpccred(clp->cl_machine_cred);
300
Benny Halevy1775bc32011-05-20 13:47:33 +0200301 nfs4_deviceid_purge_client(clp);
302
David Howells24c8dbb2006-08-22 20:06:10 -0400303 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400304 kfree(clp->server_scope);
David Howells24c8dbb2006-08-22 20:06:10 -0400305 kfree(clp);
306
307 dprintk("<-- nfs_free_client()\n");
308}
309
310/*
311 * Release a reference to a shared client record
312 */
313void nfs_put_client(struct nfs_client *clp)
314{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000315 struct nfs_net *nn;
316
David Howells27ba8512006-07-30 14:40:56 -0400317 if (!clp)
318 return;
319
David Howells24c8dbb2006-08-22 20:06:10 -0400320 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000321 nn = net_generic(clp->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400322
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000323 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
David Howells24c8dbb2006-08-22 20:06:10 -0400324 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000325 nfs_cb_idr_remove_locked(clp);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000326 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400327
328 BUG_ON(!list_empty(&clp->cl_superblocks));
329
330 nfs_free_client(clp);
331 }
332}
Andy Adamson16b374c2010-10-20 00:18:04 -0400333EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400334
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500335#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400336/*
337 * Test if two ip6 socket addresses refer to the same socket by
338 * comparing relevant fields. The padding bytes specifically, are not
339 * compared. sin6_flowinfo is not compared because it only affects QoS
340 * and sin6_scope_id is only compared if the address is "link local"
341 * because "link local" addresses need only be unique to a specific
342 * link. Conversely, ordinary unicast addresses might have different
343 * sin6_scope_id.
344 *
345 * The caller should ensure both socket addresses are AF_INET6.
346 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400347static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
348 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400349{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400350 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
351 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400352
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800353 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400354 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800355 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
356 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500357
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800358 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500359}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400360#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
361static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
362 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400363{
364 return 0;
365}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500366#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500367
David Howells24c8dbb2006-08-22 20:06:10 -0400368/*
Ian Dalld7371c42009-03-10 20:33:22 -0400369 * Test if two ip4 socket addresses refer to the same socket, by
370 * comparing relevant fields. The padding bytes specifically, are
371 * not compared.
372 *
373 * The caller should ensure both socket addresses are AF_INET.
374 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400375static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
376 const struct sockaddr *sa2)
377{
378 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
379 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
380
381 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
382}
383
384static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
385 const struct sockaddr *sa2)
386{
387 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
388 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
389
390 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
391 (sin1->sin6_port == sin2->sin6_port);
392}
393
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400394static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
395 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400396{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400397 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
398 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400399
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400400 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
401 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400402}
403
404/*
Ian Dalld7371c42009-03-10 20:33:22 -0400405 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400406 * by comparing (only) relevant fields, excluding the port number.
407 */
408static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
409 const struct sockaddr *sa2)
410{
411 if (sa1->sa_family != sa2->sa_family)
412 return 0;
413
414 switch (sa1->sa_family) {
415 case AF_INET:
416 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
417 case AF_INET6:
418 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
419 }
420 return 0;
421}
422
423/*
424 * Test if two socket addresses represent the same actual socket,
425 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400426 */
427static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
428 const struct sockaddr *sa2)
429{
430 if (sa1->sa_family != sa2->sa_family)
431 return 0;
432
433 switch (sa1->sa_family) {
434 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400435 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400436 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400437 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400438 }
439 return 0;
440}
441
Andy Adamsonc36fca52011-01-06 02:04:32 +0000442/* Common match routine for v4.0 and v4.1 callback services */
443bool
444nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
445 u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500446{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000447 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500448
Andy Adamsonc36fca52011-01-06 02:04:32 +0000449 /* Don't match clients that failed to initialise */
450 if (!(clp->cl_cons_state == NFS_CS_READY ||
451 clp->cl_cons_state == NFS_CS_SESSION_INITING))
452 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500453
Andy Adamsonc36fca52011-01-06 02:04:32 +0000454 /* Match the version and minorversion */
455 if (clp->rpc_ops->version != 4 ||
456 clp->cl_minorversion != minorversion)
457 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500458
Andy Adamsonc36fca52011-01-06 02:04:32 +0000459 /* Match only the IP address, not the port number */
460 if (!nfs_sockaddr_match_ipaddr(addr, clap))
461 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500462
Andy Adamsonc36fca52011-01-06 02:04:32 +0000463 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500464}
465
466/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500467 * Find an nfs_client on the list that matches the initialisation data
468 * that is supplied.
469 */
470static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400471{
472 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400473 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000474 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400475
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000476 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400477 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700478 /* Don't match clients that failed to initialise properly */
479 if (clp->cl_cons_state < 0)
480 continue;
481
David Howells24c8dbb2006-08-22 20:06:10 -0400482 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500483 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400484 continue;
485
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500486 if (clp->cl_proto != data->proto)
487 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400488 /* Match nfsv4 minorversion */
489 if (clp->cl_minorversion != data->minorversion)
490 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500491 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400492 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400493 continue;
494
Trond Myklebustc81468a2007-12-14 14:56:05 -0500495 atomic_inc(&clp->cl_count);
496 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400497 }
David Howells24c8dbb2006-08-22 20:06:10 -0400498 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400499}
500
501/*
502 * Look up a client by IP address and protocol version
503 * - creates a new record if one doesn't yet exist
504 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000505static struct nfs_client *
506nfs_get_client(const struct nfs_client_initdata *cl_init,
507 const struct rpc_timeout *timeparms,
508 const char *ip_addr,
509 rpc_authflavor_t authflavour,
510 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400511{
512 struct nfs_client *clp, *new = NULL;
513 int error;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000514 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400515
Chuck Leverd7422c42007-12-10 14:58:51 -0500516 dprintk("--> nfs_get_client(%s,v%u)\n",
517 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400518
519 /* see if the client already exists */
520 do {
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000521 spin_lock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400522
Trond Myklebustc81468a2007-12-14 14:56:05 -0500523 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400524 if (clp)
525 goto found_client;
526 if (new)
527 goto install_client;
528
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000529 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400530
Trond Myklebust3a498022007-12-14 14:56:04 -0500531 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700532 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400533
Chuck Levera21bdd92009-06-17 18:02:10 -0700534 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
535 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400536
537 /* install a new client and return with it unready */
538install_client:
539 clp = new;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000540 list_add(&clp->cl_share_link, &nn->nfs_client_list);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000541 spin_unlock(&nn->nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000542
543 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
544 authflavour, noresvport);
545 if (error < 0) {
546 nfs_put_client(clp);
547 return ERR_PTR(error);
548 }
David Howells24c8dbb2006-08-22 20:06:10 -0400549 dprintk("--> nfs_get_client() = %p [new]\n", clp);
550 return clp;
551
552 /* found an existing client
553 * - make sure it's ready before returning
554 */
555found_client:
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000556 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400557
558 if (new)
559 nfs_free_client(new);
560
Matthew Wilcox150030b2007-12-06 16:24:39 -0500561 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400562 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400563 if (error < 0) {
564 nfs_put_client(clp);
565 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400566 }
567
568 if (clp->cl_cons_state < NFS_CS_READY) {
569 error = clp->cl_cons_state;
570 nfs_put_client(clp);
571 return ERR_PTR(error);
572 }
573
David Howells54ceac42006-08-22 20:06:13 -0400574 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
575
David Howells24c8dbb2006-08-22 20:06:10 -0400576 dprintk("--> nfs_get_client() = %p [share]\n", clp);
577 return clp;
578}
579
580/*
581 * Mark a server as ready or failed
582 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400583void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400584{
585 clp->cl_cons_state = state;
586 wake_up_all(&nfs_client_active_wq);
587}
David Howells5006a762006-08-22 20:06:12 -0400588
589/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400590 * With sessions, the client is not marked ready until after a
591 * successful EXCHANGE_ID and CREATE_SESSION.
592 *
593 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
594 * other versions of NFS can be tried.
595 */
596int nfs4_check_client_ready(struct nfs_client *clp)
597{
598 if (!nfs4_has_session(clp))
599 return 0;
600 if (clp->cl_cons_state < NFS_CS_READY)
601 return -EPROTONOSUPPORT;
602 return 0;
603}
604
605/*
David Howells5006a762006-08-22 20:06:12 -0400606 * Initialise the timeout values for a connection
607 */
608static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
609 unsigned int timeo, unsigned int retrans)
610{
611 to->to_initval = timeo * HZ / 10;
612 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400613
614 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400615 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400616 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400617 if (to->to_retries == 0)
618 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500619 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400620 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400621 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
622 to->to_initval = NFS_MAX_TCP_TIMEOUT;
623 to->to_increment = to->to_initval;
624 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500625 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
626 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
627 if (to->to_maxval < to->to_initval)
628 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400629 to->to_exponential = 0;
630 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400631 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400632 if (to->to_retries == 0)
633 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400634 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400635 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400636 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
637 to->to_initval = NFS_MAX_UDP_TIMEOUT;
638 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
639 to->to_exponential = 1;
640 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400641 default:
642 BUG();
David Howells5006a762006-08-22 20:06:12 -0400643 }
644}
645
646/*
647 * Create an RPC client handle
648 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500649static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500650 const struct rpc_timeout *timeparms,
651 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500652 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400653{
David Howells5006a762006-08-22 20:06:12 -0400654 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400655 struct rpc_create_args args = {
Stanislav Kinsbursky6d59b8d2012-01-10 16:12:54 +0400656 .net = clp->net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500657 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400658 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500659 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500660 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400661 .servername = clp->cl_hostname,
662 .program = &nfs_program,
663 .version = clp->rpc_ops->version,
664 .authflavor = flavor,
665 };
David Howells5006a762006-08-22 20:06:12 -0400666
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500667 if (discrtry)
668 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
669 if (noresvport)
670 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
671
David Howells5006a762006-08-22 20:06:12 -0400672 if (!IS_ERR(clp->cl_rpcclient))
673 return 0;
674
Chuck Lever41877d22006-08-22 20:06:20 -0400675 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400676 if (IS_ERR(clnt)) {
677 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700678 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400679 return PTR_ERR(clnt);
680 }
681
David Howells5006a762006-08-22 20:06:12 -0400682 clp->cl_rpcclient = clnt;
683 return 0;
684}
David Howells54ceac42006-08-22 20:06:13 -0400685
686/*
687 * Version 2 or 3 client destruction
688 */
689static void nfs_destroy_server(struct nfs_server *server)
690{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400691 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
692 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500693 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400694}
695
696/*
697 * Version 2 or 3 lockd setup
698 */
699static int nfs_start_lockd(struct nfs_server *server)
700{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500701 struct nlm_host *host;
702 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500703 struct nlmclnt_initdata nlm_init = {
704 .hostname = clp->cl_hostname,
705 .address = (struct sockaddr *)&clp->cl_addr,
706 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500707 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500708 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
709 1 : 0,
Stanislav Kinsbursky66697bf2012-01-31 15:08:13 +0400710 .net = clp->net,
Chuck Lever883bb162008-01-15 16:04:20 -0500711 };
David Howells54ceac42006-08-22 20:06:13 -0400712
Chuck Lever883bb162008-01-15 16:04:20 -0500713 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500714 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400715 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
716 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500717 return 0;
718
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400719 switch (clp->cl_proto) {
720 default:
721 nlm_init.protocol = IPPROTO_TCP;
722 break;
723 case XPRT_TRANSPORT_UDP:
724 nlm_init.protocol = IPPROTO_UDP;
725 }
726
Chuck Lever883bb162008-01-15 16:04:20 -0500727 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500728 if (IS_ERR(host))
729 return PTR_ERR(host);
730
731 server->nlm_host = host;
732 server->destroy = nfs_destroy_server;
733 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400734}
735
736/*
737 * Initialise an NFSv3 ACL client connection
738 */
739#ifdef CONFIG_NFS_V3_ACL
740static void nfs_init_server_aclclient(struct nfs_server *server)
741{
Trond Myklebust40c553192007-12-14 14:56:07 -0500742 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400743 goto out_noacl;
744 if (server->flags & NFS_MOUNT_NOACL)
745 goto out_noacl;
746
747 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
748 if (IS_ERR(server->client_acl))
749 goto out_noacl;
750
751 /* No errors! Assume that Sun nfsacls are supported */
752 server->caps |= NFS_CAP_ACLS;
753 return;
754
755out_noacl:
756 server->caps &= ~NFS_CAP_ACLS;
757}
758#else
759static inline void nfs_init_server_aclclient(struct nfs_server *server)
760{
761 server->flags &= ~NFS_MOUNT_NOACL;
762 server->caps &= ~NFS_CAP_ACLS;
763}
764#endif
765
766/*
767 * Create a general RPC client
768 */
Trond Myklebust33170232007-12-20 16:03:59 -0500769static int nfs_init_server_rpcclient(struct nfs_server *server,
770 const struct rpc_timeout *timeo,
771 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400772{
773 struct nfs_client *clp = server->nfs_client;
774
775 server->client = rpc_clone_client(clp->cl_rpcclient);
776 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700777 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400778 return PTR_ERR(server->client);
779 }
780
Trond Myklebust33170232007-12-20 16:03:59 -0500781 memcpy(&server->client->cl_timeout_default,
782 timeo,
783 sizeof(server->client->cl_timeout_default));
784 server->client->cl_timeout = &server->client->cl_timeout_default;
785
David Howells54ceac42006-08-22 20:06:13 -0400786 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
787 struct rpc_auth *auth;
788
789 auth = rpcauth_create(pseudoflavour, server->client);
790 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700791 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400792 return PTR_ERR(auth);
793 }
794 }
795 server->client->cl_softrtry = 0;
796 if (server->flags & NFS_MOUNT_SOFT)
797 server->client->cl_softrtry = 1;
798
David Howells54ceac42006-08-22 20:06:13 -0400799 return 0;
800}
801
802/*
803 * Initialise an NFS2 or NFS3 client
804 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000805int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
806 const char *ip_addr, rpc_authflavor_t authflavour,
807 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400808{
David Howells54ceac42006-08-22 20:06:13 -0400809 int error;
810
811 if (clp->cl_cons_state == NFS_CS_READY) {
812 /* the client is already initialised */
813 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
814 return 0;
815 }
816
David Howells54ceac42006-08-22 20:06:13 -0400817 /*
818 * Create a client RPC handle for doing FSSTAT with UNIX auth only
819 * - RFC 2623, sec 2.3.2
820 */
Chuck Leverd7403512008-12-23 15:21:37 -0500821 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000822 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400823 if (error < 0)
824 goto error;
825 nfs_mark_client_ready(clp, NFS_CS_READY);
826 return 0;
827
828error:
829 nfs_mark_client_ready(clp, error);
830 dprintk("<-- nfs_init_client() = xerror %d\n", error);
831 return error;
832}
833
834/*
835 * Create a version 2 or 3 client
836 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400837static int nfs_init_server(struct nfs_server *server,
838 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400839{
Trond Myklebust3a498022007-12-14 14:56:04 -0500840 struct nfs_client_initdata cl_init = {
841 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500842 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500843 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500844 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500845 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400846 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500847 };
Trond Myklebust33170232007-12-20 16:03:59 -0500848 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400849 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500850 int error;
David Howells54ceac42006-08-22 20:06:13 -0400851
852 dprintk("--> nfs_init_server()\n");
853
854#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400855 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500856 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400857#endif
858
Andy Adamson45a52a02011-03-01 01:34:08 +0000859 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
860 data->timeo, data->retrans);
861
David Howells54ceac42006-08-22 20:06:13 -0400862 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000863 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
864 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400865 if (IS_ERR(clp)) {
866 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
867 return PTR_ERR(clp);
868 }
869
David Howells54ceac42006-08-22 20:06:13 -0400870 server->nfs_client = clp;
871
872 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400873 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100874 server->options = data->options;
Trond Myklebust62ab460c2009-08-09 15:06:19 -0400875 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
876 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
877 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400878
879 if (data->rsize)
880 server->rsize = nfs_block_size(data->rsize, NULL);
881 if (data->wsize)
882 server->wsize = nfs_block_size(data->wsize, NULL);
883
884 server->acregmin = data->acregmin * HZ;
885 server->acregmax = data->acregmax * HZ;
886 server->acdirmin = data->acdirmin * HZ;
887 server->acdirmax = data->acdirmax * HZ;
888
889 /* Start lockd here, before we might error out */
890 error = nfs_start_lockd(server);
891 if (error < 0)
892 goto error;
893
Chuck Leverf22d6d72008-03-14 14:10:22 -0400894 server->port = data->nfs_server.port;
895
Trond Myklebust33170232007-12-20 16:03:59 -0500896 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400897 if (error < 0)
898 goto error;
899
Chuck Lever3f8400d2008-03-14 14:10:30 -0400900 /* Preserve the values of mount_server-related mount options */
901 if (data->mount_server.addrlen) {
902 memcpy(&server->mountd_address, &data->mount_server.address,
903 data->mount_server.addrlen);
904 server->mountd_addrlen = data->mount_server.addrlen;
905 }
906 server->mountd_version = data->mount_server.version;
907 server->mountd_port = data->mount_server.port;
908 server->mountd_protocol = data->mount_server.protocol;
909
David Howells54ceac42006-08-22 20:06:13 -0400910 server->namelen = data->namlen;
911 /* Create a client RPC handle for the NFSv3 ACL management interface */
912 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400913 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
914 return 0;
915
916error:
917 server->nfs_client = NULL;
918 nfs_put_client(clp);
919 dprintk("<-- nfs_init_server() = xerror %d\n", error);
920 return error;
921}
922
923/*
924 * Load up the server record from information gained in an fsinfo record
925 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400926static void nfs_server_set_fsinfo(struct nfs_server *server,
927 struct nfs_fh *mntfh,
928 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400929{
930 unsigned long max_rpc_payload;
931
932 /* Work out a lot of parameters */
933 if (server->rsize == 0)
934 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
935 if (server->wsize == 0)
936 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
937
938 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
939 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
940 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
941 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
942
943 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
944 if (server->rsize > max_rpc_payload)
945 server->rsize = max_rpc_payload;
946 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
947 server->rsize = NFS_MAX_FILE_IO_SIZE;
948 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700949
Jens Axboed9938312009-06-12 14:45:52 +0200950 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400951 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
952
953 if (server->wsize > max_rpc_payload)
954 server->wsize = max_rpc_payload;
955 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
956 server->wsize = NFS_MAX_FILE_IO_SIZE;
957 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400958 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400959 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400960
David Howells54ceac42006-08-22 20:06:13 -0400961 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
962
963 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400964 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
965 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400966 if (server->dtsize > server->rsize)
967 server->dtsize = server->rsize;
968
969 if (server->flags & NFS_MOUNT_NOAC) {
970 server->acregmin = server->acregmax = 0;
971 server->acdirmin = server->acdirmax = 0;
972 }
973
974 server->maxfilesize = fsinfo->maxfilesize;
975
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700976 server->time_delta = fsinfo->time_delta;
977
David Howells54ceac42006-08-22 20:06:13 -0400978 /* We're airborne Set socket buffersize */
979 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
980}
981
982/*
983 * Probe filesystem information, including the FSID on v2/v3
984 */
985static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
986{
987 struct nfs_fsinfo fsinfo;
988 struct nfs_client *clp = server->nfs_client;
989 int error;
990
991 dprintk("--> nfs_probe_fsinfo()\n");
992
993 if (clp->rpc_ops->set_capabilities != NULL) {
994 error = clp->rpc_ops->set_capabilities(server, mntfh);
995 if (error < 0)
996 goto out_error;
997 }
998
999 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001000 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -04001001 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
1002 if (error < 0)
1003 goto out_error;
1004
Benny Halevy738fd0f32011-07-30 20:52:36 -04001005 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001006
1007 /* Get some general file system info */
1008 if (server->namelen == 0) {
1009 struct nfs_pathconf pathinfo;
1010
1011 pathinfo.fattr = fattr;
1012 nfs_fattr_init(fattr);
1013
1014 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1015 server->namelen = pathinfo.max_namelen;
1016 }
1017
1018 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1019 return 0;
1020
1021out_error:
1022 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1023 return error;
1024}
1025
1026/*
1027 * Copy useful information when duplicating a server record
1028 */
1029static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1030{
1031 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001032 target->rsize = source->rsize;
1033 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001034 target->acregmin = source->acregmin;
1035 target->acregmax = source->acregmax;
1036 target->acdirmin = source->acdirmin;
1037 target->acdirmax = source->acdirmax;
1038 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001039 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001040}
1041
Chuck Leverfca52382010-12-24 01:32:32 +00001042static void nfs_server_insert_lists(struct nfs_server *server)
1043{
1044 struct nfs_client *clp = server->nfs_client;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001045 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001046
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001047 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001048 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001049 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001050 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001051 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001052
1053}
1054
1055static void nfs_server_remove_lists(struct nfs_server *server)
1056{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001057 struct nfs_client *clp = server->nfs_client;
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001058 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001059
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001060 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001061 list_del_rcu(&server->client_link);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001062 if (clp && list_empty(&clp->cl_superblocks))
1063 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001064 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001065 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001066
1067 synchronize_rcu();
1068}
1069
David Howells54ceac42006-08-22 20:06:13 -04001070/*
1071 * Allocate and initialise a server record
1072 */
1073static struct nfs_server *nfs_alloc_server(void)
1074{
1075 struct nfs_server *server;
1076
1077 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1078 if (!server)
1079 return NULL;
1080
1081 server->client = server->client_acl = ERR_PTR(-EINVAL);
1082
1083 /* Zero out the NFS state stuff */
1084 INIT_LIST_HEAD(&server->client_link);
1085 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001086 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001087 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001088 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001089
Steve Dicksonef818a22007-11-08 04:05:04 -05001090 atomic_set(&server->active, 0);
1091
David Howells54ceac42006-08-22 20:06:13 -04001092 server->io_stats = nfs_alloc_iostats();
1093 if (!server->io_stats) {
1094 kfree(server);
1095 return NULL;
1096 }
1097
Jens Axboe48d07642009-09-21 09:59:39 +02001098 if (bdi_init(&server->backing_dev_info)) {
1099 nfs_free_iostats(server->io_stats);
1100 kfree(server);
1101 return NULL;
1102 }
1103
Trond Myklebust9157c312012-01-17 22:04:24 -05001104 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001105 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001106 pnfs_init_server(server);
1107
David Howells54ceac42006-08-22 20:06:13 -04001108 return server;
1109}
1110
1111/*
1112 * Free up a server record
1113 */
1114void nfs_free_server(struct nfs_server *server)
1115{
1116 dprintk("--> nfs_free_server()\n");
1117
Chuck Leverfca52382010-12-24 01:32:32 +00001118 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001119 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001120
1121 if (server->destroy != NULL)
1122 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001123
1124 if (!IS_ERR(server->client_acl))
1125 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001126 if (!IS_ERR(server->client))
1127 rpc_shutdown_client(server->client);
1128
1129 nfs_put_client(server->nfs_client);
1130
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001131 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001132 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001133 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001134 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001135 kfree(server);
1136 nfs_release_automount_timer();
1137 dprintk("<-- nfs_free_server()\n");
1138}
1139
1140/*
1141 * Create a version 2 or 3 volume record
1142 * - keyed on server and FSID
1143 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001144struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001145 struct nfs_fh *mntfh)
1146{
1147 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001148 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001149 int error;
1150
1151 server = nfs_alloc_server();
1152 if (!server)
1153 return ERR_PTR(-ENOMEM);
1154
Trond Myklebustfbca7792010-04-16 16:22:46 -04001155 error = -ENOMEM;
1156 fattr = nfs_alloc_fattr();
1157 if (fattr == NULL)
1158 goto error;
1159
David Howells54ceac42006-08-22 20:06:13 -04001160 /* Get a client representation */
1161 error = nfs_init_server(server, data);
1162 if (error < 0)
1163 goto error;
1164
1165 BUG_ON(!server->nfs_client);
1166 BUG_ON(!server->nfs_client->rpc_ops);
1167 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1168
1169 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001170 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001171 if (error < 0)
1172 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001173 if (server->nfs_client->rpc_ops->version == 3) {
1174 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1175 server->namelen = NFS3_MAXNAMLEN;
1176 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1177 server->caps |= NFS_CAP_READDIRPLUS;
1178 } else {
1179 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1180 server->namelen = NFS2_MAXNAMLEN;
1181 }
1182
Trond Myklebustfbca7792010-04-16 16:22:46 -04001183 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1184 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001185 if (error < 0) {
1186 dprintk("nfs_create_server: getattr error = %d\n", -error);
1187 goto error;
1188 }
1189 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001190 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001191
David Howells6daabf12006-08-24 15:44:16 -04001192 dprintk("Server FSID: %llx:%llx\n",
1193 (unsigned long long) server->fsid.major,
1194 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001195
Chuck Leverfca52382010-12-24 01:32:32 +00001196 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001197 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001198 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001199 return server;
1200
1201error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001202 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001203 nfs_free_server(server);
1204 return ERR_PTR(error);
1205}
1206
1207#ifdef CONFIG_NFS_V4
1208/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001209 * NFSv4.0 callback thread helper
1210 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001211 * Find a client by callback identifier
1212 */
1213struct nfs_client *
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001214nfs4_find_client_ident(struct net *net, int cb_ident)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001215{
1216 struct nfs_client *clp;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001217 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001218
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001219 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001220 clp = idr_find(&nn->cb_ident_idr, cb_ident);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001221 if (clp)
1222 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001223 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001224 return clp;
1225}
1226
1227#if defined(CONFIG_NFS_V4_1)
1228/*
1229 * NFSv4.1 callback thread helper
1230 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1231 * minorversion, and sessionID
1232 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001233 * Returns NULL if no such client
1234 */
1235struct nfs_client *
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001236nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001237 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001238{
1239 struct nfs_client *clp;
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001240 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001241
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001242 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001243 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001244 if (nfs4_cb_match_client(addr, clp, 1) == false)
1245 continue;
1246
1247 if (!nfs4_has_session(clp))
1248 continue;
1249
Andy Adamson778be232011-01-25 15:38:01 +00001250 /* Match sessionid*/
1251 if (memcmp(clp->cl_session->sess_id.data,
1252 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001253 continue;
1254
1255 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001256 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001257 return clp;
1258 }
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001259 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001260 return NULL;
1261}
1262
1263#else /* CONFIG_NFS_V4_1 */
1264
1265struct nfs_client *
1266nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001267 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001268{
1269 return NULL;
1270}
1271#endif /* CONFIG_NFS_V4_1 */
1272
1273/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001274 * Initialize the NFS4 callback service
1275 */
1276static int nfs4_init_callback(struct nfs_client *clp)
1277{
1278 int error;
1279
1280 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001281 if (nfs4_has_session(clp)) {
1282 error = xprt_setup_backchannel(
1283 clp->cl_rpcclient->cl_xprt,
1284 NFS41_BC_MIN_CALLBACKS);
1285 if (error < 0)
1286 return error;
1287 }
1288
Trond Myklebust97dc1352010-06-16 09:52:26 -04001289 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001290 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001291 if (error < 0) {
1292 dprintk("%s: failed to start callback. Error = %d\n",
1293 __func__, error);
1294 return error;
1295 }
1296 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1297 }
1298 return 0;
1299}
1300
1301/*
Andy Adamson557134a2009-04-01 09:21:53 -04001302 * Initialize the minor version specific parts of an NFS4 client record
1303 */
1304static int nfs4_init_client_minor_version(struct nfs_client *clp)
1305{
1306#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001307 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001308 struct nfs4_session *session = NULL;
1309 /*
1310 * Create the session and mark it expired.
1311 * When a SEQUENCE operation encounters the expired session
1312 * it will do session recovery to initialize it.
1313 */
1314 session = nfs4_alloc_session(clp);
1315 if (!session)
1316 return -ENOMEM;
1317
1318 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001319 /*
1320 * The create session reply races with the server back
1321 * channel probe. Mark the client NFS_CS_SESSION_INITING
1322 * so that the client back channel can find the
1323 * nfs_client struct
1324 */
1325 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001326 }
1327#endif /* CONFIG_NFS_V4_1 */
1328
Benny Halevy71468512009-04-01 09:22:56 -04001329 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001330}
1331
1332/*
David Howells54ceac42006-08-22 20:06:13 -04001333 * Initialise an NFS4 client record
1334 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001335int nfs4_init_client(struct nfs_client *clp,
1336 const struct rpc_timeout *timeparms,
1337 const char *ip_addr,
1338 rpc_authflavor_t authflavour,
1339 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001340{
1341 int error;
1342
1343 if (clp->cl_cons_state == NFS_CS_READY) {
1344 /* the client is initialised already */
1345 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1346 return 0;
1347 }
1348
1349 /* Check NFS protocol revision and initialize RPC op vector */
1350 clp->rpc_ops = &nfs_v4_clientops;
1351
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001352 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001353 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001354 if (error < 0)
1355 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001356 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001357
1358 error = nfs_idmap_new(clp);
1359 if (error < 0) {
1360 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001361 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001362 goto error;
1363 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001364 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001365
Andy Adamson557134a2009-04-01 09:21:53 -04001366 error = nfs4_init_client_minor_version(clp);
1367 if (error < 0)
1368 goto error;
1369
Andy Adamson76db6d92009-04-01 09:22:38 -04001370 if (!nfs4_has_session(clp))
1371 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001372 return 0;
1373
1374error:
1375 nfs_mark_client_ready(clp, error);
1376 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1377 return error;
1378}
1379
1380/*
1381 * Set up an NFS4 client
1382 */
1383static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001384 const char *hostname,
1385 const struct sockaddr *addr,
1386 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001387 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001388 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001389 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001390 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001391{
Trond Myklebust3a498022007-12-14 14:56:04 -05001392 struct nfs_client_initdata cl_init = {
1393 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001394 .addr = addr,
1395 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001396 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001397 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001398 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001399 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001400 };
David Howells54ceac42006-08-22 20:06:13 -04001401 struct nfs_client *clp;
1402 int error;
1403
1404 dprintk("--> nfs4_set_client()\n");
1405
1406 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001407 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1408 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001409 if (IS_ERR(clp)) {
1410 error = PTR_ERR(clp);
1411 goto error;
1412 }
David Howells54ceac42006-08-22 20:06:13 -04001413
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001414 /*
1415 * Query for the lease time on clientid setup or renewal
1416 *
1417 * Note that this will be set on nfs_clients that were created
1418 * only for the DS role and did not set this bit, but now will
1419 * serve a dual role.
1420 */
1421 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1422
David Howells54ceac42006-08-22 20:06:13 -04001423 server->nfs_client = clp;
1424 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1425 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001426error:
1427 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1428 return error;
1429}
1430
Andy Adamsond83217c2011-03-01 01:34:17 +00001431/*
1432 * Set up a pNFS Data Server client.
1433 *
1434 * Return any existing nfs_client that matches server address,port,version
1435 * and minorversion.
1436 *
1437 * For a new nfs_client, use a soft mount (default), a low retrans and a
1438 * low timeout interval so that if a connection is lost, we retry through
1439 * the MDS.
1440 */
1441struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1442 const struct sockaddr *ds_addr,
1443 int ds_addrlen, int ds_proto)
1444{
1445 struct nfs_client_initdata cl_init = {
1446 .addr = ds_addr,
1447 .addrlen = ds_addrlen,
1448 .rpc_ops = &nfs_v4_clientops,
1449 .proto = ds_proto,
1450 .minorversion = mds_clp->cl_minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001451 .net = mds_clp->net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001452 };
1453 struct rpc_timeout ds_timeout = {
1454 .to_initval = 15 * HZ,
1455 .to_maxval = 15 * HZ,
1456 .to_retries = 1,
1457 .to_exponential = 1,
1458 };
1459 struct nfs_client *clp;
1460
1461 /*
1462 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1463 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1464 * (section 13.1 RFC 5661).
1465 */
1466 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1467 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1468
1469 dprintk("<-- %s %p\n", __func__, clp);
1470 return clp;
1471}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001472EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001473
1474/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001475 * Session has been established, and the client marked ready.
1476 * Set the mount rsize and wsize with negotiated fore channel
1477 * attributes which will be bound checked in nfs_server_set_fsinfo.
1478 */
1479static void nfs4_session_set_rwsize(struct nfs_server *server)
1480{
1481#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001482 struct nfs4_session *sess;
1483 u32 server_resp_sz;
1484 u32 server_rqst_sz;
1485
Andy Adamson96b09e02009-04-01 09:22:33 -04001486 if (!nfs4_has_session(server->nfs_client))
1487 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001488 sess = server->nfs_client->cl_session;
1489 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1490 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1491
1492 if (server->rsize > server_resp_sz)
1493 server->rsize = server_resp_sz;
1494 if (server->wsize > server_rqst_sz)
1495 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001496#endif /* CONFIG_NFS_V4_1 */
1497}
1498
Trond Myklebust44950b62010-06-17 11:45:12 -04001499static int nfs4_server_common_setup(struct nfs_server *server,
1500 struct nfs_fh *mntfh)
1501{
1502 struct nfs_fattr *fattr;
1503 int error;
1504
1505 BUG_ON(!server->nfs_client);
1506 BUG_ON(!server->nfs_client->rpc_ops);
1507 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1508
Andy Adamson94de8b22011-03-01 01:34:12 +00001509 /* data servers support only a subset of NFSv4.1 */
1510 if (is_ds_only_client(server->nfs_client))
1511 return -EPROTONOSUPPORT;
1512
Trond Myklebust44950b62010-06-17 11:45:12 -04001513 fattr = nfs_alloc_fattr();
1514 if (fattr == NULL)
1515 return -ENOMEM;
1516
1517 /* We must ensure the session is initialised first */
1518 error = nfs4_init_session(server);
1519 if (error < 0)
1520 goto out;
1521
1522 /* Probe the root fh to retrieve its FSID and filehandle */
1523 error = nfs4_get_rootfh(server, mntfh);
1524 if (error < 0)
1525 goto out;
1526
1527 dprintk("Server FSID: %llx:%llx\n",
1528 (unsigned long long) server->fsid.major,
1529 (unsigned long long) server->fsid.minor);
1530 dprintk("Mount FH: %d\n", mntfh->size);
1531
1532 nfs4_session_set_rwsize(server);
1533
1534 error = nfs_probe_fsinfo(server, mntfh, fattr);
1535 if (error < 0)
1536 goto out;
1537
1538 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1539 server->namelen = NFS4_MAXNAMLEN;
1540
Chuck Leverfca52382010-12-24 01:32:32 +00001541 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001542 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001543 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001544out:
1545 nfs_free_fattr(fattr);
1546 return error;
1547}
1548
Andy Adamson96b09e02009-04-01 09:22:33 -04001549/*
David Howells54ceac42006-08-22 20:06:13 -04001550 * Create a version 4 volume record
1551 */
1552static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001553 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001554{
Trond Myklebust33170232007-12-20 16:03:59 -05001555 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001556 int error;
1557
1558 dprintk("--> nfs4_init_server()\n");
1559
Trond Myklebust33170232007-12-20 16:03:59 -05001560 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1561 data->timeo, data->retrans);
1562
Chuck Lever542fcc32008-12-23 15:21:36 -05001563 /* Initialise the client representation from the mount data */
1564 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001565 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1566 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1567 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001568 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001569
Trond Myklebust33170232007-12-20 16:03:59 -05001570 /* Get a client record */
1571 error = nfs4_set_client(server,
1572 data->nfs_server.hostname,
1573 (const struct sockaddr *)&data->nfs_server.address,
1574 data->nfs_server.addrlen,
1575 data->client_address,
1576 data->auth_flavors[0],
1577 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001578 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001579 data->minorversion,
1580 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001581 if (error < 0)
1582 goto error;
1583
Trond Myklebustb064eca22011-02-22 15:44:32 -08001584 /*
1585 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1586 * authentication.
1587 */
1588 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1589 server->caps |= NFS_CAP_UIDGID_NOMAP;
1590
David Howells54ceac42006-08-22 20:06:13 -04001591 if (data->rsize)
1592 server->rsize = nfs_block_size(data->rsize, NULL);
1593 if (data->wsize)
1594 server->wsize = nfs_block_size(data->wsize, NULL);
1595
1596 server->acregmin = data->acregmin * HZ;
1597 server->acregmax = data->acregmax * HZ;
1598 server->acdirmin = data->acdirmin * HZ;
1599 server->acdirmax = data->acdirmax * HZ;
1600
Chuck Leverf22d6d72008-03-14 14:10:22 -04001601 server->port = data->nfs_server.port;
1602
Trond Myklebust33170232007-12-20 16:03:59 -05001603 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001604
Trond Myklebust33170232007-12-20 16:03:59 -05001605error:
David Howells54ceac42006-08-22 20:06:13 -04001606 /* Done */
1607 dprintk("<-- nfs4_init_server() = %d\n", error);
1608 return error;
1609}
1610
1611/*
1612 * Create a version 4 volume record
1613 * - keyed on server and FSID
1614 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001615struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001616 struct nfs_fh *mntfh)
1617{
David Howells54ceac42006-08-22 20:06:13 -04001618 struct nfs_server *server;
1619 int error;
1620
1621 dprintk("--> nfs4_create_server()\n");
1622
1623 server = nfs_alloc_server();
1624 if (!server)
1625 return ERR_PTR(-ENOMEM);
1626
David Howells54ceac42006-08-22 20:06:13 -04001627 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001628 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001629 if (error < 0)
1630 goto error;
1631
Trond Myklebust44950b62010-06-17 11:45:12 -04001632 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001633 if (error < 0)
1634 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001635
David Howells54ceac42006-08-22 20:06:13 -04001636 dprintk("<-- nfs4_create_server() = %p\n", server);
1637 return server;
1638
1639error:
1640 nfs_free_server(server);
1641 dprintk("<-- nfs4_create_server() = error %d\n", error);
1642 return ERR_PTR(error);
1643}
1644
1645/*
1646 * Create an NFS4 referral server record
1647 */
1648struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001649 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001650{
1651 struct nfs_client *parent_client;
1652 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001653 int error;
1654
1655 dprintk("--> nfs4_create_referral_server()\n");
1656
1657 server = nfs_alloc_server();
1658 if (!server)
1659 return ERR_PTR(-ENOMEM);
1660
1661 parent_server = NFS_SB(data->sb);
1662 parent_client = parent_server->nfs_client;
1663
Chuck Lever542fcc32008-12-23 15:21:36 -05001664 /* Initialise the client representation from the parent server */
1665 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab460c2009-08-09 15:06:19 -04001666 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001667
David Howells54ceac42006-08-22 20:06:13 -04001668 /* Get a client representation.
1669 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001670 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001671 data->addr,
1672 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001673 parent_client->cl_ipaddr,
1674 data->authflavor,
1675 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001676 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001677 parent_client->cl_mvops->minor_version,
1678 parent_client->net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001679 if (error < 0)
1680 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001681
Trond Myklebust33170232007-12-20 16:03:59 -05001682 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001683 if (error < 0)
1684 goto error;
1685
Trond Myklebust44950b62010-06-17 11:45:12 -04001686 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001687 if (error < 0)
1688 goto error;
1689
David Howells54ceac42006-08-22 20:06:13 -04001690 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1691 return server;
1692
1693error:
1694 nfs_free_server(server);
1695 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1696 return ERR_PTR(error);
1697}
1698
1699#endif /* CONFIG_NFS_V4 */
1700
1701/*
1702 * Clone an NFS2, NFS3 or NFS4 server record
1703 */
1704struct nfs_server *nfs_clone_server(struct nfs_server *source,
1705 struct nfs_fh *fh,
1706 struct nfs_fattr *fattr)
1707{
1708 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001709 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001710 int error;
1711
1712 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001713 (unsigned long long) fattr->fsid.major,
1714 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001715
1716 server = nfs_alloc_server();
1717 if (!server)
1718 return ERR_PTR(-ENOMEM);
1719
Trond Myklebustfbca7792010-04-16 16:22:46 -04001720 error = -ENOMEM;
1721 fattr_fsinfo = nfs_alloc_fattr();
1722 if (fattr_fsinfo == NULL)
1723 goto out_free_server;
1724
David Howells54ceac42006-08-22 20:06:13 -04001725 /* Copy data from the source */
1726 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001727 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001728 atomic_inc(&server->nfs_client->cl_count);
1729 nfs_server_copy_userdata(server, source);
1730
1731 server->fsid = fattr->fsid;
1732
Trond Myklebust33170232007-12-20 16:03:59 -05001733 error = nfs_init_server_rpcclient(server,
1734 source->client->cl_timeout,
1735 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001736 if (error < 0)
1737 goto out_free_server;
1738 if (!IS_ERR(source->client_acl))
1739 nfs_init_server_aclclient(server);
1740
1741 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001742 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001743 if (error < 0)
1744 goto out_free_server;
1745
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001746 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1747 server->namelen = NFS4_MAXNAMLEN;
1748
David Howells54ceac42006-08-22 20:06:13 -04001749 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001750 (unsigned long long) server->fsid.major,
1751 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001752
1753 error = nfs_start_lockd(server);
1754 if (error < 0)
1755 goto out_free_server;
1756
Chuck Leverfca52382010-12-24 01:32:32 +00001757 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001758 server->mount_time = jiffies;
1759
Trond Myklebustfbca7792010-04-16 16:22:46 -04001760 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001761 dprintk("<-- nfs_clone_server() = %p\n", server);
1762 return server;
1763
1764out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001765 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001766 nfs_free_server(server);
1767 dprintk("<-- nfs_clone_server() = error %d\n", error);
1768 return ERR_PTR(error);
1769}
David Howells6aaca562006-08-22 20:06:13 -04001770
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001771void nfs_clients_init(struct net *net)
1772{
1773 struct nfs_net *nn = net_generic(net, nfs_net_id);
1774
1775 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001776 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001777#ifdef CONFIG_NFS_V4
1778 idr_init(&nn->cb_ident_idr);
1779#endif
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001780}
1781
David Howells6aaca562006-08-22 20:06:13 -04001782#ifdef CONFIG_PROC_FS
1783static struct proc_dir_entry *proc_fs_nfs;
1784
1785static int nfs_server_list_open(struct inode *inode, struct file *file);
1786static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1787static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1788static void nfs_server_list_stop(struct seq_file *p, void *v);
1789static int nfs_server_list_show(struct seq_file *m, void *v);
1790
James Morris88e9d342009-09-22 16:43:43 -07001791static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001792 .start = nfs_server_list_start,
1793 .next = nfs_server_list_next,
1794 .stop = nfs_server_list_stop,
1795 .show = nfs_server_list_show,
1796};
1797
Arjan van de Ven00977a52007-02-12 00:55:34 -08001798static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001799 .open = nfs_server_list_open,
1800 .read = seq_read,
1801 .llseek = seq_lseek,
1802 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001803 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001804};
1805
1806static int nfs_volume_list_open(struct inode *inode, struct file *file);
1807static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1808static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1809static void nfs_volume_list_stop(struct seq_file *p, void *v);
1810static int nfs_volume_list_show(struct seq_file *m, void *v);
1811
James Morris88e9d342009-09-22 16:43:43 -07001812static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001813 .start = nfs_volume_list_start,
1814 .next = nfs_volume_list_next,
1815 .stop = nfs_volume_list_stop,
1816 .show = nfs_volume_list_show,
1817};
1818
Arjan van de Ven00977a52007-02-12 00:55:34 -08001819static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001820 .open = nfs_volume_list_open,
1821 .read = seq_read,
1822 .llseek = seq_lseek,
1823 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001824 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001825};
1826
1827/*
1828 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1829 * we're dealing
1830 */
1831static int nfs_server_list_open(struct inode *inode, struct file *file)
1832{
1833 struct seq_file *m;
1834 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001835 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1836 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001837
1838 ret = seq_open(file, &nfs_server_list_ops);
1839 if (ret < 0)
1840 return ret;
1841
1842 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001843 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001844
1845 return 0;
1846}
1847
1848/*
1849 * set up the iterator to start reading from the server list and return the first item
1850 */
1851static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1852{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001853 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1854
David Howells6aaca562006-08-22 20:06:13 -04001855 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001856 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001857 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001858}
1859
1860/*
1861 * move to next server
1862 */
1863static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1864{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001865 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1866
1867 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001868}
1869
1870/*
1871 * clean up after reading from the transports list
1872 */
1873static void nfs_server_list_stop(struct seq_file *p, void *v)
1874{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001875 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1876
1877 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001878}
1879
1880/*
1881 * display a header line followed by a load of call lines
1882 */
1883static int nfs_server_list_show(struct seq_file *m, void *v)
1884{
1885 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001886 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001887
1888 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001889 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001890 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1891 return 0;
1892 }
1893
1894 /* display one transport per line on subsequent lines */
1895 clp = list_entry(v, struct nfs_client, cl_share_link);
1896
Malahal Naineni940aab42011-09-20 17:27:14 -07001897 /* Check if the client is initialized */
1898 if (clp->cl_cons_state != NFS_CS_READY)
1899 return 0;
1900
Chuck Lever5d8515c2007-12-10 14:57:16 -05001901 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001902 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001903 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1904 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001905 atomic_read(&clp->cl_count),
1906 clp->cl_hostname);
1907
1908 return 0;
1909}
1910
1911/*
1912 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1913 */
1914static int nfs_volume_list_open(struct inode *inode, struct file *file)
1915{
1916 struct seq_file *m;
1917 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001918 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1919 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001920
1921 ret = seq_open(file, &nfs_volume_list_ops);
1922 if (ret < 0)
1923 return ret;
1924
1925 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001926 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001927
1928 return 0;
1929}
1930
1931/*
1932 * set up the iterator to start reading from the volume list and return the first item
1933 */
1934static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1935{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001936 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1937
David Howells6aaca562006-08-22 20:06:13 -04001938 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001939 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001940 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001941}
1942
1943/*
1944 * move to next volume
1945 */
1946static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1947{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001948 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1949
1950 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001951}
1952
1953/*
1954 * clean up after reading from the transports list
1955 */
1956static void nfs_volume_list_stop(struct seq_file *p, void *v)
1957{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001958 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1959
1960 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001961}
1962
1963/*
1964 * display a header line followed by a load of call lines
1965 */
1966static int nfs_volume_list_show(struct seq_file *m, void *v)
1967{
1968 struct nfs_server *server;
1969 struct nfs_client *clp;
1970 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001971 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001972
1973 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001974 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001975 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001976 return 0;
1977 }
1978 /* display one transport per line on subsequent lines */
1979 server = list_entry(v, struct nfs_server, master_link);
1980 clp = server->nfs_client;
1981
1982 snprintf(dev, 8, "%u:%u",
1983 MAJOR(server->s_dev), MINOR(server->s_dev));
1984
1985 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001986 (unsigned long long) server->fsid.major,
1987 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001988
David Howells5d1acff2009-04-03 16:42:47 +01001989 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001990 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001991 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1992 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001993 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001994 fsid,
1995 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001996
1997 return 0;
1998}
1999
2000/*
2001 * initialise the /proc/fs/nfsfs/ directory
2002 */
2003int __init nfs_fs_proc_init(void)
2004{
2005 struct proc_dir_entry *p;
2006
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002007 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002008 if (!proc_fs_nfs)
2009 goto error_0;
2010
David Howells6aaca562006-08-22 20:06:13 -04002011 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002012 p = proc_create("servers", S_IFREG|S_IRUGO,
2013 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002014 if (!p)
2015 goto error_1;
2016
David Howells6aaca562006-08-22 20:06:13 -04002017 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002018 p = proc_create("volumes", S_IFREG|S_IRUGO,
2019 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002020 if (!p)
2021 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002022 return 0;
2023
2024error_2:
2025 remove_proc_entry("servers", proc_fs_nfs);
2026error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002027 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002028error_0:
2029 return -ENOMEM;
2030}
2031
2032/*
2033 * clean up the /proc/fs/nfsfs/ directory
2034 */
2035void nfs_fs_proc_exit(void)
2036{
2037 remove_proc_entry("volumes", proc_fs_nfs);
2038 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002039 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002040}
2041
2042#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002043
2044module_param(nfs4_disable_idmapping, bool, 0644);
2045MODULE_PARM_DESC(nfs4_disable_idmapping,
2046 "Turn off NFSv4 idmapping when using 'sec=sys'");