blob: 4f75ec593be8b8395899b44fa63fafa1742e8eb7 [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>
Andy Adamson0b5b7ae2009-04-01 09:23:15 -040040#include <linux/sunrpc/bc_xprt.h>
David Howells24c8dbb2006-08-22 20:06:10 -040041
42#include <asm/system.h>
43
44#include "nfs4_fs.h"
45#include "callback.h"
46#include "delegation.h"
47#include "iostat.h"
48#include "internal.h"
David Howells14727282009-04-03 16:42:42 +010049#include "fscache.h"
David Howells24c8dbb2006-08-22 20:06:10 -040050
51#define NFSDBG_FACILITY NFSDBG_CLIENT
52
53static DEFINE_SPINLOCK(nfs_client_lock);
54static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040055static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040056static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
57
58/*
David Howells5006a762006-08-22 20:06:12 -040059 * RPC cruft for NFS
60 */
61static struct rpc_version *nfs_version[5] = {
62 [2] = &nfs_version2,
63#ifdef CONFIG_NFS_V3
64 [3] = &nfs_version3,
65#endif
66#ifdef CONFIG_NFS_V4
67 [4] = &nfs_version4,
68#endif
69};
70
71struct rpc_program nfs_program = {
72 .name = "nfs",
73 .number = NFS_PROGRAM,
74 .nrvers = ARRAY_SIZE(nfs_version),
75 .version = nfs_version,
76 .stats = &nfs_rpcstat,
77 .pipe_dir_name = "/nfs",
78};
79
80struct rpc_stat nfs_rpcstat = {
81 .program = &nfs_program
82};
83
84
85#ifdef CONFIG_NFS_V3_ACL
86static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
87static struct rpc_version * nfsacl_version[] = {
88 [3] = &nfsacl_version3,
89};
90
91struct rpc_program nfsacl_program = {
92 .name = "nfsacl",
93 .number = NFS_ACL_PROGRAM,
94 .nrvers = ARRAY_SIZE(nfsacl_version),
95 .version = nfsacl_version,
96 .stats = &nfsacl_rpcstat,
97};
98#endif /* CONFIG_NFS_V3_ACL */
99
Trond Myklebust3a498022007-12-14 14:56:04 -0500100struct nfs_client_initdata {
101 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500102 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500103 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500104 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500105 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400106 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500107};
108
David Howells5006a762006-08-22 20:06:12 -0400109/*
David Howells24c8dbb2006-08-22 20:06:10 -0400110 * Allocate a shared client record
111 *
112 * Since these are allocated/deallocated very rarely, we don't
113 * bother putting them in a slab cache...
114 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500115static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400116{
117 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400118 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400119
120 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
121 goto error_0;
122
Trond Myklebust40c553192007-12-14 14:56:07 -0500123 clp->rpc_ops = cl_init->rpc_ops;
124
David Howells24c8dbb2006-08-22 20:06:10 -0400125 atomic_set(&clp->cl_count, 1);
126 clp->cl_cons_state = NFS_CS_INITING;
127
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500128 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
129 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400130
Trond Myklebust3a498022007-12-14 14:56:04 -0500131 if (cl_init->hostname) {
132 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400133 if (!clp->cl_hostname)
Benny Halevy71468512009-04-01 09:22:56 -0400134 goto error_cleanup;
David Howells24c8dbb2006-08-22 20:06:10 -0400135 }
136
137 INIT_LIST_HEAD(&clp->cl_superblocks);
138 clp->cl_rpcclient = ERR_PTR(-EINVAL);
139
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500140 clp->cl_proto = cl_init->proto;
141
David Howells24c8dbb2006-08-22 20:06:10 -0400142#ifdef CONFIG_NFS_V4
David Howells24c8dbb2006-08-22 20:06:10 -0400143 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400144 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000145 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400146 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
147 clp->cl_boot_time = CURRENT_TIME;
148 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400149 clp->cl_minorversion = cl_init->minorversion;
David Howells24c8dbb2006-08-22 20:06:10 -0400150#endif
Trond Myklebust7c67db32008-04-07 20:50:11 -0400151 cred = rpc_lookup_machine_cred();
152 if (!IS_ERR(cred))
153 clp->cl_machine_cred = cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400154
David Howells14727282009-04-03 16:42:42 +0100155 nfs_fscache_get_client_cookie(clp);
156
David Howells24c8dbb2006-08-22 20:06:10 -0400157 return clp;
158
Benny Halevy71468512009-04-01 09:22:56 -0400159error_cleanup:
David Howells24c8dbb2006-08-22 20:06:10 -0400160 kfree(clp);
161error_0:
162 return NULL;
163}
164
Trond Myklebust5dd31772006-08-24 01:03:05 -0400165static void nfs4_shutdown_client(struct nfs_client *clp)
166{
167#ifdef CONFIG_NFS_V4
168 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
169 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400170 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400171 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
172 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500173
174 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400175#endif
176}
177
David Howells24c8dbb2006-08-22 20:06:10 -0400178/*
Benny Halevy9bdaa862009-04-01 09:22:55 -0400179 * Destroy the NFS4 callback service
180 */
181static void nfs4_destroy_callback(struct nfs_client *clp)
182{
183#ifdef CONFIG_NFS_V4
184 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
Andy Adamsone82dc222009-04-01 09:23:14 -0400185 nfs_callback_down(clp->cl_minorversion);
Benny Halevy9bdaa862009-04-01 09:22:55 -0400186#endif /* CONFIG_NFS_V4 */
187}
188
189/*
Andy Adamson557134a2009-04-01 09:21:53 -0400190 * Clears/puts all minor version specific parts from an nfs_client struct
191 * reverting it to minorversion 0.
192 */
193static void nfs4_clear_client_minor_version(struct nfs_client *clp)
194{
195#ifdef CONFIG_NFS_V4_1
196 if (nfs4_has_session(clp)) {
197 nfs4_destroy_session(clp->cl_session);
198 clp->cl_session = NULL;
199 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400200
201 clp->cl_call_sync = _nfs4_call_sync;
Andy Adamson557134a2009-04-01 09:21:53 -0400202#endif /* CONFIG_NFS_V4_1 */
Benny Halevy71468512009-04-01 09:22:56 -0400203
204 nfs4_destroy_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -0400205}
206
207/*
David Howells24c8dbb2006-08-22 20:06:10 -0400208 * Destroy a shared client record
209 */
210static void nfs_free_client(struct nfs_client *clp)
211{
Trond Myklebust40c553192007-12-14 14:56:07 -0500212 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400213
Andy Adamson557134a2009-04-01 09:21:53 -0400214 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400215 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400216
David Howells14727282009-04-03 16:42:42 +0100217 nfs_fscache_release_client_cookie(clp);
218
David Howells24c8dbb2006-08-22 20:06:10 -0400219 /* -EIO all pending I/O */
220 if (!IS_ERR(clp->cl_rpcclient))
221 rpc_shutdown_client(clp->cl_rpcclient);
222
Trond Myklebust7c67db32008-04-07 20:50:11 -0400223 if (clp->cl_machine_cred != NULL)
224 put_rpccred(clp->cl_machine_cred);
225
David Howells24c8dbb2006-08-22 20:06:10 -0400226 kfree(clp->cl_hostname);
227 kfree(clp);
228
229 dprintk("<-- nfs_free_client()\n");
230}
231
232/*
233 * Release a reference to a shared client record
234 */
235void nfs_put_client(struct nfs_client *clp)
236{
David Howells27ba8512006-07-30 14:40:56 -0400237 if (!clp)
238 return;
239
David Howells24c8dbb2006-08-22 20:06:10 -0400240 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
241
242 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
243 list_del(&clp->cl_share_link);
244 spin_unlock(&nfs_client_lock);
245
246 BUG_ON(!list_empty(&clp->cl_superblocks));
247
248 nfs_free_client(clp);
249 }
250}
251
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500252#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400253/*
254 * Test if two ip6 socket addresses refer to the same socket by
255 * comparing relevant fields. The padding bytes specifically, are not
256 * compared. sin6_flowinfo is not compared because it only affects QoS
257 * and sin6_scope_id is only compared if the address is "link local"
258 * because "link local" addresses need only be unique to a specific
259 * link. Conversely, ordinary unicast addresses might have different
260 * sin6_scope_id.
261 *
262 * The caller should ensure both socket addresses are AF_INET6.
263 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400264static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
265 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400266{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400267 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
268 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400269
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400270 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
271 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400272 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500273
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400274 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500275}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400276#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
277static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
278 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400279{
280 return 0;
281}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500282#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500283
David Howells24c8dbb2006-08-22 20:06:10 -0400284/*
Ian Dalld7371c42009-03-10 20:33:22 -0400285 * Test if two ip4 socket addresses refer to the same socket, by
286 * comparing relevant fields. The padding bytes specifically, are
287 * not compared.
288 *
289 * The caller should ensure both socket addresses are AF_INET.
290 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400291static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
292 const struct sockaddr *sa2)
293{
294 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
295 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
296
297 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
298}
299
300static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
301 const struct sockaddr *sa2)
302{
303 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
304 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
305
306 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
307 (sin1->sin6_port == sin2->sin6_port);
308}
309
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400310static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
311 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400312{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400313 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
314 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400315
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400316 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
317 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400318}
319
320/*
Ian Dalld7371c42009-03-10 20:33:22 -0400321 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400322 * by comparing (only) relevant fields, excluding the port number.
323 */
324static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
325 const struct sockaddr *sa2)
326{
327 if (sa1->sa_family != sa2->sa_family)
328 return 0;
329
330 switch (sa1->sa_family) {
331 case AF_INET:
332 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
333 case AF_INET6:
334 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
335 }
336 return 0;
337}
338
339/*
340 * Test if two socket addresses represent the same actual socket,
341 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400342 */
343static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
344 const struct sockaddr *sa2)
345{
346 if (sa1->sa_family != sa2->sa_family)
347 return 0;
348
349 switch (sa1->sa_family) {
350 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400351 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400352 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400353 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400354 }
355 return 0;
356}
357
358/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500359 * Find a client by IP address and protocol version
360 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400361 */
Chuck Leverff052642007-12-10 14:58:44 -0500362struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500363{
364 struct nfs_client *clp;
365
366 spin_lock(&nfs_client_lock);
367 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500368 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
369
Trond Myklebustc81468a2007-12-14 14:56:05 -0500370 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400371 if (!(clp->cl_cons_state == NFS_CS_READY ||
372 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500373 continue;
374
375 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500376 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500377 continue;
378
379 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500380 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500381 continue;
382
383 atomic_inc(&clp->cl_count);
384 spin_unlock(&nfs_client_lock);
385 return clp;
386 }
387 spin_unlock(&nfs_client_lock);
388 return NULL;
389}
390
391/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500392 * Find a client by IP address and protocol version
393 * - returns NULL if no such client
394 */
395struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
396{
397 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
398 u32 nfsvers = clp->rpc_ops->version;
399
400 spin_lock(&nfs_client_lock);
401 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
402 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
403
404 /* Don't match clients that failed to initialise properly */
405 if (clp->cl_cons_state != NFS_CS_READY)
406 continue;
407
408 /* Different NFS versions cannot share the same nfs_client */
409 if (clp->rpc_ops->version != nfsvers)
410 continue;
411
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500412 /* Match only the IP address, not the port number */
413 if (!nfs_sockaddr_match_ipaddr(sap, clap))
414 continue;
415
416 atomic_inc(&clp->cl_count);
417 spin_unlock(&nfs_client_lock);
418 return clp;
419 }
420 spin_unlock(&nfs_client_lock);
421 return NULL;
422}
423
424/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500425 * Find an nfs_client on the list that matches the initialisation data
426 * that is supplied.
427 */
428static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400429{
430 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400431 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400432
433 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400434 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700435 /* Don't match clients that failed to initialise properly */
436 if (clp->cl_cons_state < 0)
437 continue;
438
David Howells24c8dbb2006-08-22 20:06:10 -0400439 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500440 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400441 continue;
442
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500443 if (clp->cl_proto != data->proto)
444 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400445 /* Match nfsv4 minorversion */
446 if (clp->cl_minorversion != data->minorversion)
447 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500448 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400449 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400450 continue;
451
Trond Myklebustc81468a2007-12-14 14:56:05 -0500452 atomic_inc(&clp->cl_count);
453 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400454 }
David Howells24c8dbb2006-08-22 20:06:10 -0400455 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400456}
457
458/*
459 * Look up a client by IP address and protocol version
460 * - creates a new record if one doesn't yet exist
461 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500462static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400463{
464 struct nfs_client *clp, *new = NULL;
465 int error;
466
Chuck Leverd7422c42007-12-10 14:58:51 -0500467 dprintk("--> nfs_get_client(%s,v%u)\n",
468 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400469
470 /* see if the client already exists */
471 do {
472 spin_lock(&nfs_client_lock);
473
Trond Myklebustc81468a2007-12-14 14:56:05 -0500474 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400475 if (clp)
476 goto found_client;
477 if (new)
478 goto install_client;
479
480 spin_unlock(&nfs_client_lock);
481
Trond Myklebust3a498022007-12-14 14:56:04 -0500482 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400483 } while (new);
484
485 return ERR_PTR(-ENOMEM);
486
487 /* install a new client and return with it unready */
488install_client:
489 clp = new;
490 list_add(&clp->cl_share_link, &nfs_client_list);
491 spin_unlock(&nfs_client_lock);
492 dprintk("--> nfs_get_client() = %p [new]\n", clp);
493 return clp;
494
495 /* found an existing client
496 * - make sure it's ready before returning
497 */
498found_client:
499 spin_unlock(&nfs_client_lock);
500
501 if (new)
502 nfs_free_client(new);
503
Matthew Wilcox150030b2007-12-06 16:24:39 -0500504 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400505 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400506 if (error < 0) {
507 nfs_put_client(clp);
508 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400509 }
510
511 if (clp->cl_cons_state < NFS_CS_READY) {
512 error = clp->cl_cons_state;
513 nfs_put_client(clp);
514 return ERR_PTR(error);
515 }
516
David Howells54ceac42006-08-22 20:06:13 -0400517 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
518
David Howells24c8dbb2006-08-22 20:06:10 -0400519 dprintk("--> nfs_get_client() = %p [share]\n", clp);
520 return clp;
521}
522
523/*
524 * Mark a server as ready or failed
525 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400526void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400527{
528 clp->cl_cons_state = state;
529 wake_up_all(&nfs_client_active_wq);
530}
David Howells5006a762006-08-22 20:06:12 -0400531
532/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400533 * With sessions, the client is not marked ready until after a
534 * successful EXCHANGE_ID and CREATE_SESSION.
535 *
536 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
537 * other versions of NFS can be tried.
538 */
539int nfs4_check_client_ready(struct nfs_client *clp)
540{
541 if (!nfs4_has_session(clp))
542 return 0;
543 if (clp->cl_cons_state < NFS_CS_READY)
544 return -EPROTONOSUPPORT;
545 return 0;
546}
547
548/*
David Howells5006a762006-08-22 20:06:12 -0400549 * Initialise the timeout values for a connection
550 */
551static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
552 unsigned int timeo, unsigned int retrans)
553{
554 to->to_initval = timeo * HZ / 10;
555 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400556
557 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400558 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400559 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400560 if (to->to_retries == 0)
561 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500562 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400563 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400564 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
565 to->to_initval = NFS_MAX_TCP_TIMEOUT;
566 to->to_increment = to->to_initval;
567 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500568 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
569 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
570 if (to->to_maxval < to->to_initval)
571 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400572 to->to_exponential = 0;
573 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400574 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400575 if (to->to_retries == 0)
576 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400577 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400578 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400579 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
580 to->to_initval = NFS_MAX_UDP_TIMEOUT;
581 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
582 to->to_exponential = 1;
583 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400584 default:
585 BUG();
David Howells5006a762006-08-22 20:06:12 -0400586 }
587}
588
589/*
590 * Create an RPC client handle
591 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500592static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500593 const struct rpc_timeout *timeparms,
594 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500595 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400596{
David Howells5006a762006-08-22 20:06:12 -0400597 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400598 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500599 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400600 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500601 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500602 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400603 .servername = clp->cl_hostname,
604 .program = &nfs_program,
605 .version = clp->rpc_ops->version,
606 .authflavor = flavor,
607 };
David Howells5006a762006-08-22 20:06:12 -0400608
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500609 if (discrtry)
610 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
611 if (noresvport)
612 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
613
David Howells5006a762006-08-22 20:06:12 -0400614 if (!IS_ERR(clp->cl_rpcclient))
615 return 0;
616
Chuck Lever41877d22006-08-22 20:06:20 -0400617 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400618 if (IS_ERR(clnt)) {
619 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700620 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400621 return PTR_ERR(clnt);
622 }
623
David Howells5006a762006-08-22 20:06:12 -0400624 clp->cl_rpcclient = clnt;
625 return 0;
626}
David Howells54ceac42006-08-22 20:06:13 -0400627
628/*
629 * Version 2 or 3 client destruction
630 */
631static void nfs_destroy_server(struct nfs_server *server)
632{
David Howells54ceac42006-08-22 20:06:13 -0400633 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500634 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400635}
636
637/*
638 * Version 2 or 3 lockd setup
639 */
640static int nfs_start_lockd(struct nfs_server *server)
641{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500642 struct nlm_host *host;
643 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500644 struct nlmclnt_initdata nlm_init = {
645 .hostname = clp->cl_hostname,
646 .address = (struct sockaddr *)&clp->cl_addr,
647 .addrlen = clp->cl_addrlen,
648 .protocol = server->flags & NFS_MOUNT_TCP ?
649 IPPROTO_TCP : IPPROTO_UDP,
650 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500651 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
652 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500653 };
David Howells54ceac42006-08-22 20:06:13 -0400654
Chuck Lever883bb162008-01-15 16:04:20 -0500655 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500656 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400657 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500658 return 0;
659
Chuck Lever883bb162008-01-15 16:04:20 -0500660 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500661 if (IS_ERR(host))
662 return PTR_ERR(host);
663
664 server->nlm_host = host;
665 server->destroy = nfs_destroy_server;
666 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400667}
668
669/*
670 * Initialise an NFSv3 ACL client connection
671 */
672#ifdef CONFIG_NFS_V3_ACL
673static void nfs_init_server_aclclient(struct nfs_server *server)
674{
Trond Myklebust40c553192007-12-14 14:56:07 -0500675 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400676 goto out_noacl;
677 if (server->flags & NFS_MOUNT_NOACL)
678 goto out_noacl;
679
680 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
681 if (IS_ERR(server->client_acl))
682 goto out_noacl;
683
684 /* No errors! Assume that Sun nfsacls are supported */
685 server->caps |= NFS_CAP_ACLS;
686 return;
687
688out_noacl:
689 server->caps &= ~NFS_CAP_ACLS;
690}
691#else
692static inline void nfs_init_server_aclclient(struct nfs_server *server)
693{
694 server->flags &= ~NFS_MOUNT_NOACL;
695 server->caps &= ~NFS_CAP_ACLS;
696}
697#endif
698
699/*
700 * Create a general RPC client
701 */
Trond Myklebust33170232007-12-20 16:03:59 -0500702static int nfs_init_server_rpcclient(struct nfs_server *server,
703 const struct rpc_timeout *timeo,
704 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400705{
706 struct nfs_client *clp = server->nfs_client;
707
708 server->client = rpc_clone_client(clp->cl_rpcclient);
709 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700710 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400711 return PTR_ERR(server->client);
712 }
713
Trond Myklebust33170232007-12-20 16:03:59 -0500714 memcpy(&server->client->cl_timeout_default,
715 timeo,
716 sizeof(server->client->cl_timeout_default));
717 server->client->cl_timeout = &server->client->cl_timeout_default;
718
David Howells54ceac42006-08-22 20:06:13 -0400719 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
720 struct rpc_auth *auth;
721
722 auth = rpcauth_create(pseudoflavour, server->client);
723 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700724 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400725 return PTR_ERR(auth);
726 }
727 }
728 server->client->cl_softrtry = 0;
729 if (server->flags & NFS_MOUNT_SOFT)
730 server->client->cl_softrtry = 1;
731
David Howells54ceac42006-08-22 20:06:13 -0400732 return 0;
733}
734
735/*
736 * Initialise an NFS2 or NFS3 client
737 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400738static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500739 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400740 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400741{
David Howells54ceac42006-08-22 20:06:13 -0400742 int error;
743
744 if (clp->cl_cons_state == NFS_CS_READY) {
745 /* the client is already initialised */
746 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
747 return 0;
748 }
749
David Howells54ceac42006-08-22 20:06:13 -0400750 /*
751 * Create a client RPC handle for doing FSSTAT with UNIX auth only
752 * - RFC 2623, sec 2.3.2
753 */
Chuck Leverd7403512008-12-23 15:21:37 -0500754 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
755 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400756 if (error < 0)
757 goto error;
758 nfs_mark_client_ready(clp, NFS_CS_READY);
759 return 0;
760
761error:
762 nfs_mark_client_ready(clp, error);
763 dprintk("<-- nfs_init_client() = xerror %d\n", error);
764 return error;
765}
766
767/*
768 * Create a version 2 or 3 client
769 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400770static int nfs_init_server(struct nfs_server *server,
771 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400772{
Trond Myklebust3a498022007-12-14 14:56:04 -0500773 struct nfs_client_initdata cl_init = {
774 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500775 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500776 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500777 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500778 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500779 };
Trond Myklebust33170232007-12-20 16:03:59 -0500780 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400781 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500782 int error;
David Howells54ceac42006-08-22 20:06:13 -0400783
784 dprintk("--> nfs_init_server()\n");
785
786#ifdef CONFIG_NFS_V3
787 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500788 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400789#endif
790
791 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500792 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400793 if (IS_ERR(clp)) {
794 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
795 return PTR_ERR(clp);
796 }
797
Trond Myklebust33170232007-12-20 16:03:59 -0500798 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
799 data->timeo, data->retrans);
800 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400801 if (error < 0)
802 goto error;
803
804 server->nfs_client = clp;
805
806 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400807 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100808 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400809
810 if (data->rsize)
811 server->rsize = nfs_block_size(data->rsize, NULL);
812 if (data->wsize)
813 server->wsize = nfs_block_size(data->wsize, NULL);
814
815 server->acregmin = data->acregmin * HZ;
816 server->acregmax = data->acregmax * HZ;
817 server->acdirmin = data->acdirmin * HZ;
818 server->acdirmax = data->acdirmax * HZ;
819
820 /* Start lockd here, before we might error out */
821 error = nfs_start_lockd(server);
822 if (error < 0)
823 goto error;
824
Chuck Leverf22d6d72008-03-14 14:10:22 -0400825 server->port = data->nfs_server.port;
826
Trond Myklebust33170232007-12-20 16:03:59 -0500827 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400828 if (error < 0)
829 goto error;
830
Chuck Lever3f8400d2008-03-14 14:10:30 -0400831 /* Preserve the values of mount_server-related mount options */
832 if (data->mount_server.addrlen) {
833 memcpy(&server->mountd_address, &data->mount_server.address,
834 data->mount_server.addrlen);
835 server->mountd_addrlen = data->mount_server.addrlen;
836 }
837 server->mountd_version = data->mount_server.version;
838 server->mountd_port = data->mount_server.port;
839 server->mountd_protocol = data->mount_server.protocol;
840
David Howells54ceac42006-08-22 20:06:13 -0400841 server->namelen = data->namlen;
842 /* Create a client RPC handle for the NFSv3 ACL management interface */
843 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400844 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
845 return 0;
846
847error:
848 server->nfs_client = NULL;
849 nfs_put_client(clp);
850 dprintk("<-- nfs_init_server() = xerror %d\n", error);
851 return error;
852}
853
854/*
855 * Load up the server record from information gained in an fsinfo record
856 */
857static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
858{
859 unsigned long max_rpc_payload;
860
861 /* Work out a lot of parameters */
862 if (server->rsize == 0)
863 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
864 if (server->wsize == 0)
865 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
866
867 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
868 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
869 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
870 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
871
872 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
873 if (server->rsize > max_rpc_payload)
874 server->rsize = max_rpc_payload;
875 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
876 server->rsize = NFS_MAX_FILE_IO_SIZE;
877 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700878
David Howells54ceac42006-08-22 20:06:13 -0400879 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
880
881 if (server->wsize > max_rpc_payload)
882 server->wsize = max_rpc_payload;
883 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
884 server->wsize = NFS_MAX_FILE_IO_SIZE;
885 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
886 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
887
888 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
889 if (server->dtsize > PAGE_CACHE_SIZE)
890 server->dtsize = PAGE_CACHE_SIZE;
891 if (server->dtsize > server->rsize)
892 server->dtsize = server->rsize;
893
894 if (server->flags & NFS_MOUNT_NOAC) {
895 server->acregmin = server->acregmax = 0;
896 server->acdirmin = server->acdirmax = 0;
897 }
898
899 server->maxfilesize = fsinfo->maxfilesize;
900
901 /* We're airborne Set socket buffersize */
902 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
903}
904
905/*
906 * Probe filesystem information, including the FSID on v2/v3
907 */
908static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
909{
910 struct nfs_fsinfo fsinfo;
911 struct nfs_client *clp = server->nfs_client;
912 int error;
913
914 dprintk("--> nfs_probe_fsinfo()\n");
915
916 if (clp->rpc_ops->set_capabilities != NULL) {
917 error = clp->rpc_ops->set_capabilities(server, mntfh);
918 if (error < 0)
919 goto out_error;
920 }
921
922 fsinfo.fattr = fattr;
923 nfs_fattr_init(fattr);
924 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
925 if (error < 0)
926 goto out_error;
927
928 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700929 error = bdi_init(&server->backing_dev_info);
930 if (error)
931 goto out_error;
932
David Howells54ceac42006-08-22 20:06:13 -0400933
934 /* Get some general file system info */
935 if (server->namelen == 0) {
936 struct nfs_pathconf pathinfo;
937
938 pathinfo.fattr = fattr;
939 nfs_fattr_init(fattr);
940
941 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
942 server->namelen = pathinfo.max_namelen;
943 }
944
945 dprintk("<-- nfs_probe_fsinfo() = 0\n");
946 return 0;
947
948out_error:
949 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
950 return error;
951}
952
953/*
954 * Copy useful information when duplicating a server record
955 */
956static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
957{
958 target->flags = source->flags;
959 target->acregmin = source->acregmin;
960 target->acregmax = source->acregmax;
961 target->acdirmin = source->acdirmin;
962 target->acdirmax = source->acdirmax;
963 target->caps = source->caps;
964}
965
966/*
967 * Allocate and initialise a server record
968 */
969static struct nfs_server *nfs_alloc_server(void)
970{
971 struct nfs_server *server;
972
973 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
974 if (!server)
975 return NULL;
976
977 server->client = server->client_acl = ERR_PTR(-EINVAL);
978
979 /* Zero out the NFS state stuff */
980 INIT_LIST_HEAD(&server->client_link);
981 INIT_LIST_HEAD(&server->master_link);
982
Steve Dicksonef818a22007-11-08 04:05:04 -0500983 atomic_set(&server->active, 0);
984
David Howells54ceac42006-08-22 20:06:13 -0400985 server->io_stats = nfs_alloc_iostats();
986 if (!server->io_stats) {
987 kfree(server);
988 return NULL;
989 }
990
991 return server;
992}
993
994/*
995 * Free up a server record
996 */
997void nfs_free_server(struct nfs_server *server)
998{
999 dprintk("--> nfs_free_server()\n");
1000
1001 spin_lock(&nfs_client_lock);
1002 list_del(&server->client_link);
1003 list_del(&server->master_link);
1004 spin_unlock(&nfs_client_lock);
1005
1006 if (server->destroy != NULL)
1007 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001008
1009 if (!IS_ERR(server->client_acl))
1010 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001011 if (!IS_ERR(server->client))
1012 rpc_shutdown_client(server->client);
1013
1014 nfs_put_client(server->nfs_client);
1015
1016 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001017 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001018 kfree(server);
1019 nfs_release_automount_timer();
1020 dprintk("<-- nfs_free_server()\n");
1021}
1022
1023/*
1024 * Create a version 2 or 3 volume record
1025 * - keyed on server and FSID
1026 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001027struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001028 struct nfs_fh *mntfh)
1029{
1030 struct nfs_server *server;
1031 struct nfs_fattr fattr;
1032 int error;
1033
1034 server = nfs_alloc_server();
1035 if (!server)
1036 return ERR_PTR(-ENOMEM);
1037
1038 /* Get a client representation */
1039 error = nfs_init_server(server, data);
1040 if (error < 0)
1041 goto error;
1042
1043 BUG_ON(!server->nfs_client);
1044 BUG_ON(!server->nfs_client->rpc_ops);
1045 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1046
1047 /* Probe the root fh to retrieve its FSID */
1048 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1049 if (error < 0)
1050 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001051 if (server->nfs_client->rpc_ops->version == 3) {
1052 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1053 server->namelen = NFS3_MAXNAMLEN;
1054 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1055 server->caps |= NFS_CAP_READDIRPLUS;
1056 } else {
1057 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1058 server->namelen = NFS2_MAXNAMLEN;
1059 }
1060
David Howells54ceac42006-08-22 20:06:13 -04001061 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1062 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1063 if (error < 0) {
1064 dprintk("nfs_create_server: getattr error = %d\n", -error);
1065 goto error;
1066 }
1067 }
1068 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1069
David Howells6daabf12006-08-24 15:44:16 -04001070 dprintk("Server FSID: %llx:%llx\n",
1071 (unsigned long long) server->fsid.major,
1072 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001073
1074 BUG_ON(!server->nfs_client);
1075 BUG_ON(!server->nfs_client->rpc_ops);
1076 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1077
1078 spin_lock(&nfs_client_lock);
1079 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1080 list_add_tail(&server->master_link, &nfs_volume_list);
1081 spin_unlock(&nfs_client_lock);
1082
1083 server->mount_time = jiffies;
1084 return server;
1085
1086error:
1087 nfs_free_server(server);
1088 return ERR_PTR(error);
1089}
1090
1091#ifdef CONFIG_NFS_V4
1092/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001093 * Initialize the NFS4 callback service
1094 */
1095static int nfs4_init_callback(struct nfs_client *clp)
1096{
1097 int error;
1098
1099 if (clp->rpc_ops->version == 4) {
Andy Adamson0b5b7ae2009-04-01 09:23:15 -04001100 if (nfs4_has_session(clp)) {
1101 error = xprt_setup_backchannel(
1102 clp->cl_rpcclient->cl_xprt,
1103 NFS41_BC_MIN_CALLBACKS);
1104 if (error < 0)
1105 return error;
1106 }
1107
Benny Halevy71468512009-04-01 09:22:56 -04001108 error = nfs_callback_up(clp->cl_minorversion,
1109 clp->cl_rpcclient->cl_xprt);
Benny Halevy9bdaa862009-04-01 09:22:55 -04001110 if (error < 0) {
1111 dprintk("%s: failed to start callback. Error = %d\n",
1112 __func__, error);
1113 return error;
1114 }
1115 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1116 }
1117 return 0;
1118}
1119
1120/*
Andy Adamson557134a2009-04-01 09:21:53 -04001121 * Initialize the minor version specific parts of an NFS4 client record
1122 */
1123static int nfs4_init_client_minor_version(struct nfs_client *clp)
1124{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001125 clp->cl_call_sync = _nfs4_call_sync;
1126
Andy Adamson557134a2009-04-01 09:21:53 -04001127#if defined(CONFIG_NFS_V4_1)
1128 if (clp->cl_minorversion) {
1129 struct nfs4_session *session = NULL;
1130 /*
1131 * Create the session and mark it expired.
1132 * When a SEQUENCE operation encounters the expired session
1133 * it will do session recovery to initialize it.
1134 */
1135 session = nfs4_alloc_session(clp);
1136 if (!session)
1137 return -ENOMEM;
1138
1139 clp->cl_session = session;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001140 clp->cl_call_sync = _nfs4_call_sync_session;
Andy Adamson557134a2009-04-01 09:21:53 -04001141 }
1142#endif /* CONFIG_NFS_V4_1 */
1143
Benny Halevy71468512009-04-01 09:22:56 -04001144 return nfs4_init_callback(clp);
Andy Adamson557134a2009-04-01 09:21:53 -04001145}
1146
1147/*
David Howells54ceac42006-08-22 20:06:13 -04001148 * Initialise an NFS4 client record
1149 */
1150static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001151 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001152 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001153 rpc_authflavor_t authflavour,
1154 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001155{
1156 int error;
1157
1158 if (clp->cl_cons_state == NFS_CS_READY) {
1159 /* the client is initialised already */
1160 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1161 return 0;
1162 }
1163
1164 /* Check NFS protocol revision and initialize RPC op vector */
1165 clp->rpc_ops = &nfs_v4_clientops;
1166
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001167 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001168 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001169 if (error < 0)
1170 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001171 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001172
1173 error = nfs_idmap_new(clp);
1174 if (error < 0) {
1175 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001176 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001177 goto error;
1178 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001179 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001180
Andy Adamson557134a2009-04-01 09:21:53 -04001181 error = nfs4_init_client_minor_version(clp);
1182 if (error < 0)
1183 goto error;
1184
Andy Adamson76db6d92009-04-01 09:22:38 -04001185 if (!nfs4_has_session(clp))
1186 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001187 return 0;
1188
1189error:
1190 nfs_mark_client_ready(clp, error);
1191 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1192 return error;
1193}
1194
1195/*
1196 * Set up an NFS4 client
1197 */
1198static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001199 const char *hostname,
1200 const struct sockaddr *addr,
1201 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001202 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001203 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001204 int proto, const struct rpc_timeout *timeparms,
1205 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001206{
Trond Myklebust3a498022007-12-14 14:56:04 -05001207 struct nfs_client_initdata cl_init = {
1208 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001209 .addr = addr,
1210 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001211 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001212 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001213 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001214 };
David Howells54ceac42006-08-22 20:06:13 -04001215 struct nfs_client *clp;
1216 int error;
1217
1218 dprintk("--> nfs4_set_client()\n");
1219
1220 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001221 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001222 if (IS_ERR(clp)) {
1223 error = PTR_ERR(clp);
1224 goto error;
1225 }
Chuck Leverd7403512008-12-23 15:21:37 -05001226 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1227 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001228 if (error < 0)
1229 goto error_put;
1230
1231 server->nfs_client = clp;
1232 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1233 return 0;
1234
1235error_put:
1236 nfs_put_client(clp);
1237error:
1238 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1239 return error;
1240}
1241
1242/*
Andy Adamson557134a2009-04-01 09:21:53 -04001243 * Initialize a session.
1244 * Note: save the mount rsize and wsize for create_server negotiation.
1245 */
1246static void nfs4_init_session(struct nfs_client *clp,
1247 unsigned int wsize, unsigned int rsize)
1248{
1249#if defined(CONFIG_NFS_V4_1)
1250 if (nfs4_has_session(clp)) {
1251 clp->cl_session->fc_attrs.max_rqst_sz = wsize;
1252 clp->cl_session->fc_attrs.max_resp_sz = rsize;
1253 }
1254#endif /* CONFIG_NFS_V4_1 */
1255}
1256
1257/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001258 * Session has been established, and the client marked ready.
1259 * Set the mount rsize and wsize with negotiated fore channel
1260 * attributes which will be bound checked in nfs_server_set_fsinfo.
1261 */
1262static void nfs4_session_set_rwsize(struct nfs_server *server)
1263{
1264#ifdef CONFIG_NFS_V4_1
1265 if (!nfs4_has_session(server->nfs_client))
1266 return;
1267 server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1268 server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
1269#endif /* CONFIG_NFS_V4_1 */
1270}
1271
1272/*
David Howells54ceac42006-08-22 20:06:13 -04001273 * Create a version 4 volume record
1274 */
1275static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001276 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001277{
Trond Myklebust33170232007-12-20 16:03:59 -05001278 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001279 int error;
1280
1281 dprintk("--> nfs4_init_server()\n");
1282
Trond Myklebust33170232007-12-20 16:03:59 -05001283 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1284 data->timeo, data->retrans);
1285
Chuck Lever542fcc32008-12-23 15:21:36 -05001286 /* Initialise the client representation from the mount data */
1287 server->flags = data->flags;
1288 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001289 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001290
Trond Myklebust33170232007-12-20 16:03:59 -05001291 /* Get a client record */
1292 error = nfs4_set_client(server,
1293 data->nfs_server.hostname,
1294 (const struct sockaddr *)&data->nfs_server.address,
1295 data->nfs_server.addrlen,
1296 data->client_address,
1297 data->auth_flavors[0],
1298 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001299 &timeparms,
1300 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001301 if (error < 0)
1302 goto error;
1303
David Howells54ceac42006-08-22 20:06:13 -04001304 if (data->rsize)
1305 server->rsize = nfs_block_size(data->rsize, NULL);
1306 if (data->wsize)
1307 server->wsize = nfs_block_size(data->wsize, NULL);
1308
1309 server->acregmin = data->acregmin * HZ;
1310 server->acregmax = data->acregmax * HZ;
1311 server->acdirmin = data->acdirmin * HZ;
1312 server->acdirmax = data->acdirmax * HZ;
1313
Chuck Leverf22d6d72008-03-14 14:10:22 -04001314 server->port = data->nfs_server.port;
1315
Trond Myklebust33170232007-12-20 16:03:59 -05001316 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001317
Trond Myklebust33170232007-12-20 16:03:59 -05001318error:
David Howells54ceac42006-08-22 20:06:13 -04001319 /* Done */
1320 dprintk("<-- nfs4_init_server() = %d\n", error);
1321 return error;
1322}
1323
1324/*
1325 * Create a version 4 volume record
1326 * - keyed on server and FSID
1327 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001328struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001329 struct nfs_fh *mntfh)
1330{
1331 struct nfs_fattr fattr;
1332 struct nfs_server *server;
1333 int error;
1334
1335 dprintk("--> nfs4_create_server()\n");
1336
1337 server = nfs_alloc_server();
1338 if (!server)
1339 return ERR_PTR(-ENOMEM);
1340
David Howells54ceac42006-08-22 20:06:13 -04001341 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001342 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001343 if (error < 0)
1344 goto error;
1345
1346 BUG_ON(!server->nfs_client);
1347 BUG_ON(!server->nfs_client->rpc_ops);
1348 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1349
Andy Adamson557134a2009-04-01 09:21:53 -04001350 nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
1351
David Howells54ceac42006-08-22 20:06:13 -04001352 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001353 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001354 if (error < 0)
1355 goto error;
1356
David Howells6daabf12006-08-24 15:44:16 -04001357 dprintk("Server FSID: %llx:%llx\n",
1358 (unsigned long long) server->fsid.major,
1359 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001360 dprintk("Mount FH: %d\n", mntfh->size);
1361
Andy Adamson96b09e02009-04-01 09:22:33 -04001362 nfs4_session_set_rwsize(server);
1363
David Howells54ceac42006-08-22 20:06:13 -04001364 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1365 if (error < 0)
1366 goto error;
1367
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001368 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1369 server->namelen = NFS4_MAXNAMLEN;
1370
David Howells54ceac42006-08-22 20:06:13 -04001371 BUG_ON(!server->nfs_client);
1372 BUG_ON(!server->nfs_client->rpc_ops);
1373 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1374
1375 spin_lock(&nfs_client_lock);
1376 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1377 list_add_tail(&server->master_link, &nfs_volume_list);
1378 spin_unlock(&nfs_client_lock);
1379
1380 server->mount_time = jiffies;
1381 dprintk("<-- nfs4_create_server() = %p\n", server);
1382 return server;
1383
1384error:
1385 nfs_free_server(server);
1386 dprintk("<-- nfs4_create_server() = error %d\n", error);
1387 return ERR_PTR(error);
1388}
1389
1390/*
1391 * Create an NFS4 referral server record
1392 */
1393struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001394 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001395{
1396 struct nfs_client *parent_client;
1397 struct nfs_server *server, *parent_server;
1398 struct nfs_fattr fattr;
1399 int error;
1400
1401 dprintk("--> nfs4_create_referral_server()\n");
1402
1403 server = nfs_alloc_server();
1404 if (!server)
1405 return ERR_PTR(-ENOMEM);
1406
1407 parent_server = NFS_SB(data->sb);
1408 parent_client = parent_server->nfs_client;
1409
Chuck Lever542fcc32008-12-23 15:21:36 -05001410 /* Initialise the client representation from the parent server */
1411 nfs_server_copy_userdata(server, parent_server);
1412 server->caps |= NFS_CAP_ATOMIC_OPEN;
1413
David Howells54ceac42006-08-22 20:06:13 -04001414 /* Get a client representation.
1415 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001416 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001417 data->addr,
1418 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001419 parent_client->cl_ipaddr,
1420 data->authflavor,
1421 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001422 parent_server->client->cl_timeout,
1423 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001424 if (error < 0)
1425 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001426
Trond Myklebust33170232007-12-20 16:03:59 -05001427 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001428 if (error < 0)
1429 goto error;
1430
1431 BUG_ON(!server->nfs_client);
1432 BUG_ON(!server->nfs_client->rpc_ops);
1433 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1434
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001435 /* Probe the root fh to retrieve its FSID and filehandle */
1436 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1437 if (error < 0)
1438 goto error;
1439
David Howells54ceac42006-08-22 20:06:13 -04001440 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001441 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001442 if (error < 0)
1443 goto error;
1444
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001445 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1446 server->namelen = NFS4_MAXNAMLEN;
1447
David Howells54ceac42006-08-22 20:06:13 -04001448 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001449 (unsigned long long) server->fsid.major,
1450 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001451
1452 spin_lock(&nfs_client_lock);
1453 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1454 list_add_tail(&server->master_link, &nfs_volume_list);
1455 spin_unlock(&nfs_client_lock);
1456
1457 server->mount_time = jiffies;
1458
1459 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1460 return server;
1461
1462error:
1463 nfs_free_server(server);
1464 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1465 return ERR_PTR(error);
1466}
1467
1468#endif /* CONFIG_NFS_V4 */
1469
1470/*
1471 * Clone an NFS2, NFS3 or NFS4 server record
1472 */
1473struct nfs_server *nfs_clone_server(struct nfs_server *source,
1474 struct nfs_fh *fh,
1475 struct nfs_fattr *fattr)
1476{
1477 struct nfs_server *server;
1478 struct nfs_fattr fattr_fsinfo;
1479 int error;
1480
1481 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001482 (unsigned long long) fattr->fsid.major,
1483 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001484
1485 server = nfs_alloc_server();
1486 if (!server)
1487 return ERR_PTR(-ENOMEM);
1488
1489 /* Copy data from the source */
1490 server->nfs_client = source->nfs_client;
1491 atomic_inc(&server->nfs_client->cl_count);
1492 nfs_server_copy_userdata(server, source);
1493
1494 server->fsid = fattr->fsid;
1495
Trond Myklebust33170232007-12-20 16:03:59 -05001496 error = nfs_init_server_rpcclient(server,
1497 source->client->cl_timeout,
1498 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001499 if (error < 0)
1500 goto out_free_server;
1501 if (!IS_ERR(source->client_acl))
1502 nfs_init_server_aclclient(server);
1503
1504 /* probe the filesystem info for this server filesystem */
1505 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1506 if (error < 0)
1507 goto out_free_server;
1508
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001509 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1510 server->namelen = NFS4_MAXNAMLEN;
1511
David Howells54ceac42006-08-22 20:06:13 -04001512 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001513 (unsigned long long) server->fsid.major,
1514 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001515
1516 error = nfs_start_lockd(server);
1517 if (error < 0)
1518 goto out_free_server;
1519
1520 spin_lock(&nfs_client_lock);
1521 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1522 list_add_tail(&server->master_link, &nfs_volume_list);
1523 spin_unlock(&nfs_client_lock);
1524
1525 server->mount_time = jiffies;
1526
1527 dprintk("<-- nfs_clone_server() = %p\n", server);
1528 return server;
1529
1530out_free_server:
1531 nfs_free_server(server);
1532 dprintk("<-- nfs_clone_server() = error %d\n", error);
1533 return ERR_PTR(error);
1534}
David Howells6aaca562006-08-22 20:06:13 -04001535
1536#ifdef CONFIG_PROC_FS
1537static struct proc_dir_entry *proc_fs_nfs;
1538
1539static int nfs_server_list_open(struct inode *inode, struct file *file);
1540static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1541static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1542static void nfs_server_list_stop(struct seq_file *p, void *v);
1543static int nfs_server_list_show(struct seq_file *m, void *v);
1544
1545static struct seq_operations nfs_server_list_ops = {
1546 .start = nfs_server_list_start,
1547 .next = nfs_server_list_next,
1548 .stop = nfs_server_list_stop,
1549 .show = nfs_server_list_show,
1550};
1551
Arjan van de Ven00977a52007-02-12 00:55:34 -08001552static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001553 .open = nfs_server_list_open,
1554 .read = seq_read,
1555 .llseek = seq_lseek,
1556 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001557 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001558};
1559
1560static int nfs_volume_list_open(struct inode *inode, struct file *file);
1561static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1562static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1563static void nfs_volume_list_stop(struct seq_file *p, void *v);
1564static int nfs_volume_list_show(struct seq_file *m, void *v);
1565
1566static struct seq_operations nfs_volume_list_ops = {
1567 .start = nfs_volume_list_start,
1568 .next = nfs_volume_list_next,
1569 .stop = nfs_volume_list_stop,
1570 .show = nfs_volume_list_show,
1571};
1572
Arjan van de Ven00977a52007-02-12 00:55:34 -08001573static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001574 .open = nfs_volume_list_open,
1575 .read = seq_read,
1576 .llseek = seq_lseek,
1577 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001578 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001579};
1580
1581/*
1582 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1583 * we're dealing
1584 */
1585static int nfs_server_list_open(struct inode *inode, struct file *file)
1586{
1587 struct seq_file *m;
1588 int ret;
1589
1590 ret = seq_open(file, &nfs_server_list_ops);
1591 if (ret < 0)
1592 return ret;
1593
1594 m = file->private_data;
1595 m->private = PDE(inode)->data;
1596
1597 return 0;
1598}
1599
1600/*
1601 * set up the iterator to start reading from the server list and return the first item
1602 */
1603static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1604{
David Howells6aaca562006-08-22 20:06:13 -04001605 /* lock the list against modification */
1606 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001607 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001608}
1609
1610/*
1611 * move to next server
1612 */
1613static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1614{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001615 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001616}
1617
1618/*
1619 * clean up after reading from the transports list
1620 */
1621static void nfs_server_list_stop(struct seq_file *p, void *v)
1622{
1623 spin_unlock(&nfs_client_lock);
1624}
1625
1626/*
1627 * display a header line followed by a load of call lines
1628 */
1629static int nfs_server_list_show(struct seq_file *m, void *v)
1630{
1631 struct nfs_client *clp;
1632
1633 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001634 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001635 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1636 return 0;
1637 }
1638
1639 /* display one transport per line on subsequent lines */
1640 clp = list_entry(v, struct nfs_client, cl_share_link);
1641
Chuck Lever5d8515c2007-12-10 14:57:16 -05001642 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001643 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001644 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1645 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001646 atomic_read(&clp->cl_count),
1647 clp->cl_hostname);
1648
1649 return 0;
1650}
1651
1652/*
1653 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1654 */
1655static int nfs_volume_list_open(struct inode *inode, struct file *file)
1656{
1657 struct seq_file *m;
1658 int ret;
1659
1660 ret = seq_open(file, &nfs_volume_list_ops);
1661 if (ret < 0)
1662 return ret;
1663
1664 m = file->private_data;
1665 m->private = PDE(inode)->data;
1666
1667 return 0;
1668}
1669
1670/*
1671 * set up the iterator to start reading from the volume list and return the first item
1672 */
1673static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1674{
David Howells6aaca562006-08-22 20:06:13 -04001675 /* lock the list against modification */
1676 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001677 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001678}
1679
1680/*
1681 * move to next volume
1682 */
1683static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1684{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001685 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001686}
1687
1688/*
1689 * clean up after reading from the transports list
1690 */
1691static void nfs_volume_list_stop(struct seq_file *p, void *v)
1692{
1693 spin_unlock(&nfs_client_lock);
1694}
1695
1696/*
1697 * display a header line followed by a load of call lines
1698 */
1699static int nfs_volume_list_show(struct seq_file *m, void *v)
1700{
1701 struct nfs_server *server;
1702 struct nfs_client *clp;
1703 char dev[8], fsid[17];
1704
1705 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001706 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001707 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001708 return 0;
1709 }
1710 /* display one transport per line on subsequent lines */
1711 server = list_entry(v, struct nfs_server, master_link);
1712 clp = server->nfs_client;
1713
1714 snprintf(dev, 8, "%u:%u",
1715 MAJOR(server->s_dev), MINOR(server->s_dev));
1716
1717 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001718 (unsigned long long) server->fsid.major,
1719 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001720
David Howells5d1acff2009-04-03 16:42:47 +01001721 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001722 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001723 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1724 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001725 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001726 fsid,
1727 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001728
1729 return 0;
1730}
1731
1732/*
1733 * initialise the /proc/fs/nfsfs/ directory
1734 */
1735int __init nfs_fs_proc_init(void)
1736{
1737 struct proc_dir_entry *p;
1738
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001739 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001740 if (!proc_fs_nfs)
1741 goto error_0;
1742
David Howells6aaca562006-08-22 20:06:13 -04001743 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001744 p = proc_create("servers", S_IFREG|S_IRUGO,
1745 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001746 if (!p)
1747 goto error_1;
1748
David Howells6aaca562006-08-22 20:06:13 -04001749 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001750 p = proc_create("volumes", S_IFREG|S_IRUGO,
1751 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001752 if (!p)
1753 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001754 return 0;
1755
1756error_2:
1757 remove_proc_entry("servers", proc_fs_nfs);
1758error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001759 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001760error_0:
1761 return -ENOMEM;
1762}
1763
1764/*
1765 * clean up the /proc/fs/nfsfs/ directory
1766 */
1767void nfs_fs_proc_exit(void)
1768{
1769 remove_proc_entry("volumes", proc_fs_nfs);
1770 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001771 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001772}
1773
1774#endif /* CONFIG_PROC_FS */