blob: d58e8386e6bc80475b2cbea46fca22ebe19ac147 [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050039#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040040#include <linux/nfs_xdr.h>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040041#include <linux/sunrpc/bc_xprt.h>
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000042#include <linux/nsproxy.h>
43#include <linux/pid_namespace.h>
David Howells24c8dbb2006-08-22 20:06:10 -040044
45#include <asm/system.h>
46
47#include "nfs4_fs.h"
48#include "callback.h"
49#include "delegation.h"
50#include "iostat.h"
51#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010052#include "fscache.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040053#include "pnfs.h"
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000054#include "netns.h"
David Howells24c8dbb2006-08-22 20:06:10 -040055
56#define NFSDBG_FACILITY NFSDBG_CLIENT
57
Stanislav Kinsburskyeee17322012-01-10 16:13:19 +040058DEFINE_SPINLOCK(nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -040059static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000060#ifdef CONFIG_NFS_V4
61static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */
62
63/*
64 * Get a unique NFSv4.0 callback identifier which will be used
65 * by the V4.0 callback service to lookup the nfs_client struct
66 */
67static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
68{
69 int ret = 0;
70
71 if (clp->rpc_ops->version != 4 || minorversion != 0)
72 return ret;
73retry:
74 if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL))
75 return -ENOMEM;
76 spin_lock(&nfs_client_lock);
77 ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident);
78 spin_unlock(&nfs_client_lock);
79 if (ret == -EAGAIN)
80 goto retry;
81 return ret;
82}
83#endif /* CONFIG_NFS_V4 */
David Howells24c8dbb2006-08-22 20:06:10 -040084
85/*
Trond Myklebustb064eca22011-02-22 15:44:32 -080086 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
87 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103088static bool nfs4_disable_idmapping = true;
Trond Myklebustb064eca22011-02-22 15:44:32 -080089
90/*
David Howells5006a762006-08-22 20:06:12 -040091 * RPC cruft for NFS
92 */
Trond Myklebusta613fa12012-01-20 13:53:56 -050093static const struct rpc_version *nfs_version[5] = {
David Howells5006a762006-08-22 20:06:12 -040094 [2] = &nfs_version2,
95#ifdef CONFIG_NFS_V3
96 [3] = &nfs_version3,
97#endif
98#ifdef CONFIG_NFS_V4
99 [4] = &nfs_version4,
100#endif
101};
102
Trond Myklebusta613fa12012-01-20 13:53:56 -0500103const struct rpc_program nfs_program = {
David Howells5006a762006-08-22 20:06:12 -0400104 .name = "nfs",
105 .number = NFS_PROGRAM,
106 .nrvers = ARRAY_SIZE(nfs_version),
107 .version = nfs_version,
108 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400109 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -0400110};
111
112struct rpc_stat nfs_rpcstat = {
113 .program = &nfs_program
114};
115
116
117#ifdef CONFIG_NFS_V3_ACL
118static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
Trond Myklebusta613fa12012-01-20 13:53:56 -0500119static const struct rpc_version *nfsacl_version[] = {
David Howells5006a762006-08-22 20:06:12 -0400120 [3] = &nfsacl_version3,
121};
122
Trond Myklebusta613fa12012-01-20 13:53:56 -0500123const struct rpc_program nfsacl_program = {
David Howells5006a762006-08-22 20:06:12 -0400124 .name = "nfsacl",
125 .number = NFS_ACL_PROGRAM,
126 .nrvers = ARRAY_SIZE(nfsacl_version),
127 .version = nfsacl_version,
128 .stats = &nfsacl_rpcstat,
129};
130#endif /* CONFIG_NFS_V3_ACL */
131
Trond Myklebust3a498022007-12-14 14:56:04 -0500132struct nfs_client_initdata {
133 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500134 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500135 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500136 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500137 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400138 u32 minorversion;
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400139 struct net *net;
Trond Myklebust3a498022007-12-14 14:56:04 -0500140};
141
David Howells5006a762006-08-22 20:06:12 -0400142/*
David Howells24c8dbb2006-08-22 20:06:10 -0400143 * Allocate a shared client record
144 *
145 * Since these are allocated/deallocated very rarely, we don't
146 * bother putting them in a slab cache...
147 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500148static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400149{
150 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400151 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700152 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400153
154 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
155 goto error_0;
156
Trond Myklebust40c553192007-12-14 14:56:07 -0500157 clp->rpc_ops = cl_init->rpc_ops;
158
David Howells24c8dbb2006-08-22 20:06:10 -0400159 atomic_set(&clp->cl_count, 1);
160 clp->cl_cons_state = NFS_CS_INITING;
161
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500162 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
163 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400164
Trond Myklebust3a498022007-12-14 14:56:04 -0500165 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700166 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500167 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400168 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400169 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400170 }
171
172 INIT_LIST_HEAD(&clp->cl_superblocks);
173 clp->cl_rpcclient = ERR_PTR(-EINVAL);
174
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500175 clp->cl_proto = cl_init->proto;
176
David Howells24c8dbb2006-08-22 20:06:10 -0400177#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000178 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
179 if (err)
180 goto error_cleanup;
181
David Howells24c8dbb2006-08-22 20:06:10 -0400182 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000183 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400184 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
185 clp->cl_boot_time = CURRENT_TIME;
186 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400187 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400188 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400189#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500190 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400191 if (!IS_ERR(cred))
192 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100193 nfs_fscache_get_client_cookie(clp);
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400194 clp->net = cl_init->net;
David Howells14727282009-04-03 16:42:42 +0100195
David Howells24c8dbb2006-08-22 20:06:10 -0400196 return clp;
197
Benny Halevy71468512009-04-01 09:22:56 -0400198error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400199 kfree(clp);
200error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700201 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400202}
203
Trond Myklebust5dd31772006-08-24 01:03:05 -0400204#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400205#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000206static void nfs4_shutdown_session(struct nfs_client *clp)
207{
208 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400209 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400210}
Andy Adamsonea005282011-01-06 02:04:29 +0000211#else /* CONFIG_NFS_V4_1 */
212static void nfs4_shutdown_session(struct nfs_client *clp)
213{
214}
215#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400216
217/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800218 * Destroy the NFS4 callback service
219 */
220static void nfs4_destroy_callback(struct nfs_client *clp)
221{
222 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400223 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800224}
225
226static void nfs4_shutdown_client(struct nfs_client *clp)
227{
228 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
229 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000230 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800231 nfs4_destroy_callback(clp);
232 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
233 nfs_idmap_delete(clp);
234
235 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
236}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000237
238/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
239void nfs_cleanup_cb_ident_idr(void)
240{
241 idr_destroy(&cb_ident_idr);
242}
243
244/* nfs_client_lock held */
245static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
246{
247 if (clp->cl_cb_ident)
248 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
249}
250
Fred Isamanf7e89172011-01-06 11:36:32 +0000251static void pnfs_init_server(struct nfs_server *server)
252{
253 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
254}
255
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500256static void nfs4_destroy_server(struct nfs_server *server)
257{
258 nfs4_purge_state_owners(server);
259}
260
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800261#else
262static void nfs4_shutdown_client(struct nfs_client *clp)
263{
264}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000265
266void nfs_cleanup_cb_ident_idr(void)
267{
268}
269
270static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
271{
272}
Fred Isamanf7e89172011-01-06 11:36:32 +0000273
274static void pnfs_init_server(struct nfs_server *server)
275{
276}
277
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800278#endif /* CONFIG_NFS_V4 */
279
280/*
David Howells24c8dbb2006-08-22 20:06:10 -0400281 * Destroy a shared client record
282 */
283static void nfs_free_client(struct nfs_client *clp)
284{
Trond Myklebust40c553192007-12-14 14:56:07 -0500285 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400286
Trond Myklebust5dd31772006-08-24 01:03:05 -0400287 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400288
David Howells14727282009-04-03 16:42:42 +0100289 nfs_fscache_release_client_cookie(clp);
290
David Howells24c8dbb2006-08-22 20:06:10 -0400291 /* -EIO all pending I/O */
292 if (!IS_ERR(clp->cl_rpcclient))
293 rpc_shutdown_client(clp->cl_rpcclient);
294
Trond Myklebust7c67db32008-04-07 20:50:11 -0400295 if (clp->cl_machine_cred != NULL)
296 put_rpccred(clp->cl_machine_cred);
297
Benny Halevy1775bc32011-05-20 13:47:33 +0200298 nfs4_deviceid_purge_client(clp);
299
David Howells24c8dbb2006-08-22 20:06:10 -0400300 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400301 kfree(clp->server_scope);
David Howells24c8dbb2006-08-22 20:06:10 -0400302 kfree(clp);
303
304 dprintk("<-- nfs_free_client()\n");
305}
306
307/*
308 * Release a reference to a shared client record
309 */
310void nfs_put_client(struct nfs_client *clp)
311{
David Howells27ba8512006-07-30 14:40:56 -0400312 if (!clp)
313 return;
314
David Howells24c8dbb2006-08-22 20:06:10 -0400315 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
316
317 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
318 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000319 nfs_cb_idr_remove_locked(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400320 spin_unlock(&nfs_client_lock);
321
322 BUG_ON(!list_empty(&clp->cl_superblocks));
323
324 nfs_free_client(clp);
325 }
326}
Andy Adamson16b374c2010-10-20 00:18:04 -0400327EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400328
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500329#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400330/*
331 * Test if two ip6 socket addresses refer to the same socket by
332 * comparing relevant fields. The padding bytes specifically, are not
333 * compared. sin6_flowinfo is not compared because it only affects QoS
334 * and sin6_scope_id is only compared if the address is "link local"
335 * because "link local" addresses need only be unique to a specific
336 * link. Conversely, ordinary unicast addresses might have different
337 * sin6_scope_id.
338 *
339 * The caller should ensure both socket addresses are AF_INET6.
340 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400341static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
342 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400343{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400344 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
345 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400346
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800347 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400348 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800349 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
350 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500351
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800352 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500353}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400354#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
355static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
356 const struct sockaddr *sa2)
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400357{
358 return 0;
359}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500360#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500361
David Howells24c8dbb2006-08-22 20:06:10 -0400362/*
Ian Dalld7371c42009-03-10 20:33:22 -0400363 * Test if two ip4 socket addresses refer to the same socket, by
364 * comparing relevant fields. The padding bytes specifically, are
365 * not compared.
366 *
367 * The caller should ensure both socket addresses are AF_INET.
368 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400369static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
370 const struct sockaddr *sa2)
371{
372 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
373 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
374
375 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
376}
377
378static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
379 const struct sockaddr *sa2)
380{
381 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
382 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
383
384 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
385 (sin1->sin6_port == sin2->sin6_port);
386}
387
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400388static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
389 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400390{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400391 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
392 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400393
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400394 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
395 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400396}
397
398/*
Ian Dalld7371c42009-03-10 20:33:22 -0400399 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400400 * by comparing (only) relevant fields, excluding the port number.
401 */
402static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
403 const struct sockaddr *sa2)
404{
405 if (sa1->sa_family != sa2->sa_family)
406 return 0;
407
408 switch (sa1->sa_family) {
409 case AF_INET:
410 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
411 case AF_INET6:
412 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
413 }
414 return 0;
415}
416
417/*
418 * Test if two socket addresses represent the same actual socket,
419 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400420 */
421static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
422 const struct sockaddr *sa2)
423{
424 if (sa1->sa_family != sa2->sa_family)
425 return 0;
426
427 switch (sa1->sa_family) {
428 case AF_INET:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400429 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400430 case AF_INET6:
Trond Myklebust9f4c899c2009-03-12 14:51:32 -0400431 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400432 }
433 return 0;
434}
435
Andy Adamsonc36fca52011-01-06 02:04:32 +0000436/* Common match routine for v4.0 and v4.1 callback services */
437bool
438nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
439 u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500440{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000441 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500442
Andy Adamsonc36fca52011-01-06 02:04:32 +0000443 /* Don't match clients that failed to initialise */
444 if (!(clp->cl_cons_state == NFS_CS_READY ||
445 clp->cl_cons_state == NFS_CS_SESSION_INITING))
446 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500447
Andy Adamsonc36fca52011-01-06 02:04:32 +0000448 /* Match the version and minorversion */
449 if (clp->rpc_ops->version != 4 ||
450 clp->cl_minorversion != minorversion)
451 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500452
Andy Adamsonc36fca52011-01-06 02:04:32 +0000453 /* Match only the IP address, not the port number */
454 if (!nfs_sockaddr_match_ipaddr(addr, clap))
455 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500456
Andy Adamsonc36fca52011-01-06 02:04:32 +0000457 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500458}
459
460/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500461 * Find an nfs_client on the list that matches the initialisation data
462 * that is supplied.
463 */
464static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400465{
466 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400467 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000468 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400469
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000470 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400471 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700472 /* Don't match clients that failed to initialise properly */
473 if (clp->cl_cons_state < 0)
474 continue;
475
David Howells24c8dbb2006-08-22 20:06:10 -0400476 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500477 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400478 continue;
479
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500480 if (clp->cl_proto != data->proto)
481 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400482 /* Match nfsv4 minorversion */
483 if (clp->cl_minorversion != data->minorversion)
484 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500485 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400486 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400487 continue;
488
Trond Myklebustc81468a2007-12-14 14:56:05 -0500489 atomic_inc(&clp->cl_count);
490 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400491 }
David Howells24c8dbb2006-08-22 20:06:10 -0400492 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400493}
494
495/*
496 * Look up a client by IP address and protocol version
497 * - creates a new record if one doesn't yet exist
498 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000499static struct nfs_client *
500nfs_get_client(const struct nfs_client_initdata *cl_init,
501 const struct rpc_timeout *timeparms,
502 const char *ip_addr,
503 rpc_authflavor_t authflavour,
504 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400505{
506 struct nfs_client *clp, *new = NULL;
507 int error;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000508 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400509
Chuck Leverd7422c42007-12-10 14:58:51 -0500510 dprintk("--> nfs_get_client(%s,v%u)\n",
511 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400512
513 /* see if the client already exists */
514 do {
515 spin_lock(&nfs_client_lock);
516
Trond Myklebustc81468a2007-12-14 14:56:05 -0500517 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400518 if (clp)
519 goto found_client;
520 if (new)
521 goto install_client;
522
523 spin_unlock(&nfs_client_lock);
524
Trond Myklebust3a498022007-12-14 14:56:04 -0500525 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700526 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400527
Chuck Levera21bdd92009-06-17 18:02:10 -0700528 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
529 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400530
531 /* install a new client and return with it unready */
532install_client:
533 clp = new;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000534 list_add(&clp->cl_share_link, &nn->nfs_client_list);
David Howells24c8dbb2006-08-22 20:06:10 -0400535 spin_unlock(&nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000536
537 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
538 authflavour, noresvport);
539 if (error < 0) {
540 nfs_put_client(clp);
541 return ERR_PTR(error);
542 }
David Howells24c8dbb2006-08-22 20:06:10 -0400543 dprintk("--> nfs_get_client() = %p [new]\n", clp);
544 return clp;
545
546 /* found an existing client
547 * - make sure it's ready before returning
548 */
549found_client:
550 spin_unlock(&nfs_client_lock);
551
552 if (new)
553 nfs_free_client(new);
554
Matthew Wilcox150030b2007-12-06 16:24:39 -0500555 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400556 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400557 if (error < 0) {
558 nfs_put_client(clp);
559 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400560 }
561
562 if (clp->cl_cons_state < NFS_CS_READY) {
563 error = clp->cl_cons_state;
564 nfs_put_client(clp);
565 return ERR_PTR(error);
566 }
567
David Howells54ceac42006-08-22 20:06:13 -0400568 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
569
David Howells24c8dbb2006-08-22 20:06:10 -0400570 dprintk("--> nfs_get_client() = %p [share]\n", clp);
571 return clp;
572}
573
574/*
575 * Mark a server as ready or failed
576 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400577void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400578{
579 clp->cl_cons_state = state;
580 wake_up_all(&nfs_client_active_wq);
581}
David Howells5006a762006-08-22 20:06:12 -0400582
583/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400584 * With sessions, the client is not marked ready until after a
585 * successful EXCHANGE_ID and CREATE_SESSION.
586 *
587 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
588 * other versions of NFS can be tried.
589 */
590int nfs4_check_client_ready(struct nfs_client *clp)
591{
592 if (!nfs4_has_session(clp))
593 return 0;
594 if (clp->cl_cons_state < NFS_CS_READY)
595 return -EPROTONOSUPPORT;
596 return 0;
597}
598
599/*
David Howells5006a762006-08-22 20:06:12 -0400600 * Initialise the timeout values for a connection
601 */
602static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
603 unsigned int timeo, unsigned int retrans)
604{
605 to->to_initval = timeo * HZ / 10;
606 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400607
608 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400609 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400610 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400611 if (to->to_retries == 0)
612 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500613 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400614 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400615 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
616 to->to_initval = NFS_MAX_TCP_TIMEOUT;
617 to->to_increment = to->to_initval;
618 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500619 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
620 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
621 if (to->to_maxval < to->to_initval)
622 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400623 to->to_exponential = 0;
624 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400625 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400626 if (to->to_retries == 0)
627 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400628 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400629 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400630 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
631 to->to_initval = NFS_MAX_UDP_TIMEOUT;
632 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
633 to->to_exponential = 1;
634 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400635 default:
636 BUG();
David Howells5006a762006-08-22 20:06:12 -0400637 }
638}
639
640/*
641 * Create an RPC client handle
642 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500643static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500644 const struct rpc_timeout *timeparms,
645 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500646 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400647{
David Howells5006a762006-08-22 20:06:12 -0400648 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400649 struct rpc_create_args args = {
Stanislav Kinsbursky6d59b8d2012-01-10 16:12:54 +0400650 .net = clp->net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500651 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400652 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500653 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500654 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400655 .servername = clp->cl_hostname,
656 .program = &nfs_program,
657 .version = clp->rpc_ops->version,
658 .authflavor = flavor,
659 };
David Howells5006a762006-08-22 20:06:12 -0400660
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500661 if (discrtry)
662 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
663 if (noresvport)
664 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
665
David Howells5006a762006-08-22 20:06:12 -0400666 if (!IS_ERR(clp->cl_rpcclient))
667 return 0;
668
Chuck Lever41877d22006-08-22 20:06:20 -0400669 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400670 if (IS_ERR(clnt)) {
671 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700672 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400673 return PTR_ERR(clnt);
674 }
675
David Howells5006a762006-08-22 20:06:12 -0400676 clp->cl_rpcclient = clnt;
677 return 0;
678}
David Howells54ceac42006-08-22 20:06:13 -0400679
680/*
681 * Version 2 or 3 client destruction
682 */
683static void nfs_destroy_server(struct nfs_server *server)
684{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400685 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
686 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500687 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400688}
689
690/*
691 * Version 2 or 3 lockd setup
692 */
693static int nfs_start_lockd(struct nfs_server *server)
694{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500695 struct nlm_host *host;
696 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500697 struct nlmclnt_initdata nlm_init = {
698 .hostname = clp->cl_hostname,
699 .address = (struct sockaddr *)&clp->cl_addr,
700 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500701 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500702 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
703 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500704 };
David Howells54ceac42006-08-22 20:06:13 -0400705
Chuck Lever883bb162008-01-15 16:04:20 -0500706 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500707 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400708 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
709 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500710 return 0;
711
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400712 switch (clp->cl_proto) {
713 default:
714 nlm_init.protocol = IPPROTO_TCP;
715 break;
716 case XPRT_TRANSPORT_UDP:
717 nlm_init.protocol = IPPROTO_UDP;
718 }
719
Chuck Lever883bb162008-01-15 16:04:20 -0500720 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500721 if (IS_ERR(host))
722 return PTR_ERR(host);
723
724 server->nlm_host = host;
725 server->destroy = nfs_destroy_server;
726 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400727}
728
729/*
730 * Initialise an NFSv3 ACL client connection
731 */
732#ifdef CONFIG_NFS_V3_ACL
733static void nfs_init_server_aclclient(struct nfs_server *server)
734{
Trond Myklebust40c553192007-12-14 14:56:07 -0500735 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400736 goto out_noacl;
737 if (server->flags & NFS_MOUNT_NOACL)
738 goto out_noacl;
739
740 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
741 if (IS_ERR(server->client_acl))
742 goto out_noacl;
743
744 /* No errors! Assume that Sun nfsacls are supported */
745 server->caps |= NFS_CAP_ACLS;
746 return;
747
748out_noacl:
749 server->caps &= ~NFS_CAP_ACLS;
750}
751#else
752static inline void nfs_init_server_aclclient(struct nfs_server *server)
753{
754 server->flags &= ~NFS_MOUNT_NOACL;
755 server->caps &= ~NFS_CAP_ACLS;
756}
757#endif
758
759/*
760 * Create a general RPC client
761 */
Trond Myklebust33170232007-12-20 16:03:59 -0500762static int nfs_init_server_rpcclient(struct nfs_server *server,
763 const struct rpc_timeout *timeo,
764 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400765{
766 struct nfs_client *clp = server->nfs_client;
767
768 server->client = rpc_clone_client(clp->cl_rpcclient);
769 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700770 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400771 return PTR_ERR(server->client);
772 }
773
Trond Myklebust33170232007-12-20 16:03:59 -0500774 memcpy(&server->client->cl_timeout_default,
775 timeo,
776 sizeof(server->client->cl_timeout_default));
777 server->client->cl_timeout = &server->client->cl_timeout_default;
778
David Howells54ceac42006-08-22 20:06:13 -0400779 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
780 struct rpc_auth *auth;
781
782 auth = rpcauth_create(pseudoflavour, server->client);
783 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700784 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400785 return PTR_ERR(auth);
786 }
787 }
788 server->client->cl_softrtry = 0;
789 if (server->flags & NFS_MOUNT_SOFT)
790 server->client->cl_softrtry = 1;
791
David Howells54ceac42006-08-22 20:06:13 -0400792 return 0;
793}
794
795/*
796 * Initialise an NFS2 or NFS3 client
797 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000798int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
799 const char *ip_addr, rpc_authflavor_t authflavour,
800 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400801{
David Howells54ceac42006-08-22 20:06:13 -0400802 int error;
803
804 if (clp->cl_cons_state == NFS_CS_READY) {
805 /* the client is already initialised */
806 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
807 return 0;
808 }
809
David Howells54ceac42006-08-22 20:06:13 -0400810 /*
811 * Create a client RPC handle for doing FSSTAT with UNIX auth only
812 * - RFC 2623, sec 2.3.2
813 */
Chuck Leverd7403512008-12-23 15:21:37 -0500814 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000815 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400816 if (error < 0)
817 goto error;
818 nfs_mark_client_ready(clp, NFS_CS_READY);
819 return 0;
820
821error:
822 nfs_mark_client_ready(clp, error);
823 dprintk("<-- nfs_init_client() = xerror %d\n", error);
824 return error;
825}
826
827/*
828 * Create a version 2 or 3 client
829 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400830static int nfs_init_server(struct nfs_server *server,
831 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400832{
Trond Myklebust3a498022007-12-14 14:56:04 -0500833 struct nfs_client_initdata cl_init = {
834 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500835 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500836 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500837 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500838 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400839 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500840 };
Trond Myklebust33170232007-12-20 16:03:59 -0500841 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400842 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500843 int error;
David Howells54ceac42006-08-22 20:06:13 -0400844
845 dprintk("--> nfs_init_server()\n");
846
847#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400848 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500849 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400850#endif
851
Andy Adamson45a52a02011-03-01 01:34:08 +0000852 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
853 data->timeo, data->retrans);
854
David Howells54ceac42006-08-22 20:06:13 -0400855 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000856 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
857 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400858 if (IS_ERR(clp)) {
859 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
860 return PTR_ERR(clp);
861 }
862
David Howells54ceac42006-08-22 20:06:13 -0400863 server->nfs_client = clp;
864
865 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400866 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100867 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400868 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
869 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
870 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400871
872 if (data->rsize)
873 server->rsize = nfs_block_size(data->rsize, NULL);
874 if (data->wsize)
875 server->wsize = nfs_block_size(data->wsize, NULL);
876
877 server->acregmin = data->acregmin * HZ;
878 server->acregmax = data->acregmax * HZ;
879 server->acdirmin = data->acdirmin * HZ;
880 server->acdirmax = data->acdirmax * HZ;
881
882 /* Start lockd here, before we might error out */
883 error = nfs_start_lockd(server);
884 if (error < 0)
885 goto error;
886
Chuck Leverf22d6d72008-03-14 14:10:22 -0400887 server->port = data->nfs_server.port;
888
Trond Myklebust33170232007-12-20 16:03:59 -0500889 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400890 if (error < 0)
891 goto error;
892
Chuck Lever3f8400d2008-03-14 14:10:30 -0400893 /* Preserve the values of mount_server-related mount options */
894 if (data->mount_server.addrlen) {
895 memcpy(&server->mountd_address, &data->mount_server.address,
896 data->mount_server.addrlen);
897 server->mountd_addrlen = data->mount_server.addrlen;
898 }
899 server->mountd_version = data->mount_server.version;
900 server->mountd_port = data->mount_server.port;
901 server->mountd_protocol = data->mount_server.protocol;
902
David Howells54ceac42006-08-22 20:06:13 -0400903 server->namelen = data->namlen;
904 /* Create a client RPC handle for the NFSv3 ACL management interface */
905 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400906 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
907 return 0;
908
909error:
910 server->nfs_client = NULL;
911 nfs_put_client(clp);
912 dprintk("<-- nfs_init_server() = xerror %d\n", error);
913 return error;
914}
915
916/*
917 * Load up the server record from information gained in an fsinfo record
918 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400919static void nfs_server_set_fsinfo(struct nfs_server *server,
920 struct nfs_fh *mntfh,
921 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400922{
923 unsigned long max_rpc_payload;
924
925 /* Work out a lot of parameters */
926 if (server->rsize == 0)
927 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
928 if (server->wsize == 0)
929 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
930
931 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
932 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
933 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
934 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
935
936 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
937 if (server->rsize > max_rpc_payload)
938 server->rsize = max_rpc_payload;
939 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
940 server->rsize = NFS_MAX_FILE_IO_SIZE;
941 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700942
Jens Axboed9938312009-06-12 14:45:52 +0200943 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400944 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
945
946 if (server->wsize > max_rpc_payload)
947 server->wsize = max_rpc_payload;
948 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
949 server->wsize = NFS_MAX_FILE_IO_SIZE;
950 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400951 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400952 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400953
David Howells54ceac42006-08-22 20:06:13 -0400954 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
955
956 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400957 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
958 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400959 if (server->dtsize > server->rsize)
960 server->dtsize = server->rsize;
961
962 if (server->flags & NFS_MOUNT_NOAC) {
963 server->acregmin = server->acregmax = 0;
964 server->acdirmin = server->acdirmax = 0;
965 }
966
967 server->maxfilesize = fsinfo->maxfilesize;
968
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700969 server->time_delta = fsinfo->time_delta;
970
David Howells54ceac42006-08-22 20:06:13 -0400971 /* We're airborne Set socket buffersize */
972 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
973}
974
975/*
976 * Probe filesystem information, including the FSID on v2/v3
977 */
978static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
979{
980 struct nfs_fsinfo fsinfo;
981 struct nfs_client *clp = server->nfs_client;
982 int error;
983
984 dprintk("--> nfs_probe_fsinfo()\n");
985
986 if (clp->rpc_ops->set_capabilities != NULL) {
987 error = clp->rpc_ops->set_capabilities(server, mntfh);
988 if (error < 0)
989 goto out_error;
990 }
991
992 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400993 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400994 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
995 if (error < 0)
996 goto out_error;
997
Benny Halevy738fd0f32011-07-30 20:52:36 -0400998 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -0400999
1000 /* Get some general file system info */
1001 if (server->namelen == 0) {
1002 struct nfs_pathconf pathinfo;
1003
1004 pathinfo.fattr = fattr;
1005 nfs_fattr_init(fattr);
1006
1007 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1008 server->namelen = pathinfo.max_namelen;
1009 }
1010
1011 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1012 return 0;
1013
1014out_error:
1015 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1016 return error;
1017}
1018
1019/*
1020 * Copy useful information when duplicating a server record
1021 */
1022static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1023{
1024 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001025 target->rsize = source->rsize;
1026 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001027 target->acregmin = source->acregmin;
1028 target->acregmax = source->acregmax;
1029 target->acdirmin = source->acdirmin;
1030 target->acdirmax = source->acdirmax;
1031 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001032 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001033}
1034
Chuck Leverfca52382010-12-24 01:32:32 +00001035static void nfs_server_insert_lists(struct nfs_server *server)
1036{
1037 struct nfs_client *clp = server->nfs_client;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001038 struct nfs_net *nn = net_generic(clp->net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001039
1040 spin_lock(&nfs_client_lock);
1041 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001042 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001043 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001044 spin_unlock(&nfs_client_lock);
1045
1046}
1047
1048static void nfs_server_remove_lists(struct nfs_server *server)
1049{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001050 struct nfs_client *clp = server->nfs_client;
1051
Chuck Leverfca52382010-12-24 01:32:32 +00001052 spin_lock(&nfs_client_lock);
1053 list_del_rcu(&server->client_link);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001054 if (clp && list_empty(&clp->cl_superblocks))
1055 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001056 list_del(&server->master_link);
1057 spin_unlock(&nfs_client_lock);
1058
1059 synchronize_rcu();
1060}
1061
David Howells54ceac42006-08-22 20:06:13 -04001062/*
1063 * Allocate and initialise a server record
1064 */
1065static struct nfs_server *nfs_alloc_server(void)
1066{
1067 struct nfs_server *server;
1068
1069 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1070 if (!server)
1071 return NULL;
1072
1073 server->client = server->client_acl = ERR_PTR(-EINVAL);
1074
1075 /* Zero out the NFS state stuff */
1076 INIT_LIST_HEAD(&server->client_link);
1077 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001078 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001079 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001080 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001081
Steve Dicksonef818a22007-11-08 04:05:04 -05001082 atomic_set(&server->active, 0);
1083
David Howells54ceac42006-08-22 20:06:13 -04001084 server->io_stats = nfs_alloc_iostats();
1085 if (!server->io_stats) {
1086 kfree(server);
1087 return NULL;
1088 }
1089
Jens Axboe48d07642009-09-21 09:59:39 +02001090 if (bdi_init(&server->backing_dev_info)) {
1091 nfs_free_iostats(server->io_stats);
1092 kfree(server);
1093 return NULL;
1094 }
1095
Trond Myklebust9157c312012-01-17 22:04:24 -05001096 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001097 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001098 pnfs_init_server(server);
1099
David Howells54ceac42006-08-22 20:06:13 -04001100 return server;
1101}
1102
1103/*
1104 * Free up a server record
1105 */
1106void nfs_free_server(struct nfs_server *server)
1107{
1108 dprintk("--> nfs_free_server()\n");
1109
Chuck Leverfca52382010-12-24 01:32:32 +00001110 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001111 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001112
1113 if (server->destroy != NULL)
1114 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001115
1116 if (!IS_ERR(server->client_acl))
1117 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001118 if (!IS_ERR(server->client))
1119 rpc_shutdown_client(server->client);
1120
1121 nfs_put_client(server->nfs_client);
1122
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001123 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001124 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001125 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001126 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001127 kfree(server);
1128 nfs_release_automount_timer();
1129 dprintk("<-- nfs_free_server()\n");
1130}
1131
1132/*
1133 * Create a version 2 or 3 volume record
1134 * - keyed on server and FSID
1135 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001136struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001137 struct nfs_fh *mntfh)
1138{
1139 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001140 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001141 int error;
1142
1143 server = nfs_alloc_server();
1144 if (!server)
1145 return ERR_PTR(-ENOMEM);
1146
Trond Myklebustfbca7792010-04-16 16:22:46 -04001147 error = -ENOMEM;
1148 fattr = nfs_alloc_fattr();
1149 if (fattr == NULL)
1150 goto error;
1151
David Howells54ceac42006-08-22 20:06:13 -04001152 /* Get a client representation */
1153 error = nfs_init_server(server, data);
1154 if (error < 0)
1155 goto error;
1156
1157 BUG_ON(!server->nfs_client);
1158 BUG_ON(!server->nfs_client->rpc_ops);
1159 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1160
1161 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001162 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001163 if (error < 0)
1164 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001165 if (server->nfs_client->rpc_ops->version == 3) {
1166 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1167 server->namelen = NFS3_MAXNAMLEN;
1168 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1169 server->caps |= NFS_CAP_READDIRPLUS;
1170 } else {
1171 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1172 server->namelen = NFS2_MAXNAMLEN;
1173 }
1174
Trond Myklebustfbca7792010-04-16 16:22:46 -04001175 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1176 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001177 if (error < 0) {
1178 dprintk("nfs_create_server: getattr error = %d\n", -error);
1179 goto error;
1180 }
1181 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001182 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001183
David Howells6daabf12006-08-24 15:44:16 -04001184 dprintk("Server FSID: %llx:%llx\n",
1185 (unsigned long long) server->fsid.major,
1186 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001187
Chuck Leverfca52382010-12-24 01:32:32 +00001188 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001189 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001190 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001191 return server;
1192
1193error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001194 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001195 nfs_free_server(server);
1196 return ERR_PTR(error);
1197}
1198
1199#ifdef CONFIG_NFS_V4
1200/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001201 * NFSv4.0 callback thread helper
1202 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001203 * Find a client by callback identifier
1204 */
1205struct nfs_client *
1206nfs4_find_client_ident(int cb_ident)
1207{
1208 struct nfs_client *clp;
1209
1210 spin_lock(&nfs_client_lock);
1211 clp = idr_find(&cb_ident_idr, cb_ident);
1212 if (clp)
1213 atomic_inc(&clp->cl_count);
1214 spin_unlock(&nfs_client_lock);
1215 return clp;
1216}
1217
1218#if defined(CONFIG_NFS_V4_1)
1219/*
1220 * NFSv4.1 callback thread helper
1221 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1222 * minorversion, and sessionID
1223 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001224 * Returns NULL if no such client
1225 */
1226struct nfs_client *
1227nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001228 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001229{
1230 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001231 struct nfs_net *nn = net_generic(&init_net, nfs_net_id);
Andy Adamsonc36fca52011-01-06 02:04:32 +00001232
1233 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001234 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Andy Adamsonc36fca52011-01-06 02:04:32 +00001235 if (nfs4_cb_match_client(addr, clp, 1) == false)
1236 continue;
1237
1238 if (!nfs4_has_session(clp))
1239 continue;
1240
Andy Adamson778be232011-01-25 15:38:01 +00001241 /* Match sessionid*/
1242 if (memcmp(clp->cl_session->sess_id.data,
1243 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001244 continue;
1245
1246 atomic_inc(&clp->cl_count);
1247 spin_unlock(&nfs_client_lock);
1248 return clp;
1249 }
1250 spin_unlock(&nfs_client_lock);
1251 return NULL;
1252}
1253
1254#else /* CONFIG_NFS_V4_1 */
1255
1256struct nfs_client *
1257nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001258 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001259{
1260 return NULL;
1261}
1262#endif /* CONFIG_NFS_V4_1 */
1263
1264/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001265 * Initialize the NFS4 callback service
1266 */
1267static int nfs4_init_callback(struct nfs_client *clp)
1268{
1269 int error;
1270
1271 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001272 if (nfs4_has_session(clp)) {
1273 error = xprt_setup_backchannel(
1274 clp->cl_rpcclient->cl_xprt,
1275 NFS41_BC_MIN_CALLBACKS);
1276 if (error < 0)
1277 return error;
1278 }
1279
Trond Myklebust97dc1352010-06-16 09:52:26 -04001280 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001281 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001282 if (error < 0) {
1283 dprintk("%s: failed to start callback. Error = %d\n",
1284 __func__, error);
1285 return error;
1286 }
1287 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1288 }
1289 return 0;
1290}
1291
1292/*
Andy Adamson557134a2009-04-01 09:21:53 -04001293 * Initialize the minor version specific parts of an NFS4 client record
1294 */
1295static int nfs4_init_client_minor_version(struct nfs_client *clp)
1296{
1297#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001298 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001299 struct nfs4_session *session = NULL;
1300 /*
1301 * Create the session and mark it expired.
1302 * When a SEQUENCE operation encounters the expired session
1303 * it will do session recovery to initialize it.
1304 */
1305 session = nfs4_alloc_session(clp);
1306 if (!session)
1307 return -ENOMEM;
1308
1309 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001310 /*
1311 * The create session reply races with the server back
1312 * channel probe. Mark the client NFS_CS_SESSION_INITING
1313 * so that the client back channel can find the
1314 * nfs_client struct
1315 */
1316 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001317 }
1318#endif /* CONFIG_NFS_V4_1 */
1319
Benny Halevy71468512009-04-01 09:22:56 -04001320 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001321}
1322
1323/*
David Howells54ceac42006-08-22 20:06:13 -04001324 * Initialise an NFS4 client record
1325 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001326int nfs4_init_client(struct nfs_client *clp,
1327 const struct rpc_timeout *timeparms,
1328 const char *ip_addr,
1329 rpc_authflavor_t authflavour,
1330 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001331{
1332 int error;
1333
1334 if (clp->cl_cons_state == NFS_CS_READY) {
1335 /* the client is initialised already */
1336 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1337 return 0;
1338 }
1339
1340 /* Check NFS protocol revision and initialize RPC op vector */
1341 clp->rpc_ops = &nfs_v4_clientops;
1342
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001343 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001344 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001345 if (error < 0)
1346 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001347 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001348
1349 error = nfs_idmap_new(clp);
1350 if (error < 0) {
1351 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001352 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001353 goto error;
1354 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001355 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001356
Andy Adamson557134a2009-04-01 09:21:53 -04001357 error = nfs4_init_client_minor_version(clp);
1358 if (error < 0)
1359 goto error;
1360
Andy Adamson76db6d92009-04-01 09:22:38 -04001361 if (!nfs4_has_session(clp))
1362 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001363 return 0;
1364
1365error:
1366 nfs_mark_client_ready(clp, error);
1367 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1368 return error;
1369}
1370
1371/*
1372 * Set up an NFS4 client
1373 */
1374static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001375 const char *hostname,
1376 const struct sockaddr *addr,
1377 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001378 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001379 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001380 int proto, const struct rpc_timeout *timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001381 u32 minorversion, struct net *net)
David Howells54ceac42006-08-22 20:06:13 -04001382{
Trond Myklebust3a498022007-12-14 14:56:04 -05001383 struct nfs_client_initdata cl_init = {
1384 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001385 .addr = addr,
1386 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001387 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001388 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001389 .minorversion = minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001390 .net = net,
Trond Myklebust3a498022007-12-14 14:56:04 -05001391 };
David Howells54ceac42006-08-22 20:06:13 -04001392 struct nfs_client *clp;
1393 int error;
1394
1395 dprintk("--> nfs4_set_client()\n");
1396
1397 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001398 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1399 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001400 if (IS_ERR(clp)) {
1401 error = PTR_ERR(clp);
1402 goto error;
1403 }
David Howells54ceac42006-08-22 20:06:13 -04001404
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001405 /*
1406 * Query for the lease time on clientid setup or renewal
1407 *
1408 * Note that this will be set on nfs_clients that were created
1409 * only for the DS role and did not set this bit, but now will
1410 * serve a dual role.
1411 */
1412 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1413
David Howells54ceac42006-08-22 20:06:13 -04001414 server->nfs_client = clp;
1415 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1416 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001417error:
1418 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1419 return error;
1420}
1421
Andy Adamsond83217c2011-03-01 01:34:17 +00001422/*
1423 * Set up a pNFS Data Server client.
1424 *
1425 * Return any existing nfs_client that matches server address,port,version
1426 * and minorversion.
1427 *
1428 * For a new nfs_client, use a soft mount (default), a low retrans and a
1429 * low timeout interval so that if a connection is lost, we retry through
1430 * the MDS.
1431 */
1432struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1433 const struct sockaddr *ds_addr,
1434 int ds_addrlen, int ds_proto)
1435{
1436 struct nfs_client_initdata cl_init = {
1437 .addr = ds_addr,
1438 .addrlen = ds_addrlen,
1439 .rpc_ops = &nfs_v4_clientops,
1440 .proto = ds_proto,
1441 .minorversion = mds_clp->cl_minorversion,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001442 .net = mds_clp->net,
Andy Adamsond83217c2011-03-01 01:34:17 +00001443 };
1444 struct rpc_timeout ds_timeout = {
1445 .to_initval = 15 * HZ,
1446 .to_maxval = 15 * HZ,
1447 .to_retries = 1,
1448 .to_exponential = 1,
1449 };
1450 struct nfs_client *clp;
1451
1452 /*
1453 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1454 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1455 * (section 13.1 RFC 5661).
1456 */
1457 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1458 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1459
1460 dprintk("<-- %s %p\n", __func__, clp);
1461 return clp;
1462}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001463EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001464
1465/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001466 * Session has been established, and the client marked ready.
1467 * Set the mount rsize and wsize with negotiated fore channel
1468 * attributes which will be bound checked in nfs_server_set_fsinfo.
1469 */
1470static void nfs4_session_set_rwsize(struct nfs_server *server)
1471{
1472#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001473 struct nfs4_session *sess;
1474 u32 server_resp_sz;
1475 u32 server_rqst_sz;
1476
Andy Adamson96b09e02009-04-01 09:22:33 -04001477 if (!nfs4_has_session(server->nfs_client))
1478 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001479 sess = server->nfs_client->cl_session;
1480 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1481 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1482
1483 if (server->rsize > server_resp_sz)
1484 server->rsize = server_resp_sz;
1485 if (server->wsize > server_rqst_sz)
1486 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001487#endif /* CONFIG_NFS_V4_1 */
1488}
1489
Trond Myklebust44950b62010-06-17 11:45:12 -04001490static int nfs4_server_common_setup(struct nfs_server *server,
1491 struct nfs_fh *mntfh)
1492{
1493 struct nfs_fattr *fattr;
1494 int error;
1495
1496 BUG_ON(!server->nfs_client);
1497 BUG_ON(!server->nfs_client->rpc_ops);
1498 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1499
Andy Adamson94de8b22011-03-01 01:34:12 +00001500 /* data servers support only a subset of NFSv4.1 */
1501 if (is_ds_only_client(server->nfs_client))
1502 return -EPROTONOSUPPORT;
1503
Trond Myklebust44950b62010-06-17 11:45:12 -04001504 fattr = nfs_alloc_fattr();
1505 if (fattr == NULL)
1506 return -ENOMEM;
1507
1508 /* We must ensure the session is initialised first */
1509 error = nfs4_init_session(server);
1510 if (error < 0)
1511 goto out;
1512
1513 /* Probe the root fh to retrieve its FSID and filehandle */
1514 error = nfs4_get_rootfh(server, mntfh);
1515 if (error < 0)
1516 goto out;
1517
1518 dprintk("Server FSID: %llx:%llx\n",
1519 (unsigned long long) server->fsid.major,
1520 (unsigned long long) server->fsid.minor);
1521 dprintk("Mount FH: %d\n", mntfh->size);
1522
1523 nfs4_session_set_rwsize(server);
1524
1525 error = nfs_probe_fsinfo(server, mntfh, fattr);
1526 if (error < 0)
1527 goto out;
1528
1529 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1530 server->namelen = NFS4_MAXNAMLEN;
1531
Chuck Leverfca52382010-12-24 01:32:32 +00001532 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001533 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001534 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001535out:
1536 nfs_free_fattr(fattr);
1537 return error;
1538}
1539
Andy Adamson96b09e02009-04-01 09:22:33 -04001540/*
David Howells54ceac42006-08-22 20:06:13 -04001541 * Create a version 4 volume record
1542 */
1543static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001544 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001545{
Trond Myklebust33170232007-12-20 16:03:59 -05001546 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001547 int error;
1548
1549 dprintk("--> nfs4_init_server()\n");
1550
Trond Myklebust33170232007-12-20 16:03:59 -05001551 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1552 data->timeo, data->retrans);
1553
Chuck Lever542fcc32008-12-23 15:21:36 -05001554 /* Initialise the client representation from the mount data */
1555 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001556 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1557 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1558 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001559 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001560
Trond Myklebust33170232007-12-20 16:03:59 -05001561 /* Get a client record */
1562 error = nfs4_set_client(server,
1563 data->nfs_server.hostname,
1564 (const struct sockaddr *)&data->nfs_server.address,
1565 data->nfs_server.addrlen,
1566 data->client_address,
1567 data->auth_flavors[0],
1568 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001569 &timeparms,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001570 data->minorversion,
1571 data->net);
Trond Myklebust33170232007-12-20 16:03:59 -05001572 if (error < 0)
1573 goto error;
1574
Trond Myklebustb064eca22011-02-22 15:44:32 -08001575 /*
1576 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1577 * authentication.
1578 */
1579 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1580 server->caps |= NFS_CAP_UIDGID_NOMAP;
1581
David Howells54ceac42006-08-22 20:06:13 -04001582 if (data->rsize)
1583 server->rsize = nfs_block_size(data->rsize, NULL);
1584 if (data->wsize)
1585 server->wsize = nfs_block_size(data->wsize, NULL);
1586
1587 server->acregmin = data->acregmin * HZ;
1588 server->acregmax = data->acregmax * HZ;
1589 server->acdirmin = data->acdirmin * HZ;
1590 server->acdirmax = data->acdirmax * HZ;
1591
Chuck Leverf22d6d72008-03-14 14:10:22 -04001592 server->port = data->nfs_server.port;
1593
Trond Myklebust33170232007-12-20 16:03:59 -05001594 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001595
Trond Myklebust33170232007-12-20 16:03:59 -05001596error:
David Howells54ceac42006-08-22 20:06:13 -04001597 /* Done */
1598 dprintk("<-- nfs4_init_server() = %d\n", error);
1599 return error;
1600}
1601
1602/*
1603 * Create a version 4 volume record
1604 * - keyed on server and FSID
1605 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001606struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001607 struct nfs_fh *mntfh)
1608{
David Howells54ceac42006-08-22 20:06:13 -04001609 struct nfs_server *server;
1610 int error;
1611
1612 dprintk("--> nfs4_create_server()\n");
1613
1614 server = nfs_alloc_server();
1615 if (!server)
1616 return ERR_PTR(-ENOMEM);
1617
David Howells54ceac42006-08-22 20:06:13 -04001618 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001619 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001620 if (error < 0)
1621 goto error;
1622
Trond Myklebust44950b62010-06-17 11:45:12 -04001623 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001624 if (error < 0)
1625 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001626
David Howells54ceac42006-08-22 20:06:13 -04001627 dprintk("<-- nfs4_create_server() = %p\n", server);
1628 return server;
1629
1630error:
1631 nfs_free_server(server);
1632 dprintk("<-- nfs4_create_server() = error %d\n", error);
1633 return ERR_PTR(error);
1634}
1635
1636/*
1637 * Create an NFS4 referral server record
1638 */
1639struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001640 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001641{
1642 struct nfs_client *parent_client;
1643 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001644 int error;
1645
1646 dprintk("--> nfs4_create_referral_server()\n");
1647
1648 server = nfs_alloc_server();
1649 if (!server)
1650 return ERR_PTR(-ENOMEM);
1651
1652 parent_server = NFS_SB(data->sb);
1653 parent_client = parent_server->nfs_client;
1654
Chuck Lever542fcc32008-12-23 15:21:36 -05001655 /* Initialise the client representation from the parent server */
1656 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001657 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001658
David Howells54ceac42006-08-22 20:06:13 -04001659 /* Get a client representation.
1660 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001661 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001662 data->addr,
1663 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001664 parent_client->cl_ipaddr,
1665 data->authflavor,
1666 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001667 parent_server->client->cl_timeout,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +04001668 parent_client->cl_mvops->minor_version,
1669 parent_client->net);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001670 if (error < 0)
1671 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001672
Trond Myklebust33170232007-12-20 16:03:59 -05001673 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001674 if (error < 0)
1675 goto error;
1676
Trond Myklebust44950b62010-06-17 11:45:12 -04001677 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001678 if (error < 0)
1679 goto error;
1680
David Howells54ceac42006-08-22 20:06:13 -04001681 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1682 return server;
1683
1684error:
1685 nfs_free_server(server);
1686 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1687 return ERR_PTR(error);
1688}
1689
1690#endif /* CONFIG_NFS_V4 */
1691
1692/*
1693 * Clone an NFS2, NFS3 or NFS4 server record
1694 */
1695struct nfs_server *nfs_clone_server(struct nfs_server *source,
1696 struct nfs_fh *fh,
1697 struct nfs_fattr *fattr)
1698{
1699 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001700 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001701 int error;
1702
1703 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001704 (unsigned long long) fattr->fsid.major,
1705 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001706
1707 server = nfs_alloc_server();
1708 if (!server)
1709 return ERR_PTR(-ENOMEM);
1710
Trond Myklebustfbca7792010-04-16 16:22:46 -04001711 error = -ENOMEM;
1712 fattr_fsinfo = nfs_alloc_fattr();
1713 if (fattr_fsinfo == NULL)
1714 goto out_free_server;
1715
David Howells54ceac42006-08-22 20:06:13 -04001716 /* Copy data from the source */
1717 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001718 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001719 atomic_inc(&server->nfs_client->cl_count);
1720 nfs_server_copy_userdata(server, source);
1721
1722 server->fsid = fattr->fsid;
1723
Trond Myklebust33170232007-12-20 16:03:59 -05001724 error = nfs_init_server_rpcclient(server,
1725 source->client->cl_timeout,
1726 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001727 if (error < 0)
1728 goto out_free_server;
1729 if (!IS_ERR(source->client_acl))
1730 nfs_init_server_aclclient(server);
1731
1732 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001733 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001734 if (error < 0)
1735 goto out_free_server;
1736
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001737 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1738 server->namelen = NFS4_MAXNAMLEN;
1739
David Howells54ceac42006-08-22 20:06:13 -04001740 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001741 (unsigned long long) server->fsid.major,
1742 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001743
1744 error = nfs_start_lockd(server);
1745 if (error < 0)
1746 goto out_free_server;
1747
Chuck Leverfca52382010-12-24 01:32:32 +00001748 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001749 server->mount_time = jiffies;
1750
Trond Myklebustfbca7792010-04-16 16:22:46 -04001751 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001752 dprintk("<-- nfs_clone_server() = %p\n", server);
1753 return server;
1754
1755out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001756 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001757 nfs_free_server(server);
1758 dprintk("<-- nfs_clone_server() = error %d\n", error);
1759 return ERR_PTR(error);
1760}
David Howells6aaca562006-08-22 20:06:13 -04001761
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001762void nfs_clients_init(struct net *net)
1763{
1764 struct nfs_net *nn = net_generic(net, nfs_net_id);
1765
1766 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001767 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001768}
1769
David Howells6aaca562006-08-22 20:06:13 -04001770#ifdef CONFIG_PROC_FS
1771static struct proc_dir_entry *proc_fs_nfs;
1772
1773static int nfs_server_list_open(struct inode *inode, struct file *file);
1774static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1775static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1776static void nfs_server_list_stop(struct seq_file *p, void *v);
1777static int nfs_server_list_show(struct seq_file *m, void *v);
1778
James Morris88e9d342009-09-22 16:43:43 -07001779static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001780 .start = nfs_server_list_start,
1781 .next = nfs_server_list_next,
1782 .stop = nfs_server_list_stop,
1783 .show = nfs_server_list_show,
1784};
1785
Arjan van de Ven00977a52007-02-12 00:55:34 -08001786static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001787 .open = nfs_server_list_open,
1788 .read = seq_read,
1789 .llseek = seq_lseek,
1790 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001791 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001792};
1793
1794static int nfs_volume_list_open(struct inode *inode, struct file *file);
1795static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1796static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1797static void nfs_volume_list_stop(struct seq_file *p, void *v);
1798static int nfs_volume_list_show(struct seq_file *m, void *v);
1799
James Morris88e9d342009-09-22 16:43:43 -07001800static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001801 .start = nfs_volume_list_start,
1802 .next = nfs_volume_list_next,
1803 .stop = nfs_volume_list_stop,
1804 .show = nfs_volume_list_show,
1805};
1806
Arjan van de Ven00977a52007-02-12 00:55:34 -08001807static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001808 .open = nfs_volume_list_open,
1809 .read = seq_read,
1810 .llseek = seq_lseek,
1811 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001812 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001813};
1814
1815/*
1816 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1817 * we're dealing
1818 */
1819static int nfs_server_list_open(struct inode *inode, struct file *file)
1820{
1821 struct seq_file *m;
1822 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001823 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1824 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001825
1826 ret = seq_open(file, &nfs_server_list_ops);
1827 if (ret < 0)
1828 return ret;
1829
1830 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001831 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001832
1833 return 0;
1834}
1835
1836/*
1837 * set up the iterator to start reading from the server list and return the first item
1838 */
1839static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1840{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001841 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1842
David Howells6aaca562006-08-22 20:06:13 -04001843 /* lock the list against modification */
1844 spin_lock(&nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001845 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001846}
1847
1848/*
1849 * move to next server
1850 */
1851static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1852{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001853 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1854
1855 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001856}
1857
1858/*
1859 * clean up after reading from the transports list
1860 */
1861static void nfs_server_list_stop(struct seq_file *p, void *v)
1862{
1863 spin_unlock(&nfs_client_lock);
1864}
1865
1866/*
1867 * display a header line followed by a load of call lines
1868 */
1869static int nfs_server_list_show(struct seq_file *m, void *v)
1870{
1871 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001872 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001873
1874 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001875 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001876 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1877 return 0;
1878 }
1879
1880 /* display one transport per line on subsequent lines */
1881 clp = list_entry(v, struct nfs_client, cl_share_link);
1882
Malahal Naineni940aab42011-09-20 17:27:14 -07001883 /* Check if the client is initialized */
1884 if (clp->cl_cons_state != NFS_CS_READY)
1885 return 0;
1886
Chuck Lever5d8515c2007-12-10 14:57:16 -05001887 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001888 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001889 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1890 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001891 atomic_read(&clp->cl_count),
1892 clp->cl_hostname);
1893
1894 return 0;
1895}
1896
1897/*
1898 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1899 */
1900static int nfs_volume_list_open(struct inode *inode, struct file *file)
1901{
1902 struct seq_file *m;
1903 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001904 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1905 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001906
1907 ret = seq_open(file, &nfs_volume_list_ops);
1908 if (ret < 0)
1909 return ret;
1910
1911 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001912 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001913
1914 return 0;
1915}
1916
1917/*
1918 * set up the iterator to start reading from the volume list and return the first item
1919 */
1920static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1921{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001922 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1923
David Howells6aaca562006-08-22 20:06:13 -04001924 /* lock the list against modification */
1925 spin_lock(&nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001926 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001927}
1928
1929/*
1930 * move to next volume
1931 */
1932static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1933{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001934 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1935
1936 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001937}
1938
1939/*
1940 * clean up after reading from the transports list
1941 */
1942static void nfs_volume_list_stop(struct seq_file *p, void *v)
1943{
1944 spin_unlock(&nfs_client_lock);
1945}
1946
1947/*
1948 * display a header line followed by a load of call lines
1949 */
1950static int nfs_volume_list_show(struct seq_file *m, void *v)
1951{
1952 struct nfs_server *server;
1953 struct nfs_client *clp;
1954 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001955 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001956
1957 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001958 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001959 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001960 return 0;
1961 }
1962 /* display one transport per line on subsequent lines */
1963 server = list_entry(v, struct nfs_server, master_link);
1964 clp = server->nfs_client;
1965
1966 snprintf(dev, 8, "%u:%u",
1967 MAJOR(server->s_dev), MINOR(server->s_dev));
1968
1969 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001970 (unsigned long long) server->fsid.major,
1971 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001972
David Howells5d1acff2009-04-03 16:42:47 +01001973 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001974 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001975 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1976 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001977 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001978 fsid,
1979 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001980
1981 return 0;
1982}
1983
1984/*
1985 * initialise the /proc/fs/nfsfs/ directory
1986 */
1987int __init nfs_fs_proc_init(void)
1988{
1989 struct proc_dir_entry *p;
1990
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001991 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001992 if (!proc_fs_nfs)
1993 goto error_0;
1994
David Howells6aaca562006-08-22 20:06:13 -04001995 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001996 p = proc_create("servers", S_IFREG|S_IRUGO,
1997 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001998 if (!p)
1999 goto error_1;
2000
David Howells6aaca562006-08-22 20:06:13 -04002001 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07002002 p = proc_create("volumes", S_IFREG|S_IRUGO,
2003 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04002004 if (!p)
2005 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04002006 return 0;
2007
2008error_2:
2009 remove_proc_entry("servers", proc_fs_nfs);
2010error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002011 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002012error_0:
2013 return -ENOMEM;
2014}
2015
2016/*
2017 * clean up the /proc/fs/nfsfs/ directory
2018 */
2019void nfs_fs_proc_exit(void)
2020{
2021 remove_proc_entry("volumes", proc_fs_nfs);
2022 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002023 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002024}
2025
2026#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002027
2028module_param(nfs4_disable_idmapping, bool, 0644);
2029MODULE_PARM_DESC(nfs4_disable_idmapping,
2030 "Turn off NFSv4 idmapping when using 'sec=sys'");