blob: 0b3ce86f6fc9526c015195184bc100737699bf99 [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\0896a7252007-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\0896a7252007-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\0896a7252007-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))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500461 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400462}
463
464/*
465 * Version 2 or 3 lockd setup
466 */
467static int nfs_start_lockd(struct nfs_server *server)
468{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500469 struct nlm_host *host;
470 struct nfs_client *clp = server->nfs_client;
471 u32 nfs_version = clp->rpc_ops->version;
472 unsigned short protocol = server->flags & NFS_MOUNT_TCP ?
473 IPPROTO_TCP : IPPROTO_UDP;
David Howells54ceac42006-08-22 20:06:13 -0400474
Chuck Lever9289e7f2008-01-11 17:09:52 -0500475 if (nfs_version > 3)
476 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400477 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500478 return 0;
479
480 host = nlmclnt_init(clp->cl_hostname,
481 (struct sockaddr *)&clp->cl_addr,
482 clp->cl_addrlen, protocol, nfs_version);
483 if (IS_ERR(host))
484 return PTR_ERR(host);
485
486 server->nlm_host = host;
487 server->destroy = nfs_destroy_server;
488 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400489}
490
491/*
492 * Initialise an NFSv3 ACL client connection
493 */
494#ifdef CONFIG_NFS_V3_ACL
495static void nfs_init_server_aclclient(struct nfs_server *server)
496{
Trond Myklebust40c553192007-12-14 14:56:07 -0500497 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400498 goto out_noacl;
499 if (server->flags & NFS_MOUNT_NOACL)
500 goto out_noacl;
501
502 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
503 if (IS_ERR(server->client_acl))
504 goto out_noacl;
505
506 /* No errors! Assume that Sun nfsacls are supported */
507 server->caps |= NFS_CAP_ACLS;
508 return;
509
510out_noacl:
511 server->caps &= ~NFS_CAP_ACLS;
512}
513#else
514static inline void nfs_init_server_aclclient(struct nfs_server *server)
515{
516 server->flags &= ~NFS_MOUNT_NOACL;
517 server->caps &= ~NFS_CAP_ACLS;
518}
519#endif
520
521/*
522 * Create a general RPC client
523 */
Trond Myklebust33170232007-12-20 16:03:59 -0500524static int nfs_init_server_rpcclient(struct nfs_server *server,
525 const struct rpc_timeout *timeo,
526 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400527{
528 struct nfs_client *clp = server->nfs_client;
529
530 server->client = rpc_clone_client(clp->cl_rpcclient);
531 if (IS_ERR(server->client)) {
532 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
533 return PTR_ERR(server->client);
534 }
535
Trond Myklebust33170232007-12-20 16:03:59 -0500536 memcpy(&server->client->cl_timeout_default,
537 timeo,
538 sizeof(server->client->cl_timeout_default));
539 server->client->cl_timeout = &server->client->cl_timeout_default;
540
David Howells54ceac42006-08-22 20:06:13 -0400541 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
542 struct rpc_auth *auth;
543
544 auth = rpcauth_create(pseudoflavour, server->client);
545 if (IS_ERR(auth)) {
546 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
547 return PTR_ERR(auth);
548 }
549 }
550 server->client->cl_softrtry = 0;
551 if (server->flags & NFS_MOUNT_SOFT)
552 server->client->cl_softrtry = 1;
553
554 server->client->cl_intr = 0;
555 if (server->flags & NFS4_MOUNT_INTR)
556 server->client->cl_intr = 1;
557
558 return 0;
559}
560
561/*
562 * Initialise an NFS2 or NFS3 client
563 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400564static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500565 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400566 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400567{
David Howells54ceac42006-08-22 20:06:13 -0400568 int error;
569
570 if (clp->cl_cons_state == NFS_CS_READY) {
571 /* the client is already initialised */
572 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
573 return 0;
574 }
575
David Howells54ceac42006-08-22 20:06:13 -0400576 /*
577 * Create a client RPC handle for doing FSSTAT with UNIX auth only
578 * - RFC 2623, sec 2.3.2
579 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500580 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400581 if (error < 0)
582 goto error;
583 nfs_mark_client_ready(clp, NFS_CS_READY);
584 return 0;
585
586error:
587 nfs_mark_client_ready(clp, error);
588 dprintk("<-- nfs_init_client() = xerror %d\n", error);
589 return error;
590}
591
592/*
593 * Create a version 2 or 3 client
594 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400595static int nfs_init_server(struct nfs_server *server,
596 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400597{
Trond Myklebust3a498022007-12-14 14:56:04 -0500598 struct nfs_client_initdata cl_init = {
599 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500600 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500601 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500602 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500603 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500604 };
Trond Myklebust33170232007-12-20 16:03:59 -0500605 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400606 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500607 int error;
David Howells54ceac42006-08-22 20:06:13 -0400608
609 dprintk("--> nfs_init_server()\n");
610
611#ifdef CONFIG_NFS_V3
612 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500613 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400614#endif
615
616 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500617 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400618 if (IS_ERR(clp)) {
619 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
620 return PTR_ERR(clp);
621 }
622
Trond Myklebust33170232007-12-20 16:03:59 -0500623 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
624 data->timeo, data->retrans);
625 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400626 if (error < 0)
627 goto error;
628
629 server->nfs_client = clp;
630
631 /* Initialise the client representation from the mount data */
632 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
633
634 if (data->rsize)
635 server->rsize = nfs_block_size(data->rsize, NULL);
636 if (data->wsize)
637 server->wsize = nfs_block_size(data->wsize, NULL);
638
639 server->acregmin = data->acregmin * HZ;
640 server->acregmax = data->acregmax * HZ;
641 server->acdirmin = data->acdirmin * HZ;
642 server->acdirmax = data->acdirmax * HZ;
643
644 /* Start lockd here, before we might error out */
645 error = nfs_start_lockd(server);
646 if (error < 0)
647 goto error;
648
Trond Myklebust33170232007-12-20 16:03:59 -0500649 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400650 if (error < 0)
651 goto error;
652
653 server->namelen = data->namlen;
654 /* Create a client RPC handle for the NFSv3 ACL management interface */
655 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400656 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
657 return 0;
658
659error:
660 server->nfs_client = NULL;
661 nfs_put_client(clp);
662 dprintk("<-- nfs_init_server() = xerror %d\n", error);
663 return error;
664}
665
666/*
667 * Load up the server record from information gained in an fsinfo record
668 */
669static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
670{
671 unsigned long max_rpc_payload;
672
673 /* Work out a lot of parameters */
674 if (server->rsize == 0)
675 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
676 if (server->wsize == 0)
677 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
678
679 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
680 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
681 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
682 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
683
684 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
685 if (server->rsize > max_rpc_payload)
686 server->rsize = max_rpc_payload;
687 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
688 server->rsize = NFS_MAX_FILE_IO_SIZE;
689 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700690
David Howells54ceac42006-08-22 20:06:13 -0400691 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
692
693 if (server->wsize > max_rpc_payload)
694 server->wsize = max_rpc_payload;
695 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
696 server->wsize = NFS_MAX_FILE_IO_SIZE;
697 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
698 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
699
700 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
701 if (server->dtsize > PAGE_CACHE_SIZE)
702 server->dtsize = PAGE_CACHE_SIZE;
703 if (server->dtsize > server->rsize)
704 server->dtsize = server->rsize;
705
706 if (server->flags & NFS_MOUNT_NOAC) {
707 server->acregmin = server->acregmax = 0;
708 server->acdirmin = server->acdirmax = 0;
709 }
710
711 server->maxfilesize = fsinfo->maxfilesize;
712
713 /* We're airborne Set socket buffersize */
714 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
715}
716
717/*
718 * Probe filesystem information, including the FSID on v2/v3
719 */
720static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
721{
722 struct nfs_fsinfo fsinfo;
723 struct nfs_client *clp = server->nfs_client;
724 int error;
725
726 dprintk("--> nfs_probe_fsinfo()\n");
727
728 if (clp->rpc_ops->set_capabilities != NULL) {
729 error = clp->rpc_ops->set_capabilities(server, mntfh);
730 if (error < 0)
731 goto out_error;
732 }
733
734 fsinfo.fattr = fattr;
735 nfs_fattr_init(fattr);
736 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
737 if (error < 0)
738 goto out_error;
739
740 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700741 error = bdi_init(&server->backing_dev_info);
742 if (error)
743 goto out_error;
744
David Howells54ceac42006-08-22 20:06:13 -0400745
746 /* Get some general file system info */
747 if (server->namelen == 0) {
748 struct nfs_pathconf pathinfo;
749
750 pathinfo.fattr = fattr;
751 nfs_fattr_init(fattr);
752
753 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
754 server->namelen = pathinfo.max_namelen;
755 }
756
757 dprintk("<-- nfs_probe_fsinfo() = 0\n");
758 return 0;
759
760out_error:
761 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
762 return error;
763}
764
765/*
766 * Copy useful information when duplicating a server record
767 */
768static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
769{
770 target->flags = source->flags;
771 target->acregmin = source->acregmin;
772 target->acregmax = source->acregmax;
773 target->acdirmin = source->acdirmin;
774 target->acdirmax = source->acdirmax;
775 target->caps = source->caps;
776}
777
778/*
779 * Allocate and initialise a server record
780 */
781static struct nfs_server *nfs_alloc_server(void)
782{
783 struct nfs_server *server;
784
785 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
786 if (!server)
787 return NULL;
788
789 server->client = server->client_acl = ERR_PTR(-EINVAL);
790
791 /* Zero out the NFS state stuff */
792 INIT_LIST_HEAD(&server->client_link);
793 INIT_LIST_HEAD(&server->master_link);
794
Steve Dicksonef818a22007-11-08 04:05:04 -0500795 init_waitqueue_head(&server->active_wq);
796 atomic_set(&server->active, 0);
797
David Howells54ceac42006-08-22 20:06:13 -0400798 server->io_stats = nfs_alloc_iostats();
799 if (!server->io_stats) {
800 kfree(server);
801 return NULL;
802 }
803
804 return server;
805}
806
807/*
808 * Free up a server record
809 */
810void nfs_free_server(struct nfs_server *server)
811{
812 dprintk("--> nfs_free_server()\n");
813
814 spin_lock(&nfs_client_lock);
815 list_del(&server->client_link);
816 list_del(&server->master_link);
817 spin_unlock(&nfs_client_lock);
818
819 if (server->destroy != NULL)
820 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500821
822 if (!IS_ERR(server->client_acl))
823 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400824 if (!IS_ERR(server->client))
825 rpc_shutdown_client(server->client);
826
827 nfs_put_client(server->nfs_client);
828
829 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700830 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400831 kfree(server);
832 nfs_release_automount_timer();
833 dprintk("<-- nfs_free_server()\n");
834}
835
836/*
837 * Create a version 2 or 3 volume record
838 * - keyed on server and FSID
839 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400840struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400841 struct nfs_fh *mntfh)
842{
843 struct nfs_server *server;
844 struct nfs_fattr fattr;
845 int error;
846
847 server = nfs_alloc_server();
848 if (!server)
849 return ERR_PTR(-ENOMEM);
850
851 /* Get a client representation */
852 error = nfs_init_server(server, data);
853 if (error < 0)
854 goto error;
855
856 BUG_ON(!server->nfs_client);
857 BUG_ON(!server->nfs_client->rpc_ops);
858 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
859
860 /* Probe the root fh to retrieve its FSID */
861 error = nfs_probe_fsinfo(server, mntfh, &fattr);
862 if (error < 0)
863 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400864 if (server->nfs_client->rpc_ops->version == 3) {
865 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
866 server->namelen = NFS3_MAXNAMLEN;
867 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
868 server->caps |= NFS_CAP_READDIRPLUS;
869 } else {
870 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
871 server->namelen = NFS2_MAXNAMLEN;
872 }
873
David Howells54ceac42006-08-22 20:06:13 -0400874 if (!(fattr.valid & NFS_ATTR_FATTR)) {
875 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
876 if (error < 0) {
877 dprintk("nfs_create_server: getattr error = %d\n", -error);
878 goto error;
879 }
880 }
881 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
882
David Howells6daabf12006-08-24 15:44:16 -0400883 dprintk("Server FSID: %llx:%llx\n",
884 (unsigned long long) server->fsid.major,
885 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400886
887 BUG_ON(!server->nfs_client);
888 BUG_ON(!server->nfs_client->rpc_ops);
889 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
890
891 spin_lock(&nfs_client_lock);
892 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
893 list_add_tail(&server->master_link, &nfs_volume_list);
894 spin_unlock(&nfs_client_lock);
895
896 server->mount_time = jiffies;
897 return server;
898
899error:
900 nfs_free_server(server);
901 return ERR_PTR(error);
902}
903
904#ifdef CONFIG_NFS_V4
905/*
906 * Initialise an NFS4 client record
907 */
908static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500909 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700910 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400911 rpc_authflavor_t authflavour)
912{
913 int error;
914
915 if (clp->cl_cons_state == NFS_CS_READY) {
916 /* the client is initialised already */
917 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
918 return 0;
919 }
920
921 /* Check NFS protocol revision and initialize RPC op vector */
922 clp->rpc_ops = &nfs_v4_clientops;
923
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500924 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500925 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400926 if (error < 0)
927 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700928 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400929
930 error = nfs_idmap_new(clp);
931 if (error < 0) {
932 dprintk("%s: failed to create idmapper. Error = %d\n",
933 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400934 goto error;
935 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400936 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400937
938 nfs_mark_client_ready(clp, NFS_CS_READY);
939 return 0;
940
941error:
942 nfs_mark_client_ready(clp, error);
943 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
944 return error;
945}
946
947/*
948 * Set up an NFS4 client
949 */
950static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500951 const char *hostname,
952 const struct sockaddr *addr,
953 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700954 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400955 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -0500956 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -0400957{
Trond Myklebust3a498022007-12-14 14:56:04 -0500958 struct nfs_client_initdata cl_init = {
959 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500960 .addr = addr,
961 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500962 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500963 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -0500964 };
David Howells54ceac42006-08-22 20:06:13 -0400965 struct nfs_client *clp;
966 int error;
967
968 dprintk("--> nfs4_set_client()\n");
969
970 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500971 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400972 if (IS_ERR(clp)) {
973 error = PTR_ERR(clp);
974 goto error;
975 }
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500976 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400977 if (error < 0)
978 goto error_put;
979
980 server->nfs_client = clp;
981 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
982 return 0;
983
984error_put:
985 nfs_put_client(clp);
986error:
987 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
988 return error;
989}
990
991/*
992 * Create a version 4 volume record
993 */
994static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400995 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400996{
Trond Myklebust33170232007-12-20 16:03:59 -0500997 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400998 int error;
999
1000 dprintk("--> nfs4_init_server()\n");
1001
Trond Myklebust33170232007-12-20 16:03:59 -05001002 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1003 data->timeo, data->retrans);
1004
1005 /* Get a client record */
1006 error = nfs4_set_client(server,
1007 data->nfs_server.hostname,
1008 (const struct sockaddr *)&data->nfs_server.address,
1009 data->nfs_server.addrlen,
1010 data->client_address,
1011 data->auth_flavors[0],
1012 data->nfs_server.protocol,
1013 &timeparms);
1014 if (error < 0)
1015 goto error;
1016
David Howells54ceac42006-08-22 20:06:13 -04001017 /* Initialise the client representation from the mount data */
1018 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1019 server->caps |= NFS_CAP_ATOMIC_OPEN;
1020
1021 if (data->rsize)
1022 server->rsize = nfs_block_size(data->rsize, NULL);
1023 if (data->wsize)
1024 server->wsize = nfs_block_size(data->wsize, NULL);
1025
1026 server->acregmin = data->acregmin * HZ;
1027 server->acregmax = data->acregmax * HZ;
1028 server->acdirmin = data->acdirmin * HZ;
1029 server->acdirmax = data->acdirmax * HZ;
1030
Trond Myklebust33170232007-12-20 16:03:59 -05001031 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001032
Trond Myklebust33170232007-12-20 16:03:59 -05001033error:
David Howells54ceac42006-08-22 20:06:13 -04001034 /* Done */
1035 dprintk("<-- nfs4_init_server() = %d\n", error);
1036 return error;
1037}
1038
1039/*
1040 * Create a version 4 volume record
1041 * - keyed on server and FSID
1042 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001043struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001044 struct nfs_fh *mntfh)
1045{
1046 struct nfs_fattr fattr;
1047 struct nfs_server *server;
1048 int error;
1049
1050 dprintk("--> nfs4_create_server()\n");
1051
1052 server = nfs_alloc_server();
1053 if (!server)
1054 return ERR_PTR(-ENOMEM);
1055
David Howells54ceac42006-08-22 20:06:13 -04001056 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001057 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001058 if (error < 0)
1059 goto error;
1060
1061 BUG_ON(!server->nfs_client);
1062 BUG_ON(!server->nfs_client->rpc_ops);
1063 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1064
1065 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001066 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001067 if (error < 0)
1068 goto error;
1069
David Howells6daabf12006-08-24 15:44:16 -04001070 dprintk("Server FSID: %llx:%llx\n",
1071 (unsigned long long) server->fsid.major,
1072 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001073 dprintk("Mount FH: %d\n", mntfh->size);
1074
1075 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1076 if (error < 0)
1077 goto error;
1078
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001079 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1080 server->namelen = NFS4_MAXNAMLEN;
1081
David Howells54ceac42006-08-22 20:06:13 -04001082 BUG_ON(!server->nfs_client);
1083 BUG_ON(!server->nfs_client->rpc_ops);
1084 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1085
1086 spin_lock(&nfs_client_lock);
1087 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1088 list_add_tail(&server->master_link, &nfs_volume_list);
1089 spin_unlock(&nfs_client_lock);
1090
1091 server->mount_time = jiffies;
1092 dprintk("<-- nfs4_create_server() = %p\n", server);
1093 return server;
1094
1095error:
1096 nfs_free_server(server);
1097 dprintk("<-- nfs4_create_server() = error %d\n", error);
1098 return ERR_PTR(error);
1099}
1100
1101/*
1102 * Create an NFS4 referral server record
1103 */
1104struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001105 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001106{
1107 struct nfs_client *parent_client;
1108 struct nfs_server *server, *parent_server;
1109 struct nfs_fattr fattr;
1110 int error;
1111
1112 dprintk("--> nfs4_create_referral_server()\n");
1113
1114 server = nfs_alloc_server();
1115 if (!server)
1116 return ERR_PTR(-ENOMEM);
1117
1118 parent_server = NFS_SB(data->sb);
1119 parent_client = parent_server->nfs_client;
1120
1121 /* Get a client representation.
1122 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001123 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001124 data->addr,
1125 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001126 parent_client->cl_ipaddr,
1127 data->authflavor,
1128 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001129 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001130 if (error < 0)
1131 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001132
1133 /* Initialise the client representation from the parent server */
1134 nfs_server_copy_userdata(server, parent_server);
1135 server->caps |= NFS_CAP_ATOMIC_OPEN;
1136
Trond Myklebust33170232007-12-20 16:03:59 -05001137 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001138 if (error < 0)
1139 goto error;
1140
1141 BUG_ON(!server->nfs_client);
1142 BUG_ON(!server->nfs_client->rpc_ops);
1143 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1144
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001145 /* Probe the root fh to retrieve its FSID and filehandle */
1146 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1147 if (error < 0)
1148 goto error;
1149
David Howells54ceac42006-08-22 20:06:13 -04001150 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001151 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001152 if (error < 0)
1153 goto error;
1154
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001155 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1156 server->namelen = NFS4_MAXNAMLEN;
1157
David Howells54ceac42006-08-22 20:06:13 -04001158 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001159 (unsigned long long) server->fsid.major,
1160 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001161
1162 spin_lock(&nfs_client_lock);
1163 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1164 list_add_tail(&server->master_link, &nfs_volume_list);
1165 spin_unlock(&nfs_client_lock);
1166
1167 server->mount_time = jiffies;
1168
1169 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1170 return server;
1171
1172error:
1173 nfs_free_server(server);
1174 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1175 return ERR_PTR(error);
1176}
1177
1178#endif /* CONFIG_NFS_V4 */
1179
1180/*
1181 * Clone an NFS2, NFS3 or NFS4 server record
1182 */
1183struct nfs_server *nfs_clone_server(struct nfs_server *source,
1184 struct nfs_fh *fh,
1185 struct nfs_fattr *fattr)
1186{
1187 struct nfs_server *server;
1188 struct nfs_fattr fattr_fsinfo;
1189 int error;
1190
1191 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001192 (unsigned long long) fattr->fsid.major,
1193 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001194
1195 server = nfs_alloc_server();
1196 if (!server)
1197 return ERR_PTR(-ENOMEM);
1198
1199 /* Copy data from the source */
1200 server->nfs_client = source->nfs_client;
1201 atomic_inc(&server->nfs_client->cl_count);
1202 nfs_server_copy_userdata(server, source);
1203
1204 server->fsid = fattr->fsid;
1205
Trond Myklebust33170232007-12-20 16:03:59 -05001206 error = nfs_init_server_rpcclient(server,
1207 source->client->cl_timeout,
1208 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001209 if (error < 0)
1210 goto out_free_server;
1211 if (!IS_ERR(source->client_acl))
1212 nfs_init_server_aclclient(server);
1213
1214 /* probe the filesystem info for this server filesystem */
1215 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1216 if (error < 0)
1217 goto out_free_server;
1218
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001219 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1220 server->namelen = NFS4_MAXNAMLEN;
1221
David Howells54ceac42006-08-22 20:06:13 -04001222 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001223 (unsigned long long) server->fsid.major,
1224 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001225
1226 error = nfs_start_lockd(server);
1227 if (error < 0)
1228 goto out_free_server;
1229
1230 spin_lock(&nfs_client_lock);
1231 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1232 list_add_tail(&server->master_link, &nfs_volume_list);
1233 spin_unlock(&nfs_client_lock);
1234
1235 server->mount_time = jiffies;
1236
1237 dprintk("<-- nfs_clone_server() = %p\n", server);
1238 return server;
1239
1240out_free_server:
1241 nfs_free_server(server);
1242 dprintk("<-- nfs_clone_server() = error %d\n", error);
1243 return ERR_PTR(error);
1244}
David Howells6aaca562006-08-22 20:06:13 -04001245
1246#ifdef CONFIG_PROC_FS
1247static struct proc_dir_entry *proc_fs_nfs;
1248
1249static int nfs_server_list_open(struct inode *inode, struct file *file);
1250static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1251static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1252static void nfs_server_list_stop(struct seq_file *p, void *v);
1253static int nfs_server_list_show(struct seq_file *m, void *v);
1254
1255static struct seq_operations nfs_server_list_ops = {
1256 .start = nfs_server_list_start,
1257 .next = nfs_server_list_next,
1258 .stop = nfs_server_list_stop,
1259 .show = nfs_server_list_show,
1260};
1261
Arjan van de Ven00977a52007-02-12 00:55:34 -08001262static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001263 .open = nfs_server_list_open,
1264 .read = seq_read,
1265 .llseek = seq_lseek,
1266 .release = seq_release,
1267};
1268
1269static int nfs_volume_list_open(struct inode *inode, struct file *file);
1270static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1271static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1272static void nfs_volume_list_stop(struct seq_file *p, void *v);
1273static int nfs_volume_list_show(struct seq_file *m, void *v);
1274
1275static struct seq_operations nfs_volume_list_ops = {
1276 .start = nfs_volume_list_start,
1277 .next = nfs_volume_list_next,
1278 .stop = nfs_volume_list_stop,
1279 .show = nfs_volume_list_show,
1280};
1281
Arjan van de Ven00977a52007-02-12 00:55:34 -08001282static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001283 .open = nfs_volume_list_open,
1284 .read = seq_read,
1285 .llseek = seq_lseek,
1286 .release = seq_release,
1287};
1288
1289/*
1290 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1291 * we're dealing
1292 */
1293static int nfs_server_list_open(struct inode *inode, struct file *file)
1294{
1295 struct seq_file *m;
1296 int ret;
1297
1298 ret = seq_open(file, &nfs_server_list_ops);
1299 if (ret < 0)
1300 return ret;
1301
1302 m = file->private_data;
1303 m->private = PDE(inode)->data;
1304
1305 return 0;
1306}
1307
1308/*
1309 * set up the iterator to start reading from the server list and return the first item
1310 */
1311static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1312{
David Howells6aaca562006-08-22 20:06:13 -04001313 /* lock the list against modification */
1314 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001315 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001316}
1317
1318/*
1319 * move to next server
1320 */
1321static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1322{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001323 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001324}
1325
1326/*
1327 * clean up after reading from the transports list
1328 */
1329static void nfs_server_list_stop(struct seq_file *p, void *v)
1330{
1331 spin_unlock(&nfs_client_lock);
1332}
1333
1334/*
1335 * display a header line followed by a load of call lines
1336 */
1337static int nfs_server_list_show(struct seq_file *m, void *v)
1338{
1339 struct nfs_client *clp;
1340
1341 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001342 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001343 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1344 return 0;
1345 }
1346
1347 /* display one transport per line on subsequent lines */
1348 clp = list_entry(v, struct nfs_client, cl_share_link);
1349
Chuck Lever5d8515c2007-12-10 14:57:16 -05001350 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001351 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001352 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1353 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001354 atomic_read(&clp->cl_count),
1355 clp->cl_hostname);
1356
1357 return 0;
1358}
1359
1360/*
1361 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1362 */
1363static int nfs_volume_list_open(struct inode *inode, struct file *file)
1364{
1365 struct seq_file *m;
1366 int ret;
1367
1368 ret = seq_open(file, &nfs_volume_list_ops);
1369 if (ret < 0)
1370 return ret;
1371
1372 m = file->private_data;
1373 m->private = PDE(inode)->data;
1374
1375 return 0;
1376}
1377
1378/*
1379 * set up the iterator to start reading from the volume list and return the first item
1380 */
1381static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1382{
David Howells6aaca562006-08-22 20:06:13 -04001383 /* lock the list against modification */
1384 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001385 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001386}
1387
1388/*
1389 * move to next volume
1390 */
1391static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1392{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001393 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001394}
1395
1396/*
1397 * clean up after reading from the transports list
1398 */
1399static void nfs_volume_list_stop(struct seq_file *p, void *v)
1400{
1401 spin_unlock(&nfs_client_lock);
1402}
1403
1404/*
1405 * display a header line followed by a load of call lines
1406 */
1407static int nfs_volume_list_show(struct seq_file *m, void *v)
1408{
1409 struct nfs_server *server;
1410 struct nfs_client *clp;
1411 char dev[8], fsid[17];
1412
1413 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001414 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001415 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1416 return 0;
1417 }
1418 /* display one transport per line on subsequent lines */
1419 server = list_entry(v, struct nfs_server, master_link);
1420 clp = server->nfs_client;
1421
1422 snprintf(dev, 8, "%u:%u",
1423 MAJOR(server->s_dev), MINOR(server->s_dev));
1424
1425 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001426 (unsigned long long) server->fsid.major,
1427 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001428
Chuck Lever5d8515c2007-12-10 14:57:16 -05001429 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001430 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001431 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1432 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001433 dev,
1434 fsid);
1435
1436 return 0;
1437}
1438
1439/*
1440 * initialise the /proc/fs/nfsfs/ directory
1441 */
1442int __init nfs_fs_proc_init(void)
1443{
1444 struct proc_dir_entry *p;
1445
1446 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1447 if (!proc_fs_nfs)
1448 goto error_0;
1449
1450 proc_fs_nfs->owner = THIS_MODULE;
1451
1452 /* a file of servers with which we're dealing */
1453 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1454 if (!p)
1455 goto error_1;
1456
1457 p->proc_fops = &nfs_server_list_fops;
1458 p->owner = THIS_MODULE;
1459
1460 /* a file of volumes that we have mounted */
1461 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1462 if (!p)
1463 goto error_2;
1464
1465 p->proc_fops = &nfs_volume_list_fops;
1466 p->owner = THIS_MODULE;
1467 return 0;
1468
1469error_2:
1470 remove_proc_entry("servers", proc_fs_nfs);
1471error_1:
1472 remove_proc_entry("nfsfs", proc_root_fs);
1473error_0:
1474 return -ENOMEM;
1475}
1476
1477/*
1478 * clean up the /proc/fs/nfsfs/ directory
1479 */
1480void nfs_fs_proc_exit(void)
1481{
1482 remove_proc_entry("volumes", proc_fs_nfs);
1483 remove_proc_entry("servers", proc_fs_nfs);
1484 remove_proc_entry("nfsfs", proc_root_fs);
1485}
1486
1487#endif /* CONFIG_PROC_FS */