blob: 31778f74357de4bb3f923f6fb6d37382b598ddd1 [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>
David Howells24c8dbb2006-08-22 20:06:10 -040042
43#include <asm/system.h>
44
45#include "nfs4_fs.h"
46#include "callback.h"
47#include "delegation.h"
48#include "iostat.h"
49#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010050#include "fscache.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040051#include "pnfs.h"
David Howells24c8dbb2006-08-22 20:06:10 -040052
53#define NFSDBG_FACILITY NFSDBG_CLIENT
54
55static DEFINE_SPINLOCK(nfs_client_lock);
56static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040057static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040058static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
Andy Adamsonf4eecd52011-01-06 02:04:30 +000059#ifdef CONFIG_NFS_V4
60static DEFINE_IDR(cb_ident_idr); /* Protected by nfs_client_lock */
61
62/*
63 * Get a unique NFSv4.0 callback identifier which will be used
64 * by the V4.0 callback service to lookup the nfs_client struct
65 */
66static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
67{
68 int ret = 0;
69
70 if (clp->rpc_ops->version != 4 || minorversion != 0)
71 return ret;
72retry:
73 if (!idr_pre_get(&cb_ident_idr, GFP_KERNEL))
74 return -ENOMEM;
75 spin_lock(&nfs_client_lock);
76 ret = idr_get_new(&cb_ident_idr, clp, &clp->cl_cb_ident);
77 spin_unlock(&nfs_client_lock);
78 if (ret == -EAGAIN)
79 goto retry;
80 return ret;
81}
82#endif /* CONFIG_NFS_V4 */
David Howells24c8dbb2006-08-22 20:06:10 -040083
84/*
Trond Myklebustb064eca22011-02-22 15:44:32 -080085 * Turn off NFSv4 uid/gid mapping when using AUTH_SYS
86 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103087static bool nfs4_disable_idmapping = true;
Trond Myklebustb064eca22011-02-22 15:44:32 -080088
89/*
David Howells5006a762006-08-22 20:06:12 -040090 * RPC cruft for NFS
91 */
92static struct rpc_version *nfs_version[5] = {
93 [2] = &nfs_version2,
94#ifdef CONFIG_NFS_V3
95 [3] = &nfs_version3,
96#endif
97#ifdef CONFIG_NFS_V4
98 [4] = &nfs_version4,
99#endif
100};
101
102struct rpc_program nfs_program = {
103 .name = "nfs",
104 .number = NFS_PROGRAM,
105 .nrvers = ARRAY_SIZE(nfs_version),
106 .version = nfs_version,
107 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -0400108 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -0400109};
110
111struct rpc_stat nfs_rpcstat = {
112 .program = &nfs_program
113};
114
115
116#ifdef CONFIG_NFS_V3_ACL
117static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
118static struct rpc_version * nfsacl_version[] = {
119 [3] = &nfsacl_version3,
120};
121
122struct rpc_program nfsacl_program = {
123 .name = "nfsacl",
124 .number = NFS_ACL_PROGRAM,
125 .nrvers = ARRAY_SIZE(nfsacl_version),
126 .version = nfsacl_version,
127 .stats = &nfsacl_rpcstat,
128};
129#endif /* CONFIG_NFS_V3_ACL */
130
Trond Myklebust3a498022007-12-14 14:56:04 -0500131struct nfs_client_initdata {
132 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500133 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500134 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500135 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500136 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400137 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500138};
139
David Howells5006a762006-08-22 20:06:12 -0400140/*
David Howells24c8dbb2006-08-22 20:06:10 -0400141 * Allocate a shared client record
142 *
143 * Since these are allocated/deallocated very rarely, we don't
144 * bother putting them in a slab cache...
145 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500146static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400147{
148 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400149 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700150 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400151
152 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
153 goto error_0;
154
Trond Myklebust40c553192007-12-14 14:56:07 -0500155 clp->rpc_ops = cl_init->rpc_ops;
156
David Howells24c8dbb2006-08-22 20:06:10 -0400157 atomic_set(&clp->cl_count, 1);
158 clp->cl_cons_state = NFS_CS_INITING;
159
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500160 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
161 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400162
Trond Myklebust3a498022007-12-14 14:56:04 -0500163 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700164 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500165 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400166 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400167 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400168 }
169
170 INIT_LIST_HEAD(&clp->cl_superblocks);
171 clp->cl_rpcclient = ERR_PTR(-EINVAL);
172
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500173 clp->cl_proto = cl_init->proto;
174
David Howells24c8dbb2006-08-22 20:06:10 -0400175#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000176 err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
177 if (err)
178 goto error_cleanup;
179
David Howells24c8dbb2006-08-22 20:06:10 -0400180 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000181 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400182 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
183 clp->cl_boot_time = CURRENT_TIME;
184 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400185 clp->cl_minorversion = cl_init->minorversion;
Trond Myklebust97dc1352010-06-16 09:52:26 -0400186 clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
David Howells24c8dbb2006-08-22 20:06:10 -0400187#endif
Trond Myklebust68c97152012-01-03 13:22:46 -0500188 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400189 if (!IS_ERR(cred))
190 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100191 nfs_fscache_get_client_cookie(clp);
192
David Howells24c8dbb2006-08-22 20:06:10 -0400193 return clp;
194
Benny Halevy71468512009-04-01 09:22:56 -0400195error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400196 kfree(clp);
197error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700198 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400199}
200
Trond Myklebust5dd31772006-08-24 01:03:05 -0400201#ifdef CONFIG_NFS_V4
Andy Adamson557134a2009-04-01 09:21:53 -0400202#ifdef CONFIG_NFS_V4_1
Andy Adamsonea005282011-01-06 02:04:29 +0000203static void nfs4_shutdown_session(struct nfs_client *clp)
204{
205 if (nfs4_has_session(clp))
Andy Adamson557134a2009-04-01 09:21:53 -0400206 nfs4_destroy_session(clp->cl_session);
Andy Adamson557134a2009-04-01 09:21:53 -0400207}
Andy Adamsonea005282011-01-06 02:04:29 +0000208#else /* CONFIG_NFS_V4_1 */
209static void nfs4_shutdown_session(struct nfs_client *clp)
210{
211}
212#endif /* CONFIG_NFS_V4_1 */
Andy Adamson557134a2009-04-01 09:21:53 -0400213
214/*
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800215 * Destroy the NFS4 callback service
216 */
217static void nfs4_destroy_callback(struct nfs_client *clp)
218{
219 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Trond Myklebust97dc1352010-06-16 09:52:26 -0400220 nfs_callback_down(clp->cl_mvops->minor_version);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800221}
222
223static void nfs4_shutdown_client(struct nfs_client *clp)
224{
225 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
226 nfs4_kill_renewd(clp);
Andy Adamsonea005282011-01-06 02:04:29 +0000227 nfs4_shutdown_session(clp);
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800228 nfs4_destroy_callback(clp);
229 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
230 nfs_idmap_delete(clp);
231
232 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
233}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000234
235/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
236void nfs_cleanup_cb_ident_idr(void)
237{
238 idr_destroy(&cb_ident_idr);
239}
240
241/* nfs_client_lock held */
242static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
243{
244 if (clp->cl_cb_ident)
245 idr_remove(&cb_ident_idr, clp->cl_cb_ident);
246}
247
Fred Isamanf7e89172011-01-06 11:36:32 +0000248static void pnfs_init_server(struct nfs_server *server)
249{
250 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
251}
252
Chuck Lever0aaaf5c2011-12-06 16:13:48 -0500253static void nfs4_destroy_server(struct nfs_server *server)
254{
255 nfs4_purge_state_owners(server);
256}
257
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800258#else
259static void nfs4_shutdown_client(struct nfs_client *clp)
260{
261}
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000262
263void nfs_cleanup_cb_ident_idr(void)
264{
265}
266
267static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
268{
269}
Fred Isamanf7e89172011-01-06 11:36:32 +0000270
271static void pnfs_init_server(struct nfs_server *server)
272{
273}
274
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800275#endif /* CONFIG_NFS_V4 */
276
277/*
David Howells24c8dbb2006-08-22 20:06:10 -0400278 * Destroy a shared client record
279 */
280static void nfs_free_client(struct nfs_client *clp)
281{
Trond Myklebust40c553192007-12-14 14:56:07 -0500282 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400283
Trond Myklebust5dd31772006-08-24 01:03:05 -0400284 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400285
David Howells14727282009-04-03 16:42:42 +0100286 nfs_fscache_release_client_cookie(clp);
287
David Howells24c8dbb2006-08-22 20:06:10 -0400288 /* -EIO all pending I/O */
289 if (!IS_ERR(clp->cl_rpcclient))
290 rpc_shutdown_client(clp->cl_rpcclient);
291
Trond Myklebust7c67db32008-04-07 20:50:11 -0400292 if (clp->cl_machine_cred != NULL)
293 put_rpccred(clp->cl_machine_cred);
294
Benny Halevy1775bc32011-05-20 13:47:33 +0200295 nfs4_deviceid_purge_client(clp);
296
David Howells24c8dbb2006-08-22 20:06:10 -0400297 kfree(clp->cl_hostname);
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -0400298 kfree(clp->server_scope);
David Howells24c8dbb2006-08-22 20:06:10 -0400299 kfree(clp);
300
301 dprintk("<-- nfs_free_client()\n");
302}
303
304/*
305 * Release a reference to a shared client record
306 */
307void nfs_put_client(struct nfs_client *clp)
308{
David Howells27ba8512006-07-30 14:40:56 -0400309 if (!clp)
310 return;
311
David Howells24c8dbb2006-08-22 20:06:10 -0400312 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
313
314 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
315 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000316 nfs_cb_idr_remove_locked(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400317 spin_unlock(&nfs_client_lock);
318
319 BUG_ON(!list_empty(&clp->cl_superblocks));
320
321 nfs_free_client(clp);
322 }
323}
Andy Adamson16b374c2010-10-20 00:18:04 -0400324EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400325
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500326#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400327/*
328 * Test if two ip6 socket addresses refer to the same socket by
329 * comparing relevant fields. The padding bytes specifically, are not
330 * compared. sin6_flowinfo is not compared because it only affects QoS
331 * and sin6_scope_id is only compared if the address is "link local"
332 * because "link local" addresses need only be unique to a specific
333 * link. Conversely, ordinary unicast addresses might have different
334 * sin6_scope_id.
335 *
336 * The caller should ensure both socket addresses are AF_INET6.
337 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400338static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
339 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400340{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400341 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
342 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400343
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800344 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400345 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800346 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
347 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500348
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800349 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500350}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400351#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
352static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
353 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400354{
355 return 0;
356}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500357#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500358
David Howells24c8dbb2006-08-22 20:06:10 -0400359/*
Ian Dalld7371c42009-03-10 20:33:22 -0400360 * Test if two ip4 socket addresses refer to the same socket, by
361 * comparing relevant fields. The padding bytes specifically, are
362 * not compared.
363 *
364 * The caller should ensure both socket addresses are AF_INET.
365 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400366static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
367 const struct sockaddr *sa2)
368{
369 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
370 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
371
372 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
373}
374
375static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
376 const struct sockaddr *sa2)
377{
378 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
379 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
380
381 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
382 (sin1->sin6_port == sin2->sin6_port);
383}
384
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400385static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
386 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400387{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400388 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
389 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400390
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400391 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
392 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400393}
394
395/*
Ian Dalld7371c42009-03-10 20:33:22 -0400396 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400397 * by comparing (only) relevant fields, excluding the port number.
398 */
399static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
400 const struct sockaddr *sa2)
401{
402 if (sa1->sa_family != sa2->sa_family)
403 return 0;
404
405 switch (sa1->sa_family) {
406 case AF_INET:
407 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
408 case AF_INET6:
409 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
410 }
411 return 0;
412}
413
414/*
415 * Test if two socket addresses represent the same actual socket,
416 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400417 */
418static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
419 const struct sockaddr *sa2)
420{
421 if (sa1->sa_family != sa2->sa_family)
422 return 0;
423
424 switch (sa1->sa_family) {
425 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400426 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400427 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400428 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400429 }
430 return 0;
431}
432
Andy Adamsonc36fca52011-01-06 02:04:32 +0000433/* Common match routine for v4.0 and v4.1 callback services */
434bool
435nfs4_cb_match_client(const struct sockaddr *addr, struct nfs_client *clp,
436 u32 minorversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500437{
Andy Adamsonc36fca52011-01-06 02:04:32 +0000438 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500439
Andy Adamsonc36fca52011-01-06 02:04:32 +0000440 /* Don't match clients that failed to initialise */
441 if (!(clp->cl_cons_state == NFS_CS_READY ||
442 clp->cl_cons_state == NFS_CS_SESSION_INITING))
443 return false;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500444
Andy Adamsonc36fca52011-01-06 02:04:32 +0000445 /* Match the version and minorversion */
446 if (clp->rpc_ops->version != 4 ||
447 clp->cl_minorversion != minorversion)
448 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500449
Andy Adamsonc36fca52011-01-06 02:04:32 +0000450 /* Match only the IP address, not the port number */
451 if (!nfs_sockaddr_match_ipaddr(addr, clap))
452 return false;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500453
Andy Adamsonc36fca52011-01-06 02:04:32 +0000454 return true;
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500455}
456
457/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500458 * Find an nfs_client on the list that matches the initialisation data
459 * that is supplied.
460 */
461static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400462{
463 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400464 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400465
466 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400467 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700468 /* Don't match clients that failed to initialise properly */
469 if (clp->cl_cons_state < 0)
470 continue;
471
David Howells24c8dbb2006-08-22 20:06:10 -0400472 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500473 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400474 continue;
475
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500476 if (clp->cl_proto != data->proto)
477 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400478 /* Match nfsv4 minorversion */
479 if (clp->cl_minorversion != data->minorversion)
480 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500481 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400482 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400483 continue;
484
Trond Myklebustc81468a2007-12-14 14:56:05 -0500485 atomic_inc(&clp->cl_count);
486 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400487 }
David Howells24c8dbb2006-08-22 20:06:10 -0400488 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400489}
490
491/*
492 * Look up a client by IP address and protocol version
493 * - creates a new record if one doesn't yet exist
494 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000495static struct nfs_client *
496nfs_get_client(const struct nfs_client_initdata *cl_init,
497 const struct rpc_timeout *timeparms,
498 const char *ip_addr,
499 rpc_authflavor_t authflavour,
500 int noresvport)
David Howells24c8dbb2006-08-22 20:06:10 -0400501{
502 struct nfs_client *clp, *new = NULL;
503 int error;
504
Chuck Leverd7422c42007-12-10 14:58:51 -0500505 dprintk("--> nfs_get_client(%s,v%u)\n",
506 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400507
508 /* see if the client already exists */
509 do {
510 spin_lock(&nfs_client_lock);
511
Trond Myklebustc81468a2007-12-14 14:56:05 -0500512 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400513 if (clp)
514 goto found_client;
515 if (new)
516 goto install_client;
517
518 spin_unlock(&nfs_client_lock);
519
Trond Myklebust3a498022007-12-14 14:56:04 -0500520 new = nfs_alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700521 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400522
Chuck Levera21bdd92009-06-17 18:02:10 -0700523 dprintk("--> nfs_get_client() = %ld [failed]\n", PTR_ERR(new));
524 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400525
526 /* install a new client and return with it unready */
527install_client:
528 clp = new;
529 list_add(&clp->cl_share_link, &nfs_client_list);
530 spin_unlock(&nfs_client_lock);
Andy Adamson45a52a02011-03-01 01:34:08 +0000531
532 error = cl_init->rpc_ops->init_client(clp, timeparms, ip_addr,
533 authflavour, noresvport);
534 if (error < 0) {
535 nfs_put_client(clp);
536 return ERR_PTR(error);
537 }
David Howells24c8dbb2006-08-22 20:06:10 -0400538 dprintk("--> nfs_get_client() = %p [new]\n", clp);
539 return clp;
540
541 /* found an existing client
542 * - make sure it's ready before returning
543 */
544found_client:
545 spin_unlock(&nfs_client_lock);
546
547 if (new)
548 nfs_free_client(new);
549
Matthew Wilcox150030b2007-12-06 16:24:39 -0500550 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400551 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400552 if (error < 0) {
553 nfs_put_client(clp);
554 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400555 }
556
557 if (clp->cl_cons_state < NFS_CS_READY) {
558 error = clp->cl_cons_state;
559 nfs_put_client(clp);
560 return ERR_PTR(error);
561 }
562
David Howells54ceac42006-08-22 20:06:13 -0400563 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
564
David Howells24c8dbb2006-08-22 20:06:10 -0400565 dprintk("--> nfs_get_client() = %p [share]\n", clp);
566 return clp;
567}
568
569/*
570 * Mark a server as ready or failed
571 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400572void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400573{
574 clp->cl_cons_state = state;
575 wake_up_all(&nfs_client_active_wq);
576}
David Howells5006a762006-08-22 20:06:12 -0400577
578/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400579 * With sessions, the client is not marked ready until after a
580 * successful EXCHANGE_ID and CREATE_SESSION.
581 *
582 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
583 * other versions of NFS can be tried.
584 */
585int nfs4_check_client_ready(struct nfs_client *clp)
586{
587 if (!nfs4_has_session(clp))
588 return 0;
589 if (clp->cl_cons_state < NFS_CS_READY)
590 return -EPROTONOSUPPORT;
591 return 0;
592}
593
594/*
David Howells5006a762006-08-22 20:06:12 -0400595 * Initialise the timeout values for a connection
596 */
597static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
598 unsigned int timeo, unsigned int retrans)
599{
600 to->to_initval = timeo * HZ / 10;
601 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400602
603 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400604 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400605 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400606 if (to->to_retries == 0)
607 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500608 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400609 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400610 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
611 to->to_initval = NFS_MAX_TCP_TIMEOUT;
612 to->to_increment = to->to_initval;
613 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500614 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
615 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
616 if (to->to_maxval < to->to_initval)
617 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400618 to->to_exponential = 0;
619 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400620 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400621 if (to->to_retries == 0)
622 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400623 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400624 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400625 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
626 to->to_initval = NFS_MAX_UDP_TIMEOUT;
627 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
628 to->to_exponential = 1;
629 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400630 default:
631 BUG();
David Howells5006a762006-08-22 20:06:12 -0400632 }
633}
634
635/*
636 * Create an RPC client handle
637 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500638static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500639 const struct rpc_timeout *timeparms,
640 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500641 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400642{
David Howells5006a762006-08-22 20:06:12 -0400643 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400644 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400645 .net = &init_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500646 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400647 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500648 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500649 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400650 .servername = clp->cl_hostname,
651 .program = &nfs_program,
652 .version = clp->rpc_ops->version,
653 .authflavor = flavor,
654 };
David Howells5006a762006-08-22 20:06:12 -0400655
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500656 if (discrtry)
657 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
658 if (noresvport)
659 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
660
David Howells5006a762006-08-22 20:06:12 -0400661 if (!IS_ERR(clp->cl_rpcclient))
662 return 0;
663
Chuck Lever41877d22006-08-22 20:06:20 -0400664 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400665 if (IS_ERR(clnt)) {
666 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700667 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400668 return PTR_ERR(clnt);
669 }
670
David Howells5006a762006-08-22 20:06:12 -0400671 clp->cl_rpcclient = clnt;
672 return 0;
673}
David Howells54ceac42006-08-22 20:06:13 -0400674
675/*
676 * Version 2 or 3 client destruction
677 */
678static void nfs_destroy_server(struct nfs_server *server)
679{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400680 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
681 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500682 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400683}
684
685/*
686 * Version 2 or 3 lockd setup
687 */
688static int nfs_start_lockd(struct nfs_server *server)
689{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500690 struct nlm_host *host;
691 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500692 struct nlmclnt_initdata nlm_init = {
693 .hostname = clp->cl_hostname,
694 .address = (struct sockaddr *)&clp->cl_addr,
695 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500696 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500697 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
698 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500699 };
David Howells54ceac42006-08-22 20:06:13 -0400700
Chuck Lever883bb162008-01-15 16:04:20 -0500701 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500702 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400703 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
704 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500705 return 0;
706
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400707 switch (clp->cl_proto) {
708 default:
709 nlm_init.protocol = IPPROTO_TCP;
710 break;
711 case XPRT_TRANSPORT_UDP:
712 nlm_init.protocol = IPPROTO_UDP;
713 }
714
Chuck Lever883bb162008-01-15 16:04:20 -0500715 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500716 if (IS_ERR(host))
717 return PTR_ERR(host);
718
719 server->nlm_host = host;
720 server->destroy = nfs_destroy_server;
721 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400722}
723
724/*
725 * Initialise an NFSv3 ACL client connection
726 */
727#ifdef CONFIG_NFS_V3_ACL
728static void nfs_init_server_aclclient(struct nfs_server *server)
729{
Trond Myklebust40c553192007-12-14 14:56:07 -0500730 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400731 goto out_noacl;
732 if (server->flags & NFS_MOUNT_NOACL)
733 goto out_noacl;
734
735 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
736 if (IS_ERR(server->client_acl))
737 goto out_noacl;
738
739 /* No errors! Assume that Sun nfsacls are supported */
740 server->caps |= NFS_CAP_ACLS;
741 return;
742
743out_noacl:
744 server->caps &= ~NFS_CAP_ACLS;
745}
746#else
747static inline void nfs_init_server_aclclient(struct nfs_server *server)
748{
749 server->flags &= ~NFS_MOUNT_NOACL;
750 server->caps &= ~NFS_CAP_ACLS;
751}
752#endif
753
754/*
755 * Create a general RPC client
756 */
Trond Myklebust33170232007-12-20 16:03:59 -0500757static int nfs_init_server_rpcclient(struct nfs_server *server,
758 const struct rpc_timeout *timeo,
759 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400760{
761 struct nfs_client *clp = server->nfs_client;
762
763 server->client = rpc_clone_client(clp->cl_rpcclient);
764 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700765 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400766 return PTR_ERR(server->client);
767 }
768
Trond Myklebust33170232007-12-20 16:03:59 -0500769 memcpy(&server->client->cl_timeout_default,
770 timeo,
771 sizeof(server->client->cl_timeout_default));
772 server->client->cl_timeout = &server->client->cl_timeout_default;
773
David Howells54ceac42006-08-22 20:06:13 -0400774 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
775 struct rpc_auth *auth;
776
777 auth = rpcauth_create(pseudoflavour, server->client);
778 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700779 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400780 return PTR_ERR(auth);
781 }
782 }
783 server->client->cl_softrtry = 0;
784 if (server->flags & NFS_MOUNT_SOFT)
785 server->client->cl_softrtry = 1;
786
David Howells54ceac42006-08-22 20:06:13 -0400787 return 0;
788}
789
790/*
791 * Initialise an NFS2 or NFS3 client
792 */
Andy Adamson45a52a02011-03-01 01:34:08 +0000793int nfs_init_client(struct nfs_client *clp, const struct rpc_timeout *timeparms,
794 const char *ip_addr, rpc_authflavor_t authflavour,
795 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -0400796{
David Howells54ceac42006-08-22 20:06:13 -0400797 int error;
798
799 if (clp->cl_cons_state == NFS_CS_READY) {
800 /* the client is already initialised */
801 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
802 return 0;
803 }
804
David Howells54ceac42006-08-22 20:06:13 -0400805 /*
806 * Create a client RPC handle for doing FSSTAT with UNIX auth only
807 * - RFC 2623, sec 2.3.2
808 */
Chuck Leverd7403512008-12-23 15:21:37 -0500809 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
Andy Adamson45a52a02011-03-01 01:34:08 +0000810 0, noresvport);
David Howells54ceac42006-08-22 20:06:13 -0400811 if (error < 0)
812 goto error;
813 nfs_mark_client_ready(clp, NFS_CS_READY);
814 return 0;
815
816error:
817 nfs_mark_client_ready(clp, error);
818 dprintk("<-- nfs_init_client() = xerror %d\n", error);
819 return error;
820}
821
822/*
823 * Create a version 2 or 3 client
824 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400825static int nfs_init_server(struct nfs_server *server,
826 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400827{
Trond Myklebust3a498022007-12-14 14:56:04 -0500828 struct nfs_client_initdata cl_init = {
829 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500830 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500831 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500832 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500833 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500834 };
Trond Myklebust33170232007-12-20 16:03:59 -0500835 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400836 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500837 int error;
David Howells54ceac42006-08-22 20:06:13 -0400838
839 dprintk("--> nfs_init_server()\n");
840
841#ifdef CONFIG_NFS_V3
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400842 if (data->version == 3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500843 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400844#endif
845
Andy Adamson45a52a02011-03-01 01:34:08 +0000846 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
847 data->timeo, data->retrans);
848
David Howells54ceac42006-08-22 20:06:13 -0400849 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +0000850 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX,
851 data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400852 if (IS_ERR(clp)) {
853 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
854 return PTR_ERR(clp);
855 }
856
David Howells54ceac42006-08-22 20:06:13 -0400857 server->nfs_client = clp;
858
859 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400860 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100861 server->options = data->options;
Trond Myklebust62ab460c2009-08-09 15:06:19 -0400862 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
863 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
864 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
David Howells54ceac42006-08-22 20:06:13 -0400865
866 if (data->rsize)
867 server->rsize = nfs_block_size(data->rsize, NULL);
868 if (data->wsize)
869 server->wsize = nfs_block_size(data->wsize, NULL);
870
871 server->acregmin = data->acregmin * HZ;
872 server->acregmax = data->acregmax * HZ;
873 server->acdirmin = data->acdirmin * HZ;
874 server->acdirmax = data->acdirmax * HZ;
875
876 /* Start lockd here, before we might error out */
877 error = nfs_start_lockd(server);
878 if (error < 0)
879 goto error;
880
Chuck Leverf22d6d72008-03-14 14:10:22 -0400881 server->port = data->nfs_server.port;
882
Trond Myklebust33170232007-12-20 16:03:59 -0500883 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400884 if (error < 0)
885 goto error;
886
Chuck Lever3f8400d2008-03-14 14:10:30 -0400887 /* Preserve the values of mount_server-related mount options */
888 if (data->mount_server.addrlen) {
889 memcpy(&server->mountd_address, &data->mount_server.address,
890 data->mount_server.addrlen);
891 server->mountd_addrlen = data->mount_server.addrlen;
892 }
893 server->mountd_version = data->mount_server.version;
894 server->mountd_port = data->mount_server.port;
895 server->mountd_protocol = data->mount_server.protocol;
896
David Howells54ceac42006-08-22 20:06:13 -0400897 server->namelen = data->namlen;
898 /* Create a client RPC handle for the NFSv3 ACL management interface */
899 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400900 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
901 return 0;
902
903error:
904 server->nfs_client = NULL;
905 nfs_put_client(clp);
906 dprintk("<-- nfs_init_server() = xerror %d\n", error);
907 return error;
908}
909
910/*
911 * Load up the server record from information gained in an fsinfo record
912 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400913static void nfs_server_set_fsinfo(struct nfs_server *server,
914 struct nfs_fh *mntfh,
915 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400916{
917 unsigned long max_rpc_payload;
918
919 /* Work out a lot of parameters */
920 if (server->rsize == 0)
921 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
922 if (server->wsize == 0)
923 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
924
925 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
926 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
927 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
928 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
929
930 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
931 if (server->rsize > max_rpc_payload)
932 server->rsize = max_rpc_payload;
933 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
934 server->rsize = NFS_MAX_FILE_IO_SIZE;
935 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700936
Jens Axboed9938312009-06-12 14:45:52 +0200937 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400938 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
939
940 if (server->wsize > max_rpc_payload)
941 server->wsize = max_rpc_payload;
942 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
943 server->wsize = NFS_MAX_FILE_IO_SIZE;
944 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400945 server->pnfs_blksize = fsinfo->blksize;
Benny Halevy738fd0f32011-07-30 20:52:36 -0400946 set_pnfs_layoutdriver(server, mntfh, fsinfo->layouttype);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400947
David Howells54ceac42006-08-22 20:06:13 -0400948 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
949
950 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400951 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
952 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400953 if (server->dtsize > server->rsize)
954 server->dtsize = server->rsize;
955
956 if (server->flags & NFS_MOUNT_NOAC) {
957 server->acregmin = server->acregmax = 0;
958 server->acdirmin = server->acdirmax = 0;
959 }
960
961 server->maxfilesize = fsinfo->maxfilesize;
962
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700963 server->time_delta = fsinfo->time_delta;
964
David Howells54ceac42006-08-22 20:06:13 -0400965 /* We're airborne Set socket buffersize */
966 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
967}
968
969/*
970 * Probe filesystem information, including the FSID on v2/v3
971 */
972static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
973{
974 struct nfs_fsinfo fsinfo;
975 struct nfs_client *clp = server->nfs_client;
976 int error;
977
978 dprintk("--> nfs_probe_fsinfo()\n");
979
980 if (clp->rpc_ops->set_capabilities != NULL) {
981 error = clp->rpc_ops->set_capabilities(server, mntfh);
982 if (error < 0)
983 goto out_error;
984 }
985
986 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400987 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400988 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
989 if (error < 0)
990 goto out_error;
991
Benny Halevy738fd0f32011-07-30 20:52:36 -0400992 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -0400993
994 /* Get some general file system info */
995 if (server->namelen == 0) {
996 struct nfs_pathconf pathinfo;
997
998 pathinfo.fattr = fattr;
999 nfs_fattr_init(fattr);
1000
1001 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
1002 server->namelen = pathinfo.max_namelen;
1003 }
1004
1005 dprintk("<-- nfs_probe_fsinfo() = 0\n");
1006 return 0;
1007
1008out_error:
1009 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
1010 return error;
1011}
1012
1013/*
1014 * Copy useful information when duplicating a server record
1015 */
1016static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
1017{
1018 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -04001019 target->rsize = source->rsize;
1020 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -04001021 target->acregmin = source->acregmin;
1022 target->acregmax = source->acregmax;
1023 target->acdirmin = source->acdirmin;
1024 target->acdirmax = source->acdirmax;
1025 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001026 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001027}
1028
Chuck Leverfca52382010-12-24 01:32:32 +00001029static void nfs_server_insert_lists(struct nfs_server *server)
1030{
1031 struct nfs_client *clp = server->nfs_client;
1032
1033 spin_lock(&nfs_client_lock);
1034 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
1035 list_add_tail(&server->master_link, &nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001036 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001037 spin_unlock(&nfs_client_lock);
1038
1039}
1040
1041static void nfs_server_remove_lists(struct nfs_server *server)
1042{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001043 struct nfs_client *clp = server->nfs_client;
1044
Chuck Leverfca52382010-12-24 01:32:32 +00001045 spin_lock(&nfs_client_lock);
1046 list_del_rcu(&server->client_link);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001047 if (clp && list_empty(&clp->cl_superblocks))
1048 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001049 list_del(&server->master_link);
1050 spin_unlock(&nfs_client_lock);
1051
1052 synchronize_rcu();
1053}
1054
David Howells54ceac42006-08-22 20:06:13 -04001055/*
1056 * Allocate and initialise a server record
1057 */
1058static struct nfs_server *nfs_alloc_server(void)
1059{
1060 struct nfs_server *server;
1061
1062 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1063 if (!server)
1064 return NULL;
1065
1066 server->client = server->client_acl = ERR_PTR(-EINVAL);
1067
1068 /* Zero out the NFS state stuff */
1069 INIT_LIST_HEAD(&server->client_link);
1070 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001071 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001072 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001073 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001074
Steve Dicksonef818a22007-11-08 04:05:04 -05001075 atomic_set(&server->active, 0);
1076
David Howells54ceac42006-08-22 20:06:13 -04001077 server->io_stats = nfs_alloc_iostats();
1078 if (!server->io_stats) {
1079 kfree(server);
1080 return NULL;
1081 }
1082
Jens Axboe48d07642009-09-21 09:59:39 +02001083 if (bdi_init(&server->backing_dev_info)) {
1084 nfs_free_iostats(server->io_stats);
1085 kfree(server);
1086 return NULL;
1087 }
1088
Fred Isamanf7e89172011-01-06 11:36:32 +00001089 pnfs_init_server(server);
1090
David Howells54ceac42006-08-22 20:06:13 -04001091 return server;
1092}
1093
1094/*
1095 * Free up a server record
1096 */
1097void nfs_free_server(struct nfs_server *server)
1098{
1099 dprintk("--> nfs_free_server()\n");
1100
Chuck Leverfca52382010-12-24 01:32:32 +00001101 nfs_server_remove_lists(server);
Ricardo Labiaga85e174b2010-10-20 00:17:58 -04001102 unset_pnfs_layoutdriver(server);
David Howells54ceac42006-08-22 20:06:13 -04001103
1104 if (server->destroy != NULL)
1105 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001106
1107 if (!IS_ERR(server->client_acl))
1108 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001109 if (!IS_ERR(server->client))
1110 rpc_shutdown_client(server->client);
1111
1112 nfs_put_client(server->nfs_client);
1113
1114 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001115 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001116 kfree(server);
1117 nfs_release_automount_timer();
1118 dprintk("<-- nfs_free_server()\n");
1119}
1120
1121/*
1122 * Create a version 2 or 3 volume record
1123 * - keyed on server and FSID
1124 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001125struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001126 struct nfs_fh *mntfh)
1127{
1128 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001129 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001130 int error;
1131
1132 server = nfs_alloc_server();
1133 if (!server)
1134 return ERR_PTR(-ENOMEM);
1135
Trond Myklebustfbca7792010-04-16 16:22:46 -04001136 error = -ENOMEM;
1137 fattr = nfs_alloc_fattr();
1138 if (fattr == NULL)
1139 goto error;
1140
David Howells54ceac42006-08-22 20:06:13 -04001141 /* Get a client representation */
1142 error = nfs_init_server(server, data);
1143 if (error < 0)
1144 goto error;
1145
1146 BUG_ON(!server->nfs_client);
1147 BUG_ON(!server->nfs_client->rpc_ops);
1148 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1149
1150 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001151 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001152 if (error < 0)
1153 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001154 if (server->nfs_client->rpc_ops->version == 3) {
1155 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1156 server->namelen = NFS3_MAXNAMLEN;
1157 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1158 server->caps |= NFS_CAP_READDIRPLUS;
1159 } else {
1160 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1161 server->namelen = NFS2_MAXNAMLEN;
1162 }
1163
Trond Myklebustfbca7792010-04-16 16:22:46 -04001164 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1165 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001166 if (error < 0) {
1167 dprintk("nfs_create_server: getattr error = %d\n", -error);
1168 goto error;
1169 }
1170 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001171 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001172
David Howells6daabf12006-08-24 15:44:16 -04001173 dprintk("Server FSID: %llx:%llx\n",
1174 (unsigned long long) server->fsid.major,
1175 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001176
Chuck Leverfca52382010-12-24 01:32:32 +00001177 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001178 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001179 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001180 return server;
1181
1182error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001183 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001184 nfs_free_server(server);
1185 return ERR_PTR(error);
1186}
1187
1188#ifdef CONFIG_NFS_V4
1189/*
Andy Adamsonc36fca52011-01-06 02:04:32 +00001190 * NFSv4.0 callback thread helper
1191 *
1192 * Find a client by IP address, protocol version, and minorversion
1193 *
1194 * Called from the pg_authenticate method. The callback identifier
1195 * is not used as it has not been decoded.
1196 *
1197 * Returns NULL if no such client
1198 */
1199struct nfs_client *
1200nfs4_find_client_no_ident(const struct sockaddr *addr)
1201{
1202 struct nfs_client *clp;
1203
1204 spin_lock(&nfs_client_lock);
1205 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1206 if (nfs4_cb_match_client(addr, clp, 0) == false)
1207 continue;
1208 atomic_inc(&clp->cl_count);
1209 spin_unlock(&nfs_client_lock);
1210 return clp;
1211 }
1212 spin_unlock(&nfs_client_lock);
1213 return NULL;
1214}
1215
1216/*
1217 * NFSv4.0 callback thread helper
1218 *
1219 * Find a client by callback identifier
1220 */
1221struct nfs_client *
1222nfs4_find_client_ident(int cb_ident)
1223{
1224 struct nfs_client *clp;
1225
1226 spin_lock(&nfs_client_lock);
1227 clp = idr_find(&cb_ident_idr, cb_ident);
1228 if (clp)
1229 atomic_inc(&clp->cl_count);
1230 spin_unlock(&nfs_client_lock);
1231 return clp;
1232}
1233
1234#if defined(CONFIG_NFS_V4_1)
1235/*
1236 * NFSv4.1 callback thread helper
1237 * For CB_COMPOUND calls, find a client by IP address, protocol version,
1238 * minorversion, and sessionID
1239 *
Andy Adamsonc36fca52011-01-06 02:04:32 +00001240 * Returns NULL if no such client
1241 */
1242struct nfs_client *
1243nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001244 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001245{
1246 struct nfs_client *clp;
1247
1248 spin_lock(&nfs_client_lock);
1249 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
1250 if (nfs4_cb_match_client(addr, clp, 1) == false)
1251 continue;
1252
1253 if (!nfs4_has_session(clp))
1254 continue;
1255
Andy Adamson778be232011-01-25 15:38:01 +00001256 /* Match sessionid*/
1257 if (memcmp(clp->cl_session->sess_id.data,
1258 sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001259 continue;
1260
1261 atomic_inc(&clp->cl_count);
1262 spin_unlock(&nfs_client_lock);
1263 return clp;
1264 }
1265 spin_unlock(&nfs_client_lock);
1266 return NULL;
1267}
1268
1269#else /* CONFIG_NFS_V4_1 */
1270
1271struct nfs_client *
1272nfs4_find_client_sessionid(const struct sockaddr *addr,
Andy Adamson778be232011-01-25 15:38:01 +00001273 struct nfs4_sessionid *sid)
Andy Adamsonc36fca52011-01-06 02:04:32 +00001274{
1275 return NULL;
1276}
1277#endif /* CONFIG_NFS_V4_1 */
1278
1279/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001280 * Initialize the NFS4 callback service
1281 */
1282static int nfs4_init_callback(struct nfs_client *clp)
1283{
1284 int error;
1285
1286 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001287 if (nfs4_has_session(clp)) {
1288 error = xprt_setup_backchannel(
1289 clp->cl_rpcclient->cl_xprt,
1290 NFS41_BC_MIN_CALLBACKS);
1291 if (error < 0)
1292 return error;
1293 }
1294
Trond Myklebust97dc1352010-06-16 09:52:26 -04001295 error = nfs_callback_up(clp->cl_mvops->minor_version,
Benny Halevy71468512009-04-01 09:22:56 -04001296 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001297 if (error < 0) {
1298 dprintk("%s: failed to start callback. Error = %d\n",
1299 __func__, error);
1300 return error;
1301 }
1302 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1303 }
1304 return 0;
1305}
1306
1307/*
Andy Adamson557134a2009-04-01 09:21:53 -04001308 * Initialize the minor version specific parts of an NFS4 client record
1309 */
1310static int nfs4_init_client_minor_version(struct nfs_client *clp)
1311{
1312#if defined(CONFIG_NFS_V4_1)
Trond Myklebust97dc1352010-06-16 09:52:26 -04001313 if (clp->cl_mvops->minor_version) {
Andy Adamson557134a2009-04-01 09:21:53 -04001314 struct nfs4_session *session = NULL;
1315 /*
1316 * Create the session and mark it expired.
1317 * When a SEQUENCE operation encounters the expired session
1318 * it will do session recovery to initialize it.
1319 */
1320 session = nfs4_alloc_session(clp);
1321 if (!session)
1322 return -ENOMEM;
1323
1324 clp->cl_session = session;
Trond Myklebustfe74ba32010-06-16 09:52:27 -04001325 /*
1326 * The create session reply races with the server back
1327 * channel probe. Mark the client NFS_CS_SESSION_INITING
1328 * so that the client back channel can find the
1329 * nfs_client struct
1330 */
1331 clp->cl_cons_state = NFS_CS_SESSION_INITING;
Andy Adamson557134a2009-04-01 09:21:53 -04001332 }
1333#endif /* CONFIG_NFS_V4_1 */
1334
Benny Halevy71468512009-04-01 09:22:56 -04001335 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001336}
1337
1338/*
David Howells54ceac42006-08-22 20:06:13 -04001339 * Initialise an NFS4 client record
1340 */
Andy Adamson45a52a02011-03-01 01:34:08 +00001341int nfs4_init_client(struct nfs_client *clp,
1342 const struct rpc_timeout *timeparms,
1343 const char *ip_addr,
1344 rpc_authflavor_t authflavour,
1345 int noresvport)
David Howells54ceac42006-08-22 20:06:13 -04001346{
1347 int error;
1348
1349 if (clp->cl_cons_state == NFS_CS_READY) {
1350 /* the client is initialised already */
1351 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1352 return 0;
1353 }
1354
1355 /* Check NFS protocol revision and initialize RPC op vector */
1356 clp->rpc_ops = &nfs_v4_clientops;
1357
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001358 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Andy Adamson45a52a02011-03-01 01:34:08 +00001359 1, noresvport);
David Howells54ceac42006-08-22 20:06:13 -04001360 if (error < 0)
1361 goto error;
Ben Hutchingsf4373bf2009-10-06 15:42:18 -04001362 strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001363
1364 error = nfs_idmap_new(clp);
1365 if (error < 0) {
1366 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001367 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001368 goto error;
1369 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001370 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001371
Andy Adamson557134a2009-04-01 09:21:53 -04001372 error = nfs4_init_client_minor_version(clp);
1373 if (error < 0)
1374 goto error;
1375
Andy Adamson76db6d92009-04-01 09:22:38 -04001376 if (!nfs4_has_session(clp))
1377 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001378 return 0;
1379
1380error:
1381 nfs_mark_client_ready(clp, error);
1382 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1383 return error;
1384}
1385
1386/*
1387 * Set up an NFS4 client
1388 */
1389static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001390 const char *hostname,
1391 const struct sockaddr *addr,
1392 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001393 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001394 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001395 int proto, const struct rpc_timeout *timeparms,
1396 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001397{
Trond Myklebust3a498022007-12-14 14:56:04 -05001398 struct nfs_client_initdata cl_init = {
1399 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001400 .addr = addr,
1401 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001402 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001403 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001404 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001405 };
David Howells54ceac42006-08-22 20:06:13 -04001406 struct nfs_client *clp;
1407 int error;
1408
1409 dprintk("--> nfs4_set_client()\n");
1410
1411 /* Allocate or find a client reference we can use */
Andy Adamson45a52a02011-03-01 01:34:08 +00001412 clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour,
1413 server->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001414 if (IS_ERR(clp)) {
1415 error = PTR_ERR(clp);
1416 goto error;
1417 }
David Howells54ceac42006-08-22 20:06:13 -04001418
Andy Adamsond6fb79d2011-03-01 01:34:11 +00001419 /*
1420 * Query for the lease time on clientid setup or renewal
1421 *
1422 * Note that this will be set on nfs_clients that were created
1423 * only for the DS role and did not set this bit, but now will
1424 * serve a dual role.
1425 */
1426 set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
1427
David Howells54ceac42006-08-22 20:06:13 -04001428 server->nfs_client = clp;
1429 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1430 return 0;
David Howells54ceac42006-08-22 20:06:13 -04001431error:
1432 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1433 return error;
1434}
1435
Andy Adamsond83217c2011-03-01 01:34:17 +00001436/*
1437 * Set up a pNFS Data Server client.
1438 *
1439 * Return any existing nfs_client that matches server address,port,version
1440 * and minorversion.
1441 *
1442 * For a new nfs_client, use a soft mount (default), a low retrans and a
1443 * low timeout interval so that if a connection is lost, we retry through
1444 * the MDS.
1445 */
1446struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
1447 const struct sockaddr *ds_addr,
1448 int ds_addrlen, int ds_proto)
1449{
1450 struct nfs_client_initdata cl_init = {
1451 .addr = ds_addr,
1452 .addrlen = ds_addrlen,
1453 .rpc_ops = &nfs_v4_clientops,
1454 .proto = ds_proto,
1455 .minorversion = mds_clp->cl_minorversion,
1456 };
1457 struct rpc_timeout ds_timeout = {
1458 .to_initval = 15 * HZ,
1459 .to_maxval = 15 * HZ,
1460 .to_retries = 1,
1461 .to_exponential = 1,
1462 };
1463 struct nfs_client *clp;
1464
1465 /*
1466 * Set an authflavor equual to the MDS value. Use the MDS nfs_client
1467 * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
1468 * (section 13.1 RFC 5661).
1469 */
1470 clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
1471 mds_clp->cl_rpcclient->cl_auth->au_flavor, 0);
1472
1473 dprintk("<-- %s %p\n", __func__, clp);
1474 return clp;
1475}
Trond Myklebust94b134a2011-07-13 19:26:49 -04001476EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
Andy Adamson557134a2009-04-01 09:21:53 -04001477
1478/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001479 * Session has been established, and the client marked ready.
1480 * Set the mount rsize and wsize with negotiated fore channel
1481 * attributes which will be bound checked in nfs_server_set_fsinfo.
1482 */
1483static void nfs4_session_set_rwsize(struct nfs_server *server)
1484{
1485#ifdef CONFIG_NFS_V4_1
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001486 struct nfs4_session *sess;
1487 u32 server_resp_sz;
1488 u32 server_rqst_sz;
1489
Andy Adamson96b09e02009-04-01 09:22:33 -04001490 if (!nfs4_has_session(server->nfs_client))
1491 return;
Alexandros Batsakis2449ea22009-12-05 13:36:55 -05001492 sess = server->nfs_client->cl_session;
1493 server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1494 server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1495
1496 if (server->rsize > server_resp_sz)
1497 server->rsize = server_resp_sz;
1498 if (server->wsize > server_rqst_sz)
1499 server->wsize = server_rqst_sz;
Andy Adamson96b09e02009-04-01 09:22:33 -04001500#endif /* CONFIG_NFS_V4_1 */
1501}
1502
Trond Myklebust44950b62010-06-17 11:45:12 -04001503static int nfs4_server_common_setup(struct nfs_server *server,
1504 struct nfs_fh *mntfh)
1505{
1506 struct nfs_fattr *fattr;
1507 int error;
1508
1509 BUG_ON(!server->nfs_client);
1510 BUG_ON(!server->nfs_client->rpc_ops);
1511 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1512
Andy Adamson94de8b22011-03-01 01:34:12 +00001513 /* data servers support only a subset of NFSv4.1 */
1514 if (is_ds_only_client(server->nfs_client))
1515 return -EPROTONOSUPPORT;
1516
Trond Myklebust44950b62010-06-17 11:45:12 -04001517 fattr = nfs_alloc_fattr();
1518 if (fattr == NULL)
1519 return -ENOMEM;
1520
1521 /* We must ensure the session is initialised first */
1522 error = nfs4_init_session(server);
1523 if (error < 0)
1524 goto out;
1525
1526 /* Probe the root fh to retrieve its FSID and filehandle */
1527 error = nfs4_get_rootfh(server, mntfh);
1528 if (error < 0)
1529 goto out;
1530
1531 dprintk("Server FSID: %llx:%llx\n",
1532 (unsigned long long) server->fsid.major,
1533 (unsigned long long) server->fsid.minor);
1534 dprintk("Mount FH: %d\n", mntfh->size);
1535
1536 nfs4_session_set_rwsize(server);
1537
1538 error = nfs_probe_fsinfo(server, mntfh, fattr);
1539 if (error < 0)
1540 goto out;
1541
1542 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1543 server->namelen = NFS4_MAXNAMLEN;
1544
Chuck Leverfca52382010-12-24 01:32:32 +00001545 nfs_server_insert_lists(server);
Trond Myklebust44950b62010-06-17 11:45:12 -04001546 server->mount_time = jiffies;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001547 server->destroy = nfs4_destroy_server;
Trond Myklebust44950b62010-06-17 11:45:12 -04001548out:
1549 nfs_free_fattr(fattr);
1550 return error;
1551}
1552
Andy Adamson96b09e02009-04-01 09:22:33 -04001553/*
David Howells54ceac42006-08-22 20:06:13 -04001554 * Create a version 4 volume record
1555 */
1556static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001557 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001558{
Trond Myklebust33170232007-12-20 16:03:59 -05001559 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001560 int error;
1561
1562 dprintk("--> nfs4_init_server()\n");
1563
Trond Myklebust33170232007-12-20 16:03:59 -05001564 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1565 data->timeo, data->retrans);
1566
Chuck Lever542fcc32008-12-23 15:21:36 -05001567 /* Initialise the client representation from the mount data */
1568 server->flags = data->flags;
Bryan Schumaker82f2e542010-10-21 16:33:18 -04001569 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
1570 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1571 server->caps |= NFS_CAP_READDIRPLUS;
David Howellsb797cac2009-04-03 16:42:48 +01001572 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001573
Trond Myklebust33170232007-12-20 16:03:59 -05001574 /* Get a client record */
1575 error = nfs4_set_client(server,
1576 data->nfs_server.hostname,
1577 (const struct sockaddr *)&data->nfs_server.address,
1578 data->nfs_server.addrlen,
1579 data->client_address,
1580 data->auth_flavors[0],
1581 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001582 &timeparms,
1583 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001584 if (error < 0)
1585 goto error;
1586
Trond Myklebustb064eca22011-02-22 15:44:32 -08001587 /*
1588 * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1589 * authentication.
1590 */
1591 if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
1592 server->caps |= NFS_CAP_UIDGID_NOMAP;
1593
David Howells54ceac42006-08-22 20:06:13 -04001594 if (data->rsize)
1595 server->rsize = nfs_block_size(data->rsize, NULL);
1596 if (data->wsize)
1597 server->wsize = nfs_block_size(data->wsize, NULL);
1598
1599 server->acregmin = data->acregmin * HZ;
1600 server->acregmax = data->acregmax * HZ;
1601 server->acdirmin = data->acdirmin * HZ;
1602 server->acdirmax = data->acdirmax * HZ;
1603
Chuck Leverf22d6d72008-03-14 14:10:22 -04001604 server->port = data->nfs_server.port;
1605
Trond Myklebust33170232007-12-20 16:03:59 -05001606 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001607
Trond Myklebust33170232007-12-20 16:03:59 -05001608error:
David Howells54ceac42006-08-22 20:06:13 -04001609 /* Done */
1610 dprintk("<-- nfs4_init_server() = %d\n", error);
1611 return error;
1612}
1613
1614/*
1615 * Create a version 4 volume record
1616 * - keyed on server and FSID
1617 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001618struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001619 struct nfs_fh *mntfh)
1620{
David Howells54ceac42006-08-22 20:06:13 -04001621 struct nfs_server *server;
1622 int error;
1623
1624 dprintk("--> nfs4_create_server()\n");
1625
1626 server = nfs_alloc_server();
1627 if (!server)
1628 return ERR_PTR(-ENOMEM);
1629
David Howells54ceac42006-08-22 20:06:13 -04001630 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001631 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001632 if (error < 0)
1633 goto error;
1634
Trond Myklebust44950b62010-06-17 11:45:12 -04001635 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustfccba802009-07-21 16:48:07 -04001636 if (error < 0)
1637 goto error;
Andy Adamson557134a2009-04-01 09:21:53 -04001638
David Howells54ceac42006-08-22 20:06:13 -04001639 dprintk("<-- nfs4_create_server() = %p\n", server);
1640 return server;
1641
1642error:
1643 nfs_free_server(server);
1644 dprintk("<-- nfs4_create_server() = error %d\n", error);
1645 return ERR_PTR(error);
1646}
1647
1648/*
1649 * Create an NFS4 referral server record
1650 */
1651struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001652 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001653{
1654 struct nfs_client *parent_client;
1655 struct nfs_server *server, *parent_server;
David Howells54ceac42006-08-22 20:06:13 -04001656 int error;
1657
1658 dprintk("--> nfs4_create_referral_server()\n");
1659
1660 server = nfs_alloc_server();
1661 if (!server)
1662 return ERR_PTR(-ENOMEM);
1663
1664 parent_server = NFS_SB(data->sb);
1665 parent_client = parent_server->nfs_client;
1666
Chuck Lever542fcc32008-12-23 15:21:36 -05001667 /* Initialise the client representation from the parent server */
1668 nfs_server_copy_userdata(server, parent_server);
Trond Myklebust62ab460c2009-08-09 15:06:19 -04001669 server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
Chuck Lever542fcc32008-12-23 15:21:36 -05001670
David Howells54ceac42006-08-22 20:06:13 -04001671 /* Get a client representation.
1672 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001673 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001674 data->addr,
1675 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001676 parent_client->cl_ipaddr,
1677 data->authflavor,
1678 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001679 parent_server->client->cl_timeout,
Trond Myklebust97dc1352010-06-16 09:52:26 -04001680 parent_client->cl_mvops->minor_version);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001681 if (error < 0)
1682 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001683
Trond Myklebust33170232007-12-20 16:03:59 -05001684 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001685 if (error < 0)
1686 goto error;
1687
Trond Myklebust44950b62010-06-17 11:45:12 -04001688 error = nfs4_server_common_setup(server, mntfh);
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001689 if (error < 0)
1690 goto error;
1691
David Howells54ceac42006-08-22 20:06:13 -04001692 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1693 return server;
1694
1695error:
1696 nfs_free_server(server);
1697 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1698 return ERR_PTR(error);
1699}
1700
1701#endif /* CONFIG_NFS_V4 */
1702
1703/*
1704 * Clone an NFS2, NFS3 or NFS4 server record
1705 */
1706struct nfs_server *nfs_clone_server(struct nfs_server *source,
1707 struct nfs_fh *fh,
1708 struct nfs_fattr *fattr)
1709{
1710 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001711 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001712 int error;
1713
1714 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001715 (unsigned long long) fattr->fsid.major,
1716 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001717
1718 server = nfs_alloc_server();
1719 if (!server)
1720 return ERR_PTR(-ENOMEM);
1721
Trond Myklebustfbca7792010-04-16 16:22:46 -04001722 error = -ENOMEM;
1723 fattr_fsinfo = nfs_alloc_fattr();
1724 if (fattr_fsinfo == NULL)
1725 goto out_free_server;
1726
David Howells54ceac42006-08-22 20:06:13 -04001727 /* Copy data from the source */
1728 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001729 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001730 atomic_inc(&server->nfs_client->cl_count);
1731 nfs_server_copy_userdata(server, source);
1732
1733 server->fsid = fattr->fsid;
1734
Trond Myklebust33170232007-12-20 16:03:59 -05001735 error = nfs_init_server_rpcclient(server,
1736 source->client->cl_timeout,
1737 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001738 if (error < 0)
1739 goto out_free_server;
1740 if (!IS_ERR(source->client_acl))
1741 nfs_init_server_aclclient(server);
1742
1743 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001744 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001745 if (error < 0)
1746 goto out_free_server;
1747
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001748 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1749 server->namelen = NFS4_MAXNAMLEN;
1750
David Howells54ceac42006-08-22 20:06:13 -04001751 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001752 (unsigned long long) server->fsid.major,
1753 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001754
1755 error = nfs_start_lockd(server);
1756 if (error < 0)
1757 goto out_free_server;
1758
Chuck Leverfca52382010-12-24 01:32:32 +00001759 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001760 server->mount_time = jiffies;
1761
Trond Myklebustfbca7792010-04-16 16:22:46 -04001762 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001763 dprintk("<-- nfs_clone_server() = %p\n", server);
1764 return server;
1765
1766out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001767 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001768 nfs_free_server(server);
1769 dprintk("<-- nfs_clone_server() = error %d\n", error);
1770 return ERR_PTR(error);
1771}
David Howells6aaca562006-08-22 20:06:13 -04001772
1773#ifdef CONFIG_PROC_FS
1774static struct proc_dir_entry *proc_fs_nfs;
1775
1776static int nfs_server_list_open(struct inode *inode, struct file *file);
1777static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1778static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1779static void nfs_server_list_stop(struct seq_file *p, void *v);
1780static int nfs_server_list_show(struct seq_file *m, void *v);
1781
James Morris88e9d342009-09-22 16:43:43 -07001782static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001783 .start = nfs_server_list_start,
1784 .next = nfs_server_list_next,
1785 .stop = nfs_server_list_stop,
1786 .show = nfs_server_list_show,
1787};
1788
Arjan van de Ven00977a52007-02-12 00:55:34 -08001789static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001790 .open = nfs_server_list_open,
1791 .read = seq_read,
1792 .llseek = seq_lseek,
1793 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001794 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001795};
1796
1797static int nfs_volume_list_open(struct inode *inode, struct file *file);
1798static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1799static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1800static void nfs_volume_list_stop(struct seq_file *p, void *v);
1801static int nfs_volume_list_show(struct seq_file *m, void *v);
1802
James Morris88e9d342009-09-22 16:43:43 -07001803static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001804 .start = nfs_volume_list_start,
1805 .next = nfs_volume_list_next,
1806 .stop = nfs_volume_list_stop,
1807 .show = nfs_volume_list_show,
1808};
1809
Arjan van de Ven00977a52007-02-12 00:55:34 -08001810static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001811 .open = nfs_volume_list_open,
1812 .read = seq_read,
1813 .llseek = seq_lseek,
1814 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001815 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001816};
1817
1818/*
1819 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1820 * we're dealing
1821 */
1822static int nfs_server_list_open(struct inode *inode, struct file *file)
1823{
1824 struct seq_file *m;
1825 int ret;
1826
1827 ret = seq_open(file, &nfs_server_list_ops);
1828 if (ret < 0)
1829 return ret;
1830
1831 m = file->private_data;
1832 m->private = PDE(inode)->data;
1833
1834 return 0;
1835}
1836
1837/*
1838 * set up the iterator to start reading from the server list and return the first item
1839 */
1840static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1841{
David Howells6aaca562006-08-22 20:06:13 -04001842 /* lock the list against modification */
1843 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001844 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001845}
1846
1847/*
1848 * move to next server
1849 */
1850static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1851{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001852 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001853}
1854
1855/*
1856 * clean up after reading from the transports list
1857 */
1858static void nfs_server_list_stop(struct seq_file *p, void *v)
1859{
1860 spin_unlock(&nfs_client_lock);
1861}
1862
1863/*
1864 * display a header line followed by a load of call lines
1865 */
1866static int nfs_server_list_show(struct seq_file *m, void *v)
1867{
1868 struct nfs_client *clp;
1869
1870 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001871 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001872 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1873 return 0;
1874 }
1875
1876 /* display one transport per line on subsequent lines */
1877 clp = list_entry(v, struct nfs_client, cl_share_link);
1878
Malahal Naineni940aab42011-09-20 17:27:14 -07001879 /* Check if the client is initialized */
1880 if (clp->cl_cons_state != NFS_CS_READY)
1881 return 0;
1882
Chuck Lever5d8515c2007-12-10 14:57:16 -05001883 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001884 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001885 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1886 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001887 atomic_read(&clp->cl_count),
1888 clp->cl_hostname);
1889
1890 return 0;
1891}
1892
1893/*
1894 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1895 */
1896static int nfs_volume_list_open(struct inode *inode, struct file *file)
1897{
1898 struct seq_file *m;
1899 int ret;
1900
1901 ret = seq_open(file, &nfs_volume_list_ops);
1902 if (ret < 0)
1903 return ret;
1904
1905 m = file->private_data;
1906 m->private = PDE(inode)->data;
1907
1908 return 0;
1909}
1910
1911/*
1912 * set up the iterator to start reading from the volume list and return the first item
1913 */
1914static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1915{
David Howells6aaca562006-08-22 20:06:13 -04001916 /* lock the list against modification */
1917 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001918 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001919}
1920
1921/*
1922 * move to next volume
1923 */
1924static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1925{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001926 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001927}
1928
1929/*
1930 * clean up after reading from the transports list
1931 */
1932static void nfs_volume_list_stop(struct seq_file *p, void *v)
1933{
1934 spin_unlock(&nfs_client_lock);
1935}
1936
1937/*
1938 * display a header line followed by a load of call lines
1939 */
1940static int nfs_volume_list_show(struct seq_file *m, void *v)
1941{
1942 struct nfs_server *server;
1943 struct nfs_client *clp;
1944 char dev[8], fsid[17];
1945
1946 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001947 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001948 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001949 return 0;
1950 }
1951 /* display one transport per line on subsequent lines */
1952 server = list_entry(v, struct nfs_server, master_link);
1953 clp = server->nfs_client;
1954
1955 snprintf(dev, 8, "%u:%u",
1956 MAJOR(server->s_dev), MINOR(server->s_dev));
1957
1958 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001959 (unsigned long long) server->fsid.major,
1960 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001961
David Howells5d1acff2009-04-03 16:42:47 +01001962 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001963 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001964 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1965 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001966 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001967 fsid,
1968 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001969
1970 return 0;
1971}
1972
1973/*
1974 * initialise the /proc/fs/nfsfs/ directory
1975 */
1976int __init nfs_fs_proc_init(void)
1977{
1978 struct proc_dir_entry *p;
1979
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001980 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001981 if (!proc_fs_nfs)
1982 goto error_0;
1983
David Howells6aaca562006-08-22 20:06:13 -04001984 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001985 p = proc_create("servers", S_IFREG|S_IRUGO,
1986 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001987 if (!p)
1988 goto error_1;
1989
David Howells6aaca562006-08-22 20:06:13 -04001990 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001991 p = proc_create("volumes", S_IFREG|S_IRUGO,
1992 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001993 if (!p)
1994 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001995 return 0;
1996
1997error_2:
1998 remove_proc_entry("servers", proc_fs_nfs);
1999error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002000 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002001error_0:
2002 return -ENOMEM;
2003}
2004
2005/*
2006 * clean up the /proc/fs/nfsfs/ directory
2007 */
2008void nfs_fs_proc_exit(void)
2009{
2010 remove_proc_entry("volumes", proc_fs_nfs);
2011 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07002012 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04002013}
2014
2015#endif /* CONFIG_PROC_FS */
Trond Myklebustb064eca22011-02-22 15:44:32 -08002016
2017module_param(nfs4_disable_idmapping, bool, 0644);
2018MODULE_PARM_DESC(nfs4_disable_idmapping,
2019 "Turn off NFSv4 idmapping when using 'sec=sys'");