blob: 8687b6b6edc160d69725d5f24dd198526c3f184b [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
Bryan Schumakerab7017a2012-07-30 16:05:16 -040086static struct nfs_subversion *find_nfs_version(unsigned int version)
87{
88 struct nfs_subversion *nfs;
89 spin_lock(&nfs_version_lock);
90
91 list_for_each_entry(nfs, &nfs_versions, list) {
92 if (nfs->rpc_ops->version == version) {
93 spin_unlock(&nfs_version_lock);
94 return nfs;
95 }
96 };
97
98 spin_unlock(&nfs_version_lock);
99 return ERR_PTR(-EPROTONOSUPPORT);;
100}
101
102struct nfs_subversion *get_nfs_version(unsigned int version)
103{
104 struct nfs_subversion *nfs = find_nfs_version(version);
105
106 if (IS_ERR(nfs)) {
107 mutex_lock(&nfs_version_mutex);
108 request_module("nfs%d", version);
109 nfs = find_nfs_version(version);
110 mutex_unlock(&nfs_version_mutex);
111 }
112
113 if (!IS_ERR(nfs))
114 try_module_get(nfs->owner);
115 return nfs;
116}
117
118void put_nfs_version(struct nfs_subversion *nfs)
119{
120 module_put(nfs->owner);
121}
122
123void register_nfs_version(struct nfs_subversion *nfs)
124{
125 spin_lock(&nfs_version_lock);
126
127 list_add(&nfs->list, &nfs_versions);
128 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
129
130 spin_unlock(&nfs_version_lock);
131}
132EXPORT_SYMBOL_GPL(register_nfs_version);
133
134void unregister_nfs_version(struct nfs_subversion *nfs)
135{
136 spin_lock(&nfs_version_lock);
137
138 nfs_version[nfs->rpc_ops->version] = NULL;
139 list_del(&nfs->list);
140
141 spin_unlock(&nfs_version_lock);
142}
143EXPORT_SYMBOL_GPL(unregister_nfs_version);
144
145/*
146 * Preload all configured NFS versions during module init.
147 * This function should be edited after each protocol is converted,
148 * and eventually removed.
149 */
150int __init nfs_register_versions(void)
151{
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400152 return init_nfs_v4();
153}
154
155/*
156 * Remove each pre-loaded NFS version
157 */
158void nfs_unregister_versions(void)
159{
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400160 exit_nfs_v4();
161}
162
David Howells5006a762006-08-22 20:06:12 -0400163/*
David Howells24c8dbb2006-08-22 20:06:10 -0400164 * Allocate a shared client record
165 *
166 * Since these are allocated/deallocated very rarely, we don't
167 * bother putting them in a slab cache...
168 */
Bryan Schumaker6663ee72012-06-20 15:53:46 -0400169struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400170{
171 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400172 struct rpc_cred *cred;
Chuck Levera21bdd92009-06-17 18:02:10 -0700173 int err = -ENOMEM;
David Howells24c8dbb2006-08-22 20:06:10 -0400174
175 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
176 goto error_0;
177
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400178 clp->cl_nfs_mod = cl_init->nfs_mod;
179 try_module_get(clp->cl_nfs_mod->owner);
180
181 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
Trond Myklebust40c553192007-12-14 14:56:07 -0500182
David Howells24c8dbb2006-08-22 20:06:10 -0400183 atomic_set(&clp->cl_count, 1);
184 clp->cl_cons_state = NFS_CS_INITING;
185
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500186 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
187 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400188
Trond Myklebust3a498022007-12-14 14:56:04 -0500189 if (cl_init->hostname) {
Chuck Levera21bdd92009-06-17 18:02:10 -0700190 err = -ENOMEM;
Trond Myklebust3a498022007-12-14 14:56:04 -0500191 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400192 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400193 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400194 }
195
196 INIT_LIST_HEAD(&clp->cl_superblocks);
197 clp->cl_rpcclient = ERR_PTR(-EINVAL);
198
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500199 clp->cl_proto = cl_init->proto;
Chuck Lever73ea6662012-05-21 22:44:50 -0400200 clp->cl_net = get_net(cl_init->net);
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500201
Trond Myklebust68c97152012-01-03 13:22:46 -0500202 cred = rpc_lookup_machine_cred("*");
Trond Myklebust7c67db32008-04-07 20:50:11 -0400203 if (!IS_ERR(cred))
204 clp->cl_machine_cred = cred;
David Howells14727282009-04-03 16:42:42 +0100205 nfs_fscache_get_client_cookie(clp);
206
David Howells24c8dbb2006-08-22 20:06:10 -0400207 return clp;
208
Benny Halevy71468512009-04-01 09:22:56 -0400209error_cleanup:
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400210 put_nfs_version(clp->cl_nfs_mod);
David Howells24c8dbb2006-08-22 20:06:10 -0400211 kfree(clp);
212error_0:
Chuck Levera21bdd92009-06-17 18:02:10 -0700213 return ERR_PTR(err);
David Howells24c8dbb2006-08-22 20:06:10 -0400214}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400215EXPORT_SYMBOL_GPL(nfs_alloc_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400216
Trond Myklebust5dd31772006-08-24 01:03:05 -0400217#ifdef CONFIG_NFS_V4
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000218/* idr_remove_all is not needed as all id's are removed by nfs_put_client */
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000219void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000220{
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000221 struct nfs_net *nn = net_generic(net, nfs_net_id);
222
223 idr_destroy(&nn->cb_ident_idr);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000224}
225
226/* nfs_client_lock held */
227static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
228{
Chuck Lever73ea6662012-05-21 22:44:50 -0400229 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000230
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000231 if (clp->cl_cb_ident)
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000232 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000233}
234
Fred Isamanf7e89172011-01-06 11:36:32 +0000235static void pnfs_init_server(struct nfs_server *server)
236{
237 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
238}
239
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800240#else
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +0000241void nfs_cleanup_cb_ident_idr(struct net *net)
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000242{
243}
244
245static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
246{
247}
Fred Isamanf7e89172011-01-06 11:36:32 +0000248
249static void pnfs_init_server(struct nfs_server *server)
250{
251}
252
Alexandros Batsakis888ef2e2010-02-05 03:45:03 -0800253#endif /* CONFIG_NFS_V4 */
254
255/*
David Howells24c8dbb2006-08-22 20:06:10 -0400256 * Destroy a shared client record
257 */
Bryan Schumakercdb7ece2012-06-20 15:53:45 -0400258void nfs_free_client(struct nfs_client *clp)
David Howells24c8dbb2006-08-22 20:06:10 -0400259{
Trond Myklebust40c553192007-12-14 14:56:07 -0500260 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400261
David Howells14727282009-04-03 16:42:42 +0100262 nfs_fscache_release_client_cookie(clp);
263
David Howells24c8dbb2006-08-22 20:06:10 -0400264 /* -EIO all pending I/O */
265 if (!IS_ERR(clp->cl_rpcclient))
266 rpc_shutdown_client(clp->cl_rpcclient);
267
Trond Myklebust7c67db32008-04-07 20:50:11 -0400268 if (clp->cl_machine_cred != NULL)
269 put_rpccred(clp->cl_machine_cred);
270
Chuck Lever73ea6662012-05-21 22:44:50 -0400271 put_net(clp->cl_net);
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400272 put_nfs_version(clp->cl_nfs_mod);
David Howells24c8dbb2006-08-22 20:06:10 -0400273 kfree(clp->cl_hostname);
274 kfree(clp);
275
276 dprintk("<-- nfs_free_client()\n");
277}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400278EXPORT_SYMBOL_GPL(nfs_free_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400279
280/*
281 * Release a reference to a shared client record
282 */
283void nfs_put_client(struct nfs_client *clp)
284{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000285 struct nfs_net *nn;
286
David Howells27ba8512006-07-30 14:40:56 -0400287 if (!clp)
288 return;
289
David Howells24c8dbb2006-08-22 20:06:10 -0400290 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
Chuck Lever73ea6662012-05-21 22:44:50 -0400291 nn = net_generic(clp->cl_net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400292
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000293 if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
David Howells24c8dbb2006-08-22 20:06:10 -0400294 list_del(&clp->cl_share_link);
Andy Adamsonf4eecd52011-01-06 02:04:30 +0000295 nfs_cb_idr_remove_locked(clp);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000296 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400297
298 BUG_ON(!list_empty(&clp->cl_superblocks));
299
Bryan Schumakercdb7ece2012-06-20 15:53:45 -0400300 clp->rpc_ops->free_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400301 }
302}
Andy Adamson16b374c2010-10-20 00:18:04 -0400303EXPORT_SYMBOL_GPL(nfs_put_client);
David Howells24c8dbb2006-08-22 20:06:10 -0400304
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500305#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400306/*
307 * Test if two ip6 socket addresses refer to the same socket by
308 * comparing relevant fields. The padding bytes specifically, are not
309 * compared. sin6_flowinfo is not compared because it only affects QoS
310 * and sin6_scope_id is only compared if the address is "link local"
311 * because "link local" addresses need only be unique to a specific
312 * link. Conversely, ordinary unicast addresses might have different
313 * sin6_scope_id.
314 *
315 * The caller should ensure both socket addresses are AF_INET6.
316 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400317static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400319{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400320 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
321 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400322
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800323 if (!ipv6_addr_equal(&sin1->sin6_addr, &sin2->sin6_addr))
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400324 return 0;
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800325 else if (ipv6_addr_type(&sin1->sin6_addr) & IPV6_ADDR_LINKLOCAL)
326 return sin1->sin6_scope_id == sin2->sin6_scope_id;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500327
Mi Jinlongb9dd3ab2011-10-12 15:09:34 +0800328 return 1;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500329}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400330#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
331static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
332 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400333{
334 return 0;
335}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500336#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500337
David Howells24c8dbb2006-08-22 20:06:10 -0400338/*
Ian Dalld7371c42009-03-10 20:33:22 -0400339 * Test if two ip4 socket addresses refer to the same socket, by
340 * comparing relevant fields. The padding bytes specifically, are
341 * not compared.
342 *
343 * The caller should ensure both socket addresses are AF_INET.
344 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400345static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
346 const struct sockaddr *sa2)
347{
348 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
349 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
350
351 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
352}
353
354static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
355 const struct sockaddr *sa2)
356{
357 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
358 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
359
360 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
361 (sin1->sin6_port == sin2->sin6_port);
362}
363
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400364static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
365 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400366{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400367 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
368 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400369
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400370 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
371 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400372}
373
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400374#if defined(CONFIG_NFS_V4_1)
Ian Dalld7371c42009-03-10 20:33:22 -0400375/*
Ian Dalld7371c42009-03-10 20:33:22 -0400376 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400377 * by comparing (only) relevant fields, excluding the port number.
378 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400379int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
380 const struct sockaddr *sa2)
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400381{
382 if (sa1->sa_family != sa2->sa_family)
383 return 0;
384
385 switch (sa1->sa_family) {
386 case AF_INET:
387 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
388 case AF_INET6:
389 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
390 }
391 return 0;
392}
Bryan Schumaker4b7c8dd2012-03-12 11:28:24 -0400393#endif /* CONFIG_NFS_V4_1 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400394
395/*
396 * Test if two socket addresses represent the same actual socket,
397 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400398 */
399static int nfs_sockaddr_cmp(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:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400407 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400408 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400409 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400410 }
411 return 0;
412}
413
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500414/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500415 * Find an nfs_client on the list that matches the initialisation data
416 * that is supplied.
417 */
418static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400419{
420 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400421 const struct sockaddr *sap = data->addr;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000422 struct nfs_net *nn = net_generic(data->net, nfs_net_id);
David Howells24c8dbb2006-08-22 20:06:10 -0400423
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000424 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400425 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700426 /* Don't match clients that failed to initialise properly */
427 if (clp->cl_cons_state < 0)
428 continue;
429
David Howells24c8dbb2006-08-22 20:06:10 -0400430 /* Different NFS versions cannot share the same nfs_client */
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400431 if (clp->rpc_ops != data->nfs_mod->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400432 continue;
433
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500434 if (clp->cl_proto != data->proto)
435 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400436 /* Match nfsv4 minorversion */
437 if (clp->cl_minorversion != data->minorversion)
438 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500439 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400440 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400441 continue;
442
Trond Myklebustc81468a2007-12-14 14:56:05 -0500443 atomic_inc(&clp->cl_count);
444 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400445 }
David Howells24c8dbb2006-08-22 20:06:10 -0400446 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400447}
448
Trond Myklebust4697bd52012-05-23 13:24:36 -0400449static bool nfs_client_init_is_complete(const struct nfs_client *clp)
450{
451 return clp->cl_cons_state != NFS_CS_INITING;
452}
453
454int nfs_wait_client_init_complete(const struct nfs_client *clp)
455{
456 return wait_event_killable(nfs_client_active_wq,
457 nfs_client_init_is_complete(clp));
458}
459
David Howells24c8dbb2006-08-22 20:06:10 -0400460/*
Chuck Leverf4117032012-05-21 22:45:50 -0400461 * Found an existing client. Make sure it's ready before returning.
462 */
463static struct nfs_client *
464nfs_found_client(const struct nfs_client_initdata *cl_init,
465 struct nfs_client *clp)
466{
467 int error;
468
Trond Myklebust4697bd52012-05-23 13:24:36 -0400469 error = nfs_wait_client_init_complete(clp);
Chuck Leverf4117032012-05-21 22:45:50 -0400470 if (error < 0) {
471 nfs_put_client(clp);
472 return ERR_PTR(-ERESTARTSYS);
473 }
474
475 if (clp->cl_cons_state < NFS_CS_READY) {
476 error = clp->cl_cons_state;
477 nfs_put_client(clp);
478 return ERR_PTR(error);
479 }
480
Trond Myklebust54ac4712012-05-23 13:26:10 -0400481 smp_rmb();
482
Chuck Leverf4117032012-05-21 22:45:50 -0400483 dprintk("<-- %s found nfs_client %p for %s\n",
484 __func__, clp, cl_init->hostname ?: "");
485 return clp;
486}
487
488/*
David Howells24c8dbb2006-08-22 20:06:10 -0400489 * Look up a client by IP address and protocol version
490 * - creates a new record if one doesn't yet exist
491 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400492struct nfs_client *
Andy Adamson45a52a02011-03-01 01:34:08 +0000493nfs_get_client(const struct nfs_client_initdata *cl_init,
494 const struct rpc_timeout *timeparms,
495 const char *ip_addr,
Chuck Lever4bf590e2012-05-21 22:46:07 -0400496 rpc_authflavor_t authflavour)
David Howells24c8dbb2006-08-22 20:06:10 -0400497{
498 struct nfs_client *clp, *new = NULL;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +0000499 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400500 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
David Howells24c8dbb2006-08-22 20:06:10 -0400501
Chuck Leverd7422c42007-12-10 14:58:51 -0500502 dprintk("--> nfs_get_client(%s,v%u)\n",
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400503 cl_init->hostname ?: "", rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400504
505 /* see if the client already exists */
506 do {
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000507 spin_lock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400508
Trond Myklebustc81468a2007-12-14 14:56:05 -0500509 clp = nfs_match_client(cl_init);
Chuck Leverf4117032012-05-21 22:45:50 -0400510 if (clp) {
511 spin_unlock(&nn->nfs_client_lock);
512 if (new)
Bryan Schumakercdb7ece2012-06-20 15:53:45 -0400513 new->rpc_ops->free_client(new);
Chuck Leverf4117032012-05-21 22:45:50 -0400514 return nfs_found_client(cl_init, clp);
515 }
Chuck Lever8cab4c32012-05-21 22:45:59 -0400516 if (new) {
517 list_add(&new->cl_share_link, &nn->nfs_client_list);
518 spin_unlock(&nn->nfs_client_lock);
Chuck Lever4bf590e2012-05-21 22:46:07 -0400519 new->cl_flags = cl_init->init_flags;
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400520 return rpc_ops->init_client(new, timeparms, ip_addr,
521 authflavour);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400522 }
David Howells24c8dbb2006-08-22 20:06:10 -0400523
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000524 spin_unlock(&nn->nfs_client_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400525
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400526 new = rpc_ops->alloc_client(cl_init);
Chuck Levera21bdd92009-06-17 18:02:10 -0700527 } while (!IS_ERR(new));
David Howells24c8dbb2006-08-22 20:06:10 -0400528
Chuck Leverf4117032012-05-21 22:45:50 -0400529 dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
530 cl_init->hostname ?: "", PTR_ERR(new));
Chuck Levera21bdd92009-06-17 18:02:10 -0700531 return new;
David Howells24c8dbb2006-08-22 20:06:10 -0400532}
533
534/*
535 * Mark a server as ready or failed
536 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400537void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400538{
Trond Myklebust54ac4712012-05-23 13:26:10 -0400539 smp_wmb();
David Howells24c8dbb2006-08-22 20:06:10 -0400540 clp->cl_cons_state = state;
541 wake_up_all(&nfs_client_active_wq);
542}
David Howells5006a762006-08-22 20:06:12 -0400543
544/*
545 * Initialise the timeout values for a connection
546 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400547void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
David Howells5006a762006-08-22 20:06:12 -0400548 unsigned int timeo, unsigned int retrans)
549{
550 to->to_initval = timeo * HZ / 10;
551 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400552
553 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400554 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400555 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400556 if (to->to_retries == 0)
557 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500558 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400559 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400560 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
561 to->to_initval = NFS_MAX_TCP_TIMEOUT;
562 to->to_increment = to->to_initval;
563 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500564 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
565 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
566 if (to->to_maxval < to->to_initval)
567 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400568 to->to_exponential = 0;
569 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400570 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400571 if (to->to_retries == 0)
572 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400573 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400574 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400575 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
576 to->to_initval = NFS_MAX_UDP_TIMEOUT;
577 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
578 to->to_exponential = 1;
579 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400580 default:
581 BUG();
David Howells5006a762006-08-22 20:06:12 -0400582 }
583}
584
585/*
586 * Create an RPC client handle
587 */
Bryan Schumaker428360d2012-07-16 16:39:17 -0400588int nfs_create_rpc_client(struct nfs_client *clp,
589 const struct rpc_timeout *timeparms,
590 rpc_authflavor_t flavor)
David Howells5006a762006-08-22 20:06:12 -0400591{
David Howells5006a762006-08-22 20:06:12 -0400592 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400593 struct rpc_create_args args = {
Chuck Lever73ea6662012-05-21 22:44:50 -0400594 .net = clp->cl_net,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500595 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400596 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500597 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500598 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400599 .servername = clp->cl_hostname,
600 .program = &nfs_program,
601 .version = clp->rpc_ops->version,
602 .authflavor = flavor,
603 };
David Howells5006a762006-08-22 20:06:12 -0400604
Chuck Lever4bf590e2012-05-21 22:46:07 -0400605 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500606 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
Chuck Lever4bf590e2012-05-21 22:46:07 -0400607 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500608 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
609
David Howells5006a762006-08-22 20:06:12 -0400610 if (!IS_ERR(clp->cl_rpcclient))
611 return 0;
612
Chuck Lever41877d22006-08-22 20:06:20 -0400613 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400614 if (IS_ERR(clnt)) {
615 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700616 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400617 return PTR_ERR(clnt);
618 }
619
David Howells5006a762006-08-22 20:06:12 -0400620 clp->cl_rpcclient = clnt;
621 return 0;
622}
David Howells54ceac42006-08-22 20:06:13 -0400623
624/*
625 * Version 2 or 3 client destruction
626 */
627static void nfs_destroy_server(struct nfs_server *server)
628{
Suresh Jayaraman5eebde22010-09-23 08:55:58 -0400629 if (!(server->flags & NFS_MOUNT_LOCAL_FLOCK) ||
630 !(server->flags & NFS_MOUNT_LOCAL_FCNTL))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500631 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400632}
633
634/*
635 * Version 2 or 3 lockd setup
636 */
637static int nfs_start_lockd(struct nfs_server *server)
638{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500639 struct nlm_host *host;
640 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500641 struct nlmclnt_initdata nlm_init = {
642 .hostname = clp->cl_hostname,
643 .address = (struct sockaddr *)&clp->cl_addr,
644 .addrlen = clp->cl_addrlen,
Chuck Lever883bb162008-01-15 16:04:20 -0500645 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500646 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
647 1 : 0,
Chuck Lever73ea6662012-05-21 22:44:50 -0400648 .net = clp->cl_net,
Chuck Lever883bb162008-01-15 16:04:20 -0500649 };
David Howells54ceac42006-08-22 20:06:13 -0400650
Chuck Lever883bb162008-01-15 16:04:20 -0500651 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500652 return 0;
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 return 0;
656
Trond Myklebust8a6e5de2009-09-23 14:36:37 -0400657 switch (clp->cl_proto) {
658 default:
659 nlm_init.protocol = IPPROTO_TCP;
660 break;
661 case XPRT_TRANSPORT_UDP:
662 nlm_init.protocol = IPPROTO_UDP;
663 }
664
Chuck Lever883bb162008-01-15 16:04:20 -0500665 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500666 if (IS_ERR(host))
667 return PTR_ERR(host);
668
669 server->nlm_host = host;
670 server->destroy = nfs_destroy_server;
671 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400672}
673
674/*
David Howells54ceac42006-08-22 20:06:13 -0400675 * Create a general RPC client
676 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400677int nfs_init_server_rpcclient(struct nfs_server *server,
Trond Myklebust33170232007-12-20 16:03:59 -0500678 const struct rpc_timeout *timeo,
679 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400680{
681 struct nfs_client *clp = server->nfs_client;
682
683 server->client = rpc_clone_client(clp->cl_rpcclient);
684 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700685 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400686 return PTR_ERR(server->client);
687 }
688
Trond Myklebust33170232007-12-20 16:03:59 -0500689 memcpy(&server->client->cl_timeout_default,
690 timeo,
691 sizeof(server->client->cl_timeout_default));
692 server->client->cl_timeout = &server->client->cl_timeout_default;
693
David Howells54ceac42006-08-22 20:06:13 -0400694 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
695 struct rpc_auth *auth;
696
697 auth = rpcauth_create(pseudoflavour, server->client);
698 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700699 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400700 return PTR_ERR(auth);
701 }
702 }
703 server->client->cl_softrtry = 0;
704 if (server->flags & NFS_MOUNT_SOFT)
705 server->client->cl_softrtry = 1;
706
David Howells54ceac42006-08-22 20:06:13 -0400707 return 0;
708}
709
Chuck Lever8cab4c32012-05-21 22:45:59 -0400710/**
711 * nfs_init_client - Initialise an NFS2 or NFS3 client
712 *
713 * @clp: nfs_client to initialise
714 * @timeparms: timeout parameters for underlying RPC transport
715 * @ip_addr: IP presentation address (not used)
716 * @authflavor: authentication flavor for underlying RPC transport
Chuck Lever8cab4c32012-05-21 22:45:59 -0400717 *
718 * Returns pointer to an NFS client, or an ERR_PTR value.
David Howells54ceac42006-08-22 20:06:13 -0400719 */
Chuck Lever8cab4c32012-05-21 22:45:59 -0400720struct nfs_client *nfs_init_client(struct nfs_client *clp,
721 const struct rpc_timeout *timeparms,
Chuck Lever4bf590e2012-05-21 22:46:07 -0400722 const char *ip_addr, rpc_authflavor_t authflavour)
David Howells54ceac42006-08-22 20:06:13 -0400723{
David Howells54ceac42006-08-22 20:06:13 -0400724 int error;
725
726 if (clp->cl_cons_state == NFS_CS_READY) {
727 /* the client is already initialised */
728 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400729 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400730 }
731
David Howells54ceac42006-08-22 20:06:13 -0400732 /*
733 * Create a client RPC handle for doing FSSTAT with UNIX auth only
734 * - RFC 2623, sec 2.3.2
735 */
Chuck Lever4bf590e2012-05-21 22:46:07 -0400736 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
David Howells54ceac42006-08-22 20:06:13 -0400737 if (error < 0)
738 goto error;
739 nfs_mark_client_ready(clp, NFS_CS_READY);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400740 return clp;
David Howells54ceac42006-08-22 20:06:13 -0400741
742error:
743 nfs_mark_client_ready(clp, error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400744 nfs_put_client(clp);
David Howells54ceac42006-08-22 20:06:13 -0400745 dprintk("<-- nfs_init_client() = xerror %d\n", error);
Chuck Lever8cab4c32012-05-21 22:45:59 -0400746 return ERR_PTR(error);
David Howells54ceac42006-08-22 20:06:13 -0400747}
Bryan Schumakerddda8e02012-07-30 16:05:23 -0400748EXPORT_SYMBOL_GPL(nfs_init_client);
David Howells54ceac42006-08-22 20:06:13 -0400749
750/*
751 * Create a version 2 or 3 client
752 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400753static int nfs_init_server(struct nfs_server *server,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400754 const struct nfs_parsed_mount_data *data,
755 struct nfs_subversion *nfs_mod)
David Howells54ceac42006-08-22 20:06:13 -0400756{
Trond Myklebust3a498022007-12-14 14:56:04 -0500757 struct nfs_client_initdata cl_init = {
758 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500759 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500760 .addrlen = data->nfs_server.addrlen,
Bryan Schumakerab7017a2012-07-30 16:05:16 -0400761 .nfs_mod = nfs_mod,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500762 .proto = data->nfs_server.protocol,
Stanislav Kinsburskye50a7a12012-01-10 16:12:46 +0400763 .net = data->net,
Trond Myklebust3a498022007-12-14 14:56:04 -0500764 };
Trond Myklebust33170232007-12-20 16:03:59 -0500765 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400766 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500767 int error;
David Howells54ceac42006-08-22 20:06:13 -0400768
769 dprintk("--> nfs_init_server()\n");
770
Andy Adamson45a52a02011-03-01 01:34:08 +0000771 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
772 data->timeo, data->retrans);
Chuck Lever4bf590e2012-05-21 22:46:07 -0400773 if (data->flags & NFS_MOUNT_NORESVPORT)
774 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
Andy Adamson45a52a02011-03-01 01:34:08 +0000775
David Howells54ceac42006-08-22 20:06:13 -0400776 /* Allocate or find a client reference we can use */
Chuck Lever4bf590e2012-05-21 22:46:07 -0400777 clp = nfs_get_client(&cl_init, &timeparms, NULL, RPC_AUTH_UNIX);
David Howells54ceac42006-08-22 20:06:13 -0400778 if (IS_ERR(clp)) {
779 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
780 return PTR_ERR(clp);
781 }
782
David Howells54ceac42006-08-22 20:06:13 -0400783 server->nfs_client = clp;
784
785 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400786 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100787 server->options = data->options;
Trond Myklebust62ab4602009-08-09 15:06:19 -0400788 server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
789 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
Trond Myklebust3a1556e2012-04-27 13:48:18 -0400790 NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME|NFS_CAP_CHANGE_ATTR;
David Howells54ceac42006-08-22 20:06:13 -0400791
792 if (data->rsize)
793 server->rsize = nfs_block_size(data->rsize, NULL);
794 if (data->wsize)
795 server->wsize = nfs_block_size(data->wsize, NULL);
796
797 server->acregmin = data->acregmin * HZ;
798 server->acregmax = data->acregmax * HZ;
799 server->acdirmin = data->acdirmin * HZ;
800 server->acdirmax = data->acdirmax * HZ;
801
802 /* Start lockd here, before we might error out */
803 error = nfs_start_lockd(server);
804 if (error < 0)
805 goto error;
806
Chuck Leverf22d6d72008-03-14 14:10:22 -0400807 server->port = data->nfs_server.port;
808
Trond Myklebust33170232007-12-20 16:03:59 -0500809 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400810 if (error < 0)
811 goto error;
812
Chuck Lever3f8400d2008-03-14 14:10:30 -0400813 /* Preserve the values of mount_server-related mount options */
814 if (data->mount_server.addrlen) {
815 memcpy(&server->mountd_address, &data->mount_server.address,
816 data->mount_server.addrlen);
817 server->mountd_addrlen = data->mount_server.addrlen;
818 }
819 server->mountd_version = data->mount_server.version;
820 server->mountd_port = data->mount_server.port;
821 server->mountd_protocol = data->mount_server.protocol;
822
David Howells54ceac42006-08-22 20:06:13 -0400823 server->namelen = data->namlen;
David Howells54ceac42006-08-22 20:06:13 -0400824 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
825 return 0;
826
827error:
828 server->nfs_client = NULL;
829 nfs_put_client(clp);
830 dprintk("<-- nfs_init_server() = xerror %d\n", error);
831 return error;
832}
833
834/*
835 * Load up the server record from information gained in an fsinfo record
836 */
Benny Halevy738fd0f32011-07-30 20:52:36 -0400837static void nfs_server_set_fsinfo(struct nfs_server *server,
838 struct nfs_fh *mntfh,
839 struct nfs_fsinfo *fsinfo)
David Howells54ceac42006-08-22 20:06:13 -0400840{
841 unsigned long max_rpc_payload;
842
843 /* Work out a lot of parameters */
844 if (server->rsize == 0)
845 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
846 if (server->wsize == 0)
847 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
848
849 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
850 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
851 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
852 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
853
854 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
855 if (server->rsize > max_rpc_payload)
856 server->rsize = max_rpc_payload;
857 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
858 server->rsize = NFS_MAX_FILE_IO_SIZE;
859 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700860
Jens Axboed9938312009-06-12 14:45:52 +0200861 server->backing_dev_info.name = "nfs";
David Howells54ceac42006-08-22 20:06:13 -0400862 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
863
864 if (server->wsize > max_rpc_payload)
865 server->wsize = max_rpc_payload;
866 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
867 server->wsize = NFS_MAX_FILE_IO_SIZE;
868 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Fred Isamandae100c2011-07-30 20:52:37 -0400869 server->pnfs_blksize = fsinfo->blksize;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400870
David Howells54ceac42006-08-22 20:06:13 -0400871 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
872
873 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -0400874 if (server->dtsize > PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES)
875 server->dtsize = PAGE_CACHE_SIZE * NFS_MAX_READDIR_PAGES;
David Howells54ceac42006-08-22 20:06:13 -0400876 if (server->dtsize > server->rsize)
877 server->dtsize = server->rsize;
878
879 if (server->flags & NFS_MOUNT_NOAC) {
880 server->acregmin = server->acregmax = 0;
881 server->acdirmin = server->acdirmax = 0;
882 }
883
884 server->maxfilesize = fsinfo->maxfilesize;
885
Ricardo Labiaga6b967242010-10-12 16:30:05 -0700886 server->time_delta = fsinfo->time_delta;
887
David Howells54ceac42006-08-22 20:06:13 -0400888 /* We're airborne Set socket buffersize */
889 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
890}
891
892/*
893 * Probe filesystem information, including the FSID on v2/v3
894 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400895int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
David Howells54ceac42006-08-22 20:06:13 -0400896{
897 struct nfs_fsinfo fsinfo;
898 struct nfs_client *clp = server->nfs_client;
899 int error;
900
901 dprintk("--> nfs_probe_fsinfo()\n");
902
903 if (clp->rpc_ops->set_capabilities != NULL) {
904 error = clp->rpc_ops->set_capabilities(server, mntfh);
905 if (error < 0)
906 goto out_error;
907 }
908
909 fsinfo.fattr = fattr;
Ricardo Labiaga85e174b2010-10-20 00:17:58 -0400910 fsinfo.layouttype = 0;
David Howells54ceac42006-08-22 20:06:13 -0400911 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
912 if (error < 0)
913 goto out_error;
914
Benny Halevy738fd0f32011-07-30 20:52:36 -0400915 nfs_server_set_fsinfo(server, mntfh, &fsinfo);
David Howells54ceac42006-08-22 20:06:13 -0400916
917 /* Get some general file system info */
918 if (server->namelen == 0) {
919 struct nfs_pathconf pathinfo;
920
921 pathinfo.fattr = fattr;
922 nfs_fattr_init(fattr);
923
924 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
925 server->namelen = pathinfo.max_namelen;
926 }
927
928 dprintk("<-- nfs_probe_fsinfo() = 0\n");
929 return 0;
930
931out_error:
932 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
933 return error;
934}
935
936/*
937 * Copy useful information when duplicating a server record
938 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400939void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
David Howells54ceac42006-08-22 20:06:13 -0400940{
941 target->flags = source->flags;
Chuck Lever356e76b2010-04-22 15:35:56 -0400942 target->rsize = source->rsize;
943 target->wsize = source->wsize;
David Howells54ceac42006-08-22 20:06:13 -0400944 target->acregmin = source->acregmin;
945 target->acregmax = source->acregmax;
946 target->acdirmin = source->acdirmin;
947 target->acdirmax = source->acdirmax;
948 target->caps = source->caps;
David Howells2df54802009-09-23 14:36:39 -0400949 target->options = source->options;
David Howells54ceac42006-08-22 20:06:13 -0400950}
951
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400952void nfs_server_insert_lists(struct nfs_server *server)
Chuck Leverfca52382010-12-24 01:32:32 +0000953{
954 struct nfs_client *clp = server->nfs_client;
Chuck Lever73ea6662012-05-21 22:44:50 -0400955 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Chuck Leverfca52382010-12-24 01:32:32 +0000956
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000957 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +0000958 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +0000959 list_add_tail(&server->master_link, &nn->nfs_volume_list);
Andy Adamsond3b4c9d2011-03-01 01:34:10 +0000960 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000961 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +0000962
963}
964
965static void nfs_server_remove_lists(struct nfs_server *server)
966{
Andy Adamsond3b4c9d2011-03-01 01:34:10 +0000967 struct nfs_client *clp = server->nfs_client;
Trond Myklebust4c03ae42012-02-07 00:05:11 -0500968 struct nfs_net *nn;
Andy Adamsond3b4c9d2011-03-01 01:34:10 +0000969
Trond Myklebust4c03ae42012-02-07 00:05:11 -0500970 if (clp == NULL)
971 return;
Chuck Lever73ea6662012-05-21 22:44:50 -0400972 nn = net_generic(clp->cl_net, nfs_net_id);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000973 spin_lock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +0000974 list_del_rcu(&server->client_link);
Trond Myklebust4c03ae42012-02-07 00:05:11 -0500975 if (list_empty(&clp->cl_superblocks))
Andy Adamsond3b4c9d2011-03-01 01:34:10 +0000976 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
Chuck Leverfca52382010-12-24 01:32:32 +0000977 list_del(&server->master_link);
Stanislav Kinsburskydc030852012-01-23 17:26:31 +0000978 spin_unlock(&nn->nfs_client_lock);
Chuck Leverfca52382010-12-24 01:32:32 +0000979
980 synchronize_rcu();
981}
982
David Howells54ceac42006-08-22 20:06:13 -0400983/*
984 * Allocate and initialise a server record
985 */
Bryan Schumakerfcf10392012-07-16 16:39:18 -0400986struct nfs_server *nfs_alloc_server(void)
David Howells54ceac42006-08-22 20:06:13 -0400987{
988 struct nfs_server *server;
989
990 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
991 if (!server)
992 return NULL;
993
994 server->client = server->client_acl = ERR_PTR(-EINVAL);
995
996 /* Zero out the NFS state stuff */
997 INIT_LIST_HEAD(&server->client_link);
998 INIT_LIST_HEAD(&server->master_link);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000999 INIT_LIST_HEAD(&server->delegations);
Weston Andros Adamson6382a442011-06-01 16:44:44 -04001000 INIT_LIST_HEAD(&server->layouts);
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001001 INIT_LIST_HEAD(&server->state_owners_lru);
David Howells54ceac42006-08-22 20:06:13 -04001002
Steve Dicksonef818a22007-11-08 04:05:04 -05001003 atomic_set(&server->active, 0);
1004
David Howells54ceac42006-08-22 20:06:13 -04001005 server->io_stats = nfs_alloc_iostats();
1006 if (!server->io_stats) {
1007 kfree(server);
1008 return NULL;
1009 }
1010
Jens Axboe48d07642009-09-21 09:59:39 +02001011 if (bdi_init(&server->backing_dev_info)) {
1012 nfs_free_iostats(server->io_stats);
1013 kfree(server);
1014 return NULL;
1015 }
1016
Trond Myklebust9157c31d2012-01-17 22:04:24 -05001017 ida_init(&server->openowner_id);
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001018 ida_init(&server->lockowner_id);
Fred Isamanf7e89172011-01-06 11:36:32 +00001019 pnfs_init_server(server);
1020
David Howells54ceac42006-08-22 20:06:13 -04001021 return server;
1022}
1023
1024/*
1025 * Free up a server record
1026 */
1027void nfs_free_server(struct nfs_server *server)
1028{
1029 dprintk("--> nfs_free_server()\n");
1030
Chuck Leverfca52382010-12-24 01:32:32 +00001031 nfs_server_remove_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001032
1033 if (server->destroy != NULL)
1034 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001035
1036 if (!IS_ERR(server->client_acl))
1037 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001038 if (!IS_ERR(server->client))
1039 rpc_shutdown_client(server->client);
1040
1041 nfs_put_client(server->nfs_client);
1042
Trond Myklebustd2d7ce22012-01-17 22:04:25 -05001043 ida_destroy(&server->lockowner_id);
Trond Myklebust9157c31d2012-01-17 22:04:24 -05001044 ida_destroy(&server->openowner_id);
David Howells54ceac42006-08-22 20:06:13 -04001045 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001046 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001047 kfree(server);
1048 nfs_release_automount_timer();
1049 dprintk("<-- nfs_free_server()\n");
1050}
1051
1052/*
1053 * Create a version 2 or 3 volume record
1054 * - keyed on server and FSID
1055 */
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001056struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
Bryan Schumakerab7017a2012-07-30 16:05:16 -04001057 struct nfs_subversion *nfs_mod)
David Howells54ceac42006-08-22 20:06:13 -04001058{
1059 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001060 struct nfs_fattr *fattr;
David Howells54ceac42006-08-22 20:06:13 -04001061 int error;
1062
1063 server = nfs_alloc_server();
1064 if (!server)
1065 return ERR_PTR(-ENOMEM);
1066
Trond Myklebustfbca7792010-04-16 16:22:46 -04001067 error = -ENOMEM;
1068 fattr = nfs_alloc_fattr();
1069 if (fattr == NULL)
1070 goto error;
1071
David Howells54ceac42006-08-22 20:06:13 -04001072 /* Get a client representation */
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001073 error = nfs_init_server(server, mount_info->parsed, nfs_mod);
David Howells54ceac42006-08-22 20:06:13 -04001074 if (error < 0)
1075 goto error;
1076
1077 BUG_ON(!server->nfs_client);
1078 BUG_ON(!server->nfs_client->rpc_ops);
1079 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1080
1081 /* Probe the root fh to retrieve its FSID */
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001082 error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001083 if (error < 0)
1084 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001085 if (server->nfs_client->rpc_ops->version == 3) {
1086 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1087 server->namelen = NFS3_MAXNAMLEN;
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001088 if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001089 server->caps |= NFS_CAP_READDIRPLUS;
1090 } else {
1091 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1092 server->namelen = NFS2_MAXNAMLEN;
1093 }
1094
Trond Myklebustfbca7792010-04-16 16:22:46 -04001095 if (!(fattr->valid & NFS_ATTR_FATTR)) {
Bryan Schumaker1179acc2012-07-30 16:05:19 -04001096 error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr);
David Howells54ceac42006-08-22 20:06:13 -04001097 if (error < 0) {
1098 dprintk("nfs_create_server: getattr error = %d\n", -error);
1099 goto error;
1100 }
1101 }
Trond Myklebustfbca7792010-04-16 16:22:46 -04001102 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
David Howells54ceac42006-08-22 20:06:13 -04001103
David Howells6daabf12006-08-24 15:44:16 -04001104 dprintk("Server FSID: %llx:%llx\n",
1105 (unsigned long long) server->fsid.major,
1106 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001107
Chuck Leverfca52382010-12-24 01:32:32 +00001108 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001109 server->mount_time = jiffies;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001110 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001111 return server;
1112
1113error:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001114 nfs_free_fattr(fattr);
David Howells54ceac42006-08-22 20:06:13 -04001115 nfs_free_server(server);
1116 return ERR_PTR(error);
1117}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001118EXPORT_SYMBOL_GPL(nfs_create_server);
David Howells54ceac42006-08-22 20:06:13 -04001119
David Howells54ceac42006-08-22 20:06:13 -04001120/*
1121 * Clone an NFS2, NFS3 or NFS4 server record
1122 */
1123struct nfs_server *nfs_clone_server(struct nfs_server *source,
1124 struct nfs_fh *fh,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001125 struct nfs_fattr *fattr,
1126 rpc_authflavor_t flavor)
David Howells54ceac42006-08-22 20:06:13 -04001127{
1128 struct nfs_server *server;
Trond Myklebustfbca7792010-04-16 16:22:46 -04001129 struct nfs_fattr *fattr_fsinfo;
David Howells54ceac42006-08-22 20:06:13 -04001130 int error;
1131
1132 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001133 (unsigned long long) fattr->fsid.major,
1134 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001135
1136 server = nfs_alloc_server();
1137 if (!server)
1138 return ERR_PTR(-ENOMEM);
1139
Trond Myklebustfbca7792010-04-16 16:22:46 -04001140 error = -ENOMEM;
1141 fattr_fsinfo = nfs_alloc_fattr();
1142 if (fattr_fsinfo == NULL)
1143 goto out_free_server;
1144
David Howells54ceac42006-08-22 20:06:13 -04001145 /* Copy data from the source */
1146 server->nfs_client = source->nfs_client;
Chuck Lever0aaaf5c2011-12-06 16:13:48 -05001147 server->destroy = source->destroy;
David Howells54ceac42006-08-22 20:06:13 -04001148 atomic_inc(&server->nfs_client->cl_count);
1149 nfs_server_copy_userdata(server, source);
1150
1151 server->fsid = fattr->fsid;
1152
Trond Myklebust33170232007-12-20 16:03:59 -05001153 error = nfs_init_server_rpcclient(server,
1154 source->client->cl_timeout,
Bryan Schumaker7e6eb682012-04-27 13:27:42 -04001155 flavor);
David Howells54ceac42006-08-22 20:06:13 -04001156 if (error < 0)
1157 goto out_free_server;
David Howells54ceac42006-08-22 20:06:13 -04001158
1159 /* probe the filesystem info for this server filesystem */
Trond Myklebustfbca7792010-04-16 16:22:46 -04001160 error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001161 if (error < 0)
1162 goto out_free_server;
1163
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001164 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1165 server->namelen = NFS4_MAXNAMLEN;
1166
David Howells54ceac42006-08-22 20:06:13 -04001167 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001168 (unsigned long long) server->fsid.major,
1169 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001170
1171 error = nfs_start_lockd(server);
1172 if (error < 0)
1173 goto out_free_server;
1174
Chuck Leverfca52382010-12-24 01:32:32 +00001175 nfs_server_insert_lists(server);
David Howells54ceac42006-08-22 20:06:13 -04001176 server->mount_time = jiffies;
1177
Trond Myklebustfbca7792010-04-16 16:22:46 -04001178 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001179 dprintk("<-- nfs_clone_server() = %p\n", server);
1180 return server;
1181
1182out_free_server:
Trond Myklebustfbca7792010-04-16 16:22:46 -04001183 nfs_free_fattr(fattr_fsinfo);
David Howells54ceac42006-08-22 20:06:13 -04001184 nfs_free_server(server);
1185 dprintk("<-- nfs_clone_server() = error %d\n", error);
1186 return ERR_PTR(error);
1187}
Bryan Schumakerddda8e02012-07-30 16:05:23 -04001188EXPORT_SYMBOL_GPL(nfs_clone_server);
David Howells6aaca562006-08-22 20:06:13 -04001189
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001190void nfs_clients_init(struct net *net)
1191{
1192 struct nfs_net *nn = net_generic(net, nfs_net_id);
1193
1194 INIT_LIST_HEAD(&nn->nfs_client_list);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001195 INIT_LIST_HEAD(&nn->nfs_volume_list);
Stanislav Kinsbursky28cd1b32012-01-23 17:26:22 +00001196#ifdef CONFIG_NFS_V4
1197 idr_init(&nn->cb_ident_idr);
1198#endif
Trond Myklebust4c03ae42012-02-07 00:05:11 -05001199 spin_lock_init(&nn->nfs_client_lock);
Chuck Leverf0920752012-05-21 22:45:41 -04001200 nn->boot_time = CURRENT_TIME;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001201}
1202
David Howells6aaca562006-08-22 20:06:13 -04001203#ifdef CONFIG_PROC_FS
1204static struct proc_dir_entry *proc_fs_nfs;
1205
1206static int nfs_server_list_open(struct inode *inode, struct file *file);
1207static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1208static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1209static void nfs_server_list_stop(struct seq_file *p, void *v);
1210static int nfs_server_list_show(struct seq_file *m, void *v);
1211
James Morris88e9d342009-09-22 16:43:43 -07001212static const struct seq_operations nfs_server_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001213 .start = nfs_server_list_start,
1214 .next = nfs_server_list_next,
1215 .stop = nfs_server_list_stop,
1216 .show = nfs_server_list_show,
1217};
1218
Arjan van de Ven00977a52007-02-12 00:55:34 -08001219static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001220 .open = nfs_server_list_open,
1221 .read = seq_read,
1222 .llseek = seq_lseek,
1223 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001224 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001225};
1226
1227static int nfs_volume_list_open(struct inode *inode, struct file *file);
1228static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1229static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1230static void nfs_volume_list_stop(struct seq_file *p, void *v);
1231static int nfs_volume_list_show(struct seq_file *m, void *v);
1232
James Morris88e9d342009-09-22 16:43:43 -07001233static const struct seq_operations nfs_volume_list_ops = {
David Howells6aaca562006-08-22 20:06:13 -04001234 .start = nfs_volume_list_start,
1235 .next = nfs_volume_list_next,
1236 .stop = nfs_volume_list_stop,
1237 .show = nfs_volume_list_show,
1238};
1239
Arjan van de Ven00977a52007-02-12 00:55:34 -08001240static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001241 .open = nfs_volume_list_open,
1242 .read = seq_read,
1243 .llseek = seq_lseek,
1244 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001245 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001246};
1247
1248/*
1249 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1250 * we're dealing
1251 */
1252static int nfs_server_list_open(struct inode *inode, struct file *file)
1253{
1254 struct seq_file *m;
1255 int ret;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001256 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1257 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001258
1259 ret = seq_open(file, &nfs_server_list_ops);
1260 if (ret < 0)
1261 return ret;
1262
1263 m = file->private_data;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001264 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001265
1266 return 0;
1267}
1268
1269/*
1270 * set up the iterator to start reading from the server list and return the first item
1271 */
1272static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1273{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001274 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1275
David Howells6aaca562006-08-22 20:06:13 -04001276 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001277 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001278 return seq_list_start_head(&nn->nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001279}
1280
1281/*
1282 * move to next server
1283 */
1284static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1285{
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001286 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1287
1288 return seq_list_next(v, &nn->nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001289}
1290
1291/*
1292 * clean up after reading from the transports list
1293 */
1294static void nfs_server_list_stop(struct seq_file *p, void *v)
1295{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001296 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1297
1298 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001299}
1300
1301/*
1302 * display a header line followed by a load of call lines
1303 */
1304static int nfs_server_list_show(struct seq_file *m, void *v)
1305{
1306 struct nfs_client *clp;
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001307 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001308
1309 /* display header on line 1 */
Stanislav Kinsbursky6b131682012-01-23 17:26:05 +00001310 if (v == &nn->nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001311 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1312 return 0;
1313 }
1314
1315 /* display one transport per line on subsequent lines */
1316 clp = list_entry(v, struct nfs_client, cl_share_link);
1317
Malahal Naineni940aab42011-09-20 17:27:14 -07001318 /* Check if the client is initialized */
1319 if (clp->cl_cons_state != NFS_CS_READY)
1320 return 0;
1321
Trond Myklebust2446ab62012-03-01 17:00:56 -05001322 rcu_read_lock();
Chuck Lever5d8515c2007-12-10 14:57:16 -05001323 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001324 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001325 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1326 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001327 atomic_read(&clp->cl_count),
1328 clp->cl_hostname);
Trond Myklebust2446ab62012-03-01 17:00:56 -05001329 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001330
1331 return 0;
1332}
1333
1334/*
1335 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1336 */
1337static int nfs_volume_list_open(struct inode *inode, struct file *file)
1338{
1339 struct seq_file *m;
1340 int ret;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001341 struct pid_namespace *pid_ns = file->f_dentry->d_sb->s_fs_info;
1342 struct net *net = pid_ns->child_reaper->nsproxy->net_ns;
David Howells6aaca562006-08-22 20:06:13 -04001343
1344 ret = seq_open(file, &nfs_volume_list_ops);
1345 if (ret < 0)
1346 return ret;
1347
1348 m = file->private_data;
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001349 m->private = net;
David Howells6aaca562006-08-22 20:06:13 -04001350
1351 return 0;
1352}
1353
1354/*
1355 * set up the iterator to start reading from the volume list and return the first item
1356 */
1357static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1358{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001359 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
1360
David Howells6aaca562006-08-22 20:06:13 -04001361 /* lock the list against modification */
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001362 spin_lock(&nn->nfs_client_lock);
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001363 return seq_list_start_head(&nn->nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001364}
1365
1366/*
1367 * move to next volume
1368 */
1369static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1370{
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001371 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1372
1373 return seq_list_next(v, &nn->nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001374}
1375
1376/*
1377 * clean up after reading from the transports list
1378 */
1379static void nfs_volume_list_stop(struct seq_file *p, void *v)
1380{
Stanislav Kinsburskydc030852012-01-23 17:26:31 +00001381 struct nfs_net *nn = net_generic(p->private, nfs_net_id);
1382
1383 spin_unlock(&nn->nfs_client_lock);
David Howells6aaca562006-08-22 20:06:13 -04001384}
1385
1386/*
1387 * display a header line followed by a load of call lines
1388 */
1389static int nfs_volume_list_show(struct seq_file *m, void *v)
1390{
1391 struct nfs_server *server;
1392 struct nfs_client *clp;
1393 char dev[8], fsid[17];
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001394 struct nfs_net *nn = net_generic(m->private, nfs_net_id);
David Howells6aaca562006-08-22 20:06:13 -04001395
1396 /* display header on line 1 */
Stanislav Kinsburskyc25d32b2012-01-23 17:26:14 +00001397 if (v == &nn->nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001398 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001399 return 0;
1400 }
1401 /* display one transport per line on subsequent lines */
1402 server = list_entry(v, struct nfs_server, master_link);
1403 clp = server->nfs_client;
1404
1405 snprintf(dev, 8, "%u:%u",
1406 MAJOR(server->s_dev), MINOR(server->s_dev));
1407
1408 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001409 (unsigned long long) server->fsid.major,
1410 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001411
Trond Myklebust2446ab62012-03-01 17:00:56 -05001412 rcu_read_lock();
David Howells5d1acff2009-04-03 16:42:47 +01001413 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001414 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001415 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1416 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001417 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001418 fsid,
1419 nfs_server_fscache_state(server));
Trond Myklebust2446ab62012-03-01 17:00:56 -05001420 rcu_read_unlock();
David Howells6aaca562006-08-22 20:06:13 -04001421
1422 return 0;
1423}
1424
1425/*
1426 * initialise the /proc/fs/nfsfs/ directory
1427 */
1428int __init nfs_fs_proc_init(void)
1429{
1430 struct proc_dir_entry *p;
1431
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001432 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001433 if (!proc_fs_nfs)
1434 goto error_0;
1435
David Howells6aaca562006-08-22 20:06:13 -04001436 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001437 p = proc_create("servers", S_IFREG|S_IRUGO,
1438 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001439 if (!p)
1440 goto error_1;
1441
David Howells6aaca562006-08-22 20:06:13 -04001442 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001443 p = proc_create("volumes", S_IFREG|S_IRUGO,
1444 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001445 if (!p)
1446 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001447 return 0;
1448
1449error_2:
1450 remove_proc_entry("servers", proc_fs_nfs);
1451error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001452 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001453error_0:
1454 return -ENOMEM;
1455}
1456
1457/*
1458 * clean up the /proc/fs/nfsfs/ directory
1459 */
1460void nfs_fs_proc_exit(void)
1461{
1462 remove_proc_entry("volumes", proc_fs_nfs);
1463 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001464 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001465}
1466
1467#endif /* CONFIG_PROC_FS */