blob: d9657d455730f9ec02bee0a31b40855bfb7a70cd [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
Benny Halevy9bdaa862009-04-01 09:22:55 -040050static int nfs4_init_callback(struct nfs_client *);
51static void nfs4_destroy_callback(struct nfs_client *);
52
David Howells24c8dbb2006-08-22 20:06:10 -040053#define NFSDBG_FACILITY NFSDBG_CLIENT
54
55static DEFINE_SPINLOCK(nfs_client_lock);
56static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040057static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040058static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
59
60/*
David Howells5006a762006-08-22 20:06:12 -040061 * RPC cruft for NFS
62 */
63static struct rpc_version *nfs_version[5] = {
64 [2] = &nfs_version2,
65#ifdef CONFIG_NFS_V3
66 [3] = &nfs_version3,
67#endif
68#ifdef CONFIG_NFS_V4
69 [4] = &nfs_version4,
70#endif
71};
72
73struct rpc_program nfs_program = {
74 .name = "nfs",
75 .number = NFS_PROGRAM,
76 .nrvers = ARRAY_SIZE(nfs_version),
77 .version = nfs_version,
78 .stats = &nfs_rpcstat,
79 .pipe_dir_name = "/nfs",
80};
81
82struct rpc_stat nfs_rpcstat = {
83 .program = &nfs_program
84};
85
86
87#ifdef CONFIG_NFS_V3_ACL
88static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
89static struct rpc_version * nfsacl_version[] = {
90 [3] = &nfsacl_version3,
91};
92
93struct rpc_program nfsacl_program = {
94 .name = "nfsacl",
95 .number = NFS_ACL_PROGRAM,
96 .nrvers = ARRAY_SIZE(nfsacl_version),
97 .version = nfsacl_version,
98 .stats = &nfsacl_rpcstat,
99};
100#endif /* CONFIG_NFS_V3_ACL */
101
Trond Myklebust3a498022007-12-14 14:56:04 -0500102struct nfs_client_initdata {
103 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500104 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500105 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500106 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500107 int proto;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400108 u32 minorversion;
Trond Myklebust3a498022007-12-14 14:56:04 -0500109};
110
David Howells5006a762006-08-22 20:06:12 -0400111/*
David Howells24c8dbb2006-08-22 20:06:10 -0400112 * Allocate a shared client record
113 *
114 * Since these are allocated/deallocated very rarely, we don't
115 * bother putting them in a slab cache...
116 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500117static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400118{
119 struct nfs_client *clp;
Trond Myklebust7c67db32008-04-07 20:50:11 -0400120 struct rpc_cred *cred;
David Howells24c8dbb2006-08-22 20:06:10 -0400121
122 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
123 goto error_0;
124
Trond Myklebust40c553192007-12-14 14:56:07 -0500125 clp->rpc_ops = cl_init->rpc_ops;
126
Benny Halevy9bdaa862009-04-01 09:22:55 -0400127 if (nfs4_init_callback(clp) < 0)
128 goto error_2;
David Howells24c8dbb2006-08-22 20:06:10 -0400129
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:
Benny Halevy9bdaa862009-04-01 09:22:55 -0400165 nfs4_destroy_callback(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400166error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400167 kfree(clp);
168error_0:
169 return NULL;
170}
171
Trond Myklebust5dd31772006-08-24 01:03:05 -0400172static void nfs4_shutdown_client(struct nfs_client *clp)
173{
174#ifdef CONFIG_NFS_V4
175 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
176 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400177 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400178 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
179 nfs_idmap_delete(clp);
Trond Myklebustf6a1cc82008-02-22 17:06:55 -0500180
181 rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400182#endif
183}
184
David Howells24c8dbb2006-08-22 20:06:10 -0400185/*
Benny Halevy9bdaa862009-04-01 09:22:55 -0400186 * Destroy the NFS4 callback service
187 */
188static void nfs4_destroy_callback(struct nfs_client *clp)
189{
190#ifdef CONFIG_NFS_V4
191 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
192 nfs_callback_down();
193#endif /* CONFIG_NFS_V4 */
194}
195
196/*
Andy Adamson557134a2009-04-01 09:21:53 -0400197 * Clears/puts all minor version specific parts from an nfs_client struct
198 * reverting it to minorversion 0.
199 */
200static void nfs4_clear_client_minor_version(struct nfs_client *clp)
201{
202#ifdef CONFIG_NFS_V4_1
203 if (nfs4_has_session(clp)) {
204 nfs4_destroy_session(clp->cl_session);
205 clp->cl_session = NULL;
206 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400207
208 clp->cl_call_sync = _nfs4_call_sync;
Andy Adamson557134a2009-04-01 09:21:53 -0400209#endif /* CONFIG_NFS_V4_1 */
210}
211
212/*
David Howells24c8dbb2006-08-22 20:06:10 -0400213 * Destroy a shared client record
214 */
215static void nfs_free_client(struct nfs_client *clp)
216{
Trond Myklebust40c553192007-12-14 14:56:07 -0500217 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400218
Andy Adamson557134a2009-04-01 09:21:53 -0400219 nfs4_clear_client_minor_version(clp);
Trond Myklebust5dd31772006-08-24 01:03:05 -0400220 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400221
David Howells14727282009-04-03 16:42:42 +0100222 nfs_fscache_release_client_cookie(clp);
223
David Howells24c8dbb2006-08-22 20:06:10 -0400224 /* -EIO all pending I/O */
225 if (!IS_ERR(clp->cl_rpcclient))
226 rpc_shutdown_client(clp->cl_rpcclient);
227
Benny Halevy9bdaa862009-04-01 09:22:55 -0400228 nfs4_destroy_callback(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400229
Trond Myklebust7c67db32008-04-07 20:50:11 -0400230 if (clp->cl_machine_cred != NULL)
231 put_rpccred(clp->cl_machine_cred);
232
David Howells24c8dbb2006-08-22 20:06:10 -0400233 kfree(clp->cl_hostname);
234 kfree(clp);
235
236 dprintk("<-- nfs_free_client()\n");
237}
238
239/*
240 * Release a reference to a shared client record
241 */
242void nfs_put_client(struct nfs_client *clp)
243{
David Howells27ba8512006-07-30 14:40:56 -0400244 if (!clp)
245 return;
246
David Howells24c8dbb2006-08-22 20:06:10 -0400247 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
248
249 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
250 list_del(&clp->cl_share_link);
251 spin_unlock(&nfs_client_lock);
252
253 BUG_ON(!list_empty(&clp->cl_superblocks));
254
255 nfs_free_client(clp);
256 }
257}
258
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500259#if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400260/*
261 * Test if two ip6 socket addresses refer to the same socket by
262 * comparing relevant fields. The padding bytes specifically, are not
263 * compared. sin6_flowinfo is not compared because it only affects QoS
264 * and sin6_scope_id is only compared if the address is "link local"
265 * because "link local" addresses need only be unique to a specific
266 * link. Conversely, ordinary unicast addresses might have different
267 * sin6_scope_id.
268 *
269 * The caller should ensure both socket addresses are AF_INET6.
270 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400271static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
272 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400273{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400274 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
275 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400276
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400277 if (ipv6_addr_scope(&sin1->sin6_addr) == IPV6_ADDR_SCOPE_LINKLOCAL &&
278 sin1->sin6_scope_id != sin2->sin6_scope_id)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400279 return 0;
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500280
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400281 return ipv6_addr_equal(&sin1->sin6_addr, &sin1->sin6_addr);
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500282}
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400283#else /* !defined(CONFIG_IPV6) && !defined(CONFIG_IPV6_MODULE) */
284static int nfs_sockaddr_match_ipaddr6(const struct sockaddr *sa1,
285 const struct sockaddr *sa2)
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400286{
287 return 0;
288}
Trond Myklebust9082a5c2008-12-23 15:21:53 -0500289#endif
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500290
David Howells24c8dbb2006-08-22 20:06:10 -0400291/*
Ian Dalld7371c42009-03-10 20:33:22 -0400292 * Test if two ip4 socket addresses refer to the same socket, by
293 * comparing relevant fields. The padding bytes specifically, are
294 * not compared.
295 *
296 * The caller should ensure both socket addresses are AF_INET.
297 */
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400298static int nfs_sockaddr_match_ipaddr4(const struct sockaddr *sa1,
299 const struct sockaddr *sa2)
300{
301 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
302 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
303
304 return sin1->sin_addr.s_addr == sin2->sin_addr.s_addr;
305}
306
307static int nfs_sockaddr_cmp_ip6(const struct sockaddr *sa1,
308 const struct sockaddr *sa2)
309{
310 const struct sockaddr_in6 *sin1 = (const struct sockaddr_in6 *)sa1;
311 const struct sockaddr_in6 *sin2 = (const struct sockaddr_in6 *)sa2;
312
313 return nfs_sockaddr_match_ipaddr6(sa1, sa2) &&
314 (sin1->sin6_port == sin2->sin6_port);
315}
316
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400317static int nfs_sockaddr_cmp_ip4(const struct sockaddr *sa1,
318 const struct sockaddr *sa2)
Ian Dalld7371c42009-03-10 20:33:22 -0400319{
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400320 const struct sockaddr_in *sin1 = (const struct sockaddr_in *)sa1;
321 const struct sockaddr_in *sin2 = (const struct sockaddr_in *)sa2;
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400322
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400323 return nfs_sockaddr_match_ipaddr4(sa1, sa2) &&
324 (sin1->sin_port == sin2->sin_port);
Ian Dalld7371c42009-03-10 20:33:22 -0400325}
326
327/*
Ian Dalld7371c42009-03-10 20:33:22 -0400328 * Test if two socket addresses represent the same actual socket,
Chuck Lever3c8c45d2009-03-18 20:48:14 -0400329 * by comparing (only) relevant fields, excluding the port number.
330 */
331static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
332 const struct sockaddr *sa2)
333{
334 if (sa1->sa_family != sa2->sa_family)
335 return 0;
336
337 switch (sa1->sa_family) {
338 case AF_INET:
339 return nfs_sockaddr_match_ipaddr4(sa1, sa2);
340 case AF_INET6:
341 return nfs_sockaddr_match_ipaddr6(sa1, sa2);
342 }
343 return 0;
344}
345
346/*
347 * Test if two socket addresses represent the same actual socket,
348 * by comparing (only) relevant fields, including the port number.
Ian Dalld7371c42009-03-10 20:33:22 -0400349 */
350static int nfs_sockaddr_cmp(const struct sockaddr *sa1,
351 const struct sockaddr *sa2)
352{
353 if (sa1->sa_family != sa2->sa_family)
354 return 0;
355
356 switch (sa1->sa_family) {
357 case AF_INET:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400358 return nfs_sockaddr_cmp_ip4(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400359 case AF_INET6:
Trond Myklebust9f4c8992009-03-12 14:51:32 -0400360 return nfs_sockaddr_cmp_ip6(sa1, sa2);
Ian Dalld7371c42009-03-10 20:33:22 -0400361 }
362 return 0;
363}
364
365/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500366 * Find a client by IP address and protocol version
367 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400368 */
Chuck Leverff052642007-12-10 14:58:44 -0500369struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500370{
371 struct nfs_client *clp;
372
373 spin_lock(&nfs_client_lock);
374 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500375 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
376
Trond Myklebustc81468a2007-12-14 14:56:05 -0500377 /* Don't match clients that failed to initialise properly */
Andy Adamson76db6d92009-04-01 09:22:38 -0400378 if (!(clp->cl_cons_state == NFS_CS_READY ||
379 clp->cl_cons_state == NFS_CS_SESSION_INITING))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500380 continue;
381
382 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500383 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500384 continue;
385
386 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500387 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500388 continue;
389
390 atomic_inc(&clp->cl_count);
391 spin_unlock(&nfs_client_lock);
392 return clp;
393 }
394 spin_unlock(&nfs_client_lock);
395 return NULL;
396}
397
398/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500399 * Find a client by IP address and protocol version
400 * - returns NULL if no such client
401 */
402struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
403{
404 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
405 u32 nfsvers = clp->rpc_ops->version;
406
407 spin_lock(&nfs_client_lock);
408 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
409 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
410
411 /* Don't match clients that failed to initialise properly */
412 if (clp->cl_cons_state != NFS_CS_READY)
413 continue;
414
415 /* Different NFS versions cannot share the same nfs_client */
416 if (clp->rpc_ops->version != nfsvers)
417 continue;
418
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500419 /* Match only the IP address, not the port number */
420 if (!nfs_sockaddr_match_ipaddr(sap, clap))
421 continue;
422
423 atomic_inc(&clp->cl_count);
424 spin_unlock(&nfs_client_lock);
425 return clp;
426 }
427 spin_unlock(&nfs_client_lock);
428 return NULL;
429}
430
431/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500432 * Find an nfs_client on the list that matches the initialisation data
433 * that is supplied.
434 */
435static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400436{
437 struct nfs_client *clp;
Ian Dalld7371c42009-03-10 20:33:22 -0400438 const struct sockaddr *sap = data->addr;
David Howells24c8dbb2006-08-22 20:06:10 -0400439
440 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Ian Dalld7371c42009-03-10 20:33:22 -0400441 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
Trond Myklebust13bbc062006-10-19 23:28:40 -0700442 /* Don't match clients that failed to initialise properly */
443 if (clp->cl_cons_state < 0)
444 continue;
445
David Howells24c8dbb2006-08-22 20:06:10 -0400446 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500447 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400448 continue;
449
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500450 if (clp->cl_proto != data->proto)
451 continue;
Benny Halevy5aae4a92009-04-01 09:21:50 -0400452 /* Match nfsv4 minorversion */
453 if (clp->cl_minorversion != data->minorversion)
454 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500455 /* Match the full socket address */
Ian Dalld7371c42009-03-10 20:33:22 -0400456 if (!nfs_sockaddr_cmp(sap, clap))
David Howells24c8dbb2006-08-22 20:06:10 -0400457 continue;
458
Trond Myklebustc81468a2007-12-14 14:56:05 -0500459 atomic_inc(&clp->cl_count);
460 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400461 }
David Howells24c8dbb2006-08-22 20:06:10 -0400462 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400463}
464
465/*
466 * Look up a client by IP address and protocol version
467 * - creates a new record if one doesn't yet exist
468 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500469static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400470{
471 struct nfs_client *clp, *new = NULL;
472 int error;
473
Chuck Leverd7422c42007-12-10 14:58:51 -0500474 dprintk("--> nfs_get_client(%s,v%u)\n",
475 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400476
477 /* see if the client already exists */
478 do {
479 spin_lock(&nfs_client_lock);
480
Trond Myklebustc81468a2007-12-14 14:56:05 -0500481 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400482 if (clp)
483 goto found_client;
484 if (new)
485 goto install_client;
486
487 spin_unlock(&nfs_client_lock);
488
Trond Myklebust3a498022007-12-14 14:56:04 -0500489 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400490 } while (new);
491
492 return ERR_PTR(-ENOMEM);
493
494 /* install a new client and return with it unready */
495install_client:
496 clp = new;
497 list_add(&clp->cl_share_link, &nfs_client_list);
498 spin_unlock(&nfs_client_lock);
499 dprintk("--> nfs_get_client() = %p [new]\n", clp);
500 return clp;
501
502 /* found an existing client
503 * - make sure it's ready before returning
504 */
505found_client:
506 spin_unlock(&nfs_client_lock);
507
508 if (new)
509 nfs_free_client(new);
510
Matthew Wilcox150030b2007-12-06 16:24:39 -0500511 error = wait_event_killable(nfs_client_active_wq,
Andy Adamson76db6d92009-04-01 09:22:38 -0400512 clp->cl_cons_state < NFS_CS_INITING);
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400513 if (error < 0) {
514 nfs_put_client(clp);
515 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400516 }
517
518 if (clp->cl_cons_state < NFS_CS_READY) {
519 error = clp->cl_cons_state;
520 nfs_put_client(clp);
521 return ERR_PTR(error);
522 }
523
David Howells54ceac42006-08-22 20:06:13 -0400524 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
525
David Howells24c8dbb2006-08-22 20:06:10 -0400526 dprintk("--> nfs_get_client() = %p [share]\n", clp);
527 return clp;
528}
529
530/*
531 * Mark a server as ready or failed
532 */
Andy Adamson76db6d92009-04-01 09:22:38 -0400533void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400534{
535 clp->cl_cons_state = state;
536 wake_up_all(&nfs_client_active_wq);
537}
David Howells5006a762006-08-22 20:06:12 -0400538
539/*
Benny Halevy008f55d2009-04-01 09:22:50 -0400540 * With sessions, the client is not marked ready until after a
541 * successful EXCHANGE_ID and CREATE_SESSION.
542 *
543 * Map errors cl_cons_state errors to EPROTONOSUPPORT to indicate
544 * other versions of NFS can be tried.
545 */
546int nfs4_check_client_ready(struct nfs_client *clp)
547{
548 if (!nfs4_has_session(clp))
549 return 0;
550 if (clp->cl_cons_state < NFS_CS_READY)
551 return -EPROTONOSUPPORT;
552 return 0;
553}
554
555/*
David Howells5006a762006-08-22 20:06:12 -0400556 * Initialise the timeout values for a connection
557 */
558static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
559 unsigned int timeo, unsigned int retrans)
560{
561 to->to_initval = timeo * HZ / 10;
562 to->to_retries = retrans;
David Howells5006a762006-08-22 20:06:12 -0400563
564 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400565 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400566 case XPRT_TRANSPORT_RDMA:
Trond Myklebust259875e2008-07-02 14:43:47 -0400567 if (to->to_retries == 0)
568 to->to_retries = NFS_DEF_TCP_RETRANS;
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500569 if (to->to_initval == 0)
Trond Myklebust259875e2008-07-02 14:43:47 -0400570 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400571 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
572 to->to_initval = NFS_MAX_TCP_TIMEOUT;
573 to->to_increment = to->to_initval;
574 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500575 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
576 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
577 if (to->to_maxval < to->to_initval)
578 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400579 to->to_exponential = 0;
580 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400581 case XPRT_TRANSPORT_UDP:
Trond Myklebust259875e2008-07-02 14:43:47 -0400582 if (to->to_retries == 0)
583 to->to_retries = NFS_DEF_UDP_RETRANS;
David Howells5006a762006-08-22 20:06:12 -0400584 if (!to->to_initval)
Trond Myklebust259875e2008-07-02 14:43:47 -0400585 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
David Howells5006a762006-08-22 20:06:12 -0400586 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
587 to->to_initval = NFS_MAX_UDP_TIMEOUT;
588 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
589 to->to_exponential = 1;
590 break;
Trond Myklebust259875e2008-07-02 14:43:47 -0400591 default:
592 BUG();
David Howells5006a762006-08-22 20:06:12 -0400593 }
594}
595
596/*
597 * Create an RPC client handle
598 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500599static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500600 const struct rpc_timeout *timeparms,
601 rpc_authflavor_t flavor,
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500602 int discrtry, int noresvport)
David Howells5006a762006-08-22 20:06:12 -0400603{
David Howells5006a762006-08-22 20:06:12 -0400604 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400605 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500606 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400607 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500608 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500609 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400610 .servername = clp->cl_hostname,
611 .program = &nfs_program,
612 .version = clp->rpc_ops->version,
613 .authflavor = flavor,
614 };
David Howells5006a762006-08-22 20:06:12 -0400615
Chuck Lever4a01b8a2008-12-23 15:21:35 -0500616 if (discrtry)
617 args.flags |= RPC_CLNT_CREATE_DISCRTRY;
618 if (noresvport)
619 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
620
David Howells5006a762006-08-22 20:06:12 -0400621 if (!IS_ERR(clp->cl_rpcclient))
622 return 0;
623
Chuck Lever41877d22006-08-22 20:06:20 -0400624 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400625 if (IS_ERR(clnt)) {
626 dprintk("%s: cannot create RPC client. Error = %ld\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700627 __func__, PTR_ERR(clnt));
David Howells5006a762006-08-22 20:06:12 -0400628 return PTR_ERR(clnt);
629 }
630
David Howells5006a762006-08-22 20:06:12 -0400631 clp->cl_rpcclient = clnt;
632 return 0;
633}
David Howells54ceac42006-08-22 20:06:13 -0400634
635/*
636 * Version 2 or 3 client destruction
637 */
638static void nfs_destroy_server(struct nfs_server *server)
639{
David Howells54ceac42006-08-22 20:06:13 -0400640 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500641 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400642}
643
644/*
645 * Version 2 or 3 lockd setup
646 */
647static int nfs_start_lockd(struct nfs_server *server)
648{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500649 struct nlm_host *host;
650 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500651 struct nlmclnt_initdata nlm_init = {
652 .hostname = clp->cl_hostname,
653 .address = (struct sockaddr *)&clp->cl_addr,
654 .addrlen = clp->cl_addrlen,
655 .protocol = server->flags & NFS_MOUNT_TCP ?
656 IPPROTO_TCP : IPPROTO_UDP,
657 .nfs_version = clp->rpc_ops->version,
Chuck Lever0cb26592008-12-23 15:21:38 -0500658 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ?
659 1 : 0,
Chuck Lever883bb162008-01-15 16:04:20 -0500660 };
David Howells54ceac42006-08-22 20:06:13 -0400661
Chuck Lever883bb162008-01-15 16:04:20 -0500662 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500663 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400664 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500665 return 0;
666
Chuck Lever883bb162008-01-15 16:04:20 -0500667 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500668 if (IS_ERR(host))
669 return PTR_ERR(host);
670
671 server->nlm_host = host;
672 server->destroy = nfs_destroy_server;
673 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400674}
675
676/*
677 * Initialise an NFSv3 ACL client connection
678 */
679#ifdef CONFIG_NFS_V3_ACL
680static void nfs_init_server_aclclient(struct nfs_server *server)
681{
Trond Myklebust40c553192007-12-14 14:56:07 -0500682 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400683 goto out_noacl;
684 if (server->flags & NFS_MOUNT_NOACL)
685 goto out_noacl;
686
687 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
688 if (IS_ERR(server->client_acl))
689 goto out_noacl;
690
691 /* No errors! Assume that Sun nfsacls are supported */
692 server->caps |= NFS_CAP_ACLS;
693 return;
694
695out_noacl:
696 server->caps &= ~NFS_CAP_ACLS;
697}
698#else
699static inline void nfs_init_server_aclclient(struct nfs_server *server)
700{
701 server->flags &= ~NFS_MOUNT_NOACL;
702 server->caps &= ~NFS_CAP_ACLS;
703}
704#endif
705
706/*
707 * Create a general RPC client
708 */
Trond Myklebust33170232007-12-20 16:03:59 -0500709static int nfs_init_server_rpcclient(struct nfs_server *server,
710 const struct rpc_timeout *timeo,
711 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400712{
713 struct nfs_client *clp = server->nfs_client;
714
715 server->client = rpc_clone_client(clp->cl_rpcclient);
716 if (IS_ERR(server->client)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700717 dprintk("%s: couldn't create rpc_client!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400718 return PTR_ERR(server->client);
719 }
720
Trond Myklebust33170232007-12-20 16:03:59 -0500721 memcpy(&server->client->cl_timeout_default,
722 timeo,
723 sizeof(server->client->cl_timeout_default));
724 server->client->cl_timeout = &server->client->cl_timeout_default;
725
David Howells54ceac42006-08-22 20:06:13 -0400726 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
727 struct rpc_auth *auth;
728
729 auth = rpcauth_create(pseudoflavour, server->client);
730 if (IS_ERR(auth)) {
Harvey Harrison3110ff82008-05-02 13:42:44 -0700731 dprintk("%s: couldn't create credcache!\n", __func__);
David Howells54ceac42006-08-22 20:06:13 -0400732 return PTR_ERR(auth);
733 }
734 }
735 server->client->cl_softrtry = 0;
736 if (server->flags & NFS_MOUNT_SOFT)
737 server->client->cl_softrtry = 1;
738
David Howells54ceac42006-08-22 20:06:13 -0400739 return 0;
740}
741
742/*
743 * Initialise an NFS2 or NFS3 client
744 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400745static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500746 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400747 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400748{
David Howells54ceac42006-08-22 20:06:13 -0400749 int error;
750
751 if (clp->cl_cons_state == NFS_CS_READY) {
752 /* the client is already initialised */
753 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
754 return 0;
755 }
756
David Howells54ceac42006-08-22 20:06:13 -0400757 /*
758 * Create a client RPC handle for doing FSSTAT with UNIX auth only
759 * - RFC 2623, sec 2.3.2
760 */
Chuck Leverd7403512008-12-23 15:21:37 -0500761 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX,
762 0, data->flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -0400763 if (error < 0)
764 goto error;
765 nfs_mark_client_ready(clp, NFS_CS_READY);
766 return 0;
767
768error:
769 nfs_mark_client_ready(clp, error);
770 dprintk("<-- nfs_init_client() = xerror %d\n", error);
771 return error;
772}
773
774/*
775 * Create a version 2 or 3 client
776 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400777static int nfs_init_server(struct nfs_server *server,
778 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400779{
Trond Myklebust3a498022007-12-14 14:56:04 -0500780 struct nfs_client_initdata cl_init = {
781 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500782 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500783 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500784 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500785 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500786 };
Trond Myklebust33170232007-12-20 16:03:59 -0500787 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400788 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500789 int error;
David Howells54ceac42006-08-22 20:06:13 -0400790
791 dprintk("--> nfs_init_server()\n");
792
793#ifdef CONFIG_NFS_V3
794 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500795 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400796#endif
797
798 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500799 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400800 if (IS_ERR(clp)) {
801 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
802 return PTR_ERR(clp);
803 }
804
Trond Myklebust33170232007-12-20 16:03:59 -0500805 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
806 data->timeo, data->retrans);
807 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400808 if (error < 0)
809 goto error;
810
811 server->nfs_client = clp;
812
813 /* Initialise the client representation from the mount data */
Trond Myklebustff3525a2008-08-15 16:59:14 -0400814 server->flags = data->flags;
David Howellsb797cac2009-04-03 16:42:48 +0100815 server->options = data->options;
David Howells54ceac42006-08-22 20:06:13 -0400816
817 if (data->rsize)
818 server->rsize = nfs_block_size(data->rsize, NULL);
819 if (data->wsize)
820 server->wsize = nfs_block_size(data->wsize, NULL);
821
822 server->acregmin = data->acregmin * HZ;
823 server->acregmax = data->acregmax * HZ;
824 server->acdirmin = data->acdirmin * HZ;
825 server->acdirmax = data->acdirmax * HZ;
826
827 /* Start lockd here, before we might error out */
828 error = nfs_start_lockd(server);
829 if (error < 0)
830 goto error;
831
Chuck Leverf22d6d72008-03-14 14:10:22 -0400832 server->port = data->nfs_server.port;
833
Trond Myklebust33170232007-12-20 16:03:59 -0500834 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400835 if (error < 0)
836 goto error;
837
Chuck Lever3f8400d2008-03-14 14:10:30 -0400838 /* Preserve the values of mount_server-related mount options */
839 if (data->mount_server.addrlen) {
840 memcpy(&server->mountd_address, &data->mount_server.address,
841 data->mount_server.addrlen);
842 server->mountd_addrlen = data->mount_server.addrlen;
843 }
844 server->mountd_version = data->mount_server.version;
845 server->mountd_port = data->mount_server.port;
846 server->mountd_protocol = data->mount_server.protocol;
847
David Howells54ceac42006-08-22 20:06:13 -0400848 server->namelen = data->namlen;
849 /* Create a client RPC handle for the NFSv3 ACL management interface */
850 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400851 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
852 return 0;
853
854error:
855 server->nfs_client = NULL;
856 nfs_put_client(clp);
857 dprintk("<-- nfs_init_server() = xerror %d\n", error);
858 return error;
859}
860
861/*
862 * Load up the server record from information gained in an fsinfo record
863 */
864static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
865{
866 unsigned long max_rpc_payload;
867
868 /* Work out a lot of parameters */
869 if (server->rsize == 0)
870 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
871 if (server->wsize == 0)
872 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
873
874 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
875 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
876 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
877 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
878
879 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
880 if (server->rsize > max_rpc_payload)
881 server->rsize = max_rpc_payload;
882 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
883 server->rsize = NFS_MAX_FILE_IO_SIZE;
884 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700885
David Howells54ceac42006-08-22 20:06:13 -0400886 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
887
888 if (server->wsize > max_rpc_payload)
889 server->wsize = max_rpc_payload;
890 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
891 server->wsize = NFS_MAX_FILE_IO_SIZE;
892 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
893 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
894
895 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
896 if (server->dtsize > PAGE_CACHE_SIZE)
897 server->dtsize = PAGE_CACHE_SIZE;
898 if (server->dtsize > server->rsize)
899 server->dtsize = server->rsize;
900
901 if (server->flags & NFS_MOUNT_NOAC) {
902 server->acregmin = server->acregmax = 0;
903 server->acdirmin = server->acdirmax = 0;
904 }
905
906 server->maxfilesize = fsinfo->maxfilesize;
907
908 /* We're airborne Set socket buffersize */
909 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
910}
911
912/*
913 * Probe filesystem information, including the FSID on v2/v3
914 */
915static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
916{
917 struct nfs_fsinfo fsinfo;
918 struct nfs_client *clp = server->nfs_client;
919 int error;
920
921 dprintk("--> nfs_probe_fsinfo()\n");
922
923 if (clp->rpc_ops->set_capabilities != NULL) {
924 error = clp->rpc_ops->set_capabilities(server, mntfh);
925 if (error < 0)
926 goto out_error;
927 }
928
929 fsinfo.fattr = fattr;
930 nfs_fattr_init(fattr);
931 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
932 if (error < 0)
933 goto out_error;
934
935 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700936 error = bdi_init(&server->backing_dev_info);
937 if (error)
938 goto out_error;
939
David Howells54ceac42006-08-22 20:06:13 -0400940
941 /* Get some general file system info */
942 if (server->namelen == 0) {
943 struct nfs_pathconf pathinfo;
944
945 pathinfo.fattr = fattr;
946 nfs_fattr_init(fattr);
947
948 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
949 server->namelen = pathinfo.max_namelen;
950 }
951
952 dprintk("<-- nfs_probe_fsinfo() = 0\n");
953 return 0;
954
955out_error:
956 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
957 return error;
958}
959
960/*
961 * Copy useful information when duplicating a server record
962 */
963static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
964{
965 target->flags = source->flags;
966 target->acregmin = source->acregmin;
967 target->acregmax = source->acregmax;
968 target->acdirmin = source->acdirmin;
969 target->acdirmax = source->acdirmax;
970 target->caps = source->caps;
971}
972
973/*
974 * Allocate and initialise a server record
975 */
976static struct nfs_server *nfs_alloc_server(void)
977{
978 struct nfs_server *server;
979
980 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
981 if (!server)
982 return NULL;
983
984 server->client = server->client_acl = ERR_PTR(-EINVAL);
985
986 /* Zero out the NFS state stuff */
987 INIT_LIST_HEAD(&server->client_link);
988 INIT_LIST_HEAD(&server->master_link);
989
Steve Dicksonef818a22007-11-08 04:05:04 -0500990 atomic_set(&server->active, 0);
991
David Howells54ceac42006-08-22 20:06:13 -0400992 server->io_stats = nfs_alloc_iostats();
993 if (!server->io_stats) {
994 kfree(server);
995 return NULL;
996 }
997
998 return server;
999}
1000
1001/*
1002 * Free up a server record
1003 */
1004void nfs_free_server(struct nfs_server *server)
1005{
1006 dprintk("--> nfs_free_server()\n");
1007
1008 spin_lock(&nfs_client_lock);
1009 list_del(&server->client_link);
1010 list_del(&server->master_link);
1011 spin_unlock(&nfs_client_lock);
1012
1013 if (server->destroy != NULL)
1014 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -05001015
1016 if (!IS_ERR(server->client_acl))
1017 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -04001018 if (!IS_ERR(server->client))
1019 rpc_shutdown_client(server->client);
1020
1021 nfs_put_client(server->nfs_client);
1022
1023 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -07001024 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -04001025 kfree(server);
1026 nfs_release_automount_timer();
1027 dprintk("<-- nfs_free_server()\n");
1028}
1029
1030/*
1031 * Create a version 2 or 3 volume record
1032 * - keyed on server and FSID
1033 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -04001034struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001035 struct nfs_fh *mntfh)
1036{
1037 struct nfs_server *server;
1038 struct nfs_fattr fattr;
1039 int error;
1040
1041 server = nfs_alloc_server();
1042 if (!server)
1043 return ERR_PTR(-ENOMEM);
1044
1045 /* Get a client representation */
1046 error = nfs_init_server(server, data);
1047 if (error < 0)
1048 goto error;
1049
1050 BUG_ON(!server->nfs_client);
1051 BUG_ON(!server->nfs_client->rpc_ops);
1052 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1053
1054 /* Probe the root fh to retrieve its FSID */
1055 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1056 if (error < 0)
1057 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001058 if (server->nfs_client->rpc_ops->version == 3) {
1059 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
1060 server->namelen = NFS3_MAXNAMLEN;
1061 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
1062 server->caps |= NFS_CAP_READDIRPLUS;
1063 } else {
1064 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
1065 server->namelen = NFS2_MAXNAMLEN;
1066 }
1067
David Howells54ceac42006-08-22 20:06:13 -04001068 if (!(fattr.valid & NFS_ATTR_FATTR)) {
1069 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
1070 if (error < 0) {
1071 dprintk("nfs_create_server: getattr error = %d\n", -error);
1072 goto error;
1073 }
1074 }
1075 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
1076
David Howells6daabf12006-08-24 15:44:16 -04001077 dprintk("Server FSID: %llx:%llx\n",
1078 (unsigned long long) server->fsid.major,
1079 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001080
1081 BUG_ON(!server->nfs_client);
1082 BUG_ON(!server->nfs_client->rpc_ops);
1083 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1084
1085 spin_lock(&nfs_client_lock);
1086 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1087 list_add_tail(&server->master_link, &nfs_volume_list);
1088 spin_unlock(&nfs_client_lock);
1089
1090 server->mount_time = jiffies;
1091 return server;
1092
1093error:
1094 nfs_free_server(server);
1095 return ERR_PTR(error);
1096}
1097
1098#ifdef CONFIG_NFS_V4
1099/*
Benny Halevy9bdaa862009-04-01 09:22:55 -04001100 * Initialize the NFS4 callback service
1101 */
1102static int nfs4_init_callback(struct nfs_client *clp)
1103{
1104 int error;
1105
1106 if (clp->rpc_ops->version == 4) {
1107 error = nfs_callback_up();
1108 if (error < 0) {
1109 dprintk("%s: failed to start callback. Error = %d\n",
1110 __func__, error);
1111 return error;
1112 }
1113 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
1114 }
1115 return 0;
1116}
1117
1118/*
Andy Adamson557134a2009-04-01 09:21:53 -04001119 * Initialize the minor version specific parts of an NFS4 client record
1120 */
1121static int nfs4_init_client_minor_version(struct nfs_client *clp)
1122{
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001123 clp->cl_call_sync = _nfs4_call_sync;
1124
Andy Adamson557134a2009-04-01 09:21:53 -04001125#if defined(CONFIG_NFS_V4_1)
1126 if (clp->cl_minorversion) {
1127 struct nfs4_session *session = NULL;
1128 /*
1129 * Create the session and mark it expired.
1130 * When a SEQUENCE operation encounters the expired session
1131 * it will do session recovery to initialize it.
1132 */
1133 session = nfs4_alloc_session(clp);
1134 if (!session)
1135 return -ENOMEM;
1136
1137 clp->cl_session = session;
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001138 clp->cl_call_sync = _nfs4_call_sync_session;
Andy Adamson557134a2009-04-01 09:21:53 -04001139 }
1140#endif /* CONFIG_NFS_V4_1 */
1141
1142 return 0;
1143}
1144
1145/*
David Howells54ceac42006-08-22 20:06:13 -04001146 * Initialise an NFS4 client record
1147 */
1148static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -05001149 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001150 const char *ip_addr,
Chuck Leverd7403512008-12-23 15:21:37 -05001151 rpc_authflavor_t authflavour,
1152 int flags)
David Howells54ceac42006-08-22 20:06:13 -04001153{
1154 int error;
1155
1156 if (clp->cl_cons_state == NFS_CS_READY) {
1157 /* the client is initialised already */
1158 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
1159 return 0;
1160 }
1161
1162 /* Check NFS protocol revision and initialize RPC op vector */
1163 clp->rpc_ops = &nfs_v4_clientops;
1164
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001165 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Leverd7403512008-12-23 15:21:37 -05001166 1, flags & NFS_MOUNT_NORESVPORT);
David Howells54ceac42006-08-22 20:06:13 -04001167 if (error < 0)
1168 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001169 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -04001170
1171 error = nfs_idmap_new(clp);
1172 if (error < 0) {
1173 dprintk("%s: failed to create idmapper. Error = %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -07001174 __func__, error);
David Howells54ceac42006-08-22 20:06:13 -04001175 goto error;
1176 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -04001177 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -04001178
Andy Adamson557134a2009-04-01 09:21:53 -04001179 error = nfs4_init_client_minor_version(clp);
1180 if (error < 0)
1181 goto error;
1182
Andy Adamson76db6d92009-04-01 09:22:38 -04001183 if (!nfs4_has_session(clp))
1184 nfs_mark_client_ready(clp, NFS_CS_READY);
David Howells54ceac42006-08-22 20:06:13 -04001185 return 0;
1186
1187error:
1188 nfs_mark_client_ready(clp, error);
1189 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1190 return error;
1191}
1192
1193/*
1194 * Set up an NFS4 client
1195 */
1196static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -05001197 const char *hostname,
1198 const struct sockaddr *addr,
1199 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001200 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -04001201 rpc_authflavor_t authflavour,
Benny Halevy94a417f2009-04-01 09:21:49 -04001202 int proto, const struct rpc_timeout *timeparms,
1203 u32 minorversion)
David Howells54ceac42006-08-22 20:06:13 -04001204{
Trond Myklebust3a498022007-12-14 14:56:04 -05001205 struct nfs_client_initdata cl_init = {
1206 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001207 .addr = addr,
1208 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -05001209 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001210 .proto = proto,
Benny Halevy5aae4a92009-04-01 09:21:50 -04001211 .minorversion = minorversion,
Trond Myklebust3a498022007-12-14 14:56:04 -05001212 };
David Howells54ceac42006-08-22 20:06:13 -04001213 struct nfs_client *clp;
1214 int error;
1215
1216 dprintk("--> nfs4_set_client()\n");
1217
1218 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001219 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001220 if (IS_ERR(clp)) {
1221 error = PTR_ERR(clp);
1222 goto error;
1223 }
Chuck Leverd7403512008-12-23 15:21:37 -05001224 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour,
1225 server->flags);
David Howells54ceac42006-08-22 20:06:13 -04001226 if (error < 0)
1227 goto error_put;
1228
1229 server->nfs_client = clp;
1230 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1231 return 0;
1232
1233error_put:
1234 nfs_put_client(clp);
1235error:
1236 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1237 return error;
1238}
1239
1240/*
Andy Adamson557134a2009-04-01 09:21:53 -04001241 * Initialize a session.
1242 * Note: save the mount rsize and wsize for create_server negotiation.
1243 */
1244static void nfs4_init_session(struct nfs_client *clp,
1245 unsigned int wsize, unsigned int rsize)
1246{
1247#if defined(CONFIG_NFS_V4_1)
1248 if (nfs4_has_session(clp)) {
1249 clp->cl_session->fc_attrs.max_rqst_sz = wsize;
1250 clp->cl_session->fc_attrs.max_resp_sz = rsize;
1251 }
1252#endif /* CONFIG_NFS_V4_1 */
1253}
1254
1255/*
Andy Adamson96b09e02009-04-01 09:22:33 -04001256 * Session has been established, and the client marked ready.
1257 * Set the mount rsize and wsize with negotiated fore channel
1258 * attributes which will be bound checked in nfs_server_set_fsinfo.
1259 */
1260static void nfs4_session_set_rwsize(struct nfs_server *server)
1261{
1262#ifdef CONFIG_NFS_V4_1
1263 if (!nfs4_has_session(server->nfs_client))
1264 return;
1265 server->rsize = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
1266 server->wsize = server->nfs_client->cl_session->fc_attrs.max_rqst_sz;
1267#endif /* CONFIG_NFS_V4_1 */
1268}
1269
1270/*
David Howells54ceac42006-08-22 20:06:13 -04001271 * Create a version 4 volume record
1272 */
1273static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001274 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001275{
Trond Myklebust33170232007-12-20 16:03:59 -05001276 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001277 int error;
1278
1279 dprintk("--> nfs4_init_server()\n");
1280
Trond Myklebust33170232007-12-20 16:03:59 -05001281 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1282 data->timeo, data->retrans);
1283
Chuck Lever542fcc32008-12-23 15:21:36 -05001284 /* Initialise the client representation from the mount data */
1285 server->flags = data->flags;
1286 server->caps |= NFS_CAP_ATOMIC_OPEN;
David Howellsb797cac2009-04-03 16:42:48 +01001287 server->options = data->options;
Chuck Lever542fcc32008-12-23 15:21:36 -05001288
Trond Myklebust33170232007-12-20 16:03:59 -05001289 /* Get a client record */
1290 error = nfs4_set_client(server,
1291 data->nfs_server.hostname,
1292 (const struct sockaddr *)&data->nfs_server.address,
1293 data->nfs_server.addrlen,
1294 data->client_address,
1295 data->auth_flavors[0],
1296 data->nfs_server.protocol,
Benny Halevy94a417f2009-04-01 09:21:49 -04001297 &timeparms,
1298 data->minorversion);
Trond Myklebust33170232007-12-20 16:03:59 -05001299 if (error < 0)
1300 goto error;
1301
David Howells54ceac42006-08-22 20:06:13 -04001302 if (data->rsize)
1303 server->rsize = nfs_block_size(data->rsize, NULL);
1304 if (data->wsize)
1305 server->wsize = nfs_block_size(data->wsize, NULL);
1306
1307 server->acregmin = data->acregmin * HZ;
1308 server->acregmax = data->acregmax * HZ;
1309 server->acdirmin = data->acdirmin * HZ;
1310 server->acdirmax = data->acdirmax * HZ;
1311
Chuck Leverf22d6d72008-03-14 14:10:22 -04001312 server->port = data->nfs_server.port;
1313
Trond Myklebust33170232007-12-20 16:03:59 -05001314 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001315
Trond Myklebust33170232007-12-20 16:03:59 -05001316error:
David Howells54ceac42006-08-22 20:06:13 -04001317 /* Done */
1318 dprintk("<-- nfs4_init_server() = %d\n", error);
1319 return error;
1320}
1321
1322/*
1323 * Create a version 4 volume record
1324 * - keyed on server and FSID
1325 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001326struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001327 struct nfs_fh *mntfh)
1328{
1329 struct nfs_fattr fattr;
1330 struct nfs_server *server;
1331 int error;
1332
1333 dprintk("--> nfs4_create_server()\n");
1334
1335 server = nfs_alloc_server();
1336 if (!server)
1337 return ERR_PTR(-ENOMEM);
1338
David Howells54ceac42006-08-22 20:06:13 -04001339 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001340 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001341 if (error < 0)
1342 goto error;
1343
1344 BUG_ON(!server->nfs_client);
1345 BUG_ON(!server->nfs_client->rpc_ops);
1346 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1347
Andy Adamson557134a2009-04-01 09:21:53 -04001348 nfs4_init_session(server->nfs_client, server->wsize, server->rsize);
1349
David Howells54ceac42006-08-22 20:06:13 -04001350 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001351 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001352 if (error < 0)
1353 goto error;
1354
David Howells6daabf12006-08-24 15:44:16 -04001355 dprintk("Server FSID: %llx:%llx\n",
1356 (unsigned long long) server->fsid.major,
1357 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001358 dprintk("Mount FH: %d\n", mntfh->size);
1359
Andy Adamson96b09e02009-04-01 09:22:33 -04001360 nfs4_session_set_rwsize(server);
1361
David Howells54ceac42006-08-22 20:06:13 -04001362 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1363 if (error < 0)
1364 goto error;
1365
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001366 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1367 server->namelen = NFS4_MAXNAMLEN;
1368
David Howells54ceac42006-08-22 20:06:13 -04001369 BUG_ON(!server->nfs_client);
1370 BUG_ON(!server->nfs_client->rpc_ops);
1371 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1372
1373 spin_lock(&nfs_client_lock);
1374 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1375 list_add_tail(&server->master_link, &nfs_volume_list);
1376 spin_unlock(&nfs_client_lock);
1377
1378 server->mount_time = jiffies;
1379 dprintk("<-- nfs4_create_server() = %p\n", server);
1380 return server;
1381
1382error:
1383 nfs_free_server(server);
1384 dprintk("<-- nfs4_create_server() = error %d\n", error);
1385 return ERR_PTR(error);
1386}
1387
1388/*
1389 * Create an NFS4 referral server record
1390 */
1391struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001392 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001393{
1394 struct nfs_client *parent_client;
1395 struct nfs_server *server, *parent_server;
1396 struct nfs_fattr fattr;
1397 int error;
1398
1399 dprintk("--> nfs4_create_referral_server()\n");
1400
1401 server = nfs_alloc_server();
1402 if (!server)
1403 return ERR_PTR(-ENOMEM);
1404
1405 parent_server = NFS_SB(data->sb);
1406 parent_client = parent_server->nfs_client;
1407
Chuck Lever542fcc32008-12-23 15:21:36 -05001408 /* Initialise the client representation from the parent server */
1409 nfs_server_copy_userdata(server, parent_server);
1410 server->caps |= NFS_CAP_ATOMIC_OPEN;
1411
David Howells54ceac42006-08-22 20:06:13 -04001412 /* Get a client representation.
1413 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001414 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001415 data->addr,
1416 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001417 parent_client->cl_ipaddr,
1418 data->authflavor,
1419 parent_server->client->cl_xprt->prot,
Benny Halevy94a417f2009-04-01 09:21:49 -04001420 parent_server->client->cl_timeout,
1421 parent_client->cl_minorversion);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001422 if (error < 0)
1423 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001424
Trond Myklebust33170232007-12-20 16:03:59 -05001425 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001426 if (error < 0)
1427 goto error;
1428
1429 BUG_ON(!server->nfs_client);
1430 BUG_ON(!server->nfs_client->rpc_ops);
1431 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1432
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001433 /* Probe the root fh to retrieve its FSID and filehandle */
1434 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1435 if (error < 0)
1436 goto error;
1437
David Howells54ceac42006-08-22 20:06:13 -04001438 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001439 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001440 if (error < 0)
1441 goto error;
1442
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001443 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1444 server->namelen = NFS4_MAXNAMLEN;
1445
David Howells54ceac42006-08-22 20:06:13 -04001446 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001447 (unsigned long long) server->fsid.major,
1448 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001449
1450 spin_lock(&nfs_client_lock);
1451 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1452 list_add_tail(&server->master_link, &nfs_volume_list);
1453 spin_unlock(&nfs_client_lock);
1454
1455 server->mount_time = jiffies;
1456
1457 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1458 return server;
1459
1460error:
1461 nfs_free_server(server);
1462 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1463 return ERR_PTR(error);
1464}
1465
1466#endif /* CONFIG_NFS_V4 */
1467
1468/*
1469 * Clone an NFS2, NFS3 or NFS4 server record
1470 */
1471struct nfs_server *nfs_clone_server(struct nfs_server *source,
1472 struct nfs_fh *fh,
1473 struct nfs_fattr *fattr)
1474{
1475 struct nfs_server *server;
1476 struct nfs_fattr fattr_fsinfo;
1477 int error;
1478
1479 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001480 (unsigned long long) fattr->fsid.major,
1481 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001482
1483 server = nfs_alloc_server();
1484 if (!server)
1485 return ERR_PTR(-ENOMEM);
1486
1487 /* Copy data from the source */
1488 server->nfs_client = source->nfs_client;
1489 atomic_inc(&server->nfs_client->cl_count);
1490 nfs_server_copy_userdata(server, source);
1491
1492 server->fsid = fattr->fsid;
1493
Trond Myklebust33170232007-12-20 16:03:59 -05001494 error = nfs_init_server_rpcclient(server,
1495 source->client->cl_timeout,
1496 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001497 if (error < 0)
1498 goto out_free_server;
1499 if (!IS_ERR(source->client_acl))
1500 nfs_init_server_aclclient(server);
1501
1502 /* probe the filesystem info for this server filesystem */
1503 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1504 if (error < 0)
1505 goto out_free_server;
1506
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001507 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1508 server->namelen = NFS4_MAXNAMLEN;
1509
David Howells54ceac42006-08-22 20:06:13 -04001510 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001511 (unsigned long long) server->fsid.major,
1512 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001513
1514 error = nfs_start_lockd(server);
1515 if (error < 0)
1516 goto out_free_server;
1517
1518 spin_lock(&nfs_client_lock);
1519 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1520 list_add_tail(&server->master_link, &nfs_volume_list);
1521 spin_unlock(&nfs_client_lock);
1522
1523 server->mount_time = jiffies;
1524
1525 dprintk("<-- nfs_clone_server() = %p\n", server);
1526 return server;
1527
1528out_free_server:
1529 nfs_free_server(server);
1530 dprintk("<-- nfs_clone_server() = error %d\n", error);
1531 return ERR_PTR(error);
1532}
David Howells6aaca562006-08-22 20:06:13 -04001533
1534#ifdef CONFIG_PROC_FS
1535static struct proc_dir_entry *proc_fs_nfs;
1536
1537static int nfs_server_list_open(struct inode *inode, struct file *file);
1538static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1539static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1540static void nfs_server_list_stop(struct seq_file *p, void *v);
1541static int nfs_server_list_show(struct seq_file *m, void *v);
1542
1543static struct seq_operations nfs_server_list_ops = {
1544 .start = nfs_server_list_start,
1545 .next = nfs_server_list_next,
1546 .stop = nfs_server_list_stop,
1547 .show = nfs_server_list_show,
1548};
1549
Arjan van de Ven00977a52007-02-12 00:55:34 -08001550static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001551 .open = nfs_server_list_open,
1552 .read = seq_read,
1553 .llseek = seq_lseek,
1554 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001555 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001556};
1557
1558static int nfs_volume_list_open(struct inode *inode, struct file *file);
1559static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1560static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1561static void nfs_volume_list_stop(struct seq_file *p, void *v);
1562static int nfs_volume_list_show(struct seq_file *m, void *v);
1563
1564static struct seq_operations nfs_volume_list_ops = {
1565 .start = nfs_volume_list_start,
1566 .next = nfs_volume_list_next,
1567 .stop = nfs_volume_list_stop,
1568 .show = nfs_volume_list_show,
1569};
1570
Arjan van de Ven00977a52007-02-12 00:55:34 -08001571static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001572 .open = nfs_volume_list_open,
1573 .read = seq_read,
1574 .llseek = seq_lseek,
1575 .release = seq_release,
Denis V. Lunev34b37232008-04-29 01:02:07 -07001576 .owner = THIS_MODULE,
David Howells6aaca562006-08-22 20:06:13 -04001577};
1578
1579/*
1580 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1581 * we're dealing
1582 */
1583static int nfs_server_list_open(struct inode *inode, struct file *file)
1584{
1585 struct seq_file *m;
1586 int ret;
1587
1588 ret = seq_open(file, &nfs_server_list_ops);
1589 if (ret < 0)
1590 return ret;
1591
1592 m = file->private_data;
1593 m->private = PDE(inode)->data;
1594
1595 return 0;
1596}
1597
1598/*
1599 * set up the iterator to start reading from the server list and return the first item
1600 */
1601static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1602{
David Howells6aaca562006-08-22 20:06:13 -04001603 /* lock the list against modification */
1604 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001605 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001606}
1607
1608/*
1609 * move to next server
1610 */
1611static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1612{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001613 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001614}
1615
1616/*
1617 * clean up after reading from the transports list
1618 */
1619static void nfs_server_list_stop(struct seq_file *p, void *v)
1620{
1621 spin_unlock(&nfs_client_lock);
1622}
1623
1624/*
1625 * display a header line followed by a load of call lines
1626 */
1627static int nfs_server_list_show(struct seq_file *m, void *v)
1628{
1629 struct nfs_client *clp;
1630
1631 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001632 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001633 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1634 return 0;
1635 }
1636
1637 /* display one transport per line on subsequent lines */
1638 clp = list_entry(v, struct nfs_client, cl_share_link);
1639
Chuck Lever5d8515c2007-12-10 14:57:16 -05001640 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001641 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001642 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1643 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001644 atomic_read(&clp->cl_count),
1645 clp->cl_hostname);
1646
1647 return 0;
1648}
1649
1650/*
1651 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1652 */
1653static int nfs_volume_list_open(struct inode *inode, struct file *file)
1654{
1655 struct seq_file *m;
1656 int ret;
1657
1658 ret = seq_open(file, &nfs_volume_list_ops);
1659 if (ret < 0)
1660 return ret;
1661
1662 m = file->private_data;
1663 m->private = PDE(inode)->data;
1664
1665 return 0;
1666}
1667
1668/*
1669 * set up the iterator to start reading from the volume list and return the first item
1670 */
1671static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1672{
David Howells6aaca562006-08-22 20:06:13 -04001673 /* lock the list against modification */
1674 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001675 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001676}
1677
1678/*
1679 * move to next volume
1680 */
1681static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1682{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001683 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001684}
1685
1686/*
1687 * clean up after reading from the transports list
1688 */
1689static void nfs_volume_list_stop(struct seq_file *p, void *v)
1690{
1691 spin_unlock(&nfs_client_lock);
1692}
1693
1694/*
1695 * display a header line followed by a load of call lines
1696 */
1697static int nfs_volume_list_show(struct seq_file *m, void *v)
1698{
1699 struct nfs_server *server;
1700 struct nfs_client *clp;
1701 char dev[8], fsid[17];
1702
1703 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001704 if (v == &nfs_volume_list) {
David Howells5d1acff2009-04-03 16:42:47 +01001705 seq_puts(m, "NV SERVER PORT DEV FSID FSC\n");
David Howells6aaca562006-08-22 20:06:13 -04001706 return 0;
1707 }
1708 /* display one transport per line on subsequent lines */
1709 server = list_entry(v, struct nfs_server, master_link);
1710 clp = server->nfs_client;
1711
1712 snprintf(dev, 8, "%u:%u",
1713 MAJOR(server->s_dev), MINOR(server->s_dev));
1714
1715 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001716 (unsigned long long) server->fsid.major,
1717 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001718
David Howells5d1acff2009-04-03 16:42:47 +01001719 seq_printf(m, "v%u %s %s %-7s %-17s %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001720 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001721 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1722 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001723 dev,
David Howells5d1acff2009-04-03 16:42:47 +01001724 fsid,
1725 nfs_server_fscache_state(server));
David Howells6aaca562006-08-22 20:06:13 -04001726
1727 return 0;
1728}
1729
1730/*
1731 * initialise the /proc/fs/nfsfs/ directory
1732 */
1733int __init nfs_fs_proc_init(void)
1734{
1735 struct proc_dir_entry *p;
1736
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001737 proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001738 if (!proc_fs_nfs)
1739 goto error_0;
1740
David Howells6aaca562006-08-22 20:06:13 -04001741 /* a file of servers with which we're dealing */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001742 p = proc_create("servers", S_IFREG|S_IRUGO,
1743 proc_fs_nfs, &nfs_server_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001744 if (!p)
1745 goto error_1;
1746
David Howells6aaca562006-08-22 20:06:13 -04001747 /* a file of volumes that we have mounted */
Denis V. Lunev34b37232008-04-29 01:02:07 -07001748 p = proc_create("volumes", S_IFREG|S_IRUGO,
1749 proc_fs_nfs, &nfs_volume_list_fops);
David Howells6aaca562006-08-22 20:06:13 -04001750 if (!p)
1751 goto error_2;
David Howells6aaca562006-08-22 20:06:13 -04001752 return 0;
1753
1754error_2:
1755 remove_proc_entry("servers", proc_fs_nfs);
1756error_1:
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001757 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001758error_0:
1759 return -ENOMEM;
1760}
1761
1762/*
1763 * clean up the /proc/fs/nfsfs/ directory
1764 */
1765void nfs_fs_proc_exit(void)
1766{
1767 remove_proc_entry("volumes", proc_fs_nfs);
1768 remove_proc_entry("servers", proc_fs_nfs);
Alexey Dobriyan36a5aeb2008-04-29 01:01:42 -07001769 remove_proc_entry("fs/nfsfs", NULL);
David Howells6aaca562006-08-22 20:06:13 -04001770}
1771
1772#endif /* CONFIG_PROC_FS */