blob: 18fcb05a070754f5050f2340313565f1fe290e88 [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"
48
49#define NFSDBG_FACILITY NFSDBG_CLIENT
50
51static DEFINE_SPINLOCK(nfs_client_lock);
52static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040053static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040054static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55
56/*
David Howells5006a762006-08-22 20:06:12 -040057 * RPC cruft for NFS
58 */
59static struct rpc_version *nfs_version[5] = {
60 [2] = &nfs_version2,
61#ifdef CONFIG_NFS_V3
62 [3] = &nfs_version3,
63#endif
64#ifdef CONFIG_NFS_V4
65 [4] = &nfs_version4,
66#endif
67};
68
69struct rpc_program nfs_program = {
70 .name = "nfs",
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
76};
77
78struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
80};
81
82
83#ifdef CONFIG_NFS_V3_ACL
84static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
87};
88
89struct rpc_program nfsacl_program = {
90 .name = "nfsacl",
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
95};
96#endif /* CONFIG_NFS_V3_ACL */
97
Trond Myklebust3a498022007-12-14 14:56:04 -050098struct nfs_client_initdata {
99 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500100 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500101 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500102 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500103 int proto;
Trond Myklebust3a498022007-12-14 14:56:04 -0500104};
105
David Howells5006a762006-08-22 20:06:12 -0400106/*
David Howells24c8dbb2006-08-22 20:06:10 -0400107 * Allocate a shared client record
108 *
109 * Since these are allocated/deallocated very rarely, we don't
110 * bother putting them in a slab cache...
111 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500112static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400113{
114 struct nfs_client *clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400115
116 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
117 goto error_0;
118
Trond Myklebust40c553192007-12-14 14:56:07 -0500119 clp->rpc_ops = cl_init->rpc_ops;
120
121 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400122 if (nfs_callback_up() < 0)
123 goto error_2;
124 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
125 }
126
127 atomic_set(&clp->cl_count, 1);
128 clp->cl_cons_state = NFS_CS_INITING;
129
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500130 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
131 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400132
Trond Myklebust3a498022007-12-14 14:56:04 -0500133 if (cl_init->hostname) {
134 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400135 if (!clp->cl_hostname)
136 goto error_3;
137 }
138
139 INIT_LIST_HEAD(&clp->cl_superblocks);
140 clp->cl_rpcclient = ERR_PTR(-EINVAL);
141
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500142 clp->cl_proto = cl_init->proto;
143
David Howells24c8dbb2006-08-22 20:06:10 -0400144#ifdef CONFIG_NFS_V4
145 init_rwsem(&clp->cl_sem);
146 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400147 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000148 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400149 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
150 clp->cl_boot_time = CURRENT_TIME;
151 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
152#endif
153
154 return clp;
155
156error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400157 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
158 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400159error_2:
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);
173#endif
174}
175
David Howells24c8dbb2006-08-22 20:06:10 -0400176/*
177 * Destroy a shared client record
178 */
179static void nfs_free_client(struct nfs_client *clp)
180{
Trond Myklebust40c553192007-12-14 14:56:07 -0500181 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400182
Trond Myklebust5dd31772006-08-24 01:03:05 -0400183 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400184
185 /* -EIO all pending I/O */
186 if (!IS_ERR(clp->cl_rpcclient))
187 rpc_shutdown_client(clp->cl_rpcclient);
188
189 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
190 nfs_callback_down();
191
David Howells24c8dbb2006-08-22 20:06:10 -0400192 kfree(clp->cl_hostname);
193 kfree(clp);
194
195 dprintk("<-- nfs_free_client()\n");
196}
197
198/*
199 * Release a reference to a shared client record
200 */
201void nfs_put_client(struct nfs_client *clp)
202{
David Howells27ba8512006-07-30 14:40:56 -0400203 if (!clp)
204 return;
205
David Howells24c8dbb2006-08-22 20:06:10 -0400206 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
207
208 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
209 list_del(&clp->cl_share_link);
210 spin_unlock(&nfs_client_lock);
211
212 BUG_ON(!list_empty(&clp->cl_superblocks));
213
214 nfs_free_client(clp);
215 }
216}
217
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500218static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
219 const struct sockaddr_in *sa2)
220{
221 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
222}
223
224static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
225 const struct sockaddr_in6 *sa2)
226{
227 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
228}
229
230static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
231 const struct sockaddr *sa2)
232{
233 switch (sa1->sa_family) {
234 case AF_INET:
235 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
236 (const struct sockaddr_in *)sa2);
237 case AF_INET6:
238 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
239 (const struct sockaddr_in6 *)sa2);
240 }
241 BUG();
242}
243
David Howells24c8dbb2006-08-22 20:06:10 -0400244/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500245 * Find a client by IP address and protocol version
246 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400247 */
Chuck Leverff052642007-12-10 14:58:44 -0500248struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500249{
250 struct nfs_client *clp;
251
252 spin_lock(&nfs_client_lock);
253 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500254 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
255
Trond Myklebustc81468a2007-12-14 14:56:05 -0500256 /* Don't match clients that failed to initialise properly */
257 if (clp->cl_cons_state != NFS_CS_READY)
258 continue;
259
260 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500261 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500262 continue;
263
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500264 if (addr->sa_family != clap->sa_family)
265 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500266 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500267 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500268 continue;
269
270 atomic_inc(&clp->cl_count);
271 spin_unlock(&nfs_client_lock);
272 return clp;
273 }
274 spin_unlock(&nfs_client_lock);
275 return NULL;
276}
277
278/*
279 * Find an nfs_client on the list that matches the initialisation data
280 * that is supplied.
281 */
282static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400283{
284 struct nfs_client *clp;
285
286 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700287 /* Don't match clients that failed to initialise properly */
288 if (clp->cl_cons_state < 0)
289 continue;
290
David Howells24c8dbb2006-08-22 20:06:10 -0400291 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500292 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400293 continue;
294
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500295 if (clp->cl_proto != data->proto)
296 continue;
297
Trond Myklebustc81468a2007-12-14 14:56:05 -0500298 /* Match the full socket address */
299 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400300 continue;
301
Trond Myklebustc81468a2007-12-14 14:56:05 -0500302 atomic_inc(&clp->cl_count);
303 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400304 }
David Howells24c8dbb2006-08-22 20:06:10 -0400305 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400306}
307
308/*
309 * Look up a client by IP address and protocol version
310 * - creates a new record if one doesn't yet exist
311 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500312static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400313{
314 struct nfs_client *clp, *new = NULL;
315 int error;
316
Chuck Leverd7422c42007-12-10 14:58:51 -0500317 dprintk("--> nfs_get_client(%s,v%u)\n",
318 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400319
320 /* see if the client already exists */
321 do {
322 spin_lock(&nfs_client_lock);
323
Trond Myklebustc81468a2007-12-14 14:56:05 -0500324 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400325 if (clp)
326 goto found_client;
327 if (new)
328 goto install_client;
329
330 spin_unlock(&nfs_client_lock);
331
Trond Myklebust3a498022007-12-14 14:56:04 -0500332 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400333 } while (new);
334
335 return ERR_PTR(-ENOMEM);
336
337 /* install a new client and return with it unready */
338install_client:
339 clp = new;
340 list_add(&clp->cl_share_link, &nfs_client_list);
341 spin_unlock(&nfs_client_lock);
342 dprintk("--> nfs_get_client() = %p [new]\n", clp);
343 return clp;
344
345 /* found an existing client
346 * - make sure it's ready before returning
347 */
348found_client:
349 spin_unlock(&nfs_client_lock);
350
351 if (new)
352 nfs_free_client(new);
353
Linus Torvalds83250492006-10-08 17:28:25 -0700354 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400355 clp->cl_cons_state != NFS_CS_INITING);
356 if (error < 0) {
357 nfs_put_client(clp);
358 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400359 }
360
361 if (clp->cl_cons_state < NFS_CS_READY) {
362 error = clp->cl_cons_state;
363 nfs_put_client(clp);
364 return ERR_PTR(error);
365 }
366
David Howells54ceac42006-08-22 20:06:13 -0400367 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
368
David Howells24c8dbb2006-08-22 20:06:10 -0400369 dprintk("--> nfs_get_client() = %p [share]\n", clp);
370 return clp;
371}
372
373/*
374 * Mark a server as ready or failed
375 */
David Howells54ceac42006-08-22 20:06:13 -0400376static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400377{
378 clp->cl_cons_state = state;
379 wake_up_all(&nfs_client_active_wq);
380}
David Howells5006a762006-08-22 20:06:12 -0400381
382/*
383 * Initialise the timeout values for a connection
384 */
385static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
386 unsigned int timeo, unsigned int retrans)
387{
388 to->to_initval = timeo * HZ / 10;
389 to->to_retries = retrans;
390 if (!to->to_retries)
391 to->to_retries = 2;
392
393 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400394 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400395 case XPRT_TRANSPORT_RDMA:
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500396 if (to->to_initval == 0)
David Howells5006a762006-08-22 20:06:12 -0400397 to->to_initval = 60 * HZ;
398 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
399 to->to_initval = NFS_MAX_TCP_TIMEOUT;
400 to->to_increment = to->to_initval;
401 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500402 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
403 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
404 if (to->to_maxval < to->to_initval)
405 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400406 to->to_exponential = 0;
407 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400408 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400409 default:
410 if (!to->to_initval)
411 to->to_initval = 11 * HZ / 10;
412 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
413 to->to_initval = NFS_MAX_UDP_TIMEOUT;
414 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
415 to->to_exponential = 1;
416 break;
417 }
418}
419
420/*
421 * Create an RPC client handle
422 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500423static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500424 const struct rpc_timeout *timeparms,
425 rpc_authflavor_t flavor,
426 int flags)
David Howells5006a762006-08-22 20:06:12 -0400427{
David Howells5006a762006-08-22 20:06:12 -0400428 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400429 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500430 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400431 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500432 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500433 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400434 .servername = clp->cl_hostname,
435 .program = &nfs_program,
436 .version = clp->rpc_ops->version,
437 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500438 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400439 };
David Howells5006a762006-08-22 20:06:12 -0400440
441 if (!IS_ERR(clp->cl_rpcclient))
442 return 0;
443
Chuck Lever41877d22006-08-22 20:06:20 -0400444 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400445 if (IS_ERR(clnt)) {
446 dprintk("%s: cannot create RPC client. Error = %ld\n",
447 __FUNCTION__, PTR_ERR(clnt));
448 return PTR_ERR(clnt);
449 }
450
David Howells5006a762006-08-22 20:06:12 -0400451 clp->cl_rpcclient = clnt;
452 return 0;
453}
David Howells54ceac42006-08-22 20:06:13 -0400454
455/*
456 * Version 2 or 3 client destruction
457 */
458static void nfs_destroy_server(struct nfs_server *server)
459{
David Howells54ceac42006-08-22 20:06:13 -0400460 if (!(server->flags & NFS_MOUNT_NONLM))
461 lockd_down(); /* release rpc.lockd */
462}
463
464/*
465 * Version 2 or 3 lockd setup
466 */
467static int nfs_start_lockd(struct nfs_server *server)
468{
469 int error = 0;
470
Trond Myklebust40c553192007-12-14 14:56:07 -0500471 if (server->nfs_client->rpc_ops->version > 3)
David Howells54ceac42006-08-22 20:06:13 -0400472 goto out;
473 if (server->flags & NFS_MOUNT_NONLM)
474 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700475 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
476 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400477 if (error < 0)
478 server->flags |= NFS_MOUNT_NONLM;
479 else
480 server->destroy = nfs_destroy_server;
481out:
482 return error;
483}
484
485/*
486 * Initialise an NFSv3 ACL client connection
487 */
488#ifdef CONFIG_NFS_V3_ACL
489static void nfs_init_server_aclclient(struct nfs_server *server)
490{
Trond Myklebust40c553192007-12-14 14:56:07 -0500491 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400492 goto out_noacl;
493 if (server->flags & NFS_MOUNT_NOACL)
494 goto out_noacl;
495
496 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
497 if (IS_ERR(server->client_acl))
498 goto out_noacl;
499
500 /* No errors! Assume that Sun nfsacls are supported */
501 server->caps |= NFS_CAP_ACLS;
502 return;
503
504out_noacl:
505 server->caps &= ~NFS_CAP_ACLS;
506}
507#else
508static inline void nfs_init_server_aclclient(struct nfs_server *server)
509{
510 server->flags &= ~NFS_MOUNT_NOACL;
511 server->caps &= ~NFS_CAP_ACLS;
512}
513#endif
514
515/*
516 * Create a general RPC client
517 */
Trond Myklebust33170232007-12-20 16:03:59 -0500518static int nfs_init_server_rpcclient(struct nfs_server *server,
519 const struct rpc_timeout *timeo,
520 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400521{
522 struct nfs_client *clp = server->nfs_client;
523
524 server->client = rpc_clone_client(clp->cl_rpcclient);
525 if (IS_ERR(server->client)) {
526 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
527 return PTR_ERR(server->client);
528 }
529
Trond Myklebust33170232007-12-20 16:03:59 -0500530 memcpy(&server->client->cl_timeout_default,
531 timeo,
532 sizeof(server->client->cl_timeout_default));
533 server->client->cl_timeout = &server->client->cl_timeout_default;
534
David Howells54ceac42006-08-22 20:06:13 -0400535 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
536 struct rpc_auth *auth;
537
538 auth = rpcauth_create(pseudoflavour, server->client);
539 if (IS_ERR(auth)) {
540 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
541 return PTR_ERR(auth);
542 }
543 }
544 server->client->cl_softrtry = 0;
545 if (server->flags & NFS_MOUNT_SOFT)
546 server->client->cl_softrtry = 1;
547
548 server->client->cl_intr = 0;
549 if (server->flags & NFS4_MOUNT_INTR)
550 server->client->cl_intr = 1;
551
552 return 0;
553}
554
555/*
556 * Initialise an NFS2 or NFS3 client
557 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400558static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500559 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400560 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400561{
David Howells54ceac42006-08-22 20:06:13 -0400562 int error;
563
564 if (clp->cl_cons_state == NFS_CS_READY) {
565 /* the client is already initialised */
566 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
567 return 0;
568 }
569
David Howells54ceac42006-08-22 20:06:13 -0400570 /*
571 * Create a client RPC handle for doing FSSTAT with UNIX auth only
572 * - RFC 2623, sec 2.3.2
573 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500574 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400575 if (error < 0)
576 goto error;
577 nfs_mark_client_ready(clp, NFS_CS_READY);
578 return 0;
579
580error:
581 nfs_mark_client_ready(clp, error);
582 dprintk("<-- nfs_init_client() = xerror %d\n", error);
583 return error;
584}
585
586/*
587 * Create a version 2 or 3 client
588 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400589static int nfs_init_server(struct nfs_server *server,
590 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400591{
Trond Myklebust3a498022007-12-14 14:56:04 -0500592 struct nfs_client_initdata cl_init = {
593 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500594 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500595 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500596 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500597 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500598 };
Trond Myklebust33170232007-12-20 16:03:59 -0500599 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400600 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500601 int error;
David Howells54ceac42006-08-22 20:06:13 -0400602
603 dprintk("--> nfs_init_server()\n");
604
605#ifdef CONFIG_NFS_V3
606 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500607 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400608#endif
609
610 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500611 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400612 if (IS_ERR(clp)) {
613 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
614 return PTR_ERR(clp);
615 }
616
Trond Myklebust33170232007-12-20 16:03:59 -0500617 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
618 data->timeo, data->retrans);
619 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400620 if (error < 0)
621 goto error;
622
623 server->nfs_client = clp;
624
625 /* Initialise the client representation from the mount data */
626 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
627
628 if (data->rsize)
629 server->rsize = nfs_block_size(data->rsize, NULL);
630 if (data->wsize)
631 server->wsize = nfs_block_size(data->wsize, NULL);
632
633 server->acregmin = data->acregmin * HZ;
634 server->acregmax = data->acregmax * HZ;
635 server->acdirmin = data->acdirmin * HZ;
636 server->acdirmax = data->acdirmax * HZ;
637
638 /* Start lockd here, before we might error out */
639 error = nfs_start_lockd(server);
640 if (error < 0)
641 goto error;
642
Trond Myklebust33170232007-12-20 16:03:59 -0500643 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400644 if (error < 0)
645 goto error;
646
647 server->namelen = data->namlen;
648 /* Create a client RPC handle for the NFSv3 ACL management interface */
649 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400650 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
651 return 0;
652
653error:
654 server->nfs_client = NULL;
655 nfs_put_client(clp);
656 dprintk("<-- nfs_init_server() = xerror %d\n", error);
657 return error;
658}
659
660/*
661 * Load up the server record from information gained in an fsinfo record
662 */
663static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
664{
665 unsigned long max_rpc_payload;
666
667 /* Work out a lot of parameters */
668 if (server->rsize == 0)
669 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
670 if (server->wsize == 0)
671 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
672
673 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
674 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
675 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
676 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
677
678 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
679 if (server->rsize > max_rpc_payload)
680 server->rsize = max_rpc_payload;
681 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
682 server->rsize = NFS_MAX_FILE_IO_SIZE;
683 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700684
David Howells54ceac42006-08-22 20:06:13 -0400685 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
686
687 if (server->wsize > max_rpc_payload)
688 server->wsize = max_rpc_payload;
689 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
690 server->wsize = NFS_MAX_FILE_IO_SIZE;
691 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
692 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
693
694 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
695 if (server->dtsize > PAGE_CACHE_SIZE)
696 server->dtsize = PAGE_CACHE_SIZE;
697 if (server->dtsize > server->rsize)
698 server->dtsize = server->rsize;
699
700 if (server->flags & NFS_MOUNT_NOAC) {
701 server->acregmin = server->acregmax = 0;
702 server->acdirmin = server->acdirmax = 0;
703 }
704
705 server->maxfilesize = fsinfo->maxfilesize;
706
707 /* We're airborne Set socket buffersize */
708 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
709}
710
711/*
712 * Probe filesystem information, including the FSID on v2/v3
713 */
714static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
715{
716 struct nfs_fsinfo fsinfo;
717 struct nfs_client *clp = server->nfs_client;
718 int error;
719
720 dprintk("--> nfs_probe_fsinfo()\n");
721
722 if (clp->rpc_ops->set_capabilities != NULL) {
723 error = clp->rpc_ops->set_capabilities(server, mntfh);
724 if (error < 0)
725 goto out_error;
726 }
727
728 fsinfo.fattr = fattr;
729 nfs_fattr_init(fattr);
730 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
731 if (error < 0)
732 goto out_error;
733
734 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700735 error = bdi_init(&server->backing_dev_info);
736 if (error)
737 goto out_error;
738
David Howells54ceac42006-08-22 20:06:13 -0400739
740 /* Get some general file system info */
741 if (server->namelen == 0) {
742 struct nfs_pathconf pathinfo;
743
744 pathinfo.fattr = fattr;
745 nfs_fattr_init(fattr);
746
747 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
748 server->namelen = pathinfo.max_namelen;
749 }
750
751 dprintk("<-- nfs_probe_fsinfo() = 0\n");
752 return 0;
753
754out_error:
755 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
756 return error;
757}
758
759/*
760 * Copy useful information when duplicating a server record
761 */
762static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
763{
764 target->flags = source->flags;
765 target->acregmin = source->acregmin;
766 target->acregmax = source->acregmax;
767 target->acdirmin = source->acdirmin;
768 target->acdirmax = source->acdirmax;
769 target->caps = source->caps;
770}
771
772/*
773 * Allocate and initialise a server record
774 */
775static struct nfs_server *nfs_alloc_server(void)
776{
777 struct nfs_server *server;
778
779 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
780 if (!server)
781 return NULL;
782
783 server->client = server->client_acl = ERR_PTR(-EINVAL);
784
785 /* Zero out the NFS state stuff */
786 INIT_LIST_HEAD(&server->client_link);
787 INIT_LIST_HEAD(&server->master_link);
788
Steve Dicksonef818a22007-11-08 04:05:04 -0500789 init_waitqueue_head(&server->active_wq);
790 atomic_set(&server->active, 0);
791
David Howells54ceac42006-08-22 20:06:13 -0400792 server->io_stats = nfs_alloc_iostats();
793 if (!server->io_stats) {
794 kfree(server);
795 return NULL;
796 }
797
798 return server;
799}
800
801/*
802 * Free up a server record
803 */
804void nfs_free_server(struct nfs_server *server)
805{
806 dprintk("--> nfs_free_server()\n");
807
808 spin_lock(&nfs_client_lock);
809 list_del(&server->client_link);
810 list_del(&server->master_link);
811 spin_unlock(&nfs_client_lock);
812
813 if (server->destroy != NULL)
814 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500815
816 if (!IS_ERR(server->client_acl))
817 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400818 if (!IS_ERR(server->client))
819 rpc_shutdown_client(server->client);
820
821 nfs_put_client(server->nfs_client);
822
823 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700824 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400825 kfree(server);
826 nfs_release_automount_timer();
827 dprintk("<-- nfs_free_server()\n");
828}
829
830/*
831 * Create a version 2 or 3 volume record
832 * - keyed on server and FSID
833 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400834struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400835 struct nfs_fh *mntfh)
836{
837 struct nfs_server *server;
838 struct nfs_fattr fattr;
839 int error;
840
841 server = nfs_alloc_server();
842 if (!server)
843 return ERR_PTR(-ENOMEM);
844
845 /* Get a client representation */
846 error = nfs_init_server(server, data);
847 if (error < 0)
848 goto error;
849
850 BUG_ON(!server->nfs_client);
851 BUG_ON(!server->nfs_client->rpc_ops);
852 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
853
854 /* Probe the root fh to retrieve its FSID */
855 error = nfs_probe_fsinfo(server, mntfh, &fattr);
856 if (error < 0)
857 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400858 if (server->nfs_client->rpc_ops->version == 3) {
859 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
860 server->namelen = NFS3_MAXNAMLEN;
861 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
862 server->caps |= NFS_CAP_READDIRPLUS;
863 } else {
864 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
865 server->namelen = NFS2_MAXNAMLEN;
866 }
867
David Howells54ceac42006-08-22 20:06:13 -0400868 if (!(fattr.valid & NFS_ATTR_FATTR)) {
869 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
870 if (error < 0) {
871 dprintk("nfs_create_server: getattr error = %d\n", -error);
872 goto error;
873 }
874 }
875 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
876
David Howells6daabf12006-08-24 15:44:16 -0400877 dprintk("Server FSID: %llx:%llx\n",
878 (unsigned long long) server->fsid.major,
879 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400880
881 BUG_ON(!server->nfs_client);
882 BUG_ON(!server->nfs_client->rpc_ops);
883 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
884
885 spin_lock(&nfs_client_lock);
886 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
887 list_add_tail(&server->master_link, &nfs_volume_list);
888 spin_unlock(&nfs_client_lock);
889
890 server->mount_time = jiffies;
891 return server;
892
893error:
894 nfs_free_server(server);
895 return ERR_PTR(error);
896}
897
898#ifdef CONFIG_NFS_V4
899/*
900 * Initialise an NFS4 client record
901 */
902static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500903 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700904 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400905 rpc_authflavor_t authflavour)
906{
907 int error;
908
909 if (clp->cl_cons_state == NFS_CS_READY) {
910 /* the client is initialised already */
911 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
912 return 0;
913 }
914
915 /* Check NFS protocol revision and initialize RPC op vector */
916 clp->rpc_ops = &nfs_v4_clientops;
917
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500918 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500919 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400920 if (error < 0)
921 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700922 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400923
924 error = nfs_idmap_new(clp);
925 if (error < 0) {
926 dprintk("%s: failed to create idmapper. Error = %d\n",
927 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400928 goto error;
929 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400930 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400931
932 nfs_mark_client_ready(clp, NFS_CS_READY);
933 return 0;
934
935error:
936 nfs_mark_client_ready(clp, error);
937 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
938 return error;
939}
940
941/*
942 * Set up an NFS4 client
943 */
944static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500945 const char *hostname,
946 const struct sockaddr *addr,
947 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700948 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400949 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -0500950 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -0400951{
Trond Myklebust3a498022007-12-14 14:56:04 -0500952 struct nfs_client_initdata cl_init = {
953 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500954 .addr = addr,
955 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500956 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500957 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -0500958 };
David Howells54ceac42006-08-22 20:06:13 -0400959 struct nfs_client *clp;
960 int error;
961
962 dprintk("--> nfs4_set_client()\n");
963
964 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500965 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400966 if (IS_ERR(clp)) {
967 error = PTR_ERR(clp);
968 goto error;
969 }
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500970 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400971 if (error < 0)
972 goto error_put;
973
974 server->nfs_client = clp;
975 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
976 return 0;
977
978error_put:
979 nfs_put_client(clp);
980error:
981 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
982 return error;
983}
984
985/*
986 * Create a version 4 volume record
987 */
988static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400989 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400990{
Trond Myklebust33170232007-12-20 16:03:59 -0500991 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400992 int error;
993
994 dprintk("--> nfs4_init_server()\n");
995
Trond Myklebust33170232007-12-20 16:03:59 -0500996 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
997 data->timeo, data->retrans);
998
999 /* Get a client record */
1000 error = nfs4_set_client(server,
1001 data->nfs_server.hostname,
1002 (const struct sockaddr *)&data->nfs_server.address,
1003 data->nfs_server.addrlen,
1004 data->client_address,
1005 data->auth_flavors[0],
1006 data->nfs_server.protocol,
1007 &timeparms);
1008 if (error < 0)
1009 goto error;
1010
David Howells54ceac42006-08-22 20:06:13 -04001011 /* Initialise the client representation from the mount data */
1012 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1013 server->caps |= NFS_CAP_ATOMIC_OPEN;
1014
1015 if (data->rsize)
1016 server->rsize = nfs_block_size(data->rsize, NULL);
1017 if (data->wsize)
1018 server->wsize = nfs_block_size(data->wsize, NULL);
1019
1020 server->acregmin = data->acregmin * HZ;
1021 server->acregmax = data->acregmax * HZ;
1022 server->acdirmin = data->acdirmin * HZ;
1023 server->acdirmax = data->acdirmax * HZ;
1024
Trond Myklebust33170232007-12-20 16:03:59 -05001025 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001026
Trond Myklebust33170232007-12-20 16:03:59 -05001027error:
David Howells54ceac42006-08-22 20:06:13 -04001028 /* Done */
1029 dprintk("<-- nfs4_init_server() = %d\n", error);
1030 return error;
1031}
1032
1033/*
1034 * Create a version 4 volume record
1035 * - keyed on server and FSID
1036 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001037struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001038 struct nfs_fh *mntfh)
1039{
1040 struct nfs_fattr fattr;
1041 struct nfs_server *server;
1042 int error;
1043
1044 dprintk("--> nfs4_create_server()\n");
1045
1046 server = nfs_alloc_server();
1047 if (!server)
1048 return ERR_PTR(-ENOMEM);
1049
David Howells54ceac42006-08-22 20:06:13 -04001050 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001051 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001052 if (error < 0)
1053 goto error;
1054
1055 BUG_ON(!server->nfs_client);
1056 BUG_ON(!server->nfs_client->rpc_ops);
1057 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1058
1059 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001060 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001061 if (error < 0)
1062 goto error;
1063
David Howells6daabf12006-08-24 15:44:16 -04001064 dprintk("Server FSID: %llx:%llx\n",
1065 (unsigned long long) server->fsid.major,
1066 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001067 dprintk("Mount FH: %d\n", mntfh->size);
1068
1069 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1070 if (error < 0)
1071 goto error;
1072
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001073 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1074 server->namelen = NFS4_MAXNAMLEN;
1075
David Howells54ceac42006-08-22 20:06:13 -04001076 BUG_ON(!server->nfs_client);
1077 BUG_ON(!server->nfs_client->rpc_ops);
1078 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1079
1080 spin_lock(&nfs_client_lock);
1081 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1082 list_add_tail(&server->master_link, &nfs_volume_list);
1083 spin_unlock(&nfs_client_lock);
1084
1085 server->mount_time = jiffies;
1086 dprintk("<-- nfs4_create_server() = %p\n", server);
1087 return server;
1088
1089error:
1090 nfs_free_server(server);
1091 dprintk("<-- nfs4_create_server() = error %d\n", error);
1092 return ERR_PTR(error);
1093}
1094
1095/*
1096 * Create an NFS4 referral server record
1097 */
1098struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001099 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001100{
1101 struct nfs_client *parent_client;
1102 struct nfs_server *server, *parent_server;
1103 struct nfs_fattr fattr;
1104 int error;
1105
1106 dprintk("--> nfs4_create_referral_server()\n");
1107
1108 server = nfs_alloc_server();
1109 if (!server)
1110 return ERR_PTR(-ENOMEM);
1111
1112 parent_server = NFS_SB(data->sb);
1113 parent_client = parent_server->nfs_client;
1114
1115 /* Get a client representation.
1116 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001117 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001118 data->addr,
1119 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001120 parent_client->cl_ipaddr,
1121 data->authflavor,
1122 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001123 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001124 if (error < 0)
1125 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001126
1127 /* Initialise the client representation from the parent server */
1128 nfs_server_copy_userdata(server, parent_server);
1129 server->caps |= NFS_CAP_ATOMIC_OPEN;
1130
Trond Myklebust33170232007-12-20 16:03:59 -05001131 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001132 if (error < 0)
1133 goto error;
1134
1135 BUG_ON(!server->nfs_client);
1136 BUG_ON(!server->nfs_client->rpc_ops);
1137 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1138
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001139 /* Probe the root fh to retrieve its FSID and filehandle */
1140 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1141 if (error < 0)
1142 goto error;
1143
David Howells54ceac42006-08-22 20:06:13 -04001144 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001145 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001146 if (error < 0)
1147 goto error;
1148
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001149 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1150 server->namelen = NFS4_MAXNAMLEN;
1151
David Howells54ceac42006-08-22 20:06:13 -04001152 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001153 (unsigned long long) server->fsid.major,
1154 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001155
1156 spin_lock(&nfs_client_lock);
1157 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1158 list_add_tail(&server->master_link, &nfs_volume_list);
1159 spin_unlock(&nfs_client_lock);
1160
1161 server->mount_time = jiffies;
1162
1163 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1164 return server;
1165
1166error:
1167 nfs_free_server(server);
1168 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1169 return ERR_PTR(error);
1170}
1171
1172#endif /* CONFIG_NFS_V4 */
1173
1174/*
1175 * Clone an NFS2, NFS3 or NFS4 server record
1176 */
1177struct nfs_server *nfs_clone_server(struct nfs_server *source,
1178 struct nfs_fh *fh,
1179 struct nfs_fattr *fattr)
1180{
1181 struct nfs_server *server;
1182 struct nfs_fattr fattr_fsinfo;
1183 int error;
1184
1185 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001186 (unsigned long long) fattr->fsid.major,
1187 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001188
1189 server = nfs_alloc_server();
1190 if (!server)
1191 return ERR_PTR(-ENOMEM);
1192
1193 /* Copy data from the source */
1194 server->nfs_client = source->nfs_client;
1195 atomic_inc(&server->nfs_client->cl_count);
1196 nfs_server_copy_userdata(server, source);
1197
1198 server->fsid = fattr->fsid;
1199
Trond Myklebust33170232007-12-20 16:03:59 -05001200 error = nfs_init_server_rpcclient(server,
1201 source->client->cl_timeout,
1202 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001203 if (error < 0)
1204 goto out_free_server;
1205 if (!IS_ERR(source->client_acl))
1206 nfs_init_server_aclclient(server);
1207
1208 /* probe the filesystem info for this server filesystem */
1209 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1210 if (error < 0)
1211 goto out_free_server;
1212
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001213 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1214 server->namelen = NFS4_MAXNAMLEN;
1215
David Howells54ceac42006-08-22 20:06:13 -04001216 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001217 (unsigned long long) server->fsid.major,
1218 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001219
1220 error = nfs_start_lockd(server);
1221 if (error < 0)
1222 goto out_free_server;
1223
1224 spin_lock(&nfs_client_lock);
1225 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1226 list_add_tail(&server->master_link, &nfs_volume_list);
1227 spin_unlock(&nfs_client_lock);
1228
1229 server->mount_time = jiffies;
1230
1231 dprintk("<-- nfs_clone_server() = %p\n", server);
1232 return server;
1233
1234out_free_server:
1235 nfs_free_server(server);
1236 dprintk("<-- nfs_clone_server() = error %d\n", error);
1237 return ERR_PTR(error);
1238}
David Howells6aaca562006-08-22 20:06:13 -04001239
1240#ifdef CONFIG_PROC_FS
1241static struct proc_dir_entry *proc_fs_nfs;
1242
1243static int nfs_server_list_open(struct inode *inode, struct file *file);
1244static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1245static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1246static void nfs_server_list_stop(struct seq_file *p, void *v);
1247static int nfs_server_list_show(struct seq_file *m, void *v);
1248
1249static struct seq_operations nfs_server_list_ops = {
1250 .start = nfs_server_list_start,
1251 .next = nfs_server_list_next,
1252 .stop = nfs_server_list_stop,
1253 .show = nfs_server_list_show,
1254};
1255
Arjan van de Ven00977a52007-02-12 00:55:34 -08001256static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001257 .open = nfs_server_list_open,
1258 .read = seq_read,
1259 .llseek = seq_lseek,
1260 .release = seq_release,
1261};
1262
1263static int nfs_volume_list_open(struct inode *inode, struct file *file);
1264static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1265static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1266static void nfs_volume_list_stop(struct seq_file *p, void *v);
1267static int nfs_volume_list_show(struct seq_file *m, void *v);
1268
1269static struct seq_operations nfs_volume_list_ops = {
1270 .start = nfs_volume_list_start,
1271 .next = nfs_volume_list_next,
1272 .stop = nfs_volume_list_stop,
1273 .show = nfs_volume_list_show,
1274};
1275
Arjan van de Ven00977a52007-02-12 00:55:34 -08001276static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001277 .open = nfs_volume_list_open,
1278 .read = seq_read,
1279 .llseek = seq_lseek,
1280 .release = seq_release,
1281};
1282
1283/*
1284 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1285 * we're dealing
1286 */
1287static int nfs_server_list_open(struct inode *inode, struct file *file)
1288{
1289 struct seq_file *m;
1290 int ret;
1291
1292 ret = seq_open(file, &nfs_server_list_ops);
1293 if (ret < 0)
1294 return ret;
1295
1296 m = file->private_data;
1297 m->private = PDE(inode)->data;
1298
1299 return 0;
1300}
1301
1302/*
1303 * set up the iterator to start reading from the server list and return the first item
1304 */
1305static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1306{
David Howells6aaca562006-08-22 20:06:13 -04001307 /* lock the list against modification */
1308 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001309 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001310}
1311
1312/*
1313 * move to next server
1314 */
1315static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1316{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001317 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001318}
1319
1320/*
1321 * clean up after reading from the transports list
1322 */
1323static void nfs_server_list_stop(struct seq_file *p, void *v)
1324{
1325 spin_unlock(&nfs_client_lock);
1326}
1327
1328/*
1329 * display a header line followed by a load of call lines
1330 */
1331static int nfs_server_list_show(struct seq_file *m, void *v)
1332{
1333 struct nfs_client *clp;
1334
1335 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001336 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001337 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1338 return 0;
1339 }
1340
1341 /* display one transport per line on subsequent lines */
1342 clp = list_entry(v, struct nfs_client, cl_share_link);
1343
Chuck Lever5d8515c2007-12-10 14:57:16 -05001344 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001345 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001346 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1347 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001348 atomic_read(&clp->cl_count),
1349 clp->cl_hostname);
1350
1351 return 0;
1352}
1353
1354/*
1355 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1356 */
1357static int nfs_volume_list_open(struct inode *inode, struct file *file)
1358{
1359 struct seq_file *m;
1360 int ret;
1361
1362 ret = seq_open(file, &nfs_volume_list_ops);
1363 if (ret < 0)
1364 return ret;
1365
1366 m = file->private_data;
1367 m->private = PDE(inode)->data;
1368
1369 return 0;
1370}
1371
1372/*
1373 * set up the iterator to start reading from the volume list and return the first item
1374 */
1375static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1376{
David Howells6aaca562006-08-22 20:06:13 -04001377 /* lock the list against modification */
1378 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001379 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001380}
1381
1382/*
1383 * move to next volume
1384 */
1385static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1386{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001387 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001388}
1389
1390/*
1391 * clean up after reading from the transports list
1392 */
1393static void nfs_volume_list_stop(struct seq_file *p, void *v)
1394{
1395 spin_unlock(&nfs_client_lock);
1396}
1397
1398/*
1399 * display a header line followed by a load of call lines
1400 */
1401static int nfs_volume_list_show(struct seq_file *m, void *v)
1402{
1403 struct nfs_server *server;
1404 struct nfs_client *clp;
1405 char dev[8], fsid[17];
1406
1407 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001408 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001409 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1410 return 0;
1411 }
1412 /* display one transport per line on subsequent lines */
1413 server = list_entry(v, struct nfs_server, master_link);
1414 clp = server->nfs_client;
1415
1416 snprintf(dev, 8, "%u:%u",
1417 MAJOR(server->s_dev), MINOR(server->s_dev));
1418
1419 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001420 (unsigned long long) server->fsid.major,
1421 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001422
Chuck Lever5d8515c2007-12-10 14:57:16 -05001423 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001424 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001425 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1426 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001427 dev,
1428 fsid);
1429
1430 return 0;
1431}
1432
1433/*
1434 * initialise the /proc/fs/nfsfs/ directory
1435 */
1436int __init nfs_fs_proc_init(void)
1437{
1438 struct proc_dir_entry *p;
1439
1440 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1441 if (!proc_fs_nfs)
1442 goto error_0;
1443
1444 proc_fs_nfs->owner = THIS_MODULE;
1445
1446 /* a file of servers with which we're dealing */
1447 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1448 if (!p)
1449 goto error_1;
1450
1451 p->proc_fops = &nfs_server_list_fops;
1452 p->owner = THIS_MODULE;
1453
1454 /* a file of volumes that we have mounted */
1455 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1456 if (!p)
1457 goto error_2;
1458
1459 p->proc_fops = &nfs_volume_list_fops;
1460 p->owner = THIS_MODULE;
1461 return 0;
1462
1463error_2:
1464 remove_proc_entry("servers", proc_fs_nfs);
1465error_1:
1466 remove_proc_entry("nfsfs", proc_root_fs);
1467error_0:
1468 return -ENOMEM;
1469}
1470
1471/*
1472 * clean up the /proc/fs/nfsfs/ directory
1473 */
1474void nfs_fs_proc_exit(void)
1475{
1476 remove_proc_entry("volumes", proc_fs_nfs);
1477 remove_proc_entry("servers", proc_fs_nfs);
1478 remove_proc_entry("nfsfs", proc_root_fs);
1479}
1480
1481#endif /* CONFIG_PROC_FS */