blob: 8a4b3c2c5a2b6f642a4b15cfeb88c65c49c6d87c [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>
Al Viro40401532012-02-13 03:58:52 +000039#include <linux/idr.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050040#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040041#include <linux/nfs_xdr.h>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040042#include <linux/sunrpc/bc_xprt.h>
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000043#include <linux/nsproxy.h>
44#include <linux/pid_namespace.h>
David Howells24c8dbb2006-08-22 20:06:10 -040045
David Howells24c8dbb2006-08-22 20:06:10 -040046
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;
Chuck Lever73ea6662012-05-21 22:44:50 -040068 struct nfs_net *nn = net_generic(clp->cl_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] = {
Bryan Schumaker2ba68002012-05-10 16:47:19 -040093#ifdef CONFIG_NFS_V2
David Howells5006a762006-08-22 20:06:12 -040094 [2] = &nfs_version2,
Bryan Schumaker2ba68002012-05-10 16:47:19 -040095#endif
David Howells5006a762006-08-22 20:06:12 -040096#ifdef CONFIG_NFS_V3
97 [3] = &nfs_version3,
98#endif
99#ifdef CONFIG_NFS_V4
100 [4] = &nfs_version4,
101#endif
102};
103
Trond Myklebusta613fa12012-01-20 13:53:56 -0500104const struct rpc_program nfs_program = {
David Howells5006a762006-08-22 20:06:12 -0400105 .name = "nfs",
106 .number = NFS_PROGRAM,
107 .nrvers = ARRAY_SIZE(nfs_version),
108 .version = nfs_version,
109 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400110 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -0400111};
112
113struct rpc_stat nfs_rpcstat = {
114 .program = &nfs_program
115};
116
117
118#ifdef CONFIG_NFS_V3_ACL
119static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
Trond Myklebusta613fa12012-01-20 13:53:56 -0500120static const struct rpc_version *nfsacl_version[] = {
David Howells5006a762006-08-22 20:06:12 -0400121 [3] = &nfsacl_version3,
122};
123
Trond Myklebusta613fa12012-01-20 13:53:56 -0500124const struct rpc_program nfsacl_program = {
David Howells5006a762006-08-22 20:06:12 -0400125 .name = "nfsacl",
126 .number = NFS_ACL_PROGRAM,
127 .nrvers = ARRAY_SIZE(nfsacl_version),
128 .version = nfsacl_version,
129 .stats = &nfsacl_rpcstat,
130};
131#endif /* CONFIG_NFS_V3_ACL */
132
Trond Myklebust3a498022007-12-14 14:56:04 -0500133struct nfs_client_initdata {
134 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500135 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500136 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500137 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500138 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400139 u32 minorversion;
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400140 struct net *net;
Trond Myklebust3a498022007-12-14 14:56:04 -0500141};
142
David Howells5006a762006-08-22 20:06:12 -0400143/*
David Howells24c8dbb2006-08-22 20:06:10 -0400144 * Allocate a shared client record
145 *
146 * Since these are allocated/deallocated very rarely, we don't
147 * bother putting them in a slab cache...
148 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500149static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400150{
151 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400152 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700153 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400154
155 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
156 goto error_0;
157
Trond Myklebust40c553192007-12-14 14:56:07 -0500158 clp->rpc_ops = cl_init->rpc_ops;
159
David Howells24c8dbb2006-08-22 20:06:10 -0400160 atomic_set(&clp->cl_count, 1);
161 clp->cl_cons_state = NFS_CS_INITING;
162
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500163 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
164 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400165
Trond Myklebust3a498022007-12-14 14:56:04 -0500166 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700167 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500168 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400169 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400170 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400171 }
172
173 INIT_LIST_HEAD(&clp->cl_superblocks);
174 clp->cl_rpcclient = ERR_PTR(-EINVAL);
175
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500176 clp->cl_proto = cl_init->proto;
Chuck Lever73ea6662012-05-21 22:44:50 -0400177 clp->cl_net = get_net(cl_init->net);
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500178
David Howells24c8dbb2006-08-22 20:06:10 -0400179#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000180 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
181 if (err)
182 goto error_cleanup;
183
David Howells24c8dbb2006-08-22 20:06:10 -0400184 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000185 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400186 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
David Howells24c8dbb2006-08-22 20:06:10 -0400187 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400188 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400189 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400190#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500191 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400192 if (!IS_ERR(cred))
193 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100194 nfs_fscache_get_client_cookie(clp);
195
David Howells24c8dbb2006-08-22 20:06:10 -0400196 return clp;
197
Benny Halevy71468512009-04-01 09:22:56 -0400198error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400199 kfree(clp);
200error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700201 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400202}
203
Trond Myklebust5dd31772006-08-24 01:03:05 -0400204#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400205#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000206static void nfs4_shutdown_session(struct nfs_client *clp)
207{
Trond Myklebust7df529a2012-02-26 17:34:22 -0500208 if (nfs4_has_session(clp)) {
209 nfs4_deviceid_purge_client(clp);
Andy Adamson557134a2009-04-01 09:21:53 -0400210 nfs4_destroy_session(clp->cl_session);
Trond Myklebust7df529a2012-02-26 17:34:22 -0500211 }
212
Andy Adamson557134a2009-04-01 09:21:53 -0400213}
Andy Adamsonea005282011-01-06 02:04:29 +0000214#else /* CONFIG_NFS_V4_1 */
215static void nfs4_shutdown_session(struct nfs_client *clp)
216{
217}
218#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400219
220/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800221 * Destroy the NFS4 callback service
222 */
223static void nfs4_destroy_callback(struct nfs_client *clp)
224{
225 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400226 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800227}
228
229static void nfs4_shutdown_client(struct nfs_client *clp)
230{
231 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
232 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000233 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800234 nfs4_destroy_callback(clp);
235 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
236 nfs_idmap_delete(clp);
237
238 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Chuck Lever79d4e1f2012-05-21 22:44:31 -0400239 kfree(clp->cl_serverscope);
Chuck Lever59155542012-05-21 22:44:41 -0400240 kfree(clp->cl_implid);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800241}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000242
243/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000244void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000245{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000246 struct nfs_net *nn = net_generic(net, nfs_net_id);
247
248 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000249}
250
251/* nfs_client_lock held */
252static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
253{
Chuck Lever73ea6662012-05-21 22:44:50 -0400254 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000255
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000256 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000257 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000258}
259
Fred Isamanf7e89172011-01-06 11:36:32 +0000260static void pnfs_init_server(struct nfs_server *server)
261{
262 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
263}
264
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500265static void nfs4_destroy_server(struct nfs_server *server)
266{
267 nfs4_purge_state_owners(server);
268}
269
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800270#else
271static void nfs4_shutdown_client(struct nfs_client *clp)
272{
273}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000274
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000275void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000276{
277}
278
279static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
280{
281}
Fred Isamanf7e89172011-01-06 11:36:32 +0000282
283static void pnfs_init_server(struct nfs_server *server)
284{
285}
286
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800287#endif /* CONFIG_NFS_V4 */
288
289/*
David Howells24c8dbb2006-08-22 20:06:10 -0400290 * Destroy a shared client record
291 */
292static void nfs_free_client(struct nfs_client *clp)
293{
Trond Myklebust40c553192007-12-14 14:56:07 -0500294 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400295
Trond Myklebust5dd31772006-08-24 01:03:05 -0400296 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400297
David Howells14727282009-04-03 16:42:42 +0100298 nfs_fscache_release_client_cookie(clp);
299
David Howells24c8dbb2006-08-22 20:06:10 -0400300 /* -EIO all pending I/O */
301 if (!IS_ERR(clp->cl_rpcclient))
302 rpc_shutdown_client(clp->cl_rpcclient);
303
Trond Myklebust7c67db32008-04-07 20:50:11 -0400304 if (clp->cl_machine_cred != NULL)
305 put_rpccred(clp->cl_machine_cred);
306
Chuck Lever73ea6662012-05-21 22:44:50 -0400307 put_net(clp->cl_net);
David Howells24c8dbb2006-08-22 20:06:10 -0400308 kfree(clp->cl_hostname);
309 kfree(clp);
310
311 dprintk("<-- nfs_free_client()\n");
312}
313
314/*
315 * Release a reference to a shared client record
316 */
317void nfs_put_client(struct nfs_client *clp)
318{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000319 struct nfs_net *nn;
320
David Howells27ba8512006-07-30 14:40:56 -0400321 if (!clp)
322 return;
323
David Howells24c8dbb2006-08-22 20:06:10 -0400324 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
Chuck Lever73ea6662012-05-21 22:44:50 -0400325 nn = net_generic(clp->cl_net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400326
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000327 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
David Howells24c8dbb2006-08-22 20:06:10 -0400328 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000329 nfs_cb_idr_remove_locked(clp);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000330 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400331
332 BUG_ON(!list_empty(&clp->cl_superblocks));
333
334 nfs_free_client(clp);
335 }
336}
Andy Adamson16b374c2010-10-20 00:18:04 -0400337EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400338
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500339#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400340/*
341 * Test if two ip6 socket addresses refer to the same socket by
342 * comparing relevant fields. The padding bytes specifically, are not
343 * compared. sin6_flowinfo is not compared because it only affects QoS
344 * and sin6_scope_id is only compared if the address is "link local"
345 * because "link local" addresses need only be unique to a specific
346 * link. Conversely, ordinary unicast addresses might have different
347 * sin6_scope_id.
348 *
349 * The caller should ensure both socket addresses are AF_INET6.
350 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400351static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
352 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400353{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400354 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
355 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400356
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800357 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400358 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800359 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
360 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500361
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800362 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500363}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400364#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
365static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
366 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400367{
368 return 0;
369}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500370#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500371
David Howells24c8dbb2006-08-22 20:06:10 -0400372/*
Ian Dalld7371c42009-03-10 20:33:22 -0400373 * Test if two ip4 socket addresses refer to the same socket, by
374 * comparing relevant fields. The padding bytes specifically, are
375 * not compared.
376 *
377 * The caller should ensure both socket addresses are AF_INET.
378 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400379static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
380 const struct sockaddr *sa2)
381{
382 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
383 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
384
385 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
386}
387
388static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
389 const struct sockaddr *sa2)
390{
391 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
392 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
393
394 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
395 (sin1->sin6_port == sin2->sin6_port);
396}
397
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400398static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
399 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400400{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400401 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
402 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400403
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400404 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
405 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400406}
407
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400408#if defined(CONFIG_NFS_V4_1)
Ian Dalld7371c42009-03-10 20:33:22 -0400409/*
Ian Dalld7371c42009-03-10 20:33:22 -0400410 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400411 * by comparing (only) relevant fields, excluding the port number.
412 */
413static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
414 const struct sockaddr *sa2)
415{
416 if (sa1->sa_family != sa2->sa_family)
417 return 0;
418
419 switch (sa1->sa_family) {
420 case AF_INET:
421 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
422 case AF_INET6:
423 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
424 }
425 return 0;
426}
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400427#endif /* CONFIG_NFS_V4_1 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400428
429/*
430 * Test if two socket addresses represent the same actual socket,
431 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400432 */
433static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
434 const struct sockaddr *sa2)
435{
436 if (sa1->sa_family != sa2->sa_family)
437 return 0;
438
439 switch (sa1->sa_family) {
440 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400441 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400442 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400443 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400444 }
445 return 0;
446}
447
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400448#if defined(CONFIG_NFS_V4_1)
Andy Adamsonc36fca52011-01-06 02:04:32 +0000449/* Common match routine for v4.0 and v4.1 callback services */
Trond Myklebust17280172012-03-11 13:11:00 -0400450static bool nfs4_cb_match_client(const struct sockaddr *addr,
451 struct nfs_client *clp, u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500452{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000453 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500454
Andy Adamsonc36fca52011-01-06 02:04:32 +0000455 /* Don't match clients that failed to initialise */
456 if (!(clp->cl_cons_state == NFS_CS_READY ||
457 clp->cl_cons_state == NFS_CS_SESSION_INITING))
458 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500459
Andy Adamsonc36fca52011-01-06 02:04:32 +0000460 /* Match the version and minorversion */
461 if (clp->rpc_ops->version != 4 ||
462 clp->cl_minorversion != minorversion)
463 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500464
Andy Adamsonc36fca52011-01-06 02:04:32 +0000465 /* Match only the IP address, not the port number */
466 if (!nfs_sockaddr_match_ipaddr(addr, clap))
467 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500468
Andy Adamsonc36fca52011-01-06 02:04:32 +0000469 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500470}
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400471#endif /* CONFIG_NFS_V4_1 */
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500472
473/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500474 * Find an nfs_client on the list that matches the initialisation data
475 * that is supplied.
476 */
477static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400478{
479 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400480 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000481 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400482
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000483 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400484 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700485 /* Don't match clients that failed to initialise properly */
486 if (clp->cl_cons_state < 0)
487 continue;
488
David Howells24c8dbb2006-08-22 20:06:10 -0400489 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500490 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400491 continue;
492
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500493 if (clp->cl_proto != data->proto)
494 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400495 /* Match nfsv4 minorversion */
496 if (clp->cl_minorversion != data->minorversion)
497 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500498 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400499 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400500 continue;
501
Trond Myklebustc81468a2007-12-14 14:56:05 -0500502 atomic_inc(&clp->cl_count);
503 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400504 }
David Howells24c8dbb2006-08-22 20:06:10 -0400505 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400506}
507
508/*
Chuck Leverf4117032012-05-21 22:45:50 -0400509 * Found an existing client. Make sure it's ready before returning.
510 */
511static struct nfs_client *
512nfs_found_client(const struct nfs_client_initdata *cl_init,
513 struct nfs_client *clp)
514{
515 int error;
516
517 error = wait_event_killable(nfs_client_active_wq,
518 clp->cl_cons_state < NFS_CS_INITING);
519 if (error < 0) {
520 nfs_put_client(clp);
521 return ERR_PTR(-ERESTARTSYS);
522 }
523
524 if (clp->cl_cons_state < NFS_CS_READY) {
525 error = clp->cl_cons_state;
526 nfs_put_client(clp);
527 return ERR_PTR(error);
528 }
529
530 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
531
532 dprintk("<-- %s found nfs_client %p for %s\n",
533 __func__, clp, cl_init->hostname ?: "");
534 return clp;
535}
536
537/*
David Howells24c8dbb2006-08-22 20:06:10 -0400538 * Look up a client by IP address and protocol version
539 * - creates a new record if one doesn't yet exist
540 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000541static struct nfs_client *
542nfs_get_client(const struct nfs_client_initdata *cl_init,
543 const struct rpc_timeout *timeparms,
544 const char *ip_addr,
545 rpc_authflavor_t authflavour,
546 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400547{
548 struct nfs_client *clp, *new = NULL;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000549 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400550
Chuck Leverd7422c42007-12-10 14:58:51 -0500551 dprintk("--> nfs_get_client(%s,v%u)\n",
552 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400553
554 /* see if the client already exists */
555 do {
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000556 spin_lock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400557
Trond Myklebustc81468a2007-12-14 14:56:05 -0500558 clp = nfs_match_client(cl_init);
Chuck Leverf4117032012-05-21 22:45:50 -0400559 if (clp) {
560 spin_unlock(&nn->nfs_client_lock);
561 if (new)
562 nfs_free_client(new);
563 return nfs_found_client(cl_init, clp);
564 }
Chuck Lever8cab4c32012-05-21 22:45:59 -0400565 if (new) {
566 list_add(&new->cl_share_link, &nn->nfs_client_list);
567 spin_unlock(&nn->nfs_client_lock);
568 return cl_init->rpc_ops->init_client(new,
569 timeparms, ip_addr,
570 authflavour, noresvport);
571 }
David Howells24c8dbb2006-08-22 20:06:10 -0400572
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000573 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400574
Trond Myklebust3a498022007-12-14 14:56:04 -0500575 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700576 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400577
Chuck Leverf4117032012-05-21 22:45:50 -0400578 dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
579 cl_init->hostname ?: "", PTR_ERR(new));
Chuck Levera21bdd92009-06-17 18:02:10 -0700580 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400581}
582
583/*
584 * Mark a server as ready or failed
585 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400586void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400587{
588 clp->cl_cons_state = state;
589 wake_up_all(&nfs_client_active_wq);
590}
David Howells5006a762006-08-22 20:06:12 -0400591
592/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400593 * With sessions, the client is not marked ready until after a
594 * successful EXCHANGE_ID and CREATE_SESSION.
595 *
596 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
597 * other versions of NFS can be tried.
598 */
599int nfs4_check_client_ready(struct nfs_client *clp)
600{
601 if (!nfs4_has_session(clp))
602 return 0;
603 if (clp->cl_cons_state < NFS_CS_READY)
604 return -EPROTONOSUPPORT;
605 return 0;
606}
607
608/*
David Howells5006a762006-08-22 20:06:12 -0400609 * Initialise the timeout values for a connection
610 */
611static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
612 unsigned int timeo, unsigned int retrans)
613{
614 to->to_initval = timeo * HZ / 10;
615 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400616
617 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400618 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400619 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400620 if (to->to_retries == 0)
621 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500622 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400623 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400624 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
625 to->to_initval = NFS_MAX_TCP_TIMEOUT;
626 to->to_increment = to->to_initval;
627 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500628 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
629 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
630 if (to->to_maxval < to->to_initval)
631 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400632 to->to_exponential = 0;
633 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400634 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400635 if (to->to_retries == 0)
636 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400637 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400638 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400639 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
640 to->to_initval = NFS_MAX_UDP_TIMEOUT;
641 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
642 to->to_exponential = 1;
643 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400644 default:
645 BUG();
David Howells5006a762006-08-22 20:06:12 -0400646 }
647}
648
649/*
650 * Create an RPC client handle
651 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500652static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500653 const struct rpc_timeout *timeparms,
654 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500655 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400656{
David Howells5006a762006-08-22 20:06:12 -0400657 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400658 struct rpc_create_args args = {
Chuck Lever73ea6662012-05-21 22:44:50 -0400659 .net = clp->cl_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500660 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400661 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500662 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500663 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400664 .servername = clp->cl_hostname,
665 .program = &nfs_program,
666 .version = clp->rpc_ops->version,
667 .authflavor = flavor,
668 };
David Howells5006a762006-08-22 20:06:12 -0400669
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500670 if (discrtry)
671 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
672 if (noresvport)
673 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
674
David Howells5006a762006-08-22 20:06:12 -0400675 if (!IS_ERR(clp->cl_rpcclient))
676 return 0;
677
Chuck Lever41877d22006-08-22 20:06:20 -0400678 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400679 if (IS_ERR(clnt)) {
680 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700681 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400682 return PTR_ERR(clnt);
683 }
684
David Howells5006a762006-08-22 20:06:12 -0400685 clp->cl_rpcclient = clnt;
686 return 0;
687}
David Howells54ceac42006-08-22 20:06:13 -0400688
689/*
690 * Version 2 or 3 client destruction
691 */
692static void nfs_destroy_server(struct nfs_server *server)
693{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400694 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
695 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500696 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400697}
698
699/*
700 * Version 2 or 3 lockd setup
701 */
702static int nfs_start_lockd(struct nfs_server *server)
703{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500704 struct nlm_host *host;
705 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500706 struct nlmclnt_initdata nlm_init = {
707 .hostname = clp->cl_hostname,
708 .address = (struct sockaddr *)&clp->cl_addr,
709 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500710 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500711 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
712 1 : 0,
Chuck Lever73ea6662012-05-21 22:44:50 -0400713 .net = clp->cl_net,
Chuck Lever883bb162008-01-15 16:04:20 -0500714 };
David Howells54ceac42006-08-22 20:06:13 -0400715
Chuck Lever883bb162008-01-15 16:04:20 -0500716 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500717 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400718 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
719 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500720 return 0;
721
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400722 switch (clp->cl_proto) {
723 default:
724 nlm_init.protocol = IPPROTO_TCP;
725 break;
726 case XPRT_TRANSPORT_UDP:
727 nlm_init.protocol = IPPROTO_UDP;
728 }
729
Chuck Lever883bb162008-01-15 16:04:20 -0500730 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500731 if (IS_ERR(host))
732 return PTR_ERR(host);
733
734 server->nlm_host = host;
735 server->destroy = nfs_destroy_server;
736 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400737}
738
739/*
740 * Initialise an NFSv3 ACL client connection
741 */
742#ifdef CONFIG_NFS_V3_ACL
743static void nfs_init_server_aclclient(struct nfs_server *server)
744{
Trond Myklebust40c553192007-12-14 14:56:07 -0500745 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400746 goto out_noacl;
747 if (server->flags & NFS_MOUNT_NOACL)
748 goto out_noacl;
749
750 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
751 if (IS_ERR(server->client_acl))
752 goto out_noacl;
753
754 /* No errors! Assume that Sun nfsacls are supported */
755 server->caps |= NFS_CAP_ACLS;
756 return;
757
758out_noacl:
759 server->caps &= ~NFS_CAP_ACLS;
760}
761#else
762static inline void nfs_init_server_aclclient(struct nfs_server *server)
763{
764 server->flags &= ~NFS_MOUNT_NOACL;
765 server->caps &= ~NFS_CAP_ACLS;
766}
767#endif
768
769/*
770 * Create a general RPC client
771 */
Trond Myklebust33170232007-12-20 16:03:59 -0500772static int nfs_init_server_rpcclient(struct nfs_server *server,
773 const struct rpc_timeout *timeo,
774 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400775{
776 struct nfs_client *clp = server->nfs_client;
777
778 server->client = rpc_clone_client(clp->cl_rpcclient);
779 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700780 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400781 return PTR_ERR(server->client);
782 }
783
Trond Myklebust33170232007-12-20 16:03:59 -0500784 memcpy(&server->client->cl_timeout_default,
785 timeo,
786 sizeof(server->client->cl_timeout_default));
787 server->client->cl_timeout = &server->client->cl_timeout_default;
788
David Howells54ceac42006-08-22 20:06:13 -0400789 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
790 struct rpc_auth *auth;
791
792 auth = rpcauth_create(pseudoflavour, server->client);
793 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700794 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400795 return PTR_ERR(auth);
796 }
797 }
798 server->client->cl_softrtry = 0;
799 if (server->flags & NFS_MOUNT_SOFT)
800 server->client->cl_softrtry = 1;
801
David Howells54ceac42006-08-22 20:06:13 -0400802 return 0;
803}
804
Chuck Lever8cab4c32012-05-21 22:45:59 -0400805/**
806 * nfs_init_client - Initialise an NFS2 or NFS3 client
807 *
808 * @clp: nfs_client to initialise
809 * @timeparms: timeout parameters for underlying RPC transport
810 * @ip_addr: IP presentation address (not used)
811 * @authflavor: authentication flavor for underlying RPC transport
812 * @noresvport: set if RPC transport can use an ephemeral source port
813 *
814 * Returns pointer to an NFS client, or an ERR_PTR value.
David Howells54ceac42006-08-22 20:06:13 -0400815 */
Chuck Lever8cab4c32012-05-21 22:45:59 -0400816struct nfs_client *nfs_init_client(struct nfs_client *clp,
817 const struct rpc_timeout *timeparms,
Andy Adamson45a52a02011-03-01 01:34:08 +0000818 const char *ip_addr, rpc_authflavor_t authflavour,
819 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400820{
David Howells54ceac42006-08-22 20:06:13 -0400821 int error;
822
823 if (clp->cl_cons_state == NFS_CS_READY) {
824 /* the client is already initialised */
825 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400826 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400827 }
828
David Howells54ceac42006-08-22 20:06:13 -0400829 /*
830 * Create a client RPC handle for doing FSSTAT with UNIX auth only
831 * - RFC 2623, sec 2.3.2
832 */
Chuck Leverd7403512008-12-23 15:21:37 -0500833 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000834 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400835 if (error < 0)
836 goto error;
837 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400838 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400839
840error:
841 nfs_mark_client_ready(clp, error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400842 nfs_put_client(clp);
David Howells54ceac42006-08-22 20:06:13 -0400843 dprintk("<-- nfs_init_client() = xerror %d\n", error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400844 return ERR_PTR(error);
David Howells54ceac42006-08-22 20:06:13 -0400845}
846
847/*
848 * Create a version 2 or 3 client
849 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400850static int nfs_init_server(struct nfs_server *server,
851 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400852{
Trond Myklebust3a498022007-12-14 14:56:04 -0500853 struct nfs_client_initdata cl_init = {
854 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500855 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500856 .addrlen = data->nfs_server.addrlen,
Bryan Schumaker2ba68002012-05-10 16:47:19 -0400857 .rpc_ops = NULL,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500858 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400859 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500860 };
Trond Myklebust33170232007-12-20 16:03:59 -0500861 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400862 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500863 int error;
David Howells54ceac42006-08-22 20:06:13 -0400864
865 dprintk("--> nfs_init_server()\n");
866
Bryan Schumaker2ba68002012-05-10 16:47:19 -0400867 switch (data->version) {
868#ifdef CONFIG_NFS_V2
869 case 2:
870 cl_init.rpc_ops = &nfs_v2_clientops;
871 break;
David Howells54ceac42006-08-22 20:06:13 -0400872#endif
Bryan Schumaker2ba68002012-05-10 16:47:19 -0400873#ifdef CONFIG_NFS_V3
874 case 3:
875 cl_init.rpc_ops = &nfs_v3_clientops;
876 break;
877#endif
878 default:
879 return -EPROTONOSUPPORT;
880 }
David Howells54ceac42006-08-22 20:06:13 -0400881
Andy Adamson45a52a02011-03-01 01:34:08 +0000882 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
883 data->timeo, data->retrans);
884
David Howells54ceac42006-08-22 20:06:13 -0400885 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000886 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
887 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400888 if (IS_ERR(clp)) {
889 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
890 return PTR_ERR(clp);
891 }
892
David Howells54ceac42006-08-22 20:06:13 -0400893 server->nfs_client = clp;
894
895 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400896 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100897 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400898 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
899 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400900 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME|NFS_CAP_CHANGE_ATTR;
David Howells54ceac42006-08-22 20:06:13 -0400901
902 if (data->rsize)
903 server->rsize = nfs_block_size(data->rsize, NULL);
904 if (data->wsize)
905 server->wsize = nfs_block_size(data->wsize, NULL);
906
907 server->acregmin = data->acregmin * HZ;
908 server->acregmax = data->acregmax * HZ;
909 server->acdirmin = data->acdirmin * HZ;
910 server->acdirmax = data->acdirmax * HZ;
911
912 /* Start lockd here, before we might error out */
913 error = nfs_start_lockd(server);
914 if (error < 0)
915 goto error;
916
Chuck Leverf22d6d72008-03-14 14:10:22 -0400917 server->port = data->nfs_server.port;
918
Trond Myklebust33170232007-12-20 16:03:59 -0500919 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400920 if (error < 0)
921 goto error;
922
Chuck Lever3f8400d2008-03-14 14:10:30 -0400923 /* Preserve the values of mount_server-related mount options */
924 if (data->mount_server.addrlen) {
925 memcpy(&server->mountd_address, &data->mount_server.address,
926 data->mount_server.addrlen);
927 server->mountd_addrlen = data->mount_server.addrlen;
928 }
929 server->mountd_version = data->mount_server.version;
930 server->mountd_port = data->mount_server.port;
931 server->mountd_protocol = data->mount_server.protocol;
932
David Howells54ceac42006-08-22 20:06:13 -0400933 server->namelen = data->namlen;
934 /* Create a client RPC handle for the NFSv3 ACL management interface */
935 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400936 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
937 return 0;
938
939error:
940 server->nfs_client = NULL;
941 nfs_put_client(clp);
942 dprintk("<-- nfs_init_server() = xerror %d\n", error);
943 return error;
944}
945
946/*
947 * Load up the server record from information gained in an fsinfo record
948 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400949static void nfs_server_set_fsinfo(struct nfs_server *server,
950 struct nfs_fh *mntfh,
951 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400952{
953 unsigned long max_rpc_payload;
954
955 /* Work out a lot of parameters */
956 if (server->rsize == 0)
957 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
958 if (server->wsize == 0)
959 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
960
961 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
962 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
963 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
964 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
965
966 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
967 if (server->rsize > max_rpc_payload)
968 server->rsize = max_rpc_payload;
969 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
970 server->rsize = NFS_MAX_FILE_IO_SIZE;
971 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700972
Jens Axboed9938312009-06-12 14:45:52 +0200973 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400974 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
975
976 if (server->wsize > max_rpc_payload)
977 server->wsize = max_rpc_payload;
978 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
979 server->wsize = NFS_MAX_FILE_IO_SIZE;
980 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400981 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400982 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400983
David Howells54ceac42006-08-22 20:06:13 -0400984 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
985
986 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400987 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
988 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400989 if (server->dtsize > server->rsize)
990 server->dtsize = server->rsize;
991
992 if (server->flags & NFS_MOUNT_NOAC) {
993 server->acregmin = server->acregmax = 0;
994 server->acdirmin = server->acdirmax = 0;
995 }
996
997 server->maxfilesize = fsinfo->maxfilesize;
998
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700999 server->time_delta = fsinfo->time_delta;
1000
David Howells54ceac42006-08-22 20:06:13 -04001001 /* We're airborne Set socket buffersize */
1002 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
1003}
1004
1005/*
1006 * Probe filesystem information, including the FSID on v2/v3
1007 */
1008static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
1009{
1010 struct nfs_fsinfo fsinfo;
1011 struct nfs_client *clp = server->nfs_client;
1012 int error;
1013
1014 dprintk("--> nfs_probe_fsinfo()\n");
1015
1016 if (clp->rpc_ops->set_capabilities != NULL) {
1017 error = clp->rpc_ops->set_capabilities(server, mntfh);
1018 if (error < 0)
1019 goto out_error;
1020 }
1021
1022 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001023 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -04001024 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
1025 if (error < 0)
1026 goto out_error;
1027
Benny Halevy738fd0f32011-07-30 20:52:36 -04001028 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001029
1030 /* Get some general file system info */
1031 if (server->namelen == 0) {
1032 struct nfs_pathconf pathinfo;
1033
1034 pathinfo.fattr = fattr;
1035 nfs_fattr_init(fattr);
1036
1037 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1038 server->namelen = pathinfo.max_namelen;
1039 }
1040
1041 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1042 return 0;
1043
1044out_error:
1045 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1046 return error;
1047}
1048
1049/*
1050 * Copy useful information when duplicating a server record
1051 */
1052static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1053{
1054 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001055 target->rsize = source->rsize;
1056 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001057 target->acregmin = source->acregmin;
1058 target->acregmax = source->acregmax;
1059 target->acdirmin = source->acdirmin;
1060 target->acdirmax = source->acdirmax;
1061 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001062 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001063}
1064
Chuck Leverfca52382010-12-24 01:32:32 +00001065static void nfs_server_insert_lists(struct nfs_server *server)
1066{
1067 struct nfs_client *clp = server->nfs_client;
Chuck Lever73ea6662012-05-21 22:44:50 -04001068 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001069
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001070 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001071 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001072 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001073 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001074 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001075
1076}
1077
1078static void nfs_server_remove_lists(struct nfs_server *server)
1079{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001080 struct nfs_client *clp = server->nfs_client;
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001081 struct nfs_net *nn;
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001082
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001083 if (clp == NULL)
1084 return;
Chuck Lever73ea6662012-05-21 22:44:50 -04001085 nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001086 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001087 list_del_rcu(&server->client_link);
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001088 if (list_empty(&clp->cl_superblocks))
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001089 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001090 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001091 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001092
1093 synchronize_rcu();
1094}
1095
David Howells54ceac42006-08-22 20:06:13 -04001096/*
1097 * Allocate and initialise a server record
1098 */
1099static struct nfs_server *nfs_alloc_server(void)
1100{
1101 struct nfs_server *server;
1102
1103 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1104 if (!server)
1105 return NULL;
1106
1107 server->client = server->client_acl = ERR_PTR(-EINVAL);
1108
1109 /* Zero out the NFS state stuff */
1110 INIT_LIST_HEAD(&server->client_link);
1111 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001112 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001113 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001114 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001115
Steve Dicksonef818a22007-11-08 04:05:04 -05001116 atomic_set(&server->active, 0);
1117
David Howells54ceac42006-08-22 20:06:13 -04001118 server->io_stats = nfs_alloc_iostats();
1119 if (!server->io_stats) {
1120 kfree(server);
1121 return NULL;
1122 }
1123
Jens Axboe48d07642009-09-21 09:59:39 +02001124 if (bdi_init(&server->backing_dev_info)) {
1125 nfs_free_iostats(server->io_stats);
1126 kfree(server);
1127 return NULL;
1128 }
1129
Trond Myklebust9157c31d2012-01-17 22:04:24 -05001130 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001131 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001132 pnfs_init_server(server);
1133
David Howells54ceac42006-08-22 20:06:13 -04001134 return server;
1135}
1136
1137/*
1138 * Free up a server record
1139 */
1140void nfs_free_server(struct nfs_server *server)
1141{
1142 dprintk("--> nfs_free_server()\n");
1143
Chuck Leverfca52382010-12-24 01:32:32 +00001144 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001145 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001146
1147 if (server->destroy != NULL)
1148 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001149
1150 if (!IS_ERR(server->client_acl))
1151 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001152 if (!IS_ERR(server->client))
1153 rpc_shutdown_client(server->client);
1154
1155 nfs_put_client(server->nfs_client);
1156
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001157 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c31d2012-01-17 22:04:24 -05001158 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001159 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001160 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001161 kfree(server);
1162 nfs_release_automount_timer();
1163 dprintk("<-- nfs_free_server()\n");
1164}
1165
1166/*
1167 * Create a version 2 or 3 volume record
1168 * - keyed on server and FSID
1169 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001170struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001171 struct nfs_fh *mntfh)
1172{
1173 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001174 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001175 int error;
1176
1177 server = nfs_alloc_server();
1178 if (!server)
1179 return ERR_PTR(-ENOMEM);
1180
Trond Myklebustfbca7792010-04-16 16:22:46 -04001181 error = -ENOMEM;
1182 fattr = nfs_alloc_fattr();
1183 if (fattr == NULL)
1184 goto error;
1185
David Howells54ceac42006-08-22 20:06:13 -04001186 /* Get a client representation */
1187 error = nfs_init_server(server, data);
1188 if (error < 0)
1189 goto error;
1190
1191 BUG_ON(!server->nfs_client);
1192 BUG_ON(!server->nfs_client->rpc_ops);
1193 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1194
1195 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001196 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001197 if (error < 0)
1198 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001199 if (server->nfs_client->rpc_ops->version == 3) {
1200 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1201 server->namelen = NFS3_MAXNAMLEN;
1202 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1203 server->caps |= NFS_CAP_READDIRPLUS;
1204 } else {
1205 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1206 server->namelen = NFS2_MAXNAMLEN;
1207 }
1208
Trond Myklebustfbca7792010-04-16 16:22:46 -04001209 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1210 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001211 if (error < 0) {
1212 dprintk("nfs_create_server: getattr error = %d\n", -error);
1213 goto error;
1214 }
1215 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001216 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001217
David Howells6daabf12006-08-24 15:44:16 -04001218 dprintk("Server FSID: %llx:%llx\n",
1219 (unsigned long long) server->fsid.major,
1220 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001221
Chuck Leverfca52382010-12-24 01:32:32 +00001222 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001223 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001224 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001225 return server;
1226
1227error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001228 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001229 nfs_free_server(server);
1230 return ERR_PTR(error);
1231}
1232
1233#ifdef CONFIG_NFS_V4
1234/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001235 * NFSv4.0 callback thread helper
1236 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001237 * Find a client by callback identifier
1238 */
1239struct nfs_client *
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001240nfs4_find_client_ident(struct net *net, int cb_ident)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001241{
1242 struct nfs_client *clp;
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001243 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 Kinsbursky28cd1b32012-01-23 17:26:22 +00001246 clp = idr_find(&nn->cb_ident_idr, cb_ident);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001247 if (clp)
1248 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001249 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001250 return clp;
1251}
1252
1253#if defined(CONFIG_NFS_V4_1)
1254/*
1255 * NFSv4.1 callback thread helper
1256 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1257 * minorversion, and sessionID
1258 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001259 * Returns NULL if no such client
1260 */
1261struct nfs_client *
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001262nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001263 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001264{
1265 struct nfs_client *clp;
Stanislav Kinsburskyc7add9a2012-01-26 15:11:49 +04001266 struct nfs_net *nn = net_generic(net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001267
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001268 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001269 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001270 if (nfs4_cb_match_client(addr, clp, 1) == false)
1271 continue;
1272
1273 if (!nfs4_has_session(clp))
1274 continue;
1275
Andy Adamson778be232011-01-25 15:38:01 +00001276 /* Match sessionid*/
1277 if (memcmp(clp->cl_session->sess_id.data,
1278 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001279 continue;
1280
1281 atomic_inc(&clp->cl_count);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001282 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001283 return clp;
1284 }
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001285 spin_unlock(&nn->nfs_client_lock);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001286 return NULL;
1287}
1288
1289#else /* CONFIG_NFS_V4_1 */
1290
1291struct nfs_client *
Stanislav Kinsburskyb6d1e832012-02-07 19:53:19 +04001292nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001293 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001294{
1295 return NULL;
1296}
1297#endif /* CONFIG_NFS_V4_1 */
1298
1299/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001300 * Initialize the NFS4 callback service
1301 */
1302static int nfs4_init_callback(struct nfs_client *clp)
1303{
1304 int error;
1305
1306 if (clp->rpc_ops->version == 4) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05001307 struct rpc_xprt *xprt;
1308
1309 xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
1310
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001311 if (nfs4_has_session(clp)) {
Trond Myklebust2446ab62012-03-01 17:00:56 -05001312 error = xprt_setup_backchannel(xprt,
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001313 NFS41_BC_MIN_CALLBACKS);
1314 if (error < 0)
1315 return error;
1316 }
1317
Trond Myklebust2446ab62012-03-01 17:00:56 -05001318 error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001319 if (error < 0) {
1320 dprintk("%s: failed to start callback. Error = %d\n",
1321 __func__, error);
1322 return error;
1323 }
1324 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1325 }
1326 return 0;
1327}
1328
1329/*
Andy Adamson557134a2009-04-01 09:21:53 -04001330 * Initialize the minor version specific parts of an NFS4 client record
1331 */
1332static int nfs4_init_client_minor_version(struct nfs_client *clp)
1333{
1334#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001335 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001336 struct nfs4_session *session = NULL;
1337 /*
1338 * Create the session and mark it expired.
1339 * When a SEQUENCE operation encounters the expired session
1340 * it will do session recovery to initialize it.
1341 */
1342 session = nfs4_alloc_session(clp);
1343 if (!session)
1344 return -ENOMEM;
1345
1346 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001347 /*
1348 * The create session reply races with the server back
1349 * channel probe. Mark the client NFS_CS_SESSION_INITING
1350 * so that the client back channel can find the
1351 * nfs_client struct
1352 */
1353 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001354 }
1355#endif /* CONFIG_NFS_V4_1 */
1356
Benny Halevy71468512009-04-01 09:22:56 -04001357 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001358}
1359
Chuck Lever8cab4c32012-05-21 22:45:59 -04001360/**
1361 * nfs4_init_client - Initialise an NFS4 client record
1362 *
1363 * @clp: nfs_client to initialise
1364 * @timeparms: timeout parameters for underlying RPC transport
1365 * @ip_addr: callback IP address in presentation format
1366 * @authflavor: authentication flavor for underlying RPC transport
1367 * @noresvport: set if RPC transport can use an ephemeral source port
1368 *
1369 * Returns pointer to an NFS client, or an ERR_PTR value.
David Howells54ceac42006-08-22 20:06:13 -04001370 */
Chuck Lever8cab4c32012-05-21 22:45:59 -04001371struct nfs_client *nfs4_init_client(struct nfs_client *clp,
1372 const struct rpc_timeout *timeparms,
1373 const char *ip_addr,
1374 rpc_authflavor_t authflavour,
1375 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001376{
Chuck Lever31b8e2a2012-03-01 17:01:23 -05001377 char buf[INET6_ADDRSTRLEN + 1];
David Howells54ceac42006-08-22 20:06:13 -04001378 int error;
1379
1380 if (clp->cl_cons_state == NFS_CS_READY) {
1381 /* the client is initialised already */
1382 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
Chuck Lever8cab4c32012-05-21 22:45:59 -04001383 return clp;
David Howells54ceac42006-08-22 20:06:13 -04001384 }
1385
1386 /* Check NFS protocol revision and initialize RPC op vector */
1387 clp->rpc_ops = &nfs_v4_clientops;
1388
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001389 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001390 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001391 if (error < 0)
1392 goto error;
Chuck Lever31b8e2a2012-03-01 17:01:23 -05001393
1394 /* If no clientaddr= option was specified, find a usable cb address */
1395 if (ip_addr == NULL) {
1396 struct sockaddr_storage cb_addr;
1397 struct sockaddr *sap = (struct sockaddr *)&cb_addr;
1398
1399 error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
1400 if (error < 0)
1401 goto error;
1402 error = rpc_ntop(sap, buf, sizeof(buf));
1403 if (error < 0)
1404 goto error;
1405 ip_addr = (const char *)buf;
1406 }
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001407 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001408
1409 error = nfs_idmap_new(clp);
1410 if (error < 0) {
1411 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001412 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001413 goto error;
1414 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001415 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001416
Andy Adamson557134a2009-04-01 09:21:53 -04001417 error = nfs4_init_client_minor_version(clp);
1418 if (error < 0)
1419 goto error;
1420
Andy Adamson76db6d92009-04-01 09:22:38 -04001421 if (!nfs4_has_session(clp))
1422 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever8cab4c32012-05-21 22:45:59 -04001423 return clp;
David Howells54ceac42006-08-22 20:06:13 -04001424
1425error:
1426 nfs_mark_client_ready(clp, error);
Chuck Lever8cab4c32012-05-21 22:45:59 -04001427 nfs_put_client(clp);
David Howells54ceac42006-08-22 20:06:13 -04001428 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
Chuck Lever8cab4c32012-05-21 22:45:59 -04001429 return ERR_PTR(error);
David Howells54ceac42006-08-22 20:06:13 -04001430}
1431
1432/*
1433 * Set up an NFS4 client
1434 */
1435static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001436 const char *hostname,
1437 const struct sockaddr *addr,
1438 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001439 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001440 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001441 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001442 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001443{
Trond Myklebust3a498022007-12-14 14:56:04 -05001444 struct nfs_client_initdata cl_init = {
1445 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001446 .addr = addr,
1447 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001448 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001449 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001450 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001451 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001452 };
David Howells54ceac42006-08-22 20:06:13 -04001453 struct nfs_client *clp;
1454 int error;
1455
1456 dprintk("--> nfs4_set_client()\n");
1457
1458 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001459 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1460 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001461 if (IS_ERR(clp)) {
1462 error = PTR_ERR(clp);
1463 goto error;
1464 }
David Howells54ceac42006-08-22 20:06:13 -04001465
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001466 /*
1467 * Query for the lease time on clientid setup or renewal
1468 *
1469 * Note that this will be set on nfs_clients that were created
1470 * only for the DS role and did not set this bit, but now will
1471 * serve a dual role.
1472 */
1473 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1474
David Howells54ceac42006-08-22 20:06:13 -04001475 server->nfs_client = clp;
1476 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1477 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001478error:
1479 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1480 return error;
1481}
1482
Andy Adamsond83217c2011-03-01 01:34:17 +00001483/*
1484 * Set up a pNFS Data Server client.
1485 *
1486 * Return any existing nfs_client that matches server address,port,version
1487 * and minorversion.
1488 *
1489 * For a new nfs_client, use a soft mount (default), a low retrans and a
1490 * low timeout interval so that if a connection is lost, we retry through
1491 * the MDS.
1492 */
1493struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
Andy Adamson98fc6852012-04-27 17:53:45 -04001494 const struct sockaddr *ds_addr, int ds_addrlen,
1495 int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
Andy Adamsond83217c2011-03-01 01:34:17 +00001496{
1497 struct nfs_client_initdata cl_init = {
1498 .addr = ds_addr,
1499 .addrlen = ds_addrlen,
1500 .rpc_ops = &nfs_v4_clientops,
1501 .proto = ds_proto,
1502 .minorversion = mds_clp->cl_minorversion,
Chuck Lever73ea6662012-05-21 22:44:50 -04001503 .net = mds_clp->cl_net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001504 };
Andy Adamson98fc6852012-04-27 17:53:45 -04001505 struct rpc_timeout ds_timeout;
Andy Adamsond83217c2011-03-01 01:34:17 +00001506 struct nfs_client *clp;
1507
1508 /*
1509 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1510 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1511 * (section 13.1 RFC 5661).
1512 */
Andy Adamson98fc6852012-04-27 17:53:45 -04001513 nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
Andy Adamsond83217c2011-03-01 01:34:17 +00001514 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1515 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1516
1517 dprintk("<-- %s %p\n", __func__, clp);
1518 return clp;
1519}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001520EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001521
1522/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001523 * Session has been established, and the client marked ready.
1524 * Set the mount rsize and wsize with negotiated fore channel
1525 * attributes which will be bound checked in nfs_server_set_fsinfo.
1526 */
1527static void nfs4_session_set_rwsize(struct nfs_server *server)
1528{
1529#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001530 struct nfs4_session *sess;
1531 u32 server_resp_sz;
1532 u32 server_rqst_sz;
1533
Andy Adamson96b09e02009-04-01 09:22:33 -04001534 if (!nfs4_has_session(server->nfs_client))
1535 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001536 sess = server->nfs_client->cl_session;
1537 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1538 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1539
1540 if (server->rsize > server_resp_sz)
1541 server->rsize = server_resp_sz;
1542 if (server->wsize > server_rqst_sz)
1543 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001544#endif /* CONFIG_NFS_V4_1 */
1545}
1546
Trond Myklebust44950b62010-06-17 11:45:12 -04001547static int nfs4_server_common_setup(struct nfs_server *server,
1548 struct nfs_fh *mntfh)
1549{
1550 struct nfs_fattr *fattr;
1551 int error;
1552
1553 BUG_ON(!server->nfs_client);
1554 BUG_ON(!server->nfs_client->rpc_ops);
1555 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1556
Andy Adamson94de8b22011-03-01 01:34:12 +00001557 /* data servers support only a subset of NFSv4.1 */
1558 if (is_ds_only_client(server->nfs_client))
1559 return -EPROTONOSUPPORT;
1560
Trond Myklebust44950b62010-06-17 11:45:12 -04001561 fattr = nfs_alloc_fattr();
1562 if (fattr == NULL)
1563 return -ENOMEM;
1564
1565 /* We must ensure the session is initialised first */
1566 error = nfs4_init_session(server);
1567 if (error < 0)
1568 goto out;
1569
1570 /* Probe the root fh to retrieve its FSID and filehandle */
1571 error = nfs4_get_rootfh(server, mntfh);
1572 if (error < 0)
1573 goto out;
1574
1575 dprintk("Server FSID: %llx:%llx\n",
1576 (unsigned long long) server->fsid.major,
1577 (unsigned long long) server->fsid.minor);
1578 dprintk("Mount FH: %d\n", mntfh->size);
1579
1580 nfs4_session_set_rwsize(server);
1581
1582 error = nfs_probe_fsinfo(server, mntfh, fattr);
1583 if (error < 0)
1584 goto out;
1585
1586 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1587 server->namelen = NFS4_MAXNAMLEN;
1588
Chuck Leverfca52382010-12-24 01:32:32 +00001589 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001590 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001591 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001592out:
1593 nfs_free_fattr(fattr);
1594 return error;
1595}
1596
Andy Adamson96b09e02009-04-01 09:22:33 -04001597/*
David Howells54ceac42006-08-22 20:06:13 -04001598 * Create a version 4 volume record
1599 */
1600static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001601 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001602{
Trond Myklebust33170232007-12-20 16:03:59 -05001603 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001604 int error;
1605
1606 dprintk("--> nfs4_init_server()\n");
1607
Trond Myklebust33170232007-12-20 16:03:59 -05001608 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1609 data->timeo, data->retrans);
1610
Chuck Lever542fcc32008-12-23 15:21:36 -05001611 /* Initialise the client representation from the mount data */
1612 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001613 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1614 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1615 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001616 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001617
Trond Myklebust33170232007-12-20 16:03:59 -05001618 /* Get a client record */
1619 error = nfs4_set_client(server,
1620 data->nfs_server.hostname,
1621 (const struct sockaddr *)&data->nfs_server.address,
1622 data->nfs_server.addrlen,
1623 data->client_address,
1624 data->auth_flavors[0],
1625 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001626 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001627 data->minorversion,
1628 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001629 if (error < 0)
1630 goto error;
1631
Trond Myklebustb064eca22011-02-22 15:44:32 -08001632 /*
1633 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1634 * authentication.
1635 */
1636 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1637 server->caps |= NFS_CAP_UIDGID_NOMAP;
1638
David Howells54ceac42006-08-22 20:06:13 -04001639 if (data->rsize)
1640 server->rsize = nfs_block_size(data->rsize, NULL);
1641 if (data->wsize)
1642 server->wsize = nfs_block_size(data->wsize, NULL);
1643
1644 server->acregmin = data->acregmin * HZ;
1645 server->acregmax = data->acregmax * HZ;
1646 server->acdirmin = data->acdirmin * HZ;
1647 server->acdirmax = data->acdirmax * HZ;
1648
Chuck Leverf22d6d72008-03-14 14:10:22 -04001649 server->port = data->nfs_server.port;
1650
Trond Myklebust33170232007-12-20 16:03:59 -05001651 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001652
Trond Myklebust33170232007-12-20 16:03:59 -05001653error:
David Howells54ceac42006-08-22 20:06:13 -04001654 /* Done */
1655 dprintk("<-- nfs4_init_server() = %d\n", error);
1656 return error;
1657}
1658
1659/*
1660 * Create a version 4 volume record
1661 * - keyed on server and FSID
1662 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001663struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001664 struct nfs_fh *mntfh)
1665{
David Howells54ceac42006-08-22 20:06:13 -04001666 struct nfs_server *server;
1667 int error;
1668
1669 dprintk("--> nfs4_create_server()\n");
1670
1671 server = nfs_alloc_server();
1672 if (!server)
1673 return ERR_PTR(-ENOMEM);
1674
David Howells54ceac42006-08-22 20:06:13 -04001675 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001676 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001677 if (error < 0)
1678 goto error;
1679
Trond Myklebust44950b62010-06-17 11:45:12 -04001680 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001681 if (error < 0)
1682 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001683
David Howells54ceac42006-08-22 20:06:13 -04001684 dprintk("<-- nfs4_create_server() = %p\n", server);
1685 return server;
1686
1687error:
1688 nfs_free_server(server);
1689 dprintk("<-- nfs4_create_server() = error %d\n", error);
1690 return ERR_PTR(error);
1691}
1692
1693/*
1694 * Create an NFS4 referral server record
1695 */
1696struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001697 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001698{
1699 struct nfs_client *parent_client;
1700 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001701 int error;
1702
1703 dprintk("--> nfs4_create_referral_server()\n");
1704
1705 server = nfs_alloc_server();
1706 if (!server)
1707 return ERR_PTR(-ENOMEM);
1708
1709 parent_server = NFS_SB(data->sb);
1710 parent_client = parent_server->nfs_client;
1711
Chuck Lever542fcc32008-12-23 15:21:36 -05001712 /* Initialise the client representation from the parent server */
1713 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001714 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001715
David Howells54ceac42006-08-22 20:06:13 -04001716 /* Get a client representation.
1717 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001718 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001719 data->addr,
1720 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001721 parent_client->cl_ipaddr,
1722 data->authflavor,
Trond Myklebust2446ab62012-03-01 17:00:56 -05001723 rpc_protocol(parent_server->client),
Benny Halevy94a417f2009-04-01 09:21:49 -04001724 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001725 parent_client->cl_mvops->minor_version,
Chuck Lever73ea6662012-05-21 22:44:50 -04001726 parent_client->cl_net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001727 if (error < 0)
1728 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001729
Trond Myklebust33170232007-12-20 16:03:59 -05001730 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001731 if (error < 0)
1732 goto error;
1733
Trond Myklebust44950b62010-06-17 11:45:12 -04001734 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001735 if (error < 0)
1736 goto error;
1737
David Howells54ceac42006-08-22 20:06:13 -04001738 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1739 return server;
1740
1741error:
1742 nfs_free_server(server);
1743 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1744 return ERR_PTR(error);
1745}
1746
1747#endif /* CONFIG_NFS_V4 */
1748
1749/*
1750 * Clone an NFS2, NFS3 or NFS4 server record
1751 */
1752struct nfs_server *nfs_clone_server(struct nfs_server *source,
1753 struct nfs_fh *fh,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001754 struct nfs_fattr *fattr,
1755 rpc_authflavor_t flavor)
David Howells54ceac42006-08-22 20:06:13 -04001756{
1757 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001758 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001759 int error;
1760
1761 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001762 (unsigned long long) fattr->fsid.major,
1763 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001764
1765 server = nfs_alloc_server();
1766 if (!server)
1767 return ERR_PTR(-ENOMEM);
1768
Trond Myklebustfbca7792010-04-16 16:22:46 -04001769 error = -ENOMEM;
1770 fattr_fsinfo = nfs_alloc_fattr();
1771 if (fattr_fsinfo == NULL)
1772 goto out_free_server;
1773
David Howells54ceac42006-08-22 20:06:13 -04001774 /* Copy data from the source */
1775 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001776 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001777 atomic_inc(&server->nfs_client->cl_count);
1778 nfs_server_copy_userdata(server, source);
1779
1780 server->fsid = fattr->fsid;
1781
Trond Myklebust33170232007-12-20 16:03:59 -05001782 error = nfs_init_server_rpcclient(server,
1783 source->client->cl_timeout,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001784 flavor);
David Howells54ceac42006-08-22 20:06:13 -04001785 if (error < 0)
1786 goto out_free_server;
1787 if (!IS_ERR(source->client_acl))
1788 nfs_init_server_aclclient(server);
1789
1790 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001791 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001792 if (error < 0)
1793 goto out_free_server;
1794
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001795 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1796 server->namelen = NFS4_MAXNAMLEN;
1797
David Howells54ceac42006-08-22 20:06:13 -04001798 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001799 (unsigned long long) server->fsid.major,
1800 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001801
1802 error = nfs_start_lockd(server);
1803 if (error < 0)
1804 goto out_free_server;
1805
Chuck Leverfca52382010-12-24 01:32:32 +00001806 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001807 server->mount_time = jiffies;
1808
Trond Myklebustfbca7792010-04-16 16:22:46 -04001809 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001810 dprintk("<-- nfs_clone_server() = %p\n", server);
1811 return server;
1812
1813out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001814 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001815 nfs_free_server(server);
1816 dprintk("<-- nfs_clone_server() = error %d\n", error);
1817 return ERR_PTR(error);
1818}
David Howells6aaca562006-08-22 20:06:13 -04001819
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001820void nfs_clients_init(struct net *net)
1821{
1822 struct nfs_net *nn = net_generic(net, nfs_net_id);
1823
1824 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001825 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001826#ifdef CONFIG_NFS_V4
1827 idr_init(&nn->cb_ident_idr);
1828#endif
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001829 spin_lock_init(&nn->nfs_client_lock);
Chuck Leverf0920752012-05-21 22:45:41 -04001830 nn->boot_time = CURRENT_TIME;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001831}
1832
David Howells6aaca562006-08-22 20:06:13 -04001833#ifdef CONFIG_PROC_FS
1834static struct proc_dir_entry *proc_fs_nfs;
1835
1836static int nfs_server_list_open(struct inode *inode, struct file *file);
1837static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1838static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1839static void nfs_server_list_stop(struct seq_file *p, void *v);
1840static int nfs_server_list_show(struct seq_file *m, void *v);
1841
James Morris88e9d342009-09-22 16:43:43 -07001842static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001843 .start = nfs_server_list_start,
1844 .next = nfs_server_list_next,
1845 .stop = nfs_server_list_stop,
1846 .show = nfs_server_list_show,
1847};
1848
Arjan van de Ven00977a52007-02-12 00:55:34 -08001849static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001850 .open = nfs_server_list_open,
1851 .read = seq_read,
1852 .llseek = seq_lseek,
1853 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001854 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001855};
1856
1857static int nfs_volume_list_open(struct inode *inode, struct file *file);
1858static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1859static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1860static void nfs_volume_list_stop(struct seq_file *p, void *v);
1861static int nfs_volume_list_show(struct seq_file *m, void *v);
1862
James Morris88e9d342009-09-22 16:43:43 -07001863static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001864 .start = nfs_volume_list_start,
1865 .next = nfs_volume_list_next,
1866 .stop = nfs_volume_list_stop,
1867 .show = nfs_volume_list_show,
1868};
1869
Arjan van de Ven00977a52007-02-12 00:55:34 -08001870static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001871 .open = nfs_volume_list_open,
1872 .read = seq_read,
1873 .llseek = seq_lseek,
1874 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001875 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001876};
1877
1878/*
1879 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1880 * we're dealing
1881 */
1882static int nfs_server_list_open(struct inode *inode, struct file *file)
1883{
1884 struct seq_file *m;
1885 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001886 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1887 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001888
1889 ret = seq_open(file, &nfs_server_list_ops);
1890 if (ret < 0)
1891 return ret;
1892
1893 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001894 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001895
1896 return 0;
1897}
1898
1899/*
1900 * set up the iterator to start reading from the server list and return the first item
1901 */
1902static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1903{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001904 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1905
David Howells6aaca562006-08-22 20:06:13 -04001906 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001907 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001908 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001909}
1910
1911/*
1912 * move to next server
1913 */
1914static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1915{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001916 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1917
1918 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001919}
1920
1921/*
1922 * clean up after reading from the transports list
1923 */
1924static void nfs_server_list_stop(struct seq_file *p, void *v)
1925{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001926 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1927
1928 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001929}
1930
1931/*
1932 * display a header line followed by a load of call lines
1933 */
1934static int nfs_server_list_show(struct seq_file *m, void *v)
1935{
1936 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001937 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001938
1939 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001940 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001941 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1942 return 0;
1943 }
1944
1945 /* display one transport per line on subsequent lines */
1946 clp = list_entry(v, struct nfs_client, cl_share_link);
1947
Malahal Naineni940aab42011-09-20 17:27:14 -07001948 /* Check if the client is initialized */
1949 if (clp->cl_cons_state != NFS_CS_READY)
1950 return 0;
1951
Trond Myklebust2446ab62012-03-01 17:00:56 -05001952 rcu_read_lock();
Chuck Lever5d8515c2007-12-10 14:57:16 -05001953 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001954 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001955 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1956 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001957 atomic_read(&clp->cl_count),
1958 clp->cl_hostname);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001959 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001960
1961 return 0;
1962}
1963
1964/*
1965 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1966 */
1967static int nfs_volume_list_open(struct inode *inode, struct file *file)
1968{
1969 struct seq_file *m;
1970 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001971 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1972 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001973
1974 ret = seq_open(file, &nfs_volume_list_ops);
1975 if (ret < 0)
1976 return ret;
1977
1978 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001979 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001980
1981 return 0;
1982}
1983
1984/*
1985 * set up the iterator to start reading from the volume list and return the first item
1986 */
1987static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1988{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001989 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1990
David Howells6aaca562006-08-22 20:06:13 -04001991 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001992 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001993 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001994}
1995
1996/*
1997 * move to next volume
1998 */
1999static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
2000{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00002001 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
2002
2003 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04002004}
2005
2006/*
2007 * clean up after reading from the transports list
2008 */
2009static void nfs_volume_list_stop(struct seq_file *p, void *v)
2010{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00002011 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
2012
2013 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04002014}
2015
2016/*
2017 * display a header line followed by a load of call lines
2018 */
2019static int nfs_volume_list_show(struct seq_file *m, void *v)
2020{
2021 struct nfs_server *server;
2022 struct nfs_client *clp;
2023 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00002024 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04002025
2026 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00002027 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01002028 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04002029 return 0;
2030 }
2031 /* display one transport per line on subsequent lines */
2032 server = list_entry(v, struct nfs_server, master_link);
2033 clp = server->nfs_client;
2034
2035 snprintf(dev, 8, "%u:%u",
2036 MAJOR(server->s_dev), MINOR(server->s_dev));
2037
2038 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04002039 (unsigned long long) server->fsid.major,
2040 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04002041
Trond Myklebust2446ab62012-03-01 17:00:56 -05002042 rcu_read_lock();
David Howells5d1acff2009-04-03 16:42:47 +01002043 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05002044 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05002045 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
2046 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04002047 dev,
David Howells5d1acff2009-04-03 16:42:47 +01002048 fsid,
2049 nfs_server_fscache_state(server));
Trond Myklebust2446ab62012-03-01 17:00:56 -05002050 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04002051
2052 return 0;
2053}
2054
2055/*
2056 * initialise the /proc/fs/nfsfs/ directory
2057 */
2058int __init nfs_fs_proc_init(void)
2059{
2060 struct proc_dir_entry *p;
2061
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002062 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002063 if (!proc_fs_nfs)
2064 goto error_0;
2065
David Howells6aaca562006-08-22 20:06:13 -04002066 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002067 p = proc_create("servers", S_IFREG|S_IRUGO,
2068 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002069 if (!p)
2070 goto error_1;
2071
David Howells6aaca562006-08-22 20:06:13 -04002072 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002073 p = proc_create("volumes", S_IFREG|S_IRUGO,
2074 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002075 if (!p)
2076 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002077 return 0;
2078
2079error_2:
2080 remove_proc_entry("servers", proc_fs_nfs);
2081error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002082 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002083error_0:
2084 return -ENOMEM;
2085}
2086
2087/*
2088 * clean up the /proc/fs/nfsfs/ directory
2089 */
2090void nfs_fs_proc_exit(void)
2091{
2092 remove_proc_entry("volumes", proc_fs_nfs);
2093 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002094 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002095}
2096
2097#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002098
2099module_param(nfs4_disable_idmapping, bool, 0644);
2100MODULE_PARM_DESC(nfs4_disable_idmapping,
2101 "Turn off NFSv4 idmapping when using 'sec=sys'");