blob: c5c0175898f68311f7a19c8c87e641de27acb30a [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/*
Trond Myklebust3fbd67a2008-01-26 01:06:40 -0500279 * Find a client by IP address and protocol version
280 * - returns NULL if no such client
281 */
282struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
283{
284 struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
285 u32 nfsvers = clp->rpc_ops->version;
286
287 spin_lock(&nfs_client_lock);
288 list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
289 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
290
291 /* Don't match clients that failed to initialise properly */
292 if (clp->cl_cons_state != NFS_CS_READY)
293 continue;
294
295 /* Different NFS versions cannot share the same nfs_client */
296 if (clp->rpc_ops->version != nfsvers)
297 continue;
298
299 if (sap->sa_family != clap->sa_family)
300 continue;
301 /* Match only the IP address, not the port number */
302 if (!nfs_sockaddr_match_ipaddr(sap, clap))
303 continue;
304
305 atomic_inc(&clp->cl_count);
306 spin_unlock(&nfs_client_lock);
307 return clp;
308 }
309 spin_unlock(&nfs_client_lock);
310 return NULL;
311}
312
313/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500314 * Find an nfs_client on the list that matches the initialisation data
315 * that is supplied.
316 */
317static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400318{
319 struct nfs_client *clp;
320
321 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700322 /* Don't match clients that failed to initialise properly */
323 if (clp->cl_cons_state < 0)
324 continue;
325
David Howells24c8dbb2006-08-22 20:06:10 -0400326 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500327 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400328 continue;
329
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500330 if (clp->cl_proto != data->proto)
331 continue;
332
Trond Myklebustc81468a2007-12-14 14:56:05 -0500333 /* Match the full socket address */
334 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400335 continue;
336
Trond Myklebustc81468a2007-12-14 14:56:05 -0500337 atomic_inc(&clp->cl_count);
338 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400339 }
David Howells24c8dbb2006-08-22 20:06:10 -0400340 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400341}
342
343/*
344 * Look up a client by IP address and protocol version
345 * - creates a new record if one doesn't yet exist
346 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500347static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400348{
349 struct nfs_client *clp, *new = NULL;
350 int error;
351
Chuck Leverd7422c42007-12-10 14:58:51 -0500352 dprintk("--> nfs_get_client(%s,v%u)\n",
353 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400354
355 /* see if the client already exists */
356 do {
357 spin_lock(&nfs_client_lock);
358
Trond Myklebustc81468a2007-12-14 14:56:05 -0500359 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400360 if (clp)
361 goto found_client;
362 if (new)
363 goto install_client;
364
365 spin_unlock(&nfs_client_lock);
366
Trond Myklebust3a498022007-12-14 14:56:04 -0500367 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400368 } while (new);
369
370 return ERR_PTR(-ENOMEM);
371
372 /* install a new client and return with it unready */
373install_client:
374 clp = new;
375 list_add(&clp->cl_share_link, &nfs_client_list);
376 spin_unlock(&nfs_client_lock);
377 dprintk("--> nfs_get_client() = %p [new]\n", clp);
378 return clp;
379
380 /* found an existing client
381 * - make sure it's ready before returning
382 */
383found_client:
384 spin_unlock(&nfs_client_lock);
385
386 if (new)
387 nfs_free_client(new);
388
Matthew Wilcox150030b2007-12-06 16:24:39 -0500389 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400390 clp->cl_cons_state != NFS_CS_INITING);
391 if (error < 0) {
392 nfs_put_client(clp);
393 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400394 }
395
396 if (clp->cl_cons_state < NFS_CS_READY) {
397 error = clp->cl_cons_state;
398 nfs_put_client(clp);
399 return ERR_PTR(error);
400 }
401
David Howells54ceac42006-08-22 20:06:13 -0400402 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
403
David Howells24c8dbb2006-08-22 20:06:10 -0400404 dprintk("--> nfs_get_client() = %p [share]\n", clp);
405 return clp;
406}
407
408/*
409 * Mark a server as ready or failed
410 */
David Howells54ceac42006-08-22 20:06:13 -0400411static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400412{
413 clp->cl_cons_state = state;
414 wake_up_all(&nfs_client_active_wq);
415}
David Howells5006a762006-08-22 20:06:12 -0400416
417/*
418 * Initialise the timeout values for a connection
419 */
420static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
421 unsigned int timeo, unsigned int retrans)
422{
423 to->to_initval = timeo * HZ / 10;
424 to->to_retries = retrans;
425 if (!to->to_retries)
426 to->to_retries = 2;
427
428 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400429 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400430 case XPRT_TRANSPORT_RDMA:
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500431 if (to->to_initval == 0)
David Howells5006a762006-08-22 20:06:12 -0400432 to->to_initval = 60 * HZ;
433 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
434 to->to_initval = NFS_MAX_TCP_TIMEOUT;
435 to->to_increment = to->to_initval;
436 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500437 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
438 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
439 if (to->to_maxval < to->to_initval)
440 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400441 to->to_exponential = 0;
442 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400443 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400444 default:
445 if (!to->to_initval)
446 to->to_initval = 11 * HZ / 10;
447 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
448 to->to_initval = NFS_MAX_UDP_TIMEOUT;
449 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
450 to->to_exponential = 1;
451 break;
452 }
453}
454
455/*
456 * Create an RPC client handle
457 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500458static int nfs_create_rpc_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500459 const struct rpc_timeout *timeparms,
460 rpc_authflavor_t flavor,
461 int flags)
David Howells5006a762006-08-22 20:06:12 -0400462{
David Howells5006a762006-08-22 20:06:12 -0400463 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400464 struct rpc_create_args args = {
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500465 .protocol = clp->cl_proto,
Chuck Lever41877d22006-08-22 20:06:20 -0400466 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500467 .addrsize = clp->cl_addrlen,
Trond Myklebust33170232007-12-20 16:03:59 -0500468 .timeout = timeparms,
Chuck Lever41877d22006-08-22 20:06:20 -0400469 .servername = clp->cl_hostname,
470 .program = &nfs_program,
471 .version = clp->rpc_ops->version,
472 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500473 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400474 };
David Howells5006a762006-08-22 20:06:12 -0400475
476 if (!IS_ERR(clp->cl_rpcclient))
477 return 0;
478
Chuck Lever41877d22006-08-22 20:06:20 -0400479 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400480 if (IS_ERR(clnt)) {
481 dprintk("%s: cannot create RPC client. Error = %ld\n",
482 __FUNCTION__, PTR_ERR(clnt));
483 return PTR_ERR(clnt);
484 }
485
David Howells5006a762006-08-22 20:06:12 -0400486 clp->cl_rpcclient = clnt;
487 return 0;
488}
David Howells54ceac42006-08-22 20:06:13 -0400489
490/*
491 * Version 2 or 3 client destruction
492 */
493static void nfs_destroy_server(struct nfs_server *server)
494{
David Howells54ceac42006-08-22 20:06:13 -0400495 if (!(server->flags & NFS_MOUNT_NONLM))
Chuck Lever9289e7f2008-01-11 17:09:52 -0500496 nlmclnt_done(server->nlm_host);
David Howells54ceac42006-08-22 20:06:13 -0400497}
498
499/*
500 * Version 2 or 3 lockd setup
501 */
502static int nfs_start_lockd(struct nfs_server *server)
503{
Chuck Lever9289e7f2008-01-11 17:09:52 -0500504 struct nlm_host *host;
505 struct nfs_client *clp = server->nfs_client;
Chuck Lever883bb162008-01-15 16:04:20 -0500506 struct nlmclnt_initdata nlm_init = {
507 .hostname = clp->cl_hostname,
508 .address = (struct sockaddr *)&clp->cl_addr,
509 .addrlen = clp->cl_addrlen,
510 .protocol = server->flags & NFS_MOUNT_TCP ?
511 IPPROTO_TCP : IPPROTO_UDP,
512 .nfs_version = clp->rpc_ops->version,
513 };
David Howells54ceac42006-08-22 20:06:13 -0400514
Chuck Lever883bb162008-01-15 16:04:20 -0500515 if (nlm_init.nfs_version > 3)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500516 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400517 if (server->flags & NFS_MOUNT_NONLM)
Chuck Lever9289e7f2008-01-11 17:09:52 -0500518 return 0;
519
Chuck Lever883bb162008-01-15 16:04:20 -0500520 host = nlmclnt_init(&nlm_init);
Chuck Lever9289e7f2008-01-11 17:09:52 -0500521 if (IS_ERR(host))
522 return PTR_ERR(host);
523
524 server->nlm_host = host;
525 server->destroy = nfs_destroy_server;
526 return 0;
David Howells54ceac42006-08-22 20:06:13 -0400527}
528
529/*
530 * Initialise an NFSv3 ACL client connection
531 */
532#ifdef CONFIG_NFS_V3_ACL
533static void nfs_init_server_aclclient(struct nfs_server *server)
534{
Trond Myklebust40c553192007-12-14 14:56:07 -0500535 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400536 goto out_noacl;
537 if (server->flags & NFS_MOUNT_NOACL)
538 goto out_noacl;
539
540 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
541 if (IS_ERR(server->client_acl))
542 goto out_noacl;
543
544 /* No errors! Assume that Sun nfsacls are supported */
545 server->caps |= NFS_CAP_ACLS;
546 return;
547
548out_noacl:
549 server->caps &= ~NFS_CAP_ACLS;
550}
551#else
552static inline void nfs_init_server_aclclient(struct nfs_server *server)
553{
554 server->flags &= ~NFS_MOUNT_NOACL;
555 server->caps &= ~NFS_CAP_ACLS;
556}
557#endif
558
559/*
560 * Create a general RPC client
561 */
Trond Myklebust33170232007-12-20 16:03:59 -0500562static int nfs_init_server_rpcclient(struct nfs_server *server,
563 const struct rpc_timeout *timeo,
564 rpc_authflavor_t pseudoflavour)
David Howells54ceac42006-08-22 20:06:13 -0400565{
566 struct nfs_client *clp = server->nfs_client;
567
568 server->client = rpc_clone_client(clp->cl_rpcclient);
569 if (IS_ERR(server->client)) {
570 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
571 return PTR_ERR(server->client);
572 }
573
Trond Myklebust33170232007-12-20 16:03:59 -0500574 memcpy(&server->client->cl_timeout_default,
575 timeo,
576 sizeof(server->client->cl_timeout_default));
577 server->client->cl_timeout = &server->client->cl_timeout_default;
578
David Howells54ceac42006-08-22 20:06:13 -0400579 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
580 struct rpc_auth *auth;
581
582 auth = rpcauth_create(pseudoflavour, server->client);
583 if (IS_ERR(auth)) {
584 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
585 return PTR_ERR(auth);
586 }
587 }
588 server->client->cl_softrtry = 0;
589 if (server->flags & NFS_MOUNT_SOFT)
590 server->client->cl_softrtry = 1;
591
David Howells54ceac42006-08-22 20:06:13 -0400592 return 0;
593}
594
595/*
596 * Initialise an NFS2 or NFS3 client
597 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400598static int nfs_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500599 const struct rpc_timeout *timeparms,
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400600 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400601{
David Howells54ceac42006-08-22 20:06:13 -0400602 int error;
603
604 if (clp->cl_cons_state == NFS_CS_READY) {
605 /* the client is already initialised */
606 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
607 return 0;
608 }
609
David Howells54ceac42006-08-22 20:06:13 -0400610 /*
611 * Create a client RPC handle for doing FSSTAT with UNIX auth only
612 * - RFC 2623, sec 2.3.2
613 */
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500614 error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400615 if (error < 0)
616 goto error;
617 nfs_mark_client_ready(clp, NFS_CS_READY);
618 return 0;
619
620error:
621 nfs_mark_client_ready(clp, error);
622 dprintk("<-- nfs_init_client() = xerror %d\n", error);
623 return error;
624}
625
626/*
627 * Create a version 2 or 3 client
628 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400629static int nfs_init_server(struct nfs_server *server,
630 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400631{
Trond Myklebust3a498022007-12-14 14:56:04 -0500632 struct nfs_client_initdata cl_init = {
633 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500634 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500635 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500636 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500637 .proto = data->nfs_server.protocol,
Trond Myklebust3a498022007-12-14 14:56:04 -0500638 };
Trond Myklebust33170232007-12-20 16:03:59 -0500639 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -0400640 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500641 int error;
David Howells54ceac42006-08-22 20:06:13 -0400642
643 dprintk("--> nfs_init_server()\n");
644
645#ifdef CONFIG_NFS_V3
646 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500647 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400648#endif
649
650 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500651 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400652 if (IS_ERR(clp)) {
653 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
654 return PTR_ERR(clp);
655 }
656
Trond Myklebust33170232007-12-20 16:03:59 -0500657 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
658 data->timeo, data->retrans);
659 error = nfs_init_client(clp, &timeparms, data);
David Howells54ceac42006-08-22 20:06:13 -0400660 if (error < 0)
661 goto error;
662
663 server->nfs_client = clp;
664
665 /* Initialise the client representation from the mount data */
666 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
667
668 if (data->rsize)
669 server->rsize = nfs_block_size(data->rsize, NULL);
670 if (data->wsize)
671 server->wsize = nfs_block_size(data->wsize, NULL);
672
673 server->acregmin = data->acregmin * HZ;
674 server->acregmax = data->acregmax * HZ;
675 server->acdirmin = data->acdirmin * HZ;
676 server->acdirmax = data->acdirmax * HZ;
677
678 /* Start lockd here, before we might error out */
679 error = nfs_start_lockd(server);
680 if (error < 0)
681 goto error;
682
Trond Myklebust33170232007-12-20 16:03:59 -0500683 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400684 if (error < 0)
685 goto error;
686
687 server->namelen = data->namlen;
688 /* Create a client RPC handle for the NFSv3 ACL management interface */
689 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400690 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
691 return 0;
692
693error:
694 server->nfs_client = NULL;
695 nfs_put_client(clp);
696 dprintk("<-- nfs_init_server() = xerror %d\n", error);
697 return error;
698}
699
700/*
701 * Load up the server record from information gained in an fsinfo record
702 */
703static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
704{
705 unsigned long max_rpc_payload;
706
707 /* Work out a lot of parameters */
708 if (server->rsize == 0)
709 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
710 if (server->wsize == 0)
711 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
712
713 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
714 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
715 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
716 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
717
718 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
719 if (server->rsize > max_rpc_payload)
720 server->rsize = max_rpc_payload;
721 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
722 server->rsize = NFS_MAX_FILE_IO_SIZE;
723 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700724
David Howells54ceac42006-08-22 20:06:13 -0400725 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
726
727 if (server->wsize > max_rpc_payload)
728 server->wsize = max_rpc_payload;
729 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
730 server->wsize = NFS_MAX_FILE_IO_SIZE;
731 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
732 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
733
734 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
735 if (server->dtsize > PAGE_CACHE_SIZE)
736 server->dtsize = PAGE_CACHE_SIZE;
737 if (server->dtsize > server->rsize)
738 server->dtsize = server->rsize;
739
740 if (server->flags & NFS_MOUNT_NOAC) {
741 server->acregmin = server->acregmax = 0;
742 server->acdirmin = server->acdirmax = 0;
743 }
744
745 server->maxfilesize = fsinfo->maxfilesize;
746
747 /* We're airborne Set socket buffersize */
748 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
749}
750
751/*
752 * Probe filesystem information, including the FSID on v2/v3
753 */
754static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
755{
756 struct nfs_fsinfo fsinfo;
757 struct nfs_client *clp = server->nfs_client;
758 int error;
759
760 dprintk("--> nfs_probe_fsinfo()\n");
761
762 if (clp->rpc_ops->set_capabilities != NULL) {
763 error = clp->rpc_ops->set_capabilities(server, mntfh);
764 if (error < 0)
765 goto out_error;
766 }
767
768 fsinfo.fattr = fattr;
769 nfs_fattr_init(fattr);
770 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
771 if (error < 0)
772 goto out_error;
773
774 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700775 error = bdi_init(&server->backing_dev_info);
776 if (error)
777 goto out_error;
778
David Howells54ceac42006-08-22 20:06:13 -0400779
780 /* Get some general file system info */
781 if (server->namelen == 0) {
782 struct nfs_pathconf pathinfo;
783
784 pathinfo.fattr = fattr;
785 nfs_fattr_init(fattr);
786
787 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
788 server->namelen = pathinfo.max_namelen;
789 }
790
791 dprintk("<-- nfs_probe_fsinfo() = 0\n");
792 return 0;
793
794out_error:
795 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
796 return error;
797}
798
799/*
800 * Copy useful information when duplicating a server record
801 */
802static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
803{
804 target->flags = source->flags;
805 target->acregmin = source->acregmin;
806 target->acregmax = source->acregmax;
807 target->acdirmin = source->acdirmin;
808 target->acdirmax = source->acdirmax;
809 target->caps = source->caps;
810}
811
812/*
813 * Allocate and initialise a server record
814 */
815static struct nfs_server *nfs_alloc_server(void)
816{
817 struct nfs_server *server;
818
819 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
820 if (!server)
821 return NULL;
822
823 server->client = server->client_acl = ERR_PTR(-EINVAL);
824
825 /* Zero out the NFS state stuff */
826 INIT_LIST_HEAD(&server->client_link);
827 INIT_LIST_HEAD(&server->master_link);
828
Steve Dicksonef818a22007-11-08 04:05:04 -0500829 init_waitqueue_head(&server->active_wq);
830 atomic_set(&server->active, 0);
831
David Howells54ceac42006-08-22 20:06:13 -0400832 server->io_stats = nfs_alloc_iostats();
833 if (!server->io_stats) {
834 kfree(server);
835 return NULL;
836 }
837
838 return server;
839}
840
841/*
842 * Free up a server record
843 */
844void nfs_free_server(struct nfs_server *server)
845{
846 dprintk("--> nfs_free_server()\n");
847
848 spin_lock(&nfs_client_lock);
849 list_del(&server->client_link);
850 list_del(&server->master_link);
851 spin_unlock(&nfs_client_lock);
852
853 if (server->destroy != NULL)
854 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500855
856 if (!IS_ERR(server->client_acl))
857 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400858 if (!IS_ERR(server->client))
859 rpc_shutdown_client(server->client);
860
861 nfs_put_client(server->nfs_client);
862
863 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700864 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400865 kfree(server);
866 nfs_release_automount_timer();
867 dprintk("<-- nfs_free_server()\n");
868}
869
870/*
871 * Create a version 2 or 3 volume record
872 * - keyed on server and FSID
873 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400874struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400875 struct nfs_fh *mntfh)
876{
877 struct nfs_server *server;
878 struct nfs_fattr fattr;
879 int error;
880
881 server = nfs_alloc_server();
882 if (!server)
883 return ERR_PTR(-ENOMEM);
884
885 /* Get a client representation */
886 error = nfs_init_server(server, data);
887 if (error < 0)
888 goto error;
889
890 BUG_ON(!server->nfs_client);
891 BUG_ON(!server->nfs_client->rpc_ops);
892 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
893
894 /* Probe the root fh to retrieve its FSID */
895 error = nfs_probe_fsinfo(server, mntfh, &fattr);
896 if (error < 0)
897 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400898 if (server->nfs_client->rpc_ops->version == 3) {
899 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
900 server->namelen = NFS3_MAXNAMLEN;
901 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
902 server->caps |= NFS_CAP_READDIRPLUS;
903 } else {
904 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
905 server->namelen = NFS2_MAXNAMLEN;
906 }
907
David Howells54ceac42006-08-22 20:06:13 -0400908 if (!(fattr.valid & NFS_ATTR_FATTR)) {
909 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
910 if (error < 0) {
911 dprintk("nfs_create_server: getattr error = %d\n", -error);
912 goto error;
913 }
914 }
915 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
916
David Howells6daabf12006-08-24 15:44:16 -0400917 dprintk("Server FSID: %llx:%llx\n",
918 (unsigned long long) server->fsid.major,
919 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400920
921 BUG_ON(!server->nfs_client);
922 BUG_ON(!server->nfs_client->rpc_ops);
923 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
924
925 spin_lock(&nfs_client_lock);
926 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
927 list_add_tail(&server->master_link, &nfs_volume_list);
928 spin_unlock(&nfs_client_lock);
929
930 server->mount_time = jiffies;
931 return server;
932
933error:
934 nfs_free_server(server);
935 return ERR_PTR(error);
936}
937
938#ifdef CONFIG_NFS_V4
939/*
940 * Initialise an NFS4 client record
941 */
942static int nfs4_init_client(struct nfs_client *clp,
Trond Myklebust33170232007-12-20 16:03:59 -0500943 const struct rpc_timeout *timeparms,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700944 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400945 rpc_authflavor_t authflavour)
946{
947 int error;
948
949 if (clp->cl_cons_state == NFS_CS_READY) {
950 /* the client is initialised already */
951 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
952 return 0;
953 }
954
955 /* Check NFS protocol revision and initialize RPC op vector */
956 clp->rpc_ops = &nfs_v4_clientops;
957
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500958 error = nfs_create_rpc_client(clp, timeparms, authflavour,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500959 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400960 if (error < 0)
961 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700962 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400963
964 error = nfs_idmap_new(clp);
965 if (error < 0) {
966 dprintk("%s: failed to create idmapper. Error = %d\n",
967 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400968 goto error;
969 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400970 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400971
972 nfs_mark_client_ready(clp, NFS_CS_READY);
973 return 0;
974
975error:
976 nfs_mark_client_ready(clp, error);
977 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
978 return error;
979}
980
981/*
982 * Set up an NFS4 client
983 */
984static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500985 const char *hostname,
986 const struct sockaddr *addr,
987 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700988 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400989 rpc_authflavor_t authflavour,
Trond Myklebust33170232007-12-20 16:03:59 -0500990 int proto, const struct rpc_timeout *timeparms)
David Howells54ceac42006-08-22 20:06:13 -0400991{
Trond Myklebust3a498022007-12-14 14:56:04 -0500992 struct nfs_client_initdata cl_init = {
993 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500994 .addr = addr,
995 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500996 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust59dca3b2008-01-03 16:29:06 -0500997 .proto = proto,
Trond Myklebust3a498022007-12-14 14:56:04 -0500998 };
David Howells54ceac42006-08-22 20:06:13 -0400999 struct nfs_client *clp;
1000 int error;
1001
1002 dprintk("--> nfs4_set_client()\n");
1003
1004 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -05001005 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -04001006 if (IS_ERR(clp)) {
1007 error = PTR_ERR(clp);
1008 goto error;
1009 }
Trond Myklebust59dca3b2008-01-03 16:29:06 -05001010 error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -04001011 if (error < 0)
1012 goto error_put;
1013
1014 server->nfs_client = clp;
1015 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1016 return 0;
1017
1018error_put:
1019 nfs_put_client(clp);
1020error:
1021 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1022 return error;
1023}
1024
1025/*
1026 * Create a version 4 volume record
1027 */
1028static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001029 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -04001030{
Trond Myklebust33170232007-12-20 16:03:59 -05001031 struct rpc_timeout timeparms;
David Howells54ceac42006-08-22 20:06:13 -04001032 int error;
1033
1034 dprintk("--> nfs4_init_server()\n");
1035
Trond Myklebust33170232007-12-20 16:03:59 -05001036 nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1037 data->timeo, data->retrans);
1038
1039 /* Get a client record */
1040 error = nfs4_set_client(server,
1041 data->nfs_server.hostname,
1042 (const struct sockaddr *)&data->nfs_server.address,
1043 data->nfs_server.addrlen,
1044 data->client_address,
1045 data->auth_flavors[0],
1046 data->nfs_server.protocol,
1047 &timeparms);
1048 if (error < 0)
1049 goto error;
1050
David Howells54ceac42006-08-22 20:06:13 -04001051 /* Initialise the client representation from the mount data */
1052 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
1053 server->caps |= NFS_CAP_ATOMIC_OPEN;
1054
1055 if (data->rsize)
1056 server->rsize = nfs_block_size(data->rsize, NULL);
1057 if (data->wsize)
1058 server->wsize = nfs_block_size(data->wsize, NULL);
1059
1060 server->acregmin = data->acregmin * HZ;
1061 server->acregmax = data->acregmax * HZ;
1062 server->acdirmin = data->acdirmin * HZ;
1063 server->acdirmax = data->acdirmax * HZ;
1064
Trond Myklebust33170232007-12-20 16:03:59 -05001065 error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -04001066
Trond Myklebust33170232007-12-20 16:03:59 -05001067error:
David Howells54ceac42006-08-22 20:06:13 -04001068 /* Done */
1069 dprintk("<-- nfs4_init_server() = %d\n", error);
1070 return error;
1071}
1072
1073/*
1074 * Create a version 4 volume record
1075 * - keyed on server and FSID
1076 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001077struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001078 struct nfs_fh *mntfh)
1079{
1080 struct nfs_fattr fattr;
1081 struct nfs_server *server;
1082 int error;
1083
1084 dprintk("--> nfs4_create_server()\n");
1085
1086 server = nfs_alloc_server();
1087 if (!server)
1088 return ERR_PTR(-ENOMEM);
1089
David Howells54ceac42006-08-22 20:06:13 -04001090 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001091 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001092 if (error < 0)
1093 goto error;
1094
1095 BUG_ON(!server->nfs_client);
1096 BUG_ON(!server->nfs_client->rpc_ops);
1097 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1098
1099 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001100 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001101 if (error < 0)
1102 goto error;
1103
David Howells6daabf12006-08-24 15:44:16 -04001104 dprintk("Server FSID: %llx:%llx\n",
1105 (unsigned long long) server->fsid.major,
1106 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001107 dprintk("Mount FH: %d\n", mntfh->size);
1108
1109 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1110 if (error < 0)
1111 goto error;
1112
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001113 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1114 server->namelen = NFS4_MAXNAMLEN;
1115
David Howells54ceac42006-08-22 20:06:13 -04001116 BUG_ON(!server->nfs_client);
1117 BUG_ON(!server->nfs_client->rpc_ops);
1118 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1119
1120 spin_lock(&nfs_client_lock);
1121 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1122 list_add_tail(&server->master_link, &nfs_volume_list);
1123 spin_unlock(&nfs_client_lock);
1124
1125 server->mount_time = jiffies;
1126 dprintk("<-- nfs4_create_server() = %p\n", server);
1127 return server;
1128
1129error:
1130 nfs_free_server(server);
1131 dprintk("<-- nfs4_create_server() = error %d\n", error);
1132 return ERR_PTR(error);
1133}
1134
1135/*
1136 * Create an NFS4 referral server record
1137 */
1138struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001139 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001140{
1141 struct nfs_client *parent_client;
1142 struct nfs_server *server, *parent_server;
1143 struct nfs_fattr fattr;
1144 int error;
1145
1146 dprintk("--> nfs4_create_referral_server()\n");
1147
1148 server = nfs_alloc_server();
1149 if (!server)
1150 return ERR_PTR(-ENOMEM);
1151
1152 parent_server = NFS_SB(data->sb);
1153 parent_client = parent_server->nfs_client;
1154
1155 /* Get a client representation.
1156 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001157 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001158 data->addr,
1159 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001160 parent_client->cl_ipaddr,
1161 data->authflavor,
1162 parent_server->client->cl_xprt->prot,
Trond Myklebust33170232007-12-20 16:03:59 -05001163 parent_server->client->cl_timeout);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001164 if (error < 0)
1165 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001166
1167 /* Initialise the client representation from the parent server */
1168 nfs_server_copy_userdata(server, parent_server);
1169 server->caps |= NFS_CAP_ATOMIC_OPEN;
1170
Trond Myklebust33170232007-12-20 16:03:59 -05001171 error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
David Howells54ceac42006-08-22 20:06:13 -04001172 if (error < 0)
1173 goto error;
1174
1175 BUG_ON(!server->nfs_client);
1176 BUG_ON(!server->nfs_client->rpc_ops);
1177 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1178
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001179 /* Probe the root fh to retrieve its FSID and filehandle */
1180 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1181 if (error < 0)
1182 goto error;
1183
David Howells54ceac42006-08-22 20:06:13 -04001184 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001185 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001186 if (error < 0)
1187 goto error;
1188
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001189 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1190 server->namelen = NFS4_MAXNAMLEN;
1191
David Howells54ceac42006-08-22 20:06:13 -04001192 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001193 (unsigned long long) server->fsid.major,
1194 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001195
1196 spin_lock(&nfs_client_lock);
1197 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1198 list_add_tail(&server->master_link, &nfs_volume_list);
1199 spin_unlock(&nfs_client_lock);
1200
1201 server->mount_time = jiffies;
1202
1203 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1204 return server;
1205
1206error:
1207 nfs_free_server(server);
1208 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1209 return ERR_PTR(error);
1210}
1211
1212#endif /* CONFIG_NFS_V4 */
1213
1214/*
1215 * Clone an NFS2, NFS3 or NFS4 server record
1216 */
1217struct nfs_server *nfs_clone_server(struct nfs_server *source,
1218 struct nfs_fh *fh,
1219 struct nfs_fattr *fattr)
1220{
1221 struct nfs_server *server;
1222 struct nfs_fattr fattr_fsinfo;
1223 int error;
1224
1225 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001226 (unsigned long long) fattr->fsid.major,
1227 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001228
1229 server = nfs_alloc_server();
1230 if (!server)
1231 return ERR_PTR(-ENOMEM);
1232
1233 /* Copy data from the source */
1234 server->nfs_client = source->nfs_client;
1235 atomic_inc(&server->nfs_client->cl_count);
1236 nfs_server_copy_userdata(server, source);
1237
1238 server->fsid = fattr->fsid;
1239
Trond Myklebust33170232007-12-20 16:03:59 -05001240 error = nfs_init_server_rpcclient(server,
1241 source->client->cl_timeout,
1242 source->client->cl_auth->au_flavor);
David Howells54ceac42006-08-22 20:06:13 -04001243 if (error < 0)
1244 goto out_free_server;
1245 if (!IS_ERR(source->client_acl))
1246 nfs_init_server_aclclient(server);
1247
1248 /* probe the filesystem info for this server filesystem */
1249 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1250 if (error < 0)
1251 goto out_free_server;
1252
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001253 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1254 server->namelen = NFS4_MAXNAMLEN;
1255
David Howells54ceac42006-08-22 20:06:13 -04001256 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001257 (unsigned long long) server->fsid.major,
1258 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001259
1260 error = nfs_start_lockd(server);
1261 if (error < 0)
1262 goto out_free_server;
1263
1264 spin_lock(&nfs_client_lock);
1265 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1266 list_add_tail(&server->master_link, &nfs_volume_list);
1267 spin_unlock(&nfs_client_lock);
1268
1269 server->mount_time = jiffies;
1270
1271 dprintk("<-- nfs_clone_server() = %p\n", server);
1272 return server;
1273
1274out_free_server:
1275 nfs_free_server(server);
1276 dprintk("<-- nfs_clone_server() = error %d\n", error);
1277 return ERR_PTR(error);
1278}
David Howells6aaca562006-08-22 20:06:13 -04001279
1280#ifdef CONFIG_PROC_FS
1281static struct proc_dir_entry *proc_fs_nfs;
1282
1283static int nfs_server_list_open(struct inode *inode, struct file *file);
1284static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1285static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1286static void nfs_server_list_stop(struct seq_file *p, void *v);
1287static int nfs_server_list_show(struct seq_file *m, void *v);
1288
1289static struct seq_operations nfs_server_list_ops = {
1290 .start = nfs_server_list_start,
1291 .next = nfs_server_list_next,
1292 .stop = nfs_server_list_stop,
1293 .show = nfs_server_list_show,
1294};
1295
Arjan van de Ven00977a52007-02-12 00:55:34 -08001296static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001297 .open = nfs_server_list_open,
1298 .read = seq_read,
1299 .llseek = seq_lseek,
1300 .release = seq_release,
1301};
1302
1303static int nfs_volume_list_open(struct inode *inode, struct file *file);
1304static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1305static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1306static void nfs_volume_list_stop(struct seq_file *p, void *v);
1307static int nfs_volume_list_show(struct seq_file *m, void *v);
1308
1309static struct seq_operations nfs_volume_list_ops = {
1310 .start = nfs_volume_list_start,
1311 .next = nfs_volume_list_next,
1312 .stop = nfs_volume_list_stop,
1313 .show = nfs_volume_list_show,
1314};
1315
Arjan van de Ven00977a52007-02-12 00:55:34 -08001316static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001317 .open = nfs_volume_list_open,
1318 .read = seq_read,
1319 .llseek = seq_lseek,
1320 .release = seq_release,
1321};
1322
1323/*
1324 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1325 * we're dealing
1326 */
1327static int nfs_server_list_open(struct inode *inode, struct file *file)
1328{
1329 struct seq_file *m;
1330 int ret;
1331
1332 ret = seq_open(file, &nfs_server_list_ops);
1333 if (ret < 0)
1334 return ret;
1335
1336 m = file->private_data;
1337 m->private = PDE(inode)->data;
1338
1339 return 0;
1340}
1341
1342/*
1343 * set up the iterator to start reading from the server list and return the first item
1344 */
1345static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1346{
David Howells6aaca562006-08-22 20:06:13 -04001347 /* lock the list against modification */
1348 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001349 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001350}
1351
1352/*
1353 * move to next server
1354 */
1355static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1356{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001357 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001358}
1359
1360/*
1361 * clean up after reading from the transports list
1362 */
1363static void nfs_server_list_stop(struct seq_file *p, void *v)
1364{
1365 spin_unlock(&nfs_client_lock);
1366}
1367
1368/*
1369 * display a header line followed by a load of call lines
1370 */
1371static int nfs_server_list_show(struct seq_file *m, void *v)
1372{
1373 struct nfs_client *clp;
1374
1375 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001376 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001377 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1378 return 0;
1379 }
1380
1381 /* display one transport per line on subsequent lines */
1382 clp = list_entry(v, struct nfs_client, cl_share_link);
1383
Chuck Lever5d8515c2007-12-10 14:57:16 -05001384 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001385 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001386 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1387 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001388 atomic_read(&clp->cl_count),
1389 clp->cl_hostname);
1390
1391 return 0;
1392}
1393
1394/*
1395 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1396 */
1397static int nfs_volume_list_open(struct inode *inode, struct file *file)
1398{
1399 struct seq_file *m;
1400 int ret;
1401
1402 ret = seq_open(file, &nfs_volume_list_ops);
1403 if (ret < 0)
1404 return ret;
1405
1406 m = file->private_data;
1407 m->private = PDE(inode)->data;
1408
1409 return 0;
1410}
1411
1412/*
1413 * set up the iterator to start reading from the volume list and return the first item
1414 */
1415static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1416{
David Howells6aaca562006-08-22 20:06:13 -04001417 /* lock the list against modification */
1418 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001419 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001420}
1421
1422/*
1423 * move to next volume
1424 */
1425static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1426{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001427 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001428}
1429
1430/*
1431 * clean up after reading from the transports list
1432 */
1433static void nfs_volume_list_stop(struct seq_file *p, void *v)
1434{
1435 spin_unlock(&nfs_client_lock);
1436}
1437
1438/*
1439 * display a header line followed by a load of call lines
1440 */
1441static int nfs_volume_list_show(struct seq_file *m, void *v)
1442{
1443 struct nfs_server *server;
1444 struct nfs_client *clp;
1445 char dev[8], fsid[17];
1446
1447 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001448 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001449 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1450 return 0;
1451 }
1452 /* display one transport per line on subsequent lines */
1453 server = list_entry(v, struct nfs_server, master_link);
1454 clp = server->nfs_client;
1455
1456 snprintf(dev, 8, "%u:%u",
1457 MAJOR(server->s_dev), MINOR(server->s_dev));
1458
1459 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001460 (unsigned long long) server->fsid.major,
1461 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001462
Chuck Lever5d8515c2007-12-10 14:57:16 -05001463 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001464 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001465 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1466 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001467 dev,
1468 fsid);
1469
1470 return 0;
1471}
1472
1473/*
1474 * initialise the /proc/fs/nfsfs/ directory
1475 */
1476int __init nfs_fs_proc_init(void)
1477{
1478 struct proc_dir_entry *p;
1479
1480 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1481 if (!proc_fs_nfs)
1482 goto error_0;
1483
1484 proc_fs_nfs->owner = THIS_MODULE;
1485
1486 /* a file of servers with which we're dealing */
1487 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1488 if (!p)
1489 goto error_1;
1490
1491 p->proc_fops = &nfs_server_list_fops;
1492 p->owner = THIS_MODULE;
1493
1494 /* a file of volumes that we have mounted */
1495 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1496 if (!p)
1497 goto error_2;
1498
1499 p->proc_fops = &nfs_volume_list_fops;
1500 p->owner = THIS_MODULE;
1501 return 0;
1502
1503error_2:
1504 remove_proc_entry("servers", proc_fs_nfs);
1505error_1:
1506 remove_proc_entry("nfsfs", proc_root_fs);
1507error_0:
1508 return -ENOMEM;
1509}
1510
1511/*
1512 * clean up the /proc/fs/nfsfs/ directory
1513 */
1514void nfs_fs_proc_exit(void)
1515{
1516 remove_proc_entry("volumes", proc_fs_nfs);
1517 remove_proc_entry("servers", proc_fs_nfs);
1518 remove_proc_entry("nfsfs", proc_root_fs);
1519}
1520
1521#endif /* CONFIG_PROC_FS */