blob: 6f6267cb6badf4e91bac77666d8f52d9527e7cc6 [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 Myklebust62ab4602009-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;
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001058 struct nfs_net *nn;
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001059
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001060 if (clp == NULL)
1061 return;
1062 nn = net_generic(clp->net, nfs_net_id);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001063 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001064 list_del_rcu(&server->client_link);
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001065 if (list_empty(&clp->cl_superblocks))
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001066 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001067 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001068 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001069
1070 synchronize_rcu();
1071}
1072
David Howells54ceac42006-08-22 20:06:13 -04001073/*
1074 * Allocate and initialise a server record
1075 */
1076static struct nfs_server *nfs_alloc_server(void)
1077{
1078 struct nfs_server *server;
1079
1080 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1081 if (!server)
1082 return NULL;
1083
1084 server->client = server->client_acl = ERR_PTR(-EINVAL);
1085
1086 /* Zero out the NFS state stuff */
1087 INIT_LIST_HEAD(&server->client_link);
1088 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001089 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001090 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001091 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001092
Steve Dicksonef818a22007-11-08 04:05:04 -05001093 atomic_set(&server->active, 0);
1094
David Howells54ceac42006-08-22 20:06:13 -04001095 server->io_stats = nfs_alloc_iostats();
1096 if (!server->io_stats) {
1097 kfree(server);
1098 return NULL;
1099 }
1100
Jens Axboe48d07642009-09-21 09:59:39 +02001101 if (bdi_init(&server->backing_dev_info)) {
1102 nfs_free_iostats(server->io_stats);
1103 kfree(server);
1104 return NULL;
1105 }
1106
Trond Myklebust9157c312012-01-17 22:04:24 -05001107 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001108 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001109 pnfs_init_server(server);
1110
David Howells54ceac42006-08-22 20:06:13 -04001111 return server;
1112}
1113
1114/*
1115 * Free up a server record
1116 */
1117void nfs_free_server(struct nfs_server *server)
1118{
1119 dprintk("--> nfs_free_server()\n");
1120
Chuck Leverfca52382010-12-24 01:32:32 +00001121 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001122 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001123
1124 if (server->destroy != NULL)
1125 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001126
1127 if (!IS_ERR(server->client_acl))
1128 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001129 if (!IS_ERR(server->client))
1130 rpc_shutdown_client(server->client);
1131
1132 nfs_put_client(server->nfs_client);
1133
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001134 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001135 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001136 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001137 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001138 kfree(server);
1139 nfs_release_automount_timer();
1140 dprintk("<-- nfs_free_server()\n");
1141}
1142
1143/*
1144 * Create a version 2 or 3 volume record
1145 * - keyed on server and FSID
1146 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001147struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001148 struct nfs_fh *mntfh)
1149{
1150 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001151 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001152 int error;
1153
1154 server = nfs_alloc_server();
1155 if (!server)
1156 return ERR_PTR(-ENOMEM);
1157
Trond Myklebustfbca7792010-04-16 16:22:46 -04001158 error = -ENOMEM;
1159 fattr = nfs_alloc_fattr();
1160 if (fattr == NULL)
1161 goto error;
1162
David Howells54ceac42006-08-22 20:06:13 -04001163 /* Get a client representation */
1164 error = nfs_init_server(server, data);
1165 if (error < 0)
1166 goto error;
1167
1168 BUG_ON(!server->nfs_client);
1169 BUG_ON(!server->nfs_client->rpc_ops);
1170 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1171
1172 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001173 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001174 if (error < 0)
1175 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001176 if (server->nfs_client->rpc_ops->version == 3) {
1177 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1178 server->namelen = NFS3_MAXNAMLEN;
1179 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1180 server->caps |= NFS_CAP_READDIRPLUS;
1181 } else {
1182 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1183 server->namelen = NFS2_MAXNAMLEN;
1184 }
1185
Trond Myklebustfbca7792010-04-16 16:22:46 -04001186 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1187 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001188 if (error < 0) {
1189 dprintk("nfs_create_server: getattr error = %d\n", -error);
1190 goto error;
1191 }
1192 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001193 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001194
David Howells6daabf12006-08-24 15:44:16 -04001195 dprintk("Server FSID: %llx:%llx\n",
1196 (unsigned long long) server->fsid.major,
1197 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001198
Chuck Leverfca52382010-12-24 01:32:32 +00001199 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001200 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001201 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001202 return server;
1203
1204error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001205 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001206 nfs_free_server(server);
1207 return ERR_PTR(error);
1208}
1209
1210#ifdef CONFIG_NFS_V4
1211/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001212 * NFSv4.0 callback thread helper
1213 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001214 * Find a client by callback identifier
1215 */
1216struct nfs_client *
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001217nfs4_find_client_ident(struct net *net, int cb_ident)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001218{
1219 struct nfs_client *clp;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001220 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001221
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001222 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001223 clp = idr_find(&nn->cb_ident_idr, cb_ident);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001224 if (clp)
1225 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001226 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001227 return clp;
1228}
1229
1230#if defined(CONFIG_NFS_V4_1)
1231/*
1232 * NFSv4.1 callback thread helper
1233 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1234 * minorversion, and sessionID
1235 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001236 * Returns NULL if no such client
1237 */
1238struct nfs_client *
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001239nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001240 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001241{
1242 struct nfs_client *clp;
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001243 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001244
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001245 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001246 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001247 if (nfs4_cb_match_client(addr, clp, 1) == false)
1248 continue;
1249
1250 if (!nfs4_has_session(clp))
1251 continue;
1252
Andy Adamson778be232011-01-25 15:38:01 +00001253 /* Match sessionid*/
1254 if (memcmp(clp->cl_session->sess_id.data,
1255 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001256 continue;
1257
1258 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001259 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001260 return clp;
1261 }
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001262 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001263 return NULL;
1264}
1265
1266#else /* CONFIG_NFS_V4_1 */
1267
1268struct nfs_client *
1269nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001270 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001271{
1272 return NULL;
1273}
1274#endif /* CONFIG_NFS_V4_1 */
1275
1276/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001277 * Initialize the NFS4 callback service
1278 */
1279static int nfs4_init_callback(struct nfs_client *clp)
1280{
1281 int error;
1282
1283 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001284 if (nfs4_has_session(clp)) {
1285 error = xprt_setup_backchannel(
1286 clp->cl_rpcclient->cl_xprt,
1287 NFS41_BC_MIN_CALLBACKS);
1288 if (error < 0)
1289 return error;
1290 }
1291
Trond Myklebust97dc1352010-06-16 09:52:26 -04001292 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001293 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001294 if (error < 0) {
1295 dprintk("%s: failed to start callback. Error = %d\n",
1296 __func__, error);
1297 return error;
1298 }
1299 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1300 }
1301 return 0;
1302}
1303
1304/*
Andy Adamson557134a2009-04-01 09:21:53 -04001305 * Initialize the minor version specific parts of an NFS4 client record
1306 */
1307static int nfs4_init_client_minor_version(struct nfs_client *clp)
1308{
1309#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001310 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001311 struct nfs4_session *session = NULL;
1312 /*
1313 * Create the session and mark it expired.
1314 * When a SEQUENCE operation encounters the expired session
1315 * it will do session recovery to initialize it.
1316 */
1317 session = nfs4_alloc_session(clp);
1318 if (!session)
1319 return -ENOMEM;
1320
1321 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001322 /*
1323 * The create session reply races with the server back
1324 * channel probe. Mark the client NFS_CS_SESSION_INITING
1325 * so that the client back channel can find the
1326 * nfs_client struct
1327 */
1328 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001329 }
1330#endif /* CONFIG_NFS_V4_1 */
1331
Benny Halevy71468512009-04-01 09:22:56 -04001332 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001333}
1334
1335/*
David Howells54ceac42006-08-22 20:06:13 -04001336 * Initialise an NFS4 client record
1337 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001338int nfs4_init_client(struct nfs_client *clp,
1339 const struct rpc_timeout *timeparms,
1340 const char *ip_addr,
1341 rpc_authflavor_t authflavour,
1342 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001343{
1344 int error;
1345
1346 if (clp->cl_cons_state == NFS_CS_READY) {
1347 /* the client is initialised already */
1348 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1349 return 0;
1350 }
1351
1352 /* Check NFS protocol revision and initialize RPC op vector */
1353 clp->rpc_ops = &nfs_v4_clientops;
1354
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001355 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001356 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001357 if (error < 0)
1358 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001359 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001360
1361 error = nfs_idmap_new(clp);
1362 if (error < 0) {
1363 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001364 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001365 goto error;
1366 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001367 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001368
Andy Adamson557134a2009-04-01 09:21:53 -04001369 error = nfs4_init_client_minor_version(clp);
1370 if (error < 0)
1371 goto error;
1372
Andy Adamson76db6d92009-04-01 09:22:38 -04001373 if (!nfs4_has_session(clp))
1374 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001375 return 0;
1376
1377error:
1378 nfs_mark_client_ready(clp, error);
1379 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1380 return error;
1381}
1382
1383/*
1384 * Set up an NFS4 client
1385 */
1386static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001387 const char *hostname,
1388 const struct sockaddr *addr,
1389 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001390 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001391 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001392 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001393 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001394{
Trond Myklebust3a498022007-12-14 14:56:04 -05001395 struct nfs_client_initdata cl_init = {
1396 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001397 .addr = addr,
1398 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001399 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001400 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001401 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001402 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001403 };
David Howells54ceac42006-08-22 20:06:13 -04001404 struct nfs_client *clp;
1405 int error;
1406
1407 dprintk("--> nfs4_set_client()\n");
1408
1409 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001410 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1411 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001412 if (IS_ERR(clp)) {
1413 error = PTR_ERR(clp);
1414 goto error;
1415 }
David Howells54ceac42006-08-22 20:06:13 -04001416
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001417 /*
1418 * Query for the lease time on clientid setup or renewal
1419 *
1420 * Note that this will be set on nfs_clients that were created
1421 * only for the DS role and did not set this bit, but now will
1422 * serve a dual role.
1423 */
1424 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1425
David Howells54ceac42006-08-22 20:06:13 -04001426 server->nfs_client = clp;
1427 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1428 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001429error:
1430 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1431 return error;
1432}
1433
Andy Adamsond83217c2011-03-01 01:34:17 +00001434/*
1435 * Set up a pNFS Data Server client.
1436 *
1437 * Return any existing nfs_client that matches server address,port,version
1438 * and minorversion.
1439 *
1440 * For a new nfs_client, use a soft mount (default), a low retrans and a
1441 * low timeout interval so that if a connection is lost, we retry through
1442 * the MDS.
1443 */
1444struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1445 const struct sockaddr *ds_addr,
1446 int ds_addrlen, int ds_proto)
1447{
1448 struct nfs_client_initdata cl_init = {
1449 .addr = ds_addr,
1450 .addrlen = ds_addrlen,
1451 .rpc_ops = &nfs_v4_clientops,
1452 .proto = ds_proto,
1453 .minorversion = mds_clp->cl_minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001454 .net = mds_clp->net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001455 };
1456 struct rpc_timeout ds_timeout = {
1457 .to_initval = 15 * HZ,
1458 .to_maxval = 15 * HZ,
1459 .to_retries = 1,
1460 .to_exponential = 1,
1461 };
1462 struct nfs_client *clp;
1463
1464 /*
1465 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1466 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1467 * (section 13.1 RFC 5661).
1468 */
1469 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1470 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1471
1472 dprintk("<-- %s %p\n", __func__, clp);
1473 return clp;
1474}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001475EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001476
1477/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001478 * Session has been established, and the client marked ready.
1479 * Set the mount rsize and wsize with negotiated fore channel
1480 * attributes which will be bound checked in nfs_server_set_fsinfo.
1481 */
1482static void nfs4_session_set_rwsize(struct nfs_server *server)
1483{
1484#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001485 struct nfs4_session *sess;
1486 u32 server_resp_sz;
1487 u32 server_rqst_sz;
1488
Andy Adamson96b09e02009-04-01 09:22:33 -04001489 if (!nfs4_has_session(server->nfs_client))
1490 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001491 sess = server->nfs_client->cl_session;
1492 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1493 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1494
1495 if (server->rsize > server_resp_sz)
1496 server->rsize = server_resp_sz;
1497 if (server->wsize > server_rqst_sz)
1498 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001499#endif /* CONFIG_NFS_V4_1 */
1500}
1501
Trond Myklebust44950b62010-06-17 11:45:12 -04001502static int nfs4_server_common_setup(struct nfs_server *server,
1503 struct nfs_fh *mntfh)
1504{
1505 struct nfs_fattr *fattr;
1506 int error;
1507
1508 BUG_ON(!server->nfs_client);
1509 BUG_ON(!server->nfs_client->rpc_ops);
1510 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1511
Andy Adamson94de8b22011-03-01 01:34:12 +00001512 /* data servers support only a subset of NFSv4.1 */
1513 if (is_ds_only_client(server->nfs_client))
1514 return -EPROTONOSUPPORT;
1515
Trond Myklebust44950b62010-06-17 11:45:12 -04001516 fattr = nfs_alloc_fattr();
1517 if (fattr == NULL)
1518 return -ENOMEM;
1519
1520 /* We must ensure the session is initialised first */
1521 error = nfs4_init_session(server);
1522 if (error < 0)
1523 goto out;
1524
1525 /* Probe the root fh to retrieve its FSID and filehandle */
1526 error = nfs4_get_rootfh(server, mntfh);
1527 if (error < 0)
1528 goto out;
1529
1530 dprintk("Server FSID: %llx:%llx\n",
1531 (unsigned long long) server->fsid.major,
1532 (unsigned long long) server->fsid.minor);
1533 dprintk("Mount FH: %d\n", mntfh->size);
1534
1535 nfs4_session_set_rwsize(server);
1536
1537 error = nfs_probe_fsinfo(server, mntfh, fattr);
1538 if (error < 0)
1539 goto out;
1540
1541 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1542 server->namelen = NFS4_MAXNAMLEN;
1543
Chuck Leverfca52382010-12-24 01:32:32 +00001544 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001545 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001546 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001547out:
1548 nfs_free_fattr(fattr);
1549 return error;
1550}
1551
Andy Adamson96b09e02009-04-01 09:22:33 -04001552/*
David Howells54ceac42006-08-22 20:06:13 -04001553 * Create a version 4 volume record
1554 */
1555static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001556 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001557{
Trond Myklebust33170232007-12-20 16:03:59 -05001558 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001559 int error;
1560
1561 dprintk("--> nfs4_init_server()\n");
1562
Trond Myklebust33170232007-12-20 16:03:59 -05001563 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1564 data->timeo, data->retrans);
1565
Chuck Lever542fcc32008-12-23 15:21:36 -05001566 /* Initialise the client representation from the mount data */
1567 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001568 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1569 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1570 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001571 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001572
Trond Myklebust33170232007-12-20 16:03:59 -05001573 /* Get a client record */
1574 error = nfs4_set_client(server,
1575 data->nfs_server.hostname,
1576 (const struct sockaddr *)&data->nfs_server.address,
1577 data->nfs_server.addrlen,
1578 data->client_address,
1579 data->auth_flavors[0],
1580 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001581 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001582 data->minorversion,
1583 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001584 if (error < 0)
1585 goto error;
1586
Trond Myklebustb064eca22011-02-22 15:44:32 -08001587 /*
1588 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1589 * authentication.
1590 */
1591 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1592 server->caps |= NFS_CAP_UIDGID_NOMAP;
1593
David Howells54ceac42006-08-22 20:06:13 -04001594 if (data->rsize)
1595 server->rsize = nfs_block_size(data->rsize, NULL);
1596 if (data->wsize)
1597 server->wsize = nfs_block_size(data->wsize, NULL);
1598
1599 server->acregmin = data->acregmin * HZ;
1600 server->acregmax = data->acregmax * HZ;
1601 server->acdirmin = data->acdirmin * HZ;
1602 server->acdirmax = data->acdirmax * HZ;
1603
Chuck Leverf22d6d72008-03-14 14:10:22 -04001604 server->port = data->nfs_server.port;
1605
Trond Myklebust33170232007-12-20 16:03:59 -05001606 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001607
Trond Myklebust33170232007-12-20 16:03:59 -05001608error:
David Howells54ceac42006-08-22 20:06:13 -04001609 /* Done */
1610 dprintk("<-- nfs4_init_server() = %d\n", error);
1611 return error;
1612}
1613
1614/*
1615 * Create a version 4 volume record
1616 * - keyed on server and FSID
1617 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001618struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001619 struct nfs_fh *mntfh)
1620{
David Howells54ceac42006-08-22 20:06:13 -04001621 struct nfs_server *server;
1622 int error;
1623
1624 dprintk("--> nfs4_create_server()\n");
1625
1626 server = nfs_alloc_server();
1627 if (!server)
1628 return ERR_PTR(-ENOMEM);
1629
David Howells54ceac42006-08-22 20:06:13 -04001630 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001631 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001632 if (error < 0)
1633 goto error;
1634
Trond Myklebust44950b62010-06-17 11:45:12 -04001635 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001636 if (error < 0)
1637 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001638
David Howells54ceac42006-08-22 20:06:13 -04001639 dprintk("<-- nfs4_create_server() = %p\n", server);
1640 return server;
1641
1642error:
1643 nfs_free_server(server);
1644 dprintk("<-- nfs4_create_server() = error %d\n", error);
1645 return ERR_PTR(error);
1646}
1647
1648/*
1649 * Create an NFS4 referral server record
1650 */
1651struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001652 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001653{
1654 struct nfs_client *parent_client;
1655 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001656 int error;
1657
1658 dprintk("--> nfs4_create_referral_server()\n");
1659
1660 server = nfs_alloc_server();
1661 if (!server)
1662 return ERR_PTR(-ENOMEM);
1663
1664 parent_server = NFS_SB(data->sb);
1665 parent_client = parent_server->nfs_client;
1666
Chuck Lever542fcc32008-12-23 15:21:36 -05001667 /* Initialise the client representation from the parent server */
1668 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001669 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001670
David Howells54ceac42006-08-22 20:06:13 -04001671 /* Get a client representation.
1672 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001673 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001674 data->addr,
1675 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001676 parent_client->cl_ipaddr,
1677 data->authflavor,
1678 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001679 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001680 parent_client->cl_mvops->minor_version,
1681 parent_client->net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001682 if (error < 0)
1683 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001684
Trond Myklebust33170232007-12-20 16:03:59 -05001685 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001686 if (error < 0)
1687 goto error;
1688
Trond Myklebust44950b62010-06-17 11:45:12 -04001689 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001690 if (error < 0)
1691 goto error;
1692
David Howells54ceac42006-08-22 20:06:13 -04001693 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1694 return server;
1695
1696error:
1697 nfs_free_server(server);
1698 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1699 return ERR_PTR(error);
1700}
1701
1702#endif /* CONFIG_NFS_V4 */
1703
1704/*
1705 * Clone an NFS2, NFS3 or NFS4 server record
1706 */
1707struct nfs_server *nfs_clone_server(struct nfs_server *source,
1708 struct nfs_fh *fh,
1709 struct nfs_fattr *fattr)
1710{
1711 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001712 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001713 int error;
1714
1715 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001716 (unsigned long long) fattr->fsid.major,
1717 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001718
1719 server = nfs_alloc_server();
1720 if (!server)
1721 return ERR_PTR(-ENOMEM);
1722
Trond Myklebustfbca7792010-04-16 16:22:46 -04001723 error = -ENOMEM;
1724 fattr_fsinfo = nfs_alloc_fattr();
1725 if (fattr_fsinfo == NULL)
1726 goto out_free_server;
1727
David Howells54ceac42006-08-22 20:06:13 -04001728 /* Copy data from the source */
1729 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001730 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001731 atomic_inc(&server->nfs_client->cl_count);
1732 nfs_server_copy_userdata(server, source);
1733
1734 server->fsid = fattr->fsid;
1735
Trond Myklebust33170232007-12-20 16:03:59 -05001736 error = nfs_init_server_rpcclient(server,
1737 source->client->cl_timeout,
1738 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001739 if (error < 0)
1740 goto out_free_server;
1741 if (!IS_ERR(source->client_acl))
1742 nfs_init_server_aclclient(server);
1743
1744 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001745 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001746 if (error < 0)
1747 goto out_free_server;
1748
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001749 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1750 server->namelen = NFS4_MAXNAMLEN;
1751
David Howells54ceac42006-08-22 20:06:13 -04001752 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001753 (unsigned long long) server->fsid.major,
1754 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001755
1756 error = nfs_start_lockd(server);
1757 if (error < 0)
1758 goto out_free_server;
1759
Chuck Leverfca52382010-12-24 01:32:32 +00001760 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001761 server->mount_time = jiffies;
1762
Trond Myklebustfbca7792010-04-16 16:22:46 -04001763 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001764 dprintk("<-- nfs_clone_server() = %p\n", server);
1765 return server;
1766
1767out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001768 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001769 nfs_free_server(server);
1770 dprintk("<-- nfs_clone_server() = error %d\n", error);
1771 return ERR_PTR(error);
1772}
David Howells6aaca562006-08-22 20:06:13 -04001773
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001774void nfs_clients_init(struct net *net)
1775{
1776 struct nfs_net *nn = net_generic(net, nfs_net_id);
1777
1778 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001779 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001780#ifdef CONFIG_NFS_V4
1781 idr_init(&nn->cb_ident_idr);
1782#endif
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001783 spin_lock_init(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001784}
1785
David Howells6aaca562006-08-22 20:06:13 -04001786#ifdef CONFIG_PROC_FS
1787static struct proc_dir_entry *proc_fs_nfs;
1788
1789static int nfs_server_list_open(struct inode *inode, struct file *file);
1790static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1791static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1792static void nfs_server_list_stop(struct seq_file *p, void *v);
1793static int nfs_server_list_show(struct seq_file *m, void *v);
1794
James Morris88e9d342009-09-22 16:43:43 -07001795static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001796 .start = nfs_server_list_start,
1797 .next = nfs_server_list_next,
1798 .stop = nfs_server_list_stop,
1799 .show = nfs_server_list_show,
1800};
1801
Arjan van de Ven00977a52007-02-12 00:55:34 -08001802static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001803 .open = nfs_server_list_open,
1804 .read = seq_read,
1805 .llseek = seq_lseek,
1806 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001807 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001808};
1809
1810static int nfs_volume_list_open(struct inode *inode, struct file *file);
1811static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1812static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1813static void nfs_volume_list_stop(struct seq_file *p, void *v);
1814static int nfs_volume_list_show(struct seq_file *m, void *v);
1815
James Morris88e9d342009-09-22 16:43:43 -07001816static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001817 .start = nfs_volume_list_start,
1818 .next = nfs_volume_list_next,
1819 .stop = nfs_volume_list_stop,
1820 .show = nfs_volume_list_show,
1821};
1822
Arjan van de Ven00977a52007-02-12 00:55:34 -08001823static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001824 .open = nfs_volume_list_open,
1825 .read = seq_read,
1826 .llseek = seq_lseek,
1827 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001828 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001829};
1830
1831/*
1832 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1833 * we're dealing
1834 */
1835static int nfs_server_list_open(struct inode *inode, struct file *file)
1836{
1837 struct seq_file *m;
1838 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001839 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1840 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001841
1842 ret = seq_open(file, &nfs_server_list_ops);
1843 if (ret < 0)
1844 return ret;
1845
1846 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001847 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001848
1849 return 0;
1850}
1851
1852/*
1853 * set up the iterator to start reading from the server list and return the first item
1854 */
1855static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1856{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001857 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1858
David Howells6aaca562006-08-22 20:06:13 -04001859 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001860 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001861 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001862}
1863
1864/*
1865 * move to next server
1866 */
1867static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1868{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001869 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1870
1871 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001872}
1873
1874/*
1875 * clean up after reading from the transports list
1876 */
1877static void nfs_server_list_stop(struct seq_file *p, void *v)
1878{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001879 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1880
1881 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001882}
1883
1884/*
1885 * display a header line followed by a load of call lines
1886 */
1887static int nfs_server_list_show(struct seq_file *m, void *v)
1888{
1889 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001890 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001891
1892 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001893 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001894 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1895 return 0;
1896 }
1897
1898 /* display one transport per line on subsequent lines */
1899 clp = list_entry(v, struct nfs_client, cl_share_link);
1900
Malahal Naineni940aab42011-09-20 17:27:14 -07001901 /* Check if the client is initialized */
1902 if (clp->cl_cons_state != NFS_CS_READY)
1903 return 0;
1904
Chuck Lever5d8515c2007-12-10 14:57:16 -05001905 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001906 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001907 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1908 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001909 atomic_read(&clp->cl_count),
1910 clp->cl_hostname);
1911
1912 return 0;
1913}
1914
1915/*
1916 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1917 */
1918static int nfs_volume_list_open(struct inode *inode, struct file *file)
1919{
1920 struct seq_file *m;
1921 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001922 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1923 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001924
1925 ret = seq_open(file, &nfs_volume_list_ops);
1926 if (ret < 0)
1927 return ret;
1928
1929 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001930 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001931
1932 return 0;
1933}
1934
1935/*
1936 * set up the iterator to start reading from the volume list and return the first item
1937 */
1938static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1939{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001940 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1941
David Howells6aaca562006-08-22 20:06:13 -04001942 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001943 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001944 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001945}
1946
1947/*
1948 * move to next volume
1949 */
1950static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1951{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001952 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1953
1954 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001955}
1956
1957/*
1958 * clean up after reading from the transports list
1959 */
1960static void nfs_volume_list_stop(struct seq_file *p, void *v)
1961{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001962 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1963
1964 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001965}
1966
1967/*
1968 * display a header line followed by a load of call lines
1969 */
1970static int nfs_volume_list_show(struct seq_file *m, void *v)
1971{
1972 struct nfs_server *server;
1973 struct nfs_client *clp;
1974 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001975 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001976
1977 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001978 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001979 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001980 return 0;
1981 }
1982 /* display one transport per line on subsequent lines */
1983 server = list_entry(v, struct nfs_server, master_link);
1984 clp = server->nfs_client;
1985
1986 snprintf(dev, 8, "%u:%u",
1987 MAJOR(server->s_dev), MINOR(server->s_dev));
1988
1989 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001990 (unsigned long long) server->fsid.major,
1991 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001992
David Howells5d1acff2009-04-03 16:42:47 +01001993 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001994 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001995 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1996 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001997 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001998 fsid,
1999 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04002000
2001 return 0;
2002}
2003
2004/*
2005 * initialise the /proc/fs/nfsfs/ directory
2006 */
2007int __init nfs_fs_proc_init(void)
2008{
2009 struct proc_dir_entry *p;
2010
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002011 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002012 if (!proc_fs_nfs)
2013 goto error_0;
2014
David Howells6aaca562006-08-22 20:06:13 -04002015 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002016 p = proc_create("servers", S_IFREG|S_IRUGO,
2017 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002018 if (!p)
2019 goto error_1;
2020
David Howells6aaca562006-08-22 20:06:13 -04002021 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002022 p = proc_create("volumes", S_IFREG|S_IRUGO,
2023 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002024 if (!p)
2025 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002026 return 0;
2027
2028error_2:
2029 remove_proc_entry("servers", proc_fs_nfs);
2030error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002031 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002032error_0:
2033 return -ENOMEM;
2034}
2035
2036/*
2037 * clean up the /proc/fs/nfsfs/ directory
2038 */
2039void nfs_fs_proc_exit(void)
2040{
2041 remove_proc_entry("volumes", proc_fs_nfs);
2042 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002043 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002044}
2045
2046#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002047
2048module_param(nfs4_disable_idmapping, bool, 0644);
2049MODULE_PARM_DESC(nfs4_disable_idmapping,
2050 "Turn off NFSv4 idmapping when using 'sec=sys'");