blob: f1f047c376d9a43a84adcdde138d96d13402b43d [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;
Trond Myklebustabd96692012-02-19 08:46:49 +0100175 clp->net = get_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{
Trond Myklebust7df529a2012-02-26 17:34:22 -0500207 if (nfs4_has_session(clp)) {
208 nfs4_deviceid_purge_client(clp);
Andy Adamson557134a2009-04-01 09:21:53 -0400209 nfs4_destroy_session(clp->cl_session);
Trond Myklebust7df529a2012-02-26 17:34:22 -0500210 }
211
Andy Adamson557134a2009-04-01 09:21:53 -0400212}
Andy Adamsonea005282011-01-06 02:04:29 +0000213#else /* CONFIG_NFS_V4_1 */
214static void nfs4_shutdown_session(struct nfs_client *clp)
215{
216}
217#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400218
219/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800220 * Destroy the NFS4 callback service
221 */
222static void nfs4_destroy_callback(struct nfs_client *clp)
223{
224 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400225 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800226}
227
228static void nfs4_shutdown_client(struct nfs_client *clp)
229{
230 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
231 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000232 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800233 nfs4_destroy_callback(clp);
234 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
235 nfs_idmap_delete(clp);
236
237 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
238}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000239
240/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000241void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000242{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000243 struct nfs_net *nn = net_generic(net, nfs_net_id);
244
245 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000246}
247
248/* nfs_client_lock held */
249static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
250{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000251 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
252
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000253 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000254 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000255}
256
Fred Isamanf7e89172011-01-06 11:36:32 +0000257static void pnfs_init_server(struct nfs_server *server)
258{
259 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
260}
261
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500262static void nfs4_destroy_server(struct nfs_server *server)
263{
264 nfs4_purge_state_owners(server);
265}
266
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800267#else
268static void nfs4_shutdown_client(struct nfs_client *clp)
269{
270}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000271
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000272void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000273{
274}
275
276static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
277{
278}
Fred Isamanf7e89172011-01-06 11:36:32 +0000279
280static void pnfs_init_server(struct nfs_server *server)
281{
282}
283
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800284#endif /* CONFIG_NFS_V4 */
285
286/*
David Howells24c8dbb2006-08-22 20:06:10 -0400287 * Destroy a shared client record
288 */
289static void nfs_free_client(struct nfs_client *clp)
290{
Trond Myklebust40c553192007-12-14 14:56:07 -0500291 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400292
Trond Myklebust5dd31772006-08-24 01:03:05 -0400293 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400294
David Howells14727282009-04-03 16:42:42 +0100295 nfs_fscache_release_client_cookie(clp);
296
David Howells24c8dbb2006-08-22 20:06:10 -0400297 /* -EIO all pending I/O */
298 if (!IS_ERR(clp->cl_rpcclient))
299 rpc_shutdown_client(clp->cl_rpcclient);
300
Trond Myklebust7c67db32008-04-07 20:50:11 -0400301 if (clp->cl_machine_cred != NULL)
302 put_rpccred(clp->cl_machine_cred);
303
Trond Myklebustabd96692012-02-19 08:46:49 +0100304 put_net(clp->net);
David Howells24c8dbb2006-08-22 20:06:10 -0400305 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400306 kfree(clp->server_scope);
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -0500307 kfree(clp->impl_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400308 kfree(clp);
309
310 dprintk("<-- nfs_free_client()\n");
311}
312
313/*
314 * Release a reference to a shared client record
315 */
316void nfs_put_client(struct nfs_client *clp)
317{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000318 struct nfs_net *nn;
319
David Howells27ba8512006-07-30 14:40:56 -0400320 if (!clp)
321 return;
322
David Howells24c8dbb2006-08-22 20:06:10 -0400323 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000324 nn = net_generic(clp->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400325
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000326 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
David Howells24c8dbb2006-08-22 20:06:10 -0400327 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000328 nfs_cb_idr_remove_locked(clp);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000329 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400330
331 BUG_ON(!list_empty(&clp->cl_superblocks));
332
333 nfs_free_client(clp);
334 }
335}
Andy Adamson16b374c2010-10-20 00:18:04 -0400336EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400337
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500338#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400339/*
340 * Test if two ip6 socket addresses refer to the same socket by
341 * comparing relevant fields. The padding bytes specifically, are not
342 * compared. sin6_flowinfo is not compared because it only affects QoS
343 * and sin6_scope_id is only compared if the address is "link local"
344 * because "link local" addresses need only be unique to a specific
345 * link. Conversely, ordinary unicast addresses might have different
346 * sin6_scope_id.
347 *
348 * The caller should ensure both socket addresses are AF_INET6.
349 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400350static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
351 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400352{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400353 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
354 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400355
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800356 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400357 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800358 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
359 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500360
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800361 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500362}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400363#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
364static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
365 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400366{
367 return 0;
368}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500369#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500370
David Howells24c8dbb2006-08-22 20:06:10 -0400371/*
Ian Dalld7371c42009-03-10 20:33:22 -0400372 * Test if two ip4 socket addresses refer to the same socket, by
373 * comparing relevant fields. The padding bytes specifically, are
374 * not compared.
375 *
376 * The caller should ensure both socket addresses are AF_INET.
377 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400378static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
379 const struct sockaddr *sa2)
380{
381 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
382 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
383
384 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
385}
386
387static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
388 const struct sockaddr *sa2)
389{
390 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
391 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
392
393 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
394 (sin1->sin6_port == sin2->sin6_port);
395}
396
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400397static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
398 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400399{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400400 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
401 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400402
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400403 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
404 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400405}
406
407/*
Ian Dalld7371c42009-03-10 20:33:22 -0400408 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400409 * by comparing (only) relevant fields, excluding the port number.
410 */
411static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
412 const struct sockaddr *sa2)
413{
414 if (sa1->sa_family != sa2->sa_family)
415 return 0;
416
417 switch (sa1->sa_family) {
418 case AF_INET:
419 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
420 case AF_INET6:
421 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
422 }
423 return 0;
424}
425
426/*
427 * Test if two socket addresses represent the same actual socket,
428 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400429 */
430static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
431 const struct sockaddr *sa2)
432{
433 if (sa1->sa_family != sa2->sa_family)
434 return 0;
435
436 switch (sa1->sa_family) {
437 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400438 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400439 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400440 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400441 }
442 return 0;
443}
444
Andy Adamsonc36fca52011-01-06 02:04:32 +0000445/* Common match routine for v4.0 and v4.1 callback services */
Trond Myklebust17280172012-03-11 13:11:00 -0400446static bool nfs4_cb_match_client(const struct sockaddr *addr,
447 struct nfs_client *clp, u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500448{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000449 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500450
Andy Adamsonc36fca52011-01-06 02:04:32 +0000451 /* Don't match clients that failed to initialise */
452 if (!(clp->cl_cons_state == NFS_CS_READY ||
453 clp->cl_cons_state == NFS_CS_SESSION_INITING))
454 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500455
Andy Adamsonc36fca52011-01-06 02:04:32 +0000456 /* Match the version and minorversion */
457 if (clp->rpc_ops->version != 4 ||
458 clp->cl_minorversion != minorversion)
459 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500460
Andy Adamsonc36fca52011-01-06 02:04:32 +0000461 /* Match only the IP address, not the port number */
462 if (!nfs_sockaddr_match_ipaddr(addr, clap))
463 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500464
Andy Adamsonc36fca52011-01-06 02:04:32 +0000465 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500466}
467
468/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500469 * Find an nfs_client on the list that matches the initialisation data
470 * that is supplied.
471 */
472static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400473{
474 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400475 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000476 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400477
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000478 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400479 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700480 /* Don't match clients that failed to initialise properly */
481 if (clp->cl_cons_state < 0)
482 continue;
483
David Howells24c8dbb2006-08-22 20:06:10 -0400484 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500485 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400486 continue;
487
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500488 if (clp->cl_proto != data->proto)
489 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400490 /* Match nfsv4 minorversion */
491 if (clp->cl_minorversion != data->minorversion)
492 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500493 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400494 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400495 continue;
496
Trond Myklebustc81468a2007-12-14 14:56:05 -0500497 atomic_inc(&clp->cl_count);
498 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400499 }
David Howells24c8dbb2006-08-22 20:06:10 -0400500 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400501}
502
503/*
504 * Look up a client by IP address and protocol version
505 * - creates a new record if one doesn't yet exist
506 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000507static struct nfs_client *
508nfs_get_client(const struct nfs_client_initdata *cl_init,
509 const struct rpc_timeout *timeparms,
510 const char *ip_addr,
511 rpc_authflavor_t authflavour,
512 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400513{
514 struct nfs_client *clp, *new = NULL;
515 int error;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000516 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400517
Chuck Leverd7422c42007-12-10 14:58:51 -0500518 dprintk("--> nfs_get_client(%s,v%u)\n",
519 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400520
521 /* see if the client already exists */
522 do {
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000523 spin_lock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400524
Trond Myklebustc81468a2007-12-14 14:56:05 -0500525 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400526 if (clp)
527 goto found_client;
528 if (new)
529 goto install_client;
530
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000531 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400532
Trond Myklebust3a498022007-12-14 14:56:04 -0500533 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700534 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400535
Chuck Levera21bdd92009-06-17 18:02:10 -0700536 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
537 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400538
539 /* install a new client and return with it unready */
540install_client:
541 clp = new;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000542 list_add(&clp->cl_share_link, &nn->nfs_client_list);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000543 spin_unlock(&nn->nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000544
545 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
546 authflavour, noresvport);
547 if (error < 0) {
548 nfs_put_client(clp);
549 return ERR_PTR(error);
550 }
David Howells24c8dbb2006-08-22 20:06:10 -0400551 dprintk("--> nfs_get_client() = %p [new]\n", clp);
552 return clp;
553
554 /* found an existing client
555 * - make sure it's ready before returning
556 */
557found_client:
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000558 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400559
560 if (new)
561 nfs_free_client(new);
562
Matthew Wilcox150030b2007-12-06 16:24:39 -0500563 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400564 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400565 if (error < 0) {
566 nfs_put_client(clp);
567 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400568 }
569
570 if (clp->cl_cons_state < NFS_CS_READY) {
571 error = clp->cl_cons_state;
572 nfs_put_client(clp);
573 return ERR_PTR(error);
574 }
575
David Howells54ceac42006-08-22 20:06:13 -0400576 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
577
David Howells24c8dbb2006-08-22 20:06:10 -0400578 dprintk("--> nfs_get_client() = %p [share]\n", clp);
579 return clp;
580}
581
582/*
583 * Mark a server as ready or failed
584 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400585void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400586{
587 clp->cl_cons_state = state;
588 wake_up_all(&nfs_client_active_wq);
589}
David Howells5006a762006-08-22 20:06:12 -0400590
591/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400592 * With sessions, the client is not marked ready until after a
593 * successful EXCHANGE_ID and CREATE_SESSION.
594 *
595 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
596 * other versions of NFS can be tried.
597 */
598int nfs4_check_client_ready(struct nfs_client *clp)
599{
600 if (!nfs4_has_session(clp))
601 return 0;
602 if (clp->cl_cons_state < NFS_CS_READY)
603 return -EPROTONOSUPPORT;
604 return 0;
605}
606
607/*
David Howells5006a762006-08-22 20:06:12 -0400608 * Initialise the timeout values for a connection
609 */
610static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
611 unsigned int timeo, unsigned int retrans)
612{
613 to->to_initval = timeo * HZ / 10;
614 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400615
616 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400617 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400618 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400619 if (to->to_retries == 0)
620 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500621 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400622 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400623 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
624 to->to_initval = NFS_MAX_TCP_TIMEOUT;
625 to->to_increment = to->to_initval;
626 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500627 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
628 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
629 if (to->to_maxval < to->to_initval)
630 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400631 to->to_exponential = 0;
632 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400633 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400634 if (to->to_retries == 0)
635 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400636 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400637 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400638 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
639 to->to_initval = NFS_MAX_UDP_TIMEOUT;
640 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
641 to->to_exponential = 1;
642 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400643 default:
644 BUG();
David Howells5006a762006-08-22 20:06:12 -0400645 }
646}
647
648/*
649 * Create an RPC client handle
650 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500651static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500652 const struct rpc_timeout *timeparms,
653 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500654 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400655{
David Howells5006a762006-08-22 20:06:12 -0400656 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400657 struct rpc_create_args args = {
Stanislav Kinsbursky6d59b8d2012-01-10 16:12:54 +0400658 .net = clp->net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500659 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400660 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500661 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500662 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400663 .servername = clp->cl_hostname,
664 .program = &nfs_program,
665 .version = clp->rpc_ops->version,
666 .authflavor = flavor,
667 };
David Howells5006a762006-08-22 20:06:12 -0400668
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500669 if (discrtry)
670 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
671 if (noresvport)
672 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
673
David Howells5006a762006-08-22 20:06:12 -0400674 if (!IS_ERR(clp->cl_rpcclient))
675 return 0;
676
Chuck Lever41877d22006-08-22 20:06:20 -0400677 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400678 if (IS_ERR(clnt)) {
679 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700680 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400681 return PTR_ERR(clnt);
682 }
683
David Howells5006a762006-08-22 20:06:12 -0400684 clp->cl_rpcclient = clnt;
685 return 0;
686}
David Howells54ceac42006-08-22 20:06:13 -0400687
688/*
689 * Version 2 or 3 client destruction
690 */
691static void nfs_destroy_server(struct nfs_server *server)
692{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400693 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
694 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500695 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400696}
697
698/*
699 * Version 2 or 3 lockd setup
700 */
701static int nfs_start_lockd(struct nfs_server *server)
702{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500703 struct nlm_host *host;
704 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500705 struct nlmclnt_initdata nlm_init = {
706 .hostname = clp->cl_hostname,
707 .address = (struct sockaddr *)&clp->cl_addr,
708 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500709 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500710 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
711 1 : 0,
Stanislav Kinsbursky66697bf2012-01-31 15:08:13 +0400712 .net = clp->net,
Chuck Lever883bb162008-01-15 16:04:20 -0500713 };
David Howells54ceac42006-08-22 20:06:13 -0400714
Chuck Lever883bb162008-01-15 16:04:20 -0500715 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500716 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400717 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
718 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500719 return 0;
720
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400721 switch (clp->cl_proto) {
722 default:
723 nlm_init.protocol = IPPROTO_TCP;
724 break;
725 case XPRT_TRANSPORT_UDP:
726 nlm_init.protocol = IPPROTO_UDP;
727 }
728
Chuck Lever883bb162008-01-15 16:04:20 -0500729 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500730 if (IS_ERR(host))
731 return PTR_ERR(host);
732
733 server->nlm_host = host;
734 server->destroy = nfs_destroy_server;
735 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400736}
737
738/*
739 * Initialise an NFSv3 ACL client connection
740 */
741#ifdef CONFIG_NFS_V3_ACL
742static void nfs_init_server_aclclient(struct nfs_server *server)
743{
Trond Myklebust40c553192007-12-14 14:56:07 -0500744 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400745 goto out_noacl;
746 if (server->flags & NFS_MOUNT_NOACL)
747 goto out_noacl;
748
749 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
750 if (IS_ERR(server->client_acl))
751 goto out_noacl;
752
753 /* No errors! Assume that Sun nfsacls are supported */
754 server->caps |= NFS_CAP_ACLS;
755 return;
756
757out_noacl:
758 server->caps &= ~NFS_CAP_ACLS;
759}
760#else
761static inline void nfs_init_server_aclclient(struct nfs_server *server)
762{
763 server->flags &= ~NFS_MOUNT_NOACL;
764 server->caps &= ~NFS_CAP_ACLS;
765}
766#endif
767
768/*
769 * Create a general RPC client
770 */
Trond Myklebust33170232007-12-20 16:03:59 -0500771static int nfs_init_server_rpcclient(struct nfs_server *server,
772 const struct rpc_timeout *timeo,
773 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400774{
775 struct nfs_client *clp = server->nfs_client;
776
777 server->client = rpc_clone_client(clp->cl_rpcclient);
778 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700779 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400780 return PTR_ERR(server->client);
781 }
782
Trond Myklebust33170232007-12-20 16:03:59 -0500783 memcpy(&server->client->cl_timeout_default,
784 timeo,
785 sizeof(server->client->cl_timeout_default));
786 server->client->cl_timeout = &server->client->cl_timeout_default;
787
David Howells54ceac42006-08-22 20:06:13 -0400788 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
789 struct rpc_auth *auth;
790
791 auth = rpcauth_create(pseudoflavour, server->client);
792 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700793 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400794 return PTR_ERR(auth);
795 }
796 }
797 server->client->cl_softrtry = 0;
798 if (server->flags & NFS_MOUNT_SOFT)
799 server->client->cl_softrtry = 1;
800
David Howells54ceac42006-08-22 20:06:13 -0400801 return 0;
802}
803
804/*
805 * Initialise an NFS2 or NFS3 client
806 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000807int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
808 const char *ip_addr, rpc_authflavor_t authflavour,
809 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400810{
David Howells54ceac42006-08-22 20:06:13 -0400811 int error;
812
813 if (clp->cl_cons_state == NFS_CS_READY) {
814 /* the client is already initialised */
815 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
816 return 0;
817 }
818
David Howells54ceac42006-08-22 20:06:13 -0400819 /*
820 * Create a client RPC handle for doing FSSTAT with UNIX auth only
821 * - RFC 2623, sec 2.3.2
822 */
Chuck Leverd7403512008-12-23 15:21:37 -0500823 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000824 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400825 if (error < 0)
826 goto error;
827 nfs_mark_client_ready(clp, NFS_CS_READY);
828 return 0;
829
830error:
831 nfs_mark_client_ready(clp, error);
832 dprintk("<-- nfs_init_client() = xerror %d\n", error);
833 return error;
834}
835
836/*
837 * Create a version 2 or 3 client
838 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400839static int nfs_init_server(struct nfs_server *server,
840 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400841{
Trond Myklebust3a498022007-12-14 14:56:04 -0500842 struct nfs_client_initdata cl_init = {
843 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500844 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500845 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500846 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500847 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400848 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500849 };
Trond Myklebust33170232007-12-20 16:03:59 -0500850 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400851 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500852 int error;
David Howells54ceac42006-08-22 20:06:13 -0400853
854 dprintk("--> nfs_init_server()\n");
855
856#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400857 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500858 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400859#endif
860
Andy Adamson45a52a02011-03-01 01:34:08 +0000861 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
862 data->timeo, data->retrans);
863
David Howells54ceac42006-08-22 20:06:13 -0400864 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000865 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
866 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400867 if (IS_ERR(clp)) {
868 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
869 return PTR_ERR(clp);
870 }
871
David Howells54ceac42006-08-22 20:06:13 -0400872 server->nfs_client = clp;
873
874 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400875 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100876 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400877 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
878 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
879 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400880
881 if (data->rsize)
882 server->rsize = nfs_block_size(data->rsize, NULL);
883 if (data->wsize)
884 server->wsize = nfs_block_size(data->wsize, NULL);
885
886 server->acregmin = data->acregmin * HZ;
887 server->acregmax = data->acregmax * HZ;
888 server->acdirmin = data->acdirmin * HZ;
889 server->acdirmax = data->acdirmax * HZ;
890
891 /* Start lockd here, before we might error out */
892 error = nfs_start_lockd(server);
893 if (error < 0)
894 goto error;
895
Chuck Leverf22d6d72008-03-14 14:10:22 -0400896 server->port = data->nfs_server.port;
897
Trond Myklebust33170232007-12-20 16:03:59 -0500898 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400899 if (error < 0)
900 goto error;
901
Chuck Lever3f8400d2008-03-14 14:10:30 -0400902 /* Preserve the values of mount_server-related mount options */
903 if (data->mount_server.addrlen) {
904 memcpy(&server->mountd_address, &data->mount_server.address,
905 data->mount_server.addrlen);
906 server->mountd_addrlen = data->mount_server.addrlen;
907 }
908 server->mountd_version = data->mount_server.version;
909 server->mountd_port = data->mount_server.port;
910 server->mountd_protocol = data->mount_server.protocol;
911
David Howells54ceac42006-08-22 20:06:13 -0400912 server->namelen = data->namlen;
913 /* Create a client RPC handle for the NFSv3 ACL management interface */
914 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400915 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
916 return 0;
917
918error:
919 server->nfs_client = NULL;
920 nfs_put_client(clp);
921 dprintk("<-- nfs_init_server() = xerror %d\n", error);
922 return error;
923}
924
925/*
926 * Load up the server record from information gained in an fsinfo record
927 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400928static void nfs_server_set_fsinfo(struct nfs_server *server,
929 struct nfs_fh *mntfh,
930 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400931{
932 unsigned long max_rpc_payload;
933
934 /* Work out a lot of parameters */
935 if (server->rsize == 0)
936 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
937 if (server->wsize == 0)
938 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
939
940 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
941 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
942 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
943 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
944
945 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
946 if (server->rsize > max_rpc_payload)
947 server->rsize = max_rpc_payload;
948 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
949 server->rsize = NFS_MAX_FILE_IO_SIZE;
950 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700951
Jens Axboed9938312009-06-12 14:45:52 +0200952 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400953 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
954
955 if (server->wsize > max_rpc_payload)
956 server->wsize = max_rpc_payload;
957 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
958 server->wsize = NFS_MAX_FILE_IO_SIZE;
959 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400960 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400961 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400962
David Howells54ceac42006-08-22 20:06:13 -0400963 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
964
965 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400966 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
967 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400968 if (server->dtsize > server->rsize)
969 server->dtsize = server->rsize;
970
971 if (server->flags & NFS_MOUNT_NOAC) {
972 server->acregmin = server->acregmax = 0;
973 server->acdirmin = server->acdirmax = 0;
974 }
975
976 server->maxfilesize = fsinfo->maxfilesize;
977
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700978 server->time_delta = fsinfo->time_delta;
979
David Howells54ceac42006-08-22 20:06:13 -0400980 /* We're airborne Set socket buffersize */
981 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
982}
983
984/*
985 * Probe filesystem information, including the FSID on v2/v3
986 */
987static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
988{
989 struct nfs_fsinfo fsinfo;
990 struct nfs_client *clp = server->nfs_client;
991 int error;
992
993 dprintk("--> nfs_probe_fsinfo()\n");
994
995 if (clp->rpc_ops->set_capabilities != NULL) {
996 error = clp->rpc_ops->set_capabilities(server, mntfh);
997 if (error < 0)
998 goto out_error;
999 }
1000
1001 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001002 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -04001003 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
1004 if (error < 0)
1005 goto out_error;
1006
Benny Halevy738fd0f32011-07-30 20:52:36 -04001007 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001008
1009 /* Get some general file system info */
1010 if (server->namelen == 0) {
1011 struct nfs_pathconf pathinfo;
1012
1013 pathinfo.fattr = fattr;
1014 nfs_fattr_init(fattr);
1015
1016 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1017 server->namelen = pathinfo.max_namelen;
1018 }
1019
1020 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1021 return 0;
1022
1023out_error:
1024 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1025 return error;
1026}
1027
1028/*
1029 * Copy useful information when duplicating a server record
1030 */
1031static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1032{
1033 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001034 target->rsize = source->rsize;
1035 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001036 target->acregmin = source->acregmin;
1037 target->acregmax = source->acregmax;
1038 target->acdirmin = source->acdirmin;
1039 target->acdirmax = source->acdirmax;
1040 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001041 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001042}
1043
Chuck Leverfca52382010-12-24 01:32:32 +00001044static void nfs_server_insert_lists(struct nfs_server *server)
1045{
1046 struct nfs_client *clp = server->nfs_client;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001047 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001048
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001049 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001050 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001051 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001052 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001053 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001054
1055}
1056
1057static void nfs_server_remove_lists(struct nfs_server *server)
1058{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001059 struct nfs_client *clp = server->nfs_client;
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001060 struct nfs_net *nn;
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001061
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001062 if (clp == NULL)
1063 return;
1064 nn = net_generic(clp->net, nfs_net_id);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001065 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001066 list_del_rcu(&server->client_link);
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001067 if (list_empty(&clp->cl_superblocks))
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001068 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001069 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001070 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001071
1072 synchronize_rcu();
1073}
1074
David Howells54ceac42006-08-22 20:06:13 -04001075/*
1076 * Allocate and initialise a server record
1077 */
1078static struct nfs_server *nfs_alloc_server(void)
1079{
1080 struct nfs_server *server;
1081
1082 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1083 if (!server)
1084 return NULL;
1085
1086 server->client = server->client_acl = ERR_PTR(-EINVAL);
1087
1088 /* Zero out the NFS state stuff */
1089 INIT_LIST_HEAD(&server->client_link);
1090 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001091 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001092 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001093 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001094
Steve Dicksonef818a22007-11-08 04:05:04 -05001095 atomic_set(&server->active, 0);
1096
David Howells54ceac42006-08-22 20:06:13 -04001097 server->io_stats = nfs_alloc_iostats();
1098 if (!server->io_stats) {
1099 kfree(server);
1100 return NULL;
1101 }
1102
Jens Axboe48d07642009-09-21 09:59:39 +02001103 if (bdi_init(&server->backing_dev_info)) {
1104 nfs_free_iostats(server->io_stats);
1105 kfree(server);
1106 return NULL;
1107 }
1108
Trond Myklebust9157c312012-01-17 22:04:24 -05001109 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001110 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001111 pnfs_init_server(server);
1112
David Howells54ceac42006-08-22 20:06:13 -04001113 return server;
1114}
1115
1116/*
1117 * Free up a server record
1118 */
1119void nfs_free_server(struct nfs_server *server)
1120{
1121 dprintk("--> nfs_free_server()\n");
1122
Chuck Leverfca52382010-12-24 01:32:32 +00001123 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001124 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001125
1126 if (server->destroy != NULL)
1127 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001128
1129 if (!IS_ERR(server->client_acl))
1130 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001131 if (!IS_ERR(server->client))
1132 rpc_shutdown_client(server->client);
1133
1134 nfs_put_client(server->nfs_client);
1135
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001136 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001137 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001138 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001139 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001140 kfree(server);
1141 nfs_release_automount_timer();
1142 dprintk("<-- nfs_free_server()\n");
1143}
1144
1145/*
1146 * Create a version 2 or 3 volume record
1147 * - keyed on server and FSID
1148 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001149struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001150 struct nfs_fh *mntfh)
1151{
1152 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001153 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001154 int error;
1155
1156 server = nfs_alloc_server();
1157 if (!server)
1158 return ERR_PTR(-ENOMEM);
1159
Trond Myklebustfbca7792010-04-16 16:22:46 -04001160 error = -ENOMEM;
1161 fattr = nfs_alloc_fattr();
1162 if (fattr == NULL)
1163 goto error;
1164
David Howells54ceac42006-08-22 20:06:13 -04001165 /* Get a client representation */
1166 error = nfs_init_server(server, data);
1167 if (error < 0)
1168 goto error;
1169
1170 BUG_ON(!server->nfs_client);
1171 BUG_ON(!server->nfs_client->rpc_ops);
1172 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1173
1174 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001175 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001176 if (error < 0)
1177 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001178 if (server->nfs_client->rpc_ops->version == 3) {
1179 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1180 server->namelen = NFS3_MAXNAMLEN;
1181 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1182 server->caps |= NFS_CAP_READDIRPLUS;
1183 } else {
1184 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1185 server->namelen = NFS2_MAXNAMLEN;
1186 }
1187
Trond Myklebustfbca7792010-04-16 16:22:46 -04001188 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1189 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001190 if (error < 0) {
1191 dprintk("nfs_create_server: getattr error = %d\n", -error);
1192 goto error;
1193 }
1194 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001195 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001196
David Howells6daabf12006-08-24 15:44:16 -04001197 dprintk("Server FSID: %llx:%llx\n",
1198 (unsigned long long) server->fsid.major,
1199 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001200
Chuck Leverfca52382010-12-24 01:32:32 +00001201 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001202 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001203 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001204 return server;
1205
1206error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001207 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001208 nfs_free_server(server);
1209 return ERR_PTR(error);
1210}
1211
1212#ifdef CONFIG_NFS_V4
1213/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001214 * NFSv4.0 callback thread helper
1215 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001216 * Find a client by callback identifier
1217 */
1218struct nfs_client *
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001219nfs4_find_client_ident(struct net *net, int cb_ident)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001220{
1221 struct nfs_client *clp;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001222 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001223
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001224 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001225 clp = idr_find(&nn->cb_ident_idr, cb_ident);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001226 if (clp)
1227 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001228 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001229 return clp;
1230}
1231
1232#if defined(CONFIG_NFS_V4_1)
1233/*
1234 * NFSv4.1 callback thread helper
1235 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1236 * minorversion, and sessionID
1237 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001238 * Returns NULL if no such client
1239 */
1240struct nfs_client *
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001241nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001242 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001243{
1244 struct nfs_client *clp;
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001245 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001246
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001247 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001248 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001249 if (nfs4_cb_match_client(addr, clp, 1) == false)
1250 continue;
1251
1252 if (!nfs4_has_session(clp))
1253 continue;
1254
Andy Adamson778be232011-01-25 15:38:01 +00001255 /* Match sessionid*/
1256 if (memcmp(clp->cl_session->sess_id.data,
1257 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001258 continue;
1259
1260 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001261 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001262 return clp;
1263 }
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001264 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001265 return NULL;
1266}
1267
1268#else /* CONFIG_NFS_V4_1 */
1269
1270struct nfs_client *
Stanislav Kinsburskyb6d1e832012-02-07 19:53:19 +04001271nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001272 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001273{
1274 return NULL;
1275}
1276#endif /* CONFIG_NFS_V4_1 */
1277
1278/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001279 * Initialize the NFS4 callback service
1280 */
1281static int nfs4_init_callback(struct nfs_client *clp)
1282{
1283 int error;
1284
1285 if (clp->rpc_ops->version == 4) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05001286 struct rpc_xprt *xprt;
1287
1288 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
1289
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001290 if (nfs4_has_session(clp)) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05001291 error = xprt_setup_backchannel(xprt,
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001292 NFS41_BC_MIN_CALLBACKS);
1293 if (error < 0)
1294 return error;
1295 }
1296
Trond Myklebust2446ab62012-03-01 17:00:56 -05001297 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001298 if (error < 0) {
1299 dprintk("%s: failed to start callback. Error = %d\n",
1300 __func__, error);
1301 return error;
1302 }
1303 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1304 }
1305 return 0;
1306}
1307
1308/*
Andy Adamson557134a2009-04-01 09:21:53 -04001309 * Initialize the minor version specific parts of an NFS4 client record
1310 */
1311static int nfs4_init_client_minor_version(struct nfs_client *clp)
1312{
1313#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001314 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001315 struct nfs4_session *session = NULL;
1316 /*
1317 * Create the session and mark it expired.
1318 * When a SEQUENCE operation encounters the expired session
1319 * it will do session recovery to initialize it.
1320 */
1321 session = nfs4_alloc_session(clp);
1322 if (!session)
1323 return -ENOMEM;
1324
1325 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001326 /*
1327 * The create session reply races with the server back
1328 * channel probe. Mark the client NFS_CS_SESSION_INITING
1329 * so that the client back channel can find the
1330 * nfs_client struct
1331 */
1332 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001333 }
1334#endif /* CONFIG_NFS_V4_1 */
1335
Benny Halevy71468512009-04-01 09:22:56 -04001336 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001337}
1338
1339/*
David Howells54ceac42006-08-22 20:06:13 -04001340 * Initialise an NFS4 client record
1341 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001342int nfs4_init_client(struct nfs_client *clp,
1343 const struct rpc_timeout *timeparms,
1344 const char *ip_addr,
1345 rpc_authflavor_t authflavour,
1346 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001347{
Chuck Lever31b8e2a2012-03-01 17:01:23 -05001348 char buf[INET6_ADDRSTRLEN + 1];
David Howells54ceac42006-08-22 20:06:13 -04001349 int error;
1350
1351 if (clp->cl_cons_state == NFS_CS_READY) {
1352 /* the client is initialised already */
1353 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1354 return 0;
1355 }
1356
1357 /* Check NFS protocol revision and initialize RPC op vector */
1358 clp->rpc_ops = &nfs_v4_clientops;
1359
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001360 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001361 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001362 if (error < 0)
1363 goto error;
Chuck Lever31b8e2a2012-03-01 17:01:23 -05001364
1365 /* If no clientaddr= option was specified, find a usable cb address */
1366 if (ip_addr == NULL) {
1367 struct sockaddr_storage cb_addr;
1368 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
1369
1370 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
1371 if (error < 0)
1372 goto error;
1373 error = rpc_ntop(sap, buf, sizeof(buf));
1374 if (error < 0)
1375 goto error;
1376 ip_addr = (const char *)buf;
1377 }
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001378 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001379
1380 error = nfs_idmap_new(clp);
1381 if (error < 0) {
1382 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001383 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001384 goto error;
1385 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001386 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001387
Andy Adamson557134a2009-04-01 09:21:53 -04001388 error = nfs4_init_client_minor_version(clp);
1389 if (error < 0)
1390 goto error;
1391
Andy Adamson76db6d92009-04-01 09:22:38 -04001392 if (!nfs4_has_session(clp))
1393 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001394 return 0;
1395
1396error:
1397 nfs_mark_client_ready(clp, error);
1398 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1399 return error;
1400}
1401
1402/*
1403 * Set up an NFS4 client
1404 */
1405static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001406 const char *hostname,
1407 const struct sockaddr *addr,
1408 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001409 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001410 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001411 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001412 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001413{
Trond Myklebust3a498022007-12-14 14:56:04 -05001414 struct nfs_client_initdata cl_init = {
1415 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001416 .addr = addr,
1417 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001418 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001419 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001420 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001421 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001422 };
David Howells54ceac42006-08-22 20:06:13 -04001423 struct nfs_client *clp;
1424 int error;
1425
1426 dprintk("--> nfs4_set_client()\n");
1427
1428 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001429 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1430 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001431 if (IS_ERR(clp)) {
1432 error = PTR_ERR(clp);
1433 goto error;
1434 }
David Howells54ceac42006-08-22 20:06:13 -04001435
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001436 /*
1437 * Query for the lease time on clientid setup or renewal
1438 *
1439 * Note that this will be set on nfs_clients that were created
1440 * only for the DS role and did not set this bit, but now will
1441 * serve a dual role.
1442 */
1443 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1444
David Howells54ceac42006-08-22 20:06:13 -04001445 server->nfs_client = clp;
1446 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1447 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001448error:
1449 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1450 return error;
1451}
1452
Andy Adamsond83217c2011-03-01 01:34:17 +00001453/*
1454 * Set up a pNFS Data Server client.
1455 *
1456 * Return any existing nfs_client that matches server address,port,version
1457 * and minorversion.
1458 *
1459 * For a new nfs_client, use a soft mount (default), a low retrans and a
1460 * low timeout interval so that if a connection is lost, we retry through
1461 * the MDS.
1462 */
1463struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1464 const struct sockaddr *ds_addr,
1465 int ds_addrlen, int ds_proto)
1466{
1467 struct nfs_client_initdata cl_init = {
1468 .addr = ds_addr,
1469 .addrlen = ds_addrlen,
1470 .rpc_ops = &nfs_v4_clientops,
1471 .proto = ds_proto,
1472 .minorversion = mds_clp->cl_minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001473 .net = mds_clp->net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001474 };
1475 struct rpc_timeout ds_timeout = {
1476 .to_initval = 15 * HZ,
1477 .to_maxval = 15 * HZ,
1478 .to_retries = 1,
1479 .to_exponential = 1,
1480 };
1481 struct nfs_client *clp;
1482
1483 /*
1484 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1485 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1486 * (section 13.1 RFC 5661).
1487 */
1488 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1489 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1490
1491 dprintk("<-- %s %p\n", __func__, clp);
1492 return clp;
1493}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001494EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001495
1496/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001497 * Session has been established, and the client marked ready.
1498 * Set the mount rsize and wsize with negotiated fore channel
1499 * attributes which will be bound checked in nfs_server_set_fsinfo.
1500 */
1501static void nfs4_session_set_rwsize(struct nfs_server *server)
1502{
1503#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001504 struct nfs4_session *sess;
1505 u32 server_resp_sz;
1506 u32 server_rqst_sz;
1507
Andy Adamson96b09e02009-04-01 09:22:33 -04001508 if (!nfs4_has_session(server->nfs_client))
1509 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001510 sess = server->nfs_client->cl_session;
1511 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1512 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1513
1514 if (server->rsize > server_resp_sz)
1515 server->rsize = server_resp_sz;
1516 if (server->wsize > server_rqst_sz)
1517 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001518#endif /* CONFIG_NFS_V4_1 */
1519}
1520
Trond Myklebust44950b62010-06-17 11:45:12 -04001521static int nfs4_server_common_setup(struct nfs_server *server,
1522 struct nfs_fh *mntfh)
1523{
1524 struct nfs_fattr *fattr;
1525 int error;
1526
1527 BUG_ON(!server->nfs_client);
1528 BUG_ON(!server->nfs_client->rpc_ops);
1529 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1530
Andy Adamson94de8b22011-03-01 01:34:12 +00001531 /* data servers support only a subset of NFSv4.1 */
1532 if (is_ds_only_client(server->nfs_client))
1533 return -EPROTONOSUPPORT;
1534
Trond Myklebust44950b62010-06-17 11:45:12 -04001535 fattr = nfs_alloc_fattr();
1536 if (fattr == NULL)
1537 return -ENOMEM;
1538
1539 /* We must ensure the session is initialised first */
1540 error = nfs4_init_session(server);
1541 if (error < 0)
1542 goto out;
1543
1544 /* Probe the root fh to retrieve its FSID and filehandle */
1545 error = nfs4_get_rootfh(server, mntfh);
1546 if (error < 0)
1547 goto out;
1548
1549 dprintk("Server FSID: %llx:%llx\n",
1550 (unsigned long long) server->fsid.major,
1551 (unsigned long long) server->fsid.minor);
1552 dprintk("Mount FH: %d\n", mntfh->size);
1553
1554 nfs4_session_set_rwsize(server);
1555
1556 error = nfs_probe_fsinfo(server, mntfh, fattr);
1557 if (error < 0)
1558 goto out;
1559
1560 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1561 server->namelen = NFS4_MAXNAMLEN;
1562
Chuck Leverfca52382010-12-24 01:32:32 +00001563 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001564 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001565 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001566out:
1567 nfs_free_fattr(fattr);
1568 return error;
1569}
1570
Andy Adamson96b09e02009-04-01 09:22:33 -04001571/*
David Howells54ceac42006-08-22 20:06:13 -04001572 * Create a version 4 volume record
1573 */
1574static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001575 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001576{
Trond Myklebust33170232007-12-20 16:03:59 -05001577 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001578 int error;
1579
1580 dprintk("--> nfs4_init_server()\n");
1581
Trond Myklebust33170232007-12-20 16:03:59 -05001582 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1583 data->timeo, data->retrans);
1584
Chuck Lever542fcc32008-12-23 15:21:36 -05001585 /* Initialise the client representation from the mount data */
1586 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001587 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1588 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1589 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001590 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001591
Trond Myklebust33170232007-12-20 16:03:59 -05001592 /* Get a client record */
1593 error = nfs4_set_client(server,
1594 data->nfs_server.hostname,
1595 (const struct sockaddr *)&data->nfs_server.address,
1596 data->nfs_server.addrlen,
1597 data->client_address,
1598 data->auth_flavors[0],
1599 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001600 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001601 data->minorversion,
1602 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001603 if (error < 0)
1604 goto error;
1605
Trond Myklebustb064eca22011-02-22 15:44:32 -08001606 /*
1607 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1608 * authentication.
1609 */
1610 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1611 server->caps |= NFS_CAP_UIDGID_NOMAP;
1612
David Howells54ceac42006-08-22 20:06:13 -04001613 if (data->rsize)
1614 server->rsize = nfs_block_size(data->rsize, NULL);
1615 if (data->wsize)
1616 server->wsize = nfs_block_size(data->wsize, NULL);
1617
1618 server->acregmin = data->acregmin * HZ;
1619 server->acregmax = data->acregmax * HZ;
1620 server->acdirmin = data->acdirmin * HZ;
1621 server->acdirmax = data->acdirmax * HZ;
1622
Chuck Leverf22d6d72008-03-14 14:10:22 -04001623 server->port = data->nfs_server.port;
1624
Trond Myklebust33170232007-12-20 16:03:59 -05001625 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001626
Trond Myklebust33170232007-12-20 16:03:59 -05001627error:
David Howells54ceac42006-08-22 20:06:13 -04001628 /* Done */
1629 dprintk("<-- nfs4_init_server() = %d\n", error);
1630 return error;
1631}
1632
1633/*
1634 * Create a version 4 volume record
1635 * - keyed on server and FSID
1636 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001637struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001638 struct nfs_fh *mntfh)
1639{
David Howells54ceac42006-08-22 20:06:13 -04001640 struct nfs_server *server;
1641 int error;
1642
1643 dprintk("--> nfs4_create_server()\n");
1644
1645 server = nfs_alloc_server();
1646 if (!server)
1647 return ERR_PTR(-ENOMEM);
1648
David Howells54ceac42006-08-22 20:06:13 -04001649 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001650 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001651 if (error < 0)
1652 goto error;
1653
Trond Myklebust44950b62010-06-17 11:45:12 -04001654 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001655 if (error < 0)
1656 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001657
David Howells54ceac42006-08-22 20:06:13 -04001658 dprintk("<-- nfs4_create_server() = %p\n", server);
1659 return server;
1660
1661error:
1662 nfs_free_server(server);
1663 dprintk("<-- nfs4_create_server() = error %d\n", error);
1664 return ERR_PTR(error);
1665}
1666
1667/*
1668 * Create an NFS4 referral server record
1669 */
1670struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001671 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001672{
1673 struct nfs_client *parent_client;
1674 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001675 int error;
1676
1677 dprintk("--> nfs4_create_referral_server()\n");
1678
1679 server = nfs_alloc_server();
1680 if (!server)
1681 return ERR_PTR(-ENOMEM);
1682
1683 parent_server = NFS_SB(data->sb);
1684 parent_client = parent_server->nfs_client;
1685
Chuck Lever542fcc32008-12-23 15:21:36 -05001686 /* Initialise the client representation from the parent server */
1687 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001688 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001689
David Howells54ceac42006-08-22 20:06:13 -04001690 /* Get a client representation.
1691 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001692 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001693 data->addr,
1694 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001695 parent_client->cl_ipaddr,
1696 data->authflavor,
Trond Myklebust2446ab62012-03-01 17:00:56 -05001697 rpc_protocol(parent_server->client),
Benny Halevy94a417f2009-04-01 09:21:49 -04001698 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001699 parent_client->cl_mvops->minor_version,
1700 parent_client->net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001701 if (error < 0)
1702 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001703
Trond Myklebust33170232007-12-20 16:03:59 -05001704 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001705 if (error < 0)
1706 goto error;
1707
Trond Myklebust44950b62010-06-17 11:45:12 -04001708 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001709 if (error < 0)
1710 goto error;
1711
David Howells54ceac42006-08-22 20:06:13 -04001712 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1713 return server;
1714
1715error:
1716 nfs_free_server(server);
1717 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1718 return ERR_PTR(error);
1719}
1720
1721#endif /* CONFIG_NFS_V4 */
1722
1723/*
1724 * Clone an NFS2, NFS3 or NFS4 server record
1725 */
1726struct nfs_server *nfs_clone_server(struct nfs_server *source,
1727 struct nfs_fh *fh,
1728 struct nfs_fattr *fattr)
1729{
1730 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001731 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001732 int error;
1733
1734 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001735 (unsigned long long) fattr->fsid.major,
1736 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001737
1738 server = nfs_alloc_server();
1739 if (!server)
1740 return ERR_PTR(-ENOMEM);
1741
Trond Myklebustfbca7792010-04-16 16:22:46 -04001742 error = -ENOMEM;
1743 fattr_fsinfo = nfs_alloc_fattr();
1744 if (fattr_fsinfo == NULL)
1745 goto out_free_server;
1746
David Howells54ceac42006-08-22 20:06:13 -04001747 /* Copy data from the source */
1748 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001749 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001750 atomic_inc(&server->nfs_client->cl_count);
1751 nfs_server_copy_userdata(server, source);
1752
1753 server->fsid = fattr->fsid;
1754
Trond Myklebust33170232007-12-20 16:03:59 -05001755 error = nfs_init_server_rpcclient(server,
1756 source->client->cl_timeout,
1757 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001758 if (error < 0)
1759 goto out_free_server;
1760 if (!IS_ERR(source->client_acl))
1761 nfs_init_server_aclclient(server);
1762
1763 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001764 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001765 if (error < 0)
1766 goto out_free_server;
1767
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001768 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1769 server->namelen = NFS4_MAXNAMLEN;
1770
David Howells54ceac42006-08-22 20:06:13 -04001771 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001772 (unsigned long long) server->fsid.major,
1773 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001774
1775 error = nfs_start_lockd(server);
1776 if (error < 0)
1777 goto out_free_server;
1778
Chuck Leverfca52382010-12-24 01:32:32 +00001779 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001780 server->mount_time = jiffies;
1781
Trond Myklebustfbca7792010-04-16 16:22:46 -04001782 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001783 dprintk("<-- nfs_clone_server() = %p\n", server);
1784 return server;
1785
1786out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001787 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001788 nfs_free_server(server);
1789 dprintk("<-- nfs_clone_server() = error %d\n", error);
1790 return ERR_PTR(error);
1791}
David Howells6aaca562006-08-22 20:06:13 -04001792
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001793void nfs_clients_init(struct net *net)
1794{
1795 struct nfs_net *nn = net_generic(net, nfs_net_id);
1796
1797 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001798 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001799#ifdef CONFIG_NFS_V4
1800 idr_init(&nn->cb_ident_idr);
1801#endif
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001802 spin_lock_init(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001803}
1804
David Howells6aaca562006-08-22 20:06:13 -04001805#ifdef CONFIG_PROC_FS
1806static struct proc_dir_entry *proc_fs_nfs;
1807
1808static int nfs_server_list_open(struct inode *inode, struct file *file);
1809static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1810static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1811static void nfs_server_list_stop(struct seq_file *p, void *v);
1812static int nfs_server_list_show(struct seq_file *m, void *v);
1813
James Morris88e9d342009-09-22 16:43:43 -07001814static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001815 .start = nfs_server_list_start,
1816 .next = nfs_server_list_next,
1817 .stop = nfs_server_list_stop,
1818 .show = nfs_server_list_show,
1819};
1820
Arjan van de Ven00977a52007-02-12 00:55:34 -08001821static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001822 .open = nfs_server_list_open,
1823 .read = seq_read,
1824 .llseek = seq_lseek,
1825 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001826 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001827};
1828
1829static int nfs_volume_list_open(struct inode *inode, struct file *file);
1830static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1831static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1832static void nfs_volume_list_stop(struct seq_file *p, void *v);
1833static int nfs_volume_list_show(struct seq_file *m, void *v);
1834
James Morris88e9d342009-09-22 16:43:43 -07001835static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001836 .start = nfs_volume_list_start,
1837 .next = nfs_volume_list_next,
1838 .stop = nfs_volume_list_stop,
1839 .show = nfs_volume_list_show,
1840};
1841
Arjan van de Ven00977a52007-02-12 00:55:34 -08001842static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001843 .open = nfs_volume_list_open,
1844 .read = seq_read,
1845 .llseek = seq_lseek,
1846 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001847 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001848};
1849
1850/*
1851 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1852 * we're dealing
1853 */
1854static int nfs_server_list_open(struct inode *inode, struct file *file)
1855{
1856 struct seq_file *m;
1857 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001858 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1859 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001860
1861 ret = seq_open(file, &nfs_server_list_ops);
1862 if (ret < 0)
1863 return ret;
1864
1865 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001866 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001867
1868 return 0;
1869}
1870
1871/*
1872 * set up the iterator to start reading from the server list and return the first item
1873 */
1874static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1875{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001876 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1877
David Howells6aaca562006-08-22 20:06:13 -04001878 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001879 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001880 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001881}
1882
1883/*
1884 * move to next server
1885 */
1886static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1887{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001888 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1889
1890 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001891}
1892
1893/*
1894 * clean up after reading from the transports list
1895 */
1896static void nfs_server_list_stop(struct seq_file *p, void *v)
1897{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001898 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1899
1900 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001901}
1902
1903/*
1904 * display a header line followed by a load of call lines
1905 */
1906static int nfs_server_list_show(struct seq_file *m, void *v)
1907{
1908 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001909 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001910
1911 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001912 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001913 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1914 return 0;
1915 }
1916
1917 /* display one transport per line on subsequent lines */
1918 clp = list_entry(v, struct nfs_client, cl_share_link);
1919
Malahal Naineni940aab42011-09-20 17:27:14 -07001920 /* Check if the client is initialized */
1921 if (clp->cl_cons_state != NFS_CS_READY)
1922 return 0;
1923
Trond Myklebust2446ab62012-03-01 17:00:56 -05001924 rcu_read_lock();
Chuck Lever5d8515c2007-12-10 14:57:16 -05001925 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001926 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001927 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1928 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001929 atomic_read(&clp->cl_count),
1930 clp->cl_hostname);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001931 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001932
1933 return 0;
1934}
1935
1936/*
1937 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1938 */
1939static int nfs_volume_list_open(struct inode *inode, struct file *file)
1940{
1941 struct seq_file *m;
1942 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001943 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1944 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001945
1946 ret = seq_open(file, &nfs_volume_list_ops);
1947 if (ret < 0)
1948 return ret;
1949
1950 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001951 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001952
1953 return 0;
1954}
1955
1956/*
1957 * set up the iterator to start reading from the volume list and return the first item
1958 */
1959static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1960{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001961 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1962
David Howells6aaca562006-08-22 20:06:13 -04001963 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001964 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001965 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001966}
1967
1968/*
1969 * move to next volume
1970 */
1971static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1972{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001973 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1974
1975 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001976}
1977
1978/*
1979 * clean up after reading from the transports list
1980 */
1981static void nfs_volume_list_stop(struct seq_file *p, void *v)
1982{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001983 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1984
1985 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001986}
1987
1988/*
1989 * display a header line followed by a load of call lines
1990 */
1991static int nfs_volume_list_show(struct seq_file *m, void *v)
1992{
1993 struct nfs_server *server;
1994 struct nfs_client *clp;
1995 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001996 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001997
1998 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001999 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01002000 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04002001 return 0;
2002 }
2003 /* display one transport per line on subsequent lines */
2004 server = list_entry(v, struct nfs_server, master_link);
2005 clp = server->nfs_client;
2006
2007 snprintf(dev, 8, "%u:%u",
2008 MAJOR(server->s_dev), MINOR(server->s_dev));
2009
2010 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04002011 (unsigned long long) server->fsid.major,
2012 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04002013
Trond Myklebust2446ab62012-03-01 17:00:56 -05002014 rcu_read_lock();
David Howells5d1acff2009-04-03 16:42:47 +01002015 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05002016 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05002017 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
2018 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04002019 dev,
David Howells5d1acff2009-04-03 16:42:47 +01002020 fsid,
2021 nfs_server_fscache_state(server));
Trond Myklebust2446ab62012-03-01 17:00:56 -05002022 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04002023
2024 return 0;
2025}
2026
2027/*
2028 * initialise the /proc/fs/nfsfs/ directory
2029 */
2030int __init nfs_fs_proc_init(void)
2031{
2032 struct proc_dir_entry *p;
2033
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002034 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002035 if (!proc_fs_nfs)
2036 goto error_0;
2037
David Howells6aaca562006-08-22 20:06:13 -04002038 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002039 p = proc_create("servers", S_IFREG|S_IRUGO,
2040 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002041 if (!p)
2042 goto error_1;
2043
David Howells6aaca562006-08-22 20:06:13 -04002044 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002045 p = proc_create("volumes", S_IFREG|S_IRUGO,
2046 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002047 if (!p)
2048 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002049 return 0;
2050
2051error_2:
2052 remove_proc_entry("servers", proc_fs_nfs);
2053error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002054 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002055error_0:
2056 return -ENOMEM;
2057}
2058
2059/*
2060 * clean up the /proc/fs/nfsfs/ directory
2061 */
2062void nfs_fs_proc_exit(void)
2063{
2064 remove_proc_entry("volumes", proc_fs_nfs);
2065 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002066 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002067}
2068
2069#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002070
2071module_param(nfs4_disable_idmapping, bool, 0644);
2072MODULE_PARM_DESC(nfs4_disable_idmapping,
2073 "Turn off NFSv4 idmapping when using 'sec=sys'");