blob: 3e232bf56dfb2f1e25a3b593df7eeb15cc22fabe [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>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010048#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040049
50#define NFSDBG_FACILITY NFSDBG_CLIENT
51
52static DEFINE_SPINLOCK(nfs_client_lock);
53static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040054static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040055static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
56
57/*
David Howells5006a762006-08-22 20:06:12 -040058 * RPC cruft for NFS
59 */
60static struct rpc_version *nfs_version[5] = {
61 [2] = &nfs_version2,
62#ifdef CONFIG_NFS_V3
63 [3] = &nfs_version3,
64#endif
65#ifdef CONFIG_NFS_V4
66 [4] = &nfs_version4,
67#endif
68};
69
70struct rpc_program nfs_program = {
71 .name = "nfs",
72 .number = NFS_PROGRAM,
73 .nrvers = ARRAY_SIZE(nfs_version),
74 .version = nfs_version,
75 .stats = &nfs_rpcstat,
76 .pipe_dir_name = "/nfs",
77};
78
79struct rpc_stat nfs_rpcstat = {
80 .program = &nfs_program
81};
82
83
84#ifdef CONFIG_NFS_V3_ACL
85static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
86static struct rpc_version * nfsacl_version[] = {
87 [3] = &nfsacl_version3,
88};
89
90struct rpc_program nfsacl_program = {
91 .name = "nfsacl",
92 .number = NFS_ACL_PROGRAM,
93 .nrvers = ARRAY_SIZE(nfsacl_version),
94 .version = nfsacl_version,
95 .stats = &nfsacl_rpcstat,
96};
97#endif /* CONFIG_NFS_V3_ACL */
98
Trond Myklebust3a498022007-12-14 14:56:04 -050099struct nfs_client_initdata {
100 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500101 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500102 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500103 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500104 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400105 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500106};
107
David Howells5006a762006-08-22 20:06:12 -0400108/*
David Howells24c8dbb2006-08-22 20:06:10 -0400109 * Allocate a shared client record
110 *
111 * Since these are allocated/deallocated very rarely, we don't
112 * bother putting them in a slab cache...
113 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500114static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400115{
116 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400117 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400118
119 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
120 goto error_0;
121
Trond Myklebust40c553192007-12-14 14:56:07 -0500122 clp->rpc_ops = cl_init->rpc_ops;
123
124 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400125 if (nfs_callback_up() < 0)
126 goto error_2;
127 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
128 }
129
130 atomic_set(&clp->cl_count, 1);
131 clp->cl_cons_state = NFS_CS_INITING;
132
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500133 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
134 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400135
Trond Myklebust3a498022007-12-14 14:56:04 -0500136 if (cl_init->hostname) {
137 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400138 if (!clp->cl_hostname)
139 goto error_3;
140 }
141
142 INIT_LIST_HEAD(&clp->cl_superblocks);
143 clp->cl_rpcclient = ERR_PTR(-EINVAL);
144
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500145 clp->cl_proto = cl_init->proto;
146
David Howells24c8dbb2006-08-22 20:06:10 -0400147#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400148 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000150 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400151 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
152 clp->cl_boot_time = CURRENT_TIME;
153 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400154 clp->cl_minorversion = cl_init->minorversion;
David Howells24c8dbb2006-08-22 20:06:10 -0400155#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400156 cred = rpc_lookup_machine_cred();
157 if (!IS_ERR(cred))
158 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400159
David Howells14727282009-04-03 16:42:42 +0100160 nfs_fscache_get_client_cookie(clp);
161
David Howells24c8dbb2006-08-22 20:06:10 -0400162 return clp;
163
164error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400165 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
166 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400167error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400168 kfree(clp);
169error_0:
170 return NULL;
171}
172
Trond Myklebust5dd31772006-08-24 01:03:05 -0400173static void nfs4_shutdown_client(struct nfs_client *clp)
174{
175#ifdef CONFIG_NFS_V4
176 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
177 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400178 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400179 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
180 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500181
182 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400183#endif
184}
185
David Howells24c8dbb2006-08-22 20:06:10 -0400186/*
Andy Adamson557134a2009-04-01 09:21:53 -0400187 * Clears/puts all minor version specific parts from an nfs_client struct
188 * reverting it to minorversion 0.
189 */
190static void nfs4_clear_client_minor_version(struct nfs_client *clp)
191{
192#ifdef CONFIG_NFS_V4_1
193 if (nfs4_has_session(clp)) {
194 nfs4_destroy_session(clp->cl_session);
195 clp->cl_session = NULL;
196 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400197
198 clp->cl_call_sync = _nfs4_call_sync;
Andy Adamson557134a2009-04-01 09:21:53 -0400199#endif /* CONFIG_NFS_V4_1 */
200}
201
202/*
David Howells24c8dbb2006-08-22 20:06:10 -0400203 * Destroy a shared client record
204 */
205static void nfs_free_client(struct nfs_client *clp)
206{
Trond Myklebust40c553192007-12-14 14:56:07 -0500207 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400208
Andy Adamson557134a2009-04-01 09:21:53 -0400209 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400210 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400211
David Howells14727282009-04-03 16:42:42 +0100212 nfs_fscache_release_client_cookie(clp);
213
David Howells24c8dbb2006-08-22 20:06:10 -0400214 /* -EIO all pending I/O */
215 if (!IS_ERR(clp->cl_rpcclient))
216 rpc_shutdown_client(clp->cl_rpcclient);
217
218 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
219 nfs_callback_down();
220
Trond Myklebust7c67db32008-04-07 20:50:11 -0400221 if (clp->cl_machine_cred != NULL)
222 put_rpccred(clp->cl_machine_cred);
223
David Howells24c8dbb2006-08-22 20:06:10 -0400224 kfree(clp->cl_hostname);
225 kfree(clp);
226
227 dprintk("<-- nfs_free_client()\n");
228}
229
230/*
231 * Release a reference to a shared client record
232 */
233void nfs_put_client(struct nfs_client *clp)
234{
David Howells27ba8512006-07-30 14:40:56 -0400235 if (!clp)
236 return;
237
David Howells24c8dbb2006-08-22 20:06:10 -0400238 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
239
240 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
241 list_del(&clp->cl_share_link);
242 spin_unlock(&nfs_client_lock);
243
244 BUG_ON(!list_empty(&clp->cl_superblocks));
245
246 nfs_free_client(clp);
247 }
248}
249
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500250#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400251/*
252 * Test if two ip6 socket addresses refer to the same socket by
253 * comparing relevant fields. The padding bytes specifically, are not
254 * compared. sin6_flowinfo is not compared because it only affects QoS
255 * and sin6_scope_id is only compared if the address is "link local"
256 * because "link local" addresses need only be unique to a specific
257 * link. Conversely, ordinary unicast addresses might have different
258 * sin6_scope_id.
259 *
260 * The caller should ensure both socket addresses are AF_INET6.
261 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400262static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
263 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400264{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400265 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
266 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400267
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400268 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
269 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400270 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500271
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400272 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500273}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400274#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
275static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
276 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400277{
278 return 0;
279}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500280#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500281
David Howells24c8dbb2006-08-22 20:06:10 -0400282/*
Ian Dalld7371c42009-03-10 20:33:22 -0400283 * Test if two ip4 socket addresses refer to the same socket, by
284 * comparing relevant fields. The padding bytes specifically, are
285 * not compared.
286 *
287 * The caller should ensure both socket addresses are AF_INET.
288 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400289static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
290 const struct sockaddr *sa2)
291{
292 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
293 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
294
295 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
296}
297
298static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
299 const struct sockaddr *sa2)
300{
301 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
302 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
303
304 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
305 (sin1->sin6_port == sin2->sin6_port);
306}
307
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400308static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
309 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400310{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400311 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
312 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400313
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400314 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
315 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400316}
317
318/*
Ian Dalld7371c42009-03-10 20:33:22 -0400319 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400320 * by comparing (only) relevant fields, excluding the port number.
321 */
322static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
323 const struct sockaddr *sa2)
324{
325 if (sa1->sa_family != sa2->sa_family)
326 return 0;
327
328 switch (sa1->sa_family) {
329 case AF_INET:
330 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
331 case AF_INET6:
332 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
333 }
334 return 0;
335}
336
337/*
338 * Test if two socket addresses represent the same actual socket,
339 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400340 */
341static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
342 const struct sockaddr *sa2)
343{
344 if (sa1->sa_family != sa2->sa_family)
345 return 0;
346
347 switch (sa1->sa_family) {
348 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400349 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400350 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400351 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400352 }
353 return 0;
354}
355
356/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500357 * Find a client by IP address and protocol version
358 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400359 */
Chuck Leverff052642007-12-10 14:58:44 -0500360struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500361{
362 struct nfs_client *clp;
363
364 spin_lock(&nfs_client_lock);
365 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500366 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
367
Trond Myklebustc81468a2007-12-14 14:56:05 -0500368 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400369 if (!(clp->cl_cons_state == NFS_CS_READY ||
370 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500371 continue;
372
373 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500374 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500375 continue;
376
377 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500378 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500379 continue;
380
381 atomic_inc(&clp->cl_count);
382 spin_unlock(&nfs_client_lock);
383 return clp;
384 }
385 spin_unlock(&nfs_client_lock);
386 return NULL;
387}
388
389/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500390 * Find a client by IP address and protocol version
391 * - returns NULL if no such client
392 */
393struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
394{
395 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
396 u32 nfsvers = clp->rpc_ops->version;
397
398 spin_lock(&nfs_client_lock);
399 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
400 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
401
402 /* Don't match clients that failed to initialise properly */
403 if (clp->cl_cons_state != NFS_CS_READY)
404 continue;
405
406 /* Different NFS versions cannot share the same nfs_client */
407 if (clp->rpc_ops->version != nfsvers)
408 continue;
409
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500410 /* Match only the IP address, not the port number */
411 if (!nfs_sockaddr_match_ipaddr(sap, clap))
412 continue;
413
414 atomic_inc(&clp->cl_count);
415 spin_unlock(&nfs_client_lock);
416 return clp;
417 }
418 spin_unlock(&nfs_client_lock);
419 return NULL;
420}
421
422/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500423 * Find an nfs_client on the list that matches the initialisation data
424 * that is supplied.
425 */
426static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400427{
428 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400429 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400430
431 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400432 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700433 /* Don't match clients that failed to initialise properly */
434 if (clp->cl_cons_state < 0)
435 continue;
436
David Howells24c8dbb2006-08-22 20:06:10 -0400437 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500438 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400439 continue;
440
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500441 if (clp->cl_proto != data->proto)
442 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400443 /* Match nfsv4 minorversion */
444 if (clp->cl_minorversion != data->minorversion)
445 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500446 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400447 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400448 continue;
449
Trond Myklebustc81468a2007-12-14 14:56:05 -0500450 atomic_inc(&clp->cl_count);
451 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400452 }
David Howells24c8dbb2006-08-22 20:06:10 -0400453 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400454}
455
456/*
457 * Look up a client by IP address and protocol version
458 * - creates a new record if one doesn't yet exist
459 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500460static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400461{
462 struct nfs_client *clp, *new = NULL;
463 int error;
464
Chuck Leverd7422c42007-12-10 14:58:51 -0500465 dprintk("--> nfs_get_client(%s,v%u)\n",
466 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400467
468 /* see if the client already exists */
469 do {
470 spin_lock(&nfs_client_lock);
471
Trond Myklebustc81468a2007-12-14 14:56:05 -0500472 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400473 if (clp)
474 goto found_client;
475 if (new)
476 goto install_client;
477
478 spin_unlock(&nfs_client_lock);
479
Trond Myklebust3a498022007-12-14 14:56:04 -0500480 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400481 } while (new);
482
483 return ERR_PTR(-ENOMEM);
484
485 /* install a new client and return with it unready */
486install_client:
487 clp = new;
488 list_add(&clp->cl_share_link, &nfs_client_list);
489 spin_unlock(&nfs_client_lock);
490 dprintk("--> nfs_get_client() = %p [new]\n", clp);
491 return clp;
492
493 /* found an existing client
494 * - make sure it's ready before returning
495 */
496found_client:
497 spin_unlock(&nfs_client_lock);
498
499 if (new)
500 nfs_free_client(new);
501
Matthew Wilcox150030b2007-12-06 16:24:39 -0500502 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400503 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400504 if (error < 0) {
505 nfs_put_client(clp);
506 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400507 }
508
509 if (clp->cl_cons_state < NFS_CS_READY) {
510 error = clp->cl_cons_state;
511 nfs_put_client(clp);
512 return ERR_PTR(error);
513 }
514
David Howells54ceac42006-08-22 20:06:13 -0400515 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
516
David Howells24c8dbb2006-08-22 20:06:10 -0400517 dprintk("--> nfs_get_client() = %p [share]\n", clp);
518 return clp;
519}
520
521/*
522 * Mark a server as ready or failed
523 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400524void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400525{
526 clp->cl_cons_state = state;
527 wake_up_all(&nfs_client_active_wq);
528}
David Howells5006a762006-08-22 20:06:12 -0400529
530/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400531 * With sessions, the client is not marked ready until after a
532 * successful EXCHANGE_ID and CREATE_SESSION.
533 *
534 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
535 * other versions of NFS can be tried.
536 */
537int nfs4_check_client_ready(struct nfs_client *clp)
538{
539 if (!nfs4_has_session(clp))
540 return 0;
541 if (clp->cl_cons_state < NFS_CS_READY)
542 return -EPROTONOSUPPORT;
543 return 0;
544}
545
546/*
David Howells5006a762006-08-22 20:06:12 -0400547 * Initialise the timeout values for a connection
548 */
549static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
550 unsigned int timeo, unsigned int retrans)
551{
552 to->to_initval = timeo * HZ / 10;
553 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400554
555 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400556 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400557 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400558 if (to->to_retries == 0)
559 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500560 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400561 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400562 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
563 to->to_initval = NFS_MAX_TCP_TIMEOUT;
564 to->to_increment = to->to_initval;
565 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500566 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
567 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
568 if (to->to_maxval < to->to_initval)
569 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400570 to->to_exponential = 0;
571 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400572 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400573 if (to->to_retries == 0)
574 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400575 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400576 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400577 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
578 to->to_initval = NFS_MAX_UDP_TIMEOUT;
579 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
580 to->to_exponential = 1;
581 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400582 default:
583 BUG();
David Howells5006a762006-08-22 20:06:12 -0400584 }
585}
586
587/*
588 * Create an RPC client handle
589 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500590static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500591 const struct rpc_timeout *timeparms,
592 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500593 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400594{
David Howells5006a762006-08-22 20:06:12 -0400595 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400596 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500597 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400598 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500599 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500600 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400601 .servername = clp->cl_hostname,
602 .program = &nfs_program,
603 .version = clp->rpc_ops->version,
604 .authflavor = flavor,
605 };
David Howells5006a762006-08-22 20:06:12 -0400606
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500607 if (discrtry)
608 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
609 if (noresvport)
610 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
611
David Howells5006a762006-08-22 20:06:12 -0400612 if (!IS_ERR(clp->cl_rpcclient))
613 return 0;
614
Chuck Lever41877d22006-08-22 20:06:20 -0400615 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400616 if (IS_ERR(clnt)) {
617 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700618 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400619 return PTR_ERR(clnt);
620 }
621
David Howells5006a762006-08-22 20:06:12 -0400622 clp->cl_rpcclient = clnt;
623 return 0;
624}
David Howells54ceac42006-08-22 20:06:13 -0400625
626/*
627 * Version 2 or 3 client destruction
628 */
629static void nfs_destroy_server(struct nfs_server *server)
630{
David Howells54ceac42006-08-22 20:06:13 -0400631 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500632 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400633}
634
635/*
636 * Version 2 or 3 lockd setup
637 */
638static int nfs_start_lockd(struct nfs_server *server)
639{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500640 struct nlm_host *host;
641 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500642 struct nlmclnt_initdata nlm_init = {
643 .hostname = clp->cl_hostname,
644 .address = (struct sockaddr *)&clp->cl_addr,
645 .addrlen = clp->cl_addrlen,
646 .protocol = server->flags & NFS_MOUNT_TCP ?
647 IPPROTO_TCP : IPPROTO_UDP,
648 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500649 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
650 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500651 };
David Howells54ceac42006-08-22 20:06:13 -0400652
Chuck Lever883bb162008-01-15 16:04:20 -0500653 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500654 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400655 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500656 return 0;
657
Chuck Lever883bb162008-01-15 16:04:20 -0500658 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500659 if (IS_ERR(host))
660 return PTR_ERR(host);
661
662 server->nlm_host = host;
663 server->destroy = nfs_destroy_server;
664 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400665}
666
667/*
668 * Initialise an NFSv3 ACL client connection
669 */
670#ifdef CONFIG_NFS_V3_ACL
671static void nfs_init_server_aclclient(struct nfs_server *server)
672{
Trond Myklebust40c553192007-12-14 14:56:07 -0500673 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400674 goto out_noacl;
675 if (server->flags & NFS_MOUNT_NOACL)
676 goto out_noacl;
677
678 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
679 if (IS_ERR(server->client_acl))
680 goto out_noacl;
681
682 /* No errors! Assume that Sun nfsacls are supported */
683 server->caps |= NFS_CAP_ACLS;
684 return;
685
686out_noacl:
687 server->caps &= ~NFS_CAP_ACLS;
688}
689#else
690static inline void nfs_init_server_aclclient(struct nfs_server *server)
691{
692 server->flags &= ~NFS_MOUNT_NOACL;
693 server->caps &= ~NFS_CAP_ACLS;
694}
695#endif
696
697/*
698 * Create a general RPC client
699 */
Trond Myklebust33170232007-12-20 16:03:59 -0500700static int nfs_init_server_rpcclient(struct nfs_server *server,
701 const struct rpc_timeout *timeo,
702 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400703{
704 struct nfs_client *clp = server->nfs_client;
705
706 server->client = rpc_clone_client(clp->cl_rpcclient);
707 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700708 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400709 return PTR_ERR(server->client);
710 }
711
Trond Myklebust33170232007-12-20 16:03:59 -0500712 memcpy(&server->client->cl_timeout_default,
713 timeo,
714 sizeof(server->client->cl_timeout_default));
715 server->client->cl_timeout = &server->client->cl_timeout_default;
716
David Howells54ceac42006-08-22 20:06:13 -0400717 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
718 struct rpc_auth *auth;
719
720 auth = rpcauth_create(pseudoflavour, server->client);
721 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700722 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400723 return PTR_ERR(auth);
724 }
725 }
726 server->client->cl_softrtry = 0;
727 if (server->flags & NFS_MOUNT_SOFT)
728 server->client->cl_softrtry = 1;
729
David Howells54ceac42006-08-22 20:06:13 -0400730 return 0;
731}
732
733/*
734 * Initialise an NFS2 or NFS3 client
735 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400736static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500737 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400738 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400739{
David Howells54ceac42006-08-22 20:06:13 -0400740 int error;
741
742 if (clp->cl_cons_state == NFS_CS_READY) {
743 /* the client is already initialised */
744 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
745 return 0;
746 }
747
David Howells54ceac42006-08-22 20:06:13 -0400748 /*
749 * Create a client RPC handle for doing FSSTAT with UNIX auth only
750 * - RFC 2623, sec 2.3.2
751 */
Chuck Leverd7403512008-12-23 15:21:37 -0500752 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
753 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400754 if (error < 0)
755 goto error;
756 nfs_mark_client_ready(clp, NFS_CS_READY);
757 return 0;
758
759error:
760 nfs_mark_client_ready(clp, error);
761 dprintk("<-- nfs_init_client() = xerror %d\n", error);
762 return error;
763}
764
765/*
766 * Create a version 2 or 3 client
767 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400768static int nfs_init_server(struct nfs_server *server,
769 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400770{
Trond Myklebust3a498022007-12-14 14:56:04 -0500771 struct nfs_client_initdata cl_init = {
772 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500773 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500774 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500775 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500776 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500777 };
Trond Myklebust33170232007-12-20 16:03:59 -0500778 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400779 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500780 int error;
David Howells54ceac42006-08-22 20:06:13 -0400781
782 dprintk("--> nfs_init_server()\n");
783
784#ifdef CONFIG_NFS_V3
785 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500786 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400787#endif
788
789 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500790 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400791 if (IS_ERR(clp)) {
792 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
793 return PTR_ERR(clp);
794 }
795
Trond Myklebust33170232007-12-20 16:03:59 -0500796 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
797 data->timeo, data->retrans);
798 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400799 if (error < 0)
800 goto error;
801
802 server->nfs_client = clp;
803
804 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400805 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100806 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400807
808 if (data->rsize)
809 server->rsize = nfs_block_size(data->rsize, NULL);
810 if (data->wsize)
811 server->wsize = nfs_block_size(data->wsize, NULL);
812
813 server->acregmin = data->acregmin * HZ;
814 server->acregmax = data->acregmax * HZ;
815 server->acdirmin = data->acdirmin * HZ;
816 server->acdirmax = data->acdirmax * HZ;
817
818 /* Start lockd here, before we might error out */
819 error = nfs_start_lockd(server);
820 if (error < 0)
821 goto error;
822
Chuck Leverf22d6d72008-03-14 14:10:22 -0400823 server->port = data->nfs_server.port;
824
Trond Myklebust33170232007-12-20 16:03:59 -0500825 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400826 if (error < 0)
827 goto error;
828
Chuck Lever3f8400d2008-03-14 14:10:30 -0400829 /* Preserve the values of mount_server-related mount options */
830 if (data->mount_server.addrlen) {
831 memcpy(&server->mountd_address, &data->mount_server.address,
832 data->mount_server.addrlen);
833 server->mountd_addrlen = data->mount_server.addrlen;
834 }
835 server->mountd_version = data->mount_server.version;
836 server->mountd_port = data->mount_server.port;
837 server->mountd_protocol = data->mount_server.protocol;
838
David Howells54ceac42006-08-22 20:06:13 -0400839 server->namelen = data->namlen;
840 /* Create a client RPC handle for the NFSv3 ACL management interface */
841 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400842 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
843 return 0;
844
845error:
846 server->nfs_client = NULL;
847 nfs_put_client(clp);
848 dprintk("<-- nfs_init_server() = xerror %d\n", error);
849 return error;
850}
851
852/*
853 * Load up the server record from information gained in an fsinfo record
854 */
855static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
856{
857 unsigned long max_rpc_payload;
858
859 /* Work out a lot of parameters */
860 if (server->rsize == 0)
861 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
862 if (server->wsize == 0)
863 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
864
865 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
866 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
867 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
868 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
869
870 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
871 if (server->rsize > max_rpc_payload)
872 server->rsize = max_rpc_payload;
873 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
874 server->rsize = NFS_MAX_FILE_IO_SIZE;
875 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700876
David Howells54ceac42006-08-22 20:06:13 -0400877 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
878
879 if (server->wsize > max_rpc_payload)
880 server->wsize = max_rpc_payload;
881 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
882 server->wsize = NFS_MAX_FILE_IO_SIZE;
883 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
884 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
885
886 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
887 if (server->dtsize > PAGE_CACHE_SIZE)
888 server->dtsize = PAGE_CACHE_SIZE;
889 if (server->dtsize > server->rsize)
890 server->dtsize = server->rsize;
891
892 if (server->flags & NFS_MOUNT_NOAC) {
893 server->acregmin = server->acregmax = 0;
894 server->acdirmin = server->acdirmax = 0;
895 }
896
897 server->maxfilesize = fsinfo->maxfilesize;
898
899 /* We're airborne Set socket buffersize */
900 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
901}
902
903/*
904 * Probe filesystem information, including the FSID on v2/v3
905 */
906static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
907{
908 struct nfs_fsinfo fsinfo;
909 struct nfs_client *clp = server->nfs_client;
910 int error;
911
912 dprintk("--> nfs_probe_fsinfo()\n");
913
914 if (clp->rpc_ops->set_capabilities != NULL) {
915 error = clp->rpc_ops->set_capabilities(server, mntfh);
916 if (error < 0)
917 goto out_error;
918 }
919
920 fsinfo.fattr = fattr;
921 nfs_fattr_init(fattr);
922 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
923 if (error < 0)
924 goto out_error;
925
926 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700927 error = bdi_init(&server->backing_dev_info);
928 if (error)
929 goto out_error;
930
David Howells54ceac42006-08-22 20:06:13 -0400931
932 /* Get some general file system info */
933 if (server->namelen == 0) {
934 struct nfs_pathconf pathinfo;
935
936 pathinfo.fattr = fattr;
937 nfs_fattr_init(fattr);
938
939 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
940 server->namelen = pathinfo.max_namelen;
941 }
942
943 dprintk("<-- nfs_probe_fsinfo() = 0\n");
944 return 0;
945
946out_error:
947 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
948 return error;
949}
950
951/*
952 * Copy useful information when duplicating a server record
953 */
954static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
955{
956 target->flags = source->flags;
957 target->acregmin = source->acregmin;
958 target->acregmax = source->acregmax;
959 target->acdirmin = source->acdirmin;
960 target->acdirmax = source->acdirmax;
961 target->caps = source->caps;
962}
963
964/*
965 * Allocate and initialise a server record
966 */
967static struct nfs_server *nfs_alloc_server(void)
968{
969 struct nfs_server *server;
970
971 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
972 if (!server)
973 return NULL;
974
975 server->client = server->client_acl = ERR_PTR(-EINVAL);
976
977 /* Zero out the NFS state stuff */
978 INIT_LIST_HEAD(&server->client_link);
979 INIT_LIST_HEAD(&server->master_link);
980
Steve Dicksonef818a22007-11-08 04:05:04 -0500981 atomic_set(&server->active, 0);
982
David Howells54ceac42006-08-22 20:06:13 -0400983 server->io_stats = nfs_alloc_iostats();
984 if (!server->io_stats) {
985 kfree(server);
986 return NULL;
987 }
988
989 return server;
990}
991
992/*
993 * Free up a server record
994 */
995void nfs_free_server(struct nfs_server *server)
996{
997 dprintk("--> nfs_free_server()\n");
998
999 spin_lock(&nfs_client_lock);
1000 list_del(&server->client_link);
1001 list_del(&server->master_link);
1002 spin_unlock(&nfs_client_lock);
1003
1004 if (server->destroy != NULL)
1005 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001006
1007 if (!IS_ERR(server->client_acl))
1008 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001009 if (!IS_ERR(server->client))
1010 rpc_shutdown_client(server->client);
1011
1012 nfs_put_client(server->nfs_client);
1013
1014 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001015 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001016 kfree(server);
1017 nfs_release_automount_timer();
1018 dprintk("<-- nfs_free_server()\n");
1019}
1020
1021/*
1022 * Create a version 2 or 3 volume record
1023 * - keyed on server and FSID
1024 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001025struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001026 struct nfs_fh *mntfh)
1027{
1028 struct nfs_server *server;
1029 struct nfs_fattr fattr;
1030 int error;
1031
1032 server = nfs_alloc_server();
1033 if (!server)
1034 return ERR_PTR(-ENOMEM);
1035
1036 /* Get a client representation */
1037 error = nfs_init_server(server, data);
1038 if (error < 0)
1039 goto error;
1040
1041 BUG_ON(!server->nfs_client);
1042 BUG_ON(!server->nfs_client->rpc_ops);
1043 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1044
1045 /* Probe the root fh to retrieve its FSID */
1046 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1047 if (error < 0)
1048 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001049 if (server->nfs_client->rpc_ops->version == 3) {
1050 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1051 server->namelen = NFS3_MAXNAMLEN;
1052 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1053 server->caps |= NFS_CAP_READDIRPLUS;
1054 } else {
1055 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1056 server->namelen = NFS2_MAXNAMLEN;
1057 }
1058
David Howells54ceac42006-08-22 20:06:13 -04001059 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1060 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1061 if (error < 0) {
1062 dprintk("nfs_create_server: getattr error = %d\n", -error);
1063 goto error;
1064 }
1065 }
1066 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1067
David Howells6daabf12006-08-24 15:44:16 -04001068 dprintk("Server FSID: %llx:%llx\n",
1069 (unsigned long long) server->fsid.major,
1070 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001071
1072 BUG_ON(!server->nfs_client);
1073 BUG_ON(!server->nfs_client->rpc_ops);
1074 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1075
1076 spin_lock(&nfs_client_lock);
1077 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1078 list_add_tail(&server->master_link, &nfs_volume_list);
1079 spin_unlock(&nfs_client_lock);
1080
1081 server->mount_time = jiffies;
1082 return server;
1083
1084error:
1085 nfs_free_server(server);
1086 return ERR_PTR(error);
1087}
1088
1089#ifdef CONFIG_NFS_V4
1090/*
Andy Adamson557134a2009-04-01 09:21:53 -04001091 * Initialize the minor version specific parts of an NFS4 client record
1092 */
1093static int nfs4_init_client_minor_version(struct nfs_client *clp)
1094{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001095 clp->cl_call_sync = _nfs4_call_sync;
1096
Andy Adamson557134a2009-04-01 09:21:53 -04001097#if defined(CONFIG_NFS_V4_1)
1098 if (clp->cl_minorversion) {
1099 struct nfs4_session *session = NULL;
1100 /*
1101 * Create the session and mark it expired.
1102 * When a SEQUENCE operation encounters the expired session
1103 * it will do session recovery to initialize it.
1104 */
1105 session = nfs4_alloc_session(clp);
1106 if (!session)
1107 return -ENOMEM;
1108
1109 clp->cl_session = session;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001110 clp->cl_call_sync = _nfs4_call_sync_session;
Andy Adamson557134a2009-04-01 09:21:53 -04001111 }
1112#endif /* CONFIG_NFS_V4_1 */
1113
1114 return 0;
1115}
1116
1117/*
David Howells54ceac42006-08-22 20:06:13 -04001118 * Initialise an NFS4 client record
1119 */
1120static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001121 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001122 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001123 rpc_authflavor_t authflavour,
1124 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001125{
1126 int error;
1127
1128 if (clp->cl_cons_state == NFS_CS_READY) {
1129 /* the client is initialised already */
1130 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1131 return 0;
1132 }
1133
1134 /* Check NFS protocol revision and initialize RPC op vector */
1135 clp->rpc_ops = &nfs_v4_clientops;
1136
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001137 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001138 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001139 if (error < 0)
1140 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001141 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001142
1143 error = nfs_idmap_new(clp);
1144 if (error < 0) {
1145 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001146 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001147 goto error;
1148 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001149 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001150
Andy Adamson557134a2009-04-01 09:21:53 -04001151 error = nfs4_init_client_minor_version(clp);
1152 if (error < 0)
1153 goto error;
1154
Andy Adamson76db6d92009-04-01 09:22:38 -04001155 if (!nfs4_has_session(clp))
1156 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001157 return 0;
1158
1159error:
1160 nfs_mark_client_ready(clp, error);
1161 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1162 return error;
1163}
1164
1165/*
1166 * Set up an NFS4 client
1167 */
1168static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001169 const char *hostname,
1170 const struct sockaddr *addr,
1171 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001172 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001173 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001174 int proto, const struct rpc_timeout *timeparms,
1175 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001176{
Trond Myklebust3a498022007-12-14 14:56:04 -05001177 struct nfs_client_initdata cl_init = {
1178 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001179 .addr = addr,
1180 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001181 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001182 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001183 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001184 };
David Howells54ceac42006-08-22 20:06:13 -04001185 struct nfs_client *clp;
1186 int error;
1187
1188 dprintk("--> nfs4_set_client()\n");
1189
1190 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001191 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001192 if (IS_ERR(clp)) {
1193 error = PTR_ERR(clp);
1194 goto error;
1195 }
Chuck Leverd7403512008-12-23 15:21:37 -05001196 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1197 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001198 if (error < 0)
1199 goto error_put;
1200
1201 server->nfs_client = clp;
1202 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1203 return 0;
1204
1205error_put:
1206 nfs_put_client(clp);
1207error:
1208 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1209 return error;
1210}
1211
1212/*
Andy Adamson557134a2009-04-01 09:21:53 -04001213 * Initialize a session.
1214 * Note: save the mount rsize and wsize for create_server negotiation.
1215 */
1216static void nfs4_init_session(struct nfs_client *clp,
1217 unsigned int wsize, unsigned int rsize)
1218{
1219#if defined(CONFIG_NFS_V4_1)
1220 if (nfs4_has_session(clp)) {
1221 clp->cl_session->fc_attrs.max_rqst_sz = wsize;
1222 clp->cl_session->fc_attrs.max_resp_sz = rsize;
1223 }
1224#endif /* CONFIG_NFS_V4_1 */
1225}
1226
1227/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001228 * Session has been established, and the client marked ready.
1229 * Set the mount rsize and wsize with negotiated fore channel
1230 * attributes which will be bound checked in nfs_server_set_fsinfo.
1231 */
1232static void nfs4_session_set_rwsize(struct nfs_server *server)
1233{
1234#ifdef CONFIG_NFS_V4_1
1235 if (!nfs4_has_session(server->nfs_client))
1236 return;
1237 server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1238 server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
1239#endif /* CONFIG_NFS_V4_1 */
1240}
1241
1242/*
David Howells54ceac42006-08-22 20:06:13 -04001243 * Create a version 4 volume record
1244 */
1245static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001246 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001247{
Trond Myklebust33170232007-12-20 16:03:59 -05001248 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001249 int error;
1250
1251 dprintk("--> nfs4_init_server()\n");
1252
Trond Myklebust33170232007-12-20 16:03:59 -05001253 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1254 data->timeo, data->retrans);
1255
Chuck Lever542fcc32008-12-23 15:21:36 -05001256 /* Initialise the client representation from the mount data */
1257 server->flags = data->flags;
1258 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001259 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001260
Trond Myklebust33170232007-12-20 16:03:59 -05001261 /* Get a client record */
1262 error = nfs4_set_client(server,
1263 data->nfs_server.hostname,
1264 (const struct sockaddr *)&data->nfs_server.address,
1265 data->nfs_server.addrlen,
1266 data->client_address,
1267 data->auth_flavors[0],
1268 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001269 &timeparms,
1270 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001271 if (error < 0)
1272 goto error;
1273
David Howells54ceac42006-08-22 20:06:13 -04001274 if (data->rsize)
1275 server->rsize = nfs_block_size(data->rsize, NULL);
1276 if (data->wsize)
1277 server->wsize = nfs_block_size(data->wsize, NULL);
1278
1279 server->acregmin = data->acregmin * HZ;
1280 server->acregmax = data->acregmax * HZ;
1281 server->acdirmin = data->acdirmin * HZ;
1282 server->acdirmax = data->acdirmax * HZ;
1283
Chuck Leverf22d6d72008-03-14 14:10:22 -04001284 server->port = data->nfs_server.port;
1285
Trond Myklebust33170232007-12-20 16:03:59 -05001286 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001287
Trond Myklebust33170232007-12-20 16:03:59 -05001288error:
David Howells54ceac42006-08-22 20:06:13 -04001289 /* Done */
1290 dprintk("<-- nfs4_init_server() = %d\n", error);
1291 return error;
1292}
1293
1294/*
1295 * Create a version 4 volume record
1296 * - keyed on server and FSID
1297 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001298struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001299 struct nfs_fh *mntfh)
1300{
1301 struct nfs_fattr fattr;
1302 struct nfs_server *server;
1303 int error;
1304
1305 dprintk("--> nfs4_create_server()\n");
1306
1307 server = nfs_alloc_server();
1308 if (!server)
1309 return ERR_PTR(-ENOMEM);
1310
David Howells54ceac42006-08-22 20:06:13 -04001311 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001312 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001313 if (error < 0)
1314 goto error;
1315
1316 BUG_ON(!server->nfs_client);
1317 BUG_ON(!server->nfs_client->rpc_ops);
1318 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1319
Andy Adamson557134a2009-04-01 09:21:53 -04001320 nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
1321
David Howells54ceac42006-08-22 20:06:13 -04001322 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001323 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001324 if (error < 0)
1325 goto error;
1326
David Howells6daabf12006-08-24 15:44:16 -04001327 dprintk("Server FSID: %llx:%llx\n",
1328 (unsigned long long) server->fsid.major,
1329 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001330 dprintk("Mount FH: %d\n", mntfh->size);
1331
Andy Adamson96b09e02009-04-01 09:22:33 -04001332 nfs4_session_set_rwsize(server);
1333
David Howells54ceac42006-08-22 20:06:13 -04001334 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1335 if (error < 0)
1336 goto error;
1337
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001338 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1339 server->namelen = NFS4_MAXNAMLEN;
1340
David Howells54ceac42006-08-22 20:06:13 -04001341 BUG_ON(!server->nfs_client);
1342 BUG_ON(!server->nfs_client->rpc_ops);
1343 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1344
1345 spin_lock(&nfs_client_lock);
1346 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1347 list_add_tail(&server->master_link, &nfs_volume_list);
1348 spin_unlock(&nfs_client_lock);
1349
1350 server->mount_time = jiffies;
1351 dprintk("<-- nfs4_create_server() = %p\n", server);
1352 return server;
1353
1354error:
1355 nfs_free_server(server);
1356 dprintk("<-- nfs4_create_server() = error %d\n", error);
1357 return ERR_PTR(error);
1358}
1359
1360/*
1361 * Create an NFS4 referral server record
1362 */
1363struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001364 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001365{
1366 struct nfs_client *parent_client;
1367 struct nfs_server *server, *parent_server;
1368 struct nfs_fattr fattr;
1369 int error;
1370
1371 dprintk("--> nfs4_create_referral_server()\n");
1372
1373 server = nfs_alloc_server();
1374 if (!server)
1375 return ERR_PTR(-ENOMEM);
1376
1377 parent_server = NFS_SB(data->sb);
1378 parent_client = parent_server->nfs_client;
1379
Chuck Lever542fcc32008-12-23 15:21:36 -05001380 /* Initialise the client representation from the parent server */
1381 nfs_server_copy_userdata(server, parent_server);
1382 server->caps |= NFS_CAP_ATOMIC_OPEN;
1383
David Howells54ceac42006-08-22 20:06:13 -04001384 /* Get a client representation.
1385 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001386 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001387 data->addr,
1388 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001389 parent_client->cl_ipaddr,
1390 data->authflavor,
1391 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001392 parent_server->client->cl_timeout,
1393 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001394 if (error < 0)
1395 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001396
Trond Myklebust33170232007-12-20 16:03:59 -05001397 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001398 if (error < 0)
1399 goto error;
1400
1401 BUG_ON(!server->nfs_client);
1402 BUG_ON(!server->nfs_client->rpc_ops);
1403 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1404
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001405 /* Probe the root fh to retrieve its FSID and filehandle */
1406 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1407 if (error < 0)
1408 goto error;
1409
David Howells54ceac42006-08-22 20:06:13 -04001410 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001411 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001412 if (error < 0)
1413 goto error;
1414
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001415 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1416 server->namelen = NFS4_MAXNAMLEN;
1417
David Howells54ceac42006-08-22 20:06:13 -04001418 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001419 (unsigned long long) server->fsid.major,
1420 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001421
1422 spin_lock(&nfs_client_lock);
1423 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1424 list_add_tail(&server->master_link, &nfs_volume_list);
1425 spin_unlock(&nfs_client_lock);
1426
1427 server->mount_time = jiffies;
1428
1429 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1430 return server;
1431
1432error:
1433 nfs_free_server(server);
1434 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1435 return ERR_PTR(error);
1436}
1437
1438#endif /* CONFIG_NFS_V4 */
1439
1440/*
1441 * Clone an NFS2, NFS3 or NFS4 server record
1442 */
1443struct nfs_server *nfs_clone_server(struct nfs_server *source,
1444 struct nfs_fh *fh,
1445 struct nfs_fattr *fattr)
1446{
1447 struct nfs_server *server;
1448 struct nfs_fattr fattr_fsinfo;
1449 int error;
1450
1451 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001452 (unsigned long long) fattr->fsid.major,
1453 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001454
1455 server = nfs_alloc_server();
1456 if (!server)
1457 return ERR_PTR(-ENOMEM);
1458
1459 /* Copy data from the source */
1460 server->nfs_client = source->nfs_client;
1461 atomic_inc(&server->nfs_client->cl_count);
1462 nfs_server_copy_userdata(server, source);
1463
1464 server->fsid = fattr->fsid;
1465
Trond Myklebust33170232007-12-20 16:03:59 -05001466 error = nfs_init_server_rpcclient(server,
1467 source->client->cl_timeout,
1468 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001469 if (error < 0)
1470 goto out_free_server;
1471 if (!IS_ERR(source->client_acl))
1472 nfs_init_server_aclclient(server);
1473
1474 /* probe the filesystem info for this server filesystem */
1475 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1476 if (error < 0)
1477 goto out_free_server;
1478
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001479 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1480 server->namelen = NFS4_MAXNAMLEN;
1481
David Howells54ceac42006-08-22 20:06:13 -04001482 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001483 (unsigned long long) server->fsid.major,
1484 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001485
1486 error = nfs_start_lockd(server);
1487 if (error < 0)
1488 goto out_free_server;
1489
1490 spin_lock(&nfs_client_lock);
1491 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1492 list_add_tail(&server->master_link, &nfs_volume_list);
1493 spin_unlock(&nfs_client_lock);
1494
1495 server->mount_time = jiffies;
1496
1497 dprintk("<-- nfs_clone_server() = %p\n", server);
1498 return server;
1499
1500out_free_server:
1501 nfs_free_server(server);
1502 dprintk("<-- nfs_clone_server() = error %d\n", error);
1503 return ERR_PTR(error);
1504}
David Howells6aaca562006-08-22 20:06:13 -04001505
1506#ifdef CONFIG_PROC_FS
1507static struct proc_dir_entry *proc_fs_nfs;
1508
1509static int nfs_server_list_open(struct inode *inode, struct file *file);
1510static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1511static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1512static void nfs_server_list_stop(struct seq_file *p, void *v);
1513static int nfs_server_list_show(struct seq_file *m, void *v);
1514
1515static struct seq_operations nfs_server_list_ops = {
1516 .start = nfs_server_list_start,
1517 .next = nfs_server_list_next,
1518 .stop = nfs_server_list_stop,
1519 .show = nfs_server_list_show,
1520};
1521
Arjan van de Ven00977a52007-02-12 00:55:34 -08001522static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001523 .open = nfs_server_list_open,
1524 .read = seq_read,
1525 .llseek = seq_lseek,
1526 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001527 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001528};
1529
1530static int nfs_volume_list_open(struct inode *inode, struct file *file);
1531static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1532static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1533static void nfs_volume_list_stop(struct seq_file *p, void *v);
1534static int nfs_volume_list_show(struct seq_file *m, void *v);
1535
1536static struct seq_operations nfs_volume_list_ops = {
1537 .start = nfs_volume_list_start,
1538 .next = nfs_volume_list_next,
1539 .stop = nfs_volume_list_stop,
1540 .show = nfs_volume_list_show,
1541};
1542
Arjan van de Ven00977a52007-02-12 00:55:34 -08001543static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001544 .open = nfs_volume_list_open,
1545 .read = seq_read,
1546 .llseek = seq_lseek,
1547 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001548 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001549};
1550
1551/*
1552 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1553 * we're dealing
1554 */
1555static int nfs_server_list_open(struct inode *inode, struct file *file)
1556{
1557 struct seq_file *m;
1558 int ret;
1559
1560 ret = seq_open(file, &nfs_server_list_ops);
1561 if (ret < 0)
1562 return ret;
1563
1564 m = file->private_data;
1565 m->private = PDE(inode)->data;
1566
1567 return 0;
1568}
1569
1570/*
1571 * set up the iterator to start reading from the server list and return the first item
1572 */
1573static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1574{
David Howells6aaca562006-08-22 20:06:13 -04001575 /* lock the list against modification */
1576 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001577 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001578}
1579
1580/*
1581 * move to next server
1582 */
1583static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1584{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001585 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001586}
1587
1588/*
1589 * clean up after reading from the transports list
1590 */
1591static void nfs_server_list_stop(struct seq_file *p, void *v)
1592{
1593 spin_unlock(&nfs_client_lock);
1594}
1595
1596/*
1597 * display a header line followed by a load of call lines
1598 */
1599static int nfs_server_list_show(struct seq_file *m, void *v)
1600{
1601 struct nfs_client *clp;
1602
1603 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001604 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001605 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1606 return 0;
1607 }
1608
1609 /* display one transport per line on subsequent lines */
1610 clp = list_entry(v, struct nfs_client, cl_share_link);
1611
Chuck Lever5d8515c2007-12-10 14:57:16 -05001612 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001613 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001614 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1615 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001616 atomic_read(&clp->cl_count),
1617 clp->cl_hostname);
1618
1619 return 0;
1620}
1621
1622/*
1623 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1624 */
1625static int nfs_volume_list_open(struct inode *inode, struct file *file)
1626{
1627 struct seq_file *m;
1628 int ret;
1629
1630 ret = seq_open(file, &nfs_volume_list_ops);
1631 if (ret < 0)
1632 return ret;
1633
1634 m = file->private_data;
1635 m->private = PDE(inode)->data;
1636
1637 return 0;
1638}
1639
1640/*
1641 * set up the iterator to start reading from the volume list and return the first item
1642 */
1643static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1644{
David Howells6aaca562006-08-22 20:06:13 -04001645 /* lock the list against modification */
1646 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001647 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001648}
1649
1650/*
1651 * move to next volume
1652 */
1653static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1654{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001655 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001656}
1657
1658/*
1659 * clean up after reading from the transports list
1660 */
1661static void nfs_volume_list_stop(struct seq_file *p, void *v)
1662{
1663 spin_unlock(&nfs_client_lock);
1664}
1665
1666/*
1667 * display a header line followed by a load of call lines
1668 */
1669static int nfs_volume_list_show(struct seq_file *m, void *v)
1670{
1671 struct nfs_server *server;
1672 struct nfs_client *clp;
1673 char dev[8], fsid[17];
1674
1675 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001676 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001677 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001678 return 0;
1679 }
1680 /* display one transport per line on subsequent lines */
1681 server = list_entry(v, struct nfs_server, master_link);
1682 clp = server->nfs_client;
1683
1684 snprintf(dev, 8, "%u:%u",
1685 MAJOR(server->s_dev), MINOR(server->s_dev));
1686
1687 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001688 (unsigned long long) server->fsid.major,
1689 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001690
David Howells5d1acff2009-04-03 16:42:47 +01001691 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001692 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001693 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1694 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001695 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001696 fsid,
1697 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001698
1699 return 0;
1700}
1701
1702/*
1703 * initialise the /proc/fs/nfsfs/ directory
1704 */
1705int __init nfs_fs_proc_init(void)
1706{
1707 struct proc_dir_entry *p;
1708
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001709 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001710 if (!proc_fs_nfs)
1711 goto error_0;
1712
David Howells6aaca562006-08-22 20:06:13 -04001713 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001714 p = proc_create("servers", S_IFREG|S_IRUGO,
1715 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001716 if (!p)
1717 goto error_1;
1718
David Howells6aaca562006-08-22 20:06:13 -04001719 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001720 p = proc_create("volumes", S_IFREG|S_IRUGO,
1721 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001722 if (!p)
1723 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001724 return 0;
1725
1726error_2:
1727 remove_proc_entry("servers", proc_fs_nfs);
1728error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001729 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001730error_0:
1731 return -ENOMEM;
1732}
1733
1734/*
1735 * clean up the /proc/fs/nfsfs/ directory
1736 */
1737void nfs_fs_proc_exit(void)
1738{
1739 remove_proc_entry("volumes", proc_fs_nfs);
1740 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001741 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001742}
1743
1744#endif /* CONFIG_PROC_FS */