blob: 462de24482b47066ab5e364d4e1227a22bbe407c [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090038#include <linux/slab.h>
Al Viro40401532012-02-13 03:58:52 +000039#include <linux/idr.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050040#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040041#include <linux/nfs_xdr.h>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040042#include <linux/sunrpc/bc_xprt.h>
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000043#include <linux/nsproxy.h>
44#include <linux/pid_namespace.h>
David Howells24c8dbb2006-08-22 20:06:10 -040045
David Howells24c8dbb2006-08-22 20:06:10 -040046
47#include "nfs4_fs.h"
48#include "callback.h"
49#include "delegation.h"
50#include "iostat.h"
51#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010052#include "fscache.h"
Ricardo Labiaga85e174b2010-10-20 00:17:58 -040053#include "pnfs.h"
Bryan Schumakerab7017a2012-07-30 16:05:16 -040054#include "nfs.h"
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +000055#include "netns.h"
David Howells24c8dbb2006-08-22 20:06:10 -040056
57#define NFSDBG_FACILITY NFSDBG_CLIENT
58
David Howells24c8dbb2006-08-22 20:06:10 -040059static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
Bryan Schumakerab7017a2012-07-30 16:05:16 -040060static DEFINE_SPINLOCK(nfs_version_lock);
61static DEFINE_MUTEX(nfs_version_mutex);
62static LIST_HEAD(nfs_versions);
David Howells24c8dbb2006-08-22 20:06:10 -040063
64/*
David Howells5006a762006-08-22 20:06:12 -040065 * RPC cruft for NFS
66 */
Trond Myklebusta613fa12012-01-20 13:53:56 -050067static const struct rpc_version *nfs_version[5] = {
Bryan Schumakerab7017a2012-07-30 16:05:16 -040068 [2] = NULL,
69 [3] = NULL,
70 [4] = NULL,
David Howells5006a762006-08-22 20:06:12 -040071};
72
Trond Myklebusta613fa12012-01-20 13:53:56 -050073const struct rpc_program nfs_program = {
David Howells5006a762006-08-22 20:06:12 -040074 .name = "nfs",
75 .number = NFS_PROGRAM,
76 .nrvers = ARRAY_SIZE(nfs_version),
77 .version = nfs_version,
78 .stats = &nfs_rpcstat,
Jim Reesfe0a9b72011-07-30 20:52:42 -040079 .pipe_dir_name = NFS_PIPE_DIRNAME,
David Howells5006a762006-08-22 20:06:12 -040080};
81
82struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
84};
85
86
87#ifdef CONFIG_NFS_V3_ACL
88static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
Trond Myklebusta613fa12012-01-20 13:53:56 -050089static const struct rpc_version *nfsacl_version[] = {
David Howells5006a762006-08-22 20:06:12 -040090 [3] = &nfsacl_version3,
91};
92
Trond Myklebusta613fa12012-01-20 13:53:56 -050093const struct rpc_program nfsacl_program = {
David Howells5006a762006-08-22 20:06:12 -040094 .name = "nfsacl",
95 .number = NFS_ACL_PROGRAM,
96 .nrvers = ARRAY_SIZE(nfsacl_version),
97 .version = nfsacl_version,
98 .stats = &nfsacl_rpcstat,
99};
100#endif /* CONFIG_NFS_V3_ACL */
101
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400102static struct nfs_subversion *find_nfs_version(unsigned int version)
103{
104 struct nfs_subversion *nfs;
105 spin_lock(&nfs_version_lock);
106
107 list_for_each_entry(nfs, &nfs_versions, list) {
108 if (nfs->rpc_ops->version == version) {
109 spin_unlock(&nfs_version_lock);
110 return nfs;
111 }
112 };
113
114 spin_unlock(&nfs_version_lock);
115 return ERR_PTR(-EPROTONOSUPPORT);;
116}
117
118struct nfs_subversion *get_nfs_version(unsigned int version)
119{
120 struct nfs_subversion *nfs = find_nfs_version(version);
121
122 if (IS_ERR(nfs)) {
123 mutex_lock(&nfs_version_mutex);
124 request_module("nfs%d", version);
125 nfs = find_nfs_version(version);
126 mutex_unlock(&nfs_version_mutex);
127 }
128
129 if (!IS_ERR(nfs))
130 try_module_get(nfs->owner);
131 return nfs;
132}
133
134void put_nfs_version(struct nfs_subversion *nfs)
135{
136 module_put(nfs->owner);
137}
138
139void register_nfs_version(struct nfs_subversion *nfs)
140{
141 spin_lock(&nfs_version_lock);
142
143 list_add(&nfs->list, &nfs_versions);
144 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
145
146 spin_unlock(&nfs_version_lock);
147}
148EXPORT_SYMBOL_GPL(register_nfs_version);
149
150void unregister_nfs_version(struct nfs_subversion *nfs)
151{
152 spin_lock(&nfs_version_lock);
153
154 nfs_version[nfs->rpc_ops->version] = NULL;
155 list_del(&nfs->list);
156
157 spin_unlock(&nfs_version_lock);
158}
159EXPORT_SYMBOL_GPL(unregister_nfs_version);
160
161/*
162 * Preload all configured NFS versions during module init.
163 * This function should be edited after each protocol is converted,
164 * and eventually removed.
165 */
166int __init nfs_register_versions(void)
167{
168 int err = init_nfs_v2();
169 if (err)
170 return err;
171
172 err = init_nfs_v3();
173 if (err)
174 return err;
175
176 return init_nfs_v4();
177}
178
179/*
180 * Remove each pre-loaded NFS version
181 */
182void nfs_unregister_versions(void)
183{
184 exit_nfs_v2();
185 exit_nfs_v3();
186 exit_nfs_v4();
187}
188
David Howells5006a762006-08-22 20:06:12 -0400189/*
David Howells24c8dbb2006-08-22 20:06:10 -0400190 * Allocate a shared client record
191 *
192 * Since these are allocated/deallocated very rarely, we don't
193 * bother putting them in a slab cache...
194 */
Bryan Schumaker6663ee72012-06-20 15:53:46 -0400195struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400196{
197 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400198 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700199 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400200
201 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
202 goto error_0;
203
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400204 clp->cl_nfs_mod = cl_init->nfs_mod;
205 try_module_get(clp->cl_nfs_mod->owner);
206
207 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
Trond Myklebust40c553192007-12-14 14:56:07 -0500208
David Howells24c8dbb2006-08-22 20:06:10 -0400209 atomic_set(&clp->cl_count, 1);
210 clp->cl_cons_state = NFS_CS_INITING;
211
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500212 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
213 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400214
Trond Myklebust3a498022007-12-14 14:56:04 -0500215 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700216 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500217 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400218 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400219 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400220 }
221
222 INIT_LIST_HEAD(&clp->cl_superblocks);
223 clp->cl_rpcclient = ERR_PTR(-EINVAL);
224
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500225 clp->cl_proto = cl_init->proto;
Chuck Lever73ea6662012-05-21 22:44:50 -0400226 clp->cl_net = get_net(cl_init->net);
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500227
Trond Myklebust68c97152012-01-03 13:22:46 -0500228 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400229 if (!IS_ERR(cred))
230 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100231 nfs_fscache_get_client_cookie(clp);
232
David Howells24c8dbb2006-08-22 20:06:10 -0400233 return clp;
234
Benny Halevy71468512009-04-01 09:22:56 -0400235error_cleanup:
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400236 put_nfs_version(clp->cl_nfs_mod);
David Howells24c8dbb2006-08-22 20:06:10 -0400237 kfree(clp);
238error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700239 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400240}
241
Trond Myklebust5dd31772006-08-24 01:03:05 -0400242#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000243/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000244void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000245{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000246 struct nfs_net *nn = net_generic(net, nfs_net_id);
247
248 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000249}
250
251/* nfs_client_lock held */
252static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
253{
Chuck Lever73ea6662012-05-21 22:44:50 -0400254 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000255
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000256 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000257 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000258}
259
Fred Isamanf7e89172011-01-06 11:36:32 +0000260static void pnfs_init_server(struct nfs_server *server)
261{
262 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
263}
264
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800265#else
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000266void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000267{
268}
269
270static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
271{
272}
Fred Isamanf7e89172011-01-06 11:36:32 +0000273
274static void pnfs_init_server(struct nfs_server *server)
275{
276}
277
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800278#endif /* CONFIG_NFS_V4 */
279
280/*
David Howells24c8dbb2006-08-22 20:06:10 -0400281 * Destroy a shared client record
282 */
Bryan Schumakercdb7eced2012-06-20 15:53:45 -0400283void nfs_free_client(struct nfs_client *clp)
David Howells24c8dbb2006-08-22 20:06:10 -0400284{
Trond Myklebust40c553192007-12-14 14:56:07 -0500285 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400286
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
Chuck Lever73ea6662012-05-21 22:44:50 -0400296 put_net(clp->cl_net);
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400297 put_nfs_version(clp->cl_nfs_mod);
David Howells24c8dbb2006-08-22 20:06:10 -0400298 kfree(clp->cl_hostname);
299 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{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000309 struct nfs_net *nn;
310
David Howells27ba8512006-07-30 14:40:56 -0400311 if (!clp)
312 return;
313
David Howells24c8dbb2006-08-22 20:06:10 -0400314 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
Chuck Lever73ea6662012-05-21 22:44:50 -0400315 nn = net_generic(clp->cl_net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400316
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000317 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
David Howells24c8dbb2006-08-22 20:06:10 -0400318 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000319 nfs_cb_idr_remove_locked(clp);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000320 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400321
322 BUG_ON(!list_empty(&clp->cl_superblocks));
323
Bryan Schumakercdb7eced2012-06-20 15:53:45 -0400324 clp->rpc_ops->free_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400325 }
326}
Andy Adamson16b374c2010-10-20 00:18:04 -0400327EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400328
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500329#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400330/*
331 * Test if two ip6 socket addresses refer to the same socket by
332 * comparing relevant fields. The padding bytes specifically, are not
333 * compared. sin6_flowinfo is not compared because it only affects QoS
334 * and sin6_scope_id is only compared if the address is "link local"
335 * because "link local" addresses need only be unique to a specific
336 * link. Conversely, ordinary unicast addresses might have different
337 * sin6_scope_id.
338 *
339 * The caller should ensure both socket addresses are AF_INET6.
340 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400341static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
342 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400343{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400344 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
345 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400346
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800347 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400348 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800349 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
350 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500351
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800352 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500353}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400354#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
355static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
356 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400357{
358 return 0;
359}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500360#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500361
David Howells24c8dbb2006-08-22 20:06:10 -0400362/*
Ian Dalld7371c42009-03-10 20:33:22 -0400363 * Test if two ip4 socket addresses refer to the same socket, by
364 * comparing relevant fields. The padding bytes specifically, are
365 * not compared.
366 *
367 * The caller should ensure both socket addresses are AF_INET.
368 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400369static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
370 const struct sockaddr *sa2)
371{
372 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
373 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
374
375 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
376}
377
378static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
379 const struct sockaddr *sa2)
380{
381 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
382 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
383
384 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
385 (sin1->sin6_port == sin2->sin6_port);
386}
387
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400388static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
389 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400390{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400391 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
392 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400393
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400394 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
395 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400396}
397
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400398#if defined(CONFIG_NFS_V4_1)
Ian Dalld7371c42009-03-10 20:33:22 -0400399/*
Ian Dalld7371c42009-03-10 20:33:22 -0400400 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400401 * by comparing (only) relevant fields, excluding the port number.
402 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400403int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
404 const struct sockaddr *sa2)
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400405{
406 if (sa1->sa_family != sa2->sa_family)
407 return 0;
408
409 switch (sa1->sa_family) {
410 case AF_INET:
411 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
412 case AF_INET6:
413 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
414 }
415 return 0;
416}
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400417#endif /* CONFIG_NFS_V4_1 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400418
419/*
420 * Test if two socket addresses represent the same actual socket,
421 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400422 */
423static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
424 const struct sockaddr *sa2)
425{
426 if (sa1->sa_family != sa2->sa_family)
427 return 0;
428
429 switch (sa1->sa_family) {
430 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400431 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400432 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400433 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400434 }
435 return 0;
436}
437
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500438/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500439 * Find an nfs_client on the list that matches the initialisation data
440 * that is supplied.
441 */
442static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400443{
444 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400445 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000446 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400447
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000448 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400449 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700450 /* Don't match clients that failed to initialise properly */
451 if (clp->cl_cons_state < 0)
452 continue;
453
David Howells24c8dbb2006-08-22 20:06:10 -0400454 /* Different NFS versions cannot share the same nfs_client */
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400455 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400456 continue;
457
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500458 if (clp->cl_proto != data->proto)
459 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400460 /* Match nfsv4 minorversion */
461 if (clp->cl_minorversion != data->minorversion)
462 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500463 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400464 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400465 continue;
466
Trond Myklebustc81468a2007-12-14 14:56:05 -0500467 atomic_inc(&clp->cl_count);
468 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400469 }
David Howells24c8dbb2006-08-22 20:06:10 -0400470 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400471}
472
Trond Myklebust4697bd52012-05-23 13:24:36 -0400473static bool nfs_client_init_is_complete(const struct nfs_client *clp)
474{
475 return clp->cl_cons_state != NFS_CS_INITING;
476}
477
478int nfs_wait_client_init_complete(const struct nfs_client *clp)
479{
480 return wait_event_killable(nfs_client_active_wq,
481 nfs_client_init_is_complete(clp));
482}
483
David Howells24c8dbb2006-08-22 20:06:10 -0400484/*
Chuck Leverf4117032012-05-21 22:45:50 -0400485 * Found an existing client. Make sure it's ready before returning.
486 */
487static struct nfs_client *
488nfs_found_client(const struct nfs_client_initdata *cl_init,
489 struct nfs_client *clp)
490{
491 int error;
492
Trond Myklebust4697bd52012-05-23 13:24:36 -0400493 error = nfs_wait_client_init_complete(clp);
Chuck Leverf4117032012-05-21 22:45:50 -0400494 if (error < 0) {
495 nfs_put_client(clp);
496 return ERR_PTR(-ERESTARTSYS);
497 }
498
499 if (clp->cl_cons_state < NFS_CS_READY) {
500 error = clp->cl_cons_state;
501 nfs_put_client(clp);
502 return ERR_PTR(error);
503 }
504
Trond Myklebust54ac4712012-05-23 13:26:10 -0400505 smp_rmb();
506
Chuck Leverf4117032012-05-21 22:45:50 -0400507 dprintk("<-- %s found nfs_client %p for %s\n",
508 __func__, clp, cl_init->hostname ?: "");
509 return clp;
510}
511
512/*
David Howells24c8dbb2006-08-22 20:06:10 -0400513 * Look up a client by IP address and protocol version
514 * - creates a new record if one doesn't yet exist
515 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400516struct nfs_client *
Andy Adamson45a52a02011-03-01 01:34:08 +0000517nfs_get_client(const struct nfs_client_initdata *cl_init,
518 const struct rpc_timeout *timeparms,
519 const char *ip_addr,
Chuck Lever4bf590e2012-05-21 22:46:07 -0400520 rpc_authflavor_t authflavour)
David Howells24c8dbb2006-08-22 20:06:10 -0400521{
522 struct nfs_client *clp, *new = NULL;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000523 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400524 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
David Howells24c8dbb2006-08-22 20:06:10 -0400525
Chuck Leverd7422c42007-12-10 14:58:51 -0500526 dprintk("--> nfs_get_client(%s,v%u)\n",
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400527 cl_init->hostname ?: "", rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400528
529 /* see if the client already exists */
530 do {
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000531 spin_lock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400532
Trond Myklebustc81468a2007-12-14 14:56:05 -0500533 clp = nfs_match_client(cl_init);
Chuck Leverf4117032012-05-21 22:45:50 -0400534 if (clp) {
535 spin_unlock(&nn->nfs_client_lock);
536 if (new)
Bryan Schumakercdb7eced2012-06-20 15:53:45 -0400537 new->rpc_ops->free_client(new);
Chuck Leverf4117032012-05-21 22:45:50 -0400538 return nfs_found_client(cl_init, clp);
539 }
Chuck Lever8cab4c32012-05-21 22:45:59 -0400540 if (new) {
541 list_add(&new->cl_share_link, &nn->nfs_client_list);
542 spin_unlock(&nn->nfs_client_lock);
Chuck Lever4bf590e2012-05-21 22:46:07 -0400543 new->cl_flags = cl_init->init_flags;
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400544 return rpc_ops->init_client(new, timeparms, ip_addr,
545 authflavour);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400546 }
David Howells24c8dbb2006-08-22 20:06:10 -0400547
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000548 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400549
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400550 new = rpc_ops->alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700551 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400552
Chuck Leverf4117032012-05-21 22:45:50 -0400553 dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
554 cl_init->hostname ?: "", PTR_ERR(new));
Chuck Levera21bdd92009-06-17 18:02:10 -0700555 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400556}
557
558/*
559 * Mark a server as ready or failed
560 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400561void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400562{
Trond Myklebust54ac4712012-05-23 13:26:10 -0400563 smp_wmb();
David Howells24c8dbb2006-08-22 20:06:10 -0400564 clp->cl_cons_state = state;
565 wake_up_all(&nfs_client_active_wq);
566}
David Howells5006a762006-08-22 20:06:12 -0400567
568/*
569 * Initialise the timeout values for a connection
570 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400571void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
David Howells5006a762006-08-22 20:06:12 -0400572 unsigned int timeo, unsigned int retrans)
573{
574 to->to_initval = timeo * HZ / 10;
575 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400576
577 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400578 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400579 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400580 if (to->to_retries == 0)
581 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500582 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400583 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400584 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
585 to->to_initval = NFS_MAX_TCP_TIMEOUT;
586 to->to_increment = to->to_initval;
587 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500588 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
589 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
590 if (to->to_maxval < to->to_initval)
591 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400592 to->to_exponential = 0;
593 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400594 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400595 if (to->to_retries == 0)
596 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400597 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400598 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400599 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
600 to->to_initval = NFS_MAX_UDP_TIMEOUT;
601 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
602 to->to_exponential = 1;
603 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400604 default:
605 BUG();
David Howells5006a762006-08-22 20:06:12 -0400606 }
607}
608
609/*
610 * Create an RPC client handle
611 */
Bryan Schumaker428360d2012-07-16 16:39:17 -0400612int nfs_create_rpc_client(struct nfs_client *clp,
613 const struct rpc_timeout *timeparms,
614 rpc_authflavor_t flavor)
David Howells5006a762006-08-22 20:06:12 -0400615{
David Howells5006a762006-08-22 20:06:12 -0400616 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400617 struct rpc_create_args args = {
Chuck Lever73ea6662012-05-21 22:44:50 -0400618 .net = clp->cl_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500619 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400620 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500621 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500622 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400623 .servername = clp->cl_hostname,
624 .program = &nfs_program,
625 .version = clp->rpc_ops->version,
626 .authflavor = flavor,
627 };
David Howells5006a762006-08-22 20:06:12 -0400628
Chuck Lever4bf590e2012-05-21 22:46:07 -0400629 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500630 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
Chuck Lever4bf590e2012-05-21 22:46:07 -0400631 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500632 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
633
David Howells5006a762006-08-22 20:06:12 -0400634 if (!IS_ERR(clp->cl_rpcclient))
635 return 0;
636
Chuck Lever41877d22006-08-22 20:06:20 -0400637 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400638 if (IS_ERR(clnt)) {
639 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700640 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400641 return PTR_ERR(clnt);
642 }
643
David Howells5006a762006-08-22 20:06:12 -0400644 clp->cl_rpcclient = clnt;
645 return 0;
646}
David Howells54ceac42006-08-22 20:06:13 -0400647
648/*
649 * Version 2 or 3 client destruction
650 */
651static void nfs_destroy_server(struct nfs_server *server)
652{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400653 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
654 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500655 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400656}
657
658/*
659 * Version 2 or 3 lockd setup
660 */
661static int nfs_start_lockd(struct nfs_server *server)
662{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500663 struct nlm_host *host;
664 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500665 struct nlmclnt_initdata nlm_init = {
666 .hostname = clp->cl_hostname,
667 .address = (struct sockaddr *)&clp->cl_addr,
668 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500669 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500670 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
671 1 : 0,
Chuck Lever73ea6662012-05-21 22:44:50 -0400672 .net = clp->cl_net,
Chuck Lever883bb162008-01-15 16:04:20 -0500673 };
David Howells54ceac42006-08-22 20:06:13 -0400674
Chuck Lever883bb162008-01-15 16:04:20 -0500675 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500676 return 0;
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400677 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
678 (server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500679 return 0;
680
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400681 switch (clp->cl_proto) {
682 default:
683 nlm_init.protocol = IPPROTO_TCP;
684 break;
685 case XPRT_TRANSPORT_UDP:
686 nlm_init.protocol = IPPROTO_UDP;
687 }
688
Chuck Lever883bb162008-01-15 16:04:20 -0500689 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500690 if (IS_ERR(host))
691 return PTR_ERR(host);
692
693 server->nlm_host = host;
694 server->destroy = nfs_destroy_server;
695 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400696}
697
698/*
699 * Initialise an NFSv3 ACL client connection
700 */
701#ifdef CONFIG_NFS_V3_ACL
702static void nfs_init_server_aclclient(struct nfs_server *server)
703{
Trond Myklebust40c553192007-12-14 14:56:07 -0500704 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400705 goto out_noacl;
706 if (server->flags & NFS_MOUNT_NOACL)
707 goto out_noacl;
708
709 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
710 if (IS_ERR(server->client_acl))
711 goto out_noacl;
712
713 /* No errors! Assume that Sun nfsacls are supported */
714 server->caps |= NFS_CAP_ACLS;
715 return;
716
717out_noacl:
718 server->caps &= ~NFS_CAP_ACLS;
719}
720#else
721static inline void nfs_init_server_aclclient(struct nfs_server *server)
722{
723 server->flags &= ~NFS_MOUNT_NOACL;
724 server->caps &= ~NFS_CAP_ACLS;
725}
726#endif
727
728/*
729 * Create a general RPC client
730 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400731int nfs_init_server_rpcclient(struct nfs_server *server,
Trond Myklebust33170232007-12-20 16:03:59 -0500732 const struct rpc_timeout *timeo,
733 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400734{
735 struct nfs_client *clp = server->nfs_client;
736
737 server->client = rpc_clone_client(clp->cl_rpcclient);
738 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700739 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400740 return PTR_ERR(server->client);
741 }
742
Trond Myklebust33170232007-12-20 16:03:59 -0500743 memcpy(&server->client->cl_timeout_default,
744 timeo,
745 sizeof(server->client->cl_timeout_default));
746 server->client->cl_timeout = &server->client->cl_timeout_default;
747
David Howells54ceac42006-08-22 20:06:13 -0400748 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
749 struct rpc_auth *auth;
750
751 auth = rpcauth_create(pseudoflavour, server->client);
752 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700753 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400754 return PTR_ERR(auth);
755 }
756 }
757 server->client->cl_softrtry = 0;
758 if (server->flags & NFS_MOUNT_SOFT)
759 server->client->cl_softrtry = 1;
760
David Howells54ceac42006-08-22 20:06:13 -0400761 return 0;
762}
763
Chuck Lever8cab4c32012-05-21 22:45:59 -0400764/**
765 * nfs_init_client - Initialise an NFS2 or NFS3 client
766 *
767 * @clp: nfs_client to initialise
768 * @timeparms: timeout parameters for underlying RPC transport
769 * @ip_addr: IP presentation address (not used)
770 * @authflavor: authentication flavor for underlying RPC transport
Chuck Lever8cab4c32012-05-21 22:45:59 -0400771 *
772 * Returns pointer to an NFS client, or an ERR_PTR value.
David Howells54ceac42006-08-22 20:06:13 -0400773 */
Chuck Lever8cab4c32012-05-21 22:45:59 -0400774struct nfs_client *nfs_init_client(struct nfs_client *clp,
775 const struct rpc_timeout *timeparms,
Chuck Lever4bf590e2012-05-21 22:46:07 -0400776 const char *ip_addr, rpc_authflavor_t authflavour)
David Howells54ceac42006-08-22 20:06:13 -0400777{
David Howells54ceac42006-08-22 20:06:13 -0400778 int error;
779
780 if (clp->cl_cons_state == NFS_CS_READY) {
781 /* the client is already initialised */
782 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400783 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400784 }
785
David Howells54ceac42006-08-22 20:06:13 -0400786 /*
787 * Create a client RPC handle for doing FSSTAT with UNIX auth only
788 * - RFC 2623, sec 2.3.2
789 */
Chuck Lever4bf590e2012-05-21 22:46:07 -0400790 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
David Howells54ceac42006-08-22 20:06:13 -0400791 if (error < 0)
792 goto error;
793 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400794 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400795
796error:
797 nfs_mark_client_ready(clp, error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400798 nfs_put_client(clp);
David Howells54ceac42006-08-22 20:06:13 -0400799 dprintk("<-- nfs_init_client() = xerror %d\n", error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400800 return ERR_PTR(error);
David Howells54ceac42006-08-22 20:06:13 -0400801}
802
803/*
804 * Create a version 2 or 3 client
805 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400806static int nfs_init_server(struct nfs_server *server,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400807 const struct nfs_parsed_mount_data *data,
808 struct nfs_subversion *nfs_mod)
David Howells54ceac42006-08-22 20:06:13 -0400809{
Trond Myklebust3a498022007-12-14 14:56:04 -0500810 struct nfs_client_initdata cl_init = {
811 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500812 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500813 .addrlen = data->nfs_server.addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400814 .nfs_mod = nfs_mod,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500815 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400816 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500817 };
Trond Myklebust33170232007-12-20 16:03:59 -0500818 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400819 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500820 int error;
David Howells54ceac42006-08-22 20:06:13 -0400821
822 dprintk("--> nfs_init_server()\n");
823
Andy Adamson45a52a02011-03-01 01:34:08 +0000824 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
825 data->timeo, data->retrans);
Chuck Lever4bf590e2012-05-21 22:46:07 -0400826 if (data->flags & NFS_MOUNT_NORESVPORT)
827 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Andy Adamson45a52a02011-03-01 01:34:08 +0000828
David Howells54ceac42006-08-22 20:06:13 -0400829 /* Allocate or find a client reference we can use */
Chuck Lever4bf590e2012-05-21 22:46:07 -0400830 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX);
David Howells54ceac42006-08-22 20:06:13 -0400831 if (IS_ERR(clp)) {
832 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
833 return PTR_ERR(clp);
834 }
835
David Howells54ceac42006-08-22 20:06:13 -0400836 server->nfs_client = clp;
837
838 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400839 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100840 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400841 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
842 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400843 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME|NFS_CAP_CHANGE_ATTR;
David Howells54ceac42006-08-22 20:06:13 -0400844
845 if (data->rsize)
846 server->rsize = nfs_block_size(data->rsize, NULL);
847 if (data->wsize)
848 server->wsize = nfs_block_size(data->wsize, NULL);
849
850 server->acregmin = data->acregmin * HZ;
851 server->acregmax = data->acregmax * HZ;
852 server->acdirmin = data->acdirmin * HZ;
853 server->acdirmax = data->acdirmax * HZ;
854
855 /* Start lockd here, before we might error out */
856 error = nfs_start_lockd(server);
857 if (error < 0)
858 goto error;
859
Chuck Leverf22d6d72008-03-14 14:10:22 -0400860 server->port = data->nfs_server.port;
861
Trond Myklebust33170232007-12-20 16:03:59 -0500862 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400863 if (error < 0)
864 goto error;
865
Chuck Lever3f8400d2008-03-14 14:10:30 -0400866 /* Preserve the values of mount_server-related mount options */
867 if (data->mount_server.addrlen) {
868 memcpy(&server->mountd_address, &data->mount_server.address,
869 data->mount_server.addrlen);
870 server->mountd_addrlen = data->mount_server.addrlen;
871 }
872 server->mountd_version = data->mount_server.version;
873 server->mountd_port = data->mount_server.port;
874 server->mountd_protocol = data->mount_server.protocol;
875
David Howells54ceac42006-08-22 20:06:13 -0400876 server->namelen = data->namlen;
877 /* Create a client RPC handle for the NFSv3 ACL management interface */
878 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400879 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
880 return 0;
881
882error:
883 server->nfs_client = NULL;
884 nfs_put_client(clp);
885 dprintk("<-- nfs_init_server() = xerror %d\n", error);
886 return error;
887}
888
889/*
890 * Load up the server record from information gained in an fsinfo record
891 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400892static void nfs_server_set_fsinfo(struct nfs_server *server,
893 struct nfs_fh *mntfh,
894 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400895{
896 unsigned long max_rpc_payload;
897
898 /* Work out a lot of parameters */
899 if (server->rsize == 0)
900 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
901 if (server->wsize == 0)
902 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
903
904 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
905 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
906 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
907 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
908
909 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
910 if (server->rsize > max_rpc_payload)
911 server->rsize = max_rpc_payload;
912 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
913 server->rsize = NFS_MAX_FILE_IO_SIZE;
914 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700915
Jens Axboed9938312009-06-12 14:45:52 +0200916 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400917 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
918
919 if (server->wsize > max_rpc_payload)
920 server->wsize = max_rpc_payload;
921 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
922 server->wsize = NFS_MAX_FILE_IO_SIZE;
923 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400924 server->pnfs_blksize = fsinfo->blksize;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400925
David Howells54ceac42006-08-22 20:06:13 -0400926 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
927
928 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400929 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
930 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400931 if (server->dtsize > server->rsize)
932 server->dtsize = server->rsize;
933
934 if (server->flags & NFS_MOUNT_NOAC) {
935 server->acregmin = server->acregmax = 0;
936 server->acdirmin = server->acdirmax = 0;
937 }
938
939 server->maxfilesize = fsinfo->maxfilesize;
940
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700941 server->time_delta = fsinfo->time_delta;
942
David Howells54ceac42006-08-22 20:06:13 -0400943 /* We're airborne Set socket buffersize */
944 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
945}
946
947/*
948 * Probe filesystem information, including the FSID on v2/v3
949 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400950int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
David Howells54ceac42006-08-22 20:06:13 -0400951{
952 struct nfs_fsinfo fsinfo;
953 struct nfs_client *clp = server->nfs_client;
954 int error;
955
956 dprintk("--> nfs_probe_fsinfo()\n");
957
958 if (clp->rpc_ops->set_capabilities != NULL) {
959 error = clp->rpc_ops->set_capabilities(server, mntfh);
960 if (error < 0)
961 goto out_error;
962 }
963
964 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400965 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400966 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
967 if (error < 0)
968 goto out_error;
969
Benny Halevy738fd0f32011-07-30 20:52:36 -0400970 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -0400971
972 /* Get some general file system info */
973 if (server->namelen == 0) {
974 struct nfs_pathconf pathinfo;
975
976 pathinfo.fattr = fattr;
977 nfs_fattr_init(fattr);
978
979 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
980 server->namelen = pathinfo.max_namelen;
981 }
982
983 dprintk("<-- nfs_probe_fsinfo() = 0\n");
984 return 0;
985
986out_error:
987 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
988 return error;
989}
990
991/*
992 * Copy useful information when duplicating a server record
993 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400994void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
David Howells54ceac42006-08-22 20:06:13 -0400995{
996 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400997 target->rsize = source->rsize;
998 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400999 target->acregmin = source->acregmin;
1000 target->acregmax = source->acregmax;
1001 target->acdirmin = source->acdirmin;
1002 target->acdirmax = source->acdirmax;
1003 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -04001004 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -04001005}
1006
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001007void nfs_server_insert_lists(struct nfs_server *server)
Chuck Leverfca52382010-12-24 01:32:32 +00001008{
1009 struct nfs_client *clp = server->nfs_client;
Chuck Lever73ea6662012-05-21 22:44:50 -04001010 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +00001011
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001012 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001013 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001014 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001015 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001016 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001017
1018}
1019
1020static void nfs_server_remove_lists(struct nfs_server *server)
1021{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001022 struct nfs_client *clp = server->nfs_client;
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001023 struct nfs_net *nn;
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001024
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001025 if (clp == NULL)
1026 return;
Chuck Lever73ea6662012-05-21 22:44:50 -04001027 nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001028 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001029 list_del_rcu(&server->client_link);
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001030 if (list_empty(&clp->cl_superblocks))
Andy Adamsond3b4c9d2011-03-01 01:34:10 +00001031 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +00001032 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001033 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +00001034
1035 synchronize_rcu();
1036}
1037
David Howells54ceac42006-08-22 20:06:13 -04001038/*
1039 * Allocate and initialise a server record
1040 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -04001041struct nfs_server *nfs_alloc_server(void)
David Howells54ceac42006-08-22 20:06:13 -04001042{
1043 struct nfs_server *server;
1044
1045 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
1046 if (!server)
1047 return NULL;
1048
1049 server->client = server->client_acl = ERR_PTR(-EINVAL);
1050
1051 /* Zero out the NFS state stuff */
1052 INIT_LIST_HEAD(&server->client_link);
1053 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001054 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001055 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001056 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001057
Steve Dicksonef818a22007-11-08 04:05:04 -05001058 atomic_set(&server->active, 0);
1059
David Howells54ceac42006-08-22 20:06:13 -04001060 server->io_stats = nfs_alloc_iostats();
1061 if (!server->io_stats) {
1062 kfree(server);
1063 return NULL;
1064 }
1065
Jens Axboe48d07642009-09-21 09:59:39 +02001066 if (bdi_init(&server->backing_dev_info)) {
1067 nfs_free_iostats(server->io_stats);
1068 kfree(server);
1069 return NULL;
1070 }
1071
Trond Myklebust9157c312012-01-17 22:04:24 -05001072 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001073 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001074 pnfs_init_server(server);
1075
David Howells54ceac42006-08-22 20:06:13 -04001076 return server;
1077}
1078
1079/*
1080 * Free up a server record
1081 */
1082void nfs_free_server(struct nfs_server *server)
1083{
1084 dprintk("--> nfs_free_server()\n");
1085
Chuck Leverfca52382010-12-24 01:32:32 +00001086 nfs_server_remove_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001087
1088 if (server->destroy != NULL)
1089 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001090
1091 if (!IS_ERR(server->client_acl))
1092 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001093 if (!IS_ERR(server->client))
1094 rpc_shutdown_client(server->client);
1095
1096 nfs_put_client(server->nfs_client);
1097
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001098 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c312012-01-17 22:04:24 -05001099 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001100 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001101 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001102 kfree(server);
1103 nfs_release_automount_timer();
1104 dprintk("<-- nfs_free_server()\n");
1105}
1106
1107/*
1108 * Create a version 2 or 3 volume record
1109 * - keyed on server and FSID
1110 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001111struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
Bryan Schumakerab7017a2012-07-30 16:05:16 -04001112 struct nfs_fh *mntfh,
1113 struct nfs_subversion *nfs_mod)
David Howells54ceac42006-08-22 20:06:13 -04001114{
1115 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001116 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001117 int error;
1118
1119 server = nfs_alloc_server();
1120 if (!server)
1121 return ERR_PTR(-ENOMEM);
1122
Trond Myklebustfbca7792010-04-16 16:22:46 -04001123 error = -ENOMEM;
1124 fattr = nfs_alloc_fattr();
1125 if (fattr == NULL)
1126 goto error;
1127
David Howells54ceac42006-08-22 20:06:13 -04001128 /* Get a client representation */
Bryan Schumakerab7017a2012-07-30 16:05:16 -04001129 error = nfs_init_server(server, data, nfs_mod);
David Howells54ceac42006-08-22 20:06:13 -04001130 if (error < 0)
1131 goto error;
1132
1133 BUG_ON(!server->nfs_client);
1134 BUG_ON(!server->nfs_client->rpc_ops);
1135 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1136
1137 /* Probe the root fh to retrieve its FSID */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001138 error = nfs_probe_fsinfo(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001139 if (error < 0)
1140 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001141 if (server->nfs_client->rpc_ops->version == 3) {
1142 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1143 server->namelen = NFS3_MAXNAMLEN;
1144 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1145 server->caps |= NFS_CAP_READDIRPLUS;
1146 } else {
1147 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1148 server->namelen = NFS2_MAXNAMLEN;
1149 }
1150
Trond Myklebustfbca7792010-04-16 16:22:46 -04001151 if (!(fattr->valid & NFS_ATTR_FATTR)) {
1152 error = server->nfs_client->rpc_ops->getattr(server, mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001153 if (error < 0) {
1154 dprintk("nfs_create_server: getattr error = %d\n", -error);
1155 goto error;
1156 }
1157 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001158 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001159
David Howells6daabf12006-08-24 15:44:16 -04001160 dprintk("Server FSID: %llx:%llx\n",
1161 (unsigned long long) server->fsid.major,
1162 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001163
Chuck Leverfca52382010-12-24 01:32:32 +00001164 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001165 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001166 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001167 return server;
1168
1169error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001170 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001171 nfs_free_server(server);
1172 return ERR_PTR(error);
1173}
1174
David Howells54ceac42006-08-22 20:06:13 -04001175/*
1176 * Clone an NFS2, NFS3 or NFS4 server record
1177 */
1178struct nfs_server *nfs_clone_server(struct nfs_server *source,
1179 struct nfs_fh *fh,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001180 struct nfs_fattr *fattr,
1181 rpc_authflavor_t flavor)
David Howells54ceac42006-08-22 20:06:13 -04001182{
1183 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001184 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001185 int error;
1186
1187 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001188 (unsigned long long) fattr->fsid.major,
1189 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001190
1191 server = nfs_alloc_server();
1192 if (!server)
1193 return ERR_PTR(-ENOMEM);
1194
Trond Myklebustfbca7792010-04-16 16:22:46 -04001195 error = -ENOMEM;
1196 fattr_fsinfo = nfs_alloc_fattr();
1197 if (fattr_fsinfo == NULL)
1198 goto out_free_server;
1199
David Howells54ceac42006-08-22 20:06:13 -04001200 /* Copy data from the source */
1201 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001202 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001203 atomic_inc(&server->nfs_client->cl_count);
1204 nfs_server_copy_userdata(server, source);
1205
1206 server->fsid = fattr->fsid;
1207
Trond Myklebust33170232007-12-20 16:03:59 -05001208 error = nfs_init_server_rpcclient(server,
1209 source->client->cl_timeout,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001210 flavor);
David Howells54ceac42006-08-22 20:06:13 -04001211 if (error < 0)
1212 goto out_free_server;
1213 if (!IS_ERR(source->client_acl))
1214 nfs_init_server_aclclient(server);
1215
1216 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001217 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001218 if (error < 0)
1219 goto out_free_server;
1220
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001221 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1222 server->namelen = NFS4_MAXNAMLEN;
1223
David Howells54ceac42006-08-22 20:06:13 -04001224 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001225 (unsigned long long) server->fsid.major,
1226 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001227
1228 error = nfs_start_lockd(server);
1229 if (error < 0)
1230 goto out_free_server;
1231
Chuck Leverfca52382010-12-24 01:32:32 +00001232 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001233 server->mount_time = jiffies;
1234
Trond Myklebustfbca7792010-04-16 16:22:46 -04001235 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001236 dprintk("<-- nfs_clone_server() = %p\n", server);
1237 return server;
1238
1239out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001240 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001241 nfs_free_server(server);
1242 dprintk("<-- nfs_clone_server() = error %d\n", error);
1243 return ERR_PTR(error);
1244}
David Howells6aaca562006-08-22 20:06:13 -04001245
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001246void nfs_clients_init(struct net *net)
1247{
1248 struct nfs_net *nn = net_generic(net, nfs_net_id);
1249
1250 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001251 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001252#ifdef CONFIG_NFS_V4
1253 idr_init(&nn->cb_ident_idr);
1254#endif
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001255 spin_lock_init(&nn->nfs_client_lock);
Chuck Leverf0920752012-05-21 22:45:41 -04001256 nn->boot_time = CURRENT_TIME;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001257}
1258
David Howells6aaca562006-08-22 20:06:13 -04001259#ifdef CONFIG_PROC_FS
1260static struct proc_dir_entry *proc_fs_nfs;
1261
1262static int nfs_server_list_open(struct inode *inode, struct file *file);
1263static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1264static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1265static void nfs_server_list_stop(struct seq_file *p, void *v);
1266static int nfs_server_list_show(struct seq_file *m, void *v);
1267
James Morris88e9d342009-09-22 16:43:43 -07001268static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001269 .start = nfs_server_list_start,
1270 .next = nfs_server_list_next,
1271 .stop = nfs_server_list_stop,
1272 .show = nfs_server_list_show,
1273};
1274
Arjan van de Ven00977a52007-02-12 00:55:34 -08001275static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001276 .open = nfs_server_list_open,
1277 .read = seq_read,
1278 .llseek = seq_lseek,
1279 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001280 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001281};
1282
1283static int nfs_volume_list_open(struct inode *inode, struct file *file);
1284static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1285static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1286static void nfs_volume_list_stop(struct seq_file *p, void *v);
1287static int nfs_volume_list_show(struct seq_file *m, void *v);
1288
James Morris88e9d342009-09-22 16:43:43 -07001289static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001290 .start = nfs_volume_list_start,
1291 .next = nfs_volume_list_next,
1292 .stop = nfs_volume_list_stop,
1293 .show = nfs_volume_list_show,
1294};
1295
Arjan van de Ven00977a52007-02-12 00:55:34 -08001296static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001297 .open = nfs_volume_list_open,
1298 .read = seq_read,
1299 .llseek = seq_lseek,
1300 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001301 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001302};
1303
1304/*
1305 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1306 * we're dealing
1307 */
1308static int nfs_server_list_open(struct inode *inode, struct file *file)
1309{
1310 struct seq_file *m;
1311 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001312 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1313 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001314
1315 ret = seq_open(file, &nfs_server_list_ops);
1316 if (ret < 0)
1317 return ret;
1318
1319 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001320 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001321
1322 return 0;
1323}
1324
1325/*
1326 * set up the iterator to start reading from the server list and return the first item
1327 */
1328static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1329{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001330 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1331
David Howells6aaca562006-08-22 20:06:13 -04001332 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001333 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001334 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001335}
1336
1337/*
1338 * move to next server
1339 */
1340static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1341{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001342 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1343
1344 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001345}
1346
1347/*
1348 * clean up after reading from the transports list
1349 */
1350static void nfs_server_list_stop(struct seq_file *p, void *v)
1351{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001352 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1353
1354 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001355}
1356
1357/*
1358 * display a header line followed by a load of call lines
1359 */
1360static int nfs_server_list_show(struct seq_file *m, void *v)
1361{
1362 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001363 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001364
1365 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001366 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001367 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1368 return 0;
1369 }
1370
1371 /* display one transport per line on subsequent lines */
1372 clp = list_entry(v, struct nfs_client, cl_share_link);
1373
Malahal Naineni940aab42011-09-20 17:27:14 -07001374 /* Check if the client is initialized */
1375 if (clp->cl_cons_state != NFS_CS_READY)
1376 return 0;
1377
Trond Myklebust2446ab62012-03-01 17:00:56 -05001378 rcu_read_lock();
Chuck Lever5d8515c2007-12-10 14:57:16 -05001379 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001380 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001381 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1382 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001383 atomic_read(&clp->cl_count),
1384 clp->cl_hostname);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001385 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001386
1387 return 0;
1388}
1389
1390/*
1391 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1392 */
1393static int nfs_volume_list_open(struct inode *inode, struct file *file)
1394{
1395 struct seq_file *m;
1396 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001397 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1398 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001399
1400 ret = seq_open(file, &nfs_volume_list_ops);
1401 if (ret < 0)
1402 return ret;
1403
1404 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001405 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001406
1407 return 0;
1408}
1409
1410/*
1411 * set up the iterator to start reading from the volume list and return the first item
1412 */
1413static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1414{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001415 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1416
David Howells6aaca562006-08-22 20:06:13 -04001417 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001418 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001419 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001420}
1421
1422/*
1423 * move to next volume
1424 */
1425static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1426{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001427 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1428
1429 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001430}
1431
1432/*
1433 * clean up after reading from the transports list
1434 */
1435static void nfs_volume_list_stop(struct seq_file *p, void *v)
1436{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001437 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1438
1439 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001440}
1441
1442/*
1443 * display a header line followed by a load of call lines
1444 */
1445static int nfs_volume_list_show(struct seq_file *m, void *v)
1446{
1447 struct nfs_server *server;
1448 struct nfs_client *clp;
1449 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001450 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001451
1452 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001453 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001454 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001455 return 0;
1456 }
1457 /* display one transport per line on subsequent lines */
1458 server = list_entry(v, struct nfs_server, master_link);
1459 clp = server->nfs_client;
1460
1461 snprintf(dev, 8, "%u:%u",
1462 MAJOR(server->s_dev), MINOR(server->s_dev));
1463
1464 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001465 (unsigned long long) server->fsid.major,
1466 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001467
Trond Myklebust2446ab62012-03-01 17:00:56 -05001468 rcu_read_lock();
David Howells5d1acff2009-04-03 16:42:47 +01001469 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001470 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001471 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1472 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001473 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001474 fsid,
1475 nfs_server_fscache_state(server));
Trond Myklebust2446ab62012-03-01 17:00:56 -05001476 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001477
1478 return 0;
1479}
1480
1481/*
1482 * initialise the /proc/fs/nfsfs/ directory
1483 */
1484int __init nfs_fs_proc_init(void)
1485{
1486 struct proc_dir_entry *p;
1487
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001488 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001489 if (!proc_fs_nfs)
1490 goto error_0;
1491
David Howells6aaca562006-08-22 20:06:13 -04001492 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001493 p = proc_create("servers", S_IFREG|S_IRUGO,
1494 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001495 if (!p)
1496 goto error_1;
1497
David Howells6aaca562006-08-22 20:06:13 -04001498 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001499 p = proc_create("volumes", S_IFREG|S_IRUGO,
1500 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001501 if (!p)
1502 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001503 return 0;
1504
1505error_2:
1506 remove_proc_entry("servers", proc_fs_nfs);
1507error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001508 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001509error_0:
1510 return -ENOMEM;
1511}
1512
1513/*
1514 * clean up the /proc/fs/nfsfs/ directory
1515 */
1516void nfs_fs_proc_exit(void)
1517{
1518 remove_proc_entry("volumes", proc_fs_nfs);
1519 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001520 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001521}
1522
1523#endif /* CONFIG_PROC_FS */