blob: d4f772ebd1efd7f86d4da5f9c83f96da8a76c1bf [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>
David Howells24c8dbb2006-08-22 20:06:10 -040043
44#include <asm/system.h>
45
46#include "nfs4_fs.h"
47#include "callback.h"
48#include "delegation.h"
49#include "iostat.h"
50#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010051#include "fscache.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040052#include "pnfs.h"
David Howells24c8dbb2006-08-22 20:06:10 -040053
54#define NFSDBG_FACILITY NFSDBG_CLIENT
55
56static DEFINE_SPINLOCK(nfs_client_lock);
57static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040058static LIST_HEAD(nfs_volume_list);
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 */
93static struct rpc_version *nfs_version[5] = {
94 [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
103struct rpc_program nfs_program = {
104 .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 };
119static struct rpc_version * nfsacl_version[] = {
120 [3] = &nfsacl_version3,
121};
122
123struct rpc_program nfsacl_program = {
124 .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;
Trond Myklebust3a498022007-12-14 14:56:04 -0500139};
140
David Howells5006a762006-08-22 20:06:12 -0400141/*
David Howells24c8dbb2006-08-22 20:06:10 -0400142 * Allocate a shared client record
143 *
144 * Since these are allocated/deallocated very rarely, we don't
145 * bother putting them in a slab cache...
146 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500147static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400148{
149 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400150 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700151 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400152
153 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
154 goto error_0;
155
Trond Myklebust40c553192007-12-14 14:56:07 -0500156 clp->rpc_ops = cl_init->rpc_ops;
157
David Howells24c8dbb2006-08-22 20:06:10 -0400158 atomic_set(&clp->cl_count, 1);
159 clp->cl_cons_state = NFS_CS_INITING;
160
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500161 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
162 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400163
Trond Myklebust3a498022007-12-14 14:56:04 -0500164 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700165 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500166 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400167 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400168 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400169 }
170
171 INIT_LIST_HEAD(&clp->cl_superblocks);
172 clp->cl_rpcclient = ERR_PTR(-EINVAL);
173
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500174 clp->cl_proto = cl_init->proto;
175
David Howells24c8dbb2006-08-22 20:06:10 -0400176#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000177 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
178 if (err)
179 goto error_cleanup;
180
David Howells24c8dbb2006-08-22 20:06:10 -0400181 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000182 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400183 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
184 clp->cl_boot_time = CURRENT_TIME;
185 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400186 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400187 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400188#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500189 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400190 if (!IS_ERR(cred))
191 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100192 nfs_fscache_get_client_cookie(clp);
193
David Howells24c8dbb2006-08-22 20:06:10 -0400194 return clp;
195
Benny Halevy71468512009-04-01 09:22:56 -0400196error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400197 kfree(clp);
198error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700199 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400200}
201
Trond Myklebust5dd31772006-08-24 01:03:05 -0400202#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400203#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000204static void nfs4_shutdown_session(struct nfs_client *clp)
205{
206 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400207 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400208}
Andy Adamsonea005282011-01-06 02:04:29 +0000209#else /* CONFIG_NFS_V4_1 */
210static void nfs4_shutdown_session(struct nfs_client *clp)
211{
212}
213#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400214
215/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800216 * Destroy the NFS4 callback service
217 */
218static void nfs4_destroy_callback(struct nfs_client *clp)
219{
220 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400221 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800222}
223
224static void nfs4_shutdown_client(struct nfs_client *clp)
225{
226 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
227 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000228 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800229 nfs4_destroy_callback(clp);
230 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
231 nfs_idmap_delete(clp);
232
233 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
234}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000235
236/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
237void nfs_cleanup_cb_ident_idr(void)
238{
239 idr_destroy(&cb_ident_idr);
240}
241
242/* nfs_client_lock held */
243static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
244{
245 if (clp->cl_cb_ident)
246 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
247}
248
Fred Isamanf7e89172011-01-06 11:36:32 +0000249static void pnfs_init_server(struct nfs_server *server)
250{
251 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
252}
253
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500254static void nfs4_destroy_server(struct nfs_server *server)
255{
256 nfs4_purge_state_owners(server);
257}
258
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800259#else
260static void nfs4_shutdown_client(struct nfs_client *clp)
261{
262}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000263
264void nfs_cleanup_cb_ident_idr(void)
265{
266}
267
268static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
269{
270}
Fred Isamanf7e89172011-01-06 11:36:32 +0000271
272static void pnfs_init_server(struct nfs_server *server)
273{
274}
275
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800276#endif /* CONFIG_NFS_V4 */
277
278/*
David Howells24c8dbb2006-08-22 20:06:10 -0400279 * Destroy a shared client record
280 */
281static void nfs_free_client(struct nfs_client *clp)
282{
Trond Myklebust40c553192007-12-14 14:56:07 -0500283 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400284
Trond Myklebust5dd31772006-08-24 01:03:05 -0400285 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400286
David Howells14727282009-04-03 16:42:42 +0100287 nfs_fscache_release_client_cookie(clp);
288
David Howells24c8dbb2006-08-22 20:06:10 -0400289 /* -EIO all pending I/O */
290 if (!IS_ERR(clp->cl_rpcclient))
291 rpc_shutdown_client(clp->cl_rpcclient);
292
Trond Myklebust7c67db32008-04-07 20:50:11 -0400293 if (clp->cl_machine_cred != NULL)
294 put_rpccred(clp->cl_machine_cred);
295
Benny Halevy1775bc32011-05-20 13:47:33 +0200296 nfs4_deviceid_purge_client(clp);
297
David Howells24c8dbb2006-08-22 20:06:10 -0400298 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400299 kfree(clp->server_scope);
David Howells24c8dbb2006-08-22 20:06:10 -0400300 kfree(clp);
301
302 dprintk("<-- nfs_free_client()\n");
303}
304
305/*
306 * Release a reference to a shared client record
307 */
308void nfs_put_client(struct nfs_client *clp)
309{
David Howells27ba8512006-07-30 14:40:56 -0400310 if (!clp)
311 return;
312
David Howells24c8dbb2006-08-22 20:06:10 -0400313 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
314
315 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
316 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000317 nfs_cb_idr_remove_locked(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400318 spin_unlock(&nfs_client_lock);
319
320 BUG_ON(!list_empty(&clp->cl_superblocks));
321
322 nfs_free_client(clp);
323 }
324}
Andy Adamson16b374c2010-10-20 00:18:04 -0400325EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400326
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500327#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400328/*
329 * Test if two ip6 socket addresses refer to the same socket by
330 * comparing relevant fields. The padding bytes specifically, are not
331 * compared. sin6_flowinfo is not compared because it only affects QoS
332 * and sin6_scope_id is only compared if the address is "link local"
333 * because "link local" addresses need only be unique to a specific
334 * link. Conversely, ordinary unicast addresses might have different
335 * sin6_scope_id.
336 *
337 * The caller should ensure both socket addresses are AF_INET6.
338 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400339static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
340 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400341{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400342 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
343 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400344
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800345 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400346 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800347 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
348 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500349
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800350 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500351}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400352#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
353static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
354 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400355{
356 return 0;
357}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500358#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500359
David Howells24c8dbb2006-08-22 20:06:10 -0400360/*
Ian Dalld7371c42009-03-10 20:33:22 -0400361 * Test if two ip4 socket addresses refer to the same socket, by
362 * comparing relevant fields. The padding bytes specifically, are
363 * not compared.
364 *
365 * The caller should ensure both socket addresses are AF_INET.
366 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400367static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
368 const struct sockaddr *sa2)
369{
370 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
371 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
372
373 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
374}
375
376static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
377 const struct sockaddr *sa2)
378{
379 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
380 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
381
382 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
383 (sin1->sin6_port == sin2->sin6_port);
384}
385
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400386static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
387 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400388{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400389 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
390 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400391
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400392 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
393 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400394}
395
396/*
Ian Dalld7371c42009-03-10 20:33:22 -0400397 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400398 * by comparing (only) relevant fields, excluding the port number.
399 */
400static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
401 const struct sockaddr *sa2)
402{
403 if (sa1->sa_family != sa2->sa_family)
404 return 0;
405
406 switch (sa1->sa_family) {
407 case AF_INET:
408 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
409 case AF_INET6:
410 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
411 }
412 return 0;
413}
414
415/*
416 * Test if two socket addresses represent the same actual socket,
417 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400418 */
419static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
420 const struct sockaddr *sa2)
421{
422 if (sa1->sa_family != sa2->sa_family)
423 return 0;
424
425 switch (sa1->sa_family) {
426 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400427 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400428 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400429 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400430 }
431 return 0;
432}
433
Andy Adamsonc36fca52011-01-06 02:04:32 +0000434/* Common match routine for v4.0 and v4.1 callback services */
435bool
436nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
437 u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500438{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000439 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500440
Andy Adamsonc36fca52011-01-06 02:04:32 +0000441 /* Don't match clients that failed to initialise */
442 if (!(clp->cl_cons_state == NFS_CS_READY ||
443 clp->cl_cons_state == NFS_CS_SESSION_INITING))
444 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500445
Andy Adamsonc36fca52011-01-06 02:04:32 +0000446 /* Match the version and minorversion */
447 if (clp->rpc_ops->version != 4 ||
448 clp->cl_minorversion != minorversion)
449 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500450
Andy Adamsonc36fca52011-01-06 02:04:32 +0000451 /* Match only the IP address, not the port number */
452 if (!nfs_sockaddr_match_ipaddr(addr, clap))
453 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500454
Andy Adamsonc36fca52011-01-06 02:04:32 +0000455 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500456}
457
458/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500459 * Find an nfs_client on the list that matches the initialisation data
460 * that is supplied.
461 */
462static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400463{
464 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400465 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400466
467 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400468 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700469 /* Don't match clients that failed to initialise properly */
470 if (clp->cl_cons_state < 0)
471 continue;
472
David Howells24c8dbb2006-08-22 20:06:10 -0400473 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500474 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400475 continue;
476
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500477 if (clp->cl_proto != data->proto)
478 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400479 /* Match nfsv4 minorversion */
480 if (clp->cl_minorversion != data->minorversion)
481 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500482 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400483 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400484 continue;
485
Trond Myklebustc81468a2007-12-14 14:56:05 -0500486 atomic_inc(&clp->cl_count);
487 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400488 }
David Howells24c8dbb2006-08-22 20:06:10 -0400489 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400490}
491
492/*
493 * Look up a client by IP address and protocol version
494 * - creates a new record if one doesn't yet exist
495 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000496static struct nfs_client *
497nfs_get_client(const struct nfs_client_initdata *cl_init,
498 const struct rpc_timeout *timeparms,
499 const char *ip_addr,
500 rpc_authflavor_t authflavour,
501 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400502{
503 struct nfs_client *clp, *new = NULL;
504 int error;
505
Chuck Leverd7422c42007-12-10 14:58:51 -0500506 dprintk("--> nfs_get_client(%s,v%u)\n",
507 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400508
509 /* see if the client already exists */
510 do {
511 spin_lock(&nfs_client_lock);
512
Trond Myklebustc81468a2007-12-14 14:56:05 -0500513 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400514 if (clp)
515 goto found_client;
516 if (new)
517 goto install_client;
518
519 spin_unlock(&nfs_client_lock);
520
Trond Myklebust3a498022007-12-14 14:56:04 -0500521 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700522 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400523
Chuck Levera21bdd92009-06-17 18:02:10 -0700524 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
525 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400526
527 /* install a new client and return with it unready */
528install_client:
529 clp = new;
530 list_add(&clp->cl_share_link, &nfs_client_list);
531 spin_unlock(&nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000532
533 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
534 authflavour, noresvport);
535 if (error < 0) {
536 nfs_put_client(clp);
537 return ERR_PTR(error);
538 }
David Howells24c8dbb2006-08-22 20:06:10 -0400539 dprintk("--> nfs_get_client() = %p [new]\n", clp);
540 return clp;
541
542 /* found an existing client
543 * - make sure it's ready before returning
544 */
545found_client:
546 spin_unlock(&nfs_client_lock);
547
548 if (new)
549 nfs_free_client(new);
550
Matthew Wilcox150030b2007-12-06 16:24:39 -0500551 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400552 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400553 if (error < 0) {
554 nfs_put_client(clp);
555 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400556 }
557
558 if (clp->cl_cons_state < NFS_CS_READY) {
559 error = clp->cl_cons_state;
560 nfs_put_client(clp);
561 return ERR_PTR(error);
562 }
563
David Howells54ceac42006-08-22 20:06:13 -0400564 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
565
David Howells24c8dbb2006-08-22 20:06:10 -0400566 dprintk("--> nfs_get_client() = %p [share]\n", clp);
567 return clp;
568}
569
570/*
571 * Mark a server as ready or failed
572 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400573void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400574{
575 clp->cl_cons_state = state;
576 wake_up_all(&nfs_client_active_wq);
577}
David Howells5006a762006-08-22 20:06:12 -0400578
579/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400580 * With sessions, the client is not marked ready until after a
581 * successful EXCHANGE_ID and CREATE_SESSION.
582 *
583 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
584 * other versions of NFS can be tried.
585 */
586int nfs4_check_client_ready(struct nfs_client *clp)
587{
588 if (!nfs4_has_session(clp))
589 return 0;
590 if (clp->cl_cons_state < NFS_CS_READY)
591 return -EPROTONOSUPPORT;
592 return 0;
593}
594
595/*
David Howells5006a762006-08-22 20:06:12 -0400596 * Initialise the timeout values for a connection
597 */
598static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
599 unsigned int timeo, unsigned int retrans)
600{
601 to->to_initval = timeo * HZ / 10;
602 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400603
604 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400605 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400606 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400607 if (to->to_retries == 0)
608 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500609 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400610 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400611 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
612 to->to_initval = NFS_MAX_TCP_TIMEOUT;
613 to->to_increment = to->to_initval;
614 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500615 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
616 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
617 if (to->to_maxval < to->to_initval)
618 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400619 to->to_exponential = 0;
620 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400621 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400622 if (to->to_retries == 0)
623 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400624 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400625 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400626 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
627 to->to_initval = NFS_MAX_UDP_TIMEOUT;
628 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
629 to->to_exponential = 1;
630 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400631 default:
632 BUG();
David Howells5006a762006-08-22 20:06:12 -0400633 }
634}
635
636/*
637 * Create an RPC client handle
638 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500639static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500640 const struct rpc_timeout *timeparms,
641 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500642 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400643{
David Howells5006a762006-08-22 20:06:12 -0400644 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400645 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400646 .net = &init_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500647 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400648 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500649 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500650 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400651 .servername = clp->cl_hostname,
652 .program = &nfs_program,
653 .version = clp->rpc_ops->version,
654 .authflavor = flavor,
655 };
David Howells5006a762006-08-22 20:06:12 -0400656
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500657 if (discrtry)
658 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
659 if (noresvport)
660 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
661
David Howells5006a762006-08-22 20:06:12 -0400662 if (!IS_ERR(clp->cl_rpcclient))
663 return 0;
664
Chuck Lever41877d22006-08-22 20:06:20 -0400665 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400666 if (IS_ERR(clnt)) {
667 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700668 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400669 return PTR_ERR(clnt);
670 }
671
David Howells5006a762006-08-22 20:06:12 -0400672 clp->cl_rpcclient = clnt;
673 return 0;
674}
David Howells54ceac42006-08-22 20:06:13 -0400675
676/*
677 * Version 2 or 3 client destruction
678 */
679static void nfs_destroy_server(struct nfs_server *server)
680{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400681 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
682 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500683 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400684}
685
686/*
687 * Version 2 or 3 lockd setup
688 */
689static int nfs_start_lockd(struct nfs_server *server)
690{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500691 struct nlm_host *host;
692 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500693 struct nlmclnt_initdata nlm_init = {
694 .hostname = clp->cl_hostname,
695 .address = (struct sockaddr *)&clp->cl_addr,
696 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500697 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500698 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
699 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500700 };
David Howells54ceac42006-08-22 20:06:13 -0400701
Chuck Lever883bb162008-01-15 16:04:20 -0500702 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500703 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400704 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
705 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500706 return 0;
707
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400708 switch (clp->cl_proto) {
709 default:
710 nlm_init.protocol = IPPROTO_TCP;
711 break;
712 case XPRT_TRANSPORT_UDP:
713 nlm_init.protocol = IPPROTO_UDP;
714 }
715
Chuck Lever883bb162008-01-15 16:04:20 -0500716 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500717 if (IS_ERR(host))
718 return PTR_ERR(host);
719
720 server->nlm_host = host;
721 server->destroy = nfs_destroy_server;
722 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400723}
724
725/*
726 * Initialise an NFSv3 ACL client connection
727 */
728#ifdef CONFIG_NFS_V3_ACL
729static void nfs_init_server_aclclient(struct nfs_server *server)
730{
Trond Myklebust40c553192007-12-14 14:56:07 -0500731 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400732 goto out_noacl;
733 if (server->flags & NFS_MOUNT_NOACL)
734 goto out_noacl;
735
736 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
737 if (IS_ERR(server->client_acl))
738 goto out_noacl;
739
740 /* No errors! Assume that Sun nfsacls are supported */
741 server->caps |= NFS_CAP_ACLS;
742 return;
743
744out_noacl:
745 server->caps &= ~NFS_CAP_ACLS;
746}
747#else
748static inline void nfs_init_server_aclclient(struct nfs_server *server)
749{
750 server->flags &= ~NFS_MOUNT_NOACL;
751 server->caps &= ~NFS_CAP_ACLS;
752}
753#endif
754
755/*
756 * Create a general RPC client
757 */
Trond Myklebust33170232007-12-20 16:03:59 -0500758static int nfs_init_server_rpcclient(struct nfs_server *server,
759 const struct rpc_timeout *timeo,
760 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400761{
762 struct nfs_client *clp = server->nfs_client;
763
764 server->client = rpc_clone_client(clp->cl_rpcclient);
765 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700766 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400767 return PTR_ERR(server->client);
768 }
769
Trond Myklebust33170232007-12-20 16:03:59 -0500770 memcpy(&server->client->cl_timeout_default,
771 timeo,
772 sizeof(server->client->cl_timeout_default));
773 server->client->cl_timeout = &server->client->cl_timeout_default;
774
David Howells54ceac42006-08-22 20:06:13 -0400775 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
776 struct rpc_auth *auth;
777
778 auth = rpcauth_create(pseudoflavour, server->client);
779 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700780 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400781 return PTR_ERR(auth);
782 }
783 }
784 server->client->cl_softrtry = 0;
785 if (server->flags & NFS_MOUNT_SOFT)
786 server->client->cl_softrtry = 1;
787
David Howells54ceac42006-08-22 20:06:13 -0400788 return 0;
789}
790
791/*
792 * Initialise an NFS2 or NFS3 client
793 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000794int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
795 const char *ip_addr, rpc_authflavor_t authflavour,
796 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400797{
David Howells54ceac42006-08-22 20:06:13 -0400798 int error;
799
800 if (clp->cl_cons_state == NFS_CS_READY) {
801 /* the client is already initialised */
802 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
803 return 0;
804 }
805
David Howells54ceac42006-08-22 20:06:13 -0400806 /*
807 * Create a client RPC handle for doing FSSTAT with UNIX auth only
808 * - RFC 2623, sec 2.3.2
809 */
Chuck Leverd7403512008-12-23 15:21:37 -0500810 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000811 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400812 if (error < 0)
813 goto error;
814 nfs_mark_client_ready(clp, NFS_CS_READY);
815 return 0;
816
817error:
818 nfs_mark_client_ready(clp, error);
819 dprintk("<-- nfs_init_client() = xerror %d\n", error);
820 return error;
821}
822
823/*
824 * Create a version 2 or 3 client
825 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400826static int nfs_init_server(struct nfs_server *server,
827 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400828{
Trond Myklebust3a498022007-12-14 14:56:04 -0500829 struct nfs_client_initdata cl_init = {
830 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500831 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500832 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500833 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500834 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500835 };
Trond Myklebust33170232007-12-20 16:03:59 -0500836 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400837 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500838 int error;
David Howells54ceac42006-08-22 20:06:13 -0400839
840 dprintk("--> nfs_init_server()\n");
841
842#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400843 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500844 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400845#endif
846
Andy Adamson45a52a02011-03-01 01:34:08 +0000847 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
848 data->timeo, data->retrans);
849
David Howells54ceac42006-08-22 20:06:13 -0400850 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000851 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
852 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400853 if (IS_ERR(clp)) {
854 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
855 return PTR_ERR(clp);
856 }
857
David Howells54ceac42006-08-22 20:06:13 -0400858 server->nfs_client = clp;
859
860 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400861 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100862 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400863 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
864 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
865 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400866
867 if (data->rsize)
868 server->rsize = nfs_block_size(data->rsize, NULL);
869 if (data->wsize)
870 server->wsize = nfs_block_size(data->wsize, NULL);
871
872 server->acregmin = data->acregmin * HZ;
873 server->acregmax = data->acregmax * HZ;
874 server->acdirmin = data->acdirmin * HZ;
875 server->acdirmax = data->acdirmax * HZ;
876
877 /* Start lockd here, before we might error out */
878 error = nfs_start_lockd(server);
879 if (error < 0)
880 goto error;
881
Chuck Leverf22d6d72008-03-14 14:10:22 -0400882 server->port = data->nfs_server.port;
883
Trond Myklebust33170232007-12-20 16:03:59 -0500884 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400885 if (error < 0)
886 goto error;
887
Chuck Lever3f8400d2008-03-14 14:10:30 -0400888 /* Preserve the values of mount_server-related mount options */
889 if (data->mount_server.addrlen) {
890 memcpy(&server->mountd_address, &data->mount_server.address,
891 data->mount_server.addrlen);
892 server->mountd_addrlen = data->mount_server.addrlen;
893 }
894 server->mountd_version = data->mount_server.version;
895 server->mountd_port = data->mount_server.port;
896 server->mountd_protocol = data->mount_server.protocol;
897
David Howells54ceac42006-08-22 20:06:13 -0400898 server->namelen = data->namlen;
899 /* Create a client RPC handle for the NFSv3 ACL management interface */
900 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400901 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
902 return 0;
903
904error:
905 server->nfs_client = NULL;
906 nfs_put_client(clp);
907 dprintk("<-- nfs_init_server() = xerror %d\n", error);
908 return error;
909}
910
911/*
912 * Load up the server record from information gained in an fsinfo record
913 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400914static void nfs_server_set_fsinfo(struct nfs_server *server,
915 struct nfs_fh *mntfh,
916 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400917{
918 unsigned long max_rpc_payload;
919
920 /* Work out a lot of parameters */
921 if (server->rsize == 0)
922 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
923 if (server->wsize == 0)
924 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
925
926 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
927 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
928 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
929 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
930
931 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
932 if (server->rsize > max_rpc_payload)
933 server->rsize = max_rpc_payload;
934 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
935 server->rsize = NFS_MAX_FILE_IO_SIZE;
936 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700937
Jens Axboed9938312009-06-12 14:45:52 +0200938 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400939 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
940
941 if (server->wsize > max_rpc_payload)
942 server->wsize = max_rpc_payload;
943 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
944 server->wsize = NFS_MAX_FILE_IO_SIZE;
945 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400946 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400947 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400948
David Howells54ceac42006-08-22 20:06:13 -0400949 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
950
951 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400952 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
953 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400954 if (server->dtsize > server->rsize)
955 server->dtsize = server->rsize;
956
957 if (server->flags & NFS_MOUNT_NOAC) {
958 server->acregmin = server->acregmax = 0;
959 server->acdirmin = server->acdirmax = 0;
960 }
961
962 server->maxfilesize = fsinfo->maxfilesize;
963
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700964 server->time_delta = fsinfo->time_delta;
965
David Howells54ceac42006-08-22 20:06:13 -0400966 /* We're airborne Set socket buffersize */
967 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
968}
969
970/*
971 * Probe filesystem information, including the FSID on v2/v3
972 */
973static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
974{
975 struct nfs_fsinfo fsinfo;
976 struct nfs_client *clp = server->nfs_client;
977 int error;
978
979 dprintk("--> nfs_probe_fsinfo()\n");
980
981 if (clp->rpc_ops->set_capabilities != NULL) {
982 error = clp->rpc_ops->set_capabilities(server, mntfh);
983 if (error < 0)
984 goto out_error;
985 }
986
987 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400988 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400989 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
990 if (error < 0)
991 goto out_error;
992
Benny Halevy738fd0f32011-07-30 20:52:36 -0400993 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -0400994
995 /* Get some general file system info */
996 if (server->namelen == 0) {
997 struct nfs_pathconf pathinfo;
998
999 pathinfo.fattr = fattr;
1000 nfs_fattr_init(fattr);
1001
1002 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1003 server->namelen = pathinfo.max_namelen;
1004 }
1005
1006 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1007 return 0;
1008
1009out_error:
1010 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1011 return error;
1012}
1013
1014/*
1015 * Copy useful information when duplicating a server record
1016 */
1017static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1018{
1019 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001020 target->rsize = source->rsize;
1021 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001022 target->acregmin = source->acregmin;
1023 target->acregmax = source->acregmax;
1024 target->acdirmin = source->acdirmin;
1025 target->acdirmax = source->acdirmax;
1026 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001027 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001028}
1029
Chuck Leverfca52382010-12-24 01:32:32 +00001030static void nfs_server_insert_lists(struct nfs_server *server)
1031{
1032 struct nfs_client *clp = server->nfs_client;
1033
1034 spin_lock(&nfs_client_lock);
1035 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1036 list_add_tail(&server->master_link, &nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001037 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001038 spin_unlock(&nfs_client_lock);
1039
1040}
1041
1042static void nfs_server_remove_lists(struct nfs_server *server)
1043{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001044 struct nfs_client *clp = server->nfs_client;
1045
Chuck Leverfca52382010-12-24 01:32:32 +00001046 spin_lock(&nfs_client_lock);
1047 list_del_rcu(&server->client_link);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001048 if (clp && list_empty(&clp->cl_superblocks))
1049 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001050 list_del(&server->master_link);
1051 spin_unlock(&nfs_client_lock);
1052
1053 synchronize_rcu();
1054}
1055
David Howells54ceac42006-08-22 20:06:13 -04001056/*
1057 * Allocate and initialise a server record
1058 */
1059static struct nfs_server *nfs_alloc_server(void)
1060{
1061 struct nfs_server *server;
1062
1063 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1064 if (!server)
1065 return NULL;
1066
1067 server->client = server->client_acl = ERR_PTR(-EINVAL);
1068
1069 /* Zero out the NFS state stuff */
1070 INIT_LIST_HEAD(&server->client_link);
1071 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001072 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001073 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001074 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001075
Steve Dicksonef818a22007-11-08 04:05:04 -05001076 atomic_set(&server->active, 0);
1077
David Howells54ceac42006-08-22 20:06:13 -04001078 server->io_stats = nfs_alloc_iostats();
1079 if (!server->io_stats) {
1080 kfree(server);
1081 return NULL;
1082 }
1083
Jens Axboe48d07642009-09-21 09:59:39 +02001084 if (bdi_init(&server->backing_dev_info)) {
1085 nfs_free_iostats(server->io_stats);
1086 kfree(server);
1087 return NULL;
1088 }
1089
Fred Isamanf7e89172011-01-06 11:36:32 +00001090 pnfs_init_server(server);
1091
David Howells54ceac42006-08-22 20:06:13 -04001092 return server;
1093}
1094
1095/*
1096 * Free up a server record
1097 */
1098void nfs_free_server(struct nfs_server *server)
1099{
1100 dprintk("--> nfs_free_server()\n");
1101
Chuck Leverfca52382010-12-24 01:32:32 +00001102 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001103 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001104
1105 if (server->destroy != NULL)
1106 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001107
1108 if (!IS_ERR(server->client_acl))
1109 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001110 if (!IS_ERR(server->client))
1111 rpc_shutdown_client(server->client);
1112
1113 nfs_put_client(server->nfs_client);
1114
1115 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001116 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001117 kfree(server);
1118 nfs_release_automount_timer();
1119 dprintk("<-- nfs_free_server()\n");
1120}
1121
1122/*
1123 * Create a version 2 or 3 volume record
1124 * - keyed on server and FSID
1125 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001126struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001127 struct nfs_fh *mntfh)
1128{
1129 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001130 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001131 int error;
1132
1133 server = nfs_alloc_server();
1134 if (!server)
1135 return ERR_PTR(-ENOMEM);
1136
Trond Myklebustfbca7792010-04-16 16:22:46 -04001137 error = -ENOMEM;
1138 fattr = nfs_alloc_fattr();
1139 if (fattr == NULL)
1140 goto error;
1141
David Howells54ceac42006-08-22 20:06:13 -04001142 /* Get a client representation */
1143 error = nfs_init_server(server, data);
1144 if (error < 0)
1145 goto error;
1146
1147 BUG_ON(!server->nfs_client);
1148 BUG_ON(!server->nfs_client->rpc_ops);
1149 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1150
1151 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001152 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001153 if (error < 0)
1154 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001155 if (server->nfs_client->rpc_ops->version == 3) {
1156 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1157 server->namelen = NFS3_MAXNAMLEN;
1158 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1159 server->caps |= NFS_CAP_READDIRPLUS;
1160 } else {
1161 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1162 server->namelen = NFS2_MAXNAMLEN;
1163 }
1164
Trond Myklebustfbca7792010-04-16 16:22:46 -04001165 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1166 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001167 if (error < 0) {
1168 dprintk("nfs_create_server: getattr error = %d\n", -error);
1169 goto error;
1170 }
1171 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001172 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001173
David Howells6daabf12006-08-24 15:44:16 -04001174 dprintk("Server FSID: %llx:%llx\n",
1175 (unsigned long long) server->fsid.major,
1176 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001177
Chuck Leverfca52382010-12-24 01:32:32 +00001178 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001179 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001180 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001181 return server;
1182
1183error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001184 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001185 nfs_free_server(server);
1186 return ERR_PTR(error);
1187}
1188
1189#ifdef CONFIG_NFS_V4
1190/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001191 * NFSv4.0 callback thread helper
1192 *
1193 * Find a client by IP address, protocol version, and minorversion
1194 *
1195 * Called from the pg_authenticate method. The callback identifier
1196 * is not used as it has not been decoded.
1197 *
1198 * Returns NULL if no such client
1199 */
1200struct nfs_client *
1201nfs4_find_client_no_ident(const struct sockaddr *addr)
1202{
1203 struct nfs_client *clp;
1204
1205 spin_lock(&nfs_client_lock);
1206 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1207 if (nfs4_cb_match_client(addr, clp, 0) == false)
1208 continue;
1209 atomic_inc(&clp->cl_count);
1210 spin_unlock(&nfs_client_lock);
1211 return clp;
1212 }
1213 spin_unlock(&nfs_client_lock);
1214 return NULL;
1215}
1216
1217/*
1218 * NFSv4.0 callback thread helper
1219 *
1220 * Find a client by callback identifier
1221 */
1222struct nfs_client *
1223nfs4_find_client_ident(int cb_ident)
1224{
1225 struct nfs_client *clp;
1226
1227 spin_lock(&nfs_client_lock);
1228 clp = idr_find(&cb_ident_idr, cb_ident);
1229 if (clp)
1230 atomic_inc(&clp->cl_count);
1231 spin_unlock(&nfs_client_lock);
1232 return clp;
1233}
1234
1235#if defined(CONFIG_NFS_V4_1)
1236/*
1237 * NFSv4.1 callback thread helper
1238 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1239 * minorversion, and sessionID
1240 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001241 * Returns NULL if no such client
1242 */
1243struct nfs_client *
1244nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001245 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001246{
1247 struct nfs_client *clp;
1248
1249 spin_lock(&nfs_client_lock);
1250 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1251 if (nfs4_cb_match_client(addr, clp, 1) == false)
1252 continue;
1253
1254 if (!nfs4_has_session(clp))
1255 continue;
1256
Andy Adamson778be232011-01-25 15:38:01 +00001257 /* Match sessionid*/
1258 if (memcmp(clp->cl_session->sess_id.data,
1259 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001260 continue;
1261
1262 atomic_inc(&clp->cl_count);
1263 spin_unlock(&nfs_client_lock);
1264 return clp;
1265 }
1266 spin_unlock(&nfs_client_lock);
1267 return NULL;
1268}
1269
1270#else /* CONFIG_NFS_V4_1 */
1271
1272struct nfs_client *
1273nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001274 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001275{
1276 return NULL;
1277}
1278#endif /* CONFIG_NFS_V4_1 */
1279
1280/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001281 * Initialize the NFS4 callback service
1282 */
1283static int nfs4_init_callback(struct nfs_client *clp)
1284{
1285 int error;
1286
1287 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001288 if (nfs4_has_session(clp)) {
1289 error = xprt_setup_backchannel(
1290 clp->cl_rpcclient->cl_xprt,
1291 NFS41_BC_MIN_CALLBACKS);
1292 if (error < 0)
1293 return error;
1294 }
1295
Trond Myklebust97dc1352010-06-16 09:52:26 -04001296 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001297 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001298 if (error < 0) {
1299 dprintk("%s: failed to start callback. Error = %d\n",
1300 __func__, error);
1301 return error;
1302 }
1303 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1304 }
1305 return 0;
1306}
1307
1308/*
Andy Adamson557134a2009-04-01 09:21:53 -04001309 * Initialize the minor version specific parts of an NFS4 client record
1310 */
1311static int nfs4_init_client_minor_version(struct nfs_client *clp)
1312{
1313#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001314 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001315 struct nfs4_session *session = NULL;
1316 /*
1317 * Create the session and mark it expired.
1318 * When a SEQUENCE operation encounters the expired session
1319 * it will do session recovery to initialize it.
1320 */
1321 session = nfs4_alloc_session(clp);
1322 if (!session)
1323 return -ENOMEM;
1324
1325 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001326 /*
1327 * The create session reply races with the server back
1328 * channel probe. Mark the client NFS_CS_SESSION_INITING
1329 * so that the client back channel can find the
1330 * nfs_client struct
1331 */
1332 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001333 }
1334#endif /* CONFIG_NFS_V4_1 */
1335
Benny Halevy71468512009-04-01 09:22:56 -04001336 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001337}
1338
1339/*
David Howells54ceac42006-08-22 20:06:13 -04001340 * Initialise an NFS4 client record
1341 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001342int nfs4_init_client(struct nfs_client *clp,
1343 const struct rpc_timeout *timeparms,
1344 const char *ip_addr,
1345 rpc_authflavor_t authflavour,
1346 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001347{
1348 int error;
1349
1350 if (clp->cl_cons_state == NFS_CS_READY) {
1351 /* the client is initialised already */
1352 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1353 return 0;
1354 }
1355
1356 /* Check NFS protocol revision and initialize RPC op vector */
1357 clp->rpc_ops = &nfs_v4_clientops;
1358
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001359 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001360 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001361 if (error < 0)
1362 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001363 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001364
1365 error = nfs_idmap_new(clp);
1366 if (error < 0) {
1367 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001368 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001369 goto error;
1370 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001371 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001372
Andy Adamson557134a2009-04-01 09:21:53 -04001373 error = nfs4_init_client_minor_version(clp);
1374 if (error < 0)
1375 goto error;
1376
Andy Adamson76db6d92009-04-01 09:22:38 -04001377 if (!nfs4_has_session(clp))
1378 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001379 return 0;
1380
1381error:
1382 nfs_mark_client_ready(clp, error);
1383 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1384 return error;
1385}
1386
1387/*
1388 * Set up an NFS4 client
1389 */
1390static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001391 const char *hostname,
1392 const struct sockaddr *addr,
1393 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001394 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001395 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001396 int proto, const struct rpc_timeout *timeparms,
1397 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001398{
Trond Myklebust3a498022007-12-14 14:56:04 -05001399 struct nfs_client_initdata cl_init = {
1400 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001401 .addr = addr,
1402 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001403 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001404 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001405 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001406 };
David Howells54ceac42006-08-22 20:06:13 -04001407 struct nfs_client *clp;
1408 int error;
1409
1410 dprintk("--> nfs4_set_client()\n");
1411
1412 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001413 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1414 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001415 if (IS_ERR(clp)) {
1416 error = PTR_ERR(clp);
1417 goto error;
1418 }
David Howells54ceac42006-08-22 20:06:13 -04001419
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001420 /*
1421 * Query for the lease time on clientid setup or renewal
1422 *
1423 * Note that this will be set on nfs_clients that were created
1424 * only for the DS role and did not set this bit, but now will
1425 * serve a dual role.
1426 */
1427 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1428
David Howells54ceac42006-08-22 20:06:13 -04001429 server->nfs_client = clp;
1430 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1431 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001432error:
1433 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1434 return error;
1435}
1436
Andy Adamsond83217c2011-03-01 01:34:17 +00001437/*
1438 * Set up a pNFS Data Server client.
1439 *
1440 * Return any existing nfs_client that matches server address,port,version
1441 * and minorversion.
1442 *
1443 * For a new nfs_client, use a soft mount (default), a low retrans and a
1444 * low timeout interval so that if a connection is lost, we retry through
1445 * the MDS.
1446 */
1447struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1448 const struct sockaddr *ds_addr,
1449 int ds_addrlen, int ds_proto)
1450{
1451 struct nfs_client_initdata cl_init = {
1452 .addr = ds_addr,
1453 .addrlen = ds_addrlen,
1454 .rpc_ops = &nfs_v4_clientops,
1455 .proto = ds_proto,
1456 .minorversion = mds_clp->cl_minorversion,
1457 };
1458 struct rpc_timeout ds_timeout = {
1459 .to_initval = 15 * HZ,
1460 .to_maxval = 15 * HZ,
1461 .to_retries = 1,
1462 .to_exponential = 1,
1463 };
1464 struct nfs_client *clp;
1465
1466 /*
1467 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1468 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1469 * (section 13.1 RFC 5661).
1470 */
1471 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1472 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1473
1474 dprintk("<-- %s %p\n", __func__, clp);
1475 return clp;
1476}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001477EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001478
1479/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001480 * Session has been established, and the client marked ready.
1481 * Set the mount rsize and wsize with negotiated fore channel
1482 * attributes which will be bound checked in nfs_server_set_fsinfo.
1483 */
1484static void nfs4_session_set_rwsize(struct nfs_server *server)
1485{
1486#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001487 struct nfs4_session *sess;
1488 u32 server_resp_sz;
1489 u32 server_rqst_sz;
1490
Andy Adamson96b09e02009-04-01 09:22:33 -04001491 if (!nfs4_has_session(server->nfs_client))
1492 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001493 sess = server->nfs_client->cl_session;
1494 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1495 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1496
1497 if (server->rsize > server_resp_sz)
1498 server->rsize = server_resp_sz;
1499 if (server->wsize > server_rqst_sz)
1500 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001501#endif /* CONFIG_NFS_V4_1 */
1502}
1503
Trond Myklebust44950b62010-06-17 11:45:12 -04001504static int nfs4_server_common_setup(struct nfs_server *server,
1505 struct nfs_fh *mntfh)
1506{
1507 struct nfs_fattr *fattr;
1508 int error;
1509
1510 BUG_ON(!server->nfs_client);
1511 BUG_ON(!server->nfs_client->rpc_ops);
1512 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1513
Andy Adamson94de8b22011-03-01 01:34:12 +00001514 /* data servers support only a subset of NFSv4.1 */
1515 if (is_ds_only_client(server->nfs_client))
1516 return -EPROTONOSUPPORT;
1517
Trond Myklebust44950b62010-06-17 11:45:12 -04001518 fattr = nfs_alloc_fattr();
1519 if (fattr == NULL)
1520 return -ENOMEM;
1521
1522 /* We must ensure the session is initialised first */
1523 error = nfs4_init_session(server);
1524 if (error < 0)
1525 goto out;
1526
1527 /* Probe the root fh to retrieve its FSID and filehandle */
1528 error = nfs4_get_rootfh(server, mntfh);
1529 if (error < 0)
1530 goto out;
1531
1532 dprintk("Server FSID: %llx:%llx\n",
1533 (unsigned long long) server->fsid.major,
1534 (unsigned long long) server->fsid.minor);
1535 dprintk("Mount FH: %d\n", mntfh->size);
1536
1537 nfs4_session_set_rwsize(server);
1538
1539 error = nfs_probe_fsinfo(server, mntfh, fattr);
1540 if (error < 0)
1541 goto out;
1542
1543 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1544 server->namelen = NFS4_MAXNAMLEN;
1545
Chuck Leverfca52382010-12-24 01:32:32 +00001546 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001547 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001548 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001549out:
1550 nfs_free_fattr(fattr);
1551 return error;
1552}
1553
Andy Adamson96b09e02009-04-01 09:22:33 -04001554/*
David Howells54ceac42006-08-22 20:06:13 -04001555 * Create a version 4 volume record
1556 */
1557static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001558 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001559{
Trond Myklebust33170232007-12-20 16:03:59 -05001560 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001561 int error;
1562
1563 dprintk("--> nfs4_init_server()\n");
1564
Trond Myklebust33170232007-12-20 16:03:59 -05001565 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1566 data->timeo, data->retrans);
1567
Chuck Lever542fcc32008-12-23 15:21:36 -05001568 /* Initialise the client representation from the mount data */
1569 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001570 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1571 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1572 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001573 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001574
Trond Myklebust33170232007-12-20 16:03:59 -05001575 /* Get a client record */
1576 error = nfs4_set_client(server,
1577 data->nfs_server.hostname,
1578 (const struct sockaddr *)&data->nfs_server.address,
1579 data->nfs_server.addrlen,
1580 data->client_address,
1581 data->auth_flavors[0],
1582 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001583 &timeparms,
1584 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001585 if (error < 0)
1586 goto error;
1587
Trond Myklebustb064eca22011-02-22 15:44:32 -08001588 /*
1589 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1590 * authentication.
1591 */
1592 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1593 server->caps |= NFS_CAP_UIDGID_NOMAP;
1594
David Howells54ceac42006-08-22 20:06:13 -04001595 if (data->rsize)
1596 server->rsize = nfs_block_size(data->rsize, NULL);
1597 if (data->wsize)
1598 server->wsize = nfs_block_size(data->wsize, NULL);
1599
1600 server->acregmin = data->acregmin * HZ;
1601 server->acregmax = data->acregmax * HZ;
1602 server->acdirmin = data->acdirmin * HZ;
1603 server->acdirmax = data->acdirmax * HZ;
1604
Chuck Leverf22d6d72008-03-14 14:10:22 -04001605 server->port = data->nfs_server.port;
1606
Trond Myklebust33170232007-12-20 16:03:59 -05001607 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001608
Trond Myklebust33170232007-12-20 16:03:59 -05001609error:
David Howells54ceac42006-08-22 20:06:13 -04001610 /* Done */
1611 dprintk("<-- nfs4_init_server() = %d\n", error);
1612 return error;
1613}
1614
1615/*
1616 * Create a version 4 volume record
1617 * - keyed on server and FSID
1618 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001619struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001620 struct nfs_fh *mntfh)
1621{
David Howells54ceac42006-08-22 20:06:13 -04001622 struct nfs_server *server;
1623 int error;
1624
1625 dprintk("--> nfs4_create_server()\n");
1626
1627 server = nfs_alloc_server();
1628 if (!server)
1629 return ERR_PTR(-ENOMEM);
1630
David Howells54ceac42006-08-22 20:06:13 -04001631 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001632 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001633 if (error < 0)
1634 goto error;
1635
Trond Myklebust44950b62010-06-17 11:45:12 -04001636 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001637 if (error < 0)
1638 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001639
David Howells54ceac42006-08-22 20:06:13 -04001640 dprintk("<-- nfs4_create_server() = %p\n", server);
1641 return server;
1642
1643error:
1644 nfs_free_server(server);
1645 dprintk("<-- nfs4_create_server() = error %d\n", error);
1646 return ERR_PTR(error);
1647}
1648
1649/*
1650 * Create an NFS4 referral server record
1651 */
1652struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001653 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001654{
1655 struct nfs_client *parent_client;
1656 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001657 int error;
1658
1659 dprintk("--> nfs4_create_referral_server()\n");
1660
1661 server = nfs_alloc_server();
1662 if (!server)
1663 return ERR_PTR(-ENOMEM);
1664
1665 parent_server = NFS_SB(data->sb);
1666 parent_client = parent_server->nfs_client;
1667
Chuck Lever542fcc32008-12-23 15:21:36 -05001668 /* Initialise the client representation from the parent server */
1669 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab4602009-08-09 15:06:19 -04001670 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001671
David Howells54ceac42006-08-22 20:06:13 -04001672 /* Get a client representation.
1673 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001674 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001675 data->addr,
1676 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001677 parent_client->cl_ipaddr,
1678 data->authflavor,
1679 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001680 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001681 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001682 if (error < 0)
1683 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001684
Trond Myklebust33170232007-12-20 16:03:59 -05001685 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001686 if (error < 0)
1687 goto error;
1688
Trond Myklebust44950b62010-06-17 11:45:12 -04001689 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001690 if (error < 0)
1691 goto error;
1692
David Howells54ceac42006-08-22 20:06:13 -04001693 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1694 return server;
1695
1696error:
1697 nfs_free_server(server);
1698 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1699 return ERR_PTR(error);
1700}
1701
1702#endif /* CONFIG_NFS_V4 */
1703
1704/*
1705 * Clone an NFS2, NFS3 or NFS4 server record
1706 */
1707struct nfs_server *nfs_clone_server(struct nfs_server *source,
1708 struct nfs_fh *fh,
1709 struct nfs_fattr *fattr)
1710{
1711 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001712 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001713 int error;
1714
1715 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001716 (unsigned long long) fattr->fsid.major,
1717 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001718
1719 server = nfs_alloc_server();
1720 if (!server)
1721 return ERR_PTR(-ENOMEM);
1722
Trond Myklebustfbca7792010-04-16 16:22:46 -04001723 error = -ENOMEM;
1724 fattr_fsinfo = nfs_alloc_fattr();
1725 if (fattr_fsinfo == NULL)
1726 goto out_free_server;
1727
David Howells54ceac42006-08-22 20:06:13 -04001728 /* Copy data from the source */
1729 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001730 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001731 atomic_inc(&server->nfs_client->cl_count);
1732 nfs_server_copy_userdata(server, source);
1733
1734 server->fsid = fattr->fsid;
1735
Trond Myklebust33170232007-12-20 16:03:59 -05001736 error = nfs_init_server_rpcclient(server,
1737 source->client->cl_timeout,
1738 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001739 if (error < 0)
1740 goto out_free_server;
1741 if (!IS_ERR(source->client_acl))
1742 nfs_init_server_aclclient(server);
1743
1744 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001745 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001746 if (error < 0)
1747 goto out_free_server;
1748
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001749 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1750 server->namelen = NFS4_MAXNAMLEN;
1751
David Howells54ceac42006-08-22 20:06:13 -04001752 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001753 (unsigned long long) server->fsid.major,
1754 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001755
1756 error = nfs_start_lockd(server);
1757 if (error < 0)
1758 goto out_free_server;
1759
Chuck Leverfca52382010-12-24 01:32:32 +00001760 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001761 server->mount_time = jiffies;
1762
Trond Myklebustfbca7792010-04-16 16:22:46 -04001763 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001764 dprintk("<-- nfs_clone_server() = %p\n", server);
1765 return server;
1766
1767out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001768 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001769 nfs_free_server(server);
1770 dprintk("<-- nfs_clone_server() = error %d\n", error);
1771 return ERR_PTR(error);
1772}
David Howells6aaca562006-08-22 20:06:13 -04001773
1774#ifdef CONFIG_PROC_FS
1775static struct proc_dir_entry *proc_fs_nfs;
1776
1777static int nfs_server_list_open(struct inode *inode, struct file *file);
1778static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1779static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1780static void nfs_server_list_stop(struct seq_file *p, void *v);
1781static int nfs_server_list_show(struct seq_file *m, void *v);
1782
James Morris88e9d342009-09-22 16:43:43 -07001783static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001784 .start = nfs_server_list_start,
1785 .next = nfs_server_list_next,
1786 .stop = nfs_server_list_stop,
1787 .show = nfs_server_list_show,
1788};
1789
Arjan van de Ven00977a52007-02-12 00:55:34 -08001790static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001791 .open = nfs_server_list_open,
1792 .read = seq_read,
1793 .llseek = seq_lseek,
1794 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001795 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001796};
1797
1798static int nfs_volume_list_open(struct inode *inode, struct file *file);
1799static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1800static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1801static void nfs_volume_list_stop(struct seq_file *p, void *v);
1802static int nfs_volume_list_show(struct seq_file *m, void *v);
1803
James Morris88e9d342009-09-22 16:43:43 -07001804static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001805 .start = nfs_volume_list_start,
1806 .next = nfs_volume_list_next,
1807 .stop = nfs_volume_list_stop,
1808 .show = nfs_volume_list_show,
1809};
1810
Arjan van de Ven00977a52007-02-12 00:55:34 -08001811static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001812 .open = nfs_volume_list_open,
1813 .read = seq_read,
1814 .llseek = seq_lseek,
1815 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001816 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001817};
1818
1819/*
1820 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1821 * we're dealing
1822 */
1823static int nfs_server_list_open(struct inode *inode, struct file *file)
1824{
1825 struct seq_file *m;
1826 int ret;
1827
1828 ret = seq_open(file, &nfs_server_list_ops);
1829 if (ret < 0)
1830 return ret;
1831
1832 m = file->private_data;
1833 m->private = PDE(inode)->data;
1834
1835 return 0;
1836}
1837
1838/*
1839 * set up the iterator to start reading from the server list and return the first item
1840 */
1841static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1842{
David Howells6aaca562006-08-22 20:06:13 -04001843 /* lock the list against modification */
1844 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001845 return seq_list_start_head(&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{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001853 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001854}
1855
1856/*
1857 * clean up after reading from the transports list
1858 */
1859static void nfs_server_list_stop(struct seq_file *p, void *v)
1860{
1861 spin_unlock(&nfs_client_lock);
1862}
1863
1864/*
1865 * display a header line followed by a load of call lines
1866 */
1867static int nfs_server_list_show(struct seq_file *m, void *v)
1868{
1869 struct nfs_client *clp;
1870
1871 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001872 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001873 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1874 return 0;
1875 }
1876
1877 /* display one transport per line on subsequent lines */
1878 clp = list_entry(v, struct nfs_client, cl_share_link);
1879
Malahal Naineni940aab42011-09-20 17:27:14 -07001880 /* Check if the client is initialized */
1881 if (clp->cl_cons_state != NFS_CS_READY)
1882 return 0;
1883
Chuck Lever5d8515c2007-12-10 14:57:16 -05001884 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001885 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001886 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1887 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001888 atomic_read(&clp->cl_count),
1889 clp->cl_hostname);
1890
1891 return 0;
1892}
1893
1894/*
1895 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1896 */
1897static int nfs_volume_list_open(struct inode *inode, struct file *file)
1898{
1899 struct seq_file *m;
1900 int ret;
1901
1902 ret = seq_open(file, &nfs_volume_list_ops);
1903 if (ret < 0)
1904 return ret;
1905
1906 m = file->private_data;
1907 m->private = PDE(inode)->data;
1908
1909 return 0;
1910}
1911
1912/*
1913 * set up the iterator to start reading from the volume list and return the first item
1914 */
1915static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1916{
David Howells6aaca562006-08-22 20:06:13 -04001917 /* lock the list against modification */
1918 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001919 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001920}
1921
1922/*
1923 * move to next volume
1924 */
1925static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1926{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001927 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001928}
1929
1930/*
1931 * clean up after reading from the transports list
1932 */
1933static void nfs_volume_list_stop(struct seq_file *p, void *v)
1934{
1935 spin_unlock(&nfs_client_lock);
1936}
1937
1938/*
1939 * display a header line followed by a load of call lines
1940 */
1941static int nfs_volume_list_show(struct seq_file *m, void *v)
1942{
1943 struct nfs_server *server;
1944 struct nfs_client *clp;
1945 char dev[8], fsid[17];
1946
1947 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001948 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001949 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001950 return 0;
1951 }
1952 /* display one transport per line on subsequent lines */
1953 server = list_entry(v, struct nfs_server, master_link);
1954 clp = server->nfs_client;
1955
1956 snprintf(dev, 8, "%u:%u",
1957 MAJOR(server->s_dev), MINOR(server->s_dev));
1958
1959 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001960 (unsigned long long) server->fsid.major,
1961 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001962
David Howells5d1acff2009-04-03 16:42:47 +01001963 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001964 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001965 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1966 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001967 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001968 fsid,
1969 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001970
1971 return 0;
1972}
1973
1974/*
1975 * initialise the /proc/fs/nfsfs/ directory
1976 */
1977int __init nfs_fs_proc_init(void)
1978{
1979 struct proc_dir_entry *p;
1980
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001981 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001982 if (!proc_fs_nfs)
1983 goto error_0;
1984
David Howells6aaca562006-08-22 20:06:13 -04001985 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001986 p = proc_create("servers", S_IFREG|S_IRUGO,
1987 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001988 if (!p)
1989 goto error_1;
1990
David Howells6aaca562006-08-22 20:06:13 -04001991 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001992 p = proc_create("volumes", S_IFREG|S_IRUGO,
1993 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001994 if (!p)
1995 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001996 return 0;
1997
1998error_2:
1999 remove_proc_entry("servers", proc_fs_nfs);
2000error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002001 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002002error_0:
2003 return -ENOMEM;
2004}
2005
2006/*
2007 * clean up the /proc/fs/nfsfs/ directory
2008 */
2009void nfs_fs_proc_exit(void)
2010{
2011 remove_proc_entry("volumes", proc_fs_nfs);
2012 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002013 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002014}
2015
2016#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002017
2018module_param(nfs4_disable_idmapping, bool, 0644);
2019MODULE_PARM_DESC(nfs4_disable_idmapping,
2020 "Turn off NFSv4 idmapping when using 'sec=sys'");